2 * Copyright 2000, 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"
27 LDAPAsynConnection::LDAPAsynConnection(const string& hostname, int port,
28 LDAPConstraints *cons ){
29 DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPAsynConnection::LDAPAsynConnection()"
31 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
32 " host:" << hostname << endl << " port:" << port << endl);
35 this->init(hostname, port);
36 this->setConstraints(cons);
39 LDAPAsynConnection::~LDAPAsynConnection(){
40 DEBUG(LDAP_DEBUG_DESTROY,
41 "LDAPAsynConnection::~LDAPAsynConnection()" << endl);
46 void LDAPAsynConnection::init(const string& hostname, int port){
47 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::init" << endl);
48 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
49 " hostname:" << hostname << endl
50 << " port:" << port << endl);
51 cur_session=ldap_init(hostname.c_str(),port);
55 ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
56 ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
59 void LDAPAsynConnection::start_tls(){
61 if( ldap_start_tls_s( cur_session, NULL, NULL ) != LDAP_SUCCESS ) {
62 throw LDAPException(this);
66 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
67 const string& passwd, const LDAPConstraints *cons){
68 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" << endl);
69 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " dn:" << dn << endl
70 << " passwd:" << passwd << endl);
71 LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
73 LDAPMessageQueue *ret = req->sendRequest();
75 }catch(LDAPException e){
81 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope,
83 const StringList& attrs,
85 const LDAPConstraints *cons){
86 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" << endl);
87 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " base:" << base << endl
88 << " scope:" << scope << endl
89 << " filter:" << filter << endl );
90 LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs,
91 attrsOnly, this, cons);
93 LDAPMessageQueue *ret = req->sendRequest();
95 }catch(LDAPException e){
101 LDAPMessageQueue* LDAPAsynConnection::del(const string& dn,
102 const LDAPConstraints *cons){
103 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
104 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
105 LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
107 LDAPMessageQueue *ret = req->sendRequest();
109 }catch(LDAPException e){
115 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn,
116 const LDAPAttribute& attr, const LDAPConstraints *cons){
117 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
118 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
119 << " attr:" << attr << endl);
120 LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, this, cons);
122 LDAPMessageQueue *ret = req->sendRequest();
124 }catch(LDAPException e){
130 LDAPMessageQueue* LDAPAsynConnection::add( const LDAPEntry* le,
131 const LDAPConstraints *cons){
132 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
133 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," entry:" << *le << endl);
134 LDAPAddRequest *req = new LDAPAddRequest(le, this, cons);
136 LDAPMessageQueue *ret = req->sendRequest();
138 }catch(LDAPException e){
144 LDAPMessageQueue* LDAPAsynConnection::modify(const string& dn,
145 const LDAPModList *mod, const LDAPConstraints *cons){
146 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
147 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
148 LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
150 LDAPMessageQueue *ret = req->sendRequest();
152 }catch(LDAPException e){
158 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn,
159 const string& newRDN, bool delOldRDN, const string& newParentDN,
160 const LDAPConstraints *cons ){
161 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
162 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
163 << " newRDN:" << newRDN << endl
164 << " newParentDN:" << newParentDN << endl
165 << " delOldRDN:" << delOldRDN << endl);
166 LDAPModDNRequest *req = new LDAPModDNRequest(dn, newRDN, delOldRDN,
167 newParentDN, this, cons );
169 LDAPMessageQueue *ret = req->sendRequest();
171 }catch(LDAPException e){
178 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid,
179 const string& value, const LDAPConstraints *cons ){
180 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
181 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," oid:" << oid << endl);
182 LDAPExtRequest *req = new LDAPExtRequest(oid, value, this,cons);
184 LDAPMessageQueue *ret = req->sendRequest();
186 }catch(LDAPException e){
193 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
194 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
195 LDAPRequestStack *reqStack=q->getRequestStack();
197 while(! reqStack->empty()){
199 if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0)
201 throw LDAPException(this);
208 void LDAPAsynConnection::unbind(){
209 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
211 LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
212 LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
213 int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
215 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
216 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
217 if(err != LDAP_SUCCESS){
218 throw LDAPException(err);
223 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
224 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
228 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
229 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
233 LDAP* LDAPAsynConnection::getSessionHandle() const{
234 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
238 const string& LDAPAsynConnection::getHost() const{
239 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
243 int LDAPAsynConnection::getPort() const{
244 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
248 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
249 const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
250 const LDAPConstraints* cons) const {
251 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
252 LDAPUrlList::const_iterator conUrl;
253 LDAPAsynConnection* tmpConn=0;
254 const LDAPRebind* rebind = cons->getReferralRebind();
255 LDAPRebindAuth* auth = 0;
257 for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
258 string host= conUrl->getHost();
259 int port= conUrl->getPort();
260 DEBUG(LDAP_DEBUG_TRACE," connecting to: " << host << ":" <<
262 //Set the new connection's constraints-object ?
263 tmpConn=new LDAPAsynConnection(host.c_str(),port);
267 auth=rebind->getRebindAuth(host, port);
270 string dn = auth->getDN();
271 string passwd = auth->getPassword();
273 const char* c_passwd=0;
278 c_passwd = passwd.c_str();
280 err = ldap_simple_bind_s(tmpConn->getSessionHandle(), c_dn,
284 err = ldap_simple_bind_s(tmpConn->getSessionHandle(), 0,0);
286 if( err == LDAP_SUCCESS ){