kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
f5835d
diff -up openssh/auth2-pubkey.c.refactor openssh/auth2-pubkey.c
4369a3
--- openssh/auth2-pubkey.c.refactor	2019-04-04 13:19:12.188821236 +0200
4369a3
+++ openssh/auth2-pubkey.c	2019-04-04 13:19:12.276822078 +0200
f5835d
@@ -72,6 +72,9 @@
f5835d
 extern ServerOptions options;
f5835d
 extern u_char *session_id2;
f5835d
 extern u_int session_id2_len;
f5835d
+extern int inetd_flag;
f5835d
+extern int rexeced_flag;
f5835d
+extern Authctxt *the_authctxt;
f5835d
 
f5835d
 static char *
f5835d
 format_key(const struct sshkey *key)
4369a3
@@ -511,7 +514,8 @@ match_principals_command(struct ssh *ssh
f5835d
 
f5835d
 	if ((pid = subprocess("AuthorizedPrincipalsCommand", runas_pw, command,
f5835d
 	    ac, av, &f,
f5835d
-	    SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
f5835d
+	    SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
f5835d
+	    (inetd_flag && !rexeced_flag), the_authctxt)) == 0)
f5835d
 		goto out;
f5835d
 
f5835d
 	uid_swapped = 1;
4369a3
@@ -981,7 +985,8 @@ user_key_command_allowed2(struct ssh *ss
f5835d
 
f5835d
 	if ((pid = subprocess("AuthorizedKeysCommand", runas_pw, command,
f5835d
 	    ac, av, &f,
f5835d
-	    SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
f5835d
+	    SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
f5835d
+	    (inetd_flag && !rexeced_flag), the_authctxt)) == 0)
f5835d
 		goto out;
f5835d
 
f5835d
 	uid_swapped = 1;
f5835d
diff -up openssh/auth.c.refactor openssh/auth.c
4369a3
--- openssh/auth.c.refactor	2019-04-04 13:19:12.235821686 +0200
4369a3
+++ openssh/auth.c	2019-04-04 13:19:12.276822078 +0200
4369a3
@@ -756,7 +756,8 @@ auth_get_canonical_hostname(struct ssh *
f5835d
  */
f5835d
 pid_t
f5835d
 subprocess(const char *tag, struct passwd *pw, const char *command,
f5835d
-    int ac, char **av, FILE **child, u_int flags)
f5835d
+    int ac, char **av, FILE **child, u_int flags, int inetd,
f5835d
+    void *the_authctxt)
f5835d
 {
f5835d
 	FILE *f = NULL;
f5835d
 	struct stat st;
4369a3
@@ -872,7 +873,7 @@ subprocess(const char *tag, struct passw
f5835d
 		}
f5835d
 
f5835d
 #ifdef WITH_SELINUX
f5835d
-		if (sshd_selinux_setup_env_variables() < 0) {
f5835d
+		if (sshd_selinux_setup_env_variables(inetd, the_authctxt) < 0) {
f5835d
 			error ("failed to copy environment:  %s",
f5835d
 			    strerror(errno));
f5835d
 			_exit(127);
f5835d
diff -up openssh/auth.h.refactor openssh/auth.h
4369a3
--- openssh/auth.h.refactor	2019-04-04 13:19:12.251821839 +0200
4369a3
+++ openssh/auth.h	2019-04-04 13:19:12.276822078 +0200
4369a3
@@ -235,7 +235,7 @@ struct passwd *fakepw(void);
f5835d
 #define	SSH_SUBPROCESS_STDOUT_CAPTURE  (1<<1)  /* Redirect stdout */
f5835d
 #define	SSH_SUBPROCESS_STDERR_DISCARD  (1<<2)  /* Discard stderr */
f5835d
 pid_t	subprocess(const char *, struct passwd *,
f5835d
-    const char *, int, char **, FILE **, u_int flags);
f5835d
+    const char *, int, char **, FILE **, u_int flags, int, void *);
f5835d
 
f5835d
 int	 sys_auth_passwd(struct ssh *, const char *);
f5835d
 
f5835d
diff -up openssh/openbsd-compat/port-linux.h.refactor openssh/openbsd-compat/port-linux.h
4369a3
--- openssh/openbsd-compat/port-linux.h.refactor	2019-04-04 13:19:12.256821887 +0200
4369a3
+++ openssh/openbsd-compat/port-linux.h	2019-04-04 13:19:12.276822078 +0200
f5835d
@@ -26,8 +26,8 @@ void ssh_selinux_setfscreatecon(const ch
f5835d
 
f5835d
 int sshd_selinux_enabled(void);
f5835d
 void sshd_selinux_copy_context(void);
f5835d
-void sshd_selinux_setup_exec_context(char *);
f5835d
-int sshd_selinux_setup_env_variables(void);
f5835d
+void sshd_selinux_setup_exec_context(char *, int, int(char *, const char *), void *, int);
f5835d
+int sshd_selinux_setup_env_variables(int inetd, void *);
f5835d
 void sshd_selinux_change_privsep_preauth_context(void);
f5835d
 #endif
f5835d
 
f5835d
diff -up openssh/openbsd-compat/port-linux-sshd.c.refactor openssh/openbsd-compat/port-linux-sshd.c
4369a3
--- openssh/openbsd-compat/port-linux-sshd.c.refactor	2019-04-04 13:19:12.256821887 +0200
4369a3
+++ openssh/openbsd-compat/port-linux-sshd.c	2019-04-04 13:19:12.276822078 +0200
4369a3
@@ -49,11 +49,6 @@
f5835d
 #include <unistd.h>
f5835d
 #endif
f5835d
 
f5835d
-extern ServerOptions options;
f5835d
-extern Authctxt *the_authctxt;
f5835d
-extern int inetd_flag;
f5835d
-extern int rexeced_flag;
f5835d
-
f5835d
 /* Wrapper around is_selinux_enabled() to log its return value once only */
f5835d
 int
f5835d
 sshd_selinux_enabled(void)
4369a3
@@ -223,7 +218,8 @@ get_user_context(const char *sename, con
f5835d
 }
f5835d
 
f5835d
 static void
f5835d
-ssh_selinux_get_role_level(char **role, const char **level)
f5835d
+ssh_selinux_get_role_level(char **role, const char **level,
f5835d
+    Authctxt *the_authctxt)
f5835d
 {
f5835d
 	*role = NULL;
f5835d
 	*level = NULL;
4369a3
@@ -241,8 +237,8 @@ ssh_selinux_get_role_level(char **role,
f5835d
 
f5835d
 /* Return the default security context for the given username */
f5835d
 static int
f5835d
-sshd_selinux_getctxbyname(char *pwname,
f5835d
-	security_context_t *default_sc, security_context_t *user_sc)
f5835d
+sshd_selinux_getctxbyname(char *pwname, security_context_t *default_sc,
f5835d
+    security_context_t *user_sc, int inetd, Authctxt *the_authctxt)
f5835d
 {
f5835d
 	char *sename, *lvl;
f5835d
 	char *role;
4369a3
@@ -250,7 +246,7 @@ sshd_selinux_getctxbyname(char *pwname,
f5835d
 	int r = 0;
f5835d
 	context_t con = NULL;
f5835d
 
f5835d
-	ssh_selinux_get_role_level(&role, &reqlvl);
f5835d
+	ssh_selinux_get_role_level(&role, &reqlvl, the_authctxt);
f5835d
 
f5835d
 #ifdef HAVE_GETSEUSERBYNAME
f5835d
 	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
4369a3
@@ -272,7 +268,7 @@ sshd_selinux_getctxbyname(char *pwname,
f5835d
 
f5835d
 	if (r == 0) {
f5835d
 		/* If launched from xinetd, we must use current level */
f5835d
-		if (inetd_flag && !rexeced_flag) {
f5835d
+		if (inetd) {
f5835d
 			security_context_t sshdsc=NULL;
f5835d
 
f5835d
 			if (getcon_raw(&sshdsc) < 0)
4369a3
@@ -333,7 +329,8 @@ sshd_selinux_getctxbyname(char *pwname,
f5835d
 
f5835d
 /* Setup environment variables for pam_selinux */
f5835d
 static int
f5835d
-sshd_selinux_setup_variables(int(*set_it)(char *, const char *))
f5835d
+sshd_selinux_setup_variables(int(*set_it)(char *, const char *), int inetd,
f5835d
+    Authctxt *the_authctxt)
f5835d
 {
f5835d
 	const char *reqlvl;
f5835d
 	char *role;
4369a3
@@ -342,11 +339,11 @@ sshd_selinux_setup_variables(int(*set_it
f5835d
 
f5835d
 	debug3("%s: setting execution context", __func__);
f5835d
 
f5835d
-	ssh_selinux_get_role_level(&role, &reqlvl);
f5835d
+	ssh_selinux_get_role_level(&role, &reqlvl, the_authctxt);
f5835d
 
f5835d
 	rv = set_it("SELINUX_ROLE_REQUESTED", role ? role : "");
f5835d
 
f5835d
-	if (inetd_flag && !rexeced_flag) {
f5835d
+	if (inetd) {
f5835d
 		use_current = "1";
f5835d
 	} else {
f5835d
 		use_current = "";
4369a3
@@ -362,9 +359,10 @@ sshd_selinux_setup_variables(int(*set_it
f5835d
 }
f5835d
 
f5835d
 static int
f5835d
-sshd_selinux_setup_pam_variables(void)
f5835d
+sshd_selinux_setup_pam_variables(int inetd,
f5835d
+    int(pam_setenv)(char *, const char *), Authctxt *the_authctxt)
f5835d
 {
f5835d
-	return sshd_selinux_setup_variables(do_pam_putenv);
f5835d
+	return sshd_selinux_setup_variables(pam_setenv, inetd, the_authctxt);
f5835d
 }
f5835d
 
f5835d
 static int
4369a3
@@ -374,25 +372,28 @@ do_setenv(char *name, const char *value)
f5835d
 }
f5835d
 
f5835d
 int
f5835d
-sshd_selinux_setup_env_variables(void)
f5835d
+sshd_selinux_setup_env_variables(int inetd, void *the_authctxt)
f5835d
 {
f5835d
-	return sshd_selinux_setup_variables(do_setenv);
f5835d
+	Authctxt *authctxt = (Authctxt *) the_authctxt;
f5835d
+	return sshd_selinux_setup_variables(do_setenv, inetd, authctxt);
f5835d
 }
f5835d
 
f5835d
 /* Set the execution context to the default for the specified user */
f5835d
 void
f5835d
-sshd_selinux_setup_exec_context(char *pwname)
f5835d
+sshd_selinux_setup_exec_context(char *pwname, int inetd,
f5835d
+    int(pam_setenv)(char *, const char *), void *the_authctxt, int use_pam)
f5835d
 {
f5835d
 	security_context_t user_ctx = NULL;
f5835d
 	int r = 0;
f5835d
 	security_context_t default_ctx = NULL;
f5835d
+	Authctxt *authctxt = (Authctxt *) the_authctxt;
f5835d
 
f5835d
 	if (!sshd_selinux_enabled())
f5835d
 		return;
f5835d
 
f5835d
-	if (options.use_pam) {
f5835d
+	if (use_pam) {
f5835d
 		/* do not compute context, just setup environment for pam_selinux */
f5835d
-		if (sshd_selinux_setup_pam_variables()) {
f5835d
+		if (sshd_selinux_setup_pam_variables(inetd, pam_setenv, authctxt)) {
f5835d
 			switch (security_getenforce()) {
f5835d
 			case -1:
f5835d
 				fatal("%s: security_getenforce() failed", __func__);
4369a3
@@ -410,7 +411,7 @@ sshd_selinux_setup_exec_context(char *pw
f5835d
 
f5835d
 	debug3("%s: setting execution context", __func__);
f5835d
 
f5835d
-	r = sshd_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);
f5835d
+	r = sshd_selinux_getctxbyname(pwname, &default_ctx, &user_ctx, inetd, authctxt);
f5835d
 	if (r >= 0) {
f5835d
 		r = setexeccon(user_ctx);
f5835d
 		if (r < 0) {
f5835d
diff -up openssh/platform.c.refactor openssh/platform.c
4369a3
--- openssh/platform.c.refactor	2019-04-04 13:19:12.204821389 +0200
4369a3
+++ openssh/platform.c	2019-04-04 13:19:12.277822088 +0200
4369a3
@@ -32,6 +32,9 @@
f5835d
 
f5835d
 extern int use_privsep;
f5835d
 extern ServerOptions options;
f5835d
+extern int inetd_flag;
f5835d
+extern int rexeced_flag;
f5835d
+extern Authctxt *the_authctxt;
f5835d
 
f5835d
 void
f5835d
 platform_pre_listen(void)
4369a3
@@ -183,7 +186,9 @@ platform_setusercontext_post_groups(stru
f5835d
 	}
f5835d
 #endif /* HAVE_SETPCRED */
f5835d
 #ifdef WITH_SELINUX
f5835d
-	sshd_selinux_setup_exec_context(pw->pw_name);
f5835d
+	sshd_selinux_setup_exec_context(pw->pw_name,
f5835d
+	    (inetd_flag && !rexeced_flag), do_pam_putenv, the_authctxt,
f5835d
+	    options.use_pam);
f5835d
 #endif
f5835d
 }
f5835d
 
f5835d
diff -up openssh/sshd.c.refactor openssh/sshd.c
4369a3
--- openssh/sshd.c.refactor	2019-04-04 13:19:12.275822068 +0200
4369a3
+++ openssh/sshd.c	2019-04-04 13:19:51.270195262 +0200
4369a3
@@ -158,7 +158,7 @@ int debug_flag = 0;
4369a3
 static int test_flag = 0;
4369a3
 
4369a3
 /* Flag indicating that the daemon is being started from inetd. */
4369a3
-static int inetd_flag = 0;
4369a3
+int inetd_flag = 0;
4369a3
 
4369a3
 /* Flag indicating that sshd should not detach and become a daemon. */
4369a3
 static int no_daemon_flag = 0;
4369a3
@@ -171,7 +171,7 @@ static char **saved_argv;
4369a3
 static int saved_argc;
4369a3
 
4369a3
 /* re-exec */
4369a3
-static int rexeced_flag = 0;
4369a3
+int rexeced_flag = 0;
4369a3
 static int rexec_flag = 1;
4369a3
 static int rexec_argc = 0;
4369a3
 static char **rexec_argv;
4369a3
@@ -2192,7 +2192,9 @@ main(int ac, char **av)
f5835d
 	}
f5835d
 #endif
f5835d
 #ifdef WITH_SELINUX
f5835d
-	sshd_selinux_setup_exec_context(authctxt->pw->pw_name);
f5835d
+	sshd_selinux_setup_exec_context(authctxt->pw->pw_name,
f5835d
+	    (inetd_flag && !rexeced_flag), do_pam_putenv, the_authctxt,
f5835d
+	    options.use_pam);
f5835d
 #endif
f5835d
 #ifdef USE_PAM
f5835d
 	if (options.use_pam) {