add Charles to the man page
[claws.git] / src / ldaputil.c
blob305036ce39a84fb5860ab79c40c9e87d39b20192
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 struct berval cred;
203 if ( passwd != NULL ) {
204 cred.bv_val = (char *) passwd;
205 cred.bv_len = strlen( passwd );
206 } else {
207 cred.bv_val = "";
208 cred.bv_len = 0;
211 debug_print("binding: DN->%s\n", dn?dn:"null");
212 #ifdef G_OS_UNIX
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, dn, 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);
250 ldapctl_set_bind_password(ctl, bindPW, FALSE, FALSE);
252 ld = ldapsvr_connect(ctl);
253 if (ld == NULL) {
254 ldapctl_free(ctl);
255 return NULL;
257 baseDN = ldaputil_test_v3( ld, tov, &rc );
258 if (baseDN)
259 debug_print("Using LDAP v3\n");
261 #ifdef G_OS_UNIX
262 if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
263 #else
264 if( baseDN == NULL) {
265 #endif
266 baseDN = ldaputil_test_v2( ld, tov );
267 if (baseDN)
268 debug_print("Using LDAP v2\n");
270 if (ld)
271 ldapsvr_disconnect(ld);
273 ldapctl_free(ctl);
275 return baseDN;
279 * Attempt to connect to the server.
280 * Enter:
281 * \param host Host name.
282 * \param port Port number.
283 * \return <i>TRUE</i> if connected successfully.
285 gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls, int secs ) {
286 gboolean retVal = FALSE;
287 LdapControl *ctl = ldapctl_create();
288 LDAP *ld;
290 ldapctl_set_tls(ctl, tls);
291 ldapctl_set_ssl(ctl, ssl);
292 ldapctl_set_port(ctl, port);
293 ldapctl_set_host(ctl, host);
294 ldapctl_set_timeout(ctl, secs);
296 ld = ldapsvr_connect(ctl);
297 if( ld != NULL ) {
298 ldapsvr_disconnect(ld);
299 debug_print("ld != NULL\n");
300 retVal = TRUE;
302 ldapctl_free(ctl);
304 return retVal;
308 * Test whether LDAP libraries installed.
309 * Return: TRUE if library available.
311 gboolean ldaputil_test_ldap_lib( void ) {
312 return TRUE;
315 const gchar *ldaputil_get_error(LDAP *ld)
317 gchar *ld_error = NULL;
318 static gchar error[512];
320 ldap_get_option( ld, LDAP_OPT_ERROR_STRING, &ld_error);
321 if (ld_error != NULL)
322 strncpy2(error, ld_error, sizeof(error));
323 else
324 strncpy2(error, _("Unknown error"), sizeof(error));
325 #ifndef G_OS_WIN32
326 /* From https://msdn.microsoft.com/en-us/library/aa366594%28v=vs.85%29.aspx
327 * "LDAP_OPT_ERROR_STRING returns a pointer to an internal static
328 * string table, and ldap_memfree should not be called when using
329 * this session option."
331 ldap_memfree(ld_error);
332 #endif
334 return error;
336 #endif /* USE_LDAP */
339 * End of Source.