017ff1
diff --git a/auth-pam.c b/auth-pam.c
017ff1
index d789bad..cd1a775 100644
017ff1
--- a/auth-pam.c
017ff1
+++ b/auth-pam.c
017ff1
@@ -1068,7 +1068,7 @@ is_pam_session_open(void)
f09e2e
  * during the ssh authentication process.
f09e2e
  */
f09e2e
 int
f09e2e
-do_pam_putenv(char *name, char *value)
f09e2e
+do_pam_putenv(char *name, const char *value)
f09e2e
 {
f09e2e
 	int ret = 1;
f09e2e
 #ifdef HAVE_PAM_PUTENV
017ff1
diff --git a/auth-pam.h b/auth-pam.h
017ff1
index a1a2b52..b109a5a 100644
017ff1
--- a/auth-pam.h
017ff1
+++ b/auth-pam.h
f09e2e
@@ -38,7 +38,7 @@ void do_pam_session(void);
f09e2e
 void do_pam_set_tty(const char *);
f09e2e
 void do_pam_setcred(int );
f09e2e
 void do_pam_chauthtok(void);
f09e2e
-int do_pam_putenv(char *, char *);
f09e2e
+int do_pam_putenv(char *, const char *);
f09e2e
 char ** fetch_pam_environment(void);
f09e2e
 char ** fetch_pam_child_environment(void);
f09e2e
 void free_pam_environment(char **);
017ff1
diff --git a/auth.h b/auth.h
017ff1
index 124e597..4605588 100644
017ff1
--- a/auth.h
017ff1
+++ b/auth.h
f09e2e
@@ -59,6 +59,9 @@ struct Authctxt {
f09e2e
 	char		*service;
f09e2e
 	struct passwd	*pw;		/* set if 'valid' */
f09e2e
 	char		*style;
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	char		*role;
f09e2e
+#endif
f09e2e
 	void		*kbdintctxt;
f09e2e
 	char		*info;		/* Extra info for next auth_log */
017ff1
 #ifdef BSD_AUTH
017ff1
diff --git a/auth1.c b/auth1.c
017ff1
index 0f870b3..df040bb 100644
017ff1
--- a/auth1.c
017ff1
+++ b/auth1.c
f09e2e
@@ -381,6 +381,9 @@ do_authentication(Authctxt *authctxt)
f09e2e
 {
f09e2e
 	u_int ulen;
f09e2e
 	char *user, *style = NULL;
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	char *role=NULL;
f09e2e
+#endif
f09e2e
 
f09e2e
 	/* Get the name of the user that we wish to log in as. */
f09e2e
 	packet_read_expect(SSH_CMSG_USER);
f09e2e
@@ -389,11 +392,24 @@ do_authentication(Authctxt *authctxt)
f09e2e
 	user = packet_get_cstring(&ulen);
f09e2e
 	packet_check_eom();
f09e2e
 
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	if ((role = strchr(user, '/')) != NULL)
f09e2e
+		*role++ = '\0';
f09e2e
+#endif
f09e2e
+
f09e2e
 	if ((style = strchr(user, ':')) != NULL)
f09e2e
 		*style++ = '\0';
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	else
f09e2e
+		if (role && (style = strchr(role, ':')) != NULL)
f09e2e
+			*style++ = '\0';
f09e2e
+#endif
f09e2e
 
f09e2e
 	authctxt->user = user;
f09e2e
 	authctxt->style = style;
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	authctxt->role = role;
f09e2e
+#endif
f09e2e
 
f09e2e
 	/* Verify that the user is a valid user. */
f09e2e
 	if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
017ff1
diff --git a/auth2-gss.c b/auth2-gss.c
017ff1
index c28a705..4756dd7 100644
017ff1
--- a/auth2-gss.c
017ff1
+++ b/auth2-gss.c
017ff1
@@ -251,6 +251,7 @@ input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
f09e2e
 	Authctxt *authctxt = ctxt;
f09e2e
 	Gssctxt *gssctxt;
f09e2e
 	int authenticated = 0;
f09e2e
+	char *micuser;
f09e2e
 	Buffer b;
f09e2e
 	gss_buffer_desc mic, gssbuf;
f09e2e
 	u_int len;
017ff1
@@ -263,7 +264,13 @@ input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
f09e2e
 	mic.value = packet_get_string(&len;;
f09e2e
 	mic.length = len;
f09e2e
 
f09e2e
-	ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	if (authctxt->role && (strlen(authctxt->role) > 0))
f09e2e
+		xasprintf(&micuser, "%s/%s", authctxt->user, authctxt->role);
f09e2e
+	else
f09e2e
+#endif
f09e2e
+		micuser = authctxt->user;
f09e2e
+	ssh_gssapi_buildmic(&b, micuser, authctxt->service,
f09e2e
 	    "gssapi-with-mic");
f09e2e
 
f09e2e
 	gssbuf.value = buffer_ptr(&b);
017ff1
@@ -275,6 +282,8 @@ input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
f09e2e
 		logit("GSSAPI MIC check failed");
f09e2e
 
f09e2e
 	buffer_free(&b);
f09e2e
+	if (micuser != authctxt->user)
f09e2e
+		free(micuser);
f09e2e
 	free(mic.value);
f09e2e
 
f09e2e
 	authctxt->postponed = 0;
017ff1
diff --git a/auth2-hostbased.c b/auth2-hostbased.c
017ff1
index eca0069..95d678e 100644
017ff1
--- a/auth2-hostbased.c
017ff1
+++ b/auth2-hostbased.c
017ff1
@@ -112,7 +112,15 @@ userauth_hostbased(Authctxt *authctxt)
f09e2e
 	buffer_put_string(&b, session_id2, session_id2_len);
f09e2e
 	/* reconstruct packet */
f09e2e
 	buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
f09e2e
-	buffer_put_cstring(&b, authctxt->user);
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	if (authctxt->role) {
f09e2e
+		buffer_put_int(&b, strlen(authctxt->user)+strlen(authctxt->role)+1);
f09e2e
+		buffer_append(&b, authctxt->user, strlen(authctxt->user));
f09e2e
+		buffer_put_char(&b, '/');
f09e2e
+		buffer_append(&b, authctxt->role, strlen(authctxt->role));
f09e2e
+	} else 
f09e2e
+#endif
f09e2e
+		buffer_put_cstring(&b, authctxt->user);
f09e2e
 	buffer_put_cstring(&b, service);
f09e2e
 	buffer_put_cstring(&b, "hostbased");
f09e2e
 	buffer_put_string(&b, pkalg, alen);
017ff1
diff --git a/auth2-pubkey.c b/auth2-pubkey.c
017ff1
index 749b11a..c0ae0d4 100644
017ff1
--- a/auth2-pubkey.c
017ff1
+++ b/auth2-pubkey.c
017ff1
@@ -133,9 +133,11 @@ userauth_pubkey(Authctxt *authctxt)
f09e2e
 		}
f09e2e
 		/* reconstruct packet */
f09e2e
 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
f09e2e
-		xasprintf(&userstyle, "%s%s%s", authctxt->user,
f09e2e
+		xasprintf(&userstyle, "%s%s%s%s%s", authctxt->user,
f09e2e
 		    authctxt->style ? ":" : "",
f09e2e
-		    authctxt->style ? authctxt->style : "");
f09e2e
+		    authctxt->style ? authctxt->style : "",
f09e2e
+		    authctxt->role ? "/" : "",
f09e2e
+		    authctxt->role ? authctxt->role : "");
f09e2e
 		buffer_put_cstring(&b, userstyle);
f09e2e
 		free(userstyle);
f09e2e
 		buffer_put_cstring(&b,
017ff1
diff --git a/auth2.c b/auth2.c
017ff1
index a5490c0..5f4f26f 100644
017ff1
--- a/auth2.c
017ff1
+++ b/auth2.c
017ff1
@@ -215,6 +215,9 @@ input_userauth_request(int type, u_int32_t seq, void *ctxt)
f09e2e
 	Authctxt *authctxt = ctxt;
f09e2e
 	Authmethod *m = NULL;
f09e2e
 	char *user, *service, *method, *style = NULL;
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	char *role = NULL;
f09e2e
+#endif
f09e2e
 	int authenticated = 0;
f09e2e
 
f09e2e
 	if (authctxt == NULL)
017ff1
@@ -226,6 +229,11 @@ input_userauth_request(int type, u_int32_t seq, void *ctxt)
f09e2e
 	debug("userauth-request for user %s service %s method %s", user, service, method);
f09e2e
 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
f09e2e
 
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	if ((role = strchr(user, '/')) != NULL)
f09e2e
+		*role++ = 0;
f09e2e
+#endif
f09e2e
+
f09e2e
 	if ((style = strchr(user, ':')) != NULL)
f09e2e
 		*style++ = 0;
f09e2e
 
017ff1
@@ -251,8 +259,15 @@ input_userauth_request(int type, u_int32_t seq, void *ctxt)
f09e2e
 		    use_privsep ? " [net]" : "");
f09e2e
 		authctxt->service = xstrdup(service);
f09e2e
 		authctxt->style = style ? xstrdup(style) : NULL;
f09e2e
-		if (use_privsep)
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+		authctxt->role = role ? xstrdup(role) : NULL;
f09e2e
+#endif
f09e2e
+		if (use_privsep) {
f09e2e
 			mm_inform_authserv(service, style);
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+			mm_inform_authrole(role);
f09e2e
+#endif
f09e2e
+		}
f09e2e
 		userauth_banner();
f09e2e
 		if (auth2_setup_methods_lists(authctxt) != 0)
f09e2e
 			packet_disconnect("no authentication methods enabled");
017ff1
diff --git a/misc.c b/misc.c
017ff1
index e4c8c32..f31cd91 100644
017ff1
--- a/misc.c
017ff1
+++ b/misc.c
017ff1
@@ -430,6 +430,7 @@ char *
f09e2e
 colon(char *cp)
f09e2e
 {
f09e2e
 	int flag = 0;
f09e2e
+	int start = 1;
f09e2e
 
f09e2e
 	if (*cp == ':')		/* Leading colon is part of file name. */
f09e2e
 		return NULL;
017ff1
@@ -445,6 +446,13 @@ colon(char *cp)
f09e2e
 			return (cp);
f09e2e
 		if (*cp == '/')
f09e2e
 			return NULL;
f09e2e
+		if (start) {
f09e2e
+		/* Slash on beginning or after dots only denotes file name. */
f09e2e
+			if (*cp == '/')
f09e2e
+				return (0);
f09e2e
+			if (*cp != '.')
f09e2e
+				start = 0;
f09e2e
+		}
f09e2e
 	}
f09e2e
 	return NULL;
f09e2e
 }
017ff1
diff --git a/monitor.c b/monitor.c
017ff1
index 531c4f9..229fada 100644
017ff1
--- a/monitor.c
017ff1
+++ b/monitor.c
017ff1
@@ -145,6 +145,9 @@ int mm_answer_sign(int, Buffer *);
f09e2e
 int mm_answer_pwnamallow(int, Buffer *);
f09e2e
 int mm_answer_auth2_read_banner(int, Buffer *);
f09e2e
 int mm_answer_authserv(int, Buffer *);
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+int mm_answer_authrole(int, Buffer *);
f09e2e
+#endif
f09e2e
 int mm_answer_authpassword(int, Buffer *);
f09e2e
 int mm_answer_bsdauthquery(int, Buffer *);
f09e2e
 int mm_answer_bsdauthrespond(int, Buffer *);
017ff1
@@ -219,6 +222,9 @@ struct mon_table mon_dispatch_proto20[] = {
f09e2e
     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
f09e2e
     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
f09e2e
     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+    {MONITOR_REQ_AUTHROLE, MON_ONCE, mm_answer_authrole},
f09e2e
+#endif
f09e2e
     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
f09e2e
     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
f09e2e
 #ifdef USE_PAM
017ff1
@@ -805,6 +811,9 @@ mm_answer_pwnamallow(int sock, Buffer *m)
f09e2e
 	else {
f09e2e
 		/* Allow service/style information on the auth context */
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHROLE, 1);
f09e2e
+#endif
f09e2e
 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
f09e2e
 	}
f09e2e
 #ifdef USE_PAM
017ff1
@@ -846,6 +855,25 @@ mm_answer_authserv(int sock, Buffer *m)
f09e2e
 	return (0);
f09e2e
 }
f09e2e
 
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+int
f09e2e
+mm_answer_authrole(int sock, Buffer *m)
f09e2e
+{
f09e2e
+	monitor_permit_authentications(1);
f09e2e
+
f09e2e
+	authctxt->role = buffer_get_string(m, NULL);
f09e2e
+	debug3("%s: role=%s",
f09e2e
+	    __func__, authctxt->role);
f09e2e
+
f09e2e
+	if (strlen(authctxt->role) == 0) {
f09e2e
+		free(authctxt->role);
f09e2e
+		authctxt->role = NULL;
f09e2e
+	}
f09e2e
+
f09e2e
+	return (0);
f09e2e
+}
f09e2e
+#endif
f09e2e
+
f09e2e
 int
f09e2e
 mm_answer_authpassword(int sock, Buffer *m)
f09e2e
 {
017ff1
@@ -1220,7 +1248,7 @@ static int
f09e2e
 monitor_valid_userblob(u_char *data, u_int datalen)
f09e2e
 {
f09e2e
 	Buffer b;
f09e2e
-	char *p, *userstyle;
f09e2e
+	char *p, *r, *userstyle;
f09e2e
 	u_int len;
f09e2e
 	int fail = 0;
f09e2e
 
017ff1
@@ -1246,6 +1274,8 @@ monitor_valid_userblob(u_char *data, u_int datalen)
f09e2e
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
f09e2e
 		fail++;
f09e2e
 	p = buffer_get_cstring(&b, NULL);
f09e2e
+	if ((r = strchr(p, '/')) != NULL)
f09e2e
+		*r = '\0';
f09e2e
 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
f09e2e
 	    authctxt->style ? ":" : "",
f09e2e
 	    authctxt->style ? authctxt->style : "");
017ff1
@@ -1281,7 +1311,7 @@ monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
f09e2e
     char *chost)
f09e2e
 {
f09e2e
 	Buffer b;
f09e2e
-	char *p, *userstyle;
f09e2e
+	char *p, *r, *userstyle;
f09e2e
 	u_int len;
f09e2e
 	int fail = 0;
f09e2e
 
017ff1
@@ -1298,6 +1328,8 @@ monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
f09e2e
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
f09e2e
 		fail++;
f09e2e
 	p = buffer_get_cstring(&b, NULL);
f09e2e
+	if ((r = strchr(p, '/')) != NULL)
f09e2e
+		*r = '\0';
f09e2e
 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
f09e2e
 	    authctxt->style ? ":" : "",
f09e2e
 	    authctxt->style ? authctxt->style : "");
017ff1
diff --git a/monitor.h b/monitor.h
017ff1
index 5bc41b5..20e2b4a 100644
017ff1
--- a/monitor.h
017ff1
+++ b/monitor.h
017ff1
@@ -57,6 +57,10 @@ enum monitor_reqtype {
017ff1
 	MONITOR_REQ_GSSCHECKMIC = 48, MONITOR_ANS_GSSCHECKMIC = 49,
017ff1
 	MONITOR_REQ_TERM = 50,
017ff1
 
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	MONITOR_REQ_AUTHROLE = 80,
f09e2e
+#endif
017ff1
+
f09e2e
 	MONITOR_REQ_PAM_START = 100,
f09e2e
 	MONITOR_REQ_PAM_ACCOUNT = 102, MONITOR_ANS_PAM_ACCOUNT = 103,
017ff1
 	MONITOR_REQ_PAM_INIT_CTX = 104, MONITOR_ANS_PAM_INIT_CTX = 105,
017ff1
diff --git a/monitor_wrap.c b/monitor_wrap.c
017ff1
index 1a47e41..d1b6d99 100644
017ff1
--- a/monitor_wrap.c
017ff1
+++ b/monitor_wrap.c
017ff1
@@ -336,6 +336,25 @@ mm_inform_authserv(char *service, char *style)
f09e2e
 	buffer_free(&m);
f09e2e
 }
f09e2e
 
f09e2e
+/* Inform the privileged process about role */
f09e2e
+
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+void
f09e2e
+mm_inform_authrole(char *role)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	debug3("%s entering", __func__);
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_cstring(&m, role ? role : "");
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHROLE, &m);
f09e2e
+
f09e2e
+	buffer_free(&m);
f09e2e
+}
f09e2e
+#endif
f09e2e
+
f09e2e
 /* Do the password authentication */
