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);
34 this->init(hostname, port);
35 this->setConstraints(cons);
38 LDAPAsynConnection::~LDAPAsynConnection(){
39 DEBUG(LDAP_DEBUG_DESTROY,
40 "LDAPAsynConnection::~LDAPAsynConnection()" << endl);
42 ldap_destroy_cache(cur_session);
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);
53 cur_session=ldap_init(hostname.c_str(),port);
57 ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
58 ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
61 int LDAPAsynConnection::start_tls(){
62 return ldap_start_tls_s( cur_session, NULL, NULL );
65 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
66 const string& passwd, const LDAPConstraints *cons){
67 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" << endl);
68 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " dn:" << dn << endl
69 << " passwd:" << passwd << endl);
70 LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
72 LDAPMessageQueue *ret = req->sendRequest();
74 }catch(LDAPException e){
80 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope,
82 const StringList& attrs,
84 const LDAPConstraints *cons){
85 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" << endl);
86 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, " base:" << base << endl
87 << " scope:" << scope << endl
88 << " filter:" << filter << endl );
89 LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs,
90 attrsOnly, this, cons);
92 LDAPMessageQueue *ret = req->sendRequest();
94 }catch(LDAPException e){
100 LDAPMessageQueue* LDAPAsynConnection::del(const string& dn,
101 const LDAPConstraints *cons){
102 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
103 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
104 LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
106 LDAPMessageQueue *ret = req->sendRequest();
108 }catch(LDAPException e){
114 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn,
115 const LDAPAttribute& attr, const LDAPConstraints *cons){
116 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
117 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
118 << " attr:" << attr << endl);
119 LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, this, cons);
121 LDAPMessageQueue *ret = req->sendRequest();
123 }catch(LDAPException e){
129 LDAPMessageQueue* LDAPAsynConnection::add( const LDAPEntry* le,
130 const LDAPConstraints *cons){
131 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
132 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," entry:" << *le << endl);
133 LDAPAddRequest *req = new LDAPAddRequest(le, this, cons);
135 LDAPMessageQueue *ret = req->sendRequest();
137 }catch(LDAPException e){
143 LDAPMessageQueue* LDAPAsynConnection::modify(const string& dn,
144 const LDAPModList *mod, const LDAPConstraints *cons){
145 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
146 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl);
147 LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
149 LDAPMessageQueue *ret = req->sendRequest();
151 }catch(LDAPException e){
157 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn,
158 const string& newRDN, bool delOldRDN, const string& newParentDN,
159 const LDAPConstraints *cons ){
160 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
161 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," dn:" << dn << endl
162 << " newRDN:" << newRDN << endl
163 << " newParentDN:" << newParentDN << endl
164 << " delOldRDN:" << delOldRDN << endl);
165 LDAPModDNRequest *req = new LDAPModDNRequest(dn, newRDN, delOldRDN,
166 newParentDN, this, cons );
168 LDAPMessageQueue *ret = req->sendRequest();
170 }catch(LDAPException e){
177 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid,
178 const string& value, const LDAPConstraints *cons ){
179 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
180 DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," oid:" << oid << endl);
181 LDAPExtRequest *req = new LDAPExtRequest(oid, value, this,cons);
183 LDAPMessageQueue *ret = req->sendRequest();
185 }catch(LDAPException e){
192 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
193 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
194 LDAPRequestStack *reqStack=q->getRequestStack();
196 while(! reqStack->empty()){
198 if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0)
200 throw LDAPException(this);
207 void LDAPAsynConnection::unbind(){
208 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
210 LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
211 LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
212 int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
214 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
215 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
216 if(err != LDAP_SUCCESS){
217 throw LDAPException(err);
222 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
223 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
227 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
228 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
232 LDAP* LDAPAsynConnection::getSessionHandle() const{
233 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
237 const string& LDAPAsynConnection::getHost() const{
238 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
242 int LDAPAsynConnection::getPort() const{
243 DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
247 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
248 const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
249 const LDAPConstraints* cons) const {
250 DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
251 LDAPUrlList::const_iterator conUrl;
252 LDAPAsynConnection* tmpConn=0;
253 const LDAPRebind* rebind = cons->getReferralRebind();
254 LDAPRebindAuth* auth = 0;
256 for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
257 string host= conUrl->getHost();
258 int port= conUrl->getPort();
259 DEBUG(LDAP_DEBUG_TRACE," connecting to: " << host << ":" <<
261 //Set the new connection's constraints-object ?
262 tmpConn=new LDAPAsynConnection(host.c_str(),port);
266 auth=rebind->getRebindAuth(host, port);
269 string dn = auth->getDN();
270 string passwd = auth->getPassword();
272 const char* c_passwd=0;
277 c_passwd = passwd.c_str();
279 err = ldap_simple_bind_s(tmpConn->getSessionHandle(), c_dn,
283 err = ldap_simple_bind_s(tmpConn->getSessionHandle(), 0,0);
285 if( err == LDAP_SUCCESS ){
297 int LDAPAsynConnection::enableCache(long timeout, long maxmem){
298 int retval = ldap_enable_cache(cur_session, timeout, maxmem);
300 m_cacheEnabled = true;
304 void LDAPAsynConnection::disableCache(){
305 ldap_disable_cache(cur_session);
306 m_cacheEnabled = false;
309 void LDAPAsynConnection::uncache_entry(string &dn){
311 ldap_uncache_entry(cur_session, dn.c_str());
315 void LDAPAsynConnection::flush_cache(){
317 ldap_flush_cache(cur_session);