[monitoring-plugins] Use default OPENSSL sha functions if available

RincewindsHat git at monitoring-plugins.org
Thu Sep 21 12:10:11 CEST 2023


 Module: monitoring-plugins
 Branch: master
 Commit: 7c98e2b345b91d8ef3fb1f7a1bcf74194d54c966
 Author: RincewindsHat <12514511+RincewindsHat at users.noreply.github.com>
   Date: Sun Mar 12 12:14:41 2023 +0100
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=7c98e2b

Use default OPENSSL sha functions if available

---

 lib/utils_base.c | 27 +++++++++++++++++++--------
 lib/utils_base.h |  4 +++-
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/lib/utils_base.c b/lib/utils_base.c
index eb1823b..39032cb 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -402,26 +402,37 @@ int mp_translate_state (char *state_text) {
  * parse of argv, so that uniqueness in parameters are reflected there.
  */
 char *_np_state_generate_key() {
-	struct sha256_ctx ctx;
 	int i;
 	char **argv = this_monitoring_plugin->argv;
 	unsigned char result[20];
 	char keyname[41];
 	char *p=NULL;
+#ifdef USE_OPENSSL
+	/*
+	 * This code path is chosen if openssl is available (which should be the most common
+	 * scenario). Alternatively, the gnulib implementation/
+	 *
+	 */
+	EVP_MD_CTX *ctx = EVP_MD_CTX_new();
+
+	EVP_DigestInit(ctx, EVP_sha256());
+
+	for(i=0; i<this_monitoring_plugin->argc; i++) {
+		EVP_DigestUpdate(ctx, argv[i], strlen(argv[i]));
+	}
+
+	EVP_DigestFinalXOF(ctx, &result, 20);
+#else
+	struct sha256_ctx ctx;
 
-	sha256_init_ctx(&ctx);
-	
 	for(i=0; i<this_monitoring_plugin->argc; i++) {
 		sha256_process_bytes(argv[i], strlen(argv[i]), &ctx);
 	}
 
 	sha256_finish_ctx(&ctx, &result);
-	
-	for (i=0; i<20; ++i) {
-		sprintf(&keyname[2*i], "%02x", result[i]);
-	}
+#endif // FOUNDOPENSSL
 	keyname[40]='\0';
-	
+
 	p = strdup(keyname);
 	if(p==NULL) {
 		die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
diff --git a/lib/utils_base.h b/lib/utils_base.h
index 5906550..9cb4276 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -2,7 +2,9 @@
 #define _UTILS_BASE_
 /* Header file for Monitoring Plugins utils_base.c */
 
-#include "sha256.h"
+#ifndef USE_OPENSSL
+# include "sha256.h"
+#endif
 
 /* This file holds header information for thresholds - use this in preference to 
    individual plugin logic */



More information about the Commits mailing list