Fix bug #3574: Template addressing
[claws.git] / src / ldaputil.c
blob21e5d97d734e85dd02d74287f2ff14645d51dbcf
1 /*
2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2015 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/>.
20 * Some utility functions to access LDAP servers.
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #include "claws-features.h"
26 #endif
28 #ifdef USE_LDAP
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <string.h>
33 #include <sys/time.h>
34 #include <errno.h>
35 #include "common/utils.h"
36 #include "ldaputil.h"
37 #include "ldapserver.h"
38 #include "ldapctrl.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"
46 /**
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
51 * g_free() when done.
53 static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
54 GList *baseDN = NULL;
55 gint rc, i;
56 LDAPMessage *result = NULL, *e;
57 gchar *attribs[2];
58 BerElement *ber;
59 gchar *attribute;
60 struct berval **vals;
61 struct timeval timeout;
63 /* Set timeout */
64 timeout.tv_usec = 0L;
65 if( tov > 0 ) {
66 timeout.tv_sec = tov;
68 else {
69 timeout.tv_sec = 30L;
72 /* Test for LDAP version 3 */
73 attribs[0] = SYLDAP_V3_TEST_ATTR;
74 attribs[1] = NULL;
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 ) {
80 /* Process entries */
81 for( e = ldap_first_entry( ld, result );
82 e != NULL;
83 e = ldap_next_entry( ld, e ) )
85 /* Process attributes */
86 for( attribute = ldap_first_attribute( ld, e, &ber );
87 attribute != NULL;
88 attribute = ldap_next_attribute( ld, e, ber ) )
90 if( strcasecmp(
91 attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
93 vals = ldap_get_values_len( ld, e, attribute );
94 if( vals != NULL ) {
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 );
104 if( ber != NULL ) {
105 ber_free( ber, 0 );
107 ber = NULL;
109 } else
110 debug_print("LDAP: Error %d (%s)\n", rc, ldaputil_get_error(ld));
112 if (errcode)
113 *errcode = rc;
114 if (result)
115 ldap_msgfree( result );
116 return baseDN;
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;
128 gint rc, i;
129 LDAPMessage *result = NULL, *e;
130 gchar *attribs[1];
131 BerElement *ber;
132 gchar *attribute;
133 struct berval **vals;
134 struct timeval timeout;
136 /* Set timeout */
137 timeout.tv_usec = 0L;
138 if( tov > 0 ) {
139 timeout.tv_sec = tov;
141 else {
142 timeout.tv_sec = 30L;
145 attribs[0] = NULL;
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 );
153 e != NULL;
154 e = ldap_next_entry( ld, e ) )
156 /* Process attributes */
157 for( attribute = ldap_first_attribute( ld, e, &ber );
158 attribute != NULL;
159 attribute = ldap_next_attribute( ld, e, ber ) )
161 if( strcasecmp(
162 attribute,
163 SYLDAP_V2_TEST_ATTR ) == 0 ) {
164 vals = ldap_get_values_len( ld, e, attribute );
165 if( vals != NULL ) {
166 for( i = 0; vals[i] != NULL; i++ ) {
167 char *ch, *tmp;
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, ':' );
174 if( ch ) {
175 gchar *bn = g_strdup( ++ch );
176 g_strstrip( bn );
177 baseDN = g_list_append(
178 baseDN, g_strdup( bn ) );
179 g_free( bn );
181 g_free(tmp);
184 ldap_value_free_len( vals );
186 ldap_memfree( attribute );
188 if( ber != NULL ) {
189 ber_free( ber, 0 );
191 ber = NULL;
194 if (result)
195 ldap_msgfree( result );
196 return baseDN;
199 int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
201 debug_print("binding: DN->%s\n", dn?dn:"null");
202 #ifdef G_OS_UNIX
203 struct berval cred;
205 if ( passwd != NULL ) {
206 cred.bv_val = (char *) passwd;
207 cred.bv_len = strlen( passwd );
208 } else {
209 cred.bv_val = "";
210 cred.bv_len = 0;
213 return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
214 NULL, NULL, NULL );
215 #else
216 return ldap_simple_bind_s(ld, (PCHAR)dn, (PCHAR)passwd);
217 #endif
221 * Attempt to discover the base DN for the server.
222 * \param host Host name.
223 * \param port Port number.
224 * \param bindDN Bind DN (optional).
225 * \param bindPW Bind PW (optional).
226 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
227 * \return List of Base DN's, or NULL if could not read. This list should be
228 * g_free() when done.
230 GList *ldaputil_read_basedn(
231 const gchar *host, const gint port, const gchar *bindDN,
232 const gchar *bindPW, const gint tov, int ssl, int tls )
234 GList *baseDN = NULL;
235 LDAP *ld = NULL;
236 LdapControl *ctl = ldapctl_create();
237 gint rc;
239 if( host == NULL )
240 return NULL;
241 if( port < 1 )
242 return NULL;
244 ldapctl_set_tls(ctl, tls);
245 ldapctl_set_ssl(ctl, ssl);
246 ldapctl_set_port(ctl, port);
247 ldapctl_set_host(ctl, host);
248 ldapctl_set_timeout(ctl, tov);
249 ldapctl_set_bind_dn(ctl, bindDN);
251 ld = ldapsvr_connect(ctl);
252 if (ld == NULL) {
253 ldapctl_free(ctl);
254 return NULL;
256 baseDN = ldaputil_test_v3( ld, tov, &rc );
257 if (baseDN)
258 debug_print("Using LDAP v3\n");
260 #ifdef G_OS_UNIX
261 if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
262 #else
263 if( baseDN == NULL) {
264 #endif
265 baseDN = ldaputil_test_v2( ld, tov );
266 if (baseDN)
267 debug_print("Using LDAP v2\n");
269 if (ld)
270 ldapsvr_disconnect(ld);
272 ldapctl_free(ctl);
274 return baseDN;
278 * Attempt to connect to the server.
279 * Enter:
280 * \param host Host name.
281 * \param port Port number.
282 * \return <i>TRUE</i> if connected successfully.
284 gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls, int secs ) {
285 gboolean retVal = FALSE;
286 LdapControl *ctl = ldapctl_create();
287 LDAP *ld;
289 ldapctl_set_tls(ctl, tls);
290 ldapctl_set_ssl(ctl, ssl);
291 ldapctl_set_port(ctl, port);
292 ldapctl_set_host(ctl, host);
293 ldapctl_set_timeout(ctl, secs);
295 ld = ldapsvr_connect(ctl);
296 if( ld != NULL ) {
297 ldapsvr_disconnect(ld);
298 debug_print("ld != NULL\n");
299 retVal = TRUE;
301 ldapctl_free(ctl);
303 return retVal;
307 * Test whether LDAP libraries installed.
308 * Return: TRUE if library available.
310 gboolean ldaputil_test_ldap_lib( void ) {
311 return TRUE;
314 const gchar *ldaputil_get_error(LDAP *ld)
316 gchar *ld_error = NULL;
317 static gchar error[512];
319 ldap_get_option( ld, LDAP_OPT_ERROR_STRING, &ld_error);
320 if (ld_error != NULL)
321 strncpy2(error, ld_error, sizeof(error));
322 else
323 strncpy2(error, _("Unknown error"), sizeof(error));
324 #ifndef G_OS_WIN32
325 /* From https://msdn.microsoft.com/en-us/library/aa366594%28v=vs.85%29.aspx
326 * "LDAP_OPT_ERROR_STRING returns a pointer to an internal static
327 * string table, and ldap_memfree should not be called when using
328 * this session option."
330 ldap_memfree(ld_error);
331 #endif
333 return error;
335 #endif /* USE_LDAP */
338 * End of Source.