From b5c28d2d370fcbedc1e20d0838d53b157aedb6a8 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Wed, 28 Jun 2023 11:29:30 +0200 Subject: [PATCH] use dynamic sized command line buffer Previously we used a fixed size 8k buffer when parsing command line arguments. This sounds much, but there are command lines bigger than 8k and they are simply cut off without any warning. Instead we use a dynamic sized buffer with the size of the raw command now. --- src/naemon/common.h | 2 -- src/naemon/macros.c | 72 ++++++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/naemon/common.h b/src/naemon/common.h index 26e8ed07..98df18c3 100644 --- a/src/naemon/common.h +++ b/src/naemon/common.h @@ -471,8 +471,6 @@ NAGIOS_END_DECL #define MAX_FILENAME_LENGTH 256 /* max length of path/filename that Nagios will process */ #define MAX_INPUT_BUFFER 1024 /* size in bytes of max. input buffer (for reading files, misc stuff) */ -#define MAX_COMMAND_BUFFER 8192 /* max length of raw or processed command line */ - #define MAX_DATETIME_LENGTH 48 diff --git a/src/naemon/macros.c b/src/naemon/macros.c index 66422304..c0f460c8 100644 --- a/src/naemon/macros.c +++ b/src/naemon/macros.c @@ -121,8 +121,9 @@ static int grab_custom_object_macro_r(nagios_macros *mac, char *macro_name, cust /* given a "raw" command, return the "expanded" or "whole" command line */ int get_raw_command_line_r(nagios_macros *mac, command *cmd_ptr, char *cmd, char **full_command, int macro_options) { - char temp_arg[MAX_COMMAND_BUFFER] = ""; + char *temp_arg = NULL; char *arg_buffer = NULL; + size_t cmd_len = 0; register int x = 0; register int y = 0; register int arg_index = 0; @@ -139,51 +140,56 @@ int get_raw_command_line_r(nagios_macros *mac, command *cmd_ptr, char *cmd, char /* get the full command line */ *full_command = nm_strdup((cmd_ptr->command_line == NULL) ? "" : cmd_ptr->command_line); - /* XXX: Crazy indent */ - /* get the command arguments */ - if (cmd != NULL) { + if (cmd == NULL) { + log_debug_info(DEBUGL_COMMANDS | DEBUGL_CHECKS | DEBUGL_MACROS, 2, "Expanded Command Output: %s\n", *full_command); + return OK; + } - /* skip the command name (we're about to get the arguments)... */ - for (arg_index = 0;; arg_index++) { - if (cmd[arg_index] == '!' || cmd[arg_index] == '\x0') - break; - } + cmd_len = strlen(cmd); + temp_arg = nm_malloc(cmd_len); - /* get each command argument */ - for (x = 0; x < MAX_COMMAND_ARGUMENTS; x++) { + /* get the command arguments */ + /* skip the command name (we're about to get the arguments)... */ + for (arg_index = 0;; arg_index++) { + if (cmd[arg_index] == '!' || cmd[arg_index] == '\x0') + break; + } - /* we reached the end of the arguments... */ - if (cmd[arg_index] == '\x0') - break; + /* get each command argument */ + for (x = 0; x < MAX_COMMAND_ARGUMENTS; x++) { - /* get the next argument */ - /* can't use strtok(), as that's used in process_macros... */ - for (arg_index++, y = 0; y < (int)sizeof(temp_arg) - 1; arg_index++) { + /* we reached the end of the arguments... */ + if (cmd[arg_index] == '\x0') + break; - /* handle escaped argument delimiters */ - if (cmd[arg_index] == '\\' && cmd[arg_index + 1] == '!') { - arg_index++; - } else if (cmd[arg_index] == '!' || cmd[arg_index] == '\x0') { - /* end of argument */ - break; - } + /* get the next argument */ + /* can't use strtok(), as that's used in process_macros... */ + for (arg_index++, y = 0; y < (int)cmd_len - 1; arg_index++) { - /* copy the character */ - temp_arg[y] = cmd[arg_index]; - y++; + /* handle escaped argument delimiters */ + if (cmd[arg_index] == '\\' && cmd[arg_index + 1] == '!') { + arg_index++; + } else if (cmd[arg_index] == '!' || cmd[arg_index] == '\x0') { + /* end of argument */ + break; } - temp_arg[y] = '\x0'; - - /* ADDED 01/29/04 EG */ - /* process any macros we find in the argument */ - process_macros_r(mac, temp_arg, &arg_buffer, macro_options); - mac->argv[x] = arg_buffer; + /* copy the character */ + temp_arg[y] = cmd[arg_index]; + y++; } + temp_arg[y] = '\x0'; + + /* ADDED 01/29/04 EG */ + /* process any macros we find in the argument */ + process_macros_r(mac, temp_arg, &arg_buffer, macro_options); + + mac->argv[x] = arg_buffer; } log_debug_info(DEBUGL_COMMANDS | DEBUGL_CHECKS | DEBUGL_MACROS, 2, "Expanded Command Output: %s\n", *full_command); + nm_free(temp_arg); return OK; }