2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2009 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 );
81 if ( ctl->hostName == NULL )
84 g_strstrip( ctl->hostName );
85 debug_print("setting hostname: %s\n", ctl->hostName);
89 * Specify port to be used.
90 * \param ctl Control object to process.
93 void ldapctl_set_port( LdapControl* ctl, const gint value ) {
98 ctl->port = LDAPCTL_DFL_PORT;
100 debug_print("setting port: %d\n", ctl->port);
104 * Specify base DN to be used.
105 * \param ctl Control object to process.
106 * \param value Base DN.
108 void ldapctl_set_base_dn( LdapControl* ctl, const gchar *value ) {
109 ctl->baseDN = mgu_replace_string( ctl->baseDN, value );
111 if ( ctl->baseDN == NULL )
114 g_strstrip( ctl->baseDN );
115 debug_print("setting baseDN: %s\n", ctl->baseDN);
119 * Specify bind DN to be used.
120 * \param ctl Control object to process.
121 * \param value Bind DN.
123 void ldapctl_set_bind_dn( LdapControl* ctl, const gchar *value ) {
124 ctl->bindDN = mgu_replace_string( ctl->bindDN, value );
126 if ( ctl->bindDN == NULL )
129 g_strstrip( ctl->bindDN );
130 debug_print("setting bindDN: %s\n", ctl->bindDN);
134 * Specify bind password to be used.
135 * \param ctl Control object to process.
136 * \param value Password.
138 void ldapctl_set_bind_password( LdapControl* ctl, const gchar *value ) {
139 ctl->bindPass = mgu_replace_string( ctl->bindPass, value );
141 if ( ctl->bindPass == NULL )
144 g_strstrip( ctl->bindPass );
145 debug_print("setting bindPassword\n");
149 * Specify maximum number of entries to retrieve.
150 * \param ctl Control object to process.
151 * \param value Maximum entries.
153 void ldapctl_set_max_entries( LdapControl* ctl, const gint value ) {
155 ctl->maxEntries = value;
158 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
160 debug_print("setting maxEntries: %d\n", ctl->maxEntries);
164 * Specify timeout value for LDAP operation (in seconds).
165 * \param ctl Control object to process.
166 * \param value Timeout.
168 void ldapctl_set_timeout( LdapControl* ctl, const gint value ) {
170 ctl->timeOut = value;
173 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
175 debug_print("setting timeOut: %d\n", ctl->timeOut);
179 * Specify maximum age of query (in seconds) before query is retired.
180 * \param ctl Control object to process.
181 * \param value Maximum age.
183 void ldapctl_set_max_query_age( LdapControl* ctl, const gint value ) {
184 if( value > LDAPCTL_MAX_QUERY_AGE ) {
185 ctl->maxQueryAge = LDAPCTL_MAX_QUERY_AGE;
187 else if( value < 1 ) {
188 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
191 ctl->maxQueryAge = value;
193 debug_print("setting maxAge: %d\n", ctl->maxQueryAge);
197 * Specify matching option to be used for searches.
198 * \param ctl Control object to process.
199 * \param value Matching option, as follows:
201 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
202 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
205 void ldapctl_set_matching_option( LdapControl* ctl, const gint value ) {
206 if( value < LDAPCTL_MATCH_BEGINWITH ) {
207 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
209 else if( value > LDAPCTL_MATCH_CONTAINS ) {
210 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
213 ctl->matchingOption = value;
215 debug_print("setting matchingOption: %d\n", ctl->matchingOption);
219 * Specify TLS option.
220 * \param ctl Control object to process.
221 * \param value <i>TRUE</i> to enable TLS.
223 void ldapctl_set_tls( LdapControl* ctl, const gboolean value ) {
224 ctl->enableTLS = value;
225 debug_print("setting TLS: %d\n", ctl->enableTLS);
228 void ldapctl_set_ssl( LdapControl* ctl, const gboolean value ) {
229 ctl->enableSSL = value;
230 debug_print("setting SSL: %d\n", ctl->enableSSL);
234 * Return search criteria list.
235 * \param ctl Control data object.
236 * \return Linked list of character strings containing LDAP attribute names to
237 * use for a search. This should not be modified directly. Use the
238 * <code>ldapctl_set_criteria_list()</code>,
239 * <code>ldapctl_criteria_list_clear()</code> and
240 * <code>ldapctl_criteria_list_add()</code> functions for this purpose.
242 GList *ldapctl_get_criteria_list( const LdapControl* ctl ) {
243 cm_return_val_if_fail( ctl != NULL, NULL );
244 return ctl->listCriteria;
248 * Clear list of LDAP search attributes.
249 * \param ctl Control data object.
251 void ldapctl_criteria_list_clear( LdapControl *ctl ) {
252 cm_return_if_fail( ctl != NULL );
253 mgu_free_dlist( ctl->listCriteria );
254 ctl->listCriteria = NULL;
258 * Add LDAP attribute to criteria list.
259 * \param ctl Control object to process.
260 * \param attr Attribute name to append. If not NULL and unique, a copy will
261 * be appended to the list.
263 void ldapctl_criteria_list_add( LdapControl *ctl, gchar *attr ) {
264 cm_return_if_fail( ctl != NULL );
266 if( mgu_list_test_unq_nc( ctl->listCriteria, attr ) ) {
267 debug_print("adding to criteria list: %s\n", attr);
268 ctl->listCriteria = g_list_append(
269 ctl->listCriteria, g_strdup( attr ) );
275 * Clear LDAP server member variables.
276 * \param ctl Control object to clear.
278 static void ldapctl_clear( LdapControl *ctl ) {
279 cm_return_if_fail( ctl != NULL );
281 debug_print("clearing ldap controller members\n");
282 /* Free internal stuff */
283 g_free( ctl->hostName );
284 g_free( ctl->baseDN );
285 g_free( ctl->bindDN );
286 g_free( ctl->bindPass );
287 g_free( ctl->attribEMail );
288 g_free( ctl->attribCName );
289 g_free( ctl->attribFName );
290 g_free( ctl->attribLName );
291 g_free( ctl->attribDName );
293 ldapctl_criteria_list_clear( ctl );
296 ctl->hostName = NULL;
300 ctl->bindPass = NULL;
301 ctl->attribEMail = NULL;
302 ctl->attribCName = NULL;
303 ctl->attribFName = NULL;
304 ctl->attribLName = NULL;
305 ctl->attribDName = NULL;
308 ctl->maxQueryAge = 0;
309 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
311 ctl->enableTLS = FALSE;
312 ctl->enableSSL = FALSE;
316 * Free up LDAP server interface object by releasing internal memory.
317 * \param ctl Control object to free.
319 void ldapctl_free( LdapControl *ctl ) {
320 cm_return_if_fail( ctl != NULL );
322 debug_print("releasing requested memory for ldap controller\n");
323 /* Free internal stuff */
324 ldapctl_clear( ctl );
327 pthread_mutex_destroy( ctl->mutexCtl );
328 g_free( ctl->mutexCtl );
329 ctl->mutexCtl = NULL;
331 /* Now release LDAP control object */
336 * Display object to specified stream.
337 * \param ctl Control object to process.
338 * \param stream Output stream.
340 void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
341 cm_return_if_fail( ctl != NULL );
343 pthread_mutex_lock( ctl->mutexCtl );
344 fprintf( stream, "LdapControl:\n" );
345 fprintf( stream, "host name: '%s'\n", ctl->hostName?ctl->hostName:"null" );
346 fprintf( stream, " port: %d\n", ctl->port );
347 fprintf( stream, " base dn: '%s'\n", ctl->baseDN?ctl->baseDN:"null" );
348 fprintf( stream, " bind dn: '%s'\n", ctl->bindDN?ctl->bindDN:"null" );
349 fprintf( stream, "bind pass: '%s'\n", ctl->bindPass?ctl->bindPass:"null" );
350 fprintf( stream, "attr mail: '%s'\n", ctl->attribEMail?ctl->attribEMail:"null" );
351 fprintf( stream, "attr comn: '%s'\n", ctl->attribCName?ctl->attribCName:"null" );
352 fprintf( stream, "attr frst: '%s'\n", ctl->attribFName?ctl->attribFName:"null" );
353 fprintf( stream, "attr last: '%s'\n", ctl->attribLName?ctl->attribLName:"null" );
354 fprintf( stream, "attr disn: '%s'\n", ctl->attribDName?ctl->attribDName:"null" );
355 fprintf( stream, "max entry: %d\n", ctl->maxEntries );
356 fprintf( stream, " timeout: %d\n", ctl->timeOut );
357 fprintf( stream, " max age: %d\n", ctl->maxQueryAge );
358 fprintf( stream, "match opt: %d\n", ctl->matchingOption );
359 fprintf( stream, " version: %d\n", ctl->version );
360 fprintf( stream, " TLS: %s\n", ctl->enableTLS ? "yes" : "no" );
361 fprintf( stream, " SSL: %s\n", ctl->enableSSL ? "yes" : "no" );
362 fprintf( stream, "crit list:\n" );
363 if( ctl->listCriteria ) {
364 mgu_print_dlist( ctl->listCriteria, stream );
367 fprintf( stream, "\t!!!none!!!\n" );
369 pthread_mutex_unlock( ctl->mutexCtl );
373 * Copy member variables to specified object. Mutex lock object is
375 * \param ctlFrom Object to copy from.
376 * \param ctlTo Destination object.
378 void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
381 cm_return_if_fail( ctlFrom != NULL );
382 cm_return_if_fail( ctlTo != NULL );
384 debug_print("ldap controller copy\n");
385 /* Lock both objects */
386 pthread_mutex_lock( ctlFrom->mutexCtl );
387 pthread_mutex_lock( ctlTo->mutexCtl );
389 /* Clear our destination */
390 ldapctl_clear( ctlTo );
393 ctlTo->hostName = g_strdup( ctlFrom->hostName );
394 ctlTo->baseDN = g_strdup( ctlFrom->baseDN );
395 ctlTo->bindDN = g_strdup( ctlFrom->bindDN );
396 ctlTo->bindPass = g_strdup( ctlFrom->bindPass );
397 ctlTo->attribEMail = g_strdup( ctlFrom->attribEMail );
398 ctlTo->attribCName = g_strdup( ctlFrom->attribCName );
399 ctlTo->attribFName = g_strdup( ctlFrom->attribFName );
400 ctlTo->attribLName = g_strdup( ctlFrom->attribLName );
401 ctlTo->attribDName = g_strdup( ctlFrom->attribDName );
403 /* Copy search criteria */
404 node = ctlFrom->listCriteria;
406 ctlTo->listCriteria = g_list_append(
407 ctlTo->listCriteria, g_strdup( node->data ) );
408 node = g_list_next( node );
411 /* Copy other members */
412 ctlTo->port = ctlFrom->port;
413 ctlTo->maxEntries = ctlFrom->maxEntries;
414 ctlTo->timeOut = ctlFrom->timeOut;
415 ctlTo->maxQueryAge = ctlFrom->maxQueryAge;
416 ctlTo->matchingOption = ctlFrom->matchingOption;
417 ctlTo->version = ctlFrom->version;
418 ctlTo->enableTLS = ctlFrom->enableTLS;
419 ctlTo->enableSSL = ctlFrom->enableSSL;
422 pthread_mutex_unlock( ctlTo->mutexCtl );
423 pthread_mutex_unlock( ctlFrom->mutexCtl );
427 * Search criteria fragment - two terms - begin with (default).
429 static gchar *_criteria2BeginWith = "(&(givenName=%s*)(sn=%s*))";
432 * Search criteria fragment - two terms - contains.
434 static gchar *_criteria2Contains = "(&(givenName=*%s*)(sn=*%s*))";
437 * Create an LDAP search criteria by parsing specified search term. The search
438 * term may contain two names separated by the first embedded space found in
439 * the search term. It is assumed that the two tokens are first name and last
440 * name, or vice versa. An appropriate search criteria will be constructed.
442 * \param searchTerm Reference to search term to process.
443 * \param matchOption Set to the following:
445 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
446 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
449 * \return Formatted search criteria, or <code>NULL</code> if there is no
450 * embedded spaces. The search term should be g_free() when no
453 static gchar *ldapctl_build_ldap_criteria(
454 const gchar *searchTerm, const gint matchOption )
463 if( matchOption == LDAPCTL_MATCH_CONTAINS ) {
464 criteriaFmt = _criteria2Contains;
467 criteriaFmt = _criteria2BeginWith;
470 term = g_strdup( searchTerm );
473 /* Find first space character */
478 t2 = g_strdup( 1 + p );
485 /* Format search criteria */
489 p1 = g_strdup_printf( criteriaFmt, t1, t2 );
490 p2 = g_strdup_printf( criteriaFmt, t2, t1 );
491 crit = g_strdup_printf( "(&(|%s%s)(mail=*))", p1, p2 );
498 debug_print("search criteria: %s\n", crit?crit:"null");
504 * Search criteria fragment - single term - begin with (default).
506 static gchar *_criteriaBeginWith = "(%s=%s*)";
509 * Search criteria fragment - single term - contains.
511 static gchar *_criteriaContains = "(%s=*%s*)";
514 * Build a formatted LDAP search criteria string from criteria list.
515 * \param ctl Control object to process.
516 * \param searchVal Value to search for.
517 * \return Formatted string. Should be g_free() when done.
519 gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
521 gchar *p1, *p2, *retVal;
524 cm_return_val_if_fail( ctl != NULL, NULL );
525 cm_return_val_if_fail( searchVal != NULL, NULL );
527 /* Test whether there are more that one search terms */
528 retVal = ldapctl_build_ldap_criteria( searchVal, ctl->matchingOption );
529 if( retVal ) return retVal;
531 if( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) {
532 criteriaFmt = _criteriaContains;
535 criteriaFmt = _criteriaBeginWith;
538 /* No - just a simple search */
539 /* p1 contains previous formatted criteria */
540 /* p2 contains next formatted criteria */
541 retVal = p1 = p2 = NULL;
542 node = ctl->listCriteria;
546 node = g_list_next( node );
548 /* Switch pointers */
549 tmp = p1; p1 = p2; p2 = tmp;
552 /* Subsequent time through */
555 /* Format query criteria */
556 crit = g_strdup_printf( criteriaFmt, attr, searchVal );
558 /* Append to existing criteria */
560 p2 = g_strdup_printf( "(|%s%s)", p1, crit );
565 /* First time through - Format query criteria */
566 p2 = g_strdup_printf( criteriaFmt, attr, searchVal );
571 /* Nothing processed - format a default attribute */
572 retVal = g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL );
575 /* We have something - free up previous result */
579 debug_print("current search string: %s\n", retVal);
584 * Return array of pointers to attributes for LDAP query.
585 * \param ctl Control object to process.
586 * \return NULL terminated list.
588 char **ldapctl_attribute_array( LdapControl *ctl ) {
592 cm_return_val_if_fail( ctl != NULL, NULL );
594 node = ctl->listCriteria;
595 cnt = g_list_length( ctl->listCriteria );
596 ptrArray = g_new0( char *, 1 + cnt );
599 ptrArray[ i++ ] = node->data;
600 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
601 node = g_list_next( node );
603 ptrArray[ i ] = NULL;
608 * Return array of pointers to attributes for LDAP query.
609 * \param ctl Control object to process.
610 * \return NULL terminated list.
612 char **ldapctl_full_attribute_array( LdapControl *ctl ) {
617 cm_return_val_if_fail( ctl != NULL, NULL );
619 def = ctl->listCriteria;
621 tmp = g_list_append(tmp, g_strdup(def->data));
625 def = ldapctl_get_default_criteria_list();
628 if( g_list_find_custom(tmp, (gpointer)def->data,
629 (GCompareFunc)strcmp2) == NULL) {
630 tmp = g_list_append(tmp, g_strdup(def->data));
636 cnt = g_list_length( tmp );
637 ptrArray = g_new0( char *, 1 + cnt);
640 ptrArray[ i++ ] = node->data;
641 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
642 node = g_list_next( node );
644 ptrArray[ i ] = NULL;
649 * Free array of pointers allocated by ldapctl_criteria_array().
650 * param ptrArray Array to clear.
652 void ldapctl_free_attribute_array( char **ptrArray ) {
655 /* Clear array to NULL's */
656 for( i = 0; ptrArray[i] != NULL; i++ ) {
663 * Parse LDAP search string, building list of LDAP criteria attributes. This
664 * may be used to convert an old style Sylpheed LDAP search criteria to the
665 * new format. The old style uses a standard LDAP search string, for example:
667 * (&(mail=*)(cn=%s*))
669 * This function extracts the two LDAP attributes <code>mail</code> and
670 * <code>cn</code>, adding each to a list.
672 * \param ctl Control object to process.
673 * \param criteria LDAP search criteria string.
675 void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
681 cm_return_if_fail( ctl != NULL );
683 ldapctl_criteria_list_clear( ctl );
684 if( criteria == NULL ) return;
695 attrib = g_strndup( pFrom, iLen );
696 g_strstrip( attrib );
697 ldapctl_criteria_list_add( ctl, attrib );
707 * Return the default LDAP search criteria string.
708 * \return Formatted string or <i>""</i>. Should be g_free() when done.
710 gchar *ldapctl_get_default_criteria() {
711 gchar *retVal = g_strdup(LDAPCTL_DFL_ATTR_LIST);
712 const gchar **attrs = ATTRIBUTE;
715 gchar *tmp = g_strdup_printf("%s, %s", retVal, *attrs++);
719 debug_print("default search criteria: %s\n", retVal);
724 * Return the default LDAP search criteria list.
725 * \return GList or <i>NULL</i>.
727 GList *ldapctl_get_default_criteria_list() {
728 gchar *criteria, *item;
729 gchar **c_list, **w_list;
730 GList *attr_list = NULL;
732 criteria = ldapctl_get_default_criteria();
733 c_list = g_strsplit(criteria, " ", 0);
737 while ((criteria = *w_list++) != 0) {
738 /* copy string elimination <,> */
740 item = g_strndup(criteria, strlen(criteria) - 1);
742 item = g_strdup(criteria);
743 debug_print("adding attribute to list: %s\n", item);
744 attr_list = g_list_append(attr_list, g_strdup(item));
752 * Compare to GList for equality.
753 * \param l1 First GList
754 * \param l2 Second GList
755 * \Return TRUE or FALSE
757 gboolean ldapctl_compare_list(GList *l1, GList *l2) {
758 gchar *first, *second;
761 if ((! l1 && l2) || (l1 && ! l2))
764 first = (gchar *) l1->data;
765 second = (gchar *) l2->data;
766 /*debug_print("comparing: %s = %s\n", first, second);*/
767 if ( ! (first && second) || strcmp(first, second) != 0) {
770 l1 = g_list_next(l1);
771 l2 = g_list_next(l2);
776 #endif /* USE_LDAP */