Fix more compile warnings for Windows in socket.c.
[claws.git] / src / ldaputil.c
index 76f9fa7b5024f71732e7062a59dc52939ea243d6..21e5d97d734e85dd02d74287f2ff14645d51dbcf 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2003-2007 Match Grun and the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2003-2015 Match Grun and the Claws Mail team
  *
  * 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
@@ -14,7 +14,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 /*
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef USE_LDAP
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <string.h>
 #include <sys/time.h>
-#include <ldap.h>
-#include <lber.h>
 #include <errno.h>
 #include "common/utils.h"
+#include "ldaputil.h"
+#include "ldapserver.h"
+#include "ldapctrl.h"
 
 #define SYLDAP_TEST_FILTER   "(objectclass=*)"
 #define SYLDAP_SEARCHBASE_V2 "cn=config"
@@ -104,7 +106,9 @@ static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
                        }
                        ber = NULL;
                }
-       } 
+       } else
+               debug_print("LDAP: Error %d (%s)\n", rc, ldaputil_get_error(ld));
+       
        if (errcode)
                *errcode = rc;
        if (result)
@@ -169,8 +173,7 @@ static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
                                                        ch = ( char * ) strchr( tmp, ':' );
                                                        if( ch ) {
                                                                gchar *bn = g_strdup( ++ch );
-                                                               g_strchomp( bn );
-                                                               g_strchug( bn );
+                                                               g_strstrip( bn );
                                                                baseDN = g_list_append(
                                                                        baseDN, g_strdup( bn ) );
                                                                g_free( bn );
@@ -195,6 +198,8 @@ static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
 
 int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
 {
+       debug_print("binding: DN->%s\n", dn?dn:"null");
+#ifdef G_OS_UNIX
        struct berval cred;
 
        if ( passwd != NULL ) {
@@ -205,9 +210,11 @@ int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *pa
                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 );
+#else
+       return ldap_simple_bind_s(ld, (PCHAR)dn, (PCHAR)passwd);
+#endif
 }
 
 /**
@@ -226,80 +233,43 @@ GList *ldaputil_read_basedn(
 {
        GList *baseDN = NULL;
        LDAP *ld = NULL;
+       LdapControl *ctl = ldapctl_create();
        gint rc;
-       gchar *uri = NULL;
-#ifdef USE_LDAP_TLS
-       gint version;
-#endif
-
-       if( host == NULL ) return baseDN;
-       if( port < 1 ) return baseDN;
-
-       /* Connect to server. */
-
-       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
-          || (tls && !ssl)
-#endif                 
-       ) {
-               version = LDAP_VERSION3;
-               rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
+       if( host == NULL ) 
+               return NULL;
+       if( port < 1 ) 
+               return NULL;
+
+       ldapctl_set_tls(ctl, tls);
+       ldapctl_set_ssl(ctl, ssl);
+       ldapctl_set_port(ctl, port);
+       ldapctl_set_host(ctl, host);
+       ldapctl_set_timeout(ctl, tov);
+       ldapctl_set_bind_dn(ctl, bindDN);
+
+       ld = ldapsvr_connect(ctl);
+       if (ld == NULL) {
+               ldapctl_free(ctl);
+               return NULL;
        }
-#ifdef USE_LDAP_TLS
-       
-       if( tls && !ssl ) {
-               /* Handle TLS */
-               if( rc != LDAP_OPT_SUCCESS ) {
-                       ldap_unbind_ext( ld, NULL, NULL );
-                       return baseDN;
-               }
-               rc = ldap_start_tls_s( ld, NULL, NULL );
-               if (rc != 0) {
-                       ldap_unbind_ext( ld, NULL, NULL );
-                       return baseDN;
-               }
-       }
-#endif
-
-       /* Bind to the server, if required */
-       if( bindDN ) {
-               if( *bindDN != '\0' ) {
-                       rc = claws_ldap_simple_bind_s( ld, bindDN, bindPW );
-                       if( rc != LDAP_SUCCESS ) {
-                               g_printerr("LDAP: %s\n", ldap_err2string(rc));
-                               ldap_unbind_ext( ld, NULL, NULL );
-                               return baseDN;
-                       }
-               }
-       }
-
-       /* Test for LDAP version 3 */
        baseDN = ldaputil_test_v3( ld, tov, &rc );
-       if (baseDN) {
+       if (baseDN)
                debug_print("Using LDAP v3\n");
-       }
 
+#ifdef G_OS_UNIX
        if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
+#else
+       if( baseDN == NULL) {
+#endif
                baseDN = ldaputil_test_v2( ld, tov );
-               if (baseDN) {
+               if (baseDN)
                        debug_print("Using LDAP v2\n");
-               }
        }
-       if (ld && !LDAP_API_ERROR(rc))
-               ldap_unbind_ext( ld, NULL, NULL );
+       if (ld)
+               ldapsvr_disconnect(ld);
+
+       ldapctl_free(ctl);
        
        return baseDN;
 }
