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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * Functions for LDAP control data.
38 * Create new LDAP control block object.
39 * \return Initialized control object.
41 LdapControl *ldapctl_create( void ) {
44 ctl = g_new0( LdapControl, 1 );
46 ctl->port = LDAPCTL_DFL_PORT;
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;
60 ctl->enableTLS = FALSE;
61 ctl->enableSSL = FALSE;
63 /* Mutex to protect control block */
64 ctl->mutexCtl = g_malloc0( sizeof( pthread_mutex_t ) );
65 pthread_mutex_init( ctl->mutexCtl, NULL );
71 * Specify hostname to be used.
72 * \param ctl Control object to process.
73 * \param value Host name.
75 void ldapctl_set_host( LdapControl* ctl, const gchar *value ) {
76 ctl->hostName = mgu_replace_string( ctl->hostName, value );
77 g_strstrip( ctl->hostName );
81 * Specify port to be used.
82 * \param ctl Control object to process.
85 void ldapctl_set_port( LdapControl* ctl, const gint value ) {
90 ctl->port = LDAPCTL_DFL_PORT;
95 * Specify base DN to be used.
96 * \param ctl Control object to process.
97 * \param value Base DN.
99 void ldapctl_set_base_dn( LdapControl* ctl, const gchar *value ) {
100 ctl->baseDN = mgu_replace_string( ctl->baseDN, value );
101 g_strstrip( ctl->baseDN );
105 * Specify bind DN to be used.
106 * \param ctl Control object to process.
107 * \param value Bind DN.
109 void ldapctl_set_bind_dn( LdapControl* ctl, const gchar *value ) {
110 ctl->bindDN = mgu_replace_string( ctl->bindDN, value );
111 g_strstrip( ctl->bindDN );
115 * Specify bind password to be used.
116 * \param ctl Control object to process.
117 * \param value Password.
119 void ldapctl_set_bind_password( LdapControl* ctl, const gchar *value ) {
120 ctl->bindPass = mgu_replace_string( ctl->bindPass, value );
121 g_strstrip( ctl->bindPass );
125 * Specify maximum number of entries to retrieve.
126 * \param ctl Control object to process.
127 * \param value Maximum entries.
129 void ldapctl_set_max_entries( LdapControl* ctl, const gint value ) {
131 ctl->maxEntries = value;
134 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
139 * Specify timeout value for LDAP operation (in seconds).
140 * \param ctl Control object to process.
141 * \param value Timeout.
143 void ldapctl_set_timeout( LdapControl* ctl, const gint value ) {
145 ctl->timeOut = value;
148 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
153 * Specify maximum age of query (in seconds) before query is retired.
154 * \param ctl Control object to process.
155 * \param value Maximum age.
157 void ldapctl_set_max_query_age( LdapControl* ctl, const gint value ) {
158 if( value > LDAPCTL_MAX_QUERY_AGE ) {
159 ctl->maxQueryAge = LDAPCTL_MAX_QUERY_AGE;
161 else if( value < 1 ) {
162 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
165 ctl->maxQueryAge = value;
170 * Specify matching option to be used for searches.
171 * \param ctl Control object to process.
172 * \param value Matching option, as follows:
174 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
175 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
178 void ldapctl_set_matching_option( LdapControl* ctl, const gint value ) {
179 if( value < LDAPCTL_MATCH_BEGINWITH ) {
180 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
182 else if( value > LDAPCTL_MATCH_CONTAINS ) {
183 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
186 ctl->matchingOption = value;
191 * Specify TLS option.
192 * \param ctl Control object to process.
193 * \param value <i>TRUE</i> to enable TLS.
195 void ldapctl_set_tls( LdapControl* ctl, const gboolean value ) {
196 ctl->enableTLS = value;
199 void ldapctl_set_ssl( LdapControl* ctl, const gboolean value ) {
200 ctl->enableSSL = value;
204 * Specify search criteria list to be used.
205 * \param ctl Control data object.
206 * \param value Linked list of LDAP attribute names to use for search.
208 void ldapctl_set_criteria_list( LdapControl* ctl, GList *value ) {
209 g_return_if_fail( ctl != NULL );
210 mgu_free_dlist( ctl->listCriteria );
211 ctl->listCriteria = value;
215 * Return search criteria list.
216 * \param ctl Control data object.
217 * \return Linked list of character strings containing LDAP attribute names to
218 * use for a search. This should not be modified directly. Use the
219 * <code>ldapctl_set_criteria_list()</code>,
220 * <code>ldapctl_criteria_list_clear()</code> and
221 * <code>ldapctl_criteria_list_add()</code> functions for this purpose.
223 GList *ldapctl_get_criteria_list( const LdapControl* ctl ) {
224 g_return_val_if_fail( ctl != NULL, NULL );
225 return ctl->listCriteria;
229 * Clear list of LDAP search attributes.
230 * \param ctl Control data object.
232 void ldapctl_criteria_list_clear( LdapControl *ctl ) {
233 g_return_if_fail( ctl != NULL );
234 mgu_free_dlist( ctl->listCriteria );
235 ctl->listCriteria = NULL;
239 * Add LDAP attribute to criteria list.
240 * \param ctl Control object to process.
241 * \param attr Attribute name to append. If not NULL and unique, a copy will
242 * be appended to the list.
244 void ldapctl_criteria_list_add( LdapControl *ctl, gchar *attr ) {
245 g_return_if_fail( ctl != NULL );
247 if( mgu_list_test_unq_nc( ctl->listCriteria, attr ) ) {
248 ctl->listCriteria = g_list_append(
249 ctl->listCriteria, g_strdup( attr ) );
255 * Build criteria list using default attributes.
256 * \param ctl Control object to process.
258 void ldapctl_default_attributes( LdapControl *ctl ) {
259 g_return_if_fail( ctl != NULL );
261 ldapctl_criteria_list_clear( ctl );
262 ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_COMMONNAME );
263 ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_GIVENNAME );
264 ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_SURNAME );
265 ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_EMAIL );
269 * Clear LDAP server member variables.
270 * \param ctl Control object to clear.
272 void ldapctl_clear( LdapControl *ctl ) {
273 g_return_if_fail( ctl != NULL );
275 /* Free internal stuff */
276 g_free( ctl->hostName );
277 g_free( ctl->baseDN );
278 g_free( ctl->bindDN );
279 g_free( ctl->bindPass );
280 g_free( ctl->attribEMail );
281 g_free( ctl->attribCName );
282 g_free( ctl->attribFName );
283 g_free( ctl->attribLName );
285 ldapctl_criteria_list_clear( ctl );
288 ctl->hostName = NULL;
292 ctl->bindPass = NULL;
293 ctl->attribEMail = NULL;
294 ctl->attribCName = NULL;
295 ctl->attribFName = NULL;
296 ctl->attribLName = NULL;
299 ctl->maxQueryAge = 0;
300 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
302 ctl->enableTLS = FALSE;
303 ctl->enableSSL = FALSE;
307 * Free up LDAP server interface object by releasing internal memory.
308 * \param ctl Control object to free.
310 void ldapctl_free( LdapControl *ctl ) {
311 g_return_if_fail( ctl != NULL );
313 /* Free internal stuff */
314 ldapctl_clear( ctl );
317 pthread_mutex_destroy( ctl->mutexCtl );
318 g_free( ctl->mutexCtl );
319 ctl->mutexCtl = NULL;
321 /* Now release LDAP control object */
326 * Setup default (empty) values for specified object.
327 * \param ctl Control object to process.
329 void ldapctl_default_values( LdapControl *ctl ) {
330 g_return_if_fail( ctl != NULL );
332 /* Clear our destination */
333 ldapctl_clear( ctl );
336 ctl->hostName = g_strdup( "" );
337 ctl->baseDN = g_strdup( "" );
338 ctl->bindDN = g_strdup( "" );
339 ctl->bindPass = g_strdup( "" );
340 ctl->port = LDAPCTL_DFL_PORT;
341 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
342 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
343 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
344 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
346 ctl->enableTLS = FALSE;
347 ctl->enableSSL = FALSE;
349 ldapctl_default_attributes( ctl );
353 * Display object to specified stream.
354 * \param ctl Control object to process.
355 * \param stream Output stream.
357 void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
358 g_return_if_fail( ctl != NULL );
360 pthread_mutex_lock( ctl->mutexCtl );
361 fprintf( stream, "LdapControl:\n" );
362 fprintf( stream, "host name: '%s'\n", ctl->hostName );
363 fprintf( stream, " port: %d\n", ctl->port );
364 fprintf( stream, " base dn: '%s'\n", ctl->baseDN );
365 fprintf( stream, " bind dn: '%s'\n", ctl->bindDN );
366 fprintf( stream, "bind pass: '%s'\n", ctl->bindPass );
367 fprintf( stream, "attr mail: '%s'\n", ctl->attribEMail );
368 fprintf( stream, "attr comn: '%s'\n", ctl->attribCName );
369 fprintf( stream, "attr frst: '%s'\n", ctl->attribFName );
370 fprintf( stream, "attr last: '%s'\n", ctl->attribLName );
371 fprintf( stream, "max entry: %d\n", ctl->maxEntries );
372 fprintf( stream, " timeout: %d\n", ctl->timeOut );
373 fprintf( stream, " max age: %d\n", ctl->maxQueryAge );
374 fprintf( stream, "match opt: %d\n", ctl->matchingOption );
375 fprintf( stream, " version: %d\n", ctl->version );
376 fprintf( stream, " TLS: %s\n", ctl->enableTLS ? "yes" : "no" );
377 fprintf( stream, " SSL: %s\n", ctl->enableSSL ? "yes" : "no" );
378 fprintf( stream, "crit list:\n" );
379 if( ctl->listCriteria ) {
380 mgu_print_dlist( ctl->listCriteria, stream );
383 fprintf( stream, "\t!!!none!!!\n" );
385 pthread_mutex_unlock( ctl->mutexCtl );
389 * Copy member variables to specified object. Mutex lock object is
391 * \param ctlFrom Object to copy from.
392 * \param ctlTo Destination object.
394 void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
397 g_return_if_fail( ctlFrom != NULL );
398 g_return_if_fail( ctlTo != NULL );
400 /* Lock both objects */
401 pthread_mutex_lock( ctlFrom->mutexCtl );
402 pthread_mutex_lock( ctlTo->mutexCtl );
404 /* Clear our destination */
405 ldapctl_clear( ctlTo );
408 ctlTo->hostName = g_strdup( ctlFrom->hostName );
409 ctlTo->baseDN = g_strdup( ctlFrom->baseDN );
410 ctlTo->bindDN = g_strdup( ctlFrom->bindDN );
411 ctlTo->bindPass = g_strdup( ctlFrom->bindPass );
412 ctlTo->attribEMail = g_strdup( ctlFrom->attribEMail );
413 ctlTo->attribCName = g_strdup( ctlFrom->attribCName );
414 ctlTo->attribFName = g_strdup( ctlFrom->attribFName );
415 ctlTo->attribLName = g_strdup( ctlFrom->attribLName );
417 /* Copy search criteria */
418 node = ctlFrom->listCriteria;
420 ctlTo->listCriteria = g_list_append(
421 ctlTo->listCriteria, g_strdup( node->data ) );
422 node = g_list_next( node );
425 /* Copy other members */
426 ctlTo->port = ctlFrom->port;
427 ctlTo->maxEntries = ctlFrom->maxEntries;
428 ctlTo->timeOut = ctlFrom->timeOut;
429 ctlTo->maxQueryAge = ctlFrom->maxQueryAge;
430 ctlTo->matchingOption = ctlFrom->matchingOption;
431 ctlTo->version = ctlFrom->version;
432 ctlTo->enableTLS = ctlFrom->enableTLS;
433 ctlTo->enableSSL = ctlFrom->enableSSL;
436 pthread_mutex_unlock( ctlTo->mutexCtl );
437 pthread_mutex_unlock( ctlFrom->mutexCtl );
441 * Search criteria fragment - two terms - begin with (default).
443 static gchar *_criteria2BeginWith = "(&(givenName=%s*)(sn=%s*))";
446 * Search criteria fragment - two terms - contains.
448 static gchar *_criteria2Contains = "(&(givenName=*%s*)(sn=*%s*))";
451 * Create an LDAP search criteria by parsing specified search term. The search
452 * term may contain two names separated by the first embedded space found in
453 * the search term. It is assumed that the two tokens are first name and last
454 * name, or vice versa. An appropriate search criteria will be constructed.
456 * \param searchTerm Reference to search term to process.
457 * \param matchOption Set to the following:
459 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
460 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
463 * \return Formatted search criteria, or <code>NULL</code> if there is no
464 * embedded spaces. The search term should be g_free() when no
467 static gchar *ldapctl_build_ldap_criteria(
468 const gchar *searchTerm, const gint matchOption )
477 if( matchOption == LDAPCTL_MATCH_CONTAINS ) {
478 criteriaFmt = _criteria2Contains;
481 criteriaFmt = _criteria2BeginWith;
484 term = g_strdup( searchTerm );
487 /* Find first space character */
492 t2 = g_strdup( 1 + p );
499 /* Format search criteria */
503 p1 = g_strdup_printf( criteriaFmt, t1, t2 );
504 p2 = g_strdup_printf( criteriaFmt, t2, t1 );
505 crit = g_strdup_printf( "(&(|%s%s)(mail=*))", p1, p2 );
517 * Search criteria fragment - single term - begin with (default).
519 static gchar *_criteriaBeginWith = "(%s=%s*)";
522 * Search criteria fragment - single term - contains.
524 static gchar *_criteriaContains = "(%s=*%s*)";
527 * Build a formatted LDAP search criteria string from criteria list.
528 * \param ctl Control object to process.
529 * \param searchVal Value to search for.
530 * \return Formatted string. Should be g_free() when done.
532 gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
534 gchar *p1, *p2, *retVal;
537 g_return_val_if_fail( ctl != NULL, NULL );
538 g_return_val_if_fail( searchVal != NULL, NULL );
540 /* Test whether there are more that one search terms */
541 retVal = ldapctl_build_ldap_criteria( searchVal, ctl->matchingOption );
542 if( retVal ) return retVal;
544 if( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) {
545 criteriaFmt = _criteriaContains;
548 criteriaFmt = _criteriaBeginWith;
551 /* No - just a simple search */
552 /* p1 contains previous formatted criteria */
553 /* p2 contains next formatted criteria */
554 retVal = p1 = p2 = NULL;
555 node = ctl->listCriteria;
560 node = g_list_next( node );
562 /* Switch pointers */
563 tmp = p1; p1 = p2; p2 = tmp;
566 /* Subsequent time through */
569 /* Format query criteria */
570 crit = g_strdup_printf( criteriaFmt, attr, searchVal );
572 /* Append to existing criteria */
574 p2 = g_strdup_printf( "(|%s%s)", p1, crit );
579 /* First time through - Format query criteria */
580 p2 = g_strdup_printf( criteriaFmt, attr, searchVal );
585 /* Nothing processed - format a default attribute */
586 retVal = g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL );
589 /* We have something - free up previous result */
597 * Return array of pointers to attributes for LDAP query.
598 * \param ctl Control object to process.
599 * \return NULL terminated list.
601 char **ldapctl_attribute_array( LdapControl *ctl ) {
605 g_return_val_if_fail( ctl != NULL, NULL );
607 cnt = g_list_length( ctl->listCriteria );
608 ptrArray = g_new0( char *, 1 + cnt );
610 node = ctl->listCriteria;
612 ptrArray[ i++ ] = node->data;
613 node = g_list_next( node );
615 ptrArray[ i ] = NULL;
620 * Free array of pointers allocated by ldapctl_criteria_array().
621 * param ptrArray Array to clear.
623 void ldapctl_free_attribute_array( char **ptrArray ) {
626 /* Clear array to NULL's */
627 for( i = 0; ptrArray[i] != NULL; i++ ) {
634 * Parse LDAP search string, building list of LDAP criteria attributes. This
635 * may be used to convert an old style Sylpheed LDAP search criteria to the
636 * new format. The old style uses a standard LDAP search string, for example:
638 * (&(mail=*)(cn=%s*))
640 * This function extracts the two LDAP attributes <code>mail</code> and
641 * <code>cn</code>, adding each to a list.
643 * \param ctl Control object to process.
644 * \param criteria LDAP search criteria string.
646 void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
652 g_return_if_fail( ctl != NULL );
654 ldapctl_criteria_list_clear( ctl );
655 if( criteria == NULL ) return;
666 attrib = g_strndup( pFrom, iLen );
667 g_strstrip( attrib );
668 ldapctl_criteria_list_add( ctl, attrib );
677 #endif /* USE_LDAP */