2007-08-21 [colin] 2.10.0cvs142
[claws.git] / src / ldapctrl.c
index 391694b392a17204ccd81cbde7ff3b0ddd6a94df..9207d3d583798f46ab778c39e4a9a335f6a1f5b7 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2003 Match Grun
+ * 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
  *
  * 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,
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 /*
  */
 
 /*
@@ -33,6 +33,8 @@
 
 #include "ldapctrl.h"
 #include "mgutils.h"
 
 #include "ldapctrl.h"
 #include "mgutils.h"
+#include "editaddress_other_attributes_ldap.h"
+#include "common/utils.h"
 
 /**
  * Create new LDAP control block object.
 
 /**
  * Create new LDAP control block object.
@@ -52,10 +54,14 @@ LdapControl *ldapctl_create( void ) {
        ctl->attribCName = g_strdup( LDAPCTL_ATTR_COMMONNAME );
        ctl->attribFName = g_strdup( LDAPCTL_ATTR_GIVENNAME );
        ctl->attribLName = g_strdup( LDAPCTL_ATTR_SURNAME );
        ctl->attribCName = g_strdup( LDAPCTL_ATTR_COMMONNAME );
        ctl->attribFName = g_strdup( LDAPCTL_ATTR_GIVENNAME );
        ctl->attribLName = g_strdup( LDAPCTL_ATTR_SURNAME );
+       ctl->attribDName = g_strdup( LDAPCTL_ATTR_DISPLAYNAME );
        ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
        ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
        ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
        ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
        ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
        ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
        ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
        ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
+       ctl->version = 0;
+       ctl->enableTLS = FALSE;
+       ctl->enableSSL = FALSE;
 
        /* Mutex to protect control block */
        ctl->mutexCtl = g_malloc0( sizeof( pthread_mutex_t ) );
 
        /* Mutex to protect control block */
        ctl->mutexCtl = g_malloc0( sizeof( pthread_mutex_t ) );
