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
->attribDName
= g_strdup( LDAPCTL_ATTR_DISPLAYNAME
);
56 ctl
->maxEntries
= LDAPCTL_MAX_ENTRIES
;
57 ctl
->timeOut
= LDAPCTL_DFL_TIMEOUT
;
58 ctl
->maxQueryAge
= LDAPCTL_DFL_QUERY_AGE
;
59 ctl
->matchingOption
= LDAPCTL_MATCH_BEGINWITH
;
61 ctl
->enableTLS
= FALSE
;
62 ctl
->enableSSL
= FALSE
;
64 /* Mutex to protect control block */
65 ctl
->mutexCtl
= g_malloc0( sizeof( pthread_mutex_t
) );
66 pthread_mutex_init( ctl
->mutexCtl
, NULL
);
72 * Specify hostname to be used.
73 * \param ctl Control object to process.
74 * \param value Host name.
76 void ldapctl_set_host( LdapControl
* ctl
, const gchar
*value
) {
77 ctl
->hostName
= mgu_replace_string( ctl
->hostName
, value
);
78 g_strstrip( ctl
->hostName
);
82 * Specify port to be used.
83 * \param ctl Control object to process.
86 void ldapctl_set_port( LdapControl
* ctl
, const gint value
) {
91 ctl
->port
= LDAPCTL_DFL_PORT
;
96 * Specify base DN to be used.
97 * \param ctl Control object to process.
98 * \param value Base DN.
100 void ldapctl_set_base_dn( LdapControl
* ctl
, const gchar
*value
) {
101 ctl
->baseDN
= mgu_replace_string( ctl
->baseDN
, value
);
102 g_strstrip( ctl
->baseDN
);
106 * Specify bind DN to be used.
107 * \param ctl Control object to process.
108 * \param value Bind DN.
110 void ldapctl_set_bind_dn( LdapControl
* ctl
, const gchar
*value
) {
111 ctl
->bindDN
= mgu_replace_string( ctl
->bindDN
, value
);
112 g_strstrip( ctl
->bindDN
);
116 * Specify bind password to be used.
117 * \param ctl Control object to process.
118 * \param value Password.
120 void ldapctl_set_bind_password( LdapControl
* ctl
, const gchar
*value
) {
121 ctl
->bindPass
= mgu_replace_string( ctl
->bindPass
, value
);
122 g_strstrip( ctl
->bindPass
);
126 * Specify maximum number of entries to retrieve.
127 * \param ctl Control object to process.
128 * \param value Maximum entries.
130 void ldapctl_set_max_entries( LdapControl
* ctl
, const gint value
) {
132 ctl
->maxEntries
= value
;
135 ctl
->maxEntries
= LDAPCTL_MAX_ENTRIES
;
140 * Specify timeout value for LDAP operation (in seconds).
141 * \param ctl Control object to process.
142 * \param value Timeout.
144 void ldapctl_set_timeout( LdapControl
* ctl
, const gint value
) {
146 ctl
->timeOut
= value
;
149 ctl
->timeOut
= LDAPCTL_DFL_TIMEOUT
;
154 * Specify maximum age of query (in seconds) before query is retired.
155 * \param ctl Control object to process.
156 * \param value Maximum age.
158 void ldapctl_set_max_query_age( LdapControl
* ctl
, const gint value
) {
159 if( value
> LDAPCTL_MAX_QUERY_AGE
) {
160 ctl
->maxQueryAge
= LDAPCTL_MAX_QUERY_AGE
;
162 else if( value
< 1 ) {
163 ctl
->maxQueryAge
= LDAPCTL_DFL_QUERY_AGE
;
166 ctl
->maxQueryAge
= value
;
171 * Specify matching option to be used for searches.
172 * \param ctl Control object to process.
173 * \param value Matching option, as follows:
175 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
176 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
179 void ldapctl_set_matching_option( LdapControl
* ctl
, const gint value
) {
180 if( value
< LDAPCTL_MATCH_BEGINWITH
) {
181 ctl
->matchingOption
= LDAPCTL_MATCH_BEGINWITH
;
183 else if( value
> LDAPCTL_MATCH_CONTAINS
) {
184 ctl
->matchingOption
= LDAPCTL_MATCH_BEGINWITH
;
187 ctl
->matchingOption
= value
;
192 * Specify TLS option.
193 * \param ctl Control object to process.
194 * \param value <i>TRUE</i> to enable TLS.
196 void ldapctl_set_tls( LdapControl
* ctl
, const gboolean value
) {
197 ctl
->enableTLS
= value
;
200 void ldapctl_set_ssl( LdapControl
* ctl
, const gboolean value
) {
201 ctl
->enableSSL
= value
;
205 * Return search criteria list.
206 * \param ctl Control data object.
207 * \return Linked list of character strings containing LDAP attribute names to
208 * use for a search. This should not be modified directly. Use the
209 * <code>ldapctl_set_criteria_list()</code>,
210 * <code>ldapctl_criteria_list_clear()</code> and
211 * <code>ldapctl_criteria_list_add()</code> functions for this purpose.
213 GList
*ldapctl_get_criteria_list( const LdapControl
* ctl
) {
214 g_return_val_if_fail( ctl
!= NULL
, NULL
);
215 return ctl
->listCriteria
;
219 * Clear list of LDAP search attributes.
220 * \param ctl Control data object.
222 void ldapctl_criteria_list_clear( LdapControl
*ctl
) {
223 g_return_if_fail( ctl
!= NULL
);
224 mgu_free_dlist( ctl
->listCriteria
);
225 ctl
->listCriteria
= NULL
;
229 * Add LDAP attribute to criteria list.
230 * \param ctl Control object to process.
231 * \param attr Attribute name to append. If not NULL and unique, a copy will
232 * be appended to the list.
234 void ldapctl_criteria_list_add( LdapControl
*ctl
, gchar
*attr
) {
235 g_return_if_fail( ctl
!= NULL
);
237 if( mgu_list_test_unq_nc( ctl
->listCriteria
, attr
) ) {
238 ctl
->listCriteria
= g_list_append(
239 ctl
->listCriteria
, g_strdup( attr
) );
245 * Clear LDAP server member variables.
246 * \param ctl Control object to clear.
248 static void ldapctl_clear( LdapControl
*ctl
) {
249 g_return_if_fail( ctl
!= NULL
);
251 /* Free internal stuff */
252 g_free( ctl
->hostName
);
253 g_free( ctl
->baseDN
);
254 g_free( ctl
->bindDN
);
255 g_free( ctl
->bindPass
);
256 g_free( ctl
->attribEMail
);
257 g_free( ctl
->attribCName
);
258 g_free( ctl
->attribFName
);
259 g_free( ctl
->attribLName
);
260 g_free( ctl
->attribDName
);
262 ldapctl_criteria_list_clear( ctl
);
265 ctl
->hostName
= NULL
;
269 ctl
->bindPass
= NULL
;
270 ctl
->attribEMail
= NULL
;
271 ctl
->attribCName
= NULL
;
272 ctl
->attribFName
= NULL
;
273 ctl
->attribLName
= NULL
;
274 ctl
->attribDName
= NULL
;
277 ctl
->maxQueryAge
= 0;
278 ctl
->matchingOption
= LDAPCTL_MATCH_BEGINWITH
;
280 ctl
->enableTLS
= FALSE
;
281 ctl
->enableSSL
= FALSE
;
285 * Free up LDAP server interface object by releasing internal memory.
286 * \param ctl Control object to free.
288 void ldapctl_free( LdapControl
*ctl
) {
289 g_return_if_fail( ctl
!= NULL
);
291 /* Free internal stuff */
292 ldapctl_clear( ctl
);
295 pthread_mutex_destroy( ctl
->mutexCtl
);
296 g_free( ctl
->mutexCtl
);
297 ctl
->mutexCtl
= NULL
;
299 /* Now release LDAP control object */
304 * Display object to specified stream.
305 * \param ctl Control object to process.
306 * \param stream Output stream.
308 void ldapctl_print( const LdapControl
*ctl
, FILE *stream
) {
309 g_return_if_fail( ctl
!= NULL
);
311 pthread_mutex_lock( ctl
->mutexCtl
);
312 fprintf( stream
, "LdapControl:\n" );
313 fprintf( stream
, "host name: '%s'\n", ctl
->hostName
);
314 fprintf( stream
, " port: %d\n", ctl
->port
);
315 fprintf( stream
, " base dn: '%s'\n", ctl
->baseDN
);
316 fprintf( stream
, " bind dn: '%s'\n", ctl
->bindDN
);
317 fprintf( stream
, "bind pass: '%s'\n", ctl
->bindPass
);
318 fprintf( stream
, "attr mail: '%s'\n", ctl
->attribEMail
);
319 fprintf( stream
, "attr comn: '%s'\n", ctl
->attribCName
);
320 fprintf( stream
, "attr frst: '%s'\n", ctl
->attribFName
);
321 fprintf( stream
, "attr last: '%s'\n", ctl
->attribLName
);
322 fprintf( stream
, "attr disn: '%s'\n", ctl
->attribDName
);
323 fprintf( stream
, "max entry: %d\n", ctl
->maxEntries
);
324 fprintf( stream
, " timeout: %d\n", ctl
->timeOut
);
325 fprintf( stream
, " max age: %d\n", ctl
->maxQueryAge
);
326 fprintf( stream
, "match opt: %d\n", ctl
->matchingOption
);
327 fprintf( stream
, " version: %d\n", ctl
->version
);
328 fprintf( stream
, " TLS: %s\n", ctl
->enableTLS
? "yes" : "no" );
329 fprintf( stream
, " SSL: %s\n", ctl
->enableSSL
? "yes" : "no" );
330 fprintf( stream
, "crit list:\n" );
331 if( ctl
->listCriteria
) {
332 mgu_print_dlist( ctl
->listCriteria
, stream
);
335 fprintf( stream
, "\t!!!none!!!\n" );
337 pthread_mutex_unlock( ctl
->mutexCtl
);
341 * Copy member variables to specified object. Mutex lock object is
343 * \param ctlFrom Object to copy from.
344 * \param ctlTo Destination object.
346 void ldapctl_copy( const LdapControl
*ctlFrom
, LdapControl
*ctlTo
) {
349 g_return_if_fail( ctlFrom
!= NULL
);
350 g_return_if_fail( ctlTo
!= NULL
);
352 /* Lock both objects */
353 pthread_mutex_lock( ctlFrom
->mutexCtl
);
354 pthread_mutex_lock( ctlTo
->mutexCtl
);
356 /* Clear our destination */
357 ldapctl_clear( ctlTo
);
360 ctlTo
->hostName
= g_strdup( ctlFrom
->hostName
);
361 ctlTo
->baseDN
= g_strdup( ctlFrom
->baseDN
);
362 ctlTo
->bindDN
= g_strdup( ctlFrom
->bindDN
);
363 ctlTo
->bindPass
= g_strdup( ctlFrom
->bindPass
);
364 ctlTo
->attribEMail
= g_strdup( ctlFrom
->attribEMail
);
365 ctlTo
->attribCName
= g_strdup( ctlFrom
->attribCName
);
366 ctlTo
->attribFName
= g_strdup( ctlFrom
->attribFName
);
367 ctlTo
->attribLName
= g_strdup( ctlFrom
->attribLName
);
368 ctlTo
->attribDName
= g_strdup( ctlFrom
->attribDName
);
370 /* Copy search criteria */
371 node
= ctlFrom
->listCriteria
;
373 ctlTo
->listCriteria
= g_list_append(
374 ctlTo
->listCriteria
, g_strdup( node
->data
) );
375 node
= g_list_next( node
);
378 /* Copy other members */
379 ctlTo
->port
= ctlFrom
->port
;
380 ctlTo
->maxEntries
= ctlFrom
->maxEntries
;
381 ctlTo
->timeOut
= ctlFrom
->timeOut
;
382 ctlTo
->maxQueryAge
= ctlFrom
->maxQueryAge
;
383 ctlTo
->matchingOption
= ctlFrom
->matchingOption
;
384 ctlTo
->version
= ctlFrom
->version
;
385 ctlTo
->enableTLS
= ctlFrom
->enableTLS
;
386 ctlTo
->enableSSL
= ctlFrom
->enableSSL
;
389 pthread_mutex_unlock( ctlTo
->mutexCtl
);
390 pthread_mutex_unlock( ctlFrom
->mutexCtl
);
394 * Search criteria fragment - two terms - begin with (default).
396 static gchar
*_criteria2BeginWith
= "(&(givenName=%s*)(sn=%s*))";
399 * Search criteria fragment - two terms - contains.
401 static gchar
*_criteria2Contains
= "(&(givenName=*%s*)(sn=*%s*))";
404 * Create an LDAP search criteria by parsing specified search term. The search
405 * term may contain two names separated by the first embedded space found in
406 * the search term. It is assumed that the two tokens are first name and last
407 * name, or vice versa. An appropriate search criteria will be constructed.
409 * \param searchTerm Reference to search term to process.
410 * \param matchOption Set to the following:
412 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
413 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
416 * \return Formatted search criteria, or <code>NULL</code> if there is no
417 * embedded spaces. The search term should be g_free() when no
420 static gchar
*ldapctl_build_ldap_criteria(
421 const gchar
*searchTerm
, const gint matchOption
)
430 if( matchOption
== LDAPCTL_MATCH_CONTAINS
) {
431 criteriaFmt
= _criteria2Contains
;
434 criteriaFmt
= _criteria2BeginWith
;
437 term
= g_strdup( searchTerm
);
440 /* Find first space character */
445 t2
= g_strdup( 1 + p
);
452 /* Format search criteria */
456 p1
= g_strdup_printf( criteriaFmt
, t1
, t2
);
457 p2
= g_strdup_printf( criteriaFmt
, t2
, t1
);
458 crit
= g_strdup_printf( "(&(|%s%s)(mail=*))", p1
, p2
);
470 * Search criteria fragment - single term - begin with (default).
472 static gchar
*_criteriaBeginWith
= "(%s=%s*)";
475 * Search criteria fragment - single term - contains.
477 static gchar
*_criteriaContains
= "(%s=*%s*)";
480 * Build a formatted LDAP search criteria string from criteria list.
481 * \param ctl Control object to process.
482 * \param searchVal Value to search for.
483 * \return Formatted string. Should be g_free() when done.
485 gchar
*ldapctl_format_criteria( LdapControl
*ctl
, const gchar
*searchVal
) {
487 gchar
*p1
, *p2
, *retVal
;
490 g_return_val_if_fail( ctl
!= NULL
, NULL
);
491 g_return_val_if_fail( searchVal
!= NULL
, NULL
);
493 /* Test whether there are more that one search terms */
494 retVal
= ldapctl_build_ldap_criteria( searchVal
, ctl
->matchingOption
);
495 if( retVal
) return retVal
;
497 if( ctl
->matchingOption
== LDAPCTL_MATCH_CONTAINS
) {
498 criteriaFmt
= _criteriaContains
;
501 criteriaFmt
= _criteriaBeginWith
;
504 /* No - just a simple search */
505 /* p1 contains previous formatted criteria */
506 /* p2 contains next formatted criteria */
507 retVal
= p1
= p2
= NULL
;
508 node
= ctl
->listCriteria
;
513 node
= g_list_next( node
);
515 /* Switch pointers */
516 tmp
= p1
; p1
= p2
; p2
= tmp
;
519 /* Subsequent time through */
522 /* Format query criteria */
523 crit
= g_strdup_printf( criteriaFmt
, attr
, searchVal
);
525 /* Append to existing criteria */
527 p2
= g_strdup_printf( "(|%s%s)", p1
, crit
);
532 /* First time through - Format query criteria */
533 p2
= g_strdup_printf( criteriaFmt
, attr
, searchVal
);
538 /* Nothing processed - format a default attribute */
539 retVal
= g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL
);
542 /* We have something - free up previous result */
550 * Return array of pointers to attributes for LDAP query.
551 * \param ctl Control object to process.
552 * \return NULL terminated list.
554 char **ldapctl_attribute_array( LdapControl
*ctl
) {
558 g_return_val_if_fail( ctl
!= NULL
, NULL
);
560 cnt
= g_list_length( ctl
->listCriteria
);
561 ptrArray
= g_new0( char *, 1 + cnt
);
563 node
= ctl
->listCriteria
;
565 ptrArray
[ i
++ ] = node
->data
;
566 node
= g_list_next( node
);
568 ptrArray
[ i
] = NULL
;
573 * Free array of pointers allocated by ldapctl_criteria_array().
574 * param ptrArray Array to clear.
576 void ldapctl_free_attribute_array( char **ptrArray
) {
579 /* Clear array to NULL's */
580 for( i
= 0; ptrArray
[i
] != NULL
; i
++ ) {
587 * Parse LDAP search string, building list of LDAP criteria attributes. This
588 * may be used to convert an old style Sylpheed LDAP search criteria to the
589 * new format. The old style uses a standard LDAP search string, for example:
591 * (&(mail=*)(cn=%s*))
593 * This function extracts the two LDAP attributes <code>mail</code> and
594 * <code>cn</code>, adding each to a list.
596 * \param ctl Control object to process.
597 * \param criteria LDAP search criteria string.
599 void ldapctl_parse_ldap_search( LdapControl
*ctl
, gchar
*criteria
) {
605 g_return_if_fail( ctl
!= NULL
);
607 ldapctl_criteria_list_clear( ctl
);
608 if( criteria
== NULL
) return;
619 attrib
= g_strndup( pFrom
, iLen
);
620 g_strstrip( attrib
);
621 ldapctl_criteria_list_add( ctl
, attrib
);
630 #endif /* USE_LDAP */