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