2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2012 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.
26 #include "claws-features.h"
32 #include <glib/gi18n.h>
36 #include "common/utils.h"
38 #include "ldapserver.h"
41 #define SYLDAP_TEST_FILTER "(objectclass=*)"
42 #define SYLDAP_SEARCHBASE_V2 "cn=config"
43 #define SYLDAP_SEARCHBASE_V3 ""
44 #define SYLDAP_V2_TEST_ATTR "database"
45 #define SYLDAP_V3_TEST_ATTR "namingcontexts"
48 * Attempt to discover the base DN for a server using LDAP version 3.
49 * \param ld LDAP handle for a connected server.
50 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
51 * \return List of Base DN's, or NULL if could not read. List should be
54 static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
57 LDAPMessage *result = NULL, *e;
62 struct timeval timeout;
73 /* Test for LDAP version 3 */
74 attribs[0] = SYLDAP_V3_TEST_ATTR;
76 rc = ldap_search_ext_s(
77 ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
78 attribs, 0, NULL, NULL, &timeout, 0, &result );
80 if( rc == LDAP_SUCCESS ) {
82 for( e = ldap_first_entry( ld, result );
84 e = ldap_next_entry( ld, e ) )
86 /* Process attributes */
87 for( attribute = ldap_first_attribute( ld, e, &ber );
89 attribute = ldap_next_attribute( ld, e, ber ) )
92 attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
94 vals = ldap_get_values_len( ld, e, attribute );
96 for( i = 0; vals[i] != NULL; i++ ) {
97 baseDN = g_list_append(
98 baseDN, g_strndup( vals[i]->bv_val, vals[i]->bv_len ) );
101 ldap_value_free_len( vals );
103 ldap_memfree( attribute );
111 debug_print("LDAP: Error %d (%s)\n", rc, ldaputil_get_error(ld));
116 ldap_msgfree( result );
121 * Attempt to discover the base DN for a server using LDAP version 2.
122 * \param ld LDAP handle for a connected server.
123 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
124 * \return List of Base DN's, or NULL if could not read. List should be
125 * g_free() when done.
127 static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
128 GList *baseDN = NULL;
130 LDAPMessage *result = NULL, *e;
134 struct berval **vals;
135 struct timeval timeout;
138 timeout.tv_usec = 0L;
140 timeout.tv_sec = tov;
143 timeout.tv_sec = 30L;
147 rc = ldap_search_ext_s(
148 ld, SYLDAP_SEARCHBASE_V2, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
149 attribs, 0, NULL, NULL, &timeout, 0, &result );
151 if( rc == LDAP_SUCCESS ) {
152 /* Process entries */
153 for( e = ldap_first_entry( ld, result );
155 e = ldap_next_entry( ld, e ) )
157 /* Process attributes */
158 for( attribute = ldap_first_attribute( ld, e, &ber );
160 attribute = ldap_next_attribute( ld, e, ber ) )
164 SYLDAP_V2_TEST_ATTR ) == 0 ) {
165 vals = ldap_get_values_len( ld, e, attribute );
167 for( i = 0; vals[i] != NULL; i++ ) {
170 * Strip the 'ldb:' from the
171 * front of the value.
173 tmp = g_strndup( vals[i]->bv_val, vals[i]->bv_len);
174 ch = ( char * ) strchr( tmp, ':' );
176 gchar *bn = g_strdup( ++ch );
179 baseDN = g_list_append(
180 baseDN, g_strdup( bn ) );
186 ldap_value_free_len( vals );
188 ldap_memfree( attribute );
197 ldap_msgfree( result );
201 int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
205 if ( passwd != NULL ) {
206 cred.bv_val = (char *) passwd;
207 cred.bv_len = strlen( passwd );
213 debug_print("binding: DN->%s\n", dn?dn:"null");
215 return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
218 return ldap_simple_bind_s(ld, dn, passwd);
223 * Attempt to discover the base DN for the server.
224 * \param host Host name.
225 * \param port Port number.
226 * \param bindDN Bind DN (optional).
227 * \param bindPW Bind PW (optional).
228 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
229 * \return List of Base DN's, or NULL if could not read. This list should be
230 * g_free() when done.
232 GList *ldaputil_read_basedn(
233 const gchar *host, const gint port, const gchar *bindDN,
234 const gchar *bindPW, const gint tov, int ssl, int tls )
236 GList *baseDN = NULL;
238 LdapControl *ctl = ldapctl_create();
246 ldapctl_set_tls(ctl, tls);
247 ldapctl_set_ssl(ctl, ssl);
248 ldapctl_set_port(ctl, port);
249 ldapctl_set_host(ctl, host);
250 ldapctl_set_timeout(ctl, tov);
251 ldapctl_set_bind_dn(ctl, bindDN);
252 ldapctl_set_bind_password(ctl, bindPW, FALSE, FALSE);
254 ld = ldapsvr_connect(ctl);
259 baseDN = ldaputil_test_v3( ld, tov, &rc );
261 debug_print("Using LDAP v3\n");
264 if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
266 if( baseDN == NULL) {
268 baseDN = ldaputil_test_v2( ld, tov );
270 debug_print("Using LDAP v2\n");
273 ldapsvr_disconnect(ld);
281 * Attempt to connect to the server.
283 * \param host Host name.
284 * \param port Port number.
285 * \return <i>TRUE</i> if connected successfully.
287 gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls, int secs ) {
288 gboolean retVal = FALSE;
289 LdapControl *ctl = ldapctl_create();
292 ldapctl_set_tls(ctl, tls);
293 ldapctl_set_ssl(ctl, ssl);
294 ldapctl_set_port(ctl, port);
295 ldapctl_set_host(ctl, host);
296 ldapctl_set_timeout(ctl, secs);
298 ld = ldapsvr_connect(ctl);
300 ldapsvr_disconnect(ld);
301 debug_print("ld != NULL\n");
310 * Test whether LDAP libraries installed.
311 * Return: TRUE if library available.
313 gboolean ldaputil_test_ldap_lib( void ) {
317 const gchar *ldaputil_get_error(LDAP *ld)
319 gchar *ld_error = NULL;
320 static gchar error[512];
322 ldap_get_option( ld, LDAP_OPT_ERROR_STRING, &ld_error);
323 if (ld_error != NULL)
324 strncpy2(error, ld_error, sizeof(error));
326 strncpy2(error, _("Unknown error"), sizeof(error));
327 ldap_memfree(ld_error);
331 #endif /* USE_LDAP */