@@ -72,6 +78,7 @@ LdapControl *ldapctl_create( void ) {
 void ldapctl_set_host( LdapControl* ctl, const gchar *value ) {
        ctl->hostName = mgu_replace_string( ctl->hostName, value );
        g_strstrip( ctl->hostName );
 void ldapctl_set_host( LdapControl* ctl, const gchar *value ) {
        ctl->hostName = mgu_replace_string( ctl->hostName, value );
        g_strstrip( ctl->hostName );
+       debug_print("setting hostname: %s\n", ctl->hostName);
 }
 
 /**
 }
 
 /**
@@ -86,6 +93,7 @@ void ldapctl_set_port( LdapControl* ctl, const gint value ) {
        else {
                ctl->port = LDAPCTL_DFL_PORT;
        }
        else {
                ctl->port = LDAPCTL_DFL_PORT;
        }
+       debug_print("setting port: %d\n", ctl->port);
 }
 
 /**
 }
 
 /**
@@ -96,6 +104,7 @@ void ldapctl_set_port( LdapControl* ctl, const gint value ) {
 void ldapctl_set_base_dn( LdapControl* ctl, const gchar *value ) {
        ctl->baseDN = mgu_replace_string( ctl->baseDN, value );
        g_strstrip( ctl->baseDN );
 void ldapctl_set_base_dn( LdapControl* ctl, const gchar *value ) {
        ctl->baseDN = mgu_replace_string( ctl->baseDN, value );
        g_strstrip( ctl->baseDN );
+       debug_print("setting baseDN: %s\n", ctl->baseDN);
 }
 
 /**
 }
 
 /**
@@ -106,6 +115,7 @@ void ldapctl_set_base_dn( LdapControl* ctl, const gchar *value ) {
 void ldapctl_set_bind_dn( LdapControl* ctl, const gchar *value ) {
        ctl->bindDN = mgu_replace_string( ctl->bindDN, value );
        g_strstrip( ctl->bindDN );
 void ldapctl_set_bind_dn( LdapControl* ctl, const gchar *value ) {
        ctl->bindDN = mgu_replace_string( ctl->bindDN, value );
        g_strstrip( ctl->bindDN );
+       debug_print("setting bindDN: %s\n", ctl->bindDN);
 }
 
 /**
 }
 
 /**
@@ -116,6 +126,7 @@ void ldapctl_set_bind_dn( LdapControl* ctl, const gchar *value ) {
 void ldapctl_set_bind_password( LdapControl* ctl, const gchar *value ) {
        ctl->bindPass = mgu_replace_string( ctl->bindPass, value );
        g_strstrip( ctl->bindPass );
 void ldapctl_set_bind_password( LdapControl* ctl, const gchar *value ) {
        ctl->bindPass = mgu_replace_string( ctl->bindPass, value );
        g_strstrip( ctl->bindPass );
+       debug_print("setting bindPassword");
 }
 
 /**
 }
 
 /**
@@ -130,6 +141,7 @@ void ldapctl_set_max_entries( LdapControl* ctl, const gint value ) {
        else {
                ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
        }
        else {
                ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
        }
+       debug_print("setting maxEntries: %d\n", ctl->maxEntries);
 }
 
 /**
 }
 
 /**
@@ -144,6 +156,7 @@ void ldapctl_set_timeout( LdapControl* ctl, const gint value ) {
        else {
                ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
        }
        else {
                ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
        }
+       debug_print("setting timeOut: %d\n", ctl->timeOut);
 }
 
 /**
 }
 
 /**
@@ -161,6 +174,7 @@ void ldapctl_set_max_query_age( LdapControl* ctl, const gint value ) {
        else {
                ctl->maxQueryAge = value;
        }
        else {
                ctl->maxQueryAge = value;
        }
+       debug_print("setting maxAge: %d\n", ctl->maxQueryAge);
 }
 
 /**
 }
 
 /**
@@ -182,17 +196,22 @@ void ldapctl_set_matching_option( LdapControl* ctl, const gint value ) {
        else {
                ctl->matchingOption = value;
        }
        else {
                ctl->matchingOption = value;
        }
+       debug_print("setting matchingOption: %d\n", ctl->matchingOption);
 }
 
 /**
 }
 
 /**
- * Specify search criteria list to be used.
- * \param ctl   Control data object.
- * \param value Linked list of LDAP attribute names to use for search.
+ * Specify TLS option.
+ * \param ctl   Control object to process.
+ * \param value <i>TRUE</i> to enable TLS.
  */
  */
-void ldapctl_set_criteria_list( LdapControl* ctl, GList *value ) {
-       g_return_if_fail( ctl != NULL );
-       mgu_free_dlist( ctl->listCriteria );
-       ctl->listCriteria = value;
+void ldapctl_set_tls( LdapControl* ctl, const gboolean value ) {
+       ctl->enableTLS = value;
+       debug_print("setting TLS: %d\n", ctl->enableTLS);
+}
+
+void ldapctl_set_ssl( LdapControl* ctl, const gboolean value ) {
+       ctl->enableSSL = value;
+       debug_print("setting SSL: %d\n", ctl->enableSSL);
 }
 
 /**
 }
 
 /**
@@ -229,33 +248,21 @@ void ldapctl_criteria_list_add( LdapControl *ctl, gchar *attr ) {
        g_return_if_fail( ctl != NULL );
        if( attr != NULL ) {
                if( mgu_list_test_unq_nc( ctl->listCriteria, attr ) ) {
        g_return_if_fail( ctl != NULL );
        if( attr != NULL ) {
                if( mgu_list_test_unq_nc( ctl->listCriteria, attr ) ) {
+                       debug_print("adding to criteria list: %s\n", attr);
                        ctl->listCriteria = g_list_append(
                                ctl->listCriteria, g_strdup( attr ) );
                }
        }
 }
 
                        ctl->listCriteria = g_list_append(
                                ctl->listCriteria, g_strdup( attr ) );
                }
        }
 }
 
-/**
- * Build criteria list using default attributes.
- * \param ctl  Control object to process.
- */
-void ldapctl_default_attributes( LdapControl *ctl ) {
-       g_return_if_fail( ctl != NULL );
-
-       ldapctl_criteria_list_clear( ctl );
-       ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_COMMONNAME );
-       ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_GIVENNAME );
-       ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_SURNAME );
-       ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_EMAIL );
-}
-
 /**
  * Clear LDAP server member variables.
  * \param ctl Control object to clear.
  */
 /**
  * Clear LDAP server member variables.
  * \param ctl Control object to clear.
  */
