2007-08-22 [wwp] 2.10.0cvs151
[claws.git] / src / ldapquery.c
index 6316cd173973f8d073e9fa5ac98289b6fba0be8e..1da82a4cb2f63c7860848891a9a444cce61d0638 100644 (file)
@@ -4,7 +4,7 @@
  *
  * 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,8 @@
  * 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/>.
+ * 
  */
 
 /*
@@ -39,6 +39,7 @@
 
 #include "addritem.h"
 #include "addrcache.h"
+#include "common/utils.h"
 
 /*
  * Key for thread specific data.
@@ -113,8 +114,12 @@ void ldapqry_set_control( LdapQuery *qry, LdapControl *ctl ) {
  * \param value Name.
  */
 void ldapqry_set_name( LdapQuery* qry, const gchar *value ) {
+       g_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 +128,12 @@ void ldapqry_set_name( LdapQuery* qry, const gchar *value ) {
  * \param value 
  */
 void ldapqry_set_search_value( LdapQuery *qry, const gchar *value ) {
+       g_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));
 }
 
 /**
@@ -159,6 +168,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 ) {
+       g_return_if_fail( qry != NULL );
        ADDRQUERY_ID(qry) = value;
 }
 
@@ -353,12 +363,17 @@ void ldapqry_free( LdapQuery *qry ) {
  */
 static void ldapqry_free_lists(
                GSList *listName, GSList *listAddr, GSList *listFirst,
-               GSList *listLast )
+               GSList *listLast, GSList *listDisplay, GSList *other_attrs )
 {
+       GSList *cur = other_attrs;
        mgu_free_list( listName );
        mgu_free_list( listAddr );
        mgu_free_list( listFirst );
        mgu_free_list( listLast );
+       mgu_free_list( listDisplay );
+       for(;cur; cur = cur->next)
+               addritem_free_attribute((UserAttribute *)cur->data);
+       g_slist_free(other_attrs);
 }
 
 /**
@@ -377,7 +392,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) );
                }
        }
@@ -398,7 +414,8 @@ 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] ); */
+                       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 ));
                }
        }
