From f1250933e4a2ac09a3d0b25b3877068e12f44da5 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Fri, 9 Apr 2010 15:57:20 -0700 Subject: Fix command auditing to work right The command auditing patch had a number of issues: * was looking at shell_input_line rather than what user entered * reopened audit file descriptor on each command * left audit_fd dangling in child * looked up tty on each command It still does getcwd() on each command but that probably can't be helped. --- eval.c | 69 +++++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) (limited to 'eval.c') diff --git a/eval.c b/eval.c index a9937a8..8342392 100644 --- a/eval.c +++ b/eval.c @@ -46,6 +46,7 @@ #endif #if defined (AUDIT_SHELL) +# include "filecntl.h" # include # include #endif @@ -69,42 +70,49 @@ extern int current_readline_line_index; #if defined (AUDIT_SHELL) static int audit_fd = -1; +static char *audit_tty; static int audit_start () { - audit_fd = audit_open (); if (audit_fd < 0) - return -1; - else - return 0; + { + audit_fd = audit_open (); + if (audit_fd < 0) + { + if (errno != EINVAL && errno != EPROTONOSUPPORT + && errno != EAFNOSUPPORT) + return -1; + } + else + SET_CLOSE_ON_EXEC(audit_fd); + } + + if (audit_tty == NULL) + { + char *tty = ttyname(fileno(stdin)); + if (tty) + audit_tty = strdup(tty); + } + + return 0; } -static int -audit (cmd, result) - char *cmd; +static void +audit (result) int result; { - int rc; - - if (audit_fd < 0) - return 0; - - rc = audit_log_user_command (audit_fd, AUDIT_USER_CMD, cmd, - NULL, !result); - close (audit_fd); - audit_fd = -1; - return rc; + audit_log_user_command (audit_fd, AUDIT_USER_CMD, current_readline_line, + audit_tty, result == EXECUTION_SUCCESS); } #endif - /* Read and execute commands until EOF is reached. This assumes that the input source has already been initialized. */ int reader_loop () { - int our_indirection_level; + int our_indirection_level, result; COMMAND * volatile current_command; current_command = (COMMAND *)NULL; @@ -112,6 +120,11 @@ reader_loop () our_indirection_level = ++indirection_level; +#if defined (AUDIT_SHELL) + if (audited && interactive_shell && audit_start () < 0) + return EXECUTION_FAILURE; +#endif + while (EOF_Reached == 0) { int code; @@ -186,24 +199,10 @@ reader_loop () executing = 1; stdin_redir = 0; -#if defined (AUDIT_SHELL) - if (audited && interactive_shell) - { - if (audit_start () < 0) - { - if (errno != EINVAL && errno != EPROTONOSUPPORT && - errno != EAFNOSUPPORT) - return EXECUTION_FAILURE; - } - } -#endif - execute_command (current_command); + result = execute_command (current_command); #if defined (AUDIT_SHELL) - { - extern char *shell_input_line; - audit (shell_input_line, last_command_exit_value); - } + audit (result); #endif exec_done: -- cgit v1.2.3