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"
39 * Create new LDAP control block object.
40 * \return Initialized control object.
42 LdapControl *ldapctl_create( void ) {
45 ctl = g_new0( LdapControl, 1 );
47 ctl->port = LDAPCTL_DFL_PORT;
51 ctl->listCriteria = NULL;
52 ctl->attribEMail = g_strdup( LDAPCTL_ATTR_EMAIL );
53 ctl->attribCName = g_strdup( LDAPCTL_ATTR_COMMONNAME );
54 ctl->attribFName = g_strdup( LDAPCTL_ATTR_GIVENNAME );
55 ctl->attribLName = g_strdup( LDAPCTL_ATTR_SURNAME );
56 ctl->attribDName = g_strdup( LDAPCTL_ATTR_DISPLAYNAME );
57 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
58 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
59 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
60 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
62 ctl->enableTLS = FALSE;
63 ctl->enableSSL = FALSE;
65 /* Mutex to protect control block */
66 ctl->mutexCtl = g_malloc0( sizeof( pthread_mutex_t ) );
67 pthread_mutex_init( ctl->mutexCtl, NULL );
73 * Specify hostname to be used.
74 * \param ctl Control object to process.
75 * \param value Host name.
77 void ldapctl_set_host( LdapControl* ctl, const gchar *value ) {
78 ctl->hostName = mgu_replace_string( ctl->hostName, value );
79 g_strstrip( ctl->hostName );
83 * Specify port to be used.
84 * \param ctl Control object to process.
87 void ldapctl_set_port( LdapControl* ctl, const gint value ) {
92 ctl->port = LDAPCTL_DFL_PORT;
97 * Specify base DN to be used.
98 * \param ctl Control object to process.
99 * \param value Base DN.
101 void ldapctl_set_base_dn( LdapControl* ctl, const gchar *value ) {
102 ctl->baseDN = mgu_replace_string( ctl->baseDN, value );
103 g_strstrip( ctl->baseDN );
107 * Specify bind DN to be used.
108 * \param ctl Control object to process.
109 * \param value Bind DN.
111 void ldapctl_set_bind_dn( LdapControl* ctl, const gchar *value ) {
112 ctl->bindDN = mgu_replace_string( ctl->bindDN, value );
113 g_strstrip( ctl->bindDN );
117 * Specify bind password to be used.
118 * \param ctl Control object to process.
119 * \param value Password.
121 void ldapctl_set_bind_password( LdapControl* ctl, const gchar *value ) {
122 ctl->bindPass = mgu_replace_string( ctl->bindPass, value );
123 g_strstrip( ctl->bindPass );
127 * Specify maximum number of entries to retrieve.
128 * \param ctl Control object to process.
129 * \param value Maximum entries.
131 void ldapctl_set_max_entries( LdapControl* ctl, const gint value ) {
133 ctl->maxEntries = value;
136 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
141 * Specify timeout value for LDAP operation (in seconds).
142 * \param ctl Control object to process.
143 * \param value Timeout.
145 void ldapctl_set_timeout( LdapControl* ctl, const gint value ) {
147 ctl->timeOut = value;
150 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
155 * Specify maximum age of query (in seconds) before query is retired.
156 * \param ctl Control object to process.
157 * \param value Maximum age.
159 void ldapctl_set_max_query_age( LdapControl* ctl, const gint value ) {
160 if( value > LDAPCTL_MAX_QUERY_AGE ) {
161 ctl->maxQueryAge = LDAPCTL_MAX_QUERY_AGE;
163 else if( value < 1 ) {
164 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
167 ctl->maxQueryAge = value;
172 * Specify matching option to be used for searches.
173 * \param ctl Control object to process.
174 * \param value Matching option, as follows:
176 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
177 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
180 void ldapctl_set_matching_option( LdapControl* ctl, const gint value ) {
181 if( value < LDAPCTL_MATCH_BEGINWITH ) {
182 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
184 else if( value > LDAPCTL_MATCH_CONTAINS ) {
185 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
188 ctl->matchingOption = value;
193 * Specify TLS option.
194 * \param ctl Control object to process.
195 * \param value <i>TRUE</i> to enable TLS.
197 void ldapctl_set_tls( LdapControl* ctl, const gboolean value ) {
198 ctl->enableTLS = value;
201 void ldapctl_set_ssl( LdapControl* ctl, const gboolean value ) {
202 ctl->enableSSL = value;
206 * Return search criteria list.
207 * \param ctl Control data object.
208 * \return Linked list of character strings containing LDAP attribute names to
209 * use for a search. This should not be modified directly. Use the
210 * <code>ldapctl_set_criteria_list()</code>,
211 * <code>ldapctl_criteria_list_clear()</code> and
212 * <code>ldapctl_criteria_list_add()</code> functions for this purpose.
214 GList *ldapctl_get_criteria_list( const LdapControl* ctl ) {
215 g_return_val_if_fail( ctl != NULL, NULL );
216 return ctl->listCriteria;
220 * Clear list of LDAP search attributes.
221 * \param ctl Control data object.
223 void ldapctl_criteria_list_clear( LdapControl *ctl ) {
224 g_return_if_fail( ctl != NULL );
225 mgu_free_dlist( ctl->listCriteria );
226 ctl->listCriteria = NULL;
230 * Add LDAP attribute to criteria list.
231 * \param ctl Control object to process.
232 * \param attr Attribute name to append. If not NULL and unique, a copy will
233 * be appended to the list.
235 void ldapctl_criteria_list_add( LdapControl *ctl, gchar *attr ) {
236 g_return_if_fail( ctl != NULL );
238 if( mgu_list_test_unq_nc( ctl->listCriteria, attr ) ) {
239 ctl->listCriteria = g_list_append(
240 ctl->listCriteria, g_strdup( attr ) );
246 * Clear LDAP server member variables.
247 * \param ctl Control object to clear.
249 static void ldapctl_clear( LdapControl *ctl ) {
250 g_return_if_fail( ctl != NULL );
252 /* Free internal stuff */
253 g_free( ctl->hostName );
254 g_free( ctl->baseDN );
255 g_free( ctl->bindDN );
256 g_free( ctl->bindPass );
257 g_free( ctl->attribEMail );
258 g_free( ctl->attribCName );
259 g_free( ctl->attribFName );
260 g_free( ctl->attribLName );
261 g_free( ctl->attribDName );
263 ldapctl_criteria_list_clear( ctl );
266 ctl->hostName = NULL;
270 ctl->bindPass = NULL;
271 ctl->attribEMail = NULL;
272 ctl->attribCName = NULL;
273 ctl->attribFName = NULL;
274 ctl->attribLName = NULL;
275 ctl->attribDName = NULL;
278 ctl->maxQueryAge = 0;
279 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
281 ctl->enableTLS = FALSE;
282 ctl->enableSSL = FALSE;
286 * Free up LDAP server interface object by releasing internal memory.
287 * \param ctl Control object to free.
289 void ldapctl_free( LdapControl *ctl ) {
290 g_return_if_fail( ctl != NULL );
292 /* Free internal stuff */
293 ldapctl_clear( ctl );
296 pthread_mutex_destroy( ctl->mutexCtl );
297 g_free( ctl->mutexCtl );
298 ctl->mutexCtl = NULL;
300 /* Now release LDAP control object */
305 * Display object to specified stream.
306 * \param ctl Control object to process.
307 * \param stream Output stream.
309 void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
310 g_return_if_fail( ctl != NULL );
312 pthread_mutex_lock( ctl->mutexCtl );
313 fprintf( stream, "LdapControl:\n" );
314 fprintf( stream, "host name: '%s'\n", ctl->hostName );
315 fprintf( stream, " port: %d\n", ctl->port );
316 fprintf( stream, " base dn: '%s'\n", ctl->baseDN );
317 fprintf( stream, " bind dn: '%s'\n", ctl->bindDN );
318 fprintf( stream, "bind pass: '%s'\n", ctl->bindPass );
319 fprintf( stream, "attr mail: '%s'\n", ctl->attribEMail );
320 fprintf( stream, "attr comn: '%s'\n", ctl->attribCName );
321 fprintf( stream, "attr frst: '%s'\n", ctl->attribFName );
322 fprintf( stream, "attr last: '%s'\n", ctl->attribLName );
323 fprintf( stream, "attr disn: '%s'\n", ctl->attribDName );
324 fprintf( stream, "max entry: %d\n", ctl->maxEntries );
325 fprintf( stream, " timeout: %d\n", ctl->timeOut );
326 fprintf( stream, " max age: %d\n", ctl->maxQueryAge );
327 fprintf( stream, "match opt: %d\n", ctl->matchingOption );
328 fprintf( stream, " version: %d\n", ctl->version );
329 fprintf( stream, " TLS: %s\n", ctl->enableTLS ? "yes" : "no" );
330 fprintf( stream, " SSL: %s\n", ctl->enableSSL ? "yes" : "no" );
331 fprintf( stream, "crit list:\n" );
332 if( ctl->listCriteria ) {
333 mgu_print_dlist( ctl->listCriteria, stream );
336 fprintf( stream, "\t!!!none!!!\n" );
338 pthread_mutex_unlock( ctl->mutexCtl );
342 * Copy member variables to specified object. Mutex lock object is
344 * \param ctlFrom Object to copy from.
345 * \param ctlTo Destination object.
347 void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
350 g_return_if_fail( ctlFrom != NULL );
351 g_return_if_fail( ctlTo != NULL );
353 /* Lock both objects */
354 pthread_mutex_lock( ctlFrom->mutexCtl );
355 pthread_mutex_lock( ctlTo->mutexCtl );
357 /* Clear our destination */
358 ldapctl_clear( ctlTo );
361 ctlTo->hostName = g_strdup( ctlFrom->hostName );
362 ctlTo->baseDN = g_strdup( ctlFrom->baseDN );
363 ctlTo->bindDN = g_strdup( ctlFrom->bindDN );
364 ctlTo->bindPass = g_strdup( ctlFrom->bindPass );
365 ctlTo->attribEMail = g_strdup( ctlFrom->attribEMail );
366 ctlTo->attribCName = g_strdup( ctlFrom->attribCName );
367 ctlTo->attribFName = g_strdup( ctlFrom->attribFName );
368 ctlTo->attribLName = g_strdup( ctlFrom->attribLName );
369 ctlTo->attribDName = g_strdup( ctlFrom->attribDName );
371 /* Copy search criteria */
372 node = ctlFrom->listCriteria;
374 ctlTo->listCriteria = g_list_append(
375 ctlTo->listCriteria, g_strdup( node->data ) );
376 node = g_list_next( node );
379 /* Copy other members */
380 ctlTo->port = ctlFrom->port;
381 ctlTo->maxEntries = ctlFrom->maxEntries;
382 ctlTo->timeOut = ctlFrom->timeOut;
383 ctlTo->maxQueryAge = ctlFrom->maxQueryAge;
384 ctlTo->matchingOption = ctlFrom->matchingOption;
385 ctlTo->version = ctlFrom->version;
386 ctlTo->enableTLS = ctlFrom->enableTLS;
387 ctlTo->enableSSL = ctlFrom->enableSSL;
390 pthread_mutex_unlock( ctlTo->mutexCtl );
391 pthread_mutex_unlock( ctlFrom->mutexCtl );
395 * Search criteria fragment - two terms - begin with (default).
397 static gchar *_criteria2BeginWith = "(&(givenName=%s*)(sn=%s*))";
400 * Search criteria fragment - two terms - contains.
402 static gchar *_criteria2Contains = "(&(givenName=*%s*)(sn=*%s*))";
405 * Create an LDAP search criteria by parsing specified search term. The search
406 * term may contain two names separated by the first embedded space found in
407 * the search term. It is assumed that the two tokens are first name and last
408 * name, or vice versa. An appropriate search criteria will be constructed.
410 * \param searchTerm Reference to search term to process.
411 * \param matchOption Set to the following:
413 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
414 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
417 * \return Formatted search criteria, or <code>NULL</code> if there is no
418 * embedded spaces. The search term should be g_free() when no
421 static gchar *ldapctl_build_ldap_criteria(
422 const gchar *searchTerm, const gint matchOption )
431 if( matchOption == LDAPCTL_MATCH_CONTAINS ) {
432 criteriaFmt = _criteria2Contains;
435 criteriaFmt = _criteria2BeginWith;
438 term = g_strdup( searchTerm );
441 /* Find first space character */
446 t2 = g_strdup( 1 + p );
453 /* Format search criteria */
457 p1 = g_strdup_printf( criteriaFmt, t1, t2 );
458 p2 = g_strdup_printf( criteriaFmt, t2, t1 );
459 crit = g_strdup_printf( "(&(|%s%s)(mail=*))", p1, p2 );
471 * Search criteria fragment - single term - begin with (default).
473 static gchar *_criteriaBeginWith = "(%s=%s*)";
476 * Search criteria fragment - single term - contains.
478 static gchar *_criteriaContains = "(%s=*%s*)";
481 * Build a formatted LDAP search criteria string from criteria list.
482 * \param ctl Control object to process.
483 * \param searchVal Value to search for.
484 * \return Formatted string. Should be g_free() when done.
486 gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
488 gchar *p1, *p2, *retVal;
491 g_return_val_if_fail( ctl != NULL, NULL );
492 g_return_val_if_fail( searchVal != NULL, NULL );
494 /* Test whether there are more that one search terms */
495 retVal = ldapctl_build_ldap_criteria( searchVal, ctl->matchingOption );
496 if( retVal ) return retVal;
498 if( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) {
499 criteriaFmt = _criteriaContains;
502 criteriaFmt = _criteriaBeginWith;
505 /* No - just a simple search */
506 /* p1 contains previous formatted criteria */
507 /* p2 contains next formatted criteria */
508 retVal = p1 = p2 = NULL;
509 node = ctl->listCriteria;
514 node = g_list_next( node );
516 /* Switch pointers */
517 tmp = p1; p1 = p2; p2 = tmp;
520 /* Subsequent time through */
523 /* Format query criteria */
524 crit = g_strdup_printf( criteriaFmt, attr, searchVal );
526 /* Append to existing criteria */
528 p2 = g_strdup_printf( "(|%s%s)", p1, crit );
533 /* First time through - Format query criteria */
534 p2 = g_strdup_printf( criteriaFmt, attr, searchVal );
539 /* Nothing processed - format a default attribute */
540 retVal = g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL );
543 /* We have something - free up previous result */
551 * Return array of pointers to attributes for LDAP query.
552 * \param ctl Control object to process.
553 * \return NULL terminated list.
555 char **ldapctl_attribute_array( LdapControl *ctl ) {
559 g_return_val_if_fail( ctl != NULL, NULL );
561 def = ldapctl_get_default_criteria_list();
562 /* check if this servers config is updated to the new
563 * default list of search criteria. If not update the list */
564 if (! ldapctl_compare_list(ctl->listCriteria, def)) {
565 /* Deep copy search criteria */
566 ldapctl_criteria_list_clear(ctl);
568 ctl->listCriteria = g_list_append(
569 ctl->listCriteria, g_strdup(def->data));
570 def = g_list_next(def);
573 node = ctl->listCriteria;
574 cnt = g_list_length( ctl->listCriteria );
575 ptrArray = g_new0( char *, 1 + cnt );
578 ptrArray[ i++ ] = node->data;
579 node = g_list_next( node );
581 ptrArray[ i ] = NULL;
586 * Free array of pointers allocated by ldapctl_criteria_array().
587 * param ptrArray Array to clear.
589 void ldapctl_free_attribute_array( char **ptrArray ) {
592 /* Clear array to NULL's */
593 for( i = 0; ptrArray[i] != NULL; i++ ) {
600 * Parse LDAP search string, building list of LDAP criteria attributes. This
601 * may be used to convert an old style Sylpheed LDAP search criteria to the
602 * new format. The old style uses a standard LDAP search string, for example:
604 * (&(mail=*)(cn=%s*))
606 * This function extracts the two LDAP attributes <code>mail</code> and
607 * <code>cn</code>, adding each to a list.
609 * \param ctl Control object to process.
610 * \param criteria LDAP search criteria string.
612 void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
618 g_return_if_fail( ctl != NULL );
620 ldapctl_criteria_list_clear( ctl );
621 if( criteria == NULL ) return;
632 attrib = g_strndup( pFrom, iLen );
633 g_strstrip( attrib );
634 ldapctl_criteria_list_add( ctl, attrib );
644 * Return the default LDAP search criteria string.
645 * \return Formatted string or <i>""</i>. Should be g_free() when done.
647 gchar *ldapctl_get_default_criteria() {
648 gchar *retVal = LDAPCTL_DFL_ATTR_LIST;
649 const gchar **attrs = ATTRIBUTE;
652 retVal = g_strdup_printf("%s, %s", retVal, *attrs++);
658 * Return the default LDAP search criteria list.
659 * \return GList or <i>NULL</i>.
661 GList *ldapctl_get_default_criteria_list() {
662 gchar *criteria, *item;
663 gchar **c_list, **w_list;
664 GList *attr_list = NULL;
666 criteria = ldapctl_get_default_criteria();
667 c_list = g_strsplit(criteria, " ", 0);
671 while ((criteria = *w_list++) != 0) {
672 /* copy string elimination <,> */
674 item = g_strndup(criteria, strlen(criteria) - 1);
676 item = g_strdup(criteria);
677 attr_list = g_list_append(attr_list, g_strdup(item));
685 * Compare to GList for equality.
686 * \param l1 First GList
687 * \param l2 Second GList
688 * \Return TRUE or FALSE
690 gboolean ldapctl_compare_list(GList *l1, GList *l2) {
691 gchar *first, *second;
694 if ((! l1 && l2) || (l1 && ! l2))
697 first = (gchar *) l1->data;
698 second = (gchar *) l2->data;
699 if ( ! (first && second) || strcmp(first, second) != 0) {
702 l1 = g_list_next(l1);
703 l2 = g_list_next(l2);
708 #endif /* USE_LDAP */