kentpeacock / rpms / openssh

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