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