Jan F 8fe1509
diff -up openssh-5.8p1/session.c.sftpcontext openssh-5.8p1/session.c
Jan F 8fe1509
--- openssh-5.8p1/session.c.sftpcontext	2011-03-17 06:20:41.651773603 +0100
Jan F 8fe1509
+++ openssh-5.8p1/session.c	2011-03-17 06:39:11.947648737 +0100
Jan F 8fe1509
@@ -1479,12 +1479,21 @@ safely_chroot(const char *path, uid_t ui
Jan F 8fe1509
 
Jan F 8fe1509
 /* Set login name, uid, gid, and groups. */
Jan F 8fe1509
 void
Jan F 8fe1509
+#ifdef WITH_SELINUX
Jan F 8fe1509
+do_setusercontext(struct passwd *pw, const char *context)
Jan F 8fe1509
+#else
Jan F 8fe1509
 do_setusercontext(struct passwd *pw)
Jan F 8fe1509
+#endif
Jan F 8fe1509
 {
Jan F 8fe1509
 	char *chroot_path, *tmp;
Jan F 8fe1509
 
Jan F 8fe1509
 	platform_setusercontext(pw);
Jan F 8fe1509
 
Jan F 8fe1509
+#ifdef WITH_SELINUX
Jan F 8fe1509
+	if (context)
Jan F 8fe1509
+		ssh_selinux_change_context(context);
Jan F 8fe1509
+#endif
Jan F 8fe1509
+
Jan F 8fe1509
 	if (platform_privileged_uidswap()) {
Jan F 8fe1509
 #ifdef HAVE_LOGIN_CAP
Jan F 8fe1509
 		if (setusercontext(lc, pw, pw->pw_uid,
Jan F 8fe1509
@@ -1633,7 +1642,11 @@ do_child(Session *s, const char *command
Jan F 8fe1509
 
Jan F 8fe1509
 	/* Force a password change */
Jan F 8fe1509
 	if (s->authctxt->force_pwchange) {
Jan F 8fe1509
+#ifdef WITH_SELINUX
Jan F 8fe1509
+		do_setusercontext(pw, NULL); //should be passwd_t context set here?
Jan F 8fe1509
+#else
Jan F 8fe1509
 		do_setusercontext(pw);
Jan F 8fe1509
+#endif
Jan F 8fe1509
 		child_close_fds();
Jan F 8fe1509
 		do_pwchange(s);
Jan F 8fe1509
 		exit(1);
Jan F 8fe1509
@@ -1660,7 +1673,11 @@ do_child(Session *s, const char *command
Jan F 8fe1509
 		/* When PAM is enabled we rely on it to do the nologin check */
Jan F 8fe1509
 		if (!options.use_pam)
Jan F 8fe1509
 			do_nologin(pw);
Jan F 8fe1509
+#ifdef WITH_SELINUX
Jan F 8fe1509
+		do_setusercontext(pw, s->is_subsystem == SUBSYSTEM_INT_SFTP ? "sftpd_t" : NULL);
Jan F 8fe1509
+#else
Jan F 8fe1509
 		do_setusercontext(pw);
Jan F 8fe1509
+#endif
Jan F 8fe1509
 		/*
Jan F 8fe1509
 		 * PAM session modules in do_setusercontext may have
Jan F 8fe1509
 		 * generated messages, so if this in an interactive
Jan F 8fe1509
@@ -1780,9 +1797,6 @@ do_child(Session *s, const char *command
Jan F 8fe1509
 		argv[i] = NULL;
Jan F 8fe1509
 		optind = optreset = 1;
Jan F 8fe1509
 		__progname = argv[0];
Jan F 8fe1509
-#ifdef WITH_SELINUX
Jan F 8fe1509
-		ssh_selinux_change_context("sftpd_t");
Jan F 8fe1509
-#endif
Jan F 8fe1509
 		exit(sftp_server_main(i, argv, s->pw));
Jan F 8fe1509
 	}
Jan F 8fe1509
 
Jan F 8fe1509
diff -up openssh-5.8p1/session.h.sftpcontext openssh-5.8p1/session.h
Jan F 8fe1509
--- openssh-5.8p1/session.h.sftpcontext	2011-03-17 06:38:15.287648531 +0100
Jan F 8fe1509
+++ openssh-5.8p1/session.h	2011-03-17 06:40:25.907648653 +0100
Jan F 8fe1509
@@ -84,7 +84,11 @@ Session	*session_new(void);
Jan F 8fe1509
 Session *session_by_id(int);
Jan F 8fe1509
 Session	*session_by_tty(char *);
Jan F 8fe1509
 void	 session_close(Session *);
Jan F 8fe1509
+#ifdef WITH_SELINUX
Jan F 8fe1509
+void	 do_setusercontext(struct passwd *, const char *);
Jan F 8fe1509
+#else
Jan F 8fe1509
 void	 do_setusercontext(struct passwd *);
Jan F 8fe1509
+#endif
Jan F 8fe1509
 void	 child_set_env(char ***envp, u_int *envsizep, const char *name,
Jan F 8fe1509
 		       const char *value);
Jan F 8fe1509
 
Jan F 8fe1509
diff -up openssh-5.8p1/sshd.c.sftpcontext openssh-5.8p1/sshd.c
Jan F 8fe1509
--- openssh-5.8p1/sshd.c.sftpcontext	2011-03-17 06:40:55.961663207 +0100
Jan F 8fe1509
+++ openssh-5.8p1/sshd.c	2011-03-17 06:45:14.037735849 +0100
Jan F 8fe1509
@@ -772,7 +772,11 @@ privsep_postauth(Authctxt *authctxt)
Jan F 8fe1509
 	RAND_seed(rnd, sizeof(rnd));
Jan F 8fe1509
 
Jan F 8fe1509
 	/* Drop privileges */
Jan F 8fe1509
-	do_setusercontext(authctxt->pw);
Jan F 8fe1509
+#ifdef WITH_SELINUX
Jan F 8fe1509
+	do_setusercontext(authctxt->pw, NULL);
Jan F 8fe1509
+#else
Jan F 8fe1509
+	do_setusercontext(authctxt->pw);
Jan F 8fe1509
+#endif
Jan F 8fe1509
 
Jan F 8fe1509
  skip:
Jan F 8fe1509
 	/* It is safe now to apply the key state */