-void ldapctl_clear( LdapControl *ctl ) {
+static void ldapctl_clear( LdapControl *ctl ) {
        g_return_if_fail( ctl != NULL );
 
        g_return_if_fail( ctl != NULL );
 
+       debug_print("clearing ldap controller members\n");
        /* Free internal stuff */
        g_free( ctl->hostName );
        g_free( ctl->baseDN );
        /* Free internal stuff */
        g_free( ctl->hostName );
        g_free( ctl->baseDN );
@@ -265,6 +272,7 @@ void ldapctl_clear( LdapControl *ctl ) {
        g_free( ctl->attribCName );
        g_free( ctl->attribFName );
        g_free( ctl->attribLName );
        g_free( ctl->attribCName );
        g_free( ctl->attribFName );
        g_free( ctl->attribLName );
+       g_free( ctl->attribDName );
 
        ldapctl_criteria_list_clear( ctl );
 
 
        ldapctl_criteria_list_clear( ctl );
 
@@ -278,10 +286,14 @@ void ldapctl_clear( LdapControl *ctl ) {
        ctl->attribCName = NULL;
        ctl->attribFName = NULL;
        ctl->attribLName = NULL;
        ctl->attribCName = NULL;
        ctl->attribFName = NULL;
        ctl->attribLName = NULL;
+       ctl->attribDName = NULL;
        ctl->maxEntries = 0;
        ctl->timeOut = 0;
        ctl->maxQueryAge = 0;
        ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
        ctl->maxEntries = 0;
        ctl->timeOut = 0;
        ctl->maxQueryAge = 0;
        ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
+       ctl->version = 0;
+       ctl->enableTLS = FALSE;
+       ctl->enableSSL = FALSE;
 }
 
 /**
 }
 
 /**
@@ -291,6 +303,7 @@ void ldapctl_clear( LdapControl *ctl ) {
 void ldapctl_free( LdapControl *ctl ) {
        g_return_if_fail( ctl != NULL );
 
 void ldapctl_free( LdapControl *ctl ) {
        g_return_if_fail( ctl != NULL );
 
+       debug_print("releasing requested memory for ldap controller\n");
        /* Free internal stuff */
        ldapctl_clear( ctl );
 
        /* Free internal stuff */
        ldapctl_clear( ctl );
 
@@ -303,30 +316,6 @@ void ldapctl_free( LdapControl *ctl ) {
        g_free( ctl );
 }
 
        g_free( ctl );
 }
 
-/**
- * Setup default (empty) values for specified object.
- * \param ctl  Control object to process.
- */
-void ldapctl_default_values( LdapControl *ctl ) {
-       g_return_if_fail( ctl != NULL );
-
-       /* Clear our destination */
-       ldapctl_clear( ctl );
-
-       /* Copy strings */
-       ctl->hostName = g_strdup( "" );
-       ctl->baseDN = g_strdup( "" );
-       ctl->bindDN = g_strdup( "" );
-       ctl->bindPass = g_strdup( "" );
-       ctl->port = LDAPCTL_DFL_PORT;
-       ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
-       ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
-       ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
-       ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
-
-       ldapctl_default_attributes( ctl );
-}
-
 /**
  * Display object to specified stream.
  * \param ctl    Control object to process.
 /**
  * Display object to specified stream.
  * \param ctl    Control object to process.
@@ -346,10 +335,14 @@ void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
        fprintf( stream, "attr comn: '%s'\n", ctl->attribCName );
        fprintf( stream, "attr frst: '%s'\n", ctl->attribFName );
        fprintf( stream, "attr last: '%s'\n", ctl->attribLName );
        fprintf( stream, "attr comn: '%s'\n", ctl->attribCName );
        fprintf( stream, "attr frst: '%s'\n", ctl->attribFName );
        fprintf( stream, "attr last: '%s'\n", ctl->attribLName );
+       fprintf( stream, "attr disn: '%s'\n", ctl->attribDName );
        fprintf( stream, "max entry: %d\n",   ctl->maxEntries );
        fprintf( stream, "  timeout: %d\n",   ctl->timeOut );
        fprintf( stream, "  max age: %d\n",   ctl->maxQueryAge );
        fprintf( stream, "match opt: %d\n",   ctl->matchingOption );
        fprintf( stream, "max entry: %d\n",   ctl->maxEntries );
        fprintf( stream, "  timeout: %d\n",   ctl->timeOut );
        fprintf( stream, "  max age: %d\n",   ctl->maxQueryAge );
        fprintf( stream, "match opt: %d\n",   ctl->matchingOption );
+       fprintf( stream, "  version: %d\n",   ctl->version );
+       fprintf( stream, "      TLS: %s\n",   ctl->enableTLS ? "yes" : "no" );
+       fprintf( stream, "      SSL: %s\n",   ctl->enableSSL ? "yes" : "no" );
        fprintf( stream, "crit list:\n" );
        if( ctl->listCriteria ) {
                mgu_print_dlist( ctl->listCriteria, stream );
        fprintf( stream, "crit list:\n" );
        if( ctl->listCriteria ) {
                mgu_print_dlist( ctl->listCriteria, stream );
@@ -372,6 +365,7 @@ void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
        g_return_if_fail( ctlFrom != NULL );
        g_return_if_fail( ctlTo != NULL );
 
        g_return_if_fail( ctlFrom != NULL );
        g_return_if_fail( ctlTo != NULL );
 
+       debug_print("ldap controller copy\n");
        /* Lock both objects */
        pthread_mutex_lock( ctlFrom->mutexCtl );
        pthread_mutex_lock( ctlTo->mutexCtl );
        /* Lock both objects */
        pthread_mutex_lock( ctlFrom->mutexCtl );
        pthread_mutex_lock( ctlTo->mutexCtl );
@@ -388,6 +382,7 @@ void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
        ctlTo->attribCName = g_strdup( ctlFrom->attribCName );
        ctlTo->attribFName = g_strdup( ctlFrom->attribFName );
        ctlTo->attribLName = g_strdup( ctlFrom->attribLName );
        ctlTo->attribCName = g_strdup( ctlFrom->attribCName );
        ctlTo->attribFName = g_strdup( ctlFrom->attribFName );
        ctlTo->attribLName = g_strdup( ctlFrom->attribLName );
+       ctlTo->attribDName = g_strdup( ctlFrom->attribDName );
 
        /* Copy search criteria */
        node = ctlFrom->listCriteria;
 
        /* Copy search criteria */
        node = ctlFrom->listCriteria;
@@ -403,6 +398,9 @@ void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
        ctlTo->timeOut = ctlFrom->timeOut;
        ctlTo->maxQueryAge = ctlFrom->maxQueryAge;
        ctlTo->matchingOption = ctlFrom->matchingOption;
        ctlTo->timeOut = ctlFrom->timeOut;
        ctlTo->maxQueryAge = ctlFrom->maxQueryAge;
        ctlTo->matchingOption = ctlFrom->matchingOption;
+       ctlTo->version = ctlFrom->version;
+       ctlTo->enableTLS = ctlFrom->enableTLS;
+       ctlTo->enableSSL = ctlFrom->enableSSL;
 
        /* Unlock */
        pthread_mutex_unlock( ctlTo->mutexCtl );
 
        /* Unlock */
        pthread_mutex_unlock( ctlTo->mutexCtl );
@@ -481,6 +479,7 @@ static gchar *ldapctl_build_ldap_criteria(
                g_free( p2 );
        }
        g_free( term );
                g_free( p2 );
        }
        g_free( term );
+       debug_print("search criteria: %s\n", crit);
        return crit;
 }
 
        return crit;
 }
 
