2010-05-22 [mir] 3.7.6cvs8
[claws.git] / src / ldapctrl.c
index 891019f4bcc62bd436b5b81b58daca07c628e630..e436c0ef4640fce660c8354126a6cb624ef990e5 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2003-2007 Match Grun and the Claws Mail team
+ * Copyright (C) 2003-2009 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
 
 #include "ldapctrl.h"
 #include "mgutils.h"
+#include "passcrypt.h"
 #include "editaddress_other_attributes_ldap.h"
+#include "common/utils.h"
+#include "common/quoted-printable.h"
 
 /**
  * Create new LDAP control block object.
@@ -76,7 +79,12 @@ LdapControl *ldapctl_create( void ) {
  */
 void ldapctl_set_host( LdapControl* ctl, const gchar *value ) {
        ctl->hostName = mgu_replace_string( ctl->hostName, value );
+
+       if ( ctl->hostName == NULL )
+               return;
+
        g_strstrip( ctl->hostName );
+       debug_print("setting hostname: %s\n", ctl->hostName);
 }
 
 /**
@@ -91,6 +99,7 @@ void ldapctl_set_port( LdapControl* ctl, const gint value ) {
        else {
                ctl->port = LDAPCTL_DFL_PORT;
        }
+       debug_print("setting port: %d\n", ctl->port);
 }
 
 /**
@@ -100,7 +109,12 @@ 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 );
+
+       if ( ctl->baseDN == NULL )
+               return;
+
        g_strstrip( ctl->baseDN );
+       debug_print("setting baseDN: %s\n", ctl->baseDN);
 }
 
 /**
@@ -110,17 +124,98 @@ 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 );
+
+       if ( ctl->bindDN == NULL )
+               return;
+
        g_strstrip( ctl->bindDN );
+       debug_print("setting bindDN: %s\n", ctl->bindDN);
 }
 
 /**
  * Specify bind password to be used.
  * \param ctl  Control object to process.
  * \param value Password.
+ * \param encrypt Encrypt password
+ * \param change Save encrypted
  */
