/**
* collectd - src/openldap.c
* Copyright (C) 2011 Kimo Rosenbaum
- * Copyright (C) 2014 Marc Fournier
+ * Copyright (C) 2014-2015 Marc Fournier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
#include "plugin.h"
#include "configfile.h"
+#if defined(__APPLE__)
+#pragma clang diagnostic push
+#pragma clang diagnostic warning "-Wdeprecated-declarations"
+#endif
+
#include <lber.h>
#include <ldap.h>
{
char *name;
+ char *binddn;
+ char *password;
char *cacert;
char *host;
int state;
};
typedef struct cldap_s cldap_t; /* }}} */
+static cldap_t **databases = NULL;
+static size_t databases_num = 0;
+
static void cldap_free (cldap_t *st) /* {{{ */
{
if (st == NULL)
return;
+ sfree (st->binddn);
+ sfree (st->password);
sfree (st->cacert);
sfree (st->host);
sfree (st->name);
{
LDAP *ld;
int rc;
+
+ if (st->state && st->ld)
+ {
+ DEBUG ("openldap plugin: Already connected to %s", st->url);
+ return (0);
+ }
+
rc = ldap_initialize (&ld, st->url);
if (rc != LDAP_SUCCESS)
{
ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
&(const struct timeval){st->timeout, 0});
+ ldap_set_option (st->ld, LDAP_OPT_RESTART, LDAP_OPT_ON);
+
if (st->cacert != NULL)
ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
}
struct berval cred;
- cred.bv_val = "";
- cred.bv_len = 0;
+ if (st->password != NULL)
+ {
+ cred.bv_val = st->password;
+ cred.bv_len = strlen (st->password);
+ }
+ else
+ {
+ cred.bv_val = "";
+ cred.bv_len = 0;
+ }
- rc = ldap_sasl_bind_s (st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
+ rc = ldap_sasl_bind_s (st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred,
+ NULL, NULL, NULL);
if (rc != LDAP_SUCCESS)
{
ERROR ("openldap plugin: Failed to bind to %s: %s",
if ((st->host == NULL)
|| (strcmp ("", st->host) == 0)
|| (strcmp ("localhost", st->host) == 0))
- {
sstrncpy (vl.host, hostname_g, sizeof (vl.host));
- }
else
- {
sstrncpy (vl.host, st->host, sizeof (vl.host));
- }
sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
if (st->name != NULL)
ERROR ("openldap plugin: Failed to execute search: %s",
ldap_err2string (rc));
ldap_msgfree (result);
+ st->state = 0;
ldap_unbind_ext_s (st->ld, NULL, NULL);
return (-1);
}
}
ldap_msgfree (result);
- ldap_unbind_ext_s (st->ld, NULL, NULL);
return (0);
} /* }}} int cldap_read_host */
int i;
int status;
- st = malloc (sizeof (*st));
+ st = calloc (1, sizeof (*st));
if (st == NULL)
{
- ERROR ("openldap plugin: malloc failed.");
+ ERROR ("openldap plugin: calloc failed.");
return (-1);
}
- memset (st, 0, sizeof (*st));
status = cf_util_get_string (ci, &st->name);
if (status != 0)
}
st->starttls = 0;
- st->timeout = -1;
+ st->timeout = (long) (CDTIME_T_TO_MS(plugin_get_interval()) / 1000);
st->verifyhost = 1;
st->version = LDAP_VERSION3;
{
oconfig_item_t *child = ci->children + i;
- if (strcasecmp ("CACert", child->key) == 0)
+ if (strcasecmp ("BindDN", child->key) == 0)
+ status = cf_util_get_string (child, &st->binddn);
+ else if (strcasecmp ("Password", child->key) == 0)
+ status = cf_util_get_string (child, &st->password);
+ else if (strcasecmp ("CACert", child->key) == 0)
status = cf_util_get_string (child, &st->cacert);
else if (strcasecmp ("StartTLS", child->key) == 0)
status = cf_util_get_boolean (child, &st->starttls);
}
if ((status == 0) && (ludpp->lud_host != NULL))
- {
st->host = strdup (ludpp->lud_host);
- }
ldap_free_urldesc (ludpp);
}
if (status == 0)
{
- user_data_t ud;
- char callback_name[3*DATA_MAX_NAME_LEN];
-
- memset (&ud, 0, sizeof (ud));
- ud.data = st;
-
- memset (callback_name, 0, sizeof (callback_name));
- ssnprintf (callback_name, sizeof (callback_name),
- "openldap/%s/%s",
- (st->host != NULL) ? st->host : hostname_g,
- (st->name != NULL) ? st->name : "default"),
-
- status = plugin_register_complex_read (/* group = */ NULL,
- /* name = */ callback_name,
- /* callback = */ cldap_read_host,
- /* interval = */ NULL,
- /* user_data = */ &ud);
+ cldap_t **temp;
+
+ temp = (cldap_t **) realloc (databases,
+ sizeof (*databases) * (databases_num + 1));
+
+ if (temp == NULL)
+ {
+ ERROR ("openldap plugin: realloc failed");
+ status = -1;
+ }
+ else
+ {
+ user_data_t ud;
+ char callback_name[3*DATA_MAX_NAME_LEN];
+
+ databases = temp;
+ databases[databases_num] = st;
+ databases_num++;
+
+ memset (&ud, 0, sizeof (ud));
+ ud.data = st;
+
+ memset (callback_name, 0, sizeof (callback_name));
+ ssnprintf (callback_name, sizeof (callback_name),
+ "openldap/%s/%s",
+ (st->host != NULL) ? st->host : hostname_g,
+ (st->name != NULL) ? st->name : "default"),
+
+ status = plugin_register_complex_read (/* group = */ NULL,
+ /* name = */ callback_name,
+ /* callback = */ cldap_read_host,
+ /* interval = */ 0,
+ /* user_data = */ &ud);
+ }
}
if (status != 0)
return (0);
} /* }}} int cldap_init */
+static int cldap_shutdown (void) /* {{{ */
+{
+ size_t i;
+
+ for (i = 0; i < databases_num; i++)
+ if (databases[i]->ld != NULL)
+ ldap_unbind_ext_s (databases[i]->ld, NULL, NULL);
+ sfree (databases);
+ databases_num = 0;
+
+ return (0);
+} /* }}} int cldap_shutdown */
+
void module_register (void) /* {{{ */
{
plugin_register_complex_config ("openldap", cldap_config);
plugin_register_init ("openldap", cldap_init);
+ plugin_register_shutdown ("openldap", cldap_shutdown);
} /* }}} void module_register */
+
+#if defined(__APPLE__)
+#pragma clang diagnostic pop
+#endif