rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
8f2528
diff --git a/gss-serv-krb5.c b/gss-serv-krb5.c
8f2528
index 42de994..60de320 100644
8f2528
--- a/gss-serv-krb5.c
8f2528
+++ b/gss-serv-krb5.c
8f2528
@@ -32,7 +32,9 @@
8f2528
 #include <sys/types.h>
8f2528
 
8f2528
 #include <stdarg.h>
8f2528
+#include <stdio.h>
8f2528
 #include <string.h>
8f2528
+#include <unistd.h>
8f2528
 
8f2528
 #include "xmalloc.h"
8f2528
 #include "key.h"
8f2528
@@ -40,6 +42,7 @@
8f2528
 #include "buffer.h"
8f2528
 #include "ssh-gss.h"
8f2528
 
8f2528
+extern Authctxt *the_authctxt;
8f2528
 extern ServerOptions options;
8f2528
 
8f2528
 #ifdef HEIMDAL
8f2528
@@ -55,6 +59,13 @@ extern ServerOptions options;
8f2528
 # include <gssapi/gssapi_krb5.h>
8f2528
 #endif
8f2528
 
8f2528
+/* all commands are allowed by default */
8f2528
+char **k5users_allowed_cmds = NULL;
8f2528
+
8f2528
+static int ssh_gssapi_k5login_exists();
8f2528
+static int ssh_gssapi_krb5_cmdok(krb5_principal, const char *, const char *,
8f2528
+    int);
8f2528
+
8f2528
 static krb5_context krb_context = NULL;
8f2528
 
8f2528
 /* Initialise the krb5 library, for the stuff that GSSAPI won't do */
8f2528
@@ -87,6 +98,7 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
8f2528
 	krb5_principal princ;
8f2528
 	int retval;
8f2528
 	const char *errmsg;
8f2528
+	int k5login_exists;
8f2528
 
8f2528
 	if (ssh_gssapi_krb5_init() == 0)
8f2528
 		return 0;
8f2528
@@ -98,10 +110,22 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
8f2528
 		krb5_free_error_message(krb_context, errmsg);
8f2528
 		return 0;
8f2528
 	}
8f2528
-	if (krb5_kuserok(krb_context, princ, name)) {
8f2528
+	/* krb5_kuserok() returns 1 if .k5login DNE and this is self-login.
8f2528
+	 * We have to make sure to check .k5users in that case. */
8f2528
+	k5login_exists = ssh_gssapi_k5login_exists();
8f2528
+	/* NOTE: .k5login and .k5users must opened as root, not the user,
8f2528
+	 * because if they are on a krb5-protected filesystem, user credentials
8f2528
+	 * to access these files aren't available yet. */
8f2528
+	if (krb5_kuserok(krb_context, princ, name) && k5login_exists) {
8f2528
 		retval = 1;
8f2528
 		logit("Authorized to %s, krb5 principal %s (krb5_kuserok)",
8f2528
 		    name, (char *)client->displayname.value);
8f2528
+	} else if (ssh_gssapi_krb5_cmdok(princ, client->exportedname.value,
8f2528
+		name, k5login_exists)) {
8f2528
+		retval = 1;
8f2528
+		logit("Authorized to %s, krb5 principal %s "
8f2528
+		    "(ssh_gssapi_krb5_cmdok)",
8f2528
+		    name, (char *)client->displayname.value);
8f2528
 	} else
8f2528
 		retval = 0;
8f2528
 
8f2528
@@ -109,6 +133,135 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
8f2528
 	return retval;
8f2528
 }
8f2528
 
