0259fb8
diff -up openssh-5.0p1/auth-pam.h.pam_selinux openssh-5.0p1/auth-pam.h
0259fb8
--- openssh-5.0p1/auth-pam.h.pam_selinux	2004-09-11 14:17:26.000000000 +0200
0259fb8
+++ openssh-5.0p1/auth-pam.h	2008-04-30 14:25:28.000000000 +0200
0259fb8
@@ -38,7 +38,7 @@ void do_pam_session(void);
0259fb8
 void do_pam_set_tty(const char *);
0259fb8
 void do_pam_setcred(int );
0259fb8
 void do_pam_chauthtok(void);
0259fb8
-int do_pam_putenv(char *, char *);
0259fb8
+int do_pam_putenv(char *, const char *);
0259fb8
 char ** fetch_pam_environment(void);
0259fb8
 char ** fetch_pam_child_environment(void);
0259fb8
 void free_pam_environment(char **);
0259fb8
diff -up openssh-5.0p1/auth-pam.c.pam_selinux openssh-5.0p1/auth-pam.c
0259fb8
--- openssh-5.0p1/auth-pam.c.pam_selinux	2008-03-11 12:58:25.000000000 +0100
0259fb8
+++ openssh-5.0p1/auth-pam.c	2008-04-30 14:25:21.000000000 +0200
0259fb8
@@ -1069,7 +1069,7 @@ is_pam_session_open(void)
0259fb8
  * during the ssh authentication process.
0259fb8
  */
0259fb8
 int
