2007-08-19 [colin] 2.10.0cvs133
[claws.git] / src / ldapctrl.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003-2007 Match Grun and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 /*
21  * Functions for LDAP control data.
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
34 #include "ldapctrl.h"
35 #include "mgutils.h"
36 #include "editaddress_other_attributes_ldap.h"
37
38 /**
39  * Create new LDAP control block object.
40  * \return Initialized control object.
41  */
42 LdapControl *ldapctl_create( void ) {
43         LdapControl *ctl;
44
45         ctl = g_new0( LdapControl, 1 );
46         ctl->hostName = NULL;
47         ctl->port = LDAPCTL_DFL_PORT;
48         ctl->baseDN = NULL;
49         ctl->bindDN = NULL;
50         ctl->bindPass = NULL;
51         ctl->listCriteria = NULL;
52         ctl->attribEMail = g_strdup( LDAPCTL_ATTR_EMAIL );
53         ctl->attribCName = g_strdup( LDAPCTL_ATTR_COMMONNAME );
54         ctl->attribFName = g_strdup( LDAPCTL_ATTR_GIVENNAME );
55         ctl->attribLName = g_strdup( LDAPCTL_ATTR_SURNAME );
56         ctl->attribDName = g_strdup( LDAPCTL_ATTR_DISPLAYNAME );
57         ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
58         ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
59         ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
60         ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
61         ctl->version = 0;
62         ctl->enableTLS = FALSE;
63         ctl->enableSSL = FALSE;
64
65         /* Mutex to protect control block */
66         ctl->mutexCtl = g_malloc0( sizeof( pthread_mutex_t ) );
67         pthread_mutex_init( ctl->mutexCtl, NULL );
68
69         return ctl;
70 }
71
72 /**
73  * Specify hostname to be used.
74  * \param ctl   Control object to process.
75  * \param value Host name.
76  */
77 void ldapctl_set_host( LdapControl* ctl, const gchar *value ) {
78         ctl->hostName = mgu_replace_string( ctl->hostName, value );
79         g_strstrip( ctl->hostName );
80 }
81
82 /**
83  * Specify port to be used.
84  * \param ctl  Control object to process.
85  * \param value Port.
86  */
87 void ldapctl_set_port( LdapControl* ctl, const gint value ) {
88         if( value > 0 ) {
89                 ctl->port = value;
90         }
91         else {
92                 ctl->port = LDAPCTL_DFL_PORT;
93         }
94 }
95
96 /**
97  * Specify base DN to be used.
98  * \param ctl  Control object to process.
99  * \param value Base DN.
100  */
101 void ldapctl_set_base_dn( LdapControl* ctl, const gchar *value ) {
102         ctl->baseDN = mgu_replace_string( ctl->baseDN, value );
103         g_strstrip( ctl->baseDN );
104 }
105
106 /**
107  * Specify bind DN to be used.
108  * \param ctl  Control object to process.
109  * \param value Bind DN.
110  */
111 void ldapctl_set_bind_dn( LdapControl* ctl, const gchar *value ) {
112         ctl->bindDN = mgu_replace_string( ctl->bindDN, value );
113         g_strstrip( ctl->bindDN );
114 }
115
116 /**
117  * Specify bind password to be used.
118  * \param ctl  Control object to process.
119  * \param value Password.
120  */
121 void ldapctl_set_bind_password( LdapControl* ctl, const gchar *value ) {
122         ctl->bindPass = mgu_replace_string( ctl->bindPass, value );
123         g_strstrip( ctl->bindPass );
124 }
125
126 /**
127  * Specify maximum number of entries to retrieve.
128  * \param ctl  Control object to process.
129  * \param value Maximum entries.
130  */
131 void ldapctl_set_max_entries( LdapControl* ctl, const gint value ) {
132         if( value > 0 ) {
133                 ctl->maxEntries = value;
134         }
135         else {
136                 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
137         }
138 }
139
140 /**
141  * Specify timeout value for LDAP operation (in seconds).
142  * \param ctl  Control object to process.
143  * \param value Timeout.
144  */
145 void ldapctl_set_timeout( LdapControl* ctl, const gint value ) {
146         if( value > 0 ) {
147                 ctl->timeOut = value;
148         }
149         else {
150                 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
151         }
152 }
153
154 /**
155  * Specify maximum age of query (in seconds) before query is retired.
156  * \param ctl  Control object to process.
157  * \param value Maximum age.
158  */
159 void ldapctl_set_max_query_age( LdapControl* ctl, const gint value ) {
160         if( value > LDAPCTL_MAX_QUERY_AGE ) {
161                 ctl->maxQueryAge = LDAPCTL_MAX_QUERY_AGE;
162         }
163         else if( value < 1 ) {
164                 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
165         }
166         else {
167                 ctl->maxQueryAge = value;
168         }
169 }
170
171 /**
172  * Specify matching option to be used for searches.
173  * \param ctl   Control object to process.
174  * \param value Matching option, as follows:
175  * <ul>
176  * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
177  * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
178  * </ul>
179  */
180 void ldapctl_set_matching_option( LdapControl* ctl, const gint value ) {
181         if( value < LDAPCTL_MATCH_BEGINWITH ) {
182                 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
183         }
184         else if( value > LDAPCTL_MATCH_CONTAINS ) {
185                 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
186         }
187         else {
188                 ctl->matchingOption = value;
189         }
190 }
191
192 /**
193  * Specify TLS option.
194  * \param ctl   Control object to process.
195  * \param value <i>TRUE</i> to enable TLS.
196  */
197 void ldapctl_set_tls( LdapControl* ctl, const gboolean value ) {
198         ctl->enableTLS = value;
199 }
200
201 void ldapctl_set_ssl( LdapControl* ctl, const gboolean value ) {
202         ctl->enableSSL = value;
203 }
204
205 /**
206  * Return search criteria list.
207  * \param  ctl  Control data object.
208  * \return Linked list of character strings containing LDAP attribute names to
209  *         use for a search. This should not be modified directly. Use the
210  *         <code>ldapctl_set_criteria_list()</code>,
211  *         <code>ldapctl_criteria_list_clear()</code> and
212  *         <code>ldapctl_criteria_list_add()</code> functions for this purpose.
213  */
214 GList *ldapctl_get_criteria_list( const LdapControl* ctl ) {
215         g_return_val_if_fail( ctl != NULL, NULL );
216         return ctl->listCriteria;
217 }
218
219 /**
220  * Clear list of LDAP search attributes.
221  * \param  ctl  Control data object.
222  */
223 void ldapctl_criteria_list_clear( LdapControl *ctl ) {
224         g_return_if_fail( ctl != NULL );
225         mgu_free_dlist( ctl->listCriteria );
226         ctl->listCriteria = NULL;
227 }
228
229 /**
230  * Add LDAP attribute to criteria list.
231  * \param ctl  Control object to process.
232  * \param attr Attribute name to append. If not NULL and unique, a copy will
233  *             be appended to the list.
234  */
235 void ldapctl_criteria_list_add( LdapControl *ctl, gchar *attr ) {
236         g_return_if_fail( ctl != NULL );
237         if( attr != NULL ) {
238                 if( mgu_list_test_unq_nc( ctl->listCriteria, attr ) ) {
239                         ctl->listCriteria = g_list_append(
240                                 ctl->listCriteria, g_strdup( attr ) );
241                 }
242         }
243 }
244
245 /**
246  * Clear LDAP server member variables.
247  * \param ctl Control object to clear.
248  */
249 static void ldapctl_clear( LdapControl *ctl ) {
250         g_return_if_fail( ctl != NULL );
251
252         /* Free internal stuff */
253         g_free( ctl->hostName );
254         g_free( ctl->baseDN );
255         g_free( ctl->bindDN );
256         g_free( ctl->bindPass );
257         g_free( ctl->attribEMail );
258         g_free( ctl->attribCName );
259         g_free( ctl->attribFName );
260         g_free( ctl->attribLName );
261         g_free( ctl->attribDName );
262
263         ldapctl_criteria_list_clear( ctl );
264
265         /* Clear pointers */
266         ctl->hostName = NULL;
267         ctl->port = 0;
268         ctl->baseDN = NULL;
269         ctl->bindDN = NULL;
270         ctl->bindPass = NULL;
271         ctl->attribEMail = NULL;
272         ctl->attribCName = NULL;
273         ctl->attribFName = NULL;
274         ctl->attribLName = NULL;
275         ctl->attribDName = NULL;
276         ctl->maxEntries = 0;
277         ctl->timeOut = 0;
278         ctl->maxQueryAge = 0;
279         ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
280         ctl->version = 0;
281         ctl->enableTLS = FALSE;
282         ctl->enableSSL = FALSE;
283 }
284
285 /**
286  * Free up LDAP server interface object by releasing internal memory.
287  * \param ctl Control object to free.
288  */
289 void ldapctl_free( LdapControl *ctl ) {
290         g_return_if_fail( ctl != NULL );
291
292         /* Free internal stuff */
293         ldapctl_clear( ctl );
294
295         /* Free the mutex */
296         pthread_mutex_destroy( ctl->mutexCtl );
297         g_free( ctl->mutexCtl );
298         ctl->mutexCtl = NULL;
299
300         /* Now release LDAP control object */
301         g_free( ctl );
302 }
303
304 /**
305  * Display object to specified stream.
306  * \param ctl    Control object to process.
307  * \param stream Output stream.
308  */
309 void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
310         g_return_if_fail( ctl != NULL );
311
312         pthread_mutex_lock( ctl->mutexCtl );
313         fprintf( stream, "LdapControl:\n" );
314         fprintf( stream, "host name: '%s'\n", ctl->hostName );
315         fprintf( stream, "     port: %d\n",   ctl->port );
316         fprintf( stream, "  base dn: '%s'\n", ctl->baseDN );
317         fprintf( stream, "  bind dn: '%s'\n", ctl->bindDN );
318         fprintf( stream, "bind pass: '%s'\n", ctl->bindPass );
319         fprintf( stream, "attr mail: '%s'\n", ctl->attribEMail );
320         fprintf( stream, "attr comn: '%s'\n", ctl->attribCName );
321         fprintf( stream, "attr frst: '%s'\n", ctl->attribFName );
322         fprintf( stream, "attr last: '%s'\n", ctl->attribLName );
323         fprintf( stream, "attr disn: '%s'\n", ctl->attribDName );
324         fprintf( stream, "max entry: %d\n",   ctl->maxEntries );
325         fprintf( stream, "  timeout: %d\n",   ctl->timeOut );
326         fprintf( stream, "  max age: %d\n",   ctl->maxQueryAge );
327         fprintf( stream, "match opt: %d\n",   ctl->matchingOption );
328         fprintf( stream, "  version: %d\n",   ctl->version );
329         fprintf( stream, "      TLS: %s\n",   ctl->enableTLS ? "yes" : "no" );
330         fprintf( stream, "      SSL: %s\n",   ctl->enableSSL ? "yes" : "no" );
331         fprintf( stream, "crit list:\n" );
332         if( ctl->listCriteria ) {
333                 mgu_print_dlist( ctl->listCriteria, stream );
334         }
335         else {
336                 fprintf( stream, "\t!!!none!!!\n" );
337         }
338         pthread_mutex_unlock( ctl->mutexCtl );
339 }
340
341 /**
342  * Copy member variables to specified object. Mutex lock object is
343  * not copied.
344  * \param ctlFrom Object to copy from.
345  * \param ctlTo   Destination object.
346  */
347 void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
348         GList *node;
349
350         g_return_if_fail( ctlFrom != NULL );
351         g_return_if_fail( ctlTo != NULL );
352
353         /* Lock both objects */
354         pthread_mutex_lock( ctlFrom->mutexCtl );
355         pthread_mutex_lock( ctlTo->mutexCtl );
356
357         /* Clear our destination */
358         ldapctl_clear( ctlTo );
359
360         /* Copy strings */
361         ctlTo->hostName = g_strdup( ctlFrom->hostName );
362         ctlTo->baseDN = g_strdup( ctlFrom->baseDN );
363         ctlTo->bindDN = g_strdup( ctlFrom->bindDN );
364         ctlTo->bindPass = g_strdup( ctlFrom->bindPass );
365         ctlTo->attribEMail = g_strdup( ctlFrom->attribEMail );
366         ctlTo->attribCName = g_strdup( ctlFrom->attribCName );
367         ctlTo->attribFName = g_strdup( ctlFrom->attribFName );
368         ctlTo->attribLName = g_strdup( ctlFrom->attribLName );
369         ctlTo->attribDName = g_strdup( ctlFrom->attribDName );
370
371         /* Copy search criteria */
372         node = ctlFrom->listCriteria;
373         while( node ) {
374                 ctlTo->listCriteria = g_list_append(
375                         ctlTo->listCriteria, g_strdup( node->data ) );
376                 node = g_list_next( node );
377         }
378
379         /* Copy other members */
380         ctlTo->port = ctlFrom->port;
381         ctlTo->maxEntries = ctlFrom->maxEntries;
382         ctlTo->timeOut = ctlFrom->timeOut;
383         ctlTo->maxQueryAge = ctlFrom->maxQueryAge;
384         ctlTo->matchingOption = ctlFrom->matchingOption;
385         ctlTo->version = ctlFrom->version;
386         ctlTo->enableTLS = ctlFrom->enableTLS;
387         ctlTo->enableSSL = ctlFrom->enableSSL;
388
389         /* Unlock */
390         pthread_mutex_unlock( ctlTo->mutexCtl );
391         pthread_mutex_unlock( ctlFrom->mutexCtl );
392 }
393
394 /**
395  * Search criteria fragment - two terms - begin with (default).
396  */
397 static gchar *_criteria2BeginWith = "(&(givenName=%s*)(sn=%s*))";
398
399 /**
400  * Search criteria fragment - two terms - contains.
401  */
402 static gchar *_criteria2Contains  = "(&(givenName=*%s*)(sn=*%s*))";
403
404 /**
405  * Create an LDAP search criteria by parsing specified search term. The search
406  * term may contain two names separated by the first embedded space found in
407  * the search term. It is assumed that the two tokens are first name and last
408  * name, or vice versa. An appropriate search criteria will be constructed.
409  *
410  * \param  searchTerm   Reference to search term to process.
411  * \param  matchOption  Set to the following:
412  * <ul>
413  * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
414  * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
415  * </ul>
416  *
417  * \return Formatted search criteria, or <code>NULL</code> if there is no
418  *         embedded spaces. The search term should be g_free() when no
419  *         longer required.
420  */
421 static gchar *ldapctl_build_ldap_criteria(
422                 const gchar *searchTerm, const gint matchOption )
423 {
424         gchar *p;
425         gchar *t1;
426         gchar *t2 = NULL;
427         gchar *term;
428         gchar *crit = NULL;
429         gchar *criteriaFmt;
430
431         if( matchOption == LDAPCTL_MATCH_CONTAINS ) {
432                 criteriaFmt = _criteria2Contains;
433         }
434         else {
435                 criteriaFmt = _criteria2BeginWith;
436         }
437
438         term = g_strdup( searchTerm );
439         g_strstrip( term );
440
441         /* Find first space character */        
442         t1 = p = term;
443         while( *p ) {
444                 if( *p == ' ' ) {
445                         *p = '\0';
446                         t2 = g_strdup( 1 + p );
447                         break;
448                 }
449                 p++;
450         }
451
452         if( t2 ) {
453                 /* Format search criteria */
454                 gchar *p1, *p2;
455
456                 g_strstrip( t2 );
457                 p1 = g_strdup_printf( criteriaFmt, t1, t2 );
458                 p2 = g_strdup_printf( criteriaFmt, t2, t1 );
459                 crit = g_strdup_printf( "(&(|%s%s)(mail=*))", p1, p2 );
460
461                 g_free( t2 );
462                 g_free( p1 );
463                 g_free( p2 );
464         }
465         g_free( term );
466         return crit;
467 }
468
469
470 /**
471  * Search criteria fragment - single term - begin with (default).
472  */
473 static gchar *_criteriaBeginWith = "(%s=%s*)";
474
475 /**
476  * Search criteria fragment - single term - contains.
477  */
478 static gchar *_criteriaContains  = "(%s=*%s*)";
479
480 /**
481  * Build a formatted LDAP search criteria string from criteria list.
482  * \param ctl  Control object to process.
483  * \param searchVal Value to search for.
484  * \return Formatted string. Should be g_free() when done.
485  */
486 gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
487         GList *node;
488         gchar *p1, *p2, *retVal;
489         gchar *criteriaFmt;
490
491         g_return_val_if_fail( ctl != NULL, NULL );
492         g_return_val_if_fail( searchVal != NULL, NULL );
493
494         /* Test whether there are more that one search terms */
495         retVal = ldapctl_build_ldap_criteria( searchVal, ctl->matchingOption );
496         if( retVal ) return retVal;
497
498         if( ctl->matchingOption ==  LDAPCTL_MATCH_CONTAINS ) {
499                 criteriaFmt = _criteriaContains;
500         }
501         else {
502                 criteriaFmt = _criteriaBeginWith;
503         }
504
505         /* No - just a simple search */
506         /* p1 contains previous formatted criteria */
507         /* p2 contains next formatted criteria */
508         retVal = p1 = p2 = NULL;
509         node = ctl->listCriteria;
510         while( node ) {
511                 gchar *attr, *tmp;
512
513                 attr = node->data;
514                 node = g_list_next( node );
515
516                 /* Switch pointers */
517                 tmp = p1; p1 = p2; p2 = tmp;
518
519                 if( p1 ) {
520                         /* Subsequent time through */
521                         gchar *crit;
522
523                         /* Format query criteria */
524                         crit = g_strdup_printf( criteriaFmt, attr, searchVal );
525
526                         /* Append to existing criteria */                       
527                         g_free( p2 );
528                         p2 = g_strdup_printf( "(|%s%s)", p1, crit );
529
530                         g_free( crit );
531                 }
532                 else {
533                         /* First time through - Format query criteria */
534                         p2 = g_strdup_printf( criteriaFmt, attr, searchVal );
535                 }
536         }
537
538         if( p2 == NULL ) {
539                 /* Nothing processed - format a default attribute */
540                 retVal = g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL );
541         }
542         else {
543                 /* We have something - free up previous result */
544                 retVal = p2;
545                 g_free( p1 );
546         }
547         return retVal;
548 }
549
550 /**
551  * Return array of pointers to attributes for LDAP query.
552  * \param  ctl  Control object to process.
553  * \return NULL terminated list.
554  */
555 char **ldapctl_attribute_array( LdapControl *ctl ) {
556         char **ptrArray;
557         GList *node, *def;
558         gint cnt, i;
559         g_return_val_if_fail( ctl != NULL, NULL );
560
561         def = ldapctl_get_default_criteria_list();
562         /* check if this servers config is updated to the new
563          * default list of search criteria. If not update the list */
564         if (! ldapctl_compare_list(ctl->listCriteria, def)) {
565                 /* Deep copy search criteria */
566                 ldapctl_criteria_list_clear(ctl);
567                 while(def) {
568                         ctl->listCriteria = g_list_append(
569                                 ctl->listCriteria, g_strdup(def->data));
570                         def = g_list_next(def);
571                 }
572         }
573         node = ctl->listCriteria;
574         cnt = g_list_length( ctl->listCriteria );
575         ptrArray = g_new0( char *, 1 + cnt );
576         i = 0;
577         while( node ) {
578                 ptrArray[ i++ ] = node->data;
579                 node = g_list_next( node );
580         }
581         ptrArray[ i ] = NULL;
582         return ptrArray;
583 }
584
585 /**
586  * Free array of pointers allocated by ldapctl_criteria_array().
587  * param ptrArray Array to clear.
588  */
589 void ldapctl_free_attribute_array( char **ptrArray ) {
590         gint i;
591
592         /* Clear array to NULL's */
593         for( i = 0; ptrArray[i] != NULL; i++ ) {
594                 ptrArray[i] = NULL;
595         }
596         g_free( ptrArray );
597 }       
598
599 /**
600  * Parse LDAP search string, building list of LDAP criteria attributes. This
601  * may be used to convert an old style Sylpheed LDAP search criteria to the
602  * new format. The old style uses a standard LDAP search string, for example:
603  * <pre>
604  *    (&(mail=*)(cn=%s*))
605  * </pre>
606  * This function extracts the two LDAP attributes <code>mail</code> and
607  * <code>cn</code>, adding each to a list.
608  *
609  * \param ctl Control object to process.
610  * \param criteria LDAP search criteria string.
611  */
612 void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
613         gchar *ptr;
614         gchar *pFrom;
615         gchar *attrib;
616         gint iLen;
617
618         g_return_if_fail( ctl != NULL );
619
620         ldapctl_criteria_list_clear( ctl );
621         if( criteria == NULL ) return;
622
623         pFrom = NULL;
624         ptr = criteria;
625         while( *ptr ) {
626                 if( *ptr == '(' ) {
627                         pFrom = 1 + ptr;
628                 }
629                 if( *ptr == '=' ) {
630                         if( pFrom ) {
631                                 iLen = ptr - pFrom;
632                                 attrib = g_strndup( pFrom, iLen );
633                                 g_strstrip( attrib );
634                                 ldapctl_criteria_list_add( ctl, attrib );
635                                 g_free( attrib );
636                         }
637                         pFrom = NULL;
638                 }
639                 ptr++;
640         }
641 }
642
643 /**
644  * Return the default LDAP search criteria string.
645  * \return Formatted string or <i>""</i>. Should be g_free() when done.
646  */
647 gchar *ldapctl_get_default_criteria() {
648         gchar *retVal = LDAPCTL_DFL_ATTR_LIST;
649         const gchar **attrs = ATTRIBUTE; 
650
651         while (*attrs) {
652                 retVal = g_strdup_printf("%s, %s", retVal, *attrs++);
653         }
654         return retVal;
655 }
656
657 /**
658  * Return the default LDAP search criteria list.
659  * \return GList or <i>NULL</i>.
660  */
661 GList *ldapctl_get_default_criteria_list() {
662         gchar *criteria, *item;
663         gchar **c_list, **w_list;
664         GList *attr_list = NULL;
665
666         criteria = ldapctl_get_default_criteria();
667         c_list = g_strsplit(criteria, " ", 0);
668         g_free(criteria);
669         criteria = NULL;
670         w_list = c_list;
671         while ((criteria = *w_list++) != 0) {
672                 /* copy string elimination <,> */
673                 if (*w_list)
674                         item = g_strndup(criteria, strlen(criteria) - 1);
675                 else
676                         item = g_strdup(criteria);
677                 attr_list = g_list_append(attr_list, g_strdup(item));
678                 g_free(item);
679         }
680         g_strfreev(c_list);
681         return attr_list;
682 }
683
684 /**
685  * Compare to GList for equality.
686  * \param l1 First GList
687  * \param l2 Second GList
688  * \Return TRUE or FALSE
689  */
690 gboolean ldapctl_compare_list(GList *l1, GList *l2) {
691         gchar *first, *second;
692         if (! l1 && ! l2)
693                 return TRUE;
694         if ((! l1 && l2) || (l1 && ! l2))
695                 return FALSE;
696         while (l1 && l2) {
697                 first = (gchar *) l1->data;
698                 second = (gchar *) l2->data;
699                 if ( ! (first && second) || strcmp(first, second) != 0) {
700                         return FALSE;
701                 }
702                 l1 = g_list_next(l1);
703                 l2 = g_list_next(l2);
704         }
705         return TRUE;
706 }
707
708 #endif  /* USE_LDAP */
709
710 /*
711  * End of Source.
712  */
713