8f2528
+/* Test for existence of .k5login.
8f2528
+ * We need this as part of our .k5users check, because krb5_kuserok()
8f2528
+ * returns success if .k5login DNE and user is logging in as himself.
8f2528
+ * With .k5login absent and .k5users present, we don't want absence
8f2528
+ * of .k5login to authorize self-login.  (absence of both is required)
8f2528
+ * Returns 1 if .k5login is available, 0 otherwise.
8f2528
+ */
8f2528
+static int
8f2528
+ssh_gssapi_k5login_exists()
8f2528
+{
8f2528
+	char file[MAXPATHLEN];
8f2528
+	struct passwd *pw = the_authctxt->pw;
8f2528
+
8f2528
+	snprintf(file, sizeof(file), "%s/.k5login", pw->pw_dir);
8f2528
+	return access(file, F_OK) == 0;
8f2528
+}
8f2528
+
8f2528
+/* check .k5users for login or command authorization
8f2528
+ * Returns 1 if principal is authorized, 0 otherwise.
8f2528
+ * If principal is authorized, (global) k5users_allowed_cmds may be populated.
8f2528
+ */
8f2528
+static int
8f2528
+ssh_gssapi_krb5_cmdok(krb5_principal principal, const char *name,
8f2528
+    const char *luser, int k5login_exists)
8f2528
+{
8f2528
+	FILE *fp;
8f2528
+	char file[MAXPATHLEN];
8f2528
+	char line[BUFSIZ];
8f2528
+	char kuser[65]; /* match krb5_kuserok() */
8f2528
+	struct stat st;
8f2528
+	struct passwd *pw = the_authctxt->pw;
8f2528
+	int found_principal = 0;
8f2528
+	int ncommands = 0, allcommands = 0;
8f2528
+	u_long linenum;
8f2528
+
8f2528
+	snprintf(file, sizeof(file), "%s/.k5users", pw->pw_dir);
8f2528
+	/* If both .k5login and .k5users DNE, self-login is ok. */
8f2528
+	if (!k5login_exists && (access(file, F_OK) == -1)) {
8f2528
+		return (krb5_aname_to_localname(krb_context, principal,
8f2528
+		    sizeof(kuser), kuser) == 0) &&
8f2528
+		    (strcmp(kuser, luser) == 0);
8f2528
+	}
8f2528
+	if ((fp = fopen(file, "r")) == NULL) {
8f2528
+		int saved_errno = errno;
8f2528
+		/* 2nd access check to ease debugging if file perms are wrong.
8f2528
+		 * But we don't want to report this if .k5users simply DNE. */
8f2528
+		if (access(file, F_OK) == 0) {
8f2528
+			logit("User %s fopen %s failed: %s",
8f2528
+			    pw->pw_name, file, strerror(saved_errno));
8f2528
+		}
8f2528
+		return 0;
8f2528
+	}
8f2528
+	/* .k5users must be owned either by the user or by root */
8f2528
+	if (fstat(fileno(fp), &st) == -1) {
8f2528
+		/* can happen, but very wierd error so report it */
8f2528
+		logit("User %s fstat %s failed: %s",
8f2528
+		    pw->pw_name, file, strerror(errno));
8f2528
+		fclose(fp);
8f2528
+		return 0;
8f2528
+	}
8f2528
+	if (!(st.st_uid == pw->pw_uid || st.st_uid == 0)) {
8f2528
+		logit("User %s %s is not owned by root or user",
8f2528
+		    pw->pw_name, file);
8f2528
+		fclose(fp);
8f2528
+		return 0;
8f2528
+	}
8f2528
+	/* .k5users must be a regular file.  krb5_kuserok() doesn't do this
8f2528
+	  * check, but we don't want to be deficient if they add a check. */
8f2528
+	if (!S_ISREG(st.st_mode)) {
8f2528
+		logit("User %s %s is not a regular file", pw->pw_name, file);
8f2528
+		fclose(fp);
8f2528
+		return 0;
8f2528
+	}
8f2528
+	/* file exists; initialize k5users_allowed_cmds (to none!) */
8f2528
+	k5users_allowed_cmds = xcalloc(++ncommands,
8f2528
+	    sizeof(*k5users_allowed_cmds));
8f2528
+
8f2528
+	/* Check each line.  ksu allows unlimited length lines.  We don't. */
8f2528
+	while (!allcommands && read_keyfile_line(fp, file, line, sizeof(line),
8f2528
+	    &linenum) != -1) {
8f2528
+		char *token;
8f2528
+
8f2528
+		/* we parse just like ksu, even though we could do better */
8f2528
+		if ((token = strtok(line, " \t\n")) == NULL)
8f2528
+			continue;
8f2528
+		if (strcmp(name, token) == 0) {
8f2528
+			/* we matched on client principal */
8f2528
+			found_principal = 1;
8f2528
+			if ((token = strtok(NULL, " \t\n")) == NULL) {
8f2528
+				/* only shell is allowed */
8f2528
+				k5users_allowed_cmds[ncommands-1] =
8f2528
+				    xstrdup(pw->pw_shell);
8f2528
+				k5users_allowed_cmds =
8f2528
+				    xreallocarray(k5users_allowed_cmds, ++ncommands,
8f2528
+					sizeof(*k5users_allowed_cmds));
8f2528
+				break;
8f2528
+			}
8f2528
+			/* process the allowed commands */
8f2528
+			while (token) {
8f2528
+				if (strcmp(token, "*") == 0) {
8f2528
+					allcommands = 1;
8f2528
+					break;
8f2528
+				}
8f2528
+				k5users_allowed_cmds[ncommands-1] =
8f2528
+				    xstrdup(token);
8f2528
+				k5users_allowed_cmds =
8f2528
+				    xreallocarray(k5users_allowed_cmds, ++ncommands,
8f2528
+					sizeof(*k5users_allowed_cmds));
8f2528
+				token = strtok(NULL, " \t\n");
8f2528
+			}
8f2528
+		}
8f2528
+       }
8f2528
+	if (k5users_allowed_cmds) {
8f2528
+		/* terminate vector */
8f2528
+		k5users_allowed_cmds[ncommands-1] = NULL;
8f2528
+		/* if all commands are allowed, free vector */
8f2528
+		if (allcommands) {
8f2528
+			int i;
8f2528
+			for (i = 0; i < ncommands; i++) {
8f2528
+				free(k5users_allowed_cmds[i]);
8f2528
+			}
8f2528
+			free(k5users_allowed_cmds);
8f2528
+			k5users_allowed_cmds = NULL;
8f2528
+		}
8f2528
+	}
8f2528
+	fclose(fp);
8f2528
+	return found_principal;
8f2528
+}
8f2528
+ 
8f2528
 
