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 "LDAPExtRequest.h"
16 #include "LDAPEntry.h"
17 #include "LDAPModDNRequest.h"
18 #include "LDAPModifyRequest.h"
19 #include "LDAPRequest.h"
20 #include "LDAPRebind.h"
21 #include "LDAPRebindAuth.h"
22 #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);
52 m_uri.setScheme("ldap");
53 m_uri.setHost(hostname);
56 const char *ldapuri = m_uri.getURLString().c_str();
57 int ret = ldap_initialize(&cur_session, ldapuri);
58 if ( ret != LDAP_SUCCESS ) {
59 throw LDAPException( ret );
62 ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
63 ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
66 void LDAPAsynConnection::initialize(const std::string& uri){
67 m_uri.setURLString(uri);
68 int ret = ldap_initialize(&cur_session, m_uri.getURLString().c_str());
69 if ( ret != LDAP_SUCCESS ) {
70 throw LDAPException( ret );
73 ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
74 ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
77 void LDAPAsynConnection::start_tls(){
78 if( ldap_start_tls_s( cur_session, NULL, NULL ) != LDAP_SUCCESS ) {
79 throw LDAPException(this);
83 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
84 const string& passwd, const LDAPConstraints *cons){
85 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" << endl);
86 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " dn:" << dn << endl
87 << " passwd:" << passwd << endl);
88 LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
90 LDAPMessageQueue *ret = req->sendRequest();
92 }catch(LDAPException e){
98 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope,
100 const StringList& attrs,
102 const LDAPConstraints *cons){
103 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" << endl);
104 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " base:" << base << endl
105 << " scope:" << scope << endl
106 << " filter:" << filter << endl );
107 LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs,
108 attrsOnly, this, cons);
110 LDAPMessageQueue *ret = req->sendRequest();
112 }catch(LDAPException e){
118 LDAPMessageQueue* LDAPAsynConnection::del(const string& dn,
119 const LDAPConstraints *cons){
120 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
121 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
122 LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
124 LDAPMessageQueue *ret = req->sendRequest();
126 }catch(LDAPException e){
132 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn,
133 const LDAPAttribute& attr, const LDAPConstraints *cons){
134 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
135 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
136 << " attr:" << attr << endl);
137 LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, this, cons);
139 LDAPMessageQueue *ret = req->sendRequest();
141 }catch(LDAPException e){
147 LDAPMessageQueue* LDAPAsynConnection::add( const LDAPEntry* le,
148 const LDAPConstraints *cons){
149 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
150 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," entry:" << *le << endl);
151 LDAPAddRequest *req = new LDAPAddRequest(le, this, cons);
153 LDAPMessageQueue *ret = req->sendRequest();
155 }catch(LDAPException e){
161 LDAPMessageQueue* LDAPAsynConnection::modify(const string& dn,
162 const LDAPModList *mod, const LDAPConstraints *cons){
163 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
164 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
165 LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
167 LDAPMessageQueue *ret = req->sendRequest();
169 }catch(LDAPException e){
175 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn,
176 const string& newRDN, bool delOldRDN, const string& newParentDN,
177 const LDAPConstraints *cons ){
178 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
179 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
180 << " newRDN:" << newRDN << endl
181 << " newParentDN:" << newParentDN << endl
182 << " delOldRDN:" << delOldRDN << endl);
183 LDAPModDNRequest *req = new LDAPModDNRequest(dn, newRDN, delOldRDN,
184 newParentDN, this, cons );
186 LDAPMessageQueue *ret = req->sendRequest();
188 }catch(LDAPException e){
195 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid,
196 const string& value, const LDAPConstraints *cons ){
197 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
198 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," oid:" << oid << endl);
199 LDAPExtRequest *req = new LDAPExtRequest(oid, value, this,cons);
201 LDAPMessageQueue *ret = req->sendRequest();
203 }catch(LDAPException e){
210 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
211 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
212 LDAPRequestStack *reqStack=q->getRequestStack();
214 while(! reqStack->empty()){
216 if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0)
218 throw LDAPException(this);
225 void LDAPAsynConnection::unbind(){
226 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
228 LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
229 LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
230 int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
232 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
233 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
234 if(err != LDAP_SUCCESS){
235 throw LDAPException(err);
240 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
241 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
245 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
246 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
250 LDAP* LDAPAsynConnection::getSessionHandle() const{
251 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
255 const string& LDAPAsynConnection::getHost() const{
256 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
257 return m_uri.getHost();
260 int LDAPAsynConnection::getPort() const{
261 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
262 return m_uri.getPort();
265 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
266 const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
267 const LDAPConstraints* cons) const {
268 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
269 LDAPUrlList::const_iterator conUrl;
270 LDAPAsynConnection* tmpConn=0;
271 const LDAPRebind* rebind = cons->getReferralRebind();
272 LDAPRebindAuth* auth = 0;
274 for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
275 string host= conUrl->getHost();
276 int port= conUrl->getPort();
277 DEBUG(LDAP_DEBUG_TRACE," connecting to: " << host << ":" <<
279 //Set the new connection's constraints-object ?
280 tmpConn=new LDAPAsynConnection(host.c_str(),port);
284 auth=rebind->getRebindAuth(host, port);
287 string dn = auth->getDN();
288 string passwd = auth->getPassword();
290 struct berval c_passwd = { 0, 0 };
295 c_passwd.bv_val = const_cast<char*>(passwd.c_str());
296 c_passwd.bv_len = passwd.size();
298 err = ldap_sasl_bind_s(tmpConn->getSessionHandle(), c_dn,
299 LDAP_SASL_SIMPLE, &c_passwd, NULL, NULL, NULL);
302 err = ldap_sasl_bind_s(tmpConn->getSessionHandle(),NULL,
303 LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL);
305 if( err == LDAP_SUCCESS ){