b58e57
diff -up openssh-7.4p1/auth2-pubkey.c.keycat openssh-7.4p1/auth2-pubkey.c
b58e57
--- openssh-7.4p1/auth2-pubkey.c.keycat	2017-02-08 14:32:33.015581448 +0100
b58e57
+++ openssh-7.4p1/auth2-pubkey.c	2017-02-08 14:40:26.125216292 +0100
b58e57
@@ -1043,6 +1043,14 @@ user_key_command_allowed2(struct passwd
b58e57
 		xasprintf(&command, "%s %s", av[0], av[1]);
b58e57
 	}
b58e57
 
b58e57
+#ifdef WITH_SELINUX
b58e57
+		if (sshd_selinux_setup_env_variables() < 0) {
b58e57
+			error ("failed to copy environment:  %s",
b58e57
+			    strerror(errno));
b58e57
+			_exit(127);
b58e57
+		}
b58e57
+#endif
b58e57
+
b58e57
 	if ((pid = subprocess("AuthorizedKeysCommand", pw, command,
b58e57
 	    ac, av, &f)) == 0)
b58e57
 		goto out;
b58e57
diff -up openssh-7.4p1/configure.ac.keycat openssh-7.4p1/configure.ac
b58e57
--- openssh-7.4p1/configure.ac.keycat	2017-02-08 14:32:33.011581451 +0100
b58e57
+++ openssh-7.4p1/configure.ac	2017-02-08 14:32:33.016581448 +0100
b58e57
@@ -3129,6 +3129,7 @@ AC_ARG_WITH([pam],
b58e57
 			PAM_MSG="yes"
b58e57
 
b58e57
 			SSHDLIBS="$SSHDLIBS -lpam"
b58e57
+			KEYCATLIBS="$KEYCATLIBS -lpam"
b58e57
 			AC_DEFINE([USE_PAM], [1],
b58e57
 				[Define if you want to enable PAM support])
b58e57
 
b58e57
@@ -3139,6 +3140,7 @@ AC_ARG_WITH([pam],
b58e57
 					;;
b58e57
 				*)
b58e57
 					SSHDLIBS="$SSHDLIBS -ldl"
b58e57
+					KEYCATLIBS="$KEYCATLIBS -ldl"
b58e57
 					;;
b58e57
 				esac
b58e57
 			fi
b58e57
@@ -4255,6 +4257,7 @@ AC_ARG_WITH([selinux],
b58e57
 )
b58e57
 AC_SUBST([SSHLIBS])
b58e57
 AC_SUBST([SSHDLIBS])
b58e57
+AC_SUBST([KEYCATLIBS])
b58e57
 
b58e57
 # Check whether user wants Kerberos 5 support
b58e57
 KRB5_MSG="no"
b58e57
@@ -5206,6 +5209,9 @@ fi
b58e57
 if test ! -z "${SSHLIBS}"; then
b58e57
 echo "          +for ssh: ${SSHLIBS}"
b58e57
 fi
b58e57
+if test ! -z "${KEYCATLIBS}"; then
b58e57
+echo "   +for ssh-keycat: ${KEYCATLIBS}"
b58e57
+fi
b58e57
 
b58e57
 echo ""
b58e57
 
b58e57
diff -up openssh-7.4p1/HOWTO.ssh-keycat.keycat openssh-7.4p1/HOWTO.ssh-keycat
b58e57
--- openssh-7.4p1/HOWTO.ssh-keycat.keycat	2017-02-08 14:32:33.014581449 +0100
b58e57
+++ openssh-7.4p1/HOWTO.ssh-keycat	2017-02-08 14:32:33.014581449 +0100
b58e57
@@ -0,0 +1,12 @@
b58e57
+The ssh-keycat retrieves the content of the ~/.ssh/authorized_keys
b58e57
+of an user in any environment. This includes environments with
b58e57
+polyinstantiation of home directories and SELinux MLS policy enabled.
b58e57
+
b58e57
+To use ssh-keycat, set these options in /etc/ssh/sshd_config file:
b58e57
+        AuthorizedKeysCommand /usr/libexec/openssh/ssh-keycat
b58e57
+        AuthorizedKeysCommandUser root
b58e57
+
b58e57
+Do not forget to enable public key authentication:
b58e57
+        PubkeyAuthentication yes
b58e57
+
b58e57
+
b58e57
diff -up openssh-7.4p1/Makefile.in.keycat openssh-7.4p1/Makefile.in
b58e57
--- openssh-7.4p1/Makefile.in.keycat	2017-02-08 14:32:33.012581451 +0100
b58e57
+++ openssh-7.4p1/Makefile.in	2017-02-08 14:38:28.839306815 +0100
b58e57
@@ -27,6 +27,7 @@ SFTP_SERVER=$(libexecdir)/sftp-server
b58e57
 SSH_KEYSIGN=$(libexecdir)/ssh-keysign
b58e57
 SSH_LDAP_HELPER=$(libexecdir)/ssh-ldap-helper
b58e57
 SSH_LDAP_WRAPPER=$(libexecdir)/ssh-ldap-wrapper
b58e57
+SSH_KEYCAT=$(libexecdir)/ssh-keycat
b58e57
 SSH_PKCS11_HELPER=$(libexecdir)/ssh-pkcs11-helper
b58e57
 PRIVSEP_PATH=@PRIVSEP_PATH@
b58e57
 SSH_PRIVSEP_USER=@SSH_PRIVSEP_USER@
b58e57
@@ -51,6 +52,7 @@ K5LIBS=@K5LIBS@
b58e57
 GSSLIBS=@GSSLIBS@
b58e57
 SSHLIBS=@SSHLIBS@
b58e57
 SSHDLIBS=@SSHDLIBS@
b58e57
+KEYCATLIBS=@KEYCATLIBS@
b58e57
 LIBEDIT=@LIBEDIT@
b58e57
 AR=@AR@
b58e57
 AWK=@AWK@
b58e57
@@ -65,7 +67,7 @@ EXEEXT=@EXEEXT@
b58e57
 MANFMT=@MANFMT@
b58e57
 INSTALL_SSH_LDAP_HELPER=@INSTALL_SSH_LDAP_HELPER@
b58e57
 
b58e57
-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)
b58e57
+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)
b58e57
 
