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