kentpeacock / rpms / openssh

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