017ff1
diff --git a/auth-krb5.c b/auth-krb5.c
017ff1
index 6c62bdf..11c8562 100644
017ff1
--- a/auth-krb5.c
017ff1
+++ b/auth-krb5.c
017ff1
@@ -54,6 +54,21 @@
017ff1
 
017ff1
 extern ServerOptions	 options;
017ff1
 
017ff1
+int
017ff1
+ssh_krb5_kuserok(krb5_context krb5_ctx, krb5_principal krb5_user, const char *client,
017ff1
+                 int k5login_exists)
017ff1
+{
017ff1
+	if (options.use_kuserok || !k5login_exists)
017ff1
+		return krb5_kuserok(krb5_ctx, krb5_user, client);
017ff1
+	else {
017ff1
+		char kuser[65];
017ff1
+
017ff1
+		if (krb5_aname_to_localname(krb5_ctx, krb5_user, sizeof(kuser), kuser))
017ff1
+			return 0;
017ff1
+		return strcmp(kuser, client) == 0;
017ff1
+	}
017ff1
+}
017ff1
+
017ff1
 static int
017ff1
 krb5_init(void *context)
017ff1
 {
017ff1
@@ -157,8 +172,9 @@ auth_krb5_password(Authctxt *authctxt, const char *password)
017ff1
 	if (problem)
017ff1
 		goto out;
017ff1
 
017ff1
-	if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
017ff1
-	    authctxt->pw->pw_name)) {
017ff1
+	/* Use !options.use_kuserok here to make ssh_krb5_kuserok() not
017ff1
+	 * depend on the existance of .k5login */
017ff1
+	if (!ssh_krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user, authctxt->pw->pw_name, !options.use_kuserok)) {
017ff1
 		problem = -1;
017ff1
 		goto out;
017ff1
 	}
017ff1
diff --git a/gss-serv-krb5.c b/gss-serv-krb5.c
017ff1
index 60de320..0a4930e 100644
017ff1
--- a/gss-serv-krb5.c
017ff1
+++ b/gss-serv-krb5.c
017ff1
@@ -67,6 +67,7 @@ static int ssh_gssapi_krb5_cmdok(krb5_principal, const char *, const char *,
017ff1
     int);
017ff1
 
017ff1
 static krb5_context krb_context = NULL;
017ff1
+extern int ssh_krb5_kuserok(krb5_context, krb5_principal, const char *, int);
017ff1
 
017ff1
 /* Initialise the krb5 library, for the stuff that GSSAPI won't do */
017ff1
 
017ff1
@@ -92,6 +93,103 @@ ssh_gssapi_krb5_init(void)
017ff1
  * Returns true if the user is OK to log in, otherwise returns 0
017ff1
  */
017ff1
 