f09e2e
 int
f09e2e
 mm_auth_password(Authctxt *authctxt, char *password)
017ff1
diff --git a/monitor_wrap.h b/monitor_wrap.h
017ff1
index 18c2501..9d5e5ba 100644
017ff1
--- a/monitor_wrap.h
017ff1
+++ b/monitor_wrap.h
f09e2e
@@ -42,6 +42,9 @@ int mm_is_monitor(void);
f09e2e
 DH *mm_choose_dh(int, int, int);
f09e2e
 int mm_key_sign(Key *, u_char **, u_int *, u_char *, u_int);
f09e2e
 void mm_inform_authserv(char *, char *);
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+void mm_inform_authrole(char *);
f09e2e
+#endif
f09e2e
 struct passwd *mm_getpwnamallow(const char *);
f09e2e
 char *mm_auth2_read_banner(void);
f09e2e
 int mm_auth_password(struct Authctxt *, char *);
017ff1
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in
017ff1
index 6ecfb93..b912dbe 100644
017ff1
--- a/openbsd-compat/Makefile.in
017ff1
+++ b/openbsd-compat/Makefile.in
017ff1
@@ -20,7 +20,7 @@ OPENBSD=base64.o basename.o bcrypt_pbkdf.o bindresvport.o blowfish.o daemon.o di
f09e2e
 
