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 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 * Some utility functions to access LDAP servers.
36 #include "common/utils.h"
38 #define SYLDAP_TEST_FILTER "(objectclass=*)"
39 #define SYLDAP_SEARCHBASE_V2 "cn=config"
40 #define SYLDAP_SEARCHBASE_V3 ""
41 #define SYLDAP_V2_TEST_ATTR "database"
42 #define SYLDAP_V3_TEST_ATTR "namingcontexts"
45 * Attempt to discover the base DN for a server using LDAP version 3.
46 * \param ld LDAP handle for a connected server.
47 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
48 * \return List of Base DN's, or NULL if could not read. List should be
51 static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
54 LDAPMessage *result = NULL, *e;
59 struct timeval timeout;
70 /* Test for LDAP version 3 */
71 attribs[0] = SYLDAP_V3_TEST_ATTR;
73 rc = ldap_search_ext_s(
74 ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
75 attribs, 0, NULL, NULL, &timeout, 0, &result );
77 if( rc == LDAP_SUCCESS ) {
79 for( e = ldap_first_entry( ld, result );
81 e = ldap_next_entry( ld, e ) )
83 /* Process attributes */
84 for( attribute = ldap_first_attribute( ld, e, &ber );
86 attribute = ldap_next_attribute( ld, e, ber ) )
89 attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
91 vals = ldap_get_values_len( ld, e, attribute );
93 for( i = 0; vals[i] != NULL; i++ ) {
94 baseDN = g_list_append(
95 baseDN, g_strndup( vals[i]->bv_val, vals[i]->bv_len ) );
98 ldap_value_free_len( vals );
100 ldap_memfree( attribute );
111 ldap_msgfree( result );
116 * Attempt to discover the base DN for a server using LDAP version 2.
117 * \param ld LDAP handle for a connected server.
118 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
119 * \return List of Base DN's, or NULL if could not read. List should be
120 * g_free() when done.
122 static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
123 GList *baseDN = NULL;
125 LDAPMessage *result = NULL, *e;
129 struct berval **vals;
130 struct timeval timeout;
133 timeout.tv_usec = 0L;
135 timeout.tv_sec = tov;
138 timeout.tv_sec = 30L;
142 rc = ldap_search_ext_s(
143 ld, SYLDAP_SEARCHBASE_V2, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
144 attribs, 0, NULL, NULL, &timeout, 0, &result );
146 if( rc == LDAP_SUCCESS ) {
147 /* Process entries */
148 for( e = ldap_first_entry( ld, result );
150 e = ldap_next_entry( ld, e ) )
152 /* Process attributes */
153 for( attribute = ldap_first_attribute( ld, e, &ber );
155 attribute = ldap_next_attribute( ld, e, ber ) )
159 SYLDAP_V2_TEST_ATTR ) == 0 ) {
160 vals = ldap_get_values_len( ld, e, attribute );
162 for( i = 0; vals[i] != NULL; i++ ) {
165 * Strip the 'ldb:' from the
166 * front of the value.
168 tmp = g_strndup( vals[i]->bv_val, vals[i]->bv_len);
169 ch = ( char * ) strchr( tmp, ':' );
171 gchar *bn = g_strdup( ++ch );
174 baseDN = g_list_append(
175 baseDN, g_strdup( bn ) );
181 ldap_value_free_len( vals );
183 ldap_memfree( attribute );
192 ldap_msgfree( result );
196 int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
200 if ( passwd != NULL ) {
201 cred.bv_val = (char *) passwd;
202 cred.bv_len = strlen( passwd );
208 debug_print("binding: DN->%s\n", dn?dn:"null");
209 return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
214 * Attempt to discover the base DN for the server.
215 * \param host Host name.
216 * \param port Port number.
217 * \param bindDN Bind DN (optional).
218 * \param bindPW Bind PW (optional).
219 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
220 * \return List of Base DN's, or NULL if could not read. This list should be
221 * g_free() when done.
223 GList *ldaputil_read_basedn(
224 const gchar *host, const gint port, const gchar *bindDN,
225 const gchar *bindPW, const gint tov, int ssl, int tls )
227 GList *baseDN = NULL;
235 if( host == NULL ) return baseDN;
236 if( port < 1 ) return baseDN;
238 /* Connect to server. */
240 uri = g_strdup_printf("ldap%s://%s:%d",
243 debug_print("URI: %s\n", uri);
244 rc = ldap_initialize(&ld, uri);
251 if ((bindDN && *bindDN)
256 version = LDAP_VERSION3;
257 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
263 if( rc != LDAP_OPT_SUCCESS ) {
264 ldap_unbind_ext( ld, NULL, NULL );
267 rc = ldap_start_tls_s( ld, NULL, NULL );
269 ldap_unbind_ext( ld, NULL, NULL );
275 /* Bind to the server, if required */
277 if( *bindDN != '\0' ) {
278 rc = claws_ldap_simple_bind_s( ld, bindDN, bindPW );
279 if( rc != LDAP_SUCCESS ) {
280 g_printerr("LDAP: %s\n", ldap_err2string(rc));
281 ldap_unbind_ext( ld, NULL, NULL );
287 /* Test for LDAP version 3 */
288 baseDN = ldaputil_test_v3( ld, tov, &rc );
290 debug_print("Using LDAP v3\n");
293 if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
294 baseDN = ldaputil_test_v2( ld, tov );
296 debug_print("Using LDAP v2\n");
299 if (ld && !LDAP_API_ERROR(rc))
300 ldap_unbind_ext( ld, NULL, NULL );
306 * Attempt to connect to the server.
308 * \param host Host name.
309 * \param port Port number.
310 * \return <i>TRUE</i> if connected successfully.
312 gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls ) {
313 gboolean retVal = FALSE;
321 if( host == NULL ) return retVal;
322 if( port < 1 ) return retVal;
324 uri = g_strdup_printf("ldap%s://%s:%d",
327 debug_print("URI: %s\n", uri);
328 ldap_initialize(&ld, uri);
335 GList *dummy = ldaputil_test_v3( ld, 10, &rc );
338 if (LDAP_API_ERROR(rc))
344 version = LDAP_VERSION3;
345 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
346 if( rc != LDAP_OPT_SUCCESS ) {
347 ldap_unbind_ext( ld, NULL, NULL );
351 rc = ldap_start_tls_s( ld, NULL, NULL );
353 ldap_unbind_ext( ld, NULL, NULL );
359 ldap_unbind_ext( ld, NULL, NULL );
360 debug_print("ld != NULL\n");
367 * Test whether LDAP libraries installed.
368 * Return: TRUE if library available.
370 gboolean ldaputil_test_ldap_lib( void ) {
374 #endif /* USE_LDAP */