2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2007 Match Grun and the Claws Mail team
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.
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.
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.
21 * Some utility functions to access LDAP servers.
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"
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
50 static GList *ldaputil_test_v3( LDAP *ld, gint tov, gint *errcode ) {
53 LDAPMessage *result = NULL, *e;
58 struct timeval timeout;
69 /* Test for LDAP version 3 */
70 attribs[0] = SYLDAP_V3_TEST_ATTR;
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 );
76 if( rc == LDAP_SUCCESS ) {
78 for( e = ldap_first_entry( ld, result );
80 e = ldap_next_entry( ld, e ) )
82 /* Process attributes */
83 for( attribute = ldap_first_attribute( ld, e, &ber );
85 attribute = ldap_next_attribute( ld, e, ber ) )
88 attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
90 vals = ldap_get_values_len( ld, e, attribute );
92 for( i = 0; vals[i] != NULL; i++ ) {
93 baseDN = g_list_append(
94 baseDN, g_strndup( vals[i]->bv_val, vals[i]->bv_len ) );
97 ldap_value_free_len( vals );
99 ldap_memfree( attribute );
110 ldap_msgfree( result );
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.
121 static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
122 GList *baseDN = NULL;
124 LDAPMessage *result = NULL, *e;
128 struct berval **vals;
129 struct timeval timeout;
132 timeout.tv_usec = 0L;
134 timeout.tv_sec = tov;
137 timeout.tv_sec = 30L;
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 );
145 if( rc == LDAP_SUCCESS ) {
146 /* Process entries */
147 for( e = ldap_first_entry( ld, result );
149 e = ldap_next_entry( ld, e ) )
151 /* Process attributes */
152 for( attribute = ldap_first_attribute( ld, e, &ber );
154 attribute = ldap_next_attribute( ld, e, ber ) )
158 SYLDAP_V2_TEST_ATTR ) == 0 ) {
159 vals = ldap_get_values_len( ld, e, attribute );
161 for( i = 0; vals[i] != NULL; i++ ) {
164 * Strip the 'ldb:' from the
165 * front of the value.
167 tmp = g_strndup( vals[i]->bv_val, vals[i]->bv_len);
168 ch = ( char * ) strchr( tmp, ':' );
170 gchar *bn = g_strdup( ++ch );
173 baseDN = g_list_append(
174 baseDN, g_strdup( bn ) );
180 ldap_value_free_len( vals );
182 ldap_memfree( attribute );
191 ldap_msgfree( result );
196 * Attempt to discover the base DN for the server.
197 * \param host Host name.
198 * \param port Port number.
199 * \param bindDN Bind DN (optional).
200 * \param bindPW Bind PW (optional).
201 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
202 * \return List of Base DN's, or NULL if could not read. This list should be
203 * g_free() when done.
205 GList *ldaputil_read_basedn(
206 const gchar *host, const gint port, const gchar *bindDN,
207 const gchar *bindPW, const gint tov, int ssl, int tls )
209 GList *baseDN = NULL;
216 if( host == NULL ) return baseDN;
217 if( port < 1 ) return baseDN;
219 /* Connect to server. */
222 ld = ldap_init( host, port );
224 gchar *uri = g_strdup_printf("ldaps://%s:%d",
226 rc = ldap_initialize(&ld, uri);
235 version = LDAP_VERSION3;
236 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
237 if( rc != LDAP_OPT_SUCCESS ) {
241 rc = ldap_start_tls_s( ld, NULL, NULL );
249 /* Bind to the server, if required */
251 if( *bindDN != '\0' ) {
252 rc = ldap_simple_bind_s( ld, bindDN, bindPW );
253 if( rc != LDAP_SUCCESS ) {
260 /* Test for LDAP version 3 */
261 baseDN = ldaputil_test_v3( ld, tov, &rc );
263 if( baseDN == NULL && !LDAP_API_ERROR(rc) ) {
264 baseDN = ldaputil_test_v2( ld, tov );
266 if (ld && !LDAP_API_ERROR(rc))
273 * Attempt to connect to the server.
275 * \param host Host name.
276 * \param port Port number.
277 * \return <i>TRUE</i> if connected successfully.
279 gboolean ldaputil_test_connect( const gchar *host, const gint port, int ssl, int tls ) {
280 gboolean retVal = FALSE;
286 if( host == NULL ) return retVal;
287 if( port < 1 ) return retVal;
289 ld = ldap_open( host, port );
291 gchar *uri = g_strdup_printf("ldaps://%s:%d",
293 ldap_initialize(&ld, uri);
301 GList *dummy = ldaputil_test_v3( ld, 10, &rc );
304 if (LDAP_API_ERROR(rc))
310 version = LDAP_VERSION3;
311 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
312 if( rc != LDAP_OPT_SUCCESS ) {
317 rc = ldap_start_tls_s( ld, NULL, NULL );
332 * Test whether LDAP libraries installed.
333 * Return: TRUE if library available.
335 gboolean ldaputil_test_ldap_lib( void ) {
339 #endif /* USE_LDAP */