]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPAsynConnection.cpp
Sync with HEAD
[openldap] / contrib / ldapc++ / src / LDAPAsynConnection.cpp
1 /*
2  * Copyright 2000-2006, 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 "LDAPExtRequest.h"
16 #include "LDAPEntry.h"
17 #include "LDAPModDNRequest.h"
18 #include "LDAPModifyRequest.h"
19 #include "LDAPRequest.h"
20 #include "LDAPRebind.h"
21 #include "LDAPRebindAuth.h"
22 #include "LDAPSearchRequest.h"
23 #include <sstream>
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
52     char* ldapuri;
53     LDAPURLDesc url;
54     memset( &url, 0, sizeof(url));
55
56     url.lud_scheme = "ldap";
57     url.lud_host = strdup(hostname.c_str());
58     url.lud_port = port;
59     url.lud_scope = LDAP_SCOPE_DEFAULT;
60
61     ldapuri = ldap_url_desc2str( &url );
62     int ret = ldap_initialize(&cur_session, ldapuri);
63     if ( ret != LDAP_SUCCESS ) {
64         throw LDAPException( ret );
65     }
66     m_host=hostname;
67     m_port=port;
68     int opt=3;
69     ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
70     ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
71 }
72
73 void LDAPAsynConnection::start_tls(){
74     if( ldap_start_tls_s( cur_session, NULL, NULL ) != LDAP_SUCCESS ) {
75         throw LDAPException(this);
76     }
77 }
78
79 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
80         const string& passwd, const LDAPConstraints *cons){
81     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" <<  endl);
82     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, "   dn:" << dn << endl
83                << "   passwd:" << passwd << endl);
84     LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
85     try{
86         LDAPMessageQueue *ret = req->sendRequest();
87         return ret;
88     }catch(LDAPException e){
89         delete req;
90         throw;
91     }
92 }
93
94 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope, 
95                                          const string& filter, 
96                                          const StringList& attrs, 
97                                          bool attrsOnly,
98                                          const LDAPConstraints *cons){
99     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" <<  endl);
100     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, "   base:" << base << endl
101                << "   scope:" << scope << endl
102                << "   filter:" << filter << endl );
103     LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs, 
104             attrsOnly, 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::del(const string& dn, 
115         const LDAPConstraints *cons){
116     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
117     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl);
118     LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
119     try{
120         LDAPMessageQueue *ret = req->sendRequest();
121         return ret;
122     }catch(LDAPException e){
123         delete req;
124         throw;
125     }
126 }
127
128 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn, 
129         const LDAPAttribute& attr, const LDAPConstraints *cons){
130     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
131     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl
132             << "   attr:" << attr << endl);
133     LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, 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::add( const LDAPEntry* le, 
144         const LDAPConstraints *cons){
145     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
146     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   entry:" << *le << endl);
147     LDAPAddRequest *req = new LDAPAddRequest(le, 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::modify(const string& dn,
158         const LDAPModList *mod, const LDAPConstraints *cons){
159     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
160     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl);
161     LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, 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 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn, 
172         const string& newRDN, bool delOldRDN, const string& newParentDN,
173         const LDAPConstraints *cons ){
174     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
175     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl
176             << "   newRDN:" << newRDN << endl
177             << "   newParentDN:" << newParentDN << endl
178             << "   delOldRDN:" << delOldRDN << endl);
179     LDAPModDNRequest *req = new  LDAPModDNRequest(dn, newRDN, delOldRDN, 
180             newParentDN, this, cons );
181     try{
182         LDAPMessageQueue *ret = req->sendRequest();
183         return ret;
184     }catch(LDAPException e){
185         delete req;
186         throw;
187     }
188 }
189
190
191 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid, 
192         const string& value, const LDAPConstraints *cons ){
193     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
194     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   oid:" << oid << endl);
195     LDAPExtRequest *req = new  LDAPExtRequest(oid, value, this,cons);
196     try{
197         LDAPMessageQueue *ret = req->sendRequest();
198         return ret;
199     }catch(LDAPException e){
200         delete req;
201         throw;
202     }
203 }
204
205
206 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
207     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
208     LDAPRequestStack *reqStack=q->getRequestStack();
209     LDAPRequest *req;
210     while(! reqStack->empty()){
211         req=reqStack->top();
212         if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0) 
213                 != LDAP_SUCCESS){
214             throw LDAPException(this);
215         }
216         delete req;
217         reqStack->pop();
218     }
219 }
220
221 void LDAPAsynConnection::unbind(){
222     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
223     if(cur_session){
224         LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
225         LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
226         int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
227         cur_session=0;
228         LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
229         LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
230         if(err != LDAP_SUCCESS){
231             throw LDAPException(err);
232         }
233     }
234 }
235
236 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
237     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
238     m_constr=cons;
239 }
240
241 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
242     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
243     return m_constr;
244 }
245  
246 LDAP* LDAPAsynConnection::getSessionHandle() const{ 
247     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
248     return cur_session;
249 }
250
251 const string& LDAPAsynConnection::getHost() const{
252     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
253     return m_host;
254 }
255
256 int LDAPAsynConnection::getPort() const{
257     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
258     return m_port;
259 }
260
261 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
262         const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
263         const LDAPConstraints* cons) const {
264     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
265     LDAPUrlList::const_iterator conUrl;
266     LDAPAsynConnection* tmpConn=0;
267     const LDAPRebind* rebind = cons->getReferralRebind();
268     LDAPRebindAuth* auth = 0;
269
270     for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
271         string host= conUrl->getHost();
272         int port= conUrl->getPort();
273         DEBUG(LDAP_DEBUG_TRACE,"   connecting to: " << host << ":" <<
274                 port << endl);
275         //Set the new connection's constraints-object ?
276         tmpConn=new LDAPAsynConnection(host.c_str(),port);
277         int err=0;
278
279         if(rebind){ 
280             auth=rebind->getRebindAuth(host, port);
281         }
282         if(auth){
283             string dn = auth->getDN();
284             string passwd = auth->getPassword();
285             const char* c_dn=0;
286             struct berval c_passwd = { 0, 0 };
287             if(dn != ""){
288                 c_dn = dn.c_str();
289             }
290             if(passwd != ""){
291                 c_passwd.bv_val = const_cast<char*>(passwd.c_str());
292                 c_passwd.bv_len = passwd.size();
293             }
294             err = ldap_sasl_bind_s(tmpConn->getSessionHandle(), c_dn,
295                     LDAP_SASL_SIMPLE, &c_passwd, NULL, NULL, NULL);
296         } else {   
297             // Do anonymous bind
298             err = ldap_sasl_bind_s(tmpConn->getSessionHandle(),NULL,
299                     LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL);
300         }
301         if( err == LDAP_SUCCESS ){
302             usedUrl=conUrl;
303             return tmpConn;
304         }else{
305             delete tmpConn;
306             tmpConn=0;
307         }
308         auth=0;
309     }
310     return 0;
311 }
312