rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
943807
diff -up openssh/misc.c.keycat openssh/misc.c
943807
--- openssh/misc.c.keycat	2015-06-24 10:57:50.158849606 +0200
943807
+++ openssh/misc.c	2015-06-24 11:04:23.989868638 +0200
943807
@@ -966,6 +966,13 @@ subprocess(const char *tag, struct passw
943807
 			error("%s: dup2: %s", tag, strerror(errno));
943807
 			_exit(1);
943807
 		}
943807
+#ifdef WITH_SELINUX
943807
+		if (sshd_selinux_setup_env_variables() < 0) {
943807
+			error ("failed to copy environment:  %s",
943807
+			    strerror(errno));
943807
+			_exit(127);
943807
+		}
943807
+#endif
943807
 		if (env != NULL)
943807
 			execve(av[0], av, env);
943807
 		else
943807
diff -up openssh/HOWTO.ssh-keycat.keycat openssh/HOWTO.ssh-keycat
943807
--- openssh/HOWTO.ssh-keycat.keycat	2015-06-24 10:57:50.157849608 +0200
943807
+++ openssh/HOWTO.ssh-keycat	2015-06-24 10:57:50.157849608 +0200
943807
@@ -0,0 +1,12 @@
943807
+The ssh-keycat retrieves the content of the ~/.ssh/authorized_keys
943807
+of an user in any environment. This includes environments with
943807
+polyinstantiation of home directories and SELinux MLS policy enabled.
943807
+
943807
+To use ssh-keycat, set these options in /etc/ssh/sshd_config file:
943807
+        AuthorizedKeysCommand /usr/libexec/openssh/ssh-keycat
943807
+        AuthorizedKeysCommandUser root
943807
+
943807
+Do not forget to enable public key authentication:
943807
+        PubkeyAuthentication yes
943807
+
943807
+
943807
diff -up openssh/Makefile.in.keycat openssh/Makefile.in
943807
--- openssh/Makefile.in.keycat	2015-06-24 10:57:50.152849621 +0200
943807
+++ openssh/Makefile.in	2015-06-24 10:57:50.157849608 +0200
943807
@@ -27,6 +27,7 @@ SFTP_SERVER=$(libexecdir)/sftp-server
943807
 ASKPASS_PROGRAM=$(libexecdir)/ssh-askpass
943807
 SFTP_SERVER=$(libexecdir)/sftp-server
943807
 SSH_KEYSIGN=$(libexecdir)/ssh-keysign
943807
+SSH_KEYCAT=$(libexecdir)/ssh-keycat
943807
 SSH_PKCS11_HELPER=$(libexecdir)/ssh-pkcs11-helper
943807
 SSH_SK_HELPER=$(libexecdir)/ssh-sk-helper
943807
 PRIVSEP_PATH=@PRIVSEP_PATH@
943807
@@ -52,6 +52,7 @@ K5LIBS=@K5LIBS@
943807
 K5LIBS=@K5LIBS@
943807
 GSSLIBS=@GSSLIBS@
943807
 SSHDLIBS=@SSHDLIBS@
943807
+KEYCATLIBS=@KEYCATLIBS@
943807
 LIBEDIT=@LIBEDIT@
943807
 LIBFIDO2=@LIBFIDO2@
943807
 AR=@AR@
943807
@@ -65,7 +66,7 @@ EXEEXT=@EXEEXT@
943807
 
943807
 .SUFFIXES: .lo
943807
 
943807
-TARGETS=ssh$(EXEEXT) sshd$(EXEEXT) ssh-add$(EXEEXT) ssh-keygen$(EXEEXT) ssh-keyscan${EXEEXT} ssh-keysign${EXEEXT} ssh-pkcs11-helper$(EXEEXT) ssh-agent$(EXEEXT) scp$(EXEEXT) sftp-server$(EXEEXT) sftp$(EXEEXT) ssh-sk-helper$(EXEEXT)
943807
+TARGETS=ssh$(EXEEXT) sshd$(EXEEXT) ssh-add$(EXEEXT) ssh-keygen$(EXEEXT) ssh-keyscan${EXEEXT} ssh-keysign${EXEEXT} ssh-pkcs11-helper$(EXEEXT) ssh-agent$(EXEEXT) scp$(EXEEXT) sftp-server$(EXEEXT) sftp$(EXEEXT) ssh-sk-helper$(EXEEXT) ssh-keycat$(EXEEXT)
943807
 
943807
 XMSS_OBJS=\
943807
 	ssh-xmss.o \