b58e57
 LIBOPENSSH_OBJS=\
b58e57
 	ssh_api.o \
b58e57
@@ -190,6 +192,9 @@ ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT)
b58e57
 ssh-ldap-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o
b58e57
 	$(LD) -o $@ ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat -lfipscheck $(LIBS)
b58e57
 
b58e57
+ssh-keycat$(EXEEXT): $(LIBCOMPAT) $(SSHDOBJS) libssh.a ssh-keycat.o
b58e57
+	$(LD) -o $@ ssh-keycat.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(KEYCATLIBS) $(SSHLIBS)
b58e57
+
b58e57
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
b58e57
 	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
b58e57
 
b58e57
@@ -332,6 +337,7 @@ install-files:
b58e57
 		$(INSTALL) -m 0700 $(STRIP_OPT) ssh-ldap-helper $(DESTDIR)$(SSH_LDAP_HELPER) ; \
b58e57
 		$(INSTALL) -m 0700 ssh-ldap-wrapper $(DESTDIR)$(SSH_LDAP_WRAPPER) ; \
b58e57
 	fi
b58e57
+	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-keycat$(EXEEXT) $(DESTDIR)$(libexecdir)/ssh-keycat$(EXEEXT)
b58e57
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp$(EXEEXT) $(DESTDIR)$(bindir)/sftp$(EXEEXT)
b58e57
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp-server$(EXEEXT) $(DESTDIR)$(SFTP_SERVER)$(EXEEXT)
b58e57
 	$(INSTALL) -m 644 ssh.1.out $(DESTDIR)$(mandir)/$(mansubdir)1/ssh.1
