f09e2e
diff -up openssh-6.3p1/HOWTO.ssh-keycat.keycat openssh-6.3p1/HOWTO.ssh-keycat
f09e2e
--- openssh-6.3p1/HOWTO.ssh-keycat.keycat	2013-10-10 15:16:33.445566916 +0200
f09e2e
+++ openssh-6.3p1/HOWTO.ssh-keycat	2013-10-10 15:16:33.445566916 +0200
f09e2e
@@ -0,0 +1,12 @@
f09e2e
+The ssh-keycat retrieves the content of the ~/.ssh/authorized_keys
f09e2e
+of an user in any environment. This includes environments with
f09e2e
+polyinstantiation of home directories and SELinux MLS policy enabled.
f09e2e
+
f09e2e
+To use ssh-keycat, set these options in /etc/ssh/sshd_config file:
f09e2e
+        AuthorizedKeysCommand /usr/libexec/openssh/ssh-keycat
f09e2e
+        AuthorizedKeysCommandRunAs root
f09e2e
+
f09e2e
+Do not forget to enable public key authentication:
f09e2e
+        PubkeyAuthentication yes
f09e2e
+
f09e2e
+
f09e2e
diff -up openssh-6.3p1/Makefile.in.keycat openssh-6.3p1/Makefile.in
f09e2e
--- openssh-6.3p1/Makefile.in.keycat	2013-10-10 15:16:33.442566930 +0200
f09e2e
+++ openssh-6.3p1/Makefile.in	2013-10-10 15:16:33.445566916 +0200
f09e2e
@@ -27,6 +27,7 @@ SFTP_SERVER=$(libexecdir)/sftp-server
f09e2e
 SSH_KEYSIGN=$(libexecdir)/ssh-keysign
f09e2e
 SSH_LDAP_HELPER=$(libexecdir)/ssh-ldap-helper
f09e2e
 SSH_LDAP_WRAPPER=$(libexecdir)/ssh-ldap-wrapper
f09e2e
+SSH_KEYCAT=$(libexecdir)/ssh-keycat
f09e2e
 SSH_PKCS11_HELPER=$(libexecdir)/ssh-pkcs11-helper
f09e2e
 PRIVSEP_PATH=@PRIVSEP_PATH@
f09e2e
 SSH_PRIVSEP_USER=@SSH_PRIVSEP_USER@
f09e2e
@@ -64,7 +65,7 @@ EXEEXT=@EXEEXT@
f09e2e
 MANFMT=@MANFMT@
f09e2e
 INSTALL_SSH_LDAP_HELPER=@INSTALL_SSH_LDAP_HELPER@
f09e2e
 
f09e2e
-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-ldap-helper$(EXEEXT)
f09e2e
+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-ldap-helper$(EXEEXT) ssh-keycat$(EXEEXT)
f09e2e
 
f09e2e
 LIBSSH_OBJS=authfd.o authfile.o bufaux.o bufbn.o buffer.o \
f09e2e
 	canohost.o channels.o cipher.o cipher-aes.o \
f09e2e
@@ -172,6 +173,9 @@ ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT)
f09e2e
 ssh-ldap-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o
f09e2e
 	$(LD) -o $@ ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
f09e2e
 
f09e2e
+ssh-keycat$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keycat.o
f09e2e
+	$(LD) -o $@ ssh-keycat.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(SSHDLIBS)
f09e2e
+
f09e2e
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o roaming_dummy.o
f09e2e
 	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
f09e2e
 
f09e2e
@@ -279,6 +283,7 @@ install-files:
f09e2e
 		$(INSTALL) -m 0700 $(STRIP_OPT) ssh-ldap-helper $(DESTDIR)$(SSH_LDAP_HELPER) ; \
f09e2e
 		$(INSTALL) -m 0700 ssh-ldap-wrapper $(DESTDIR)$(SSH_LDAP_WRAPPER) ; \
f09e2e
 	fi
f09e2e
+	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-keycat$(EXEEXT) $(DESTDIR)$(libexecdir)/ssh-keycat$(EXEEXT)
f09e2e
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp$(EXEEXT) $(DESTDIR)$(bindir)/sftp$(EXEEXT)
f09e2e
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp-server$(EXEEXT) $(DESTDIR)$(SFTP_SERVER)$(EXEEXT)
f09e2e
 	$(INSTALL) -m 644 ssh.1.out $(DESTDIR)$(mandir)/$(mansubdir)1/ssh.1
