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