vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jan F 5b4ccb
diff -up openssh-5.8p2/gss-serv-krb5.c.force_krb openssh-5.8p2/gss-serv-krb5.c
Jan F 5b4ccb
--- openssh-5.8p2/gss-serv-krb5.c.force_krb	2006-09-01 07:38:36.000000000 +0200
Jan F 5b4ccb
+++ openssh-5.8p2/gss-serv-krb5.c	2011-05-19 03:41:45.801109545 +0200
Jan F 5b4ccb
@@ -32,7 +32,9 @@
Jan F 5b4ccb
 #include <sys/types.h>
Jan F 5b4ccb
 
Jan F 5b4ccb
 #include <stdarg.h>
Jan F 5b4ccb
+#include <stdio.h>
Jan F 5b4ccb
 #include <string.h>
Jan F 5b4ccb
+#include <unistd.h>
Jan F 5b4ccb
 
Jan F 5b4ccb
 #include "xmalloc.h"
Jan F 5b4ccb
 #include "key.h"
Jan F 5b4ccb
@@ -40,12 +42,11 @@
Jan F 5b4ccb
 #include "auth.h"
Jan F 5b4ccb
 #include "log.h"
Jan F 5b4ccb
 #include "servconf.h"
Jan F 5b4ccb
+#include "misc.h"
Jan F 5b4ccb
 
Jan F 5b4ccb
 #include "buffer.h"
Jan F 5b4ccb
 #include "ssh-gss.h"
Jan F 5b4ccb
 
Jan F 5b4ccb
-extern ServerOptions options;
Jan F 5b4ccb
-
Jan F 5b4ccb
 #ifdef HEIMDAL
Jan F 5b4ccb
 # include <krb5.h>
Jan F 5b4ccb
 #else
Jan F 5b4ccb
@@ -56,6 +57,16 @@ extern ServerOptions options;
Jan F 5b4ccb
 # endif
Jan F 5b4ccb
 #endif
Jan F 5b4ccb
 
Jan F 5b4ccb
+extern Authctxt *the_authctxt;
Jan F 5b4ccb
+extern ServerOptions options;
Jan F 5b4ccb
+
Jan F 5b4ccb
+/* all commands are allowed by default */
Jan F 5b4ccb
+char **k5users_allowed_cmds = NULL;
Jan F 5b4ccb
+
Jan F 5b4ccb
+static int ssh_gssapi_k5login_exists();
Jan F 5b4ccb
+static int ssh_gssapi_krb5_cmdok(krb5_principal, const char *, const char *,
Jan F 5b4ccb
+    int);
Jan F 5b4ccb
+
Jan F 5b4ccb
 static krb5_context krb_context = NULL;
Jan F 5b4ccb
 
Jan F 5b4ccb
 /* Initialise the krb5 library, for the stuff that GSSAPI won't do */
Jan F 5b4ccb
@@ -83,10 +94,11 @@ ssh_gssapi_krb5_init(void)
Jan F 5b4ccb
  */
Jan F 5b4ccb
 
Jan F 5b4ccb
 static int
Jan F 5b4ccb
-ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
Jan F 5b4ccb
+ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *luser)
Jan F 5b4ccb
 {
Jan F 5b4ccb
 	krb5_principal princ;
Jan F 5b4ccb
 	int retval;
Jan F 5b4ccb
+	int k5login_exists;
Jan F 5b4ccb
 
Jan F 5b4ccb
 	if (ssh_gssapi_krb5_init() == 0)
Jan F 5b4ccb
 		return 0;
Jan F 5b4ccb
@@ -97,10 +109,22 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client
Jan F 5b4ccb
 		    krb5_get_err_text(krb_context, retval));
Jan F 5b4ccb
 		return 0;
Jan F 5b4ccb
 	}
