Use network log for LDAP operations output
[claws.git] / src / ldapquery.c
index cfef3ece45a60976f5eb92409ad25f545892e466..7f0d2930ea972e2cc7f56e41fb4094a2ef76f832 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * 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-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
- * 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,7 @@
  * 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/>.
  */
 
 /*
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef USE_LDAP
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <sys/time.h>
 #include <string.h>
-#include <lber.h>
 
+#include "defs.h"
 #include "ldaputil.h"
 #include "ldapquery.h"
 #include "ldapctrl.h"
+#include "ldapserver.h"
 #include "mgutils.h"
 
 #include "addritem.h"
 #include "addrcache.h"
+#include "common/utils.h"
+#include "log.h"
 
 /*
  * Key for thread specific data.
@@ -103,7 +107,7 @@ LdapQuery *ldapqry_create( void ) {
  * \param ctl Control object.
  */
 void ldapqry_set_control( LdapQuery *qry, LdapControl *ctl ) {
-       g_return_if_fail( qry != NULL );
+       cm_return_if_fail( qry != NULL );
        qry->control = ctl;
 }
 
@@ -113,8 +117,12 @@ void ldapqry_set_control( LdapQuery *qry, LdapControl *ctl ) {
  * \param value Name.
  */
 void ldapqry_set_name( LdapQuery* qry, const gchar *value ) {
+       cm_return_if_fail( qry != NULL );
        ADDRQUERY_NAME(qry) = mgu_replace_string( ADDRQUERY_NAME(qry), value );
+       if (ADDRQUERY_NAME(qry) == NULL)
+               return;
        g_strstrip( ADDRQUERY_NAME(qry) );
+       debug_print("set name: %s\n", ADDRQUERY_NAME(qry));
 }
 
 /**
@@ -123,8 +131,12 @@ void ldapqry_set_name( LdapQuery* qry, const gchar *value ) {
  * \param value 
  */
 void ldapqry_set_search_value( LdapQuery *qry, const gchar *value ) {
+       cm_return_if_fail( qry != NULL );
        ADDRQUERY_SEARCHVALUE(qry) = mgu_replace_string( ADDRQUERY_SEARCHVALUE(qry), value );
+       if (ADDRQUERY_SEARCHVALUE(qry) == NULL)
+               return;
        g_strstrip( ADDRQUERY_SEARCHVALUE(qry) );
+       debug_print("search value: %s\n", ADDRQUERY_SEARCHVALUE(qry));
 }
 
 /**
@@ -149,7 +161,7 @@ void ldapqry_set_query_type( LdapQuery* qry, const gint value ) {
  * \param value Type.
  */
 void ldapqry_set_search_type( LdapQuery *qry, const AddrSearchType value ) {
-       g_return_if_fail( qry != NULL );
+       cm_return_if_fail( qry != NULL );
        ADDRQUERY_SEARCHTYPE(qry) = value;
 }
 
@@ -159,6 +171,7 @@ void ldapqry_set_search_type( LdapQuery *qry, const AddrSearchType value ) {
  * \param value ID for the query.
  */
 void ldapqry_set_query_id( LdapQuery* qry, const gint value ) {
+       cm_return_if_fail( qry != NULL );
        ADDRQUERY_ID(qry) = value;
 }
 
@@ -215,7 +228,7 @@ void ldapqry_set_callback_end( LdapQuery *qry, void *func ) {
  * \param value Value of stop flag.
  */
 void ldapqry_set_stop_flag( LdapQuery *qry, const gboolean value ) {
-       g_return_if_fail( qry != NULL );
+       cm_return_if_fail( qry != NULL );
 
        pthread_mutex_lock( qry->mutexStop );
        qry->stopFlag = value;
@@ -230,7 +243,7 @@ void ldapqry_set_stop_flag( LdapQuery *qry, const gboolean value ) {
  */
 static gboolean ldapqry_get_stop_flag( LdapQuery *qry ) {
        gboolean value;
-       g_return_val_if_fail( qry != NULL, TRUE );
+       cm_return_val_if_fail( qry != NULL, TRUE );
 
        pthread_mutex_lock( qry->mutexStop );
        value = qry->stopFlag;
@@ -244,7 +257,7 @@ static gboolean ldapqry_get_stop_flag( LdapQuery *qry ) {
  * \param value Value of busy flag.
  */
 static void ldapqry_set_busy_flag( LdapQuery *qry, const gboolean value ) {
-       g_return_if_fail( qry != NULL );
+       cm_return_if_fail( qry != NULL );
        if (qry->mutexBusy == NULL)
                return; /* exiting, mutex already freed */
 
@@ -261,7 +274,7 @@ static void ldapqry_set_busy_flag( LdapQuery *qry, const gboolean value ) {
  */
 static gboolean ldapqry_get_busy_flag( LdapQuery *qry ) {
        gboolean value;
-       g_return_val_if_fail( qry != NULL, FALSE );
+       cm_return_val_if_fail( qry != NULL, FALSE );
 
        pthread_mutex_lock( qry->mutexBusy );
        value = qry->busyFlag;
@@ -275,7 +288,7 @@ static gboolean ldapqry_get_busy_flag( LdapQuery *qry ) {
  * \param value Value of aged flag.
  */
 static void ldapqry_set_aged_flag( LdapQuery *qry, const gboolean value ) {
-       g_return_if_fail( qry != NULL );
+       cm_return_if_fail( qry != NULL );
        qry->agedFlag = value;
 }
 
@@ -284,7 +297,7 @@ static void ldapqry_set_aged_flag( LdapQuery *qry, const gboolean value ) {
  * \param qry Query object.
  */
 static void ldapqry_clear( LdapQuery *qry ) {
-       g_return_if_fail( qry != NULL );
+       cm_return_if_fail( qry != NULL );
 
        /* Free internal stuff */
        g_free( ADDRQUERY_NAME(qry) );
@@ -314,7 +327,7 @@ static void ldapqry_clear( LdapQuery *qry ) {
  * \param qry Query object to process.
  */
 void ldapqry_free( LdapQuery *qry ) {
-       g_return_if_fail( qry != NULL );
+       cm_return_if_fail( qry != NULL );
 
        /* Clear out internal members */
        ADDRQUERY_TYPE(qry) = ADDRQUERY_NONE;
@@ -382,7 +395,8 @@ static GSList *ldapqry_add_list_values(
 
        if( ( vals = ldap_get_values_len( ld, entry, attr ) ) != NULL ) {
                for( i = 0; vals[i] != NULL; i++ ) {
-                       /* printf( "lv\t%s: %s\n", attr, vals[i] ); */
+                       /*debug_print("lv\t%s: %s\n", attr?attr:"null",
+                                       vals[i]->bv_val?vals[i]->bv_val:"null");*/
                        list = g_slist_append( list, g_strndup( vals[i]->bv_val, vals[i]->bv_len) );
                }
        }
@@ -403,8 +417,19 @@ static GSList *ldapqry_add_single_value( LDAP *ld, LDAPMessage *entry, char *att
 
        if( ( vals = ldap_get_values_len( ld, entry, attr ) ) != NULL ) {
                if( vals[0] != NULL ) {
-                       /* printf( "sv\t%s: %s\n", attr, vals[0] ); */
-                       list = g_slist_append( list, g_strndup( vals[0]->bv_val, vals[0]->bv_len ));
+                       if (strcmp(attr, "jpegPhoto")) {
+                               debug_print("sv\t%s: %s\n", attr?attr:"null",
+                                               vals[0]->bv_val?vals[0]->bv_val:"null");
+                               list = g_slist_append( list, g_strndup( vals[0]->bv_val, vals[0]->bv_len ));
+                       } else {
+                               char *file = get_tmp_file();
+                               FILE *fp = g_fopen(file, "wb");
+                               if (fp) {
+                                       fwrite(vals[0]->bv_val, 1, vals[0]->bv_len, fp);
+                                       fclose(fp);
+                               }
+                               list = g_slist_append( list, file);
+                       }
                }
        }
        ldap_value_free_len( vals );
@@ -439,10 +464,11 @@ static GList *ldapqry_build_items_fl(
 {
        GSList *nodeAddress, *cur;
        gchar *firstName = NULL, *lastName = NULL, *fullName = NULL;
-       gboolean allocated;
+       gboolean allocated = FALSE;
        ItemPerson *person;
        ItemEMail *email;
        ItemFolder *folder;
+       gchar *picfile = NULL;
        GList *listReturn = NULL;
 
        folder = ADDRQUERY_FOLDER(qry);
@@ -482,7 +508,7 @@ static GList *ldapqry_build_items_fl(
                                }
                        }
                        if( fullName ) {
-                               g_strchug( fullName ); g_strchomp( fullName );
+                               g_strstrip( fullName );
                                allocated = TRUE;
                        }
                }
@@ -499,7 +525,24 @@ static GList *ldapqry_build_items_fl(
        
        for (cur = attributes; cur; cur = cur->next) {
                UserAttribute *attrib = addritem_copy_attribute((UserAttribute *)cur->data);
-               addritem_person_add_attribute( person, attrib );
+               if (attrib->name && strcmp(attrib->name, "jpegPhoto")) {
+                       addritem_person_add_attribute( person, attrib );
+               } else {
+                       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",
+                                       qry->server->control->hostName?qry->server->control->hostName:"nohost", 
+                                       qry->server->control->baseDN?qry->server->control->baseDN:"nobase", 
+                                       dn);
+                               picfile = g_strdup_printf("%s%s.png", dir, filename);
+                               addritem_person_set_picture( person, filename );
+                               rename_force(attrib->value, picfile);
+                               g_free(filename);
+                               g_free(picfile);
+                               g_free(dir);
+                       }
+               }
        }
        
        addrcache_folder_add_person( cache, ADDRQUERY_FOLDER(qry), person );
@@ -514,7 +557,9 @@ static GList *ldapqry_build_items_fl(
                addrcache_id_email( cache, email );
                addrcache_person_add_email( cache, person, email );
                addritem_person_add_email( person, email );
-               /*addritem_print_item_email(email, stdout);*/
+               /*if (debug_get_mode()) {
+                       addritem_print_item_email(email, stdout);
+               }*/
                listReturn = g_list_append( listReturn, email );
                nodeAddress = g_slist_next( nodeAddress );
        }
@@ -552,7 +597,7 @@ static GList *ldapqry_process_single_entry(
        listReturn = NULL;
        ctl = qry->control;
        dnEntry = ldap_get_dn( ld, e );
-       /* printf( "DN: %s\n", dnEntry ); */
+       debug_print( "DN: %s\n", dnEntry?dnEntry:"null" );
 
        /* Process all attributes */
        for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL;
@@ -596,7 +641,7 @@ static GList *ldapqry_process_single_entry(
        if( ber != NULL ) {
                ber_free( ber, 0 );
        }
-       g_free( dnEntry );
+       ldap_memfree( dnEntry );
 
        return listReturn;
 }
@@ -645,26 +690,23 @@ void ldapqry_touch( LdapQuery *qry ) {
 static gint ldapqry_connect( LdapQuery *qry ) {
        LdapControl *ctl;
        LDAP *ld = NULL;
-       gint rc;
-       gint version;
-       gchar *uri = NULL;
 
        /* Initialize connection */
-       /* printf( "===ldapqry_connect===\n" ); */
-       /* ldapqry_print( qry, stdout ); */
+       if (debug_get_mode()) {
+               debug_print("===ldapqry_connect===\n");
+               /*ldapqry_print(qry, stdout);*/
+       }
        ctl = qry->control;
-       /* ldapctl_print( ctl, stdout ); */
-       /* printf( "======\n" ); */
+       /*if (debug_get_mode()) {
+               ldapctl_print(ctl, stdout);
+               debug_print("======\n");
+       }*/
        ldapqry_touch( qry );
        qry->startTime = qry->touchTime;
        qry->elapsedTime = -1;
        ADDRQUERY_RETVAL(qry) = LDAPRC_INIT;
 
-       uri = g_strdup_printf("ldap%s://%s:%d",
-                               ctl->enableSSL?"s":"",
-                               ctl->hostName, ctl->port);
-       ldap_initialize(&ld, uri);
-       g_free(uri);
+       ld = ldapsvr_connect(ctl);
 
        if (ld == NULL)
                return ADDRQUERY_RETVAL(qry);
@@ -676,49 +718,9 @@ static gint ldapqry_connect( LdapQuery *qry ) {
        }
        ldapqry_touch( qry );
 
-       /*
-       printf( "connected to LDAP host %s on port %d\n", ctl->hostName, ctl->port );
-       */
-
-#ifdef USE_LDAP_TLS
-       /* Handle TLS */
-       version = LDAP_VERSION3;
-       rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
-       if( rc == LDAP_OPT_SUCCESS ) {
-               ctl->version = LDAP_VERSION3;
-       }
-
-       if( ctl->version == LDAP_VERSION3 ) {
-               if( ctl->enableTLS && !ctl->enableSSL ) {
-                       ADDRQUERY_RETVAL(qry) = LDAPRC_TLS;
-                       rc = ldap_start_tls_s( ld, NULL, NULL );
-                       
-                       /* printf( "rc=%d\n", rc );
-                       printf( "LDAP Status: set_option: %s\n", ldap_err2string( rc ) ); */
-                       
-                       if( rc != LDAP_SUCCESS ) {
-                               return ADDRQUERY_RETVAL(qry);
-                       }
-               }
-       }
-#endif
+       debug_print("connected to LDAP host %s on port %d\n",
+                       ctl->hostName?ctl->hostName:"null", ctl->port);
 
-       /* Bind to the server, if required */
-       ADDRQUERY_RETVAL(qry) = LDAPRC_BIND;
-       if( ctl->bindDN ) {
-               if( * ctl->bindDN != '\0' ) {
-                       /* printf( "binding...\n" ); */
-                       rc = claws_ldap_simple_bind_s( ld, ctl->bindDN, ctl->bindPass );
-                       /* printf( "rc=%d\n", rc ); */
-                       if( rc != LDAP_SUCCESS ) {
-                               /*
-                               printf( "LDAP Error: ldap_simple_bind_s: %s\n",
-                                       ldap_err2string( rc ) );
-                               */
-                               return ADDRQUERY_RETVAL(qry);
-                       }
-               }
-       }
        ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
        if( ldapqry_get_stop_flag( qry ) ) {
                return ADDRQUERY_RETVAL(qry);
@@ -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 );
@@ -759,7 +770,7 @@ static gint ldapqry_disconnect( LdapQuery *qry ) {
 static gint ldapqry_search_retrieve( LdapQuery *qry ) {
        LdapControl *ctl;
        LDAP *ld;
-       LDAPMessage *result, *e = NULL;
+       LDAPMessage *result = NULL, *e = NULL;
        char **attribs;
        gchar *criteria;
        gboolean searchFlag;
@@ -780,11 +791,11 @@ static gint ldapqry_search_retrieve( LdapQuery *qry ) {
        ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
 
        /* Define all attributes we are interested in. */
-       attribs = ldapctl_attribute_array( ctl );
+       attribs = ldapctl_full_attribute_array( ctl );
 
        /* Create LDAP search string */
        criteria = ldapctl_format_criteria( ctl, ADDRQUERY_SEARCHVALUE(qry) );
-       /* printf( "Search criteria ::%s::\n", criteria ); */
+       debug_print("Search criteria ::%s::\n", criteria?criteria:"null");
 
        /*
         * Execute the search - this step may take some time to complete
@@ -793,10 +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 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;
@@ -804,26 +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 ) {
+       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 {
-               /*
-               printf( "LDAP Error: ldap_search_st: %d\n", rc );
-               printf( "LDAP Error: ldap_search_st: %s\n", ldap_err2string( rc ) );
-               */
+               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;
 
-       /*
-       printf( "Total results are: %d\n", ldap_count_entries( ld, result ) );
-       */
+#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;
@@ -854,17 +869,10 @@ static gint ldapqry_search_retrieve( LdapQuery *qry ) {
                listEMail = ldapqry_process_single_entry( cache, qry, ld, e );
 
                /* Process callback */
-               if( qry->callBackEntry ) {
+               if( qry->callBackEntry )
                        qry->callBackEntry( qry, ADDRQUERY_ID(qry), listEMail, qry->data );
-               }
-               else {
-                       /*GList *node = listEMail;
-                       while (node) {
-                               addritem_print_item_email(node->data, stdout);
-                               node = g_list_next(node);
-                       }*/
+               else
                        g_list_free( listEMail );
-               }
                pthread_mutex_unlock( qry->mutexEntry );
        }
 
@@ -918,8 +926,8 @@ static gint ldapqry_perform_locate( LdapQuery *qry );
 static gint ldapqry_search( LdapQuery *qry ) {
        gint retVal;
 
-       g_return_val_if_fail( qry != NULL, -1 );
-       g_return_val_if_fail( qry->control != NULL, -1 );
+       cm_return_val_if_fail( qry != NULL, -1 );
+       cm_return_val_if_fail( qry->control != NULL, -1 );
 
        ldapqry_touch( qry );
        qry->completed = FALSE;
@@ -944,16 +952,12 @@ static gint ldapqry_search( LdapQuery *qry ) {
                qry->server->addressCache->dataRead = TRUE;
                qry->server->addressCache->accessFlag = FALSE;
                if( ldapqry_get_stop_flag( qry ) ) {
-                       /*
-                       printf( "Search was terminated prematurely\n" );
-                       */
+                       debug_print("Search was terminated prematurely\n");
                }
                else {
                        ldapqry_touch( qry );
                        qry->completed = TRUE;
-                       /*
-                       printf( "Search ran to completion\n" );
-                       */
+                       debug_print("Search ran to completion\n");
                }
        }
        ldapqry_set_stop_flag( qry, TRUE );
@@ -974,22 +978,24 @@ static gint ldapqry_search( LdapQuery *qry ) {
  * \return Error/status code.
  */
 gint ldapqry_read_data_th( LdapQuery *qry ) {
-       g_return_val_if_fail( qry != NULL, -1 );
-       g_return_val_if_fail( qry->control != NULL, -1 );
+       cm_return_val_if_fail( qry != NULL, -1 );
+       cm_return_val_if_fail( qry->control != NULL, -1 );
 
        ldapqry_set_stop_flag( qry, FALSE );
        ldapqry_touch( qry );
        if( ldapqry_check_search( qry ) ) {
                if( ADDRQUERY_RETVAL(qry) == LDAPRC_SUCCESS ) {
-                       /*
-                       printf( "Starting LDAP search thread\n");
-                       */
+                       debug_print("Starting LDAP search thread\n");
                        ldapqry_set_busy_flag( qry, TRUE );
                        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);
@@ -1004,9 +1010,7 @@ static void ldapqry_destroyer( void * ptr ) {
        LdapQuery *qry;
 
        qry = ( LdapQuery * ) ptr;
-       /*
-       printf( "ldapqry_destroyer::%d::%s\n", (int) pthread_self(), ADDRQUERY_NAME(qry) );
-       */
+       cm_return_if_fail( qry != NULL );
 
        /* Perform any destruction here */
        if( qry->control != NULL ) {
@@ -1022,14 +1026,11 @@ static void ldapqry_destroyer( void * ptr ) {
  * \param qry Query object to process.
  */
 void ldapqry_cancel( LdapQuery *qry ) {
-       g_return_if_fail( qry != NULL );
+       cm_return_if_fail( qry != NULL );
 
-       /*
-       printf( "cancelling::%d::%s\n", (int) pthread_self(), ADDRQUERY_NAME(qry) );
-       */
        if( ldapqry_get_busy_flag( qry ) ) {
                if( qry->thread ) {
-                       /* printf( "calling pthread_cancel\n" ); */
+                       debug_print("calling pthread_cancel\n");
                        pthread_cancel( * qry->thread );
                }
        }
@@ -1040,15 +1041,13 @@ void ldapqry_cancel( LdapQuery *qry ) {
  * any LDAP queries to initialize thread specific data.
  */
 void ldapqry_initialize( void ) {
-       /* printf( "ldapqry_initialize...\n" ); */
+       debug_print("ldapqry_initialize...\n");
        if( ! _queryThreadInit_ ) {
-               /*
-               printf( "ldapqry_initialize::creating thread specific area\n" );
-               */
+               debug_print("ldapqry_initialize::creating thread specific area\n");
                pthread_key_create( &_queryThreadKey_, ldapqry_destroyer );
                _queryThreadInit_ = TRUE;
        }
-       /* printf( "ldapqry_initialize... done!\n" ); */
+       debug_print("ldapqry_initialize... done!\n");
 }
 
 /**
@@ -1059,7 +1058,7 @@ void ldapqry_initialize( void ) {
 void ldapqry_age( LdapQuery *qry, gint maxAge ) {
        gint age;
 
-       g_return_if_fail( qry != NULL );
+       cm_return_if_fail( qry != NULL );
 
        /* Limit the time that queries can hang around */       
        if( maxAge < 1 ) maxAge = LDAPCTL_MAX_QUERY_AGE;
@@ -1079,7 +1078,7 @@ void ldapqry_delete_folder( LdapQuery *qry ) {
        AddressCache *cache;
        ItemFolder *folder;
 
-       g_return_if_fail( qry != NULL );
+       cm_return_if_fail( qry != NULL );
 
        folder = ADDRQUERY_FOLDER(qry);
        if( folder ) {
@@ -1231,15 +1230,20 @@ static gint ldapqry_locate_retrieve( LdapQuery *qry ) {
        }
        ADDRQUERY_RETVAL(qry) = LDAPRC_SEARCH;
        if( rc != LDAP_SUCCESS ) {
-               /*
-               printf( "LDAP Error: ldap_search_st: %s\n", ldap_err2string( rc ) );
-               */
+               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"));
        }
 
-       /*
-       printf( "Total results are: %d\n", ldap_count_entries( ld, result ) );
-       */
+#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;
@@ -1343,6 +1347,22 @@ gboolean ldapquery_remove_results( LdapQuery *qry ) {
        return retVal;
 }
 
+void ldapqry_print(LdapQuery *qry, FILE *stream) {
+       cm_return_if_fail( qry != NULL );
+
+       ldapsvr_print_data(qry->server, stream);
+       ldapctl_print(qry->control, stream);
+       fprintf(stream, "entriesRead: %d\n", qry->entriesRead);
+       fprintf(stream, "elapsedTime: %d\n", qry->elapsedTime);
+       fprintf(stream, "stopFlag: %d\n", qry->stopFlag);
+       fprintf(stream, "busyFlag: %d\n", qry->busyFlag);
+       fprintf(stream, "agedFlag: %d\n", qry->agedFlag);
+       fprintf(stream, "completed: %d\n", qry->completed);
+       fprintf(stream, "startTime: %d\n", (int) qry->startTime);
+       fprintf(stream, "touchTime: %d\n", (int) qry->touchTime);
+       fprintf(stream, "data: %s\n", qry->data?(gchar *)qry->data:"null");
+}
+
 #endif /* USE_LDAP */
 
 /*