kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
017ff1
diff --git a/log.c b/log.c
017ff1
index 32e1d2e..d4caeb5 100644
017ff1
--- a/log.c
017ff1
+++ b/log.c
017ff1
@@ -241,6 +241,11 @@ debug3(const char *fmt,...)
017ff1
 void
017ff1
 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
017ff1
 {
017ff1
+	log_init_handler(av0, level, facility, on_stderr, 1);
017ff1
+}
017ff1
+
017ff1
+void
017ff1
+log_init_handler(char *av0, LogLevel level, SyslogFacility facility, int on_stderr, int reset_handler) {
017ff1
 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
017ff1
 	struct syslog_data sdata = SYSLOG_DATA_INIT;
017ff1
 #endif
017ff1
@@ -264,8 +269,10 @@ log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
017ff1
 		exit(1);
017ff1
 	}
017ff1
 
017ff1
-	log_handler = NULL;
017ff1
-	log_handler_ctx = NULL;
017ff1
+	if (reset_handler) {
017ff1
+		log_handler = NULL;
017ff1
+		log_handler_ctx = NULL;
017ff1
+	}
017ff1
 
017ff1
 	log_on_stderr = on_stderr;
017ff1
 	if (on_stderr)
017ff1
diff --git a/log.h b/log.h
017ff1
index ae7df25..30c3310 100644
017ff1
--- a/log.h
017ff1
+++ b/log.h
017ff1
@@ -49,6 +49,7 @@ typedef enum {
017ff1
 typedef void (log_handler_fn)(LogLevel, const char *, void *);
017ff1
 
017ff1
 void     log_init(char *, LogLevel, SyslogFacility, int);
017ff1
+void     log_init_handler(char *, LogLevel, SyslogFacility, int, int);
017ff1
 void     log_change_level(LogLevel);
017ff1
 int      log_is_on_stderr(void);
017ff1
 void     log_redirect_stderr_to(const char *);
017ff1
diff --git a/monitor.c b/monitor.c
017ff1
index 7461fae..da2f766 100644
017ff1
--- a/monitor.c
017ff1
+++ b/monitor.c
017ff1
@@ -364,6 +364,8 @@ monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
017ff1
 	close(pmonitor->m_log_sendfd);
017ff1
 	pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
017ff1
 
017ff1
+	pmonitor->m_state = "preauth";
017ff1
+
017ff1
 	authctxt = _authctxt;
017ff1
 	memset(authctxt, 0, sizeof(*authctxt));
017ff1
 
017ff1
@@ -472,6 +474,8 @@ monitor_child_postauth(struct monitor *pmonitor)
017ff1
 	close(pmonitor->m_recvfd);
017ff1
 	pmonitor->m_recvfd = -1;
017ff1
 
017ff1
+	pmonitor->m_state = "postauth";
017ff1
+
017ff1
 	monitor_set_child_handler(pmonitor->m_pid);
017ff1
 	signal(SIGHUP, &monitor_child_handler);
017ff1
 	signal(SIGTERM, &monitor_child_handler);
017ff1
@@ -552,7 +556,7 @@ monitor_read_log(struct monitor *pmonitor)
017ff1
 	if (log_level_name(level) == NULL)
017ff1
 		fatal("%s: invalid log level %u (corrupted message?)",
017ff1
 		    __func__, level);
017ff1
-	do_log2(level, "%s [preauth]", msg);
017ff1
+	do_log2(level, "%s [%s]", msg, pmonitor->m_state);
017ff1
 
017ff1
 	buffer_free(&logmsg);
017ff1
 	free(msg);
017ff1
@@ -2083,13 +2087,28 @@ monitor_init(void)
017ff1
 		mm_init_compression(mon->m_zlib);
017ff1
 	}
017ff1
 
017ff1
+	mon->m_state = "";
017ff1
+
017ff1
 	return mon;
017ff1
 }
017ff1
 
017ff1
 void
017ff1
-monitor_reinit(struct monitor *mon)
017ff1
+monitor_reinit(struct monitor *mon, const char *chroot_dir)
017ff1
 {
017ff1
-	monitor_openfds(mon, 0);
017ff1
+	struct stat dev_log_stat;
017ff1
+	char *dev_log_path;
017ff1
+	int do_logfds = 0;
017ff1
+
017ff1
+	if (chroot_dir != NULL) {
017ff1
+		xasprintf(&dev_log_path, "%s/dev/log", chroot_dir);
017ff1
+
017ff1
+		if (stat(dev_log_path, &dev_log_stat) != 0) {
017ff1
+			debug("%s: /dev/log doesn't exist in %s chroot - will try to log via monitor using [postauth] suffix", __func__, chroot_dir);
017ff1
+			do_logfds = 1;
017ff1
+		}
017ff1
+		free(dev_log_path);
017ff1
+	}
017ff1
+	monitor_openfds(mon, do_logfds);
017ff1
 }
