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