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