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