bmh10 / rpms / openssh

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