Jan F 5b4ccb
-	if (krb5_kuserok(krb_context, princ, name)) {
Jan F 5b4ccb
+	/* krb5_kuserok() returns 1 if .k5login DNE and this is self-login.
Jan F 5b4ccb
+	 * We have to make sure to check .k5users in that case. */
Jan F 5b4ccb
+	k5login_exists = ssh_gssapi_k5login_exists();
Jan F 5b4ccb
+	/* NOTE: .k5login and .k5users must opened as root, not the user,
Jan F 5b4ccb
+	 * because if they are on a krb5-protected filesystem, user credentials
Jan F 5b4ccb
+	 * to access these files aren't available yet. */
Jan F 5b4ccb
+	if (krb5_kuserok(krb_context, princ, luser) && k5login_exists) {
Jan F 5b4ccb
 		retval = 1;
Jan F 5b4ccb
 		logit("Authorized to %s, krb5 principal %s (krb5_kuserok)",
Jan F 5b4ccb
-		    name, (char *)client->displayname.value);
Jan F 5b4ccb
+		    luser, (char *)client->displayname.value);
Jan F 5b4ccb
+	} else if (ssh_gssapi_krb5_cmdok(princ, client->exportedname.value,
Jan F 5b4ccb
+		luser, k5login_exists)) {
Jan F 5b4ccb
+		retval = 1;
Jan F 5b4ccb
+		logit("Authorized to %s, krb5 principal %s "
Jan F 5b4ccb
+		    "(ssh_gssapi_krb5_cmdok)",
Jan F 5b4ccb
+		    luser, (char *)client->displayname.value);
Jan F 5b4ccb
 	} else
Jan F 5b4ccb
 		retval = 0;
Jan F 5b4ccb
 
Jan F 5b4ccb
@@ -108,6 +132,134 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client
Jan F 5b4ccb
 	return retval;
Jan F 5b4ccb
 }
Jan F 5b4ccb
 
