rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
3e8b5b
diff -up openssh-7.4p1/auth-krb5.c.kuserok openssh-7.4p1/auth-krb5.c
3e8b5b
--- openssh-7.4p1/auth-krb5.c.kuserok	2016-12-23 14:36:07.640465939 +0100
3e8b5b
+++ openssh-7.4p1/auth-krb5.c	2016-12-23 14:36:07.644465936 +0100
3e8b5b
@@ -56,6 +56,21 @@
3e8b5b
 
3e8b5b
 extern ServerOptions	 options;
3e8b5b
 
3e8b5b
+int
3e8b5b
+ssh_krb5_kuserok(krb5_context krb5_ctx, krb5_principal krb5_user, const char *client,
3e8b5b
+                 int k5login_exists)
3e8b5b
+{
3e8b5b
+	if (options.use_kuserok || !k5login_exists)
3e8b5b
+		return krb5_kuserok(krb5_ctx, krb5_user, client);
3e8b5b
+	else {
3e8b5b
+		char kuser[65];
3e8b5b
+
3e8b5b
+		if (krb5_aname_to_localname(krb5_ctx, krb5_user, sizeof(kuser), kuser))
3e8b5b
+			return 0;
3e8b5b
+		return strcmp(kuser, client) == 0;
3e8b5b
+	}
3e8b5b
+}
3e8b5b
+
3e8b5b
 static int
3e8b5b
 krb5_init(void *context)
3e8b5b
 {
3e8b5b
@@ -160,8 +175,9 @@ auth_krb5_password(Authctxt *authctxt, c
3e8b5b
 	if (problem)
3e8b5b
 		goto out;
3e8b5b
 
3e8b5b
-	if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
3e8b5b
-	    authctxt->pw->pw_name)) {
3e8b5b
+	/* Use !options.use_kuserok here to make ssh_krb5_kuserok() not
3e8b5b
+	 * depend on the existance of .k5login */
3e8b5b
+	if (!ssh_krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user, authctxt->pw->pw_name, !options.use_kuserok)) {
3e8b5b
 		problem = -1;
3e8b5b
 		goto out;
3e8b5b
 	}
3e8b5b
diff -up openssh-7.4p1/gss-serv-krb5.c.kuserok openssh-7.4p1/gss-serv-krb5.c
3e8b5b
--- openssh-7.4p1/gss-serv-krb5.c.kuserok	2016-12-23 14:36:07.640465939 +0100
3e8b5b
+++ openssh-7.4p1/gss-serv-krb5.c	2016-12-23 14:36:07.644465936 +0100
3e8b5b
@@ -67,6 +67,7 @@ static int ssh_gssapi_krb5_cmdok(krb5_pr
3e8b5b
     int);
3e8b5b
 
3e8b5b
 static krb5_context krb_context = NULL;
3e8b5b
+extern int ssh_krb5_kuserok(krb5_context, krb5_principal, const char *, int);
3e8b5b
 
3e8b5b
 /* Initialise the krb5 library, for the stuff that GSSAPI won't do */
3e8b5b
 
3e8b5b
@@ -92,6 +93,103 @@ ssh_gssapi_krb5_init(void)
3e8b5b
  * Returns true if the user is OK to log in, otherwise returns 0
3e8b5b
  */
3e8b5b
 
