]> git.sur5r.net Git - openldap/blobdiff - contrib/ldapc++/src/LDAPAsynConnection.cpp
Merge remote branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / contrib / ldapc++ / src / LDAPAsynConnection.cpp
index d422dff332eeb08329386f176eeb1f6e2a18d71a..5a1d49a06a0e565f66058e0fdc3821ec85e8b91d 100644 (file)
@@ -1,5 +1,6 @@
+// $OpenLDAP$
 /*
- * Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 
@@ -12,7 +13,6 @@
 #include "LDAPBindRequest.h"
 #include "LDAPCompareRequest.h"
 #include "LDAPDeleteRequest.h"
-#include "LDAPException.h"
 #include "LDAPExtRequest.h"
 #include "LDAPEntry.h"
 #include "LDAPModDNRequest.h"
 #include "LDAPRebind.h"
 #include "LDAPRebindAuth.h"
 #include "LDAPSearchRequest.h"
+#include <lber.h>
+#include <sstream>
 
-LDAPAsynConnection::LDAPAsynConnection(const string& hostname, int port,
+using namespace std;
+
+LDAPAsynConnection::LDAPAsynConnection(const string& url, int port,
                                LDAPConstraints *cons ){
     DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPAsynConnection::LDAPAsynConnection()"
             << endl);
     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
-            "   host:" << hostname << endl << "   port:" << port << endl);
+            "   URL:" << url << endl << "   port:" << port << endl);
     cur_session=0;
-    this->init(hostname, port);
+    m_constr = 0;
+    // Is this an LDAP URI?
+    if ( url.find("://") == std::string::npos ) {
+       this->init(url, port);
+    } else {
+       this->initialize(url);
+    }
     this->setConstraints(cons);
 }
 
-LDAPAsynConnection::~LDAPAsynConnection(){
-    DEBUG(LDAP_DEBUG_DESTROY,
-            "LDAPAsynConnection::~LDAPAsynConnection()" << endl);
-    delete m_constr;        
-    unbind();
-}
+LDAPAsynConnection::~LDAPAsynConnection(){}
 
 void LDAPAsynConnection::init(const string& hostname, int port){
     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::init" << endl);
     DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER,
             "   hostname:" << hostname << endl
             << "   port:" << port << endl);
-    cur_session=ldap_init(hostname.c_str(),port);
-    m_host=hostname;
-    m_port=port;
+
+    m_uri.setScheme("ldap");
+    m_uri.setHost(hostname);
+    m_uri.setPort(port);
+    
+    const char *ldapuri = m_uri.getURLString().c_str();
+    int ret = ldap_initialize(&cur_session, ldapuri);
+    if ( ret != LDAP_SUCCESS ) {
+        throw LDAPException( ret );
+    }
     int opt=3;
     ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
     ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
 }
 
+void LDAPAsynConnection::initialize(const std::string& uri){
+       m_uri.setURLString(uri);
+    int ret = ldap_initialize(&cur_session, m_uri.getURLString().c_str());
+    if ( ret != LDAP_SUCCESS ) {
+        throw LDAPException( ret );
+    }
+    int opt=3;
+    ldap_set_option(cur_session, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
+    ldap_set_option(cur_session, LDAP_OPT_PROTOCOL_VERSION, &opt);
+}
+
+void LDAPAsynConnection::start_tls(){
+    int ret = ldap_start_tls_s( cur_session, NULL, NULL );
+    if( ret != LDAP_SUCCESS ) {
+        throw LDAPException(this);
+    }
+}
+
 LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
         const string& passwd, const LDAPConstraints *cons){
     DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::bind()" <<  endl);
@@ -68,6 +98,41 @@ LDAPMessageQueue* LDAPAsynConnection::bind(const string& dn,
     }
 }
 
+LDAPMessageQueue* LDAPAsynConnection::saslBind(const std::string &mech,
+               const std::string &cred,
+               const LDAPConstraints *cons)
+{
+    DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::saslBind()" <<  endl);
+    LDAPSaslBindRequest *req = new LDAPSaslBindRequest(mech, cred, this, cons);
+    try{
+        LDAPMessageQueue *ret = req->sendRequest();
+        return ret;
+    }catch(LDAPException e){
+        delete req;
+        throw;
+    }
+
+}
+
+LDAPMessageQueue* LDAPAsynConnection::saslInteractiveBind(
+                        const std::string &mech,
+                        int flags,
+                        SaslInteractionHandler *sih,
+                        const LDAPConstraints *cons)
+{
+    DEBUG(LDAP_DEBUG_TRACE, "LDAPAsynConnection::saslInteractiveBind" 
+            << std::endl);
+    LDAPSaslInteractiveBind *req = 
+            new LDAPSaslInteractiveBind(mech, flags, sih, this, cons);
+    try {
+        LDAPMessageQueue *ret = req->sendRequest();
+        return ret;
+    }catch(LDAPException e){
+        delete req;
+        throw;
+    } 
+}
+
 LDAPMessageQueue* LDAPAsynConnection::search(const string& base,int scope, 
                                          const string& filter, 
                                          const StringList& attrs, 
@@ -202,8 +267,8 @@ void LDAPAsynConnection::unbind(){
         LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
         int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
         cur_session=0;
-        ldap_controls_free(tmpSrvCtrls);
-        ldap_controls_free(tmpClCtrls);
+        LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
+        LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
         if(err != LDAP_SUCCESS){
             throw LDAPException(err);
         }
@@ -220,6 +285,10 @@ const LDAPConstraints* LDAPAsynConnection::getConstraints() const {
     return m_constr;
 }
  
+TlsOptions LDAPAsynConnection::getTlsOptions() const {
+    return TlsOptions( cur_session );
+}
+
 LDAP* LDAPAsynConnection::getSessionHandle() const{ 
     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getSessionHandle()" << endl);
     return cur_session;
@@ -227,12 +296,12 @@ LDAP* LDAPAsynConnection::getSessionHandle() const{
 
 const string& LDAPAsynConnection::getHost() const{
     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::setHost()" << endl);
-    return m_host;
+    return m_uri.getHost();
 }
 
 int LDAPAsynConnection::getPort() const{
     DEBUG(LDAP_DEBUG_TRACE,"LDAPAsynConnection::getPort()" << endl);
-    return m_port;
+    return m_uri.getPort();
 }
 
 LDAPAsynConnection* LDAPAsynConnection::referralConnect(
@@ -260,18 +329,20 @@ LDAPAsynConnection* LDAPAsynConnection::referralConnect(
             string dn = auth->getDN();
             string passwd = auth->getPassword();
             const char* c_dn=0;
-            const char* c_passwd=0;
+            struct berval c_passwd = { 0, 0 };
             if(dn != ""){
                 c_dn = dn.c_str();
             }
             if(passwd != ""){
-                c_passwd = passwd.c_str();
+                c_passwd.bv_val = const_cast<char*>(passwd.c_str());
+                c_passwd.bv_len = passwd.size();
             }
-            err = ldap_simple_bind_s(tmpConn->getSessionHandle(), c_dn,
-                    c_passwd);
+            err = ldap_sasl_bind_s(tmpConn->getSessionHandle(), c_dn,
+                    LDAP_SASL_SIMPLE, &c_passwd, NULL, NULL, NULL);
         } else {   
             // Do anonymous bind
-            err = ldap_simple_bind_s(tmpConn->getSessionHandle(), 0,0);
+            err = ldap_sasl_bind_s(tmpConn->getSessionHandle(),NULL,
+                    LDAP_SASL_SIMPLE, NULL, NULL, NULL, NULL);
         }
         if( err == LDAP_SUCCESS ){
             usedUrl=conUrl;