2007-08-22 [colin] 2.10.0cvs146
[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         uri = g_strdup_printf("ldap%s://%s:%d",
241                         ssl?"s":"",
242                         host, port);
243         debug_print("URI: %s\n", uri);
244         rc = ldap_initialize(&ld, uri);
245         g_free(uri);
246         
247         if( ld == NULL ) {
248                 return baseDN;
249         }
250 #ifdef USE_LDAP_TLS
251         if (bindDN && *bindDN) {
252                 version = LDAP_VERSION3;
253                 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
254         }
255         if( tls && !ssl ) {
256                 /* Handle TLS */
257                 if( rc != LDAP_OPT_SUCCESS ) {
258                         ldap_unbind_ext( ld, NULL, NULL );
259                         return baseDN;
260                 }
261                 rc = ldap_start_tls_s( ld, NULL, NULL );
262                 if (rc != 0) {
263                         ldap_unbind_ext( ld, NULL, NULL );
264                         return baseDN;
265                 }
266         }
267 #endif
268
269         /* Bind to the server, if required */
270         if( bindDN ) {
271                 if( *bindDN != '\0' ) {
272                         rc = claws_ldap_simple_bind_s( ld, bindDN, bindPW );
273                         if( rc != LDAP_SUCCESS ) {
274                                 g_printerr("LDAP: %s\n", ldap_err2string(rc));
275                                 ldap_unbind_ext( ld, NULL, NULL );
276                                 return baseDN;
277                         }
278                 }
279         }
280
281         /* Test for LDAP version 3 */
282         baseDN = ldaputil_test_v3( ld, tov, &rc );
283         if (baseDN) {
284                 debug_print("Using LDAP v3\n");
285         }
286
287         if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
288                 baseDN = ldaputil_test_v2( ld, tov );
289                 if (baseDN) {
290                         debug_print("Using LDAP v2\n");
291                 }
292         }
293         if (ld && !LDAP_API_ERROR(rc))
294                 ldap_unbind_ext( ld, NULL, NULL );
295         
296         return baseDN;
297 }
298
299 /**
300  * Attempt to connect to the server.
301  * Enter:
302  * \param  host Host name.
303  * \param  port Port number.
304  * \return <i>TRUE</i> if connected successfully.
305  */
306 gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls ) {
307         gboolean retVal = FALSE;
308         LDAP *ld;
309 #ifdef USE_LDAP_TLS
310         gint rc;
311         gint version;
312 #endif
313         gchar *uri = NULL;
314
315         if( host == NULL ) return retVal;
316         if( port < 1 ) return retVal;
317         
318         uri = g_strdup_printf("ldap%s://%s:%d",
319                                 ssl?"s":"",
320                                 host, port);
321         debug_print("URI: %s\n", uri);
322         ldap_initialize(&ld, uri);
323         g_free(uri);
324         if (ld == NULL)
325                 return FALSE;
326
327 #ifdef USE_LDAP_TLS
328         if (ssl) {
329                 GList *dummy = ldaputil_test_v3( ld, 10, &rc );
330                 if (dummy)
331                         g_list_free(dummy);
332                 if (LDAP_API_ERROR(rc))
333                         return FALSE;
334         }
335
336         if( tls && !ssl ) {
337                 /* Handle TLS */
338                 version = LDAP_VERSION3;
339                 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
340                 if( rc != LDAP_OPT_SUCCESS ) {
341                         ldap_unbind_ext( ld, NULL, NULL );
342                         return FALSE;
343                 }
344
345                 rc = ldap_start_tls_s( ld, NULL, NULL );
346                 if (rc != 0) {
347                         ldap_unbind_ext( ld, NULL, NULL );
348                         return FALSE;
349                 }
350         }
351 #endif
352         if( ld != NULL ) {
353                 ldap_unbind_ext( ld, NULL, NULL );
354                 debug_print("ld != NULL\n");
355                 retVal = TRUE;
356         }
357         return retVal;
358 }
359
360 /**
361  * Test whether LDAP libraries installed.
362  * Return: TRUE if library available.
363  */
364 gboolean ldaputil_test_ldap_lib( void ) {
365         return TRUE;
366 }
367
368 #endif  /* USE_LDAP */
369
370 /*
371  * End of Source.
372 */