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