]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPAsynConnection.cpp
74011169f54b87a901b865e6d7c371aeba064f44
[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     m_constr = 0;
35     this->init(hostname, port);
36     this->setConstraints(cons);
37 }
38
39 LDAPAsynConnection::~LDAPAsynConnection(){
40     DEBUG(LDAP_DEBUG_DESTROY,
41             "LDAPAsynConnection::~LDAPAsynConnection()" << endl);
42     unbind();
43     //delete m_constr;        
44 }
45
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);
51     cur_session=ldap_init(hostname.c_str(),port);
52     m_host=hostname;
53     m_port=port;
54     int opt=3;
55     ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
56     ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
57 }
58
59 int LDAPAsynConnection::start_tls(){
60     return ldap_start_tls_s( cur_session, NULL, NULL );
61 }
62
63 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
64         const string& passwd, const LDAPConstraints *cons){
65     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" <<  endl);
66     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, "   dn:" << dn << endl
67                << "   passwd:" << passwd << endl);
68     LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
69     try{
70         LDAPMessageQueue *ret = req->sendRequest();
71         return ret;
72     }catch(LDAPException e){
73         delete req;
74         throw;
75     }
76 }
77
78 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope, 
79                                          const string& filter, 
80                                          const StringList& attrs, 
81                                          bool attrsOnly,
82                                          const LDAPConstraints *cons){
83     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" <<  endl);
84     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, "   base:" << base << endl
85                << "   scope:" << scope << endl
86                << "   filter:" << filter << endl );
87     LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs, 
88             attrsOnly, this, cons);
89     try{
90         LDAPMessageQueue *ret = req->sendRequest();
91         return ret;
92     }catch(LDAPException e){
93         delete req;
94         throw;
95     }
96 }
97
98 LDAPMessageQueue* LDAPAsynConnection::del(const string& dn, 
99         const LDAPConstraints *cons){
100     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
101     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl);
102     LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
103     try{
104         LDAPMessageQueue *ret = req->sendRequest();
105         return ret;
106     }catch(LDAPException e){
107         delete req;
108         throw;
109     }
110 }
111
112 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn, 
113         const LDAPAttribute& attr, const LDAPConstraints *cons){
114     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
115     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl
116             << "   attr:" << attr << endl);
117     LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, this, cons);
118     try{
119         LDAPMessageQueue *ret = req->sendRequest();
120         return ret;
121     }catch(LDAPException e){
122         delete req;
123         throw;
124     }
125 }
126
127 LDAPMessageQueue* LDAPAsynConnection::add( const LDAPEntry* le, 
128         const LDAPConstraints *cons){
129     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
130     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   entry:" << *le << endl);
131     LDAPAddRequest *req = new LDAPAddRequest(le, this, cons);
132     try{
133         LDAPMessageQueue *ret = req->sendRequest();
134         return ret;
135     }catch(LDAPException e){
136         delete req;
137         throw;
138     }
139 }
140
141 LDAPMessageQueue* LDAPAsynConnection::modify(const string& dn,
142         const LDAPModList *mod, const LDAPConstraints *cons){
143     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
144     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl);
145     LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
146     try{
147         LDAPMessageQueue *ret = req->sendRequest();
148         return ret;
149     }catch(LDAPException e){
150         delete req;
151         throw;
152     }
153 }
154
155 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn, 
156         const string& newRDN, bool delOldRDN, const string& newParentDN,
157         const LDAPConstraints *cons ){
158     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
159     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl
160             << "   newRDN:" << newRDN << endl
161             << "   newParentDN:" << newParentDN << endl
162             << "   delOldRDN:" << delOldRDN << endl);
163     LDAPModDNRequest *req = new  LDAPModDNRequest(dn, newRDN, delOldRDN, 
164             newParentDN, this, cons );
165     try{
166         LDAPMessageQueue *ret = req->sendRequest();
167         return ret;
168     }catch(LDAPException e){
169         delete req;
170         throw;
171     }
172 }
173
174
175 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid, 
176         const string& value, const LDAPConstraints *cons ){
177     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
178     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   oid:" << oid << endl);
179     LDAPExtRequest *req = new  LDAPExtRequest(oid, value, this,cons);
180     try{
181         LDAPMessageQueue *ret = req->sendRequest();
182         return ret;
183     }catch(LDAPException e){
184         delete req;
185         throw;
186     }
187 }
188
189
190 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
191     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
192     LDAPRequestStack *reqStack=q->getRequestStack();
193     LDAPRequest *req;
194     while(! reqStack->empty()){
195         req=reqStack->top();
196         if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0) 
197                 != LDAP_SUCCESS){
198             throw LDAPException(this);
199         }
200         delete req;
201         reqStack->pop();
202     }
203 }
204
205 void LDAPAsynConnection::unbind(){
206     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
207     if(cur_session){
208         LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
209         LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
210         int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
211         cur_session=0;
212         LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
213         LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
214         if(err != LDAP_SUCCESS){
215             throw LDAPException(err);
216         }
217     }
218 }
219
220 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
221     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
222     m_constr=cons;
223 }
224
225 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
226     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
227     return m_constr;
228 }
229  
230 LDAP* LDAPAsynConnection::getSessionHandle() const{ 
231     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
232     return cur_session;
233 }
234
235 const string& LDAPAsynConnection::getHost() const{
236     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
237     return m_host;
238 }
239
240 int LDAPAsynConnection::getPort() const{
241     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
242     return m_port;
243 }
244
245 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
246         const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
247         const LDAPConstraints* cons) const {
248     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
249     LDAPUrlList::const_iterator conUrl;
250     LDAPAsynConnection* tmpConn=0;
251     const LDAPRebind* rebind = cons->getReferralRebind();
252     LDAPRebindAuth* auth = 0;
253
254     for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
255         string host= conUrl->getHost();
256         int port= conUrl->getPort();
257         DEBUG(LDAP_DEBUG_TRACE,"   connecting to: " << host << ":" <<
258                 port << endl);
259         //Set the new connection's constraints-object ?
260         tmpConn=new LDAPAsynConnection(host.c_str(),port);
261         int err=0;
262
263         if(rebind){ 
264             auth=rebind->getRebindAuth(host, port);
265         }
266         if(auth){
267             string dn = auth->getDN();
268             string passwd = auth->getPassword();
269             const char* c_dn=0;
270             const char* c_passwd=0;
271             if(dn != ""){
272                 c_dn = dn.c_str();
273             }
274             if(passwd != ""){
275                 c_passwd = passwd.c_str();
276             }
277             err = ldap_simple_bind_s(tmpConn->getSessionHandle(), c_dn,
278                     c_passwd);
279         } else {   
280             // Do anonymous bind
281             err = ldap_simple_bind_s(tmpConn->getSessionHandle(), 0,0);
282         }
283         if( err == LDAP_SUCCESS ){
284             usedUrl=conUrl;
285             return tmpConn;
286         }else{
287             delete tmpConn;
288             tmpConn=0;
289         }
290         auth=0;
291     }
292     return 0;
293 }
294