943807
@@ -190,6 +191,9 @@ ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT)
943807
 ssh-sk-helper$(EXEEXT): $(LIBCOMPAT) libssh.a $(SKHELPER_OBJS)
943807
 	$(LD) -o $@ $(SKHELPER_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS) $(LIBFIDO2)
943807
 
943807
+ssh-keycat$(EXEEXT): $(LIBCOMPAT) $(SSHDOBJS) libssh.a ssh-keycat.o uidswap.o
943807
+	$(LD) -o $@ ssh-keycat.o uidswap.o $(LDFLAGS) -lssh -lopenbsd-compat $(KEYCATLIBS) $(LIBS)
943807
+
943807
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHKEYSCAN_OBJS)
943807
 	$(LD) -o $@ $(SSHKEYSCAN_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
943807
 
943807
@@ -321,6 +325,7 @@ install-files:
943807
 	$(INSTALL) -m 4711 $(STRIP_OPT) ssh-keysign$(EXEEXT) $(DESTDIR)$(SSH_KEYSIGN)$(EXEEXT)
943807
 	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-pkcs11-helper$(EXEEXT) $(DESTDIR)$(SSH_PKCS11_HELPER)$(EXEEXT)
943807
 	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-sk-helper$(EXEEXT) $(DESTDIR)$(SSH_SK_HELPER)$(EXEEXT)
943807
+	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-keycat$(EXEEXT) $(DESTDIR)$(libexecdir)/ssh-keycat$(EXEEXT)
943807
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp$(EXEEXT) $(DESTDIR)$(bindir)/sftp$(EXEEXT)
943807
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp-server$(EXEEXT) $(DESTDIR)$(SFTP_SERVER)$(EXEEXT)
943807
 	$(INSTALL) -m 644 ssh.1.out $(DESTDIR)$(mandir)/$(mansubdir)1/ssh.1
943807
diff -up openssh/openbsd-compat/port-linux.h.keycat openssh/openbsd-compat/port-linux.h
943807
--- openssh/openbsd-compat/port-linux.h.keycat	2015-06-24 10:57:50.150849626 +0200
943807
+++ openssh/openbsd-compat/port-linux.h	2015-06-24 10:57:50.160849601 +0200
943807
@@ -25,8 +25,10 @@ void ssh_selinux_setup_pty(char *, const
943807
 void ssh_selinux_change_context(const char *);
943807
 void ssh_selinux_setfscreatecon(const char *);
943807
 
943807
+int sshd_selinux_enabled(void);
943807
 void sshd_selinux_copy_context(void);
943807
 void sshd_selinux_setup_exec_context(char *);
943807
+int sshd_selinux_setup_env_variables(void);
943807
 #endif
943807
 
943807
 #ifdef LINUX_OOM_ADJUST
943807
diff -up openssh/openbsd-compat/port-linux-sshd.c.keycat openssh/openbsd-compat/port-linux-sshd.c
943807
--- openssh/openbsd-compat/port-linux-sshd.c.keycat	2015-06-24 10:57:50.150849626 +0200
943807
+++ openssh/openbsd-compat/port-linux-sshd.c	2015-06-24 10:57:50.159849603 +0200
943807
@@ -54,6 +54,20 @@ extern Authctxt *the_authctxt;
943807
 extern int inetd_flag;
943807
 extern int rexeced_flag;
943807
 
943807
+/* Wrapper around is_selinux_enabled() to log its return value once only */
943807
+int
943807
+sshd_selinux_enabled(void)
943807
+{
943807
+	static int enabled = -1;
943807
+
943807
+	if (enabled == -1) {
943807
+		enabled = (is_selinux_enabled() == 1);
943807
+		debug("SELinux support %s", enabled ? "enabled" : "disabled");
943807
+	}
943807
+
943807
+	return (enabled);
943807
+}
943807
+
943807
 /* Send audit message */
943807
 static int
943807
 sshd_selinux_send_audit_message(int success, security_context_t default_context,
943807
@@ -308,7 +322,7 @@ sshd_selinux_getctxbyname(char *pwname,
943807
 
943807
 /* Setup environment variables for pam_selinux */
943807
 static int
943807
-sshd_selinux_setup_pam_variables(void)
943807
+sshd_selinux_setup_variables(int(*set_it)(char *, const char *))
943807
 {
943807
 	const char *reqlvl;
943807
 	char *role;
943807
@@ -319,16 +333,16 @@ sshd_selinux_setup_pam_variables(void)
943807
 
943807
 	ssh_selinux_get_role_level(&role, &reqlvl);
943807
 
943807
-	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
943807
+	rv = set_it("SELINUX_ROLE_REQUESTED", role ? role : "");
943807
 
943807
 	if (inetd_flag && !rexeced_flag) {
943807
 		use_current = "1";
943807
 	} else {
943807
 		use_current = "";
943807
-		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
943807
+		rv = rv || set_it("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
943807
 	}
943807
 
943807
-	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
943807
+	rv = rv || set_it("SELINUX_USE_CURRENT_RANGE", use_current);
943807
 
943807
 	if (role != NULL)
943807
 		free(role);
943807
@@ -336,6 +350,24 @@ sshd_selinux_setup_pam_variables(void)
943807
 	return rv;
943807
 }
943807
 
943807
+static int
943807
+sshd_selinux_setup_pam_variables(void)
943807
+{
943807
+	return sshd_selinux_setup_variables(do_pam_putenv);
943807
+}
943807
+
943807
+static int
943807
+do_setenv(char *name, const char *value)
943807
+{
943807
+	return setenv(name, value, 1);
943807
+}
943807
+
943807
+int
943807
+sshd_selinux_setup_env_variables(void)
943807
+{
943807
+	return sshd_selinux_setup_variables(do_setenv);
943807
+}
943807
+
943807
 /* Set the execution context to the default for the specified user */
943807
 void
943807
 sshd_selinux_setup_exec_context(char *pwname)
943807
@@ -344,7 +376,7 @@ sshd_selinux_setup_exec_context(char *pw
943807
 	int r = 0;
943807
 	security_context_t default_ctx = NULL;
943807
 
943807
-	if (!ssh_selinux_enabled())
943807
+	if (!sshd_selinux_enabled())
943807
 		return;
943807
 
943807
 	if (options.use_pam) {
943807
@@ -415,7 +447,7 @@ sshd_selinux_copy_context(void)
943807
 {
943807
 	security_context_t *ctx;
943807
 
943807
-	if (!ssh_selinux_enabled())
943807
+	if (!sshd_selinux_enabled())
943807
 		return;
943807
 
943807
 	if (getexeccon((security_context_t *)&ctx) != 0) {
943807
diff -up openssh/platform.c.keycat openssh/platform.c
943807
--- openssh/platform.c.keycat	2015-06-24 10:57:50.147849633 +0200
943807
+++ openssh/platform.c	2015-06-24 10:57:50.160849601 +0200
943807
@@ -103,7 +103,7 @@ platform_setusercontext(struct passwd *p
943807
 {
943807
 #ifdef WITH_SELINUX
943807
 	/* Cache selinux status for later use */
943807
-	(void)ssh_selinux_enabled();
943807
+	(void)sshd_selinux_enabled();
943807
 #endif
943807
 
943807
 #ifdef USE_SOLARIS_PROJECTS
943807
diff -up openssh/ssh-keycat.c.keycat openssh/ssh-keycat.c
943807
--- openssh/ssh-keycat.c.keycat	2015-06-24 10:57:50.161849599 +0200
943807
+++ openssh/ssh-keycat.c	2015-06-24 10:57:50.161849599 +0200
943807
@@ -0,0 +1,241 @@
943807
+/*
943807
+ * Redistribution and use in source and binary forms, with or without
943807
+ * modification, are permitted provided that the following conditions
943807
+ * are met:
943807
+ * 1. Redistributions of source code must retain the above copyright
943807
+ *    notice, and the entire permission notice in its entirety,
943807
+ *    including the disclaimer of warranties.
943807
+ * 2. Redistributions in binary form must reproduce the above copyright
943807
+ *    notice, this list of conditions and the following disclaimer in the
943807
+ *    documentation and/or other materials provided with the distribution.
943807
+ * 3. The name of the author may not be used to endorse or promote
943807
+ *    products derived from this software without specific prior
943807
+ *    written permission.
943807
+ *
943807
+ * ALTERNATIVELY, this product may be distributed under the terms of
943807
+ * the GNU Public License, in which case the provisions of the GPL are
943807
+ * required INSTEAD OF the above restrictions.  (This clause is
943807
+ * necessary due to a potential bad interaction between the GPL and
943807
+ * the restrictions contained in a BSD-style copyright.)
943807
+ *
943807
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
943807
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
943807
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
943807
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
943807
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
943807
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
943807
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
943807
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
943807
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
943807
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
943807
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
943807
+ */
943807
+
943807
+/*
943807
+ * Copyright (c) 2011 Red Hat, Inc.
943807
+ * Written by Tomas Mraz <tmraz@redhat.com>
943807
+*/
943807
+
943807
+#define _GNU_SOURCE
943807
+
943807
+#include "config.h"
943807
+#include <stdio.h>
943807
+#include <stdlib.h>
943807
+#include <string.h>
943807
+#include <sys/types.h>
943807
+#include <sys/stat.h>
943807
+#include <pwd.h>
943807
+#include <fcntl.h>
943807
+#include <unistd.h>
943807
+#ifdef HAVE_STDINT_H
943807
+#include <stdint.h>
943807
+#endif
943807
+
943807
+#include <security/pam_appl.h>
943807
+
943807
+#include "uidswap.h"
943807
+#include "misc.h"
943807
+
943807
+#define ERR_USAGE 1
943807
+#define ERR_PAM_START 2
943807
+#define ERR_OPEN_SESSION 3
943807
+#define ERR_CLOSE_SESSION 4
943807
+#define ERR_PAM_END 5
943807
+#define ERR_GETPWNAM 6
943807
+#define ERR_MEMORY 7
943807
+#define ERR_OPEN 8
943807
+#define ERR_FILE_MODE 9
943807
+#define ERR_FDOPEN 10
943807
+#define ERR_STAT 11
943807
+#define ERR_WRITE 12
943807
+#define ERR_PAM_PUTENV 13
943807
+#define BUFLEN 4096
943807
+
943807
+/* Just ignore the messages in the conversation function */
943807
+static int
943807
+dummy_conv(int num_msg, const struct pam_message **msgm,
943807
+	   struct pam_response **response, void *appdata_ptr)
943807
+{
943807
+	struct pam_response *rsp;
943807
+
943807
+	(void)msgm;
943807
+	(void)appdata_ptr;
943807
+
943807
+	if (num_msg <= 0)
943807
+		return PAM_CONV_ERR;
943807
+
943807
+	/* Just allocate the array as empty responses */
943807
+	rsp = calloc (num_msg, sizeof (struct pam_response));
943807
+	if (rsp == NULL)
943807
+		return PAM_CONV_ERR;
943807
+
943807
+	*response = rsp;
943807
+	return PAM_SUCCESS;
943807
+}
943807
+
943807
+static struct pam_conv conv = {
943807
+	dummy_conv,
943807
+	NULL
943807
+};
943807
+
943807
+char *
943807
+make_auth_keys_name(const struct passwd *pwd)
943807
+{
943807
+	char *fname;
943807
+
943807
+	if (asprintf(&fname, "%s/.ssh/authorized_keys", pwd->pw_dir) < 0)
943807
+		return NULL;
943807
+
943807
+	return fname;
943807
+}
943807
+
943807
+int
943807
+dump_keys(const char *user)
943807
+{
943807
+	struct passwd *pwd;
943807
+	int fd = -1;
943807
+	FILE *f = NULL;
943807
+	char *fname = NULL;
943807
+	int rv = 0;
943807
+	char buf[BUFLEN];
943807
+	size_t len;
943807
+	struct stat st;
943807
+
943807
+	if ((pwd = getpwnam(user)) == NULL) {
943807
+		return ERR_GETPWNAM;
943807
+	}
943807
+
943807
+	if ((fname = make_auth_keys_name(pwd)) == NULL) {
943807
+		return ERR_MEMORY;
943807
+	}
943807
+
943807
+	temporarily_use_uid(pwd);
943807
+
943807
+	if ((fd = open(fname, O_RDONLY|O_NONBLOCK|O_NOFOLLOW, 0)) < 0) {
943807
+		rv = ERR_OPEN;
943807
+		goto fail;
943807
+	}
943807
+
943807
+	if (fstat(fd, &st) < 0) {
943807
+		rv = ERR_STAT;
943807
+		goto fail;
943807
+	}
943807
+
943807
+	if (!S_ISREG(st.st_mode) || 
943807
+		(st.st_uid != pwd->pw_uid && st.st_uid != 0)) {
943807
+		rv = ERR_FILE_MODE;
943807
+		goto fail;
943807
+	}
943807
+
943807
+	unset_nonblock(fd);
943807
+
943807
+	if ((f = fdopen(fd, "r")) == NULL) {
943807
+		rv = ERR_FDOPEN;
943807
+		goto fail;
943807
+	}
943807
+
943807
+	fd = -1;
943807
+
943807
+	while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
943807
+		rv = fwrite(buf, 1, len, stdout) != len ? ERR_WRITE : 0;
943807
+	}
943807
+
943807
+fail:
943807
+	if (fd != -1)
943807
+		close(fd);
943807
+	if (f != NULL)
943807
+		fclose(f);
943807
+	free(fname);
943807
+	restore_uid();
943807
+	return rv;
943807
+}
943807
+
943807
+static const char *env_names[] = { "SELINUX_ROLE_REQUESTED",
943807
+	"SELINUX_LEVEL_REQUESTED",
943807
+	"SELINUX_USE_CURRENT_RANGE"
943807
+};
943807
+
943807
+extern char **environ;
943807
+
943807
+int
943807
+set_pam_environment(pam_handle_t *pamh)
943807
+{
943807
+	int i;
943807
+	size_t j;
943807
+
943807
+	for (j = 0; j < sizeof(env_names)/sizeof(env_names[0]); ++j) {
943807
+		int len = strlen(env_names[j]);
943807
+
943807
+		for (i = 0; environ[i] != NULL; ++i) {
943807
+			if (strncmp(env_names[j], environ[i], len) == 0 &&
943807
+			    environ[i][len] == '=') {
943807
+				if (pam_putenv(pamh, environ[i]) != PAM_SUCCESS)
943807
+					return ERR_PAM_PUTENV;
943807
+			}
943807
+		}
943807
+	}
943807
+
943807
+	return 0;
943807
+}
943807
+
943807
+int
943807
+main(int argc, char *argv[])
943807
+{
943807
+	pam_handle_t *pamh = NULL;
943807
+	int retval;
943807
+	int ev = 0;
943807
+
943807
+	if (argc != 2) {
943807
+		fprintf(stderr, "Usage: %s <user-name>\n", argv[0]);
943807
+		return ERR_USAGE;
943807
+	}
943807
+
943807
+	retval = pam_start("ssh-keycat", argv[1], &conv, &pamh);
943807
+	if (retval != PAM_SUCCESS) {
943807
+		return ERR_PAM_START;
943807
+	}
943807
+
943807
+	ev = set_pam_environment(pamh);
943807
+	if (ev != 0)
943807
+		goto finish;
943807
+
943807
+	retval = pam_open_session(pamh, PAM_SILENT);
943807
+	if (retval != PAM_SUCCESS) {
943807
+		ev = ERR_OPEN_SESSION;
943807
+		goto finish;
943807
+	}
943807
+
943807
+	ev = dump_keys(argv[1]);
943807
+
943807
+	retval = pam_close_session(pamh, PAM_SILENT);
943807
+	if (retval != PAM_SUCCESS) {
943807
+		ev = ERR_CLOSE_SESSION;
943807
+	}
943807
+
943807
+finish:
943807
+	retval = pam_end (pamh,retval);
943807
+	if (retval != PAM_SUCCESS) {
943807
+		ev = ERR_PAM_END;
943807
+	}
943807
+	return ev;
943807
+}
943807
diff --git a/configure.ac b/configure.ac
943807
index 3bbccfd..6481f1f 100644
943807
--- a/configure.ac
943807
+++ b/configure.ac
943807
@@ -2952,6 +2952,7 @@ AC_ARG_WITH([pam],
943807
 			PAM_MSG="yes"
943807
 
943807
 			SSHDLIBS="$SSHDLIBS -lpam"
943807
+			KEYCATLIBS="$KEYCATLIBS -lpam"
943807
 			AC_DEFINE([USE_PAM], [1],
943807
 				[Define if you want to enable PAM support])
943807
 
943807
@@ -3105,6 +3106,7 @@
943807
 					;;
943807
 				*)
943807
 					SSHDLIBS="$SSHDLIBS -ldl"
943807
+					KEYCATLIBS="$KEYCATLIBS -ldl"
943807
 					;;
943807
 				esac
943807
 			fi
943807
@@ -4042,6 +4044,7 @@ AC_ARG_WITH([selinux],
943807
 	fi ]
943807
 )
943807
 AC_SUBST([SSHDLIBS])
943807
+AC_SUBST([KEYCATLIBS])
943807
 
943807
 # Check whether user wants Kerberos 5 support
943807
 KRB5_MSG="no"
943807
@@ -5031,6 +5034,9 @@ fi
943807
 if test ! -z "${SSHDLIBS}"; then
943807
 echo "         +for sshd: ${SSHDLIBS}"
943807
 fi
943807
+if test ! -z "${KEYCATLIBS}"; then
943807
+echo "   +for ssh-keycat: ${KEYCATLIBS}"
943807
+fi
943807
 
943807
 echo ""
943807