017ff1
 COMPAT=arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o getrrsetbyname-ldns.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-setres_id.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o
f09e2e
 
f09e2e
-PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o
017ff1
+PORTS=port-aix.o port-irix.o port-linux.o port-linux-sshd.o port-solaris.o port-tun.o port-uw.o
f09e2e
 
f09e2e
 .c.o:
f09e2e
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
017ff1
diff --git a/openbsd-compat/port-linux-sshd.c b/openbsd-compat/port-linux-sshd.c
017ff1
new file mode 100644
017ff1
index 0000000..c18524e
017ff1
--- /dev/null
017ff1
+++ b/openbsd-compat/port-linux-sshd.c
017ff1
@@ -0,0 +1,414 @@
017ff1
+/*
017ff1
+ * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
017ff1
+ * Copyright (c) 2014 Petr Lautrbach <plautrba@redhat.com>
017ff1
+ *
017ff1
+ * Permission to use, copy, modify, and distribute this software for any
017ff1
+ * purpose with or without fee is hereby granted, provided that the above
017ff1
+ * copyright notice and this permission notice appear in all copies.
017ff1
+ *
017ff1
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
017ff1
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
017ff1
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
017ff1
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
017ff1
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
017ff1
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
017ff1
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017ff1
+ */
017ff1
+
017ff1
+/*
017ff1
+ * Linux-specific portability code - just SELinux support for sshd at present
017ff1
+ */
017ff1
+
017ff1
+#include "includes.h"
017ff1
+
017ff1
+#if defined(WITH_SELINUX) || defined(LINUX_OOM_ADJUST)
017ff1
+#include <errno.h>
017ff1
+#include <stdarg.h>
017ff1
+#include <string.h>
017ff1
+#include <stdio.h>
017ff1
+
017ff1
+#include "log.h"
017ff1
+#include "xmalloc.h"
f09e2e
+#include "servconf.h"
017ff1
+#include "port-linux.h"
f09e2e
+#include "key.h"
f09e2e
+#include "hostfile.h"
f09e2e
+#include "auth.h"
017ff1
+
017ff1
+#ifdef WITH_SELINUX
017ff1
+#include <selinux/selinux.h>
017ff1
+#include <selinux/flask.h>
f09e2e
+#include <selinux/context.h>
017ff1
+#include <selinux/get_context_list.h>
f09e2e
+#include <selinux/get_default_type.h>
f09e2e
+#include <selinux/av_permissions.h>
f09e2e
+
f09e2e
+#ifdef HAVE_LINUX_AUDIT
f09e2e
+#include <libaudit.h>
f09e2e
+#include <unistd.h>
f09e2e
+#endif
017ff1
+
f09e2e
+extern ServerOptions options;
f09e2e
+extern Authctxt *the_authctxt;
f09e2e
+extern int inetd_flag;
f09e2e
+extern int rexeced_flag;
f09e2e
+
f09e2e
+/* Send audit message */
f09e2e
+static int
017ff1
+sshd_selinux_send_audit_message(int success, security_context_t default_context,
f09e2e
+		       security_context_t selected_context)
017ff1
+{
f09e2e
+	int rc=0;
f09e2e
+#ifdef HAVE_LINUX_AUDIT
f09e2e
+	char *msg = NULL;
f09e2e
+	int audit_fd = audit_open();
f09e2e
+	security_context_t default_raw=NULL;
f09e2e
+	security_context_t selected_raw=NULL;
f09e2e
+	rc = -1;
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f09e2e
+					errno == EAFNOSUPPORT)
f09e2e
+				return 0; /* No audit support in kernel */
f09e2e
+		error("Error connecting to audit system.");
f09e2e
+		return rc;
f09e2e
+	}
f09e2e
+	if (selinux_trans_to_raw_context(default_context, &default_raw) < 0) {
f09e2e
+		error("Error translating default context.");
f09e2e
+		default_raw = NULL;
f09e2e
+	}
f09e2e
+	if (selinux_trans_to_raw_context(selected_context, &selected_raw) < 0) {
f09e2e
+		error("Error translating selected context.");
f09e2e
+		selected_raw = NULL;
f09e2e
+	}
f09e2e
+	if (asprintf(&msg, "sshd: default-context=%s selected-context=%s",
f09e2e
+		     default_raw ? default_raw : (default_context ? default_context: "?"),
f09e2e
+		     selected_context ? selected_raw : (selected_context ? selected_context :"?")) < 0) {
f09e2e
+		error("Error allocating memory.");
f09e2e
+		goto out;
f09e2e
+	}
f09e2e
+	if (audit_log_user_message(audit_fd, AUDIT_USER_ROLE_CHANGE,
f09e2e
+				   msg, NULL, NULL, NULL, success) <= 0) {
f09e2e
+		error("Error sending audit message.");
f09e2e
+		goto out;
f09e2e
+	}
f09e2e
+	rc = 0;
f09e2e
+      out:
f09e2e
+	free(msg);
f09e2e
+	freecon(default_raw);
f09e2e
+	freecon(selected_raw);
f09e2e
+	close(audit_fd);
f09e2e
+#endif
f09e2e
+	return rc;
f09e2e
+}
f09e2e
+
f09e2e
+static int
f09e2e
+mls_range_allowed(security_context_t src, security_context_t dst)
f09e2e
+{
f09e2e
+	struct av_decision avd;
f09e2e
+	int retval;
f09e2e
+	unsigned int bit = CONTEXT__CONTAINS;
f09e2e
+
f09e2e
+	debug("%s: src:%s dst:%s", __func__, src, dst);
f09e2e
+	retval = security_compute_av(src, dst, SECCLASS_CONTEXT, bit, &avd);
f09e2e
+	if (retval || ((bit & avd.allowed) != bit))
f09e2e
+		return 0;
f09e2e
+
f09e2e
+	return 1;
f09e2e
+}
f09e2e
+
f09e2e
+static int
f09e2e
+get_user_context(const char *sename, const char *role, const char *lvl,
f09e2e
+	security_context_t *sc) {
f09e2e
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
f09e2e
+	if (lvl == NULL || lvl[0] == '\0' || get_default_context_with_level(sename, lvl, NULL, sc) != 0) {
f09e2e
+	        /* User may have requested a level completely outside of his 
f09e2e
+	           allowed range. We get a context just for auditing as the
f09e2e
+	           range check below will certainly fail for default context. */
f09e2e
+#endif
f09e2e
+		if (get_default_context(sename, NULL, sc) != 0) {
f09e2e
+			*sc = NULL;
f09e2e
+			return -1;
f09e2e
+		}
f09e2e
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
f09e2e
+	}
f09e2e
+#endif
f09e2e
+	if (role != NULL && role[0]) {
f09e2e
+		context_t con;
f09e2e
+		char *type=NULL;
f09e2e
+		if (get_default_type(role, &type) != 0) {
f09e2e
+			error("get_default_type: failed to get default type for '%s'",
f09e2e
+				role);
f09e2e
+			goto out;
f09e2e
+		}
f09e2e
+		con = context_new(*sc);
f09e2e
+		if (!con) {
f09e2e
+			goto out;
f09e2e
+		}
f09e2e
+		context_role_set(con, role);
f09e2e
+		context_type_set(con, type);
f09e2e
+		freecon(*sc);
f09e2e
+		*sc = strdup(context_str(con));
f09e2e
+		context_free(con);
017ff1
+		if (!*sc)
f09e2e
+			return -1;
f09e2e
+	}
f09e2e
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
f09e2e
+	if (lvl != NULL && lvl[0]) {
f09e2e
+		/* verify that the requested range is obtained */
f09e2e
+		context_t con;
f09e2e
+		security_context_t obtained_raw;
f09e2e
+		security_context_t requested_raw;
f09e2e
+		con = context_new(*sc);
f09e2e
+		if (!con) {
f09e2e
+			goto out;
f09e2e
+		}
f09e2e
+		context_range_set(con, lvl);
f09e2e
+		if (selinux_trans_to_raw_context(*sc, &obtained_raw) < 0) {
f09e2e
+			context_free(con);
f09e2e
+			goto out;
f09e2e
+		}
f09e2e
+		if (selinux_trans_to_raw_context(context_str(con), &requested_raw) < 0) {
f09e2e
+			freecon(obtained_raw);
f09e2e
+			context_free(con);
f09e2e
+			goto out;
f09e2e
+		}
017ff1
+
f09e2e
+		debug("get_user_context: obtained context '%s' requested context '%s'",
f09e2e
+			obtained_raw, requested_raw);
f09e2e
+		if (strcmp(obtained_raw, requested_raw)) {
f09e2e
+			/* set the context to the real requested one but fail */
f09e2e
+			freecon(requested_raw);
f09e2e
+			freecon(obtained_raw);
f09e2e
+			freecon(*sc);
f09e2e
+			*sc = strdup(context_str(con));
f09e2e
+			context_free(con);
f09e2e
+			return -1;
f09e2e
+		}
f09e2e
+		freecon(requested_raw);
f09e2e
+		freecon(obtained_raw);
f09e2e
+		context_free(con);
017ff1
+	}
f09e2e
+#endif
f09e2e
+	return 0;
f09e2e
+      out:
f09e2e
+	freecon(*sc);
f09e2e
+	*sc = NULL;
f09e2e
+	return -1;
f09e2e
+}
017ff1
+
f09e2e
+static void
f09e2e
+ssh_selinux_get_role_level(char **role, const char **level)
f09e2e
+{
f09e2e
+	*role = NULL;
f09e2e
+	*level = NULL;
f09e2e
+	if (the_authctxt) {
f09e2e
+		if (the_authctxt->role != NULL) {
f09e2e
+			char *slash;
f09e2e
+			*role = xstrdup(the_authctxt->role);
f09e2e
+			if ((slash = strchr(*role, '/')) != NULL) {
f09e2e
+				*slash = '\0';
f09e2e
+				*level = slash + 1;
f09e2e
+			}
f09e2e
+		}
f09e2e
+	}
017ff1
+}
017ff1
+
017ff1
+/* Return the default security context for the given username */
017ff1
+static int
017ff1
+sshd_selinux_getctxbyname(char *pwname,
f09e2e
+	security_context_t *default_sc, security_context_t *user_sc)
017ff1
+{
f09e2e
+	char *sename, *lvl;
f09e2e
+	char *role;
f09e2e
+	const char *reqlvl;
f09e2e
+	int r = 0;
f09e2e
+	context_t con = NULL;
017ff1
+
f09e2e
+	ssh_selinux_get_role_level(&role, &reqlvl);
017ff1
+
017ff1
+#ifdef HAVE_GETSEUSERBYNAME
f09e2e
+	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
f09e2e
+		sename = NULL;
f09e2e
+		lvl = NULL;
f09e2e
+	}
017ff1
+#else
017ff1
+	sename = pwname;
f09e2e
+	lvl = "";
017ff1
+#endif
017ff1
+
f09e2e
+	if (r == 0) {
017ff1
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
f09e2e
+		r = get_default_context_with_level(sename, lvl, NULL, default_sc);
017ff1
+#else
f09e2e
+		r = get_default_context(sename, NULL, default_sc);
017ff1
+#endif
f09e2e
+	}
f09e2e
+
f09e2e
+	if (r == 0) {
f09e2e
+		/* If launched from xinetd, we must use current level */
f09e2e
+		if (inetd_flag && !rexeced_flag) {
f09e2e
+			security_context_t sshdsc=NULL;
f09e2e
+
f09e2e
+			if (getcon_raw(&sshdsc) < 0)
f09e2e
+				fatal("failed to allocate security context");
f09e2e
+
f09e2e
+			if ((con=context_new(sshdsc)) == NULL)
f09e2e
+				fatal("failed to allocate selinux context");
f09e2e
+			reqlvl = context_range_get(con);
f09e2e
+			freecon(sshdsc);
f09e2e
+			if (reqlvl !=NULL && lvl != NULL && strcmp(reqlvl, lvl) == 0)
f09e2e
+			    /* we actually don't change level */
f09e2e
+			    reqlvl = "";
f09e2e
+
f09e2e
+			debug("%s: current connection level '%s'", __func__, reqlvl);
017ff1
+
017ff1
+		}
017ff1
+
f09e2e
+		if ((reqlvl != NULL && reqlvl[0]) || (role != NULL && role[0])) {
f09e2e
+			r = get_user_context(sename, role, reqlvl, user_sc);
017ff1
+
f09e2e
+			if (r == 0 && reqlvl != NULL && reqlvl[0]) {
f09e2e
+				security_context_t default_level_sc = *default_sc;
f09e2e
+				if (role != NULL && role[0]) {
f09e2e
+					if (get_user_context(sename, role, lvl, &default_level_sc) < 0)
f09e2e
+						default_level_sc = *default_sc;
f09e2e
+				}
f09e2e
+				/* verify that the requested range is contained in the user range */
f09e2e
+				if (mls_range_allowed(default_level_sc, *user_sc)) {
f09e2e
+					logit("permit MLS level %s (user range %s)", reqlvl, lvl);
f09e2e
+				} else {
f09e2e
+					r = -1;
f09e2e
+					error("deny MLS level %s (user range %s)", reqlvl, lvl);
f09e2e
+				}
f09e2e
+				if (default_level_sc != *default_sc)
f09e2e
+					freecon(default_level_sc);
f09e2e
+			}
f09e2e
+		} else {
f09e2e
+			*user_sc = *default_sc;
f09e2e
+		}
f09e2e
+	}
f09e2e
+	if (r != 0) {
f09e2e
+		error("%s: Failed to get default SELinux security "
f09e2e
+		    "context for %s", __func__, pwname);
017ff1
+	}
017ff1
+
017ff1
+#ifdef HAVE_GETSEUSERBYNAME
017ff1
+	free(sename);
017ff1
+	free(lvl);
017ff1
+#endif
017ff1
+
f09e2e
+	if (role != NULL)
f09e2e
+		free(role);
f09e2e
+	if (con)
f09e2e
+		context_free(con);
f09e2e
+
f09e2e
+	return (r);
f09e2e
+}
f09e2e
+
f09e2e
+/* Setup environment variables for pam_selinux */
f09e2e
+static int
017ff1
+sshd_selinux_setup_pam_variables(void)
f09e2e
+{
f09e2e
+	const char *reqlvl;
f09e2e
+	char *role;
f09e2e
+	char *use_current;
f09e2e
+	int rv;
f09e2e
+
f09e2e
+	debug3("%s: setting execution context", __func__);
f09e2e
+
f09e2e
+	ssh_selinux_get_role_level(&role, &reqlvl);
f09e2e
+
f09e2e
+	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
f09e2e
+
f09e2e
+	if (inetd_flag && !rexeced_flag) {
f09e2e
+		use_current = "1";
f09e2e
+	} else {
f09e2e
+		use_current = "";
f09e2e
+		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
f09e2e
+	}
f09e2e
+
f09e2e
+	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
f09e2e
+
f09e2e
+	if (role != NULL)
f09e2e
+		free(role);
f09e2e
+
f09e2e
+	return rv;
017ff1
+}
017ff1
+
017ff1
+/* Set the execution context to the default for the specified user */
017ff1
+void
017ff1
+sshd_selinux_setup_exec_context(char *pwname)
017ff1
+{
017ff1
+	security_context_t user_ctx = NULL;
f09e2e
+	int r = 0;
f09e2e
+	security_context_t default_ctx = NULL;
017ff1
+
017ff1
+	if (!ssh_selinux_enabled())
017ff1
+		return;
017ff1
+
f09e2e
+	if (options.use_pam) {
f09e2e
+		/* do not compute context, just setup environment for pam_selinux */
017ff1
+		if (sshd_selinux_setup_pam_variables()) {
f09e2e
+			switch (security_getenforce()) {
f09e2e
+			case -1:
f09e2e
+				fatal("%s: security_getenforce() failed", __func__);
f09e2e
+			case 0:
f09e2e
+				error("%s: SELinux PAM variable setup failure. Continuing in permissive mode.",
f09e2e
+				    __func__);
f09e2e
+			break;
f09e2e
+			default:
f09e2e
+				fatal("%s: SELinux PAM variable setup failure. Aborting connection.",
f09e2e
+				    __func__);
f09e2e
+			}
f09e2e
+		}
f09e2e
+		return;
f09e2e
+	}
f09e2e
+
017ff1
+	debug3("%s: setting execution context", __func__);
017ff1
+
017ff1
+	r = sshd_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);
f09e2e
+	if (r >= 0) {
f09e2e
+		r = setexeccon(user_ctx);
f09e2e
+		if (r < 0) {
f09e2e
+			error("%s: Failed to set SELinux execution context %s for %s",
f09e2e
+			    __func__, user_ctx, pwname);
017ff1
+		}
f09e2e
+#ifdef HAVE_SETKEYCREATECON
f09e2e
+		else if (setkeycreatecon(user_ctx) < 0) {
f09e2e
+			error("%s: Failed to set SELinux keyring creation context %s for %s",
f09e2e
+			    __func__, user_ctx, pwname);
f09e2e
+		}
f09e2e
+#endif
f09e2e
+	}
f09e2e
+	if (user_ctx == NULL) {
f09e2e
+		user_ctx = default_ctx;
f09e2e
+	}
f09e2e
+	if (r < 0 || user_ctx != default_ctx) {
f09e2e
+		/* audit just the case when user changed a role or there was
f09e2e
+		   a failure */
017ff1
+		sshd_selinux_send_audit_message(r >= 0, default_ctx, user_ctx);
f09e2e
+	}
f09e2e
+	if (r < 0) {
017ff1
+		switch (security_getenforce()) {
017ff1
+		case -1:
017ff1
+			fatal("%s: security_getenforce() failed", __func__);
017ff1
+		case 0:
f09e2e
+			error("%s: SELinux failure. Continuing in permissive mode.",
f09e2e
+			    __func__);
017ff1
+			break;
017ff1
+		default:
f09e2e
+			fatal("%s: SELinux failure. Aborting connection.",
f09e2e
+			    __func__);
017ff1
+		}
017ff1
+	}
f09e2e
+	if (user_ctx != NULL && user_ctx != default_ctx)
017ff1
+		freecon(user_ctx);
f09e2e
+	if (default_ctx != NULL)
f09e2e
+		freecon(default_ctx);
017ff1
+
017ff1
+	debug3("%s: done", __func__);
017ff1
+}
017ff1
+
017ff1
+#endif
017ff1
+#endif
017ff1
+
017ff1
diff --git a/openbsd-compat/port-linux.c b/openbsd-compat/port-linux.c
017ff1
index 4637a7a..22ea8ef 100644
017ff1
--- a/openbsd-compat/port-linux.c
017ff1
+++ b/openbsd-compat/port-linux.c
017ff1
@@ -103,37 +103,6 @@ ssh_selinux_getctxbyname(char *pwname)
017ff1
 	return sc;