3e8b5b
+/* The purpose of the function is to find out if a Kerberos principal is
3e8b5b
+ * allowed to log in as the given local user. This is a general problem with
3e8b5b
+ * Kerberized services because by design the Kerberos principals are
3e8b5b
+ * completely independent from the local user names. This is one of the
3e8b5b
+ * reasons why Kerberos is working well on different operating systems like
3e8b5b
+ * Windows and UNIX/Linux. Nevertheless a relationship between a Kerberos
3e8b5b
+ * principal and a local user name must be established because otherwise every
3e8b5b
+ * access would be granted for every principal with a valid ticket.
3e8b5b
+ *
3e8b5b
+ * Since it is a general issue libkrb5 provides some functions for
3e8b5b
+ * applications to find out about the relationship between the Kerberos
3e8b5b
+ * principal and a local user name. They are krb5_kuserok() and
3e8b5b
+ * krb5_aname_to_localname().
3e8b5b
+ *
3e8b5b
+ * krb5_kuserok() can be used to "Determine if a principal is authorized to
3e8b5b
+ * log in as a local user" (from the MIT Kerberos documentation of this
3e8b5b
+ * function). Which is exactly what we are looking for and should be the
3e8b5b
+ * preferred choice. It accepts the Kerberos principal and a local user name
3e8b5b
+ * and let libkrb5 or its plugins determine if they relate to each other or
3e8b5b
+ * not.
3e8b5b
+ *
3e8b5b
+ * krb5_aname_to_localname() can use used to "Convert a principal name to a
3e8b5b
+ * local name" (from the MIT Kerberos documentation of this function). It
3e8b5b
+ * accepts a Kerberos principle and returns a local name and it is up to the
3e8b5b
+ * application to do any additional checks. There are two issues using
3e8b5b
+ * krb5_aname_to_localname(). First, since POSIX user names are case
3e8b5b
+ * sensitive, the calling application in general has no other choice than
3e8b5b
+ * doing a case-sensitive string comparison between the name returned by
3e8b5b
+ * krb5_aname_to_localname() and the name used at the login prompt. When the
3e8b5b
+ * users are provided by a case in-sensitive server, e.g. Active Directory,
3e8b5b
+ * this might lead to login failures because the user typing the name at the
3e8b5b
+ * login prompt might not be aware of the right case. Another issue might be
3e8b5b
+ * caused if there are multiple alias names available for a single user. E.g.
3e8b5b
+ * the canonical name of a user is user@group.department.example.com but there
3e8b5b
+ * exists a shorter login name, e.g. user@example.com, to safe typing at the
3e8b5b
+ * login prompt. Here krb5_aname_to_localname() can only return the canonical
3e8b5b
+ * name, but if the short alias is used at the login prompt authentication
3e8b5b
+ * will fail as well. All this can be avoided by using krb5_kuserok() and
3e8b5b
+ * configuring krb5.conf or using a suitable plugin to meet the needs of the
3e8b5b
+ * given environment.
3e8b5b
+ *
3e8b5b
+ * The Fedora and RHEL version of openssh contain two patches which modify the
3e8b5b
+ * access control behavior:
3e8b5b
+ *  - openssh-6.6p1-kuserok.patch
3e8b5b
+ *  - openssh-6.6p1-force_krb.patch
3e8b5b
+ *
3e8b5b
+ * openssh-6.6p1-kuserok.patch adds a new option KerberosUseKuserok for
3e8b5b
+ * sshd_config which controls if krb5_kuserok() is used to check if the
3e8b5b
+ * principle is authorized or if krb5_aname_to_localname() should be used.
3e8b5b
+ * The reason to add this patch was that krb5_kuserok() by default checks if
3e8b5b
+ * a .k5login file exits in the users home-directory. With this the user can
3e8b5b
+ * give access to his account for any given principal which might be
3e8b5b
+ * in violation with company policies and it would be useful if this can be
3e8b5b
+ * rejected. Nevertheless the patch ignores the fact that krb5_kuserok() does
3e8b5b
+ * no only check .k5login but other sources as well and checking .k5login can
3e8b5b
+ * be disabled for all applications in krb5.conf as well. With this new
3e8b5b
+ * option KerberosUseKuserok set to 'no' (and this is the default for RHEL7
3e8b5b
+ * and Fedora 21) openssh can only use krb5_aname_to_localname() with the
3e8b5b
+ * restrictions mentioned above.
3e8b5b
+ *
3e8b5b
+ * openssh-6.6p1-force_krb.patch adds a ksu like behaviour to ssh, i.e. when
3e8b5b
+ * using GSSAPI authentication only commands configured in the .k5user can be
3e8b5b
+ * executed. Here the wrong assumption that krb5_kuserok() only checks
3e8b5b
+ * .k5login is made as well. In contrast ksu checks .k5login directly and
3e8b5b
+ * does not use krb5_kuserok() which might be more useful for the given
3e8b5b
+ * purpose. Additionally this patch is not synced with
3e8b5b
+ * openssh-6.6p1-kuserok.patch.
3e8b5b
+ *
3e8b5b
+ * The current patch tries to restore the usage of krb5_kuserok() so that e.g.
3e8b5b
+ * localauth plugins can be used. It does so by adding a forth parameter to
3e8b5b
+ * ssh_krb5_kuserok() which indicates whether .k5login exists or not. If it
3e8b5b
+ * does not exists krb5_kuserok() is called even if KerberosUseKuserok is set
3e8b5b
+ * to 'no' because the intent of the option is to not check .k5login and if it
3e8b5b
+ * does not exists krb5_kuserok() returns a result without checking .k5login.
3e8b5b
+ * If .k5login does exists and KerberosUseKuserok is 'no' we fall back to
3e8b5b
+ * krb5_aname_to_localname(). This is in my point of view an acceptable
3e8b5b
+ * limitation and does not break the current behaviour.
3e8b5b
+ *
3e8b5b
+ * Additionally with this patch ssh_krb5_kuserok() is called in
3e8b5b
+ * ssh_gssapi_krb5_cmdok() instead of only krb5_aname_to_localname() is
3e8b5b
+ * neither .k5login nor .k5users exists to allow plugin evaluation via
3e8b5b
+ * krb5_kuserok() as well.
3e8b5b
+ *
3e8b5b
+ * I tried to keep the patch as minimal as possible, nevertheless I see some
3e8b5b
+ * areas for improvement which, if they make sense, have to be evaluated
3e8b5b
+ * carefully because they might change existing behaviour and cause breaks
3e8b5b
+ * during upgrade:
3e8b5b
+ * - I wonder if disabling .k5login usage make sense in sshd or if it should
3e8b5b
+ *   be better disabled globally in krb5.conf
3e8b5b
+ * - if really needed openssh-6.6p1-kuserok.patch should be fixed to really
3e8b5b
+ *   only disable checking .k5login and maybe .k5users
3e8b5b
+ * - the ksu behaviour should be configurable and maybe check the .k5login and
3e8b5b
+ *   .k5users files directly like ksu itself does
3e8b5b
+ * - to make krb5_aname_to_localname() more useful an option for sshd to use
3e8b5b
+ *   the canonical name (the one returned by getpwnam()) instead of the name
3e8b5b
+ *   given at the login prompt might be useful */
3e8b5b
+
3e8b5b
 static int
