Fix crash on Windows when doing a LDAP search.
[claws.git] / src / ldapquery.c
index 70af9f3a94c6804074443058b07cc61e072883ec..b729d945f46dcde72d555e37a6723b701ce910c2 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-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/>.
- * 
  */
 
 /*
@@ -23,6 +22,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef USE_LDAP
 #include <glib.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"
@@ -506,7 +506,7 @@ static GList *ldapqry_build_items_fl(
                                }
                        }
                        if( fullName ) {
-                               g_strchug( fullName ); g_strchomp( fullName );
+                               g_strstrip( fullName );
                                allocated = TRUE;
                        }
                }
@@ -526,7 +526,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",
@@ -639,7 +639,7 @@ static GList *ldapqry_process_single_entry(
        if( ber != NULL ) {
                ber_free( ber, 0 );
        }
-       g_free( dnEntry );
+       ldap_memfree( dnEntry );
 
        return listReturn;
 }
@@ -688,9 +688,6 @@ 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 */
        if (debug_get_mode()) {
@@ -707,13 +704,7 @@ static gint ldapqry_connect( LdapQuery *qry ) {
        qry->elapsedTime = -1;
        ADDRQUERY_RETVAL(qry) = LDAPRC_INIT;
 
-       ldapsrv_set_options (ctl->timeOut, NULL);
-
-       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);
@@ -728,42 +719,6 @@ static gint ldapqry_connect( LdapQuery *qry ) {
        debug_print("connected to LDAP host %s on port %d\n",
                        ctl->hostName?ctl->hostName:"null", 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 );
-                       
-                       debug_print("rc=%d\n", rc);
-                       debug_print("LDAP Status: set_option: %s\n", ldap_err2string(rc));
-
-                       if( rc != LDAP_SUCCESS ) {
-                               return ADDRQUERY_RETVAL(qry);
-                       }
-               }
-       }
-#endif
-
-       /* Bind to the server, if required */
-       ADDRQUERY_RETVAL(qry) = LDAPRC_BIND;
-       if( ctl->bindDN ) {
-               if( * ctl->bindDN != '\0' ) {
-                       debug_print("binding...\n");
-                       rc = claws_ldap_simple_bind_s( ld, ctl->bindDN, ctl->bindPass );
-                       debug_print("rc=%d\n", rc);
-                       if( rc != LDAP_SUCCESS ) {
-                               debug_print("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);
@@ -804,7 +759,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;
@@ -839,7 +794,7 @@ static gint ldapqry_search_retrieve( LdapQuery *qry ) {
        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", ldap_err2string(rc));
+       debug_print("LDAP Error: ldap_search_st: %s\n", ldaputil_get_error(ld));
        ldapctl_free_attribute_array( attribs );
        g_free( criteria );
        criteria = NULL;
@@ -856,12 +811,12 @@ static gint ldapqry_search_retrieve( LdapQuery *qry ) {
        else if( rc == LDAP_SUCCESS ) {
                searchFlag = TRUE;
        }
-       else if( rc == LDAP_PARTIAL_RESULTS ) {
+       else if( rc == LDAP_PARTIAL_RESULTS || (result && ldap_count_entries(ld, result) > 0) ) {
                searchFlag = TRUE;
        }
        else {
                debug_print("LDAP Error: ldap_search_st: %d\n", rc);
-               debug_print("LDAP Error: ldap_search_st: %s\n", ldap_err2string(rc));
+               debug_print("LDAP Error: ldap_search_st: %s\n", ldaputil_get_error(ld));
                return ADDRQUERY_RETVAL(qry);
        }
        ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
@@ -897,19 +852,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 {
-                       /*if (debug_get_mode()) {
-                               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 );
        }
 
@@ -1027,8 +973,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);
@@ -1045,9 +995,6 @@ static void ldapqry_destroyer( void * ptr ) {
        qry = ( LdapQuery * ) ptr;
        cm_return_if_fail( qry != NULL );
 
-       debug_print("ldapqry_destroyer::%d::%s\n", (int) pthread_self(),
-                       ADDRQUERY_NAME(qry)?ADDRQUERY_NAME(qry):"null");
-
        /* Perform any destruction here */
        if( qry->control != NULL ) {
                ldapctl_free( qry->control );
@@ -1064,8 +1011,6 @@ static void ldapqry_destroyer( void * ptr ) {
 void ldapqry_cancel( LdapQuery *qry ) {
        cm_return_if_fail( qry != NULL );
 
-       debug_print("cancelling::%d::%s\n", (int) pthread_self(),
-                       ADDRQUERY_NAME(qry)?ADDRQUERY_NAME(qry):"null");
        if( ldapqry_get_busy_flag( qry ) ) {
                if( qry->thread ) {
                        debug_print("calling pthread_cancel\n");
@@ -1268,7 +1213,7 @@ 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", ldap_err2string(rc));
+               debug_print("LDAP Error: ldap_search_st: %s\n", ldaputil_get_error(ld));
                return ADDRQUERY_RETVAL(qry);
        }