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);
54 memset( &url, 0, sizeof(url));
56 url.lud_scheme = "ldap";
57 url.lud_host = strdup(hostname.c_str());
59 url.lud_scope = LDAP_SCOPE_DEFAULT;
61 ldapuri = ldap_url_desc2str( &url );
62 int ret = ldap_initialize(&cur_session, ldapuri);
63 if ( ret != LDAP_SUCCESS ) {
64 throw LDAPException( ret );
69 ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
70 ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
73 void LDAPAsynConnection::start_tls(){
74 if( ldap_start_tls_s( cur_session, NULL, NULL ) != LDAP_SUCCESS ) {
75 throw LDAPException(this);
79 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
80 const string& passwd, const LDAPConstraints *cons){
81 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" << endl);
82 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " dn:" << dn << endl
83 << " passwd:" << passwd << endl);
84 LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
86 LDAPMessageQueue *ret = req->sendRequest();
88 }catch(LDAPException e){
94 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope,
96 const StringList& attrs,
98 const LDAPConstraints *cons){
99 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" << endl);
100 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " base:" << base << endl
101 << " scope:" << scope << endl
102 << " filter:" << filter << endl );
103 LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs,
104 attrsOnly, this, cons);
106 LDAPMessageQueue *ret = req->sendRequest();
108 }catch(LDAPException e){
114 LDAPMessageQueue* LDAPAsynConnection::del(const string& dn,
115 const LDAPConstraints *cons){
116 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
117 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
118 LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
120 LDAPMessageQueue *ret = req->sendRequest();
122 }catch(LDAPException e){
128 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn,
129 const LDAPAttribute& attr, const LDAPConstraints *cons){
130 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
131 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
132 << " attr:" << attr << endl);
133 LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, this, cons);
135 LDAPMessageQueue *ret = req->sendRequest();
137 }catch(LDAPException e){
143 LDAPMessageQueue* LDAPAsynConnection::add( const LDAPEntry* le,
144 const LDAPConstraints *cons){
145 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
146 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," entry:" << *le << endl);
147 LDAPAddRequest *req = new LDAPAddRequest(le, this, cons);
149 LDAPMessageQueue *ret = req->sendRequest();
151 }catch(LDAPException e){
157 LDAPMessageQueue* LDAPAsynConnection::modify(const string& dn,
158 const LDAPModList *mod, const LDAPConstraints *cons){
159 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
160 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
161 LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
163 LDAPMessageQueue *ret = req->sendRequest();
165 }catch(LDAPException e){
171 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn,
172 const string& newRDN, bool delOldRDN, const string& newParentDN,
173 const LDAPConstraints *cons ){
174 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
175 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
176 << " newRDN:" << newRDN << endl
177 << " newParentDN:" << newParentDN << endl
178 << " delOldRDN:" << delOldRDN << endl);
179 LDAPModDNRequest *req = new LDAPModDNRequest(dn, newRDN, delOldRDN,
180 newParentDN, this, cons );
182 LDAPMessageQueue *ret = req->sendRequest();
184 }catch(LDAPException e){
191 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid,
192 const string& value, const LDAPConstraints *cons ){
193 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
194 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," oid:" << oid << endl);
195 LDAPExtRequest *req = new LDAPExtRequest(oid, value, this,cons);
197 LDAPMessageQueue *ret = req->sendRequest();
199 }catch(LDAPException e){
206 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
207 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
208 LDAPRequestStack *reqStack=q->getRequestStack();
210 while(! reqStack->empty()){
212 if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0)
214 throw LDAPException(this);
221 void LDAPAsynConnection::unbind(){
222 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
224 LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
225 LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
226 int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
228 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
229 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
230 if(err != LDAP_SUCCESS){
231 throw LDAPException(err);
236 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
237 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
241 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
242 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
246 LDAP* LDAPAsynConnection::getSessionHandle() const{
247 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
251 const string& LDAPAsynConnection::getHost() const{
252 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
256 int LDAPAsynConnection::getPort() const{
257 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
261 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
262 const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
263 const LDAPConstraints* cons) const {
264 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
265 LDAPUrlList::const_iterator conUrl;
266 LDAPAsynConnection* tmpConn=0;
267 const LDAPRebind* rebind = cons->getReferralRebind();
268 LDAPRebindAuth* auth = 0;
270 for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
271 string host= conUrl->getHost();
272 int port= conUrl->getPort();
273 DEBUG(LDAP_DEBUG_TRACE," connecting to: " << host << ":" <<
275 //Set the new connection's constraints-object ?
276 tmpConn=new LDAPAsynConnection(host.c_str(),port);
280 auth=rebind->getRebindAuth(host, port);
283 string dn = auth->getDN();
284 string passwd = auth->getPassword();
286 struct berval c_passwd = { 0, 0 };
291 c_passwd.bv_val = const_cast<char*>(passwd.c_str());
292 c_passwd.bv_len = passwd.size();
294 err = ldap_sasl_bind_s(tmpConn->getSessionHandle(), c_dn,
295 LDAP_SASL_SIMPLE, &c_passwd, NULL, NULL, NULL);
298 err = ldap_sasl_bind_s(tmpConn->getSessionHandle(),NULL,
299 LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL);
301 if( err == LDAP_SUCCESS ){