Jan F 003cb0b
diff -up openssh-5.8p1/auth1.c.role openssh-5.8p1/auth1.c
Jan F 003cb0b
--- openssh-5.8p1/auth1.c.role	2010-08-31 14:36:39.000000000 +0200
Jan F 3657adf
+++ openssh-5.8p1/auth1.c	2011-03-22 18:32:17.907648664 +0100
974c89c
@@ -384,6 +384,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);
Jan F 003cb0b
@@ -392,11 +395,24 @@ do_authentication(Authctxt *authctxt)
Jan F 003cb0b
 	user = packet_get_cstring(&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
 	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)
Jan F 003cb0b
diff -up openssh-5.8p1/auth2.c.role openssh-5.8p1/auth2.c
Jan F 3657adf
--- openssh-5.8p1/auth2.c.role	2011-03-22 18:32:10.876648814 +0100
Jan F 3657adf
+++ openssh-5.8p1/auth2.c	2011-03-22 18:32:17.959648657 +0100
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
 
Jan F 3657adf
@@ -249,8 +257,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) {
Jan F 003cb0b
diff -up openssh-5.8p1/auth2-gss.c.role openssh-5.8p1/auth2-gss.c
Jan F 003cb0b
--- openssh-5.8p1/auth2-gss.c.role	2007-12-02 12:59:45.000000000 +0100
Jan F 3657adf
+++ openssh-5.8p1/auth2-gss.c	2011-03-22 18:32:18.009679649 +0100
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;
Jan F 003cb0b
diff -up openssh-5.8p1/auth2-hostbased.c.role openssh-5.8p1/auth2-hostbased.c
Jan F 3657adf
--- openssh-5.8p1/auth2-hostbased.c.role	2011-03-22 18:32:12.012648649 +0100
Jan F 3657adf
+++ openssh-5.8p1/auth2-hostbased.c	2011-03-22 18:32:18.052648698 +0100
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);
Jan F 003cb0b
diff -up openssh-5.8p1/auth2-pubkey.c.role openssh-5.8p1/auth2-pubkey.c
Jan F 3657adf
--- openssh-5.8p1/auth2-pubkey.c.role	2011-03-22 18:32:16.318648486 +0100
Jan F 3657adf
+++ openssh-5.8p1/auth2-pubkey.c	2011-03-22 18:32:18.107648652 +0100
Jan F 003cb0b
@@ -122,7 +122,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" :
Jan F 003cb0b
diff -up openssh-5.8p1/auth.h.role openssh-5.8p1/auth.h
Jan F 3657adf
--- openssh-5.8p1/auth.h.role	2011-03-22 18:32:12.117648630 +0100
Jan F 3657adf
+++ openssh-5.8p1/auth.h	2011-03-22 18:32:18.170648709 +0100
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
Jan F 003cb0b
diff -up openssh-5.8p1/auth-pam.c.role openssh-5.8p1/auth-pam.c
Jan F 003cb0b
--- openssh-5.8p1/auth-pam.c.role	2009-07-12 14:07:21.000000000 +0200
Jan F 3657adf
+++ openssh-5.8p1/auth-pam.c	2011-03-22 18:32:18.230844134 +0100
Jan F 003cb0b
@@ -1069,7 +1069,7 @@ is_pam_session_open(void)
Jan F 003cb0b
  * during the ssh authentication process.
Jan F 003cb0b
  */
Jan F 003cb0b
 int
