bmh10 / rpms / openssh

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