2 * Copyright 2000-2006, OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9 #include "LDAPAsynConnection.h"
11 #include "LDAPAddRequest.h"
12 #include "LDAPBindRequest.h"
13 #include "LDAPCompareRequest.h"
14 #include "LDAPDeleteRequest.h"
15 #include "LDAPException.h"
16 #include "LDAPExtRequest.h"
17 #include "LDAPEntry.h"
18 #include "LDAPModDNRequest.h"
19 #include "LDAPModifyRequest.h"
20 #include "LDAPRequest.h"
21 #include "LDAPRebind.h"
22 #include "LDAPRebindAuth.h"
23 #include "LDAPSearchRequest.h"
28 LDAPAsynConnection::LDAPAsynConnection(const string& hostname, int port,
29 LDAPConstraints *cons ){
30 DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPAsynConnection::LDAPAsynConnection()"
32 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
33 " host:" << hostname << endl << " port:" << port << endl);
36 this->init(hostname, port);
37 this->setConstraints(cons);
40 LDAPAsynConnection::~LDAPAsynConnection(){
41 DEBUG(LDAP_DEBUG_DESTROY,
42 "LDAPAsynConnection::~LDAPAsynConnection()" << endl);
47 void LDAPAsynConnection::init(const string& hostname, int port){
48 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::init" << endl);
49 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
50 " hostname:" << hostname << endl
51 << " port:" << port << endl);
55 memset( &url, 0, sizeof(url));
57 url.lud_scheme = "ldap";
58 url.lud_host = strdup(hostname.c_str());
60 url.lud_scope = LDAP_SCOPE_DEFAULT;
62 ldapuri = ldap_url_desc2str( &url );
63 int ret = ldap_initialize(&cur_session, ldapuri);
64 if ( ret != LDAP_SUCCESS ) {
65 throw LDAPException( ret );
70 ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
71 ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
74 void LDAPAsynConnection::start_tls(){
76 if( ldap_start_tls_s( cur_session, NULL, NULL ) != LDAP_SUCCESS ) {
77 throw LDAPException(this);
81 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
82 const string& passwd, const LDAPConstraints *cons){
83 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" << endl);
84 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " dn:" << dn << endl
85 << " passwd:" << passwd << endl);
86 LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
88 LDAPMessageQueue *ret = req->sendRequest();
90 }catch(LDAPException e){
96 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope,
98 const StringList& attrs,
100 const LDAPConstraints *cons){
101 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" << endl);
102 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " base:" << base << endl
103 << " scope:" << scope << endl
104 << " filter:" << filter << endl );
105 LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs,
106 attrsOnly, this, cons);
108 LDAPMessageQueue *ret = req->sendRequest();
110 }catch(LDAPException e){
116 LDAPMessageQueue* LDAPAsynConnection::del(const string& dn,
117 const LDAPConstraints *cons){
118 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
119 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
120 LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
122 LDAPMessageQueue *ret = req->sendRequest();
124 }catch(LDAPException e){
130 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn,
131 const LDAPAttribute& attr, const LDAPConstraints *cons){
132 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
133 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
134 << " attr:" << attr << endl);
135 LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, this, cons);
137 LDAPMessageQueue *ret = req->sendRequest();
139 }catch(LDAPException e){
145 LDAPMessageQueue* LDAPAsynConnection::add( const LDAPEntry* le,
146 const LDAPConstraints *cons){
147 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
148 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," entry:" << *le << endl);
149 LDAPAddRequest *req = new LDAPAddRequest(le, this, cons);
151 LDAPMessageQueue *ret = req->sendRequest();
153 }catch(LDAPException e){
159 LDAPMessageQueue* LDAPAsynConnection::modify(const string& dn,
160 const LDAPModList *mod, const LDAPConstraints *cons){
161 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
162 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
163 LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
165 LDAPMessageQueue *ret = req->sendRequest();
167 }catch(LDAPException e){
173 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn,
174 const string& newRDN, bool delOldRDN, const string& newParentDN,
175 const LDAPConstraints *cons ){
176 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
177 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
178 << " newRDN:" << newRDN << endl
179 << " newParentDN:" << newParentDN << endl
180 << " delOldRDN:" << delOldRDN << endl);
181 LDAPModDNRequest *req = new LDAPModDNRequest(dn, newRDN, delOldRDN,
182 newParentDN, this, cons );
184 LDAPMessageQueue *ret = req->sendRequest();
186 }catch(LDAPException e){
193 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid,
194 const string& value, const LDAPConstraints *cons ){
195 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
196 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," oid:" << oid << endl);
197 LDAPExtRequest *req = new LDAPExtRequest(oid, value, this,cons);
199 LDAPMessageQueue *ret = req->sendRequest();
201 }catch(LDAPException e){
208 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
209 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
210 LDAPRequestStack *reqStack=q->getRequestStack();
212 while(! reqStack->empty()){
214 if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0)
216 throw LDAPException(this);
223 void LDAPAsynConnection::unbind(){
224 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
226 LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
227 LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
228 int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
230 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
231 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
232 if(err != LDAP_SUCCESS){
233 throw LDAPException(err);
238 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
239 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
243 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
244 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
248 LDAP* LDAPAsynConnection::getSessionHandle() const{
249 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
253 const string& LDAPAsynConnection::getHost() const{
254 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
258 int LDAPAsynConnection::getPort() const{
259 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
263 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
264 const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
265 const LDAPConstraints* cons) const {
266 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
267 LDAPUrlList::const_iterator conUrl;
268 LDAPAsynConnection* tmpConn=0;
269 const LDAPRebind* rebind = cons->getReferralRebind();
270 LDAPRebindAuth* auth = 0;
272 for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
273 string host= conUrl->getHost();
274 int port= conUrl->getPort();
275 DEBUG(LDAP_DEBUG_TRACE," connecting to: " << host << ":" <<
277 //Set the new connection's constraints-object ?
278 tmpConn=new LDAPAsynConnection(host.c_str(),port);
282 auth=rebind->getRebindAuth(host, port);
285 string dn = auth->getDN();
286 string passwd = auth->getPassword();
288 struct berval c_passwd = { 0, 0 };
293 c_passwd.bv_val = const_cast<char*>(passwd.c_str());
294 c_passwd.bv_len = passwd.size();
296 err = ldap_sasl_bind_s(tmpConn->getSessionHandle(), c_dn,
297 LDAP_SASL_SIMPLE, &c_passwd, NULL, NULL, NULL);
300 err = ldap_sasl_bind_s(tmpConn->getSessionHandle(),NULL,
301 LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL);
303 if( err == LDAP_SUCCESS ){