| 1 |
// $OpenLDAP$ |
| 2 |
/* |
| 3 |
* Copyright 2000-2024 The OpenLDAP Foundation, All Rights Reserved. |
| 4 |
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "config.h" |
| 8 |
#include "ac/time.h" |
| 9 |
#include "debug.h" |
| 10 |
#include "LDAPSearchRequest.h" |
| 11 |
#include "LDAPException.h" |
| 12 |
#include "LDAPSearchReference.h" |
| 13 |
#include "LDAPResult.h" |
| 14 |
#include "LDAPRequest.h" |
| 15 |
#include "LDAPUrl.h" |
| 16 |
|
| 17 |
using namespace std; |
| 18 |
|
| 19 |
LDAPSearchRequest::LDAPSearchRequest(const LDAPSearchRequest& req ) : |
| 20 |
LDAPRequest (req){ |
| 21 |
DEBUG(LDAP_DEBUG_CONSTRUCT, |
| 22 |
"LDAPSearchRequest::LDAPSearchRequest(&)" << endl); |
| 23 |
m_base=req.m_base; |
| 24 |
m_scope=req.m_scope; |
| 25 |
m_filter=req.m_filter; |
| 26 |
m_attrs=req.m_attrs; |
| 27 |
m_attrsOnly=req.m_attrsOnly; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
LDAPSearchRequest::LDAPSearchRequest(const string& base, int scope, |
| 32 |
const string& filter, const StringList& attrs, bool attrsOnly, |
| 33 |
LDAPAsynConnection *connect, |
| 34 |
const LDAPConstraints* cons, bool isReferral, |
| 35 |
const LDAPRequest* parent) |
| 36 |
: LDAPRequest (connect,cons,isReferral,parent) { |
| 37 |
|
| 38 |
DEBUG(LDAP_DEBUG_CONSTRUCT, |
| 39 |
"LDAPSearchRequest:LDAPSearchRequest()" << endl); |
| 40 |
DEBUG(LDAP_DEBUG_CONSTRUCT & LDAP_DEBUG_PARAMETER, |
| 41 |
" base:" << base << endl << " scope:" << scope << endl |
| 42 |
<< " filter:" << filter << endl); |
| 43 |
m_requestType=LDAPRequest::SEARCH; |
| 44 |
//insert some validating and copying here |
| 45 |
m_base=base; |
| 46 |
m_scope=scope; |
| 47 |
if(filter == ""){ |
| 48 |
m_filter="objectClass=*"; |
| 49 |
}else{ |
| 50 |
m_filter=filter; |
| 51 |
} |
| 52 |
m_attrs=attrs; |
| 53 |
m_attrsOnly=attrsOnly; |
| 54 |
} |
| 55 |
|
| 56 |
LDAPSearchRequest::~LDAPSearchRequest(){ |
| 57 |
DEBUG(LDAP_DEBUG_DESTROY, "LDAPSearchRequest::~LDAPSearchRequest" << endl); |
| 58 |
} |
| 59 |
|
| 60 |
LDAPMessageQueue* LDAPSearchRequest::sendRequest(){ |
| 61 |
int msgID; |
| 62 |
DEBUG(LDAP_DEBUG_TRACE, "LDAPSearchRequest::sendRequest()" << endl); |
| 63 |
timeval* tmptime=m_cons->getTimeoutStruct(); |
| 64 |
char** tmpattrs=m_attrs.toCharArray(); |
| 65 |
LDAPControl** tmpSrvCtrl=m_cons->getSrvCtrlsArray(); |
| 66 |
LDAPControl** tmpClCtrl=m_cons->getClCtrlsArray(); |
| 67 |
int aliasDeref = m_cons->getAliasDeref(); |
| 68 |
ldap_set_option(m_connection->getSessionHandle(), LDAP_OPT_DEREF, |
| 69 |
&aliasDeref); |
| 70 |
int err=ldap_search_ext(m_connection->getSessionHandle(), m_base.c_str(), |
| 71 |
m_scope, m_filter.c_str(), tmpattrs, m_attrsOnly, tmpSrvCtrl, |
| 72 |
tmpClCtrl, tmptime, m_cons->getSizeLimit(), &msgID ); |
| 73 |
delete tmptime; |
| 74 |
ber_memvfree((void**)tmpattrs); |
| 75 |
LDAPControlSet::freeLDAPControlArray(tmpSrvCtrl); |
| 76 |
LDAPControlSet::freeLDAPControlArray(tmpClCtrl); |
| 77 |
|
| 78 |
if (err != LDAP_SUCCESS){ |
| 79 |
throw LDAPException(err); |
| 80 |
} else if (isReferral()){ |
| 81 |
m_msgID=msgID; |
| 82 |
return 0; |
| 83 |
}else{ |
| 84 |
m_msgID=msgID; |
| 85 |
return new LDAPMessageQueue(this); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
LDAPRequest* LDAPSearchRequest::followReferral(LDAPMsg* ref){ |
| 90 |
DEBUG(LDAP_DEBUG_TRACE, "LDAPSearchRequest::followReferral()" << endl); |
| 91 |
LDAPUrlList urls; |
| 92 |
LDAPUrlList::const_iterator usedUrl; |
| 93 |
LDAPAsynConnection* con; |
| 94 |
string filter; |
| 95 |
int scope; |
| 96 |
if(ref->getMessageType() == LDAPMsg::SEARCH_REFERENCE){ |
| 97 |
urls = ((LDAPSearchReference *)ref)->getUrls(); |
| 98 |
}else{ |
| 99 |
urls = ((LDAPResult *)ref)->getReferralUrls(); |
| 100 |
} |
| 101 |
con = getConnection()->referralConnect(urls,usedUrl,m_cons); |
| 102 |
if(con != 0){ |
| 103 |
if((usedUrl->getFilter() != "") && |
| 104 |
(usedUrl->getFilter() != m_filter)){ |
| 105 |
filter=usedUrl->getFilter(); |
| 106 |
}else{ |
| 107 |
filter=m_filter; |
| 108 |
} |
| 109 |
if( (ref->getMessageType() == LDAPMsg::SEARCH_REFERENCE) && |
| 110 |
(m_scope == LDAPAsynConnection::SEARCH_ONE) |
| 111 |
){ |
| 112 |
scope = LDAPAsynConnection::SEARCH_BASE; |
| 113 |
DEBUG(LDAP_DEBUG_TRACE," adjusted scope to BASE" << endl); |
| 114 |
}else{ |
| 115 |
scope = m_scope; |
| 116 |
} |
| 117 |
}else{ |
| 118 |
return 0; |
| 119 |
} |
| 120 |
return new LDAPSearchRequest(usedUrl->getDN(), scope, filter, |
| 121 |
m_attrs, m_attrsOnly, con, m_cons,true,this); |
| 122 |
} |
| 123 |
|
| 124 |
bool LDAPSearchRequest::equals(const LDAPRequest* req)const{ |
| 125 |
DEBUG(LDAP_DEBUG_TRACE,"LDAPSearchRequest::equals()" << endl); |
| 126 |
if( LDAPRequest::equals(req)){ |
| 127 |
LDAPSearchRequest* sreq = (LDAPSearchRequest*)req; |
| 128 |
if ( (m_base == sreq->m_base) && |
| 129 |
(m_scope == sreq->m_scope) |
| 130 |
){ |
| 131 |
return true; |
| 132 |
} |
| 133 |
} |
| 134 |
return false; |
| 135 |
} |