8f2528
 /* This writes out any forwarded credentials from the structure populated
8f2528
  * during userauth. Called after we have setuid to the user */
8f2528
diff --git a/session.c b/session.c
8f2528
index b5dc144..ba4589b 100644
8f2528
--- a/session.c
8f2528
+++ b/session.c
8f2528
@@ -806,6 +806,29 @@ do_exec(Session *s, const char *command)
8f2528
 		command = forced_command;
8f2528
 		forced = "(key-option)";
8f2528
 	}
8f2528
+#ifdef GSSAPI
8f2528
+#ifdef KRB5 /* k5users_allowed_cmds only available w/ GSSAPI+KRB5 */
8f2528
+	else if (k5users_allowed_cmds) {
8f2528
+		const char *match = command;
8f2528
+		int allowed = 0, i = 0;
8f2528
+
8f2528
+		if (!match)
8f2528
+			match = s->pw->pw_shell;
8f2528
+		while (k5users_allowed_cmds[i]) {
8f2528
+			if (strcmp(match, k5users_allowed_cmds[i++]) == 0) {
8f2528
+				debug("Allowed command '%.900s'", match);
8f2528
+				allowed = 1;
8f2528
+				break;
8f2528
+			}
8f2528
+		}
8f2528
+		if (!allowed) {
8f2528
+			debug("command '%.900s' not allowed", match);
8f2528
+			return 1;
8f2528
+		}
8f2528
+	}
8f2528
+#endif
8f2528
+#endif
8f2528
+
8f2528
 	if (forced != NULL) {
8f2528
 		if (IS_INTERNAL_SFTP(command)) {
8f2528
 			s->is_subsystem = s->is_subsystem ?
8f2528
diff --git a/ssh-gss.h b/ssh-gss.h
8f2528
index 0374c88..509109a 100644
8f2528
--- a/ssh-gss.h
8f2528
+++ b/ssh-gss.h
8f2528
@@ -49,6 +49,10 @@
8f2528
 #  endif /* !HAVE_DECL_GSS_C_NT_... */
8f2528
 
8f2528
 # endif /* !HEIMDAL */
8f2528
+
8f2528
+/* .k5users support */
8f2528
+extern char **k5users_allowed_cmds;
8f2528
+
8f2528
 #endif /* KRB5 */
8f2528
 
8f2528
 /* draft-ietf-secsh-gsskeyex-06 */
8f2528
diff --git a/sshd.8 b/sshd.8
8f2528
index 058d37a..5c4f15b 100644
8f2528
--- a/sshd.8
8f2528
+++ b/sshd.8
8f2528
@@ -327,6 +327,7 @@ Finally, the server and the client enter an authentication dialog.
8f2528
 The client tries to authenticate itself using
8f2528
 host-based authentication,
8f2528
 public key authentication,
8f2528
+GSSAPI authentication,
8f2528
 challenge-response authentication,
8f2528
 or password authentication.
8f2528
 .Pp
8f2528
@@ -800,6 +801,12 @@ This file is used in exactly the same way as
8f2528
 but allows host-based authentication without permitting login with
8f2528
 rlogin/rsh.
8f2528
 .Pp
8f2528
+.It Pa ~/.k5login
8f2528
+.It Pa ~/.k5users
8f2528
+These files enforce GSSAPI/Kerberos authentication access control.
8f2528
+Further details are described in
8f2528
+.Xr ksu 1 .
8f2528
+.Pp
8f2528
 .It Pa ~/.ssh/
8f2528
 This directory is the default location for all user-specific configuration
8f2528
 and authentication information.