[Nagios-devel] [PATCH 1/2] Macros: Make each macro tell which

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
Guest

[Nagios-devel] [PATCH 1/2] Macros: Make each macro tell which

Post by Guest »

From: Max Sikström

Not all macros should appearently accept command line escaping, but just a few
related to plugin output, and user input.

This commit builds that table again, which was available earlier in another
form. But this time, it isn't used to tell how to escape a macro, but just
which kinds of escaping that should be possible.

Signed-off-by: Max Sikström
---
common/macros.c | 31 ++++++++++++++++++++++++++++---
1 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/common/macros.c b/common/macros.c
index fe3b4d2..fa22233 100644
--- a/common/macros.c
+++ b/common/macros.c
@@ -39,6 +39,7 @@ struct macro_key_code {
char *name; /* macro key name */
int code; /* numeric macro code, usable in case statements */
char *value;
+ int options; /* Options for how the macro can be escaped. Not all macros should be able to be stripped */
};

static struct macro_key_code macro_keys[MACRO_X_COUNT];
@@ -110,6 +111,7 @@ int process_macros_r(nagios_macros *mac, char *input_buffer, char **output_buffe
char *original_macro = NULL;
int result = OK;
int free_macro = FALSE;
+ int macro_options = 0; /* Meta information about macro (how it can be escaped) */

log_debug_info(DEBUGL_FUNCTIONS, 0, "process_macros_r()\n");

@@ -163,7 +165,7 @@ int process_macros_r(nagios_macros *mac, char *input_buffer, char **output_buffe

/* grab the macro value */
free_macro = FALSE;
- result = grab_macro_value_r(mac, temp_buffer, &selected_macro, NULL, &free_macro);
+ result = grab_macro_value_r(mac, temp_buffer, &selected_macro, &macro_options, &free_macro);
log_debug_info(DEBUGL_MACROS, 2, " Processed '%s', Free: %d\n", temp_buffer, free_macro);

/* an error occurred - we couldn't parse the macro, so continue on */
@@ -198,7 +200,7 @@ int process_macros_r(nagios_macros *mac, char *input_buffer, char **output_buffe
log_debug_info(DEBUGL_MACROS, 2, " Processed '%s', Free: %d, Cleaning options: %d\n", temp_buffer, free_macro, options);

/* URL encode the macro if requested - this allocates new memory */
- if(options & URL_ENCODE_MACRO_CHARS) {
+ if((options & macro_options) & URL_ENCODE_MACRO_CHARS) {
original_macro = selected_macro;
selected_macro = get_url_encoded_string(selected_macro);
if(free_macro == TRUE) {
@@ -208,7 +210,7 @@ int process_macros_r(nagios_macros *mac, char *input_buffer, char **output_buffe
}

/* some macros are cleaned... */
- if((options & STRIP_ILLEGAL_MACRO_CHARS) || (options & ESCAPE_MACRO_CHARS)) {
+ if(((options & macro_options) & STRIP_ILLEGAL_MACRO_CHARS) || ((options & macro_options) & ESCAPE_MACRO_CHARS)) {
char *cleaned_macro = NULL;

/* add the (cleaned) processed macro to the end of the already processed buffer */
@@ -405,6 +407,8 @@ int grab_macro_value_r(nagios_macros *mac, char *macro_buffer, char **output, in
if(macro_buffer == NULL || free_macro == NULL)
return ERROR;

+ if(clean_options)
+ *clean_options = 0;

/*
* We handle argv and user macros first, since those are by far
@@ -480,6 +484,12 @@ int grab_macro_value_r(nagios_macros *mac, char *macro_buffer, char **output, in

/* get the macro value */
result = grab_macrox_value_r(mac, mkey->code, arg[0], arg[1], output, free_macro);
+
+ /* Return the macro attributes */
+
+ if(clean_options) {
+ *clean_options = mkey->options;
+ }
}
/***** CONTACT ADDRESS MACROS *****/
/* NOTE: the code below should be broken out into a separate function */
@@ -2494,6 +2504,21 @@ int init_macros(void) {
for (x = 0; x < MACRO_X_COUNT; x++) {
macro_keys[x].code = x;
macro_keys[x].name = macro_x_names[x];
+
+ /* This tells which escaping is possible to do on the macro */
+ macro_keys[x].options = URL_ENCODE_MACRO_CHARS;
+ switch(x) {
+ case MACRO_HOSTOUTPUT:
+ case MACRO_HOSTPERFDATA:
+ case MACRO_HOSTACKAUTHOR:
+ case MACRO_HOSTACKCOMMENT:
+ case MACRO_SERVICEOUTPUT:
+ case MACRO_SERVICEPERFDATA:
+ case MACRO_SERVICEACKAUTHOR:
+ case MACRO_SERVICEACKCOMMENT:
+ macro_keys[x].options |= STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS;
+ break;
+ }
}

qsort(macro_keys, x, sizeof(struct m

...[email truncated]...


This post was automatically imported from historical nagios-devel mailing list archives
Original poster: msikstrom@op5.com
Locked