2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2012 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.
26 #include "claws-features.h"
37 #include "passwordstore.h"
38 #include "editaddress_other_attributes_ldap.h"
39 #include "common/utils.h"
40 #include "common/quoted-printable.h"
43 * Create new LDAP control block object.
44 * \return Initialized control object.
46 LdapControl *ldapctl_create( void ) {
49 ctl = g_new0( LdapControl, 1 );
51 ctl->port = LDAPCTL_DFL_PORT;
54 ctl->listCriteria = NULL;
55 ctl->attribEMail = g_strdup( LDAPCTL_ATTR_EMAIL );
56 ctl->attribCName = g_strdup( LDAPCTL_ATTR_COMMONNAME );
57 ctl->attribFName = g_strdup( LDAPCTL_ATTR_GIVENNAME );
58 ctl->attribLName = g_strdup( LDAPCTL_ATTR_SURNAME );
59 ctl->attribDName = g_strdup( LDAPCTL_ATTR_DISPLAYNAME );
60 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
61 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
62 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
63 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
65 ctl->enableTLS = FALSE;
66 ctl->enableSSL = FALSE;
68 /* Mutex to protect control block */
69 ctl->mutexCtl = g_malloc0( sizeof( pthread_mutex_t ) );
70 pthread_mutex_init( ctl->mutexCtl, NULL );
76 * Specify hostname to be used.
77 * \param ctl Control object to process.
78 * \param value Host name.
80 void ldapctl_set_host( LdapControl* ctl, const gchar *value ) {
81 ctl->hostName = mgu_replace_string( ctl->hostName, value );
83 if ( ctl->hostName == NULL )
86 g_strstrip( ctl->hostName );
87 debug_print("setting hostname: %s\n", ctl->hostName);
91 * Specify port to be used.
92 * \param ctl Control object to process.
95 void ldapctl_set_port( LdapControl* ctl, const gint value ) {
100 ctl->port = LDAPCTL_DFL_PORT;
102 debug_print("setting port: %d\n", ctl->port);
106 * Specify base DN to be used.
107 * \param ctl Control object to process.
108 * \param value Base DN.
110 void ldapctl_set_base_dn( LdapControl* ctl, const gchar *value ) {
111 ctl->baseDN = mgu_replace_string( ctl->baseDN, value );
113 if ( ctl->baseDN == NULL )
116 g_strstrip( ctl->baseDN );
117 debug_print("setting baseDN: %s\n", ctl->baseDN);
121 * Specify bind DN to be used.
122 * \param ctl Control object to process.
123 * \param value Bind DN.
125 void ldapctl_set_bind_dn( LdapControl* ctl, const gchar *value ) {
126 ctl->bindDN = mgu_replace_string( ctl->bindDN, value );
128 if ( ctl->bindDN == NULL )
131 g_strstrip( ctl->bindDN );
132 debug_print("setting bindDN: %s\n", ctl->bindDN);
136 * Specify maximum number of entries to retrieve.
137 * \param ctl Control object to process.
138 * \param value Maximum entries.
140 void ldapctl_set_max_entries( LdapControl* ctl, const gint value ) {
142 ctl->maxEntries = value;
145 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
147 debug_print("setting maxEntries: %d\n", ctl->maxEntries);
151 * Specify timeout value for LDAP operation (in seconds).
152 * \param ctl Control object to process.
153 * \param value Timeout.
155 void ldapctl_set_timeout( LdapControl* ctl, const gint value ) {
157 ctl->timeOut = value;
160 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
162 debug_print("setting timeOut: %d\n", ctl->timeOut);
166 * Specify maximum age of query (in seconds) before query is retired.
167 * \param ctl Control object to process.
168 * \param value Maximum age.
170 void ldapctl_set_max_query_age( LdapControl* ctl, const gint value ) {
171 if( value > LDAPCTL_MAX_QUERY_AGE ) {
172 ctl->maxQueryAge = LDAPCTL_MAX_QUERY_AGE;
174 else if( value < 1 ) {
175 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
178 ctl->maxQueryAge = value;
180 debug_print("setting maxAge: %d\n", ctl->maxQueryAge);
184 * Specify matching option to be used for searches.
185 * \param ctl Control object to process.
186 * \param value Matching option, as follows:
188 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
189 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
192 void ldapctl_set_matching_option( LdapControl* ctl, const gint value ) {
193 if( value < LDAPCTL_MATCH_BEGINWITH ) {
194 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
196 else if( value > LDAPCTL_MATCH_CONTAINS ) {
197 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
200 ctl->matchingOption = value;
202 debug_print("setting matchingOption: %d\n", ctl->matchingOption);
206 * Specify TLS option.
207 * \param ctl Control object to process.
208 * \param value <i>TRUE</i> to enable TLS.
210 void ldapctl_set_tls( LdapControl* ctl, const gboolean value ) {
211 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
212 ctl->enableTLS = value;
213 debug_print("setting STARTTLS: %d\n", ctl->enableTLS);
217 void ldapctl_set_ssl( LdapControl* ctl, const gboolean value ) {
218 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
219 ctl->enableSSL = value;
220 debug_print("setting SSL/TLS: %d\n", ctl->enableSSL);
225 * Return search criteria list.
226 * \param ctl Control data object.
227 * \return Linked list of character strings containing LDAP attribute names to
228 * use for a search. This should not be modified directly. Use the
229 * <code>ldapctl_set_criteria_list()</code>,
230 * <code>ldapctl_criteria_list_clear()</code> and
231 * <code>ldapctl_criteria_list_add()</code> functions for this purpose.
233 GList *ldapctl_get_criteria_list( const LdapControl* ctl ) {
234 cm_return_val_if_fail( ctl != NULL, NULL );
235 return ctl->listCriteria;
239 * Clear list of LDAP search attributes.
240 * \param ctl Control data object.
242 void ldapctl_criteria_list_clear( LdapControl *ctl ) {
243 cm_return_if_fail( ctl != NULL );
244 mgu_free_dlist( ctl->listCriteria );
245 ctl->listCriteria = NULL;
249 * Add LDAP attribute to criteria list.
250 * \param ctl Control object to process.
251 * \param attr Attribute name to append. If not NULL and unique, a copy will
252 * be appended to the list.
254 void ldapctl_criteria_list_add( LdapControl *ctl, gchar *attr ) {
255 cm_return_if_fail( ctl != NULL );
257 if( mgu_list_test_unq_nc( ctl->listCriteria, attr ) ) {
258 debug_print("adding to criteria list: %s\n", attr);
259 ctl->listCriteria = g_list_append(
260 ctl->listCriteria, g_strdup( attr ) );
266 * Clear LDAP server member variables.
267 * \param ctl Control object to clear.
269 static void ldapctl_clear( LdapControl *ctl ) {
270 cm_return_if_fail( ctl != NULL );
272 debug_print("clearing ldap controller members\n");
273 /* Free internal stuff */
274 g_free( ctl->hostName );
275 g_free( ctl->baseDN );
276 g_free( ctl->bindDN );
277 g_free( ctl->attribEMail );
278 g_free( ctl->attribCName );
279 g_free( ctl->attribFName );
280 g_free( ctl->attribLName );
281 g_free( ctl->attribDName );
283 ldapctl_criteria_list_clear( ctl );
286 ctl->hostName = NULL;
290 ctl->attribEMail = NULL;
291 ctl->attribCName = NULL;
292 ctl->attribFName = NULL;
293 ctl->attribLName = NULL;
294 ctl->attribDName = NULL;
297 ctl->maxQueryAge = 0;
298 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
300 ctl->enableTLS = FALSE;
301 ctl->enableSSL = FALSE;
305 * Free up LDAP server interface object by releasing internal memory.
306 * \param ctl Control object to free.
308 void ldapctl_free( LdapControl *ctl ) {
309 cm_return_if_fail( ctl != NULL );
311 debug_print("releasing requested memory for ldap controller\n");
312 /* Free internal stuff */
313 ldapctl_clear( ctl );
316 pthread_mutex_destroy( ctl->mutexCtl );
317 g_free( ctl->mutexCtl );
318 ctl->mutexCtl = NULL;
320 /* Now release LDAP control object */
325 * Display object to specified stream.
326 * \param ctl Control object to process.
327 * \param stream Output stream.
329 void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
330 cm_return_if_fail( ctl != NULL );
333 pthread_mutex_lock( ctl->mutexCtl );
334 fprintf( stream, "LdapControl:\n" );
335 fprintf( stream, "host name: '%s'\n", ctl->hostName?ctl->hostName:"null" );
336 fprintf( stream, " port: %d\n", ctl->port );
337 fprintf( stream, " base dn: '%s'\n", ctl->baseDN?ctl->baseDN:"null" );
338 fprintf( stream, " bind dn: '%s'\n", ctl->bindDN?ctl->bindDN:"null" );
339 pwd = passwd_store_get(PWS_CORE, "LDAP", ctl->hostName);
340 fprintf( stream, "bind pass: '%s'\n", pwd?pwd:"null" );
341 if (pwd != NULL && strlen(pwd) > 0)
342 memset(pwd, 0, strlen(pwd));
344 fprintf( stream, "attr mail: '%s'\n", ctl->attribEMail?ctl->attribEMail:"null" );
345 fprintf( stream, "attr comn: '%s'\n", ctl->attribCName?ctl->attribCName:"null" );
346 fprintf( stream, "attr frst: '%s'\n", ctl->attribFName?ctl->attribFName:"null" );
347 fprintf( stream, "attr last: '%s'\n", ctl->attribLName?ctl->attribLName:"null" );
348 fprintf( stream, "attr disn: '%s'\n", ctl->attribDName?ctl->attribDName:"null" );
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, " version: %d\n", ctl->version );
354 fprintf( stream, " STARTTLS: %s\n", ctl->enableTLS ? "yes" : "no" );
355 fprintf( stream, " SSL/TLS: %s\n", ctl->enableSSL ? "yes" : "no" );
356 fprintf( stream, "crit list:\n" );
357 if( ctl->listCriteria ) {
358 mgu_print_dlist( ctl->listCriteria, stream );
361 fprintf( stream, "\t!!!none!!!\n" );
363 pthread_mutex_unlock( ctl->mutexCtl );
367 * Copy member variables to specified object. Mutex lock object is
369 * \param ctlFrom Object to copy from.
370 * \param ctlTo Destination object.
372 void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
375 cm_return_if_fail( ctlFrom != NULL );
376 cm_return_if_fail( ctlTo != NULL );
378 debug_print("ldap controller copy\n");
379 /* Lock both objects */
380 pthread_mutex_lock( ctlFrom->mutexCtl );
381 pthread_mutex_lock( ctlTo->mutexCtl );
383 /* Clear our destination */
384 ldapctl_clear( ctlTo );
387 ctlTo->hostName = g_strdup( ctlFrom->hostName );
388 ctlTo->baseDN = g_strdup( ctlFrom->baseDN );
389 ctlTo->bindDN = g_strdup( ctlFrom->bindDN );
390 ctlTo->attribEMail = g_strdup( ctlFrom->attribEMail );
391 ctlTo->attribCName = g_strdup( ctlFrom->attribCName );
392 ctlTo->attribFName = g_strdup( ctlFrom->attribFName );
393 ctlTo->attribLName = g_strdup( ctlFrom->attribLName );
394 ctlTo->attribDName = g_strdup( ctlFrom->attribDName );
396 /* Copy search criteria */
397 node = ctlFrom->listCriteria;
399 ctlTo->listCriteria = g_list_append(
400 ctlTo->listCriteria, g_strdup( node->data ) );
401 node = g_list_next( node );
404 /* Copy other members */
405 ctlTo->port = ctlFrom->port;
406 ctlTo->maxEntries = ctlFrom->maxEntries;
407 ctlTo->timeOut = ctlFrom->timeOut;
408 ctlTo->maxQueryAge = ctlFrom->maxQueryAge;
409 ctlTo->matchingOption = ctlFrom->matchingOption;
410 ctlTo->version = ctlFrom->version;
411 ctlTo->enableTLS = ctlFrom->enableTLS;
412 ctlTo->enableSSL = ctlFrom->enableSSL;
415 pthread_mutex_unlock( ctlTo->mutexCtl );
416 pthread_mutex_unlock( ctlFrom->mutexCtl );
420 * Search criteria fragment - two terms - begin with (default).
422 static gchar *_criteria2BeginWith = "(&(givenName=%s*)(sn=%s*))";
425 * Search criteria fragment - two terms - contains.
427 static gchar *_criteria2Contains = "(&(givenName=*%s*)(sn=*%s*))";
430 * Create an LDAP search criteria by parsing specified search term. The search
431 * term may contain two names separated by the first embedded space found in
432 * the search term. It is assumed that the two tokens are first name and last
433 * name, or vice versa. An appropriate search criteria will be constructed.
435 * \param searchTerm Reference to search term to process.
436 * \param matchOption Set to the following:
438 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
439 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
442 * \return Formatted search criteria, or <code>NULL</code> if there is no
443 * embedded spaces. The search term should be g_free() when no
446 static gchar *ldapctl_build_ldap_criteria(
447 const gchar *searchTerm, const gint matchOption )
456 if( matchOption == LDAPCTL_MATCH_CONTAINS ) {
457 criteriaFmt = _criteria2Contains;
460 criteriaFmt = _criteria2BeginWith;
463 term = g_strdup( searchTerm );
466 /* Find first space character */
471 t2 = g_strdup( 1 + p );
478 /* Format search criteria */
482 p1 = g_strdup_printf( criteriaFmt, t1, t2 );
483 p2 = g_strdup_printf( criteriaFmt, t2, t1 );
484 crit = g_strdup_printf( "(&(|%s%s)(mail=*))", p1, p2 );
491 debug_print("search criteria: %s\n", crit?crit:"null");
497 * Search criteria fragment - single term - begin with (default).
499 static gchar *_criteriaBeginWith = "(%s=%s*)";
502 * Search criteria fragment - single term - contains.
504 static gchar *_criteriaContains = "(%s=*%s*)";
507 * Build a formatted LDAP search criteria string from criteria list.
508 * \param ctl Control object to process.
509 * \param searchVal Value to search for.
510 * \return Formatted string. Should be g_free() when done.
512 gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
514 gchar *p1, *p2, *retVal;
517 cm_return_val_if_fail( ctl != NULL, NULL );
518 cm_return_val_if_fail( searchVal != NULL, NULL );
520 /* Test whether there are more that one search terms */
521 retVal = ldapctl_build_ldap_criteria( searchVal, ctl->matchingOption );
522 if( retVal ) return retVal;
524 if( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) {
525 criteriaFmt = _criteriaContains;
528 criteriaFmt = _criteriaBeginWith;
531 /* No - just a simple search */
532 /* p1 contains previous formatted criteria */
533 /* p2 contains next formatted criteria */
534 retVal = p1 = p2 = NULL;
535 node = ctl->listCriteria;
539 node = g_list_next( node );
541 /* Switch pointers */
542 tmp = p1; p1 = p2; p2 = tmp;
545 /* Subsequent time through */
548 debug_print("crit: %s\n", searchVal);
549 /* fix bug when doing a search any */
550 if (strcmp("*@", searchVal) == 0) {
551 crit = g_strdup_printf( "(%s=*)", attr );
554 /* Format query criteria */
555 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 /* fix bug when doing a search any */
567 if (strcmp("*@", searchVal) == 0) {
568 p2 = g_strdup_printf( "(%s=*)", attr );
571 p2 = g_strdup_printf( criteriaFmt, attr, searchVal );
577 /* Nothing processed - format a default attribute */
578 retVal = g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL );
581 /* We have something - free up previous result */
585 debug_print("current search string: %s\n", retVal);
590 * Return array of pointers to attributes for LDAP query.
591 * \param ctl Control object to process.
592 * \return NULL terminated list.
594 char **ldapctl_attribute_array( LdapControl *ctl ) {
598 cm_return_val_if_fail( ctl != NULL, NULL );
600 node = ctl->listCriteria;
601 cnt = g_list_length( ctl->listCriteria );
602 ptrArray = g_new0( char *, 1 + cnt );
605 ptrArray[ i++ ] = node->data;
606 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
607 node = g_list_next( node );
609 ptrArray[ i ] = NULL;
614 * Return array of pointers to attributes for LDAP query.
615 * \param ctl Control object to process.
616 * \return NULL terminated list.
618 char **ldapctl_full_attribute_array( LdapControl *ctl ) {
623 cm_return_val_if_fail( ctl != NULL, NULL );
625 def = ctl->listCriteria;
627 tmp = g_list_append(tmp, g_strdup(def->data));
631 def = ldapctl_get_default_criteria_list();
635 if( g_list_find_custom(tmp, (gpointer)def->data,
636 (GCompareFunc)strcmp2) == NULL) {
637 tmp = g_list_append(tmp, g_strdup(node->data));
642 g_list_free_full(def, g_free);
645 cnt = g_list_length( tmp );
646 ptrArray = g_new0( char *, 1 + cnt);
649 ptrArray[ i++ ] = node->data;
650 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
651 node = g_list_next( node );
654 ptrArray[ i ] = NULL;
659 * Free array of pointers allocated by ldapctl_criteria_array().
660 * param ptrArray Array to clear.
662 void ldapctl_free_attribute_array( char **ptrArray ) {
665 /* Clear array to NULL's */
666 for( i = 0; ptrArray[i] != NULL; i++ ) {
674 * Parse LDAP search string, building list of LDAP criteria attributes. This
675 * may be used to convert an old style Sylpheed LDAP search criteria to the
676 * new format. The old style uses a standard LDAP search string, for example:
678 * (&(mail=*)(cn=%s*))
680 * This function extracts the two LDAP attributes <code>mail</code> and
681 * <code>cn</code>, adding each to a list.
683 * \param ctl Control object to process.
684 * \param criteria LDAP search criteria string.
686 void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
692 cm_return_if_fail( ctl != NULL );
694 ldapctl_criteria_list_clear( ctl );
695 if( criteria == NULL ) return;
706 attrib = g_strndup( pFrom, iLen );
707 g_strstrip( attrib );
708 ldapctl_criteria_list_add( ctl, attrib );
718 * Return the default LDAP search criteria string.
719 * \return Formatted string or <i>""</i>. Should be g_free() when done.
721 gchar *ldapctl_get_default_criteria() {
722 gchar *retVal = g_strdup(LDAPCTL_DFL_ATTR_LIST);
723 const gchar **attrs = ATTRIBUTE;
726 gchar *tmp = g_strdup_printf("%s, %s", retVal, *attrs++);
730 debug_print("default search criteria: %s\n", retVal);
735 * Return the default LDAP search criteria list.
736 * \return GList or <i>NULL</i>.
738 GList *ldapctl_get_default_criteria_list() {
739 gchar *criteria, *item;
740 gchar **c_list, **w_list;
741 GList *attr_list = NULL;
743 criteria = ldapctl_get_default_criteria();
744 c_list = g_strsplit(criteria, " ", 0);
748 while ((criteria = *w_list++) != 0) {
749 /* copy string elimination <,> */
751 item = g_strndup(criteria, strlen(criteria) - 1);
753 item = g_strdup(criteria);
754 debug_print("adding attribute to list: %s\n", item);
755 attr_list = g_list_append(attr_list, item);
762 * Compare to GList for equality.
763 * \param l1 First GList
764 * \param l2 Second GList
765 * \Return TRUE or FALSE
767 gboolean ldapctl_compare_list(GList *l1, GList *l2) {
768 gchar *first, *second;
771 if ((! l1 && l2) || (l1 && ! l2))
774 first = (gchar *) l1->data;
775 second = (gchar *) l2->data;
776 /*debug_print("comparing: %s = %s\n", first, second);*/
777 if ( ! (first && second) || strcmp(first, second) != 0) {
780 l1 = g_list_next(l1);
781 l2 = g_list_next(l2);
786 #endif /* USE_LDAP */