Use network log for LDAP operations output
[claws.git] / src / ldaputil.c
index 2dc77cfe897ac14fb999816a1e787db10171081b..1be1854245fa436e5a154c28e96567a96cf697f5 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2003-2009 Match Grun and the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2003-2018 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"
+#include "log.h"
 
 #define SYLDAP_TEST_FILTER   "(objectclass=*)"
 #define SYLDAP_SEARCHBASE_V2 "cn=config"
@@ -77,6 +78,7 @@ static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
                attribs, 0, NULL, NULL, &timeout, 0, &result );
 
        if( rc == LDAP_SUCCESS ) {
+               log_message(LOG_PROTOCOL, _("LDAP (search): succesful\n"));
                /* Process entries */
                for( e = ldap_first_entry( ld, result );
                     e != NULL;
@@ -106,7 +108,12 @@ static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
                        }
                        ber = NULL;
                }
-       } 
+       } else {
+               log_error(LOG_PROTOCOL, _("LDAP error (search): %d (%s)\n"),
+                               rc, ldaputil_get_error(ld));
+               debug_print("LDAP: Error %d (%s)\n", rc, ldaputil_get_error(ld));
+       }
+       
        if (errcode)
                *errcode = rc;
        if (result)
@@ -146,6 +153,7 @@ static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
                attribs, 0, NULL, NULL, &timeout, 0, &result );
 
        if( rc == LDAP_SUCCESS ) {
+               log_message(LOG_PROTOCOL, _("LDAP (search): succesful\n"));
                /* Process entries */
                for( e = ldap_first_entry( ld, result );
                     e != NULL;
@@ -171,8 +179,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 );
@@ -189,6 +196,10 @@ static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
                        }
                        ber = NULL;
                }
+       } else {
+               log_error(LOG_PROTOCOL, _("LDAP error (search): %d (%s)\n"),
+                               rc, ldaputil_get_error(ld));
+               debug_print("LDAP: Error %d (%s)\n", rc, ldaputil_get_error(ld));
        }
        if (result)
                ldap_msgfree( result );
@@ -197,6 +208,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 ) {
@@ -207,9 +220,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
 }
 
 /**
@@ -242,7 +257,6 @@ GList *ldaputil_read_basedn(
        ldapctl_set_host(ctl, host);
        ldapctl_set_timeout(ctl, tov);
        ldapctl_set_bind_dn(ctl, bindDN);
-       ldapctl_set_bind_password(ctl, bindPW, FALSE, FALSE);
 
        ld = ldapsvr_connect(ctl);
        if (ld == NULL) {
@@ -253,7 +267,11 @@ GList *ldaputil_read_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)
                        debug_print("Using LDAP v2\n");
@@ -303,6 +321,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 */
 
 /*