@@ -527,7 +526,6 @@ gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
        node = ctl->listCriteria;
        while( node ) {
                gchar *attr, *tmp;
        node = ctl->listCriteria;
        while( node ) {
                gchar *attr, *tmp;
-
                attr = node->data;
                node = g_list_next( node );
 
                attr = node->data;
                node = g_list_next( node );
 
@@ -562,6 +560,7 @@ gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
                retVal = p2;
                g_free( p1 );
        }
                retVal = p2;
                g_free( p1 );
        }
+       debug_print("current search string: %s\n", retVal);
        return retVal;
 }
 
        return retVal;
 }
 
@@ -576,12 +575,54 @@ char **ldapctl_attribute_array( LdapControl *ctl ) {
        gint cnt, i;
        g_return_val_if_fail( ctl != NULL, NULL );
 
        gint cnt, i;
        g_return_val_if_fail( ctl != NULL, NULL );
 
+       node = ctl->listCriteria;
        cnt = g_list_length( ctl->listCriteria );
        ptrArray = g_new0( char *, 1 + cnt );
        i = 0;
        cnt = g_list_length( ctl->listCriteria );
        ptrArray = g_new0( char *, 1 + cnt );
        i = 0;
-       node = ctl->listCriteria;
        while( node ) {
                ptrArray[ i++ ] = node->data;
        while( node ) {
                ptrArray[ i++ ] = node->data;
+               /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
+               node = g_list_next( node );
+       }
+       ptrArray[ i ] = NULL;
+       return ptrArray;
+}
+
+/**
+ * Return array of pointers to attributes for LDAP query.
+ * \param  ctl  Control object to process.
+ * \return NULL terminated list.
+ */
+char **ldapctl_full_attribute_array( LdapControl *ctl ) {
+       char **ptrArray;
+       GList *node, *def;
+       GList *tmp = NULL;
+       gint cnt, i;
+       g_return_val_if_fail( ctl != NULL, NULL );
+
+       def = ctl->listCriteria;
+       while (def) {
+               tmp = g_list_append(tmp, g_strdup(def->data));
+               def = def->next;
+       }
+
+       def = ldapctl_get_default_criteria_list();
+       
+       while (def) {
+               if( g_list_find_custom(tmp, (gpointer)def->data, 
+                               (GCompareFunc)strcmp2) == NULL) {
+                       tmp = g_list_append(tmp, g_strdup(def->data));
+               }
+               def = def->next;
+       }
+
+       node = tmp;
+       cnt = g_list_length( tmp );
+       ptrArray = g_new0( char *, 1 + cnt );
+       i = 0;
+       while( node ) {
+               ptrArray[ i++ ] = node->data;
+               /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
                node = g_list_next( node );
        }
        ptrArray[ i ] = NULL;
                node = g_list_next( node );
        }
        ptrArray[ i ] = NULL;
@@ -646,6 +687,76 @@ void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
        }
 }
 
        }
 }
 
