2007-09-05 [colin] 3.0.0cvs7
[claws.git] / src / ldaputil.c
index 27ec13904f8060704022d3a1232ea87edb04ee10..6186640906bd314f08f3d12b02f985ea3fb37420 100644 (file)
@@ -4,7 +4,7 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 /*
@@ -33,6 +33,7 @@
 #include <ldap.h>
 #include <lber.h>
 #include <errno.h>
+#include "common/utils.h"
 
 #define SYLDAP_TEST_FILTER   "(objectclass=*)"
 #define SYLDAP_SEARCHBASE_V2 "cn=config"
@@ -192,6 +193,23 @@ static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
        return baseDN;
 }
 
+int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
+{
+       struct berval cred;
+
+       if ( passwd != NULL ) {
+               cred.bv_val = (char *) passwd;
+               cred.bv_len = strlen( passwd );
+       } else {
+               cred.bv_val = "";
+               cred.bv_len = 0;
+       }
+
+       debug_print("binding: DN->%s\n", dn?dn:"null");
+       return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
+               NULL, NULL, NULL );
+}
+
 /**
  * Attempt to discover the base DN for the server.
  * \param  host   Host name.
@@ -209,38 +227,46 @@ GList *ldaputil_read_basedn(
        GList *baseDN = NULL;
        LDAP *ld = NULL;
        gint rc;
-#ifdef USE_LDAP_TLS
+       gchar *uri = NULL;
        gint version;
-#endif
 
        if( host == NULL ) return baseDN;
        if( port < 1 ) return baseDN;
 
        /* Connect to server. */
 
-       if (!ssl) {
-               ld = ldap_init( host, port );
-       } else {
-               gchar *uri = g_strdup_printf("ldaps://%s:%d",
-                               host, port);
-               rc = ldap_initialize(&ld, uri);
-               g_free(uri);
-       }
+       ldapsrv_set_options (tov, NULL);
+
+       uri = g_strdup_printf("ldap%s://%s:%d",
+                       ssl?"s":"",
+                       host, port);
+       debug_print("URI: %s\n", uri);
+       rc = ldap_initialize(&ld, uri);
+       g_free(uri);
+       
        if( ld == NULL ) {
                return baseDN;
        }
+
+       if ((bindDN && *bindDN)
 #ifdef USE_LDAP_TLS
-       if( tls && !ssl ) {
-               /* Handle TLS */
+          || (tls && !ssl)
+#endif                 
+       ) {
                version = LDAP_VERSION3;
                rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
+       }
+#ifdef USE_LDAP_TLS
+       
+       if( tls && !ssl ) {
+               /* Handle TLS */
                if( rc != LDAP_OPT_SUCCESS ) {
-                       ldap_unbind( ld );
+                       ldap_unbind_ext( ld, NULL, NULL );
                        return baseDN;
                }
                rc = ldap_start_tls_s( ld, NULL, NULL );
                if (rc != 0) {
-                       ldap_unbind( ld );
+                       ldap_unbind_ext( ld, NULL, NULL );
                        return baseDN;
                }
        }
@@ -249,9 +275,10 @@ GList *ldaputil_read_basedn(
        /* Bind to the server, if required */
        if( bindDN ) {
                if( *bindDN != '\0' ) {
-                       rc = ldap_simple_bind_s( ld, bindDN, bindPW );
+                       rc = claws_ldap_simple_bind_s( ld, bindDN, bindPW );
                        if( rc != LDAP_SUCCESS ) {
-                               ldap_unbind( ld );
+                               g_printerr("LDAP: %s\n", ldap_err2string(rc));
+                               ldap_unbind_ext( ld, NULL, NULL );
                                return baseDN;
                        }
                }
@@ -259,12 +286,18 @@ GList *ldaputil_read_basedn(
 
        /* Test for LDAP version 3 */
        baseDN = ldaputil_test_v3( ld, tov, &rc );
+       if (baseDN) {
+               debug_print("Using LDAP v3\n");
+       }
 
        if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
                baseDN = ldaputil_test_v2( ld, tov );
+               if (baseDN) {
+                       debug_print("Using LDAP v2\n");
+               }
        }
        if (ld && !LDAP_API_ERROR(rc))
-               ldap_unbind( ld );
+               ldap_unbind_ext( ld, NULL, NULL );
        
        return baseDN;
 }
@@ -276,29 +309,31 @@ GList *ldaputil_read_basedn(
  * \param  port Port number.
  * \return <i>TRUE</i> if connected successfully.
  */
-gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls ) {
+gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls, int secs ) {
        gboolean retVal = FALSE;
        LDAP *ld;
 #ifdef USE_LDAP_TLS
        gint rc;
        gint version;
 #endif
+       gchar *uri = NULL;
+
        if( host == NULL ) return retVal;
        if( port < 1 ) return retVal;
-       if (!ssl) {
-               ld = ldap_open( host, port );
-       } else {
-               gchar *uri = g_strdup_printf("ldaps://%s:%d",
+       
+       ldapsrv_set_options (secs, NULL);
+       uri = g_strdup_printf("ldap%s://%s:%d",
+                               ssl?"s":"",
                                host, port);
-               ldap_initialize(&ld, uri);
-               g_free(uri);
-       }
+       debug_print("URI: %s\n", uri);
+       ldap_initialize(&ld, uri);
+       g_free(uri);
        if (ld == NULL)
                return FALSE;
 
 #ifdef USE_LDAP_TLS
        if (ssl) {
-               GList *dummy = ldaputil_test_v3( ld, 10, &rc );
+               GList *dummy = ldaputil_test_v3( ld, secs, &rc );
                if (dummy)
                        g_list_free(dummy);
                if (LDAP_API_ERROR(rc))
@@ -310,19 +345,20 @@ gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int
                version = LDAP_VERSION3;
                rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
                if( rc != LDAP_OPT_SUCCESS ) {
-                       ldap_unbind( ld );
+                       ldap_unbind_ext( ld, NULL, NULL );
                        return FALSE;
                }
 
                rc = ldap_start_tls_s( ld, NULL, NULL );
                if (rc != 0) {
-                       ldap_unbind( ld );
+                       ldap_unbind_ext( ld, NULL, NULL );
                        return FALSE;
                }
        }
 #endif
        if( ld != NULL ) {
-               ldap_unbind( ld );
+               ldap_unbind_ext( ld, NULL, NULL );
+               debug_print("ld != NULL\n");
                retVal = TRUE;
        }
        return retVal;