13#ifdef ENABLE_LDAP_AUTH
16#include <glib/gstdio.h>
27#define G_LOG_DOMAIN "libgvm util"
29#define KEY_LDAP_HOST "ldaphost"
30#define KEY_LDAP_DN_AUTH "authdn"
43ldap_log (
const char *message)
45 g_debug (
"OpenLDAP: %s", message);
57 static int debug_level = 65535;
59#pragma GCC diagnostic push
60#pragma GCC diagnostic ignored "-Wpedantic"
63 ret = ber_set_option (NULL, LBER_OPT_LOG_PRINT_FN, (
void *) ldap_log);
64#pragma GCC diagnostic pop
65 if (ret != LBER_OPT_SUCCESS)
67 g_warning (
"%s: Failed to set LDAP debug print function: %s", __func__,
68 ldap_err2string (ret));
72 ret = ldap_set_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
73 if (ret != LDAP_OPT_SUCCESS)
75 g_warning (
"%s: Failed to set LDAP debug level: %s", __func__,
76 ldap_err2string (ret));
95 const gchar *username,
const gchar *password,
102 if (info == NULL || username == NULL || password == NULL || !info->
ldap_host)
104 g_debug (
"Not attempting ldap_connect: missing parameter.");
108 dn = ldap_auth_info_auth_dn (info, username);
110 ldap = ldap_auth_bind_2 (info->
ldap_host, dn, password,
115 g_debug (
"Could not bind to ldap host %s", info->
ldap_host);
119 ldap_unbind_ext_s (ldap, NULL, NULL);
140 gboolean allow_plaintext)
162 gboolean allow_plaintext, gboolean ldaps_only)
165 if (!ldap_host || !auth_dn)
168 if (ldap_auth_dn_is_good (auth_dn) == FALSE)
173 info->
auth_dn = g_strdup (auth_dn);
209 if (info == NULL || username == NULL)
212 gchar *dn = g_strdup_printf (info->
auth_dn, username);
231ldap_auth_bind (
const gchar *host,
const gchar *userdn,
const gchar *password,
232 gboolean force_encryption,
const gchar *cacert)
234 return ldap_auth_bind_2 (host, userdn, password, force_encryption, cacert,
248ldap_init_internal (
const char *host, gboolean force_encryption)
251 gchar *ldapuri = NULL;
253 int ldapv3 = LDAP_VERSION3;
255 ldapuri = g_strconcat (
"ldap://", host, NULL);
257 ldap_return = ldap_initialize (&ldap, ldapuri);
259 if (ldap == NULL || ldap_return != LDAP_SUCCESS)
261 g_warning (
"Could not init LDAP connection for authentication.");
267 ldap_return = ldap_set_option (ldap, LDAP_OPT_PROTOCOL_VERSION, &ldapv3);
268 if (ldap_return != LDAP_SUCCESS)
270 g_warning (
"Aborting, could not set ldap protocol version to 3: %s.",
271 ldap_err2string (ldap_return));
276 ldap_return = ldap_start_tls_s (ldap, NULL, NULL);
277 if (ldap_return != LDAP_SUCCESS)
280 g_warning (
"StartTLS failed, trying to establish ldaps connection.");
282 ldapuri = g_strconcat (
"ldaps://", host, NULL);
284 ldap_return = ldap_initialize (&ldap, ldapuri);
285 if (ldap == NULL || ldap_return != LDAP_SUCCESS)
287 if (force_encryption == TRUE)
289 g_warning (
"Aborting ldap authentication: Could not init LDAP "
290 "StartTLS nor ldaps: %s.",
291 ldap_err2string (ldap_return));
297 g_warning (
"Could not init LDAP StartTLS, nor ldaps: %s.",
298 ldap_err2string (ldap_return));
300 "Reinit LDAP connection to do plaintext authentication");
301 ldap_unbind_ext_s (ldap, NULL, NULL);
305 ldap_return = ldap_initialize (&ldap, ldapuri);
306 if (ldap == NULL || ldap_return != LDAP_SUCCESS)
309 "Could not reopen LDAP connection for authentication.");
315 ldap_set_option (ldap, LDAP_OPT_PROTOCOL_VERSION, &ldapv3);
316 if (ldap_return != LDAP_SUCCESS)
319 "Aborting, could not set ldap protocol version to 3: %s.",
320 ldap_err2string (ldap_return));
330 ldap_set_option (ldap, LDAP_OPT_PROTOCOL_VERSION, &ldapv3);
331 if (ldap_return != LDAP_SUCCESS)
334 "Aborting, could not set ldap protocol version to 3: %s.",
335 ldap_err2string (ldap_return));
342 g_debug (
"LDAP StartTLS initialized.");
357ldap_init_internal_ldaps_only (
const char *host)
360 gchar *ldapuri = NULL;
362 int ldapv3 = LDAP_VERSION3;
364 ldapuri = g_strconcat (
"ldaps://", host, NULL);
366 ldap_return = ldap_initialize (&ldap, ldapuri);
367 if (ldap == NULL || ldap_return != LDAP_SUCCESS)
369 g_warning (
"Could not init LDAPS connection for authentication.");
375 ldap_return = ldap_set_option (ldap, LDAP_OPT_PROTOCOL_VERSION, &ldapv3);
376 if (ldap_return != LDAP_SUCCESS)
378 g_warning (
"Aborting, could not set ldap protocol version to 3: %s.",
379 ldap_err2string (ldap_return));
384 g_debug (
"LDAPS initialized.");
404ldap_auth_bind_2 (
const gchar *host,
const gchar *userdn,
const gchar *password,
405 gboolean force_encryption,
const gchar *cacert,
410 struct berval credential;
414 if (host == NULL || userdn == NULL || password == NULL)
419 if (strlen (password) == 0)
422 if (force_encryption == FALSE)
423 g_warning (
"Allowed plaintext LDAP authentication.");
430 fd = g_file_open_tmp (NULL, &name, &error);
433 g_warning (
"Could not open temp file for LDAP CACERTFILE: %s",
435 g_error_free (error);
439 if (g_chmod (name, 0600))
440 g_warning (
"Could not chmod for LDAP CACERTFILE");
442 g_file_set_contents (name, cacert, strlen (cacert), &error);
445 g_warning (
"Could not write LDAP CACERTFILE: %s", error->message);
446 g_error_free (error);
450 if (ldap_set_option (NULL, LDAP_OPT_X_TLS_CACERTFILE, name)
452 g_warning (
"Could not set LDAP CACERTFILE option.");
460 ldap = ldap_init_internal_ldaps_only (host);
462 ldap = ldap_init_internal (host, force_encryption);
469 gchar *use_dn = NULL;
473 if (ldap_str2dn (userdn, &dn, LDAP_DN_FORMAT_LDAPV3) == LDAP_SUCCESS)
475 gchar **use_uid = NULL;
478 uid = g_strsplit (userdn,
",", 2);
479 use_uid = g_strsplit (uid[0],
"=", 2);
481 if (!g_strcmp0 (use_uid[0],
"uid"))
488 g_strfreev (use_uid);
496 credential.bv_val = NULL;
497 credential.bv_len = 0U;
498 ldap_return = ldap_sasl_bind_s (ldap, NULL, LDAP_SASL_SIMPLE, &credential,
500 if (ldap_return != LDAP_SUCCESS)
502 g_warning (
"LDAP anonymous authentication failure: %s",
503 ldap_err2string (ldap_return));
508 char *attrs[2] = {
"dn", NULL};
509 LDAPMessage *result = NULL;
510 gchar **base = g_strsplit (userdn,
",", 2);
514 ldap_search_ext_s (ldap, base[1], LDAP_SCOPE_SUBTREE, uid[0], attrs,
515 0, NULL, NULL, NULL, 1, &result);
520 if (ldap_return != LDAP_SUCCESS)
521 use_dn = g_strdup (userdn);
525 found_dn = ldap_get_dn (ldap, result);
526 if ((found_dn == NULL) || (strlen (found_dn) == 0U))
527 use_dn = g_strdup (userdn);
529 use_dn = g_strdup (found_dn);
530 ldap_memfree (found_dn);
532 ldap_msgfree (result);
536 use_dn = g_strdup (userdn);
540 credential.bv_val = g_strdup (password);
541 credential.bv_len = strlen (password);
542 ldap_return = ldap_sasl_bind_s (ldap, use_dn, LDAP_SASL_SIMPLE,
543 &credential, NULL, NULL, NULL);
544 g_free (credential.bv_val);
546 if (ldap_return != LDAP_SUCCESS)
548 g_warning (
"LDAP authentication failure: %s.",
549 ldap_err2string (ldap_return));
580ldap_auth_dn_is_good (
const gchar *authdn)
586 if (authdn == NULL || authdn[0] ==
'\0')
590 if (!strstr (authdn,
"%s"))
594 char *pos = strchr (authdn,
'%');
595 pos = strchr (pos + 1,
'%');
599 ln = strlen (authdn);
602 if (strchr (authdn,
'\\') && authdn[ln - 2] ==
'%' && authdn[ln - 1] ==
's')
606 if (authdn[0] ==
'%' && authdn[1] ==
's' && authdn[2] ==
'@')
610 eg = g_strdup_printf (authdn,
"example");
612 if (ldap_str2dn (eg, &dn, LDAP_DN_FORMAT_LDAPV3))
633 g_warning (
"%s: GVM-libs compiled without LDAP", __func__);
652 gboolean allow_plaintext)
656 (void) allow_plaintext;
676 gboolean allow_plaintext, gboolean ldaps_only)
680 (void) allow_plaintext;
697 const gchar *username,
const gchar *password,
int ldap_enable_debug()
Dummy function for enabling LDAP debugging for manager.
int ldap_connect_authenticate(const gchar *username, const gchar *password, void *ldap_auth_info, const gchar *cacert)
Dummy function for Manager.
void ldap_auth_info_free(ldap_auth_info_t info)
Dummy function for Manager.
ldap_auth_info_t ldap_auth_info_new(const gchar *ldap_host, const gchar *auth_dn, gboolean allow_plaintext)
Dummy function for manager.
ldap_auth_info_t ldap_auth_info_new_2(const gchar *ldap_host, const gchar *auth_dn, gboolean allow_plaintext, gboolean ldaps_only)
Dummy function for manager.
Header for LDAP-Connect Authentication module.
struct ldap_auth_info * ldap_auth_info_t
Authentication schema and address type.
Schema (dn) and info to use for a basic ldap authentication.
gboolean allow_plaintext
!Whether or not StartTLS or LDAPS is required.
gboolean ldaps_only
Whether to try LDAPS before StartTLS.
gchar * ldap_host
Address of the ldap server, might include port.
gchar * auth_dn
DN to authenticate with.