rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
9070b3
diff -up openssh-8.6p1/log.c.log-in-chroot openssh-8.6p1/log.c
9070b3
--- openssh-8.6p1/log.c.log-in-chroot	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/log.c	2021-05-06 11:32:25.179006811 +0200
9070b3
@@ -194,6 +194,11 @@ void
9070b3
 log_init(const char *av0, LogLevel level, SyslogFacility facility,
9070b3
     int on_stderr)
9070b3
 {
9070b3
+	log_init_handler(av0, level, facility, on_stderr, 1);
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+log_init_handler(const char *av0, LogLevel level, SyslogFacility facility, int on_stderr, int reset_handler) {
9070b3
 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
9070b3
 	struct syslog_data sdata = SYSLOG_DATA_INIT;
9070b3
 #endif
9070b3
@@ -206,8 +211,10 @@ log_init(const char *av0, LogLevel level
9070b3
 		exit(1);
9070b3
 	}
9070b3
 
9070b3
-	log_handler = NULL;
9070b3
-	log_handler_ctx = NULL;
9070b3
+	if (reset_handler) {
9070b3
+		log_handler = NULL;
9070b3
+		log_handler_ctx = NULL;
9070b3
+	}
9070b3
 
9070b3
 	log_on_stderr = on_stderr;
9070b3
 	if (on_stderr)
9070b3
diff -up openssh-8.6p1/log.h.log-in-chroot openssh-8.6p1/log.h
9070b3
--- openssh-8.6p1/log.h.log-in-chroot	2021-05-06 11:32:25.179006811 +0200
9070b3
+++ openssh-8.6p1/log.h	2021-05-06 11:34:22.349925757 +0200
9070b3
@@ -52,6 +52,7 @@ typedef enum {
9070b3
 typedef void (log_handler_fn)(LogLevel, int, const char *, void *);
9070b3
 
9070b3
 void     log_init(const char *, LogLevel, SyslogFacility, int);
9070b3
+void     log_init_handler(const char *, LogLevel, SyslogFacility, int, int);
9070b3
 LogLevel log_level_get(void);
9070b3
 int      log_change_level(LogLevel);
9070b3
 int      log_is_on_stderr(void);
9070b3
diff -up openssh-8.6p1/monitor.c.log-in-chroot openssh-8.6p1/monitor.c
9070b3
--- openssh-8.6p1/monitor.c.log-in-chroot	2021-05-06 11:32:25.153006607 +0200
9070b3
+++ openssh-8.6p1/monitor.c	2021-05-06 11:33:37.671575348 +0200
9070b3
@@ -297,6 +297,8 @@ monitor_child_preauth(struct ssh *ssh, s
9070b3
 		close(pmonitor->m_log_sendfd);
9070b3
 	pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
9070b3
 
9070b3
+	pmonitor->m_state = "preauth";
9070b3
+
9070b3
 	authctxt = (Authctxt *)ssh->authctxt;
9070b3
 	memset(authctxt, 0, sizeof(*authctxt));
9070b3
 	ssh->authctxt = authctxt;
9070b3
@@ -408,6 +410,8 @@ monitor_child_postauth(struct ssh *ssh,
9070b3
 	close(pmonitor->m_recvfd);
9070b3
 	pmonitor->m_recvfd = -1;
9070b3
 
9070b3
+	pmonitor->m_state = "postauth";
9070b3
+
9070b3
 	monitor_set_child_handler(pmonitor->m_pid);
9070b3
 	ssh_signal(SIGHUP, &monitor_child_handler);
9070b3
 	ssh_signal(SIGTERM, &monitor_child_handler);
9070b3
@@ -480,7 +484,7 @@ monitor_read_log(struct monitor *pmonito
9070b3
 	/* Log it */
9070b3
 	if (log_level_name(level) == NULL)
9070b3
 		fatal_f("invalid log level %u (corrupted message?)", level);
9070b3
-	sshlogdirect(level, forced, "%s [preauth]", msg);
9070b3
+	sshlogdirect(level, forced, "%s [%s]", msg, pmonitor->m_state);
9070b3
 
9070b3
 	sshbuf_free(logmsg);
9070b3
 	free(msg);
9070b3
@@ -1868,13 +1872,28 @@ monitor_init(void)
9070b3
 	mon = xcalloc(1, sizeof(*mon));
9070b3
 	monitor_openfds(mon, 1);
9070b3
 
9070b3
+	mon->m_state = "";
9070b3
+
9070b3
 	return mon;
9070b3
 }
9070b3
 
9070b3
 void
9070b3
-monitor_reinit(struct monitor *mon)
9070b3
+monitor_reinit(struct monitor *mon, const char *chroot_dir)
9070b3
 {
9070b3
-	monitor_openfds(mon, 0);
9070b3
+	struct stat dev_log_stat;
9070b3
+	char *dev_log_path;
9070b3
+	int do_logfds = 0;
9070b3
+
9070b3
+	if (chroot_dir != NULL) {
9070b3
+		xasprintf(&dev_log_path, "%s/dev/log", chroot_dir);
9070b3
+
9070b3
+		if (stat(dev_log_path, &dev_log_stat) != 0) {
9070b3
+			debug_f("/dev/log doesn't exist in %s chroot - will try to log via monitor using [postauth] suffix", chroot_dir);
9070b3
+			do_logfds = 1;
9070b3
+		}
9070b3
+		free(dev_log_path);
9070b3
+	}
9070b3
+	monitor_openfds(mon, do_logfds);
9070b3
 }
9070b3
 
9070b3
 #ifdef GSSAPI
9070b3
diff -up openssh-8.6p1/monitor.h.log-in-chroot openssh-8.6p1/monitor.h
9070b3
--- openssh-8.6p1/monitor.h.log-in-chroot	2021-05-06 11:32:25.153006607 +0200
9070b3
+++ openssh-8.6p1/monitor.h	2021-05-06 11:32:25.180006819 +0200
9070b3
@@ -80,10 +80,11 @@ struct monitor {
9070b3
 	int			 m_log_sendfd;
9070b3
 	struct kex		**m_pkex;
9070b3
 	pid_t			 m_pid;
9070b3
+	char		*m_state;
9070b3
 };
9070b3
 
9070b3
 struct monitor *monitor_init(void);
9070b3
-void monitor_reinit(struct monitor *);
9070b3
+void monitor_reinit(struct monitor *, const char *);
9070b3
 
9070b3
 struct Authctxt;
9070b3
 void monitor_child_preauth(struct ssh *, struct monitor *);
9070b3
diff -up openssh-8.6p1/session.c.log-in-chroot openssh-8.6p1/session.c
9070b3
--- openssh-8.6p1/session.c.log-in-chroot	2021-05-06 11:32:25.166006709 +0200
9070b3
+++ openssh-8.6p1/session.c	2021-05-06 11:32:25.181006827 +0200
9070b3
@@ -160,6 +160,7 @@ login_cap_t *lc;
9070b3
 
9070b3
 static int is_child = 0;
9070b3
 static int in_chroot = 0;
9070b3
+static int have_dev_log = 1;
9070b3
 
9070b3
 /* File containing userauth info, if ExposeAuthInfo set */
9070b3
 static char *auth_info_file = NULL;
9070b3
@@ -661,6 +662,7 @@ do_exec(struct ssh *ssh, Session *s, con
9070b3
 	int ret;
9070b3
 	const char *forced = NULL, *tty = NULL;
9070b3
 	char session_type[1024];
9070b3
+	struct stat dev_log_stat;
9070b3
 
9070b3
 	if (options.adm_forced_command) {
9070b3
 		original_command = command;
9070b3
@@ -720,6 +722,10 @@ do_exec(struct ssh *ssh, Session *s, con
9070b3
 			tty += 5;
9070b3
 	}
9070b3
 
9070b3
+	if (lstat("/dev/log", &dev_log_stat) != 0) {
9070b3
+		have_dev_log = 0;
9070b3
+	}
9070b3
+
9070b3
 	verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
9070b3
 	    session_type,
9070b3
 	    tty == NULL ? "" : " on ",
9070b3
@@ -1524,14 +1530,6 @@ child_close_fds(struct ssh *ssh)
9070b3
 
9070b3
 	/* Stop directing logs to a high-numbered fd before we close it */
9070b3
 	log_redirect_stderr_to(NULL);
9070b3
-
9070b3
-	/*
9070b3
-	 * Close any extra open file descriptors so that we don't have them
9070b3
-	 * hanging around in clients.  Note that we want to do this after
9070b3
-	 * initgroups, because at least on Solaris 2.3 it leaves file
9070b3
-	 * descriptors open.
9070b3
-	 */
9070b3
-	closefrom(STDERR_FILENO + 1);
9070b3
 }
9070b3
 
9070b3
 /*
9070b3
@@ -1665,8 +1663,6 @@ do_child(struct ssh *ssh, Session *s, co
9070b3
 			exit(1);
9070b3
 	}
9070b3
 
9070b3
-	closefrom(STDERR_FILENO + 1);
9070b3
-
9070b3
 	do_rc_files(ssh, s, shell);
9070b3
 
9070b3
 	/* restore SIGPIPE for child */
9070b3
@@ -1691,9 +1687,17 @@ do_child(struct ssh *ssh, Session *s, co
9070b3
 		argv[i] = NULL;
9070b3
 		optind = optreset = 1;
9070b3
 		__progname = argv[0];
9070b3
-		exit(sftp_server_main(i, argv, s->pw));
9070b3
+		exit(sftp_server_main(i, argv, s->pw, have_dev_log));
9070b3
 	}
9070b3
 
9070b3
+	/*
9070b3
+	 * Close any extra open file descriptors so that we don't have them
9070b3
+	 * hanging around in clients.  Note that we want to do this after
9070b3
+	 * initgroups, because at least on Solaris 2.3 it leaves file
9070b3
+	 * descriptors open.
9070b3
+	 */
9070b3
+	closefrom(STDERR_FILENO + 1);
9070b3
+
9070b3
 	fflush(NULL);
9070b3
 
9070b3
 	/* Get the last component of the shell name. */
9070b3
diff -up openssh-8.6p1/sftp.h.log-in-chroot openssh-8.6p1/sftp.h
9070b3
--- openssh-8.6p1/sftp.h.log-in-chroot	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/sftp.h	2021-05-06 11:32:25.181006827 +0200
9070b3
@@ -97,5 +97,5 @@
9070b3
 
9070b3
 struct passwd;
9070b3
 
9070b3
-int	sftp_server_main(int, char **, struct passwd *);
9070b3
+int	sftp_server_main(int, char **, struct passwd *, int);
9070b3
 void	sftp_server_cleanup_exit(int) __attribute__((noreturn));
9070b3
diff -up openssh-8.6p1/sftp-server.c.log-in-chroot openssh-8.6p1/sftp-server.c
9070b3
--- openssh-8.6p1/sftp-server.c.log-in-chroot	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/sftp-server.c	2021-05-06 11:32:25.181006827 +0200
9070b3
@@ -1644,7 +1644,7 @@ sftp_server_usage(void)
9070b3
 }
9070b3
 
9070b3
 int
9070b3
-sftp_server_main(int argc, char **argv, struct passwd *user_pw)
9070b3
+sftp_server_main(int argc, char **argv, struct passwd *user_pw, int reset_handler)
9070b3
 {
9070b3
 	fd_set *rset, *wset;
9070b3
 	int i, r, in, out, max, ch, skipargs = 0, log_stderr = 0;
9070b3
@@ -1657,7 +1657,7 @@ sftp_server_main(int argc, char **argv,
9070b3
 	extern char *__progname;
9070b3
 
9070b3
 	__progname = ssh_get_progname(argv[0]);
9070b3
-	log_init(__progname, log_level, log_facility, log_stderr);
9070b3
+	log_init_handler(__progname, log_level, log_facility, log_stderr, reset_handler);
9070b3
 
9070b3
 	pw = pwcopy(user_pw);
9070b3
 
9070b3
@@ -1730,7 +1730,7 @@ sftp_server_main(int argc, char **argv,
9070b3
 		}
9070b3
 	}
9070b3
 
9070b3
-	log_init(__progname, log_level, log_facility, log_stderr);
9070b3
+	log_init_handler(__progname, log_level, log_facility, log_stderr, reset_handler);
9070b3
 
9070b3
 	/*
9070b3
 	 * On platforms where we can, avoid making /proc/self/{mem,maps}
9070b3
diff -up openssh-8.6p1/sftp-server-main.c.log-in-chroot openssh-8.6p1/sftp-server-main.c
9070b3
--- openssh-8.6p1/sftp-server-main.c.log-in-chroot	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/sftp-server-main.c	2021-05-06 11:32:25.181006827 +0200
9070b3
@@ -50,5 +50,5 @@ main(int argc, char **argv)
9070b3
 		return 1;
9070b3
 	}
9070b3
 
9070b3
-	return (sftp_server_main(argc, argv, user_pw));
9070b3
+	return (sftp_server_main(argc, argv, user_pw, 0));
9070b3
 }
9070b3
diff -up openssh-8.6p1/sshd.c.log-in-chroot openssh-8.6p1/sshd.c
9070b3
--- openssh-8.6p1/sshd.c.log-in-chroot	2021-05-06 11:32:25.177006795 +0200
9070b3
+++ openssh-8.6p1/sshd.c	2021-05-06 11:32:25.182006834 +0200
9070b3
@@ -559,7 +559,7 @@ privsep_postauth(struct ssh *ssh, Authct
9070b3
 	}
9070b3
 
9070b3
 	/* New socket pair */
9070b3
-	monitor_reinit(pmonitor);
9070b3
+	monitor_reinit(pmonitor, options.chroot_directory);
9070b3
 
9070b3
 	pmonitor->m_pid = fork();
9070b3
 	if (pmonitor->m_pid == -1)
9070b3
@@ -578,6 +578,11 @@ privsep_postauth(struct ssh *ssh, Authct
9070b3
 
9070b3
 	close(pmonitor->m_sendfd);
9070b3
 	pmonitor->m_sendfd = -1;
9070b3
+	close(pmonitor->m_log_recvfd);
9070b3
+	pmonitor->m_log_recvfd = -1;
9070b3
+
9070b3
+	if (pmonitor->m_log_sendfd != -1)
9070b3
+		set_log_handler(mm_log_handler, pmonitor);
9070b3
 
9070b3
 	/* Demote the private keys to public keys. */
9070b3
 	demote_sensitive_data();