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