Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use dynamic sized command line buffer #431

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, it means that there was no need to further expand the command? I.e for commands like DISABLE_NOTIFICATIONS where there are no args

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, this is not external commands, but check commands... Similar point though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh, i didn't check where this is used, i just inverted the huge if clause to return early instead of having the hole function in a huge if. The functionality did not change at this point.
I should have put this in a separate commit.

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