]> git.sur5r.net Git - openldap/blobdiff - contrib/ldapc++/src/TlsOptions.cpp
Merge remote branch 'origin/mdb.master'
[openldap] / contrib / ldapc++ / src / TlsOptions.cpp
index 0cba46ea18128c857fad75dafdeb384cd600cc4a..5d936e9400fd5b87a67b3205ca0bb6f8a0e130d8 100644 (file)
@@ -1,6 +1,6 @@
 // $OpenLDAP$
 /*
- * Copyright 2010, OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 2010-2012 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 
@@ -38,8 +38,8 @@ static tls_optmap_t optmap[] = {
         static const int TLS_CONNECT_ARG
 #endif 
 
-void checkOpt( TlsOptions::tls_option opt, opttype type ){
-    if ( opt >= sizeof(optmap) ){
+static void checkOpt( TlsOptions::tls_option opt, opttype type ) {
+    if ( opt < TlsOptions::CACERTFILE || opt >= TlsOptions::LASTOPT ){
         throw( LDAPException( LDAP_PARAM_ERROR, "unknown Option" ) );
     }
 
@@ -48,19 +48,21 @@ void checkOpt( TlsOptions::tls_option opt, opttype type ){
     }
 }
 
+TlsOptions::TlsOptions() : m_ld(NULL) {}
+
 TlsOptions::TlsOptions( LDAP* ld ): m_ld(ld) { }
 
-void TlsOptions::setOption( tls_option opt, const std::string& value ) {
+void TlsOptions::setOption( tls_option opt, const std::string& value ) const {
     checkOpt(opt, STRING);
-    this->setOption( opt, (void*) value.c_str());
+    this->setOption( opt, value.empty() ? NULL : (void*) value.c_str() );
 }
 
-void TlsOptions::setOption( tls_option opt, int value ) {
+void TlsOptions::setOption( tls_option opt, int value ) const {
     checkOpt(opt, INT);
     this->setOption( opt, (void*) &value);
 }
 
-void TlsOptions::setOption( tls_option opt, void *value ) {
+void TlsOptions::setOption( tls_option opt, void *value ) const {
     int ret = ldap_set_option( m_ld, optmap[opt].optval, value);
     if ( ret != LDAP_OPT_SUCCESS )
     {
@@ -70,9 +72,10 @@ void TlsOptions::setOption( tls_option opt, void *value ) {
             throw( LDAPException( LDAP_PARAM_ERROR, "error while setting TLS option" ) );
         }
     }
+    this->newCtx();
 }
 
-void TlsOptions::getOption( tls_option opt, void* value ){
+void TlsOptions::getOption( tls_option opt, void* value ) const {
     int ret = ldap_get_option( m_ld, optmap[opt].optval, value);
     if ( ret != LDAP_OPT_SUCCESS )
     {
@@ -104,3 +107,15 @@ std::string TlsOptions::getStringOption( tls_option opt ) const {
     return strval;
 }
 
+void TlsOptions::newCtx() const {
+    int val = 0;
+    int ret = ldap_set_option( m_ld, LDAP_OPT_X_TLS_NEWCTX, &val);
+    if ( ret != LDAP_OPT_SUCCESS )
+    {
+        if ( ret != LDAP_OPT_ERROR ){
+            throw( LDAPException( ret ));
+        } else {
+            throw( LDAPException( LDAP_LOCAL_ERROR, "error while renewing TLS context" ) );
+        }
+    }
+}