3e8b5b
 ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
3e8b5b
 {
3e8b5b
@@ -116,7 +214,8 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client
3e8b5b
 	/* NOTE: .k5login and .k5users must opened as root, not the user,
3e8b5b
 	 * because if they are on a krb5-protected filesystem, user credentials
3e8b5b
 	 * to access these files aren't available yet. */
3e8b5b
-	if (krb5_kuserok(krb_context, princ, name) && k5login_exists) {
3e8b5b
+	if (ssh_krb5_kuserok(krb_context, princ, name, k5login_exists)
3e8b5b
+			&& k5login_exists) {
3e8b5b
 		retval = 1;
3e8b5b
 		logit("Authorized to %s, krb5 principal %s (krb5_kuserok)",
3e8b5b
 		    name, (char *)client->displayname.value);
3e8b5b
@@ -190,9 +289,8 @@ ssh_gssapi_krb5_cmdok(krb5_principal pri
3e8b5b
 	snprintf(file, sizeof(file), "%s/.k5users", pw->pw_dir);
3e8b5b
 	/* If both .k5login and .k5users DNE, self-login is ok. */
3e8b5b
 	if (!k5login_exists && (access(file, F_OK) == -1)) {
3e8b5b
-		return (krb5_aname_to_localname(krb_context, principal,
3e8b5b
-		    sizeof(kuser), kuser) == 0) &&
3e8b5b
-		    (strcmp(kuser, luser) == 0);
3e8b5b
+                return ssh_krb5_kuserok(krb_context, principal, luser,
3e8b5b
+                                        k5login_exists);
3e8b5b
 	}
3e8b5b
 	if ((fp = fopen(file, "r")) == NULL) {
3e8b5b
 		int saved_errno = errno;
3e8b5b
diff -up openssh-7.4p1/servconf.c.kuserok openssh-7.4p1/servconf.c
3e8b5b
--- openssh-7.4p1/servconf.c.kuserok	2016-12-23 14:36:07.630465944 +0100
3e8b5b
+++ openssh-7.4p1/servconf.c	2016-12-23 15:11:52.278133344 +0100
3e8b5b
@@ -116,6 +116,7 @@ initialize_server_options(ServerOptions
3e8b5b
 	options->gss_strict_acceptor = -1;
3e8b5b
 	options->gss_store_rekey = -1;
3e8b5b
 	options->gss_kex_algorithms = NULL;
3e8b5b
+	options->use_kuserok = -1;
3e8b5b
 	options->password_authentication = -1;
3e8b5b
 	options->kbd_interactive_authentication = -1;
3e8b5b
 	options->challenge_response_authentication = -1;
3e8b5b
@@ -278,6 +279,8 @@ fill_default_server_options(ServerOption
3e8b5b
 	if (options->gss_kex_algorithms == NULL)
3e8b5b
 		options->gss_kex_algorithms = strdup(GSS_KEX_DEFAULT_KEX);
3e8b5b
 #endif
3e8b5b
+	if (options->use_kuserok == -1)
3e8b5b
+		options->use_kuserok = 1;
3e8b5b
 	if (options->password_authentication == -1)
3e8b5b
 		options->password_authentication = 1;
3e8b5b
 	if (options->kbd_interactive_authentication == -1)
3e8b5b
@@ -399,7 +402,7 @@ typedef enum {
3e8b5b
 	sPermitRootLogin, sLogFacility, sLogLevel,
3e8b5b
 	sRhostsRSAAuthentication, sRSAAuthentication,
3e8b5b
 	sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
3e8b5b
-	sKerberosGetAFSToken, sKerberosUniqueCCache,
3e8b5b
+	sKerberosGetAFSToken, sKerberosUniqueCCache, sKerberosUseKuserok,
3e8b5b
 	sChallengeResponseAuthentication,
3e8b5b
 	sPasswordAuthentication, sKbdInteractiveAuthentication,
3e8b5b
 	sListenAddress, sAddressFamily,
3e8b5b
@@ -478,12 +481,14 @@ static struct {
3e8b5b
 	{ "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
3e8b5b
 #endif
3e8b5b
 	{ "kerberosuniqueccache", sKerberosUniqueCCache, SSHCFG_GLOBAL },
3e8b5b
+	{ "kerberosusekuserok", sKerberosUseKuserok, SSHCFG_ALL },
3e8b5b
 #else
3e8b5b
 	{ "kerberosauthentication", sUnsupported, SSHCFG_ALL },
3e8b5b
 	{ "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
3e8b5b
 	{ "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
3e8b5b
 	{ "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
3e8b5b
 	{ "kerberosuniqueccache", sUnsupported, SSHCFG_GLOBAL },
3e8b5b
+	{ "kerberosusekuserok", sUnsupported, SSHCFG_ALL },
3e8b5b
 #endif
3e8b5b
 	{ "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
3e8b5b
 	{ "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
3e8b5b
@@ -1644,6 +1649,10 @@ process_server_config_line(ServerOptions
3e8b5b
 		*activep = value;
3e8b5b
 		break;
3e8b5b
 
3e8b5b
+	case sKerberosUseKuserok:
3e8b5b
+		intptr = &options->use_kuserok;
3e8b5b
+		goto parse_flag;
3e8b5b
+
3e8b5b
 	case sPermitListen:
3e8b5b
 	case sPermitOpen:
3e8b5b
 		if (opcode == sPermitListen) {
3e8b5b
@@ -2016,6 +2025,7 @@ copy_set_server_options(ServerOptions *d
3e8b5b
 	M_CP_INTOPT(client_alive_interval);
3e8b5b
 	M_CP_INTOPT(ip_qos_interactive);
3e8b5b
 	M_CP_INTOPT(ip_qos_bulk);
3e8b5b
+	M_CP_INTOPT(use_kuserok);
3e8b5b
 	M_CP_INTOPT(rekey_limit);
3e8b5b
 	M_CP_INTOPT(rekey_interval);
3e8b5b
 	M_CP_INTOPT(log_level);
3e8b5b
@@ -2309,6 +2319,7 @@ dump_config(ServerOptions *o)
3e8b5b
 	dump_cfg_fmtint(sKerberosGetAFSToken, o->kerberos_get_afs_token);
3e8b5b
 # endif
3e8b5b
 	dump_cfg_fmtint(sKerberosUniqueCCache, o->kerberos_unique_ccache);
3e8b5b
+	dump_cfg_fmtint(sKerberosUseKuserok, o->use_kuserok);
3e8b5b
 #endif
3e8b5b
 #ifdef GSSAPI
3e8b5b
	dump_cfg_fmtint(sGssAuthentication, o->gss_authentication);
3e8b5b
diff -up openssh-7.4p1/servconf.h.kuserok openssh-7.4p1/servconf.h
3e8b5b
--- openssh-7.4p1/servconf.h.kuserok	2016-12-23 14:36:07.630465944 +0100
3e8b5b
+++ openssh-7.4p1/servconf.h	2016-12-23 14:36:07.645465936 +0100
3e8b5b
@@ -118,6 +118,7 @@ typedef struct {
3e8b5b
 						 * authenticated with Kerberos. */
3e8b5b
 	int     kerberos_unique_ccache;		/* If true, the acquired ticket will
3e8b5b
 						 * be stored in per-session ccache */
3e8b5b
+	int	use_kuserok;
3e8b5b
 	int     gss_authentication;	/* If true, permit GSSAPI authentication */
3e8b5b
 	int     gss_keyex;		/* If true, permit GSSAPI key exchange */
3e8b5b
 	int     gss_cleanup_creds;	/* If true, destroy cred cache on logout */
3e8b5b
diff -up openssh-7.4p1/sshd_config.5.kuserok openssh-7.4p1/sshd_config.5
3e8b5b
--- openssh-7.4p1/sshd_config.5.kuserok	2016-12-23 14:36:07.637465940 +0100
3e8b5b
+++ openssh-7.4p1/sshd_config.5	2016-12-23 15:14:03.117162222 +0100
3e8b5b
@@ -850,6 +850,10 @@ Specifies whether to automatically destr
3e8b5b
 .Cm no
3e8b5b
 can lead to overwriting previous tickets by subseqent connections to the same
3e8b5b
 user account.
3e8b5b
+.It Cm KerberosUseKuserok
3e8b5b
+Specifies whether to look at .k5login file for user's aliases.
3e8b5b
+The default is
3e8b5b
+.Cm yes .
3e8b5b
 .It Cm KexAlgorithms
3e8b5b
 Specifies the available KEX (Key Exchange) algorithms.
3e8b5b
 Multiple algorithms must be comma-separated.
3e8b5b
@@ -1078,6 +1082,7 @@ Available keywords are
3e8b5b
 .Cm IPQoS ,
3e8b5b
 .Cm KbdInteractiveAuthentication ,
3e8b5b
 .Cm KerberosAuthentication ,
3e8b5b
+.Cm KerberosUseKuserok ,
3e8b5b
 .Cm LogLevel ,
3e8b5b
 .Cm MaxAuthTries ,
3e8b5b
 .Cm MaxSessions ,
3e8b5b
diff -up openssh-7.4p1/sshd_config.kuserok openssh-7.4p1/sshd_config
3e8b5b
--- openssh-7.4p1/sshd_config.kuserok	2016-12-23 14:36:07.631465943 +0100
3e8b5b
+++ openssh-7.4p1/sshd_config	2016-12-23 14:36:07.646465935 +0100
3e8b5b
@@ -73,6 +73,7 @@ ChallengeResponseAuthentication no
3e8b5b
 #KerberosOrLocalPasswd yes
3e8b5b
 #KerberosTicketCleanup yes
3e8b5b
 #KerberosGetAFSToken no
3e8b5b
+#KerberosUseKuserok yes
3e8b5b
 
3e8b5b
 # GSSAPI options
3e8b5b
 GSSAPIAuthentication yes