017ff1
 
017ff1
 #ifdef GSSAPI
017ff1
diff --git a/monitor.h b/monitor.h
017ff1
index ff79fbb..00c2028 100644
017ff1
--- a/monitor.h
017ff1
+++ b/monitor.h
017ff1
@@ -83,10 +83,11 @@ struct monitor {
017ff1
 	struct mm_master	*m_zlib;
017ff1
 	struct Kex		**m_pkex;
017ff1
 	pid_t			 m_pid;
017ff1
+	char		*m_state;
017ff1
 };
017ff1
 
017ff1
 struct monitor *monitor_init(void);
017ff1
-void monitor_reinit(struct monitor *);
017ff1
+void monitor_reinit(struct monitor *, const char *);
017ff1
 void monitor_sync(struct monitor *);
017ff1
 
017ff1
 struct Authctxt;
017ff1
diff --git a/session.c b/session.c
017ff1
index e4add93..bc4a8dd 100644
017ff1
--- a/session.c
017ff1
+++ b/session.c
017ff1
@@ -160,6 +160,8 @@ login_cap_t *lc;
017ff1
 
017ff1
 static int is_child = 0;
017ff1
 
017ff1
+static int have_dev_log = 1;
017ff1
+
017ff1
 /* Name and directory of socket for authentication agent forwarding. */
017ff1
 static char *auth_sock_name = NULL;
017ff1
 static char *auth_sock_dir = NULL;
017ff1
@@ -523,8 +525,8 @@ do_exec_no_pty(Session *s, const char *command)
017ff1
 		is_child = 1;
017ff1
 
017ff1
 		/* Child.  Reinitialize the log since the pid has changed. */
017ff1
-		log_init(__progname, options.log_level,
017ff1
-		    options.log_facility, log_stderr);
017ff1
+		log_init_handler(__progname, options.log_level,
017ff1
+		    options.log_facility, log_stderr, have_dev_log);
017ff1
 
017ff1
 		/*
017ff1
 		 * Create a new session and process group since the 4.4BSD
017ff1
@@ -692,8 +694,8 @@ do_exec_pty(Session *s, const char *command)
017ff1
 		close(ptymaster);
017ff1
 
017ff1
 		/* Child.  Reinitialize the log because the pid has changed. */
017ff1
-		log_init(__progname, options.log_level,
017ff1
-		    options.log_facility, log_stderr);
017ff1
+		log_init_handler(__progname, options.log_level,
017ff1
+		    options.log_facility, log_stderr, have_dev_log);
017ff1
 		/* Close the master side of the pseudo tty. */
017ff1
 		close(ptyfd);
017ff1
 
017ff1
@@ -797,6 +799,7 @@ do_exec(Session *s, const char *command)
017ff1
 	int ret;
017ff1
 	const char *forced = NULL;
017ff1
 	char session_type[1024], *tty = NULL;
017ff1
+	struct stat dev_log_stat;
017ff1
 
017ff1
 	if (options.adm_forced_command) {
017ff1
 		original_command = command;
017ff1
@@ -854,6 +857,10 @@ do_exec(Session *s, const char *command)
017ff1
 			tty += 5;
017ff1
 	}
017ff1
 
017ff1
+	if (lstat("/dev/log", &dev_log_stat) != 0) {
017ff1
+		have_dev_log = 0;
017ff1
+	}
017ff1
+
017ff1
 	verbose("Starting session: %s%s%s for %s from %.200s port %d",
017ff1
 	    session_type,
017ff1
 	    tty == NULL ? "" : " on ",
017ff1
@@ -1681,14 +1688,6 @@ child_close_fds(void)
017ff1
 	 * descriptors left by system functions.  They will be closed later.
017ff1
 	 */
017ff1
 	endpwent();
017ff1
-
017ff1
-	/*
017ff1
-	 * Close any extra open file descriptors so that we don't have them
017ff1
-	 * hanging around in clients.  Note that we want to do this after
017ff1
-	 * initgroups, because at least on Solaris 2.3 it leaves file
017ff1
-	 * descriptors open.
017ff1
-	 */
017ff1
-	closefrom(STDERR_FILENO + 1);
017ff1
 }
017ff1
 
017ff1
 /*
017ff1
@@ -1834,8 +1833,6 @@ do_child(Session *s, const char *command)
017ff1
 			exit(1);
017ff1
 	}
017ff1
 
017ff1
-	closefrom(STDERR_FILENO + 1);
017ff1
-
017ff1
 	if (!options.use_login)
017ff1
 		do_rc_files(s, shell);
017ff1
 
017ff1
@@ -1859,9 +1856,17 @@ do_child(Session *s, const char *command)
017ff1
 		argv[i] = NULL;
017ff1
 		optind = optreset = 1;
017ff1
 		__progname = argv[0];
017ff1
-		exit(sftp_server_main(i, argv, s->pw));
017ff1
+		exit(sftp_server_main(i, argv, s->pw, have_dev_log));
017ff1
 	}
017ff1
 
017ff1
+	/*
017ff1
+	 * Close any extra open file descriptors so that we don't have them
017ff1
+	 * hanging around in clients.  Note that we want to do this after
017ff1
+	 * initgroups, because at least on Solaris 2.3 it leaves file
017ff1
+	 * descriptors open.
017ff1
+	 */
017ff1
+	closefrom(STDERR_FILENO + 1);
017ff1
+
017ff1
 	fflush(NULL);
