vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Petr Lautrbach 94c6f8
diff --git a/HOWTO.ssh-keycat b/HOWTO.ssh-keycat
Petr Lautrbach 94c6f8
new file mode 100644
Petr Lautrbach 94c6f8
index 0000000..630ec62
Petr Lautrbach 94c6f8
--- /dev/null
Petr Lautrbach 94c6f8
+++ b/HOWTO.ssh-keycat
Jan F. Chadima 69dd72
@@ -0,0 +1,12 @@
Jan F. Chadima 69dd72
+The ssh-keycat retrieves the content of the ~/.ssh/authorized_keys
Jan F. Chadima 69dd72
+of an user in any environment. This includes environments with
Jan F. Chadima 69dd72
+polyinstantiation of home directories and SELinux MLS policy enabled.
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+To use ssh-keycat, set these options in /etc/ssh/sshd_config file:
Jan F. Chadima 69dd72
+        AuthorizedKeysCommand /usr/libexec/openssh/ssh-keycat
Petr Lautrbach 8f439b
+        AuthorizedKeysCommandUser root
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+Do not forget to enable public key authentication:
Jan F. Chadima 69dd72
+        PubkeyAuthentication yes
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+
Petr Lautrbach 94c6f8
diff --git a/Makefile.in b/Makefile.in
Petr Lautrbach 94c6f8
index 411eadb..4ab6717 100644
Petr Lautrbach 94c6f8
--- a/Makefile.in
Petr Lautrbach 94c6f8
+++ b/Makefile.in
Jan F. Chadima 69dd72
@@ -27,6 +27,7 @@ SFTP_SERVER=$(libexecdir)/sftp-server
Jan F. Chadima 69dd72
 SSH_KEYSIGN=$(libexecdir)/ssh-keysign
Jan F. Chadima 69dd72
 SSH_LDAP_HELPER=$(libexecdir)/ssh-ldap-helper
Jan F. Chadima 69dd72
 SSH_LDAP_WRAPPER=$(libexecdir)/ssh-ldap-wrapper
Jan F. Chadima 69dd72
+SSH_KEYCAT=$(libexecdir)/ssh-keycat
Jan F. Chadima 69dd72
 SSH_PKCS11_HELPER=$(libexecdir)/ssh-pkcs11-helper
Jan F. Chadima 69dd72
 PRIVSEP_PATH=@PRIVSEP_PATH@
Jan F. Chadima 69dd72
 SSH_PRIVSEP_USER=@SSH_PRIVSEP_USER@
Petr Lautrbach 8a29de
@@ -64,7 +65,7 @@ EXEEXT=@EXEEXT@
Jan F. Chadima 69dd72
 MANFMT=@MANFMT@
Jan F. Chadima 69dd72
 INSTALL_SSH_LDAP_HELPER=@INSTALL_SSH_LDAP_HELPER@
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-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)
Jan F. Chadima 69dd72
+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)
Jan F. Chadima 69dd72
 
Petr Lautrbach 8a29de
 LIBSSH_OBJS=authfd.o authfile.o bufaux.o bufbn.o buffer.o \
Petr Lautrbach 8a29de
 	canohost.o channels.o cipher.o cipher-aes.o \
Petr Lautrbach 94c6f8
@@ -176,6 +177,9 @@ ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11
Jan F. Chadima 69dd72
 ssh-ldap-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o
Jan F. Chadima 69dd72
 	$(LD) -o $@ ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Jan F. Chadima 69dd72
 
Petr Lautrbach 94c6f8
+ssh-keycat$(EXEEXT): $(LIBCOMPAT) $(SSHDOBJS) libssh.a ssh-keycat.o
Petr Lautrbach 94c6f8
+	$(LD) -o $@ ssh-keycat.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(SSHLIBS)
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o roaming_dummy.o
Jan F. Chadima 69dd72
 	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
Jan F. Chadima 69dd72
 
Petr Lautrbach 94c6f8
@@ -283,6 +287,7 @@ install-files:
Jan F. Chadima 69dd72
 		$(INSTALL) -m 0700 $(STRIP_OPT) ssh-ldap-helper $(DESTDIR)$(SSH_LDAP_HELPER) ; \