Jan F 5b4ccb
+/* Test for existence of .k5login.
Jan F 5b4ccb
+ * We need this as part of our .k5users check, because krb5_kuserok()
Jan F 5b4ccb
+ * returns success if .k5login DNE and user is logging in as himself.
Jan F 5b4ccb
+ * With .k5login absent and .k5users present, we don't want absence
Jan F 5b4ccb
+ * of .k5login to authorize self-login.  (absence of both is required)
Jan F 5b4ccb
+ * Returns 1 if .k5login is available, 0 otherwise.
Jan F 5b4ccb
+ */
Jan F 5b4ccb
+static int
Jan F 5b4ccb
+ssh_gssapi_k5login_exists()
Jan F 5b4ccb
+{
Jan F 5b4ccb
+	char file[MAXPATHLEN];
Jan F 5b4ccb
+	struct passwd *pw = the_authctxt->pw;
Jan F 5b4ccb
+
Jan F 5b4ccb
+	snprintf(file, sizeof(file), "%s/.k5login", pw->pw_dir);
Jan F 5b4ccb
+	return access(file, F_OK) == 0;
Jan F 5b4ccb
+}
Jan F 5b4ccb
+
Jan F 5b4ccb
+/* check .k5users for login or command authorization
Jan F 5b4ccb
+ * Returns 1 if principal is authorized, 0 otherwise.
Jan F 5b4ccb
+ * If principal is authorized, (global) k5users_allowed_cmds may be populated.
Jan F 5b4ccb
+ */
Jan F 5b4ccb
+static int
Jan F 5b4ccb
+ssh_gssapi_krb5_cmdok(krb5_principal principal, const char *name,
Jan F 5b4ccb
+    const char *luser, int k5login_exists)
Jan F 5b4ccb
+{
Jan F 5b4ccb
+	FILE *fp;
Jan F 5b4ccb
+	char file[MAXPATHLEN];
Jan F 5b4ccb
+	char line[BUFSIZ];
Jan F 5b4ccb
+	char kuser[65]; /* match krb5_kuserok() */
Jan F 5b4ccb
+	struct stat st;
Jan F 5b4ccb
+	struct passwd *pw = the_authctxt->pw;
Jan F 5b4ccb
+	int found_principal = 0;
Jan F 5b4ccb
+	int ncommands = 0, allcommands = 0;
Jan F 5b4ccb
+	u_long linenum;
Jan F 5b4ccb
+
Jan F 5b4ccb
+	snprintf(file, sizeof(file), "%s/.k5users", pw->pw_dir);
Jan F 5b4ccb
+	/* If both .k5login and .k5users DNE, self-login is ok. */
Jan F 5b4ccb
+	if (!k5login_exists && (access(file, F_OK) == -1)) {
Jan F 5b4ccb
+		return (krb5_aname_to_localname(krb_context, principal,
Jan F 5b4ccb
+		    sizeof(kuser), kuser) == 0) &&
Jan F 5b4ccb
+		    (strcmp(kuser, luser) == 0);
Jan F 5b4ccb
+	}
Jan F 5b4ccb
+	if ((fp = fopen(file, "r")) == NULL) {
Jan F 5b4ccb
+		int saved_errno = errno;
Jan F 5b4ccb
+		/* 2nd access check to ease debugging if file perms are wrong.
Jan F 5b4ccb
+		 * But we don't want to report this if .k5users simply DNE. */
Jan F 5b4ccb
+		if (access(file, F_OK) == 0) {
Jan F 5b4ccb
+			logit("User %s fopen %s failed: %s",
Jan F 5b4ccb
+			    pw->pw_name, file, strerror(saved_errno));
Jan F 5b4ccb
+		}
Jan F 5b4ccb
+		return 0;
Jan F 5b4ccb
+	}
Jan F 5b4ccb
+	/* .k5users must be owned either by the user or by root */
Jan F 5b4ccb
+	if (fstat(fileno(fp), &st) == -1) {
Jan F 5b4ccb
+		/* can happen, but very wierd error so report it */
Jan F 5b4ccb
+		logit("User %s fstat %s failed: %s",
Jan F 5b4ccb
+		    pw->pw_name, file, strerror(errno));
Jan F 5b4ccb
+		fclose(fp);
Jan F 5b4ccb
+		return 0;
Jan F 5b4ccb
+	}
Jan F 5b4ccb
+	if (!(st.st_uid == pw->pw_uid || st.st_uid == 0)) {
Jan F 5b4ccb
+		logit("User %s %s is not owned by root or user",
Jan F 5b4ccb
+		    pw->pw_name, file);
Jan F 5b4ccb
+		fclose(fp);
Jan F 5b4ccb
+		return 0;
Jan F 5b4ccb
+	}
Jan F 5b4ccb
+	/* .k5users must be a regular file.  krb5_kuserok() doesn't do this
Jan F 5b4ccb
+	  * check, but we don't want to be deficient if they add a check. */
Jan F 5b4ccb
+	if (!S_ISREG(st.st_mode)) {
Jan F 5b4ccb
+		logit("User %s %s is not a regular file", pw->pw_name, file);
Jan F 5b4ccb
+		fclose(fp);
Jan F 5b4ccb
+		return 0;
Jan F 5b4ccb
+	}
Jan F 5b4ccb
+	/* file exists; initialize k5users_allowed_cmds (to none!) */
Jan F 5b4ccb
+	k5users_allowed_cmds = xcalloc(++ncommands,
Jan F 5b4ccb
+	    sizeof(*k5users_allowed_cmds));
Jan F 5b4ccb
+
Jan F 5b4ccb
+	/* Check each line.  ksu allows unlimited length lines.  We don't. */
Jan F 5b4ccb
+	while (!allcommands && read_keyfile_line(fp, file, line, sizeof(line),
Jan F 5b4ccb
+	    &linenum) != -1) {
Jan F 5b4ccb
+		char *token;
Jan F 5b4ccb
+
Jan F 5b4ccb
+		/* we parse just like ksu, even though we could do better */
Jan F 5b4ccb
+		token = strtok(line, " \t\n");
Jan F 5b4ccb
+		if (strcmp(name, token) == 0) {
Jan F 5b4ccb
+			/* we matched on client principal */
Jan F 5b4ccb
+			found_principal = 1;
Jan F 5b4ccb
+			if ((token = strtok(NULL, " \t\n")) == NULL) {
Jan F 5b4ccb
+				/* only shell is allowed */
Jan F 5b4ccb
+				k5users_allowed_cmds[ncommands-1] =
Jan F 5b4ccb
+				    xstrdup(pw->pw_shell);
Jan F 5b4ccb
+				k5users_allowed_cmds =
Jan F 5b4ccb
+				    xrealloc(k5users_allowed_cmds, ++ncommands,
Jan F 5b4ccb
+					sizeof(*k5users_allowed_cmds));
Jan F 5b4ccb
+				break;
Jan F 5b4ccb
+			}
Jan F 5b4ccb
+			/* process the allowed commands */
Jan F 5b4ccb
+			while (token) {
Jan F 5b4ccb
+				if (strcmp(token, "*") == 0) {
Jan F 5b4ccb
+					allcommands = 1;
Jan F 5b4ccb
+					break;
Jan F 5b4ccb
+				}
Jan F 5b4ccb
+				k5users_allowed_cmds[ncommands-1] =
Jan F 5b4ccb
+				    xstrdup(token);
Jan F 5b4ccb
+				k5users_allowed_cmds =
Jan F 5b4ccb
+				    xrealloc(k5users_allowed_cmds, ++ncommands,
Jan F 5b4ccb
+					sizeof(*k5users_allowed_cmds));
Jan F 5b4ccb
+				token = strtok(NULL, " \t\n");
Jan F 5b4ccb
+			}
Jan F 5b4ccb
+		}
Jan F 5b4ccb
+       }
Jan F 5b4ccb
+	if (k5users_allowed_cmds) {
Jan F 5b4ccb
+		/* terminate vector */
Jan F 5b4ccb
+		k5users_allowed_cmds[ncommands-1] = NULL;
Jan F 5b4ccb
+		/* if all commands are allowed, free vector */
Jan F 5b4ccb
+		if (allcommands) {
Jan F 5b4ccb
+			int i;
Jan F 5b4ccb
+			for (i = 0; i < ncommands; i++) {
Jan F 5b4ccb
+				free(k5users_allowed_cmds[i]);
Jan F 5b4ccb
+			}
Jan F 5b4ccb
+			free(k5users_allowed_cmds);
Jan F 5b4ccb
+			k5users_allowed_cmds = NULL;
Jan F 5b4ccb
+		}
Jan F 5b4ccb
+	}
Jan F 5b4ccb
+	fclose(fp);
Jan F 5b4ccb
+	return found_principal;
Jan F 5b4ccb
+}
Jan F 5b4ccb
+ 
Jan F 5b4ccb
 
