Use network log for LDAP operations output
[claws.git] / src / ldapquery.c
index 2c504abbca4d1d3dfb964b6d6e817055a6f8cf9b..7f0d2930ea972e2cc7f56e41fb4094a2ef76f832 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2003-2012 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/>.
- * 
  */
 
 /*
@@ -29,6 +28,7 @@
 #ifdef USE_LDAP
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <sys/time.h>
 #include <string.h>
 
@@ -42,6 +42,7 @@
 #include "addritem.h"
 #include "addrcache.h"
 #include "common/utils.h"
+#include "log.h"
 
 /*
  * Key for thread specific data.
@@ -507,7 +508,7 @@ static GList *ldapqry_build_items_fl(
                                }
                        }
                        if( fullName ) {
-                               g_strchug( fullName ); g_strchomp( fullName );
+                               g_strstrip( fullName );
                                allocated = TRUE;
                        }
                }
@@ -527,7 +528,7 @@ static GList *ldapqry_build_items_fl(
                if (attrib->name && strcmp(attrib->name, "jpegPhoto")) {
                        addritem_person_add_attribute( person, attrib );
                } else {
-                       if (qry && qry->server && qry->server->control) {
+                       if (qry->server && qry->server->control) {
                                gchar *dir = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, 
                                                        ADDRBOOK_DIR, G_DIR_SEPARATOR_S, NULL );
                                gchar *filename = g_strdup_printf("%s-%s-%s",
@@ -640,7 +641,7 @@ static GList *ldapqry_process_single_entry(
        if( ber != NULL ) {
                ber_free( ber, 0 );
        }
-       g_free( dnEntry );
+       ldap_memfree( dnEntry );
 
        return listReturn;
 }
@@ -737,8 +738,17 @@ static gint ldapqry_connect( LdapQuery *qry ) {
  * \return Error/status code.
  */
 static gint ldapqry_disconnect( LdapQuery *qry ) {
+       gint rc;
        /* Disconnect */
-       if( qry->ldap ) ldap_unbind_ext( qry->ldap, NULL, NULL );
+       if( qry->ldap ) {
+               rc = ldap_unbind_ext( qry->ldap, NULL, NULL );
+               if (rc != LDAP_SUCCESS) {
+                       log_error(LOG_PROTOCOL, _("LDAP error (unbind): %d (%s)\n"),
+                                       rc, ldaputil_get_error(qry->ldap));
+               } else {
+                       log_message(LOG_PROTOCOL, _("LDAP (unbind): successful\n"));
+               }
+       }
        qry->ldap = NULL;
 
        ldapqry_touch( qry );
@@ -794,12 +804,12 @@ static gint ldapqry_search_retrieve( LdapQuery *qry ) {
        ADDRQUERY_RETVAL(qry) = LDAPRC_TIMEOUT;
        rc = ldap_search_ext_s( ld, ctl->baseDN, LDAP_SCOPE_SUBTREE, criteria,
                attribs, 0, NULL, NULL, &timeout, 0, &result );
-       debug_print("LDAP Error: ldap_search_st: %d\n", rc);
-       debug_print("LDAP Error: ldap_search_st: %s\n", ldaputil_get_error(ld));
+       debug_print("LDAP ldap_search_ext_s: %d (%s)\n", rc, ldaputil_get_error(ld));
        ldapctl_free_attribute_array( attribs );
        g_free( criteria );
        criteria = NULL;
        if( rc == LDAP_TIMEOUT ) {
+               log_warning(LOG_PROTOCOL, _("LDAP (search): timeout\n"));
                return ADDRQUERY_RETVAL(qry);
        }
        ADDRQUERY_RETVAL(qry) = LDAPRC_SEARCH;
@@ -807,22 +817,28 @@ static gint ldapqry_search_retrieve( LdapQuery *qry ) {
        /* Test valid returns */
        searchFlag = FALSE;
        if( rc == LDAP_ADMINLIMIT_EXCEEDED ) {
+               log_warning(LOG_PROTOCOL, _("LDAP (search): server limits exceeded\n"));
                searchFlag = TRUE;
        }
        else if( rc == LDAP_SUCCESS ) {
+               log_message(LOG_PROTOCOL, _("LDAP (search): successful\n"));
                searchFlag = TRUE;
        }
        else if( rc == LDAP_PARTIAL_RESULTS || (result && ldap_count_entries(ld, result) > 0) ) {
+               log_message(LOG_PROTOCOL, _("LDAP (search): successful (partial results)\n"));
                searchFlag = TRUE;
        }
        else {
-               debug_print("LDAP Error: ldap_search_st: %d\n", rc);
-               debug_print("LDAP Error: ldap_search_st: %s\n", ldaputil_get_error(ld));
+               log_error(LOG_PROTOCOL, _("LDAP error (search): %d (%s)\n"), rc, ldaputil_get_error(ld));
                return ADDRQUERY_RETVAL(qry);
        }
        ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
 
+#ifdef G_OS_WIN32
+       debug_print("Total results are: %lu\n", ldap_count_entries(ld, result));
+#else
        debug_print("Total results are: %d\n", ldap_count_entries(ld, result));
+#endif
 
        /* Process results */
        first = TRUE;
@@ -974,8 +990,12 @@ gint ldapqry_read_data_th( LdapQuery *qry ) {
                        qry->thread = g_malloc0( sizeof( pthread_t ) );
 
                        /* Setup thread */                      
-                       pthread_create( qry->thread, NULL,
-                               (void *) ldapqry_search, (void *) qry );
+                       if (pthread_create( qry->thread, NULL,
+                               (void *) ldapqry_search, (void *) qry ) != 0) {
+                               g_free(qry->thread);
+                               qry->thread = NULL;
+                               ADDRQUERY_RETVAL(qry) = LDAPRC_SEARCH;
+                       }
                }
        }
        return ADDRQUERY_RETVAL(qry);
@@ -1210,11 +1230,20 @@ static gint ldapqry_locate_retrieve( LdapQuery *qry ) {
        }
        ADDRQUERY_RETVAL(qry) = LDAPRC_SEARCH;
        if( rc != LDAP_SUCCESS ) {
-               debug_print("LDAP Error: ldap_search_st: %s\n", ldaputil_get_error(ld));
+               log_error(LOG_PROTOCOL, _("LDAP error (search): %d (%s)\n"),
+                               rc, ldaputil_get_error(ld));
+               debug_print("LDAP Error: ldap_search_ext_s: %d (%s)\n",
+                               rc, ldaputil_get_error(ld));
                return ADDRQUERY_RETVAL(qry);
+       } else {
+               log_message(LOG_PROTOCOL, _("LDAP (search): successful\n"));
        }
 
+#ifdef G_OS_WIN32
+       debug_print("Total results are: %lu\n", ldap_count_entries(ld, result));
+#else
        debug_print("Total results are: %d\n", ldap_count_entries(ld, result));
+#endif
 
        /* Process results */
        ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;