2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2022 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/>.
20 * Functions for LDAP control data.
25 #include "claws-features.h"
36 #include "passwordstore.h"
37 #include "editaddress_other_attributes_ldap.h"
38 #include "common/utils.h"
39 #include "common/quoted-printable.h"
42 * Create new LDAP control block object.
43 * \return Initialized control object.
45 LdapControl
*ldapctl_create( void ) {
48 ctl
= g_new0( LdapControl
, 1 );
50 ctl
->port
= LDAPCTL_DFL_PORT
;
53 ctl
->listCriteria
= NULL
;
54 ctl
->attribEMail
= g_strdup( LDAPCTL_ATTR_EMAIL
);
55 ctl
->attribCName
= g_strdup( LDAPCTL_ATTR_COMMONNAME
);
56 ctl
->attribFName
= g_strdup( LDAPCTL_ATTR_GIVENNAME
);
57 ctl
->attribLName
= g_strdup( LDAPCTL_ATTR_SURNAME
);
58 ctl
->attribDName
= g_strdup( LDAPCTL_ATTR_DISPLAYNAME
);
59 ctl
->maxEntries
= LDAPCTL_MAX_ENTRIES
;
60 ctl
->timeOut
= LDAPCTL_DFL_TIMEOUT
;
61 ctl
->maxQueryAge
= LDAPCTL_DFL_QUERY_AGE
;
62 ctl
->matchingOption
= LDAPCTL_MATCH_BEGINWITH
;
64 ctl
->enableTLS
= FALSE
;
65 ctl
->enableSSL
= FALSE
;
67 /* Mutex to protect control block */
68 ctl
->mutexCtl
= g_malloc0( sizeof( pthread_mutex_t
) );
69 pthread_mutex_init( ctl
->mutexCtl
, NULL
);
75 * Specify hostname to be used.
76 * \param ctl Control object to process.
77 * \param value Host name.
79 void ldapctl_set_host( LdapControl
* ctl
, const gchar
*value
) {
80 ctl
->hostName
= mgu_replace_string( ctl
->hostName
, value
);
82 if ( ctl
->hostName
== NULL
)
85 g_strstrip( ctl
->hostName
);
86 debug_print("setting hostname: %s\n", ctl
->hostName
);
90 * Specify port to be used.
91 * \param ctl Control object to process.
94 void ldapctl_set_port( LdapControl
* ctl
, const gint value
) {
99 ctl
->port
= LDAPCTL_DFL_PORT
;
101 debug_print("setting port: %d\n", ctl
->port
);
105 * Specify base DN to be used.
106 * \param ctl Control object to process.
107 * \param value Base DN.
109 void ldapctl_set_base_dn( LdapControl
* ctl
, const gchar
*value
) {
110 ctl
->baseDN
= mgu_replace_string( ctl
->baseDN
, value
);
112 if ( ctl
->baseDN
== NULL
)
115 g_strstrip( ctl
->baseDN
);
116 debug_print("setting baseDN: %s\n", ctl
->baseDN
);
120 * Specify bind DN to be used.
121 * \param ctl Control object to process.
122 * \param value Bind DN.
124 void ldapctl_set_bind_dn( LdapControl
* ctl
, const gchar
*value
) {
125 ctl
->bindDN
= mgu_replace_string( ctl
->bindDN
, value
);
127 if ( ctl
->bindDN
== NULL
)
130 g_strstrip( ctl
->bindDN
);
131 debug_print("setting bindDN: %s\n", ctl
->bindDN
);
135 * Specify maximum number of entries to retrieve.
136 * \param ctl Control object to process.
137 * \param value Maximum entries.
139 void ldapctl_set_max_entries( LdapControl
* ctl
, const gint value
) {
141 ctl
->maxEntries
= value
;
144 ctl
->maxEntries
= LDAPCTL_MAX_ENTRIES
;
146 debug_print("setting maxEntries: %d\n", ctl
->maxEntries
);
150 * Specify timeout value for LDAP operation (in seconds).
151 * \param ctl Control object to process.
152 * \param value Timeout.
154 void ldapctl_set_timeout( LdapControl
* ctl
, const gint value
) {
156 ctl
->timeOut
= value
;
159 ctl
->timeOut
= LDAPCTL_DFL_TIMEOUT
;
161 debug_print("setting timeOut: %d\n", ctl
->timeOut
);
165 * Specify maximum age of query (in seconds) before query is retired.
166 * \param ctl Control object to process.
167 * \param value Maximum age.
169 void ldapctl_set_max_query_age( LdapControl
* ctl
, const gint value
) {
170 if( value
> LDAPCTL_MAX_QUERY_AGE
) {
171 ctl
->maxQueryAge
= LDAPCTL_MAX_QUERY_AGE
;
173 else if( value
< 1 ) {
174 ctl
->maxQueryAge
= LDAPCTL_DFL_QUERY_AGE
;
177 ctl
->maxQueryAge
= value
;
179 debug_print("setting maxAge: %d\n", ctl
->maxQueryAge
);
183 * Specify matching option to be used for searches.
184 * \param ctl Control object to process.
185 * \param value Matching option, as follows:
187 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
188 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
191 void ldapctl_set_matching_option( LdapControl
* ctl
, const gint value
) {
192 if( value
< LDAPCTL_MATCH_BEGINWITH
) {
193 ctl
->matchingOption
= LDAPCTL_MATCH_BEGINWITH
;
195 else if( value
> LDAPCTL_MATCH_CONTAINS
) {
196 ctl
->matchingOption
= LDAPCTL_MATCH_BEGINWITH
;
199 ctl
->matchingOption
= value
;
201 debug_print("setting matchingOption: %d\n", ctl
->matchingOption
);
205 * Specify TLS option.
206 * \param ctl Control object to process.
207 * \param value <i>TRUE</i> to enable TLS.
209 void ldapctl_set_tls( LdapControl
* ctl
, const gboolean value
) {
210 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
211 ctl
->enableTLS
= value
;
212 debug_print("setting STARTTLS: %d\n", ctl
->enableTLS
);
216 void ldapctl_set_ssl( LdapControl
* ctl
, const gboolean value
) {
217 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
218 ctl
->enableSSL
= value
;
219 debug_print("setting TLS: %d\n", ctl
->enableSSL
);
224 * Return search criteria list.
225 * \param ctl Control data object.
226 * \return Linked list of character strings containing LDAP attribute names to
227 * use for a search. This should not be modified directly. Use the
228 * <code>ldapctl_set_criteria_list()</code>,
229 * <code>ldapctl_criteria_list_clear()</code> and
230 * <code>ldapctl_criteria_list_add()</code> functions for this purpose.
232 GList
*ldapctl_get_criteria_list( const LdapControl
* ctl
) {
233 cm_return_val_if_fail( ctl
!= NULL
, NULL
);
234 return ctl
->listCriteria
;
238 * Clear list of LDAP search attributes.
239 * \param ctl Control data object.
241 void ldapctl_criteria_list_clear( LdapControl
*ctl
) {
242 cm_return_if_fail( ctl
!= NULL
);
243 g_list_free_full( ctl
->listCriteria
, g_free
);
244 ctl
->listCriteria
= NULL
;
248 * Add LDAP attribute to criteria list.
249 * \param ctl Control object to process.
250 * \param attr Attribute name to append. If not NULL and unique, a copy will
251 * be appended to the list.
253 void ldapctl_criteria_list_add( LdapControl
*ctl
, gchar
*attr
) {
254 cm_return_if_fail( ctl
!= NULL
);
256 if( !g_list_find_custom( ctl
->listCriteria
, attr
,
257 (GCompareFunc
)g_utf8_collate
) ) {
258 debug_print("adding to criteria list: %s\n", attr
);
259 ctl
->listCriteria
= g_list_append(
260 ctl
->listCriteria
, g_strdup( attr
) );
266 * Clear LDAP server member variables.
267 * \param ctl Control object to clear.
269 static void ldapctl_clear( LdapControl
*ctl
) {
270 cm_return_if_fail( ctl
!= NULL
);
272 debug_print("clearing ldap controller members\n");
273 /* Free internal stuff */
274 g_free( ctl
->hostName
);
275 g_free( ctl
->baseDN
);
276 g_free( ctl
->bindDN
);
277 g_free( ctl
->attribEMail
);
278 g_free( ctl
->attribCName
);
279 g_free( ctl
->attribFName
);
280 g_free( ctl
->attribLName
);
281 g_free( ctl
->attribDName
);
283 ldapctl_criteria_list_clear( ctl
);
286 ctl
->hostName
= NULL
;
290 ctl
->attribEMail
= NULL
;
291 ctl
->attribCName
= NULL
;
292 ctl
->attribFName
= NULL
;
293 ctl
->attribLName
= NULL
;
294 ctl
->attribDName
= NULL
;
297 ctl
->maxQueryAge
= 0;
298 ctl
->matchingOption
= LDAPCTL_MATCH_BEGINWITH
;
300 ctl
->enableTLS
= FALSE
;
301 ctl
->enableSSL
= FALSE
;
305 * Free up LDAP server interface object by releasing internal memory.
306 * \param ctl Control object to free.
308 void ldapctl_free( LdapControl
*ctl
) {
309 cm_return_if_fail( ctl
!= NULL
);
311 debug_print("releasing requested memory for ldap controller\n");
312 /* Free internal stuff */
313 ldapctl_clear( ctl
);
316 pthread_mutex_destroy( ctl
->mutexCtl
);
317 g_free( ctl
->mutexCtl
);
318 ctl
->mutexCtl
= NULL
;
320 /* 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
);
369 * Copy member variables to specified object. Mutex lock object is
371 * \param ctlFrom Object to copy from.
372 * \param ctlTo Destination object.
374 void ldapctl_copy( const LdapControl
*ctlFrom
, LdapControl
*ctlTo
) {
377 cm_return_if_fail( ctlFrom
!= NULL
);
378 cm_return_if_fail( ctlTo
!= NULL
);
380 debug_print("ldap controller copy\n");
381 /* Lock both objects */
382 pthread_mutex_lock( ctlFrom
->mutexCtl
);
383 pthread_mutex_lock( ctlTo
->mutexCtl
);
385 /* Clear our destination */
386 ldapctl_clear( ctlTo
);
389 ctlTo
->hostName
= g_strdup( ctlFrom
->hostName
);
390 ctlTo
->baseDN
= g_strdup( ctlFrom
->baseDN
);
391 ctlTo
->bindDN
= g_strdup( ctlFrom
->bindDN
);
392 ctlTo
->attribEMail
= g_strdup( ctlFrom
->attribEMail
);
393 ctlTo
->attribCName
= g_strdup( ctlFrom
->attribCName
);
394 ctlTo
->attribFName
= g_strdup( ctlFrom
->attribFName
);
395 ctlTo
->attribLName
= g_strdup( ctlFrom
->attribLName
);
396 ctlTo
->attribDName
= g_strdup( ctlFrom
->attribDName
);
398 /* Copy search criteria */
399 node
= ctlFrom
->listCriteria
;
401 ctlTo
->listCriteria
= g_list_append(
402 ctlTo
->listCriteria
, g_strdup( node
->data
) );
403 node
= g_list_next( node
);
406 /* Copy other members */
407 ctlTo
->port
= ctlFrom
->port
;
408 ctlTo
->maxEntries
= ctlFrom
->maxEntries
;
409 ctlTo
->timeOut
= ctlFrom
->timeOut
;
410 ctlTo
->maxQueryAge
= ctlFrom
->maxQueryAge
;
411 ctlTo
->matchingOption
= ctlFrom
->matchingOption
;
412 ctlTo
->version
= ctlFrom
->version
;
413 ctlTo
->enableTLS
= ctlFrom
->enableTLS
;
414 ctlTo
->enableSSL
= ctlFrom
->enableSSL
;
417 pthread_mutex_unlock( ctlTo
->mutexCtl
);
418 pthread_mutex_unlock( ctlFrom
->mutexCtl
);
422 * Search criteria fragment - two terms - begin with (default).
424 static gchar
*_criteria2BeginWith
= "(&(givenName=%s*)(sn=%s*))";
427 * Search criteria fragment - two terms - contains.
429 static gchar
*_criteria2Contains
= "(&(givenName=*%s*)(sn=*%s*))";
432 * Create an LDAP search criteria by parsing specified search term. The search
433 * term may contain two names separated by the first embedded space found in
434 * the search term. It is assumed that the two tokens are first name and last
435 * name, or vice versa. An appropriate search criteria will be constructed.
437 * \param searchTerm Reference to search term to process.
438 * \param matchOption Set to the following:
440 * <li><code>LDAPCTL_MATCH_BEGINWITH</code> for "begins with" search</li>
441 * <li><code>LDAPCTL_MATCH_CONTAINS</code> for "contains" search</li>
444 * \return Formatted search criteria, or <code>NULL</code> if there is no
445 * embedded spaces. The search term should be g_free() when no
448 static gchar
*ldapctl_build_ldap_criteria(
449 const gchar
*searchTerm
, const gint matchOption
)
458 if( matchOption
== LDAPCTL_MATCH_CONTAINS
) {
459 criteriaFmt
= _criteria2Contains
;
462 criteriaFmt
= _criteria2BeginWith
;
465 term
= g_strdup( searchTerm
);
468 /* Find first space character */
473 t2
= g_strdup( 1 + p
);
480 /* Format search criteria */
484 p1
= g_strdup_printf( criteriaFmt
, t1
, t2
);
485 p2
= g_strdup_printf( criteriaFmt
, t2
, t1
);
486 crit
= g_strdup_printf( "(&(|%s%s)(mail=*))", p1
, p2
);
493 debug_print("search criteria: %s\n", crit
?crit
:"null");
499 * Search criteria fragment - single term - begin with (default).
501 static gchar
*_criteriaBeginWith
= "(%s=%s*)";
504 * Search criteria fragment - single term - contains.
506 static gchar
*_criteriaContains
= "(%s=*%s*)";
509 * Build a formatted LDAP search criteria string from criteria list.
510 * \param ctl Control object to process.
511 * \param searchVal Value to search for.
512 * \return Formatted string. Should be g_free() when done.
514 gchar
*ldapctl_format_criteria( LdapControl
*ctl
, const gchar
*searchVal
) {
516 gchar
*p1
, *p2
, *retVal
;
519 cm_return_val_if_fail( ctl
!= NULL
, NULL
);
520 cm_return_val_if_fail( searchVal
!= NULL
, NULL
);
522 /* Test whether there are more that one search terms */
523 retVal
= ldapctl_build_ldap_criteria( searchVal
, ctl
->matchingOption
);
524 if( retVal
) return retVal
;
526 if( ctl
->matchingOption
== LDAPCTL_MATCH_CONTAINS
) {
527 criteriaFmt
= _criteriaContains
;
530 criteriaFmt
= _criteriaBeginWith
;
533 /* No - just a simple search */
534 /* p1 contains previous formatted criteria */
535 /* p2 contains next formatted criteria */
536 retVal
= p1
= p2
= NULL
;
537 node
= ctl
->listCriteria
;
541 node
= g_list_next( node
);
543 /* Switch pointers */
544 tmp
= p1
; p1
= p2
; p2
= tmp
;
547 /* Subsequent time through */
550 debug_print("crit: %s\n", searchVal
);
551 /* fix bug when doing a search any */
552 if (strcmp("*@", searchVal
) == 0) {
553 crit
= g_strdup_printf( "(%s=*)", attr
);
556 /* Format query criteria */
557 crit
= g_strdup_printf( criteriaFmt
, attr
, searchVal
);
560 /* Append to existing criteria */
562 p2
= g_strdup_printf( "(|%s%s)", p1
, crit
);
567 /* First time through - Format query criteria */
568 /* fix bug when doing a search any */
569 if (strcmp("*@", searchVal
) == 0) {
570 p2
= g_strdup_printf( "(%s=*)", attr
);
573 p2
= g_strdup_printf( criteriaFmt
, attr
, searchVal
);
579 /* Nothing processed - format a default attribute */
580 retVal
= g_strdup_printf( "(%s=*)", LDAPCTL_ATTR_EMAIL
);
583 /* We have something - free up previous result */
588 debug_print("current search string: %s\n", retVal
);
593 * Return array of pointers to attributes for LDAP query.
594 * \param ctl Control object to process.
595 * \return NULL terminated list.
597 char **ldapctl_attribute_array( LdapControl
*ctl
) {
601 cm_return_val_if_fail( ctl
!= NULL
, NULL
);
603 node
= ctl
->listCriteria
;
604 cnt
= g_list_length( ctl
->listCriteria
);
605 ptrArray
= g_new0( char *, 1 + cnt
);
608 ptrArray
[ i
++ ] = node
->data
;
609 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
610 node
= g_list_next( node
);
612 ptrArray
[ i
] = NULL
;
617 * Return array of pointers to attributes for LDAP query.
618 * \param ctl Control object to process.
619 * \return NULL terminated list.
621 char **ldapctl_full_attribute_array( LdapControl
*ctl
) {
626 cm_return_val_if_fail( ctl
!= NULL
, NULL
);
628 def
= ctl
->listCriteria
;
630 tmp
= g_list_append(tmp
, g_strdup(def
->data
));
634 def
= ldapctl_get_default_criteria_list();
638 if( g_list_find_custom(tmp
, (gpointer
)def
->data
,
639 (GCompareFunc
)g_strcmp0
) == NULL
) {
640 tmp
= g_list_append(tmp
, g_strdup(node
->data
));
645 g_list_free_full(def
, g_free
);
648 cnt
= g_list_length( tmp
);
649 ptrArray
= g_new0( char *, 1 + cnt
);
652 ptrArray
[ i
++ ] = node
->data
;
653 /*debug_print("adding search attribute: %s\n", (gchar *) node->data);*/
654 node
= g_list_next( node
);
657 ptrArray
[ i
] = NULL
;
662 * Free array of pointers allocated by ldapctl_criteria_array().
663 * param ptrArray Array to clear.
665 void ldapctl_free_attribute_array( char **ptrArray
) {
668 /* Clear array to NULL's */
669 for( i
= 0; ptrArray
[i
] != NULL
; i
++ ) {
677 * Parse LDAP search string, building list of LDAP criteria attributes. This
678 * may be used to convert an old style Sylpheed LDAP search criteria to the
679 * new format. The old style uses a standard LDAP search string, for example:
681 * (&(mail=*)(cn=%s*))
683 * This function extracts the two LDAP attributes <code>mail</code> and
684 * <code>cn</code>, adding each to a list.
686 * \param ctl Control object to process.
687 * \param criteria LDAP search criteria string.
689 void ldapctl_parse_ldap_search( LdapControl
*ctl
, gchar
*criteria
) {
695 cm_return_if_fail( ctl
!= NULL
);
697 ldapctl_criteria_list_clear( ctl
);
698 if( criteria
== NULL
) return;
709 attrib
= g_strndup( pFrom
, iLen
);
710 g_strstrip( attrib
);
711 ldapctl_criteria_list_add( ctl
, attrib
);
721 * Return the default LDAP search criteria string.
722 * \return Formatted string or <i>""</i>. Should be g_free() when done.
724 gchar
*ldapctl_get_default_criteria() {
725 gchar
*retVal
= g_strdup(LDAPCTL_DFL_ATTR_LIST
);
726 const gchar
**attrs
= ATTRIBUTE
;
729 gchar
*tmp
= g_strdup_printf("%s, %s", retVal
, *attrs
++);
733 debug_print("default search criteria: %s\n", retVal
);
738 * Return the default LDAP search criteria list.
739 * \return GList or <i>NULL</i>.
741 GList
*ldapctl_get_default_criteria_list() {
742 gchar
*criteria
, *item
;
743 gchar
**c_list
, **w_list
;
744 GList
*attr_list
= NULL
;
746 criteria
= ldapctl_get_default_criteria();
747 c_list
= g_strsplit(criteria
, " ", 0);
751 while ((criteria
= *w_list
++) != 0) {
752 /* copy string elimination <,> */
754 item
= g_strndup(criteria
, strlen(criteria
) - 1);
756 item
= g_strdup(criteria
);
757 debug_print("adding attribute to list: %s\n", item
);
758 attr_list
= g_list_append(attr_list
, item
);
765 * Compare to GList for equality.
766 * \param l1 First GList
767 * \param l2 Second GList
768 * \Return TRUE or FALSE
770 gboolean
ldapctl_compare_list(GList
*l1
, GList
*l2
) {
771 gchar
*first
, *second
;
774 if ((! l1
&& l2
) || (l1
&& ! l2
))
777 first
= (gchar
*) l1
->data
;
778 second
= (gchar
*) l2
->data
;
779 /*debug_print("comparing: %s = %s\n", first, second);*/
780 if ( ! (first
&& second
) || strcmp(first
, second
) != 0) {
783 l1
= g_list_next(l1
);
784 l2
= g_list_next(l2
);
789 #endif /* USE_LDAP */