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 necessary to access LDAP servers.
25 #include "claws-features.h"
31 #include <glib/gi18n.h>
37 #include "addrcache.h"
39 #include "ldapquery.h"
40 #include "ldapserver.h"
43 #include "adbookbase.h"
44 #include "passwordstore.h"
48 * Create new LDAP server interface object with no control object.
49 * \return Initialized LDAP server object.
51 LdapServer
*ldapsvr_create_noctl( void ) {
54 server
= g_new0( LdapServer
, 1 );
55 server
->type
= ADBOOKTYPE_LDAP
;
56 server
->addressCache
= addrcache_create();
57 server
->retVal
= MGU_SUCCESS
;
58 server
->control
= NULL
;
59 server
->listQuery
= NULL
;
60 server
->searchFlag
= FALSE
;
65 * Create new LDAP server interface object.
66 * \return Initialized LDAP server object.
68 LdapServer
*ldapsvr_create( void ) {
70 server
= ldapsvr_create_noctl();
71 server
->control
= ldapctl_create();
76 * Return name of server.
77 * \param server Server object.
78 * \return Name for server.
80 gchar
*ldapsvr_get_name( LdapServer
*server
) {
81 cm_return_val_if_fail( server
!= NULL
, NULL
);
82 return addrcache_get_name( server
->addressCache
);
86 * Specify name to be used.
87 * \param server Server object.
88 * \param value Name for server.
90 void ldapsvr_set_name( LdapServer
* server
, const gchar
*value
) {
91 cm_return_if_fail( server
!= NULL
);
92 addrcache_set_name( server
->addressCache
, value
);
93 debug_print("setting name: %s\n", value
?value
:"null");
97 * Refresh internal variables to force a file read.
98 * \param server Server object.
100 void ldapsvr_force_refresh( LdapServer
*server
) {
101 cm_return_if_fail( server
!= NULL
);
102 addrcache_refresh( server
->addressCache
);
106 * Return status/error code.
107 * \param server Server object.
108 * \return Status/error code.
110 gint
ldapsvr_get_status( LdapServer
*server
) {
111 cm_return_val_if_fail( server
!= NULL
, -1 );
112 return server
->retVal
;
116 * Return reference to root level folder.
117 * \param server Server object.
118 * \return Root level folder.
120 ItemFolder
*ldapsvr_get_root_folder( LdapServer
*server
) {
121 cm_return_val_if_fail( server
!= NULL
, NULL
);
123 debug_print( "ldapsvr_get_root_folder/start\n" );
124 ldapsvr_print_data( server
, stdout
);
125 debug_print( "ldapsvr_get_root_folder/done\n" );
127 return addrcache_get_root_folder( server
->addressCache
);
131 * Test whether server data has been accessed.
132 * \param server Server object.
133 * \return <i>TRUE</i> if data was accessed.
135 gboolean
ldapsvr_get_accessed( LdapServer
*server
) {
136 cm_return_val_if_fail( server
!= NULL
, FALSE
);
137 return server
->addressCache
->accessFlag
;
141 * Specify that server's data whas beed accessed.
142 * \param server Server object.
143 * \param value Value for flag.
145 void ldapsvr_set_accessed( LdapServer
*server
, const gboolean value
) {
146 cm_return_if_fail( server
!= NULL
);
147 server
->addressCache
->accessFlag
= value
;
148 debug_print("setting accessFlag: %d\n", value
);
152 * Test whether server data has been modified.
153 * \param server Server object.
154 * \return <i>TRUE</i> if data was modified.
156 gboolean
ldapsvr_get_modified( LdapServer
*server
) {
157 cm_return_val_if_fail( server
!= NULL
, FALSE
);
158 return server
->addressCache
->modified
;
162 * Specify modify flag.
163 * \param server Server object.
164 * \param value Value for flag.
166 void ldapsvr_set_modified( LdapServer
*server
, const gboolean value
) {
167 cm_return_if_fail( server
!= NULL
);
168 server
->addressCache
->modified
= value
;
169 debug_print("setting modified: %d\n", value
);
173 * Test whether data was read from server.
174 * \param server Server object.
175 * \return <i>TRUE</i> if data was read.
177 gboolean
ldapsvr_get_read_flag( LdapServer
*server
) {
178 cm_return_val_if_fail( server
!= NULL
, FALSE
);
179 return server
->addressCache
->dataRead
;
183 * Test whether server is to be used for dynamic searches.
184 * \param server Server object.
185 * \return <i>TRUE</i> if server is used for dynamic searches.
187 gboolean
ldapsvr_get_search_flag( LdapServer
*server
) {
188 cm_return_val_if_fail( server
!= NULL
, FALSE
);
189 return server
->searchFlag
;
193 * Specify that server is to be used for dynamic searches.
194 * \param server Server object.
195 * \param value Name for server.
197 void ldapsvr_set_search_flag( LdapServer
*server
, const gboolean value
) {
198 cm_return_if_fail( server
!= NULL
);
199 server
->searchFlag
= value
;
200 debug_print("setting searchFlag: %d\n", value
);
204 * Specify the reference to control data that will be used for the query. The calling
205 * module should be responsible for creating and destroying this control object.
206 * \param server Server object.
207 * \param ctl Control data.
209 void ldapsvr_set_control( LdapServer
*server
, LdapControl
*ctl
) {
210 cm_return_if_fail( server
!= NULL
);
211 addrcache_refresh( server
->addressCache
);
212 server
->control
= ctl
;
217 * \param server Server object.
219 void ldapsvr_free_all_query( LdapServer
*server
) {
221 cm_return_if_fail( server
!= NULL
);
223 node
= server
->listQuery
;
225 LdapQuery
*qry
= node
->data
;
228 node
= g_list_next( node
);
230 g_list_free( server
->listQuery
);
231 server
->listQuery
= NULL
;
235 * Add query to server.
236 * \param server Server object.
237 * \param qry Query object.
239 void ldapsvr_add_query( LdapServer
*server
, LdapQuery
*qry
) {
240 cm_return_if_fail( server
!= NULL
);
241 cm_return_if_fail( qry
!= NULL
);
243 server
->listQuery
= g_list_append( server
->listQuery
, qry
);
244 qry
->server
= server
;
248 * Free up LDAP server interface object by releasing internal memory.
249 * \param server Server object.
251 void ldapsvr_free( LdapServer
*server
) {
252 cm_return_if_fail( server
!= NULL
);
254 /* Stop and cancel any queries that may be active */
255 ldapsvr_stop_all_query( server
);
256 ldapsvr_cancel_all_query( server
);
259 addrcache_clear( server
->addressCache
);
260 addrcache_free( server
->addressCache
);
262 /* Free LDAP control block */
263 ldapctl_free( server
->control
);
264 server
->control
= NULL
;
266 /* Free all queries */
267 ldapsvr_free_all_query( server
);
270 server
->type
= ADBOOKTYPE_NONE
;
271 server
->addressCache
= NULL
;
272 server
->retVal
= MGU_SUCCESS
;
273 server
->listQuery
= NULL
;
274 server
->searchFlag
= FALSE
;
276 /* Now release LDAP object */
282 * Display object to specified stream.
283 * \param server Server object.
284 * \param stream Output stream.
286 void ldapsvr_print_data( LdapServer
*server
, FILE *stream
) {
290 cm_return_if_fail( server
!= NULL
);
292 fprintf( stream
, "LdapServer:\n" );
293 fprintf( stream
, " ret val: %d\n", server
->retVal
);
294 fprintf( stream
, "srch flag: %s\n",
295 server
->searchFlag
? "yes" : "no" );
296 if( server
->control
) {
297 ldapctl_print( server
->control
, stream
);
300 fprintf( stream
, " control: NULL\n" );
302 addrcache_print( server
->addressCache
, stream
);
303 addritem_print_item_folder( server
->addressCache
->rootFolder
, stream
);
307 node
= server
->listQuery
;
309 LdapQuery
*qry
= node
->data
;
310 fprintf( stream
, " query: %2d : %s\n", i
, ADDRQUERY_NAME(qry
) );
312 node
= g_list_next( node
);
318 * Return link list of persons.
319 * \param server Server object.
320 * \return List of persons.
322 GList
*ldapsvr_get_list_person( LdapServer
*server
) {
323 cm_return_val_if_fail( server
!= NULL
, NULL
);
324 return addrcache_get_list_person( server
->addressCache
);
328 * Return link list of folders. There are no "real" folders that are returned
330 * \param server Server object.
331 * \return List of folders.
333 GList
*ldapsvr_get_list_folder( LdapServer
*server
) {
334 cm_return_val_if_fail( server
!= NULL
, NULL
);
335 /* return addrcache_get_list_folder( server->addressCache ); */
340 * Execute specified query.
341 * \param server LDAP server.
342 * \param qry LDAP query.
344 void ldapsvr_execute_query( LdapServer
*server
, LdapQuery
*qry
) {
345 LdapControl
*ctlCopy
;
347 cm_return_if_fail( server
!= NULL
);
348 cm_return_if_fail( qry
!= NULL
);
350 /* Copy server's control data to the query */
351 ctlCopy
= ldapctl_create();
352 ldapctl_copy( server
->control
, ctlCopy
);
353 ldapqry_set_control( qry
, ctlCopy
);
354 ldapqry_initialize();
357 debug_print("ldapsvr_execute_query::checking query...\n");
358 if( ldapqry_check_search( qry
) ) {
359 debug_print("ldapsvr_execute_query::reading with thread...\n");
360 ldapqry_read_data_th( qry
);
361 if(qry
->server
->retVal
== LDAPRC_SUCCESS
) {
362 debug_print("ldapsvr_execute_query::SUCCESS with thread...\n");
365 debug_print("ldapsvr_execute_query... terminated\n");
369 * Stop all queries for specified ID.
370 * \param server Server object.
371 * \param queryID Query ID to stop.
373 void ldapsvr_stop_query_id( LdapServer
*server
, const gint queryID
) {
375 cm_return_if_fail( server
!= NULL
);
377 node
= server
->listQuery
;
379 LdapQuery
*qry
= node
->data
;
380 if( ADDRQUERY_ID(qry
) == queryID
) {
381 /* Notify thread to stop */
382 ldapqry_set_stop_flag( qry
, TRUE
);
384 node
= g_list_next( node
);
389 * Stop all queries by notifying each thread to stop.
390 * \param server Server object.
392 void ldapsvr_stop_all_query( LdapServer
*server
) {
394 cm_return_if_fail( server
!= NULL
);
396 node
= server
->listQuery
;
398 LdapQuery
*qry
= node
->data
;
399 ldapqry_set_stop_flag( qry
, TRUE
);
400 node
= g_list_next( node
);
405 * Cancel all query threads for server.
406 * \param server Server object.
408 void ldapsvr_cancel_all_query( LdapServer
*server
) {
410 cm_return_if_fail( server
!= NULL
);
412 node
= server
->listQuery
;
414 LdapQuery
*qry
= node
->data
;
415 /* Notify thread to stop */
416 ldapqry_set_stop_flag( qry
, TRUE
);
417 /* Now cancel thread */
418 ldapqry_cancel( qry
);
419 node
= g_list_next( node
);
424 * Search most recent query for specified search term. The most recent
425 * completed query is returned. If no completed query is found, the most recent
426 * incomplete is returned.
427 * \param server LdapServer.
428 * \param searchTerm Search term to locate.
429 * \return Query object, or <i>NULL</i> if none found.
431 static LdapQuery
*ldapsvr_locate_query(
432 const LdapServer
*server
, const gchar
*searchTerm
)
434 LdapQuery
*incomplete
= NULL
;
436 cm_return_val_if_fail( server
!= NULL
, NULL
);
438 node
= server
->listQuery
;
439 node
= g_list_last( node
);
440 /* Search backwards for query */
442 LdapQuery
*qry
= node
->data
;
443 if( g_utf8_collate( ADDRQUERY_SEARCHVALUE(qry
), searchTerm
) == 0 ) {
444 if( qry
->agedFlag
) continue;
445 if( qry
->completed
) {
453 node
= g_list_previous( node
);
459 * Retire aged queries. Only the following queries are retired:
461 * a) Dynamic queries.
462 * b) Explicit searches that have a hidden folders.
463 * c) Locate searches that have a hidden folder.
465 * \param server LdapServer.
467 void ldapsvr_retire_query( LdapServer
*server
) {
475 debug_print("ldapsvr_retire_query\n");
476 cm_return_if_fail( server
!= NULL
);
477 ctl
= server
->control
;
478 maxAge
= ctl
->maxQueryAge
;
480 /* Identify queries to age and move to deletion list */
482 node
= server
->listQuery
;
484 LdapQuery
*qry
= node
->data
;
486 node
= g_list_next( node
);
487 folder
= ADDRQUERY_FOLDER(qry
);
488 if( folder
== NULL
) continue;
489 if( ! folder
->isHidden
) {
490 if( ADDRQUERY_SEARCHTYPE(qry
) == ADDRSEARCH_EXPLICIT
) continue;
491 if( ADDRQUERY_SEARCHTYPE(qry
) == ADDRSEARCH_LOCATE
) continue;
494 ldapqry_age( qry
, maxAge
);
495 if( qry
->agedFlag
) {
496 /* Delete folder associated with query */
497 debug_print("deleting folder... ::%s::\n",
498 ADDRQUERY_NAME(qry
)?ADDRQUERY_NAME(qry
):"null");
499 ldapqry_delete_folder( qry
);
500 listDelete
= g_list_append( listDelete
, qry
);
505 listQuery
= server
->listQuery
;
508 LdapQuery
*qry
= node
->data
;
510 listQuery
= g_list_remove( listQuery
, qry
);
513 node
= g_list_next( node
);
515 server
->listQuery
= listQuery
;
517 /* Free up deletion list */
518 g_list_free( listDelete
);
522 * Return results of a previous query by executing callback for each address
523 * contained in specified folder.
525 * \param folder Address book folder to process.
526 * \param req Address query request object.
528 static void ldapsvr_previous_query(
529 const ItemFolder
*folder
, const QueryRequest
*req
, AddrQueryObject
*aqo
)
531 AddrSearchCallbackEntry
*callBack
;
538 callBack
= ( AddrSearchCallbackEntry
* ) req
->callBackEntry
;
541 node
= folder
->listPerson
;
543 AddrItemObject
*aio
= node
->data
;
544 if( aio
&& aio
->type
== ITEMTYPE_PERSON
) {
545 ItemPerson
*person
= node
->data
;
546 nodeEM
= person
->listEMail
;
548 ItemEMail
*email
= nodeEM
->data
;
550 nodeEM
= g_list_next( nodeEM
);
551 listEMail
= g_list_append( listEMail
, email
);
554 node
= g_list_next( node
);
556 ( callBack
) ( sender
, req
->queryID
, listEMail
, NULL
);
557 /* // g_list_free( listEMail ); */
562 * Reuse search results from a previous LDAP query. If there is a query that
563 * has the same search term as specified in the query request, then the query
566 * \param server LDAP server object.
567 * \param req Address query object.
568 * \return <i>TRUE</i> if previous query was used.
570 gboolean
ldapsvr_reuse_previous( const LdapServer
*server
, const QueryRequest
*req
) {
575 cm_return_val_if_fail( server
!= NULL
, FALSE
);
576 cm_return_val_if_fail( req
!= NULL
, FALSE
);
578 searchTerm
= req
->searchTerm
;
580 /* Test whether any queries for the same term exist */
581 qry
= ldapsvr_locate_query( server
, searchTerm
);
583 /* Touch query to ensure it hangs around for a bit longer */
584 ldapqry_touch( qry
);
585 folder
= ADDRQUERY_FOLDER(qry
);
587 ldapsvr_previous_query( folder
, req
, ADDRQUERY_OBJECT(qry
) );
595 * Construct a new LdapQuery object that will be used to perform an dynamic
598 * \param server LdapServer.
599 * \param req Query request.
600 * \return LdapQuery object, or <i>NULL</i> if none created.
602 LdapQuery
*ldapsvr_new_dynamic_search( LdapServer
*server
, QueryRequest
*req
)
609 cm_return_val_if_fail( server
!= NULL
, NULL
);
610 cm_return_val_if_fail( req
!= NULL
, NULL
);
612 /* Retire any aged queries */
613 /* // ldapsvr_retire_query( server ); */
615 /* Name of folder and query */
616 searchTerm
= req
->searchTerm
;
617 name
= g_strdup_printf( "Search '%s'", searchTerm
);
619 /* Create a folder for the search results */
620 folder
= addrcache_add_new_folder( server
->addressCache
, NULL
);
621 addritem_folder_set_name( folder
, name
);
622 addritem_folder_set_remarks( folder
, "" );
624 /* Construct a query */
625 qry
= ldapqry_create();
626 ldapqry_set_query_id( qry
, req
->queryID
);
627 ldapqry_set_search_value( qry
, searchTerm
);
628 ldapqry_set_search_type( qry
, ADDRSEARCH_DYNAMIC
);
629 ldapqry_set_callback_entry( qry
, req
->callBackEntry
);
630 ldapqry_set_callback_end( qry
, req
->callBackEnd
);
632 /* Specify folder type and back reference */
633 ADDRQUERY_FOLDER(qry
) = folder
;
634 folder
->folderType
= ADDRFOLDER_QUERY_RESULTS
;
635 folder
->folderData
= ( gpointer
) qry
;
636 folder
->isHidden
= TRUE
;
639 ldapqry_set_name( qry
, name
);
642 /* Add query to request */
643 qryreq_add_query( req
, ADDRQUERY_OBJECT(qry
) );
645 /* Now start the search */
646 ldapsvr_add_query( server
, qry
);
652 * Construct a new LdapQuery object that will be used to perform an explicit
655 * \param server LdapServer.
656 * \param req Query request.
657 * \param folder Folder that will be used to contain search results.
658 * \return LdapQuery object, or <i>NULL</i> if none created.
660 LdapQuery
*ldapsvr_new_explicit_search(
661 LdapServer
*server
, QueryRequest
*req
, ItemFolder
*folder
)
667 cm_return_val_if_fail( server
!= NULL
, NULL
);
668 cm_return_val_if_fail( req
!= NULL
, NULL
);
669 cm_return_val_if_fail( folder
!= NULL
, NULL
);
671 /* Retire any aged queries */
672 /* // ldapsvr_retire_query( server ); */
675 searchTerm
= req
->searchTerm
;
676 name
= g_strdup_printf( "Explicit search for '%s'", searchTerm
);
678 /* Construct a query */
679 qry
= ldapqry_create();
680 ldapqry_set_query_id( qry
, req
->queryID
);
681 ldapqry_set_name( qry
, name
);
682 ldapqry_set_search_value( qry
, searchTerm
);
683 ldapqry_set_search_type( qry
, ADDRSEARCH_EXPLICIT
);
684 ldapqry_set_callback_end( qry
, req
->callBackEnd
);
685 ldapqry_set_callback_entry( qry
, req
->callBackEntry
);
687 /* Specify folder type and back reference */
688 ADDRQUERY_FOLDER(qry
) = folder
;
689 folder
->folderType
= ADDRFOLDER_QUERY_RESULTS
;
690 folder
->folderData
= ( gpointer
) qry
;
693 ldapsvr_add_query( server
, qry
);
695 /* Set up query request */
696 qryreq_add_query( req
, ADDRQUERY_OBJECT(qry
) );
703 gint
ldapsvr_read_data( LdapServer
*server
)
707 cm_return_val_if_fail( server
!= NULL
, -1 );
709 name
= addrcache_get_name(server
->addressCache
);
710 debug_print("...addrbook_read_data :%s:\n", name
?name
:"null");
712 addrcache_clear(server
->addressCache
);
713 ldapsvr_free_all_query( server
);
714 server
->listQuery
= NULL
;
715 server
->addressCache
->modified
= FALSE
;
716 server
->addressCache
->accessFlag
= FALSE
;
717 server
->addressCache
->dataRead
= TRUE
;
718 addrcache_set_dirty(server
->addressCache
, FALSE
);
722 void ldapsrv_set_options (gint secs
, LDAP
*ld
)
725 static struct timeval timeout
;
726 timeout
.tv_sec
= secs
;
729 i
= LDAP_OPT_X_TLS_ALLOW
;
730 rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_REQUIRE_CERT
, &i
);
732 debug_print("cert %s\n", ldaputil_get_error(ld
));
734 debug_print("cert %s\n", ldap_err2string(rc
));
735 /* can crash old libldaps... */
736 rc
= ldap_set_option(NULL
, LDAP_OPT_NETWORK_TIMEOUT
, &timeout
);
738 debug_print("tm %s\n", ldaputil_get_error(ld
));
740 debug_print("tm %s\n", ldap_err2string(rc
));
746 #define LDAP_START_TLS_S "ldap_start_tls_sW"
747 typedef ULONG (* PFldap_start_tls_s
) (LDAP
*, PULONG
, LDAPMessage
**, PLDAPControlW
*, PLDAPControlW
*);
749 #define LDAP_START_TLS_S "ldap_start_tls_sA"
750 typedef ULONG (* PFldap_start_tls_s
) (LDAP
*, PULONG
, LDAPMessage
**, PLDAPControlA
*, PLDAPControlA
*);
751 #endif /* LDAP_UNICODE */
752 PFldap_start_tls_s Win32_ldap_start_tls_s
= NULL
;
756 * Connect to LDAP server.
757 * \param ctl Control object to process.
758 * \return LDAP Resource to LDAP.
760 LDAP
*ldapsvr_connect(LdapControl
*ctl
) {
770 cm_return_val_if_fail(ctl
!= NULL
, NULL
);
772 ldapsrv_set_options (ctl
->timeOut
, NULL
);
774 uri
= g_strdup_printf("ldaps://%s:%d", ctl
->hostName
, ctl
->port
);
776 uri
= g_strdup_printf("ldap://%s:%d", ctl
->hostName
, ctl
->port
);
778 ldap_initialize(&ld
, uri
);
780 ld
= ldap_sslinit(ctl
->hostName
, ctl
->port
, ctl
->enableSSL
);
781 if (ld
&& ctl
->enableSSL
) {
782 version
= LDAP_VERSION3
;
783 debug_print("Setting version 3\n");
784 rc
= ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, (void *)&version
);
785 if (rc
== LDAP_SUCCESS
) {
786 ctl
->version
= LDAP_VERSION3
;
787 log_print(LOG_PROTOCOL
, "LDAP (options): set version 3\n");
789 log_error(LOG_PROTOCOL
, _("LDAP error (options): %d (%s)\n"),
790 rc
, ldaputil_get_error(ld
));
791 debug_print("Failed: %s\n", ldaputil_get_error(ld
));
794 rc
= ldap_get_option(ld
, LDAP_OPT_SSL
, (void*)&op
);
795 if (rc
!= LDAP_SUCCESS
) {
796 log_warning(LOG_PROTOCOL
, _("LDAP warning (options): can't get TLS state\n"));
797 debug_print("Can't get TLS state\n");
800 if ((void *)op
!= LDAP_OPT_ON
) {
801 debug_print("Enabling TLS\n");
802 rc
= ldap_set_option(ld
, LDAP_OPT_SSL
, LDAP_OPT_ON
);
803 if (rc
!= LDAP_SUCCESS
) {
804 log_error(LOG_PROTOCOL
, _("LDAP error (options): %d (%s)\n"),
805 rc
, ldaputil_get_error(ld
));
806 debug_print("Failed: %s\n", ldaputil_get_error(ld
));
808 rc
= ldap_get_option(ld
, LDAP_OPT_SSL
, (void*)&op
);
809 if (rc
!= LDAP_SUCCESS
) {
810 log_error(LOG_PROTOCOL
, _("LDAP error (options): %d (%s)\n"),
811 rc
, ldaputil_get_error(ld
));
813 log_print(LOG_PROTOCOL
, _("LDAP (options): TLS enabled (%d)\n"), (gint
)op
);
815 debug_print("TLS now %d\n", (gint
)op
);
819 if (!ld
|| (rc
= ldap_connect(ld
, NULL
)) != LDAP_SUCCESS
) {
820 log_error(LOG_PROTOCOL
, _("LDAP error (connect): %d (%s)\n"),
821 rc
, ldaputil_get_error(ld
));
822 debug_print("ldap_connect failed: %d %s\n", rc
, ldaputil_get_error(ld
));
824 log_print(LOG_PROTOCOL
, _("LDAP (connect): completed successfully\n"));
833 debug_print("Got handle to LDAP host %s on port %d\n", ctl
->hostName
, ctl
->port
);
835 version
= LDAP_VERSION3
;
836 debug_print("Setting version 3\n");
837 rc
= ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
838 if (rc
== LDAP_OPT_SUCCESS
) {
839 ctl
->version
= LDAP_VERSION3
;
840 log_print(LOG_PROTOCOL
, "LDAP (options): set version 3\n");
842 log_error(LOG_PROTOCOL
, _("LDAP error (options): %d (%s)\n"),
843 rc
, ldaputil_get_error(ld
));
846 #if (defined USE_LDAP_TLS || defined G_OS_WIN32)
848 if (ctl
->version
== LDAP_VERSION3
) {
849 if (ctl
->enableTLS
&& !ctl
->enableSSL
) {
852 if (Win32_ldap_start_tls_s
== NULL
) {
853 void *lib
= LoadLibrary("wldap32.dll");
854 if (!lib
|| (Win32_ldap_start_tls_s
= (PFldap_start_tls_s
) GetProcAddress(lib
, LDAP_START_TLS_S
)) == NULL
) {
855 log_error(LOG_PROTOCOL
, _("LDAP error (TLS): "
856 "ldap_start_tls_s not supported on this platform\n"));
862 debug_print("Setting STARTTLS\n");
863 rc
= Win32_ldap_start_tls_s(ld
, &serv_rc
, NULL
, NULL
, NULL
);
864 debug_print("ldap_start_tls_s: %d server %ld %s\n",
865 rc
, serv_rc
, ldaputil_get_error(ld
));
867 debug_print("Setting STARTTLS\n");
868 rc
= ldap_start_tls_s(ld
, NULL
, NULL
);
870 if (rc
!= LDAP_SUCCESS
) {
871 log_error(LOG_PROTOCOL
, _("LDAP error (TLS): ldap_start_tls_s: %d (%s)\n"),
872 rc
, ldaputil_get_error(ld
));
875 log_print(LOG_PROTOCOL
, _("LDAP (TLS): started successfully\n"));
876 debug_print("Done\n");
882 /* Bind to the server, if required */
884 if (* ctl
->bindDN
!= '\0') {
885 pwd
= passwd_store_get(PWS_CORE
, "LDAP", ctl
->hostName
);
886 rc
= claws_ldap_simple_bind_s(ld
, ctl
->bindDN
, pwd
);
887 if (pwd
!= NULL
&& strlen(pwd
) > 0)
888 memset(pwd
, 0, strlen(pwd
));
890 if (rc
!= LDAP_SUCCESS
) {
891 log_error(LOG_PROTOCOL
, _("LDAP error (bind): binding DN '%s': %d (%s)\n" ),
892 ctl
->bindDN
, rc
, ldaputil_get_error(ld
));
895 log_print(LOG_PROTOCOL
, _("LDAP (bind): successfully for DN '%s'\n"),
903 * Disconnect to LDAP server.
904 * \param ld Resource to LDAP.
906 void ldapsvr_disconnect(LDAP
*ld
) {
909 cm_return_if_fail(ld
!= NULL
);
910 rc
= ldap_unbind_ext(ld
, NULL
, NULL
);
911 if (rc
!= LDAP_SUCCESS
) {
912 log_error(LOG_PROTOCOL
, _("LDAP error (unbind): %d (%s)\n"),
913 rc
, ldaputil_get_error(ld
));
915 log_print(LOG_PROTOCOL
, _("LDAP (unbind): successful\n"));
919 #endif /* USE_LDAP */