kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
f09e2e
diff -up openssh-6.3p1/auth-pam.c.role-mls openssh-6.3p1/auth-pam.c
f09e2e
--- openssh-6.3p1/auth-pam.c.role-mls	2013-10-10 14:34:43.799494546 +0200
f09e2e
+++ openssh-6.3p1/auth-pam.c	2013-10-10 14:34:43.835494375 +0200
f09e2e
@@ -1071,7 +1071,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
f09e2e
diff -up openssh-6.3p1/auth-pam.h.role-mls openssh-6.3p1/auth-pam.h
f09e2e
--- openssh-6.3p1/auth-pam.h.role-mls	2004-09-11 14:17:26.000000000 +0200
f09e2e
+++ openssh-6.3p1/auth-pam.h	2013-10-10 14:34:43.835494375 +0200
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 **);
f09e2e
diff -up openssh-6.3p1/auth.h.role-mls openssh-6.3p1/auth.h
f09e2e
--- openssh-6.3p1/auth.h.role-mls	2013-10-10 14:34:43.834494379 +0200
f09e2e
+++ openssh-6.3p1/auth.h	2013-10-10 14:38:45.060348227 +0200
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 */
f09e2e
 	void		*jpake_ctx;
f09e2e
diff -up openssh-6.3p1/auth1.c.role-mls openssh-6.3p1/auth1.c
f09e2e
--- openssh-6.3p1/auth1.c.role-mls	2013-06-02 00:01:24.000000000 +0200
f09e2e
+++ openssh-6.3p1/auth1.c	2013-10-10 14:34:43.835494375 +0200
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)
f09e2e
diff -up openssh-6.3p1/auth2-gss.c.role-mls openssh-6.3p1/auth2-gss.c
f09e2e
--- openssh-6.3p1/auth2-gss.c.role-mls	2013-06-01 23:31:18.000000000 +0200
f09e2e
+++ openssh-6.3p1/auth2-gss.c	2013-10-10 14:34:43.836494370 +0200
f09e2e
@@ -256,6 +256,7 @@ input_gssapi_mic(int type, u_int32_t ple
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;
f09e2e
@@ -268,7 +269,13 @@ input_gssapi_mic(int type, u_int32_t ple
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);
f09e2e
@@ -280,6 +287,8 @@ input_gssapi_mic(int type, u_int32_t ple
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;
f09e2e
diff -up openssh-6.3p1/auth2-hostbased.c.role-mls openssh-6.3p1/auth2-hostbased.c
f09e2e
--- openssh-6.3p1/auth2-hostbased.c.role-mls	2013-10-10 14:34:43.818494455 +0200
f09e2e
+++ openssh-6.3p1/auth2-hostbased.c	2013-10-10 14:34:43.836494370 +0200
f09e2e
@@ -106,7 +106,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);
f09e2e
diff -up openssh-6.3p1/auth2-pubkey.c.role-mls openssh-6.3p1/auth2-pubkey.c
f09e2e
--- openssh-6.3p1/auth2-pubkey.c.role-mls	2013-10-10 14:34:43.836494370 +0200
f09e2e
+++ openssh-6.3p1/auth2-pubkey.c	2013-10-10 14:57:17.452062486 +0200
f09e2e
@@ -127,9 +127,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,
f09e2e
diff -up openssh-6.3p1/auth2.c.role-mls openssh-6.3p1/auth2.c
f09e2e
--- openssh-6.3p1/auth2.c.role-mls	2013-10-10 14:34:43.819494451 +0200
f09e2e
+++ openssh-6.3p1/auth2.c	2013-10-10 14:34:43.835494375 +0200
f09e2e
@@ -221,6 +221,9 @@ input_userauth_request(int type, u_int32
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)
f09e2e
@@ -232,6 +235,11 @@ input_userauth_request(int type, u_int32
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
 
f09e2e
@@ -254,8 +262,15 @@ input_userauth_request(int type, u_int32
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");
f09e2e
diff -up openssh-6.3p1/misc.c.role-mls openssh-6.3p1/misc.c
f09e2e
--- openssh-6.3p1/misc.c.role-mls	2013-08-08 04:50:06.000000000 +0200
f09e2e
+++ openssh-6.3p1/misc.c	2013-10-10 14:34:43.836494370 +0200
f09e2e
@@ -429,6 +429,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;
f09e2e
@@ -444,6 +445,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
 }
f09e2e
diff -up openssh-6.3p1/monitor.c.role-mls openssh-6.3p1/monitor.c
f09e2e
--- openssh-6.3p1/monitor.c.role-mls	2013-10-10 14:34:43.821494441 +0200
f09e2e
+++ openssh-6.3p1/monitor.c	2013-10-10 14:54:57.933725463 +0200
f09e2e
@@ -149,6 +149,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 *);
f09e2e
@@ -233,6 +236,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
f09e2e
@@ -853,6 +859,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
f09e2e
@@ -894,6 +903,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
 {
f09e2e
@@ -1269,7 +1297,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
 
f09e2e
@@ -1295,6 +1323,8 @@ monitor_valid_userblob(u_char *data, u_i
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 : "");
f09e2e
@@ -1330,7 +1360,7 @@ monitor_valid_hostbasedblob(u_char *data
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
 
f09e2e
@@ -1347,6 +1377,8 @@ monitor_valid_hostbasedblob(u_char *data
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 : "");
f09e2e
diff -up openssh-6.3p1/monitor.h.role-mls openssh-6.3p1/monitor.h
f09e2e
--- openssh-6.3p1/monitor.h.role-mls	2013-10-10 14:34:43.821494441 +0200
f09e2e
+++ openssh-6.3p1/monitor.h	2013-10-10 14:34:43.837494365 +0200
f09e2e
@@ -61,6 +61,9 @@ enum monitor_reqtype {
f09e2e
 	MONITOR_REQ_JPAKE_STEP2 = 56, MONITOR_ANS_JPAKE_STEP2 = 57,
f09e2e
 	MONITOR_REQ_JPAKE_KEY_CONFIRM = 58, MONITOR_ANS_JPAKE_KEY_CONFIRM = 59,
f09e2e
 	MONITOR_REQ_JPAKE_CHECK_CONFIRM = 60, MONITOR_ANS_JPAKE_CHECK_CONFIRM = 61,
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	MONITOR_REQ_AUTHROLE = 80,
f09e2e
+#endif
f09e2e
 
f09e2e
 	MONITOR_REQ_PAM_START = 100,
f09e2e
 	MONITOR_REQ_PAM_ACCOUNT = 102, MONITOR_ANS_PAM_ACCOUNT = 103,
f09e2e
diff -up openssh-6.3p1/monitor_wrap.c.role-mls openssh-6.3p1/monitor_wrap.c
f09e2e
--- openssh-6.3p1/monitor_wrap.c.role-mls	2013-10-10 14:34:43.822494436 +0200
f09e2e
+++ openssh-6.3p1/monitor_wrap.c	2013-10-10 14:34:43.838494360 +0200
f09e2e
@@ -338,6 +338,25 @@ mm_inform_authserv(char *service, char *
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)
f09e2e
diff -up openssh-6.3p1/monitor_wrap.h.role-mls openssh-6.3p1/monitor_wrap.h
f09e2e
--- openssh-6.3p1/monitor_wrap.h.role-mls	2013-10-10 14:34:43.822494436 +0200
f09e2e
+++ openssh-6.3p1/monitor_wrap.h	2013-10-10 14:34:43.838494360 +0200
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 *);
f09e2e
diff -up openssh-6.3p1/openbsd-compat/Makefile.in.role-mls openssh-6.3p1/openbsd-compat/Makefile.in
f09e2e
--- openssh-6.3p1/openbsd-compat/Makefile.in.role-mls	2013-05-10 08:28:56.000000000 +0200
f09e2e
+++ openssh-6.3p1/openbsd-compat/Makefile.in	2013-10-10 14:34:43.838494360 +0200
f09e2e
@@ -20,7 +20,7 @@ OPENBSD=base64.o basename.o bindresvport
f09e2e
 
f09e2e
 COMPAT=bsd-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
f09e2e
+PORTS=port-aix.o port-irix.o port-linux.o port-linux_part_2.o port-solaris.o port-tun.o port-uw.o
f09e2e
 
f09e2e
 .c.o:
f09e2e
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
f09e2e
diff -up openssh-6.3p1/openbsd-compat/port-linux.c.role-mls openssh-6.3p1/openbsd-compat/port-linux.c
f09e2e
--- openssh-6.3p1/openbsd-compat/port-linux.c.role-mls	2013-06-02 00:07:32.000000000 +0200
f09e2e
+++ openssh-6.3p1/openbsd-compat/port-linux.c	2013-10-10 14:40:41.841793347 +0200
f09e2e
@@ -31,68 +31,271 @@
f09e2e
 
f09e2e
 #include "log.h"
f09e2e
 #include "xmalloc.h"
f09e2e
+#include "servconf.h"
f09e2e
 #include "port-linux.h"
f09e2e
+#include "key.h"
f09e2e
+#include "hostfile.h"
f09e2e
+#include "auth.h"
f09e2e
 
f09e2e
 #ifdef WITH_SELINUX
f09e2e
 #include <selinux/selinux.h>
f09e2e
 #include <selinux/flask.h>
f09e2e
+#include <selinux/context.h>
f09e2e
 #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
f09e2e
 
f09e2e
 #ifndef SSH_SELINUX_UNCONFINED_TYPE
f09e2e
 # define SSH_SELINUX_UNCONFINED_TYPE ":unconfined_t:"
f09e2e
 #endif
f09e2e
 
f09e2e
-/* Wrapper around is_selinux_enabled() to log its return value once only */
f09e2e
-int
f09e2e
-ssh_selinux_enabled(void)
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
f09e2e
+send_audit_message(int success, security_context_t default_context,
f09e2e
+		       security_context_t selected_context)
f09e2e
 {
f09e2e
-	static int enabled = -1;
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);
f09e2e
+		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
+		}
f09e2e
 
f09e2e
-	if (enabled == -1) {
f09e2e
-		enabled = (is_selinux_enabled() == 1);
f09e2e
-		debug("SELinux support %s", enabled ? "enabled" : "disabled");
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);
f09e2e
 	}
f09e2e
+#endif
f09e2e
+	return 0;
f09e2e
+      out:
f09e2e
+	freecon(*sc);
f09e2e
+	*sc = NULL;
f09e2e
+	return -1;
f09e2e
+}
f09e2e
 
