1d31ef
diff -up openssh-7.4p1/auth2-pubkey.c.keycat openssh-7.4p1/auth2-pubkey.c
1d31ef
--- openssh-7.4p1/auth2-pubkey.c.keycat	2017-02-08 14:32:33.015581448 +0100
1d31ef
+++ openssh-7.4p1/auth2-pubkey.c	2017-02-08 14:40:26.125216292 +0100
1d31ef
@@ -1043,6 +1043,14 @@ user_key_command_allowed2(struct passwd
1d31ef
 		xasprintf(&command, "%s %s", av[0], av[1]);
1d31ef
 	}
1d31ef
 
1d31ef
+#ifdef WITH_SELINUX
1d31ef
+		if (sshd_selinux_setup_env_variables() < 0) {
1d31ef
+			error ("failed to copy environment:  %s",
1d31ef
+			    strerror(errno));
1d31ef
+			_exit(127);
1d31ef
+		}
1d31ef
+#endif
1d31ef
+
1d31ef
 	if ((pid = subprocess("AuthorizedKeysCommand", pw, command,
1d31ef
 	    ac, av, &f)) == 0)
1d31ef
 		goto out;
1d31ef
diff -up openssh-7.4p1/configure.ac.keycat openssh-7.4p1/configure.ac
1d31ef
--- openssh-7.4p1/configure.ac.keycat	2017-02-08 14:32:33.011581451 +0100
1d31ef
+++ openssh-7.4p1/configure.ac	2017-02-08 14:32:33.016581448 +0100
1d31ef
@@ -3129,6 +3129,7 @@ AC_ARG_WITH([pam],
1d31ef
 			PAM_MSG="yes"
1d31ef
 
1d31ef
 			SSHDLIBS="$SSHDLIBS -lpam"
1d31ef
+			KEYCATLIBS="$KEYCATLIBS -lpam"
1d31ef
 			AC_DEFINE([USE_PAM], [1],
1d31ef
 				[Define if you want to enable PAM support])
1d31ef
 
1d31ef
@@ -3139,6 +3140,7 @@ AC_ARG_WITH([pam],
1d31ef
 					;;
1d31ef
 				*)
1d31ef
 					SSHDLIBS="$SSHDLIBS -ldl"
1d31ef
+					KEYCATLIBS="$KEYCATLIBS -ldl"
1d31ef
 					;;
1d31ef
 				esac
1d31ef
 			fi
1d31ef
@@ -4255,6 +4257,7 @@ AC_ARG_WITH([selinux],
1d31ef
 )
1d31ef
 AC_SUBST([SSHLIBS])
1d31ef
 AC_SUBST([SSHDLIBS])
1d31ef
+AC_SUBST([KEYCATLIBS])
1d31ef
 
1d31ef
 # Check whether user wants Kerberos 5 support
1d31ef
 KRB5_MSG="no"
1d31ef
@@ -5206,6 +5209,9 @@ fi
1d31ef
 if test ! -z "${SSHLIBS}"; then
1d31ef
 echo "          +for ssh: ${SSHLIBS}"
1d31ef
 fi
1d31ef
+if test ! -z "${KEYCATLIBS}"; then
1d31ef
+echo "   +for ssh-keycat: ${KEYCATLIBS}"
1d31ef
+fi
1d31ef
 
1d31ef
 echo ""
1d31ef
 
1d31ef
diff -up openssh-7.4p1/HOWTO.ssh-keycat.keycat openssh-7.4p1/HOWTO.ssh-keycat
1d31ef
--- openssh-7.4p1/HOWTO.ssh-keycat.keycat	2017-02-08 14:32:33.014581449 +0100
1d31ef
+++ openssh-7.4p1/HOWTO.ssh-keycat	2017-02-08 14:32:33.014581449 +0100
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
770374
+        AuthorizedKeysCommandUser root
f09e2e
+
f09e2e
+Do not forget to enable public key authentication:
f09e2e
+        PubkeyAuthentication yes
f09e2e
+
f09e2e
+
1d31ef
diff -up openssh-7.4p1/Makefile.in.keycat openssh-7.4p1/Makefile.in
1d31ef
--- openssh-7.4p1/Makefile.in.keycat	2017-02-08 14:32:33.012581451 +0100
1d31ef
+++ openssh-7.4p1/Makefile.in	2017-02-08 14:38:28.839306815 +0100
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@
1d31ef
@@ -51,6 +52,7 @@ K5LIBS=@K5LIBS@
f8987c
 GSSLIBS=@GSSLIBS@
f8987c
 SSHLIBS=@SSHLIBS@
f8987c
 SSHDLIBS=@SSHDLIBS@
f8987c
+KEYCATLIBS=@KEYCATLIBS@
f8987c
 LIBEDIT=@LIBEDIT@
f8987c
 AR=@AR@
f8987c
 AWK=@AWK@
1d31ef
@@ -65,7 +67,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
 
1d31ef
 LIBOPENSSH_OBJS=\
1d31ef
 	ssh_api.o \
1d31ef
@@ -190,6 +192,9 @@ ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT)
f09e2e
 ssh-ldap-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o
