2007-01-20 [colin] 2.7.1cvs38
[claws.git] / src / ldapquery.c
index cbef915052dcc474e401253d860f795da6ab4f2c..d70cbd6ddae8b2a3ebb5baa91fe67f40e10cfea2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2003-2006 Match Grun and the Sylpheed-Claws team
+ * Copyright (C) 2003-2007 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
@@ -32,6 +32,7 @@
 #include <string.h>
 #include <lber.h>
 
+#include "ldaputil.h"
 #include "ldapquery.h"
 #include "ldapctrl.h"
 #include "mgutils.h"
@@ -39,8 +40,6 @@
 #include "addritem.h"
 #include "addrcache.h"
 
-#include "ldapquery.h"
-
 /*
  * Key for thread specific data.
  */
@@ -461,15 +460,15 @@ static GSList *ldapqry_add_list_values(
 {
        GSList *list = NULL;
        gint i;
-       gchar **vals;
+       struct berval **vals;
 
-       if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
+       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] ); */
-                       list = g_slist_append( list, g_strdup( vals[i] ) );
+                       list = g_slist_append( list, g_strndup( vals[i]->bv_val, vals[i]->bv_len) );
                }
        }
-       ldap_value_free( vals );
+       ldap_value_free_len( vals );
        return list;
 }
 
@@ -482,15 +481,15 @@ static GSList *ldapqry_add_list_values(
  */
 static GSList *ldapqry_add_single_value( LDAP *ld, LDAPMessage *entry, char *attr ) {
        GSList *list = NULL;
-       gchar **vals;
+       struct berval **vals;
 
-       if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
+       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_strdup( vals[0] ) );
+                       list = g_slist_append( list, g_strndup( vals[0]->bv_val, vals[0]->bv_len ));
                }
        }
-       ldap_value_free( vals );
+       ldap_value_free_len( vals );
        return list;
 }
 
@@ -700,9 +699,10 @@ void ldapqry_touch( LdapQuery *qry ) {
  */
 static gint ldapqry_connect( LdapQuery *qry ) {
        LdapControl *ctl;
-       LDAP *ld;
+       LDAP *ld = NULL;
        gint rc;
        gint version;
+       gchar *uri = NULL;
 
        /* Initialize connection */
        /* printf( "===ldapqry_connect===\n" ); */
@@ -714,9 +714,16 @@ static gint ldapqry_connect( LdapQuery *qry ) {
        qry->startTime = qry->touchTime;
        qry->elapsedTime = -1;
        ADDRQUERY_RETVAL(qry) = LDAPRC_INIT;
-       if( ( ld = ldap_init( ctl->hostName, ctl->port ) ) == NULL ) {
+
+       uri = g_strdup_printf("ldap%s://%s:%d",
+                               ctl->enableSSL?"s":"",
+                               ctl->hostName, ctl->port);
+       ldap_initialize(&ld, uri);
+       g_free(uri);
+
+       if (ld == NULL)
                return ADDRQUERY_RETVAL(qry);
-       }
+
        qry->ldap = ld;
        ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
        if( ldapqry_get_stop_flag( qry ) ) {
@@ -737,13 +744,13 @@ static gint ldapqry_connect( LdapQuery *qry ) {
        }
 
        if( ctl->version == LDAP_VERSION3 ) {
-               if( ctl->enableTLS ) {
+               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 ) );
-                       */
+                       
+                       /* printf( "rc=%d\n", rc );
+                       printf( "LDAP Status: set_option: %s\n", ldap_err2string( rc ) ); */
+                       
                        if( rc != LDAP_SUCCESS ) {
                                return ADDRQUERY_RETVAL(qry);
                        }
@@ -756,7 +763,7 @@ static gint ldapqry_connect( LdapQuery *qry ) {
        if( ctl->bindDN ) {
                if( * ctl->bindDN != '\0' ) {
                        /* printf( "binding...\n" ); */
-                       rc = ldap_simple_bind_s( ld, ctl->bindDN, ctl->bindPass );
+                       rc = claws_ldap_simple_bind_s( ld, ctl->bindDN, ctl->bindPass );
                        /* printf( "rc=%d\n", rc ); */
                        if( rc != LDAP_SUCCESS ) {
                                /*
@@ -785,7 +792,7 @@ static gint ldapqry_connect( LdapQuery *qry ) {
  */
 static gint ldapqry_disconnect( LdapQuery *qry ) {
        /* Disconnect */
-       if( qry->ldap ) ldap_unbind( qry->ldap );
+       if( qry->ldap ) ldap_unbind_ext( qry->ldap, NULL, NULL );
        qry->ldap = NULL;
 
        ldapqry_touch( qry );
@@ -1204,17 +1211,19 @@ static GList *ldapqry_load_attrib_values(
 {
        GList *list = NULL;
        gint i;
-       gchar **vals;
+       struct berval **vals;
        NameValuePair *nvp;
 
        list = listValues;
-       if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
+       if( ( vals = ldap_get_values_len( ld, entry, attr ) ) != NULL ) {
                for( i = 0; vals[i] != NULL; i++ ) {
-                       nvp = ldapqry_create_name_value( attr, vals[i] );
+                       gchar *tmp = g_strndup( vals[i]->bv_val, vals[i]->bv_len);
+                       nvp = ldapqry_create_name_value( attr, tmp );
+                       g_free(tmp);
                        list = g_list_append( list, nvp );
                }
        }
-       ldap_value_free( vals );
+       ldap_value_free_len( vals );
        return list;
 }