5cd882e
diff -up openssh-6.2p1/gss-serv-krb5.c.force_krb openssh-6.2p1/gss-serv-krb5.c
5cd882e
--- openssh-6.2p1/gss-serv-krb5.c.force_krb	2013-03-25 20:04:53.807817333 +0100
5cd882e
+++ openssh-6.2p1/gss-serv-krb5.c	2013-03-25 20:04:53.818817403 +0100
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"
99254d5
@@ -40,12 +42,11 @@
99254d5
 #include "auth.h"
99254d5
 #include "log.h"
99254d5
 #include "servconf.h"
99254d5
+#include "misc.h"
99254d5
 
99254d5
 #include "buffer.h"
99254d5
 #include "ssh-gss.h"
99254d5
 
99254d5
-extern ServerOptions options;
99254d5
-
99254d5
 #ifdef HEIMDAL
99254d5
 # include <krb5.h>
99254d5
 #else
99254d5
@@ -56,6 +57,16 @@ extern ServerOptions options;
99254d5
 # endif
99254d5
 #endif
99254d5
 
99254d5
+extern Authctxt *the_authctxt;
99254d5
+extern ServerOptions options;
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 */
99254d5
@@ -83,10 +94,11 @@ ssh_gssapi_krb5_init(void)
99254d5
  */
99254d5
 
99254d5
 static int
99254d5
-ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
99254d5
+ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *luser)
99254d5
 {
99254d5
 	krb5_principal princ;
99254d5
 	int retval;
99254d5
+	int k5login_exists;
99254d5
 
99254d5
 	if (ssh_gssapi_krb5_init() == 0)
99254d5
 		return 0;
99254d5
@@ -97,10 +109,22 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client
99254d5
 		    krb5_get_err_text(krb_context, retval));
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. */
99254d5
+	if (krb5_kuserok(krb_context, princ, luser) && k5login_exists) {
99254d5
 		retval = 1;
99254d5
 		logit("Authorized to %s, krb5 principal %s (krb5_kuserok)",
99254d5
-		    name, (char *)client->displayname.value);
99254d5
+		    luser, (char *)client->displayname.value);
99254d5
+	} else if (ssh_gssapi_krb5_cmdok(princ, client->exportedname.value,
99254d5
+		luser, k5login_exists)) {
99254d5
+		retval = 1;
99254d5
+		logit("Authorized to %s, krb5 principal %s "
99254d5
+		    "(ssh_gssapi_krb5_cmdok)",
99254d5
+		    luser, (char *)client->displayname.value);
99254d5
 	} else
99254d5
 		retval = 0;
99254d5
 
9dd0663
@@ -108,6 +132,135 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client
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];
99254d5
+	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 =
99254d5
+				    xrealloc(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 =
99254d5
+				    xrealloc(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 */
5cd882e
diff -up openssh-6.2p1/session.c.force_krb openssh-6.2p1/session.c
5cd882e
--- openssh-6.2p1/session.c.force_krb	2013-03-25 20:04:53.724816810 +0100
5cd882e
+++ openssh-6.2p1/session.c	2013-03-25 20:04:53.818817403 +0100
5cd882e
@@ -823,6 +823,29 @@ do_exec(Session *s, const char *command)
99254d5
 		debug("Forced command (key option) '%.900s'", command);
99254d5
 	}
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;
99254d5
+ 
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
+
99254d5
 #ifdef SSH_AUDIT_EVENTS
99254d5
 	if (s->command != NULL || s->command_handle != -1)
99254d5
 		fatal("do_exec: command already set");
5cd882e
diff -up openssh-6.2p1/sshd.8.force_krb openssh-6.2p1/sshd.8
5cd882e
--- openssh-6.2p1/sshd.8.force_krb	2013-03-25 20:04:53.787817207 +0100
5cd882e
+++ openssh-6.2p1/sshd.8	2013-03-25 20:04:53.819817409 +0100
9dd0663
@@ -323,6 +323,7 @@ Finally, the server and the client enter
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
9dd0663
@@ -796,6 +797,12 @@ This file is used in exactly the same wa
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.
5cd882e
diff -up openssh-6.2p1/ssh-gss.h.force_krb openssh-6.2p1/ssh-gss.h
5cd882e
--- openssh-6.2p1/ssh-gss.h.force_krb	2013-03-25 20:04:53.819817409 +0100
5cd882e
+++ openssh-6.2p1/ssh-gss.h	2013-03-25 20:05:26.463023197 +0100
5cd882e
@@ -49,6 +49,10 @@
5cd882e
 #  endif /* !HAVE_DECL_GSS_C_NT_... */
5cd882e
 
5cd882e
 # endif /* !HEIMDAL */
99254d5
+
99254d5
+/* .k5users support */
99254d5
+extern char **k5users_allowed_cmds;
99254d5
+
99254d5
 #endif /* KRB5 */
99254d5
 
99254d5
 /* draft-ietf-secsh-gsskeyex-06 */