vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/auth.h.role-mls openssh-6.2p1/auth.h
Petr Lautrbach 8a29de
--- openssh-6.2p1/auth.h.role-mls	2013-03-25 17:47:00.565746862 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/auth.h	2013-03-25 17:47:00.602747073 +0100
Petr Lautrbach 8a29de
@@ -59,6 +59,9 @@ struct Authctxt {
Petr Lautrbach 8a29de
 	char		*service;
Petr Lautrbach 8a29de
 	struct passwd	*pw;		/* set if 'valid' */
Petr Lautrbach 8a29de
 	char		*style;
Petr Lautrbach 8a29de
+#ifdef WITH_SELINUX
Petr Lautrbach 8a29de
+	char		*role;
Petr Lautrbach 8a29de
+#endif
Petr Lautrbach 8a29de
 	void		*kbdintctxt;
Petr Lautrbach 8a29de
 	void		*jpake_ctx;
Petr Lautrbach 8a29de
 #ifdef BSD_AUTH
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/auth-pam.c.role-mls openssh-6.2p1/auth-pam.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/auth-pam.c.role-mls	2013-03-25 17:47:00.535746690 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/auth-pam.c	2013-03-25 17:47:00.602747073 +0100
Petr Lautrbach 8a29de
@@ -1074,7 +1074,7 @@ is_pam_session_open(void)
Petr Lautrbach 8a29de
  * during the ssh authentication process.
Petr Lautrbach 8a29de
  */
Petr Lautrbach 8a29de
 int
Petr Lautrbach 8a29de
-do_pam_putenv(char *name, char *value)
Petr Lautrbach 8a29de
+do_pam_putenv(char *name, const char *value)
Petr Lautrbach 8a29de
 {
Petr Lautrbach 8a29de
 	int ret = 1;
Petr Lautrbach 8a29de
 #ifdef HAVE_PAM_PUTENV
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/auth-pam.h.role-mls openssh-6.2p1/auth-pam.h
Petr Lautrbach 8a29de
--- openssh-6.2p1/auth-pam.h.role-mls	2004-09-11 14:17:26.000000000 +0200
Petr Lautrbach 8a29de
+++ openssh-6.2p1/auth-pam.h	2013-03-25 17:47:00.602747073 +0100
Petr Lautrbach 8a29de
@@ -38,7 +38,7 @@ void do_pam_session(void);
Petr Lautrbach 8a29de
 void do_pam_set_tty(const char *);
Petr Lautrbach 8a29de
 void do_pam_setcred(int );
Petr Lautrbach 8a29de
 void do_pam_chauthtok(void);
Petr Lautrbach 8a29de
-int do_pam_putenv(char *, char *);
Petr Lautrbach 8a29de
+int do_pam_putenv(char *, const char *);
Petr Lautrbach 8a29de
 char ** fetch_pam_environment(void);
Petr Lautrbach 8a29de
 char ** fetch_pam_child_environment(void);
Petr Lautrbach 8a29de
 void free_pam_environment(char **);
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/auth1.c.role-mls openssh-6.2p1/auth1.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/auth1.c.role-mls	2012-12-02 23:53:20.000000000 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/auth1.c	2013-03-25 17:47:00.600747062 +0100
Petr Lautrbach 8a29de
@@ -386,6 +386,9 @@ do_authentication(Authctxt *authctxt)
Petr Lautrbach 65ba94
 {
Petr Lautrbach 65ba94
 	u_int ulen;
Petr Lautrbach 65ba94
 	char *user, *style = NULL;
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+	char *role=NULL;
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	/* Get the name of the user that we wish to log in as. */
Petr Lautrbach 65ba94
 	packet_read_expect(SSH_CMSG_USER);
Petr Lautrbach 8a29de
@@ -394,11 +397,24 @@ do_authentication(Authctxt *authctxt)
Petr Lautrbach 65ba94
 	user = packet_get_cstring(&ulen);
Petr Lautrbach 65ba94
 	packet_check_eom();
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+	if ((role = strchr(user, '/')) != NULL)
Petr Lautrbach 65ba94
+		*role++ = '\0';
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
 	if ((style = strchr(user, ':')) != NULL)
Petr Lautrbach 65ba94
 		*style++ = '\0';
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+	else
Petr Lautrbach 65ba94
+		if (role && (style = strchr(role, ':')) != NULL)
Petr Lautrbach 65ba94
+			*style++ = '\0';
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	authctxt->user = user;
Petr Lautrbach 65ba94
 	authctxt->style = style;
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+	authctxt->role = role;
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	/* Verify that the user is a valid user. */
Petr Lautrbach 65ba94
 	if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/auth2.c.role-mls openssh-6.2p1/auth2.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/auth2.c.role-mls	2013-03-25 17:47:00.556746810 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/auth2.c	2013-03-25 17:47:00.600747062 +0100
Petr Lautrbach 5039c7
@@ -218,6 +218,9 @@ input_userauth_request(int type, u_int32
Petr Lautrbach 65ba94
 	Authctxt *authctxt = ctxt;
Petr Lautrbach 65ba94
 	Authmethod *m = NULL;
Petr Lautrbach 5039c7
 	char *user, *service, *method, *style = NULL;
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+	char *role = NULL;
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
 	int authenticated = 0;
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	if (authctxt == NULL)
Petr Lautrbach 5039c7
@@ -229,6 +232,11 @@ input_userauth_request(int type, u_int32
Petr Lautrbach 65ba94
 	debug("userauth-request for user %s service %s method %s", user, service, method);
Petr Lautrbach 65ba94
 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+	if ((role = strchr(user, '/')) != NULL)
Petr Lautrbach 65ba94
+		*role++ = 0;
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
 	if ((style = strchr(user, ':')) != NULL)
Petr Lautrbach 65ba94
 		*style++ = 0;
Petr Lautrbach 65ba94
 
Petr Lautrbach 5039c7
@@ -251,8 +259,15 @@ input_userauth_request(int type, u_int32
Petr Lautrbach 65ba94
 		    use_privsep ? " [net]" : "");
Petr Lautrbach 65ba94
 		authctxt->service = xstrdup(service);
Petr Lautrbach 65ba94
 		authctxt->style = style ? xstrdup(style) : NULL;
Petr Lautrbach 65ba94
-		if (use_privsep)
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+		authctxt->role = role ? xstrdup(role) : NULL;
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+		if (use_privsep) {
Petr Lautrbach 65ba94
 			mm_inform_authserv(service, style);
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+			mm_inform_authrole(role);
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
 		userauth_banner();
Petr Lautrbach 5039c7
 		if (auth2_setup_methods_lists(authctxt) != 0)
Petr Lautrbach 5039c7
 			packet_disconnect("no authentication methods enabled");
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/auth2-gss.c.role-mls openssh-6.2p1/auth2-gss.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/auth2-gss.c.role-mls	2012-12-02 23:53:20.000000000 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/auth2-gss.c	2013-03-25 17:47:00.601747067 +0100
Petr Lautrbach 65ba94
@@ -260,6 +260,7 @@ input_gssapi_mic(int type, u_int32_t ple
Petr Lautrbach 65ba94
 	Authctxt *authctxt = ctxt;
Petr Lautrbach 65ba94
 	Gssctxt *gssctxt;
Petr Lautrbach 65ba94
 	int authenticated = 0;
Petr Lautrbach 65ba94
+	char *micuser;
Petr Lautrbach 65ba94
 	Buffer b;
Petr Lautrbach 65ba94
 	gss_buffer_desc mic, gssbuf;
Petr Lautrbach 65ba94
 	u_int len;
Petr Lautrbach 65ba94
@@ -272,7 +273,13 @@ input_gssapi_mic(int type, u_int32_t ple
Petr Lautrbach 65ba94
 	mic.value = packet_get_string(&len;;
Petr Lautrbach 65ba94
 	mic.length = len;
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
-	ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+	if (authctxt->role && (strlen(authctxt->role) > 0))
Petr Lautrbach 65ba94
+		xasprintf(&micuser, "%s/%s", authctxt->user, authctxt->role);
Petr Lautrbach 65ba94
+	else
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+		micuser = authctxt->user;
Petr Lautrbach 65ba94
+	ssh_gssapi_buildmic(&b, micuser, authctxt->service,
Petr Lautrbach 65ba94
 	    "gssapi-with-mic");
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	gssbuf.value = buffer_ptr(&b);
Petr Lautrbach 65ba94
@@ -284,6 +291,8 @@ input_gssapi_mic(int type, u_int32_t ple
Petr Lautrbach 65ba94
 		logit("GSSAPI MIC check failed");
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	buffer_free(&b);
Petr Lautrbach 65ba94
+	if (micuser != authctxt->user)
Petr Lautrbach 65ba94
+		xfree(micuser);
Petr Lautrbach 65ba94
 	xfree(mic.value);
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	authctxt->postponed = 0;
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/auth2-hostbased.c.role-mls openssh-6.2p1/auth2-hostbased.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/auth2-hostbased.c.role-mls	2013-03-25 17:47:00.565746862 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/auth2-hostbased.c	2013-03-25 17:47:00.601747067 +0100
Petr Lautrbach 65ba94
@@ -106,7 +106,15 @@ userauth_hostbased(Authctxt *authctxt)
Petr Lautrbach 65ba94
 	buffer_put_string(&b, session_id2, session_id2_len);
Petr Lautrbach 65ba94
 	/* reconstruct packet */
Petr Lautrbach 65ba94
 	buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Petr Lautrbach 65ba94
-	buffer_put_cstring(&b, authctxt->user);
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+	if (authctxt->role) {
Petr Lautrbach 65ba94
+		buffer_put_int(&b, strlen(authctxt->user)+strlen(authctxt->role)+1);
Petr Lautrbach 65ba94
+		buffer_append(&b, authctxt->user, strlen(authctxt->user));
Petr Lautrbach 65ba94
+		buffer_put_char(&b, '/');
Petr Lautrbach 65ba94
+		buffer_append(&b, authctxt->role, strlen(authctxt->role));
Petr Lautrbach 65ba94
+	} else 
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+		buffer_put_cstring(&b, authctxt->user);
Petr Lautrbach 65ba94
 	buffer_put_cstring(&b, service);
Petr Lautrbach 65ba94
 	buffer_put_cstring(&b, "hostbased");
Petr Lautrbach 65ba94
 	buffer_put_string(&b, pkalg, alen);
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/auth2-pubkey.c.role-mls openssh-6.2p1/auth2-pubkey.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/auth2-pubkey.c.role-mls	2013-03-25 17:47:00.565746862 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/auth2-pubkey.c	2013-03-25 17:47:00.601747067 +0100
Petr Lautrbach 8a29de
@@ -127,7 +127,15 @@ userauth_pubkey(Authctxt *authctxt)
Petr Lautrbach 65ba94
 		}
Petr Lautrbach 65ba94
 		/* reconstruct packet */
Petr Lautrbach 65ba94
 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Petr Lautrbach 65ba94
-		buffer_put_cstring(&b, authctxt->user);
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+		if (authctxt->role) {
Petr Lautrbach 65ba94
+			buffer_put_int(&b, strlen(authctxt->user)+strlen(authctxt->role)+1);
Petr Lautrbach 65ba94
+			buffer_append(&b, authctxt->user, strlen(authctxt->user));
Petr Lautrbach 65ba94
+			buffer_put_char(&b, '/');
Petr Lautrbach 65ba94
+			buffer_append(&b, authctxt->role, strlen(authctxt->role));
Petr Lautrbach 65ba94
+		} else 
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+			buffer_put_cstring(&b, authctxt->user);
Petr Lautrbach 65ba94
 		buffer_put_cstring(&b,
Petr Lautrbach 65ba94
 		    datafellows & SSH_BUG_PKSERVICE ?
Petr Lautrbach 65ba94
 		    "ssh-userauth" :
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/misc.c.role-mls openssh-6.2p1/misc.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/misc.c.role-mls	2011-09-22 13:34:36.000000000 +0200
Petr Lautrbach 8a29de
+++ openssh-6.2p1/misc.c	2013-03-25 17:47:00.603747079 +0100
Petr Lautrbach 65ba94
@@ -427,6 +427,7 @@ char *
Petr Lautrbach 65ba94
 colon(char *cp)
Petr Lautrbach 65ba94
 {
Petr Lautrbach 65ba94
 	int flag = 0;
Petr Lautrbach 65ba94
+	int start = 1;
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	if (*cp == ':')		/* Leading colon is part of file name. */
Petr Lautrbach 65ba94
 		return NULL;
Petr Lautrbach 65ba94
@@ -442,6 +443,13 @@ colon(char *cp)
Petr Lautrbach 65ba94
 			return (cp);
Petr Lautrbach 65ba94
 		if (*cp == '/')
Petr Lautrbach 65ba94
 			return NULL;
Petr Lautrbach 65ba94
+		if (start) {
Petr Lautrbach 65ba94
+		/* Slash on beginning or after dots only denotes file name. */
Petr Lautrbach 65ba94
+			if (*cp == '/')
Petr Lautrbach 65ba94
+				return (0);
Petr Lautrbach 65ba94
+			if (*cp != '.')
Petr Lautrbach 65ba94
+				start = 0;
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
 	}
Petr Lautrbach 65ba94
 	return NULL;
Petr Lautrbach 65ba94
 }
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/monitor.c.role-mls openssh-6.2p1/monitor.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/monitor.c.role-mls	2013-03-25 17:47:00.587746987 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/monitor.c	2013-03-25 17:47:00.604747085 +0100
Petr Lautrbach 65ba94
@@ -148,6 +148,9 @@ int mm_answer_sign(int, Buffer *);
Petr Lautrbach 65ba94
 int mm_answer_pwnamallow(int, Buffer *);
Petr Lautrbach 65ba94
 int mm_answer_auth2_read_banner(int, Buffer *);
Petr Lautrbach 65ba94
 int mm_answer_authserv(int, Buffer *);
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+int mm_answer_authrole(int, Buffer *);
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
 int mm_answer_authpassword(int, Buffer *);
Petr Lautrbach 65ba94
 int mm_answer_bsdauthquery(int, Buffer *);
Petr Lautrbach 65ba94
 int mm_answer_bsdauthrespond(int, Buffer *);
Petr Lautrbach 8a29de
@@ -232,6 +235,9 @@ struct mon_table mon_dispatch_proto20[]
Petr Lautrbach 65ba94
     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
Petr Lautrbach 65ba94
     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
Petr Lautrbach 65ba94
     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+    {MONITOR_REQ_AUTHROLE, MON_ONCE, mm_answer_authrole},
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Petr Lautrbach 65ba94
     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Petr Lautrbach 65ba94
 #ifdef USE_PAM
Petr Lautrbach 8a29de
@@ -846,6 +852,9 @@ mm_answer_pwnamallow(int sock, Buffer *m
Petr Lautrbach 65ba94
 	else {
Petr Lautrbach 65ba94
 		/* Allow service/style information on the auth context */
Petr Lautrbach 65ba94
 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHROLE, 1);
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
Petr Lautrbach 65ba94
 	}
Petr Lautrbach 65ba94
 #ifdef USE_PAM
Petr Lautrbach 8a29de
@@ -889,6 +898,25 @@ mm_answer_authserv(int sock, Buffer *m)
Petr Lautrbach 65ba94
 	return (0);
Petr Lautrbach 65ba94
 }
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+int
Petr Lautrbach 65ba94
+mm_answer_authrole(int sock, Buffer *m)
Petr Lautrbach 65ba94
+{
Petr Lautrbach 65ba94
+	monitor_permit_authentications(1);
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	authctxt->role = buffer_get_string(m, NULL);
Petr Lautrbach 65ba94
+	debug3("%s: role=%s",
Petr Lautrbach 65ba94
+	    __func__, authctxt->role);
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	if (strlen(authctxt->role) == 0) {
Petr Lautrbach 65ba94
+		xfree(authctxt->role);
Petr Lautrbach 65ba94
+		authctxt->role = NULL;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	return (0);
Petr Lautrbach 65ba94
+}
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
 int
Petr Lautrbach 65ba94
 mm_answer_authpassword(int sock, Buffer *m)
Petr Lautrbach 65ba94
 {
Petr Lautrbach 8a29de
@@ -1262,7 +1290,7 @@ static int
Petr Lautrbach 65ba94
 monitor_valid_userblob(u_char *data, u_int datalen)
Petr Lautrbach 65ba94
 {
Petr Lautrbach 65ba94
 	Buffer b;
Petr Lautrbach 65ba94
-	char *p;
Petr Lautrbach 65ba94
+	char *p, *r;
Petr Lautrbach 65ba94
 	u_int len;
Petr Lautrbach 65ba94
 	int fail = 0;
Petr Lautrbach 65ba94
 
Petr Lautrbach 8a29de
@@ -1288,6 +1316,8 @@ monitor_valid_userblob(u_char *data, u_i
Petr Lautrbach 65ba94
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
Petr Lautrbach 65ba94
 		fail++;
Petr Lautrbach 65ba94
 	p = buffer_get_string(&b, NULL);
Petr Lautrbach 65ba94
+	if ((r = strchr(p, '/')) != NULL)
Petr Lautrbach 65ba94
+		*r = '\0';
Petr Lautrbach 65ba94
 	if (strcmp(authctxt->user, p) != 0) {
Petr Lautrbach 65ba94
 		logit("wrong user name passed to monitor: expected %s != %.100s",
Petr Lautrbach 65ba94
 		    authctxt->user, p);
Petr Lautrbach 8a29de
@@ -1319,7 +1349,7 @@ monitor_valid_hostbasedblob(u_char *data
Petr Lautrbach 65ba94
     char *chost)
Petr Lautrbach 65ba94
 {
Petr Lautrbach 65ba94
 	Buffer b;
Petr Lautrbach 65ba94
-	char *p;
Petr Lautrbach 65ba94
+	char *p, *r;
Petr Lautrbach 65ba94
 	u_int len;
Petr Lautrbach 65ba94
 	int fail = 0;
Petr Lautrbach 65ba94
 
Petr Lautrbach 8a29de
@@ -1336,6 +1366,8 @@ monitor_valid_hostbasedblob(u_char *data
Petr Lautrbach 65ba94
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
Petr Lautrbach 65ba94
 		fail++;
Petr Lautrbach 65ba94
 	p = buffer_get_string(&b, NULL);
Petr Lautrbach 65ba94
+	if ((r = strchr(p, '/')) != NULL)
Petr Lautrbach 65ba94
+		*r = '\0';
Petr Lautrbach 65ba94
 	if (strcmp(authctxt->user, p) != 0) {
Petr Lautrbach 65ba94
 		logit("wrong user name passed to monitor: expected %s != %.100s",
Petr Lautrbach 65ba94
 		    authctxt->user, p);
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/monitor.h.role-mls openssh-6.2p1/monitor.h
Petr Lautrbach 8a29de
--- openssh-6.2p1/monitor.h.role-mls	2013-03-25 17:47:00.605747090 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/monitor.h	2013-03-25 17:50:00.824775483 +0100
Petr Lautrbach 8a29de
@@ -61,6 +61,9 @@ enum monitor_reqtype {
Petr Lautrbach 8a29de
 	MONITOR_REQ_JPAKE_STEP2 = 56, MONITOR_ANS_JPAKE_STEP2 = 57,
Petr Lautrbach 8a29de
 	MONITOR_REQ_JPAKE_KEY_CONFIRM = 58, MONITOR_ANS_JPAKE_KEY_CONFIRM = 59,
Petr Lautrbach 8a29de
 	MONITOR_REQ_JPAKE_CHECK_CONFIRM = 60, MONITOR_ANS_JPAKE_CHECK_CONFIRM = 61,
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 8a29de
+	MONITOR_REQ_AUTHROLE = 80,
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 8a29de
 
Petr Lautrbach 8a29de
 	MONITOR_REQ_PAM_START = 100,
Petr Lautrbach 8a29de
 	MONITOR_REQ_PAM_ACCOUNT = 102, MONITOR_ANS_PAM_ACCOUNT = 103,
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/monitor_wrap.c.role-mls openssh-6.2p1/monitor_wrap.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/monitor_wrap.c.role-mls	2013-03-25 17:47:00.588746993 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/monitor_wrap.c	2013-03-25 17:47:00.605747090 +0100
Petr Lautrbach 65ba94
@@ -336,6 +336,25 @@ mm_inform_authserv(char *service, char *
Petr Lautrbach 65ba94
 	buffer_free(&m);
Petr Lautrbach 65ba94
 }
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
+/* Inform the privileged process about role */
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+void
Petr Lautrbach 65ba94
+mm_inform_authrole(char *role)
Petr Lautrbach 65ba94
+{
Petr Lautrbach 65ba94
+	Buffer m;
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	debug3("%s entering", __func__);
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	buffer_init(&m);
Petr Lautrbach 65ba94
+	buffer_put_cstring(&m, role ? role : "");
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHROLE, &m);
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	buffer_free(&m);
Petr Lautrbach 65ba94
+}
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
 /* Do the password authentication */
Petr Lautrbach 65ba94
 int
Petr Lautrbach 65ba94
 mm_auth_password(Authctxt *authctxt, char *password)
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/monitor_wrap.h.role-mls openssh-6.2p1/monitor_wrap.h
Petr Lautrbach 8a29de
--- openssh-6.2p1/monitor_wrap.h.role-mls	2013-03-25 17:47:00.588746993 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/monitor_wrap.h	2013-03-25 17:47:00.605747090 +0100
Petr Lautrbach 65ba94
@@ -42,6 +42,9 @@ int mm_is_monitor(void);
Petr Lautrbach 65ba94
 DH *mm_choose_dh(int, int, int);
Petr Lautrbach 65ba94
 int mm_key_sign(Key *, u_char **, u_int *, u_char *, u_int);
Petr Lautrbach 65ba94
 void mm_inform_authserv(char *, char *);
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+void mm_inform_authrole(char *);
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
 struct passwd *mm_getpwnamallow(const char *);
Petr Lautrbach 65ba94
 char *mm_auth2_read_banner(void);
Petr Lautrbach 65ba94
 int mm_auth_password(struct Authctxt *, char *);
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/openbsd-compat/Makefile.in.role-mls openssh-6.2p1/openbsd-compat/Makefile.in
Petr Lautrbach 8a29de
--- openssh-6.2p1/openbsd-compat/Makefile.in.role-mls	2013-03-25 17:47:00.606747096 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/openbsd-compat/Makefile.in	2013-03-25 17:50:36.024979473 +0100
Petr Lautrbach 65ba94
@@ -20,7 +20,7 @@ OPENBSD=base64.o basename.o bindresvport
Petr Lautrbach 65ba94
 
Petr Lautrbach 8a29de
 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
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
-PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o
Petr Lautrbach 65ba94
+PORTS=port-aix.o port-irix.o port-linux.o port-linux_part_2.o port-solaris.o port-tun.o port-uw.o
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 .c.o:
Petr Lautrbach 65ba94
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/openbsd-compat/port-linux.c.role-mls openssh-6.2p1/openbsd-compat/port-linux.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/openbsd-compat/port-linux.c.role-mls	2012-03-09 00:25:18.000000000 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/openbsd-compat/port-linux.c	2013-03-25 17:47:00.606747096 +0100
Petr Lautrbach 65ba94
@@ -31,68 +31,271 @@
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 #include "log.h"
Petr Lautrbach 65ba94
 #include "xmalloc.h"
Petr Lautrbach 65ba94
+#include "servconf.h"
Petr Lautrbach 65ba94
 #include "port-linux.h"
Petr Lautrbach 65ba94
+#include "key.h"
Petr Lautrbach 65ba94
+#include "hostfile.h"
Petr Lautrbach 65ba94
+#include "auth.h"
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 #ifdef WITH_SELINUX
Petr Lautrbach 65ba94
 #include <selinux/selinux.h>
Petr Lautrbach 65ba94
 #include <selinux/flask.h>
Petr Lautrbach 65ba94
+#include <selinux/context.h>
Petr Lautrbach 65ba94
 #include <selinux/get_context_list.h>
Petr Lautrbach 65ba94
+#include <selinux/get_default_type.h>
Petr Lautrbach 65ba94
+#include <selinux/av_permissions.h>
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+#ifdef HAVE_LINUX_AUDIT
Petr Lautrbach 65ba94
+#include <libaudit.h>
Petr Lautrbach 65ba94
+#include <unistd.h>
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 #ifndef SSH_SELINUX_UNCONFINED_TYPE
Petr Lautrbach 65ba94
 # define SSH_SELINUX_UNCONFINED_TYPE ":unconfined_t:"
Petr Lautrbach 65ba94
 #endif
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
-/* Wrapper around is_selinux_enabled() to log its return value once only */
Petr Lautrbach 65ba94
-int
Petr Lautrbach 65ba94
-ssh_selinux_enabled(void)
Petr Lautrbach 65ba94
+extern ServerOptions options;
Petr Lautrbach 65ba94
+extern Authctxt *the_authctxt;
Petr Lautrbach 65ba94
+extern int inetd_flag;
Petr Lautrbach 65ba94
+extern int rexeced_flag;
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+/* Send audit message */
Petr Lautrbach 65ba94
+static int
Petr Lautrbach 65ba94
+send_audit_message(int success, security_context_t default_context,
Petr Lautrbach 65ba94
+		       security_context_t selected_context)
Petr Lautrbach 65ba94
+{
Petr Lautrbach 65ba94
+	int rc=0;
Petr Lautrbach 65ba94
+#ifdef HAVE_LINUX_AUDIT
Petr Lautrbach 65ba94
+	char *msg = NULL;
Petr Lautrbach 65ba94
+	int audit_fd = audit_open();
Petr Lautrbach 65ba94
+	security_context_t default_raw=NULL;
Petr Lautrbach 65ba94
+	security_context_t selected_raw=NULL;
Petr Lautrbach 65ba94
+	rc = -1;
Petr Lautrbach 65ba94
+	if (audit_fd < 0) {
Petr Lautrbach 65ba94
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Petr Lautrbach 65ba94
+					errno == EAFNOSUPPORT)
Petr Lautrbach 65ba94
+				return 0; /* No audit support in kernel */
Petr Lautrbach 65ba94
+		error("Error connecting to audit system.");
Petr Lautrbach 65ba94
+		return rc;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+	if (selinux_trans_to_raw_context(default_context, &default_raw) < 0) {
Petr Lautrbach 65ba94
+		error("Error translating default context.");
Petr Lautrbach 65ba94
+		default_raw = NULL;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+	if (selinux_trans_to_raw_context(selected_context, &selected_raw) < 0) {
Petr Lautrbach 65ba94
+		error("Error translating selected context.");
Petr Lautrbach 65ba94
+		selected_raw = NULL;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+	if (asprintf(&msg, "sshd: default-context=%s selected-context=%s",
Petr Lautrbach 65ba94
+		     default_raw ? default_raw : (default_context ? default_context: "?"),
Petr Lautrbach 65ba94
+		     selected_context ? selected_raw : (selected_context ? selected_context :"?")) < 0) {
Petr Lautrbach 65ba94
+		error("Error allocating memory.");
Petr Lautrbach 65ba94
+		goto out;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+	if (audit_log_user_message(audit_fd, AUDIT_USER_ROLE_CHANGE,
Petr Lautrbach 65ba94
+				   msg, NULL, NULL, NULL, success) <= 0) {
Petr Lautrbach 65ba94
+		error("Error sending audit message.");
Petr Lautrbach 65ba94
+		goto out;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+	rc = 0;
Petr Lautrbach 65ba94
+      out:
Petr Lautrbach 65ba94
+	free(msg);
Petr Lautrbach 65ba94
+	freecon(default_raw);
Petr Lautrbach 65ba94
+	freecon(selected_raw);
Petr Lautrbach 65ba94
+	close(audit_fd);
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+	return rc;
Petr Lautrbach 65ba94
+}
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+static int
Petr Lautrbach 65ba94
+mls_range_allowed(security_context_t src, security_context_t dst)
Petr Lautrbach 65ba94
 {
Petr Lautrbach 65ba94
-	static int enabled = -1;
Petr Lautrbach 65ba94
+	struct av_decision avd;
Petr Lautrbach 65ba94
+	int retval;
Petr Lautrbach 65ba94
+	unsigned int bit = CONTEXT__CONTAINS;
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	debug("%s: src:%s dst:%s", __func__, src, dst);
Petr Lautrbach 65ba94
+	retval = security_compute_av(src, dst, SECCLASS_CONTEXT, bit, &avd);
Petr Lautrbach 65ba94
+	if (retval || ((bit & avd.allowed) != bit))
Petr Lautrbach 65ba94
+		return 0;
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	return 1;
Petr Lautrbach 65ba94
+}
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+static int
Petr Lautrbach 65ba94
+get_user_context(const char *sename, const char *role, const char *lvl,
Petr Lautrbach 65ba94
+	security_context_t *sc) {
Petr Lautrbach 65ba94
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Petr Lautrbach 65ba94
+	if (lvl == NULL || lvl[0] == '\0' || get_default_context_with_level(sename, lvl, NULL, sc) != 0) {
Petr Lautrbach 65ba94
+	        /* User may have requested a level completely outside of his 
Petr Lautrbach 65ba94
+	           allowed range. We get a context just for auditing as the
Petr Lautrbach 65ba94
+	           range check below will certainly fail for default context. */
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+		if (get_default_context(sename, NULL, sc) != 0) {
Petr Lautrbach 65ba94
+			*sc = NULL;
Petr Lautrbach 65ba94
+			return -1;
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+	if (role != NULL && role[0]) {
Petr Lautrbach 65ba94
+		context_t con;
Petr Lautrbach 65ba94
+		char *type=NULL;
Petr Lautrbach 65ba94
+		if (get_default_type(role, &type) != 0) {
Petr Lautrbach 65ba94
+			error("get_default_type: failed to get default type for '%s'",
Petr Lautrbach 65ba94
+				role);
Petr Lautrbach 65ba94
+			goto out;
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
+		con = context_new(*sc);
Petr Lautrbach 65ba94
+		if (!con) {
Petr Lautrbach 65ba94
+			goto out;
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
+		context_role_set(con, role);
Petr Lautrbach 65ba94
+		context_type_set(con, type);
Petr Lautrbach 65ba94
+		freecon(*sc);
Petr Lautrbach 65ba94
+		*sc = strdup(context_str(con));
Petr Lautrbach 65ba94
+		context_free(con);
Petr Lautrbach 65ba94
+		if (!*sc) 
Petr Lautrbach 65ba94
+			return -1;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Petr Lautrbach 65ba94
+	if (lvl != NULL && lvl[0]) {
Petr Lautrbach 65ba94
+		/* verify that the requested range is obtained */
Petr Lautrbach 65ba94
+		context_t con;
Petr Lautrbach 65ba94
+		security_context_t obtained_raw;
Petr Lautrbach 65ba94
+		security_context_t requested_raw;
Petr Lautrbach 65ba94
+		con = context_new(*sc);
Petr Lautrbach 65ba94
+		if (!con) {
Petr Lautrbach 65ba94
+			goto out;
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
+		context_range_set(con, lvl);
Petr Lautrbach 65ba94
+		if (selinux_trans_to_raw_context(*sc, &obtained_raw) < 0) {
Petr Lautrbach 65ba94
+			context_free(con);
Petr Lautrbach 65ba94
+			goto out;
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
+		if (selinux_trans_to_raw_context(context_str(con), &requested_raw) < 0) {
Petr Lautrbach 65ba94
+			freecon(obtained_raw);
Petr Lautrbach 65ba94
+			context_free(con);
Petr Lautrbach 65ba94
+			goto out;
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
-	if (enabled == -1) {
Petr Lautrbach 65ba94
-		enabled = (is_selinux_enabled() == 1);
Petr Lautrbach 65ba94
-		debug("SELinux support %s", enabled ? "enabled" : "disabled");
Petr Lautrbach 65ba94
+		debug("get_user_context: obtained context '%s' requested context '%s'",
Petr Lautrbach 65ba94
+			obtained_raw, requested_raw);
Petr Lautrbach 65ba94
+		if (strcmp(obtained_raw, requested_raw)) {
Petr Lautrbach 65ba94
+			/* set the context to the real requested one but fail */
Petr Lautrbach 65ba94
+			freecon(requested_raw);
Petr Lautrbach 65ba94
+			freecon(obtained_raw);
Petr Lautrbach 65ba94
+			freecon(*sc);
Petr Lautrbach 65ba94
+			*sc = strdup(context_str(con));
Petr Lautrbach 65ba94
+			context_free(con);
Petr Lautrbach 65ba94
+			return -1;
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
+		freecon(requested_raw);
Petr Lautrbach 65ba94
+		freecon(obtained_raw);
Petr Lautrbach 65ba94
+		context_free(con);
Petr Lautrbach 65ba94
 	}
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+	return 0;
Petr Lautrbach 65ba94
+      out:
Petr Lautrbach 65ba94
+	freecon(*sc);
Petr Lautrbach 65ba94
+	*sc = NULL;
Petr Lautrbach 65ba94
+	return -1;
Petr Lautrbach 65ba94
+}
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
-	return (enabled);
Petr Lautrbach 65ba94
+static void
Petr Lautrbach 65ba94
+ssh_selinux_get_role_level(char **role, const char **level)
Petr Lautrbach 65ba94
+{
Petr Lautrbach 65ba94
+	*role = NULL;
Petr Lautrbach 65ba94
+	*level = NULL;
Petr Lautrbach 65ba94
+	if (the_authctxt) {
Petr Lautrbach 65ba94
+		if (the_authctxt->role != NULL) {
Petr Lautrbach 65ba94
+			char *slash;
Petr Lautrbach 65ba94
+			*role = xstrdup(the_authctxt->role);
Petr Lautrbach 65ba94
+			if ((slash = strchr(*role, '/')) != NULL) {
Petr Lautrbach 65ba94
+				*slash = '\0';
Petr Lautrbach 65ba94
+				*level = slash + 1;
Petr Lautrbach 65ba94
+			}
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
 }
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 /* Return the default security context for the given username */
Petr Lautrbach 65ba94
 static security_context_t
Petr Lautrbach 65ba94
-ssh_selinux_getctxbyname(char *pwname)
Petr Lautrbach 65ba94
+ssh_selinux_getctxbyname(char *pwname,
Petr Lautrbach 65ba94
+	security_context_t *default_sc, security_context_t *user_sc)
Petr Lautrbach 65ba94
 {
Petr Lautrbach 65ba94
-	security_context_t sc = NULL;
Petr Lautrbach 65ba94
-	char *sename = NULL, *lvl = NULL;
Petr Lautrbach 65ba94
-	int r;
Petr Lautrbach 65ba94
+	char *sename, *lvl;
Petr Lautrbach 65ba94
+	char *role;
Petr Lautrbach 65ba94
+	const char *reqlvl;
Petr Lautrbach 65ba94
+	int r = 0;
Petr Lautrbach 65ba94
+	context_t con = NULL;
Petr Lautrbach 65ba94
+ 
Petr Lautrbach 65ba94
+	ssh_selinux_get_role_level(&role, &reqlvl);
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 #ifdef HAVE_GETSEUSERBYNAME
Petr Lautrbach 65ba94
-	if (getseuserbyname(pwname, &sename, &lvl) != 0)
Petr Lautrbach 65ba94
-		return NULL;
Petr Lautrbach 65ba94
+	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
Petr Lautrbach 65ba94
+		sename = NULL;
Petr Lautrbach 65ba94
+		lvl = NULL;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
 #else
Petr Lautrbach 65ba94
 	sename = pwname;
Petr Lautrbach 65ba94
-	lvl = NULL;
Petr Lautrbach 65ba94
+	lvl = "";
Petr Lautrbach 65ba94
 #endif
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
+	if (r == 0) {
Petr Lautrbach 65ba94
 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Petr Lautrbach 65ba94
-	r = get_default_context_with_level(sename, lvl, NULL, &sc);
Petr Lautrbach 65ba94
+		r = get_default_context_with_level(sename, lvl, NULL, default_sc);
Petr Lautrbach 65ba94
 #else
Petr Lautrbach 65ba94
-	r = get_default_context(sename, NULL, &sc);
Petr Lautrbach 65ba94
+		r = get_default_context(sename, NULL, default_sc);
Petr Lautrbach 65ba94
 #endif
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	if (r == 0) {
Petr Lautrbach 65ba94
+		/* If launched from xinetd, we must use current level */
Petr Lautrbach 65ba94
+		if (inetd_flag && !rexeced_flag) {
Petr Lautrbach 65ba94
+			security_context_t sshdsc=NULL;
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+			if (getcon_raw(&sshdsc) < 0)
Petr Lautrbach 65ba94
+				fatal("failed to allocate security context");
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+			if ((con=context_new(sshdsc)) == NULL)
Petr Lautrbach 65ba94
+				fatal("failed to allocate selinux context");
Petr Lautrbach 65ba94
+			reqlvl = context_range_get(con);
Petr Lautrbach 65ba94
+			freecon(sshdsc);
Petr Lautrbach 65ba94
+			if (reqlvl !=NULL && lvl != NULL && strcmp(reqlvl, lvl) == 0)
Petr Lautrbach 65ba94
+			    /* we actually don't change level */
Petr Lautrbach 65ba94
+			    reqlvl = "";
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+			debug("%s: current connection level '%s'", __func__, reqlvl);
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
-	if (r != 0) {
Petr Lautrbach 65ba94
-		switch (security_getenforce()) {
Petr Lautrbach 65ba94
-		case -1:
Petr Lautrbach 65ba94
-			fatal("%s: ssh_selinux_getctxbyname: "
Petr Lautrbach 65ba94
-			    "security_getenforce() failed", __func__);
Petr Lautrbach 65ba94
-		case 0:
Petr Lautrbach 65ba94
-			error("%s: Failed to get default SELinux security "
Petr Lautrbach 65ba94
-			    "context for %s", __func__, pwname);
Petr Lautrbach 65ba94
-			sc = NULL;
Petr Lautrbach 65ba94
-			break;
Petr Lautrbach 65ba94
-		default:
Petr Lautrbach 65ba94
-			fatal("%s: Failed to get default SELinux security "
Petr Lautrbach 65ba94
-			    "context for %s (in enforcing mode)",
Petr Lautrbach 65ba94
-			    __func__, pwname);
Petr Lautrbach 65ba94
 		}
Petr Lautrbach 65ba94
+		
Petr Lautrbach 65ba94
+		if ((reqlvl != NULL && reqlvl[0]) || (role != NULL && role[0])) {
Petr Lautrbach 65ba94
+			r = get_user_context(sename, role, reqlvl, user_sc);
Petr Lautrbach 65ba94
+		
Petr Lautrbach 65ba94
+			if (r == 0 && reqlvl != NULL && reqlvl[0]) {
Petr Lautrbach 65ba94
+				security_context_t default_level_sc = *default_sc;
Petr Lautrbach 65ba94
+				if (role != NULL && role[0]) {
Petr Lautrbach 65ba94
+					if (get_user_context(sename, role, lvl, &default_level_sc) < 0)
Petr Lautrbach 65ba94
+						default_level_sc = *default_sc;
Petr Lautrbach 65ba94
+				}
Petr Lautrbach 65ba94
+				/* verify that the requested range is contained in the user range */
Petr Lautrbach 65ba94
+				if (mls_range_allowed(default_level_sc, *user_sc)) {
Petr Lautrbach 65ba94
+					logit("permit MLS level %s (user range %s)", reqlvl, lvl);
Petr Lautrbach 65ba94
+				} else {
Petr Lautrbach 65ba94
+					r = -1;
Petr Lautrbach 65ba94
+					error("deny MLS level %s (user range %s)", reqlvl, lvl);
Petr Lautrbach 65ba94
+				}
Petr Lautrbach 65ba94
+				if (default_level_sc != *default_sc)
Petr Lautrbach 65ba94
+					freecon(default_level_sc);
Petr Lautrbach 65ba94
+			}
Petr Lautrbach 65ba94
+		} else {
Petr Lautrbach 65ba94
+			*user_sc = *default_sc;
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+	if (r != 0) {
Petr Lautrbach 65ba94
+		error("%s: Failed to get default SELinux security "
Petr Lautrbach 65ba94
+		    "context for %s", __func__, pwname);
Petr Lautrbach 65ba94
 	}
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 #ifdef HAVE_GETSEUSERBYNAME
Petr Lautrbach 65ba94
@@ -102,7 +305,42 @@ ssh_selinux_getctxbyname(char *pwname)
Petr Lautrbach 65ba94
 		xfree(lvl);
Petr Lautrbach 65ba94
 #endif
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
-	return sc;
Petr Lautrbach 65ba94
+	if (role != NULL)
Petr Lautrbach 65ba94
+		xfree(role);
Petr Lautrbach 65ba94
+	if (con)
Petr Lautrbach 65ba94
+		context_free(con);
Petr Lautrbach 65ba94
+ 
Petr Lautrbach 65ba94
+	return (r);
Petr Lautrbach 65ba94
+}
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+/* Setup environment variables for pam_selinux */
Petr Lautrbach 65ba94
+static int
Petr Lautrbach 65ba94
+ssh_selinux_setup_pam_variables(void)
Petr Lautrbach 65ba94
+{
Petr Lautrbach 65ba94
+	const char *reqlvl;
Petr Lautrbach 65ba94
+	char *role;
Petr Lautrbach 65ba94
+	char *use_current;
Petr Lautrbach 65ba94
+	int rv;
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	debug3("%s: setting execution context", __func__);
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	ssh_selinux_get_role_level(&role, &reqlvl);
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
Petr Lautrbach 65ba94
+	
Petr Lautrbach 65ba94
+	if (inetd_flag && !rexeced_flag) {
Petr Lautrbach 65ba94
+		use_current = "1";
Petr Lautrbach 65ba94
+	} else {
Petr Lautrbach 65ba94
+		use_current = "";
Petr Lautrbach 65ba94
+		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	if (role != NULL)
Petr Lautrbach 65ba94
+		xfree(role);
Petr Lautrbach 65ba94
+	
Petr Lautrbach 65ba94
+	return rv;
Petr Lautrbach 65ba94
 }
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 /* Set the execution context to the default for the specified user */
Petr Lautrbach 65ba94
@@ -110,28 +348,71 @@ void
Petr Lautrbach 65ba94
 ssh_selinux_setup_exec_context(char *pwname)
Petr Lautrbach 65ba94
 {
Petr Lautrbach 65ba94
 	security_context_t user_ctx = NULL;
Petr Lautrbach 65ba94
+	int r = 0;
Petr Lautrbach 65ba94
+	security_context_t default_ctx = NULL;
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	if (!ssh_selinux_enabled())
Petr Lautrbach 65ba94
 		return;
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
+	if (options.use_pam) {
Petr Lautrbach 65ba94
+		/* do not compute context, just setup environment for pam_selinux */
Petr Lautrbach 65ba94
+		if (ssh_selinux_setup_pam_variables()) {
Petr Lautrbach 65ba94
+			switch (security_getenforce()) {
Petr Lautrbach 65ba94
+			case -1:
Petr Lautrbach 65ba94
+				fatal("%s: security_getenforce() failed", __func__);
Petr Lautrbach 65ba94
+			case 0:
Petr Lautrbach 65ba94
+				error("%s: SELinux PAM variable setup failure. Continuing in permissive mode.",
Petr Lautrbach 65ba94
+				    __func__);
Petr Lautrbach 65ba94
+			break;
Petr Lautrbach 65ba94
+			default:
Petr Lautrbach 65ba94
+				fatal("%s: SELinux PAM variable setup failure. Aborting connection.",
Petr Lautrbach 65ba94
+				    __func__);
Petr Lautrbach 65ba94
+			}
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
+		return;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
 	debug3("%s: setting execution context", __func__);
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
-	user_ctx = ssh_selinux_getctxbyname(pwname);
Petr Lautrbach 65ba94
-	if (setexeccon(user_ctx) != 0) {
Petr Lautrbach 65ba94
+	r = ssh_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);
Petr Lautrbach 65ba94
+	if (r >= 0) {
Petr Lautrbach 65ba94
+		r = setexeccon(user_ctx);
Petr Lautrbach 65ba94
+		if (r < 0) {
Petr Lautrbach 65ba94
+			error("%s: Failed to set SELinux execution context %s for %s",
Petr Lautrbach 65ba94
+			    __func__, user_ctx, pwname);
Petr Lautrbach 65ba94
+		} 
Petr Lautrbach 65ba94
+#ifdef HAVE_SETKEYCREATECON
Petr Lautrbach 65ba94
+		else if (setkeycreatecon(user_ctx) < 0) {
Petr Lautrbach 65ba94
+			error("%s: Failed to set SELinux keyring creation context %s for %s",
Petr Lautrbach 65ba94
+			    __func__, user_ctx, pwname);
Petr Lautrbach 65ba94
+		}
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+	if (user_ctx == NULL) {
Petr Lautrbach 65ba94
+		user_ctx = default_ctx;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+	if (r < 0 || user_ctx != default_ctx) {
Petr Lautrbach 65ba94
+		/* audit just the case when user changed a role or there was
Petr Lautrbach 65ba94
+		   a failure */
Petr Lautrbach 65ba94
+		send_audit_message(r >= 0, default_ctx, user_ctx);
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+	if (r < 0) {
Petr Lautrbach 65ba94
 		switch (security_getenforce()) {
Petr Lautrbach 65ba94
 		case -1:
Petr Lautrbach 65ba94
 			fatal("%s: security_getenforce() failed", __func__);
Petr Lautrbach 65ba94
 		case 0:
Petr Lautrbach 65ba94
-			error("%s: Failed to set SELinux execution "
Petr Lautrbach 65ba94
-			    "context for %s", __func__, pwname);
Petr Lautrbach 65ba94
+			error("%s: SELinux failure. Continuing in permissive mode.",
Petr Lautrbach 65ba94
+			    __func__);
Petr Lautrbach 65ba94
 			break;
Petr Lautrbach 65ba94
 		default:
Petr Lautrbach 65ba94
-			fatal("%s: Failed to set SELinux execution context "
Petr Lautrbach 65ba94
-			    "for %s (in enforcing mode)", __func__, pwname);
Petr Lautrbach 65ba94
+			fatal("%s: SELinux failure. Aborting connection.",
Petr Lautrbach 65ba94
+			    __func__);
Petr Lautrbach 65ba94
 		}
Petr Lautrbach 65ba94
 	}
Petr Lautrbach 65ba94
-	if (user_ctx != NULL)
Petr Lautrbach 65ba94
+	if (user_ctx != NULL && user_ctx != default_ctx)
Petr Lautrbach 65ba94
 		freecon(user_ctx);
Petr Lautrbach 65ba94
+	if (default_ctx != NULL)
Petr Lautrbach 65ba94
+		freecon(default_ctx);
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	debug3("%s: done", __func__);
Petr Lautrbach 65ba94
 }
Petr Lautrbach 65ba94
@@ -149,7 +430,10 @@ ssh_selinux_setup_pty(char *pwname, cons
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	debug3("%s: setting TTY context on %s", __func__, tty);
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
-	user_ctx = ssh_selinux_getctxbyname(pwname);
Petr Lautrbach 65ba94
+	if (getexeccon(&user_ctx) < 0) {
Petr Lautrbach 65ba94
+		error("%s: getexeccon: %s", __func__, strerror(errno));
Petr Lautrbach 65ba94
+		goto out;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 	/* XXX: should these calls fatal() upon failure in enforcing mode? */
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
@@ -221,21 +505,6 @@ ssh_selinux_change_context(const char *n
Petr Lautrbach 65ba94
 	xfree(newctx);
Petr Lautrbach 65ba94
 }
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
-void
Petr Lautrbach 65ba94
-ssh_selinux_setfscreatecon(const char *path)
Petr Lautrbach 65ba94
-{
Petr Lautrbach 65ba94
-	security_context_t context;
Petr Lautrbach 65ba94
-
Petr Lautrbach 65ba94
-	if (!ssh_selinux_enabled())
Petr Lautrbach 65ba94
-		return;
Petr Lautrbach 65ba94
-	if (path == NULL) {
Petr Lautrbach 65ba94
-		setfscreatecon(NULL);
Petr Lautrbach 65ba94
-		return;
Petr Lautrbach 65ba94
-	}
Petr Lautrbach 65ba94
-	if (matchpathcon(path, 0700, &context) == 0)
Petr Lautrbach 65ba94
-		setfscreatecon(context);
Petr Lautrbach 65ba94
-}
Petr Lautrbach 65ba94
-
Petr Lautrbach 65ba94
 #endif /* WITH_SELINUX */
Petr Lautrbach 65ba94
 
Petr Lautrbach 65ba94
 #ifdef LINUX_OOM_ADJUST
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/openbsd-compat/port-linux_part_2.c.role-mls openssh-6.2p1/openbsd-compat/port-linux_part_2.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/openbsd-compat/port-linux_part_2.c.role-mls	2013-03-25 17:47:00.607747102 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/openbsd-compat/port-linux_part_2.c	2013-03-25 17:47:00.607747102 +0100
Petr Lautrbach 65ba94
@@ -0,0 +1,75 @@
Petr Lautrbach 65ba94
+/* $Id: port-linux.c,v 1.11.4.2 2011/02/04 00:43:08 djm Exp $ */
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+/*
Petr Lautrbach 65ba94
+ * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
Petr Lautrbach 65ba94
+ * Copyright (c) 2006 Damien Miller <djm@openbsd.org>
Petr Lautrbach 65ba94
+ *
Petr Lautrbach 65ba94
+ * Permission to use, copy, modify, and distribute this software for any
Petr Lautrbach 65ba94
+ * purpose with or without fee is hereby granted, provided that the above
Petr Lautrbach 65ba94
+ * copyright notice and this permission notice appear in all copies.
Petr Lautrbach 65ba94
+ *
Petr Lautrbach 65ba94
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
Petr Lautrbach 65ba94
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Petr Lautrbach 65ba94
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Petr Lautrbach 65ba94
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Petr Lautrbach 65ba94
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Petr Lautrbach 65ba94
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Petr Lautrbach 65ba94
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Petr Lautrbach 65ba94
+ */
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+/*
Petr Lautrbach 65ba94
+ * Linux-specific portability code - just SELinux support at present
Petr Lautrbach 65ba94
+ */
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+#include "includes.h"
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+#if defined(WITH_SELINUX) || defined(LINUX_OOM_ADJUST)
Petr Lautrbach 65ba94
+#include <errno.h>
Petr Lautrbach 65ba94
+#include <stdarg.h>
Petr Lautrbach 65ba94
+#include <string.h>
Petr Lautrbach 65ba94
+#include <stdio.h>
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+#include "log.h"
Petr Lautrbach 65ba94
+#include "xmalloc.h"
Petr Lautrbach 65ba94
+#include "port-linux.h"
Petr Lautrbach 65ba94
+#include "key.h"
Petr Lautrbach 65ba94
+#include "hostfile.h"
Petr Lautrbach 65ba94
+#include "auth.h"
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+#include <selinux/selinux.h>
Petr Lautrbach 65ba94
+#include <selinux/flask.h>
Petr Lautrbach 65ba94
+#include <selinux/get_context_list.h>
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+/* Wrapper around is_selinux_enabled() to log its return value once only */
Petr Lautrbach 65ba94
+int
Petr Lautrbach 65ba94
+ssh_selinux_enabled(void)
Petr Lautrbach 65ba94
+{
Petr Lautrbach 65ba94
+	static int enabled = -1;
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	if (enabled == -1) {
Petr Lautrbach 65ba94
+		enabled = (is_selinux_enabled() == 1);
Petr Lautrbach 65ba94
+		debug("SELinux support %s", enabled ? "enabled" : "disabled");
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	return (enabled);
Petr Lautrbach 65ba94
+}
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+void
Petr Lautrbach 65ba94
+ssh_selinux_setfscreatecon(const char *path)
Petr Lautrbach 65ba94
+{
Petr Lautrbach 65ba94
+	security_context_t context;
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+	if (!ssh_selinux_enabled())
Petr Lautrbach 65ba94
+		return;
Petr Lautrbach 65ba94
+	if (path == NULL) {
Petr Lautrbach 65ba94
+		setfscreatecon(NULL);
Petr Lautrbach 65ba94
+		return;
Petr Lautrbach 65ba94
+	}
Petr Lautrbach 65ba94
+	if (matchpathcon(path, 0700, &context) == 0)
Petr Lautrbach 65ba94
+		setfscreatecon(context);
Petr Lautrbach 65ba94
+}
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+#endif /* WITH_SELINUX */
Petr Lautrbach 65ba94
+
Petr Lautrbach 65ba94
+#endif /* WITH_SELINUX || LINUX_OOM_ADJUST */
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/sshd.c.role-mls openssh-6.2p1/sshd.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/sshd.c.role-mls	2013-03-25 17:47:00.589746999 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/sshd.c	2013-03-25 17:47:00.607747102 +0100
Petr Lautrbach 8a29de
@@ -2118,6 +2118,9 @@ main(int ac, char **av)
Petr Lautrbach 65ba94
 		restore_uid();
Petr Lautrbach 65ba94
 	}
Petr Lautrbach 65ba94
 #endif
Petr Lautrbach 65ba94
+#ifdef WITH_SELINUX
Petr Lautrbach 65ba94
+	ssh_selinux_setup_exec_context(authctxt->pw->pw_name);
Petr Lautrbach 65ba94
+#endif
Petr Lautrbach 65ba94
 #ifdef USE_PAM
Petr Lautrbach 65ba94
 	if (options.use_pam) {
Petr Lautrbach 65ba94
 		do_pam_setcred(1);