2008-06-27 [paul] 3.4.0cvs114
[claws.git] / src / ldaputil.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003-2007 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 #endif
27
28 #ifdef USE_LDAP
29
30 #include <glib.h>
31 #include <string.h>
32 #include <sys/time.h>
33 #include <ldap.h>
34 #include <lber.h>
35 #include <errno.h>
36 #include "common/utils.h"
37
38 #define SYLDAP_TEST_FILTER   "(objectclass=*)"
39 #define SYLDAP_SEARCHBASE_V2 "cn=config"
40 #define SYLDAP_SEARCHBASE_V3 ""
41 #define SYLDAP_V2_TEST_ATTR  "database"
42 #define SYLDAP_V3_TEST_ATTR  "namingcontexts"
43
44 /**
45  * Attempt to discover the base DN for a server using LDAP version 3.
46  * \param  ld  LDAP handle for a connected server.
47  * \param  tov Timeout value (seconds), or 0 for none, default 30 secs.
48  * \return List of Base DN's, or NULL if could not read. List should be
49  *         g_free() when done.
50  */
51 static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
52         GList *baseDN = NULL;
53         gint rc, i;
54         LDAPMessage *result = NULL, *e;
55         gchar *attribs[2];
56         BerElement *ber;
57         gchar *attribute;
58         struct berval **vals;
59         struct timeval timeout;
60
61         /* Set timeout */
62         timeout.tv_usec = 0L;
63         if( tov > 0 ) {
64                 timeout.tv_sec = tov;
65         }
66         else {
67                 timeout.tv_sec = 30L;
68         }
69
70         /* Test for LDAP version 3 */
71         attribs[0] = SYLDAP_V3_TEST_ATTR;
72         attribs[1] = NULL;
73         rc = ldap_search_ext_s(
74                 ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
75                 attribs, 0, NULL, NULL, &timeout, 0, &result );
76
77         if( rc == LDAP_SUCCESS ) {
78                 /* Process entries */
79                 for( e = ldap_first_entry( ld, result );
80                      e != NULL;
81                      e = ldap_next_entry( ld, e ) ) 
82                 {
83                         /* Process attributes */
84                         for( attribute = ldap_first_attribute( ld, e, &ber );
85                              attribute != NULL;
86                              attribute = ldap_next_attribute( ld, e, ber ) )
87                         {
88                                 if( strcasecmp(
89                                         attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
90                                 {
91                                         vals = ldap_get_values_len( ld, e, attribute );
92                                         if( vals != NULL ) {
93                                                 for( i = 0; vals[i] != NULL; i++ ) {
94                                                         baseDN = g_list_append(
95                                                                 baseDN, g_strndup( vals[i]->bv_val, vals[i]->bv_len ) );
96                                                 }
97                                         }
98                                         ldap_value_free_len( vals );
99                                 }
100                                 ldap_memfree( attribute );
101                         }
102                         if( ber != NULL ) {
103                                 ber_free( ber, 0 );
104                         }
105                         ber = NULL;
106                 }
107         } 
108         if (errcode)
109                 *errcode = rc;
110         if (result)
111                 ldap_msgfree( result );
112         return baseDN;
113 }
114
115 /**
116  * Attempt to discover the base DN for a server using LDAP version 2.
117  * \param  ld  LDAP handle for a connected server.
118  * \param  tov Timeout value (seconds), or 0 for none, default 30 secs.
119  * \return List of Base DN's, or NULL if could not read. List should be
120  *         g_free() when done.
121  */
122 static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
123         GList *baseDN = NULL;
124         gint rc, i;
125         LDAPMessage *result = NULL, *e;
126         gchar *attribs[1];
127         BerElement *ber;
128         gchar *attribute;
129         struct berval **vals;
130         struct timeval timeout;
131
132         /* Set timeout */
133         timeout.tv_usec = 0L;
134         if( tov > 0 ) {
135                 timeout.tv_sec = tov;
136         }
137         else {
138                 timeout.tv_sec = 30L;
139         }
140
141         attribs[0] = NULL;
142         rc = ldap_search_ext_s(
143                 ld, SYLDAP_SEARCHBASE_V2, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
144                 attribs, 0, NULL, NULL, &timeout, 0, &result );
145
146         if( rc == LDAP_SUCCESS ) {
147                 /* Process entries */
148                 for( e = ldap_first_entry( ld, result );
149                      e != NULL;
150                      e = ldap_next_entry( ld, e ) )
151                 {
152                         /* Process attributes */
153                         for( attribute = ldap_first_attribute( ld, e, &ber );
154                              attribute != NULL;
155                              attribute = ldap_next_attribute( ld, e, ber ) )
156                         {
157                                 if( strcasecmp(
158                                         attribute,
159                                         SYLDAP_V2_TEST_ATTR ) == 0 ) {
160                                         vals = ldap_get_values_len( ld, e, attribute );
161                                         if( vals != NULL ) {
162                                                 for( i = 0; vals[i] != NULL; i++ ) {
163                                                         char *ch, *tmp;
164                                                         /*
165                                                          * Strip the 'ldb:' from the
166                                                          * front of the value.
167                                                          */
168                                                         tmp = g_strndup( vals[i]->bv_val, vals[i]->bv_len);
169                                                         ch = ( char * ) strchr( tmp, ':' );
170                                                         if( ch ) {
171                                                                 gchar *bn = g_strdup( ++ch );
172                                                                 g_strchomp( bn );
173                                                                 g_strchug( bn );
174                                                                 baseDN = g_list_append(
175                                                                         baseDN, g_strdup( bn ) );
176                                                                 g_free( bn );
177                                                         }
178                                                         g_free(tmp);
179                                                 }
180                                         }
181                                         ldap_value_free_len( vals );
182                                 }
183                                 ldap_memfree( attribute );
184                         }
185                         if( ber != NULL ) {
186                                 ber_free( ber, 0 );
187                         }
188                         ber = NULL;
189                 }
190         }
191         if (result)
192                 ldap_msgfree( result );
193         return baseDN;
194 }
195
196 int claws_ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
197 {
198         struct berval cred;
199
200         if ( passwd != NULL ) {
201                 cred.bv_val = (char *) passwd;
202                 cred.bv_len = strlen( passwd );
203         } else {
204                 cred.bv_val = "";
205                 cred.bv_len = 0;
206         }
207
208         debug_print("binding: DN->%s\n", dn?dn:"null");
209         return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
210                 NULL, NULL, NULL );
211 }
212
213 /* from ldapsrc.c */
214 void ldapsrv_set_options (gint secs, LDAP *ld);
215
216 /**
217  * Attempt to discover the base DN for the server.
218  * \param  host   Host name.
219  * \param  port   Port number.
220  * \param  bindDN Bind DN (optional).
221  * \param  bindPW Bind PW (optional).
222  * \param  tov    Timeout value (seconds), or 0 for none, default 30 secs.
223  * \return List of Base DN's, or NULL if could not read. This list should be
224  *         g_free() when done.
225  */
226 GList *ldaputil_read_basedn(
227                 const gchar *host, const gint port, const gchar *bindDN,
228                 const gchar *bindPW, const gint tov, int ssl, int tls )
229 {
230         GList *baseDN = NULL;
231         LDAP *ld = NULL;
232         gint rc;
233         gchar *uri = NULL;
234         gint version;
235
236         if( host == NULL ) return baseDN;
237         if( port < 1 ) return baseDN;
238
239         /* Connect to server. */
240
241         ldapsrv_set_options (tov, NULL);
242
243         uri = g_strdup_printf("ldap%s://%s:%d",
244                         ssl?"s":"",
245                         host, port);
246         debug_print("URI: %s\n", uri);
247         rc = ldap_initialize(&ld, uri);
248         g_free(uri);
249         
250         if( ld == NULL ) {
251                 return baseDN;
252         }
253
254         if ((bindDN && *bindDN)
255 #ifdef USE_LDAP_TLS
256            || (tls && !ssl)
257 #endif                  
258         ) {
259                 version = LDAP_VERSION3;
260                 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
261         }
262 #ifdef USE_LDAP_TLS
263         
264         if( tls && !ssl ) {
265                 /* Handle TLS */
266                 if( rc != LDAP_OPT_SUCCESS ) {
267                         ldap_unbind_ext( ld, NULL, NULL );
268                         return baseDN;
269                 }
270                 rc = ldap_start_tls_s( ld, NULL, NULL );
271                 if (rc != 0) {
272                         ldap_unbind_ext( ld, NULL, NULL );
273                         return baseDN;
274                 }
275         }
276 #endif
277
278         /* Bind to the server, if required */
279         if( bindDN ) {
280                 if( *bindDN != '\0' ) {
281                         rc = claws_ldap_simple_bind_s( ld, bindDN, bindPW );
282                         if( rc != LDAP_SUCCESS ) {
283                                 g_printerr("LDAP: %s\n", ldap_err2string(rc));
284                                 ldap_unbind_ext( ld, NULL, NULL );
285                                 return baseDN;
286                         }
287                 }
288         }
289
290         /* Test for LDAP version 3 */
291         baseDN = ldaputil_test_v3( ld, tov, &rc );
292         if (baseDN) {
293                 debug_print("Using LDAP v3\n");
294         }
295
296         if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
297                 baseDN = ldaputil_test_v2( ld, tov );
298                 if (baseDN) {
299                         debug_print("Using LDAP v2\n");
300                 }
301         }
302         if (ld && !LDAP_API_ERROR(rc))
303                 ldap_unbind_ext( ld, NULL, NULL );
304         
305         return baseDN;
306 }
307
308 /**
309  * Attempt to connect to the server.
310  * Enter:
311  * \param  host Host name.
312  * \param  port Port number.
313  * \return <i>TRUE</i> if connected successfully.
314  */
315 gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls, int secs ) {
316         gboolean retVal = FALSE;
317         LDAP *ld;
318 #ifdef USE_LDAP_TLS
319         gint rc;
320         gint version;
321 #endif
322         gchar *uri = NULL;
323
324         if( host == NULL ) return retVal;
325         if( port < 1 ) return retVal;
326         
327         ldapsrv_set_options (secs, NULL);
328         uri = g_strdup_printf("ldap%s://%s:%d",
329                                 ssl?"s":"",
330                                 host, port);
331         debug_print("URI: %s\n", uri);
332         ldap_initialize(&ld, uri);
333         g_free(uri);
334         if (ld == NULL)
335                 return FALSE;
336
337 #ifdef USE_LDAP_TLS
338         if (ssl) {
339                 GList *dummy = ldaputil_test_v3( ld, secs, &rc );
340                 if (dummy)
341                         g_list_free(dummy);
342                 if (LDAP_API_ERROR(rc))
343                         return FALSE;
344         }
345
346         if( tls && !ssl ) {
347                 /* Handle TLS */
348                 version = LDAP_VERSION3;
349                 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
350                 if( rc != LDAP_OPT_SUCCESS ) {
351                         ldap_unbind_ext( ld, NULL, NULL );
352                         return FALSE;
353                 }
354
355                 rc = ldap_start_tls_s( ld, NULL, NULL );
356                 if (rc != 0) {
357                         ldap_unbind_ext( ld, NULL, NULL );
358                         return FALSE;
359                 }
360         }
361 #endif
362         if( ld != NULL ) {
363                 ldap_unbind_ext( ld, NULL, NULL );
364                 debug_print("ld != NULL\n");
365                 retVal = TRUE;
366         }
367         return retVal;
368 }
369
370 /**
371  * Test whether LDAP libraries installed.
372  * Return: TRUE if library available.
373  */
374 gboolean ldaputil_test_ldap_lib( void ) {
375         return TRUE;
376 }
377
378 #endif  /* USE_LDAP */
379
380 /*
381  * End of Source.
382 */