Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git] / src / openldap.c
index 0c88f8c..d424cb4 100644 (file)
@@ -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"),
  **/
 
 #include "collectd.h"
+
 #include "common.h"
 #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>
@@ -137,7 +142,7 @@ static int cldap_init_host (cldap_t *st) /* {{{ */
                cred.bv_len = 0;
        }
 
-       rc = ldap_sasl_bind_s (st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred, 
+       rc = ldap_sasl_bind_s (st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred,
                        NULL, NULL, NULL);
        if (rc != LDAP_SUCCESS)
        {
@@ -167,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)
@@ -207,7 +208,7 @@ static void cldap_submit_gauge (const char *type, const char *type_instance, /*
 static int cldap_read_host (user_data_t *ud) /* {{{ */
 {
        cldap_t *st;
-       LDAPMessage *e, *result;
+       LDAPMessage *result;
        char *dn;
        int rc;
        int status;
@@ -248,7 +249,7 @@ static int cldap_read_host (user_data_t *ud) /* {{{ */
                return (-1);
        }
 
-       for (e = ldap_first_entry (st->ld, result); e != NULL;
+       for (LDAPMessage *e = ldap_first_entry (st->ld, result); e != NULL;
                e = ldap_next_entry (st->ld, e))
        {
                if ((dn = ldap_get_dn (st->ld, e)) != NULL)
@@ -557,16 +558,14 @@ static int cldap_read_host (user_data_t *ud) /* {{{ */
 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
 {
        cldap_t *st;
-       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)
@@ -580,7 +579,7 @@ static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
        st->verifyhost = 1;
        st->version = LDAP_VERSION3;
 
-       for (i = 0; i < ci->children_num; i++)
+       for (int i = 0; i < ci->children_num; i++)
        {
                oconfig_item_t *child = ci->children + i;
 
@@ -624,9 +623,8 @@ static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
        if ((status == 0) && (st->url != NULL))
        {
                LDAPURLDesc *ludpp;
-               int rc;
 
-               if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
+               if (ldap_url_parse (st->url, &ludpp) != 0)
                {
                        ERROR ("openldap plugin: Instance `%s': "
                                "Invalid URL: `%s'",
@@ -635,9 +633,7 @@ 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);
        }
@@ -656,21 +652,20 @@ static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
                }
                else
                {
-                       user_data_t ud;
-                       char callback_name[3*DATA_MAX_NAME_LEN];
+                       char callback_name[3*DATA_MAX_NAME_LEN] = { 0 };
 
                        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"),
+                                       (st->name != NULL) ? st->name : "default");
+
+                       user_data_t ud = {
+                               .data = st
+                       };
 
                        status = plugin_register_complex_read (/* group = */ NULL,
                                        /* name      = */ callback_name,
@@ -691,10 +686,9 @@ static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
 
 static int cldap_config (oconfig_item_t *ci) /* {{{ */
 {
-       int i;
        int status = 0;
 
-       for (i = 0; i < ci->children_num; i++)
+       for (int i = 0; i < ci->children_num; i++)
        {
                oconfig_item_t *child = ci->children + i;
 
@@ -724,9 +718,7 @@ static int cldap_init (void) /* {{{ */
 
 static int cldap_shutdown (void) /* {{{ */
 {
-       size_t i;
-
-       for (i = 0; i < databases_num; i++)
+       for (size_t i = 0; i < databases_num; i++)
                if (databases[i]->ld != NULL)
                        ldap_unbind_ext_s (databases[i]->ld, NULL, NULL);
        sfree (databases);
@@ -741,3 +733,7 @@ void module_register (void) /* {{{ */
        plugin_register_init ("openldap", cldap_init);
        plugin_register_shutdown ("openldap", cldap_shutdown);
 } /* }}} void module_register */
+
+#if defined(__APPLE__)
+#pragma clang diagnostic pop
+#endif