rcolebaugh / rpms / openssh

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