f09e2e
-	return (enabled);
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
+	}
f09e2e
 }
f09e2e
 
f09e2e
 /* Return the default security context for the given username */
f09e2e
 static security_context_t
f09e2e
-ssh_selinux_getctxbyname(char *pwname)
f09e2e
+ssh_selinux_getctxbyname(char *pwname,
f09e2e
+	security_context_t *default_sc, security_context_t *user_sc)
f09e2e
 {
f09e2e
-	security_context_t sc = NULL;
f09e2e
-	char *sename = NULL, *lvl = NULL;
f09e2e
-	int r;
f09e2e
+	char *sename, *lvl;
f09e2e
+	char *role;
f09e2e
+	const char *reqlvl;
f09e2e
+	int r = 0;
f09e2e
+	context_t con = NULL;
f09e2e
+ 
f09e2e
+	ssh_selinux_get_role_level(&role, &reqlvl);
f09e2e
 
f09e2e
 #ifdef HAVE_GETSEUSERBYNAME
f09e2e
-	if (getseuserbyname(pwname, &sename, &lvl) != 0)
f09e2e
-		return NULL;
f09e2e
+	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
f09e2e
+		sename = NULL;
f09e2e
+		lvl = NULL;
f09e2e
+	}
f09e2e
 #else
f09e2e
 	sename = pwname;