@@ -430,11 +447,11 @@ static GSList *ldapqry_add_single_value( LDAP *ld, LDAPMessage *entry, char *att
 static GList *ldapqry_build_items_fl(
                AddressCache *cache, LdapQuery *qry, gchar *dn,
                GSList *listName, GSList *listAddr, GSList *listFirst,
-               GSList *listLast )
+               GSList *listLast, GSList *listDisplay, GSList *attributes )
 {
-       GSList *nodeAddress;
+       GSList *nodeAddress, *cur;
        gchar *firstName = NULL, *lastName = NULL, *fullName = NULL;
-       gboolean allocated;
+       gboolean allocated = FALSE;
        ItemPerson *person;
        ItemEMail *email;
        ItemFolder *folder;
@@ -444,6 +461,11 @@ static GList *ldapqry_build_items_fl(
        if( folder == NULL ) return listReturn;
        if( listAddr == NULL ) return listReturn;
 
+       if ( listDisplay ) {
+               allocated = FALSE;
+               fullName = listDisplay->data;
+       }
+
        /* Find longest first name in list */
        firstName = mgu_slist_longest_entry( listFirst );
 
@@ -452,28 +474,30 @@ static GList *ldapqry_build_items_fl(
                lastName = listLast->data;
        }
 
-       /* Find longest common name */
-       allocated = FALSE;
-       fullName = mgu_slist_longest_entry( listName );
-       if( fullName == NULL ) {
-               /* Format a full name from first and last names */
-               if( firstName ) {
-                       if( lastName ) {
-                               fullName = g_strdup_printf( "%s %s", firstName, lastName );
+       if ( fullName == NULL ) {
+               /* Find longest common name */
+               allocated = FALSE;
+               fullName = mgu_slist_longest_entry( listName );
+               if( fullName == NULL ) {
+                       /* Format a full name from first and last names */
+                       if( firstName ) {
+                               if( lastName ) {
+                                       fullName = g_strdup_printf( "%s %s", firstName, lastName );
+                               }
+                               else {
+                                       fullName = g_strdup_printf( "%s", firstName );
+                               }
                        }
                        else {
-                               fullName = g_strdup_printf( "%s", firstName );
+                               if( lastName ) {
+                                       fullName = g_strdup_printf( "%s", lastName );
+                               }
                        }
-               }
-               else {
-                       if( lastName ) {
-                               fullName = g_strdup_printf( "%s", lastName );
+                       if( fullName ) {
+                               g_strchug( fullName ); g_strchomp( fullName );
+                               allocated = TRUE;
                        }
                }
-               if( fullName ) {
-                       g_strchug( fullName ); g_strchomp( fullName );
-                       allocated = TRUE;
-               }
        }
 
        /* Add person into folder */            
@@ -481,8 +505,15 @@ static GList *ldapqry_build_items_fl(
        addritem_person_set_common_name( person, fullName );
        addritem_person_set_first_name( person, firstName );
        addritem_person_set_last_name( person, lastName );
+       addritem_person_set_nick_name( person, fullName );
        addrcache_id_person( cache, person );
        addritem_person_set_external_id( person, dn );
+       
+       for (cur = attributes; cur; cur = cur->next) {
+               UserAttribute *attrib = addritem_copy_attribute((UserAttribute *)cur->data);
+               addritem_person_add_attribute( person, attrib );
+       }
+       
        addrcache_folder_add_person( cache, ADDRQUERY_FOLDER(qry), person );
 
        qry->entriesRead++;
@@ -495,6 +526,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 );
+               /*if (debug_get_mode()) {
+                       addritem_print_item_email(email, stdout);
+               }*/
                listReturn = g_list_append( listReturn, email );
                nodeAddress = g_slist_next( nodeAddress );
        }
@@ -525,17 +559,18 @@ static GList *ldapqry_process_single_entry(
        BerElement *ber;
        GSList *listName = NULL, *listAddress = NULL;
        GSList *listFirst = NULL, *listLast = NULL;
+       GSList *listDisplay = NULL;
+       GSList *other_attrs = NULL;
        GList *listReturn;
 
        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;
                attribute = ldap_next_attribute( ld, e, ber ) ) {
-
                if( strcasecmp( attribute, ctl->attribEMail ) == 0 ) {
                        listAddress = ldapqry_add_list_values( ld, e, attribute );
                }
@@ -547,19 +582,30 @@ static GList *ldapqry_process_single_entry(
                }
                else if( strcasecmp( attribute, ctl->attribLName ) == 0 ) {
                        listLast = ldapqry_add_single_value( ld, e, attribute );
+               } else if( strcasecmp( attribute, ctl->attribDName ) == 0 ) {
+                       listDisplay = ldapqry_add_single_value( ld, e, attribute );
+               } else {
+                       GSList *attlist = ldapqry_add_single_value( ld, e, attribute );
+                       UserAttribute *attrib = addritem_create_attribute();
+                       const gchar *attvalue = attlist?((gchar *)attlist->data):NULL;
+                       if (attvalue) {
+                               addritem_attrib_set_name( attrib, attribute );
+                               addritem_attrib_set_value( attrib, attvalue );
+                               other_attrs = g_slist_prepend(other_attrs, attrib);
+                       }
+                       mgu_free_list(attlist);
                }
-
                /* Free memory used to store attribute */
                ldap_memfree( attribute );
        }
 
        /* Format and add items to cache */
        listReturn = ldapqry_build_items_fl(
-               cache, qry, dnEntry, listName, listAddress, listFirst, listLast );
+               cache, qry, dnEntry, listName, listAddress, listFirst, listLast, listDisplay, other_attrs );
 
        /* Free up */
-       ldapqry_free_lists( listName, listAddress, listFirst, listLast );
-       listName = listAddress = listFirst = listLast = NULL;
+       ldapqry_free_lists( listName, listAddress, listFirst, listLast, listDisplay, other_attrs );
+       listName = listAddress = listFirst = listLast = listDisplay = other_attrs = NULL;
 
        if( ber != NULL ) {
                ber_free( ber, 0 );
@@ -618,16 +664,22 @@ static gint ldapqry_connect( LdapQuery *qry ) {
        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;
 
+       ldapsrv_set_options (ctl->timeOut, NULL);
+
        uri = g_strdup_printf("ldap%s://%s:%d",
                                ctl->enableSSL?"s":"",
                                ctl->hostName, ctl->port);
@@ -644,9 +696,8 @@ static gint ldapqry_connect( LdapQuery *qry ) {
        }
        ldapqry_touch( qry );
 
-       /*
-       printf( "connected to LDAP host %s on port %d\n", ctl->hostName, ctl->port );
-       */
+       debug_print("connected to LDAP host %s on port %d\n",
+                       ctl->hostName?ctl->hostName:"null", ctl->port);
 
 #ifdef USE_LDAP_TLS
        /* Handle TLS */
@@ -661,9 +712,9 @@ static gint ldapqry_connect( LdapQuery *qry ) {
                        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 ) ); */
-                       
+                       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);
                        }
@@ -675,14 +726,11 @@ static gint ldapqry_connect( LdapQuery *qry ) {
        ADDRQUERY_RETVAL(qry) = LDAPRC_BIND;
        if( ctl->bindDN ) {
                if( * ctl->bindDN != '\0' ) {
-                       /* printf( "binding...\n" ); */
+                       debug_print("binding...\n");
                        rc = claws_ldap_simple_bind_s( ld, ctl->bindDN, ctl->bindPass );
-                       /* printf( "rc=%d\n", rc ); */
+                       debug_print("rc=%d\n", rc);
                        if( rc != LDAP_SUCCESS ) {
-                               /*
-                               printf( "LDAP Error: ldap_simple_bind_s: %s\n",
-                                       ldap_err2string( rc ) );
-                               */
+                               debug_print("LDAP Error: ldap_simple_bind_s: %s\n",     ldap_err2string(rc));
                                return ADDRQUERY_RETVAL(qry);
                        }
                }
@@ -748,11 +796,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
@@ -761,6 +809,8 @@ 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 Error: ldap_search_st: %d\n", rc);
+       debug_print("LDAP Error: ldap_search_st: %s\n", ldap_err2string(rc));
        ldapctl_free_attribute_array( attribs );
        g_free( criteria );
        criteria = NULL;
@@ -781,17 +831,13 @@ static gint ldapqry_search_retrieve( LdapQuery *qry ) {
                searchFlag = TRUE;
        }
        else {
-               /*
-               printf( "LDAP Error: ldap_search_st: %d\n", rc );
-               printf( "LDAP Error: ldap_search_st: %s\n", ldap_err2string( rc ) );
-               */
+               debug_print("LDAP Error: ldap_search_st: %d\n", rc);
+               debug_print("LDAP Error: ldap_search_st: %s\n", ldap_err2string(rc));
                return ADDRQUERY_RETVAL(qry);
        }
        ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
 
-       /*
-       printf( "Total results are: %d\n", ldap_count_entries( ld, result ) );
-       */
+       debug_print("Total results are: %d\n", ldap_count_entries(ld, result));
 
        /* Process results */
        first = TRUE;
@@ -826,6 +872,13 @@ static gint ldapqry_search_retrieve( LdapQuery *qry ) {
                        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);
+                               }
+                       }*/
                        g_list_free( listEMail );
                }
                pthread_mutex_unlock( qry->mutexEntry );
@@ -907,16 +960,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 );
@@ -944,9 +993,7 @@ gint ldapqry_read_data_th( LdapQuery *qry ) {
        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 ) );
 
