Back to index

openldap  2.4.31
LDAPAsynConnection.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 
00008 #include "config.h"
00009 #include "debug.h"
00010 #include "LDAPAsynConnection.h"
00011 
00012 #include "LDAPAddRequest.h"
00013 #include "LDAPBindRequest.h"
00014 #include "LDAPCompareRequest.h"
00015 #include "LDAPDeleteRequest.h"
00016 #include "LDAPExtRequest.h"
00017 #include "LDAPEntry.h"
00018 #include "LDAPModDNRequest.h"
00019 #include "LDAPModifyRequest.h"
00020 #include "LDAPRequest.h"
00021 #include "LDAPRebind.h"
00022 #include "LDAPRebindAuth.h"
00023 #include "LDAPSearchRequest.h"
00024 #include <lber.h>
00025 #include <sstream>
00026 
00027 using namespace std;
00028 
00029 LDAPAsynConnection::LDAPAsynConnection(const string& url, int port,
00030                                LDAPConstraints *cons ){
00031     DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPAsynConnection::LDAPAsynConnection()"
00032             << endl);
00033     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
00034             "   URL:" << url << endl << "   port:" << port << endl);
00035     cur_session=0;
00036     m_constr = 0;
00037     // Is this an LDAP URI?
00038     if ( url.find("://") == std::string::npos ) {
00039        this->init(url, port);
00040     } else {
00041        this->initialize(url);
00042     }
00043     this->setConstraints(cons);
00044 }
00045 
00046 LDAPAsynConnection::~LDAPAsynConnection(){}
00047 
00048 void LDAPAsynConnection::init(const string& hostname, int port){
00049     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::init" << endl);
00050     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
00051             "   hostname:" << hostname << endl
00052             << "   port:" << port << endl);
00053 
00054     m_uri.setScheme("ldap");
00055     m_uri.setHost(hostname);
00056     m_uri.setPort(port);
00057     
00058     const char *ldapuri = m_uri.getURLString().c_str();
00059     int ret = ldap_initialize(&cur_session, ldapuri);
00060     if ( ret != LDAP_SUCCESS ) {
00061         throw LDAPException( ret );
00062     }
00063     int opt=3;
00064     ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
00065     ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
00066 }
00067 
00068 void LDAPAsynConnection::initialize(const std::string& uri){
00069        m_uri.setURLString(uri);
00070     int ret = ldap_initialize(&cur_session, m_uri.getURLString().c_str());
00071     if ( ret != LDAP_SUCCESS ) {
00072         throw LDAPException( ret );
00073     }
00074     int opt=3;
00075     ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
00076     ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
00077 }
00078 
00079 void LDAPAsynConnection::start_tls(){
00080     int ret = ldap_start_tls_s( cur_session, NULL, NULL );
00081     if( ret != LDAP_SUCCESS ) {
00082         throw LDAPException(this);
00083     }
00084 }
00085 
00086 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
00087         const string& passwd, const LDAPConstraints *cons){
00088     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" <<  endl);
00089     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, "   dn:" << dn << endl
00090                << "   passwd:" << passwd << endl);
00091     LDAPBindRequest *req = new LDAPBindRequest(dn,passwd,this,cons);
00092     try{
00093         LDAPMessageQueue *ret = req->sendRequest();
00094         return ret;
00095     }catch(LDAPException e){
00096         delete req;
00097         throw;
00098     }
00099 }
00100 
00101 LDAPMessageQueue* LDAPAsynConnection::saslBind(const std::string &mech,
00102               const std::string &cred,
00103               const LDAPConstraints *cons)
00104 {
00105     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::saslBind()" <<  endl);
00106     LDAPSaslBindRequest *req = new LDAPSaslBindRequest(mech, cred, this, cons);
00107     try{
00108         LDAPMessageQueue *ret = req->sendRequest();
00109         return ret;
00110     }catch(LDAPException e){
00111         delete req;
00112         throw;
00113     }
00114 
00115 }
00116 
00117 LDAPMessageQueue* LDAPAsynConnection::saslInteractiveBind(
00118                         const std::string &mech,
00119                         int flags,
00120                         SaslInteractionHandler *sih,
00121                         const LDAPConstraints *cons)
00122 {
00123     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::saslInteractiveBind" 
00124             << std::endl);
00125     LDAPSaslInteractiveBind *req = 
00126             new LDAPSaslInteractiveBind(mech, flags, sih, this, cons);
00127     try {
00128         LDAPMessageQueue *ret = req->sendRequest();
00129         return ret;
00130     }catch(LDAPException e){
00131         delete req;
00132         throw;
00133     } 
00134 }
00135 
00136 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope, 
00137                                          const string& filter, 
00138                                          const StringList& attrs, 
00139                                          bool attrsOnly,
00140                                          const LDAPConstraints *cons){
00141     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::search()" <<  endl);
00142     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER, "   base:" << base << endl
00143                << "   scope:" << scope << endl
00144                << "   filter:" << filter << endl );
00145     LDAPSearchRequest *req = new LDAPSearchRequest(base, scope,filter, attrs, 
00146             attrsOnly, this, cons);
00147     try{
00148         LDAPMessageQueue *ret = req->sendRequest();
00149         return ret;
00150     }catch(LDAPException e){
00151         delete req;
00152         throw;
00153     }
00154 }
00155 
00156 LDAPMessageQueue* LDAPAsynConnection::del(const string& dn, 
00157         const LDAPConstraints *cons){
00158     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::del()" << endl);
00159     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl);
00160     LDAPDeleteRequest *req = new LDAPDeleteRequest(dn, this, cons);
00161     try{
00162         LDAPMessageQueue *ret = req->sendRequest();
00163         return ret;
00164     }catch(LDAPException e){
00165         delete req;
00166         throw;
00167     }
00168 }
00169 
00170 LDAPMessageQueue* LDAPAsynConnection::compare(const string& dn, 
00171         const LDAPAttribute& attr, const LDAPConstraints *cons){
00172     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::compare()" << endl);
00173     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl
00174             << "   attr:" << attr << endl);
00175     LDAPCompareRequest *req = new LDAPCompareRequest(dn, attr, this, cons);
00176     try{
00177         LDAPMessageQueue *ret = req->sendRequest();
00178         return ret;
00179     }catch(LDAPException e){
00180         delete req;
00181         throw;
00182     }
00183 }
00184 
00185 LDAPMessageQueue* LDAPAsynConnection::add( const LDAPEntry* le, 
00186         const LDAPConstraints *cons){
00187     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::add()" << endl);
00188     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   entry:" << *le << endl);
00189     LDAPAddRequest *req = new LDAPAddRequest(le, this, cons);
00190     try{
00191         LDAPMessageQueue *ret = req->sendRequest();
00192         return ret;
00193     }catch(LDAPException e){
00194         delete req;
00195         throw;
00196     }
00197 }
00198 
00199 LDAPMessageQueue* LDAPAsynConnection::modify(const string& dn,
00200         const LDAPModList *mod, const LDAPConstraints *cons){
00201     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::modify()" << endl);
00202     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl);
00203     LDAPModifyRequest *req = new LDAPModifyRequest(dn, mod, this, cons);
00204     try{
00205         LDAPMessageQueue *ret = req->sendRequest();
00206         return ret;
00207     }catch(LDAPException e){
00208         delete req;
00209         throw;
00210     }
00211 }
00212 
00213 LDAPMessageQueue* LDAPAsynConnection::rename(const string& dn, 
00214         const string& newRDN, bool delOldRDN, const string& newParentDN,
00215         const LDAPConstraints *cons ){
00216     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::rename()" << endl);
00217     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   dn:" << dn << endl
00218             << "   newRDN:" << newRDN << endl
00219             << "   newParentDN:" << newParentDN << endl
00220             << "   delOldRDN:" << delOldRDN << endl);
00221     LDAPModDNRequest *req = new  LDAPModDNRequest(dn, newRDN, delOldRDN, 
00222             newParentDN, this, cons );
00223     try{
00224         LDAPMessageQueue *ret = req->sendRequest();
00225         return ret;
00226     }catch(LDAPException e){
00227         delete req;
00228         throw;
00229     }
00230 }
00231 
00232 
00233 LDAPMessageQueue* LDAPAsynConnection::extOperation(const string& oid, 
00234         const string& value, const LDAPConstraints *cons ){
00235     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::extOperation()" << endl);
00236     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,"   oid:" << oid << endl);
00237     LDAPExtRequest *req = new  LDAPExtRequest(oid, value, this,cons);
00238     try{
00239         LDAPMessageQueue *ret = req->sendRequest();
00240         return ret;
00241     }catch(LDAPException e){
00242         delete req;
00243         throw;
00244     }
00245 }
00246 
00247 
00248 void LDAPAsynConnection::abandon(LDAPMessageQueue *q){
00249     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::abandon()" << endl);
00250     LDAPRequestStack *reqStack=q->getRequestStack();
00251     LDAPRequest *req;
00252     while(! reqStack->empty()){
00253         req=reqStack->top();
00254         if (ldap_abandon_ext(cur_session, req->getMsgID(), 0, 0) 
00255                 != LDAP_SUCCESS){
00256             throw LDAPException(this);
00257         }
00258         delete req;
00259         reqStack->pop();
00260     }
00261 }
00262 
00263 void LDAPAsynConnection::unbind(){
00264     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::unbind()" << endl);
00265     if(cur_session){
00266         LDAPControl** tmpSrvCtrls=m_constr->getSrvCtrlsArray();
00267         LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
00268         int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
00269         cur_session=0;
00270         LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
00271         LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
00272         if(err != LDAP_SUCCESS){
00273             throw LDAPException(err);
00274         }
00275     }
00276 }
00277 
00278 void LDAPAsynConnection::setConstraints(LDAPConstraints *cons){
00279     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setConstraints()" << endl);
00280     m_constr=cons;
00281 }
00282 
00283 const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
00284     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getConstraints()" << endl);
00285     return m_constr;
00286 }
00287  
00288 TlsOptions LDAPAsynConnection::getTlsOptions() const {
00289     return TlsOptions( cur_session );
00290 }
00291 
00292 LDAP* LDAPAsynConnection::getSessionHandle() const{ 
00293     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
00294     return cur_session;
00295 }
00296 
00297 const string& LDAPAsynConnection::getHost() const{
00298     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
00299     return m_uri.getHost();
00300 }
00301 
00302 int LDAPAsynConnection::getPort() const{
00303     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
00304     return m_uri.getPort();
00305 }
00306 
00307 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
00308         const LDAPUrlList& urls, LDAPUrlList::const_iterator& usedUrl,
00309         const LDAPConstraints* cons) const {
00310     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::referralConnect()" << endl)
00311     LDAPUrlList::const_iterator conUrl;
00312     LDAPAsynConnection* tmpConn=0;
00313     const LDAPRebind* rebind = cons->getReferralRebind();
00314     LDAPRebindAuth* auth = 0;
00315 
00316     for(conUrl=urls.begin(); conUrl!=urls.end(); conUrl++){
00317         string host= conUrl->getHost();
00318         int port= conUrl->getPort();
00319         DEBUG(LDAP_DEBUG_TRACE,"   connecting to: " << host << ":" <<
00320                 port << endl);
00321         //Set the new connection's constraints-object ?
00322         tmpConn=new LDAPAsynConnection(host.c_str(),port);
00323         int err=0;
00324 
00325         if(rebind){ 
00326             auth=rebind->getRebindAuth(host, port);
00327         }
00328         if(auth){
00329             string dn = auth->getDN();
00330             string passwd = auth->getPassword();
00331             const char* c_dn=0;
00332             struct berval c_passwd = { 0, 0 };
00333             if(dn != ""){
00334                 c_dn = dn.c_str();
00335             }
00336             if(passwd != ""){
00337                 c_passwd.bv_val = const_cast<char*>(passwd.c_str());
00338                 c_passwd.bv_len = passwd.size();
00339             }
00340             err = ldap_sasl_bind_s(tmpConn->getSessionHandle(), c_dn,
00341                     LDAP_SASL_SIMPLE, &c_passwd, NULL, NULL, NULL);
00342         } else {   
00343             // Do anonymous bind
00344             err = ldap_sasl_bind_s(tmpConn->getSessionHandle(),NULL,
00345                     LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL);
00346         }
00347         if( err == LDAP_SUCCESS ){
00348             usedUrl=conUrl;
00349             return tmpConn;
00350         }else{
00351             delete tmpConn;
00352             tmpConn=0;
00353         }
00354         auth=0;
00355     }
00356     return 0;
00357 }
00358