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