f09e2e
-	lvl = NULL;
f09e2e
+	lvl = "";
f09e2e
 #endif
f09e2e
 
f09e2e
+	if (r == 0) {
f09e2e
 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
f09e2e
-	r = get_default_context_with_level(sename, lvl, NULL, &sc);
f09e2e
+		r = get_default_context_with_level(sename, lvl, NULL, default_sc);
f09e2e
 #else
f09e2e
-	r = get_default_context(sename, NULL, &sc);
f09e2e
+		r = get_default_context(sename, NULL, default_sc);
f09e2e
 #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);
f09e2e
 
f09e2e
-	if (r != 0) {
f09e2e
-		switch (security_getenforce()) {
f09e2e
-		case -1:
f09e2e
-			fatal("%s: ssh_selinux_getctxbyname: "
f09e2e
-			    "security_getenforce() failed", __func__);
f09e2e
-		case 0:
f09e2e
-			error("%s: Failed to get default SELinux security "
f09e2e
-			    "context for %s", __func__, pwname);
f09e2e
-			sc = NULL;
f09e2e
-			break;
f09e2e
-		default:
f09e2e
-			fatal("%s: Failed to get default SELinux security "
f09e2e
-			    "context for %s (in enforcing mode)",
f09e2e
-			    __func__, pwname);
f09e2e
 		}
f09e2e
+		
f09e2e
+		if ((reqlvl != NULL && reqlvl[0]) || (role != NULL && role[0])) {
f09e2e
+			r = get_user_context(sename, role, reqlvl, user_sc);
f09e2e
+		
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);
f09e2e
 	}
f09e2e
 
f09e2e
 #ifdef HAVE_GETSEUSERBYNAME
f09e2e
@@ -100,7 +303,42 @@ ssh_selinux_getctxbyname(char *pwname)
f09e2e
 	free(lvl);
f09e2e
 #endif
f09e2e
 
f09e2e
-	return sc;
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
f09e2e
+ssh_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;
f09e2e
 }
