]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPAsynConnection.cpp
Entry rwlock is no longer needed as concurrency is managed
[openldap] / contrib / ldapc++ / src / LDAPAsynConnection.cpp
1 /*
2  * Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6
7 #include "config.h"
8 #include "debug.h"
9 #include "LDAPAsynConnection.h"
10
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"
24
25 using namespace std;
26
27 LDAPAsynConnection::LDAPAsynConnection(const string& hostname, int port,
28                                LDAPConstraints *cons ){
29     DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPAsynConnection::LDAPAsynConnection()"
30             << endl);
31     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
32             "   host:" << hostname << endl << "   port:" << port << endl);
33     cur_session=0;
34     this->init(hostname, port);
35     this->setConstraints(cons);
36 }
37
38 LDAPAsynConnection::~LDAPAsynConnection(){
39     DEBUG(LDAP_DEBUG_DESTROY,
40             "LDAPAsynConnection::~LDAPAsynConnection()" << endl);
41     if (cur_session){
42         ldap_destroy_cache(cur_session);
43     }    
44     unbind();
45     //delete m_constr;        
46 }
47
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);
54     m_host=hostname;
55     m_port=port;
56     int opt=3;
57     ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
58     ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
59 }
60
61 int LDAPAsynConnection::start_tls(){
62     return ldap_start_tls_s( cur_session, NULL, NULL );
63 }
64
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);
71     try{
72         LDAPMessageQueue *ret = req->sendRequest();
73         return ret;
74     }catch(LDAPException e){
75         delete req;
76         throw;
77     }
78 }
79
80 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope, 
81                                          const string& filter, 
82                                          const StringList& attrs, 
83                                          bool attrsOnly,
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);
91     try{
92         LDAPMessageQueue *ret = req->sendRequest();
93         return ret;
94     }catch(LDAPException e){
95         delete req;
96         throw;
97     }
98 }
99
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);
105     try{
106         LDAPMessageQueue *ret = req->sendRequest();
107         return ret;
108     }catch(LDAPException e){
109         delete req;
110         throw;
111     }
112 }
113
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);
120     try{
121         LDAPMessageQueue *ret = req->sendRequest();
122         return ret;
123     }catch(LDAPException e){
124         delete req;
125         throw;
126     }
127 }
128
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);
134     try{
135         LDAPMessageQueue *ret = req->sendRequest();
136         return ret;
137     }catch(LDAPException e){
138         delete req;
139         throw;
140     }
141 }
142
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);
148     try{
149         LDAPMessageQueue *ret = req->sendRequest();
150         return ret;
151     }catch(LDAPException e){
152         delete req;
153         throw;
154     }
155 }
156
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 );
167     try{
168         LDAPMessageQueue *ret = req->sendRequest();
169         return ret;
170     }catch(LDAPException e){
171         delete req;
172         throw;
173     }
174 }
175
176
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);
182     try{
183         LDAPMessageQueue *ret = req->sendRequest();
184         return ret;
185     }catch(LDAPException e){
186         delete req;
187         throw;
188     }
189 }
190
191
192 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
193     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
194     LDAPRequestStack *reqStack=q->getRequestStack();
195     LDAPRequest *req;
196     while(! reqStack->empty()){
197         req=reqStack->top();
198         if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0) 
199                 != LDAP_SUCCESS){
200             throw LDAPException(this);
201         }
202         delete req;
203         reqStack->pop();
204     }
205 }
206
207 void LDAPAsynConnection::unbind(){
208     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
209     if(cur_session){
210         LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
211         LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
212         int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
213         cur_session=0;
214         LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
215         LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
216         if(err != LDAP_SUCCESS){
217             throw LDAPException(err);
218         }
219     }
220 }
221
222 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
223     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
224     m_constr=cons;
225 }
226
227 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
228     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
229     return m_constr;
230 }
231  
232 LDAP* LDAPAsynConnection::getSessionHandle() const{ 
233     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
234     return cur_session;
235 }
236
237 const string& LDAPAsynConnection::getHost() const{
238     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
239     return m_host;
240 }
241
242 int LDAPAsynConnection::getPort() const{
243     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
244     return m_port;
245 }
246
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;
255
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 << ":" <<
260                 port << endl);
261         //Set the new connection's constraints-object ?
262         tmpConn=new LDAPAsynConnection(host.c_str(),port);
263         int err=0;
264
265         if(rebind){ 
266             auth=rebind->getRebindAuth(host, port);
267         }
268         if(auth){
269             string dn = auth->getDN();
270             string passwd = auth->getPassword();
271             const char* c_dn=0;
272             const char* c_passwd=0;
273             if(dn != ""){
274                 c_dn = dn.c_str();
275             }
276             if(passwd != ""){
277                 c_passwd = passwd.c_str();
278             }
279             err = ldap_simple_bind_s(tmpConn->getSessionHandle(), c_dn,
280                     c_passwd);
281         } else {   
282             // Do anonymous bind
283             err = ldap_simple_bind_s(tmpConn->getSessionHandle(), 0,0);
284         }
285         if( err == LDAP_SUCCESS ){
286             usedUrl=conUrl;
287             return tmpConn;
288         }else{
289             delete tmpConn;
290             tmpConn=0;
291         }
292         auth=0;
293     }
294     return 0;
295 }
296
297 int LDAPAsynConnection::enableCache(long timeout, long maxmem){
298     int retval = ldap_enable_cache(cur_session, timeout, maxmem);
299     if (!retval)
300         m_cacheEnabled = true;
301     return retval;    
302 }    
303
304 void LDAPAsynConnection::disableCache(){
305     ldap_disable_cache(cur_session);
306     m_cacheEnabled = false;
307 }
308
309 void LDAPAsynConnection::uncache_entry(string &dn){
310     if (m_cacheEnabled){
311         ldap_uncache_entry(cur_session, dn.c_str());
312     }
313 }
314
315 void LDAPAsynConnection::flush_cache(){
316     if (m_cacheEnabled){
317         ldap_flush_cache(cur_session);
318     }
319 }