-void ldapctl_set_bind_password( LdapControl* ctl, const gchar *value ) {
+void ldapctl_set_bind_password( 
+       LdapControl* ctl, const gchar *value, gboolean encrypt, gboolean change ) {
+       gchar *buf, *tmp;
+
        ctl->bindPass = mgu_replace_string( ctl->bindPass, value );
+
+       if ( ctl->bindPass == NULL )
+               return;
+
        g_strstrip( ctl->bindPass );
+       
+       buf = tmp = NULL;
+       if ( encrypt ) {
+               /* If first char is not ! the password is not encrypted */
+               if (ctl->bindPass[0] == '!' || change) {
+                       if (ctl->bindPass[0] != '!' && change)
+                               buf = mgu_replace_string( buf, ctl->bindPass );
+                       else {
+                               if (ctl->bindPass[1] != '|')
+                                       buf = mgu_replace_string( buf, ctl->bindPass + 1 );
+                               else {
+                                       /* quoted printable decode */
+                                       buf = mgu_replace_string( buf, ctl->bindPass + 2 );
+                                       qp_decode_line(buf);
+                               }
+                       }
+                       
+                       passcrypt_encrypt( buf, strlen(buf) );
+                       if (ctl->bindPass[0] != '!' && change) {
+                               /* quoted printable encode */
+                               tmp = g_malloc0(qp_get_q_encoding_len(buf) + 1);
+                               qp_q_encode(tmp, buf);
+                               g_free(buf);
+                               buf = g_strconcat( "!|", tmp, NULL );
+                               g_free(tmp);
+                       }
+
+                       ctl->bindPass = mgu_replace_string( ctl->bindPass, buf );
+                       g_free(buf);
+                       
+               }
+       }
+       debug_print("setting bindPassword\n");
+}
+
+/**
+ * Fetch bind password to be used.
+ * \param ctl  Control object to process.
+ * \return Decrypted password.
+ */
+gchar* ldapctl_get_bind_password( LdapControl* ctl ) {
+       gchar *pwd = NULL, *buf;
+
+       if ( ctl->bindPass != NULL ) {
+               pwd = mgu_replace_string( pwd, ctl->bindPass );
+               /* If first char is not ! the password is not encrypted */
+               if (pwd && pwd[0] == '!') {
+                       if (pwd[1] && pwd[1] == '|') {
+                               buf = g_strdup(pwd + 2);
+                               /* quoted printable decode */
+                               qp_decode_line(buf);
+                       }
+                       else {
+                               buf = g_strdup(pwd + 1);
+                       }
+                       g_free(pwd);
+                       
+                       passcrypt_decrypt( buf, strlen(buf) );
+
+                       pwd = g_strdup(buf);
+                       g_free(buf);
+               }
+       }
+
+       debug_print("getting bindPassword\n");
+
+       return pwd;
 }
 
 /**
@@ -135,6 +230,7 @@ void ldapctl_set_max_entries( LdapControl* ctl, const gint value ) {
        else {
                ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
        }
+       debug_print("setting maxEntries: %d\n", ctl->maxEntries);
 }
 
 /**
@@ -149,6 +245,7 @@ void ldapctl_set_timeout( LdapControl* ctl, const gint value ) {
        else {
                ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
        }
+       debug_print("setting timeOut: %d\n", ctl->timeOut);
 }
 
 /**
@@ -166,6 +263,7 @@ void ldapctl_set_max_query_age( LdapControl* ctl, const gint value ) {
        else {
                ctl->maxQueryAge = value;
        }
+       debug_print("setting maxAge: %d\n", ctl->maxQueryAge);
 }
 
 /**
@@ -187,6 +285,7 @@ void ldapctl_set_matching_option( LdapControl* ctl, const gint value ) {
        else {
                ctl->matchingOption = value;
        }
+       debug_print("setting matchingOption: %d\n", ctl->matchingOption);
 }
 
 /**
@@ -196,10 +295,12 @@ void ldapctl_set_matching_option( LdapControl* ctl, const gint 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);
 }
 
 /**
@@ -212,7 +313,7 @@ void ldapctl_set_ssl( LdapControl* ctl, const gboolean value ) {
  *         <code>ldapctl_criteria_list_add()</code> functions for this purpose.
  */
 GList *ldapctl_get_criteria_list( const LdapControl* ctl ) {
-       g_return_val_if_fail( ctl != NULL, NULL );
+       cm_return_val_if_fail( ctl != NULL, NULL );
        return ctl->listCriteria;
 }
 
@@ -221,7 +322,7 @@ GList *ldapctl_get_criteria_list( const LdapControl* ctl ) {
  * \param  ctl  Control data object.
  */
 void ldapctl_criteria_list_clear( LdapControl *ctl ) {
-       g_return_if_fail( ctl != NULL );
+       cm_return_if_fail( ctl != NULL );
        mgu_free_dlist( ctl->listCriteria );
        ctl->listCriteria = NULL;
 }
@@ -233,9 +334,10 @@ void ldapctl_criteria_list_clear( LdapControl *ctl ) {
  *             be appended to the list.
  */
 void ldapctl_criteria_list_add( LdapControl *ctl, gchar *attr ) {
-       g_return_if_fail( ctl != NULL );
+       cm_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 ) );
                }
@@ -247,8 +349,9 @@ void ldapctl_criteria_list_add( LdapControl *ctl, gchar *attr ) {
  * \param ctl Control object to clear.
  */
 static void ldapctl_clear( LdapControl *ctl ) {
-       g_return_if_fail( ctl != NULL );
+       cm_return_if_fail( ctl != NULL );
 
+       debug_print("clearing ldap controller members\n");
        /* Free internal stuff */
        g_free( ctl->hostName );
        g_free( ctl->baseDN );
@@ -287,8 +390,9 @@ static void ldapctl_clear( LdapControl *ctl ) {
  * \param ctl Control object to free.
  */
 void ldapctl_free( LdapControl *ctl ) {
-       g_return_if_fail( ctl != NULL );
+       cm_return_if_fail( ctl != NULL );
 
+       debug_print("releasing requested memory for ldap controller\n");
        /* Free internal stuff */
        ldapctl_clear( ctl );
 
@@ -307,20 +411,23 @@ void ldapctl_free( LdapControl *ctl ) {
  * \param stream Output stream.
  */
 void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
-       g_return_if_fail( ctl != NULL );
+       cm_return_if_fail( ctl != NULL );
+       gchar *pwd;
 
        pthread_mutex_lock( ctl->mutexCtl );
        fprintf( stream, "LdapControl:\n" );
-       fprintf( stream, "host name: '%s'\n", ctl->hostName );
+       fprintf( stream, "host name: '%s'\n", ctl->hostName?ctl->hostName:"null" );
        fprintf( stream, "     port: %d\n",   ctl->port );
-       fprintf( stream, "  base dn: '%s'\n", ctl->baseDN );
-       fprintf( stream, "  bind dn: '%s'\n", ctl->bindDN );
-       fprintf( stream, "bind pass: '%s'\n", ctl->bindPass );
-       fprintf( stream, "attr mail: '%s'\n", ctl->attribEMail );
-       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, "  base dn: '%s'\n", ctl->baseDN?ctl->baseDN:"null" );
+       fprintf( stream, "  bind dn: '%s'\n", ctl->bindDN?ctl->bindDN:"null" );
+       pwd = ldapctl_get_bind_password((LdapControl *) ctl);
+       fprintf( stream, "bind pass: '%s'\n", pwd?pwd:"null" );
+       g_free(pwd);
+       fprintf( stream, "attr mail: '%s'\n", ctl->attribEMail?ctl->attribEMail:"null" );
+       fprintf( stream, "attr comn: '%s'\n", ctl->attribCName?ctl->attribCName:"null" );
+       fprintf( stream, "attr frst: '%s'\n", ctl->attribFName?ctl->attribFName:"null" );
+       fprintf( stream, "attr last: '%s'\n", ctl->attribLName?ctl->attribLName:"null" );
+       fprintf( stream, "attr disn: '%s'\n", ctl->attribDName?ctl->attribDName:"null" );
        fprintf( stream, "max entry: %d\n",   ctl->maxEntries );
        fprintf( stream, "  timeout: %d\n",   ctl->timeOut );
        fprintf( stream, "  max age: %d\n",   ctl->maxQueryAge );
@@ -347,9 +454,10 @@ void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
 void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
        GList *node;
 
-       g_return_if_fail( ctlFrom != NULL );
-       g_return_if_fail( ctlTo != NULL );
+       cm_return_if_fail( ctlFrom != NULL );
+       cm_return_if_fail( ctlTo != NULL );
 
+       debug_print("ldap controller copy\n");
        /* Lock both objects */
        pthread_mutex_lock( ctlFrom->mutexCtl );
        pthread_mutex_lock( ctlTo->mutexCtl );
@@ -463,6 +571,7 @@ static gchar *ldapctl_build_ldap_criteria(
                g_free( p2 );
        }
        g_free( term );
+       debug_print("search criteria: %s\n", crit?crit:"null");
        return crit;
 }
 
@@ -488,8 +597,8 @@ gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
        gchar *p1, *p2, *retVal;
        gchar *criteriaFmt;
 
-       g_return_val_if_fail( ctl != NULL, NULL );
-       g_return_val_if_fail( searchVal != NULL, NULL );
+       cm_return_val_if_fail( ctl != NULL, NULL );
+       cm_return_val_if_fail( searchVal != NULL, NULL );
 
        /* Test whether there are more that one search terms */
        retVal = ldapctl_build_ldap_criteria( searchVal, ctl->matchingOption );
@@ -509,7 +618,6 @@ gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
        node = ctl->listCriteria;
        while( node ) {
                gchar *attr, *tmp;
-
                attr = node->data;
                node = g_list_next( node );
 
@@ -544,6 +652,7 @@ gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
                retVal = p2;
                g_free( p1 );
        }
+       debug_print("current search string: %s\n", retVal);
        return retVal;
 }
 
@@ -553,29 +662,59 @@ gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
  * \return NULL terminated list.
  */
 char **ldapctl_attribute_array( LdapControl *ctl ) {
+       char **ptrArray;
+       GList *node;
+       gint cnt, i;
+       cm_return_val_if_fail( ctl != NULL, NULL );
+
+       node = ctl->listCriteria;
+       cnt = g_list_length( ctl->listCriteria );
+       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;
+       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 );
+       cm_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();
-       /* check if this servers config is updated to the new
-        * default list of search criteria. If not update the list */
-       if (! ldapctl_compare_list(ctl->listCriteria, def)) {
-               /* Deep copy search criteria */
-               ldapctl_criteria_list_clear(ctl);
-               while(def) {
-                       ctl->listCriteria = g_list_append(
-                               ctl->listCriteria, g_strdup(def->data));
-                       def = g_list_next(def);
+       
+       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 = ctl->listCriteria;
-       cnt = g_list_length( ctl->listCriteria );
-       ptrArray = g_new0( char *, 1 + cnt );
+
+       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;
@@ -615,7 +754,7 @@ void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
        gchar *attrib;
        gint iLen;
 
-       g_return_if_fail( ctl != NULL );
+       cm_return_if_fail( ctl != NULL );
 
        ldapctl_criteria_list_clear( ctl );
        if( criteria == NULL ) return;
@@ -645,12 +784,15 @@ void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
  * \return Formatted string or <i>""</i>. Should be g_free() when done.
  */
 gchar *ldapctl_get_default_criteria() {
-       gchar *retVal = LDAPCTL_DFL_ATTR_LIST;
+       gchar *retVal = g_strdup(LDAPCTL_DFL_ATTR_LIST);
        const gchar **attrs = ATTRIBUTE; 
 
        while (*attrs) {
-               retVal = g_strdup_printf("%s, %s", retVal, *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;
 }
 
@@ -674,6 +816,7 @@ GList *ldapctl_get_default_criteria_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);
        }
@@ -696,6 +839,7 @@ gboolean ldapctl_compare_list(GList *l1, GList *l2) {
        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;
                }