3 * Copyright 2000-2011 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include "LDAPAsynConnection.h"
12 #include "LDAPAddRequest.h"
13 #include "LDAPBindRequest.h"
14 #include "LDAPCompareRequest.h"
15 #include "LDAPDeleteRequest.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"
29 LDAPAsynConnection::LDAPAsynConnection(const string& url, int port,
30 LDAPConstraints *cons ){
31 DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPAsynConnection::LDAPAsynConnection()"
33 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
34 " URL:" << url << endl << " port:" << port << endl);
37 // Is this an LDAP URI?
38 if ( url.find("://") == std::string::npos ) {
39 this->init(url, port);
41 this->initialize(url);
43 this->setConstraints(cons);
46 LDAPAsynConnection::~LDAPAsynConnection(){}
48 void LDAPAsynConnection::init(const string& hostname, int port){
49 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::init" << endl);
50 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
51 " hostname:" << hostname << endl
52 << " port:" << port << endl);
54 m_uri.setScheme("ldap");
55 m_uri.setHost(hostname);
58 const char *ldapuri = m_uri.getURLString().c_str();
59 int ret = ldap_initialize(&cur_session, ldapuri);
60 if ( ret != LDAP_SUCCESS ) {
61 throw LDAPException( ret );
64 ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
65 ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
68 void LDAPAsynConnection::initialize(const std::string& uri){
69 m_uri.setURLString(uri);
70 int ret = ldap_initialize(&cur_session, m_uri.getURLString().c_str());
71 if ( ret != LDAP_SUCCESS ) {
72 throw LDAPException( ret );
75 ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
76 ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
79 void LDAPAsynConnection::start_tls(){
80 int ret = ldap_start_tls_s( cur_session, NULL, NULL );
81 if( ret != LDAP_SUCCESS ) {
82 throw LDAPException(this);
86 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
87 const string& passwd, const LDAPConstraints *cons){
88 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" << endl);
89 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " dn:" << dn << endl
90 << " passwd:" << passwd << endl);
91 LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
93 LDAPMessageQueue *ret = req->sendRequest();
95 }catch(LDAPException e){
101 LDAPMessageQueue* LDAPAsynConnection::saslBind(const std::string &mech,
102 const std::string &cred,
103 const LDAPConstraints *cons)
105 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::saslBind()" << endl);
106 LDAPSaslBindRequest *req = new LDAPSaslBindRequest(mech, cred, this, cons);
108 LDAPMessageQueue *ret = req->sendRequest();
110 }catch(LDAPException e){
117 LDAPMessageQueue* LDAPAsynConnection::saslInteractiveBind(
118 const std::string &mech,
120 SaslInteractionHandler *sih,
121 const LDAPConstraints *cons)
123 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::saslInteractiveBind"
125 LDAPSaslInteractiveBind *req =
126 new LDAPSaslInteractiveBind(mech, flags, sih, this, cons);
128 LDAPMessageQueue *ret = req->sendRequest();
130 }catch(LDAPException e){
136 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope,
137 const string& filter,
138 const StringList& attrs,
140 const LDAPConstraints *cons){
141 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" << endl);
142 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " base:" << base << endl
143 << " scope:" << scope << endl
144 << " filter:" << filter << endl );
145 LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs,
146 attrsOnly, this, cons);
148 LDAPMessageQueue *ret = req->sendRequest();
150 }catch(LDAPException e){
156 LDAPMessageQueue* LDAPAsynConnection::del(const string& dn,
157 const LDAPConstraints *cons){
158 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
159 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
160 LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
162 LDAPMessageQueue *ret = req->sendRequest();
164 }catch(LDAPException e){
170 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn,
171 const LDAPAttribute& attr, const LDAPConstraints *cons){
172 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
173 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
174 << " attr:" << attr << endl);
175 LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, this, cons);
177 LDAPMessageQueue *ret = req->sendRequest();
179 }catch(LDAPException e){
185 LDAPMessageQueue* LDAPAsynConnection::add( const LDAPEntry* le,
186 const LDAPConstraints *cons){
187 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
188 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," entry:" << *le << endl);
189 LDAPAddRequest *req = new LDAPAddRequest(le, this, cons);
191 LDAPMessageQueue *ret = req->sendRequest();
193 }catch(LDAPException e){
199 LDAPMessageQueue* LDAPAsynConnection::modify(const string& dn,
200 const LDAPModList *mod, const LDAPConstraints *cons){
201 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
202 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
203 LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
205 LDAPMessageQueue *ret = req->sendRequest();
207 }catch(LDAPException e){
213 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn,
214 const string& newRDN, bool delOldRDN, const string& newParentDN,
215 const LDAPConstraints *cons ){
216 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
217 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
218 << " newRDN:" << newRDN << endl
219 << " newParentDN:" << newParentDN << endl
220 << " delOldRDN:" << delOldRDN << endl);
221 LDAPModDNRequest *req = new LDAPModDNRequest(dn, newRDN, delOldRDN,
222 newParentDN, this, cons );
224 LDAPMessageQueue *ret = req->sendRequest();
226 }catch(LDAPException e){
233 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid,
234 const string& value, const LDAPConstraints *cons ){
235 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
236 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," oid:" << oid << endl);
237 LDAPExtRequest *req = new LDAPExtRequest(oid, value, this,cons);
239 LDAPMessageQueue *ret = req->sendRequest();
241 }catch(LDAPException e){
248 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
249 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
250 LDAPRequestStack *reqStack=q->getRequestStack();
252 while(! reqStack->empty()){
254 if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0)
256 throw LDAPException(this);
263 void LDAPAsynConnection::unbind(){
264 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
266 LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
267 LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
268 int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
270 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
271 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
272 if(err != LDAP_SUCCESS){
273 throw LDAPException(err);
278 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
279 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
283 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
284 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
288 TlsOptions LDAPAsynConnection::getTlsOptions() const {
289 return TlsOptions( cur_session );
292 LDAP* LDAPAsynConnection::getSessionHandle() const{
293 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
297 const string& LDAPAsynConnection::getHost() const{
298 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
299 return m_uri.getHost();
302 int LDAPAsynConnection::getPort() const{
303 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
304 return m_uri.getPort();
307 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
308 const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
309 const LDAPConstraints* cons) const {
310 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
311 LDAPUrlList::const_iterator conUrl;
312 LDAPAsynConnection* tmpConn=0;
313 const LDAPRebind* rebind = cons->getReferralRebind();
314 LDAPRebindAuth* auth = 0;
316 for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
317 string host= conUrl->getHost();
318 int port= conUrl->getPort();
319 DEBUG(LDAP_DEBUG_TRACE," connecting to: " << host << ":" <<
321 //Set the new connection's constraints-object ?
322 tmpConn=new LDAPAsynConnection(host.c_str(),port);
326 auth=rebind->getRebindAuth(host, port);
329 string dn = auth->getDN();
330 string passwd = auth->getPassword();
332 struct berval c_passwd = { 0, 0 };
337 c_passwd.bv_val = const_cast<char*>(passwd.c_str());
338 c_passwd.bv_len = passwd.size();
340 err = ldap_sasl_bind_s(tmpConn->getSessionHandle(), c_dn,
341 LDAP_SASL_SIMPLE, &c_passwd, NULL, NULL, NULL);
344 err = ldap_sasl_bind_s(tmpConn->getSessionHandle(),NULL,
345 LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL);
347 if( err == LDAP_SUCCESS ){