+/**
+ * Return the default LDAP search criteria string.
+ * \return Formatted string or <i>""</i>. Should be g_free() when done.
+ */
+gchar *ldapctl_get_default_criteria() {
+       gchar *retVal = g_strdup(LDAPCTL_DFL_ATTR_LIST);
+       const gchar **attrs = ATTRIBUTE; 
+
+       while (*attrs) {
+               gchar *tmp = g_strdup_printf("%s, %s", retVal, *attrs++);
+               g_free(retVal);
+               retVal = tmp;
+       }
+       debug_print("default search criteria: %s\n", retVal);
+       return retVal;
+}
+
+/**
+ * Return the default LDAP search criteria list.
+ * \return GList or <i>NULL</i>.
+ */
+GList *ldapctl_get_default_criteria_list() {
+       gchar *criteria, *item;
+       gchar **c_list, **w_list;
+       GList *attr_list = NULL;
+
+       criteria = ldapctl_get_default_criteria();
+       c_list = g_strsplit(criteria, " ", 0);
+       g_free(criteria);
+       criteria = NULL;
+       w_list = c_list;
+       while ((criteria = *w_list++) != 0) {
+               /* copy string elimination <,> */
+               if (*w_list)
+                       item = g_strndup(criteria, strlen(criteria) - 1);
+               else
+                       item = g_strdup(criteria);
+               debug_print("adding attribute to list: %s\n", item);
+               attr_list = g_list_append(attr_list, g_strdup(item));
+               g_free(item);
+       }
+       g_strfreev(c_list);
+       return attr_list;
+}
+
+/**
+ * Compare to GList for equality.
+ * \param l1 First GList
+ * \param l2 Second GList
+ * \Return TRUE or FALSE
+ */
+gboolean ldapctl_compare_list(GList *l1, GList *l2) {
+       gchar *first, *second;
+       if (! l1 && ! l2)
+               return TRUE;
+       if ((! l1 && l2) || (l1 && ! l2))
+               return FALSE;
+       while (l1 && l2) {
+               first = (gchar *) l1->data;
+               second = (gchar *) l2->data;
+               /*debug_print("comparing: %s = %s\n", first, second);*/
+               if ( ! (first && second) || strcmp(first, second) != 0) {
+                       return FALSE;
+               }
+               l1 = g_list_next(l1);
+               l2 = g_list_next(l2);
+       }
+       return TRUE;
+}
+
 #endif /* USE_LDAP */
 
 /*
 #endif /* USE_LDAP */
 
 /*