2013-01-23 [colin] 3.9.0cvs59
[claws.git] / src / ldaputil.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003-2012 Match Grun and the Claws Mail team
4  *
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.
9  *
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.
14  *
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/>.
17  * 
18  */
19
20 /*
21  * Some utility functions to access LDAP servers.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #include "claws-features.h"
27 #endif
28
29 #ifdef USE_LDAP
30
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"
40
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"
46
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.
53  */
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;
63
64         /* Set timeout */
65         timeout.tv_usec = 0L;
66         if( tov > 0 ) {
67                 timeout.tv_sec = tov;
68         }
69         else {
70                 timeout.tv_sec = 30L;
71         }
72
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 );
79
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 ) ) 
85                 {
86                         /* Process attributes */
87                         for( attribute = ldap_first_attribute( ld, e, &ber );
88                              attribute != NULL;
89                              attribute = ldap_next_attribute( ld, e, ber ) )
90                         {
91                                 if( strcasecmp(
92                                         attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
93                                 {
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 ) );
99                                                 }
100                                         }
101                                         ldap_value_free_len( vals );
102                                 }
103                                 ldap_memfree( attribute );
104                         }
105                         if( ber != NULL ) {
106                                 ber_free( ber, 0 );
107                         }
108                         ber = NULL;
109                 }
110         } else
111                 debug_print("LDAP: Error %d (%s)\n", rc, ldaputil_get_error(ld));
112         
113         if (errcode)
114                 *errcode = rc;
115         if (result)
116                 ldap_msgfree( result );
117         return baseDN;
118 }
119
120 /**
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.
126  */
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;
136
137         /* Set timeout */
138         timeout.tv_usec = 0L;
139         if( tov > 0 ) {
140                 timeout.tv_sec = tov;
141         }
142         else {
143                 timeout.tv_sec = 30L;
144         }
145
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 );
150
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 ) )
156                 {
157                         /* Process attributes */
158                         for( attribute = ldap_first_attribute( ld, e, &ber );
159                              attribute != NULL;
160                              attribute = ldap_next_attribute( ld, e, ber ) )
161                         {
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;
169                                                         /*
170                                                          * Strip the 'ldb:' from the
171                                                          * front of the value.
172                                                          */
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 );
182                                                         }
183                                                         g_free(tmp);
184                                                 }
185                                         }
186                                         ldap_value_free_len( vals );
187                                 }
188                                 ldap_memfree( attribute );
189                         }
190                         if( ber != NULL ) {
191                                 ber_free( ber, 0 );
192                         }
193                         ber = NULL;
194                 }
195         }
196         if (result)
197                 ldap_msgfree( result );
198         return baseDN;
199 }
200
201 int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
202 {
203         struct berval cred;
204
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;
211         }
212
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
220 }
221
222 /**
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.
231  */
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 )
235 {
236         GList *baseDN = NULL;
237         LDAP *ld = NULL;
238         LdapControl *ctl = ldapctl_create();
239         gint rc;
240
241         if( host == NULL ) 
242                 return NULL;
243         if( port < 1 ) 
244                 return NULL;
245
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);
253
254         ld = ldapsvr_connect(ctl);
255         if (ld == NULL) {
256                 ldapctl_free(ctl);
257                 return NULL;
258         }
259         baseDN = ldaputil_test_v3( ld, tov, &rc );
260         if (baseDN)
261                 debug_print("Using LDAP v3\n");
262
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");
271         }
272         if (ld)
273                 ldapsvr_disconnect(ld);
274
275         ldapctl_free(ctl);
276         
277         return baseDN;
278 }
279
280 /**
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.
286  */
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;
291
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);
297
298         ld = ldapsvr_connect(ctl);
299         if( ld != NULL ) {
300                 ldapsvr_disconnect(ld);
301                 debug_print("ld != NULL\n");
302                 retVal = TRUE;
303         }
304         ldapctl_free(ctl);
305
306         return retVal;
307 }
308
309 /**
310  * Test whether LDAP libraries installed.
311  * Return: TRUE if library available.
312  */
313 gboolean ldaputil_test_ldap_lib( void ) {
314         return TRUE;
315 }
316
317 const gchar *ldaputil_get_error(LDAP *ld)
318 {
319         gchar *ld_error = NULL;
320         static gchar error[512];
321
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);
328
329         return error;
330 }
331 #endif  /* USE_LDAP */
332
333 /*
334  * End of Source.
335 */