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