2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2018 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"
41 #define SYLDAP_TEST_FILTER "(objectclass=*)"
42 #define SYLDAP_SEARCHBASE_V2 "cn=config"
43 #define SYLDAP_SEARCHBASE_V3 ""
44 #define SYLDAP_V2_TEST_ATTR "database"
45 #define SYLDAP_V3_TEST_ATTR "namingcontexts"
48 * Attempt to discover the base DN for a server using LDAP version 3.
49 * \param ld LDAP handle for a connected server.
50 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
51 * \return List of Base DN's, or NULL if could not read. List should be
54 static GList
*ldaputil_test_v3( LDAP
*ld
, gint tov
, gint
*errcode
) {
57 LDAPMessage
*result
= NULL
, *e
;
62 struct timeval timeout
;
73 /* Test for LDAP version 3 */
74 attribs
[0] = SYLDAP_V3_TEST_ATTR
;
76 rc
= ldap_search_ext_s(
77 ld
, SYLDAP_SEARCHBASE_V3
, LDAP_SCOPE_BASE
, SYLDAP_TEST_FILTER
,
78 attribs
, 0, NULL
, NULL
, &timeout
, 0, &result
);
80 if( rc
== LDAP_SUCCESS
) {
81 log_print(LOG_PROTOCOL
, _("LDAP (search): successful\n"));
83 for( e
= ldap_first_entry( ld
, result
);
85 e
= ldap_next_entry( ld
, e
) )
87 /* Process attributes */
88 for( attribute
= ldap_first_attribute( ld
, e
, &ber
);
90 attribute
= ldap_next_attribute( ld
, e
, ber
) )
93 attribute
, SYLDAP_V3_TEST_ATTR
) == 0 )
95 vals
= ldap_get_values_len( ld
, e
, attribute
);
97 for( i
= 0; vals
[i
] != NULL
; i
++ ) {
98 baseDN
= g_list_append(
99 baseDN
, g_strndup( vals
[i
]->bv_val
, vals
[i
]->bv_len
) );
102 ldap_value_free_len( vals
);
104 ldap_memfree( attribute
);
112 log_error(LOG_PROTOCOL
, _("LDAP error (search): %d (%s)\n"),
113 rc
, ldaputil_get_error(ld
));
114 debug_print("LDAP: Error %d (%s)\n", rc
, ldaputil_get_error(ld
));
120 ldap_msgfree( result
);
125 * Attempt to discover the base DN for a server using LDAP version 2.
126 * \param ld LDAP handle for a connected server.
127 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
128 * \return List of Base DN's, or NULL if could not read. List should be
129 * g_free() when done.
131 static GList
*ldaputil_test_v2( LDAP
*ld
, gint tov
) {
132 GList
*baseDN
= NULL
;
134 LDAPMessage
*result
= NULL
, *e
;
138 struct berval
**vals
;
139 struct timeval timeout
;
142 timeout
.tv_usec
= 0L;
144 timeout
.tv_sec
= tov
;
147 timeout
.tv_sec
= 30L;
151 rc
= ldap_search_ext_s(
152 ld
, SYLDAP_SEARCHBASE_V2
, LDAP_SCOPE_BASE
, SYLDAP_TEST_FILTER
,
153 attribs
, 0, NULL
, NULL
, &timeout
, 0, &result
);
155 if( rc
== LDAP_SUCCESS
) {
156 log_print(LOG_PROTOCOL
, _("LDAP (search): successful\n"));
157 /* Process entries */
158 for( e
= ldap_first_entry( ld
, result
);
160 e
= ldap_next_entry( ld
, e
) )
162 /* Process attributes */
163 for( attribute
= ldap_first_attribute( ld
, e
, &ber
);
165 attribute
= ldap_next_attribute( ld
, e
, ber
) )
169 SYLDAP_V2_TEST_ATTR
) == 0 ) {
170 vals
= ldap_get_values_len( ld
, e
, attribute
);
172 for( i
= 0; vals
[i
] != NULL
; i
++ ) {
175 * Strip the 'ldb:' from the
176 * front of the value.
178 tmp
= g_strndup( vals
[i
]->bv_val
, vals
[i
]->bv_len
);
179 ch
= ( char * ) strchr( tmp
, ':' );
181 gchar
*bn
= g_strdup( ++ch
);
183 baseDN
= g_list_append(
184 baseDN
, g_strdup( bn
) );
190 ldap_value_free_len( vals
);
192 ldap_memfree( attribute
);
200 log_error(LOG_PROTOCOL
, _("LDAP error (search): %d (%s)\n"),
201 rc
, ldaputil_get_error(ld
));
202 debug_print("LDAP: Error %d (%s)\n", rc
, ldaputil_get_error(ld
));
205 ldap_msgfree( result
);
209 int claws_ldap_simple_bind_s( LDAP
*ld
, LDAP_CONST
char *dn
, LDAP_CONST
char *passwd
)
211 debug_print("binding: DN->%s\n", dn
?dn
:"null");
215 if ( passwd
!= NULL
) {
216 cred
.bv_val
= (char *) passwd
;
217 cred
.bv_len
= strlen( passwd
);
223 return ldap_sasl_bind_s( ld
, dn
, LDAP_SASL_SIMPLE
, &cred
,
226 return ldap_simple_bind_s(ld
, (PCHAR
)dn
, (PCHAR
)passwd
);
231 * Attempt to discover the base DN for the server.
232 * \param host Host name.
233 * \param port Port number.
234 * \param bindDN Bind DN (optional).
235 * \param bindPW Bind PW (optional).
236 * \param tov Timeout value (seconds), or 0 for none, default 30 secs.
237 * \return List of Base DN's, or NULL if could not read. This list should be
238 * g_free() when done.
240 GList
*ldaputil_read_basedn(
241 const gchar
*host
, const gint port
, const gchar
*bindDN
,
242 const gchar
*bindPW
, const gint tov
, int ssl
, int tls
)
244 GList
*baseDN
= NULL
;
254 ctl
= ldapctl_create();
255 ldapctl_set_tls(ctl
, tls
);
256 ldapctl_set_ssl(ctl
, ssl
);
257 ldapctl_set_port(ctl
, port
);
258 ldapctl_set_host(ctl
, host
);
259 ldapctl_set_timeout(ctl
, tov
);
260 ldapctl_set_bind_dn(ctl
, bindDN
);
262 ld
= ldapsvr_connect(ctl
);
267 baseDN
= ldaputil_test_v3( ld
, tov
, &rc
);
269 debug_print("Using LDAP v3\n");
272 if( baseDN
== NULL
&& !LDAP_API_ERROR(rc
) ) {
274 if( baseDN
== NULL
) {
276 baseDN
= ldaputil_test_v2( ld
, tov
);
278 debug_print("Using LDAP v2\n");
281 ldapsvr_disconnect(ld
);
289 * Attempt to connect to the server.
291 * \param host Host name.
292 * \param port Port number.
293 * \return <i>TRUE</i> if connected successfully.
295 gboolean
ldaputil_test_connect( const gchar
*host
, const gint port
, int ssl
, int tls
, int secs
) {
296 gboolean retVal
= FALSE
;
297 LdapControl
*ctl
= ldapctl_create();
300 ldapctl_set_tls(ctl
, tls
);
301 ldapctl_set_ssl(ctl
, ssl
);
302 ldapctl_set_port(ctl
, port
);
303 ldapctl_set_host(ctl
, host
);
304 ldapctl_set_timeout(ctl
, secs
);
306 ld
= ldapsvr_connect(ctl
);
308 ldapsvr_disconnect(ld
);
309 debug_print("ld != NULL\n");
318 * Test whether LDAP libraries installed.
319 * Return: TRUE if library available.
321 gboolean
ldaputil_test_ldap_lib( void ) {
325 const gchar
*ldaputil_get_error(LDAP
*ld
)
327 gchar
*ld_error
= NULL
;
328 static gchar error
[512];
330 ldap_get_option( ld
, LDAP_OPT_ERROR_STRING
, &ld_error
);
331 if (ld_error
!= NULL
)
332 strncpy2(error
, ld_error
, sizeof(error
));
334 strncpy2(error
, _("Unknown error"), sizeof(error
));
336 /* From https://msdn.microsoft.com/en-us/library/aa366594%28v=vs.85%29.aspx
337 * "LDAP_OPT_ERROR_STRING returns a pointer to an internal static
338 * string table, and ldap_memfree should not be called when using
339 * this session option."
341 ldap_memfree(ld_error
);
346 #endif /* USE_LDAP */