a3ba41c
diff -up openssh-5.2p1/auth1.c.selinux openssh-5.2p1/auth1.c
ce94dae
--- openssh-5.2p1/auth1.c.selinux	2008-07-09 12:54:05.000000000 +0200
ce94dae
+++ openssh-5.2p1/auth1.c	2009-08-11 22:43:07.918183730 +0200
ce94dae
@@ -392,6 +392,9 @@ do_authentication(Authctxt *authctxt)
a3ba41c
 {
a3ba41c
 	u_int ulen;
ce94dae
 	char *user, *style = NULL;
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+	char *role=NULL;
ce94dae
+#endif
a3ba41c
 
a3ba41c
 	/* Get the name of the user that we wish to log in as. */
a3ba41c
 	packet_read_expect(SSH_CMSG_USER);
ce94dae
@@ -400,11 +403,25 @@ do_authentication(Authctxt *authctxt)
a3ba41c
 	user = packet_get_string(&ulen);
a3ba41c
 	packet_check_eom();
a3ba41c
 
ce94dae
+#ifdef WITH_SELINUX
a3ba41c
+	if ((role = strchr(user, '/')) != NULL)
a3ba41c
+		*role++ = '\0';
ce94dae
+#endif
a3ba41c
+
a3ba41c
 	if ((style = strchr(user, ':')) != NULL)
a3ba41c
 		*style++ = '\0';
ce94dae
+#ifdef WITH_SELINUX
a3ba41c
+	else
a3ba41c
+		if (role && (style = strchr(role, ':')) != NULL)
a3ba41c
+			*style++ = '\0';
ce94dae
+#endif
a3ba41c
+			
a3ba41c
 
a3ba41c
 	authctxt->user = user;
a3ba41c
 	authctxt->style = style;
ce94dae
+#ifdef WITH_SELINUX
a3ba41c
+	authctxt->role = role;
ce94dae
+#endif
a3ba41c
 
a3ba41c
 	/* Verify that the user is a valid user. */
a3ba41c
 	if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
ce94dae
diff -up openssh-5.2p1/auth2.c.selinux openssh-5.2p1/auth2.c
ce94dae
--- openssh-5.2p1/auth2.c.selinux	2008-11-05 06:20:46.000000000 +0100
ce94dae
+++ openssh-5.2p1/auth2.c	2009-08-11 22:43:07.919756192 +0200
ce94dae
@@ -216,6 +216,9 @@ input_userauth_request(int type, u_int32
ce94dae
 	Authctxt *authctxt = ctxt;
ce94dae
 	Authmethod *m = NULL;
ce94dae
 	char *user, *service, *method, *style = NULL;
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+	char *role = NULL;
ce94dae
+#endif
ce94dae
 	int authenticated = 0;
ce94dae
 
ce94dae
 	if (authctxt == NULL)
ce94dae
@@ -227,6 +230,11 @@ input_userauth_request(int type, u_int32
ce94dae
 	debug("userauth-request for user %s service %s method %s", user, service, method);
ce94dae
 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
ce94dae
 
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+	if ((role = strchr(user, '/')) != NULL)
ce94dae
+		*role++ = 0;
ce94dae
+#endif
ce94dae
+
ce94dae
 	if ((style = strchr(user, ':')) != NULL)
ce94dae
 		*style++ = 0;
ce94dae
 
ce94dae
@@ -252,8 +260,15 @@ input_userauth_request(int type, u_int32
ce94dae
 		    use_privsep ? " [net]" : "");
ce94dae
 		authctxt->service = xstrdup(service);
ce94dae
 		authctxt->style = style ? xstrdup(style) : NULL;
ce94dae
-		if (use_privsep)
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+		authctxt->role = role ? xstrdup(role) : NULL;
ce94dae
+#endif
ce94dae
+		if (use_privsep) {
ce94dae
 			mm_inform_authserv(service, style);
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+			mm_inform_authrole(role);
ce94dae
+#endif
ce94dae
+		}
ce94dae
 		userauth_banner();
ce94dae
 	} else if (strcmp(user, authctxt->user) != 0 ||
ce94dae
 	    strcmp(service, authctxt->service) != 0) {
ce94dae
diff -up openssh-5.2p1/auth2-gss.c.selinux openssh-5.2p1/auth2-gss.c
ce94dae
--- openssh-5.2p1/auth2-gss.c.selinux	2007-12-02 12:59:45.000000000 +0100
ce94dae
+++ openssh-5.2p1/auth2-gss.c	2009-08-11 22:43:07.921723295 +0200
ce94dae
@@ -258,6 +258,7 @@ input_gssapi_mic(int type, u_int32_t ple
ce94dae
 	Authctxt *authctxt = ctxt;
ce94dae
 	Gssctxt *gssctxt;
ce94dae
 	int authenticated = 0;
ce94dae
+	char *micuser;
ce94dae
 	Buffer b;
ce94dae
 	gss_buffer_desc mic, gssbuf;
ce94dae
 	u_int len;
ce94dae
@@ -270,7 +271,13 @@ input_gssapi_mic(int type, u_int32_t ple
ce94dae
 	mic.value = packet_get_string(&len;;
ce94dae
 	mic.length = len;
ce94dae
 
ce94dae
-	ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+	if (authctxt->role && (strlen(authctxt->role) > 0))
ce94dae
+		xasprintf(&micuser, "%s/%s", authctxt->user, authctxt->role);
ce94dae
+	else
ce94dae
+#endif
ce94dae
+		micuser = authctxt->user;
ce94dae
+	ssh_gssapi_buildmic(&b, micuser, authctxt->service,
ce94dae
 	    "gssapi-with-mic");
ce94dae
 
ce94dae
 	gssbuf.value = buffer_ptr(&b);
ce94dae
@@ -282,6 +289,8 @@ input_gssapi_mic(int type, u_int32_t ple
ce94dae
 		logit("GSSAPI MIC check failed");
ce94dae
 
ce94dae
 	buffer_free(&b);
ce94dae
+	if (micuser != authctxt->user)
ce94dae
+		xfree(micuser);
ce94dae
 	xfree(mic.value);
ce94dae
 
ce94dae
 	authctxt->postponed = 0;
ce94dae
diff -up openssh-5.2p1/auth2-hostbased.c.selinux openssh-5.2p1/auth2-hostbased.c
ce94dae
--- openssh-5.2p1/auth2-hostbased.c.selinux	2008-07-17 10:57:19.000000000 +0200
ce94dae
+++ openssh-5.2p1/auth2-hostbased.c	2009-08-11 22:43:07.923721059 +0200
ce94dae
@@ -106,7 +106,15 @@ userauth_hostbased(Authctxt *authctxt)
ce94dae
 	buffer_put_string(&b, session_id2, session_id2_len);
ce94dae
 	/* reconstruct packet */
ce94dae
 	buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
ce94dae
-	buffer_put_cstring(&b, authctxt->user);
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+	if (authctxt->role) {
ce94dae
+		buffer_put_int(&b, strlen(authctxt->user)+strlen(authctxt->role)+1);
ce94dae
+		buffer_append(&b, authctxt->user, strlen(authctxt->user));
ce94dae
+		buffer_put_char(&b, '/');
ce94dae
+		buffer_append(&b, authctxt->role, strlen(authctxt->role));
ce94dae
+	} else 
ce94dae
+#endif
ce94dae
+		buffer_put_cstring(&b, authctxt->user);
ce94dae
 	buffer_put_cstring(&b, service);
ce94dae
 	buffer_put_cstring(&b, "hostbased");
ce94dae
 	buffer_put_string(&b, pkalg, alen);
adad2a8
diff -up openssh-5.2p1/auth2-pubkey.c.selinux openssh-5.2p1/auth2-pubkey.c
adad2a8
--- openssh-5.2p1/auth2-pubkey.c.selinux	2008-07-04 04:54:25.000000000 +0200
ce94dae
+++ openssh-5.2p1/auth2-pubkey.c	2009-08-11 22:43:07.925704588 +0200
ce94dae
@@ -117,7 +117,15 @@ userauth_pubkey(Authctxt *authctxt)
adad2a8
 		}
adad2a8
 		/* reconstruct packet */
adad2a8
 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
adad2a8
-		buffer_put_cstring(&b, authctxt->user);
ce94dae
+#ifdef WITH_SELINUX
adad2a8
+		if (authctxt->role) {
adad2a8
+			buffer_put_int(&b, strlen(authctxt->user)+strlen(authctxt->role)+1);
adad2a8
+			buffer_append(&b, authctxt->user, strlen(authctxt->user));
adad2a8
+			buffer_put_char(&b, '/');
adad2a8
+			buffer_append(&b, authctxt->role, strlen(authctxt->role));
ce94dae
+		} else 
ce94dae
+#endif
adad2a8
+			buffer_put_cstring(&b, authctxt->user);
adad2a8
 		buffer_put_cstring(&b,
adad2a8
 		    datafellows & SSH_BUG_PKSERVICE ?
adad2a8
 		    "ssh-userauth" :
ce94dae
diff -up openssh-5.2p1/auth.h.selinux openssh-5.2p1/auth.h
ce94dae
--- openssh-5.2p1/auth.h.selinux	2008-11-05 06:20:46.000000000 +0100
ce94dae
+++ openssh-5.2p1/auth.h	2009-08-11 22:43:07.927199901 +0200
ce94dae
@@ -58,6 +58,9 @@ struct Authctxt {
ce94dae
 	char		*service;
ce94dae
 	struct passwd	*pw;		/* set if 'valid' */
ce94dae
 	char		*style;
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+	char		*role;
ce94dae
+#endif
ce94dae
 	void		*kbdintctxt;
ce94dae
 	void		*jpake_ctx;
ce94dae
 #ifdef BSD_AUTH
ce94dae
diff -up openssh-5.2p1/configure.ac.selinux openssh-5.2p1/configure.ac
ce94dae
--- openssh-5.2p1/configure.ac.selinux	2009-02-16 05:37:03.000000000 +0100
ce94dae
+++ openssh-5.2p1/configure.ac	2009-08-11 22:43:07.930259052 +0200
ce94dae
@@ -3335,6 +3335,7 @@ AC_ARG_WITH(selinux,
ce94dae
 		AC_CHECK_LIB(selinux, setexeccon, [ LIBSELINUX="-lselinux" ],
ce94dae
 		    AC_MSG_ERROR(SELinux support requires libselinux library))
ce94dae
 		SSHDLIBS="$SSHDLIBS $LIBSELINUX"
ce94dae
+		LIBS="$LIBS $LIBSELINUX"
ce94dae
 		AC_CHECK_FUNCS(getseuserbyname get_default_context_with_level)
ce94dae
 		LIBS="$save_LIBS"
ce94dae
 	fi ]
ce94dae
diff -up openssh-5.2p1/monitor.c.selinux openssh-5.2p1/monitor.c
ce94dae
--- openssh-5.2p1/monitor.c.selinux	2009-02-14 06:33:31.000000000 +0100
ce94dae
+++ openssh-5.2p1/monitor.c	2009-08-11 22:43:07.933623092 +0200
ce94dae
@@ -135,6 +135,9 @@ int mm_answer_sign(int, Buffer *);
ce94dae
 int mm_answer_pwnamallow(int, Buffer *);
ce94dae
 int mm_answer_auth2_read_banner(int, Buffer *);
ce94dae
 int mm_answer_authserv(int, Buffer *);
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+int mm_answer_authrole(int, Buffer *);
ce94dae
+#endif
ce94dae
 int mm_answer_authpassword(int, Buffer *);
ce94dae
 int mm_answer_bsdauthquery(int, Buffer *);
ce94dae
 int mm_answer_bsdauthrespond(int, Buffer *);
ce94dae
@@ -211,6 +214,9 @@ struct mon_table mon_dispatch_proto20[] 
ce94dae
     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
ce94dae
     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
ce94dae
     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+    {MONITOR_REQ_AUTHROLE, MON_ONCE, mm_answer_authrole},
ce94dae
+#endif
ce94dae
     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
ce94dae
     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
ce94dae
 #ifdef USE_PAM
ce94dae
@@ -680,6 +686,9 @@ mm_answer_pwnamallow(int sock, Buffer *m
ce94dae
 	else {
ce94dae
 		/* Allow service/style information on the auth context */
ce94dae
 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHROLE, 1);
ce94dae
+#endif
ce94dae
 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
ce94dae
 	}
ce94dae
 
ce94dae
@@ -724,6 +733,25 @@ mm_answer_authserv(int sock, Buffer *m)
ce94dae
 	return (0);
ce94dae
 }
ce94dae
 
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+int
ce94dae
+mm_answer_authrole(int sock, Buffer *m)
ce94dae
+{
ce94dae
+	monitor_permit_authentications(1);
ce94dae
+
ce94dae
+	authctxt->role = buffer_get_string(m, NULL);
ce94dae
+	debug3("%s: role=%s",
ce94dae
+	    __func__, authctxt->role);
ce94dae
+
ce94dae
+	if (strlen(authctxt->role) == 0) {
ce94dae
+		xfree(authctxt->role);
ce94dae
+		authctxt->role = NULL;
ce94dae
+	}
ce94dae
+
ce94dae
+	return (0);
ce94dae
+}
ce94dae
+#endif
ce94dae
+
ce94dae
 int
ce94dae
 mm_answer_authpassword(int sock, Buffer *m)
ce94dae
 {
ce94dae
@@ -1102,7 +1130,7 @@ static int
ce94dae
 monitor_valid_userblob(u_char *data, u_int datalen)
ce94dae
 {
ce94dae
 	Buffer b;
ce94dae
-	char *p;
ce94dae
+	char *p, *r;
ce94dae
 	u_int len;
ce94dae
 	int fail = 0;
adad2a8
 
ce94dae
@@ -1128,6 +1156,8 @@ monitor_valid_userblob(u_char *data, u_i
ce94dae
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
ce94dae
 		fail++;
ce94dae
 	p = buffer_get_string(&b, NULL);
ce94dae
+	if ((r = strchr(p, '/')) != NULL)
ce94dae
+		*r = '\0';
ce94dae
 	if (strcmp(authctxt->user, p) != 0) {
ce94dae
 		logit("wrong user name passed to monitor: expected %s != %.100s",
ce94dae
 		    authctxt->user, p);
ce94dae
@@ -1159,7 +1189,7 @@ monitor_valid_hostbasedblob(u_char *data
ce94dae
     char *chost)
ce94dae
 {
ce94dae
 	Buffer b;
ce94dae
-	char *p;
ce94dae
+	char *p, *r;
ce94dae
 	u_int len;
ce94dae
 	int fail = 0;
ce94dae
 
ce94dae
@@ -1176,6 +1206,8 @@ monitor_valid_hostbasedblob(u_char *data
ce94dae
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
ce94dae
 		fail++;
ce94dae
 	p = buffer_get_string(&b, NULL);
ce94dae
+	if ((r = strchr(p, '/')) != NULL)
ce94dae
+		*r = '\0';
ce94dae
 	if (strcmp(authctxt->user, p) != 0) {
ce94dae
 		logit("wrong user name passed to monitor: expected %s != %.100s",
ce94dae
 		    authctxt->user, p);
ce94dae
diff -up openssh-5.2p1/monitor.h.selinux openssh-5.2p1/monitor.h
ce94dae
--- openssh-5.2p1/monitor.h.selinux	2008-11-05 06:20:46.000000000 +0100
ce94dae
+++ openssh-5.2p1/monitor.h	2009-08-11 22:43:07.935612930 +0200
ce94dae
@@ -31,6 +31,9 @@
adad2a8
 enum monitor_reqtype {
adad2a8
 	MONITOR_REQ_MODULI, MONITOR_ANS_MODULI,
ce94dae
 	MONITOR_REQ_FREE, MONITOR_REQ_AUTHSERV,
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+	MONITOR_REQ_AUTHROLE,
ce94dae
+#endif
adad2a8
 	MONITOR_REQ_SIGN, MONITOR_ANS_SIGN,
adad2a8
 	MONITOR_REQ_PWNAM, MONITOR_ANS_PWNAM,
adad2a8
 	MONITOR_REQ_AUTH2_READ_BANNER, MONITOR_ANS_AUTH2_READ_BANNER,
adad2a8
diff -up openssh-5.2p1/monitor_wrap.c.selinux openssh-5.2p1/monitor_wrap.c
ce94dae
--- openssh-5.2p1/monitor_wrap.c.selinux	2008-11-05 06:20:47.000000000 +0100
ce94dae
+++ openssh-5.2p1/monitor_wrap.c	2009-08-11 22:43:07.937212340 +0200
ce94dae
@@ -297,6 +297,25 @@ mm_inform_authserv(char *service, char *
adad2a8
 	buffer_free(&m);
adad2a8
 }
a3ba41c
 
adad2a8
+/* Inform the privileged process about role */
adad2a8
+
ce94dae
+#ifdef WITH_SELINUX
adad2a8
+void
adad2a8
+mm_inform_authrole(char *role)
adad2a8
+{
adad2a8
+	Buffer m;
adad2a8
+
adad2a8
+	debug3("%s entering", __func__);
adad2a8
+
adad2a8
+	buffer_init(&m);
adad2a8
+	buffer_put_cstring(&m, role ? role : "");
adad2a8
+
adad2a8
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHROLE, &m);
adad2a8
+
adad2a8
+	buffer_free(&m);
adad2a8
+}
ce94dae
+#endif
adad2a8
+
adad2a8
 /* Do the password authentication */
adad2a8
 int
adad2a8
 mm_auth_password(Authctxt *authctxt, char *password)
ce94dae
diff -up openssh-5.2p1/monitor_wrap.h.selinux openssh-5.2p1/monitor_wrap.h
ce94dae
--- openssh-5.2p1/monitor_wrap.h.selinux	2008-11-05 06:20:47.000000000 +0100
ce94dae
+++ openssh-5.2p1/monitor_wrap.h	2009-08-11 22:43:07.938268752 +0200
ce94dae
@@ -41,6 +41,9 @@ int mm_is_monitor(void);
ce94dae
 DH *mm_choose_dh(int, int, int);
ce94dae
 int mm_key_sign(Key *, u_char **, u_int *, u_char *, u_int);
ce94dae
 void mm_inform_authserv(char *, char *);
ce94dae
+#ifdef WITH_SELINUX
ce94dae
+void mm_inform_authrole(char *);
ce94dae
+#endif
ce94dae
 struct passwd *mm_getpwnamallow(const char *);
ce94dae
 char *mm_auth2_read_banner(void);
ce94dae
 int mm_auth_password(struct Authctxt *, char *);
adad2a8
diff -up openssh-5.2p1/openbsd-compat/port-linux.c.selinux openssh-5.2p1/openbsd-compat/port-linux.c
adad2a8
--- openssh-5.2p1/openbsd-compat/port-linux.c.selinux	2008-03-26 21:27:21.000000000 +0100
ce94dae
+++ openssh-5.2p1/openbsd-compat/port-linux.c	2009-08-11 22:44:14.529196220 +0200
adad2a8
@@ -30,11 +30,16 @@
adad2a8
 #ifdef WITH_SELINUX
adad2a8
 #include "log.h"
adad2a8
 #include "port-linux.h"
adad2a8
+#include "key.h"
adad2a8
+#include "hostfile.h"
adad2a8
+#include "auth.h"
adad2a8
 
adad2a8
 #include <selinux/selinux.h>
adad2a8
 #include <selinux/flask.h>
adad2a8
 #include <selinux/get_context_list.h>
adad2a8
 
adad2a8
+extern Authctxt *the_authctxt;
adad2a8
+
adad2a8
 /* Wrapper around is_selinux_enabled() to log its return value once only */
adad2a8
 int
adad2a8
 ssh_selinux_enabled(void)
adad2a8
@@ -53,23 +58,36 @@ ssh_selinux_enabled(void)
adad2a8
 static security_context_t
adad2a8
 ssh_selinux_getctxbyname(char *pwname)
adad2a8
 {
adad2a8
-	security_context_t sc;
adad2a8
-	char *sename = NULL, *lvl = NULL;
adad2a8
-	int r;
adad2a8
+	security_context_t sc = NULL;
adad2a8
+	char *sename, *lvl;
adad2a8
+	char *role = NULL;
adad2a8
+	int r = 0;
adad2a8
 
adad2a8
+	if (the_authctxt) 
adad2a8
+		role=the_authctxt->role;
adad2a8
 #ifdef HAVE_GETSEUSERBYNAME
adad2a8
-	if (getseuserbyname(pwname, &sename, &lvl) != 0)
adad2a8
-		return NULL;
adad2a8
+	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
adad2a8
+		sename = NULL;
adad2a8
+		lvl = NULL;
adad2a8
+	}
adad2a8
 #else
adad2a8
 	sename = pwname;
adad2a8
 	lvl = NULL;
adad2a8
 #endif
adad2a8
 
adad2a8
+	if (r == 0) {
adad2a8
 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
adad2a8
-	r = get_default_context_with_level(sename, lvl, NULL, &sc);
adad2a8
+		if (role != NULL && role[0])
adad2a8
+			r = get_default_context_with_rolelevel(sename, role, lvl, NULL, &sc);
adad2a8
+		else
adad2a8
+			r = get_default_context_with_level(sename, lvl, NULL, &sc);
adad2a8
 #else
adad2a8
-	r = get_default_context(sename, NULL, &sc);
adad2a8
+		if (role != NULL && role[0])
adad2a8
+			r = get_default_context_with_role(sename, role, NULL, &sc);
adad2a8
+		else
adad2a8
+			r = get_default_context(sename, NULL, &sc);
adad2a8
 #endif
adad2a8
+	}
adad2a8
 
adad2a8
 	if (r != 0) {
adad2a8
 		switch (security_getenforce()) {