017ff1
 
017ff1
 	if (options.use_login) {
017ff1
diff --git a/sftp-server-main.c b/sftp-server-main.c
017ff1
index 7e644ab..e162b7a 100644
017ff1
--- a/sftp-server-main.c
017ff1
+++ b/sftp-server-main.c
017ff1
@@ -47,5 +47,5 @@ main(int argc, char **argv)
017ff1
 		return 1;
017ff1
 	}
017ff1
 
017ff1
-	return (sftp_server_main(argc, argv, user_pw));
017ff1
+	return (sftp_server_main(argc, argv, user_pw, 0));
017ff1
 }
017ff1
diff --git a/sftp-server.c b/sftp-server.c
017ff1
index b8eb59c..a0e644c 100644
017ff1
--- a/sftp-server.c
017ff1
+++ b/sftp-server.c
017ff1
@@ -1437,7 +1437,7 @@ sftp_server_usage(void)
017ff1
 }
017ff1
 
017ff1
 int
017ff1
-sftp_server_main(int argc, char **argv, struct passwd *user_pw)
017ff1
+sftp_server_main(int argc, char **argv, struct passwd *user_pw, int reset_handler)
017ff1
 {
017ff1
 	fd_set *rset, *wset;
017ff1
 	int i, in, out, max, ch, skipargs = 0, log_stderr = 0;
017ff1
@@ -1450,7 +1450,7 @@ sftp_server_main(int argc, char **argv, struct passwd *user_pw)
017ff1
 	extern char *__progname;
017ff1
 
017ff1
 	__progname = ssh_get_progname(argv[0]);
017ff1
-	log_init(__progname, log_level, log_facility, log_stderr);
017ff1
+	log_init_handler(__progname, log_level, log_facility, log_stderr, reset_handler);
017ff1
 
017ff1
 	pw = pwcopy(user_pw);
017ff1
 
017ff1
@@ -1521,7 +1521,7 @@ sftp_server_main(int argc, char **argv, struct passwd *user_pw)
017ff1
 		}
017ff1
 	}
017ff1
 
017ff1
-	log_init(__progname, log_level, log_facility, log_stderr);
017ff1
+	log_init_handler(__progname, log_level, log_facility, log_stderr, reset_handler);
017ff1
 
017ff1
 	if ((cp = getenv("SSH_CONNECTION")) != NULL) {
017ff1
 		client_addr = xstrdup(cp);
017ff1
diff --git a/sftp.h b/sftp.h
017ff1
index 2bde8bb..ddf1a39 100644
017ff1
--- a/sftp.h
017ff1
+++ b/sftp.h
017ff1
@@ -97,5 +97,5 @@
017ff1
 
017ff1
 struct passwd;
017ff1
 
017ff1
-int	sftp_server_main(int, char **, struct passwd *);
017ff1
+int	sftp_server_main(int, char **, struct passwd *, int);
017ff1
 void	sftp_server_cleanup_exit(int) __attribute__((noreturn));
017ff1
diff --git a/sshd.c b/sshd.c
017ff1
index 3eee75a..9c00bcb 100644
017ff1
--- a/sshd.c
017ff1
+++ b/sshd.c
017ff1
@@ -745,7 +745,7 @@ privsep_postauth(Authctxt *authctxt)
017ff1
 	}
017ff1
 
017ff1
 	/* New socket pair */
017ff1
-	monitor_reinit(pmonitor);
017ff1
+	monitor_reinit(pmonitor, options.chroot_directory);
017ff1
 
017ff1
 	pmonitor->m_pid = fork();
017ff1
 	if (pmonitor->m_pid == -1)
017ff1
@@ -763,6 +763,11 @@ privsep_postauth(Authctxt *authctxt)
017ff1
 
017ff1
 	close(pmonitor->m_sendfd);
017ff1
 	pmonitor->m_sendfd = -1;
017ff1
+	close(pmonitor->m_log_recvfd);
017ff1
+	pmonitor->m_log_recvfd = -1;
017ff1
+
017ff1
+	if (pmonitor->m_log_sendfd != -1)
017ff1
+		set_log_handler(mm_log_handler, pmonitor);
017ff1
 
017ff1
 	/* Demote the private keys to public keys. */
017ff1
 	demote_sensitive_data();