vishalmishra434 / rpms / openssh

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