Blame SOURCES/openssh-6.6.1p1-log-in-chroot.patch

f5835d
diff -up openssh-7.4p1/log.c.log-in-chroot openssh-7.4p1/log.c
f5835d
--- openssh-7.4p1/log.c.log-in-chroot	2016-12-19 05:59:41.000000000 +0100
f5835d
+++ openssh-7.4p1/log.c	2016-12-23 15:14:33.330168088 +0100
f5835d
@@ -250,6 +250,11 @@ debug3(const char *fmt,...)
f5835d
 void
f5835d
 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
f5835d
 {
f5835d
+	log_init_handler(av0, level, facility, on_stderr, 1);
f5835d
+}
f5835d
+
f5835d
+void
f5835d
+log_init_handler(char *av0, LogLevel level, SyslogFacility facility, int on_stderr, int reset_handler) {
f5835d
 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
f5835d
 	struct syslog_data sdata = SYSLOG_DATA_INIT;
f5835d
 #endif
f5835d
@@ -273,8 +278,10 @@ log_init(char *av0, LogLevel level, Sysl
f5835d
 		exit(1);
f5835d
 	}
f5835d
 
f5835d
-	log_handler = NULL;
f5835d
-	log_handler_ctx = NULL;
f5835d
+	if (reset_handler) {
f5835d
+		log_handler = NULL;
f5835d
+		log_handler_ctx = NULL;
f5835d
+	}
f5835d
 
f5835d
 	log_on_stderr = on_stderr;
f5835d
 	if (on_stderr)
f5835d
diff -up openssh-7.4p1/log.h.log-in-chroot openssh-7.4p1/log.h
f5835d
--- openssh-7.4p1/log.h.log-in-chroot	2016-12-19 05:59:41.000000000 +0100
f5835d
+++ openssh-7.4p1/log.h	2016-12-23 15:14:33.330168088 +0100
f5835d
@@ -49,6 +49,7 @@ typedef enum {
f5835d
 typedef void (log_handler_fn)(LogLevel, const char *, void *);
f5835d
 
f5835d
 void     log_init(char *, LogLevel, SyslogFacility, int);
f5835d
+void     log_init_handler(char *, LogLevel, SyslogFacility, int, int);
f5835d
 LogLevel log_level_get(void);
f5835d
 int      log_change_level(LogLevel);
f5835d
 int      log_is_on_stderr(void);
f5835d
diff -up openssh-7.4p1/monitor.c.log-in-chroot openssh-7.4p1/monitor.c
f5835d
--- openssh-7.4p1/monitor.c.log-in-chroot	2016-12-23 15:14:33.311168085 +0100
f5835d
+++ openssh-7.4p1/monitor.c	2016-12-23 15:16:42.154193100 +0100
f5835d
@@ -307,6 +307,8 @@ monitor_child_preauth(Authctxt *_authctx
f5835d
 		close(pmonitor->m_log_sendfd);
f5835d
 	pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
f5835d
 
f5835d
+	pmonitor->m_state = "preauth";
f5835d
+
f5835d
 	authctxt = _authctxt;
f5835d
 	memset(authctxt, 0, sizeof(*authctxt));
f5835d
 	ssh->authctxt = authctxt;
f5835d
@@ -405,6 +407,8 @@ monitor_child_postauth(struct monitor *p
f5835d
 	close(pmonitor->m_recvfd);
f5835d
 	pmonitor->m_recvfd = -1;
f5835d
 
f5835d
+	pmonitor->m_state = "postauth";
f5835d
+
f5835d
 	monitor_set_child_handler(pmonitor->m_pid);
f5835d
 	signal(SIGHUP, &monitor_child_handler);
f5835d
 	signal(SIGTERM, &monitor_child_handler);
f5835d
@@ -472,7 +476,7 @@ monitor_read_log(struct monitor *pmonito
f5835d
 	if (log_level_name(level) == NULL)
f5835d
 		fatal("%s: invalid log level %u (corrupted message?)",
f5835d
 		    __func__, level);
f5835d
-	do_log2(level, "%s [preauth]", msg);
f5835d
+	do_log2(level, "%s [%s]", msg, pmonitor->m_state);
f5835d
 
f5835d
 	sshbuf_free(logmsg);
f5835d
 	free(msg);
f5835d
@@ -1719,13 +1723,28 @@ monitor_init(void)
f5835d
 	mon = xcalloc(1, sizeof(*mon));
f5835d
 	monitor_openfds(mon, 1);
f5835d
 
f5835d
+	mon->m_state = "";
f5835d
+
f5835d
 	return mon;
f5835d
 }
f5835d
 
f5835d
 void
f5835d
-monitor_reinit(struct monitor *mon)
f5835d
+monitor_reinit(struct monitor *mon, const char *chroot_dir)
f5835d
 {
f5835d
-	monitor_openfds(mon, 0);
f5835d
+	struct stat dev_log_stat;
f5835d
+	char *dev_log_path;
f5835d
+	int do_logfds = 0;
f5835d
+
f5835d
+	if (chroot_dir != NULL) {
f5835d
+		xasprintf(&dev_log_path, "%s/dev/log", chroot_dir);
f5835d
+
f5835d
+		if (stat(dev_log_path, &dev_log_stat) != 0) {
f5835d
+			debug("%s: /dev/log doesn't exist in %s chroot - will try to log via monitor using [postauth] suffix", __func__, chroot_dir);
f5835d
+			do_logfds = 1;
f5835d
+		}
f5835d
+		free(dev_log_path);
f5835d
+	}
f5835d
+	monitor_openfds(mon, do_logfds);
f5835d
 }
f5835d
 
f5835d
 #ifdef GSSAPI
f5835d
diff -up openssh-7.4p1/monitor.h.log-in-chroot openssh-7.4p1/monitor.h
f5835d
--- openssh-7.4p1/monitor.h.log-in-chroot	2016-12-23 15:14:33.330168088 +0100
f5835d
+++ openssh-7.4p1/monitor.h	2016-12-23 15:16:28.372190424 +0100
f5835d
@@ -83,10 +83,11 @@ struct monitor {
f5835d
 	int			 m_log_sendfd;
f5835d
 	struct kex		**m_pkex;
f5835d
 	pid_t			 m_pid;
f5835d
+	char		*m_state;
f5835d
 };
f5835d
 
f5835d
 struct monitor *monitor_init(void);
f5835d
-void monitor_reinit(struct monitor *);
f5835d
+void monitor_reinit(struct monitor *, const char *);
f5835d
 
f5835d
 struct Authctxt;
f5835d
 void monitor_child_preauth(struct Authctxt *, struct monitor *);
f5835d
diff -up openssh-7.4p1/session.c.log-in-chroot openssh-7.4p1/session.c
f5835d
--- openssh-7.4p1/session.c.log-in-chroot	2016-12-23 15:14:33.319168086 +0100
f5835d
+++ openssh-7.4p1/session.c	2016-12-23 15:18:18.742211853 +0100
f5835d
@@ -160,6 +160,7 @@ login_cap_t *lc;
f5835d
 
f5835d
 static int is_child = 0;
f5835d
 static int in_chroot = 0;
f5835d
+static int have_dev_log = 1;
f5835d
 
f5835d
 /* File containing userauth info, if ExposeAuthInfo set */
f5835d
 static char *auth_info_file = NULL;
f5835d
@@ -619,6 +620,7 @@ do_exec(Session *s, const char *command)
f5835d
 	int ret;
f5835d
 	const char *forced = NULL, *tty = NULL;
f5835d
 	char session_type[1024];
f5835d
+	struct stat dev_log_stat;
f5835d
 
f5835d
 	if (options.adm_forced_command) {
f5835d
 		original_command = command;
f5835d
@@ -676,6 +678,10 @@ do_exec(Session *s, const char *command)
f5835d
 			tty += 5;
f5835d
 	}
f5835d
 
f5835d
+	if (lstat("/dev/log", &dev_log_stat) != 0) {
f5835d
+		have_dev_log = 0;
f5835d
+	}
f5835d
+
f5835d
 	verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
f5835d
 	    session_type,
f5835d
 	    tty == NULL ? "" : " on ",
f5835d
@@ -1486,14 +1492,6 @@ child_close_fds(void)
f5835d
 	 * descriptors left by system functions.  They will be closed later.
f5835d
 	 */
f5835d
 	endpwent();
f5835d
-
f5835d
-	/*
f5835d
-	 * Close any extra open file descriptors so that we don't have them
f5835d
-	 * hanging around in clients.  Note that we want to do this after
f5835d
-	 * initgroups, because at least on Solaris 2.3 it leaves file
f5835d
-	 * descriptors open.
f5835d
-	 */
f5835d
-	closefrom(STDERR_FILENO + 1);
f5835d
 }
f5835d
 
f5835d
 /*
f5835d
@@ -1629,8 +1627,6 @@ do_child(Session *s, const char *command
f5835d
 			exit(1);
f5835d
 	}
f5835d
 
f5835d
-	closefrom(STDERR_FILENO + 1);
f5835d
-
f5835d
 	do_rc_files(ssh, s, shell);
f5835d
 
f5835d
 	/* restore SIGPIPE for child */
f5835d
@@ -1653,9 +1649,17 @@ do_child(Session *s, const char *command
f5835d
 		argv[i] = NULL;
f5835d
 		optind = optreset = 1;
f5835d
 		__progname = argv[0];
f5835d
-		exit(sftp_server_main(i, argv, s->pw));
f5835d
+		exit(sftp_server_main(i, argv, s->pw, have_dev_log));
f5835d
 	}
f5835d
 
f5835d
+	/*
f5835d
+	 * Close any extra open file descriptors so that we don't have them
f5835d
+	 * hanging around in clients.  Note that we want to do this after
f5835d
+	 * initgroups, because at least on Solaris 2.3 it leaves file
f5835d
+	 * descriptors open.
f5835d
+	 */
f5835d
+	closefrom(STDERR_FILENO + 1);
f5835d
+
f5835d
 	fflush(NULL);
f5835d
 
f5835d
 	/* Get the last component of the shell name. */
f5835d
diff -up openssh-7.4p1/sftp.h.log-in-chroot openssh-7.4p1/sftp.h
f5835d
--- openssh-7.4p1/sftp.h.log-in-chroot	2016-12-19 05:59:41.000000000 +0100
f5835d
+++ openssh-7.4p1/sftp.h	2016-12-23 15:14:33.331168088 +0100
f5835d
@@ -97,5 +97,5 @@
f5835d
 
f5835d
 struct passwd;
f5835d
 
f5835d
-int	sftp_server_main(int, char **, struct passwd *);
f5835d
+int	sftp_server_main(int, char **, struct passwd *, int);
f5835d
 void	sftp_server_cleanup_exit(int) __attribute__((noreturn));
f5835d
diff -up openssh-7.4p1/sftp-server.c.log-in-chroot openssh-7.4p1/sftp-server.c
f5835d
--- openssh-7.4p1/sftp-server.c.log-in-chroot	2016-12-19 05:59:41.000000000 +0100
f5835d
+++ openssh-7.4p1/sftp-server.c	2016-12-23 15:14:33.331168088 +0100
f5835d
@@ -1497,7 +1497,7 @@ sftp_server_usage(void)
f5835d
 }
f5835d
 
f5835d
 int
f5835d
-sftp_server_main(int argc, char **argv, struct passwd *user_pw)
f5835d
+sftp_server_main(int argc, char **argv, struct passwd *user_pw, int reset_handler)
f5835d
 {
f5835d
 	fd_set *rset, *wset;
f5835d
 	int i, r, in, out, max, ch, skipargs = 0, log_stderr = 0;
f5835d
@@ -1511,7 +1511,7 @@ sftp_server_main(int argc, char **argv,
f5835d
 
f5835d
 	ssh_malloc_init();	/* must be called before any mallocs */
f5835d
 	__progname = ssh_get_progname(argv[0]);
f5835d
-	log_init(__progname, log_level, log_facility, log_stderr);
f5835d
+	log_init_handler(__progname, log_level, log_facility, log_stderr, reset_handler);
f5835d
 
f5835d
 	pw = pwcopy(user_pw);
f5835d
 
f5835d
@@ -1582,7 +1582,7 @@ sftp_server_main(int argc, char **argv,
f5835d
 		}
f5835d
 	}
f5835d
 
f5835d
-	log_init(__progname, log_level, log_facility, log_stderr);
f5835d
+	log_init_handler(__progname, log_level, log_facility, log_stderr, reset_handler);
f5835d
 
f5835d
 	/*
f5835d
 	 * On platforms where we can, avoid making /proc/self/{mem,maps}
f5835d
diff -up openssh-7.4p1/sftp-server-main.c.log-in-chroot openssh-7.4p1/sftp-server-main.c
f5835d
--- openssh-7.4p1/sftp-server-main.c.log-in-chroot	2016-12-19 05:59:41.000000000 +0100
f5835d
+++ openssh-7.4p1/sftp-server-main.c	2016-12-23 15:14:33.331168088 +0100
f5835d
@@ -49,5 +49,5 @@ main(int argc, char **argv)
f5835d
 		return 1;
f5835d
 	}
f5835d
 
f5835d
-	return (sftp_server_main(argc, argv, user_pw));
f5835d
+	return (sftp_server_main(argc, argv, user_pw, 0));
f5835d
 }
f5835d
diff -up openssh-7.4p1/sshd.c.log-in-chroot openssh-7.4p1/sshd.c
f5835d
--- openssh-7.4p1/sshd.c.log-in-chroot	2016-12-23 15:14:33.328168088 +0100
f5835d
+++ openssh-7.4p1/sshd.c	2016-12-23 15:14:33.332168088 +0100
f5835d
@@ -650,7 +650,7 @@ privsep_postauth(Authctxt *authctxt)
f5835d
 	}
f5835d
 
f5835d
 	/* New socket pair */
f5835d
-	monitor_reinit(pmonitor);
f5835d
+	monitor_reinit(pmonitor, options.chroot_directory);
f5835d
 
f5835d
 	pmonitor->m_pid = fork();
f5835d
 	if (pmonitor->m_pid == -1)
f5835d
@@ -668,6 +668,11 @@ privsep_postauth(Authctxt *authctxt)
f5835d
 
f5835d
 	close(pmonitor->m_sendfd);
f5835d
 	pmonitor->m_sendfd = -1;
f5835d
+	close(pmonitor->m_log_recvfd);
f5835d
+	pmonitor->m_log_recvfd = -1;
f5835d
+
f5835d
+	if (pmonitor->m_log_sendfd != -1)
f5835d
+		set_log_handler(mm_log_handler, pmonitor);
f5835d
 
f5835d
 	/* Demote the private keys to public keys. */
f5835d
 	demote_sensitive_data();