rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
017ff1
diff --git a/HOWTO.ssh-keycat b/HOWTO.ssh-keycat
017ff1
new file mode 100644
017ff1
index 0000000..630ec62
017ff1
--- /dev/null
017ff1
+++ b/HOWTO.ssh-keycat
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
+
017ff1
diff --git a/Makefile.in b/Makefile.in
017ff1
index 411eadb..4ab6717 100644
017ff1
--- a/Makefile.in
017ff1
+++ b/Makefile.in
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 \
017ff1
@@ -176,6 +177,9 @@ ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11
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
 
017ff1
+ssh-keycat$(EXEEXT): $(LIBCOMPAT) $(SSHDOBJS) libssh.a ssh-keycat.o
017ff1
+	$(LD) -o $@ ssh-keycat.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(SSHLIBS)
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
 
017ff1
@@ -283,6 +287,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
017ff1
diff --git a/auth2-pubkey.c b/auth2-pubkey.c
017ff1
index c0ae0d4..cb0f931 100644
017ff1
--- a/auth2-pubkey.c
017ff1
+++ b/auth2-pubkey.c
017ff1
@@ -600,6 +600,14 @@ user_key_command_allowed2(struct passwd *user_pw, Key *key)
f09e2e
 			_exit(1);
f09e2e
 		}
f09e2e
 
f09e2e
+#ifdef WITH_SELINUX
017ff1
+		if (sshd_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
 
017ff1
diff --git a/openbsd-compat/port-linux-sshd.c b/openbsd-compat/port-linux-sshd.c
017ff1
index d04f4ed..0077dd7 100644
017ff1
--- a/openbsd-compat/port-linux-sshd.c
017ff1
+++ b/openbsd-compat/port-linux-sshd.c
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)
017ff1
@@ -343,7 +375,7 @@ sshd_selinux_setup_exec_context(char *pwname)
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) {
017ff1
diff --git a/openbsd-compat/port-linux.h b/openbsd-compat/port-linux.h
017ff1
index b18893c..cb51f99 100644
017ff1
--- a/openbsd-compat/port-linux.h
017ff1
+++ b/openbsd-compat/port-linux.h
017ff1
@@ -25,8 +25,10 @@ void ssh_selinux_setup_pty(char *, const char *);
017ff1
 void ssh_selinux_change_context(const char *);
017ff1
 void ssh_selinux_setfscreatecon(const char *);
017ff1
 
017ff1
+int sshd_selinux_enabled(void);
017ff1
 void sshd_selinux_copy_context(void);
017ff1
 void sshd_selinux_setup_exec_context(char *);
017ff1
+int sshd_selinux_setup_env_variables(void);
017ff1
 #endif
017ff1
 
017ff1
 #ifdef LINUX_OOM_ADJUST
017ff1
diff --git a/platform.c b/platform.c
017ff1
index 0d39ab2..0dae387 100644
017ff1
--- a/platform.c
017ff1
+++ b/platform.c
017ff1
@@ -102,7 +102,7 @@ platform_setusercontext(struct passwd *pw)
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
017ff1
diff --git a/ssh-keycat.c b/ssh-keycat.c
017ff1
new file mode 100644
017ff1
index 0000000..f8ed7af
017ff1
--- /dev/null
017ff1
+++ b/ssh-keycat.c
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
+}