2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003 Match Grun
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 /* Mutex to protect control block */
61 ctl->mutexCtl = g_malloc0( sizeof( pthread_mutex_t ) );
62 pthread_mutex_init( ctl->mutexCtl, NULL );
68 * Specify hostname to be used.
69 * \param ctl Control object to process.
70 * \param value Host name.
72 void ldapctl_set_host( LdapControl* ctl, const gchar *value ) {
73 ctl->hostName = mgu_replace_string( ctl->hostName, value );
74 g_strstrip( ctl->hostName );
78 * Specify port to be used.
79 * \param ctl Control object to process.
82 void ldapctl_set_port( LdapControl* ctl, const gint value ) {
87 ctl->port = LDAPCTL_DFL_PORT;
92 * Specify base DN to be used.
93 * \param ctl Control object to process.
94 * \param value Base DN.
96 void ldapctl_set_base_dn( LdapControl* ctl, const gchar *value ) {
97 ctl->baseDN = mgu_replace_string( ctl->baseDN, value );
98 g_strstrip( ctl->baseDN );
102 * Specify bind DN to be used.
103 * \param ctl Control object to process.
104 * \param value Bind DN.
106 void ldapctl_set_bind_dn( LdapControl* ctl, const gchar *value ) {
107 ctl->bindDN = mgu_replace_string( ctl->bindDN, value );
108 g_strstrip( ctl->bindDN );
112 * Specify bind password to be used.
113 * \param ctl Control object to process.
114 * \param value Password.
116 void ldapctl_set_bind_password( LdapControl* ctl, const gchar *value ) {
117 ctl->bindPass = mgu_replace_string( ctl->bindPass, value );
118 g_strstrip( ctl->bindPass );
122 * Specify maximum number of entries to retrieve.
123 * \param ctl Control object to process.
124 * \param value Maximum entries.
126 void ldapctl_set_max_entries( LdapControl* ctl, const gint value ) {
128 ctl->maxEntries = value;
131 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
136 * Specify timeout value for LDAP operation (in seconds).
137 * \param ctl Control object to process.
138 * \param value Timeout.
140 void ldapctl_set_timeout( LdapControl* ctl, const gint value ) {
142 ctl->timeOut = value;
145 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
150 * Specify maximum age of query (in seconds) before query is retired.
151 * \param ctl Control object to process.
152 * \param value Maximum age.
154 void ldapctl_set_max_query_age( LdapControl* ctl, const gint value ) {
155 if( value > LDAPCTL_MAX_QUERY_AGE ) {
156 ctl->maxQueryAge = LDAPCTL_MAX_QUERY_AGE;
158 else if( value < 1 ) {
159 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
162 ctl->maxQueryAge = value;
167 * Specify matching option to be used for searches.
168 * \param ctl Control object to process.
169 * \param value Matching option, as follows:
171 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
172 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
175 void ldapctl_set_matching_option( LdapControl* ctl, const gint value ) {
176 if( value < LDAPCTL_MATCH_BEGINWITH ) {
177 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
179 else if( value > LDAPCTL_MATCH_CONTAINS ) {
180 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
183 ctl->matchingOption = value;
188 * Specify search criteria list to be used.
189 * \param ctl Control data object.
190 * \param value Linked list of LDAP attribute names to use for search.
192 void ldapctl_set_criteria_list( LdapControl* ctl, GList *value ) {
193 g_return_if_fail( ctl != NULL );
194 mgu_free_dlist( ctl->listCriteria );
195 ctl->listCriteria = value;
199 * Return search criteria list.
200 * \param ctl Control data object.
201 * \return Linked list of character strings containing LDAP attribute names to
202 * use for a search. This should not be modified directly. Use the
203 * <code>ldapctl_set_criteria_list()</code>,
204 * <code>ldapctl_criteria_list_clear()</code> and
205 * <code>ldapctl_criteria_list_add()</code> functions for this purpose.
207 GList *ldapctl_get_criteria_list( const LdapControl* ctl ) {
208 g_return_val_if_fail( ctl != NULL, NULL );
209 return ctl->listCriteria;
213 * Clear list of LDAP search attributes.
214 * \param ctl Control data object.
216 void ldapctl_criteria_list_clear( LdapControl *ctl ) {
217 g_return_if_fail( ctl != NULL );
218 mgu_free_dlist( ctl->listCriteria );
219 ctl->listCriteria = NULL;
223 * Add LDAP attribute to criteria list.
224 * \param ctl Control object to process.
225 * \param attr Attribute name to append. If not NULL and unique, a copy will
226 * be appended to the list.
228 void ldapctl_criteria_list_add( LdapControl *ctl, gchar *attr ) {
229 g_return_if_fail( ctl != NULL );
231 if( mgu_list_test_unq_nc( ctl->listCriteria, attr ) ) {
232 ctl->listCriteria = g_list_append(
233 ctl->listCriteria, g_strdup( attr ) );
239 * Build criteria list using default attributes.
240 * \param ctl Control object to process.
242 void ldapctl_default_attributes( LdapControl *ctl ) {
243 g_return_if_fail( ctl != NULL );
245 ldapctl_criteria_list_clear( ctl );
246 ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_COMMONNAME );
247 ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_GIVENNAME );
248 ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_SURNAME );
249 ldapctl_criteria_list_add( ctl, LDAPCTL_ATTR_EMAIL );
253 * Clear LDAP server member variables.
254 * \param ctl Control object to clear.
256 void ldapctl_clear( LdapControl *ctl ) {
257 g_return_if_fail( ctl != NULL );
259 /* Free internal stuff */
260 g_free( ctl->hostName );
261 g_free( ctl->baseDN );
262 g_free( ctl->bindDN );
263 g_free( ctl->bindPass );
264 g_free( ctl->attribEMail );
265 g_free( ctl->attribCName );
266 g_free( ctl->attribFName );
267 g_free( ctl->attribLName );
269 ldapctl_criteria_list_clear( ctl );
272 ctl->hostName = NULL;
276 ctl->bindPass = NULL;
277 ctl->attribEMail = NULL;
278 ctl->attribCName = NULL;
279 ctl->attribFName = NULL;
280 ctl->attribLName = NULL;
283 ctl->maxQueryAge = 0;
284 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
288 * Free up LDAP server interface object by releasing internal memory.
289 * \param ctl Control object to free.
291 void ldapctl_free( LdapControl *ctl ) {
292 g_return_if_fail( ctl != NULL );
294 /* Free internal stuff */
295 ldapctl_clear( ctl );
298 pthread_mutex_destroy( ctl->mutexCtl );
299 g_free( ctl->mutexCtl );
300 ctl->mutexCtl = NULL;
302 /* Now release LDAP control object */
307 * Setup default (empty) values for specified object.
308 * \param ctl Control object to process.
310 void ldapctl_default_values( LdapControl *ctl ) {
311 g_return_if_fail( ctl != NULL );
313 /* Clear our destination */
314 ldapctl_clear( ctl );
317 ctl->hostName = g_strdup( "" );
318 ctl->baseDN = g_strdup( "" );
319 ctl->bindDN = g_strdup( "" );
320 ctl->bindPass = g_strdup( "" );
321 ctl->port = LDAPCTL_DFL_PORT;
322 ctl->maxEntries = LDAPCTL_MAX_ENTRIES;
323 ctl->timeOut = LDAPCTL_DFL_TIMEOUT;
324 ctl->maxQueryAge = LDAPCTL_DFL_QUERY_AGE;
325 ctl->matchingOption = LDAPCTL_MATCH_BEGINWITH;
327 ldapctl_default_attributes( ctl );
331 * Display object to specified stream.
332 * \param ctl Control object to process.
333 * \param stream Output stream.
335 void ldapctl_print( const LdapControl *ctl, FILE *stream ) {
336 g_return_if_fail( ctl != NULL );
338 pthread_mutex_lock( ctl->mutexCtl );
339 fprintf( stream, "LdapControl:\n" );
340 fprintf( stream, "host name: '%s'\n", ctl->hostName );
341 fprintf( stream, " port: %d\n", ctl->port );
342 fprintf( stream, " base dn: '%s'\n", ctl->baseDN );
343 fprintf( stream, " bind dn: '%s'\n", ctl->bindDN );
344 fprintf( stream, "bind pass: '%s'\n", ctl->bindPass );
345 fprintf( stream, "attr mail: '%s'\n", ctl->attribEMail );
346 fprintf( stream, "attr comn: '%s'\n", ctl->attribCName );
347 fprintf( stream, "attr frst: '%s'\n", ctl->attribFName );
348 fprintf( stream, "attr last: '%s'\n", ctl->attribLName );
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, "crit list:\n" );
354 if( ctl->listCriteria ) {
355 mgu_print_dlist( ctl->listCriteria, stream );
358 fprintf( stream, "\t!!!none!!!\n" );
360 pthread_mutex_unlock( ctl->mutexCtl );
364 * Copy member variables to specified object. Mutex lock object is
366 * \param ctlFrom Object to copy from.
367 * \param ctlTo Destination object.
369 void ldapctl_copy( const LdapControl *ctlFrom, LdapControl *ctlTo ) {
372 g_return_if_fail( ctlFrom != NULL );
373 g_return_if_fail( ctlTo != NULL );
375 /* Lock both objects */
376 pthread_mutex_lock( ctlFrom->mutexCtl );
377 pthread_mutex_lock( ctlTo->mutexCtl );
379 /* Clear our destination */
380 ldapctl_clear( ctlTo );
383 ctlTo->hostName = g_strdup( ctlFrom->hostName );
384 ctlTo->baseDN = g_strdup( ctlFrom->baseDN );
385 ctlTo->bindDN = g_strdup( ctlFrom->bindDN );
386 ctlTo->bindPass = g_strdup( ctlFrom->bindPass );
387 ctlTo->attribEMail = g_strdup( ctlFrom->attribEMail );
388 ctlTo->attribCName = g_strdup( ctlFrom->attribCName );
389 ctlTo->attribFName = g_strdup( ctlFrom->attribFName );
390 ctlTo->attribLName = g_strdup( ctlFrom->attribLName );
392 /* Copy search criteria */
393 node = ctlFrom->listCriteria;
395 ctlTo->listCriteria = g_list_append(
396 ctlTo->listCriteria, g_strdup( node->data ) );
397 node = g_list_next( node );
400 /* Copy other members */
401 ctlTo->port = ctlFrom->port;
402 ctlTo->maxEntries = ctlFrom->maxEntries;
403 ctlTo->timeOut = ctlFrom->timeOut;
404 ctlTo->maxQueryAge = ctlFrom->maxQueryAge;
405 ctlTo->matchingOption = ctlFrom->matchingOption;
408 pthread_mutex_unlock( ctlTo->mutexCtl );
409 pthread_mutex_unlock( ctlFrom->mutexCtl );
413 * Search criteria fragment - two terms - begin with (default).
415 static gchar *_criteria2BeginWith = "(&(givenName=%s*)(sn=%s*))";
418 * Search criteria fragment - two terms - contains.
420 static gchar *_criteria2Contains = "(&(givenName=*%s*)(sn=*%s*))";
423 * Create an LDAP search criteria by parsing specified search term. The search
424 * term may contain two names separated by the first embedded space found in
425 * the search term. It is assumed that the two tokens are first name and last
426 * name, or vice versa. An appropriate search criteria will be constructed.
428 * \param searchTerm Reference to search term to process.
429 * \param matchOption Set to the following:
431 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
432 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
435 * \return Formatted search criteria, or <code>NULL</code> if there is no
436 * embedded spaces. The search term should be g_free() when no
439 static gchar *ldapctl_build_ldap_criteria(
440 const gchar *searchTerm, const gint matchOption )
449 if( matchOption == LDAPCTL_MATCH_CONTAINS ) {
450 criteriaFmt = _criteria2Contains;
453 criteriaFmt = _criteria2BeginWith;
456 term = g_strdup( searchTerm );
459 /* Find first space character */
464 t2 = g_strdup( 1 + p );
471 /* Format search criteria */
475 p1 = g_strdup_printf( criteriaFmt, t1, t2 );
476 p2 = g_strdup_printf( criteriaFmt, t2, t1 );
477 crit = g_strdup_printf( "(&(|%s%s)(mail=*))", p1, p2 );
489 * Search criteria fragment - single term - begin with (default).
491 static gchar *_criteriaBeginWith = "(%s=%s*)";
494 * Search criteria fragment - single term - contains.
496 static gchar *_criteriaContains = "(%s=*%s*)";
499 * Build a formatted LDAP search criteria string from criteria list.
500 * \param ctl Control object to process.
501 * \param searchVal Value to search for.
502 * \return Formatted string. Should be g_free() when done.
504 gchar *ldapctl_format_criteria( LdapControl *ctl, const gchar *searchVal ) {
506 gchar *p1, *p2, *retVal;
509 g_return_val_if_fail( ctl != NULL, NULL );
510 g_return_val_if_fail( searchVal != NULL, NULL );
512 /* Test whether there are more that one search terms */
513 retVal = ldapctl_build_ldap_criteria( searchVal, ctl->matchingOption );
514 if( retVal ) return retVal;
516 if( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) {
517 criteriaFmt = _criteriaContains;
520 criteriaFmt = _criteriaBeginWith;
523 /* No - just a simple search */
524 /* p1 contains previous formatted criteria */
525 /* p2 contains next formatted criteria */
526 retVal = p1 = p2 = NULL;
527 node = ctl->listCriteria;
532 node = g_list_next( node );
534 /* Switch pointers */
535 tmp = p1; p1 = p2; p2 = tmp;
538 /* Subsequent time through */
541 /* Format query criteria */
542 crit = g_strdup_printf( criteriaFmt, attr, searchVal );
544 /* Append to existing criteria */
546 p2 = g_strdup_printf( "(|%s%s)", p1, crit );
551 /* First time through - Format query criteria */
552 p2 = g_strdup_printf( criteriaFmt, attr, searchVal );
557 /* Nothing processed - format a default attribute */
558 retVal = g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL );
561 /* We have something - free up previous result */
569 * Return array of pointers to attributes for LDAP query.
570 * \param ctl Control object to process.
571 * \return NULL terminated list.
573 char **ldapctl_attribute_array( LdapControl *ctl ) {
577 g_return_val_if_fail( ctl != NULL, NULL );
579 cnt = g_list_length( ctl->listCriteria );
580 ptrArray = g_new0( char *, 1 + cnt );
582 node = ctl->listCriteria;
584 ptrArray[ i++ ] = node->data;
585 node = g_list_next( node );
587 ptrArray[ i ] = NULL;
592 * Free array of pointers allocated by ldapctl_criteria_array().
593 * param ptrArray Array to clear.
595 void ldapctl_free_attribute_array( char **ptrArray ) {
598 /* Clear array to NULL's */
599 for( i = 0; ptrArray[i] != NULL; i++ ) {
606 * Parse LDAP search string, building list of LDAP criteria attributes. This
607 * may be used to convert an old style Sylpheed LDAP search criteria to the
608 * new format. The old style uses a standard LDAP search string, for example:
610 * (&(mail=*)(cn=%s*))
612 * This function extracts the two LDAP attributes <code>mail</code> and
613 * <code>cn</code>, adding each to a list.
615 * \param ctl Control object to process.
616 * \param criteria LDAP search criteria string.
618 void ldapctl_parse_ldap_search( LdapControl *ctl, gchar *criteria ) {
624 g_return_if_fail( ctl != NULL );
626 ldapctl_criteria_list_clear( ctl );
627 if( criteria == NULL ) return;
638 attrib = g_strndup( pFrom, iLen );
639 g_strstrip( attrib );
640 ldapctl_criteria_list_add( ctl, attrib );
649 #endif /* USE_LDAP */