add user specified stylesheet option
[claws.git] / src / ldaputil.c
blob0000a1eb6dc2776148e33bcda31bd6577b5f66ac
1 /*
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.
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #include "claws-features.h"
27 #endif
29 #ifdef USE_LDAP
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <string.h>
34 #include <sys/time.h>
35 #include <errno.h>
36 #include "common/utils.h"
37 #include "ldaputil.h"
38 #include "ldapserver.h"
39 #include "ldapctrl.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"
47 /**
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
52 * g_free() when done.
54 static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
55 GList *baseDN = NULL;
56 gint rc, i;
57 LDAPMessage *result = NULL, *e;
58 gchar *attribs[2];
59 BerElement *ber;
60 gchar *attribute;
61 struct berval **vals;
62 struct timeval timeout;
64 /* Set timeout */
65 timeout.tv_usec = 0L;
66 if( tov > 0 ) {
67 timeout.tv_sec = tov;
69 else {
70 timeout.tv_sec = 30L;
73 /* Test for LDAP version 3 */
74 attribs[0] = SYLDAP_V3_TEST_ATTR;
75 attribs[1] = NULL;
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 ) {
81 /* Process entries */
82 for( e = ldap_first_entry( ld, result );
83 e != NULL;
84 e = ldap_next_entry( ld, e ) )
86 /* Process attributes */
87 for( attribute = ldap_first_attribute( ld, e, &ber );
88 attribute != NULL;
89 attribute = ldap_next_attribute( ld, e, ber ) )
91 if( strcasecmp(
92 attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
94 vals = ldap_get_values_len( ld, e, attribute );
95 if( vals != NULL ) {
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 );
105 if( ber != NULL ) {
106 ber_free( ber, 0 );
108 ber = NULL;
110 } else
111 debug_print("LDAP: Error %d (%s)\n", rc, ldaputil_get_error(ld));
113 if (errcode)
114 *errcode = rc;
115 if (result)
116 ldap_msgfree( result );
117 return baseDN;
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;
129 gint rc, i;
130 LDAPMessage *result = NULL, *e;
131 gchar *attribs[1];
132 BerElement *ber;
133 gchar *attribute;
134 struct berval **vals;
135 struct timeval timeout;
137 /* Set timeout */
138 timeout.tv_usec = 0L;
139 if( tov > 0 ) {
140 timeout.tv_sec = tov;
142 else {
143 timeout.tv_sec = 30L;
146 attribs[0] = NULL;
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 );
154 e != NULL;
155 e = ldap_next_entry( ld, e ) )
157 /* Process attributes */
158 for( attribute = ldap_first_attribute( ld, e, &ber );
159 attribute != NULL;
160 attribute = ldap_next_attribute( ld, e, ber ) )
162 if( strcasecmp(
163 attribute,
164 SYLDAP_V2_TEST_ATTR ) == 0 ) {
165 vals = ldap_get_values_len( ld, e, attribute );
166 if( vals != NULL ) {
167 for( i = 0; vals[i] != NULL; i++ ) {
168 char *ch, *tmp;
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, ':' );
175 if( ch ) {
176 gchar *bn = g_strdup( ++ch );
177 g_strchomp( bn );
178 g_strchug( bn );
179 baseDN = g_list_append(
180 baseDN, g_strdup( bn ) );
181 g_free( bn );
183 g_free(tmp);
186 ldap_value_free_len( vals );
188 ldap_memfree( attribute );
190 if( ber != NULL ) {
191 ber_free( ber, 0 );
193 ber = NULL;
196 if (result)
197 ldap_msgfree( result );
198 return baseDN;
201 int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
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 debug_print("binding: DN->%s\n", dn?dn:"null");
214 #ifdef G_OS_UNIX
215 return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
216 NULL, NULL, NULL );
217 #else
218 return ldap_simple_bind_s(ld, dn, passwd);
219 #endif
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;
237 LDAP *ld = NULL;
238 LdapControl *ctl = ldapctl_create();
239 gint rc;
241 if( host == NULL )
242 return NULL;
243 if( port < 1 )
244 return NULL;
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);
255 if (ld == NULL) {
256 ldapctl_free(ctl);
257 return NULL;
259 baseDN = ldaputil_test_v3( ld, tov, &rc );
260 if (baseDN)
261 debug_print("Using LDAP v3\n");
263 #ifdef G_OS_UNIX
264 if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
265 #else
266 if( baseDN == NULL) {
267 #endif
268 baseDN = ldaputil_test_v2( ld, tov );
269 if (baseDN)
270 debug_print("Using LDAP v2\n");
272 if (ld)
273 ldapsvr_disconnect(ld);
275 ldapctl_free(ctl);
277 return baseDN;
281 * Attempt to connect to the server.
282 * Enter:
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();
290 LDAP *ld;
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);
299 if( ld != NULL ) {
300 ldapsvr_disconnect(ld);
301 debug_print("ld != NULL\n");
302 retVal = TRUE;
304 ldapctl_free(ctl);
306 return retVal;
310 * Test whether LDAP libraries installed.
311 * Return: TRUE if library available.
313 gboolean ldaputil_test_ldap_lib( void ) {
314 return TRUE;
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));
325 else
326 strncpy2(error, _("Unknown error"), sizeof(error));
327 ldap_memfree(ld_error);
329 return error;
331 #endif /* USE_LDAP */
334 * End of Source.