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