2005-09-15 [paul] 1.9.14cvs32
[claws.git] / src / ldaputil.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003 Match Grun
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
36 #define SYLDAP_TEST_FILTER   "(objectclass=*)"
37 #define SYLDAP_SEARCHBASE_V2 "cn=config"
38 #define SYLDAP_SEARCHBASE_V3 ""
39 #define SYLDAP_V2_TEST_ATTR  "database"
40 #define SYLDAP_V3_TEST_ATTR  "namingcontexts"
41
42 /**
43  * Attempt to discover the base DN for a server using LDAP version 3.
44  * \param  ld  LDAP handle for a connected server.
45  * \param  tov Timeout value (seconds), or 0 for none, default 30 secs.
46  * \return List of Base DN's, or NULL if could not read. List should be
47  *         g_free() when done.
48  */
49 static GList *ldaputil_test_v3( LDAP *ld, gint tov ) {
50         GList *baseDN = NULL;
51         gint rc, i;
52         LDAPMessage *result, *e;
53         gchar *attribs[2];
54         BerElement *ber;
55         gchar *attribute;
56         gchar **vals;
57         struct timeval timeout;
58
59         /* Set timeout */
60         timeout.tv_usec = 0L;
61         if( tov > 0 ) {
62                 timeout.tv_sec = tov;
63         }
64         else {
65                 timeout.tv_sec = 30L;
66         }
67
68         /* Test for LDAP version 3 */
69         attribs[0] = SYLDAP_V3_TEST_ATTR;
70         attribs[1] = NULL;
71         rc = ldap_search_ext_s(
72                 ld, SYLDAP_SEARCHBASE_V3, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
73                 attribs, 0, NULL, NULL, &timeout, 0, &result );
74
75         if( rc == LDAP_SUCCESS ) {
76                 /* Process entries */
77                 for( e = ldap_first_entry( ld, result );
78                      e != NULL;
79                      e = ldap_next_entry( ld, e ) ) 
80                 {
81                         /* Process attributes */
82                         for( attribute = ldap_first_attribute( ld, e, &ber );
83                              attribute != NULL;
84                              attribute = ldap_next_attribute( ld, e, ber ) )
85                         {
86                                 if( strcasecmp(
87                                         attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
88                                 {
89                                         vals = ldap_get_values( ld, e, attribute );
90                                         if( vals != NULL ) {
91                                                 for( i = 0; vals[i] != NULL; i++ ) {
92                                                         baseDN = g_list_append(
93                                                                 baseDN, g_strdup( vals[i] ) );
94                                                 }
95                                         }
96                                         ldap_value_free( vals );
97                                 }
98                                 ldap_memfree( attribute );
99                         }
100                         if( ber != NULL ) {
101                                 ber_free( ber, 0 );
102                         }
103                         ber = NULL;
104                 }
105         }
106         ldap_msgfree( result );
107         return baseDN;
108 }
109
110 /**
111  * Attempt to discover the base DN for a server using LDAP version 2.
112  * \param  ld  LDAP handle for a connected server.
113  * \param  tov Timeout value (seconds), or 0 for none, default 30 secs.
114  * \return List of Base DN's, or NULL if could not read. List should be
115  *         g_free() when done.
116  */
117 static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
118         GList *baseDN = NULL;
119         gint rc, i;
120         LDAPMessage *result, *e;
121         gchar *attribs[1];
122         BerElement *ber;
123         gchar *attribute;
124         gchar **vals;
125         struct timeval timeout;
126
127         /* Set timeout */
128         timeout.tv_usec = 0L;
129         if( tov > 0 ) {
130                 timeout.tv_sec = tov;
131         }
132         else {
133                 timeout.tv_sec = 30L;
134         }
135
136         attribs[0] = NULL;
137         rc = ldap_search_ext_s(
138                 ld, SYLDAP_SEARCHBASE_V2, LDAP_SCOPE_BASE, SYLDAP_TEST_FILTER,
139                 attribs, 0, NULL, NULL, &timeout, 0, &result );
140
141         if( rc == LDAP_SUCCESS ) {
142                 /* Process entries */
143                 for( e = ldap_first_entry( ld, result );
144                      e != NULL;
145                      e = ldap_next_entry( ld, e ) )
146                 {
147                         /* Process attributes */
148                         for( attribute = ldap_first_attribute( ld, e, &ber );
149                              attribute != NULL;
150                              attribute = ldap_next_attribute( ld, e, ber ) )
151                         {
152                                 if( strcasecmp(
153                                         attribute,
154                                         SYLDAP_V2_TEST_ATTR ) == 0 ) {
155                                         vals = ldap_get_values( ld, e, attribute );
156                                         if( vals != NULL ) {
157                                                 for( i = 0; vals[i] != NULL; i++ ) {
158                                                         char *ch;
159                                                         /*
160                                                          * Strip the 'ldb:' from the
161                                                          * front of the value.
162                                                          */
163                                                         ch = ( char * ) strchr( vals[i], ':' );
164                                                         if( ch ) {
165                                                                 gchar *bn = g_strdup( ++ch );
166                                                                 g_strchomp( bn );
167                                                                 g_strchug( bn );
168                                                                 baseDN = g_list_append(
169                                                                         baseDN, g_strdup( bn ) );
170                                                                 g_free( bn );
171                                                         }
172                                                 }
173                                         }
174                                         ldap_value_free( vals );
175                                 }
176                                 ldap_memfree( attribute );
177                         }
178                         if( ber != NULL ) {
179                                 ber_free( ber, 0 );
180                         }
181                         ber = NULL;
182                 }
183         }
184         ldap_msgfree( result );
185         return baseDN;
186 }
187
188 /**
189  * Attempt to discover the base DN for the server.
190  * \param  host   Host name.
191  * \param  port   Port number.
192  * \param  bindDN Bind DN (optional).
193  * \param  bindPW Bind PW (optional).
194  * \param  tov    Timeout value (seconds), or 0 for none, default 30 secs.
195  * \return List of Base DN's, or NULL if could not read. This list should be
196  *         g_free() when done.
197  */
198 GList *ldaputil_read_basedn(
199                 const gchar *host, const gint port, const gchar *bindDN,
200                 const gchar *bindPW, const gint tov )
201 {
202         GList *baseDN = NULL;
203         LDAP *ld;
204         gint rc;
205
206         if( host == NULL ) return baseDN;
207         if( port < 1 ) return baseDN;
208
209         /* Connect to server. */
210         if( ( ld = ldap_init( host, port ) ) == NULL ) {
211                 return baseDN;
212         }
213
214         /* Bind to the server, if required */
215         if( bindDN ) {
216                 if( *bindDN != '\0' ) {
217                         rc = ldap_simple_bind_s( ld, bindDN, bindPW );
218                         if( rc != LDAP_SUCCESS ) {
219                                 ldap_unbind( ld );
220                                 return baseDN;
221                         }
222                 }
223         }
224
225         /* Test for LDAP version 3 */
226         baseDN = ldaputil_test_v3( ld, tov );
227         if( baseDN == NULL ) {
228                 baseDN = ldaputil_test_v2( ld, tov );
229         }
230         ldap_unbind( ld );
231         return baseDN;
232 }
233
234 /**
235  * Attempt to connect to the server.
236  * Enter:
237  * \param  host Host name.
238  * \param  port Port number.
239  * \return <i>TRUE</i> if connected successfully.
240  */
241 gboolean ldaputil_test_connect( const gchar *host, const gint port ) {
242         gboolean retVal = FALSE;
243         LDAP *ld;
244
245         if( host == NULL ) return retVal;
246         if( port < 1 ) return retVal;
247         ld = ldap_open( host, port );
248         if( ld != NULL ) {
249                 ldap_unbind( ld );
250                 retVal = TRUE;
251         }
252         return retVal;
253 }
254
255 /**
256  * Test whether LDAP libraries installed.
257  * Return: TRUE if library available.
258  */
259 gboolean ldaputil_test_ldap_lib( void ) {
260         return TRUE;
261 }
262
263 #endif  /* USE_LDAP */
264
265 /*
266  * End of Source.
267 */