f09e2e
 }
f09e2e
 
017ff1
-/* Set the execution context to the default for the specified user */
f09e2e
-void
017ff1
-ssh_selinux_setup_exec_context(char *pwname)
f09e2e
-{
017ff1
-	security_context_t user_ctx = NULL;
f09e2e
-
f09e2e
-	if (!ssh_selinux_enabled())
f09e2e
-		return;
017ff1
-
017ff1
-	debug3("%s: setting execution context", __func__);
017ff1
-
017ff1
-	user_ctx = ssh_selinux_getctxbyname(pwname);
017ff1
-	if (setexeccon(user_ctx) != 0) {
017ff1
-		switch (security_getenforce()) {
017ff1
-		case -1:
017ff1
-			fatal("%s: security_getenforce() failed", __func__);
017ff1
-		case 0:
017ff1
-			error("%s: Failed to set SELinux execution "
017ff1
-			    "context for %s", __func__, pwname);
017ff1
-			break;
017ff1
-		default:
017ff1
-			fatal("%s: Failed to set SELinux execution context "
017ff1
-			    "for %s (in enforcing mode)", __func__, pwname);
017ff1
-		}
f09e2e
-	}
017ff1
-	if (user_ctx != NULL)
017ff1
-		freecon(user_ctx);
017ff1
-
017ff1
-	debug3("%s: done", __func__);
f09e2e
-}
f09e2e
-
017ff1
 /* Set the TTY context for the specified user */
