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