kentpeacock / rpms / openssh

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