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"
25 LDAPAsynConnection::LDAPAsynConnection(const string& hostname, int port,
26 LDAPConstraints *cons ){
27 DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPAsynConnection::LDAPAsynConnection()"
29 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
30 " host:" << hostname << endl << " port:" << port << endl);
32 this->init(hostname, port);
33 this->setConstraints(cons);
36 LDAPAsynConnection::~LDAPAsynConnection(){
37 DEBUG(LDAP_DEBUG_DESTROY,
38 "LDAPAsynConnection::~LDAPAsynConnection()" << endl);
43 void LDAPAsynConnection::init(const string& hostname, int port){
44 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::init" << endl);
45 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
46 " hostname:" << hostname << endl
47 << " port:" << port << endl);
48 cur_session=ldap_init(hostname.c_str(),port);
52 ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
53 ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
56 int LDAPAsynConnection::start_tls(){
57 return ldap_start_tls_s( cur_session, NULL, NULL );
60 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
61 const string& passwd, const LDAPConstraints *cons){
62 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" << endl);
63 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " dn:" << dn << endl
64 << " passwd:" << passwd << endl);
65 LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
67 LDAPMessageQueue *ret = req->sendRequest();
69 }catch(LDAPException e){
75 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope,
77 const StringList& attrs,
79 const LDAPConstraints *cons){
80 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" << endl);
81 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " base:" << base << endl
82 << " scope:" << scope << endl
83 << " filter:" << filter << endl );
84 LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs,
85 attrsOnly, this, cons);
87 LDAPMessageQueue *ret = req->sendRequest();
89 }catch(LDAPException e){
95 LDAPMessageQueue* LDAPAsynConnection::del(const string& dn,
96 const LDAPConstraints *cons){
97 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
98 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
99 LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
101 LDAPMessageQueue *ret = req->sendRequest();
103 }catch(LDAPException e){
109 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn,
110 const LDAPAttribute& attr, const LDAPConstraints *cons){
111 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
112 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
113 << " attr:" << attr << endl);
114 LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, this, cons);
116 LDAPMessageQueue *ret = req->sendRequest();
118 }catch(LDAPException e){
124 LDAPMessageQueue* LDAPAsynConnection::add( const LDAPEntry* le,
125 const LDAPConstraints *cons){
126 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
127 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," entry:" << *le << endl);
128 LDAPAddRequest *req = new LDAPAddRequest(le, this, cons);
130 LDAPMessageQueue *ret = req->sendRequest();
132 }catch(LDAPException e){
138 LDAPMessageQueue* LDAPAsynConnection::modify(const string& dn,
139 const LDAPModList *mod, const LDAPConstraints *cons){
140 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
141 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
142 LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
144 LDAPMessageQueue *ret = req->sendRequest();
146 }catch(LDAPException e){
152 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn,
153 const string& newRDN, bool delOldRDN, const string& newParentDN,
154 const LDAPConstraints *cons ){
155 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
156 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
157 << " newRDN:" << newRDN << endl
158 << " newParentDN:" << newParentDN << endl
159 << " delOldRDN:" << delOldRDN << endl);
160 LDAPModDNRequest *req = new LDAPModDNRequest(dn, newRDN, delOldRDN,
161 newParentDN, this, cons );
163 LDAPMessageQueue *ret = req->sendRequest();
165 }catch(LDAPException e){
172 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid,
173 const string& value, const LDAPConstraints *cons ){
174 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
175 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," oid:" << oid << endl);
176 LDAPExtRequest *req = new LDAPExtRequest(oid, value, this,cons);
178 LDAPMessageQueue *ret = req->sendRequest();
180 }catch(LDAPException e){
187 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
188 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
189 LDAPRequestStack *reqStack=q->getRequestStack();
191 while(! reqStack->empty()){
193 if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0)
195 throw LDAPException(this);
202 void LDAPAsynConnection::unbind(){
203 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
205 LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
206 LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
207 int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
209 ldap_controls_free(tmpSrvCtrls);
210 ldap_controls_free(tmpClCtrls);
211 if(err != LDAP_SUCCESS){
212 throw LDAPException(err);
217 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
218 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
222 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
223 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
227 LDAP* LDAPAsynConnection::getSessionHandle() const{
228 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
232 const string& LDAPAsynConnection::getHost() const{
233 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
237 int LDAPAsynConnection::getPort() const{
238 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
242 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
243 const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
244 const LDAPConstraints* cons) const {
245 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
246 LDAPUrlList::const_iterator conUrl;
247 LDAPAsynConnection* tmpConn=0;
248 const LDAPRebind* rebind = cons->getReferralRebind();
249 LDAPRebindAuth* auth = 0;
251 for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
252 string host= conUrl->getHost();
253 int port= conUrl->getPort();
254 DEBUG(LDAP_DEBUG_TRACE," connecting to: " << host << ":" <<
256 //Set the new connection's constraints-object ?
257 tmpConn=new LDAPAsynConnection(host.c_str(),port);
261 auth=rebind->getRebindAuth(host, port);
264 string dn = auth->getDN();
265 string passwd = auth->getPassword();
267 const char* c_passwd=0;
272 c_passwd = passwd.c_str();
274 err = ldap_simple_bind_s(tmpConn->getSessionHandle(), c_dn,
278 err = ldap_simple_bind_s(tmpConn->getSessionHandle(), 0,0);
280 if( err == LDAP_SUCCESS ){