kentpeacock / rpms / openssh

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