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 int LDAPAsynConnection::start_tls(){
60 return ldap_start_tls_s( cur_session, NULL, NULL );
63 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
64 const string& passwd, const LDAPConstraints *cons){
65 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" << endl);
66 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " dn:" << dn << endl
67 << " passwd:" << passwd << endl);
68 LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
70 LDAPMessageQueue *ret = req->sendRequest();
72 }catch(LDAPException e){
78 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope,
80 const StringList& attrs,
82 const LDAPConstraints *cons){
83 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" << endl);
84 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " base:" << base << endl
85 << " scope:" << scope << endl
86 << " filter:" << filter << endl );
87 LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs,
88 attrsOnly, this, cons);
90 LDAPMessageQueue *ret = req->sendRequest();
92 }catch(LDAPException e){
98 LDAPMessageQueue* LDAPAsynConnection::del(const string& dn,
99 const LDAPConstraints *cons){
100 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
101 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
102 LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
104 LDAPMessageQueue *ret = req->sendRequest();
106 }catch(LDAPException e){
112 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn,
113 const LDAPAttribute& attr, const LDAPConstraints *cons){
114 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
115 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
116 << " attr:" << attr << endl);
117 LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, this, cons);
119 LDAPMessageQueue *ret = req->sendRequest();
121 }catch(LDAPException e){
127 LDAPMessageQueue* LDAPAsynConnection::add( const LDAPEntry* le,
128 const LDAPConstraints *cons){
129 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
130 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," entry:" << *le << endl);
131 LDAPAddRequest *req = new LDAPAddRequest(le, this, cons);
133 LDAPMessageQueue *ret = req->sendRequest();
135 }catch(LDAPException e){
141 LDAPMessageQueue* LDAPAsynConnection::modify(const string& dn,
142 const LDAPModList *mod, const LDAPConstraints *cons){
143 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
144 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
145 LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
147 LDAPMessageQueue *ret = req->sendRequest();
149 }catch(LDAPException e){
155 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn,
156 const string& newRDN, bool delOldRDN, const string& newParentDN,
157 const LDAPConstraints *cons ){
158 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
159 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
160 << " newRDN:" << newRDN << endl
161 << " newParentDN:" << newParentDN << endl
162 << " delOldRDN:" << delOldRDN << endl);
163 LDAPModDNRequest *req = new LDAPModDNRequest(dn, newRDN, delOldRDN,
164 newParentDN, this, cons );
166 LDAPMessageQueue *ret = req->sendRequest();
168 }catch(LDAPException e){
175 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid,
176 const string& value, const LDAPConstraints *cons ){
177 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
178 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," oid:" << oid << endl);
179 LDAPExtRequest *req = new LDAPExtRequest(oid, value, this,cons);
181 LDAPMessageQueue *ret = req->sendRequest();
183 }catch(LDAPException e){
190 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
191 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
192 LDAPRequestStack *reqStack=q->getRequestStack();
194 while(! reqStack->empty()){
196 if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0)
198 throw LDAPException(this);
205 void LDAPAsynConnection::unbind(){
206 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
208 LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
209 LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
210 int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
212 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
213 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
214 if(err != LDAP_SUCCESS){
215 throw LDAPException(err);
220 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
221 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
225 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
226 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
230 LDAP* LDAPAsynConnection::getSessionHandle() const{
231 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
235 const string& LDAPAsynConnection::getHost() const{
236 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
240 int LDAPAsynConnection::getPort() const{
241 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
245 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
246 const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
247 const LDAPConstraints* cons) const {
248 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
249 LDAPUrlList::const_iterator conUrl;
250 LDAPAsynConnection* tmpConn=0;
251 const LDAPRebind* rebind = cons->getReferralRebind();
252 LDAPRebindAuth* auth = 0;
254 for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
255 string host= conUrl->getHost();
256 int port= conUrl->getPort();
257 DEBUG(LDAP_DEBUG_TRACE," connecting to: " << host << ":" <<
259 //Set the new connection's constraints-object ?
260 tmpConn=new LDAPAsynConnection(host.c_str(),port);
264 auth=rebind->getRebindAuth(host, port);
267 string dn = auth->getDN();
268 string passwd = auth->getPassword();
270 const char* c_passwd=0;
275 c_passwd = passwd.c_str();
277 err = ldap_simple_bind_s(tmpConn->getSessionHandle(), c_dn,
281 err = ldap_simple_bind_s(tmpConn->getSessionHandle(), 0,0);
283 if( err == LDAP_SUCCESS ){