Jan F 5b4ccb
 /* This writes out any forwarded credentials from the structure populated
Jan F 5b4ccb
  * during userauth. Called after we have setuid to the user */
Jan F 5b4ccb
diff -up openssh-5.8p2/session.c.force_krb openssh-5.8p2/session.c
Jan F 5b4ccb
--- openssh-5.8p2/session.c.force_krb	2011-05-19 03:41:41.000000000 +0200
Jan F 5b4ccb
+++ openssh-5.8p2/session.c	2011-05-19 03:43:32.437173662 +0200
Jan F 5b4ccb
@@ -816,6 +816,29 @@ do_exec(Session *s, const char *command)
Jan F 5b4ccb
 		debug("Forced command (key option) '%.900s'", command);
Jan F 5b4ccb
 	}
Jan F 5b4ccb
 
Jan F 5b4ccb
+#ifdef GSSAPI
Jan F 5b4ccb
+#ifdef KRB5 /* k5users_allowed_cmds only available w/ GSSAPI+KRB5 */
Jan F 5b4ccb
+	else if (k5users_allowed_cmds) {
Jan F 5b4ccb
+		const char *match = command;
Jan F 5b4ccb
+		int allowed = 0, i = 0;
Jan F 5b4ccb
+ 
Jan F 5b4ccb
+		if (!match)
Jan F 5b4ccb
+			match = s->pw->pw_shell;
Jan F 5b4ccb
+		while (k5users_allowed_cmds[i]) {
Jan F 5b4ccb
+			if (strcmp(match, k5users_allowed_cmds[i++]) == 0) {
Jan F 5b4ccb
+				debug("Allowed command '%.900s'", match);
Jan F 5b4ccb
+				allowed = 1;
Jan F 5b4ccb
+				break;
Jan F 5b4ccb
+			}
Jan F 5b4ccb
+		}
Jan F 5b4ccb
+		if (!allowed) {
Jan F 5b4ccb
+			debug("command '%.900s' not allowed", match);
Jan F 5b4ccb
+			return 1;
Jan F 5b4ccb
+		}
Jan F 5b4ccb
+	}
Jan F 5b4ccb
+#endif
Jan F 5b4ccb
+#endif
Jan F 5b4ccb
+
Jan F 5b4ccb
 #ifdef SSH_AUDIT_EVENTS