@@ -313,56 +283,23 @@ GList *ldaputil_read_basedn(
  */
 gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls, int secs ) {
        gboolean retVal = FALSE;
+       LdapControl *ctl = ldapctl_create();
        LDAP *ld;
-#ifdef USE_LDAP_TLS
-       gint rc;
-       gint version;
-#endif
-       gchar *uri = NULL;
-
-       if( host == NULL ) return retVal;
-       if( port < 1 ) return retVal;
-       
-       ldapsrv_set_options (secs, NULL);
-       uri = g_strdup_printf("ldap%s://%s:%d",
-                               ssl?"s":"",
-                               host, port);
-       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, secs, &rc );
-               if (dummy)
-                       g_list_free(dummy);
-               if (LDAP_API_ERROR(rc))
-                       return FALSE;
-       }
 
-       if( tls && !ssl ) {
-               /* Handle TLS */
-               version = LDAP_VERSION3;
-               rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
-               if( rc != LDAP_OPT_SUCCESS ) {
-                       ldap_unbind_ext( ld, NULL, NULL );
-                       return FALSE;
-               }
+       ldapctl_set_tls(ctl, tls);
+       ldapctl_set_ssl(ctl, ssl);
+       ldapctl_set_port(ctl, port);
+       ldapctl_set_host(ctl, host);
+       ldapctl_set_timeout(ctl, secs);
 
-               rc = ldap_start_tls_s( ld, NULL, NULL );
-               if (rc != 0) {
-                       ldap_unbind_ext( ld, NULL, NULL );
-                       return FALSE;
-               }
-       }
-#endif
+       ld = ldapsvr_connect(ctl);
        if( ld != NULL ) {
-               ldap_unbind_ext( ld, NULL, NULL );
+               ldapsvr_disconnect(ld);
                debug_print("ld != NULL\n");
                retVal = TRUE;
        }
+       ldapctl_free(ctl);
+
        return retVal;
 }
 
@@ -374,6 +311,27 @@ gboolean ldaputil_test_ldap_lib( void ) {
        return TRUE;
 }
 
+const gchar *ldaputil_get_error(LDAP *ld)
+{
+       gchar *ld_error = NULL;
+       static gchar error[512];
+
+       ldap_get_option( ld, LDAP_OPT_ERROR_STRING, &ld_error);
+       if (ld_error != NULL)
+               strncpy2(error, ld_error, sizeof(error));
+       else
+               strncpy2(error, _("Unknown error"), sizeof(error));
+#ifndef G_OS_WIN32
+       /* From https://msdn.microsoft.com/en-us/library/aa366594%28v=vs.85%29.aspx
+        * "LDAP_OPT_ERROR_STRING returns a pointer to an internal static
+        * string table, and ldap_memfree should not be called when using
+        * this session option."
+        */
+       ldap_memfree(ld_error);
+#endif
+
+       return error;
+}
 #endif /* USE_LDAP */
 
 /*