Do not put enchant's CFLAGS into main CFLAGS make variable.
[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         mgu_free_dlist( ctl->listCriteria );
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( mgu_list_test_unq_nc( ctl->listCriteria, attr ) ) {
258                         debug_print("adding to criteria list: %s\n", attr);
259                         ctl->listCriteria = g_list_append(
260                                 ctl->listCriteria, g_strdup( attr ) );
261                 }
262         }
263 }
264
265 /**
266  * Clear LDAP server member variables.
267  * \param ctl Control object to clear.
268  */
269 static void ldapctl_clear( LdapControl *ctl ) {
270         cm_return_if_fail( ctl != NULL );
271
272         debug_print("clearing ldap controller members\n");
273         /* Free internal stuff */
274         g_free( ctl->hostName );
275         g_free( ctl->baseDN );
276         g_free( ctl->bindDN );
277         g_free( ctl->attribEMail );
278         g_free( ctl->attribCName );
279         g_free( ctl->attribFName );
280         g_free( ctl->attribLName );
281         g_free( ctl->attribDName );
282
283         ldapctl_criteria_list_clear( ctl );
284
285         /* Clear pointers */
286         ctl->hostName = NULL;
287         ctl->port = 0;
288         ctl->baseDN = NULL;
289         ctl->bindDN = NULL;
290         ctl->attribEMail = NULL;
291         ctl->attribCName = NULL;
292         ctl->attribFName = NULL;
293         ctl->attribLName = NULL;
294         ctl->attribDName = NULL;
295         ctl->maxEntries = 0;
296         ctl->timeOut = 0;
297         ctl->maxQueryAge = 0;
298         ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
299         ctl->version = 0;
300         ctl->enableTLS = FALSE;
301         ctl->enableSSL = FALSE;
302 }
303
304 /**
305  * Free up LDAP server interface object by releasing internal memory.
306  * \param ctl Control object to free.
307  */
308 void ldapctl_free( LdapControl *ctl ) {
309         cm_return_if_fail( ctl != NULL );
310
311         debug_print("releasing requested memory for ldap controller\n");
312         /* Free internal stuff */
313         ldapctl_clear( ctl );
314
315         /* Free the mutex */
316         pthread_mutex_destroy( ctl->mutexCtl );
317         g_free( ctl->mutexCtl );
318         ctl->mutexCtl = NULL;
319
320         /* Now release LDAP control object */
321         g_free( ctl );
322 }
323
324 /**
325  * Display object to specified stream.
326  * \param ctl    Control object to process.
327  * \param stream Output stream.
328  */
329 void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
330         cm_return_if_fail( ctl != NULL );
331         gchar *pwd;
332
333         pthread_mutex_lock( ctl->mutexCtl );
334         fprintf( stream, "LdapControl:\n" );
335         fprintf( stream, "host name: '%s'\n", ctl->hostName?ctl->hostName:"null" );
336         fprintf( stream, "     port: %d\n",   ctl->port );
337         fprintf( stream, "  base dn: '%s'\n", ctl->baseDN?ctl->baseDN:"null" );
338         fprintf( stream, "  bind dn: '%s'\n", ctl->bindDN?ctl->bindDN:"null" );
339         pwd = passwd_store_get(PWS_CORE, "LDAP", ctl->hostName);
340         fprintf( stream, "bind pass: '%s'\n", pwd?pwd:"null" );
341         if (pwd != NULL && strlen(pwd) > 0)
342                 memset(pwd, 0, strlen(pwd));
343         g_free(pwd);
344         fprintf( stream, "attr mail: '%s'\n", ctl->attribEMail?ctl->attribEMail:"null" );
345         fprintf( stream, "attr comn: '%s'\n", ctl->attribCName?ctl->attribCName:"null" );
346         fprintf( stream, "attr frst: '%s'\n", ctl->attribFName?ctl->attribFName:"null" );
347         fprintf( stream, "attr last: '%s'\n", ctl->attribLName?ctl->attribLName:"null" );
348         fprintf( stream, "attr disn: '%s'\n", ctl->attribDName?ctl->attribDName:"null" );
349         fprintf( stream, "max entry: %d\n",   ctl->maxEntries );
350         fprintf( stream, "  timeout: %d\n",   ctl->timeOut );
351         fprintf( stream, "  max age: %d\n",   ctl->maxQueryAge );
352         fprintf( stream, "match opt: %d\n",   ctl->matchingOption );
353         fprintf( stream, "  version: %d\n",   ctl->version );
354         fprintf( stream, " STARTTLS: %s\n",   ctl->enableTLS ? "yes" : "no" );
355         fprintf( stream, "  SSL/TLS: %s\n",   ctl->enableSSL ? "yes" : "no" );
356         fprintf( stream, "crit list:\n" );
357         if( ctl->listCriteria ) {
358                 mgu_print_dlist( ctl->listCriteria, stream );
359         }
360         else {
361                 fprintf( stream, "\t!!!none!!!\n" );
362         }
363         pthread_mutex_unlock( ctl->mutexCtl );
364 }
365
366 /**
367  * Copy member variables to specified object. Mutex lock object is
368  * not copied.
369  * \param ctlFrom Object to copy from.
370  * \param ctlTo   Destination object.
371  */
372 void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
373         GList *node;
374
375         cm_return_if_fail( ctlFrom != NULL );
376         cm_return_if_fail( ctlTo != NULL );
377
378         debug_print("ldap controller copy\n");
379         /* Lock both objects */
380         pthread_mutex_lock( ctlFrom->mutexCtl );
381         pthread_mutex_lock( ctlTo->mutexCtl );
382
383         /* Clear our destination */
384         ldapctl_clear( ctlTo );
385
386         /* Copy strings */
387         ctlTo->hostName = g_strdup( ctlFrom->hostName );
388         ctlTo->baseDN = g_strdup( ctlFrom->baseDN );
389         ctlTo->bindDN = g_strdup( ctlFrom->bindDN );
390         ctlTo->attribEMail = g_strdup( ctlFrom->attribEMail );
391         ctlTo->attribCName = g_strdup( ctlFrom->attribCName );
392         ctlTo->attribFName = g_strdup( ctlFrom->attribFName );
393         ctlTo->attribLName = g_strdup( ctlFrom->attribLName );
394         ctlTo->attribDName = g_strdup( ctlFrom->attribDName );
395
396         /* Copy search criteria */
397         node = ctlFrom->listCriteria;
398         while( node ) {
399                 ctlTo->listCriteria = g_list_append(
400                         ctlTo->listCriteria, g_strdup( node->data ) );
401                 node = g_list_next( node );
402         }
403
404         /* Copy other members */
405         ctlTo->port = ctlFrom->port;
406         ctlTo->maxEntries = ctlFrom->maxEntries;
407         ctlTo->timeOut = ctlFrom->timeOut;
408         ctlTo->maxQueryAge = ctlFrom->maxQueryAge;
409         ctlTo->matchingOption = ctlFrom->matchingOption;
410         ctlTo->version = ctlFrom->version;
411         ctlTo->enableTLS = ctlFrom->enableTLS;
412         ctlTo->enableSSL = ctlFrom->enableSSL;
413
414         /* Unlock */
415         pthread_mutex_unlock( ctlTo->mutexCtl );
416         pthread_mutex_unlock( ctlFrom->mutexCtl );
417 }
418
419 /**
420  * Search criteria fragment - two terms - begin with (default).
421  */
422 static gchar *_criteria2BeginWith = "(&(givenName=%s*)(sn=%s*))";
423
424 /**
425  * Search criteria fragment - two terms - contains.
426  */
427 static gchar *_criteria2Contains  = "(&(givenName=*%s*)(sn=*%s*))";
428
429 /**
430  * Create an LDAP search criteria by parsing specified search term. The search
431  * term may contain two names separated by the first embedded space found in
432  * the search term. It is assumed that the two tokens are first name and last
433  * name, or vice versa. An appropriate search criteria will be constructed.
434  *
435  * \param  searchTerm   Reference to search term to process.
436  * \param  matchOption  Set to the following:
437  * <ul>
438  * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
439  * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
440  * </ul>
441  *
442  * \return Formatted search criteria, or <code>NULL</code> if there is no
443  *         embedded spaces. The search term should be g_free() when no
444  *         longer required.
445  */
446 static gchar *ldapctl_build_ldap_criteria(
447                 const gchar *searchTerm, const gint matchOption )
448 {
449         gchar *p;
450         gchar *t1;
451         gchar *t2 = NULL;
452         gchar *term;
453         gchar *crit = NULL;
454         gchar *criteriaFmt;
455
456         if( matchOption == LDAPCTL_MATCH_CONTAINS ) {
457                 criteriaFmt = _criteria2Contains;
458         }
459         else {
460                 criteriaFmt = _criteria2BeginWith;
461         }
462
463         term = g_strdup( searchTerm );
464         g_strstrip( term );
465
466         /* Find first space character */        
467         t1 = p = term;
468         while( *p ) {
469                 if( *p == ' ' ) {
470                         *p = '\0';
471                         t2 = g_strdup( 1 + p );
472                         break;
473                 }
474                 p++;
475         }
476
477         if( t2 ) {
478                 /* Format search criteria */
479                 gchar *p1, *p2;
480
481                 g_strstrip( t2 );
482                 p1 = g_strdup_printf( criteriaFmt, t1, t2 );
483                 p2 = g_strdup_printf( criteriaFmt, t2, t1 );
484                 crit = g_strdup_printf( "(&(|%s%s)(mail=*))", p1, p2 );
485
486                 g_free( t2 );
487                 g_free( p1 );
488                 g_free( p2 );
489         }
490         g_free( term );
491         debug_print("search criteria: %s\n", crit?crit:"null");
492         return crit;
493 }
494
495
496 /**
497  * Search criteria fragment - single term - begin with (default).
498  */
499 static gchar *_criteriaBeginWith = "(%s=%s*)";
500
501 /**
502  * Search criteria fragment - single term - contains.
503  */
504 static gchar *_criteriaContains  = "(%s=*%s*)";
505
506 /**
507  * Build a formatted LDAP search criteria string from criteria list.
508  * \param ctl  Control object to process.
509  * \param searchVal Value to search for.
510  * \return Formatted string. Should be g_free() when done.
511  */
512 gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
513         GList *node;
514         gchar *p1, *p2, *retVal;
515         gchar *criteriaFmt;
516
517         cm_return_val_if_fail( ctl != NULL, NULL );
518         cm_return_val_if_fail( searchVal != NULL, NULL );
519
520         /* Test whether there are more that one search terms */
521         retVal = ldapctl_build_ldap_criteria( searchVal, ctl->matchingOption );
522         if( retVal ) return retVal;
523
524         if( ctl->matchingOption ==  LDAPCTL_MATCH_CONTAINS ) {
525                 criteriaFmt = _criteriaContains;
526         }
527         else {
528                 criteriaFmt = _criteriaBeginWith;
529         }
530
531         /* No - just a simple search */
532         /* p1 contains previous formatted criteria */
533         /* p2 contains next formatted criteria */
534         retVal = p1 = p2 = NULL;
535         node = ctl->listCriteria;
536         while( node ) {
537                 gchar *attr, *tmp;
538                 attr = node->data;
539                 node = g_list_next( node );
540
541                 /* Switch pointers */
542                 tmp = p1; p1 = p2; p2 = tmp;
543
544                 if( p1 ) {
545                         /* Subsequent time through */
546                         gchar *crit;
547
548                         debug_print("crit: %s\n", searchVal);
549                         /* fix bug when doing a search any */
550                         if (strcmp("*@", searchVal) == 0) {
551                             crit = g_strdup_printf( "(%s=*)", attr );
552                         }
553                         else {
554                             /* Format query criteria */
555                             crit = g_strdup_printf( criteriaFmt, attr, searchVal );
556                         }
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                         /* fix bug when doing a search any */
567                         if (strcmp("*@", searchVal) == 0) {
568                             p2 = g_strdup_printf( "(%s=*)", attr );
569                         }
570                         else {
571                             p2 = g_strdup_printf( criteriaFmt, attr, searchVal );
572                         }
573                 }
574         }
575
576         if( p2 == NULL ) {
577                 /* Nothing processed - format a default attribute */
578                 retVal = g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL );
579         }
580         else {
581                 /* We have something - free up previous result */
582                 retVal = p2;
583                 g_free( p1 );
584         }
585         debug_print("current search string: %s\n", retVal);
586         return retVal;
587 }
588
589 /**
590  * Return array of pointers to attributes for LDAP query.
591  * \param  ctl  Control object to process.
592  * \return NULL terminated list.
593  */
594 char **ldapctl_attribute_array( LdapControl *ctl ) {
595         char **ptrArray;
596         GList *node;
597         gint cnt, i;
598         cm_return_val_if_fail( ctl != NULL, NULL );
599
600         node = ctl->listCriteria;
601         cnt = g_list_length( ctl->listCriteria );
602         ptrArray = g_new0( char *, 1 + cnt );
603         i = 0;
604         while( node ) {
605                 ptrArray[ i++ ] = node->data;
606                 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
607                 node = g_list_next( node );
608         }
609         ptrArray[ i ] = NULL;
610         return ptrArray;
611 }
612
613 /**
614  * Return array of pointers to attributes for LDAP query.
615  * \param  ctl  Control object to process.
616  * \return NULL terminated list.
617  */
618 char **ldapctl_full_attribute_array( LdapControl *ctl ) {
619         char **ptrArray;
620         GList *node, *def;
621         GList *tmp = NULL;
622         gint cnt, i;
623         cm_return_val_if_fail( ctl != NULL, NULL );
624
625         def = ctl->listCriteria;
626         while (def) {
627                 tmp = g_list_append(tmp, g_strdup(def->data));
628                 def = def->next;
629         }
630
631         def = ldapctl_get_default_criteria_list();
632         node = def;
633         
634         while (node) {
635                 if( g_list_find_custom(tmp, (gpointer)def->data, 
636                                 (GCompareFunc)strcmp2) == NULL) {
637                         tmp = g_list_append(tmp, g_strdup(node->data));
638                 }
639                 node = node->next;
640         }
641
642         g_list_free_full(def, g_free);
643
644         node = tmp;
645         cnt = g_list_length( tmp );
646         ptrArray = g_new0( char *, 1 + cnt);
647         i = 0;
648         while( node ) {
649                 ptrArray[ i++ ] = node->data;
650                 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
651                 node = g_list_next( node );
652         }
653         g_list_free(tmp);
654         ptrArray[ i ] = NULL;
655         return ptrArray;
656 }
657
658 /**
659  * Free array of pointers allocated by ldapctl_criteria_array().
660  * param ptrArray Array to clear.
661  */
662 void ldapctl_free_attribute_array( char **ptrArray ) {
663         gint i;
664
665         /* Clear array to NULL's */
666         for( i = 0; ptrArray[i] != NULL; i++ ) {
667                 g_free(ptrArray[i]);
668                 ptrArray[i] = NULL;
669         }
670         g_free( ptrArray );
671 }       
672
673 /**
674  * Parse LDAP search string, building list of LDAP criteria attributes. This
675  * may be used to convert an old style Sylpheed LDAP search criteria to the
676  * new format. The old style uses a standard LDAP search string, for example:
677  * <pre>
678  *    (&(mail=*)(cn=%s*))
679  * </pre>
680  * This function extracts the two LDAP attributes <code>mail</code> and
681  * <code>cn</code>, adding each to a list.
682  *
683  * \param ctl Control object to process.
684  * \param criteria LDAP search criteria string.
685  */
686 void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
687         gchar *ptr;
688         gchar *pFrom;
689         gchar *attrib;
690         gint iLen;
691
692         cm_return_if_fail( ctl != NULL );
693
694         ldapctl_criteria_list_clear( ctl );
695         if( criteria == NULL ) return;
696
697         pFrom = NULL;
698         ptr = criteria;
699         while( *ptr ) {
700                 if( *ptr == '(' ) {
701                         pFrom = 1 + ptr;
702                 }
703                 if( *ptr == '=' ) {
704                         if( pFrom ) {
705                                 iLen = ptr - pFrom;
706                                 attrib = g_strndup( pFrom, iLen );
707                                 g_strstrip( attrib );
708                                 ldapctl_criteria_list_add( ctl, attrib );
709                                 g_free( attrib );
710                         }
711                         pFrom = NULL;
712                 }
713                 ptr++;
714         }
715 }
716
717 /**
718  * Return the default LDAP search criteria string.
719  * \return Formatted string or <i>""</i>. Should be g_free() when done.
720  */
721 gchar *ldapctl_get_default_criteria() {
722         gchar *retVal = g_strdup(LDAPCTL_DFL_ATTR_LIST);
723         const gchar **attrs = ATTRIBUTE; 
724
725         while (*attrs) {
726                 gchar *tmp = g_strdup_printf("%s, %s", retVal, *attrs++);
727                 g_free(retVal);
728                 retVal = tmp;
729         }
730         debug_print("default search criteria: %s\n", retVal);
731         return retVal;
732 }
733
734 /**
735  * Return the default LDAP search criteria list.
736  * \return GList or <i>NULL</i>.
737  */
738 GList *ldapctl_get_default_criteria_list() {
739         gchar *criteria, *item;
740         gchar **c_list, **w_list;
741         GList *attr_list = NULL;
742
743         criteria = ldapctl_get_default_criteria();
744         c_list = g_strsplit(criteria, " ", 0);
745         g_free(criteria);
746         criteria = NULL;
747         w_list = c_list;
748         while ((criteria = *w_list++) != 0) {
749                 /* copy string elimination <,> */
750                 if (*w_list)
751                         item = g_strndup(criteria, strlen(criteria) - 1);
752                 else
753                         item = g_strdup(criteria);
754                 debug_print("adding attribute to list: %s\n", item);
755                 attr_list = g_list_append(attr_list, item);
756         }
757         g_strfreev(c_list);
758         return attr_list;
759 }
760
761 /**
762  * Compare to GList for equality.
763  * \param l1 First GList
764  * \param l2 Second GList
765  * \Return TRUE or FALSE
766  */
767 gboolean ldapctl_compare_list(GList *l1, GList *l2) {
768         gchar *first, *second;
769         if (! l1 && ! l2)
770                 return TRUE;
771         if ((! l1 && l2) || (l1 && ! l2))
772                 return FALSE;
773         while (l1 && l2) {
774                 first = (gchar *) l1->data;
775                 second = (gchar *) l2->data;
776                 /*debug_print("comparing: %s = %s\n", first, second);*/
777                 if ( ! (first && second) || strcmp(first, second) != 0) {
778                         return FALSE;
779                 }
780                 l1 = g_list_next(l1);
781                 l2 = g_list_next(l2);
782         }
783         return TRUE;
784 }
785
786 #endif  /* USE_LDAP */
787
788 /*
789  * End of Source.
790  */
791