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