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