f09e2e
 
f09e2e
 /* Set the execution context to the default for the specified user */
f09e2e
@@ -108,28 +346,71 @@ void
f09e2e
 ssh_selinux_setup_exec_context(char *pwname)
f09e2e
 {
f09e2e
 	security_context_t user_ctx = NULL;
f09e2e
+	int r = 0;
f09e2e
+	security_context_t default_ctx = NULL;
f09e2e
 
f09e2e
 	if (!ssh_selinux_enabled())
f09e2e
 		return;
f09e2e
 
f09e2e
+	if (options.use_pam) {
f09e2e
+		/* do not compute context, just setup environment for pam_selinux */
f09e2e
+		if (ssh_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
+
f09e2e
 	debug3("%s: setting execution context", __func__);
f09e2e
 
f09e2e
-	user_ctx = ssh_selinux_getctxbyname(pwname);
f09e2e
-	if (setexeccon(user_ctx) != 0) {
f09e2e
+	r = ssh_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);
f09e2e
+		} 
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 */
f09e2e
+		send_audit_message(r >= 0, default_ctx, user_ctx);
f09e2e
+	}
f09e2e
+	if (r < 0) {
f09e2e
 		switch (security_getenforce()) {
f09e2e
 		case -1:
f09e2e
 			fatal("%s: security_getenforce() failed", __func__);
f09e2e
 		case 0:
f09e2e
-			error("%s: Failed to set SELinux execution "
f09e2e
-			    "context for %s", __func__, pwname);
f09e2e
+			error("%s: SELinux failure. Continuing in permissive mode.",
f09e2e
+			    __func__);
f09e2e
 			break;
f09e2e
 		default:
f09e2e
-			fatal("%s: Failed to set SELinux execution context "
f09e2e
-			    "for %s (in enforcing mode)", __func__, pwname);
f09e2e
+			fatal("%s: SELinux failure. Aborting connection.",
f09e2e
+			    __func__);
f09e2e
 		}
f09e2e
 	}
f09e2e
-	if (user_ctx != NULL)
f09e2e
+	if (user_ctx != NULL && user_ctx != default_ctx)
f09e2e
 		freecon(user_ctx);
f09e2e
+	if (default_ctx != NULL)
f09e2e
+		freecon(default_ctx);
f09e2e
 
f09e2e
 	debug3("%s: done", __func__);
f09e2e
 }
