1d31ef
diff -up openssh-7.4p1/auth-krb5.c.kuserok openssh-7.4p1/auth-krb5.c
1d31ef
--- openssh-7.4p1/auth-krb5.c.kuserok	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/auth-krb5.c	2017-02-09 09:20:00.958084311 +0100
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
 {
1d31ef
@@ -157,8 +172,9 @@ auth_krb5_password(Authctxt *authctxt, c
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
 	}
1d31ef
diff -up openssh-7.4p1/gss-serv-krb5.c.kuserok openssh-7.4p1/gss-serv-krb5.c
1d31ef
--- openssh-7.4p1/gss-serv-krb5.c.kuserok	2017-02-09 09:20:00.955084317 +0100
1d31ef
+++ openssh-7.4p1/gss-serv-krb5.c	2017-02-09 09:20:00.958084311 +0100
1d31ef
@@ -67,6 +67,7 @@ static int ssh_gssapi_krb5_cmdok(krb5_pr
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
 {
1d31ef
@@ -116,7 +214,8 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client
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);
1d31ef
@@ -171,9 +270,8 @@ ssh_gssapi_krb5_cmdok(krb5_principal pri
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;
1d31ef
diff -up openssh-7.4p1/servconf.c.kuserok openssh-7.4p1/servconf.c
1d31ef
--- openssh-7.4p1/servconf.c.kuserok	2017-02-09 09:20:00.951084326 +0100
1d31ef
+++ openssh-7.4p1/servconf.c	2017-02-09 09:21:29.802896034 +0100
1d31ef
@@ -165,6 +165,7 @@ initialize_server_options(ServerOptions
017ff1
 	options->ip_qos_interactive = -1;
017ff1
 	options->ip_qos_bulk = -1;
017ff1
 	options->version_addendum = NULL;
017ff1
+	options->use_kuserok = -1;
1d31ef
 	options->fingerprint_hash = -1;
1d31ef
 	options->disable_forwarding = -1;
017ff1
 }
1d31ef
@@ -334,6 +335,8 @@ fill_default_server_options(ServerOption
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;
1d31ef
 	if (options->fwd_opts.streamlocal_bind_mask == (mode_t)-1)
1d31ef
 		options->fwd_opts.streamlocal_bind_mask = 0177;
1d31ef
 	if (options->fwd_opts.streamlocal_bind_unlink == -1)
1d31ef
@@ -399,7 +402,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,
1d31ef
@@ -478,11 +481,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 },
1d31ef
@@ -1644,6 +1649,10 @@ process_server_config_line(ServerOptions
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')
1d31ef
@@ -2016,6 +2025,7 @@ copy_set_server_options(ServerOptions *d
1d31ef
 	M_CP_INTOPT(client_alive_interval);
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
 
1d31ef
@@ -2308,6 +2318,7 @@ dump_config(ServerOptions *o)
1d31ef
 	dump_cfg_fmtint(sAllowStreamLocalForwarding, o->allow_streamlocal_forwarding);
1d31ef
 	dump_cfg_fmtint(sStreamLocalBindUnlink, o->fwd_opts.streamlocal_bind_unlink);
017ff1
 	dump_cfg_fmtint(sUsePrivilegeSeparation, use_privsep);
017ff1
+	dump_cfg_fmtint(sKerberosUseKuserok, o->use_kuserok);
1d31ef
 	dump_cfg_fmtint(sFingerprintHash, o->fingerprint_hash);
017ff1
 
017ff1
 	/* string arguments */
1d31ef
diff -up openssh-7.4p1/servconf.h.kuserok openssh-7.4p1/servconf.h
1d31ef
--- openssh-7.4p1/servconf.h.kuserok	2017-02-09 09:20:00.951084326 +0100
1d31ef
+++ openssh-7.4p1/servconf.h	2017-02-09 09:20:00.959084309 +0100
1d31ef
@@ -174,6 +174,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;
1d31ef
diff -up openssh-7.4p1/sshd_config.5.kuserok openssh-7.4p1/sshd_config.5
1d31ef
--- openssh-7.4p1/sshd_config.5.kuserok	2017-02-09 09:20:00.959084309 +0100
1d31ef
+++ openssh-7.4p1/sshd_config.5	2017-02-09 09:22:33.517761012 +0100
1d31ef
@@ -846,6 +846,10 @@ Specifies whether to automatically destr
017ff1
 file on logout.
017ff1
 The default is
1d31ef
 .Cm yes .
017ff1
+.It Cm KerberosUseKuserok
017ff1
+Specifies whether to look at .k5login file for user's aliases.
017ff1
+The default is
1d31ef
+.Cm yes .
017ff1
 .It Cm KexAlgorithms
017ff1
 Specifies the available KEX (Key Exchange) algorithms.
017ff1
 Multiple algorithms must be comma-separated.
1d31ef
@@ -1074,6 +1078,7 @@ Available keywords are
1d31ef
 .Cm IPQoS ,
017ff1
 .Cm KbdInteractiveAuthentication ,
017ff1
 .Cm KerberosAuthentication ,
017ff1
+.Cm KerberosUseKuserok ,
017ff1
 .Cm MaxAuthTries ,
017ff1
 .Cm MaxSessions ,
017ff1
 .Cm PasswordAuthentication ,
1d31ef
diff -up openssh-7.4p1/sshd_config.kuserok openssh-7.4p1/sshd_config
1d31ef
--- openssh-7.4p1/sshd_config.kuserok	2017-02-09 09:20:00.953084322 +0100
1d31ef
+++ openssh-7.4p1/sshd_config	2017-02-09 09:20:00.959084309 +0100
1d31ef
@@ -73,6 +73,7 @@ ChallengeResponseAuthentication no
1d31ef
 #KerberosOrLocalPasswd yes
1d31ef
 #KerberosTicketCleanup yes
1d31ef
 #KerberosGetAFSToken no
1d31ef
+#KerberosUseKuserok yes
1d31ef
 
1d31ef
 # GSSAPI options
1d31ef
 GSSAPIAuthentication yes