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