Jan F. Chadima 69dd72
 		$(INSTALL) -m 0700 ssh-ldap-wrapper $(DESTDIR)$(SSH_LDAP_WRAPPER) ; \
Jan F. Chadima 69dd72
 	fi
Jan F. Chadima 69dd72
+	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-keycat$(EXEEXT) $(DESTDIR)$(libexecdir)/ssh-keycat$(EXEEXT)
Jan F. Chadima 69dd72
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp$(EXEEXT) $(DESTDIR)$(bindir)/sftp$(EXEEXT)
Jan F. Chadima 69dd72
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp-server$(EXEEXT) $(DESTDIR)$(SFTP_SERVER)$(EXEEXT)
Jan F. Chadima 69dd72
 	$(INSTALL) -m 644 ssh.1.out $(DESTDIR)$(mandir)/$(mansubdir)1/ssh.1
Petr Lautrbach 94c6f8
diff --git a/auth2-pubkey.c b/auth2-pubkey.c
Petr Lautrbach 94c6f8
index c0ae0d4..cb0f931 100644
Petr Lautrbach 94c6f8
--- a/auth2-pubkey.c
Petr Lautrbach 94c6f8
+++ b/auth2-pubkey.c
Petr Lautrbach 94c6f8
@@ -600,6 +600,14 @@ user_key_command_allowed2(struct passwd *user_pw, Key *key)
Petr Lautrbach 84822b
 			_exit(1);
Petr Lautrbach 84822b
 		}
Petr Lautrbach 84822b
 
Petr Lautrbach 84822b
+#ifdef WITH_SELINUX
Petr Lautrbach 94c6f8
+		if (sshd_selinux_setup_env_variables() < 0) {
Petr Lautrbach 84822b
+			error ("failed to copy environment:  %s",
Petr Lautrbach 84822b
+			    strerror(errno));
Petr Lautrbach 84822b
+			_exit(127);
Petr Lautrbach 84822b
+		}
Petr Lautrbach 84822b
+#endif
Petr Lautrbach 84822b
+
Petr Lautrbach 84822b
 		execl(options.authorized_keys_command,
Petr Lautrbach 84822b
 		    options.authorized_keys_command, user_pw->pw_name, NULL);
Petr Lautrbach 84822b
 
Petr Lautrbach 94c6f8
diff --git a/openbsd-compat/port-linux-sshd.c b/openbsd-compat/port-linux-sshd.c
Petr Lautrbach 94c6f8
index d04f4ed..0077dd7 100644
Petr Lautrbach 94c6f8
--- a/openbsd-compat/port-linux-sshd.c
Petr Lautrbach 94c6f8
+++ b/openbsd-compat/port-linux-sshd.c
Petr Lautrbach 94c6f8
@@ -53,6 +53,20 @@ extern Authctxt *the_authctxt;
Petr Lautrbach 94c6f8
 extern int inetd_flag;
Petr Lautrbach 94c6f8
 extern int rexeced_flag;
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
+/* Wrapper around is_selinux_enabled() to log its return value once only */
Petr Lautrbach 94c6f8
+int
Petr Lautrbach 94c6f8
+sshd_selinux_enabled(void)
Petr Lautrbach 94c6f8
+{
Petr Lautrbach 94c6f8
+	static int enabled = -1;
Petr Lautrbach 94c6f8
+
Petr Lautrbach 94c6f8
+	if (enabled == -1) {
Petr Lautrbach 94c6f8
+		enabled = (is_selinux_enabled() == 1);
Petr Lautrbach 94c6f8
+		debug("SELinux support %s", enabled ? "enabled" : "disabled");
Petr Lautrbach 94c6f8
+	}
Petr Lautrbach 94c6f8
+
Petr Lautrbach 94c6f8
+	return (enabled);
Petr Lautrbach 94c6f8
+}
Petr Lautrbach 94c6f8
+
Petr Lautrbach 94c6f8
 /* Send audit message */
Petr Lautrbach 94c6f8
 static int