f09e2e
@@ -147,7 +428,10 @@ ssh_selinux_setup_pty(char *pwname, cons
f09e2e
 
f09e2e
 	debug3("%s: setting TTY context on %s", __func__, tty);
f09e2e
 
f09e2e
-	user_ctx = ssh_selinux_getctxbyname(pwname);
f09e2e
+	if (getexeccon(&user_ctx) < 0) {
f09e2e
+		error("%s: getexeccon: %s", __func__, strerror(errno));
f09e2e
+		goto out;
f09e2e
+	}
f09e2e
 
f09e2e
 	/* XXX: should these calls fatal() upon failure in enforcing mode? */
f09e2e
 
f09e2e
@@ -219,21 +503,6 @@ ssh_selinux_change_context(const char *n
f09e2e
 	free(newctx);
f09e2e
 }
f09e2e
 
f09e2e
-void
f09e2e
-ssh_selinux_setfscreatecon(const char *path)
f09e2e
-{
f09e2e
-	security_context_t context;
f09e2e
-
f09e2e
-	if (!ssh_selinux_enabled())
f09e2e
-		return;
f09e2e
-	if (path == NULL) {
f09e2e
-		setfscreatecon(NULL);
f09e2e
-		return;
f09e2e
-	}
f09e2e
-	if (matchpathcon(path, 0700, &context) == 0)
f09e2e
-		setfscreatecon(context);
f09e2e
-}
f09e2e
-
f09e2e
 #endif /* WITH_SELINUX */
f09e2e
 
f09e2e
 #ifdef LINUX_OOM_ADJUST
f09e2e
diff -up openssh-6.3p1/openbsd-compat/port-linux_part_2.c.role-mls openssh-6.3p1/openbsd-compat/port-linux_part_2.c
f09e2e
--- openssh-6.3p1/openbsd-compat/port-linux_part_2.c.role-mls	2013-10-10 14:34:43.839494355 +0200
f09e2e
+++ openssh-6.3p1/openbsd-compat/port-linux_part_2.c	2013-10-10 14:34:43.839494355 +0200
f09e2e
@@ -0,0 +1,75 @@
f09e2e
+/* $Id: port-linux.c,v 1.11.4.2 2011/02/04 00:43:08 djm Exp $ */
f09e2e
+
f09e2e
+/*
f09e2e
+ * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
f09e2e
+ * Copyright (c) 2006 Damien Miller <djm@openbsd.org>
f09e2e
+ *
f09e2e
+ * Permission to use, copy, modify, and distribute this software for any
f09e2e
+ * purpose with or without fee is hereby granted, provided that the above
f09e2e
+ * copyright notice and this permission notice appear in all copies.
f09e2e
+ *
f09e2e
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
f09e2e
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
f09e2e
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
f09e2e
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
f09e2e
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
f09e2e
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
f09e2e
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f09e2e
+ */
f09e2e
+
f09e2e
+/*
f09e2e
+ * Linux-specific portability code - just SELinux support at present
f09e2e
+ */
f09e2e
+
f09e2e
+#include "includes.h"
f09e2e
+
f09e2e
+#if defined(WITH_SELINUX) || defined(LINUX_OOM_ADJUST)
f09e2e
+#include <errno.h>
f09e2e
+#include <stdarg.h>
f09e2e
+#include <string.h>
f09e2e
+#include <stdio.h>
f09e2e
+
f09e2e
+#include "log.h"
f09e2e
+#include "xmalloc.h"
f09e2e
+#include "port-linux.h"
f09e2e
+#include "key.h"
f09e2e
+#include "hostfile.h"
f09e2e
+#include "auth.h"
f09e2e
+
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+#include <selinux/selinux.h>
f09e2e
+#include <selinux/flask.h>
f09e2e
+#include <selinux/get_context_list.h>
f09e2e
+
f09e2e
+/* Wrapper around is_selinux_enabled() to log its return value once only */
f09e2e
+int
f09e2e
+ssh_selinux_enabled(void)
f09e2e
+{
f09e2e
+	static int enabled = -1;
f09e2e
+
f09e2e
+	if (enabled == -1) {
f09e2e
+		enabled = (is_selinux_enabled() == 1);
f09e2e
+		debug("SELinux support %s", enabled ? "enabled" : "disabled");
f09e2e
+	}
f09e2e
+
f09e2e
+	return (enabled);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+ssh_selinux_setfscreatecon(const char *path)
f09e2e
+{
f09e2e
+	security_context_t context;
f09e2e
+
f09e2e
+	if (!ssh_selinux_enabled())
f09e2e
+		return;
f09e2e
+	if (path == NULL) {
f09e2e
+		setfscreatecon(NULL);
f09e2e
+		return;
f09e2e
+	}
f09e2e
+	if (matchpathcon(path, 0700, &context) == 0)
f09e2e
+		setfscreatecon(context);
f09e2e
+}
f09e2e
+
f09e2e
+#endif /* WITH_SELINUX */
f09e2e
+
f09e2e
+#endif /* WITH_SELINUX || LINUX_OOM_ADJUST */
f09e2e
diff -up openssh-6.3p1/sshd.c.role-mls openssh-6.3p1/sshd.c
f09e2e
--- openssh-6.3p1/sshd.c.role-mls	2013-10-10 14:34:43.824494427 +0200
f09e2e
+++ openssh-6.3p1/sshd.c	2013-10-10 14:34:43.839494355 +0200
f09e2e
@@ -2179,6 +2179,9 @@ main(int ac, char **av)
f09e2e
 		restore_uid();
f09e2e
 	}
f09e2e
 #endif
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	ssh_selinux_setup_exec_context(authctxt->pw->pw_name);
f09e2e
+#endif
f09e2e
 #ifdef USE_PAM
f09e2e
 	if (options.use_pam) {
f09e2e
 		do_pam_setcred(1);