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