@@ -967,9 +1014,10 @@ static void ldapqry_destroyer( void * ptr ) {
        LdapQuery *qry;
 
        qry = ( LdapQuery * ) ptr;
-       /*
-       printf( "ldapqry_destroyer::%d::%s\n", (int) pthread_self(), ADDRQUERY_NAME(qry) );
-       */
+       g_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 ) {
@@ -987,12 +1035,11 @@ static void ldapqry_destroyer( void * ptr ) {
 void ldapqry_cancel( LdapQuery *qry ) {
        g_return_if_fail( qry != NULL );
 
-       /*
-       printf( "cancelling::%d::%s\n", (int) pthread_self(), ADDRQUERY_NAME(qry) );
-       */
+       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 ) {
-                       /* printf( "calling pthread_cancel\n" ); */
+                       debug_print("calling pthread_cancel\n");
                        pthread_cancel( * qry->thread );
                }
        }
@@ -1003,15 +1050,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");
 }
 
 /**
@@ -1194,15 +1239,11 @@ 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 ) );
-               */
+               debug_print("LDAP Error: ldap_search_st: %s\n", ldap_err2string(rc));
                return ADDRQUERY_RETVAL(qry);
        }
 
-       /*
-       printf( "Total results are: %d\n", ldap_count_entries( ld, result ) );
-       */
+       debug_print("Total results are: %d\n", ldap_count_entries(ld, result));
 
        /* Process results */
        ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
@@ -1306,6 +1347,22 @@ gboolean ldapquery_remove_results( LdapQuery *qry ) {
        return retVal;
 }
 
+void ldapqry_print(LdapQuery *qry, FILE *stream) {
+       g_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 */
 
 /*