1d31ef
 	$(LD) -o $@ ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat -lfipscheck $(LIBS)
f09e2e
 
017ff1
+ssh-keycat$(EXEEXT): $(LIBCOMPAT) $(SSHDOBJS) libssh.a ssh-keycat.o
f8987c
+	$(LD) -o $@ ssh-keycat.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(KEYCATLIBS) $(SSHLIBS)
f09e2e
+
1d31ef
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
1d31ef
 	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
f09e2e
 
1d31ef
@@ -332,6 +337,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
1d31ef
diff -up openssh-7.4p1/openbsd-compat/port-linux.h.keycat openssh-7.4p1/openbsd-compat/port-linux.h
1d31ef
--- openssh-7.4p1/openbsd-compat/port-linux.h.keycat	2017-02-08 14:32:33.009581453 +0100
1d31ef
+++ openssh-7.4p1/openbsd-compat/port-linux.h	2017-02-08 14:32:33.015581448 +0100
1d31ef
@@ -23,8 +23,10 @@ void ssh_selinux_setup_pty(char *, const
1d31ef
 void ssh_selinux_change_context(const char *);
1d31ef
 void ssh_selinux_setfscreatecon(const char *);
f09e2e
 
1d31ef
+int sshd_selinux_enabled(void);
1d31ef
 void sshd_selinux_copy_context(void);
1d31ef
 void sshd_selinux_setup_exec_context(char *);
1d31ef
+int sshd_selinux_setup_env_variables(void);
1d31ef
 #endif
f09e2e
 
1d31ef
 #ifdef LINUX_OOM_ADJUST
1d31ef
diff -up openssh-7.4p1/openbsd-compat/port-linux-sshd.c.keycat openssh-7.4p1/openbsd-compat/port-linux-sshd.c
1d31ef
--- openssh-7.4p1/openbsd-compat/port-linux-sshd.c.keycat	2017-02-08 14:32:33.008581454 +0100
1d31ef
+++ openssh-7.4p1/openbsd-compat/port-linux-sshd.c	2017-02-08 14:32:33.015581448 +0100
017ff1
@@ -53,6 +53,20 @@ extern Authctxt *the_authctxt;
017ff1
 extern int inetd_flag;
017ff1
 extern int rexeced_flag;
017ff1
 
017ff1
+/* Wrapper around is_selinux_enabled() to log its return value once only */
017ff1
+int
017ff1
+sshd_selinux_enabled(void)
017ff1
+{
017ff1
+	static int enabled = -1;
017ff1
+
017ff1
+	if (enabled == -1) {
017ff1
+		enabled = (is_selinux_enabled() == 1);
017ff1
+		debug("SELinux support %s", enabled ? "enabled" : "disabled");
017ff1
+	}
017ff1
+
017ff1
+	return (enabled);
017ff1
+}
017ff1
+
017ff1
 /* Send audit message */
017ff1
 static int
017ff1
 sshd_selinux_send_audit_message(int success, security_context_t default_context,
017ff1
@@ -307,7 +321,7 @@ sshd_selinux_getctxbyname(char *pwname,
f09e2e
 
f09e2e
 /* Setup environment variables for pam_selinux */
f09e2e
 static int
017ff1
-sshd_selinux_setup_pam_variables(void)
017ff1
+sshd_selinux_setup_variables(int(*set_it)(char *, const char *))
f09e2e
 {
f09e2e
 	const char *reqlvl;
f09e2e
 	char *role;
017ff1
@@ -318,16 +332,16 @@ sshd_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);
017ff1
@@ -335,6 +349,24 @@ sshd_selinux_setup_pam_variables(void)
f09e2e
 	return rv;
f09e2e
 }
f09e2e
 
f09e2e
+static int
017ff1
+sshd_selinux_setup_pam_variables(void)
f09e2e
+{
017ff1
+	return sshd_selinux_setup_variables(do_pam_putenv);
f09e2e
+}
f09e2e
+
f09e2e
+static int
017ff1
+do_setenv(char *name, const char *value)
f09e2e
+{
f09e2e
+	return setenv(name, value, 1);
f09e2e
+}
f09e2e
+
f09e2e
+int
017ff1
+sshd_selinux_setup_env_variables(void)
f09e2e
+{
017ff1
+	return sshd_selinux_setup_variables(do_setenv);
f09e2e
+}
f09e2e
+
f09e2e
 /* Set the execution context to the default for the specified user */
f09e2e
 void
017ff1
 sshd_selinux_setup_exec_context(char *pwname)
1d31ef
@@ -343,7 +375,7 @@ sshd_selinux_setup_exec_context(char *pw
017ff1
 	int r = 0;
017ff1
 	security_context_t default_ctx = NULL;
017ff1
 
017ff1
-	if (!ssh_selinux_enabled())
017ff1
+	if (!sshd_selinux_enabled())
017ff1
 		return;
017ff1
 
017ff1
 	if (options.use_pam) {
017ff1
@@ -414,7 +446,7 @@ sshd_selinux_copy_context(void)
017ff1
 {
017ff1
 	security_context_t *ctx;
017ff1
 
017ff1
-	if (!ssh_selinux_enabled())
017ff1
+	if (!sshd_selinux_enabled())
017ff1
 		return;
017ff1
 
017ff1
 	if (getexeccon((security_context_t *)&ctx) != 0) {
1d31ef
diff -up openssh-7.4p1/platform.c.keycat openssh-7.4p1/platform.c
1d31ef
--- openssh-7.4p1/platform.c.keycat	2017-02-08 14:32:33.007581455 +0100
1d31ef
+++ openssh-7.4p1/platform.c	2017-02-08 14:32:33.015581448 +0100
1d31ef
@@ -99,7 +99,7 @@ platform_setusercontext(struct passwd *p
017ff1
 {
017ff1
 #ifdef WITH_SELINUX
017ff1
 	/* Cache selinux status for later use */
017ff1
-	(void)ssh_selinux_enabled();
017ff1
+	(void)sshd_selinux_enabled();
017ff1
 #endif
017ff1
 
017ff1
 #ifdef USE_SOLARIS_PROJECTS
1d31ef
diff -up openssh-7.4p1/ssh-keycat.c.keycat openssh-7.4p1/ssh-keycat.c
1d31ef
--- openssh-7.4p1/ssh-keycat.c.keycat	2017-02-08 14:32:33.015581448 +0100
1d31ef
+++ openssh-7.4p1/ssh-keycat.c	2017-02-08 14:32:33.015581448 +0100
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
+}