f09e2e
diff -up openssh-6.3p1/auth2-pubkey.c.keycat openssh-6.3p1/auth2-pubkey.c
f09e2e
--- openssh-6.3p1/auth2-pubkey.c.keycat	2013-10-10 15:16:33.429566992 +0200
f09e2e
+++ openssh-6.3p1/auth2-pubkey.c	2013-10-10 15:16:33.445566916 +0200
f09e2e
@@ -606,6 +606,14 @@ user_key_command_allowed2(struct passwd
f09e2e
 			_exit(1);
f09e2e
 		}
f09e2e
 
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+		if (ssh_selinux_setup_env_variables() < 0) {
f09e2e
+			error ("failed to copy environment:  %s",
f09e2e
+			    strerror(errno));
f09e2e
+			_exit(127);
f09e2e
+		}
f09e2e
+#endif
f09e2e
+
f09e2e
 		execl(options.authorized_keys_command,
f09e2e
 		    options.authorized_keys_command, user_pw->pw_name, NULL);
f09e2e
 
f09e2e
diff -up openssh-6.3p1/openbsd-compat/port-linux.c.keycat openssh-6.3p1/openbsd-compat/port-linux.c
f09e2e
--- openssh-6.3p1/openbsd-compat/port-linux.c.keycat	2013-10-10 15:16:33.435566964 +0200
f09e2e
+++ openssh-6.3p1/openbsd-compat/port-linux.c	2013-10-10 15:32:19.946065189 +0200
f09e2e
@@ -313,7 +313,7 @@ ssh_selinux_getctxbyname(char *pwname,
f09e2e
 
f09e2e
 /* Setup environment variables for pam_selinux */
f09e2e
 static int
f09e2e
-ssh_selinux_setup_pam_variables(void)
f09e2e
+ssh_selinux_setup_variables(int(*set_it)(const char *, const char *))
f09e2e
 {
f09e2e
 	const char *reqlvl;
f09e2e
 	char *role;
f09e2e
@@ -324,16 +324,16 @@ ssh_selinux_setup_pam_variables(void)
f09e2e
 
f09e2e
 	ssh_selinux_get_role_level(&role, &reqlvl);
f09e2e
 
f09e2e
-	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
f09e2e
+	rv = set_it("SELINUX_ROLE_REQUESTED", role ? role : "");
f09e2e
 
f09e2e
 	if (inetd_flag && !rexeced_flag) {
f09e2e
 		use_current = "1";
f09e2e
 	} else {
f09e2e
 		use_current = "";
f09e2e
-		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
f09e2e
+		rv = rv || set_it("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
f09e2e
 	}
f09e2e
 
f09e2e
-	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
f09e2e
+	rv = rv || set_it("SELINUX_USE_CURRENT_RANGE", use_current);
f09e2e
 
f09e2e
 	if (role != NULL)
f09e2e
 		free(role);
f09e2e
@@ -341,6 +341,24 @@ ssh_selinux_setup_pam_variables(void)
f09e2e
 	return rv;
f09e2e
 }
f09e2e
 
f09e2e
+static int
f09e2e
+ssh_selinux_setup_pam_variables(void)
f09e2e
+{
f09e2e
+	return ssh_selinux_setup_variables(do_pam_putenv);
f09e2e
+}
f09e2e
+
f09e2e
+static int
f09e2e
+do_setenv(char *name, char *value)
f09e2e
+{
f09e2e
+	return setenv(name, value, 1);
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+ssh_selinux_setup_env_variables(void)
f09e2e
+{
f09e2e
+	return ssh_selinux_setup_variables(do_setenv);
f09e2e
+}
f09e2e
+
f09e2e
 /* Set the execution context to the default for the specified user */
f09e2e
 void
f09e2e
 ssh_selinux_setup_exec_context(char *pwname)
f09e2e
diff -up openssh-6.3p1/ssh-keycat.c.keycat openssh-6.3p1/ssh-keycat.c
f09e2e
--- openssh-6.3p1/ssh-keycat.c.keycat	2013-10-10 15:16:33.446566911 +0200
f09e2e
+++ openssh-6.3p1/ssh-keycat.c	2013-10-10 15:16:33.446566911 +0200
f09e2e
@@ -0,0 +1,238 @@
f09e2e
+/*
f09e2e
+ * Redistribution and use in source and binary forms, with or without
f09e2e
+ * modification, are permitted provided that the following conditions
f09e2e
+ * are met:
f09e2e
+ * 1. Redistributions of source code must retain the above copyright
f09e2e
+ *    notice, and the entire permission notice in its entirety,
f09e2e
+ *    including the disclaimer of warranties.
f09e2e
+ * 2. Redistributions in binary form must reproduce the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer in the
f09e2e
+ *    documentation and/or other materials provided with the distribution.
f09e2e
+ * 3. The name of the author may not be used to endorse or promote
f09e2e
+ *    products derived from this software without specific prior
f09e2e
+ *    written permission.
f09e2e
+ *
f09e2e
+ * ALTERNATIVELY, this product may be distributed under the terms of
f09e2e
+ * the GNU Public License, in which case the provisions of the GPL are
f09e2e
+ * required INSTEAD OF the above restrictions.  (This clause is
f09e2e
+ * necessary due to a potential bad interaction between the GPL and
f09e2e
+ * the restrictions contained in a BSD-style copyright.)
f09e2e
+ *
f09e2e
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
f09e2e
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f09e2e
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
f09e2e
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
f09e2e
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
f09e2e
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
f09e2e
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
f09e2e
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
f09e2e
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
f09e2e
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
f09e2e
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
f09e2e
+ */
f09e2e
+
f09e2e
+/*
f09e2e
+ * Copyright (c) 2011 Red Hat, Inc.
f09e2e
+ * Written by Tomas Mraz <tmraz@redhat.com>
f09e2e
+*/
f09e2e
+
f09e2e
+#define _GNU_SOURCE
f09e2e
+
f09e2e
+#include "config.h"
f09e2e
+#include <stdio.h>
f09e2e
+#include <stdlib.h>
f09e2e
+#include <string.h>
f09e2e
+#include <sys/types.h>
f09e2e
+#include <sys/stat.h>
f09e2e
+#include <pwd.h>
f09e2e
+#include <fcntl.h>
f09e2e
+#include <unistd.h>
f09e2e
+
f09e2e
+#include <security/pam_appl.h>
f09e2e
+
f09e2e
+#include "uidswap.h"
f09e2e
+#include "misc.h"
f09e2e
+
f09e2e
+#define ERR_USAGE 1
f09e2e
+#define ERR_PAM_START 2
f09e2e
+#define ERR_OPEN_SESSION 3
f09e2e
+#define ERR_CLOSE_SESSION 4
f09e2e
+#define ERR_PAM_END 5
f09e2e
+#define ERR_GETPWNAM 6
f09e2e
+#define ERR_MEMORY 7
f09e2e
+#define ERR_OPEN 8
f09e2e
+#define ERR_FILE_MODE 9
f09e2e
+#define ERR_FDOPEN 10
f09e2e
+#define ERR_STAT 11
f09e2e
+#define ERR_WRITE 12
f09e2e
+#define ERR_PAM_PUTENV 13
f09e2e
+#define BUFLEN 4096
f09e2e
+
f09e2e
+/* Just ignore the messages in the conversation function */
f09e2e
+static int
f09e2e
+dummy_conv(int num_msg, const struct pam_message **msgm,
f09e2e
+	   struct pam_response **response, void *appdata_ptr)
f09e2e
+{
f09e2e
+	struct pam_response *rsp;
f09e2e
+
f09e2e
+	(void)msgm;
f09e2e
+	(void)appdata_ptr;
f09e2e
+
f09e2e
+	if (num_msg <= 0)
f09e2e
+		return PAM_CONV_ERR;
f09e2e
+
f09e2e
+	/* Just allocate the array as empty responses */
f09e2e
+	rsp = calloc (num_msg, sizeof (struct pam_response));
f09e2e
+	if (rsp == NULL)
f09e2e
+		return PAM_CONV_ERR;
f09e2e
+
f09e2e
+	*response = rsp;
f09e2e
+	return PAM_SUCCESS;
f09e2e
+}
f09e2e
+
f09e2e
+static struct pam_conv conv = {
f09e2e
+	dummy_conv,
f09e2e
+	NULL
f09e2e
+};
f09e2e
+
f09e2e
+char *
f09e2e
+make_auth_keys_name(const struct passwd *pwd)
f09e2e
+{
f09e2e
+	char *fname;
f09e2e
+
f09e2e
+	if (asprintf(&fname, "%s/.ssh/authorized_keys", pwd->pw_dir) < 0)
f09e2e
+		return NULL;
f09e2e
+
f09e2e
+	return fname;
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+dump_keys(const char *user)
f09e2e
+{
f09e2e
+	struct passwd *pwd;
f09e2e
+	int fd = -1;
f09e2e
+	FILE *f = NULL;
f09e2e
+	char *fname = NULL;
f09e2e
+	int rv = 0;
f09e2e
+	char buf[BUFLEN];
f09e2e
+	size_t len;
f09e2e
+	struct stat st;
f09e2e
+
f09e2e
+	if ((pwd = getpwnam(user)) == NULL) {
f09e2e
+		return ERR_GETPWNAM;
f09e2e
+	}
f09e2e
+
f09e2e
+	if ((fname = make_auth_keys_name(pwd)) == NULL) {
f09e2e
+		return ERR_MEMORY;
f09e2e
+	}
f09e2e
+
f09e2e
+	temporarily_use_uid(pwd);
f09e2e
+
f09e2e
+	if ((fd = open(fname, O_RDONLY|O_NONBLOCK|O_NOFOLLOW, 0)) < 0) {
f09e2e
+		rv = ERR_OPEN;
f09e2e
+		goto fail;
f09e2e
+	}
f09e2e
+
f09e2e
+	if (fstat(fd, &st) < 0) {
f09e2e
+		rv = ERR_STAT;
f09e2e
+		goto fail;
f09e2e
+	}
f09e2e
+
f09e2e
+	if (!S_ISREG(st.st_mode) || 
f09e2e
+		(st.st_uid != pwd->pw_uid && st.st_uid != 0)) {
f09e2e
+		rv = ERR_FILE_MODE;
f09e2e
+		goto fail;
f09e2e
+	}
f09e2e
+
f09e2e
+	unset_nonblock(fd);
f09e2e
+
f09e2e
+	if ((f = fdopen(fd, "r")) == NULL) {
f09e2e
+		rv = ERR_FDOPEN;
f09e2e
+		goto fail;
f09e2e
+	}
f09e2e
+
f09e2e
+	fd = -1;
f09e2e
+
f09e2e
+	while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
f09e2e
+		rv = fwrite(buf, 1, len, stdout) != len ? ERR_WRITE : 0;
f09e2e
+	}
f09e2e
+
f09e2e
+fail:
f09e2e
+	if (fd != -1)
f09e2e
+		close(fd);
f09e2e
+	if (f != NULL)
f09e2e
+		fclose(f);
f09e2e
+	free(fname);
f09e2e
+	restore_uid();
f09e2e
+	return rv;
f09e2e
+}
f09e2e
+
f09e2e
+static const char *env_names[] = { "SELINUX_ROLE_REQUESTED",
f09e2e
+	"SELINUX_LEVEL_REQUESTED",
f09e2e
+	"SELINUX_USE_CURRENT_RANGE"
f09e2e
+};
f09e2e
+
f09e2e
+extern char **environ;
f09e2e
+
f09e2e
+int
f09e2e
+set_pam_environment(pam_handle_t *pamh)
f09e2e
+{
f09e2e
+	int i;
f09e2e
+	size_t j;
f09e2e
+
f09e2e
+	for (j = 0; j < sizeof(env_names)/sizeof(env_names[0]); ++j) {
f09e2e
+		int len = strlen(env_names[j]);
f09e2e
+
f09e2e
+		for (i = 0; environ[i] != NULL; ++i) {
f09e2e
+			if (strncmp(env_names[j], environ[i], len) == 0 &&
f09e2e
+			    environ[i][len] == '=') {
f09e2e
+				if (pam_putenv(pamh, environ[i]) != PAM_SUCCESS)
f09e2e
+					return ERR_PAM_PUTENV;
f09e2e
+			}
f09e2e
+		}
f09e2e
+	}
f09e2e
+
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+main(int argc, char *argv[])
f09e2e
+{
f09e2e
+	pam_handle_t *pamh = NULL;
f09e2e
+	int retval;
f09e2e
+	int ev = 0;
f09e2e
+
f09e2e
+	if (argc != 2) {
f09e2e
+		fprintf(stderr, "Usage: %s <user-name>\n", argv[0]);
f09e2e
+		return ERR_USAGE;
f09e2e
+	}
f09e2e
+
f09e2e
+	retval = pam_start("ssh-keycat", argv[1], &conv, &pamh);
f09e2e
+	if (retval != PAM_SUCCESS) {
f09e2e
+		return ERR_PAM_START;
f09e2e
+	}
f09e2e
+
f09e2e
+	ev = set_pam_environment(pamh);
f09e2e
+	if (ev != 0)
f09e2e
+		goto finish;
f09e2e
+
f09e2e
+	retval = pam_open_session(pamh, PAM_SILENT);
f09e2e
+	if (retval != PAM_SUCCESS) {
f09e2e
+		ev = ERR_OPEN_SESSION;
f09e2e
+		goto finish;
f09e2e
+	}
f09e2e
+
f09e2e
+	ev = dump_keys(argv[1]);
f09e2e
+
f09e2e
+	retval = pam_close_session(pamh, PAM_SILENT);
f09e2e
+	if (retval != PAM_SUCCESS) {
f09e2e
+		ev = ERR_CLOSE_SESSION;
f09e2e
+	}
f09e2e
+
f09e2e
+finish:
f09e2e
+	retval = pam_end (pamh,retval);
f09e2e
+	if (retval != PAM_SUCCESS) {
f09e2e
+		ev = ERR_PAM_END;
f09e2e
+	}
f09e2e
+	return ev;
f09e2e
+}