2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2011 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.
31 #include <glib/gi18n.h>
35 #include "common/utils.h"
37 #include "ldapserver.h"
40 #define SYLDAP_TEST_FILTER "(objectclass=*)"
41 #define SYLDAP_SEARCHBASE_V2 "cn=config"
42 #define SYLDAP_SEARCHBASE_V3 ""
43 #define SYLDAP_V2_TEST_ATTR "database"
44 #define SYLDAP_V3_TEST_ATTR "namingcontexts"
47 * Attempt to discover the base DN for a server using LDAP version 3.
48 * \param ld LDAP handle for a connected server.
49 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
50 * \return List of Base DN's, or NULL if could not read. List should be
53 static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
56 LDAPMessage *result = NULL, *e;
61 struct timeval timeout;
72 /* Test for LDAP version 3 */
73 attribs[0] = SYLDAP_V3_TEST_ATTR;
75 rc = ldap_search_ext_s(
76 ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
77 attribs, 0, NULL, NULL, &timeout, 0, &result );
79 if( rc == LDAP_SUCCESS ) {
81 for( e = ldap_first_entry( ld, result );
83 e = ldap_next_entry( ld, e ) )
85 /* Process attributes */
86 for( attribute = ldap_first_attribute( ld, e, &ber );
88 attribute = ldap_next_attribute( ld, e, ber ) )
91 attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
93 vals = ldap_get_values_len( ld, e, attribute );
95 for( i = 0; vals[i] != NULL; i++ ) {
96 baseDN = g_list_append(
97 baseDN, g_strndup( vals[i]->bv_val, vals[i]->bv_len ) );
100 ldap_value_free_len( vals );
102 ldap_memfree( attribute );
110 debug_print("LDAP: Error %d (%s)\n", rc, ldaputil_get_error(ld));
115 ldap_msgfree( result );
120 * Attempt to discover the base DN for a server using LDAP version 2.
121 * \param ld LDAP handle for a connected server.
122 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
123 * \return List of Base DN's, or NULL if could not read. List should be
124 * g_free() when done.
126 static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
127 GList *baseDN = NULL;
129 LDAPMessage *result = NULL, *e;
133 struct berval **vals;
134 struct timeval timeout;
137 timeout.tv_usec = 0L;
139 timeout.tv_sec = tov;
142 timeout.tv_sec = 30L;
146 rc = ldap_search_ext_s(
147 ld, SYLDAP_SEARCHBASE_V2, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
148 attribs, 0, NULL, NULL, &timeout, 0, &result );
150 if( rc == LDAP_SUCCESS ) {
151 /* Process entries */
152 for( e = ldap_first_entry( ld, result );
154 e = ldap_next_entry( ld, e ) )
156 /* Process attributes */
157 for( attribute = ldap_first_attribute( ld, e, &ber );
159 attribute = ldap_next_attribute( ld, e, ber ) )
163 SYLDAP_V2_TEST_ATTR ) == 0 ) {
164 vals = ldap_get_values_len( ld, e, attribute );
166 for( i = 0; vals[i] != NULL; i++ ) {
169 * Strip the 'ldb:' from the
170 * front of the value.
172 tmp = g_strndup( vals[i]->bv_val, vals[i]->bv_len);
173 ch = ( char * ) strchr( tmp, ':' );
175 gchar *bn = g_strdup( ++ch );
178 baseDN = g_list_append(
179 baseDN, g_strdup( bn ) );
185 ldap_value_free_len( vals );
187 ldap_memfree( attribute );
196 ldap_msgfree( result );
200 int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
204 if ( passwd != NULL ) {
205 cred.bv_val = (char *) passwd;
206 cred.bv_len = strlen( passwd );
212 debug_print("binding: DN->%s\n", dn?dn:"null");
214 return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
217 return ldap_simple_bind_s(ld, dn, passwd);
222 * Attempt to discover the base DN for the server.
223 * \param host Host name.
224 * \param port Port number.
225 * \param bindDN Bind DN (optional).
226 * \param bindPW Bind PW (optional).
227 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
228 * \return List of Base DN's, or NULL if could not read. This list should be
229 * g_free() when done.
231 GList *ldaputil_read_basedn(
232 const gchar *host, const gint port, const gchar *bindDN,
233 const gchar *bindPW, const gint tov, int ssl, int tls )
235 GList *baseDN = NULL;
237 LdapControl *ctl = ldapctl_create();
245 ldapctl_set_tls(ctl, tls);
246 ldapctl_set_ssl(ctl, ssl);
247 ldapctl_set_port(ctl, port);
248 ldapctl_set_host(ctl, host);
249 ldapctl_set_timeout(ctl, tov);
250 ldapctl_set_bind_dn(ctl, bindDN);
251 ldapctl_set_bind_password(ctl, bindPW, FALSE, FALSE);
253 ld = ldapsvr_connect(ctl);
258 baseDN = ldaputil_test_v3( ld, tov, &rc );
260 debug_print("Using LDAP v3\n");
263 if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
265 if( baseDN == NULL) {
267 baseDN = ldaputil_test_v2( ld, tov );
269 debug_print("Using LDAP v2\n");
272 ldapsvr_disconnect(ld);
280 * Attempt to connect to the server.
282 * \param host Host name.
283 * \param port Port number.
284 * \return <i>TRUE</i> if connected successfully.
286 gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls, int secs ) {
287 gboolean retVal = FALSE;
288 LdapControl *ctl = ldapctl_create();
291 ldapctl_set_tls(ctl, tls);
292 ldapctl_set_ssl(ctl, ssl);
293 ldapctl_set_port(ctl, port);
294 ldapctl_set_host(ctl, host);
295 ldapctl_set_timeout(ctl, secs);
297 ld = ldapsvr_connect(ctl);
299 ldapsvr_disconnect(ld);
300 debug_print("ld != NULL\n");
309 * Test whether LDAP libraries installed.
310 * Return: TRUE if library available.
312 gboolean ldaputil_test_ldap_lib( void ) {
316 const gchar *ldaputil_get_error(LDAP *ld)
318 gchar *ld_error = NULL;
319 static gchar error[512];
321 ldap_get_option( ld, LDAP_OPT_ERROR_STRING, &ld_error);
322 if (ld_error != NULL)
323 strncpy2(error, ld_error, sizeof(error));
325 strncpy2(error, _("Unknown error"), sizeof(error));
326 ldap_memfree(ld_error);
330 #endif /* USE_LDAP */