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