kentpeacock / rpms / openssh

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