Jan F 003cb0
diff -up openssh-5.8p1/auth1.c.role openssh-5.8p1/auth1.c
Jan F 003cb0
--- openssh-5.8p1/auth1.c.role	2010-08-31 14:36:39.000000000 +0200
Jan F 3657ad
+++ openssh-5.8p1/auth1.c	2011-03-22 18:32:17.907648664 +0100
974c89
@@ -384,6 +384,9 @@ do_authentication(Authctxt *authctxt)
a3ba41
 {
a3ba41
 	u_int ulen;
ce94da
 	char *user, *style = NULL;
ce94da
+#ifdef WITH_SELINUX
ce94da
+	char *role=NULL;
ce94da
+#endif
a3ba41
 
a3ba41
 	/* Get the name of the user that we wish to log in as. */
a3ba41
 	packet_read_expect(SSH_CMSG_USER);
Jan F 003cb0
@@ -392,11 +395,24 @@ do_authentication(Authctxt *authctxt)
Jan F 003cb0
 	user = packet_get_cstring(&ulen);
a3ba41
 	packet_check_eom();
a3ba41
 
ce94da
+#ifdef WITH_SELINUX
a3ba41
+	if ((role = strchr(user, '/')) != NULL)
a3ba41
+		*role++ = '\0';
ce94da
+#endif
a3ba41
+
a3ba41
 	if ((style = strchr(user, ':')) != NULL)
a3ba41
 		*style++ = '\0';
ce94da
+#ifdef WITH_SELINUX
a3ba41
+	else
a3ba41
+		if (role && (style = strchr(role, ':')) != NULL)
a3ba41
+			*style++ = '\0';
ce94da
+#endif
a3ba41
 
a3ba41
 	authctxt->user = user;
a3ba41
 	authctxt->style = style;
ce94da
+#ifdef WITH_SELINUX
a3ba41
+	authctxt->role = role;
ce94da
+#endif
a3ba41
 
a3ba41
 	/* Verify that the user is a valid user. */
a3ba41
 	if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Jan F 003cb0
diff -up openssh-5.8p1/auth2.c.role openssh-5.8p1/auth2.c
Jan F 3657ad
--- openssh-5.8p1/auth2.c.role	2011-03-22 18:32:10.876648814 +0100
Jan F 3657ad
+++ openssh-5.8p1/auth2.c	2011-03-22 18:32:17.959648657 +0100
ce94da
@@ -216,6 +216,9 @@ input_userauth_request(int type, u_int32
ce94da
 	Authctxt *authctxt = ctxt;
ce94da
 	Authmethod *m = NULL;
ce94da
 	char *user, *service, *method, *style = NULL;
ce94da
+#ifdef WITH_SELINUX
ce94da
+	char *role = NULL;
ce94da
+#endif
ce94da
 	int authenticated = 0;
ce94da
 
ce94da
 	if (authctxt == NULL)
ce94da
@@ -227,6 +230,11 @@ input_userauth_request(int type, u_int32
ce94da
 	debug("userauth-request for user %s service %s method %s", user, service, method);
ce94da
 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
ce94da
 
ce94da
+#ifdef WITH_SELINUX
ce94da
+	if ((role = strchr(user, '/')) != NULL)
ce94da
+		*role++ = 0;
ce94da
+#endif
ce94da
+
ce94da
 	if ((style = strchr(user, ':')) != NULL)
ce94da
 		*style++ = 0;
ce94da
 
Jan F 3657ad
@@ -249,8 +257,15 @@ input_userauth_request(int type, u_int32
ce94da
 		    use_privsep ? " [net]" : "");
ce94da
 		authctxt->service = xstrdup(service);
ce94da
 		authctxt->style = style ? xstrdup(style) : NULL;
ce94da
-		if (use_privsep)
ce94da
+#ifdef WITH_SELINUX
ce94da
+		authctxt->role = role ? xstrdup(role) : NULL;
ce94da
+#endif
ce94da
+		if (use_privsep) {
ce94da
 			mm_inform_authserv(service, style);
ce94da
+#ifdef WITH_SELINUX
ce94da
+			mm_inform_authrole(role);
ce94da
+#endif
ce94da
+		}
ce94da
 		userauth_banner();
ce94da
 	} else if (strcmp(user, authctxt->user) != 0 ||
ce94da
 	    strcmp(service, authctxt->service) != 0) {
Jan F 003cb0
diff -up openssh-5.8p1/auth2-gss.c.role openssh-5.8p1/auth2-gss.c
Jan F 003cb0
--- openssh-5.8p1/auth2-gss.c.role	2007-12-02 12:59:45.000000000 +0100
Jan F 3657ad
+++ openssh-5.8p1/auth2-gss.c	2011-03-22 18:32:18.009679649 +0100
ce94da
@@ -258,6 +258,7 @@ input_gssapi_mic(int type, u_int32_t ple
ce94da
 	Authctxt *authctxt = ctxt;
ce94da
 	Gssctxt *gssctxt;
ce94da
 	int authenticated = 0;
ce94da
+	char *micuser;
ce94da
 	Buffer b;
ce94da
 	gss_buffer_desc mic, gssbuf;
ce94da
 	u_int len;
ce94da
@@ -270,7 +271,13 @@ input_gssapi_mic(int type, u_int32_t ple
ce94da
 	mic.value = packet_get_string(&len;;
ce94da
 	mic.length = len;
ce94da
 
ce94da
-	ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
ce94da
+#ifdef WITH_SELINUX
ce94da
+	if (authctxt->role && (strlen(authctxt->role) > 0))
ce94da
+		xasprintf(&micuser, "%s/%s", authctxt->user, authctxt->role);
ce94da
+	else
ce94da
+#endif
ce94da
+		micuser = authctxt->user;
ce94da
+	ssh_gssapi_buildmic(&b, micuser, authctxt->service,
ce94da
 	    "gssapi-with-mic");
ce94da
 
ce94da
 	gssbuf.value = buffer_ptr(&b);
ce94da
@@ -282,6 +289,8 @@ input_gssapi_mic(int type, u_int32_t ple
ce94da
 		logit("GSSAPI MIC check failed");
ce94da
 
ce94da
 	buffer_free(&b);
ce94da
+	if (micuser != authctxt->user)
ce94da
+		xfree(micuser);
ce94da
 	xfree(mic.value);
ce94da
 
ce94da
 	authctxt->postponed = 0;
Jan F 003cb0
diff -up openssh-5.8p1/auth2-hostbased.c.role openssh-5.8p1/auth2-hostbased.c
Jan F 3657ad
--- openssh-5.8p1/auth2-hostbased.c.role	2011-03-22 18:32:12.012648649 +0100
Jan F 3657ad
+++ openssh-5.8p1/auth2-hostbased.c	2011-03-22 18:32:18.052648698 +0100
ce94da
@@ -106,7 +106,15 @@ userauth_hostbased(Authctxt *authctxt)
ce94da
 	buffer_put_string(&b, session_id2, session_id2_len);
ce94da
 	/* reconstruct packet */
ce94da
 	buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
ce94da
-	buffer_put_cstring(&b, authctxt->user);
ce94da
+#ifdef WITH_SELINUX
ce94da
+	if (authctxt->role) {
ce94da
+		buffer_put_int(&b, strlen(authctxt->user)+strlen(authctxt->role)+1);
ce94da
+		buffer_append(&b, authctxt->user, strlen(authctxt->user));
ce94da
+		buffer_put_char(&b, '/');
ce94da
+		buffer_append(&b, authctxt->role, strlen(authctxt->role));
ce94da
+	} else 
ce94da
+#endif
ce94da
+		buffer_put_cstring(&b, authctxt->user);
ce94da
 	buffer_put_cstring(&b, service);
ce94da
 	buffer_put_cstring(&b, "hostbased");
ce94da
 	buffer_put_string(&b, pkalg, alen);
Jan F 003cb0
diff -up openssh-5.8p1/auth2-pubkey.c.role openssh-5.8p1/auth2-pubkey.c
Jan F 3657ad
--- openssh-5.8p1/auth2-pubkey.c.role	2011-03-22 18:32:16.318648486 +0100
Jan F 3657ad
+++ openssh-5.8p1/auth2-pubkey.c	2011-03-22 18:32:18.107648652 +0100
Jan F 003cb0
@@ -122,7 +122,15 @@ userauth_pubkey(Authctxt *authctxt)
adad2a
 		}
adad2a
 		/* reconstruct packet */
adad2a
 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
adad2a
-		buffer_put_cstring(&b, authctxt->user);
ce94da
+#ifdef WITH_SELINUX
adad2a
+		if (authctxt->role) {
adad2a
+			buffer_put_int(&b, strlen(authctxt->user)+strlen(authctxt->role)+1);
adad2a
+			buffer_append(&b, authctxt->user, strlen(authctxt->user));
adad2a
+			buffer_put_char(&b, '/');
adad2a
+			buffer_append(&b, authctxt->role, strlen(authctxt->role));
ce94da
+		} else 
ce94da
+#endif
adad2a
+			buffer_put_cstring(&b, authctxt->user);
adad2a
 		buffer_put_cstring(&b,
adad2a
 		    datafellows & SSH_BUG_PKSERVICE ?
adad2a
 		    "ssh-userauth" :
Jan F 003cb0
diff -up openssh-5.8p1/auth.h.role openssh-5.8p1/auth.h
Jan F 3657ad
--- openssh-5.8p1/auth.h.role	2011-03-22 18:32:12.117648630 +0100
Jan F 3657ad
+++ openssh-5.8p1/auth.h	2011-03-22 18:32:18.170648709 +0100
ce94da
@@ -58,6 +58,9 @@ struct Authctxt {
ce94da
 	char		*service;
ce94da
 	struct passwd	*pw;		/* set if 'valid' */
ce94da
 	char		*style;
ce94da
+#ifdef WITH_SELINUX
ce94da
+	char		*role;
ce94da
+#endif
ce94da
 	void		*kbdintctxt;
ce94da
 	void		*jpake_ctx;
ce94da
 #ifdef BSD_AUTH
Jan F 003cb0
diff -up openssh-5.8p1/auth-pam.c.role openssh-5.8p1/auth-pam.c
Jan F 003cb0
--- openssh-5.8p1/auth-pam.c.role	2009-07-12 14:07:21.000000000 +0200
Jan F 3657ad
+++ openssh-5.8p1/auth-pam.c	2011-03-22 18:32:18.230844134 +0100
Jan F 003cb0
@@ -1069,7 +1069,7 @@ is_pam_session_open(void)
Jan F 003cb0
  * during the ssh authentication process.
Jan F 003cb0
  */
Jan F 003cb0
 int
Jan F 003cb0
-do_pam_putenv(char *name, char *value)
Jan F 003cb0
+do_pam_putenv(char *name, const char *value)
Jan F 003cb0
 {
Jan F 003cb0
 	int ret = 1;
Jan F 003cb0
 #ifdef HAVE_PAM_PUTENV
Jan F 003cb0
diff -up openssh-5.8p1/auth-pam.h.role openssh-5.8p1/auth-pam.h
Jan F 003cb0
--- openssh-5.8p1/auth-pam.h.role	2004-09-11 14:17:26.000000000 +0200
Jan F 3657ad
+++ openssh-5.8p1/auth-pam.h	2011-03-22 18:32:18.280648665 +0100
Jan F 003cb0
@@ -38,7 +38,7 @@ void do_pam_session(void);
Jan F 003cb0
 void do_pam_set_tty(const char *);
Jan F 003cb0
 void do_pam_setcred(int );
Jan F 003cb0
 void do_pam_chauthtok(void);
Jan F 003cb0
-int do_pam_putenv(char *, char *);
Jan F 003cb0
+int do_pam_putenv(char *, const char *);
Jan F 003cb0
 char ** fetch_pam_environment(void);
Jan F 003cb0
 char ** fetch_pam_child_environment(void);
Jan F 003cb0
 void free_pam_environment(char **);
Jan F 003cb0
diff -up openssh-5.8p1/monitor.c.role openssh-5.8p1/monitor.c
Jan F 3657ad
--- openssh-5.8p1/monitor.c.role	2011-03-22 18:32:14.465661970 +0100
Jan F 3657ad
+++ openssh-5.8p1/monitor.c	2011-03-22 18:32:18.337831292 +0100
Jan F 3657ad
@@ -140,6 +140,9 @@ int mm_answer_sign(int, Buffer *);
ce94da
 int mm_answer_pwnamallow(int, Buffer *);
ce94da
 int mm_answer_auth2_read_banner(int, Buffer *);
ce94da
 int mm_answer_authserv(int, Buffer *);
ce94da
+#ifdef WITH_SELINUX
ce94da
+int mm_answer_authrole(int, Buffer *);
ce94da
+#endif
ce94da
 int mm_answer_authpassword(int, Buffer *);
ce94da
 int mm_answer_bsdauthquery(int, Buffer *);
ce94da
 int mm_answer_bsdauthrespond(int, Buffer *);
Jan F 3657ad
@@ -221,6 +224,9 @@ struct mon_table mon_dispatch_proto20[] 
ce94da
     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
ce94da
     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
ce94da
     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
ce94da
+#ifdef WITH_SELINUX
ce94da
+    {MONITOR_REQ_AUTHROLE, MON_ONCE, mm_answer_authrole},
ce94da
+#endif
ce94da
     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
ce94da
     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
ce94da
 #ifdef USE_PAM
Jan F 3657ad
@@ -708,6 +714,9 @@ mm_answer_pwnamallow(int sock, Buffer *m
ce94da
 	else {
ce94da
 		/* Allow service/style information on the auth context */
ce94da
 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
ce94da
+#ifdef WITH_SELINUX
ce94da
+		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHROLE, 1);
ce94da
+#endif
ce94da
 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
ce94da
 	}
ce94da
 
Jan F 3657ad
@@ -752,6 +761,25 @@ mm_answer_authserv(int sock, Buffer *m)
ce94da
 	return (0);
ce94da
 }
ce94da
 
ce94da
+#ifdef WITH_SELINUX
ce94da
+int
ce94da
+mm_answer_authrole(int sock, Buffer *m)
ce94da
+{
ce94da
+	monitor_permit_authentications(1);
ce94da
+
ce94da
+	authctxt->role = buffer_get_string(m, NULL);
ce94da
+	debug3("%s: role=%s",
ce94da
+	    __func__, authctxt->role);
ce94da
+
ce94da
+	if (strlen(authctxt->role) == 0) {
ce94da
+		xfree(authctxt->role);
ce94da
+		authctxt->role = NULL;
ce94da
+	}
ce94da
+
ce94da
+	return (0);
ce94da
+}
ce94da
+#endif
ce94da
+
ce94da
 int
ce94da
 mm_answer_authpassword(int sock, Buffer *m)
ce94da
 {
Jan F 3657ad
@@ -1117,7 +1145,7 @@ static int
ce94da
 monitor_valid_userblob(u_char *data, u_int datalen)
ce94da
 {
ce94da
 	Buffer b;
ce94da
-	char *p;
ce94da
+	char *p, *r;
ce94da
 	u_int len;
ce94da
 	int fail = 0;
adad2a
 
Jan F 3657ad
@@ -1143,6 +1171,8 @@ monitor_valid_userblob(u_char *data, u_i
ce94da
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
ce94da
 		fail++;
ce94da
 	p = buffer_get_string(&b, NULL);
ce94da
+	if ((r = strchr(p, '/')) != NULL)
ce94da
+		*r = '\0';
ce94da
 	if (strcmp(authctxt->user, p) != 0) {
ce94da
 		logit("wrong user name passed to monitor: expected %s != %.100s",
ce94da
 		    authctxt->user, p);
Jan F 3657ad
@@ -1174,7 +1204,7 @@ monitor_valid_hostbasedblob(u_char *data
ce94da
     char *chost)
ce94da
 {
ce94da
 	Buffer b;
ce94da
-	char *p;
ce94da
+	char *p, *r;
ce94da
 	u_int len;
ce94da
 	int fail = 0;
ce94da
 
Jan F 3657ad
@@ -1191,6 +1221,8 @@ monitor_valid_hostbasedblob(u_char *data
ce94da
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
ce94da
 		fail++;
ce94da
 	p = buffer_get_string(&b, NULL);
ce94da
+	if ((r = strchr(p, '/')) != NULL)
ce94da
+		*r = '\0';
ce94da
 	if (strcmp(authctxt->user, p) != 0) {
ce94da
 		logit("wrong user name passed to monitor: expected %s != %.100s",
ce94da
 		    authctxt->user, p);
Jan F 003cb0
diff -up openssh-5.8p1/monitor.h.role openssh-5.8p1/monitor.h
Jan F 3657ad
--- openssh-5.8p1/monitor.h.role	2011-03-22 18:32:14.512648974 +0100
Jan F 3657ad
+++ openssh-5.8p1/monitor.h	2011-03-22 18:32:18.382648691 +0100
ce94da
@@ -31,6 +31,9 @@
adad2a
 enum monitor_reqtype {
adad2a
 	MONITOR_REQ_MODULI, MONITOR_ANS_MODULI,
ce94da
 	MONITOR_REQ_FREE, MONITOR_REQ_AUTHSERV,
ce94da
+#ifdef WITH_SELINUX
ce94da
+	MONITOR_REQ_AUTHROLE,
ce94da
+#endif
adad2a
 	MONITOR_REQ_SIGN, MONITOR_ANS_SIGN,
adad2a
 	MONITOR_REQ_PWNAM, MONITOR_ANS_PWNAM,
adad2a
 	MONITOR_REQ_AUTH2_READ_BANNER, MONITOR_ANS_AUTH2_READ_BANNER,
Jan F 003cb0
diff -up openssh-5.8p1/monitor_wrap.c.role openssh-5.8p1/monitor_wrap.c
Jan F 3657ad
--- openssh-5.8p1/monitor_wrap.c.role	2011-03-22 18:32:14.612774442 +0100
Jan F 3657ad
+++ openssh-5.8p1/monitor_wrap.c	2011-03-22 18:32:18.433648592 +0100
Jan F 003cb0
@@ -298,6 +298,25 @@ mm_inform_authserv(char *service, char *
adad2a
 	buffer_free(&m);
adad2a
 }
a3ba41
 
adad2a
+/* Inform the privileged process about role */
adad2a
+
ce94da
+#ifdef WITH_SELINUX
adad2a
+void
adad2a
+mm_inform_authrole(char *role)
adad2a
+{
adad2a
+	Buffer m;
adad2a
+
adad2a
+	debug3("%s entering", __func__);
adad2a
+
adad2a
+	buffer_init(&m);
adad2a
+	buffer_put_cstring(&m, role ? role : "");
adad2a
+
adad2a
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHROLE, &m);
adad2a
+
adad2a
+	buffer_free(&m);
adad2a
+}
ce94da
+#endif
adad2a
+
adad2a
 /* Do the password authentication */
adad2a
 int
adad2a
 mm_auth_password(Authctxt *authctxt, char *password)
Jan F 003cb0
diff -up openssh-5.8p1/monitor_wrap.h.role openssh-5.8p1/monitor_wrap.h
Jan F 3657ad
--- openssh-5.8p1/monitor_wrap.h.role	2011-03-22 18:32:14.659648967 +0100
Jan F 3657ad
+++ openssh-5.8p1/monitor_wrap.h	2011-03-22 18:32:18.486648682 +0100
ce94da
@@ -41,6 +41,9 @@ int mm_is_monitor(void);
ce94da
 DH *mm_choose_dh(int, int, int);
ce94da
 int mm_key_sign(Key *, u_char **, u_int *, u_char *, u_int);
ce94da
 void mm_inform_authserv(char *, char *);
ce94da
+#ifdef WITH_SELINUX
ce94da
+void mm_inform_authrole(char *);
ce94da
+#endif
ce94da
 struct passwd *mm_getpwnamallow(const char *);
ce94da
 char *mm_auth2_read_banner(void);
ce94da
 int mm_auth_password(struct Authctxt *, char *);
Jan F 003cb0
diff -up openssh-5.8p1/openbsd-compat/Makefile.in.role openssh-5.8p1/openbsd-compat/Makefile.in
Jan F 3657ad
--- openssh-5.8p1/openbsd-compat/Makefile.in.role	2011-03-22 18:32:15.000000000 +0100
Jan F 3657ad
+++ openssh-5.8p1/openbsd-compat/Makefile.in	2011-03-22 18:34:43.962648778 +0100
Jan F 003cb0
@@ -20,7 +20,7 @@ OPENBSD=base64.o basename.o bindresvport
Jan F 003cb0
 
Jan F 003cb0
 COMPAT=bsd-arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o
Jan F 003cb0
 
Jan F 3657ad
-PORTS=port-aix.o port-irix.o port-linux.o port-linux-prng.o port-solaris.o port-tun.o port-uw.o
Jan F 3657ad
+PORTS=port-aix.o port-irix.o port-linux.o port-linux_part_2.o port-linux-prng.o port-solaris.o port-tun.o port-uw.o
Jan F 003cb0
 
Jan F 003cb0
 .c.o:
Jan F 003cb0
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
Jan F 003cb0
diff -up openssh-5.8p1/openbsd-compat/port-linux.c.role openssh-5.8p1/openbsd-compat/port-linux.c
Jan F 3657ad
--- openssh-5.8p1/openbsd-compat/port-linux.c.role	2011-03-22 18:32:17.782648196 +0100
Jan F 3657ad
+++ openssh-5.8p1/openbsd-compat/port-linux.c	2011-03-22 18:32:18.604648721 +0100
Jan F 003cb0
@@ -31,48 +31,73 @@
Jan F 003cb0
 
adad2a
 #include "log.h"
974c89
 #include "xmalloc.h"
Jan F 003cb0
+#include "servconf.h"
adad2a
 #include "port-linux.h"
adad2a
+#include "key.h"
adad2a
+#include "hostfile.h"
adad2a
+#include "auth.h"
adad2a
 
974c89
 #ifdef WITH_SELINUX
adad2a
 #include <selinux/selinux.h>
adad2a
 #include <selinux/flask.h>
adad2a
 #include <selinux/get_context_list.h>
adad2a
 
Jan F 003cb0
-/* Wrapper around is_selinux_enabled() to log its return value once only */
Jan F 003cb0
-int
Jan F 003cb0
-ssh_selinux_enabled(void)
Jan F 003cb0
-{
Jan F 003cb0
-	static int enabled = -1;
Jan F 003cb0
+extern ServerOptions options;
adad2a
+extern Authctxt *the_authctxt;
Jan F 003cb0
+extern int inetd_flag;
Jan F 003cb0
+extern int rexeced_flag;
Jan F 003cb0
 
Jan F 003cb0
-	if (enabled == -1) {
Jan F 003cb0
-		enabled = (is_selinux_enabled() == 1);
Jan F 003cb0
-		debug("SELinux support %s", enabled ? "enabled" : "disabled");
Jan F 003cb0
+static void
Jan F 003cb0
+ssh_selinux_get_role_level(char **role, const char **level)
Jan F 003cb0
+{
Jan F 003cb0
+	*role = NULL;
Jan F 003cb0
+	*level = NULL;
Jan F 003cb0
+	if (the_authctxt) {
Jan F 003cb0
+		if (the_authctxt->role != NULL) {
Jan F 003cb0
+			char *slash;
Jan F 003cb0
+			*role = xstrdup(the_authctxt->role);
Jan F 003cb0
+			if ((slash = strchr(*role, '/')) != NULL) {
Jan F 003cb0
+				*slash = '\0';
Jan F 003cb0
+				*level = slash + 1;
Jan F 003cb0
+			}
Jan F 003cb0
+		}
Jan F 003cb0
 	}
Jan F 003cb0
-
Jan F 003cb0
-	return (enabled);
Jan F 003cb0
 }
Jan F 003cb0
 
Jan F 003cb0
 /* Return the default security context for the given username */
adad2a
 static security_context_t
adad2a
 ssh_selinux_getctxbyname(char *pwname)
adad2a
 {
adad2a
-	security_context_t sc;
adad2a
-	char *sename = NULL, *lvl = NULL;
adad2a
-	int r;
adad2a
+	security_context_t sc = NULL;
adad2a
+	char *sename, *lvl;
Jan F 003cb0
+	char *role;
Jan F 003cb0
+	const char *reqlvl;
adad2a
+	int r = 0;
adad2a
 
Jan F 003cb0
+	ssh_selinux_get_role_level(&role, &reqlvl);
adad2a
 #ifdef HAVE_GETSEUSERBYNAME
adad2a
-	if (getseuserbyname(pwname, &sename, &lvl) != 0)
adad2a
-		return NULL;
adad2a
+	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
adad2a
+		sename = NULL;
adad2a
+		lvl = NULL;
adad2a
+	}
adad2a
 #else
adad2a
 	sename = pwname;
adad2a
 	lvl = NULL;
adad2a
 #endif
adad2a
 
adad2a
+	if (r == 0) {
adad2a
 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
adad2a
-	r = get_default_context_with_level(sename, lvl, NULL, &sc);
adad2a
+		if (role != NULL && role[0])
adad2a
+			r = get_default_context_with_rolelevel(sename, role, lvl, NULL, &sc);
adad2a
+		else
adad2a
+			r = get_default_context_with_level(sename, lvl, NULL, &sc);
adad2a
 #else
adad2a
-	r = get_default_context(sename, NULL, &sc);
adad2a
+		if (role != NULL && role[0])
adad2a
+			r = get_default_context_with_role(sename, role, NULL, &sc);
adad2a
+		else
adad2a
+			r = get_default_context(sename, NULL, &sc);
adad2a
 #endif
adad2a
+	}
adad2a
 
adad2a
 	if (r != 0) {
adad2a
 		switch (security_getenforce()) {
Jan F 003cb0
@@ -100,6 +125,36 @@ ssh_selinux_getctxbyname(char *pwname)
Jan F 003cb0
 	return (sc);
Jan F 003cb0
 }
Jan F 003cb0
 
Jan F 003cb0
+/* Setup environment variables for pam_selinux */
Jan F 003cb0
+static int
Jan F 003cb0
+ssh_selinux_setup_pam_variables(void)
Jan F 003cb0
+{
Jan F 003cb0
+	const char *reqlvl;
Jan F 003cb0
+	char *role;
Jan F 003cb0
+	char *use_current;
Jan F 003cb0
+	int rv;
Jan F 003cb0
+
Jan F 003cb0
+	debug3("%s: setting execution context", __func__);
Jan F 003cb0
+
Jan F 003cb0
+	ssh_selinux_get_role_level(&role, &reqlvl);
Jan F 003cb0
+
Jan F 003cb0
+	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
Jan F 003cb0
+	
Jan F 003cb0
+	if (inetd_flag && !rexeced_flag) {
Jan F 003cb0
+		use_current = "1";
Jan F 003cb0
+	} else {
Jan F 003cb0
+		use_current = "";
Jan F 003cb0
+		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
Jan F 003cb0
+	}
Jan F 003cb0
+
Jan F 003cb0
+	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
Jan F 003cb0
+
Jan F 003cb0
+	if (role != NULL)
Jan F 003cb0
+		xfree(role);
Jan F 003cb0
+	
Jan F 003cb0
+	return rv;
Jan F 003cb0
+}
Jan F 003cb0
+
Jan F 003cb0
 /* Set the execution context to the default for the specified user */
Jan F 003cb0
 void
Jan F 003cb0
 ssh_selinux_setup_exec_context(char *pwname)
Jan F 003cb0
@@ -109,6 +164,24 @@ ssh_selinux_setup_exec_context(char *pwn
Jan F 003cb0
 	if (!ssh_selinux_enabled())
Jan F 003cb0
 		return;
Jan F 003cb0
 
Jan F 003cb0
+	if (options.use_pam) {
Jan F 003cb0
+		/* do not compute context, just setup environment for pam_selinux */
Jan F 003cb0
+		if (ssh_selinux_setup_pam_variables()) {
Jan F 003cb0
+			switch (security_getenforce()) {
Jan F 003cb0
+			case -1:
Jan F 003cb0
+				fatal("%s: security_getenforce() failed", __func__);
Jan F 003cb0
+			case 0:
Jan F 003cb0
+				error("%s: SELinux PAM variable setup failure. Continuing in permissive mode.",
Jan F 003cb0
+				    __func__);
Jan F 003cb0
+			break;
Jan F 003cb0
+			default:
Jan F 003cb0
+				fatal("%s: SELinux PAM variable setup failure. Aborting connection.",
Jan F 003cb0
+				    __func__);
Jan F 003cb0
+			}
Jan F 003cb0
+		}
Jan F 003cb0
+		return;
Jan F 003cb0
+	}
Jan F 003cb0
+
Jan F 003cb0
 	debug3("%s: setting execution context", __func__);
Jan F 003cb0
 
Jan F 003cb0
 	user_ctx = ssh_selinux_getctxbyname(pwname);
Jan F 003cb0
@@ -206,21 +279,6 @@ ssh_selinux_change_context(const char *n
Jan F 003cb0
 	xfree(newctx);
Jan F 003cb0
 }
Jan F 003cb0
 
Jan F 003cb0
-void
Jan F 003cb0
-ssh_selinux_setfscreatecon(const char *path)
Jan F 003cb0
-{
Jan F 003cb0
-	security_context_t context;
Jan F 003cb0
-
Jan F 003cb0
-	if (!ssh_selinux_enabled())
Jan F 003cb0
-		return;
Jan F 003cb0
-	if (path == NULL) {
Jan F 003cb0
-		setfscreatecon(NULL);
Jan F 003cb0
-		return;
Jan F 003cb0
-	}
Jan F 003cb0
-	if (matchpathcon(path, 0700, &context) == 0)
Jan F 003cb0
-		setfscreatecon(context);
Jan F 003cb0
-}
Jan F 003cb0
-
Jan F 003cb0
 #endif /* WITH_SELINUX */
Jan F 003cb0
 
Jan F 003cb0
 #ifdef LINUX_OOM_ADJUST
Jan F 003cb0
diff -up openssh-5.8p1/openbsd-compat/port-linux_part_2.c.role openssh-5.8p1/openbsd-compat/port-linux_part_2.c
Jan F 3657ad
--- openssh-5.8p1/openbsd-compat/port-linux_part_2.c.role	2011-03-22 18:32:18.632648682 +0100
Jan F 3657ad
+++ openssh-5.8p1/openbsd-compat/port-linux_part_2.c	2011-03-22 18:32:18.641648698 +0100
Jan F 003cb0
@@ -0,0 +1,75 @@
Jan F 003cb0
+/* $Id: port-linux.c,v 1.11.4.2 2011/02/04 00:43:08 djm Exp $ */
Jan F 003cb0
+
Jan F 003cb0
+/*
Jan F 003cb0
+ * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
Jan F 003cb0
+ * Copyright (c) 2006 Damien Miller <djm@openbsd.org>
Jan F 003cb0
+ *
Jan F 003cb0
+ * Permission to use, copy, modify, and distribute this software for any
Jan F 003cb0
+ * purpose with or without fee is hereby granted, provided that the above
Jan F 003cb0
+ * copyright notice and this permission notice appear in all copies.
Jan F 003cb0
+ *
Jan F 003cb0
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
Jan F 003cb0
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Jan F 003cb0
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Jan F 003cb0
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Jan F 003cb0
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Jan F 003cb0
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Jan F 003cb0
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Jan F 003cb0
+ */
Jan F 003cb0
+
Jan F 003cb0
+/*
Jan F 003cb0
+ * Linux-specific portability code - just SELinux support at present
Jan F 003cb0
+ */
Jan F 003cb0
+
Jan F 003cb0
+#include "includes.h"
Jan F 003cb0
+
Jan F 003cb0
+#if defined(WITH_SELINUX) || defined(LINUX_OOM_ADJUST)
Jan F 003cb0
+#include <errno.h>
Jan F 003cb0
+#include <stdarg.h>
Jan F 003cb0
+#include <string.h>
Jan F 003cb0
+#include <stdio.h>
Jan F 003cb0
+
Jan F 003cb0
+#include "log.h"
Jan F 003cb0
+#include "xmalloc.h"
Jan F 003cb0
+#include "port-linux.h"
Jan F 003cb0
+#include "key.h"
Jan F 003cb0
+#include "hostfile.h"
Jan F 003cb0
+#include "auth.h"
Jan F 003cb0
+
Jan F 003cb0
+#ifdef WITH_SELINUX
Jan F 003cb0
+#include <selinux/selinux.h>
Jan F 003cb0
+#include <selinux/flask.h>
Jan F 003cb0
+#include <selinux/get_context_list.h>
Jan F 003cb0
+
Jan F 003cb0
+/* Wrapper around is_selinux_enabled() to log its return value once only */
Jan F 003cb0
+int
Jan F 003cb0
+ssh_selinux_enabled(void)
Jan F 003cb0
+{
Jan F 003cb0
+	static int enabled = -1;
Jan F 003cb0
+
Jan F 003cb0
+	if (enabled == -1) {
Jan F 003cb0
+		enabled = (is_selinux_enabled() == 1);
Jan F 003cb0
+		debug("SELinux support %s", enabled ? "enabled" : "disabled");
Jan F 003cb0
+	}
Jan F 003cb0
+
Jan F 003cb0
+	return (enabled);
Jan F 003cb0
+}
Jan F 003cb0
+
Jan F 003cb0
+void
Jan F 003cb0
+ssh_selinux_setfscreatecon(const char *path)
Jan F 003cb0
+{
Jan F 003cb0
+	security_context_t context;
Jan F 003cb0
+
Jan F 003cb0
+	if (!ssh_selinux_enabled())
Jan F 003cb0
+		return;
Jan F 003cb0
+	if (path == NULL) {
Jan F 003cb0
+		setfscreatecon(NULL);
Jan F 003cb0
+		return;
Jan F 003cb0
+	}
Jan F 003cb0
+	if (matchpathcon(path, 0700, &context) == 0)
Jan F 003cb0
+		setfscreatecon(context);
Jan F 003cb0
+}
Jan F 003cb0
+
Jan F 003cb0
+#endif /* WITH_SELINUX */
Jan F 003cb0
+
Jan F 003cb0
+#endif /* WITH_SELINUX || LINUX_OOM_ADJUST */