Jan F 003cb0b
-do_pam_putenv(char *name, char *value)
Jan F 003cb0b
+do_pam_putenv(char *name, const char *value)
Jan F 003cb0b
 {
Jan F 003cb0b
 	int ret = 1;
Jan F 003cb0b
 #ifdef HAVE_PAM_PUTENV
Jan F 003cb0b
diff -up openssh-5.8p1/auth-pam.h.role openssh-5.8p1/auth-pam.h
Jan F 003cb0b
--- openssh-5.8p1/auth-pam.h.role	2004-09-11 14:17:26.000000000 +0200
Jan F 3657adf
+++ openssh-5.8p1/auth-pam.h	2011-03-22 18:32:18.280648665 +0100
Jan F 003cb0b
@@ -38,7 +38,7 @@ void do_pam_session(void);
Jan F 003cb0b
 void do_pam_set_tty(const char *);
Jan F 003cb0b
 void do_pam_setcred(int );
Jan F 003cb0b
 void do_pam_chauthtok(void);
Jan F 003cb0b
-int do_pam_putenv(char *, char *);
Jan F 003cb0b
+int do_pam_putenv(char *, const char *);
Jan F 003cb0b
 char ** fetch_pam_environment(void);
Jan F 003cb0b
 char ** fetch_pam_child_environment(void);
Jan F 003cb0b
 void free_pam_environment(char **);
Jan F 003cb0b
diff -up openssh-5.8p1/monitor.c.role openssh-5.8p1/monitor.c
Jan F 3657adf
--- openssh-5.8p1/monitor.c.role	2011-03-22 18:32:14.465661970 +0100
Jan F 3657adf
+++ openssh-5.8p1/monitor.c	2011-03-22 18:32:18.337831292 +0100
Jan F 3657adf
@@ -140,6 +140,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 *);
Jan F 3657adf
@@ -221,6 +224,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
Jan F 3657adf
@@ -708,6 +714,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
 
Jan F 3657adf
@@ -752,6 +761,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
 {
Jan F 3657adf
@@ -1117,7 +1145,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
 
Jan F 3657adf
@@ -1143,6 +1171,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);
Jan F 3657adf
@@ -1174,7 +1204,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
 
Jan F 3657adf
@@ -1191,6 +1221,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);
Jan F 003cb0b
diff -up openssh-5.8p1/monitor.h.role openssh-5.8p1/monitor.h
Jan F 3657adf
--- openssh-5.8p1/monitor.h.role	2011-03-22 18:32:14.512648974 +0100
Jan F 3657adf
+++ openssh-5.8p1/monitor.h	2011-03-22 18:32:18.382648691 +0100
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,
Jan F 003cb0b
diff -up openssh-5.8p1/monitor_wrap.c.role openssh-5.8p1/monitor_wrap.c
Jan F 3657adf
--- openssh-5.8p1/monitor_wrap.c.role	2011-03-22 18:32:14.612774442 +0100
Jan F 3657adf
+++ openssh-5.8p1/monitor_wrap.c	2011-03-22 18:32:18.433648592 +0100
Jan F 003cb0b
@@ -298,6 +298,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)
Jan F 003cb0b
diff -up openssh-5.8p1/monitor_wrap.h.role openssh-5.8p1/monitor_wrap.h
Jan F 3657adf
--- openssh-5.8p1/monitor_wrap.h.role	2011-03-22 18:32:14.659648967 +0100
Jan F 3657adf
+++ openssh-5.8p1/monitor_wrap.h	2011-03-22 18:32:18.486648682 +0100
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 *);
Jan F 003cb0b
diff -up openssh-5.8p1/openbsd-compat/Makefile.in.role openssh-5.8p1/openbsd-compat/Makefile.in
Jan F 3657adf
--- openssh-5.8p1/openbsd-compat/Makefile.in.role	2011-03-22 18:32:15.000000000 +0100
Jan F 3657adf
+++ openssh-5.8p1/openbsd-compat/Makefile.in	2011-03-22 18:34:43.962648778 +0100
Jan F 003cb0b
@@ -20,7 +20,7 @@ OPENBSD=base64.o basename.o bindresvport
Jan F 003cb0b
 
Jan F 003cb0b
 COMPAT=bsd-arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o
Jan F 003cb0b
 
Jan F 3657adf
-PORTS=port-aix.o port-irix.o port-linux.o port-linux-prng.o port-solaris.o port-tun.o port-uw.o
Jan F 3657adf
+PORTS=port-aix.o port-irix.o port-linux.o port-linux_part_2.o port-linux-prng.o port-solaris.o port-tun.o port-uw.o
Jan F 003cb0b
 
Jan F 003cb0b
 .c.o:
