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