X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fopenldap.c;h=615aee338b28b70cba4cab0260c09d3ba7c1a847;hb=354f9991530248e45207d236eb74c1cc3d5238ef;hp=bd79b26b7312bf076c95a247f9d3331688e5f550;hpb=f7f5868ee0378791ccd8d1788e27ebf2a02737dc;p=collectd.git diff --git a/src/openldap.c b/src/openldap.c index bd79b26b..615aee33 100644 --- a/src/openldap.c +++ b/src/openldap.c @@ -1,7 +1,7 @@ /** * 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"), @@ -43,6 +43,8 @@ struct cldap_s /* {{{ */ { char *name; + char *binddn; + char *password; char *cacert; char *host; int state; @@ -56,11 +58,16 @@ struct cldap_s /* {{{ */ }; 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); @@ -75,6 +82,13 @@ static int cldap_init_host (cldap_t *st) /* {{{ */ { 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) { @@ -92,6 +106,8 @@ static int cldap_init_host (cldap_t *st) /* {{{ */ 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); @@ -115,10 +131,19 @@ static int cldap_init_host (cldap_t *st) /* {{{ */ } 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", @@ -147,13 +172,9 @@ static void cldap_submit_value (const char *type, const char *type_instance, /* 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) @@ -223,6 +244,7 @@ static int cldap_read_host (user_data_t *ud) /* {{{ */ 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); } @@ -520,7 +542,6 @@ static int cldap_read_host (user_data_t *ud) /* {{{ */ } ldap_msgfree (result); - ldap_unbind_ext_s (st->ld, NULL, NULL); return (0); } /* }}} int cldap_read_host */ @@ -540,13 +561,12 @@ static int cldap_config_add (oconfig_item_t *ci) /* {{{ */ 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) @@ -556,7 +576,7 @@ static int cldap_config_add (oconfig_item_t *ci) /* {{{ */ } st->starttls = 0; - st->timeout = -1; + st->timeout = (long) (CDTIME_T_TO_MS(plugin_get_interval()) / 1000); st->verifyhost = 1; st->version = LDAP_VERSION3; @@ -564,7 +584,11 @@ static int cldap_config_add (oconfig_item_t *ci) /* {{{ */ { 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); @@ -611,32 +635,47 @@ static int cldap_config_add (oconfig_item_t *ci) /* {{{ */ } 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) @@ -681,10 +720,24 @@ static int cldap_init (void) /* {{{ */ 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__)