Skip to content

Commit

Permalink
use dynamic sized command line buffer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sni committed Jul 6, 2023
1 parent 12de0b0 commit b5c28d2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
2 changes: 0 additions & 2 deletions src/naemon/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
72 changes: 39 additions & 33 deletions src/naemon/macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit b5c28d2

Please sign in to comment.