2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2015 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 3 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, see <http://www.gnu.org/licenses/>.
20 * Some utility functions to access LDAP servers.
25 #include "claws-features.h"
31 #include <glib/gi18n.h>
35 #include "common/utils.h"
37 #include "ldapserver.h"
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"
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
53 static GList
*ldaputil_test_v3( LDAP
*ld
, gint tov
, gint
*errcode
) {
56 LDAPMessage
*result
= NULL
, *e
;
61 struct timeval timeout
;
72 /* Test for LDAP version 3 */
73 attribs
[0] = SYLDAP_V3_TEST_ATTR
;
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
);
79 if( rc
== LDAP_SUCCESS
) {
81 for( e
= ldap_first_entry( ld
, result
);
83 e
= ldap_next_entry( ld
, e
) )
85 /* Process attributes */
86 for( attribute
= ldap_first_attribute( ld
, e
, &ber
);
88 attribute
= ldap_next_attribute( ld
, e
, ber
) )
91 attribute
, SYLDAP_V3_TEST_ATTR
) == 0 )
93 vals
= ldap_get_values_len( ld
, e
, attribute
);
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
) );
100 ldap_value_free_len( vals
);
102 ldap_memfree( attribute
);
110 debug_print("LDAP: Error %d (%s)\n", rc
, ldaputil_get_error(ld
));
115 ldap_msgfree( result
);
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.
126 static GList
*ldaputil_test_v2( LDAP
*ld
, gint tov
) {
127 GList
*baseDN
= NULL
;
129 LDAPMessage
*result
= NULL
, *e
;
133 struct berval
**vals
;
134 struct timeval timeout
;
137 timeout
.tv_usec
= 0L;
139 timeout
.tv_sec
= tov
;
142 timeout
.tv_sec
= 30L;
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
);
150 if( rc
== LDAP_SUCCESS
) {
151 /* Process entries */
152 for( e
= ldap_first_entry( ld
, result
);
154 e
= ldap_next_entry( ld
, e
) )
156 /* Process attributes */
157 for( attribute
= ldap_first_attribute( ld
, e
, &ber
);
159 attribute
= ldap_next_attribute( ld
, e
, ber
) )
163 SYLDAP_V2_TEST_ATTR
) == 0 ) {
164 vals
= ldap_get_values_len( ld
, e
, attribute
);
166 for( i
= 0; vals
[i
] != NULL
; i
++ ) {
169 * Strip the 'ldb:' from the
170 * front of the value.
172 tmp
= g_strndup( vals
[i
]->bv_val
, vals
[i
]->bv_len
);
173 ch
= ( char * ) strchr( tmp
, ':' );
175 gchar
*bn
= g_strdup( ++ch
);
177 baseDN
= g_list_append(
178 baseDN
, g_strdup( bn
) );
184 ldap_value_free_len( vals
);
186 ldap_memfree( attribute
);
195 ldap_msgfree( result
);
199 int claws_ldap_simple_bind_s( LDAP
*ld
, LDAP_CONST
char *dn
, LDAP_CONST
char *passwd
)
201 debug_print("binding: DN->%s\n", dn
?dn
:"null");
205 if ( passwd
!= NULL
) {
206 cred
.bv_val
= (char *) passwd
;
207 cred
.bv_len
= strlen( passwd
);
213 return ldap_sasl_bind_s( ld
, dn
, LDAP_SASL_SIMPLE
, &cred
,
216 return ldap_simple_bind_s(ld
, (PCHAR
)dn
, (PCHAR
)passwd
);
221 * Attempt to discover the base DN for the server.
222 * \param host Host name.
223 * \param port Port number.
224 * \param bindDN Bind DN (optional).
225 * \param bindPW Bind PW (optional).
226 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
227 * \return List of Base DN's, or NULL if could not read. This list should be
228 * g_free() when done.
230 GList
*ldaputil_read_basedn(
231 const gchar
*host
, const gint port
, const gchar
*bindDN
,
232 const gchar
*bindPW
, const gint tov
, int ssl
, int tls
)
234 GList
*baseDN
= NULL
;
236 LdapControl
*ctl
= ldapctl_create();
244 ldapctl_set_tls(ctl
, tls
);
245 ldapctl_set_ssl(ctl
, ssl
);
246 ldapctl_set_port(ctl
, port
);
247 ldapctl_set_host(ctl
, host
);
248 ldapctl_set_timeout(ctl
, tov
);
249 ldapctl_set_bind_dn(ctl
, bindDN
);
251 ld
= ldapsvr_connect(ctl
);
256 baseDN
= ldaputil_test_v3( ld
, tov
, &rc
);
258 debug_print("Using LDAP v3\n");
261 if( baseDN
== NULL
&& !LDAP_API_ERROR(rc
) ) {
263 if( baseDN
== NULL
) {
265 baseDN
= ldaputil_test_v2( ld
, tov
);
267 debug_print("Using LDAP v2\n");
270 ldapsvr_disconnect(ld
);
278 * Attempt to connect to the server.
280 * \param host Host name.
281 * \param port Port number.
282 * \return <i>TRUE</i> if connected successfully.
284 gboolean
ldaputil_test_connect( const gchar
*host
, const gint port
, int ssl
, int tls
, int secs
) {
285 gboolean retVal
= FALSE
;
286 LdapControl
*ctl
= ldapctl_create();
289 ldapctl_set_tls(ctl
, tls
);
290 ldapctl_set_ssl(ctl
, ssl
);
291 ldapctl_set_port(ctl
, port
);
292 ldapctl_set_host(ctl
, host
);
293 ldapctl_set_timeout(ctl
, secs
);
295 ld
= ldapsvr_connect(ctl
);
297 ldapsvr_disconnect(ld
);
298 debug_print("ld != NULL\n");
307 * Test whether LDAP libraries installed.
308 * Return: TRUE if library available.
310 gboolean
ldaputil_test_ldap_lib( void ) {
314 const gchar
*ldaputil_get_error(LDAP
*ld
)
316 gchar
*ld_error
= NULL
;
317 static gchar error
[512];
319 ldap_get_option( ld
, LDAP_OPT_ERROR_STRING
, &ld_error
);
320 if (ld_error
!= NULL
)
321 strncpy2(error
, ld_error
, sizeof(error
));
323 strncpy2(error
, _("Unknown error"), sizeof(error
));
325 /* From https://msdn.microsoft.com/en-us/library/aa366594%28v=vs.85%29.aspx
326 * "LDAP_OPT_ERROR_STRING returns a pointer to an internal static
327 * string table, and ldap_memfree should not be called when using
328 * this session option."
330 ldap_memfree(ld_error
);
335 #endif /* USE_LDAP */