talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit a7890e31e6a64dbfd6a162a3813cc03c11b937a0
parent 6950d52c9925853b8c04d498dd92d5c425dbad6f
Author: Ricardo Mones <ricardo@mones.org>
Date:   Sun,  7 Jan 2018 19:47:29 +0100

Use network log for LDAP operations output

Also add some error checking and improve debug output

Diffstat:
Msrc/ldapquery.c | 32+++++++++++++++++++++++++-------
Msrc/ldapserver.c | 83++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Msrc/ldapupdate.c | 25+++++++++++++------------
Msrc/ldaputil.c | 14++++++++++++--
4 files changed, 109 insertions(+), 45 deletions(-)

diff --git a/src/ldapquery.c b/src/ldapquery.c @@ -1,6 +1,6 @@ /* * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client - * Copyright (C) 2003-2015 Match Grun and the Claws Mail team + * 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 @@ -28,6 +28,7 @@ #ifdef USE_LDAP #include <glib.h> +#include <glib/gi18n.h> #include <sys/time.h> #include <string.h> @@ -41,6 +42,7 @@ #include "addritem.h" #include "addrcache.h" #include "common/utils.h" +#include "log.h" /* * Key for thread specific data. @@ -736,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 ); @@ -793,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; @@ -806,17 +817,19 @@ 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; @@ -1217,8 +1230,13 @@ 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 diff --git a/src/ldapserver.c b/src/ldapserver.c @@ -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 "utils.h" #include "adbookbase.h" #include "passwordstore.h" +#include "log.h" /** * Create new LDAP server interface object with no control object. @@ -757,6 +758,7 @@ PFldap_start_tls_s Win32_ldap_start_tls_s = NULL; LDAP *ldapsvr_connect(LdapControl *ctl) { LDAP *ld = NULL; gint rc; + gint op; gint version; gchar *uri = NULL; gchar *pwd; @@ -776,26 +778,48 @@ LDAP *ldapsvr_connect(LdapControl *ctl) { version = LDAP_VERSION3; debug_print("Setting version 3\n"); rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (void *)&version); - if (rc == LDAP_SUCCESS) + if (rc == LDAP_SUCCESS) { ctl->version = LDAP_VERSION3; - else + log_message(LOG_PROTOCOL, "LDAP (options): set version 3\n"); + } else { + log_error(LOG_PROTOCOL, _("LDAP error (options): %d (%s)\n"), + rc, ldaputil_get_error(ld)); debug_print("Failed: %s\n", ldaputil_get_error(ld)); + } - if (ldap_get_option(ld,LDAP_OPT_SSL,(void*)&rc) != LDAP_SUCCESS) + rc = ldap_get_option(ld, LDAP_OPT_SSL, (void*)&op); + if (rc != LDAP_SUCCESS) { + log_warning(LOG_PROTOCOL, _("LDAP warning (options): can't get SSL/TLS state\n")); debug_print("Can't get SSL/TLS state\n"); + } - if ((void *)rc != LDAP_OPT_ON) { + if ((void *)op != LDAP_OPT_ON) { debug_print("Enabling SSL/TLS\n"); - if (ldap_set_option(ld,LDAP_OPT_SSL,LDAP_OPT_ON) != LDAP_SUCCESS) + op = LDAP_OPT_ON; + rc = ldap_set_option(ld, LDAP_OPT_SSL, (void *)&op); + if (rc != LDAP_SUCCESS) { + log_error(LOG_PROTOCOL, _("LDAP error (options): %d (%s)\n"), + rc, ldaputil_get_error(ld)); debug_print("Failed: %s\n", ldaputil_get_error(ld)); - else { - ldap_get_option(ld,LDAP_OPT_SSL,(void*)&rc); - debug_print("SSL/TLS now %d\n", rc); + } else { + rc = ldap_get_option(ld, LDAP_OPT_SSL, (void*)&op); + if (rc != LDAP_SUCCESS) { + log_error(LOG_PROTOCOL, _("LDAP error (options): %d (%s)\n"), + rc, ldaputil_get_error(ld)); + } else { + log_message(LOG_PROTOCOL, _("LDAP (options): SSL/TLS enabled (%d)\n"), op); + } + debug_print("SSL/TLS now %d\n", op); } - } - if (!ld || (rc = ldap_connect(ld, NULL)) != LDAP_SUCCESS) + + if (!ld || (rc = ldap_connect(ld, NULL)) != LDAP_SUCCESS) { + log_error(LOG_PROTOCOL, _("LDAP error (connect): %d (%s)\n"), + rc, ldaputil_get_error(ld)); debug_print("ldap_connect failed: %d %s\n", rc, ldaputil_get_error(ld)); + } else { + log_message(LOG_PROTOCOL, _("LDAP (connect): completed successfully\n")); + } } #endif g_free(uri); @@ -803,7 +827,6 @@ LDAP *ldapsvr_connect(LdapControl *ctl) { if (ld == NULL) return NULL; - debug_print("Got handle to LDAP host %s on port %d\n", ctl->hostName, ctl->port); version = LDAP_VERSION3; @@ -811,9 +834,11 @@ LDAP *ldapsvr_connect(LdapControl *ctl) { rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version); if (rc == LDAP_OPT_SUCCESS) { ctl->version = LDAP_VERSION3; - } else - g_printerr("LDAP: Error %d (%s)\n", - rc, ldaputil_get_error(ld)); + log_message(LOG_PROTOCOL, "LDAP (options): set version 3\n"); + } else { + log_error(LOG_PROTOCOL, _("LDAP error (options): %d (%s)\n"), + rc, ldaputil_get_error(ld)); + } #if (defined USE_LDAP_TLS || defined G_OS_WIN32) /* Handle TLS */ @@ -824,7 +849,8 @@ LDAP *ldapsvr_connect(LdapControl *ctl) { if (Win32_ldap_start_tls_s == NULL) { void *lib = LoadLibrary("wldap32.dll"); if (!lib || (Win32_ldap_start_tls_s = (PFldap_start_tls_s) GetProcAddress(lib, LDAP_START_TLS_S)) == NULL) { - g_printerr("LDAP Error(tls): ldap_start_tls_s: not supported on this platform"); + log_error(LOG_PROTOCOL, _("LDAP error (TLS): " + "ldap_start_tls_s not supported on this platform\n")); if (lib) FreeLibrary(lib); return NULL; @@ -839,10 +865,11 @@ LDAP *ldapsvr_connect(LdapControl *ctl) { rc = ldap_start_tls_s(ld, NULL, NULL); #endif if (rc != LDAP_SUCCESS) { - g_printerr("LDAP Error(tls): ldap_start_tls_s: %d %s\n", - rc, ldaputil_get_error(ld)); + log_error(LOG_PROTOCOL, _("LDAP error (TLS): ldap_start_tls_s: %d (%s)\n"), + rc, ldaputil_get_error(ld)); return NULL; } else { + log_message(LOG_PROTOCOL, _("LDAP (TLS): started successfully\n")); debug_print("Done\n"); } } @@ -858,11 +885,12 @@ LDAP *ldapsvr_connect(LdapControl *ctl) { memset(pwd, 0, strlen(pwd)); g_free(pwd); if (rc != LDAP_SUCCESS) { - g_printerr("bindDN: %s, bindPass xxx\n", ctl->bindDN); - g_printerr("LDAP Error(bind): ldap_simple_bind_s: %s\n", - ldaputil_get_error(ld)); + log_error(LOG_PROTOCOL, _("LDAP error (bind): binding DN '%s': %d (%s)\n" ), + ctl->bindDN, rc, ldaputil_get_error(ld)); return NULL; } + log_message(LOG_PROTOCOL, _("LDAP (bind): successfully for DN '%s'\n"), + ctl->bindDN); } } return ld; @@ -873,9 +901,16 @@ LDAP *ldapsvr_connect(LdapControl *ctl) { * \param ld Resource to LDAP. */ void ldapsvr_disconnect(LDAP *ld) { + gint rc; /* Disconnect */ cm_return_if_fail(ld != NULL); - ldap_unbind_ext(ld, NULL, NULL); + rc = ldap_unbind_ext(ld, NULL, NULL); + if (rc != LDAP_SUCCESS) { + log_error(LOG_PROTOCOL, _("LDAP error (unbind): %d (%s)\n"), + rc, ldaputil_get_error(ld)); + } else { + log_message(LOG_PROTOCOL, _("LDAP (unbind): successful\n")); + } } #endif /* USE_LDAP */ diff --git a/src/ldapupdate.c b/src/ldapupdate.c @@ -1,6 +1,6 @@ /* - * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client - * Copyright (C) 2003-2012 Michael Rasmussen and the Claws Mail team + * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client + * Copyright (C) 2003-2018 Michael Rasmussen 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/>. - * */ /* @@ -44,6 +43,7 @@ #include "utils.h" #include "adbookbase.h" #include "editaddress_other_attributes_ldap.h" +#include "log.h" /** * Structure to hold user defined attributes @@ -677,7 +677,8 @@ int ldapsvr_compare_manual_attr(LDAP *ld, LdapServer *server, gchar *dn, char *a rc = ldap_search_ext_s(ld, ctl->baseDN, LDAP_SCOPE_ONELEVEL, filter, NULL, 0, NULL, NULL, NULL, 0, &res); if (rc) { - g_printerr("ldap_search for attr=%s\" failed[0x%x]: %s\n",attr, rc, ldaputil_get_error(ld)); + log_error(LOG_PROTOCOL, _("LDAP error (search): for attribute '%s': %d (%s)\n"), + attr, rc, ldaputil_get_error(ld)); retVal = -2; } else { @@ -914,7 +915,8 @@ void ldapsvr_handle_other_attributes(LDAP *ld, LdapServer *server, char *dn, GHa server->retVal = LDAPRC_ALREADY_EXIST; break; default: - g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n", dn, rc, ldaputil_get_error(ld)); + log_error(LOG_PROTOCOL, _("LDAP error (modify): for DN '%s': %d (%s)\n"), + dn, rc, ldaputil_get_error(ld)); if (rc == 0x8) server->retVal = LDAPRC_STRONG_AUTH; else @@ -1056,7 +1058,7 @@ void ldapsvr_add_contact(LdapServer *server, GHashTable *contact) { server->retVal = LDAPRC_ALREADY_EXIST; break; default: - g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n", + log_error(LOG_PROTOCOL, _("LDAP error (modify): for DN '%s': %d (%s)\n"), base_dn, rc, ldaputil_get_error(ld)); if (rc == 0x8) server->retVal = LDAPRC_STRONG_AUTH; @@ -1128,9 +1130,8 @@ void ldapsvr_update_contact(LdapServer *server, GHashTable *contact) { */ } else { - g_printerr("Current dn: %s\n", dn); - g_printerr("new dn: %s\n", newRdn); - g_printerr("LDAP Error(ldap_modrdn2_s) failed[0x%x]: %s\n", rc, ldaputil_get_error(ld)); + log_error(LOG_PROTOCOL, _("LDAP error (rename): from '%s' to '%s': %d (%s)\n"), + dn, newRdn, rc, ldaputil_get_error(ld)); g_free(newRdn); clean_up(ld, server, contact); return; @@ -1285,8 +1286,8 @@ void ldapsvr_update_contact(LdapServer *server, GHashTable *contact) { mods[cnt] = NULL; rc = ldap_modify_ext_s(ld, dn, mods, NULL, NULL); if (rc) { - g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n", - dn, rc, ldaputil_get_error(ld)); + log_error(LOG_PROTOCOL, _("LDAP error (modify): for DN '%s': %d (%s)\n"), + dn, rc, ldaputil_get_error(ld)); server->retVal = LDAPRC_NAMING_VIOLATION; } if (mail) @@ -1326,7 +1327,7 @@ void ldapsvr_delete_contact(LdapServer *server, GHashTable *contact) { server->retVal = LDAPRC_SUCCESS; rc = ldap_delete_ext_s(ld, dn, NULL, NULL); if (rc) { - g_printerr("ldap_modify for dn=%s\" failed[0x%x]: %s\n", + log_error(LOG_PROTOCOL, _("LDAP error (modify): for DN '%s': %d (%s)\n"), dn, rc, ldaputil_get_error(ld)); server->retVal = LDAPRC_NODN; } diff --git a/src/ldaputil.c b/src/ldaputil.c @@ -1,6 +1,6 @@ /* * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client - * Copyright (C) 2003-2015 Match Grun and the Claws Mail team + * 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 @@ -36,6 +36,7 @@ #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,8 +108,11 @@ static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) { } ber = NULL; } - } else + } 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; @@ -148,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; @@ -190,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 );