2007-08-24 [paul] 2.10.0cvs163
[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 /**
214  * Attempt to discover the base DN for the server.
215  * \param  host   Host name.
216  * \param  port   Port number.
217  * \param  bindDN Bind DN (optional).
218  * \param  bindPW Bind PW (optional).
219  * \param  tov    Timeout value (seconds), or 0 for none, default 30 secs.
220  * \return List of Base DN's, or NULL if could not read. This list should be
221  *         g_free() when done.
222  */
223 GList *ldaputil_read_basedn(
224                 const gchar *host, const gint port, const gchar *bindDN,
225                 const gchar *bindPW, const gint tov, int ssl, int tls )
226 {
227         GList *baseDN = NULL;
228         LDAP *ld = NULL;
229         gint rc;
230         gchar *uri = NULL;
231 #ifdef USE_LDAP_TLS
232         gint version;
233 #endif
234
235         if( host == NULL ) return baseDN;
236         if( port < 1 ) return baseDN;
237
238         /* Connect to server. */
239
240         ldapsrv_set_options (tov, NULL);
241
242         uri = g_strdup_printf("ldap%s://%s:%d",
243                         ssl?"s":"",
244                         host, port);
245         debug_print("URI: %s\n", uri);
246         rc = ldap_initialize(&ld, uri);
247         g_free(uri);
248         
249         if( ld == NULL ) {
250                 return baseDN;
251         }
252
253         if ((bindDN && *bindDN)
254 #ifdef USE_LDAP_TLS
255            || (tls && !ssl)
256 #endif                  
257         ) {
258                 version = LDAP_VERSION3;
259                 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
260         }
261 #ifdef USE_LDAP_TLS
262         
263         if( tls && !ssl ) {
264                 /* Handle TLS */
265                 if( rc != LDAP_OPT_SUCCESS ) {
266                         ldap_unbind_ext( ld, NULL, NULL );
267                         return baseDN;
268                 }
269                 rc = ldap_start_tls_s( ld, NULL, NULL );
270                 if (rc != 0) {
271                         ldap_unbind_ext( ld, NULL, NULL );
272                         return baseDN;
273                 }
274         }
275 #endif
276
277         /* Bind to the server, if required */
278         if( bindDN ) {
279                 if( *bindDN != '\0' ) {
280                         rc = claws_ldap_simple_bind_s( ld, bindDN, bindPW );
281                         if( rc != LDAP_SUCCESS ) {
282                                 g_printerr("LDAP: %s\n", ldap_err2string(rc));
283                                 ldap_unbind_ext( ld, NULL, NULL );
284                                 return baseDN;
285                         }
286                 }
287         }
288
289         /* Test for LDAP version 3 */
290         baseDN = ldaputil_test_v3( ld, tov, &rc );
291         if (baseDN) {
292                 debug_print("Using LDAP v3\n");
293         }
294
295         if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
296                 baseDN = ldaputil_test_v2( ld, tov );
297                 if (baseDN) {
298                         debug_print("Using LDAP v2\n");
299                 }
300         }
301         if (ld && !LDAP_API_ERROR(rc))
302                 ldap_unbind_ext( ld, NULL, NULL );
303         
304         return baseDN;
305 }
306
307 /**
308  * Attempt to connect to the server.
309  * Enter:
310  * \param  host Host name.
311  * \param  port Port number.
312  * \return <i>TRUE</i> if connected successfully.
313  */
314 gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls, int secs ) {
315         gboolean retVal = FALSE;
316         LDAP *ld;
317 #ifdef USE_LDAP_TLS
318         gint rc;
319         gint version;
320 #endif
321         gchar *uri = NULL;
322
323         if( host == NULL ) return retVal;
324         if( port < 1 ) return retVal;
325         
326         ldapsrv_set_options (secs, NULL);
327         uri = g_strdup_printf("ldap%s://%s:%d",
328                                 ssl?"s":"",
329                                 host, port);
330         debug_print("URI: %s\n", uri);
331         ldap_initialize(&ld, uri);
332         g_free(uri);
333         if (ld == NULL)
334                 return FALSE;
335
336 #ifdef USE_LDAP_TLS
337         if (ssl) {
338                 GList *dummy = ldaputil_test_v3( ld, secs, &rc );
339                 if (dummy)
340                         g_list_free(dummy);
341                 if (LDAP_API_ERROR(rc))
342                         return FALSE;
343         }
344
345         if( tls && !ssl ) {
346                 /* Handle TLS */
347                 version = LDAP_VERSION3;
348                 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
349                 if( rc != LDAP_OPT_SUCCESS ) {
350                         ldap_unbind_ext( ld, NULL, NULL );
351                         return FALSE;
352                 }
353
354                 rc = ldap_start_tls_s( ld, NULL, NULL );
355                 if (rc != 0) {
356                         ldap_unbind_ext( ld, NULL, NULL );
357                         return FALSE;
358                 }
359         }
360 #endif
361         if( ld != NULL ) {
362                 ldap_unbind_ext( ld, NULL, NULL );
363                 debug_print("ld != NULL\n");
364                 retVal = TRUE;
365         }
366         return retVal;
367 }
368
369 /**
370  * Test whether LDAP libraries installed.
371  * Return: TRUE if library available.
372  */
373 gboolean ldaputil_test_ldap_lib( void ) {
374         return TRUE;
375 }
376
377 #endif  /* USE_LDAP */
378
379 /*
380  * End of Source.
381 */