b58e57
diff -up openssh-7.4p1/openbsd-compat/port-linux.h.keycat openssh-7.4p1/openbsd-compat/port-linux.h
b58e57
--- openssh-7.4p1/openbsd-compat/port-linux.h.keycat	2017-02-08 14:32:33.009581453 +0100
b58e57
+++ openssh-7.4p1/openbsd-compat/port-linux.h	2017-02-08 14:32:33.015581448 +0100
b58e57
@@ -23,8 +23,10 @@ void ssh_selinux_setup_pty(char *, const
b58e57
 void ssh_selinux_change_context(const char *);
b58e57
 void ssh_selinux_setfscreatecon(const char *);
b58e57
 
b58e57
+int sshd_selinux_enabled(void);
b58e57
 void sshd_selinux_copy_context(void);
b58e57
 void sshd_selinux_setup_exec_context(char *);
b58e57
+int sshd_selinux_setup_env_variables(void);
b58e57
 #endif
b58e57
 
b58e57
 #ifdef LINUX_OOM_ADJUST
b58e57
diff -up openssh-7.4p1/openbsd-compat/port-linux-sshd.c.keycat openssh-7.4p1/openbsd-compat/port-linux-sshd.c
b58e57
--- openssh-7.4p1/openbsd-compat/port-linux-sshd.c.keycat	2017-02-08 14:32:33.008581454 +0100
b58e57
+++ openssh-7.4p1/openbsd-compat/port-linux-sshd.c	2017-02-08 14:32:33.015581448 +0100
b58e57
@@ -53,6 +53,20 @@ extern Authctxt *the_authctxt;
b58e57
 extern int inetd_flag;
b58e57
 extern int rexeced_flag;
b58e57
 
b58e57
+/* Wrapper around is_selinux_enabled() to log its return value once only */
b58e57
+int
b58e57
+sshd_selinux_enabled(void)
b58e57
+{
b58e57
+	static int enabled = -1;
b58e57
+
b58e57
+	if (enabled == -1) {
b58e57
+		enabled = (is_selinux_enabled() == 1);
b58e57
+		debug("SELinux support %s", enabled ? "enabled" : "disabled");
b58e57
+	}
b58e57
+
b58e57
+	return (enabled);
b58e57
+}
b58e57
+
b58e57
 /* Send audit message */
b58e57
 static int
b58e57
 sshd_selinux_send_audit_message(int success, security_context_t default_context,
b58e57
@@ -307,7 +321,7 @@ sshd_selinux_getctxbyname(char *pwname,
b58e57
 
b58e57
 /* Setup environment variables for pam_selinux */
b58e57
 static int
b58e57
-sshd_selinux_setup_pam_variables(void)
b58e57
+sshd_selinux_setup_variables(int(*set_it)(char *, const char *))
b58e57
 {
b58e57
 	const char *reqlvl;
b58e57
 	char *role;
b58e57
@@ -318,16 +332,16 @@ sshd_selinux_setup_pam_variables(void)
b58e57
 
b58e57
 	ssh_selinux_get_role_level(&role, &reqlvl);
b58e57
 
b58e57
-	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
b58e57
+	rv = set_it("SELINUX_ROLE_REQUESTED", role ? role : "");
b58e57
 
b58e57
 	if (inetd_flag && !rexeced_flag) {
b58e57
 		use_current = "1";
b58e57
 	} else {
b58e57
 		use_current = "";
b58e57
-		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
b58e57
+		rv = rv || set_it("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
b58e57
 	}
b58e57
 
b58e57
-	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
b58e57
+	rv = rv || set_it("SELINUX_USE_CURRENT_RANGE", use_current);
b58e57
 
b58e57
 	if (role != NULL)
b58e57
 		free(role);
b58e57
@@ -335,6 +349,24 @@ sshd_selinux_setup_pam_variables(void)
b58e57
 	return rv;
b58e57
 }
b58e57
 
b58e57
+static int
b58e57
+sshd_selinux_setup_pam_variables(void)
b58e57
+{
b58e57
+	return sshd_selinux_setup_variables(do_pam_putenv);
b58e57
+}
b58e57
+
b58e57
+static int
b58e57
+do_setenv(char *name, const char *value)
b58e57
+{
b58e57
+	return setenv(name, value, 1);
b58e57
+}
b58e57
+
b58e57
+int
b58e57
+sshd_selinux_setup_env_variables(void)
b58e57
+{
b58e57
+	return sshd_selinux_setup_variables(do_setenv);
b58e57
+}
b58e57
+
b58e57
 /* Set the execution context to the default for the specified user */
b58e57
 void
b58e57
 sshd_selinux_setup_exec_context(char *pwname)
b58e57
@@ -343,7 +375,7 @@ sshd_selinux_setup_exec_context(char *pw
b58e57
 	int r = 0;
b58e57
 	security_context_t default_ctx = NULL;
b58e57
 
b58e57
-	if (!ssh_selinux_enabled())
b58e57
+	if (!sshd_selinux_enabled())
b58e57
 		return;
b58e57
 
b58e57
 	if (options.use_pam) {
b58e57
@@ -414,7 +446,7 @@ sshd_selinux_copy_context(void)
b58e57
 {
b58e57
 	security_context_t *ctx;
b58e57
 
b58e57
-	if (!ssh_selinux_enabled())
b58e57
+	if (!sshd_selinux_enabled())
b58e57
 		return;
b58e57
 
b58e57
 	if (getexeccon((security_context_t *)&ctx) != 0) {
b58e57
diff -up openssh-7.4p1/platform.c.keycat openssh-7.4p1/platform.c
b58e57
--- openssh-7.4p1/platform.c.keycat	2017-02-08 14:32:33.007581455 +0100
b58e57
+++ openssh-7.4p1/platform.c	2017-02-08 14:32:33.015581448 +0100
b58e57
@@ -99,7 +99,7 @@ platform_setusercontext(struct passwd *p
b58e57
 {
b58e57
 #ifdef WITH_SELINUX
b58e57
 	/* Cache selinux status for later use */
b58e57
-	(void)ssh_selinux_enabled();
b58e57
+	(void)sshd_selinux_enabled();
b58e57
 #endif
b58e57
 
b58e57
 #ifdef USE_SOLARIS_PROJECTS
b58e57
diff -up openssh-7.4p1/ssh-keycat.c.keycat openssh-7.4p1/ssh-keycat.c
b58e57
--- openssh-7.4p1/ssh-keycat.c.keycat	2017-02-08 14:32:33.015581448 +0100
b58e57
+++ openssh-7.4p1/ssh-keycat.c	2017-02-08 14:32:33.015581448 +0100
b58e57
@@ -0,0 +1,238 @@
b58e57
+/*
b58e57
+ * Redistribution and use in source and binary forms, with or without
b58e57
+ * modification, are permitted provided that the following conditions
b58e57
+ * are met:
b58e57
+ * 1. Redistributions of source code must retain the above copyright
b58e57
+ *    notice, and the entire permission notice in its entirety,
b58e57
+ *    including the disclaimer of warranties.
b58e57
+ * 2. Redistributions in binary form must reproduce the above copyright
b58e57
+ *    notice, this list of conditions and the following disclaimer in the
b58e57
+ *    documentation and/or other materials provided with the distribution.
b58e57
+ * 3. The name of the author may not be used to endorse or promote
b58e57
+ *    products derived from this software without specific prior
b58e57
+ *    written permission.
b58e57
+ *
b58e57
+ * ALTERNATIVELY, this product may be distributed under the terms of
b58e57
+ * the GNU Public License, in which case the provisions of the GPL are
b58e57
+ * required INSTEAD OF the above restrictions.  (This clause is
b58e57
+ * necessary due to a potential bad interaction between the GPL and
b58e57
+ * the restrictions contained in a BSD-style copyright.)
b58e57
+ *
b58e57
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
b58e57
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
b58e57
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
b58e57
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
b58e57
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
b58e57
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
b58e57
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
b58e57
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
b58e57
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
b58e57
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
b58e57
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
b58e57
+ */
b58e57
+
b58e57
+/*
b58e57
+ * Copyright (c) 2011 Red Hat, Inc.
b58e57
+ * Written by Tomas Mraz <tmraz@redhat.com>
b58e57
+*/
b58e57
+
b58e57
+#define _GNU_SOURCE
b58e57
+
b58e57
+#include "config.h"
b58e57
+#include <stdio.h>
b58e57
+#include <stdlib.h>
b58e57
+#include <string.h>
b58e57
+#include <sys/types.h>
b58e57
+#include <sys/stat.h>
b58e57
+#include <pwd.h>
b58e57
+#include <fcntl.h>
b58e57
+#include <unistd.h>
b58e57
+
b58e57
+#include <security/pam_appl.h>
b58e57
+
b58e57
+#include "uidswap.h"
b58e57
+#include "misc.h"
b58e57
+
b58e57
+#define ERR_USAGE 1
b58e57
+#define ERR_PAM_START 2
b58e57
+#define ERR_OPEN_SESSION 3
b58e57
+#define ERR_CLOSE_SESSION 4
b58e57
+#define ERR_PAM_END 5
b58e57
+#define ERR_GETPWNAM 6
b58e57
+#define ERR_MEMORY 7
b58e57
+#define ERR_OPEN 8
b58e57
+#define ERR_FILE_MODE 9
b58e57
+#define ERR_FDOPEN 10
b58e57
+#define ERR_STAT 11
b58e57
+#define ERR_WRITE 12
b58e57
+#define ERR_PAM_PUTENV 13
b58e57
+#define BUFLEN 4096
b58e57
+
b58e57
+/* Just ignore the messages in the conversation function */
b58e57
+static int
b58e57
+dummy_conv(int num_msg, const struct pam_message **msgm,
b58e57
+	   struct pam_response **response, void *appdata_ptr)
b58e57
+{
b58e57
+	struct pam_response *rsp;
b58e57
+
b58e57
+	(void)msgm;
b58e57
+	(void)appdata_ptr;
b58e57
+
b58e57
+	if (num_msg <= 0)
b58e57
+		return PAM_CONV_ERR;
b58e57
+
b58e57
+	/* Just allocate the array as empty responses */
b58e57
+	rsp = calloc (num_msg, sizeof (struct pam_response));
b58e57
+	if (rsp == NULL)
b58e57
+		return PAM_CONV_ERR;
b58e57
+
b58e57
+	*response = rsp;
b58e57
+	return PAM_SUCCESS;
b58e57
+}
b58e57
+
b58e57
+static struct pam_conv conv = {
b58e57
+	dummy_conv,
b58e57
+	NULL
b58e57
+};
b58e57
+
b58e57
+char *
b58e57
+make_auth_keys_name(const struct passwd *pwd)
b58e57
+{
b58e57
+	char *fname;
b58e57
+
b58e57
+	if (asprintf(&fname, "%s/.ssh/authorized_keys", pwd->pw_dir) < 0)
b58e57
+		return NULL;
b58e57
+
b58e57
+	return fname;
b58e57
+}
b58e57
+
b58e57
+int
b58e57
+dump_keys(const char *user)
b58e57
+{
b58e57
+	struct passwd *pwd;
b58e57
+	int fd = -1;
b58e57
+	FILE *f = NULL;
b58e57
+	char *fname = NULL;
b58e57
+	int rv = 0;
b58e57
+	char buf[BUFLEN];
b58e57
+	size_t len;
b58e57
+	struct stat st;
b58e57
+
b58e57
+	if ((pwd = getpwnam(user)) == NULL) {
b58e57
+		return ERR_GETPWNAM;
b58e57
+	}
b58e57
+
b58e57
+	if ((fname = make_auth_keys_name(pwd)) == NULL) {
b58e57
+		return ERR_MEMORY;
b58e57
+	}
b58e57
+
b58e57
+	temporarily_use_uid(pwd);
b58e57
+
b58e57
+	if ((fd = open(fname, O_RDONLY|O_NONBLOCK|O_NOFOLLOW, 0)) < 0) {
b58e57
+		rv = ERR_OPEN;
b58e57
+		goto fail;
b58e57
+	}
b58e57
+
b58e57
+	if (fstat(fd, &st) < 0) {
b58e57
+		rv = ERR_STAT;
b58e57
+		goto fail;
b58e57
+	}
b58e57
+
b58e57
+	if (!S_ISREG(st.st_mode) || 
b58e57
+		(st.st_uid != pwd->pw_uid && st.st_uid != 0)) {
b58e57
+		rv = ERR_FILE_MODE;
b58e57
+		goto fail;
b58e57
+	}
b58e57
+
b58e57
+	unset_nonblock(fd);
b58e57
+
b58e57
+	if ((f = fdopen(fd, "r")) == NULL) {
b58e57
+		rv = ERR_FDOPEN;
b58e57
+		goto fail;
b58e57
+	}
b58e57
+
b58e57
+	fd = -1;
b58e57
+
b58e57
+	while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
b58e57
+		rv = fwrite(buf, 1, len, stdout) != len ? ERR_WRITE : 0;
b58e57
+	}
b58e57
+
b58e57
+fail:
b58e57
+	if (fd != -1)
b58e57
+		close(fd);
b58e57
+	if (f != NULL)
b58e57
+		fclose(f);
b58e57
+	free(fname);
b58e57
+	restore_uid();
b58e57
+	return rv;
b58e57
+}
b58e57
+
b58e57
+static const char *env_names[] = { "SELINUX_ROLE_REQUESTED",
b58e57
+	"SELINUX_LEVEL_REQUESTED",
b58e57
+	"SELINUX_USE_CURRENT_RANGE"
b58e57
+};
b58e57
+
b58e57
+extern char **environ;
b58e57
+
b58e57
+int
b58e57
+set_pam_environment(pam_handle_t *pamh)
b58e57
+{
b58e57
+	int i;
b58e57
+	size_t j;
b58e57
+
b58e57
+	for (j = 0; j < sizeof(env_names)/sizeof(env_names[0]); ++j) {
b58e57
+		int len = strlen(env_names[j]);
b58e57
+
b58e57
+		for (i = 0; environ[i] != NULL; ++i) {
b58e57
+			if (strncmp(env_names[j], environ[i], len) == 0 &&
b58e57
+			    environ[i][len] == '=') {
b58e57
+				if (pam_putenv(pamh, environ[i]) != PAM_SUCCESS)
b58e57
+					return ERR_PAM_PUTENV;
b58e57
+			}
b58e57
+		}
b58e57
+	}
b58e57
+
b58e57
+	return 0;
b58e57
+}
b58e57
+
b58e57
+int
b58e57
+main(int argc, char *argv[])
b58e57
+{
b58e57
+	pam_handle_t *pamh = NULL;
b58e57
+	int retval;
b58e57
+	int ev = 0;
b58e57
+
b58e57
+	if (argc != 2) {
b58e57
+		fprintf(stderr, "Usage: %s <user-name>\n", argv[0]);
b58e57
+		return ERR_USAGE;
b58e57
+	}
b58e57
+
b58e57
+	retval = pam_start("ssh-keycat", argv[1], &conv, &pamh);
b58e57
+	if (retval != PAM_SUCCESS) {
b58e57
+		return ERR_PAM_START;
b58e57
+	}
b58e57
+
b58e57
+	ev = set_pam_environment(pamh);
b58e57
+	if (ev != 0)
b58e57
+		goto finish;
b58e57
+
b58e57
+	retval = pam_open_session(pamh, PAM_SILENT);
b58e57
+	if (retval != PAM_SUCCESS) {
b58e57
+		ev = ERR_OPEN_SESSION;
b58e57
+		goto finish;
b58e57
+	}
b58e57
+
b58e57
+	ev = dump_keys(argv[1]);
b58e57
+
b58e57
+	retval = pam_close_session(pamh, PAM_SILENT);
b58e57
+	if (retval != PAM_SUCCESS) {
b58e57
+		ev = ERR_CLOSE_SESSION;
b58e57
+	}
b58e57
+
b58e57
+finish:
b58e57
+	retval = pam_end (pamh,retval);
b58e57
+	if (retval != PAM_SUCCESS) {
b58e57
+		ev = ERR_PAM_END;
b58e57
+	}
b58e57
+	return ev;
b58e57
+}