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