2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2006 Match Grun and the Sylpheed-Claws team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * Functions necessary to define and perform LDAP queries.
35 #include "ldapquery.h"
40 #include "addrcache.h"
42 #include "ldapquery.h"
45 * Key for thread specific data.
47 static pthread_key_t _queryThreadKey_;
48 static gboolean _queryThreadInit_ = FALSE;
50 gboolean callbackend (gpointer data)
52 LdapQuery *qry = (LdapQuery *)data;
53 qry->callBackEnd( qry, ADDRQUERY_ID(qry), ADDRQUERY_RETVAL(qry), qry->data );
59 * Create new LDAP query object.
60 * \return Initialized query object.
62 LdapQuery *ldapqry_create( void ) {
65 qry = g_new0( LdapQuery, 1 );
66 ADDRQUERY_TYPE(qry) = ADDRQUERY_LDAP;
67 ADDRQUERY_ID(qry) = 0;
68 ADDRQUERY_SEARCHTYPE(qry) = ADDRSEARCH_NONE;
69 ADDRQUERY_NAME(qry) = NULL;
70 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
71 ADDRQUERY_FOLDER(qry) = NULL;
72 ADDRQUERY_SEARCHVALUE(qry) = NULL;
77 qry->stopFlag = FALSE;
78 qry->busyFlag = FALSE;
79 qry->agedFlag = FALSE;
80 qry->completed = FALSE;
82 qry->callBackEntry = NULL;
83 qry->callBackEnd = NULL;
87 /* Mutex to protect stop and busy flags */
88 qry->mutexStop = g_malloc0( sizeof( pthread_mutex_t ) );
89 pthread_mutex_init( qry->mutexStop, NULL );
90 qry->mutexBusy = g_malloc0( sizeof( pthread_mutex_t ) );
91 pthread_mutex_init( qry->mutexBusy, NULL );
93 /* Mutex to protect critical section */
94 qry->mutexEntry = g_malloc0( sizeof( pthread_mutex_t ) );
95 pthread_mutex_init( qry->mutexEntry, NULL );
101 * Specify the reference to control data that will be used for the query. The calling
102 * module should be responsible for creating and destroying this control object.
103 * \param qry Query object.
104 * \param ctl Control object.
106 void ldapqry_set_control( LdapQuery *qry, LdapControl *ctl ) {
107 g_return_if_fail( qry != NULL );
112 * Specify query name to be used.
113 * \param qry Query object.
116 void ldapqry_set_name( LdapQuery* qry, const gchar *value ) {
117 ADDRQUERY_NAME(qry) = mgu_replace_string( ADDRQUERY_NAME(qry), value );
118 g_strstrip( ADDRQUERY_NAME(qry) );
122 * Specify search value to be used.
123 * \param qry Query object.
126 void ldapqry_set_search_value( LdapQuery *qry, const gchar *value ) {
127 ADDRQUERY_SEARCHVALUE(qry) = mgu_replace_string( ADDRQUERY_SEARCHVALUE(qry), value );
128 g_strstrip( ADDRQUERY_SEARCHVALUE(qry) );
132 * Specify error/status.
133 * \param qry Query object.
134 * \param value Status.
136 void ldapqry_set_error_status( LdapQuery* qry, const gint value ) {
137 ADDRQUERY_RETVAL(qry) = value;
141 * Specify query type.
142 * \param qry Query object.
143 * \param value Query type, either:
145 * <li><code>LDAPQUERY_NONE</code></li>
146 * <li><code>LDAPQUERY_STATIC</code></li>
147 * <li><code>LDAPQUERY_DYNAMIC</code></li>
151 void ldapqry_set_query_type( LdapQuery* qry, const gint value ) {
152 ADDRQUERY_TYPE(qry) = value;
157 * Specify search type.
158 * \param qry Query object.
161 void ldapqry_set_search_type( LdapQuery *qry, const AddrSearchType value ) {
162 g_return_if_fail( qry != NULL );
163 ADDRQUERY_SEARCHTYPE(qry) = value;
168 * \param qry Query object.
169 * \param value ID for the query.
171 void ldapqry_set_query_id( LdapQuery* qry, const gint value ) {
172 ADDRQUERY_ID(qry) = value;
176 * Specify maximum number of LDAP entries to retrieve.
177 * \param qry Query object.
178 * \param value Entries to read.
180 void ldapqry_set_entries_read( LdapQuery* qry, const gint value ) {
182 qry->entriesRead = value;
185 qry->entriesRead = 0;
190 * Register a callback function that will be executed when each entry
191 * has been read and processed. When called, the function will be passed
192 * this query object and a GList of ItemEMail objects as arguments. An
193 * example of typical usage is shown below.
196 * ------------------------------------------------------------
197 * void myCallbackEntry( LdapQuery *qry, GList *listEMail ) {
202 * ItemEMail *email = node->data;
203 * ... process email object ...
204 * node = g_list_next( node );
206 * g_list_free( listEMail );
210 * ldapqry_set_callback_entry( qry, myCallbackEntry );
211 * ------------------------------------------------------------
214 * \param qry Query object.
215 * \param func Function.
217 void ldapqry_set_callback_entry( LdapQuery *qry, void *func ) {
218 pthread_mutex_lock( qry->mutexEntry );
219 qry->callBackEntry = func;
220 pthread_mutex_unlock( qry->mutexEntry );
224 * Register a callback function that will be executed when the search
225 * is complete. When called, the function will be passed this query
226 * object as an argument.
227 * \param qry Query object.
228 * \param func Function.
230 void ldapqry_set_callback_end( LdapQuery *qry, void *func ) {
231 qry->callBackEnd = func;
235 * Notify query to start/stop executing. This method should be called with a
236 * value if <i>TRUE</i> to terminate an existing running query.
238 * \param qry Query object.
239 * \param value Value of stop flag.
241 void ldapqry_set_stop_flag( LdapQuery *qry, const gboolean value ) {
242 g_return_if_fail( qry != NULL );
244 pthread_mutex_lock( qry->mutexStop );
245 qry->stopFlag = value;
246 pthread_mutex_unlock( qry->mutexStop );
250 * Test value of stop flag. This method should be used to determine whether a
251 * query has stopped running.
252 * \param qry Query object.
253 * \return Value of stop flag.
255 gboolean ldapqry_get_stop_flag( LdapQuery *qry ) {
257 g_return_val_if_fail( qry != NULL, TRUE );
259 pthread_mutex_lock( qry->mutexStop );
260 value = qry->stopFlag;
261 pthread_mutex_unlock( qry->mutexStop );
267 * \param qry Query object.
268 * \param value Value of busy flag.
270 void ldapqry_set_busy_flag( LdapQuery *qry, const gboolean value ) {
271 g_return_if_fail( qry != NULL );
273 pthread_mutex_lock( qry->mutexBusy );
274 qry->busyFlag = value;
275 pthread_mutex_unlock( qry->mutexBusy );
279 * Test value of busy flag. This method will return a value of <i>FALSE</i>
280 * when a query has completed running.
281 * \param qry Query object.
282 * \return Value of busy flag.
284 gboolean ldapqry_get_busy_flag( LdapQuery *qry ) {
286 g_return_val_if_fail( qry != NULL, FALSE );
288 pthread_mutex_lock( qry->mutexBusy );
289 value = qry->busyFlag;
290 pthread_mutex_unlock( qry->mutexBusy );
295 * Set query aged flag.
296 * \param qry Query object.
297 * \param value Value of aged flag.
299 void ldapqry_set_aged_flag( LdapQuery *qry, const gboolean value ) {
300 g_return_if_fail( qry != NULL );
301 qry->agedFlag = value;
305 * Test value of aged flag.
306 * \param qry Query object.
307 * \return <i>TRUE</i> if query has been marked as aged (and can be retired).
309 gboolean ldapqry_get_aged_flag( LdapQuery *qry ) {
310 g_return_val_if_fail( qry != NULL, TRUE );
311 return qry->agedFlag;
315 * Specify user data for query.
316 * \param qry Query object.
317 * \param value Data to set.
319 void ldapqry_set_data( LdapQuery *qry, const gpointer value ) {
320 g_return_if_fail( qry != NULL );
325 * Retrieve user data associated with query.
326 * \param qry Query object.
329 gpointer ldapqry_get_data( LdapQuery *qry ) {
330 g_return_val_if_fail( qry != NULL, NULL );
335 * Release the LDAP control data associated with the query.
336 * \param qry Query object to process.
338 void ldapqry_release_control( LdapQuery *qry ) {
339 g_return_if_fail( qry != NULL );
340 if( qry->control != NULL ) {
341 ldapctl_free( qry->control );
347 * Clear LDAP query member variables.
348 * \param qry Query object.
350 void ldapqry_clear( LdapQuery *qry ) {
351 g_return_if_fail( qry != NULL );
353 /* Free internal stuff */
354 g_free( ADDRQUERY_NAME(qry) );
355 g_free( ADDRQUERY_SEARCHVALUE(qry) );
357 /* Clear pointers and value */
358 ADDRQUERY_NAME(qry) = NULL;
359 ADDRQUERY_SEARCHVALUE(qry) = NULL;
360 ADDRQUERY_ID(qry) = 0;
361 ADDRQUERY_SEARCHTYPE(qry) = ADDRSEARCH_NONE;
362 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
363 qry->entriesRead = 0;
364 qry->elapsedTime = 0;
365 qry->stopFlag = FALSE;
366 qry->busyFlag = FALSE;
367 qry->agedFlag = FALSE;
368 qry->completed = FALSE;
369 qry->callBackEntry = NULL;
370 qry->callBackEnd = NULL;
376 * Free up LDAP query object by releasing internal memory. Note that
377 * the thread object will be freed by the OS.
378 * \param qry Query object to process.
380 void ldapqry_free( LdapQuery *qry ) {
381 g_return_if_fail( qry != NULL );
383 /* Clear out internal members */
384 ADDRQUERY_TYPE(qry) = ADDRQUERY_NONE;
385 ldapqry_clear( qry );
388 pthread_mutex_destroy( qry->mutexStop );
389 pthread_mutex_destroy( qry->mutexBusy );
390 pthread_mutex_destroy( qry->mutexEntry );
391 g_free( qry->mutexStop );
392 g_free( qry->mutexBusy );
393 g_free( qry->mutexEntry );
394 qry->mutexEntry = NULL;
395 qry->mutexBusy = NULL;
396 qry->mutexStop = NULL;
398 /* Do not free folder - parent server object should free */
399 ADDRQUERY_FOLDER(qry) = NULL;
401 /* Do not free thread - thread should be terminated before freeing */
404 /* Do not free LDAP control - should be destroyed before freeing */
407 /* Now release object */
412 * Display object to specified stream.
413 * \param qry Query object to process.
414 * \param stream Output stream.
416 void ldapqry_print( const LdapQuery *qry, FILE *stream ) {
417 g_return_if_fail( qry != NULL );
419 fprintf( stream, "LdapQuery:\n" );
420 fprintf( stream, " control?: %s\n", qry->control ? "yes" : "no" );
421 fprintf( stream, "err/status: %d\n", ADDRQUERY_RETVAL(qry) );
422 fprintf( stream, "query type: %d\n", ADDRQUERY_TYPE(qry) );
423 fprintf( stream, "searchType: %d\n", ADDRQUERY_SEARCHTYPE(qry) );
424 fprintf( stream, "query name: '%s'\n", ADDRQUERY_NAME(qry) );
425 fprintf( stream, "search val: '%s'\n", ADDRQUERY_SEARCHVALUE(qry) );
426 fprintf( stream, " queryID: %d\n", ADDRQUERY_ID(qry) );
427 fprintf( stream, " entries: %d\n", qry->entriesRead );
428 fprintf( stream, " elapsed: %d\n", qry->elapsedTime );
429 fprintf( stream, " stop flag: %s\n", qry->stopFlag ? "yes" : "no" );
430 fprintf( stream, " busy flag: %s\n", qry->busyFlag ? "yes" : "no" );
431 fprintf( stream, " aged flag: %s\n", qry->agedFlag ? "yes" : "no" );
432 fprintf( stream, " completed: %s\n", qry->completed ? "yes" : "no" );
436 * Free linked lists of character strings.
437 * \param listName List of common names.
438 * \param listAddr List of addresses.
439 * \param listFirst List of first names.
440 * \param listLast List of last names.
442 static void ldapqry_free_lists(
443 GSList *listName, GSList *listAddr, GSList *listFirst,
446 mgu_free_list( listName );
447 mgu_free_list( listAddr );
448 mgu_free_list( listFirst );
449 mgu_free_list( listLast );
453 * Add all LDAP attribute values to a list.
454 * \param ld LDAP handle.
455 * \param entry LDAP entry to process.
456 * \param attr LDAP attribute.
457 * \return List of values.
459 static GSList *ldapqry_add_list_values(
460 LDAP *ld, LDAPMessage *entry, char *attr )
466 if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
467 for( i = 0; vals[i] != NULL; i++ ) {
468 /* printf( "lv\t%s: %s\n", attr, vals[i] ); */
469 list = g_slist_append( list, g_strdup( vals[i] ) );
472 ldap_value_free( vals );
477 * Add a single attribute value to a list.
478 * \param ld LDAP handle.
479 * \param entry LDAP entry to process.
480 * \param attr LDAP attribute name to process.
481 * \return List of values; only one value will be present.
483 static GSList *ldapqry_add_single_value( LDAP *ld, LDAPMessage *entry, char *attr ) {
487 if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
488 if( vals[0] != NULL ) {
489 /* printf( "sv\t%s: %s\n", attr, vals[0] ); */
490 list = g_slist_append( list, g_strdup( vals[0] ) );
493 ldap_value_free( vals );
498 * Build an address list entry and append to list of address items. Name is formatted
499 * as "<first-name> <last-name>".
501 * \param cache Address cache to load.
502 * \param qry Query object to process.
503 * \param dn DN for entry found on server.
504 * \param listName List of common names for entry; see notes below.
505 * \param listAddr List of EMail addresses for entry.
506 * \param listFirst List of first names for entry.
507 * \param listLast List of last names for entry.
509 * \return List of ItemEMail objects.
512 * 1) Each LDAP server entry may have multiple LDAP attributes with the same
513 * name. For example, a single entry for a person may have more than one
514 * common name, email address, etc.
516 * 2) The DN for the entry is unique for the server.
518 static GList *ldapqry_build_items_fl(
519 AddressCache *cache, LdapQuery *qry, gchar *dn,
520 GSList *listName, GSList *listAddr, GSList *listFirst,
524 gchar *firstName = NULL, *lastName = NULL, *fullName = NULL;
529 GList *listReturn = NULL;
531 folder = ADDRQUERY_FOLDER(qry);
532 if( folder == NULL ) return listReturn;
533 if( listAddr == NULL ) return listReturn;
535 /* Find longest first name in list */
536 firstName = mgu_slist_longest_entry( listFirst );
538 /* Format last name */
540 lastName = listLast->data;
543 /* Find longest common name */
545 fullName = mgu_slist_longest_entry( listName );
546 if( fullName == NULL ) {
547 /* Format a full name from first and last names */
550 fullName = g_strdup_printf( "%s %s", firstName, lastName );
553 fullName = g_strdup_printf( "%s", firstName );
558 fullName = g_strdup_printf( "%s", lastName );
562 g_strchug( fullName ); g_strchomp( fullName );
567 /* Add person into folder */
568 person = addritem_create_item_person();
569 addritem_person_set_common_name( person, fullName );
570 addritem_person_set_first_name( person, firstName );
571 addritem_person_set_last_name( person, lastName );
572 addrcache_id_person( cache, person );
573 addritem_person_set_external_id( person, dn );
574 addrcache_folder_add_person( cache, ADDRQUERY_FOLDER(qry), person );
578 /* Add each address item */
579 nodeAddress = listAddr;
580 while( nodeAddress ) {
581 email = addritem_create_item_email();
582 addritem_email_set_address( email, nodeAddress->data );
583 addrcache_id_email( cache, email );
584 addrcache_person_add_email( cache, person, email );
585 addritem_person_add_email( person, email );
586 listReturn = g_list_append( listReturn, email );
587 nodeAddress = g_slist_next( nodeAddress );
590 /* Free any allocated memory */
594 fullName = firstName = lastName = NULL;
600 * Process a single search entry.
601 * \param cache Address cache to load.
602 * \param qry Query object to process.
603 * \param ld LDAP handle.
604 * \param e LDAP message.
605 * \return List of EMail objects found.
607 static GList *ldapqry_process_single_entry(
608 AddressCache *cache, LdapQuery *qry, LDAP *ld, LDAPMessage *e )
614 GSList *listName = NULL, *listAddress = NULL;
615 GSList *listFirst = NULL, *listLast = NULL;
620 dnEntry = ldap_get_dn( ld, e );
621 /* printf( "DN: %s\n", dnEntry ); */
623 /* Process all attributes */
624 for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL;
625 attribute = ldap_next_attribute( ld, e, ber ) ) {
627 if( strcasecmp( attribute, ctl->attribEMail ) == 0 ) {
628 listAddress = ldapqry_add_list_values( ld, e, attribute );
630 else if( strcasecmp( attribute, ctl->attribCName ) == 0 ) {
631 listName = ldapqry_add_list_values( ld, e, attribute );
633 else if( strcasecmp( attribute, ctl->attribFName ) == 0 ) {
634 listFirst = ldapqry_add_list_values( ld, e, attribute );
636 else if( strcasecmp( attribute, ctl->attribLName ) == 0 ) {
637 listLast = ldapqry_add_single_value( ld, e, attribute );
640 /* Free memory used to store attribute */
641 ldap_memfree( attribute );
644 /* Format and add items to cache */
645 listReturn = ldapqry_build_items_fl(
646 cache, qry, dnEntry, listName, listAddress, listFirst, listLast );
649 ldapqry_free_lists( listName, listAddress, listFirst, listLast );
650 listName = listAddress = listFirst = listLast = NULL;
661 * Check parameters that are required for a search. This should
662 * be called before performing a search.
663 * \param qry Query object to process.
664 * \return <i>TRUE</i> if search criteria appear OK.
666 gboolean ldapqry_check_search( LdapQuery *qry ) {
668 ADDRQUERY_RETVAL(qry) = LDAPRC_CRITERIA;
670 /* Test for control data */
676 /* Test for search value */
677 if( ADDRQUERY_SEARCHVALUE(qry) == NULL ) {
680 if( strlen( ADDRQUERY_SEARCHVALUE(qry) ) < 1 ) {
683 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
688 * Touch the query. This nudges the touch time with the current time.
689 * \param qry Query object to process.
691 void ldapqry_touch( LdapQuery *qry ) {
692 qry->touchTime = time( NULL );
693 qry->agedFlag = FALSE;
697 * Connect to LDAP server.
698 * \param qry Query object to process.
699 * \return Error/status code.
701 static gint ldapqry_connect( LdapQuery *qry ) {
707 /* Initialize connection */
708 /* printf( "===ldapqry_connect===\n" ); */
709 /* ldapqry_print( qry, stdout ); */
711 /* ldapctl_print( ctl, stdout ); */
712 /* printf( "======\n" ); */
713 ldapqry_touch( qry );
714 qry->startTime = qry->touchTime;
715 qry->elapsedTime = -1;
716 ADDRQUERY_RETVAL(qry) = LDAPRC_INIT;
717 if( ( ld = ldap_init( ctl->hostName, ctl->port ) ) == NULL ) {
718 return ADDRQUERY_RETVAL(qry);
721 ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
722 if( ldapqry_get_stop_flag( qry ) ) {
723 return ADDRQUERY_RETVAL(qry);
725 ldapqry_touch( qry );
728 printf( "connected to LDAP host %s on port %d\n", ctl->hostName, ctl->port );
733 version = LDAP_VERSION3;
734 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
735 if( rc == LDAP_OPT_SUCCESS ) {
736 ctl->version = LDAP_VERSION3;
739 if( ctl->version == LDAP_VERSION3 ) {
740 if( ctl->enableTLS ) {
741 ADDRQUERY_RETVAL(qry) = LDAPRC_TLS;
742 rc = ldap_start_tls_s( ld, NULL, NULL );
744 printf( "rc=%d\n", rc );
745 printf( "LDAP Status: set_option: %s\n", ldap_err2string( rc ) );
747 if( rc != LDAP_SUCCESS ) {
748 return ADDRQUERY_RETVAL(qry);
754 /* Bind to the server, if required */
755 ADDRQUERY_RETVAL(qry) = LDAPRC_BIND;
757 if( * ctl->bindDN != '\0' ) {
758 /* printf( "binding...\n" ); */
759 rc = ldap_simple_bind_s( ld, ctl->bindDN, ctl->bindPass );
760 /* printf( "rc=%d\n", rc ); */
761 if( rc != LDAP_SUCCESS ) {
763 printf( "LDAP Error: ldap_simple_bind_s: %s\n",
764 ldap_err2string( rc ) );
766 return ADDRQUERY_RETVAL(qry);
770 ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
771 if( ldapqry_get_stop_flag( qry ) ) {
772 return ADDRQUERY_RETVAL(qry);
774 ldapqry_touch( qry );
776 ADDRQUERY_RETVAL(qry) = LDAP_SUCCESS;
778 return ADDRQUERY_RETVAL(qry);
782 * Connect to LDAP server.
783 * \param qry Query object to process.
784 * \return Error/status code.
786 static gint ldapqry_disconnect( LdapQuery *qry ) {
788 if( qry->ldap ) ldap_unbind( qry->ldap );
791 ldapqry_touch( qry );
792 qry->elapsedTime = qry->touchTime - qry->startTime;
794 return ADDRQUERY_RETVAL(qry);
798 * Perform the LDAP search, reading LDAP entries into cache.
799 * Note that one LDAP entry can have multiple values for many of its
800 * attributes. If these attributes are E-Mail addresses; these are
801 * broken out into separate address items. For any other attribute,
802 * only the first occurrence is read.
804 * \param qry Query object to process.
805 * \return Error/status code.
807 static gint ldapqry_search_retrieve( LdapQuery *qry ) {
810 LDAPMessage *result, *e = NULL;
814 gboolean entriesFound;
816 struct timeval timeout;
821 /* Initialize some variables */
824 cache = qry->server->addressCache;
825 timeout.tv_sec = ctl->timeOut;
826 timeout.tv_usec = 0L;
827 entriesFound = FALSE;
828 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
830 /* Define all attributes we are interested in. */
831 attribs = ldapctl_attribute_array( ctl );
833 /* Create LDAP search string */
834 criteria = ldapctl_format_criteria( ctl, ADDRQUERY_SEARCHVALUE(qry) );
835 /* printf( "Search criteria ::%s::\n", criteria ); */
838 * Execute the search - this step may take some time to complete
839 * depending on network traffic and server response time.
841 ADDRQUERY_RETVAL(qry) = LDAPRC_TIMEOUT;
842 rc = ldap_search_ext_s( ld, ctl->baseDN, LDAP_SCOPE_SUBTREE, criteria,
843 attribs, 0, NULL, NULL, &timeout, 0, &result );
844 ldapctl_free_attribute_array( attribs );
847 if( rc == LDAP_TIMEOUT ) {
848 return ADDRQUERY_RETVAL(qry);
850 ADDRQUERY_RETVAL(qry) = LDAPRC_SEARCH;
852 /* Test valid returns */
854 if( rc == LDAP_ADMINLIMIT_EXCEEDED ) {
857 else if( rc == LDAP_SUCCESS ) {
860 else if( rc == LDAP_PARTIAL_RESULTS ) {
865 printf( "LDAP Error: ldap_search_st: %d\n", rc );
866 printf( "LDAP Error: ldap_search_st: %s\n", ldap_err2string( rc ) );
868 return ADDRQUERY_RETVAL(qry);
870 ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
873 printf( "Total results are: %d\n", ldap_count_entries( ld, result ) );
876 /* Process results */
878 while( searchFlag ) {
879 ldapqry_touch( qry );
880 if( qry->entriesRead >= ctl->maxEntries ) break;
883 if( ldapqry_get_stop_flag( qry ) ) {
890 e = ldap_first_entry( ld, result );
893 e = ldap_next_entry( ld, e );
895 if( e == NULL ) break;
898 /* Setup a critical section here */
899 pthread_mutex_lock( qry->mutexEntry );
902 listEMail = ldapqry_process_single_entry( cache, qry, ld, e );
904 /* Process callback */
905 if( qry->callBackEntry ) {
906 qry->callBackEntry( qry, ADDRQUERY_ID(qry), listEMail, qry->data );
909 g_list_free( listEMail );
911 pthread_mutex_unlock( qry->mutexEntry );
914 /* Free up and disconnect */
915 ldap_msgfree( result );
919 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
922 ADDRQUERY_RETVAL(qry) = LDAPRC_NOENTRIES;
926 return ADDRQUERY_RETVAL(qry);
930 * Connection, perform search and disconnect.
931 * \param qry Query object to process.
932 * \return Error/status code.
934 static gint ldapqry_perform_search( LdapQuery *qry ) {
935 /* Check search criteria */
936 if( ! ldapqry_check_search( qry ) ) {
937 return ADDRQUERY_RETVAL(qry);
942 ldapqry_connect( qry );
943 if( ADDRQUERY_RETVAL(qry) == LDAPRC_SUCCESS ) {
945 ldapqry_search_retrieve( qry );
948 ldapqry_disconnect( qry );
951 return ADDRQUERY_RETVAL(qry);
954 static gint ldapqry_perform_locate( LdapQuery *qry );
957 * Wrapper around search.
958 * \param qry Query object to process.
959 * \return Error/status code.
961 gint ldapqry_search( LdapQuery *qry ) {
964 g_return_val_if_fail( qry != NULL, -1 );
965 g_return_val_if_fail( qry->control != NULL, -1 );
967 ldapqry_touch( qry );
968 qry->completed = FALSE;
970 /* Setup pointer to thread specific area */
971 pthread_setspecific( _queryThreadKey_, qry );
973 pthread_detach( pthread_self() );
975 /* Now perform the search */
976 qry->entriesRead = 0;
977 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
978 ldapqry_set_busy_flag( qry, TRUE );
979 ldapqry_set_stop_flag( qry, FALSE );
980 if( ADDRQUERY_SEARCHTYPE(qry) == ADDRSEARCH_LOCATE ) {
981 retVal = ldapqry_perform_locate( qry );
984 retVal = ldapqry_perform_search( qry );
986 if( retVal == LDAPRC_SUCCESS ) {
987 qry->server->addressCache->dataRead = TRUE;
988 qry->server->addressCache->accessFlag = FALSE;
989 if( ldapqry_get_stop_flag( qry ) ) {
991 printf( "Search was terminated prematurely\n" );
995 ldapqry_touch( qry );
996 qry->completed = TRUE;
998 printf( "Search ran to completion\n" );
1002 ldapqry_set_stop_flag( qry, TRUE );
1003 ldapqry_set_busy_flag( qry, FALSE );
1005 /* Process callback */
1006 if( qry->callBackEnd ) {
1007 g_timeout_add(0, callbackend, qry);
1010 return ADDRQUERY_RETVAL(qry);
1014 * Read data into list using a background thread. Callback function will be
1015 * notified when search is complete.
1016 * \param qry Query object to process.
1017 * \return Error/status code.
1019 gint ldapqry_read_data_th( LdapQuery *qry ) {
1020 g_return_val_if_fail( qry != NULL, -1 );
1021 g_return_val_if_fail( qry->control != NULL, -1 );
1023 ldapqry_set_stop_flag( qry, FALSE );
1024 ldapqry_touch( qry );
1025 if( ldapqry_check_search( qry ) ) {
1026 if( ADDRQUERY_RETVAL(qry) == LDAPRC_SUCCESS ) {
1028 printf( "Starting LDAP search thread\n");
1030 ldapqry_set_busy_flag( qry, TRUE );
1031 qry->thread = g_malloc0( sizeof( pthread_t ) );
1034 pthread_create( qry->thread, NULL,
1035 (void *) ldapqry_search, (void *) qry );
1038 return ADDRQUERY_RETVAL(qry);
1042 * Cleanup LDAP thread data. This function will be called when each thread
1043 * exits. Note that the thread object will be freed by the kernel.
1044 * \param ptr Pointer to object being destroyed (a query object in this case).
1046 static void ldapqry_destroyer( void * ptr ) {
1049 qry = ( LdapQuery * ) ptr;
1051 printf( "ldapqry_destroyer::%d::%s\n", (int) pthread_self(), ADDRQUERY_NAME(qry) );
1054 /* Perform any destruction here */
1055 if( qry->control != NULL ) {
1056 ldapctl_free( qry->control );
1058 qry->control = NULL;
1060 ldapqry_set_busy_flag( qry, FALSE );
1064 * Cancel thread associated with query.
1065 * \param qry Query object to process.
1067 void ldapqry_cancel( LdapQuery *qry ) {
1068 g_return_if_fail( qry != NULL );
1071 printf( "cancelling::%d::%s\n", (int) pthread_self(), ADDRQUERY_NAME(qry) );
1073 if( ldapqry_get_busy_flag( qry ) ) {
1075 /* printf( "calling pthread_cancel\n" ); */
1076 pthread_cancel( * qry->thread );
1082 * Initialize LDAP query. This function should be called once before executing
1083 * any LDAP queries to initialize thread specific data.
1085 void ldapqry_initialize( void ) {
1086 /* printf( "ldapqry_initialize...\n" ); */
1087 if( ! _queryThreadInit_ ) {
1089 printf( "ldapqry_initialize::creating thread specific area\n" );
1091 pthread_key_create( &_queryThreadKey_, ldapqry_destroyer );
1092 _queryThreadInit_ = TRUE;
1094 /* printf( "ldapqry_initialize... done!\n" ); */
1098 * Age the query based on LDAP control parameters.
1099 * \param qry Query object to process.
1100 * \param maxAge Maximum age of query (in seconds).
1102 void ldapqry_age( LdapQuery *qry, gint maxAge ) {
1105 g_return_if_fail( qry != NULL );
1107 /* Limit the time that queries can hang around */
1108 if( maxAge < 1 ) maxAge = LDAPCTL_MAX_QUERY_AGE;
1110 /* Check age of query */
1111 age = time( NULL ) - qry->touchTime;
1112 if( age > maxAge ) {
1113 qry->agedFlag = TRUE;
1118 * Delete folder associated with query results.
1119 * \param qry Query object to process.
1121 void ldapqry_delete_folder( LdapQuery *qry ) {
1122 AddressCache *cache;
1125 g_return_if_fail( qry != NULL );
1127 folder = ADDRQUERY_FOLDER(qry);
1129 cache = qry->server->addressCache;
1130 folder = addrcache_remove_folder_delete( cache, folder );
1132 addritem_free_item_folder( folder );
1134 ADDRQUERY_FOLDER(qry) = NULL;
1139 * Create a name/value pair object.
1142 * \return Initialized object.
1144 static NameValuePair *ldapqry_create_name_value( const gchar *n, const gchar *v ) {
1145 NameValuePair *nvp = g_new0( NameValuePair, 1 );
1147 nvp->name = g_strdup( n );
1148 nvp->value = g_strdup( v );
1153 * Free up name/value pair object.
1154 * \param nvp Name/value object.
1156 void ldapqry_free_name_value( NameValuePair *nvp ) {
1158 g_free( nvp->name );
1159 g_free( nvp->value );
1160 nvp->name = nvp->value = NULL;
1166 * Print name/value pair object for debug.
1167 * \param nvp Name/value object.
1168 * \param stream Output stream.
1170 void ldapqry_print_name_value( NameValuePair *nvp, FILE *stream ) {
1172 fprintf( stream, "n/v ::%s::%s::\n", nvp->name, nvp->value );
1177 * Free up a list name/value pair objects.
1178 * \param list List of name/value objects.
1180 void ldapqry_free_list_name_value( GList *list ) {
1185 NameValuePair *nvp = ( NameValuePair * ) node->data;
1186 ldapqry_free_name_value( nvp );
1188 node = g_list_next( node );
1190 g_list_free( list );
1194 * Load a list of name/value pairs from LDAP attributes.
1195 * \param ld LDAP handle.
1196 * \param e LDAP message.
1197 * \param attr Attribute name.
1198 * \param listValues List to populate.
1199 * \return List of attribute name/value pairs.
1201 static GList *ldapqry_load_attrib_values(
1202 LDAP *ld, LDAPMessage *entry, char *attr,
1211 if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
1212 for( i = 0; vals[i] != NULL; i++ ) {
1213 nvp = ldapqry_create_name_value( attr, vals[i] );
1214 list = g_list_append( list, nvp );
1217 ldap_value_free( vals );
1222 * Fetch a list of all attributes.
1223 * \param ld LDAP handle.
1224 * \param e LDAP message.
1225 * \return List of attribute name/value pairs.
1227 static GList *ldapqry_fetch_attribs( LDAP *ld, LDAPMessage *e )
1231 GList *listValues = NULL;
1233 /* Process all attributes */
1234 for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL;
1235 attribute = ldap_next_attribute( ld, e, ber ) ) {
1236 listValues = ldapqry_load_attrib_values( ld, e, attribute, listValues );
1237 ldap_memfree( attribute );
1247 #define CRITERIA_SINGLE "(objectclass=*)"
1250 * Perform the data retrieval for a specific LDAP record.
1252 * \param qry Query object to process.
1253 * \return Error/status code.
1255 static gint ldapqry_locate_retrieve( LdapQuery *qry ) {
1258 LDAPMessage *result, *e = NULL;
1259 gboolean entriesFound;
1261 struct timeval timeout;
1266 /* Initialize some variables */
1269 dn = ADDRQUERY_SEARCHVALUE(qry);
1270 timeout.tv_sec = ctl->timeOut;
1271 timeout.tv_usec = 0L;
1272 entriesFound = FALSE;
1275 * Execute the search - this step may take some time to complete
1276 * depending on network traffic and server response time.
1278 ADDRQUERY_RETVAL(qry) = LDAPRC_TIMEOUT;
1279 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_BASE, CRITERIA_SINGLE,
1280 NULL, 0, NULL, NULL, &timeout, 0, &result );
1281 if( rc == LDAP_TIMEOUT ) {
1282 return ADDRQUERY_RETVAL(qry);
1284 ADDRQUERY_RETVAL(qry) = LDAPRC_SEARCH;
1285 if( rc != LDAP_SUCCESS ) {
1287 printf( "LDAP Error: ldap_search_st: %s\n", ldap_err2string( rc ) );
1289 return ADDRQUERY_RETVAL(qry);
1293 printf( "Total results are: %d\n", ldap_count_entries( ld, result ) );
1296 /* Process results */
1297 ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
1300 ldapqry_touch( qry );
1301 if( qry->entriesRead >= ctl->maxEntries ) break;
1304 if( ldapqry_get_stop_flag( qry ) ) {
1308 /* Retrieve entry */
1311 e = ldap_first_entry( ld, result );
1314 e = ldap_next_entry( ld, e );
1316 if( e == NULL ) break;
1318 entriesFound = TRUE;
1320 /* Setup a critical section here */
1321 pthread_mutex_lock( qry->mutexEntry );
1324 listValues = ldapqry_fetch_attribs( ld, e );
1326 /* Process callback */
1327 if( qry->callBackEntry ) {
1328 qry->callBackEntry( qry, ADDRQUERY_ID(qry), listValues, qry->data );
1330 ldapqry_free_list_name_value( listValues );
1333 pthread_mutex_unlock( qry->mutexEntry );
1336 /* Free up and disconnect */
1337 ldap_msgfree( result );
1339 if( entriesFound ) {
1340 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
1343 ADDRQUERY_RETVAL(qry) = LDAPRC_NOENTRIES;
1346 return ADDRQUERY_RETVAL(qry);
1350 * Perform the search to locate a specific LDAP record identified by
1351 * distinguished name (dn).
1353 * \param qry Query object to process.
1354 * \return Error/status code.
1356 static gint ldapqry_perform_locate( LdapQuery *qry ) {
1359 ldapqry_connect( qry );
1360 if( ADDRQUERY_RETVAL(qry) == LDAPRC_SUCCESS ) {
1361 /* Perform search */
1362 ldapqry_locate_retrieve( qry );
1365 ldapqry_disconnect( qry );
1368 /* Process callback */
1369 if( qry->callBackEnd ) {
1370 g_timeout_add(0, callbackend, qry);
1373 return ADDRQUERY_RETVAL(qry);
1377 * Remove results (folder and data) for specified LDAP query.
1378 * \param qry Query object to process.
1379 * \return TRUE if folder deleted successfully.
1381 gboolean ldapquery_remove_results( LdapQuery *qry ) {
1382 gboolean retVal = FALSE;
1384 ldapqry_set_aged_flag( qry, TRUE );
1386 if( ldapqry_get_busy_flag( qry ) ) {
1387 ldapqry_set_stop_flag( qry, TRUE );
1390 LdapServer *server = qry->server;
1391 server->listQuery = g_list_remove(server->listQuery, qry);
1398 #endif /* USE_LDAP */