kentpeacock / rpms / openssh

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