Jan F 003cb0b
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
Jan F 003cb0b
diff -up openssh-5.8p1/openbsd-compat/port-linux.c.role openssh-5.8p1/openbsd-compat/port-linux.c
Jan F 3657adf
--- openssh-5.8p1/openbsd-compat/port-linux.c.role	2011-03-22 18:32:17.782648196 +0100
Jan F 3657adf
+++ openssh-5.8p1/openbsd-compat/port-linux.c	2011-03-22 18:32:18.604648721 +0100
Jan F 003cb0b
@@ -31,48 +31,73 @@
Jan F 003cb0b
 
adad2a8
 #include "log.h"
974c89c
 #include "xmalloc.h"
Jan F 003cb0b
+#include "servconf.h"
adad2a8
 #include "port-linux.h"
adad2a8
+#include "key.h"
adad2a8
+#include "hostfile.h"
adad2a8
+#include "auth.h"
adad2a8
 
974c89c
 #ifdef WITH_SELINUX
adad2a8
 #include <selinux/selinux.h>
adad2a8
 #include <selinux/flask.h>
adad2a8
 #include <selinux/get_context_list.h>
adad2a8
 
Jan F 003cb0b
-/* Wrapper around is_selinux_enabled() to log its return value once only */
Jan F 003cb0b
-int
Jan F 003cb0b
-ssh_selinux_enabled(void)
Jan F 003cb0b
-{
Jan F 003cb0b
-	static int enabled = -1;
Jan F 003cb0b
+extern ServerOptions options;
adad2a8
+extern Authctxt *the_authctxt;
Jan F 003cb0b
+extern int inetd_flag;
Jan F 003cb0b
+extern int rexeced_flag;
Jan F 003cb0b
 
Jan F 003cb0b
-	if (enabled == -1) {
Jan F 003cb0b
-		enabled = (is_selinux_enabled() == 1);
Jan F 003cb0b
-		debug("SELinux support %s", enabled ? "enabled" : "disabled");
Jan F 003cb0b
+static void
Jan F 003cb0b
+ssh_selinux_get_role_level(char **role, const char **level)
Jan F 003cb0b
+{
Jan F 003cb0b
+	*role = NULL;
Jan F 003cb0b
+	*level = NULL;
Jan F 003cb0b
+	if (the_authctxt) {
Jan F 003cb0b
+		if (the_authctxt->role != NULL) {
Jan F 003cb0b
+			char *slash;
Jan F 003cb0b
+			*role = xstrdup(the_authctxt->role);
Jan F 003cb0b
+			if ((slash = strchr(*role, '/')) != NULL) {
Jan F 003cb0b
+				*slash = '\0';
Jan F 003cb0b
+				*level = slash + 1;
Jan F 003cb0b
+			}
Jan F 003cb0b
+		}
Jan F 003cb0b
 	}
Jan F 003cb0b
-
Jan F 003cb0b
-	return (enabled);
Jan F 003cb0b
 }
Jan F 003cb0b
 
Jan F 003cb0b
 /* Return the default security context for the given username */
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;
Jan F 003cb0b
+	char *role;
Jan F 003cb0b
+	const char *reqlvl;
adad2a8
+	int r = 0;
adad2a8
 
Jan F 003cb0b
+	ssh_selinux_get_role_level(&role, &reqlvl);
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()) {
Jan F 003cb0b
@@ -100,6 +125,36 @@ ssh_selinux_getctxbyname(char *pwname)
Jan F 003cb0b
 	return (sc);
Jan F 003cb0b
 }
Jan F 003cb0b
 
Jan F 003cb0b
+/* Setup environment variables for pam_selinux */
Jan F 003cb0b
+static int
Jan F 003cb0b
+ssh_selinux_setup_pam_variables(void)
Jan F 003cb0b
+{
Jan F 003cb0b
+	const char *reqlvl;
Jan F 003cb0b
+	char *role;
Jan F 003cb0b
+	char *use_current;
Jan F 003cb0b
+	int rv;
Jan F 003cb0b
+
Jan F 003cb0b
+	debug3("%s: setting execution context", __func__);
Jan F 003cb0b
+
Jan F 003cb0b
+	ssh_selinux_get_role_level(&role, &reqlvl);
Jan F 003cb0b
+
Jan F 003cb0b
+	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
Jan F 003cb0b
+	
Jan F 003cb0b
+	if (inetd_flag && !rexeced_flag) {
Jan F 003cb0b
+		use_current = "1";
Jan F 003cb0b
+	} else {
Jan F 003cb0b
+		use_current = "";
Jan F 003cb0b
+		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
Jan F 003cb0b
+	}
Jan F 003cb0b
+
Jan F 003cb0b
+	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
Jan F 003cb0b
+
Jan F 003cb0b
+	if (role != NULL)
Jan F 003cb0b
+		xfree(role);
Jan F 003cb0b
+	
Jan F 003cb0b
+	return rv;
Jan F 003cb0b
+}
Jan F 003cb0b
+
Jan F 003cb0b
 /* Set the execution context to the default for the specified user */
Jan F 003cb0b
 void
Jan F 003cb0b
 ssh_selinux_setup_exec_context(char *pwname)
Jan F 003cb0b
@@ -109,6 +164,24 @@ ssh_selinux_setup_exec_context(char *pwn
Jan F 003cb0b
 	if (!ssh_selinux_enabled())
Jan F 003cb0b
 		return;
Jan F 003cb0b
 
Jan F 003cb0b
+	if (options.use_pam) {
Jan F 003cb0b
+		/* do not compute context, just setup environment for pam_selinux */
Jan F 003cb0b
+		if (ssh_selinux_setup_pam_variables()) {
Jan F 003cb0b
+			switch (security_getenforce()) {
Jan F 003cb0b
+			case -1:
Jan F 003cb0b
+				fatal("%s: security_getenforce() failed", __func__);
Jan F 003cb0b
+			case 0:
Jan F 003cb0b
+				error("%s: SELinux PAM variable setup failure. Continuing in permissive mode.",
Jan F 003cb0b
+				    __func__);
Jan F 003cb0b
+			break;
Jan F 003cb0b
+			default:
Jan F 003cb0b
+				fatal("%s: SELinux PAM variable setup failure. Aborting connection.",
Jan F 003cb0b
+				    __func__);
Jan F 003cb0b
+			}
Jan F 003cb0b
+		}
Jan F 003cb0b
+		return;
Jan F 003cb0b
+	}
Jan F 003cb0b
+
Jan F 003cb0b
 	debug3("%s: setting execution context", __func__);
Jan F 003cb0b
 
Jan F 003cb0b
 	user_ctx = ssh_selinux_getctxbyname(pwname);
Jan F 003cb0b
@@ -206,21 +279,6 @@ ssh_selinux_change_context(const char *n
Jan F 003cb0b
 	xfree(newctx);
Jan F 003cb0b
 }
Jan F 003cb0b
 
Jan F 003cb0b
-void
Jan F 003cb0b
-ssh_selinux_setfscreatecon(const char *path)
Jan F 003cb0b
-{
Jan F 003cb0b
-	security_context_t context;
Jan F 003cb0b
-
Jan F 003cb0b
-	if (!ssh_selinux_enabled())
Jan F 003cb0b
-		return;
Jan F 003cb0b
-	if (path == NULL) {
Jan F 003cb0b
-		setfscreatecon(NULL);
Jan F 003cb0b
-		return;
Jan F 003cb0b
-	}
Jan F 003cb0b
-	if (matchpathcon(path, 0700, &context) == 0)
Jan F 003cb0b
-		setfscreatecon(context);
Jan F 003cb0b
-}
Jan F 003cb0b
-
Jan F 003cb0b
 #endif /* WITH_SELINUX */
Jan F 003cb0b
 
Jan F 003cb0b
 #ifdef LINUX_OOM_ADJUST
Jan F 003cb0b
diff -up openssh-5.8p1/openbsd-compat/port-linux_part_2.c.role openssh-5.8p1/openbsd-compat/port-linux_part_2.c
Jan F 3657adf
--- openssh-5.8p1/openbsd-compat/port-linux_part_2.c.role	2011-03-22 18:32:18.632648682 +0100
Jan F 3657adf
+++ openssh-5.8p1/openbsd-compat/port-linux_part_2.c	2011-03-22 18:32:18.641648698 +0100
Jan F 003cb0b
@@ -0,0 +1,75 @@
Jan F 003cb0b
+/* $Id: port-linux.c,v 1.11.4.2 2011/02/04 00:43:08 djm Exp $ */
Jan F 003cb0b
+
Jan F 003cb0b
+/*
Jan F 003cb0b
+ * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
Jan F 003cb0b
+ * Copyright (c) 2006 Damien Miller <djm@openbsd.org>
Jan F 003cb0b
+ *
Jan F 003cb0b
+ * Permission to use, copy, modify, and distribute this software for any
Jan F 003cb0b
+ * purpose with or without fee is hereby granted, provided that the above
Jan F 003cb0b
+ * copyright notice and this permission notice appear in all copies.
Jan F 003cb0b
+ *
Jan F 003cb0b
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
Jan F 003cb0b
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Jan F 003cb0b
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Jan F 003cb0b
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Jan F 003cb0b
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Jan F 003cb0b
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Jan F 003cb0b
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Jan F 003cb0b
+ */
Jan F 003cb0b
+
Jan F 003cb0b
+/*
Jan F 003cb0b
+ * Linux-specific portability code - just SELinux support at present
Jan F 003cb0b
+ */
Jan F 003cb0b
+
Jan F 003cb0b
+#include "includes.h"
Jan F 003cb0b
+
Jan F 003cb0b
+#if defined(WITH_SELINUX) || defined(LINUX_OOM_ADJUST)
Jan F 003cb0b
+#include <errno.h>
Jan F 003cb0b
+#include <stdarg.h>
Jan F 003cb0b
+#include <string.h>
Jan F 003cb0b
+#include <stdio.h>
Jan F 003cb0b
+
Jan F 003cb0b
+#include "log.h"
Jan F 003cb0b
+#include "xmalloc.h"
Jan F 003cb0b
+#include "port-linux.h"
Jan F 003cb0b
+#include "key.h"
Jan F 003cb0b
+#include "hostfile.h"
Jan F 003cb0b
+#include "auth.h"
Jan F 003cb0b
+
Jan F 003cb0b
+#ifdef WITH_SELINUX
Jan F 003cb0b
+#include <selinux/selinux.h>
Jan F 003cb0b
+#include <selinux/flask.h>
Jan F 003cb0b
+#include <selinux/get_context_list.h>
Jan F 003cb0b
+
Jan F 003cb0b
+/* Wrapper around is_selinux_enabled() to log its return value once only */
Jan F 003cb0b
+int
Jan F 003cb0b
+ssh_selinux_enabled(void)
Jan F 003cb0b
+{
Jan F 003cb0b
+	static int enabled = -1;
Jan F 003cb0b
+
Jan F 003cb0b
+	if (enabled == -1) {
Jan F 003cb0b
+		enabled = (is_selinux_enabled() == 1);
Jan F 003cb0b
+		debug("SELinux support %s", enabled ? "enabled" : "disabled");
Jan F 003cb0b
+	}
Jan F 003cb0b
+
Jan F 003cb0b
+	return (enabled);
Jan F 003cb0b
+}
Jan F 003cb0b
+
Jan F 003cb0b
+void
Jan F 003cb0b
+ssh_selinux_setfscreatecon(const char *path)
Jan F 003cb0b
+{
Jan F 003cb0b
+	security_context_t context;
Jan F 003cb0b
+
Jan F 003cb0b
+	if (!ssh_selinux_enabled())
Jan F 003cb0b
+		return;
Jan F 003cb0b
+	if (path == NULL) {
Jan F 003cb0b
+		setfscreatecon(NULL);
Jan F 003cb0b
+		return;
Jan F 003cb0b
+	}
Jan F 003cb0b
+	if (matchpathcon(path, 0700, &context) == 0)
Jan F 003cb0b
+		setfscreatecon(context);
Jan F 003cb0b
+}
Jan F 003cb0b
+
Jan F 003cb0b
+#endif /* WITH_SELINUX */
Jan F 003cb0b
+
Jan F 003cb0b
+#endif /* WITH_SELINUX || LINUX_OOM_ADJUST */