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