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