017ff1
+/* The purpose of the function is to find out if a Kerberos principal is
017ff1
+ * allowed to log in as the given local user. This is a general problem with
017ff1
+ * Kerberized services because by design the Kerberos principals are
017ff1
+ * completely independent from the local user names. This is one of the
017ff1
+ * reasons why Kerberos is working well on different operating systems like
017ff1
+ * Windows and UNIX/Linux. Nevertheless a relationship between a Kerberos
017ff1
+ * principal and a local user name must be established because otherwise every
017ff1
+ * access would be granted for every principal with a valid ticket.
017ff1
+ *
017ff1
+ * Since it is a general issue libkrb5 provides some functions for
017ff1
+ * applications to find out about the relationship between the Kerberos
017ff1
+ * principal and a local user name. They are krb5_kuserok() and
017ff1
+ * krb5_aname_to_localname().
017ff1
+ *
017ff1
+ * krb5_kuserok() can be used to "Determine if a principal is authorized to
017ff1
+ * log in as a local user" (from the MIT Kerberos documentation of this
017ff1
+ * function). Which is exactly what we are looking for and should be the
017ff1
+ * preferred choice. It accepts the Kerberos principal and a local user name
017ff1
+ * and let libkrb5 or its plugins determine if they relate to each other or
017ff1
+ * not.
017ff1
+ *
017ff1
+ * krb5_aname_to_localname() can use used to "Convert a principal name to a
017ff1
+ * local name" (from the MIT Kerberos documentation of this function). It
017ff1
+ * accepts a Kerberos principle and returns a local name and it is up to the
017ff1
+ * application to do any additional checks. There are two issues using
017ff1
+ * krb5_aname_to_localname(). First, since POSIX user names are case
017ff1
+ * sensitive, the calling application in general has no other choice than
017ff1
+ * doing a case-sensitive string comparison between the name returned by
017ff1
+ * krb5_aname_to_localname() and the name used at the login prompt. When the
017ff1
+ * users are provided by a case in-sensitive server, e.g. Active Directory,
017ff1
+ * this might lead to login failures because the user typing the name at the
017ff1
+ * login prompt might not be aware of the right case. Another issue might be
017ff1
+ * caused if there are multiple alias names available for a single user. E.g.
017ff1
+ * the canonical name of a user is user@group.department.example.com but there
017ff1
+ * exists a shorter login name, e.g. user@example.com, to safe typing at the
017ff1
+ * login prompt. Here krb5_aname_to_localname() can only return the canonical
017ff1
+ * name, but if the short alias is used at the login prompt authentication
017ff1
+ * will fail as well. All this can be avoided by using krb5_kuserok() and
017ff1
+ * configuring krb5.conf or using a suitable plugin to meet the needs of the
017ff1
+ * given environment.
017ff1
+ *
017ff1
+ * The Fedora and RHEL version of openssh contain two patches which modify the
017ff1
+ * access control behavior:
017ff1
+ *  - openssh-6.6p1-kuserok.patch
017ff1
+ *  - openssh-6.6p1-force_krb.patch
017ff1
+ *
017ff1
+ * openssh-6.6p1-kuserok.patch adds a new option KerberosUseKuserok for
017ff1
+ * sshd_config which controls if krb5_kuserok() is used to check if the
017ff1
+ * principle is authorized or if krb5_aname_to_localname() should be used.
017ff1
+ * The reason to add this patch was that krb5_kuserok() by default checks if
017ff1
+ * a .k5login file exits in the users home-directory. With this the user can
017ff1
+ * give access to his account for any given principal which might be
017ff1
+ * in violation with company policies and it would be useful if this can be
017ff1
+ * rejected. Nevertheless the patch ignores the fact that krb5_kuserok() does
017ff1
+ * no only check .k5login but other sources as well and checking .k5login can
017ff1
+ * be disabled for all applications in krb5.conf as well. With this new
017ff1
+ * option KerberosUseKuserok set to 'no' (and this is the default for RHEL7
017ff1
+ * and Fedora 21) openssh can only use krb5_aname_to_localname() with the
017ff1
+ * restrictions mentioned above.
017ff1
+ *
017ff1
+ * openssh-6.6p1-force_krb.patch adds a ksu like behaviour to ssh, i.e. when
017ff1
+ * using GSSAPI authentication only commands configured in the .k5user can be
017ff1
+ * executed. Here the wrong assumption that krb5_kuserok() only checks
017ff1
+ * .k5login is made as well. In contrast ksu checks .k5login directly and
017ff1
+ * does not use krb5_kuserok() which might be more useful for the given
017ff1
+ * purpose. Additionally this patch is not synced with
017ff1
+ * openssh-6.6p1-kuserok.patch.
017ff1
+ *
017ff1
+ * The current patch tries to restore the usage of krb5_kuserok() so that e.g.
017ff1
+ * localauth plugins can be used. It does so by adding a forth parameter to
017ff1
+ * ssh_krb5_kuserok() which indicates whether .k5login exists or not. If it
017ff1
+ * does not exists krb5_kuserok() is called even if KerberosUseKuserok is set
017ff1
+ * to 'no' because the intent of the option is to not check .k5login and if it
017ff1
+ * does not exists krb5_kuserok() returns a result without checking .k5login.
017ff1
+ * If .k5login does exists and KerberosUseKuserok is 'no' we fall back to
017ff1
+ * krb5_aname_to_localname(). This is in my point of view an acceptable
017ff1
+ * limitation and does not break the current behaviour.
017ff1
+ *
017ff1
+ * Additionally with this patch ssh_krb5_kuserok() is called in
017ff1
+ * ssh_gssapi_krb5_cmdok() instead of only krb5_aname_to_localname() is
017ff1
+ * neither .k5login nor .k5users exists to allow plugin evaluation via
017ff1
+ * krb5_kuserok() as well.
017ff1
+ *
017ff1
+ * I tried to keep the patch as minimal as possible, nevertheless I see some
017ff1
+ * areas for improvement which, if they make sense, have to be evaluated
017ff1
+ * carefully because they might change existing behaviour and cause breaks
017ff1
+ * during upgrade:
017ff1
+ * - I wonder if disabling .k5login usage make sense in sshd or if it should
017ff1
+ *   be better disabled globally in krb5.conf
017ff1
+ * - if really needed openssh-6.6p1-kuserok.patch should be fixed to really
017ff1
+ *   only disable checking .k5login and maybe .k5users
017ff1
+ * - the ksu behaviour should be configurable and maybe check the .k5login and
017ff1
+ *   .k5users files directly like ksu itself does
017ff1
+ * - to make krb5_aname_to_localname() more useful an option for sshd to use
017ff1
+ *   the canonical name (the one returned by getpwnam()) instead of the name
017ff1
+ *   given at the login prompt might be useful */
017ff1
+
017ff1
 static int
