Move exec code to call into I/O log plugin to exec_iolog.c.

This will be shared with exec_nopty.c in the future to log
stdin/stdout/stderr without running the command in a pty.
Both exec_pty.c and exec_nopty.c now use the same closure.
This commit is contained in:
Todd C. Miller
2022-09-27 13:35:45 -06:00
parent 3ca9220e50
commit 803b4939be
9 changed files with 827 additions and 765 deletions

View File

@@ -26,6 +26,13 @@
# define MSG_WAITALL 0
#endif
/*
* Linux-specific wait flag used with ptrace(2).
*/
#ifndef __WALL
# define __WALL 0
#endif
/*
* Some older systems support siginfo but predate SI_USER.
*/
@@ -35,8 +42,66 @@
# define USER_SIGNALED(_info) ((_info) != NULL && (_info)->si_code <= 0)
#endif
/* Values for ttymode. */
#define TERM_COOKED 0
#define TERM_RAW 1
struct command_details;
struct command_status;
struct sudo_event_base;
struct stat;
/*
* Indices into io_fds[] when running a command in a pty.
* Closure passed to I/O event callbacks.
*/
struct exec_closure {
struct command_details *details;
struct sudo_event_base *evbase;
struct sudo_event *backchannel_event;
struct sudo_event *fwdchannel_event;
struct sudo_event *sigint_event;
struct sudo_event *sigquit_event;
struct sudo_event *sigtstp_event;
struct sudo_event *sigterm_event;
struct sudo_event *sighup_event;
struct sudo_event *sigalrm_event;
struct sudo_event *sigpipe_event;
struct sudo_event *sigusr1_event;
struct sudo_event *sigusr2_event;
struct sudo_event *sigchld_event;
struct sudo_event *sigcont_event;
struct sudo_event *siginfo_event;
struct sudo_event *sigwinch_event;
struct command_status *cstat;
void *intercept;
pid_t monitor_pid;
pid_t cmnd_pid;
pid_t ppgrp;
short rows;
short cols;
};
/*
* I/O buffer with associated read/write events and a logging action.
* Used to, e.g. pass data from the pty to the user's terminal
* and any I/O logging plugins.
*/
struct io_buffer;
typedef bool (*sudo_io_action_t)(const char *, unsigned int, struct io_buffer *);
struct io_buffer {
SLIST_ENTRY(io_buffer) entries;
struct exec_closure *ec;
struct sudo_event *revent;
struct sudo_event *wevent;
sudo_io_action_t action;
int len; /* buffer length (how much produced) */
int off; /* write position (how much already consumed) */
char buf[64 * 1024];
};
SLIST_HEAD(io_buffer_list, io_buffer);
/*
* Indices into io_fds[] when logging I/O.
*/
#define SFD_STDIN 0
#define SFD_STDOUT 1
@@ -45,6 +110,9 @@
#define SFD_FOLLOWER 4
#define SFD_USERTTY 5
/* Evaluates to true if the event has /dev/tty as its fd. */
#define USERTTY_EVENT(_ev) (sudo_ev_get_fd((_ev)) == io_fds[SFD_USERTTY])
/*
* Special values to indicate whether continuing in foreground or background.
*/
@@ -104,18 +172,11 @@ union sudo_token_un {
# endif /* HAVE_DECL_SECCOMP_MODE_FILTER */
#endif /* _PATH_SUDO_INTERCEPT && __linux__ */
/*
* Symbols shared between exec.c, exec_nopty.c, exec_pty.c and exec_monitor.c
*/
struct command_details;
struct command_status;
struct sudo_event_base;
struct stat;
/* exec.c */
void exec_cmnd(struct command_details *details, sigset_t *mask, int intercept_fd, int errfd);
void terminate_command(pid_t pid, bool use_pgrp);
bool sudo_terminated(struct command_status *cstat);
void free_exec_closure(struct exec_closure *ec);
/* exec_common.c */
int sudo_execve(int fd, const char *path, char *const argv[], char *envp[], int intercept_fd, int flags);
@@ -126,6 +187,24 @@ char **enable_monitor(char *envp[], const char *dso);
void *intercept_setup(int fd, struct sudo_event_base *evbase, struct command_details *details);
void intercept_cleanup(void);
/* exec_iolog.c */
bool log_ttyin(const char *buf, unsigned int n, struct io_buffer *iob);
bool log_stdin(const char *buf, unsigned int n, struct io_buffer *iob);
bool log_ttyout(const char *buf, unsigned int n, struct io_buffer *iob);
bool log_stdout(const char *buf, unsigned int n, struct io_buffer *iob);
bool log_stderr(const char *buf, unsigned int n, struct io_buffer *iob);
void log_suspend(struct exec_closure *ec, int signo);
void log_winchange(struct exec_closure *ec, unsigned int rows, unsigned int cols);
void io_buf_new(int rfd, int wfd, bool (*action)(const char *, unsigned int, struct io_buffer *), void (*read_cb)(int fd, int what, void *v), void (*write_cb)(int fd, int what, void *v), struct exec_closure *ec, struct io_buffer_list *head);
int safe_close(int fd);
void ev_free_by_fd(struct sudo_event_base *evbase, int fd);
void free_io_bufs(void);
void add_io_events(struct sudo_event_base *evbase);
void del_io_events(bool nonblocking);
void init_ttyblock(void);
extern struct io_buffer_list iobufs;
extern int ttymode;
/* exec_nopty.c */
void exec_nopty(struct command_details *details, struct command_status *cstat);