Back to index

openldap  2.4.31
LDAPConnection.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 #include "debug.h"
00008 
00009 #include "LDAPResult.h"
00010 #include "LDAPException.h"
00011 #include "LDAPUrlList.h"
00012 
00013 #include "LDAPConnection.h"
00014 const int LDAPConnection::SEARCH_BASE = LDAPAsynConnection::SEARCH_BASE;
00015 const int LDAPConnection::SEARCH_ONE = LDAPAsynConnection::SEARCH_ONE;
00016 const int LDAPConnection::SEARCH_SUB = LDAPAsynConnection::SEARCH_SUB;
00017 
00018 using namespace std;
00019 
00020 LDAPConnection::LDAPConnection(const string& hostname, int port, 
00021         LDAPConstraints* cons) :
00022         LDAPAsynConnection(hostname, port, cons){
00023 }
00024 
00025 LDAPConnection::~LDAPConnection(){
00026 }
00027 
00028 void LDAPConnection::start_tls(){
00029     LDAPAsynConnection::start_tls();
00030 }
00031    
00032 void LDAPConnection::bind(const string& dn, const string& passwd,
00033         LDAPConstraints* cons){
00034     DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::bind" << endl);
00035     LDAPMessageQueue* msg=0;
00036     LDAPResult* res=0;
00037     try{
00038         msg = LDAPAsynConnection::bind(dn,passwd,cons);
00039         res = (LDAPResult*)msg->getNext();
00040     }catch(LDAPException e){
00041         delete msg;
00042         delete res;
00043         throw;
00044     }
00045     int resCode=res->getResultCode();
00046     if(resCode != LDAPResult::SUCCESS) {
00047         if(resCode == LDAPResult::REFERRAL){
00048             LDAPUrlList urls = res->getReferralUrls();
00049             delete res;
00050             delete msg;
00051             throw LDAPReferralException(urls);
00052         }else{
00053             string srvMsg = res->getErrMsg();
00054             delete res;
00055             delete msg;
00056             throw LDAPException(resCode, srvMsg);
00057         }
00058     }
00059     delete res;
00060     delete msg;   // memcheck
00061 }
00062 
00063 void LDAPConnection::saslInteractiveBind( const std::string &mech,
00064                         int flags,
00065                         SaslInteractionHandler *sih,
00066                         const LDAPConstraints *cons)
00067 {
00068     DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::bind" << endl);
00069     LDAPMessageQueue* msg=0;
00070     LDAPResult* res=0;
00071     try{
00072         msg = LDAPAsynConnection::saslInteractiveBind(mech, flags, sih, cons);
00073         res = (LDAPResult*)msg->getNext();
00074     }catch(LDAPException e){
00075         delete msg;
00076         delete res;
00077         throw;
00078     }
00079     int resCode=res->getResultCode();
00080     if(resCode != LDAPResult::SUCCESS) {
00081         if(resCode == LDAPResult::REFERRAL){
00082             LDAPUrlList urls = res->getReferralUrls();
00083             delete res;
00084             delete msg;
00085             throw LDAPReferralException(urls);
00086         }else{
00087             string srvMsg = res->getErrMsg();
00088             delete res;
00089             delete msg;
00090             throw LDAPException(resCode, srvMsg);
00091         }
00092     }
00093     delete res;
00094     delete msg;
00095 }
00096 
00097 void LDAPConnection::unbind(){
00098     LDAPAsynConnection::unbind();
00099 }
00100 
00101 bool LDAPConnection::compare(const string& dn, const LDAPAttribute& attr,
00102         LDAPConstraints* cons){
00103     DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::compare" << endl);
00104     LDAPMessageQueue* msg=0;
00105     LDAPResult* res=0;
00106     try{
00107         msg = LDAPAsynConnection::compare(dn,attr,cons);
00108         res = (LDAPResult*)msg->getNext();
00109     }catch(LDAPException e){
00110         delete msg;
00111         delete res;
00112         throw;
00113     }
00114     int resCode=res->getResultCode();
00115     switch (resCode){
00116         case LDAPResult::COMPARE_TRUE :
00117             delete res; 
00118             delete msg;
00119             return true;
00120         break;
00121         case LDAPResult::COMPARE_FALSE :
00122             delete res;
00123             delete msg;
00124             return false;
00125         break;
00126         case LDAPResult::REFERRAL :
00127         {
00128             LDAPUrlList urls = res->getReferralUrls();
00129             delete res;
00130             delete msg;
00131             throw LDAPReferralException(urls);
00132         }
00133         break;
00134         default :
00135             string srvMsg = res->getErrMsg();
00136             delete res;
00137             delete msg;
00138             throw LDAPException(resCode, srvMsg);
00139     }
00140 }
00141 
00142 void LDAPConnection::del(const string& dn, const LDAPConstraints* cons){
00143     DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::del" << endl);
00144     LDAPMessageQueue* msg=0;
00145     LDAPResult* res=0;
00146     try{
00147         msg = LDAPAsynConnection::del(dn,cons);
00148         res = (LDAPResult*)msg->getNext();
00149     }catch(LDAPException e){
00150         delete msg;
00151         delete res;
00152         throw;
00153     }
00154     int resCode=res->getResultCode();
00155     switch (resCode){
00156         case LDAPResult::SUCCESS :
00157             delete res; 
00158             delete msg;
00159         break;
00160         case LDAPResult::REFERRAL :
00161         {
00162             LDAPUrlList urls = res->getReferralUrls();
00163             delete res;
00164             delete msg;
00165             throw LDAPReferralException(urls);
00166         }
00167         break;
00168         default :
00169             string srvMsg = res->getErrMsg();
00170             delete res;
00171             delete msg;
00172             throw LDAPException(resCode, srvMsg);
00173     }
00174 
00175 }
00176 
00177 void LDAPConnection::add(const LDAPEntry* le, const LDAPConstraints* cons){
00178     DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::add" << endl);
00179     LDAPMessageQueue* msg=0;
00180     LDAPResult* res=0;
00181     try{
00182         msg = LDAPAsynConnection::add(le,cons);
00183         res = (LDAPResult*)msg->getNext();
00184     }catch(LDAPException e){
00185         delete msg;
00186         delete res;
00187         throw;
00188     }
00189     int resCode=res->getResultCode();
00190     switch (resCode){
00191         case LDAPResult::SUCCESS :
00192             delete res; 
00193             delete msg;
00194         break;
00195         case LDAPResult::REFERRAL :
00196         {
00197             LDAPUrlList urls = res->getReferralUrls();
00198             delete res;
00199             delete msg;
00200             throw LDAPReferralException(urls);
00201         }
00202         break;
00203         default :
00204             string srvMsg = res->getErrMsg();
00205             delete res;
00206             delete msg;
00207             throw LDAPException(resCode, srvMsg);
00208     }
00209 }
00210 
00211 void LDAPConnection::modify(const string& dn, const LDAPModList* mods,
00212         const LDAPConstraints* cons){
00213     DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::modify" << endl);
00214     LDAPMessageQueue* msg=0;
00215     LDAPResult* res=0;
00216     try{
00217         msg = LDAPAsynConnection::modify(dn,mods,cons);
00218         res = (LDAPResult*)msg->getNext();
00219     }catch(LDAPException e){
00220         delete msg;
00221         delete res;
00222         throw;
00223     }
00224     int resCode=res->getResultCode();
00225     switch (resCode){
00226         case LDAPResult::SUCCESS :
00227             delete res; 
00228             delete msg;
00229         break;
00230         case LDAPResult::REFERRAL :
00231         {
00232             LDAPUrlList urls = res->getReferralUrls();
00233             delete res;
00234             delete msg;
00235             throw LDAPReferralException(urls);
00236         }
00237         break;
00238         default :
00239             string srvMsg = res->getErrMsg();
00240             delete res;
00241             delete msg;
00242             throw LDAPException(resCode, srvMsg);
00243     }
00244     
00245 }
00246 
00247 void LDAPConnection::rename(const string& dn, const string& newRDN,
00248         bool delOldRDN, const string& newParentDN, 
00249         const LDAPConstraints* cons){
00250     DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::rename" << endl);
00251     LDAPMessageQueue* msg=0;
00252     LDAPResult* res=0;
00253     try{
00254         msg = LDAPAsynConnection::rename(dn,newRDN,delOldRDN, newParentDN,
00255                 cons);
00256         res = (LDAPResult*)msg->getNext();
00257     }catch(LDAPException e){
00258         delete msg;
00259         delete res;
00260         throw;
00261     }
00262     int resCode=res->getResultCode();
00263     switch (resCode){
00264         case LDAPResult::SUCCESS :
00265             delete res; 
00266             delete msg;
00267         break;
00268         case LDAPResult::REFERRAL :
00269         {
00270             LDAPUrlList urls = res->getReferralUrls();
00271             delete res;
00272             delete msg;
00273             throw LDAPReferralException(urls);
00274         }
00275         break;
00276         default :
00277             string srvMsg = res->getErrMsg();
00278             delete res;
00279             delete msg;
00280             throw LDAPException(resCode, srvMsg);
00281     }
00282 }
00283 
00284 LDAPSearchResults* LDAPConnection::search(const string& base, int scope,
00285         const string& filter, const StringList& attrs, bool attrsOnly, 
00286         const LDAPConstraints* cons){
00287     DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::search" << endl);
00288     LDAPMessageQueue* msgq=0;
00289     LDAPResult* res=0;
00290     LDAPSearchResults* results= 0;
00291     
00292     try{
00293         results = new LDAPSearchResults();
00294         msgq = LDAPAsynConnection::search(base,scope, filter, attrs, attrsOnly,
00295                 cons);
00296         res = results->readMessageQueue(msgq);
00297     }catch(LDAPException e){
00298         delete results; // memcheck
00299         delete msgq;
00300         throw;
00301     }
00302     if(res != 0){
00303         int resCode=res->getResultCode();
00304         switch (resCode){
00305             case LDAPResult::SUCCESS :
00306                 delete res; 
00307                 delete msgq;
00308                 return results;
00309             break;
00310             case LDAPResult::REFERRAL :
00311             {
00312                 LDAPUrlList urls = res->getReferralUrls();
00313                 delete results; // memcheck
00314                 delete res;
00315                 delete msgq;
00316                 throw LDAPReferralException(urls);
00317             }
00318             break;
00319             default :
00320                 string srvMsg = res->getErrMsg();
00321                 delete results; // memcheck
00322                 delete res;
00323                 delete msgq;
00324                 throw LDAPException(resCode, srvMsg);
00325         }
00326     }        
00327     return 0;
00328 }
00329 
00330 LDAPExtResult* LDAPConnection::extOperation(const string& oid, 
00331         const string& value, const LDAPConstraints *cons){
00332     DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::extOperation" << endl);
00333     LDAPMessageQueue* msg=0;
00334     LDAPExtResult* res=0;
00335     try{
00336         msg = LDAPAsynConnection::extOperation(oid,value,cons);
00337         res = (LDAPExtResult*)msg->getNext();
00338     }catch(LDAPException e){
00339         delete msg;
00340         delete res;
00341         throw;
00342     }
00343     int resCode=res->getResultCode();
00344     switch (resCode){
00345         case LDAPResult::SUCCESS :
00346             delete msg;
00347             return res;
00348         case LDAPResult::REFERRAL :
00349         {
00350             LDAPUrlList urls = res->getReferralUrls();
00351             delete res;
00352             delete msg;
00353             throw LDAPReferralException(urls);
00354         }
00355         break;
00356         default :
00357             string srvMsg = res->getErrMsg();
00358             delete res;
00359             delete msg;
00360             throw LDAPException(resCode, srvMsg);
00361     }
00362 }
00363 
00364 const string& LDAPConnection::getHost() const{
00365     return LDAPAsynConnection::getHost();
00366 }
00367 
00368 int LDAPConnection::getPort() const{
00369     return LDAPAsynConnection::getPort();
00370 }
00371 
00372 void LDAPConnection::setConstraints(LDAPConstraints* cons){
00373     LDAPAsynConnection::setConstraints(cons);
00374 }
00375 
00376 const LDAPConstraints* LDAPConnection::getConstraints() const{
00377     return LDAPAsynConnection::getConstraints();
00378 }
00379 
00380 TlsOptions LDAPConnection::getTlsOptions() const {
00381     return LDAPAsynConnection::getTlsOptions();
00382 }