Back to index

openldap  2.4.31
LDAPSearchRequest.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 #include "config.h"
00008 #include "ac/time.h"
00009 #include "debug.h"
00010 #include "LDAPSearchRequest.h"
00011 #include "LDAPException.h"
00012 #include "LDAPSearchReference.h"
00013 #include "LDAPResult.h"
00014 #include "LDAPRequest.h"
00015 #include "LDAPUrl.h"
00016 
00017 using namespace std;
00018 
00019 LDAPSearchRequest::LDAPSearchRequest(const LDAPSearchRequest& req ) :
00020         LDAPRequest (req){
00021     DEBUG(LDAP_DEBUG_CONSTRUCT, 
00022         "LDAPSearchRequest::LDAPSearchRequest(&)" << endl);
00023     m_base=req.m_base;
00024     m_scope=req.m_scope;
00025     m_filter=req.m_filter;
00026     m_attrs=req.m_attrs;
00027     m_attrsOnly=req.m_attrsOnly;
00028 }
00029         
00030 
00031 LDAPSearchRequest::LDAPSearchRequest(const string& base, int scope, 
00032         const string& filter, const StringList& attrs, bool attrsOnly,
00033         LDAPAsynConnection *connect,
00034         const LDAPConstraints* cons, bool isReferral, 
00035         const LDAPRequest* parent) 
00036             : LDAPRequest (connect,cons,isReferral,parent) {
00037     
00038     DEBUG(LDAP_DEBUG_CONSTRUCT,
00039             "LDAPSearchRequest:LDAPSearchRequest()" << endl);
00040     DEBUG(LDAP_DEBUG_CONSTRUCT & LDAP_DEBUG_PARAMETER,
00041             "   base:" << base << endl << "   scope:" << scope << endl
00042             << "   filter:" << filter << endl);
00043     m_requestType=LDAPRequest::SEARCH;
00044     //insert some validating and copying here  
00045     m_base=base;
00046     m_scope=scope;
00047     if(filter == ""){
00048         m_filter="objectClass=*";  
00049     }else{
00050         m_filter=filter;
00051     }
00052     m_attrs=attrs;
00053     m_attrsOnly=attrsOnly;
00054 }
00055 
00056 LDAPSearchRequest::~LDAPSearchRequest(){
00057     DEBUG(LDAP_DEBUG_DESTROY, "LDAPSearchRequest::~LDAPSearchRequest" << endl);
00058 }
00059 
00060 LDAPMessageQueue* LDAPSearchRequest::sendRequest(){
00061     int msgID; 
00062     DEBUG(LDAP_DEBUG_TRACE, "LDAPSearchRequest::sendRequest()" << endl);
00063     timeval* tmptime=m_cons->getTimeoutStruct();
00064     char** tmpattrs=m_attrs.toCharArray();
00065     LDAPControl** tmpSrvCtrl=m_cons->getSrvCtrlsArray();
00066     LDAPControl** tmpClCtrl=m_cons->getClCtrlsArray();
00067     int aliasDeref = m_cons->getAliasDeref();
00068     ldap_set_option(m_connection->getSessionHandle(), LDAP_OPT_DEREF, 
00069             &aliasDeref);
00070     int err=ldap_search_ext(m_connection->getSessionHandle(), m_base.c_str(),
00071             m_scope, m_filter.c_str(), tmpattrs, m_attrsOnly, tmpSrvCtrl,
00072             tmpClCtrl, tmptime, m_cons->getSizeLimit(), &msgID );
00073     delete tmptime;
00074     ber_memvfree((void**)tmpattrs);
00075     LDAPControlSet::freeLDAPControlArray(tmpSrvCtrl);
00076     LDAPControlSet::freeLDAPControlArray(tmpClCtrl);
00077 
00078     if (err != LDAP_SUCCESS){  
00079         throw LDAPException(err);
00080     } else if (isReferral()){
00081         m_msgID=msgID;
00082         return 0;
00083     }else{
00084         m_msgID=msgID;
00085         return  new LDAPMessageQueue(this);
00086     }
00087 }
00088 
00089 LDAPRequest* LDAPSearchRequest::followReferral(LDAPMsg* ref){
00090     DEBUG(LDAP_DEBUG_TRACE, "LDAPSearchRequest::followReferral()" << endl);
00091     LDAPUrlList urls;
00092     LDAPUrlList::const_iterator usedUrl;
00093     LDAPAsynConnection* con;
00094     string filter;
00095     int scope;
00096     if(ref->getMessageType() == LDAPMsg::SEARCH_REFERENCE){
00097         urls = ((LDAPSearchReference *)ref)->getUrls();
00098     }else{
00099         urls = ((LDAPResult *)ref)->getReferralUrls();
00100     }
00101     con = getConnection()->referralConnect(urls,usedUrl,m_cons);
00102     if(con != 0){
00103         if((usedUrl->getFilter() != "") && 
00104             (usedUrl->getFilter() != m_filter)){
00105                 filter=usedUrl->getFilter();
00106         }else{
00107             filter=m_filter;
00108         }
00109         if( (ref->getMessageType() == LDAPMsg::SEARCH_REFERENCE) && 
00110             (m_scope == LDAPAsynConnection::SEARCH_ONE)
00111           ){
00112             scope = LDAPAsynConnection::SEARCH_BASE;
00113             DEBUG(LDAP_DEBUG_TRACE,"   adjusted scope to BASE" << endl);
00114         }else{
00115             scope = m_scope;
00116         }
00117     }else{
00118         return 0;
00119     }
00120     return new LDAPSearchRequest(usedUrl->getDN(), scope, filter,
00121             m_attrs, m_attrsOnly, con, m_cons,true,this);
00122 }
00123 
00124 bool LDAPSearchRequest::equals(const LDAPRequest* req)const{
00125     DEBUG(LDAP_DEBUG_TRACE,"LDAPSearchRequest::equals()" << endl);
00126     if( LDAPRequest::equals(req)){
00127         LDAPSearchRequest* sreq = (LDAPSearchRequest*)req;
00128         if ( (m_base == sreq->m_base) &&
00129              (m_scope == sreq->m_scope) 
00130            ){
00131             return true;
00132         }
00133     }
00134     return false;
00135 }