bmh10 / rpms / openssh

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