[PATCH 1/2] Macros: Make each macro tell which escaping capabilities it accepts.

Max Sikstrom max.sikstrom at op5.com
Mon Aug 5 14:12:30 CEST 2013


From: Max Sikström <msikstrom at op5.com>

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 <msikstrom at op5.com>
---
 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 macro_key_code), macro_key_cmp);
-- 
1.7.1


------------------------------------------------------------------------------
Get your SQL database under version control now!
Version control is standard for application code, but databases havent 
caught up. So what steps can you take to put your SQL databases under 
version control? Why should you start doing it? Read more to find out.
http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list