removed some funny bugs
[claws.git] / src / syldap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 Match Grun
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 /*
21  * Functions necessary to access LDAP servers.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #ifdef USE_LDAP
29
30 #include <glib.h>
31 #include <sys/time.h>
32 #include <string.h>
33 #include <ldap.h>
34 #include <lber.h>
35 #include <pthread.h>
36 #include <dlfcn.h>
37
38 #include "mgutils.h"
39 #include "addritem.h"
40 #include "addrcache.h"
41 #include "syldap.h"
42
43 /*
44 * Create new LDAP server interface object.
45 */
46 SyldapServer *syldap_create() {
47         SyldapServer *ldapServer;
48         ldapServer = g_new0( SyldapServer, 1 );
49         ldapServer->name = NULL;
50         ldapServer->hostName = NULL;
51         ldapServer->port = SYLDAP_DFL_PORT;
52         ldapServer->baseDN = NULL;
53         ldapServer->bindDN = NULL;
54         ldapServer->bindPass = NULL;
55         ldapServer->searchCriteria = NULL;
56         ldapServer->searchValue = NULL;
57         ldapServer->entriesRead = 0;
58         ldapServer->maxEntries = SYLDAP_MAX_ENTRIES;
59         ldapServer->timeOut = SYLDAP_DFL_TIMEOUT;
60         ldapServer->newSearch = TRUE;
61         ldapServer->addressCache = addrcache_create();
62         ldapServer->thread = NULL;
63         ldapServer->busyFlag = FALSE;
64         ldapServer->retVal = MGU_SUCCESS;
65         ldapServer->callBack = NULL;
66         ldapServer->accessFlag = FALSE;
67         return ldapServer;
68 }
69
70 /*
71 * Specify name to be used.
72 */
73 void syldap_set_name( SyldapServer* ldapServer, const gchar *value ) {
74         ldapServer->name = mgu_replace_string( ldapServer->name, value );
75         g_strstrip( ldapServer->name );
76 }
77
78 /*
79 * Specify hostname to be used.
80 */
81 void syldap_set_host( SyldapServer* ldapServer, const gchar *value ) {
82         addrcache_refresh( ldapServer->addressCache );
83         ldapServer->hostName = mgu_replace_string( ldapServer->hostName, value );
84         g_strstrip( ldapServer->hostName );
85 }
86
87 /*
88 * Specify port to be used.
89 */
90 void syldap_set_port( SyldapServer* ldapServer, const gint value ) {
91         addrcache_refresh( ldapServer->addressCache );
92         if( value > 0 ) {
93                 ldapServer->port = value;
94         }
95         else {
96                 ldapServer->port = SYLDAP_DFL_PORT;
97         }
98 }
99
100 /*
101 * Specify base DN to be used.
102 */
103 void syldap_set_base_dn( SyldapServer* ldapServer, const gchar *value ) {
104         addrcache_refresh( ldapServer->addressCache );
105         ldapServer->baseDN = mgu_replace_string( ldapServer->baseDN, value );
106         g_strstrip( ldapServer->baseDN );
107 }
108
109 /*
110 * Specify bind DN to be used.
111 */
112 void syldap_set_bind_dn( SyldapServer* ldapServer, const gchar *value ) {
113         addrcache_refresh( ldapServer->addressCache );
114         ldapServer->bindDN = mgu_replace_string( ldapServer->bindDN, value );
115         g_strstrip( ldapServer->bindDN );
116 }
117
118 /*
119 * Specify bind password to be used.
120 */
121 void syldap_set_bind_password( SyldapServer* ldapServer, const gchar *value ) {
122         addrcache_refresh( ldapServer->addressCache );
123         ldapServer->bindPass = mgu_replace_string( ldapServer->bindPass, value );
124         g_strstrip( ldapServer->bindPass );
125 }
126
127 /*
128 * Specify search criteria to be used.
129 */
130 void syldap_set_search_criteria( SyldapServer* ldapServer, const gchar *value ) {
131         addrcache_refresh( ldapServer->addressCache );
132         ldapServer->searchCriteria = mgu_replace_string( ldapServer->searchCriteria, value );
133         g_strstrip( ldapServer->searchCriteria );
134         ldapServer->newSearch = TRUE;
135 }
136
137 /*
138 * Specify search value to be searched for.
139 */
140 void syldap_set_search_value( SyldapServer* ldapServer, const gchar *value ) {
141         addrcache_refresh( ldapServer->addressCache );
142         ldapServer->searchValue = mgu_replace_string( ldapServer->searchValue, value );
143         g_strstrip( ldapServer->searchValue );
144         ldapServer->newSearch = TRUE;
145 }
146
147 /*
148 * Specify maximum number of entries to retrieve.
149 */
150 void syldap_set_max_entries( SyldapServer* ldapServer, const gint value ) {
151         addrcache_refresh( ldapServer->addressCache );
152         if( value > 0 ) {
153                 ldapServer->maxEntries = value;
154         }
155         else {
156                 ldapServer->maxEntries = SYLDAP_MAX_ENTRIES;
157         }
158 }
159
160 /*
161 * Specify timeout value for LDAP operation (in seconds).
162 */
163 void syldap_set_timeout( SyldapServer* ldapServer, const gint value ) {
164         addrcache_refresh( ldapServer->addressCache );
165         if( value > 0 ) {
166                 ldapServer->timeOut = value;
167         }
168         else {
169                 ldapServer->timeOut = SYLDAP_DFL_TIMEOUT;
170         }
171 }
172
173 /*
174 * Register a callback function. When called, the function will be passed
175 * this object as an argument.
176 */
177 void syldap_set_callback( SyldapServer *ldapServer, void *func ) {
178         ldapServer->callBack = func;
179 }
180
181 void syldap_set_accessed( SyldapServer *ldapServer, const gboolean value ) {
182         g_return_if_fail( ldapServer != NULL );
183         ldapServer->accessFlag = value;
184 }
185
186 /*
187 * Refresh internal variables to force a file read.
188 */
189 void syldap_force_refresh( SyldapServer *ldapServer ) {
190         addrcache_refresh( ldapServer->addressCache );
191         ldapServer->newSearch = TRUE;
192 }
193
194 gint syldap_get_status( SyldapServer *ldapServer ) {
195         g_return_val_if_fail( ldapServer != NULL, -1 );
196         return ldapServer->retVal;
197 }
198
199 ItemFolder *syldap_get_root_folder( SyldapServer *ldapServer ) {
200         g_return_val_if_fail( ldapServer != NULL, NULL );
201         return addrcache_get_root_folder( ldapServer->addressCache );
202 }
203
204 gchar *syldap_get_name( SyldapServer *ldapServer ) {
205         g_return_val_if_fail( ldapServer != NULL, NULL );
206         return ldapServer->name;
207 }
208
209 gboolean syldap_get_accessed( SyldapServer *ldapServer ) {
210         g_return_val_if_fail( ldapServer != NULL, FALSE );
211         return ldapServer->accessFlag;
212 }
213
214 /*
215 * Free up LDAP server interface object by releasing internal memory.
216 */
217 void syldap_free( SyldapServer *ldapServer ) {
218         g_return_if_fail( ldapServer != NULL );
219
220         ldapServer->callBack = NULL;
221
222         /* Free internal stuff */
223         g_free( ldapServer->name );
224         g_free( ldapServer->hostName );
225         g_free( ldapServer->baseDN );
226         g_free( ldapServer->bindDN );
227         g_free( ldapServer->bindPass );
228         g_free( ldapServer->searchCriteria );
229         g_free( ldapServer->searchValue );
230
231         ldapServer->port = 0;
232         ldapServer->entriesRead = 0;
233         ldapServer->maxEntries = 0;
234         ldapServer->newSearch = FALSE;
235
236         /* Clear cache */
237         addrcache_clear( ldapServer->addressCache );
238         addrcache_free( ldapServer->addressCache );
239
240         /* Clear pointers */
241         ldapServer->name = NULL;
242         ldapServer->hostName = NULL;
243         ldapServer->baseDN = NULL;
244         ldapServer->bindDN = NULL;
245         ldapServer->bindPass = NULL;
246         ldapServer->searchCriteria = NULL;
247         ldapServer->searchValue = NULL;
248         ldapServer->addressCache = NULL;
249         ldapServer->thread = NULL;
250         ldapServer->busyFlag = FALSE;
251         ldapServer->retVal = MGU_SUCCESS;
252         ldapServer->accessFlag = FALSE;
253
254         /* Now release LDAP object */
255         g_free( ldapServer );
256
257 }
258
259 /*
260 * Display object to specified stream.
261 */
262 void syldap_print_data( SyldapServer *ldapServer, FILE *stream ) {
263         g_return_if_fail( ldapServer != NULL );
264
265         fprintf( stream, "SyldapServer:\n" );
266         fprintf( stream, "     name: '%s'\n", ldapServer->name );
267         fprintf( stream, "host name: '%s'\n", ldapServer->hostName );
268         fprintf( stream, "     port: %d\n",   ldapServer->port );
269         fprintf( stream, "  base dn: '%s'\n", ldapServer->baseDN );
270         fprintf( stream, "  bind dn: '%s'\n", ldapServer->bindDN );
271         fprintf( stream, "bind pass: '%s'\n", ldapServer->bindPass );
272         fprintf( stream, " criteria: '%s'\n", ldapServer->searchCriteria );
273         fprintf( stream, "searchval: '%s'\n", ldapServer->searchValue );
274         fprintf( stream, "max entry: %d\n",   ldapServer->maxEntries );
275         fprintf( stream, " num read: %d\n",   ldapServer->entriesRead );
276         fprintf( stream, "  ret val: %d\n",   ldapServer->retVal );
277         addrcache_print( ldapServer->addressCache, stream );
278         addritem_print_item_folder( ldapServer->addressCache->rootFolder, stream );
279 }
280
281 /*
282 * Display object to specified stream.
283 */
284 void syldap_print_short( SyldapServer *ldapServer, FILE *stream ) {
285         g_return_if_fail( ldapServer != NULL );
286
287         fprintf( stream, "SyldapServer:\n" );
288         fprintf( stream, "     name: '%s'\n", ldapServer->name );
289         fprintf( stream, "host name: '%s'\n", ldapServer->hostName );
290         fprintf( stream, "     port: %d\n",   ldapServer->port );
291         fprintf( stream, "  base dn: '%s'\n", ldapServer->baseDN );
292         fprintf( stream, "  bind dn: '%s'\n", ldapServer->bindDN );
293         fprintf( stream, "bind pass: '%s'\n", ldapServer->bindPass );
294         fprintf( stream, " criteria: '%s'\n", ldapServer->searchCriteria );
295         fprintf( stream, "searchval: '%s'\n", ldapServer->searchValue );
296         fprintf( stream, "max entry: %d\n",   ldapServer->maxEntries );
297         fprintf( stream, " num read: %d\n",   ldapServer->entriesRead );
298         fprintf( stream, "  ret val: %d\n",   ldapServer->retVal );
299 }
300
301 /*
302 * Build an address list entry and append to list of address items. Name is formatted
303 * as it appears in the common name (cn) attribute.
304 */
305 static void syldap_build_items_cn( SyldapServer *ldapServer, GSList *listName, GSList *listAddr ) {
306         ItemPerson *person;
307         ItemEMail *email;
308         GSList *nodeName = listName;
309
310         while( nodeName ) {
311                 GSList *nodeAddress = listAddr;
312                 person = addritem_create_item_person();
313                 addritem_person_set_common_name( person, nodeName->data );
314                 addrcache_id_person( ldapServer->addressCache, person );
315                 addrcache_add_person( ldapServer->addressCache, person );
316
317                 while( nodeAddress ) {
318                         email = addritem_create_item_email();
319                         addritem_email_set_address( email, nodeAddress->data );
320                         addrcache_id_email( ldapServer->addressCache, email );
321                         addrcache_person_add_email( ldapServer->addressCache, person, email );
322                         nodeAddress = g_slist_next( nodeAddress );
323                         ldapServer->entriesRead++;
324                 }
325                 nodeName = g_slist_next( nodeName );
326         }
327 }
328
329 /*
330 * Build an address list entry and append to list of address items. Name is formatted
331 * as "<first-name> <last-name>".
332 */
333 static void syldap_build_items_fl( SyldapServer *ldapServer, GSList *listAddr, GSList *listFirst, GSList *listLast  ) {
334         GSList *nodeFirst = listFirst;
335         GSList *nodeAddress = listAddr;
336         gchar *firstName = NULL, *lastName = NULL, *fullName = NULL;
337         gint iLen = 0, iLenT = 0;
338         ItemPerson *person;
339         ItemEMail *email;
340
341         /* Find longest first name in list */
342         while( nodeFirst ) {
343                 if( firstName == NULL ) {
344                         firstName = nodeFirst->data;
345                         iLen = strlen( firstName );
346                 }
347                 else {
348                         if( ( iLenT = strlen( nodeFirst->data ) ) > iLen ) {
349                                 firstName = nodeFirst->data;
350                                 iLen = iLenT;
351                         }
352                 }
353                 nodeFirst = g_slist_next( nodeFirst );
354         }
355
356         /* Format name */
357         if( listLast ) {
358                 lastName = listLast->data;
359         }
360
361         if( firstName ) {
362                 if( lastName ) {
363                         fullName = g_strdup_printf( "%s %s", firstName, lastName );
364                 }
365                 else {
366                         fullName = g_strdup_printf( "%s", firstName );
367                 }
368         }
369         else {
370                 if( lastName ) {
371                         fullName = g_strdup_printf( "%s", lastName );
372                 }
373         }
374         if( fullName ) {
375                 g_strchug( fullName ); g_strchomp( fullName );
376         }
377
378         if( nodeAddress ) {
379                 person = addritem_create_item_person();
380                 addritem_person_set_common_name( person, fullName );
381                 addritem_person_set_first_name( person, firstName );
382                 addritem_person_set_last_name( person, lastName );
383                 addrcache_id_person( ldapServer->addressCache, person );
384                 addrcache_add_person( ldapServer->addressCache, person );
385         }
386
387         /* Add address item */
388         while( nodeAddress ) {
389                 email = addritem_create_item_email();
390                 addritem_email_set_address( email, nodeAddress->data );
391                 addrcache_id_email( ldapServer->addressCache, email );
392                 addrcache_person_add_email( ldapServer->addressCache, person, email );
393                 nodeAddress = g_slist_next( nodeAddress );
394                 ldapServer->entriesRead++;
395         }
396         g_free( fullName );
397         fullName = firstName = lastName = NULL;
398
399 }
400
401 /*
402 * Add all attribute values to a list.
403 */
404 static GSList *syldap_add_list_values( LDAP *ld, LDAPMessage *entry, char *attr ) {
405         GSList *list = NULL;
406         gint i;
407         gchar **vals;
408
409         if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
410                 for( i = 0; vals[i] != NULL; i++ ) {
411                         /* printf( "lv\t%s: %s\n", attr, vals[i] ); */
412                         list = g_slist_append( list, g_strdup( vals[i] ) );
413                 }
414         }
415         ldap_value_free( vals );
416         return list;
417 }
418
419 /*
420 * Add a single attribute value to a list.
421 */
422 static GSList *syldap_add_single_value( LDAP *ld, LDAPMessage *entry, char *attr ) {
423         GSList *list = NULL;
424         gchar **vals;
425
426         if( ( vals = ldap_get_values( ld, entry, attr ) ) != NULL ) {
427                 if( vals[0] != NULL ) {
428                         /* printf( "sv\t%s: %s\n", attr, vals[0] ); */
429                         list = g_slist_append( list, g_strdup( vals[0] ) );
430                 }
431         }
432         ldap_value_free( vals );
433         return list;
434 }
435
436 /*
437 * Free linked lists of character strings.
438 */
439 static void syldap_free_lists( GSList *listName, GSList *listAddr, GSList *listID, GSList *listDN, GSList *listFirst, GSList *listLast ) {
440         mgu_free_list( listName );
441         mgu_free_list( listAddr );
442         mgu_free_list( listID );
443         mgu_free_list( listDN );
444         mgu_free_list( listFirst );
445         mgu_free_list( listLast );
446 }
447
448 /*
449 * Check parameters that are required for a search. This should
450 * be called before performing a search.
451 * Return: TRUE if search criteria appear OK.
452 */
453 gboolean syldap_check_search( SyldapServer *ldapServer ) {
454         g_return_val_if_fail( ldapServer != NULL, FALSE );
455
456         ldapServer->retVal = MGU_LDAP_CRITERIA;
457
458         /* Test search criteria */
459         if( ldapServer->searchCriteria == NULL ) {
460                 return FALSE;
461         }
462         if( strlen( ldapServer->searchCriteria ) < 1 ) {
463                 return FALSE;
464         }
465
466         if( ldapServer->searchValue == NULL ) {
467                 return FALSE;
468         }
469         if( strlen( ldapServer->searchValue ) < 1 ) {
470                 return FALSE;
471         }
472
473         ldapServer->retVal = MGU_SUCCESS;
474         return TRUE;
475 }
476
477 /*
478 * Perform the LDAP search, reading LDAP entries into cache.
479 * Note that one LDAP entry can have multiple values for many of its
480 * attributes. If these attributes are E-Mail addresses; these are
481 * broken out into separate address items. For any other attribute,
482 * only the first occurrence is read.
483 */
484 gint syldap_search( SyldapServer *ldapServer ) {
485         LDAP *ld;
486         LDAPMessage *result, *e;
487         char *attribs[10];
488         char *attribute;
489         gchar *criteria;
490         BerElement *ber;
491         gint rc;
492         GSList *listName = NULL, *listAddress = NULL, *listID = NULL;
493         GSList *listFirst = NULL, *listLast = NULL, *listDN = NULL;
494         struct timeval timeout;
495         gboolean entriesFound = FALSE;
496
497         g_return_val_if_fail( ldapServer != NULL, -1 );
498
499         ldapServer->retVal = MGU_SUCCESS;
500         if( ! syldap_check_search( ldapServer ) ) {
501                 return ldapServer->retVal;
502         }
503
504         /* Set timeout */
505         timeout.tv_sec = ldapServer->timeOut;
506         timeout.tv_usec = 0L;
507
508         ldapServer->entriesRead = 0;
509         if( ( ld = ldap_init( ldapServer->hostName, ldapServer->port ) ) == NULL ) {
510                 ldapServer->retVal = MGU_LDAP_INIT;
511                 return ldapServer->retVal;
512         }
513
514         /* printf( "connected to LDAP host %s on port %d\n", ldapServer->hostName, ldapServer->port ); */
515
516         /* Bind to the server, if required */
517         if( ldapServer->bindDN ) {
518                 if( * ldapServer->bindDN != '\0' ) {
519                         /* printf( "binding...\n" ); */
520                         rc = ldap_simple_bind_s( ld, ldapServer->bindDN, ldapServer->bindPass );
521                         /* printf( "rc=%d\n", rc ); */
522                         if( rc != LDAP_SUCCESS ) {
523                                 /* printf( "LDAP Error: ldap_simple_bind_s: %s\n", ldap_err2string( rc ) ); */
524                                 ldap_unbind( ld );
525                                 ldapServer->retVal = MGU_LDAP_BIND;
526                                 return ldapServer->retVal;
527                         }
528                 }
529         }
530
531         /* Define all attributes we are interested in. */
532         attribs[0] = SYLDAP_ATTR_DN;
533         attribs[1] = SYLDAP_ATTR_COMMONNAME;
534         attribs[2] = SYLDAP_ATTR_GIVENNAME;
535         attribs[3] = SYLDAP_ATTR_SURNAME;
536         attribs[4] = SYLDAP_ATTR_EMAIL;
537         attribs[5] = SYLDAP_ATTR_UID;
538         attribs[6] = NULL;
539
540         /* Create LDAP search string and apply search criteria */
541         criteria = g_strdup_printf( ldapServer->searchCriteria, ldapServer->searchValue );
542         rc = ldap_search_ext_s( ld, ldapServer->baseDN, LDAP_SCOPE_SUBTREE, criteria, attribs, 0, NULL, NULL,
543                        &timeout, 0, &result );
544         g_free( criteria );
545         criteria = NULL;
546         if( rc == LDAP_TIMEOUT ) {
547                 ldap_unbind( ld );
548                 ldapServer->retVal = MGU_LDAP_TIMEOUT;
549                 return ldapServer->retVal;
550         }
551         if( rc != LDAP_SUCCESS ) {
552                 /* printf( "LDAP Error: ldap_search_st: %s\n", ldap_err2string( rc ) ); */
553                 ldap_unbind( ld );
554                 ldapServer->retVal = MGU_LDAP_SEARCH;
555                 return ldapServer->retVal;
556         }
557
558         /* printf( "Total results are: %d\n", ldap_count_entries( ld, result ) ); */
559
560         /* Clear the cache if we have new entries, otherwise leave untouched. */
561         if( ldap_count_entries( ld, result ) > 0 ) {
562                 addrcache_clear( ldapServer->addressCache );
563         }
564
565         /* Process results */
566         ldapServer->entriesRead = 0;
567         for( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
568                 entriesFound = TRUE;
569                 if( ldapServer->entriesRead >= ldapServer->maxEntries ) break;          
570                 /* printf( "DN: %s\n", ldap_get_dn( ld, e ) ); */
571
572                 /* Process all attributes */
573                 for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL;
574                                attribute = ldap_next_attribute( ld, e, ber ) ) {
575                         if( strcasecmp( attribute, SYLDAP_ATTR_COMMONNAME ) == 0 ) {
576                                 listName = syldap_add_list_values( ld, e, attribute );
577                         }
578                         if( strcasecmp( attribute, SYLDAP_ATTR_EMAIL ) == 0 ) {
579                                 listAddress = syldap_add_list_values( ld, e, attribute );
580                         }
581                         if( strcasecmp( attribute, SYLDAP_ATTR_UID ) == 0 ) {
582                                 listID = syldap_add_single_value( ld, e, attribute );
583                         }
584                         if( strcasecmp( attribute, SYLDAP_ATTR_GIVENNAME ) == 0 ) {
585                                 listFirst = syldap_add_list_values( ld, e, attribute );
586                         }
587                         if( strcasecmp( attribute, SYLDAP_ATTR_SURNAME ) == 0 ) {
588                                 listLast = syldap_add_single_value( ld, e, attribute );
589                         }
590                         if( strcasecmp( attribute, SYLDAP_ATTR_DN ) == 0 ) {
591                                 listDN = syldap_add_single_value( ld, e, attribute );
592                         }
593                 }
594
595                 /* Free memory used to store attribute */
596                 ldap_memfree( attribute );
597
598                 /* Format and add items to cache */
599                 syldap_build_items_fl( ldapServer, listAddress, listFirst, listLast );
600
601                 /* Free up */
602                 syldap_free_lists( listName, listAddress, listID, listDN, listFirst, listLast );
603                 listName = listAddress = listID = listFirst = listLast = listDN = NULL;
604
605                 if( ber != NULL ) {
606                         ber_free( ber, 0 );
607                 }
608         }
609
610         syldap_free_lists( listName, listAddress, listID, listDN, listFirst, listLast );
611         listName = listAddress = listID = listFirst = listLast = listDN = NULL;
612         
613         /* Free up and disconnect */
614         ldap_msgfree( result );
615         ldap_unbind( ld );
616         ldapServer->newSearch = FALSE;
617         if( entriesFound ) {
618                 ldapServer->retVal = MGU_SUCCESS;
619         }
620         else {
621                 ldapServer->retVal = MGU_LDAP_NOENTRIES;
622         }
623         return ldapServer->retVal;
624 }
625
626 /* ============================================================================================ */
627 /*
628 * Read data into list. Main entry point
629 * Return: TRUE if file read successfully.
630 */
631 /* ============================================================================================ */
632 gint syldap_read_data( SyldapServer *ldapServer ) {
633         g_return_val_if_fail( ldapServer != NULL, -1 );
634
635         ldapServer->accessFlag = FALSE;
636         pthread_detach( pthread_self() );
637         if( ldapServer->newSearch ) {
638                 /* Read data into the list */
639                 syldap_search( ldapServer );
640
641                 /* Mark cache */
642                 ldapServer->addressCache->modified = FALSE;
643                 ldapServer->addressCache->dataRead = TRUE;
644                 ldapServer->accessFlag = FALSE;
645         }
646
647         /* Callback */
648         ldapServer->busyFlag = FALSE;
649         if( ldapServer->callBack ) {
650                 ( ldapServer->callBack )( ldapServer );
651         }
652         ldapServer->thread = NULL;
653         pthread_exit( NULL );
654         return ldapServer->retVal;
655 }
656
657 /* ============================================================================================ */
658 /*
659 * Cancel read with thread.
660 */
661 /* ============================================================================================ */
662 void syldap_cancel_read( SyldapServer *ldapServer ) {
663         g_return_if_fail( ldapServer != NULL );
664
665         if( ldapServer->thread ) {
666                 /* printf( "thread cancelled\n" ); */
667                 pthread_cancel( *ldapServer->thread );
668         }
669         ldapServer->thread = NULL;
670         ldapServer->busyFlag = FALSE;
671 }
672
673 /* ============================================================================================ */
674 /*
675 * Read data into list using a background thread.
676 * Return: TRUE if file read successfully. Callback function will be
677 * notified when search is complete.
678 */
679 /* ============================================================================================ */
680 gint syldap_read_data_th( SyldapServer *ldapServer ) {
681         pthread_t thread;
682
683         g_return_val_if_fail( ldapServer != NULL, -1 );
684
685         ldapServer->busyFlag = FALSE;
686         syldap_check_search( ldapServer );
687         if( ldapServer->retVal == MGU_SUCCESS ) {
688                 ldapServer->busyFlag = TRUE;
689                 ldapServer->thread = &thread;
690                 pthread_create( ldapServer->thread, NULL, (void *) &syldap_read_data, (void *) ldapServer );
691         }
692         return ldapServer->retVal;
693 }
694
695 /*
696 * Return link list of persons.
697 */
698 GList *syldap_get_list_person( SyldapServer *ldapServer ) {
699         g_return_val_if_fail( ldapServer != NULL, NULL );
700         return addrcache_get_list_person( ldapServer->addressCache );
701 }
702
703 /*
704 * Return link list of folders. This is always NULL since there are
705 * no folders in GnomeCard.
706 * Return: NULL.
707 */
708 GList *syldap_get_list_folder( SyldapServer *ldapServer ) {
709         g_return_val_if_fail( ldapServer != NULL, NULL );
710         return NULL;
711 }
712
713 #define SYLDAP_TEST_FILTER   "(objectclass=*)"
714 #define SYLDAP_SEARCHBASE_V2 "cn=config"
715 #define SYLDAP_SEARCHBASE_V3 ""
716 #define SYLDAP_V2_TEST_ATTR  "database"
717 #define SYLDAP_V3_TEST_ATTR  "namingcontexts"
718
719 /*
720 * Attempt to discover the base DN for the server.
721 * Enter:
722 *       host    Host name
723 *       port    Port number
724 *       bindDN  Bind DN (optional).
725 *       bindPW  Bind PW (optional).
726 *       tov     Timeout value (seconds), or 0 for none, default 30 secs.
727 * Return: List of Base DN's, or NULL if could not read. Base DN should
728 * be g_free() when done.
729 */
730 GList *syldap_read_basedn_s( const gchar *host, const gint port, const gchar *bindDN, const gchar *bindPW, const gint tov ) {
731         GList *baseDN = NULL;
732         LDAP *ld;
733         gint rc, i;
734         LDAPMessage *result, *e;
735         gchar *attribs[10];
736         BerElement *ber;
737         gchar *attribute;
738         gchar **vals;
739         struct timeval timeout;
740
741         if( host == NULL ) return baseDN;
742         if( port < 1 ) return baseDN;
743
744         /* Set timeout */
745         timeout.tv_usec = 0L;
746         if( tov > 0 ) {
747                 timeout.tv_sec = tov;
748         }
749         else {
750                 timeout.tv_sec = 30L;
751         }
752
753         /* Connect to server. */
754         if( ( ld = ldap_init( host, port ) ) == NULL ) {
755                 return baseDN;
756         }
757
758         /* Bind to the server, if required */
759         if( bindDN ) {
760                 if( *bindDN != '\0' ) {
761                         rc = ldap_simple_bind_s( ld, bindDN, bindPW );
762                         if( rc != LDAP_SUCCESS ) {
763                                 /* printf( "LDAP Error: ldap_simple_bind_s: %s\n", ldap_err2string( rc ) ); */
764                                 ldap_unbind( ld );
765                                 return baseDN;
766                         }
767                 }
768         }
769
770         /* Test for LDAP version 3 */
771         attribs[0] = SYLDAP_V3_TEST_ATTR;
772         attribs[1] = NULL;
773         rc = ldap_search_ext_s( ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER, attribs,
774                        0, NULL, NULL, &timeout, 0, &result );
775         if( rc == LDAP_SUCCESS ) {
776                 /* Process entries */
777                 for( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
778                         /* printf( "DN: %s\n", ldap_get_dn( ld, e ) ); */
779
780                         /* Process attributes */
781                         for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL;
782                                         attribute = ldap_next_attribute( ld, e, ber ) ) {
783                                 if( strcasecmp( attribute, SYLDAP_V3_TEST_ATTR ) == 0 ) {
784                                         if( ( vals = ldap_get_values( ld, e, attribute ) ) != NULL ) {
785                                                 for( i = 0; vals[i] != NULL; i++ ) {
786                                                         /* printf( "\t%s: %s\n", attribute, vals[i] ); */
787                                                         baseDN = g_list_append( baseDN, g_strdup( vals[i] ) );
788                                                 }
789                                         }
790                                         ldap_value_free( vals );
791                                 }
792                         }
793                         ldap_memfree( attribute );
794                         if( ber != NULL ) {
795                                 ber_free( ber, 0 );
796                         }
797                 }
798                 ldap_msgfree( result );
799         }
800         else {
801         }
802
803         if( baseDN == NULL ) {
804                 /* Test for LDAP version 2 */
805                 attribs[0] = NULL;
806                 rc = ldap_search_ext_s( ld, SYLDAP_SEARCHBASE_V2, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER, attribs,
807                                0, NULL, NULL, &timeout, 0, &result );
808                 if( rc == LDAP_SUCCESS ) {
809                         /* Process entries */
810                         for( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
811                                 /* if( baseDN ) break;                   */
812                                 /* printf( "DN: %s\n", ldap_get_dn( ld, e ) ); */
813
814                                 /* Process attributes */
815                                 for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL;
816                                                attribute = ldap_next_attribute( ld, e, ber ) ) {
817                                         /* if( baseDN ) break;                   */
818                                         if( strcasecmp( attribute, SYLDAP_V2_TEST_ATTR ) == 0 ) {
819                                                 if( ( vals = ldap_get_values( ld, e, attribute ) ) != NULL ) {
820                                                         for( i = 0; vals[i] != NULL; i++ ) {
821                                                                 char *ch;
822                                                                 /* Strip the 'ldb:' from the front of the value */
823                                                                 ch = ( char * ) strchr( vals[i], ':' );
824                                                                 if( ch ) {
825                                                                         gchar *bn = g_strdup( ++ch );
826                                                                         g_strchomp( bn );
827                                                                         g_strchug( bn );
828                                                                         baseDN = g_list_append( baseDN, g_strdup( bn ) );
829                                                                 }
830                                                         }
831                                                 }
832                                                 ldap_value_free( vals );
833                                         }
834                                 }
835                                 ldap_memfree( attribute );
836                                 if( ber != NULL ) {
837                                         ber_free( ber, 0 );
838                                 }
839                         }
840                         ldap_msgfree( result );
841                 }
842         }
843         ldap_unbind( ld );
844         return baseDN;
845 }
846
847 /*
848 * Attempt to discover the base DN for the server.
849 * Enter:  ldapServer Server to test.
850 * Return: List of Base DN's, or NULL if could not read. Base DN should
851 * be g_free() when done. Return code set in ldapServer.
852 */
853 GList *syldap_read_basedn( SyldapServer *ldapServer ) {
854         GList *baseDN = NULL;
855         LDAP *ld;
856         gint rc, i;
857         LDAPMessage *result, *e;
858         gchar *attribs[10];
859         BerElement *ber;
860         gchar *attribute;
861         gchar **vals;
862         struct timeval timeout;
863
864         ldapServer->retVal = MGU_BAD_ARGS;
865         if( ldapServer == NULL ) return baseDN;
866         if( ldapServer->hostName == NULL ) return baseDN;
867         if( ldapServer->port < 1 ) return baseDN;
868
869         /* Set timeout */
870         timeout.tv_usec = 0L;
871         if( ldapServer->timeOut > 0 ) {
872                 timeout.tv_sec = ldapServer->timeOut;
873         }
874         else {
875                 timeout.tv_sec = 30L;
876         }
877
878         /* Connect to server. */
879         if( ( ld = ldap_init( ldapServer->hostName, ldapServer->port ) ) == NULL ) {
880                 ldapServer->retVal = MGU_LDAP_INIT;
881                 return baseDN;
882         }
883
884         /* Bind to the server, if required */
885         if( ldapServer->bindDN ) {
886                 if( *ldapServer->bindDN != '\0' ) {
887                         rc = ldap_simple_bind_s( ld, ldapServer->bindDN, ldapServer->bindPass );
888                         if( rc != LDAP_SUCCESS ) {
889                                 /* printf( "LDAP Error: ldap_simple_bind_s: %s\n", ldap_err2string( rc ) ); */
890                                 ldap_unbind( ld );
891                                 ldapServer->retVal = MGU_LDAP_BIND;
892                                 return baseDN;
893                         }
894                 }
895         }
896
897         ldapServer->retVal = MGU_LDAP_SEARCH;
898
899         /* Test for LDAP version 3 */
900         attribs[0] = SYLDAP_V3_TEST_ATTR;
901         attribs[1] = NULL;
902         rc = ldap_search_ext_s( ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER, attribs,
903                        0, NULL, NULL, &timeout, 0, &result );
904         if( rc == LDAP_SUCCESS ) {
905                 /* Process entries */
906                 for( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
907                         /* printf( "DN: %s\n", ldap_get_dn( ld, e ) ); */
908
909                         /* Process attributes */
910                         for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL;
911                                         attribute = ldap_next_attribute( ld, e, ber ) ) {
912                                 if( strcasecmp( attribute, SYLDAP_V3_TEST_ATTR ) == 0 ) {
913                                         if( ( vals = ldap_get_values( ld, e, attribute ) ) != NULL ) {
914                                                 for( i = 0; vals[i] != NULL; i++ ) {
915                                                         /* printf( "\t%s: %s\n", attribute, vals[i] ); */
916                                                         baseDN = g_list_append( baseDN, g_strdup( vals[i] ) );
917                                                 }
918                                         }
919                                         ldap_value_free( vals );
920                                 }
921                         }
922                         ldap_memfree( attribute );
923                         if( ber != NULL ) {
924                                 ber_free( ber, 0 );
925                         }
926                 }
927                 ldap_msgfree( result );
928                 ldapServer->retVal = MGU_SUCCESS;
929         }
930         else if( rc == LDAP_TIMEOUT ) {
931                 ldapServer->retVal = MGU_LDAP_TIMEOUT;
932         }
933
934         if( baseDN == NULL ) {
935                 /* Test for LDAP version 2 */
936                 attribs[0] = NULL;
937                 rc = ldap_search_ext_s( ld, SYLDAP_SEARCHBASE_V2, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER, attribs,
938                                0, NULL, NULL, &timeout, 0, &result );
939                 if( rc == LDAP_SUCCESS ) {
940                         /* Process entries */
941                         for( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) {
942                                 /* if( baseDN ) break;                   */
943                                 /* printf( "DN: %s\n", ldap_get_dn( ld, e ) ); */
944
945                                 /* Process attributes */
946                                 for( attribute = ldap_first_attribute( ld, e, &ber ); attribute != NULL;
947                                                attribute = ldap_next_attribute( ld, e, ber ) ) {
948                                         /* if( baseDN ) break;                   */
949                                         if( strcasecmp( attribute, SYLDAP_V2_TEST_ATTR ) == 0 ) {
950                                                 if( ( vals = ldap_get_values( ld, e, attribute ) ) != NULL ) {
951                                                         for( i = 0; vals[i] != NULL; i++ ) {
952                                                                 char *ch;
953                                                                 /* Strip the 'ldb:' from the front of the value */
954                                                                 ch = ( char * ) strchr( vals[i], ':' );
955                                                                 if( ch ) {
956                                                                         gchar *bn = g_strdup( ++ch );
957                                                                         g_strchomp( bn );
958                                                                         g_strchug( bn );
959                                                                         baseDN = g_list_append( baseDN, g_strdup( bn ) );
960                                                                 }
961                                                         }
962                                                 }
963                                                 ldap_value_free( vals );
964                                         }
965                                 }
966                                 ldap_memfree( attribute );
967                                 if( ber != NULL ) {
968                                         ber_free( ber, 0 );
969                                 }
970                         }
971                         ldap_msgfree( result );
972                         ldapServer->retVal = MGU_SUCCESS;
973                 }
974                 else if( rc == LDAP_TIMEOUT ) {
975                         ldapServer->retVal = MGU_LDAP_TIMEOUT;
976                 }
977         }
978         ldap_unbind( ld );
979
980         return baseDN;
981 }
982
983 /*
984 * Attempt to connect to the server.
985 * Enter:
986 *       host    Host name
987 *       port    Port number
988 * Return: TRUE if connected successfully.
989 */
990 gboolean syldap_test_connect_s( const gchar *host, const gint port ) {
991         gboolean retVal = FALSE;
992         LDAP *ld;
993
994         if( host == NULL ) return retVal;
995         if( port < 1 ) return retVal;
996         if( ( ld = ldap_open( host, port ) ) != NULL ) {
997                 retVal = TRUE;
998         }
999         if( ld != NULL ) {
1000                 ldap_unbind( ld );
1001         }
1002         return retVal;
1003 }
1004
1005 /*
1006 * Attempt to connect to the server.
1007 * Enter:  ldapServer Server to test.
1008 * Return: TRUE if connected successfully. Return code set in ldapServer.
1009 */
1010 gboolean syldap_test_connect( SyldapServer *ldapServer ) {
1011         gboolean retVal = FALSE;
1012         LDAP *ld;
1013
1014         ldapServer->retVal = MGU_BAD_ARGS;
1015         if( ldapServer == NULL ) return retVal;
1016         if( ldapServer->hostName == NULL ) return retVal;
1017         if( ldapServer->port < 1 ) return retVal;
1018         ldapServer->retVal = MGU_LDAP_INIT;
1019         if( ( ld = ldap_open( ldapServer->hostName, ldapServer->port ) ) != NULL ) {
1020                 ldapServer->retVal = MGU_SUCCESS;
1021                 retVal = TRUE;
1022         }
1023         if( ld != NULL ) {
1024                 ldap_unbind( ld );
1025         }
1026         return retVal;
1027 }
1028
1029 #define LDAP_LINK_LIB_NAME_1 "libldap.so"
1030 #define LDAP_LINK_LIB_NAME_2 "liblber.so"
1031 #define LDAP_LINK_LIB_NAME_3 "libresolv.so"
1032 #define LDAP_LINK_LIB_NAME_4 "libpthread.so"
1033
1034 /*
1035 * Test whether LDAP libraries installed.
1036 * Return: TRUE if library available.
1037 */
1038 gboolean syldap_test_ldap_lib() {
1039         void *handle, *fun;
1040         
1041         /* Get library */
1042         handle = dlopen( LDAP_LINK_LIB_NAME_1, RTLD_LAZY );
1043         if( ! handle ) {
1044                 return FALSE;
1045         }
1046
1047         /* Test for symbols we need */
1048         fun = dlsym( handle, "ldap_init" );
1049         if( ! fun ) {
1050                 dlclose( handle );
1051                 return FALSE;
1052         }
1053         dlclose( handle ); handle = NULL; fun = NULL;
1054
1055         handle = dlopen( LDAP_LINK_LIB_NAME_2, RTLD_LAZY );
1056         if( ! handle ) {
1057                 return FALSE;
1058         }
1059         fun = dlsym( handle, "ber_init" );
1060         if( ! fun ) {
1061                 dlclose( handle );
1062                 return FALSE;
1063         }
1064         dlclose( handle ); handle = NULL; fun = NULL;
1065
1066         handle = dlopen( LDAP_LINK_LIB_NAME_3, RTLD_LAZY );
1067         if( ! handle ) {
1068                 return FALSE;
1069         }
1070         fun = dlsym( handle, "res_query" );
1071         if( ! fun ) {
1072                 dlclose( handle );
1073                 return FALSE;
1074         }
1075         dlclose( handle ); handle = NULL; fun = NULL;
1076
1077         handle = dlopen( LDAP_LINK_LIB_NAME_4, RTLD_LAZY );
1078         if( ! handle ) {
1079                 return FALSE;
1080         }
1081         fun = dlsym( handle, "pthread_create" );
1082         if( ! fun ) {
1083                 dlclose( handle );
1084                 return FALSE;
1085         }
1086         dlclose( handle ); handle = NULL; fun = NULL;
1087
1088         return TRUE;
1089 }
1090
1091 #endif  /* USE_LDAP */
1092
1093 /*
1094 * End of Source.
1095 */