5b55d09
diff -up openssh/auth2-pubkey.c.refactor openssh/auth2-pubkey.c
5b55d09
--- openssh/auth2-pubkey.c.refactor	2017-09-27 13:10:19.556830609 +0200
5b55d09
+++ openssh/auth2-pubkey.c	2017-09-27 13:10:19.677831274 +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)
5b55d09
@@ -432,7 +435,8 @@ match_principals_command(struct passwd *
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;
5b55d09
@@ -762,7 +766,8 @@ user_key_command_allowed2(struct passwd
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
3cd4899
--- openssh/auth.c.refactor	2017-09-27 13:10:19.640831071 +0200
3cd4899
+++ openssh/auth.c	2017-09-27 13:10:19.678831279 +0200
5b55d09
@@ -1435,7 +1435,8 @@ argv_assemble(int argc, char **argv)
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;
5b55d09
@@ -1551,7 +1552,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
3cd4899
--- openssh/auth.h.refactor	2017-09-25 01:48:10.000000000 +0200
3cd4899
+++ openssh/auth.h	2017-09-27 13:10:19.678831279 +0200
5b55d09
@@ -144,7 +144,7 @@ int	 exited_cleanly(pid_t, const char *,
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
5b55d09
--- openssh/openbsd-compat/port-linux.h.refactor	2017-09-27 13:10:19.634831038 +0200
5b55d09
+++ openssh/openbsd-compat/port-linux.h	2017-09-27 13:10:54.954025248 +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
5b55d09
--- openssh/openbsd-compat/port-linux-sshd.c.refactor	2017-09-27 13:10:19.634831038 +0200
5b55d09
+++ openssh/openbsd-compat/port-linux-sshd.c	2017-09-27 13:12:06.811420371 +0200
5b55d09
@@ -48,11 +48,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)
5b55d09
@@ -222,7 +217,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;
5b55d09
@@ -240,8 +236,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;
5b55d09
@@ -249,7 +245,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) {
5b55d09
@@ -271,7 +267,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)
5b55d09
@@ -332,7 +328,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;
5b55d09
@@ -341,11 +338,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 = "";
5b55d09
@@ -361,9 +358,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
5b55d09
@@ -373,25 +371,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__);
5b55d09
@@ -409,7 +410,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
5b55d09
--- openssh/platform.c.refactor	2017-09-27 13:10:19.574830708 +0200
5b55d09
+++ openssh/platform.c	2017-09-27 13:11:45.475303050 +0200
5b55d09
@@ -33,6 +33,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)
5b55d09
@@ -184,7 +187,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
5b55d09
--- openssh/sshd.c.refactor	2017-09-27 13:10:19.674831257 +0200
5b55d09
+++ openssh/sshd.c	2017-09-27 13:12:01.635391909 +0200
5b55d09
@@ -2135,7 +2135,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) {