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