2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003 Match Grun
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Some utility functions to access LDAP servers.
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"
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
49 static GList *ldaputil_test_v3( LDAP *ld, gint tov ) {
52 LDAPMessage *result, *e;
57 struct timeval timeout;
68 /* Test for LDAP version 3 */
69 attribs[0] = SYLDAP_V3_TEST_ATTR;
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 );
75 if( rc == LDAP_SUCCESS ) {
77 for( e = ldap_first_entry( ld, result );
79 e = ldap_next_entry( ld, e ) )
81 /* Process attributes */
82 for( attribute = ldap_first_attribute( ld, e, &ber );
84 attribute = ldap_next_attribute( ld, e, ber ) )
87 attribute, SYLDAP_V3_TEST_ATTR ) == 0 )
89 vals = ldap_get_values( ld, e, attribute );
91 for( i = 0; vals[i] != NULL; i++ ) {
92 baseDN = g_list_append(
93 baseDN, g_strdup( vals[i] ) );
96 ldap_value_free( vals );
98 ldap_memfree( attribute );
106 ldap_msgfree( result );
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.
117 static GList *ldaputil_test_v2( LDAP *ld, gint tov ) {
118 GList *baseDN = NULL;
120 LDAPMessage *result, *e;
125 struct timeval timeout;
128 timeout.tv_usec = 0L;
130 timeout.tv_sec = tov;
133 timeout.tv_sec = 30L;
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 );
141 if( rc == LDAP_SUCCESS ) {
142 /* Process entries */
143 for( e = ldap_first_entry( ld, result );
145 e = ldap_next_entry( ld, e ) )
147 /* Process attributes */
148 for( attribute = ldap_first_attribute( ld, e, &ber );
150 attribute = ldap_next_attribute( ld, e, ber ) )
154 SYLDAP_V2_TEST_ATTR ) == 0 ) {
155 vals = ldap_get_values( ld, e, attribute );
157 for( i = 0; vals[i] != NULL; i++ ) {
160 * Strip the 'ldb:' from the
161 * front of the value.
163 ch = ( char * ) strchr( vals[i], ':' );
165 gchar *bn = g_strdup( ++ch );
168 baseDN = g_list_append(
169 baseDN, g_strdup( bn ) );
174 ldap_value_free( vals );
176 ldap_memfree( attribute );
184 ldap_msgfree( result );
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.
198 GList *ldaputil_read_basedn(
199 const gchar *host, const gint port, const gchar *bindDN,
200 const gchar *bindPW, const gint tov )
202 GList *baseDN = NULL;
206 if( host == NULL ) return baseDN;
207 if( port < 1 ) return baseDN;
209 /* Connect to server. */
210 if( ( ld = ldap_init( host, port ) ) == NULL ) {
214 /* Bind to the server, if required */
216 if( *bindDN != '\0' ) {
217 rc = ldap_simple_bind_s( ld, bindDN, bindPW );
218 if( rc != LDAP_SUCCESS ) {
225 /* Test for LDAP version 3 */
226 baseDN = ldaputil_test_v3( ld, tov );
227 if( baseDN == NULL ) {
228 baseDN = ldaputil_test_v2( ld, tov );
235 * Attempt to connect to the server.
237 * \param host Host name.
238 * \param port Port number.
239 * \return <i>TRUE</i> if connected successfully.
241 gboolean ldaputil_test_connect( const gchar *host, const gint port ) {
242 gboolean retVal = FALSE;
245 if( host == NULL ) return retVal;
246 if( port < 1 ) return retVal;
247 ld = ldap_open( host, port );
256 * Test whether LDAP libraries installed.
257 * Return: TRUE if library available.
259 gboolean ldaputil_test_ldap_lib( void ) {
263 #endif /* USE_LDAP */