2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003 Match Grun
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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;
51 * Create new LDAP query object.
52 * \return Initialized query object.
54 LdapQuery *ldapqry_create( void ) {
57 qry = g_new0( LdapQuery, 1 );
58 ADDRQUERY_TYPE(qry) = ADDRQUERY_LDAP;
59 ADDRQUERY_ID(qry) = 0;
60 ADDRQUERY_SEARCHTYPE(qry) = ADDRSEARCH_NONE;
61 ADDRQUERY_NAME(qry) = NULL;
62 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
63 ADDRQUERY_FOLDER(qry) = NULL;
64 ADDRQUERY_SEARCHVALUE(qry) = NULL;
69 qry->stopFlag = FALSE;
70 qry->busyFlag = FALSE;
71 qry->agedFlag = FALSE;
72 qry->completed = FALSE;
74 qry->callBackEntry = NULL;
75 qry->callBackEnd = NULL;
79 /* Mutex to protect stop and busy flags */
80 qry->mutexStop = g_malloc0( sizeof( pthread_mutex_t ) );
81 pthread_mutex_init( qry->mutexStop, NULL );
82 qry->mutexBusy = g_malloc0( sizeof( pthread_mutex_t ) );
83 pthread_mutex_init( qry->mutexBusy, NULL );
85 /* Mutex to protect critical section */
86 qry->mutexEntry = g_malloc0( sizeof( pthread_mutex_t ) );
87 pthread_mutex_init( qry->mutexEntry, NULL );
93 * Specify the reference to control data that will be used for the query. The calling
94 * module should be responsible for creating and destroying this control object.
95 * \param qry Query object.
96 * \param ctl Control object.
98 void ldapqry_set_control( LdapQuery *qry, LdapControl *ctl ) {
99 g_return_if_fail( qry != NULL );
104 * Specify query name to be used.
105 * \param qry Query object.
108 void ldapqry_set_name( LdapQuery* qry, const gchar *value ) {
109 ADDRQUERY_NAME(qry) = mgu_replace_string( ADDRQUERY_NAME(qry), value );
110 g_strstrip( ADDRQUERY_NAME(qry) );
114 * Specify search value to be used.
115 * \param qry Query object.
118 void ldapqry_set_search_value( LdapQuery *qry, const gchar *value ) {
119 ADDRQUERY_SEARCHVALUE(qry) = mgu_replace_string( ADDRQUERY_SEARCHVALUE(qry), value );
120 g_strstrip( ADDRQUERY_SEARCHVALUE(qry) );
124 * Specify error/status.
125 * \param qry Query object.
126 * \param value Status.
128 void ldapqry_set_error_status( LdapQuery* qry, const gint value ) {
129 ADDRQUERY_RETVAL(qry) = value;
133 * Specify query type.
134 * \param qry Query object.
135 * \param value Query type, either:
137 * <li><code>LDAPQUERY_NONE</code></li>
138 * <li><code>LDAPQUERY_STATIC</code></li>
139 * <li><code>LDAPQUERY_DYNAMIC</code></li>
143 void ldapqry_set_query_type( LdapQuery* qry, const gint value ) {
144 ADDRQUERY_TYPE(qry) = value;
149 * Specify search type.
150 * \param qry Query object.
153 void ldapqry_set_search_type( LdapQuery *qry, const AddrSearchType value ) {
154 g_return_if_fail( qry != NULL );
155 ADDRQUERY_SEARCHTYPE(qry) = value;
160 * \param qry Query object.
161 * \param value ID for the query.
163 void ldapqry_set_query_id( LdapQuery* qry, const gint value ) {
164 ADDRQUERY_ID(qry) = value;
168 * Specify maximum number of LDAP entries to retrieve.
169 * \param qry Query object.
170 * \param value Entries to read.
172 void ldapqry_set_entries_read( LdapQuery* qry, const gint value ) {
174 qry->entriesRead = value;
177 qry->entriesRead = 0;
182 * Register a callback function that will be executed when each entry
183 * has been read and processed. When called, the function will be passed
184 * this query object and a GList of ItemEMail objects as arguments. An
185 * example of typical usage is shown below.
188 * ------------------------------------------------------------
189 * void myCallbackEntry( LdapQuery *qry, GList *listEMail ) {
194 * ItemEMail *email = node->data;
195 * ... process email object ...
196 * node = g_list_next( node );
198 * g_list_free( listEMail );
202 * ldapqry_set_callback_entry( qry, myCallbackEntry );
203 * ------------------------------------------------------------
206 * \param qry Query object.
207 * \param func Function.
209 void ldapqry_set_callback_entry( LdapQuery *qry, void *func ) {
210 pthread_mutex_lock( qry->mutexEntry );
211 qry->callBackEntry = func;
212 pthread_mutex_unlock( qry->mutexEntry );
216 * Register a callback function that will be executed when the search
217 * is complete. When called, the function will be passed this query
218 * object as an argument.
219 * \param qry Query object.
220 * \param func Function.
222 void ldapqry_set_callback_end( LdapQuery *qry, void *func ) {
223 qry->callBackEnd = func;
227 * Notify query to start/stop executing. This method should be called with a
228 * value if <i>TRUE</i> to terminate an existing running query.
230 * \param qry Query object.
231 * \param value Value of stop flag.
233 void ldapqry_set_stop_flag( LdapQuery *qry, const gboolean value ) {
234 g_return_if_fail( qry != NULL );
236 pthread_mutex_lock( qry->mutexStop );
237 qry->stopFlag = value;
238 pthread_mutex_unlock( qry->mutexStop );
242 * Test value of stop flag. This method should be used to determine whether a
243 * query has stopped running.
244 * \param qry Query object.
245 * \return Value of stop flag.
247 gboolean ldapqry_get_stop_flag( LdapQuery *qry ) {
249 g_return_if_fail( qry != NULL );
251 pthread_mutex_lock( qry->mutexStop );
252 value = qry->stopFlag;
253 pthread_mutex_unlock( qry->mutexStop );
259 * \param qry Query object.
260 * \param value Value of busy flag.
262 void ldapqry_set_busy_flag( LdapQuery *qry, const gboolean value ) {
263 g_return_if_fail( qry != NULL );
265 pthread_mutex_lock( qry->mutexBusy );
266 qry->busyFlag = value;
267 pthread_mutex_unlock( qry->mutexBusy );
271 * Test value of busy flag. This method will return a value of <i>FALSE</i>
272 * when a query has completed running.
273 * \param qry Query object.
274 * \return Value of busy flag.
276 gboolean ldapqry_get_busy_flag( LdapQuery *qry ) {
278 g_return_if_fail( qry != NULL );
280 pthread_mutex_lock( qry->mutexBusy );
281 value = qry->busyFlag;
282 pthread_mutex_unlock( qry->mutexBusy );
287 * Set query aged flag.
288 * \param qry Query object.
289 * \param value Value of aged flag.
291 void ldapqry_set_aged_flag( LdapQuery *qry, const gboolean value ) {
292 g_return_if_fail( qry != NULL );
293 qry->agedFlag = value;
297 * Test value of aged flag.
298 * \param qry Query object.
299 * \return <i>TRUE</i> if query has been marked as aged (and can be retired).
301 gboolean ldapqry_get_aged_flag( LdapQuery *qry ) {
302 g_return_if_fail( qry != NULL );
303 return qry->agedFlag;
307 * Specify user data for query.
308 * \param qry Query object.
309 * \param value Data to set.
311 void ldapqry_set_data( LdapQuery *qry, const gpointer value ) {
312 g_return_if_fail( qry != NULL );
317 * Retrieve user data associated with query.
318 * \param qry Query object.
321 gpointer ldapqry_get_data( LdapQuery *qry ) {
322 g_return_if_fail( qry != NULL );
327 * Release the LDAP control data associated with the query.
328 * \param qry Query object to process.
330 void ldapqry_release_control( LdapQuery *qry ) {
331 g_return_if_fail( qry != NULL );
332 if( qry->control != NULL ) {
333 ldapctl_free( qry->control );
339 * Clear LDAP query member variables.
340 * \param qry Query object.
342 void ldapqry_clear( LdapQuery *qry ) {
343 g_return_if_fail( qry != NULL );
345 /* Free internal stuff */
346 g_free( ADDRQUERY_NAME(qry) );
347 g_free( ADDRQUERY_SEARCHVALUE(qry) );
349 /* Clear pointers and value */
350 ADDRQUERY_NAME(qry) = NULL;
351 ADDRQUERY_SEARCHVALUE(qry) = NULL;
352 ADDRQUERY_ID(qry) = 0;
353 ADDRQUERY_SEARCHTYPE(qry) = ADDRSEARCH_NONE;
354 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
355 qry->entriesRead = 0;
356 qry->elapsedTime = 0;
357 qry->stopFlag = FALSE;
358 qry->busyFlag = FALSE;
359 qry->agedFlag = FALSE;
360 qry->completed = FALSE;
361 qry->callBackEntry = NULL;
362 qry->callBackEnd = NULL;
368 * Free up LDAP query object by releasing internal memory. Note that
369 * the thread object will be freed by the OS.
370 * \param qry Query object to process.
372 void ldapqry_free( LdapQuery *qry ) {
373 g_return_if_fail( qry != NULL );
375 /* Clear out internal members */
376 ADDRQUERY_TYPE(qry) = ADDRQUERY_NONE;
377 ldapqry_clear( qry );
380 pthread_mutex_destroy( qry->mutexStop );
381 pthread_mutex_destroy( qry->mutexBusy );
382 pthread_mutex_destroy( qry->mutexEntry );
383 g_free( qry->mutexStop );
384 g_free( qry->mutexBusy );
385 g_free( qry->mutexEntry );
386 qry->mutexEntry = NULL;
387 qry->mutexBusy = NULL;
388 qry->mutexStop = NULL;
390 /* Do not free folder - parent server object should free */
391 ADDRQUERY_FOLDER(qry) = NULL;
393 /* Do not free thread - thread should be terminated before freeing */
396 /* Do not free LDAP control - should be destroyed before freeing */
399 /* Now release object */
404 * Display object to specified stream.
405 * \param qry Query object to process.
406 * \param stream Output stream.
408 void ldapqry_print( const LdapQuery *qry, FILE *stream ) {
409 g_return_if_fail( qry != NULL );
411 fprintf( stream, "LdapQuery:\n" );
412 fprintf( stream, " control?: %s\n", qry->control ? "yes" : "no" );
413 fprintf( stream, "err/status: %d\n", ADDRQUERY_RETVAL(qry) );
414 fprintf( stream, "query type: %d\n", ADDRQUERY_TYPE(qry) );
415 fprintf( stream, "searchType: %d\n", ADDRQUERY_SEARCHTYPE(qry) );
416 fprintf( stream, "query name: '%s'\n", ADDRQUERY_NAME(qry) );
417 fprintf( stream, "search val: '%s'\n", ADDRQUERY_SEARCHVALUE(qry) );
418 fprintf( stream, " queryID: %d\n", ADDRQUERY_ID(qry) );
419 fprintf( stream, " entries: %d\n", qry->entriesRead );
420 fprintf( stream, " elapsed: %d\n", qry->elapsedTime );
421 fprintf( stream, " stop flag: %s\n", qry->stopFlag ? "yes" : "no" );
422 fprintf( stream, " busy flag: %s\n", qry->busyFlag ? "yes" : "no" );
423 fprintf( stream, " aged flag: %s\n", qry->agedFlag ? "yes" : "no" );
424 fprintf( stream, " completed: %s\n", qry->completed ? "yes" : "no" );
428 * Free linked lists of character strings.
429 * \param listName List of common names.
430 * \param listAddr List of addresses.
431 * \param listFirst List of first names.
432 * \param listLast List of last names.
434 static void ldapqry_free_lists(
435 GSList *listName, GSList *listAddr, GSList *listFirst,
438 mgu_free_list( listName );
439 mgu_free_list( listAddr );
440 mgu_free_list( listFirst );
441 mgu_free_list( listLast );
445 * Add all LDAP attribute values to a list.
446 * \param ld LDAP handle.
447 * \param entry LDAP entry to process.
448 * \param attr LDAP attribute.
449 * \return List of values.
451 static GSList *ldapqry_add_list_values(
452 LDAP *ld, LDAPMessage *entry, char *attr )
458 if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
459 for( i = 0; vals[i] != NULL; i++ ) {
460 /* printf( "lv\t%s: %s\n", attr, vals[i] ); */
461 list = g_slist_append( list, g_strdup( vals[i] ) );
464 ldap_value_free( vals );
469 * Add a single attribute value to a list.
470 * \param ld LDAP handle.
471 * \param entry LDAP entry to process.
472 * \param attr LDAP attribute name to process.
473 * \return List of values; only one value will be present.
475 static GSList *ldapqry_add_single_value( LDAP *ld, LDAPMessage *entry, char *attr ) {
479 if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
480 if( vals[0] != NULL ) {
481 /* printf( "sv\t%s: %s\n", attr, vals[0] ); */
482 list = g_slist_append( list, g_strdup( vals[0] ) );
485 ldap_value_free( vals );
490 * Build an address list entry and append to list of address items. Name is formatted
491 * as "<first-name> <last-name>".
493 * \param cache Address cache to load.
494 * \param qry Query object to process.
495 * \param dn DN for entry found on server.
496 * \param listName List of common names for entry; see notes below.
497 * \param listAddr List of EMail addresses for entry.
498 * \param listFirst List of first names for entry.
499 * \param listLast List of last names for entry.
501 * \return List of ItemEMail objects.
504 * 1) Each LDAP server entry may have multiple LDAP attributes with the same
505 * name. For example, a single entry for a person may have more than one
506 * common name, email address, etc.
508 * 2) The DN for the entry is unique for the server.
510 static GList *ldapqry_build_items_fl(
511 AddressCache *cache, LdapQuery *qry, gchar *dn,
512 GSList *listName, GSList *listAddr, GSList *listFirst,
516 gchar *firstName = NULL, *lastName = NULL, *fullName = NULL;
524 if( listAddr == NULL ) return listReturn;
526 /* Find longest first name in list */
527 firstName = mgu_slist_longest_entry( listFirst );
529 /* Format last name */
531 lastName = listLast->data;
534 /* Find longest common name */
536 fullName = mgu_slist_longest_entry( listName );
537 if( fullName == NULL ) {
538 /* Format a full name from first and last names */
541 fullName = g_strdup_printf( "%s %s", firstName, lastName );
544 fullName = g_strdup_printf( "%s", firstName );
549 fullName = g_strdup_printf( "%s", lastName );
553 g_strchug( fullName ); g_strchomp( fullName );
558 /* Create new folder for results */
559 if( ADDRQUERY_FOLDER(qry) == NULL ) {
560 folder = addritem_create_item_folder();
561 addritem_folder_set_name( folder, ADDRQUERY_NAME(qry) );
562 addritem_folder_set_remarks( folder, "" );
563 addrcache_id_folder( cache, folder );
564 addrcache_add_folder( cache, folder );
565 ADDRQUERY_FOLDER(qry) = folder;
567 /* Specify folder type and back reference */
568 folder->folderType = ADDRFOLDER_QUERY_RESULTS;
569 folder->folderData = ( gpointer ) qry;
570 folder->isHidden = TRUE;
573 /* Add person into folder */
574 person = addritem_create_item_person();
575 addritem_person_set_common_name( person, fullName );
576 addritem_person_set_first_name( person, firstName );
577 addritem_person_set_last_name( person, lastName );
578 addrcache_id_person( cache, person );
579 addritem_person_set_external_id( person, dn );
580 addrcache_folder_add_person( cache, ADDRQUERY_FOLDER(qry), person );
584 /* Add each address item */
585 nodeAddress = listAddr;
586 while( nodeAddress ) {
587 email = addritem_create_item_email();
588 addritem_email_set_address( email, nodeAddress->data );
589 addrcache_id_email( cache, email );
590 addrcache_person_add_email( cache, person, email );
591 addritem_person_add_email( person, email );
592 listReturn = g_list_append( listReturn, email );
593 nodeAddress = g_slist_next( nodeAddress );
596 /* Free any allocated memory */
600 fullName = firstName = lastName = NULL;
606 * Process a single search entry.
607 * \param cache Address cache to load.
608 * \param qry Query object to process.
609 * \param ld LDAP handle.
610 * \param e LDAP message.
611 * \return List of EMail objects found.
613 static GList *ldapqry_process_single_entry(
614 AddressCache *cache, LdapQuery *qry, LDAP *ld, LDAPMessage *e )
620 GSList *listName = NULL, *listAddress = NULL;
621 GSList *listFirst = NULL, *listLast = NULL;
626 dnEntry = ldap_get_dn( ld, e );
627 /* printf( "DN: %s\n", dnEntry ); */
629 /* Process all attributes */
630 for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL;
631 attribute = ldap_next_attribute( ld, e, ber ) ) {
633 if( strcasecmp( attribute, ctl->attribEMail ) == 0 ) {
634 listAddress = ldapqry_add_list_values( ld, e, attribute );
636 else if( strcasecmp( attribute, ctl->attribCName ) == 0 ) {
637 listName = ldapqry_add_list_values( ld, e, attribute );
639 else if( strcasecmp( attribute, ctl->attribFName ) == 0 ) {
640 listFirst = ldapqry_add_list_values( ld, e, attribute );
642 else if( strcasecmp( attribute, ctl->attribLName ) == 0 ) {
643 listLast = ldapqry_add_single_value( ld, e, attribute );
646 /* Free memory used to store attribute */
647 ldap_memfree( attribute );
650 /* Format and add items to cache */
651 listReturn = ldapqry_build_items_fl(
652 cache, qry, dnEntry, listName, listAddress, listFirst, listLast );
655 ldapqry_free_lists( listName, listAddress, listFirst, listLast );
656 listName = listAddress = listFirst = listLast = NULL;
667 * Check parameters that are required for a search. This should
668 * be called before performing a search.
669 * \param qry Query object to process.
670 * \return <i>TRUE</i> if search criteria appear OK.
672 gboolean ldapqry_check_search( LdapQuery *qry ) {
674 ADDRQUERY_RETVAL(qry) = LDAPRC_CRITERIA;
676 /* Test for control data */
682 /* Test for search value */
683 if( ADDRQUERY_SEARCHVALUE(qry) == NULL ) {
686 if( strlen( ADDRQUERY_SEARCHVALUE(qry) ) < 1 ) {
689 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
694 * Touch the query. This nudges the touch time with the current time.
695 * \param qry Query object to process.
697 void ldapqry_touch( LdapQuery *qry ) {
698 qry->touchTime = time( NULL );
699 qry->agedFlag = FALSE;
703 * Connect to LDAP server.
704 * \param qry Query object to process.
705 * \return Error/status code.
707 static gint ldapqry_connect( LdapQuery *qry ) {
712 /* Initialize connection */
713 /* printf( "===ldapqry_connect===\n" ); */
714 /* ldapqry_print( qry, stdout ); */
716 /* ldapctl_print( ctl, stdout ); */
717 /* printf( "======\n" ); */
718 ldapqry_touch( qry );
719 qry->startTime = qry->touchTime;
720 qry->elapsedTime = -1;
721 ADDRQUERY_RETVAL(qry) = LDAPRC_INIT;
722 if( ( ld = ldap_init( ctl->hostName, ctl->port ) ) == NULL ) {
723 return ADDRQUERY_RETVAL(qry);
726 ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
727 if( ldapqry_get_stop_flag( qry ) ) {
728 return ADDRQUERY_RETVAL(qry);
730 ldapqry_touch( qry );
733 printf( "connected to LDAP host %s on port %d\n", ctl->hostName, ctl->port );
736 /* Bind to the server, if required */
737 ADDRQUERY_RETVAL(qry) = LDAPRC_BIND;
739 if( * ctl->bindDN != '\0' ) {
740 /* printf( "binding...\n" ); */
741 rc = ldap_simple_bind_s( ld, ctl->bindDN, ctl->bindPass );
742 /* printf( "rc=%d\n", rc ); */
743 if( rc != LDAP_SUCCESS ) {
745 printf( "LDAP Error: ldap_simple_bind_s: %s\n",
746 ldap_err2string( rc ) );
748 return ADDRQUERY_RETVAL(qry);
752 ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
753 if( ldapqry_get_stop_flag( qry ) ) {
754 return ADDRQUERY_RETVAL(qry);
756 ldapqry_touch( qry );
758 ADDRQUERY_RETVAL(qry) = LDAP_SUCCESS;
760 return ADDRQUERY_RETVAL(qry);
764 * Connect to LDAP server.
765 * \param qry Query object to process.
766 * \return Error/status code.
768 static gint ldapqry_disconnect( LdapQuery *qry ) {
770 if( qry->ldap ) ldap_unbind( qry->ldap );
773 ldapqry_touch( qry );
774 qry->elapsedTime = qry->touchTime - qry->startTime;
776 return ADDRQUERY_RETVAL(qry);
780 * Perform the LDAP search, reading LDAP entries into cache.
781 * Note that one LDAP entry can have multiple values for many of its
782 * attributes. If these attributes are E-Mail addresses; these are
783 * broken out into separate address items. For any other attribute,
784 * only the first occurrence is read.
786 * \param qry Query object to process.
787 * \return Error/status code.
789 static gint ldapqry_search_retrieve( LdapQuery *qry ) {
792 LDAPMessage *result, *e;
795 gboolean entriesFound;
797 struct timeval timeout;
802 /* Initialize some variables */
805 cache = qry->server->addressCache;
806 timeout.tv_sec = ctl->timeOut;
807 timeout.tv_usec = 0L;
808 entriesFound = FALSE;
809 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
811 /* Define all attributes we are interested in. */
812 attribs = ldapctl_attribute_array( ctl );
814 /* Create LDAP search string */
815 criteria = ldapctl_format_criteria( ctl, ADDRQUERY_SEARCHVALUE(qry) );
816 /* printf( "Search criteria ::%s::\n", criteria ); */
819 * Execute the search - this step may take some time to complete
820 * depending on network traffic and server response time.
822 ADDRQUERY_RETVAL(qry) = LDAPRC_TIMEOUT;
823 rc = ldap_search_ext_s( ld, ctl->baseDN, LDAP_SCOPE_SUBTREE, criteria,
824 attribs, 0, NULL, NULL, &timeout, 0, &result );
825 ldapctl_free_attribute_array( attribs );
828 if( rc == LDAP_TIMEOUT ) {
829 return ADDRQUERY_RETVAL(qry);
831 ADDRQUERY_RETVAL(qry) = LDAPRC_SEARCH;
832 if( rc != LDAP_SUCCESS ) {
834 printf( "LDAP Error: ldap_search_st: %s\n", ldap_err2string( rc ) );
836 return ADDRQUERY_RETVAL(qry);
838 ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
839 if( ldapqry_get_stop_flag( qry ) ) {
840 return ADDRQUERY_RETVAL(qry);
842 ldapqry_touch( qry );
845 printf( "Total results are: %d\n", ldap_count_entries( ld, result ) );
848 /* Process results */
851 ldapqry_touch( qry );
852 if( qry->entriesRead >= ctl->maxEntries ) break;
855 if( ldapqry_get_stop_flag( qry ) ) {
862 e = ldap_first_entry( ld, result );
865 e = ldap_next_entry( ld, e );
867 if( e == NULL ) break;
871 /* Setup a critical section here */
872 pthread_mutex_lock( qry->mutexEntry );
875 listEMail = ldapqry_process_single_entry( cache, qry, ld, e );
877 /* Process callback */
878 if( qry->callBackEntry ) {
879 qry->callBackEntry( qry, ADDRQUERY_ID(qry), listEMail, qry->data );
882 g_list_free( listEMail );
885 pthread_mutex_unlock( qry->mutexEntry );
888 /* Free up and disconnect */
889 ldap_msgfree( result );
892 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
895 ADDRQUERY_RETVAL(qry) = LDAPRC_NOENTRIES;
898 return ADDRQUERY_RETVAL(qry);
902 * Connection, perform search and disconnect.
903 * \param qry Query object to process.
904 * \return Error/status code.
906 static gint ldapqry_perform_search( LdapQuery *qry ) {
907 /* Check search criteria */
908 if( ! ldapqry_check_search( qry ) ) {
909 return ADDRQUERY_RETVAL(qry);
914 ldapqry_connect( qry );
915 if( ADDRQUERY_RETVAL(qry) == LDAPRC_SUCCESS ) {
917 ldapqry_search_retrieve( qry );
920 ldapqry_disconnect( qry );
923 return ADDRQUERY_RETVAL(qry);
927 * Wrapper around search.
928 * \param qry Query object to process.
929 * \return Error/status code.
931 gint ldapqry_search( LdapQuery *qry ) {
934 g_return_val_if_fail( qry != NULL, -1 );
935 g_return_val_if_fail( qry->control != NULL, -1 );
937 ldapqry_touch( qry );
938 qry->completed = FALSE;
940 /* Setup pointer to thread specific area */
941 pthread_setspecific( _queryThreadKey_, qry );
943 pthread_detach( pthread_self() );
945 /* Now perform the search */
946 qry->entriesRead = 0;
947 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
948 ldapqry_set_busy_flag( qry, TRUE );
949 ldapqry_set_stop_flag( qry, FALSE );
950 if( ADDRQUERY_SEARCHTYPE(qry) == ADDRSEARCH_LOCATE ) {
951 retVal = ldapqry_perform_locate( qry );
954 retVal = ldapqry_perform_search( qry );
956 if( retVal == LDAPRC_SUCCESS ) {
957 qry->server->addressCache->dataRead = TRUE;
958 qry->server->addressCache->accessFlag = FALSE;
959 if( ldapqry_get_stop_flag( qry ) ) {
961 printf( "Search was terminated prematurely\n" );
965 ldapqry_touch( qry );
966 qry->completed = TRUE;
968 printf( "Search ran to completion\n" );
972 ldapqry_set_stop_flag( qry, TRUE );
973 ldapqry_set_busy_flag( qry, FALSE );
975 /* Process callback */
976 if( qry->callBackEnd ) {
977 qry->callBackEnd( qry, ADDRQUERY_ID(qry), ADDRQUERY_RETVAL(qry), qry->data );
980 return ADDRQUERY_RETVAL(qry);
984 * Read data into list using a background thread. Callback function will be
985 * notified when search is complete.
986 * \param qry Query object to process.
987 * \return Error/status code.
989 gint ldapqry_read_data_th( LdapQuery *qry ) {
990 g_return_val_if_fail( qry != NULL, -1 );
991 g_return_val_if_fail( qry->control != NULL, -1 );
993 ldapqry_set_stop_flag( qry, FALSE );
994 ldapqry_touch( qry );
995 if( ldapqry_check_search( qry ) ) {
996 if( ADDRQUERY_RETVAL(qry) == LDAPRC_SUCCESS ) {
998 printf( "Starting LDAP search thread\n");
1000 ldapqry_set_busy_flag( qry, TRUE );
1001 qry->thread = g_malloc0( sizeof( pthread_t ) );
1004 pthread_create( qry->thread, NULL,
1005 (void *) ldapqry_search, (void *) qry );
1008 return ADDRQUERY_RETVAL(qry);
1012 * Cleanup LDAP thread data. This function will be called when each thread
1013 * exits. Note that the thread object will be freed by the kernel.
1014 * \param ptr Pointer to object being destroyed (a query object in this case).
1016 static void ldapqry_destroyer( void * ptr ) {
1019 qry = ( LdapQuery * ) ptr;
1021 printf( "ldapqry_destroyer::%d::%s\n", (int) pthread_self(), ADDRQUERY_NAME(qry) );
1024 /* Perform any destruction here */
1025 if( qry->control != NULL ) {
1026 ldapctl_free( qry->control );
1028 qry->control = NULL;
1030 ldapqry_set_busy_flag( qry, FALSE );
1032 printf( "...destroy exiting\n" );
1037 * Cancel thread associated with query.
1038 * \param qry Query object to process.
1040 void ldapqry_cancel( LdapQuery *qry ) {
1041 g_return_if_fail( qry != NULL );
1044 printf( "cancelling::%d::%s\n", (int) pthread_self(), ADDRQUERY_NAME(qry) );
1046 if( ldapqry_get_busy_flag( qry ) ) {
1048 pthread_cancel( * qry->thread );
1054 * Initialize LDAP query. This function should be called once before executing
1055 * any LDAP queries to initialize thread specific data.
1057 void ldapqry_initialize( void ) {
1058 /* printf( "ldapqry_initialize...\n" ); */
1059 if( ! _queryThreadInit_ ) {
1061 printf( "ldapqry_initialize::creating thread specific area\n" );
1063 pthread_key_create( &_queryThreadKey_, ldapqry_destroyer );
1064 _queryThreadInit_ = TRUE;
1066 /* printf( "ldapqry_initialize... done!\n" ); */
1070 * Age the query based on LDAP control parameters.
1071 * \param qry Query object to process.
1072 * \param maxAge Maximum age of query (in seconds).
1074 void ldapqry_age( LdapQuery *qry, gint maxAge ) {
1077 g_return_if_fail( qry != NULL );
1079 /* Limit the time that queries can hang around */
1080 if( maxAge < 1 ) maxAge = LDAPCTL_MAX_QUERY_AGE;
1082 /* Check age of query */
1083 age = time( NULL ) - qry->touchTime;
1084 if( age > maxAge ) {
1085 qry->agedFlag = TRUE;
1090 * Delete folder associated with query results.
1091 * \param qry Query object to process.
1093 void ldapqry_delete_folder( LdapQuery *qry ) {
1094 AddressCache *cache;
1097 g_return_if_fail( qry != NULL );
1099 folder = ADDRQUERY_FOLDER(qry);
1101 cache = qry->server->addressCache;
1102 folder = addrcache_remove_folder_delete( cache, folder );
1104 addritem_free_item_folder( folder );
1106 ADDRQUERY_FOLDER(qry) = NULL;
1111 * Create a name/value pair object.
1114 * \return Initialized object.
1116 static NameValuePair *ldapqry_create_name_value( const gchar *n, const gchar *v ) {
1117 NameValuePair *nvp = g_new0( NameValuePair, 1 );
1119 nvp->name = g_strdup( n );
1120 nvp->value = g_strdup( v );
1125 * Free up name/value pair object.
1126 * \param nvp Name/value object.
1128 void ldapqry_free_name_value( NameValuePair *nvp ) {
1130 g_free( nvp->name );
1131 g_free( nvp->value );
1132 nvp->name = nvp->value = NULL;
1138 * Print name/value pair object for debug.
1139 * \param nvp Name/value object.
1140 * \param stream Output stream.
1142 void ldapqry_print_name_value( NameValuePair *nvp, FILE *stream ) {
1144 fprintf( stream, "n/v ::%s::%s::\n", nvp->name, nvp->value );
1149 * Free up a list name/value pair objects.
1150 * \param list List of name/value objects.
1152 void ldapqry_free_list_name_value( GList *list ) {
1157 NameValuePair *nvp = ( NameValuePair * ) node->data;
1158 ldapqry_free_name_value( nvp );
1160 node = g_list_next( node );
1162 g_list_free( list );
1166 * Load a list of name/value pairs from LDAP attributes.
1167 * \param ld LDAP handle.
1168 * \param e LDAP message.
1169 * \param attr Attribute name.
1170 * \param listValues List to populate.
1171 * \return List of attribute name/value pairs.
1173 static GList *ldapqry_load_attrib_values(
1174 LDAP *ld, LDAPMessage *entry, char *attr,
1183 if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
1184 for( i = 0; vals[i] != NULL; i++ ) {
1185 nvp = ldapqry_create_name_value( attr, vals[i] );
1186 list = g_list_append( list, nvp );
1189 ldap_value_free( vals );
1194 * Fetch a list of all attributes.
1195 * \param ld LDAP handle.
1196 * \param e LDAP message.
1197 * \return List of attribute name/value pairs.
1199 static GList *ldapqry_fetch_attribs( LDAP *ld, LDAPMessage *e )
1203 GList *listValues = NULL;
1205 /* Process all attributes */
1206 for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL;
1207 attribute = ldap_next_attribute( ld, e, ber ) ) {
1208 listValues = ldapqry_load_attrib_values( ld, e, attribute, listValues );
1209 ldap_memfree( attribute );
1219 #define CRITERIA_SINGLE "(objectclass=*)"
1222 * Perform the data retrieval for a specific LDAP record.
1224 * \param qry Query object to process.
1225 * \return Error/status code.
1227 static gint ldapqry_locate_retrieve( LdapQuery *qry ) {
1230 LDAPMessage *result, *e;
1231 gboolean entriesFound;
1233 struct timeval timeout;
1238 /* Initialize some variables */
1241 dn = ADDRQUERY_SEARCHVALUE(qry);
1242 timeout.tv_sec = ctl->timeOut;
1243 timeout.tv_usec = 0L;
1244 entriesFound = FALSE;
1247 * Execute the search - this step may take some time to complete
1248 * depending on network traffic and server response time.
1250 ADDRQUERY_RETVAL(qry) = LDAPRC_TIMEOUT;
1251 rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_BASE, CRITERIA_SINGLE,
1252 NULL, 0, NULL, NULL, &timeout, 0, &result );
1253 if( rc == LDAP_TIMEOUT ) {
1254 return ADDRQUERY_RETVAL(qry);
1256 ADDRQUERY_RETVAL(qry) = LDAPRC_SEARCH;
1257 if( rc != LDAP_SUCCESS ) {
1259 printf( "LDAP Error: ldap_search_st: %s\n", ldap_err2string( rc ) );
1261 return ADDRQUERY_RETVAL(qry);
1263 ADDRQUERY_RETVAL(qry) = LDAPRC_STOP_FLAG;
1264 if( ldapqry_get_stop_flag( qry ) ) {
1265 return ADDRQUERY_RETVAL(qry);
1267 ldapqry_touch( qry );
1270 printf( "Total results are: %d\n", ldap_count_entries( ld, result ) );
1273 /* Process results */
1276 ldapqry_touch( qry );
1277 if( qry->entriesRead >= ctl->maxEntries ) break;
1280 if( ldapqry_get_stop_flag( qry ) ) {
1284 /* Retrieve entry */
1287 e = ldap_first_entry( ld, result );
1290 e = ldap_next_entry( ld, e );
1292 if( e == NULL ) break;
1294 entriesFound = TRUE;
1296 /* Setup a critical section here */
1297 pthread_mutex_lock( qry->mutexEntry );
1300 listValues = ldapqry_fetch_attribs( ld, e );
1302 /* Process callback */
1303 if( qry->callBackEntry ) {
1304 qry->callBackEntry( qry, ADDRQUERY_ID(qry), listValues, qry->data );
1306 ldapqry_free_list_name_value( listValues );
1309 pthread_mutex_unlock( qry->mutexEntry );
1312 /* Free up and disconnect */
1313 ldap_msgfree( result );
1315 if( entriesFound ) {
1316 ADDRQUERY_RETVAL(qry) = LDAPRC_SUCCESS;
1319 ADDRQUERY_RETVAL(qry) = LDAPRC_NOENTRIES;
1322 return ADDRQUERY_RETVAL(qry);
1326 * Perform the search to locate a specific LDAP record identified by
1327 * distinguished name (dn).
1329 * \param qry Query object to process.
1330 * \return Error/status code.
1332 gint ldapqry_perform_locate( LdapQuery *qry ) {
1335 ldapqry_connect( qry );
1336 if( ADDRQUERY_RETVAL(qry) == LDAPRC_SUCCESS ) {
1337 /* Perform search */
1338 ldapqry_locate_retrieve( qry );
1341 ldapqry_disconnect( qry );
1344 /* Process callback */
1345 if( qry->callBackEnd ) {
1346 qry->callBackEnd( qry, ADDRQUERY_ID(qry), ADDRQUERY_RETVAL(qry), qry->data );
1349 return ADDRQUERY_RETVAL(qry);
1353 * Remove results (folder and data) for specified LDAP query.
1354 * \param qry Query object to process.
1356 void ldapquery_remove_results( LdapQuery *qry ) {
1357 /* Set query as aged - will be retired on a later call */
1358 ldapqry_set_aged_flag( qry, TRUE );
1360 printf( "ldapquery_remove_results...\n" );
1361 printf( "testing busy flag...\n" );
1363 if( ldapqry_get_busy_flag( qry ) ) {
1364 /* Query is still busy - cancel query */
1365 /* printf( "\tquery is still busy running...\n" ); */
1366 ldapqry_set_stop_flag( qry, TRUE );
1367 ldapqry_cancel( qry );
1371 /* printf( "\tquery can be deleted!\n" ); */
1372 ldapqry_delete_folder( qry );
1376 #endif /* USE_LDAP */