vishalmishra434 / rpms / openssh

Forked from rpms/openssh 3 months ago
Clone
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/auth1.c.selinux openssh-5.2p1/auth1.c
Jan F. Chadima a3ba41
--- openssh-5.2p1/auth1.c.selinux	2009-03-10 03:51:54.813223420 +0100
Jan F. Chadima a3ba41
+++ openssh-5.2p1/auth1.c	2009-03-10 03:51:55.045214236 +0100
Jan F. Chadima a3ba41
@@ -391,7 +391,7 @@ void
Jan F. Chadima a3ba41
 do_authentication(Authctxt *authctxt)
Jan F. Chadima a3ba41
 {
Jan F. Chadima a3ba41
 	u_int ulen;
Jan F. Chadima a3ba41
-	char *user, *style = NULL;
Jan F. Chadima a3ba41
+	char *user, *style = NULL, *role=NULL;
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 	/* Get the name of the user that we wish to log in as. */
Jan F. Chadima a3ba41
 	packet_read_expect(SSH_CMSG_USER);
Jan F. Chadima a3ba41
@@ -400,11 +400,19 @@ do_authentication(Authctxt *authctxt)
Jan F. Chadima a3ba41
 	user = packet_get_string(&ulen);
Jan F. Chadima a3ba41
 	packet_check_eom();
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
+	if ((role = strchr(user, '/')) != NULL)
Jan F. Chadima a3ba41
+		*role++ = '\0';
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
 	if ((style = strchr(user, ':')) != NULL)
Jan F. Chadima a3ba41
 		*style++ = '\0';
Jan F. Chadima a3ba41
+	else
Jan F. Chadima a3ba41
+		if (role && (style = strchr(role, ':')) != NULL)
Jan F. Chadima a3ba41
+			*style++ = '\0';
Jan F. Chadima a3ba41
+			
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 	authctxt->user = user;
Jan F. Chadima a3ba41
 	authctxt->style = style;
Jan F. Chadima a3ba41
+	authctxt->role = role;
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 	/* Verify that the user is a valid user. */
Jan F. Chadima a3ba41
 	if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/auth2.c.selinux openssh-5.2p1/auth2.c
Jan F. Chadima a3ba41
--- openssh-5.2p1/auth2.c.selinux	2008-11-05 06:20:46.000000000 +0100
Jan F. Chadima a3ba41
+++ openssh-5.2p1/auth2.c	2009-03-10 03:51:55.071216534 +0100
Jan F. Chadima a3ba41
@@ -215,7 +215,7 @@ input_userauth_request(int type, u_int32
Jan F. Chadima a3ba41
 {
Jan F. Chadima a3ba41
 	Authctxt *authctxt = ctxt;
Jan F. Chadima a3ba41
 	Authmethod *m = NULL;
Jan F. Chadima a3ba41
-	char *user, *service, *method, *style = NULL;
Jan F. Chadima a3ba41
+	char *user, *service, *method, *style = NULL, *role = NULL;
Jan F. Chadima a3ba41
 	int authenticated = 0;
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 	if (authctxt == NULL)
Jan F. Chadima a3ba41
@@ -227,6 +227,9 @@ input_userauth_request(int type, u_int32
Jan F. Chadima a3ba41
 	debug("userauth-request for user %s service %s method %s", user, service, method);
Jan F. Chadima a3ba41
 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
+	if ((role = strchr(user, '/')) != NULL)
Jan F. Chadima a3ba41
+		*role++ = 0;
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
 	if ((style = strchr(user, ':')) != NULL)
Jan F. Chadima a3ba41
 		*style++ = 0;
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
@@ -252,8 +255,11 @@ input_userauth_request(int type, u_int32
Jan F. Chadima a3ba41
 		    use_privsep ? " [net]" : "");
Jan F. Chadima a3ba41
 		authctxt->service = xstrdup(service);
Jan F. Chadima a3ba41
 		authctxt->style = style ? xstrdup(style) : NULL;
Jan F. Chadima a3ba41
-		if (use_privsep)
Jan F. Chadima a3ba41
+		authctxt->role = role ? xstrdup(role) : NULL;
Jan F. Chadima a3ba41
+		if (use_privsep) {
Jan F. Chadima a3ba41
 			mm_inform_authserv(service, style);
Jan F. Chadima a3ba41
+			mm_inform_authrole(role);
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
 		userauth_banner();
Jan F. Chadima a3ba41
 	} else if (strcmp(user, authctxt->user) != 0 ||
Jan F. Chadima a3ba41
 	    strcmp(service, authctxt->service) != 0) {
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/auth2-hostbased.c.selinux openssh-5.2p1/auth2-hostbased.c
Jan F. Chadima a3ba41
--- openssh-5.2p1/auth2-hostbased.c.selinux	2008-07-17 10:57:19.000000000 +0200
Jan F. Chadima a3ba41
+++ openssh-5.2p1/auth2-hostbased.c	2009-03-10 03:51:55.052265447 +0100
Jan F. Chadima a3ba41
@@ -106,7 +106,14 @@ userauth_hostbased(Authctxt *authctxt)
Jan F. Chadima a3ba41
 	buffer_put_string(&b, session_id2, session_id2_len);
Jan F. Chadima a3ba41
 	/* reconstruct packet */
Jan F. Chadima a3ba41
 	buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Jan F. Chadima a3ba41
-	buffer_put_cstring(&b, authctxt->user);
Jan F. Chadima a3ba41
+	if (authctxt->role) {
Jan F. Chadima a3ba41
+		buffer_put_int(&b, strlen(authctxt->user)+strlen(authctxt->role)+1);
Jan F. Chadima a3ba41
+		buffer_append(&b, authctxt->user, strlen(authctxt->user));
Jan F. Chadima a3ba41
+		buffer_put_char(&b, '/');
Jan F. Chadima a3ba41
+		buffer_append(&b, authctxt->role, strlen(authctxt->role));
Jan F. Chadima a3ba41
+	} else {
Jan F. Chadima a3ba41
+		buffer_put_cstring(&b, authctxt->user);
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
 	buffer_put_cstring(&b, service);
Jan F. Chadima a3ba41
 	buffer_put_cstring(&b, "hostbased");
Jan F. Chadima a3ba41
 	buffer_put_string(&b, pkalg, alen);
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/auth2-pubkey.c.selinux openssh-5.2p1/auth2-pubkey.c
Jan F. Chadima a3ba41
--- openssh-5.2p1/auth2-pubkey.c.selinux	2008-07-04 04:54:25.000000000 +0200
Jan F. Chadima a3ba41
+++ openssh-5.2p1/auth2-pubkey.c	2009-03-10 03:51:56.174214480 +0100
Jan F. Chadima a3ba41
@@ -33,6 +33,7 @@
Jan F. Chadima a3ba41
 #include <stdio.h>
Jan F. Chadima a3ba41
 #include <stdarg.h>
Jan F. Chadima a3ba41
 #include <unistd.h>
Jan F. Chadima a3ba41
+#include <openssl/fips.h>
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 #include "xmalloc.h"
Jan F. Chadima a3ba41
 #include "ssh.h"
Jan F. Chadima a3ba41
@@ -117,7 +118,14 @@ userauth_pubkey(Authctxt *authctxt)
Jan F. Chadima a3ba41
 		}
Jan F. Chadima a3ba41
 		/* reconstruct packet */
Jan F. Chadima a3ba41
 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Jan F. Chadima a3ba41
-		buffer_put_cstring(&b, authctxt->user);
Jan F. Chadima a3ba41
+		if (authctxt->role) {
Jan F. Chadima a3ba41
+			buffer_put_int(&b, strlen(authctxt->user)+strlen(authctxt->role)+1);
Jan F. Chadima a3ba41
+			buffer_append(&b, authctxt->user, strlen(authctxt->user));
Jan F. Chadima a3ba41
+			buffer_put_char(&b, '/');
Jan F. Chadima a3ba41
+			buffer_append(&b, authctxt->role, strlen(authctxt->role));
Jan F. Chadima a3ba41
+		} else {
Jan F. Chadima a3ba41
+			buffer_put_cstring(&b, authctxt->user);
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
 		buffer_put_cstring(&b,
Jan F. Chadima a3ba41
 		    datafellows & SSH_BUG_PKSERVICE ?
Jan F. Chadima a3ba41
 		    "ssh-userauth" :
Jan F. Chadima a3ba41
@@ -236,7 +244,7 @@ user_key_allowed2(struct passwd *pw, Key
Jan F. Chadima a3ba41
 			found_key = 1;
Jan F. Chadima a3ba41
 			debug("matching key found: file %s, line %lu",
Jan F. Chadima a3ba41
 			    file, linenum);
Jan F. Chadima a3ba41
-			fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
Jan F. Chadima a3ba41
+			fp = key_fingerprint(found, FIPS_mode() ? SSH_FP_SHA1 : SSH_FP_MD5, SSH_FP_HEX);
Jan F. Chadima a3ba41
 			verbose("Found matching %s key: %s",
Jan F. Chadima a3ba41
 			    key_type(found), fp);
Jan F. Chadima a3ba41
 			xfree(fp);
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/auth.h.selinux openssh-5.2p1/auth.h
Jan F. Chadima a3ba41
--- openssh-5.2p1/auth.h.selinux	2008-11-05 06:20:46.000000000 +0100
Jan F. Chadima a3ba41
+++ openssh-5.2p1/auth.h	2009-03-10 03:51:55.069276014 +0100
Jan F. Chadima a3ba41
@@ -58,6 +58,7 @@ struct Authctxt {
Jan F. Chadima a3ba41
 	char		*service;
Jan F. Chadima a3ba41
 	struct passwd	*pw;		/* set if 'valid' */
Jan F. Chadima a3ba41
 	char		*style;
Jan F. Chadima a3ba41
+	char		*role;
Jan F. Chadima a3ba41
 	void		*kbdintctxt;
Jan F. Chadima a3ba41
 	void		*jpake_ctx;
Jan F. Chadima a3ba41
 #ifdef BSD_AUTH
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/configure.ac.selinux openssh-5.2p1/configure.ac
Jan F. Chadima a3ba41
--- openssh-5.2p1/configure.ac.selinux	2009-03-10 03:51:54.901396765 +0100
Jan F. Chadima a3ba41
+++ openssh-5.2p1/configure.ac	2009-03-10 03:51:55.850215090 +0100
Jan F. Chadima a3ba41
@@ -3335,11 +3335,25 @@ AC_ARG_WITH(selinux,
Jan F. Chadima a3ba41
 		AC_CHECK_LIB(selinux, setexeccon, [ LIBSELINUX="-lselinux" ],
Jan F. Chadima a3ba41
 		    AC_MSG_ERROR(SELinux support requires libselinux library))
Jan F. Chadima a3ba41
 		SSHDLIBS="$SSHDLIBS $LIBSELINUX"
Jan F. Chadima a3ba41
+		LIBS="$LIBS $LIBSELINUX"
Jan F. Chadima a3ba41
 		AC_CHECK_FUNCS(getseuserbyname get_default_context_with_level)
Jan F. Chadima a3ba41
+		AC_CHECK_FUNCS(setkeycreatecon)
Jan F. Chadima a3ba41
 		LIBS="$save_LIBS"
Jan F. Chadima a3ba41
 	fi ]
Jan F. Chadima a3ba41
 )
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
+# Check whether user wants Linux audit support
Jan F. Chadima a3ba41
+LINUX_AUDIT_MSG="no"
Jan F. Chadima a3ba41
+AC_ARG_WITH(linux-audit,
Jan F. Chadima a3ba41
+	[  --with-linux-audit   Enable Linux audit support],
Jan F. Chadima a3ba41
+	[ if test "x$withval" != "xno" ; then
Jan F. Chadima a3ba41
+		AC_DEFINE(HAVE_LINUX_AUDIT,1,[Define if you want Linux audit support.])
Jan F. Chadima a3ba41
+		LINUX_AUDIT_MSG="yes"
Jan F. Chadima a3ba41
+		AC_CHECK_HEADERS(libaudit.h)
Jan F. Chadima a3ba41
+		SSHDLIBS="$SSHDLIBS -laudit"
Jan F. Chadima a3ba41
+	fi ]
Jan F. Chadima a3ba41
+)
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
 # Check whether user wants Kerberos 5 support
Jan F. Chadima a3ba41
 KRB5_MSG="no"
Jan F. Chadima a3ba41
 AC_ARG_WITH(kerberos5,
Jan F. Chadima a3ba41
@@ -3448,6 +3462,20 @@ AC_ARG_WITH(kerberos5,
Jan F. Chadima a3ba41
 	]
Jan F. Chadima a3ba41
 )
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
+# Check whether user wants NSS support
Jan F. Chadima a3ba41
+LIBNSS_MSG="no"
Jan F. Chadima a3ba41
+AC_ARG_WITH(nss,
Jan F. Chadima a3ba41
+	[  --with-nss   Enable NSS support],
Jan F. Chadima a3ba41
+	[ if test "x$withval" != "xno" ; then
Jan F. Chadima a3ba41
+		AC_DEFINE(HAVE_LIBNSS,1,[Define if you want NSS support.])
Jan F. Chadima a3ba41
+		LIBNSS_MSG="yes"
Jan F. Chadima a3ba41
+		CPPFLAGS="$CPPFLAGS -I/usr/include/nss3 -I/usr/include/nspr4"
Jan F. Chadima a3ba41
+		AC_CHECK_HEADERS(pk11pub.h)
Jan F. Chadima a3ba41
+		LIBS="$LIBS -lnss3"
Jan F. Chadima a3ba41
+	fi
Jan F. Chadima a3ba41
+	])
Jan F. Chadima a3ba41
+AC_SUBST(LIBNSS)
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
 # Looking for programs, paths and files
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 PRIVSEP_PATH=/var/empty
Jan F. Chadima a3ba41
@@ -4168,12 +4196,14 @@ echo "                       PAM support
Jan F. Chadima a3ba41
 echo "                   OSF SIA support: $SIA_MSG"
Jan F. Chadima a3ba41
 echo "                 KerberosV support: $KRB5_MSG"
Jan F. Chadima a3ba41
 echo "                   SELinux support: $SELINUX_MSG"
Jan F. Chadima a3ba41
+echo "               Linux audit support: $LINUX_AUDIT_MSG"
Jan F. Chadima a3ba41
 echo "                 Smartcard support: $SCARD_MSG"
Jan F. Chadima a3ba41
 echo "                     S/KEY support: $SKEY_MSG"
Jan F. Chadima a3ba41
 echo "              TCP Wrappers support: $TCPW_MSG"
Jan F. Chadima a3ba41
 echo "              MD5 password support: $MD5_MSG"
Jan F. Chadima a3ba41
 echo "                   libedit support: $LIBEDIT_MSG"
Jan F. Chadima a3ba41
 echo "  Solaris process contract support: $SPC_MSG"
Jan F. Chadima a3ba41
+echo "                       NSS support: $LIBNSS_MSG"
Jan F. Chadima a3ba41
 echo "       IP address in \$DISPLAY hack: $DISPLAY_HACK_MSG"
Jan F. Chadima a3ba41
 echo "           Translate v4 in v6 hack: $IPV4_IN6_HACK_MSG"
Jan F. Chadima a3ba41
 echo "                  BSD Auth support: $BSD_AUTH_MSG"
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/monitor.c.selinux openssh-5.2p1/monitor.c
Jan F. Chadima a3ba41
--- openssh-5.2p1/monitor.c.selinux	2009-02-14 06:33:31.000000000 +0100
Jan F. Chadima a3ba41
+++ openssh-5.2p1/monitor.c	2009-03-10 03:51:55.099559692 +0100
Jan F. Chadima a3ba41
@@ -135,6 +135,7 @@ int mm_answer_sign(int, Buffer *);
Jan F. Chadima a3ba41
 int mm_answer_pwnamallow(int, Buffer *);
Jan F. Chadima a3ba41
 int mm_answer_auth2_read_banner(int, Buffer *);
Jan F. Chadima a3ba41
 int mm_answer_authserv(int, Buffer *);
Jan F. Chadima a3ba41
+int mm_answer_authrole(int, Buffer *);
Jan F. Chadima a3ba41
 int mm_answer_authpassword(int, Buffer *);
Jan F. Chadima a3ba41
 int mm_answer_bsdauthquery(int, Buffer *);
Jan F. Chadima a3ba41
 int mm_answer_bsdauthrespond(int, Buffer *);
Jan F. Chadima a3ba41
@@ -211,6 +212,7 @@ struct mon_table mon_dispatch_proto20[] 
Jan F. Chadima a3ba41
     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
Jan F. Chadima a3ba41
     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
Jan F. Chadima a3ba41
     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Jan F. Chadima a3ba41
+    {MONITOR_REQ_AUTHROLE, MON_ONCE, mm_answer_authrole},
Jan F. Chadima a3ba41
     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Jan F. Chadima a3ba41
     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Jan F. Chadima a3ba41
 #ifdef USE_PAM
Jan F. Chadima a3ba41
@@ -680,6 +682,7 @@ mm_answer_pwnamallow(int sock, Buffer *m
Jan F. Chadima a3ba41
 	else {
Jan F. Chadima a3ba41
 		/* Allow service/style information on the auth context */
Jan F. Chadima a3ba41
 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Jan F. Chadima a3ba41
+		monitor_permit(mon_dispatch, MONITOR_REQ_AUTHROLE, 1);
Jan F. Chadima a3ba41
 		monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
Jan F. Chadima a3ba41
 	}
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
@@ -725,6 +728,23 @@ mm_answer_authserv(int sock, Buffer *m)
Jan F. Chadima a3ba41
 }
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 int
Jan F. Chadima a3ba41
+mm_answer_authrole(int sock, Buffer *m)
Jan F. Chadima a3ba41
+{
Jan F. Chadima a3ba41
+	monitor_permit_authentications(1);
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	authctxt->role = buffer_get_string(m, NULL);
Jan F. Chadima a3ba41
+	debug3("%s: role=%s",
Jan F. Chadima a3ba41
+	    __func__, authctxt->role);
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	if (strlen(authctxt->role) == 0) {
Jan F. Chadima a3ba41
+		xfree(authctxt->role);
Jan F. Chadima a3ba41
+		authctxt->role = NULL;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	return (0);
Jan F. Chadima a3ba41
+}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+int
Jan F. Chadima a3ba41
 mm_answer_authpassword(int sock, Buffer *m)
Jan F. Chadima a3ba41
 {
Jan F. Chadima a3ba41
 	static int call_count;
Jan F. Chadima a3ba41
@@ -1102,7 +1122,7 @@ static int
Jan F. Chadima a3ba41
 monitor_valid_userblob(u_char *data, u_int datalen)
Jan F. Chadima a3ba41
 {
Jan F. Chadima a3ba41
 	Buffer b;
Jan F. Chadima a3ba41
-	char *p;
Jan F. Chadima a3ba41
+	char *p, *r;
Jan F. Chadima a3ba41
 	u_int len;
Jan F. Chadima a3ba41
 	int fail = 0;
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
@@ -1128,6 +1148,8 @@ monitor_valid_userblob(u_char *data, u_i
Jan F. Chadima a3ba41
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
Jan F. Chadima a3ba41
 		fail++;
Jan F. Chadima a3ba41
 	p = buffer_get_string(&b, NULL);
Jan F. Chadima a3ba41
+	if ((r = strchr(p, '/')) != NULL)
Jan F. Chadima a3ba41
+		*r = '\0';
Jan F. Chadima a3ba41
 	if (strcmp(authctxt->user, p) != 0) {
Jan F. Chadima a3ba41
 		logit("wrong user name passed to monitor: expected %s != %.100s",
Jan F. Chadima a3ba41
 		    authctxt->user, p);
Jan F. Chadima a3ba41
@@ -1159,7 +1181,7 @@ monitor_valid_hostbasedblob(u_char *data
Jan F. Chadima a3ba41
     char *chost)
Jan F. Chadima a3ba41
 {
Jan F. Chadima a3ba41
 	Buffer b;
Jan F. Chadima a3ba41
-	char *p;
Jan F. Chadima a3ba41
+	char *p, *r;
Jan F. Chadima a3ba41
 	u_int len;
Jan F. Chadima a3ba41
 	int fail = 0;
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
@@ -1176,6 +1198,8 @@ monitor_valid_hostbasedblob(u_char *data
Jan F. Chadima a3ba41
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
Jan F. Chadima a3ba41
 		fail++;
Jan F. Chadima a3ba41
 	p = buffer_get_string(&b, NULL);
Jan F. Chadima a3ba41
+	if ((r = strchr(p, '/')) != NULL)
Jan F. Chadima a3ba41
+		*r = '\0';
Jan F. Chadima a3ba41
 	if (strcmp(authctxt->user, p) != 0) {
Jan F. Chadima a3ba41
 		logit("wrong user name passed to monitor: expected %s != %.100s",
Jan F. Chadima a3ba41
 		    authctxt->user, p);
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/monitor.h.selinux openssh-5.2p1/monitor.h
Jan F. Chadima a3ba41
--- openssh-5.2p1/monitor.h.selinux	2008-11-05 06:20:46.000000000 +0100
Jan F. Chadima a3ba41
+++ openssh-5.2p1/monitor.h	2009-03-10 03:51:55.050519421 +0100
Jan F. Chadima a3ba41
@@ -30,7 +30,7 @@
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 enum monitor_reqtype {
Jan F. Chadima a3ba41
 	MONITOR_REQ_MODULI, MONITOR_ANS_MODULI,
Jan F. Chadima a3ba41
-	MONITOR_REQ_FREE, MONITOR_REQ_AUTHSERV,
Jan F. Chadima a3ba41
+	MONITOR_REQ_FREE, MONITOR_REQ_AUTHSERV,MONITOR_REQ_AUTHROLE,
Jan F. Chadima a3ba41
 	MONITOR_REQ_SIGN, MONITOR_ANS_SIGN,
Jan F. Chadima a3ba41
 	MONITOR_REQ_PWNAM, MONITOR_ANS_PWNAM,
Jan F. Chadima a3ba41
 	MONITOR_REQ_AUTH2_READ_BANNER, MONITOR_ANS_AUTH2_READ_BANNER,
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/monitor_wrap.c.selinux openssh-5.2p1/monitor_wrap.c
Jan F. Chadima a3ba41
--- openssh-5.2p1/monitor_wrap.c.selinux	2008-11-05 06:20:47.000000000 +0100
Jan F. Chadima a3ba41
+++ openssh-5.2p1/monitor_wrap.c	2009-03-10 03:51:55.066210099 +0100
Jan F. Chadima a3ba41
@@ -297,6 +297,23 @@ mm_inform_authserv(char *service, char *
Jan F. Chadima a3ba41
 	buffer_free(&m);
Jan F. Chadima a3ba41
 }
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
+/* Inform the privileged process about role */
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+void
Jan F. Chadima a3ba41
+mm_inform_authrole(char *role)
Jan F. Chadima a3ba41
+{
Jan F. Chadima a3ba41
+	Buffer m;
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	debug3("%s entering", __func__);
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	buffer_init(&m);
Jan F. Chadima a3ba41
+	buffer_put_cstring(&m, role ? role : "");
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHROLE, &m);
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	buffer_free(&m);
Jan F. Chadima a3ba41
+}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
 /* Do the password authentication */
Jan F. Chadima a3ba41
 int
Jan F. Chadima a3ba41
 mm_auth_password(Authctxt *authctxt, char *password)
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/monitor_wrap.h.selinux openssh-5.2p1/monitor_wrap.h
Jan F. Chadima a3ba41
--- openssh-5.2p1/monitor_wrap.h.selinux	2008-11-05 06:20:47.000000000 +0100
Jan F. Chadima a3ba41
+++ openssh-5.2p1/monitor_wrap.h	2009-03-10 03:51:55.048731957 +0100
Jan F. Chadima a3ba41
@@ -41,6 +41,7 @@ int mm_is_monitor(void);
Jan F. Chadima a3ba41
 DH *mm_choose_dh(int, int, int);
Jan F. Chadima a3ba41
 int mm_key_sign(Key *, u_char **, u_int *, u_char *, u_int);
Jan F. Chadima a3ba41
 void mm_inform_authserv(char *, char *);
Jan F. Chadima a3ba41
+void mm_inform_authrole(char *);
Jan F. Chadima a3ba41
 struct passwd *mm_getpwnamallow(const char *);
Jan F. Chadima a3ba41
 char *mm_auth2_read_banner(void);
Jan F. Chadima a3ba41
 int mm_auth_password(struct Authctxt *, char *);
Jan F. Chadima a3ba41
diff -up openssh-5.2p1/openbsd-compat/port-linux.c.selinux openssh-5.2p1/openbsd-compat/port-linux.c
Jan F. Chadima a3ba41
--- openssh-5.2p1/openbsd-compat/port-linux.c.selinux	2008-03-26 21:27:21.000000000 +0100
Jan F. Chadima a3ba41
+++ openssh-5.2p1/openbsd-compat/port-linux.c	2009-03-10 03:51:55.407292576 +0100
Jan F. Chadima a3ba41
@@ -30,10 +30,28 @@
Jan F. Chadima a3ba41
 #ifdef WITH_SELINUX
Jan F. Chadima a3ba41
 #include "log.h"
Jan F. Chadima a3ba41
 #include "port-linux.h"
Jan F. Chadima a3ba41
+#include "key.h"
Jan F. Chadima a3ba41
+#include "hostfile.h"
Jan F. Chadima a3ba41
+#include "auth.h"
Jan F. Chadima a3ba41
+#include "xmalloc.h"
Jan F. Chadima a3ba41
+#include "servconf.h"
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 #include <selinux/selinux.h>
Jan F. Chadima a3ba41
 #include <selinux/flask.h>
Jan F. Chadima a3ba41
+#include <selinux/context.h>
Jan F. Chadima a3ba41
 #include <selinux/get_context_list.h>
Jan F. Chadima a3ba41
+#include <selinux/get_default_type.h>
Jan F. Chadima a3ba41
+#include <selinux/av_permissions.h>
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+#ifdef HAVE_LINUX_AUDIT
Jan F. Chadima a3ba41
+#include <libaudit.h>
Jan F. Chadima a3ba41
+#include <unistd.h>
Jan F. Chadima a3ba41
+#endif
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+extern ServerOptions options;
Jan F. Chadima a3ba41
+extern Authctxt *the_authctxt;
Jan F. Chadima a3ba41
+extern int inetd_flag;
Jan F. Chadima a3ba41
+extern int rexeced_flag;
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 /* Wrapper around is_selinux_enabled() to log its return value once only */
Jan F. Chadima a3ba41
 int
Jan F. Chadima a3ba41
@@ -49,42 +67,245 @@ ssh_selinux_enabled(void)
Jan F. Chadima a3ba41
 	return (enabled);
Jan F. Chadima a3ba41
 }
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
+/* Send audit message */
Jan F. Chadima a3ba41
+static int
Jan F. Chadima a3ba41
+send_audit_message(int success, security_context_t default_context,
Jan F. Chadima a3ba41
+		       security_context_t selected_context)
Jan F. Chadima a3ba41
+{
Jan F. Chadima a3ba41
+	int rc=0;
Jan F. Chadima a3ba41
+#ifdef HAVE_LINUX_AUDIT
Jan F. Chadima a3ba41
+	char *msg = NULL;
Jan F. Chadima a3ba41
+	int audit_fd = audit_open();
Jan F. Chadima a3ba41
+	security_context_t default_raw=NULL;
Jan F. Chadima a3ba41
+	security_context_t selected_raw=NULL;
Jan F. Chadima a3ba41
+	rc = -1;
Jan F. Chadima a3ba41
+	if (audit_fd < 0) {
Jan F. Chadima a3ba41
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Jan F. Chadima a3ba41
+                                        errno == EAFNOSUPPORT)
Jan F. Chadima a3ba41
+                        return 0; /* No audit support in kernel */
Jan F. Chadima a3ba41
+		error("Error connecting to audit system.");
Jan F. Chadima a3ba41
+		return rc;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+	if (selinux_trans_to_raw_context(default_context, &default_raw) < 0) {
Jan F. Chadima a3ba41
+		error("Error translating default context.");
Jan F. Chadima a3ba41
+		default_raw = NULL;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+	if (selinux_trans_to_raw_context(selected_context, &selected_raw) < 0) {
Jan F. Chadima a3ba41
+		error("Error translating selected context.");
Jan F. Chadima a3ba41
+		selected_raw = NULL;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+	if (asprintf(&msg, "sshd: default-context=%s selected-context=%s",
Jan F. Chadima a3ba41
+		     default_raw ? default_raw : (default_context ? default_context: "?"),
Jan F. Chadima a3ba41
+		     selected_context ? selected_raw : (selected_context ? selected_context :"?")) < 0) {
Jan F. Chadima a3ba41
+		error("Error allocating memory.");
Jan F. Chadima a3ba41
+		goto out;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+	if (audit_log_user_message(audit_fd, AUDIT_USER_ROLE_CHANGE,
Jan F. Chadima a3ba41
+				   msg, NULL, NULL, NULL, success) <= 0) {
Jan F. Chadima a3ba41
+		error("Error sending audit message.");
Jan F. Chadima a3ba41
+		goto out;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+	rc = 0;
Jan F. Chadima a3ba41
+      out:
Jan F. Chadima a3ba41
+	free(msg);
Jan F. Chadima a3ba41
+	freecon(default_raw);
Jan F. Chadima a3ba41
+	freecon(selected_raw);
Jan F. Chadima a3ba41
+	close(audit_fd);
Jan F. Chadima a3ba41
+#endif
Jan F. Chadima a3ba41
+	return rc;
Jan F. Chadima a3ba41
+}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+static int
Jan F. Chadima a3ba41
+mls_range_allowed(security_context_t src, security_context_t dst)
Jan F. Chadima a3ba41
+{
Jan F. Chadima a3ba41
+	struct av_decision avd;
Jan F. Chadima a3ba41
+	int retval;
Jan F. Chadima a3ba41
+	unsigned int bit = CONTEXT__CONTAINS;
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	debug("%s: src:%s dst:%s", __func__, src, dst);
Jan F. Chadima a3ba41
+	retval = security_compute_av(src, dst, SECCLASS_CONTEXT, bit, &avd);
Jan F. Chadima a3ba41
+	if (retval || ((bit & avd.allowed) != bit))
Jan F. Chadima a3ba41
+		return 0;
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	return 1;
Jan F. Chadima a3ba41
+}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+static int
Jan F. Chadima a3ba41
+get_user_context(const char *sename, const char *role, const char *lvl,
Jan F. Chadima a3ba41
+	security_context_t *sc) {
Jan F. Chadima a3ba41
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Jan F. Chadima a3ba41
+	if (lvl == NULL || lvl[0] == '\0' || get_default_context_with_level(sename, lvl, NULL, sc) != 0) {
Jan F. Chadima a3ba41
+	        /* User may have requested a level completely outside of his 
Jan F. Chadima a3ba41
+	           allowed range. We get a context just for auditing as the
Jan F. Chadima a3ba41
+	           range check below will certainly fail for default context. */
Jan F. Chadima a3ba41
+#endif
Jan F. Chadima a3ba41
+		if (get_default_context(sename, NULL, sc) != 0) {
Jan F. Chadima a3ba41
+			*sc = NULL;
Jan F. Chadima a3ba41
+			return -1;
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+#endif
Jan F. Chadima a3ba41
+	if (role != NULL && role[0]) {
Jan F. Chadima a3ba41
+		context_t con;
Jan F. Chadima a3ba41
+		char *type=NULL;
Jan F. Chadima a3ba41
+		if (get_default_type(role, &type) != 0) {
Jan F. Chadima a3ba41
+			error("get_default_type: failed to get default type for '%s'",
Jan F. Chadima a3ba41
+				role);
Jan F. Chadima a3ba41
+			goto out;
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+		con = context_new(*sc);
Jan F. Chadima a3ba41
+		if (!con) {
Jan F. Chadima a3ba41
+			goto out;
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+		context_role_set(con, role);
Jan F. Chadima a3ba41
+		context_type_set(con, type);
Jan F. Chadima a3ba41
+		freecon(*sc);
Jan F. Chadima a3ba41
+		*sc = strdup(context_str(con));
Jan F. Chadima a3ba41
+		context_free(con);
Jan F. Chadima a3ba41
+		if (!*sc) 
Jan F. Chadima a3ba41
+			return -1;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Jan F. Chadima a3ba41
+	if (lvl != NULL && lvl[0]) {
Jan F. Chadima a3ba41
+		/* verify that the requested range is obtained */
Jan F. Chadima a3ba41
+		context_t con;
Jan F. Chadima a3ba41
+		security_context_t obtained_raw;
Jan F. Chadima a3ba41
+		security_context_t requested_raw;
Jan F. Chadima a3ba41
+		con = context_new(*sc);
Jan F. Chadima a3ba41
+		if (!con) {
Jan F. Chadima a3ba41
+			goto out;
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+		context_range_set(con, lvl);
Jan F. Chadima a3ba41
+		if (selinux_trans_to_raw_context(*sc, &obtained_raw) < 0) {
Jan F. Chadima a3ba41
+			context_free(con);
Jan F. Chadima a3ba41
+			goto out;
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+		if (selinux_trans_to_raw_context(context_str(con), &requested_raw) < 0) {
Jan F. Chadima a3ba41
+			freecon(obtained_raw);
Jan F. Chadima a3ba41
+			context_free(con);
Jan F. Chadima a3ba41
+			goto out;
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+		debug("get_user_context: obtained context '%s' requested context '%s'",
Jan F. Chadima a3ba41
+			obtained_raw, requested_raw);
Jan F. Chadima a3ba41
+		if (strcmp(obtained_raw, requested_raw)) {
Jan F. Chadima a3ba41
+			/* set the context to the real requested one but fail */
Jan F. Chadima a3ba41
+			freecon(requested_raw);
Jan F. Chadima a3ba41
+			freecon(obtained_raw);
Jan F. Chadima a3ba41
+			freecon(*sc);
Jan F. Chadima a3ba41
+			*sc = strdup(context_str(con));
Jan F. Chadima a3ba41
+			context_free(con);
Jan F. Chadima a3ba41
+			return -1;
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+		freecon(requested_raw);
Jan F. Chadima a3ba41
+		freecon(obtained_raw);
Jan F. Chadima a3ba41
+		context_free(con);
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+#endif
Jan F. Chadima a3ba41
+	return 0;
Jan F. Chadima a3ba41
+      out:
Jan F. Chadima a3ba41
+        freecon(*sc);
Jan F. Chadima a3ba41
+        *sc = NULL;
Jan F. Chadima a3ba41
+        return -1;
Jan F. Chadima a3ba41
+}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+static void
Jan F. Chadima a3ba41
+ssh_selinux_get_role_level(char **role, const char **level)
Jan F. Chadima a3ba41
+{
Jan F. Chadima a3ba41
+	*role = NULL;
Jan F. Chadima a3ba41
+	*level = NULL;
Jan F. Chadima a3ba41
+	if (the_authctxt) {
Jan F. Chadima a3ba41
+		if (the_authctxt->role != NULL) {
Jan F. Chadima a3ba41
+			char *slash;
Jan F. Chadima a3ba41
+			*role = xstrdup(the_authctxt->role);
Jan F. Chadima a3ba41
+			if ((slash = strchr(*role, '/')) != NULL) {
Jan F. Chadima a3ba41
+				*slash = '\0';
Jan F. Chadima a3ba41
+				*level = slash + 1;
Jan F. Chadima a3ba41
+			}
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
 /* Return the default security context for the given username */
Jan F. Chadima a3ba41
-static security_context_t
Jan F. Chadima a3ba41
-ssh_selinux_getctxbyname(char *pwname)
Jan F. Chadima a3ba41
+static int
Jan F. Chadima a3ba41
+ssh_selinux_getctxbyname(char *pwname,
Jan F. Chadima a3ba41
+	security_context_t *default_sc, security_context_t *user_sc)
Jan F. Chadima a3ba41
 {
Jan F. Chadima a3ba41
-	security_context_t sc;
Jan F. Chadima a3ba41
-	char *sename = NULL, *lvl = NULL;
Jan F. Chadima a3ba41
-	int r;
Jan F. Chadima a3ba41
+	char *sename, *lvl;
Jan F. Chadima a3ba41
+	const char *reqlvl;
Jan F. Chadima a3ba41
+	char *role;
Jan F. Chadima a3ba41
+	int r = -1;
Jan F. Chadima a3ba41
+	context_t con = NULL;
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	*default_sc = NULL;
Jan F. Chadima a3ba41
+	*user_sc = NULL;
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	ssh_selinux_get_role_level(&role, &reqlvl);
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 #ifdef HAVE_GETSEUSERBYNAME
Jan F. Chadima a3ba41
-	if (getseuserbyname(pwname, &sename, &lvl) != 0)
Jan F. Chadima a3ba41
-		return NULL;
Jan F. Chadima a3ba41
+	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
Jan F. Chadima a3ba41
+		sename = NULL;
Jan F. Chadima a3ba41
+		lvl = NULL;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
 #else
Jan F. Chadima a3ba41
 	sename = pwname;
Jan F. Chadima a3ba41
-	lvl = NULL;
Jan F. Chadima a3ba41
+	lvl = "";
Jan F. Chadima a3ba41
 #endif
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
+	if (r == 0) {
Jan F. Chadima a3ba41
 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Jan F. Chadima a3ba41
-	r = get_default_context_with_level(sename, lvl, NULL, &sc);
Jan F. Chadima a3ba41
+		r = get_default_context_with_level(sename, lvl, NULL, default_sc);
Jan F. Chadima a3ba41
 #else
Jan F. Chadima a3ba41
-	r = get_default_context(sename, NULL, &sc);
Jan F. Chadima a3ba41
+		r = get_default_context(sename, NULL, default_sc);
Jan F. Chadima a3ba41
 #endif
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
-	if (r != 0) {
Jan F. Chadima a3ba41
-		switch (security_getenforce()) {
Jan F. Chadima a3ba41
-		case -1:
Jan F. Chadima a3ba41
-			fatal("%s: ssh_selinux_getctxbyname: "
Jan F. Chadima a3ba41
-			    "security_getenforce() failed", __func__);
Jan F. Chadima a3ba41
-		case 0:
Jan F. Chadima a3ba41
-			error("%s: Failed to get default SELinux security "
Jan F. Chadima a3ba41
-			    "context for %s", __func__, pwname);
Jan F. Chadima a3ba41
-			break;
Jan F. Chadima a3ba41
-		default:
Jan F. Chadima a3ba41
-			fatal("%s: Failed to get default SELinux security "
Jan F. Chadima a3ba41
-			    "context for %s (in enforcing mode)",
Jan F. Chadima a3ba41
-			    __func__, pwname);
Jan F. Chadima a3ba41
+	if (r == 0) {
Jan F. Chadima a3ba41
+		/* If launched from xinetd, we must use current level */
Jan F. Chadima a3ba41
+		if (inetd_flag && !rexeced_flag) {
Jan F. Chadima a3ba41
+			security_context_t sshdsc=NULL;
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+			if (getcon_raw(&sshdsc) < 0)
Jan F. Chadima a3ba41
+				fatal("failed to allocate security context");
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+			if ((con=context_new(sshdsc)) == NULL)
Jan F. Chadima a3ba41
+				fatal("failed to allocate selinux context");
Jan F. Chadima a3ba41
+			reqlvl = context_range_get(con);
Jan F. Chadima a3ba41
+			freecon(sshdsc);
Jan F. Chadima a3ba41
+			if (reqlvl !=NULL && lvl != NULL && strcmp(reqlvl, lvl) == 0)
Jan F. Chadima a3ba41
+			    /* we actually don't change level */
Jan F. Chadima a3ba41
+			    reqlvl = "";
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+			debug("%s: current connection level '%s'", __func__, reqlvl);
Jan F. Chadima a3ba41
 		}
Jan F. Chadima a3ba41
+		
Jan F. Chadima a3ba41
+		if ((reqlvl != NULL && reqlvl[0]) || (role != NULL && role[0])) {
Jan F. Chadima a3ba41
+			r = get_user_context(sename, role, reqlvl, user_sc);
Jan F. Chadima a3ba41
+		
Jan F. Chadima a3ba41
+			if (r == 0 && reqlvl != NULL && reqlvl[0]) {
Jan F. Chadima a3ba41
+				security_context_t default_level_sc = *default_sc;
Jan F. Chadima a3ba41
+				if (role != NULL && role[0]) {
Jan F. Chadima a3ba41
+					if (get_user_context(sename, role, lvl, &default_level_sc) < 0)
Jan F. Chadima a3ba41
+						default_level_sc = *default_sc;
Jan F. Chadima a3ba41
+				}
Jan F. Chadima a3ba41
+				/* verify that the requested range is contained in the user range */
Jan F. Chadima a3ba41
+				if (mls_range_allowed(default_level_sc, *user_sc)) {
Jan F. Chadima a3ba41
+					logit("permit MLS level %s (user range %s)", reqlvl, lvl);
Jan F. Chadima a3ba41
+				} else {
Jan F. Chadima a3ba41
+					r = -1;
Jan F. Chadima a3ba41
+					error("deny MLS level %s (user range %s)", reqlvl, lvl);
Jan F. Chadima a3ba41
+				}
Jan F. Chadima a3ba41
+				if (default_level_sc != *default_sc)
Jan F. Chadima a3ba41
+					freecon(default_level_sc);
Jan F. Chadima a3ba41
+			}
Jan F. Chadima a3ba41
+		} else {
Jan F. Chadima a3ba41
+			*user_sc = *default_sc;
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+	if (r != 0) {
Jan F. Chadima a3ba41
+		error("%s: Failed to get default SELinux security "
Jan F. Chadima a3ba41
+		    "context for %s", __func__, pwname);
Jan F. Chadima a3ba41
 	}
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 #ifdef HAVE_GETSEUSERBYNAME
Jan F. Chadima a3ba41
@@ -93,37 +314,114 @@ ssh_selinux_getctxbyname(char *pwname)
Jan F. Chadima a3ba41
 	if (lvl != NULL)
Jan F. Chadima a3ba41
 		xfree(lvl);
Jan F. Chadima a3ba41
 #endif
Jan F. Chadima a3ba41
+	if (role != NULL)
Jan F. Chadima a3ba41
+		xfree(role);
Jan F. Chadima a3ba41
+	if (con)
Jan F. Chadima a3ba41
+		context_free(con);
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
-	return (sc);
Jan F. Chadima a3ba41
+	return (r);
Jan F. Chadima a3ba41
+}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+/* Setup environment variables for pam_selinux */
Jan F. Chadima a3ba41
+static int
Jan F. Chadima a3ba41
+ssh_selinux_setup_pam_variables(void)
Jan F. Chadima a3ba41
+{
Jan F. Chadima a3ba41
+	const char *reqlvl;
Jan F. Chadima a3ba41
+	char *role;
Jan F. Chadima a3ba41
+	char *use_current;
Jan F. Chadima a3ba41
+	int rv;
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	debug3("%s: setting execution context", __func__);
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	ssh_selinux_get_role_level(&role, &reqlvl);
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
Jan F. Chadima a3ba41
+	
Jan F. Chadima a3ba41
+	if (inetd_flag && !rexeced_flag) {
Jan F. Chadima a3ba41
+		use_current = "1";
Jan F. Chadima a3ba41
+	} else {
Jan F. Chadima a3ba41
+		use_current = "";
Jan F. Chadima a3ba41
+		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
+	if (role != NULL)
Jan F. Chadima a3ba41
+		xfree(role);
Jan F. Chadima a3ba41
+	
Jan F. Chadima a3ba41
+	return rv;
Jan F. Chadima a3ba41
 }
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 /* Set the execution context to the default for the specified user */
Jan F. Chadima a3ba41
 void
Jan F. Chadima a3ba41
 ssh_selinux_setup_exec_context(char *pwname)
Jan F. Chadima a3ba41
 {
Jan F. Chadima a3ba41
+	int r = 0;
Jan F. Chadima a3ba41
+	security_context_t default_ctx = NULL;
Jan F. Chadima a3ba41
 	security_context_t user_ctx = NULL;
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 	if (!ssh_selinux_enabled())
Jan F. Chadima a3ba41
 		return;
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
+	if (options.use_pam) {
Jan F. Chadima a3ba41
+		/* do not compute context, just setup environment for pam_selinux */
Jan F. Chadima a3ba41
+		if (ssh_selinux_setup_pam_variables()) {
Jan F. Chadima a3ba41
+			switch (security_getenforce()) {
Jan F. Chadima a3ba41
+			case -1:
Jan F. Chadima a3ba41
+				fatal("%s: security_getenforce() failed", __func__);
Jan F. Chadima a3ba41
+			case 0:
Jan F. Chadima a3ba41
+				error("%s: SELinux PAM variable setup failure. Continuing in permissive mode.",
Jan F. Chadima a3ba41
+				    __func__);
Jan F. Chadima a3ba41
+			break;
Jan F. Chadima a3ba41
+			default:
Jan F. Chadima a3ba41
+				fatal("%s: SELinux PAM variable setup failure. Aborting connection.",
Jan F. Chadima a3ba41
+				    __func__);
Jan F. Chadima a3ba41
+			}
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+		return;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+
Jan F. Chadima a3ba41
 	debug3("%s: setting execution context", __func__);
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
-	user_ctx = ssh_selinux_getctxbyname(pwname);
Jan F. Chadima a3ba41
-	if (setexeccon(user_ctx) != 0) {
Jan F. Chadima a3ba41
+	r = ssh_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);
Jan F. Chadima a3ba41
+	if (r >= 0) {
Jan F. Chadima a3ba41
+		r = setexeccon(user_ctx);
Jan F. Chadima a3ba41
+		if (r < 0) {
Jan F. Chadima a3ba41
+			error("%s: Failed to set SELinux execution context %s for %s",
Jan F. Chadima a3ba41
+			    __func__, user_ctx, pwname);
Jan F. Chadima a3ba41
+		} 
Jan F. Chadima a3ba41
+#ifdef HAVE_SETKEYCREATECON
Jan F. Chadima a3ba41
+		else if (setkeycreatecon(user_ctx) < 0) {
Jan F. Chadima a3ba41
+			error("%s: Failed to set SELinux keyring creation context %s for %s",
Jan F. Chadima a3ba41
+			    __func__, user_ctx, pwname);
Jan F. Chadima a3ba41
+		}
Jan F. Chadima a3ba41
+#endif
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+	if (user_ctx == NULL) {
Jan F. Chadima a3ba41
+		user_ctx = default_ctx;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+	if (r < 0 || user_ctx != default_ctx) {
Jan F. Chadima a3ba41
+		/* audit just the case when user changed a role or there was
Jan F. Chadima a3ba41
+		   a failure */
Jan F. Chadima a3ba41
+		send_audit_message(r >= 0, default_ctx, user_ctx);
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
+	if (r < 0) {
Jan F. Chadima a3ba41
 		switch (security_getenforce()) {
Jan F. Chadima a3ba41
 		case -1:
Jan F. Chadima a3ba41
 			fatal("%s: security_getenforce() failed", __func__);
Jan F. Chadima a3ba41
 		case 0:
Jan F. Chadima a3ba41
-			error("%s: Failed to set SELinux execution "
Jan F. Chadima a3ba41
-			    "context for %s", __func__, pwname);
Jan F. Chadima a3ba41
+			error("%s: SELinux failure. Continuing in permissive mode.",
Jan F. Chadima a3ba41
+			    __func__);
Jan F. Chadima a3ba41
 			break;
Jan F. Chadima a3ba41
 		default:
Jan F. Chadima a3ba41
-			fatal("%s: Failed to set SELinux execution context "
Jan F. Chadima a3ba41
-			    "for %s (in enforcing mode)", __func__, pwname);
Jan F. Chadima a3ba41
+			fatal("%s: SELinux failure. Aborting connection.",
Jan F. Chadima a3ba41
+			    __func__);
Jan F. Chadima a3ba41
 		}
Jan F. Chadima a3ba41
 	}
Jan F. Chadima a3ba41
-	if (user_ctx != NULL)
Jan F. Chadima a3ba41
+	if (user_ctx != NULL && user_ctx != default_ctx)
Jan F. Chadima a3ba41
 		freecon(user_ctx);
Jan F. Chadima a3ba41
+	if (default_ctx != NULL)
Jan F. Chadima a3ba41
+		freecon(default_ctx);
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 	debug3("%s: done", __func__);
Jan F. Chadima a3ba41
 }
Jan F. Chadima a3ba41
@@ -141,7 +439,10 @@ ssh_selinux_setup_pty(char *pwname, cons
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 	debug3("%s: setting TTY context on %s", __func__, tty);
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
-	user_ctx = ssh_selinux_getctxbyname(pwname);
Jan F. Chadima a3ba41
+	if (getexeccon(&user_ctx) < 0) {
Jan F. Chadima a3ba41
+		error("%s: getexeccon: %s", __func__, strerror(errno));
Jan F. Chadima a3ba41
+		goto out;
Jan F. Chadima a3ba41
+	}
Jan F. Chadima a3ba41
 
Jan F. Chadima a3ba41
 	/* XXX: should these calls fatal() upon failure in enforcing mode? */
Jan F. Chadima a3ba41