Petr Lautrbach 94c6f8
 sshd_selinux_send_audit_message(int success, security_context_t default_context,
Petr Lautrbach 94c6f8
@@ -307,7 +321,7 @@ sshd_selinux_getctxbyname(char *pwname,
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 /* Setup environment variables for pam_selinux */
Jan F. Chadima 69dd72
 static int
Petr Lautrbach 94c6f8
-sshd_selinux_setup_pam_variables(void)
Petr Lautrbach 94c6f8
+sshd_selinux_setup_variables(int(*set_it)(char *, const char *))
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	const char *reqlvl;
Jan F. Chadima 69dd72
 	char *role;
Petr Lautrbach 94c6f8
@@ -318,16 +332,16 @@ sshd_selinux_setup_pam_variables(void)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	ssh_selinux_get_role_level(&role, &reqlvl);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
Jan F. Chadima 69dd72
+	rv = set_it("SELINUX_ROLE_REQUESTED", role ? role : "");
Petr Lautrbach 84822b
 
Jan F. Chadima 69dd72
 	if (inetd_flag && !rexeced_flag) {
Jan F. Chadima 69dd72
 		use_current = "1";
Jan F. Chadima 69dd72
 	} else {
Jan F. Chadima 69dd72
 		use_current = "";
Jan F. Chadima 69dd72
-		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
Jan F. Chadima 69dd72
+		rv = rv || set_it("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
Jan F. Chadima 69dd72
+	rv = rv || set_it("SELINUX_USE_CURRENT_RANGE", use_current);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	if (role != NULL)
Petr Lautrbach 84822b
 		free(role);
Petr Lautrbach 94c6f8
@@ -335,6 +349,24 @@ sshd_selinux_setup_pam_variables(void)
Jan F. Chadima 69dd72
 	return rv;
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+static int
Petr Lautrbach 94c6f8
+sshd_selinux_setup_pam_variables(void)
Jan F. Chadima 69dd72
+{
Petr Lautrbach 94c6f8
+	return sshd_selinux_setup_variables(do_pam_putenv);
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+static int
Petr Lautrbach 94c6f8
+do_setenv(char *name, const char *value)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	return setenv(name, value, 1);
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+int
Petr Lautrbach 94c6f8
+sshd_selinux_setup_env_variables(void)
Jan F. Chadima 69dd72
+{
Petr Lautrbach 94c6f8
+	return sshd_selinux_setup_variables(do_setenv);
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 /* Set the execution context to the default for the specified user */
Jan F. Chadima 69dd72
 void
Petr Lautrbach 94c6f8
 sshd_selinux_setup_exec_context(char *pwname)
Petr Lautrbach 94c6f8
@@ -343,7 +375,7 @@ sshd_selinux_setup_exec_context(char *pwname)
Petr Lautrbach 94c6f8
 	int r = 0;
Petr Lautrbach 94c6f8
 	security_context_t default_ctx = NULL;
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
-	if (!ssh_selinux_enabled())
Petr Lautrbach 94c6f8
+	if (!sshd_selinux_enabled())
Petr Lautrbach 94c6f8
 		return;
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
 	if (options.use_pam) {
Petr Lautrbach 94c6f8
@@ -414,7 +446,7 @@ sshd_selinux_copy_context(void)
Petr Lautrbach 94c6f8
 {
Petr Lautrbach 94c6f8
 	security_context_t *ctx;
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
-	if (!ssh_selinux_enabled())
Petr Lautrbach 94c6f8
+	if (!sshd_selinux_enabled())
Petr Lautrbach 94c6f8
 		return;
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
 	if (getexeccon((security_context_t *)&ctx) != 0) {
Petr Lautrbach 94c6f8
diff --git a/openbsd-compat/port-linux.h b/openbsd-compat/port-linux.h
Petr Lautrbach 94c6f8
index b18893c..cb51f99 100644
Petr Lautrbach 94c6f8
--- a/openbsd-compat/port-linux.h
Petr Lautrbach 94c6f8
+++ b/openbsd-compat/port-linux.h
Petr Lautrbach 94c6f8
@@ -25,8 +25,10 @@ void ssh_selinux_setup_pty(char *, const char *);
Petr Lautrbach 94c6f8
 void ssh_selinux_change_context(const char *);
Petr Lautrbach 94c6f8
 void ssh_selinux_setfscreatecon(const char *);
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
+int sshd_selinux_enabled(void);
Petr Lautrbach 94c6f8
 void sshd_selinux_copy_context(void);
Petr Lautrbach 94c6f8
 void sshd_selinux_setup_exec_context(char *);
Petr Lautrbach 94c6f8
+int sshd_selinux_setup_env_variables(void);
Petr Lautrbach 94c6f8
 #endif
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
 #ifdef LINUX_OOM_ADJUST
Petr Lautrbach 94c6f8
diff --git a/platform.c b/platform.c
Petr Lautrbach 94c6f8
index 0d39ab2..0dae387 100644
Petr Lautrbach 94c6f8
--- a/platform.c
Petr Lautrbach 94c6f8
+++ b/platform.c
Petr Lautrbach 94c6f8
@@ -102,7 +102,7 @@ platform_setusercontext(struct passwd *pw)
Petr Lautrbach 94c6f8
 {
Petr Lautrbach 94c6f8
 #ifdef WITH_SELINUX
Petr Lautrbach 94c6f8
 	/* Cache selinux status for later use */
Petr Lautrbach 94c6f8
-	(void)ssh_selinux_enabled();
Petr Lautrbach 94c6f8
+	(void)sshd_selinux_enabled();
Petr Lautrbach 94c6f8
 #endif
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
 #ifdef USE_SOLARIS_PROJECTS
Petr Lautrbach 94c6f8
diff --git a/ssh-keycat.c b/ssh-keycat.c
Petr Lautrbach 94c6f8
new file mode 100644
Petr Lautrbach 94c6f8
index 0000000..f8ed7af
Petr Lautrbach 94c6f8
--- /dev/null
Petr Lautrbach 94c6f8
+++ b/ssh-keycat.c
Jan F. Chadima 69dd72
@@ -0,0 +1,238 @@
Jan F. Chadima 69dd72
+/*
Jan F. Chadima 69dd72
+ * Redistribution and use in source and binary forms, with or without
Jan F. Chadima 69dd72
+ * modification, are permitted provided that the following conditions
Jan F. Chadima 69dd72
+ * are met:
Jan F. Chadima 69dd72
+ * 1. Redistributions of source code must retain the above copyright
Jan F. Chadima 69dd72
+ *    notice, and the entire permission notice in its entirety,
Jan F. Chadima 69dd72
+ *    including the disclaimer of warranties.
Jan F. Chadima 69dd72
+ * 2. Redistributions in binary form must reproduce the above copyright
Jan F. Chadima 69dd72
+ *    notice, this list of conditions and the following disclaimer in the
Jan F. Chadima 69dd72
+ *    documentation and/or other materials provided with the distribution.
Jan F. Chadima 69dd72
+ * 3. The name of the author may not be used to endorse or promote
Jan F. Chadima 69dd72
+ *    products derived from this software without specific prior
Jan F. Chadima 69dd72
+ *    written permission.
Jan F. Chadima 69dd72
+ *
Jan F. Chadima 69dd72
+ * ALTERNATIVELY, this product may be distributed under the terms of
Jan F. Chadima 69dd72
+ * the GNU Public License, in which case the provisions of the GPL are
Jan F. Chadima 69dd72
+ * required INSTEAD OF the above restrictions.  (This clause is
Jan F. Chadima 69dd72
+ * necessary due to a potential bad interaction between the GPL and
Jan F. Chadima 69dd72
+ * the restrictions contained in a BSD-style copyright.)
Jan F. Chadima 69dd72
+ *
Jan F. Chadima 69dd72
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
Jan F. Chadima 69dd72
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Jan F. Chadima 69dd72
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Jan F. Chadima 69dd72
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
Jan F. Chadima 69dd72
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Jan F. Chadima 69dd72
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Jan F. Chadima 69dd72
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Jan F. Chadima 69dd72
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Jan F. Chadima 69dd72
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Jan F. Chadima 69dd72
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Jan F. Chadima 69dd72
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
Jan F. Chadima 69dd72
+ */
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+/*
Jan F. Chadima 69dd72
+ * Copyright (c) 2011 Red Hat, Inc.
Jan F. Chadima 69dd72
+ * Written by Tomas Mraz <tmraz@redhat.com>
Jan F. Chadima 69dd72
+*/
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+#define _GNU_SOURCE
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+#include "config.h"
Jan F. Chadima 69dd72
+#include <stdio.h>
Jan F. Chadima 69dd72
+#include <stdlib.h>
Jan F. Chadima 69dd72
+#include <string.h>
Jan F. Chadima 69dd72
+#include <sys/types.h>
Jan F. Chadima 69dd72
+#include <sys/stat.h>
Jan F. Chadima 69dd72
+#include <pwd.h>
Jan F. Chadima 69dd72
+#include <fcntl.h>
Jan F. Chadima 69dd72
+#include <unistd.h>
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+#include <security/pam_appl.h>
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+#include "uidswap.h"
Jan F. Chadima 69dd72
+#include "misc.h"
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+#define ERR_USAGE 1
Jan F. Chadima 69dd72
+#define ERR_PAM_START 2
Jan F. Chadima 69dd72
+#define ERR_OPEN_SESSION 3
Jan F. Chadima 69dd72
+#define ERR_CLOSE_SESSION 4
Jan F. Chadima 69dd72
+#define ERR_PAM_END 5
Jan F. Chadima 69dd72
+#define ERR_GETPWNAM 6
Jan F. Chadima 69dd72
+#define ERR_MEMORY 7
Jan F. Chadima 69dd72
+#define ERR_OPEN 8
Jan F. Chadima 69dd72
+#define ERR_FILE_MODE 9
Jan F. Chadima 69dd72
+#define ERR_FDOPEN 10
Jan F. Chadima 69dd72
+#define ERR_STAT 11
Jan F. Chadima 69dd72
+#define ERR_WRITE 12
Jan F. Chadima 69dd72
+#define ERR_PAM_PUTENV 13
Jan F. Chadima 69dd72
+#define BUFLEN 4096
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+/* Just ignore the messages in the conversation function */
Jan F. Chadima 69dd72
+static int
Jan F. Chadima 69dd72
+dummy_conv(int num_msg, const struct pam_message **msgm,
Jan F. Chadima 69dd72
+	   struct pam_response **response, void *appdata_ptr)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	struct pam_response *rsp;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	(void)msgm;
Jan F. Chadima 69dd72
+	(void)appdata_ptr;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if (num_msg <= 0)
Jan F. Chadima 69dd72
+		return PAM_CONV_ERR;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	/* Just allocate the array as empty responses */
Jan F. Chadima 69dd72
+	rsp = calloc (num_msg, sizeof (struct pam_response));
Jan F. Chadima 69dd72
+	if (rsp == NULL)
Jan F. Chadima 69dd72
+		return PAM_CONV_ERR;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	*response = rsp;
Jan F. Chadima 69dd72
+	return PAM_SUCCESS;
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+static struct pam_conv conv = {
Jan F. Chadima 69dd72
+	dummy_conv,
Jan F. Chadima 69dd72
+	NULL
Jan F. Chadima 69dd72
+};
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+char *
Jan F. Chadima 69dd72
+make_auth_keys_name(const struct passwd *pwd)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	char *fname;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if (asprintf(&fname, "%s/.ssh/authorized_keys", pwd->pw_dir) < 0)
Jan F. Chadima 69dd72
+		return NULL;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	return fname;
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+int
Jan F. Chadima 69dd72
+dump_keys(const char *user)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	struct passwd *pwd;
Jan F. Chadima 69dd72
+	int fd = -1;
Jan F. Chadima 69dd72
+	FILE *f = NULL;
Jan F. Chadima 69dd72
+	char *fname = NULL;
Jan F. Chadima 69dd72
+	int rv = 0;
Jan F. Chadima 69dd72
+	char buf[BUFLEN];
Jan F. Chadima 69dd72
+	size_t len;
Jan F. Chadima 69dd72
+	struct stat st;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if ((pwd = getpwnam(user)) == NULL) {
Jan F. Chadima 69dd72
+		return ERR_GETPWNAM;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if ((fname = make_auth_keys_name(pwd)) == NULL) {
Jan F. Chadima 69dd72
+		return ERR_MEMORY;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	temporarily_use_uid(pwd);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if ((fd = open(fname, O_RDONLY|O_NONBLOCK|O_NOFOLLOW, 0)) < 0) {
Jan F. Chadima 69dd72
+		rv = ERR_OPEN;
Jan F. Chadima 69dd72
+		goto fail;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if (fstat(fd, &st) < 0) {
Jan F. Chadima 69dd72
+		rv = ERR_STAT;
Jan F. Chadima 69dd72
+		goto fail;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if (!S_ISREG(st.st_mode) || 
Jan F. Chadima 69dd72
+		(st.st_uid != pwd->pw_uid && st.st_uid != 0)) {
Jan F. Chadima 69dd72
+		rv = ERR_FILE_MODE;
Jan F. Chadima 69dd72
+		goto fail;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	unset_nonblock(fd);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if ((f = fdopen(fd, "r")) == NULL) {
Jan F. Chadima 69dd72
+		rv = ERR_FDOPEN;
Jan F. Chadima 69dd72
+		goto fail;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	fd = -1;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
Jan F. Chadima 69dd72
+		rv = fwrite(buf, 1, len, stdout) != len ? ERR_WRITE : 0;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+fail:
Jan F. Chadima 69dd72
+	if (fd != -1)
Jan F. Chadima 69dd72
+		close(fd);
Jan F. Chadima 69dd72
+	if (f != NULL)
Jan F. Chadima 69dd72
+		fclose(f);
Jan F. Chadima 69dd72
+	free(fname);
Jan F. Chadima 69dd72
+	restore_uid();
Jan F. Chadima 69dd72
+	return rv;
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+static const char *env_names[] = { "SELINUX_ROLE_REQUESTED",
Jan F. Chadima 69dd72
+	"SELINUX_LEVEL_REQUESTED",
Jan F. Chadima 69dd72
+	"SELINUX_USE_CURRENT_RANGE"
Jan F. Chadima 69dd72
+};
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+extern char **environ;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+int
Jan F. Chadima 69dd72
+set_pam_environment(pam_handle_t *pamh)
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	int i;
Jan F. Chadima 69dd72
+	size_t j;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	for (j = 0; j < sizeof(env_names)/sizeof(env_names[0]); ++j) {
Jan F. Chadima 69dd72
+		int len = strlen(env_names[j]);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+		for (i = 0; environ[i] != NULL; ++i) {
Jan F. Chadima 69dd72
+			if (strncmp(env_names[j], environ[i], len) == 0 &&
Jan F. Chadima 69dd72
+			    environ[i][len] == '=') {
Jan F. Chadima 69dd72
+				if (pam_putenv(pamh, environ[i]) != PAM_SUCCESS)
Jan F. Chadima 69dd72
+					return ERR_PAM_PUTENV;
Jan F. Chadima 69dd72
+			}
Jan F. Chadima 69dd72
+		}
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	return 0;
Jan F. Chadima 69dd72
+}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+int
Jan F. Chadima 69dd72
+main(int argc, char *argv[])
Jan F. Chadima 69dd72
+{
Jan F. Chadima 69dd72
+	pam_handle_t *pamh = NULL;
Jan F. Chadima 69dd72
+	int retval;
Jan F. Chadima 69dd72
+	int ev = 0;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	if (argc != 2) {
Jan F. Chadima 69dd72
+		fprintf(stderr, "Usage: %s <user-name>\n", argv[0]);
Jan F. Chadima 69dd72
+		return ERR_USAGE;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	retval = pam_start("ssh-keycat", argv[1], &conv, &pamh);
Jan F. Chadima 69dd72
+	if (retval != PAM_SUCCESS) {
Jan F. Chadima 69dd72
+		return ERR_PAM_START;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	ev = set_pam_environment(pamh);
Jan F. Chadima 69dd72
+	if (ev != 0)
Jan F. Chadima 69dd72
+		goto finish;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	retval = pam_open_session(pamh, PAM_SILENT);
Jan F. Chadima 69dd72
+	if (retval != PAM_SUCCESS) {
Jan F. Chadima 69dd72
+		ev = ERR_OPEN_SESSION;
Jan F. Chadima 69dd72
+		goto finish;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	ev = dump_keys(argv[1]);
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	retval = pam_close_session(pamh, PAM_SILENT);
Jan F. Chadima 69dd72
+	if (retval != PAM_SUCCESS) {
Jan F. Chadima 69dd72
+		ev = ERR_CLOSE_SESSION;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+finish:
Jan F. Chadima 69dd72
+	retval = pam_end (pamh,retval);
Jan F. Chadima 69dd72
+	if (retval != PAM_SUCCESS) {
Jan F. Chadima 69dd72
+		ev = ERR_PAM_END;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+	return ev;
Jan F. Chadima 69dd72
+}