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