2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2021 the Claws Mail team and 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 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 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 g_list_free_full( ctl
->listCriteria
, g_free
);
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( !g_list_find_custom( ctl
->listCriteria
, attr
,
258 (GCompareFunc
)g_utf8_collate
) ) {
259 debug_print("adding to criteria list: %s\n", attr
);
260 ctl
->listCriteria
= g_list_append(
261 ctl
->listCriteria
, g_strdup( attr
) );
267 * Clear LDAP server member variables.
268 * \param ctl Control object to clear.
270 static void ldapctl_clear( LdapControl
*ctl
) {
271 cm_return_if_fail( ctl
!= NULL
);
273 debug_print("clearing ldap controller members\n");
274 /* Free internal stuff */
275 g_free( ctl
->hostName
);
276 g_free( ctl
->baseDN
);
277 g_free( ctl
->bindDN
);
278 g_free( ctl
->attribEMail
);
279 g_free( ctl
->attribCName
);
280 g_free( ctl
->attribFName
);
281 g_free( ctl
->attribLName
);
282 g_free( ctl
->attribDName
);
284 ldapctl_criteria_list_clear( ctl
);
287 ctl
->hostName
= NULL
;
291 ctl
->attribEMail
= NULL
;
292 ctl
->attribCName
= NULL
;
293 ctl
->attribFName
= NULL
;
294 ctl
->attribLName
= NULL
;
295 ctl
->attribDName
= NULL
;
298 ctl
->maxQueryAge
= 0;
299 ctl
->matchingOption
= LDAPCTL_MATCH_BEGINWITH
;
301 ctl
->enableTLS
= FALSE
;
302 ctl
->enableSSL
= FALSE
;
306 * Free up LDAP server interface object by releasing internal memory.
307 * \param ctl Control object to free.
309 void ldapctl_free( LdapControl
*ctl
) {
310 cm_return_if_fail( ctl
!= NULL
);
312 debug_print("releasing requested memory for ldap controller\n");
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 * Display object to specified stream.
327 * \param ctl Control object to process.
328 * \param stream Output stream.
330 void ldapctl_print( const LdapControl
*ctl
, FILE *stream
) {
331 cm_return_if_fail( ctl
!= NULL
);
334 pthread_mutex_lock( ctl
->mutexCtl
);
335 fprintf( stream
, "LdapControl:\n" );
336 fprintf( stream
, "host name: '%s'\n", ctl
->hostName
?ctl
->hostName
:"null" );
337 fprintf( stream
, " port: %d\n", ctl
->port
);
338 fprintf( stream
, " base dn: '%s'\n", ctl
->baseDN
?ctl
->baseDN
:"null" );
339 fprintf( stream
, " bind dn: '%s'\n", ctl
->bindDN
?ctl
->bindDN
:"null" );
340 pwd
= passwd_store_get(PWS_CORE
, "LDAP", ctl
->hostName
);
341 fprintf( stream
, "bind pass: '%s'\n", pwd
?pwd
:"null" );
342 if (pwd
!= NULL
&& strlen(pwd
) > 0)
343 memset(pwd
, 0, strlen(pwd
));
345 fprintf( stream
, "attr mail: '%s'\n", ctl
->attribEMail
?ctl
->attribEMail
:"null" );
346 fprintf( stream
, "attr comn: '%s'\n", ctl
->attribCName
?ctl
->attribCName
:"null" );
347 fprintf( stream
, "attr frst: '%s'\n", ctl
->attribFName
?ctl
->attribFName
:"null" );
348 fprintf( stream
, "attr last: '%s'\n", ctl
->attribLName
?ctl
->attribLName
:"null" );
349 fprintf( stream
, "attr disn: '%s'\n", ctl
->attribDName
?ctl
->attribDName
:"null" );
350 fprintf( stream
, "max entry: %d\n", ctl
->maxEntries
);
351 fprintf( stream
, " timeout: %d\n", ctl
->timeOut
);
352 fprintf( stream
, " max age: %d\n", ctl
->maxQueryAge
);
353 fprintf( stream
, "match opt: %d\n", ctl
->matchingOption
);
354 fprintf( stream
, " version: %d\n", ctl
->version
);
355 fprintf( stream
, " STARTTLS: %s\n", ctl
->enableTLS
? "yes" : "no" );
356 fprintf( stream
, " TLS: %s\n", ctl
->enableSSL
? "yes" : "no" );
357 fprintf( stream
, "crit list:\n" );
358 if( ctl
->listCriteria
) {
359 mgu_print_dlist( ctl
->listCriteria
, stream
);
362 fprintf( stream
, "\t!!!none!!!\n" );
364 pthread_mutex_unlock( ctl
->mutexCtl
);
368 * Copy member variables to specified object. Mutex lock object is
370 * \param ctlFrom Object to copy from.
371 * \param ctlTo Destination object.
373 void ldapctl_copy( const LdapControl
*ctlFrom
, LdapControl
*ctlTo
) {
376 cm_return_if_fail( ctlFrom
!= NULL
);
377 cm_return_if_fail( ctlTo
!= NULL
);
379 debug_print("ldap controller copy\n");
380 /* Lock both objects */
381 pthread_mutex_lock( ctlFrom
->mutexCtl
);
382 pthread_mutex_lock( ctlTo
->mutexCtl
);
384 /* Clear our destination */
385 ldapctl_clear( ctlTo
);
388 ctlTo
->hostName
= g_strdup( ctlFrom
->hostName
);
389 ctlTo
->baseDN
= g_strdup( ctlFrom
->baseDN
);
390 ctlTo
->bindDN
= g_strdup( ctlFrom
->bindDN
);
391 ctlTo
->attribEMail
= g_strdup( ctlFrom
->attribEMail
);
392 ctlTo
->attribCName
= g_strdup( ctlFrom
->attribCName
);
393 ctlTo
->attribFName
= g_strdup( ctlFrom
->attribFName
);
394 ctlTo
->attribLName
= g_strdup( ctlFrom
->attribLName
);
395 ctlTo
->attribDName
= g_strdup( ctlFrom
->attribDName
);
397 /* Copy search criteria */
398 node
= ctlFrom
->listCriteria
;
400 ctlTo
->listCriteria
= g_list_append(
401 ctlTo
->listCriteria
, g_strdup( node
->data
) );
402 node
= g_list_next( node
);
405 /* Copy other members */
406 ctlTo
->port
= ctlFrom
->port
;
407 ctlTo
->maxEntries
= ctlFrom
->maxEntries
;
408 ctlTo
->timeOut
= ctlFrom
->timeOut
;
409 ctlTo
->maxQueryAge
= ctlFrom
->maxQueryAge
;
410 ctlTo
->matchingOption
= ctlFrom
->matchingOption
;
411 ctlTo
->version
= ctlFrom
->version
;
412 ctlTo
->enableTLS
= ctlFrom
->enableTLS
;
413 ctlTo
->enableSSL
= ctlFrom
->enableSSL
;
416 pthread_mutex_unlock( ctlTo
->mutexCtl
);
417 pthread_mutex_unlock( ctlFrom
->mutexCtl
);
421 * Search criteria fragment - two terms - begin with (default).
423 static gchar
*_criteria2BeginWith
= "(&(givenName=%s*)(sn=%s*))";
426 * Search criteria fragment - two terms - contains.
428 static gchar
*_criteria2Contains
= "(&(givenName=*%s*)(sn=*%s*))";
431 * Create an LDAP search criteria by parsing specified search term. The search
432 * term may contain two names separated by the first embedded space found in
433 * the search term. It is assumed that the two tokens are first name and last
434 * name, or vice versa. An appropriate search criteria will be constructed.
436 * \param searchTerm Reference to search term to process.
437 * \param matchOption Set to the following:
439 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
440 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
443 * \return Formatted search criteria, or <code>NULL</code> if there is no
444 * embedded spaces. The search term should be g_free() when no
447 static gchar
*ldapctl_build_ldap_criteria(
448 const gchar
*searchTerm
, const gint matchOption
)
457 if( matchOption
== LDAPCTL_MATCH_CONTAINS
) {
458 criteriaFmt
= _criteria2Contains
;
461 criteriaFmt
= _criteria2BeginWith
;
464 term
= g_strdup( searchTerm
);
467 /* Find first space character */
472 t2
= g_strdup( 1 + p
);
479 /* Format search criteria */
483 p1
= g_strdup_printf( criteriaFmt
, t1
, t2
);
484 p2
= g_strdup_printf( criteriaFmt
, t2
, t1
);
485 crit
= g_strdup_printf( "(&(|%s%s)(mail=*))", p1
, p2
);
492 debug_print("search criteria: %s\n", crit
?crit
:"null");
498 * Search criteria fragment - single term - begin with (default).
500 static gchar
*_criteriaBeginWith
= "(%s=%s*)";
503 * Search criteria fragment - single term - contains.
505 static gchar
*_criteriaContains
= "(%s=*%s*)";
508 * Build a formatted LDAP search criteria string from criteria list.
509 * \param ctl Control object to process.
510 * \param searchVal Value to search for.
511 * \return Formatted string. Should be g_free() when done.
513 gchar
*ldapctl_format_criteria( LdapControl
*ctl
, const gchar
*searchVal
) {
515 gchar
*p1
, *p2
, *retVal
;
518 cm_return_val_if_fail( ctl
!= NULL
, NULL
);
519 cm_return_val_if_fail( searchVal
!= NULL
, NULL
);
521 /* Test whether there are more that one search terms */
522 retVal
= ldapctl_build_ldap_criteria( searchVal
, ctl
->matchingOption
);
523 if( retVal
) return retVal
;
525 if( ctl
->matchingOption
== LDAPCTL_MATCH_CONTAINS
) {
526 criteriaFmt
= _criteriaContains
;
529 criteriaFmt
= _criteriaBeginWith
;
532 /* No - just a simple search */
533 /* p1 contains previous formatted criteria */
534 /* p2 contains next formatted criteria */
535 retVal
= p1
= p2
= NULL
;
536 node
= ctl
->listCriteria
;
540 node
= g_list_next( node
);
542 /* Switch pointers */
543 tmp
= p1
; p1
= p2
; p2
= tmp
;
546 /* Subsequent time through */
549 debug_print("crit: %s\n", searchVal
);
550 /* fix bug when doing a search any */
551 if (strcmp("*@", searchVal
) == 0) {
552 crit
= g_strdup_printf( "(%s=*)", attr
);
555 /* Format query criteria */
556 crit
= g_strdup_printf( criteriaFmt
, attr
, searchVal
);
559 /* Append to existing criteria */
561 p2
= g_strdup_printf( "(|%s%s)", p1
, crit
);
566 /* First time through - Format query criteria */
567 /* fix bug when doing a search any */
568 if (strcmp("*@", searchVal
) == 0) {
569 p2
= g_strdup_printf( "(%s=*)", attr
);
572 p2
= g_strdup_printf( criteriaFmt
, attr
, searchVal
);
578 /* Nothing processed - format a default attribute */
579 retVal
= g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL
);
582 /* We have something - free up previous result */
587 debug_print("current search string: %s\n", retVal
);
592 * Return array of pointers to attributes for LDAP query.
593 * \param ctl Control object to process.
594 * \return NULL terminated list.
596 char **ldapctl_attribute_array( LdapControl
*ctl
) {
600 cm_return_val_if_fail( ctl
!= NULL
, NULL
);
602 node
= ctl
->listCriteria
;
603 cnt
= g_list_length( ctl
->listCriteria
);
604 ptrArray
= g_new0( char *, 1 + cnt
);
607 ptrArray
[ i
++ ] = node
->data
;
608 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
609 node
= g_list_next( node
);
611 ptrArray
[ i
] = NULL
;
616 * Return array of pointers to attributes for LDAP query.
617 * \param ctl Control object to process.
618 * \return NULL terminated list.
620 char **ldapctl_full_attribute_array( LdapControl
*ctl
) {
625 cm_return_val_if_fail( ctl
!= NULL
, NULL
);
627 def
= ctl
->listCriteria
;
629 tmp
= g_list_append(tmp
, g_strdup(def
->data
));
633 def
= ldapctl_get_default_criteria_list();
637 if( g_list_find_custom(tmp
, (gpointer
)def
->data
,
638 (GCompareFunc
)g_strcmp0
) == NULL
) {
639 tmp
= g_list_append(tmp
, g_strdup(node
->data
));
644 g_list_free_full(def
, g_free
);
647 cnt
= g_list_length( tmp
);
648 ptrArray
= g_new0( char *, 1 + cnt
);
651 ptrArray
[ i
++ ] = node
->data
;
652 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
653 node
= g_list_next( node
);
656 ptrArray
[ i
] = NULL
;
661 * Free array of pointers allocated by ldapctl_criteria_array().
662 * param ptrArray Array to clear.
664 void ldapctl_free_attribute_array( char **ptrArray
) {
667 /* Clear array to NULL's */
668 for( i
= 0; ptrArray
[i
] != NULL
; i
++ ) {
676 * Parse LDAP search string, building list of LDAP criteria attributes. This
677 * may be used to convert an old style Sylpheed LDAP search criteria to the
678 * new format. The old style uses a standard LDAP search string, for example:
680 * (&(mail=*)(cn=%s*))
682 * This function extracts the two LDAP attributes <code>mail</code> and
683 * <code>cn</code>, adding each to a list.
685 * \param ctl Control object to process.
686 * \param criteria LDAP search criteria string.
688 void ldapctl_parse_ldap_search( LdapControl
*ctl
, gchar
*criteria
) {
694 cm_return_if_fail( ctl
!= NULL
);
696 ldapctl_criteria_list_clear( ctl
);
697 if( criteria
== NULL
) return;
708 attrib
= g_strndup( pFrom
, iLen
);
709 g_strstrip( attrib
);
710 ldapctl_criteria_list_add( ctl
, attrib
);
720 * Return the default LDAP search criteria string.
721 * \return Formatted string or <i>""</i>. Should be g_free() when done.
723 gchar
*ldapctl_get_default_criteria() {
724 gchar
*retVal
= g_strdup(LDAPCTL_DFL_ATTR_LIST
);
725 const gchar
**attrs
= ATTRIBUTE
;
728 gchar
*tmp
= g_strdup_printf("%s, %s", retVal
, *attrs
++);
732 debug_print("default search criteria: %s\n", retVal
);
737 * Return the default LDAP search criteria list.
738 * \return GList or <i>NULL</i>.
740 GList
*ldapctl_get_default_criteria_list() {
741 gchar
*criteria
, *item
;
742 gchar
**c_list
, **w_list
;
743 GList
*attr_list
= NULL
;
745 criteria
= ldapctl_get_default_criteria();
746 c_list
= g_strsplit(criteria
, " ", 0);
750 while ((criteria
= *w_list
++) != 0) {
751 /* copy string elimination <,> */
753 item
= g_strndup(criteria
, strlen(criteria
) - 1);
755 item
= g_strdup(criteria
);
756 debug_print("adding attribute to list: %s\n", item
);
757 attr_list
= g_list_append(attr_list
, item
);
764 * Compare to GList for equality.
765 * \param l1 First GList
766 * \param l2 Second GList
767 * \Return TRUE or FALSE
769 gboolean
ldapctl_compare_list(GList
*l1
, GList
*l2
) {
770 gchar
*first
, *second
;
773 if ((! l1
&& l2
) || (l1
&& ! l2
))
776 first
= (gchar
*) l1
->data
;
777 second
= (gchar
*) l2
->data
;
778 /*debug_print("comparing: %s = %s\n", first, second);*/
779 if ( ! (first
&& second
) || strcmp(first
, second
) != 0) {
782 l1
= g_list_next(l1
);
783 l2
= g_list_next(l2
);
788 #endif /* USE_LDAP */