0259fb8
-do_pam_putenv(char *name, char *value)
0259fb8
+do_pam_putenv(char *name, const char *value)
0259fb8
 {
0259fb8
 	int ret = 1;
0259fb8
 #ifdef HAVE_PAM_PUTENV
0259fb8
diff -up openssh-5.0p1/openbsd-compat/port-linux.c.pam_selinux openssh-5.0p1/openbsd-compat/port-linux.c
0259fb8
--- openssh-5.0p1/openbsd-compat/port-linux.c.pam_selinux	2008-04-07 22:01:37.000000000 +0200
0259fb8
+++ openssh-5.0p1/openbsd-compat/port-linux.c	2008-04-30 14:26:17.000000000 +0200
0259fb8
@@ -34,6 +34,7 @@
0259fb8
 #include "hostfile.h"
0259fb8
 #include "auth.h"
0259fb8
 #include "xmalloc.h"
0259fb8
+#include "servconf.h"
0259fb8
 
0259fb8
 #include <selinux/selinux.h>
0259fb8
 #include <selinux/flask.h>
0259fb8
@@ -47,6 +48,7 @@
0259fb8
 #include <unistd.h>
0259fb8
 #endif
0259fb8
 
0259fb8
+extern ServerOptions options;
0259fb8
 extern Authctxt *the_authctxt;
0259fb8
 extern int inetd_flag;
0259fb8
 extern int rexeced_flag;
0259fb8
@@ -208,29 +210,38 @@ get_user_context(const char *sename, con
0259fb8
         return -1;
0259fb8
 }
0259fb8
 
0259fb8
+static void
0259fb8
+ssh_selinux_get_role_level(char **role, const char **level)
0259fb8
+{
0259fb8
+	*role = NULL;
0259fb8
+	*level = NULL;
0259fb8
+	if (the_authctxt) {
0259fb8
+		if (the_authctxt->role != NULL) {
0259fb8
+			char *slash;
0259fb8
+			*role = xstrdup(the_authctxt->role);
0259fb8
+			if ((slash = strchr(*role, '/')) != NULL) {
0259fb8
+				*slash = '\0';
0259fb8
+				*level = slash + 1;
0259fb8
+			}
0259fb8
+		}
0259fb8
+	}
0259fb8
+}
0259fb8
+
0259fb8
 /* Return the default security context for the given username */
0259fb8
 static int
0259fb8
 ssh_selinux_getctxbyname(char *pwname,
0259fb8
 	security_context_t *default_sc, security_context_t *user_sc)
0259fb8
 {
0259fb8
 	char *sename, *lvl;
0259fb8
-	const char *reqlvl = NULL;
0259fb8
-	char *role = NULL;
0259fb8
+	const char *reqlvl;
0259fb8
+	char *role;
0259fb8
 	int r = -1;
0259fb8
 	context_t con = NULL;
0259fb8
 
0259fb8
 	*default_sc = NULL;
0259fb8
 	*user_sc = NULL;
0259fb8
-	if (the_authctxt) {
0259fb8
-		if (the_authctxt->role != NULL) {
0259fb8
-			char *slash;
0259fb8
-			role = xstrdup(the_authctxt->role);
0259fb8
-			if ((slash = strchr(role, '/')) != NULL) {
0259fb8
-				*slash = '\0';
0259fb8
-				reqlvl = slash + 1;
0259fb8
-			}
0259fb8
-		}
0259fb8
-	}
0259fb8
+
0259fb8
+	ssh_selinux_get_role_level(&role, &reqlvl);
0259fb8
 
0259fb8
 #ifdef HAVE_GETSEUSERBYNAME
0259fb8
 	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
0259fb8
@@ -311,6 +322,36 @@ ssh_selinux_getctxbyname(char *pwname,
0259fb8
 	return (r);
0259fb8
 }
0259fb8
 
0259fb8
+/* Setup environment variables for pam_selinux */
0259fb8
+static int
0259fb8
+ssh_selinux_setup_pam_variables(void)
0259fb8
+{
0259fb8
+	const char *reqlvl;
0259fb8
+	char *role;
0259fb8
+	char *use_current;
0259fb8
+	int rv;
0259fb8
+
0259fb8
+	debug3("%s: setting execution context", __func__);
0259fb8
+
0259fb8
+	ssh_selinux_get_role_level(&role, &reqlvl);
0259fb8
+
0259fb8
+	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
0259fb8
+	
0259fb8
+	if (inetd_flag && !rexeced_flag) {
0259fb8
+		use_current = "1";
0259fb8
+	} else {
0259fb8
+		use_current = "";
0259fb8
+		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
0259fb8
+	}
0259fb8
+
0259fb8
+	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
0259fb8
+
0259fb8
+	if (role != NULL)
0259fb8
+		xfree(role);
0259fb8
+	
0259fb8
+	return rv;
0259fb8
+}
0259fb8
+
0259fb8
 /* Set the execution context to the default for the specified user */
0259fb8
 void
0259fb8
 ssh_selinux_setup_exec_context(char *pwname)
0259fb8
@@ -322,6 +363,24 @@ ssh_selinux_setup_exec_context(char *pwn
0259fb8
 	if (!ssh_selinux_enabled())
0259fb8
 		return;
0259fb8
 
0259fb8
+	if (options.use_pam) {
0259fb8
+		/* do not compute context, just setup environment for pam_selinux */
0259fb8
+		if (ssh_selinux_setup_pam_variables()) {
0259fb8
+			switch (security_getenforce()) {
0259fb8
+			case -1:
0259fb8
+				fatal("%s: security_getenforce() failed", __func__);
0259fb8
+			case 0:
0259fb8
+				error("%s: SELinux PAM variable setup failure. Continuing in permissive mode.",
0259fb8
+				    __func__);
0259fb8
+			break;
0259fb8
+			default:
0259fb8
+				fatal("%s: SELinux PAM variable setup failure. Aborting connection.",
0259fb8
+				    __func__);
0259fb8
+			}
0259fb8
+		}
0259fb8
+		return;
0259fb8
+	}
0259fb8
+
0259fb8
 	debug3("%s: setting execution context", __func__);
0259fb8
 
0259fb8
 	r = ssh_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);