017ff1
 ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
017ff1
 {
017ff1
@@ -116,7 +214,8 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
017ff1
 	/* NOTE: .k5login and .k5users must opened as root, not the user,
017ff1
 	 * because if they are on a krb5-protected filesystem, user credentials
017ff1
 	 * to access these files aren't available yet. */
017ff1
-	if (krb5_kuserok(krb_context, princ, name) && k5login_exists) {
017ff1
+	if (ssh_krb5_kuserok(krb_context, princ, name, k5login_exists)
017ff1
+			&& k5login_exists) {
017ff1
 		retval = 1;
017ff1
 		logit("Authorized to %s, krb5 principal %s (krb5_kuserok)",
017ff1
 		    name, (char *)client->displayname.value);
017ff1
@@ -171,9 +270,8 @@ ssh_gssapi_krb5_cmdok(krb5_principal principal, const char *name,
017ff1
 	snprintf(file, sizeof(file), "%s/.k5users", pw->pw_dir);
017ff1
 	/* If both .k5login and .k5users DNE, self-login is ok. */
017ff1
 	if (!k5login_exists && (access(file, F_OK) == -1)) {
017ff1
-		return (krb5_aname_to_localname(krb_context, principal,
017ff1
-		    sizeof(kuser), kuser) == 0) &&
017ff1
-		    (strcmp(kuser, luser) == 0);
017ff1
+                return ssh_krb5_kuserok(krb_context, principal, luser,
017ff1
+                                        k5login_exists);
017ff1
 	}
017ff1
 	if ((fp = fopen(file, "r")) == NULL) {
017ff1
 		int saved_errno = errno;
017ff1
diff --git a/servconf.c b/servconf.c
017ff1
index 68fb9ef..904c869 100644
017ff1
--- a/servconf.c
017ff1
+++ b/servconf.c
017ff1
@@ -157,6 +157,7 @@ initialize_server_options(ServerOptions *options)
017ff1
 	options->ip_qos_interactive = -1;
017ff1
 	options->ip_qos_bulk = -1;
017ff1
 	options->version_addendum = NULL;
017ff1
+	options->use_kuserok = -1;
017ff1
 }
017ff1
 
017ff1
 void
017ff1
@@ -312,6 +313,8 @@ fill_default_server_options(ServerOptions *options)
017ff1
 		options->version_addendum = xstrdup("");
017ff1
 	if (options->show_patchlevel == -1)
017ff1
 		options->show_patchlevel = 0;
017ff1
+	if (options->use_kuserok == -1)
017ff1
+		options->use_kuserok = 1;
017ff1
 
017ff1
 	/* Turn privilege separation on by default */
017ff1
 	if (use_privsep == -1)
017ff1
@@ -338,7 +341,7 @@ typedef enum {
017ff1
 	sPermitRootLogin, sLogFacility, sLogLevel,
017ff1
 	sRhostsRSAAuthentication, sRSAAuthentication,
017ff1
 	sKerberosAuthentication, sKerberosOrLocalPasswd, sKerberosTicketCleanup,
017ff1
-	sKerberosGetAFSToken,
017ff1
+	sKerberosGetAFSToken, sKerberosUseKuserok,
017ff1
 	sKerberosTgtPassing, sChallengeResponseAuthentication,
017ff1
 	sPasswordAuthentication, sKbdInteractiveAuthentication,
017ff1
 	sListenAddress, sAddressFamily,
017ff1
@@ -410,11 +413,13 @@ static struct {
017ff1
 #else
017ff1
 	{ "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
017ff1
 #endif
017ff1
+	{ "kerberosusekuserok", sKerberosUseKuserok, SSHCFG_ALL },
017ff1
 #else
017ff1
 	{ "kerberosauthentication", sUnsupported, SSHCFG_ALL },
017ff1
 	{ "kerberosorlocalpasswd", sUnsupported, SSHCFG_GLOBAL },
017ff1
 	{ "kerberosticketcleanup", sUnsupported, SSHCFG_GLOBAL },
017ff1
 	{ "kerberosgetafstoken", sUnsupported, SSHCFG_GLOBAL },
017ff1
+	{ "kerberosusekuserok", sUnsupported, SSHCFG_ALL },
017ff1
 #endif
017ff1
 	{ "kerberostgtpassing", sUnsupported, SSHCFG_GLOBAL },
017ff1
 	{ "afstokenpassing", sUnsupported, SSHCFG_GLOBAL },
017ff1
@@ -1526,6 +1531,10 @@ process_server_config_line(ServerOptions *options, char *line,
017ff1
 		*activep = value;
017ff1
 		break;
017ff1
 
017ff1
+	case sKerberosUseKuserok:
017ff1
+		intptr = &options->use_kuserok;
017ff1
+		goto parse_flag;
017ff1
+
017ff1
 	case sPermitOpen:
017ff1
 		arg = strdelim(&cp;;
017ff1
 		if (!arg || *arg == '\0')
017ff1
@@ -1811,6 +1820,7 @@ copy_set_server_options(ServerOptions *dst, ServerOptions *src, int preauth)
017ff1
 	M_CP_INTOPT(max_authtries);
017ff1
 	M_CP_INTOPT(ip_qos_interactive);
017ff1
 	M_CP_INTOPT(ip_qos_bulk);
017ff1
+	M_CP_INTOPT(use_kuserok);
017ff1
 	M_CP_INTOPT(rekey_limit);
017ff1
 	M_CP_INTOPT(rekey_interval);
017ff1
 
017ff1
@@ -2062,6 +2072,7 @@ dump_config(ServerOptions *o)
017ff1
 	dump_cfg_fmtint(sUseDNS, o->use_dns);
017ff1
 	dump_cfg_fmtint(sAllowTcpForwarding, o->allow_tcp_forwarding);
017ff1
 	dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
017ff1
+	dump_cfg_fmtint(sKerberosUseKuserok, o->use_kuserok);
017ff1
 
017ff1
 	/* string arguments */
017ff1
 	dump_cfg_string(sPidFile, o->pid_file);
017ff1
diff --git a/servconf.h b/servconf.h
017ff1
index 37cfa9b..5117dfa 100644
017ff1
--- a/servconf.h
017ff1
+++ b/servconf.h
017ff1
@@ -173,6 +173,7 @@ typedef struct {
017ff1
 
017ff1
 	int	num_permitted_opens;
017ff1
 
017ff1
+	int	use_kuserok;
017ff1
 	char   *chroot_directory;
017ff1
 	char   *revoked_keys_file;
017ff1
 	char   *trusted_user_ca_keys;
017ff1
diff --git a/sshd_config b/sshd_config
017ff1
index adfd7b1..e772ed5 100644
017ff1
--- a/sshd_config
017ff1
+++ b/sshd_config
017ff1
@@ -87,6 +87,7 @@ ChallengeResponseAuthentication no
017ff1
 #KerberosOrLocalPasswd yes
017ff1
 #KerberosTicketCleanup yes
017ff1
 #KerberosGetAFSToken no
017ff1
+#KerberosUseKuserok yes
017ff1
 
017ff1
 # GSSAPI options
017ff1
 GSSAPIAuthentication yes
017ff1
diff --git a/sshd_config.5 b/sshd_config.5
017ff1
index 1fb002d..e0e5fff 100644
017ff1
--- a/sshd_config.5
017ff1
+++ b/sshd_config.5
017ff1
@@ -697,6 +697,10 @@ Specifies whether to automatically destroy the user's ticket cache
017ff1
 file on logout.
017ff1
 The default is
017ff1
 .Dq yes .
017ff1
+.It Cm KerberosUseKuserok
017ff1
+Specifies whether to look at .k5login file for user's aliases.
017ff1
+The default is
017ff1
+.Dq yes .
017ff1
 .It Cm KexAlgorithms
017ff1
 Specifies the available KEX (Key Exchange) algorithms.
017ff1
 Multiple algorithms must be comma-separated.
017ff1
@@ -862,6 +866,7 @@ Available keywords are
017ff1
 .Cm HostbasedUsesNameFromPacketOnly ,
017ff1
 .Cm KbdInteractiveAuthentication ,
017ff1
 .Cm KerberosAuthentication ,
017ff1
+.Cm KerberosUseKuserok ,
017ff1
 .Cm MaxAuthTries ,
017ff1
 .Cm MaxSessions ,
017ff1
 .Cm PasswordAuthentication ,