2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2007 Match Grun and the Claws Mail team
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.
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.
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/>.
21 * Functions for LDAP control data.
36 #include "editaddress_other_attributes_ldap.h"
37 #include "common/utils.h"
40 * Create new LDAP control block object.
41 * \return Initialized control object.
43 LdapControl *ldapctl_create( void ) {
46 ctl = g_new0( LdapControl, 1 );
48 ctl->port = LDAPCTL_DFL_PORT;
52 ctl->listCriteria = NULL;
53 ctl->attribEMail = g_strdup( LDAPCTL_ATTR_EMAIL );
54 ctl->attribCName = g_strdup( LDAPCTL_ATTR_COMMONNAME );
55 ctl->attribFName = g_strdup( LDAPCTL_ATTR_GIVENNAME );
56 ctl->attribLName = g_strdup( LDAPCTL_ATTR_SURNAME );
57 ctl->attribDName = g_strdup( LDAPCTL_ATTR_DISPLAYNAME );
58 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
59 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
60 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
61 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
63 ctl->enableTLS = FALSE;
64 ctl->enableSSL = FALSE;
66 /* Mutex to protect control block */
67 ctl->mutexCtl = g_malloc0( sizeof( pthread_mutex_t ) );
68 pthread_mutex_init( ctl->mutexCtl, NULL );
74 * Specify hostname to be used.
75 * \param ctl Control object to process.
76 * \param value Host name.
78 void ldapctl_set_host( LdapControl* ctl, const gchar *value ) {
79 ctl->hostName = mgu_replace_string( ctl->hostName, value );
80 g_strstrip( ctl->hostName );
81 debug_print("setting hostname: %s\n", ctl->hostName);
85 * Specify port to be used.
86 * \param ctl Control object to process.
89 void ldapctl_set_port( LdapControl* ctl, const gint value ) {
94 ctl->port = LDAPCTL_DFL_PORT;
96 debug_print("setting port: %d\n", ctl->port);
100 * Specify base DN to be used.
101 * \param ctl Control object to process.
102 * \param value Base DN.
104 void ldapctl_set_base_dn( LdapControl* ctl, const gchar *value ) {
105 ctl->baseDN = mgu_replace_string( ctl->baseDN, value );
106 g_strstrip( ctl->baseDN );
107 debug_print("setting baseDN: %s\n", ctl->baseDN);
111 * Specify bind DN to be used.
112 * \param ctl Control object to process.
113 * \param value Bind DN.
115 void ldapctl_set_bind_dn( LdapControl* ctl, const gchar *value ) {
116 ctl->bindDN = mgu_replace_string( ctl->bindDN, value );
117 g_strstrip( ctl->bindDN );
118 debug_print("setting bindDN: %s\n", ctl->bindDN);
122 * Specify bind password to be used.
123 * \param ctl Control object to process.
124 * \param value Password.
126 void ldapctl_set_bind_password( LdapControl* ctl, const gchar *value ) {
127 ctl->bindPass = mgu_replace_string( ctl->bindPass, value );
128 g_strstrip( ctl->bindPass );
129 debug_print("setting bindPassword");
133 * Specify maximum number of entries to retrieve.
134 * \param ctl Control object to process.
135 * \param value Maximum entries.
137 void ldapctl_set_max_entries( LdapControl* ctl, const gint value ) {
139 ctl->maxEntries = value;
142 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
144 debug_print("setting maxEntries: %d\n", ctl->maxEntries);
148 * Specify timeout value for LDAP operation (in seconds).
149 * \param ctl Control object to process.
150 * \param value Timeout.
152 void ldapctl_set_timeout( LdapControl* ctl, const gint value ) {
154 ctl->timeOut = value;
157 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
159 debug_print("setting timeOut: %d\n", ctl->timeOut);
163 * Specify maximum age of query (in seconds) before query is retired.
164 * \param ctl Control object to process.
165 * \param value Maximum age.
167 void ldapctl_set_max_query_age( LdapControl* ctl, const gint value ) {
168 if( value > LDAPCTL_MAX_QUERY_AGE ) {
169 ctl->maxQueryAge = LDAPCTL_MAX_QUERY_AGE;
171 else if( value < 1 ) {
172 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
175 ctl->maxQueryAge = value;
177 debug_print("setting maxAge: %d\n", ctl->maxQueryAge);
181 * Specify matching option to be used for searches.
182 * \param ctl Control object to process.
183 * \param value Matching option, as follows:
185 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
186 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
189 void ldapctl_set_matching_option( LdapControl* ctl, const gint value ) {
190 if( value < LDAPCTL_MATCH_BEGINWITH ) {
191 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
193 else if( value > LDAPCTL_MATCH_CONTAINS ) {
194 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
197 ctl->matchingOption = value;
199 debug_print("setting matchingOption: %d\n", ctl->matchingOption);
203 * Specify TLS option.
204 * \param ctl Control object to process.
205 * \param value <i>TRUE</i> to enable TLS.
207 void ldapctl_set_tls( LdapControl* ctl, const gboolean value ) {
208 ctl->enableTLS = value;
209 debug_print("setting TLS: %d\n", ctl->enableTLS);
212 void ldapctl_set_ssl( LdapControl* ctl, const gboolean value ) {
213 ctl->enableSSL = value;
214 debug_print("setting SSL: %d\n", ctl->enableSSL);
218 * Return search criteria list.
219 * \param ctl Control data object.
220 * \return Linked list of character strings containing LDAP attribute names to
221 * use for a search. This should not be modified directly. Use the
222 * <code>ldapctl_set_criteria_list()</code>,
223 * <code>ldapctl_criteria_list_clear()</code> and
224 * <code>ldapctl_criteria_list_add()</code> functions for this purpose.
226 GList *ldapctl_get_criteria_list( const LdapControl* ctl ) {
227 g_return_val_if_fail( ctl != NULL, NULL );
228 return ctl->listCriteria;
232 * Clear list of LDAP search attributes.
233 * \param ctl Control data object.
235 void ldapctl_criteria_list_clear( LdapControl *ctl ) {
236 g_return_if_fail( ctl != NULL );
237 mgu_free_dlist( ctl->listCriteria );
238 ctl->listCriteria = NULL;
242 * Add LDAP attribute to criteria list.
243 * \param ctl Control object to process.
244 * \param attr Attribute name to append. If not NULL and unique, a copy will
245 * be appended to the list.
247 void ldapctl_criteria_list_add( LdapControl *ctl, gchar *attr ) {
248 g_return_if_fail( ctl != NULL );
250 if( mgu_list_test_unq_nc( ctl->listCriteria, attr ) ) {
251 debug_print("adding to criteria list: %s\n", attr);
252 ctl->listCriteria = g_list_append(
253 ctl->listCriteria, g_strdup( attr ) );
259 * Clear LDAP server member variables.
260 * \param ctl Control object to clear.
262 static void ldapctl_clear( LdapControl *ctl ) {
263 g_return_if_fail( ctl != NULL );
265 debug_print("clearing ldap controller members\n");
266 /* Free internal stuff */
267 g_free( ctl->hostName );
268 g_free( ctl->baseDN );
269 g_free( ctl->bindDN );
270 g_free( ctl->bindPass );
271 g_free( ctl->attribEMail );
272 g_free( ctl->attribCName );
273 g_free( ctl->attribFName );
274 g_free( ctl->attribLName );
275 g_free( ctl->attribDName );
277 ldapctl_criteria_list_clear( ctl );
280 ctl->hostName = NULL;
284 ctl->bindPass = NULL;
285 ctl->attribEMail = NULL;
286 ctl->attribCName = NULL;
287 ctl->attribFName = NULL;
288 ctl->attribLName = NULL;
289 ctl->attribDName = NULL;
292 ctl->maxQueryAge = 0;
293 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
295 ctl->enableTLS = FALSE;
296 ctl->enableSSL = FALSE;
300 * Free up LDAP server interface object by releasing internal memory.
301 * \param ctl Control object to free.
303 void ldapctl_free( LdapControl *ctl ) {
304 g_return_if_fail( ctl != NULL );
306 debug_print("releasing requested memory for ldap controller\n");
307 /* Free internal stuff */
308 ldapctl_clear( ctl );
311 pthread_mutex_destroy( ctl->mutexCtl );
312 g_free( ctl->mutexCtl );
313 ctl->mutexCtl = NULL;
315 /* Now release LDAP control object */
320 * Display object to specified stream.
321 * \param ctl Control object to process.
322 * \param stream Output stream.
324 void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
325 g_return_if_fail( ctl != NULL );
327 pthread_mutex_lock( ctl->mutexCtl );
328 fprintf( stream, "LdapControl:\n" );
329 fprintf( stream, "host name: '%s'\n", ctl->hostName );
330 fprintf( stream, " port: %d\n", ctl->port );
331 fprintf( stream, " base dn: '%s'\n", ctl->baseDN );
332 fprintf( stream, " bind dn: '%s'\n", ctl->bindDN );
333 fprintf( stream, "bind pass: '%s'\n", ctl->bindPass );
334 fprintf( stream, "attr mail: '%s'\n", ctl->attribEMail );
335 fprintf( stream, "attr comn: '%s'\n", ctl->attribCName );
336 fprintf( stream, "attr frst: '%s'\n", ctl->attribFName );
337 fprintf( stream, "attr last: '%s'\n", ctl->attribLName );
338 fprintf( stream, "attr disn: '%s'\n", ctl->attribDName );
339 fprintf( stream, "max entry: %d\n", ctl->maxEntries );
340 fprintf( stream, " timeout: %d\n", ctl->timeOut );
341 fprintf( stream, " max age: %d\n", ctl->maxQueryAge );
342 fprintf( stream, "match opt: %d\n", ctl->matchingOption );
343 fprintf( stream, " version: %d\n", ctl->version );
344 fprintf( stream, " TLS: %s\n", ctl->enableTLS ? "yes" : "no" );
345 fprintf( stream, " SSL: %s\n", ctl->enableSSL ? "yes" : "no" );
346 fprintf( stream, "crit list:\n" );
347 if( ctl->listCriteria ) {
348 mgu_print_dlist( ctl->listCriteria, stream );
351 fprintf( stream, "\t!!!none!!!\n" );
353 pthread_mutex_unlock( ctl->mutexCtl );
357 * Copy member variables to specified object. Mutex lock object is
359 * \param ctlFrom Object to copy from.
360 * \param ctlTo Destination object.
362 void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
365 g_return_if_fail( ctlFrom != NULL );
366 g_return_if_fail( ctlTo != NULL );
368 debug_print("ldap controller copy\n");
369 /* Lock both objects */
370 pthread_mutex_lock( ctlFrom->mutexCtl );
371 pthread_mutex_lock( ctlTo->mutexCtl );
373 /* Clear our destination */
374 ldapctl_clear( ctlTo );
377 ctlTo->hostName = g_strdup( ctlFrom->hostName );
378 ctlTo->baseDN = g_strdup( ctlFrom->baseDN );
379 ctlTo->bindDN = g_strdup( ctlFrom->bindDN );
380 ctlTo->bindPass = g_strdup( ctlFrom->bindPass );
381 ctlTo->attribEMail = g_strdup( ctlFrom->attribEMail );
382 ctlTo->attribCName = g_strdup( ctlFrom->attribCName );
383 ctlTo->attribFName = g_strdup( ctlFrom->attribFName );
384 ctlTo->attribLName = g_strdup( ctlFrom->attribLName );
385 ctlTo->attribDName = g_strdup( ctlFrom->attribDName );
387 /* Copy search criteria */
388 node = ctlFrom->listCriteria;
390 ctlTo->listCriteria = g_list_append(
391 ctlTo->listCriteria, g_strdup( node->data ) );
392 node = g_list_next( node );
395 /* Copy other members */
396 ctlTo->port = ctlFrom->port;
397 ctlTo->maxEntries = ctlFrom->maxEntries;
398 ctlTo->timeOut = ctlFrom->timeOut;
399 ctlTo->maxQueryAge = ctlFrom->maxQueryAge;
400 ctlTo->matchingOption = ctlFrom->matchingOption;
401 ctlTo->version = ctlFrom->version;
402 ctlTo->enableTLS = ctlFrom->enableTLS;
403 ctlTo->enableSSL = ctlFrom->enableSSL;
406 pthread_mutex_unlock( ctlTo->mutexCtl );
407 pthread_mutex_unlock( ctlFrom->mutexCtl );
411 * Search criteria fragment - two terms - begin with (default).
413 static gchar *_criteria2BeginWith = "(&(givenName=%s*)(sn=%s*))";
416 * Search criteria fragment - two terms - contains.
418 static gchar *_criteria2Contains = "(&(givenName=*%s*)(sn=*%s*))";
421 * Create an LDAP search criteria by parsing specified search term. The search
422 * term may contain two names separated by the first embedded space found in
423 * the search term. It is assumed that the two tokens are first name and last
424 * name, or vice versa. An appropriate search criteria will be constructed.
426 * \param searchTerm Reference to search term to process.
427 * \param matchOption Set to the following:
429 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
430 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
433 * \return Formatted search criteria, or <code>NULL</code> if there is no
434 * embedded spaces. The search term should be g_free() when no
437 static gchar *ldapctl_build_ldap_criteria(
438 const gchar *searchTerm, const gint matchOption )
447 if( matchOption == LDAPCTL_MATCH_CONTAINS ) {
448 criteriaFmt = _criteria2Contains;
451 criteriaFmt = _criteria2BeginWith;
454 term = g_strdup( searchTerm );
457 /* Find first space character */
462 t2 = g_strdup( 1 + p );
469 /* Format search criteria */
473 p1 = g_strdup_printf( criteriaFmt, t1, t2 );
474 p2 = g_strdup_printf( criteriaFmt, t2, t1 );
475 crit = g_strdup_printf( "(&(|%s%s)(mail=*))", p1, p2 );
482 debug_print("search criteria: %s\n", crit);
488 * Search criteria fragment - single term - begin with (default).
490 static gchar *_criteriaBeginWith = "(%s=%s*)";
493 * Search criteria fragment - single term - contains.
495 static gchar *_criteriaContains = "(%s=*%s*)";
498 * Build a formatted LDAP search criteria string from criteria list.
499 * \param ctl Control object to process.
500 * \param searchVal Value to search for.
501 * \return Formatted string. Should be g_free() when done.
503 gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
505 gchar *p1, *p2, *retVal;
508 g_return_val_if_fail( ctl != NULL, NULL );
509 g_return_val_if_fail( searchVal != NULL, NULL );
511 /* Test whether there are more that one search terms */
512 retVal = ldapctl_build_ldap_criteria( searchVal, ctl->matchingOption );
513 if( retVal ) return retVal;
515 if( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) {
516 criteriaFmt = _criteriaContains;
519 criteriaFmt = _criteriaBeginWith;
522 /* No - just a simple search */
523 /* p1 contains previous formatted criteria */
524 /* p2 contains next formatted criteria */
525 retVal = p1 = p2 = NULL;
526 node = ctl->listCriteria;
530 node = g_list_next( node );
532 /* Switch pointers */
533 tmp = p1; p1 = p2; p2 = tmp;
536 /* Subsequent time through */
539 /* Format query criteria */
540 crit = g_strdup_printf( criteriaFmt, attr, searchVal );
542 /* Append to existing criteria */
544 p2 = g_strdup_printf( "(|%s%s)", p1, crit );
549 /* First time through - Format query criteria */
550 p2 = g_strdup_printf( criteriaFmt, attr, searchVal );
555 /* Nothing processed - format a default attribute */
556 retVal = g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL );
559 /* We have something - free up previous result */
563 debug_print("current search string: %s\n", retVal);
568 * Return array of pointers to attributes for LDAP query.
569 * \param ctl Control object to process.
570 * \return NULL terminated list.
572 char **ldapctl_attribute_array( LdapControl *ctl ) {
576 g_return_val_if_fail( ctl != NULL, NULL );
578 def = ldapctl_get_default_criteria_list();
579 /* check if this servers config is updated to the new
580 * default list of search criteria. If not update the list */
581 if (! ldapctl_compare_list(ctl->listCriteria, def)) {
582 /* Deep copy search criteria */
583 ldapctl_criteria_list_clear(ctl);
585 ctl->listCriteria = g_list_append(
586 ctl->listCriteria, g_strdup(def->data));
587 def = g_list_next(def);
590 node = ctl->listCriteria;
591 cnt = g_list_length( ctl->listCriteria );
592 ptrArray = g_new0( char *, 1 + cnt );
595 ptrArray[ i++ ] = node->data;
596 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
597 node = g_list_next( node );
599 ptrArray[ i ] = NULL;
604 * Free array of pointers allocated by ldapctl_criteria_array().
605 * param ptrArray Array to clear.
607 void ldapctl_free_attribute_array( char **ptrArray ) {
610 /* Clear array to NULL's */
611 for( i = 0; ptrArray[i] != NULL; i++ ) {
618 * Parse LDAP search string, building list of LDAP criteria attributes. This
619 * may be used to convert an old style Sylpheed LDAP search criteria to the
620 * new format. The old style uses a standard LDAP search string, for example:
622 * (&(mail=*)(cn=%s*))
624 * This function extracts the two LDAP attributes <code>mail</code> and
625 * <code>cn</code>, adding each to a list.
627 * \param ctl Control object to process.
628 * \param criteria LDAP search criteria string.
630 void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
636 g_return_if_fail( ctl != NULL );
638 ldapctl_criteria_list_clear( ctl );
639 if( criteria == NULL ) return;
650 attrib = g_strndup( pFrom, iLen );
651 g_strstrip( attrib );
652 ldapctl_criteria_list_add( ctl, attrib );
662 * Return the default LDAP search criteria string.
663 * \return Formatted string or <i>""</i>. Should be g_free() when done.
665 gchar *ldapctl_get_default_criteria() {
666 gchar *retVal = g_strdup(LDAPCTL_DFL_ATTR_LIST);
667 const gchar **attrs = ATTRIBUTE;
670 gchar *tmp = g_strdup_printf("%s, %s", retVal, *attrs++);
674 debug_print("default search criteria: %s\n", retVal);
679 * Return the default LDAP search criteria list.
680 * \return GList or <i>NULL</i>.
682 GList *ldapctl_get_default_criteria_list() {
683 gchar *criteria, *item;
684 gchar **c_list, **w_list;
685 GList *attr_list = NULL;
687 criteria = ldapctl_get_default_criteria();
688 c_list = g_strsplit(criteria, " ", 0);
692 while ((criteria = *w_list++) != 0) {
693 /* copy string elimination <,> */
695 item = g_strndup(criteria, strlen(criteria) - 1);
697 item = g_strdup(criteria);
698 debug_print("adding attribute to list: %s\n", item);
699 attr_list = g_list_append(attr_list, g_strdup(item));
707 * Compare to GList for equality.
708 * \param l1 First GList
709 * \param l2 Second GList
710 * \Return TRUE or FALSE
712 gboolean ldapctl_compare_list(GList *l1, GList *l2) {
713 gchar *first, *second;
716 if ((! l1 && l2) || (l1 && ! l2))
719 first = (gchar *) l1->data;
720 second = (gchar *) l2->data;
721 /*debug_print("comparing: %s = %s\n", first, second);*/
722 if ( ! (first && second) || strcmp(first, second) != 0) {
725 l1 = g_list_next(l1);
726 l2 = g_list_next(l2);
731 #endif /* USE_LDAP */