diff options
author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2022-09-17 02:57:16 +0200 |
---|---|---|
committer | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2022-09-17 02:57:16 +0200 |
commit | 14f3305df20093bc897ccb9f4a91a572ea08e4d4 (patch) | |
tree | 9353358408348aa613ecbd4493980e9acd32fb3a /lib/utils_base.c | |
parent | 62af1d0b2a31185dc8acaf3fe8fc3149414277bd (diff) | |
download | monitoring-plugins-14f3305.tar.gz |
Use openssl message digest for state files
Diffstat (limited to 'lib/utils_base.c')
-rw-r--r-- | lib/utils_base.c | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c index 08fa215c..a78d6f8a 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
@@ -33,6 +33,8 @@ | |||
33 | #include <unistd.h> | 33 | #include <unistd.h> |
34 | #include <sys/types.h> | 34 | #include <sys/types.h> |
35 | 35 | ||
36 | #include <openssl/evp.h> | ||
37 | |||
36 | #define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } } | 38 | #define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } } |
37 | 39 | ||
38 | monitoring_plugin *this_monitoring_plugin=NULL; | 40 | monitoring_plugin *this_monitoring_plugin=NULL; |
@@ -402,25 +404,41 @@ int mp_translate_state (char *state_text) { | |||
402 | * parse of argv, so that uniqueness in parameters are reflected there. | 404 | * parse of argv, so that uniqueness in parameters are reflected there. |
403 | */ | 405 | */ |
404 | char *_np_state_generate_key() { | 406 | char *_np_state_generate_key() { |
405 | struct sha1_ctx ctx; | 407 | EVP_MD_CTX *mdctx = NULL; |
406 | int i; | ||
407 | char **argv = this_monitoring_plugin->argv; | 408 | char **argv = this_monitoring_plugin->argv; |
408 | unsigned char result[20]; | 409 | |
409 | char keyname[41]; | 410 | unsigned char *result = NULL; |
411 | unsigned int result_length = 0; | ||
412 | |||
413 | const unsigned int output_string_length = 2 * 256 + 1; | ||
414 | |||
415 | char keyname[output_string_length]; | ||
410 | char *p=NULL; | 416 | char *p=NULL; |
411 | 417 | ||
412 | sha1_init_ctx(&ctx); | 418 | if((mdctx = EVP_MD_CTX_new()) == NULL) |
419 | die(STATE_UNKNOWN, _("Failed to create EVP MD context")); | ||
420 | |||
421 | if(1 != EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) | ||
422 | die(STATE_UNKNOWN, _("Failed to initialized EVP MD context")); | ||
413 | 423 | ||
414 | for(i=0; i<this_monitoring_plugin->argc; i++) { | 424 | for(unsigned int i=0; i<this_monitoring_plugin->argc; i++) { |
415 | sha1_process_bytes(argv[i], strlen(argv[i]), &ctx); | 425 | if(1 != EVP_DigestUpdate(mdctx, argv[i], strlen(argv[i]))) |
426 | die(STATE_UNKNOWN, _("Failed to compute hash")); | ||
427 | |||
416 | } | 428 | } |
417 | 429 | ||
418 | sha1_finish_ctx(&ctx, &result); | 430 | if((result = (unsigned char *)OPENSSL_malloc(EVP_MD_size(EVP_sha256()))) == NULL) |
431 | die(STATE_UNKNOWN, _("Failed to allocate result string")); | ||
432 | |||
433 | if(1 != EVP_DigestFinal_ex(mdctx, result, &result_length)) | ||
434 | die(STATE_UNKNOWN, _("Failed to finish hashing")); | ||
435 | |||
436 | EVP_MD_CTX_free(mdctx); | ||
419 | 437 | ||
420 | for (i=0; i<20; ++i) { | 438 | for (unsigned int i=0; i<256; ++i) { |
421 | sprintf(&keyname[2*i], "%02x", result[i]); | 439 | sprintf(&keyname[2*i], "%02x", result[i]); |
422 | } | 440 | } |
423 | keyname[40]='\0'; | 441 | keyname[2 * 256]='\0'; |
424 | 442 | ||
425 | p = strdup(keyname); | 443 | p = strdup(keyname); |
426 | if(p==NULL) { | 444 | if(p==NULL) { |