]> 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 "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     if( ldap_start_tls_s( cur_session, NULL, NULL ) != LDAP_SUCCESS ) {
76         throw LDAPException(this);
77     }
78 }
79
80 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
81         const string& passwd, const LDAPConstraints *cons){
82     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" <<  endl);
83     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, "   dn:" << dn << endl
84                << "   passwd:" << passwd << endl);
85     LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,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::search(const string& base,int scope, 
96                                          const string& filter, 
97                                          const StringList& attrs, 
98                                          bool attrsOnly,
99                                          const LDAPConstraints *cons){
100     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" <<  endl);
101     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, "   base:" << base << endl
102                << "   scope:" << scope << endl
103                << "   filter:" << filter << endl );
104     LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs, 
105             attrsOnly, 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::del(const string& dn, 
116         const LDAPConstraints *cons){
117     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
118     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl);
119     LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
120     try{
121         LDAPMessageQueue *ret = req->sendRequest();
122         return ret;
123     }catch(LDAPException e){
124         delete req;
125         throw;
126     }
127 }
128
129 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn, 
130         const LDAPAttribute& attr, const LDAPConstraints *cons){
131     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
132     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl
133             << "   attr:" << attr << endl);
134     LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, 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::add( const LDAPEntry* le, 
145         const LDAPConstraints *cons){
146     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
147     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   entry:" << *le << endl);
148     LDAPAddRequest *req = new LDAPAddRequest(le, 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::modify(const string& dn,
159         const LDAPModList *mod, const LDAPConstraints *cons){
160     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
161     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl);
162     LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
163     try{
164         LDAPMessageQueue *ret = req->sendRequest();
165         return ret;
166     }catch(LDAPException e){
167         delete req;
168         throw;
169     }
170 }
171
172 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn, 
173         const string& newRDN, bool delOldRDN, const string& newParentDN,
174         const LDAPConstraints *cons ){
175     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
176     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl
177             << "   newRDN:" << newRDN << endl
178             << "   newParentDN:" << newParentDN << endl
179             << "   delOldRDN:" << delOldRDN << endl);
180     LDAPModDNRequest *req = new  LDAPModDNRequest(dn, newRDN, delOldRDN, 
181             newParentDN, this, cons );
182     try{
183         LDAPMessageQueue *ret = req->sendRequest();
184         return ret;
185     }catch(LDAPException e){
186         delete req;
187         throw;
188     }
189 }
190
191
192 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid, 
193         const string& value, const LDAPConstraints *cons ){
194     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
195     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   oid:" << oid << endl);
196     LDAPExtRequest *req = new  LDAPExtRequest(oid, value, this,cons);
197     try{
198         LDAPMessageQueue *ret = req->sendRequest();
199         return ret;
200     }catch(LDAPException e){
201         delete req;
202         throw;
203     }
204 }
205
206
207 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
208     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
209     LDAPRequestStack *reqStack=q->getRequestStack();
210     LDAPRequest *req;
211     while(! reqStack->empty()){
212         req=reqStack->top();
213         if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0) 
214                 != LDAP_SUCCESS){
215             throw LDAPException(this);
216         }
217         delete req;
218         reqStack->pop();
219     }
220 }
221
222 void LDAPAsynConnection::unbind(){
223     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
224     if(cur_session){
225         LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
226         LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
227         int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
228         cur_session=0;
229         LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
230         LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
231         if(err != LDAP_SUCCESS){
232             throw LDAPException(err);
233         }
234     }
235 }
236
237 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
238     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
239     m_constr=cons;
240 }
241
242 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
243     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
244     return m_constr;
245 }
246  
247 LDAP* LDAPAsynConnection::getSessionHandle() const{ 
248     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
249     return cur_session;
250 }
251
252 const string& LDAPAsynConnection::getHost() const{
253     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
254     return m_host;
255 }
256
257 int LDAPAsynConnection::getPort() const{
258     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
259     return m_port;
260 }
261
262 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
263         const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
264         const LDAPConstraints* cons) const {
265     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
266     LDAPUrlList::const_iterator conUrl;
267     LDAPAsynConnection* tmpConn=0;
268     const LDAPRebind* rebind = cons->getReferralRebind();
269     LDAPRebindAuth* auth = 0;
270
271     for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
272         string host= conUrl->getHost();
273         int port= conUrl->getPort();
274         DEBUG(LDAP_DEBUG_TRACE,"   connecting to: " << host << ":" <<
275                 port << endl);
276         //Set the new connection's constraints-object ?
277         tmpConn=new LDAPAsynConnection(host.c_str(),port);
278         int err=0;
279
280         if(rebind){ 
281             auth=rebind->getRebindAuth(host, port);
282         }
283         if(auth){
284             string dn = auth->getDN();
285             string passwd = auth->getPassword();
286             const char* c_dn=0;
287             struct berval c_passwd = { 0, 0 };
288             if(dn != ""){
289                 c_dn = dn.c_str();
290             }
291             if(passwd != ""){
292                 c_passwd.bv_val = const_cast<char*>(passwd.c_str());
293                 c_passwd.bv_len = passwd.size();
294             }
295             err = ldap_sasl_bind_s(tmpConn->getSessionHandle(), c_dn,
296                     LDAP_SASL_SIMPLE, &c_passwd, NULL, NULL, NULL);
297         } else {   
298             // Do anonymous bind
299             err = ldap_sasl_bind_s(tmpConn->getSessionHandle(),NULL,
300                     LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL);
301         }
302         if( err == LDAP_SUCCESS ){
303             usedUrl=conUrl;
304             return tmpConn;
305         }else{
306             delete tmpConn;
307             tmpConn=0;
308         }
309         auth=0;
310     }
311     return 0;
312 }
313