017ff1
 void
017ff1
 ssh_selinux_setup_pty(char *pwname, const char *tty)
017ff1
diff --git a/openbsd-compat/port-linux.h b/openbsd-compat/port-linux.h
017ff1
index e3d1004..8ef6cc4 100644
017ff1
--- a/openbsd-compat/port-linux.h
017ff1
+++ b/openbsd-compat/port-linux.h
017ff1
@@ -22,9 +22,10 @@
017ff1
 #ifdef WITH_SELINUX
017ff1
 int ssh_selinux_enabled(void);
017ff1
 void ssh_selinux_setup_pty(char *, const char *);
017ff1
-void ssh_selinux_setup_exec_context(char *);
017ff1
 void ssh_selinux_change_context(const char *);
017ff1
 void ssh_selinux_setfscreatecon(const char *);
017ff1
+
017ff1
+void sshd_selinux_setup_exec_context(char *);
017ff1
 #endif
f09e2e
 
f09e2e
 #ifdef LINUX_OOM_ADJUST
017ff1
diff --git a/platform.c b/platform.c
017ff1
index 30fc609..0d39ab2 100644
017ff1
--- a/platform.c
017ff1
+++ b/platform.c
017ff1
@@ -183,7 +183,7 @@ platform_setusercontext_post_groups(struct passwd *pw)
017ff1
 	}
017ff1
 #endif /* HAVE_SETPCRED */
017ff1
 #ifdef WITH_SELINUX
017ff1
-	ssh_selinux_setup_exec_context(pw->pw_name);
017ff1
+	sshd_selinux_setup_exec_context(pw->pw_name);
017ff1
 #endif
017ff1
 }
017ff1
 
017ff1
diff --git a/sshd.c b/sshd.c
017ff1
index 7523de9..07f9926 100644
017ff1
--- a/sshd.c
017ff1
+++ b/sshd.c
017ff1
@@ -2138,6 +2138,9 @@ main(int ac, char **av)
f09e2e
 		restore_uid();
f09e2e
 	}
f09e2e
 #endif
f09e2e
+#ifdef WITH_SELINUX
017ff1
+	sshd_selinux_setup_exec_context(authctxt->pw->pw_name);
f09e2e
+#endif
f09e2e
 #ifdef USE_PAM
f09e2e
 	if (options.use_pam) {
f09e2e
 		do_pam_setcred(1);