Jan F 5b4ccb
 	if (s->command != NULL || s->command_handle != -1)
Jan F 5b4ccb
 		fatal("do_exec: command already set");
Jan F 5b4ccb
diff -up openssh-5.8p2/sshd.8.force_krb openssh-5.8p2/sshd.8
Jan F 5b4ccb
--- openssh-5.8p2/sshd.8.force_krb	2011-05-19 03:41:30.582114401 +0200
Jan F 5b4ccb
+++ openssh-5.8p2/sshd.8	2011-05-19 03:41:46.159106308 +0200
Jan F 5b4ccb
@@ -320,6 +320,7 @@ Finally, the server and the client enter
Jan F 5b4ccb
 The client tries to authenticate itself using
Jan F 5b4ccb
 host-based authentication,
Jan F 5b4ccb
 public key authentication,
Jan F 5b4ccb
+GSSAPI authentication,
Jan F 5b4ccb
 challenge-response authentication,
Jan F 5b4ccb
 or password authentication.
Jan F 5b4ccb
 .Pp
Jan F 5b4ccb
@@ -788,6 +789,12 @@ This file is used in exactly the same wa
Jan F 5b4ccb
 but allows host-based authentication without permitting login with
Jan F 5b4ccb
 rlogin/rsh.
Jan F 5b4ccb
 .Pp
Jan F 5b4ccb
+.It Pa ~/.k5login
Jan F 5b4ccb
+.It Pa ~/.k5users
Jan F 5b4ccb
+These files enforce GSSAPI/Kerberos authentication access control.
Jan F 5b4ccb
+Further details are described in
Jan F 5b4ccb
+.Xr ksu 1 .
Jan F 5b4ccb
+.Pp
Jan F 5b4ccb
 .It Pa ~/.ssh/
Jan F 5b4ccb
 This directory is the default location for all user-specific configuration
Jan F 5b4ccb
 and authentication information.
Jan F 5b4ccb
diff -up openssh-5.8p2/ssh-gss.h.force_krb openssh-5.8p2/ssh-gss.h
Jan F 5b4ccb
--- openssh-5.8p2/ssh-gss.h.force_krb	2007-06-12 15:40:39.000000000 +0200
Jan F 5b4ccb
+++ openssh-5.8p2/ssh-gss.h	2011-05-19 03:41:46.302234118 +0200
Jan F 5b4ccb
@@ -48,6 +48,10 @@
Jan F 5b4ccb
 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
Jan F 5b4ccb
 #endif /* GSS_C_NT_... */
Jan F 5b4ccb
 #endif /* !HEIMDAL */
Jan F 5b4ccb
+
Jan F 5b4ccb
+/* .k5users support */
Jan F 5b4ccb
+extern char **k5users_allowed_cmds;
Jan F 5b4ccb
+
Jan F 5b4ccb
 #endif /* KRB5 */
Jan F 5b4ccb
 
Jan F 5b4ccb
 /* draft-ietf-secsh-gsskeyex-06 */