summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Weiss <holger@zedat.fu-berlin.de>2013-08-27 12:37:13 (GMT)
committerHolger Weiss <holger@zedat.fu-berlin.de>2013-08-27 12:37:13 (GMT)
commite9ede0f8e3b5a5402722ae8d10862f29d30c73ae (patch)
treea76d036ad6fd103941ffd6cb387bc0e7301ba588
parent69b13552864cb6df639cceb94b8d09b1f9af8f17 (diff)
parenta20611d4357c3c3ebe0a62776a1642e7904c1410 (diff)
downloadmonitoring-plugins-e9ede0f8e3b5a5402722ae8d10862f29d30c73ae.tar.gz
Merge branch 'master' of https://github.com/ozamosi/nagios-plugins
* 'master' of https://github.com/ozamosi/nagios-plugins: check_snmp: Close potential for using uninitialized memory check_snmp: Dynamically grow all data structures Conflicts: plugins/check_snmp.c
-rw-r--r--plugins/check_snmp.c82
-rw-r--r--plugins/common.h6
2 files changed, 64 insertions, 24 deletions
diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index d2f2f8b..9ca845d 100644
--- a/plugins/check_snmp.c
+++ b/plugins/check_snmp.c
@@ -57,7 +57,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
57#define WARN_STRING 16 57#define WARN_STRING 16
58#define WARN_REGEX 32 58#define WARN_REGEX 32
59 59
60#define MAX_OIDS 8 60#define OID_COUNT_STEP 8
61 61
62/* Longopts only arguments */ 62/* Longopts only arguments */
63#define L_CALCULATE_RATE CHAR_MAX+1 63#define L_CALCULATE_RATE CHAR_MAX+1
@@ -112,6 +112,7 @@ char *privproto = NULL;
112char *authpasswd = NULL; 112char *authpasswd = NULL;
113char *privpasswd = NULL; 113char *privpasswd = NULL;
114char **oids = NULL; 114char **oids = NULL;
115size_t oids_size = NULL;
115char *label; 116char *label;
116char *units; 117char *units;
117char *port; 118char *port;
@@ -121,19 +122,22 @@ int invert_search=0;
121char **labels = NULL; 122char **labels = NULL;
122char **unitv = NULL; 123char **unitv = NULL;
123size_t nlabels = 0; 124size_t nlabels = 0;
124size_t labels_size = 8; 125size_t labels_size = OID_COUNT_STEP;
125size_t nunits = 0; 126size_t nunits = 0;
126size_t unitv_size = 8; 127size_t unitv_size = OID_COUNT_STEP;
127int numoids = 0; 128int numoids = 0;
128int numauthpriv = 0; 129int numauthpriv = 0;
129int verbose = 0; 130int verbose = 0;
130int usesnmpgetnext = FALSE; 131int usesnmpgetnext = FALSE;
131char *warning_thresholds = NULL; 132char *warning_thresholds = NULL;
132char *critical_thresholds = NULL; 133char *critical_thresholds = NULL;
133thresholds *thlds[MAX_OIDS]; 134thresholds **thlds;
134double response_value[MAX_OIDS]; 135size_t thlds_size = OID_COUNT_STEP;
136double *response_value;
137size_t response_size = OID_COUNT_STEP;
135int retries = 0; 138int retries = 0;
136int eval_method[MAX_OIDS]; 139int *eval_method;
140size_t eval_size = OID_COUNT_STEP;
137char *delimiter; 141char *delimiter;
138char *output_delim; 142char *output_delim;
139char *miblist = NULL; 143char *miblist = NULL;
@@ -142,7 +146,8 @@ int calculate_rate = 0;
142double offset = 0.0; 146double offset = 0.0;
143int rate_multiplier = 1; 147int rate_multiplier = 1;
144state_data *previous_state; 148state_data *previous_state;
145double previous_value[MAX_OIDS]; 149double *previous_value;
150size_t previous_size = OID_COUNT_STEP;
146int perf_labels = 1; 151int perf_labels = 1;
147 152
148 153
@@ -206,8 +211,11 @@ main (int argc, char **argv)
206 211
207 labels = malloc (labels_size * sizeof(*labels)); 212 labels = malloc (labels_size * sizeof(*labels));
208 unitv = malloc (unitv_size * sizeof(*unitv)); 213 unitv = malloc (unitv_size * sizeof(*unitv));
209 for (i = 0; i < MAX_OIDS; i++) 214 thlds = malloc (thlds_size * sizeof(*thlds));
210 eval_method[i] = CHECK_UNDEF; 215 response_value = malloc (response_size * sizeof(*response_value));
216 previous_value = malloc (previous_size * sizeof(*previous_value));
217 eval_method = calloc (eval_size, sizeof(*eval_method));
218 oids = calloc(oids_size, sizeof (char *));
211 219
212 label = strdup ("SNMP"); 220 label = strdup ("SNMP");
213 units = strdup (""); 221 units = strdup ("");
@@ -225,13 +233,14 @@ main (int argc, char **argv)
225 233
226 np_set_args(argc, argv); 234 np_set_args(argc, argv);
227 235
236 time(&current_time);
237
228 if (process_arguments (argc, argv) == ERROR) 238 if (process_arguments (argc, argv) == ERROR)
229 usage4 (_("Could not parse arguments")); 239 usage4 (_("Could not parse arguments"));
230 240
231 if(calculate_rate) { 241 if(calculate_rate) {
232 if (!strcmp(label, "SNMP")) 242 if (!strcmp(label, "SNMP"))
233 label = strdup("SNMP RATE"); 243 label = strdup("SNMP RATE");
234 time(&current_time);
235 i=0; 244 i=0;
236 previous_state = np_state_read(); 245 previous_state = np_state_read();
237 if(previous_state!=NULL) { 246 if(previous_state!=NULL) {
@@ -240,6 +249,10 @@ main (int argc, char **argv)
240 while((ap = strsep(&previous_string, ":")) != NULL) { 249 while((ap = strsep(&previous_string, ":")) != NULL) {
241 if(verbose>2) 250 if(verbose>2)
242 printf("State for %d=%s\n", i, ap); 251 printf("State for %d=%s\n", i, ap);
252 while (i >= previous_size) {
253 previous_size += OID_COUNT_STEP;
254 previous_value = realloc(previous_value, previous_size * sizeof(*previous_value));
255 }
243 previous_value[i++]=strtod(ap,NULL); 256 previous_value[i++]=strtod(ap,NULL);
244 } 257 }
245 } 258 }
@@ -255,6 +268,11 @@ main (int argc, char **argv)
255 w = w ? fix_snmp_range(w) : NULL; 268 w = w ? fix_snmp_range(w) : NULL;
256 c = c ? fix_snmp_range(c) : NULL; 269 c = c ? fix_snmp_range(c) : NULL;
257 270
271 while (i >= thlds_size) {
272 thlds_size += OID_COUNT_STEP;
273 thlds = realloc(thlds, thlds_size * sizeof(*thlds));
274 }
275
258 /* Skip empty thresholds, while avoiding segfault */ 276 /* Skip empty thresholds, while avoiding segfault */
259 set_thresholds(&thlds[i], 277 set_thresholds(&thlds[i],
260 w ? strpbrk(w, NP_THRESHOLDS_CHARS) : NULL, 278 w ? strpbrk(w, NP_THRESHOLDS_CHARS) : NULL,
@@ -434,6 +452,10 @@ main (int argc, char **argv)
434 ptr = strpbrk (show, "0123456789"); 452 ptr = strpbrk (show, "0123456789");
435 if (ptr == NULL) 453 if (ptr == NULL)
436 die (STATE_UNKNOWN,_("No valid data returned (%s)\n"), show); 454 die (STATE_UNKNOWN,_("No valid data returned (%s)\n"), show);
455 while (i >= response_size) {
456 response_size += OID_COUNT_STEP;
457 response_value = realloc(response_value, response_size * sizeof(*response_value));
458 }
437 response_value[i] = strtod (ptr, NULL) + offset; 459 response_value[i] = strtod (ptr, NULL) + offset;
438 460
439 if(calculate_rate) { 461 if(calculate_rate) {
@@ -461,7 +483,7 @@ main (int argc, char **argv)
461 } 483 }
462 484
463 /* Process this block for string matching */ 485 /* Process this block for string matching */
464 else if (eval_method[i] & CRIT_STRING) { 486 else if (eval_size > i && eval_method[i] & CRIT_STRING) {
465 if (strcmp (show, string_value)) 487 if (strcmp (show, string_value))
466 iresult = (invert_search==0) ? STATE_CRITICAL : STATE_OK; 488 iresult = (invert_search==0) ? STATE_CRITICAL : STATE_OK;
467 else 489 else
@@ -469,7 +491,7 @@ main (int argc, char **argv)
469 } 491 }
470 492
471 /* Process this block for regex matching */ 493 /* Process this block for regex matching */
472 else if (eval_method[i] & CRIT_REGEX) { 494 else if (eval_size > i && eval_method[i] & CRIT_REGEX) {
473 excode = regexec (&preg, response, 10, pmatch, eflags); 495 excode = regexec (&preg, response, 10, pmatch, eflags);
474 if (excode == 0) { 496 if (excode == 0) {
475 iresult = (invert_search==0) ? STATE_OK : STATE_CRITICAL; 497 iresult = (invert_search==0) ? STATE_OK : STATE_CRITICAL;
@@ -487,9 +509,9 @@ main (int argc, char **argv)
487 /* Process this block for existence-nonexistence checks */ 509 /* Process this block for existence-nonexistence checks */
488 /* TV: Should this be outside of this else block? */ 510 /* TV: Should this be outside of this else block? */
489 else { 511 else {
490 if (eval_method[i] & CRIT_PRESENT) 512 if (eval_size > i && eval_method[i] & CRIT_PRESENT)
491 iresult = STATE_CRITICAL; 513 iresult = STATE_CRITICAL;
492 else if (eval_method[i] & WARN_PRESENT) 514 else if (eval_size > i && eval_method[i] & WARN_PRESENT)
493 iresult = STATE_WARNING; 515 iresult = STATE_WARNING;
494 else if (response && iresult == STATE_DEPENDENT) 516 else if (response && iresult == STATE_DEPENDENT)
495 iresult = STATE_OK; 517 iresult = STATE_OK;
@@ -729,23 +751,36 @@ process_arguments (int argc, char **argv)
729 */ 751 */
730 needmibs = TRUE; 752 needmibs = TRUE;
731 } 753 }
732 if (!oids) oids = calloc(MAX_OIDS, sizeof (char *)); 754 for (ptr = strtok(optarg, ", "); ptr != NULL; ptr = strtok(NULL, ", "), j++) {
733 for (ptr = strtok(optarg, ", "); ptr != NULL && j < MAX_OIDS; ptr = strtok(NULL, ", "), j++) { 755 while (j >= oids_size) {
756 oids_size += OID_COUNT_STEP;
757 oids = realloc(oids, oids_size * sizeof (*oids));
758 }
734 oids[j] = strdup(ptr); 759 oids[j] = strdup(ptr);
735 } 760 }
736 numoids = j; 761 numoids = j;
737 if (c == 'E' || c == 'e') { 762 if (c == 'E' || c == 'e') {
738 jj++; 763 jj++;
739 ii++; 764 ii++;
765 while (j+1 >= eval_size) {
766 eval_size += OID_COUNT_STEP;
767 eval_method = realloc(eval_method, eval_size * sizeof(*eval_method));
768 memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8);
769 }
770 if (c == 'E')
771 eval_method[j+1] |= WARN_PRESENT;
772 else if (c == 'e')
773 eval_method[j+1] |= CRIT_PRESENT;
740 } 774 }
741 if (c == 'E')
742 eval_method[j+1] |= WARN_PRESENT;
743 else if (c == 'e')
744 eval_method[j+1] |= CRIT_PRESENT;
745 break; 775 break;
746 case 's': /* string or substring */ 776 case 's': /* string or substring */
747 strncpy (string_value, optarg, sizeof (string_value) - 1); 777 strncpy (string_value, optarg, sizeof (string_value) - 1);
748 string_value[sizeof (string_value) - 1] = 0; 778 string_value[sizeof (string_value) - 1] = 0;
779 while (jj >= eval_size) {
780 eval_size += OID_COUNT_STEP;
781 eval_method = realloc(eval_method, eval_size * sizeof(*eval_method));
782 memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8);
783 }
749 eval_method[jj++] = CRIT_STRING; 784 eval_method[jj++] = CRIT_STRING;
750 ii++; 785 ii++;
751 break; 786 break;
@@ -761,6 +796,11 @@ process_arguments (int argc, char **argv)
761 printf (_("Could Not Compile Regular Expression")); 796 printf (_("Could Not Compile Regular Expression"));
762 return ERROR; 797 return ERROR;
763 } 798 }
799 while (jj >= eval_size) {
800 eval_size += OID_COUNT_STEP;
801 eval_method = realloc(eval_method, eval_size * sizeof(*eval_method));
802 memset(eval_method + eval_size - OID_COUNT_STEP, 0, 8);
803 }
764 eval_method[jj++] = CRIT_REGEX; 804 eval_method[jj++] = CRIT_REGEX;
765 ii++; 805 ii++;
766 break; 806 break;
@@ -1127,7 +1167,7 @@ print_help (void)
1127 printf ("\n"); 1167 printf ("\n");
1128 printf ("%s\n", _("Notes:")); 1168 printf ("%s\n", _("Notes:"));
1129 printf (" %s\n", _("- Multiple OIDs (and labels) may be indicated by a comma or space-delimited ")); 1169 printf (" %s\n", _("- Multiple OIDs (and labels) may be indicated by a comma or space-delimited "));
1130 printf (" %s %i %s\n", _("list (lists with internal spaces must be quoted). Maximum:"), MAX_OIDS, _("OIDs.")); 1170 printf (" %s %i %s\n", _("list (lists with internal spaces must be quoted)."));
1131 1171
1132 printf(" -%s", UT_THRESHOLDS_NOTES); 1172 printf(" -%s", UT_THRESHOLDS_NOTES);
1133 1173
diff --git a/plugins/common.h b/plugins/common.h
index f135838..b49ad94 100644
--- a/plugins/common.h
+++ b/plugins/common.h
@@ -208,9 +208,9 @@ enum {
208# define bindtextdomain(Domainname, Dirname) /* empty */ 208# define bindtextdomain(Domainname, Dirname) /* empty */
209#endif 209#endif
210 210
211/* For non-GNU compilers to ignore __attribute__ */ 211/* For non-GNU/non-clang compilers to ignore __attribute__ */
212#ifndef __GNUC__ 212#if !defined(__GNUC__) && !defined(__CLANG__)
213# define __attribute__(x) /* do nothing */ 213# define __attribute__(noreturn) /* do nothing */
214#endif 214#endif
215 215
216#endif /* _COMMON_H_ */ 216#endif /* _COMMON_H_ */