Blame SOURCES/Process-profile-includedir-in-sorted-order.patch

665228
From bcbc07379fec90a2026d621e864db9a1f2c31e92 Mon Sep 17 00:00:00 2001
665228
From: Greg Hudson <ghudson@mit.edu>
665228
Date: Wed, 6 Jun 2018 17:58:41 -0400
665228
Subject: [PATCH] Process profile includedir in sorted order
665228
665228
In the profile library, use k5_dir_filenames() so that files within an
665228
included directory are read in a predictable order (alphanumeric
665228
within the C locale).
665228
665228
ticket: 8686
665228
(cherry picked from commit f574eda48740ad192f51e9a382a205e2ea0e60ad)
665228
(cherry picked from commit 5d868264bca1771aa16abbc8cc0aefb0e1750a73)
665228
---
665228
 doc/admin/conf_files/krb5_conf.rst |  4 ++-
665228
 src/util/profile/prof_parse.c      | 56 +++++-------------------------
665228
 2 files changed, 12 insertions(+), 48 deletions(-)
665228
665228
diff --git a/doc/admin/conf_files/krb5_conf.rst b/doc/admin/conf_files/krb5_conf.rst
665228
index 1d9bc9e34..a959e0e60 100644
665228
--- a/doc/admin/conf_files/krb5_conf.rst
665228
+++ b/doc/admin/conf_files/krb5_conf.rst
665228
@@ -58,7 +58,9 @@ alphanumeric characters, dashes, or underscores.  Starting in release
665228
 1.15, files with names ending in ".conf" are also included, unless the
665228
 name begins with ".".  Included profile files are syntactically
665228
 independent of their parents, so each included file must begin with a
665228
-section header.
665228
+section header.  Starting in release 1.17, files are read in
665228
+alphanumeric order; in previous releases, they may be read in any
665228
+order.
665228
 
665228
 The krb5.conf file can specify that configuration should be obtained
665228
 from a loadable module, rather than the file itself, using the
665228
diff --git a/src/util/profile/prof_parse.c b/src/util/profile/prof_parse.c
665228
index 1baceea9e..531e4a099 100644
665228
--- a/src/util/profile/prof_parse.c
665228
+++ b/src/util/profile/prof_parse.c
665228
@@ -246,59 +246,22 @@ static int valid_name(const char *filename)
665228
  * Include files within dirname.  Only files with names ending in ".conf", or
665228
  * consisting entirely of alphanumeric characters, dashes, and underscores are
665228
  * included.  This restriction avoids including editor backup files, .rpmsave
665228
- * files, and the like.
665228
+ * files, and the like.  Files are processed in alphanumeric order.
665228
  */
665228
 static errcode_t parse_include_dir(const char *dirname,
665228
                                    struct profile_node *root_section)
665228
 {
665228
-#ifdef _WIN32
665228
-    char *wildcard = NULL, *pathname;
665228
-    WIN32_FIND_DATA ffd;
665228
-    HANDLE handle;
665228
     errcode_t retval = 0;
665228
+    char **fnames, *pathname;
665228
+    int i;
665228
 
665228
-    if (asprintf(&wildcard, "%s\\*", dirname) < 0)
665228
-        return ENOMEM;
665228
-
665228
-    handle = FindFirstFile(wildcard, &ffd;;
665228
-    if (handle == INVALID_HANDLE_VALUE) {
665228
-        retval = PROF_FAIL_INCLUDE_DIR;
665228
-        goto cleanup;
665228
-    }
665228
-
665228
-    do {
665228
-        if (!valid_name(ffd.cFileName))
665228
-            continue;
665228
-        if (asprintf(&pathname, "%s\\%s", dirname, ffd.cFileName) < 0) {
665228
-            retval = ENOMEM;
665228
-            break;
665228
-        }
665228
-        retval = parse_include_file(pathname, root_section);
665228
-        free(pathname);
665228
-        if (retval)
665228
-            break;
665228
-    } while (FindNextFile(handle, &ffd) != 0);
665228
-
665228
-    FindClose(handle);
665228
-
665228
-cleanup:
665228
-    free(wildcard);
665228
-    return retval;
665228
-
665228
-#else /* not _WIN32 */
665228
-
665228
-    DIR     *dir;
665228
-    char    *pathname;
665228
-    errcode_t retval = 0;
665228
-    struct dirent *ent;
665228
-
665228
-    dir = opendir(dirname);
665228
-    if (dir == NULL)
665228
+    if (k5_dir_filenames(dirname, &fnames) != 0)
665228
         return PROF_FAIL_INCLUDE_DIR;
665228
-    while ((ent = readdir(dir)) != NULL) {
665228
-        if (!valid_name(ent->d_name))
665228
+
665228
+    for (i = 0; fnames != NULL && fnames[i] != NULL; i++) {
665228
+        if (!valid_name(fnames[i]))
665228
             continue;
665228
-        if (asprintf(&pathname, "%s/%s", dirname, ent->d_name) < 0) {
665228
+        if (asprintf(&pathname, "%s/%s", dirname, fnames[i]) < 0) {
665228
             retval = ENOMEM;
665228
             break;
665228
         }
665228
@@ -307,9 +270,8 @@ cleanup:
665228
         if (retval)
665228
             break;
665228
     }
665228
-    closedir(dir);
665228
+    k5_free_filenames(fnames);
665228
     return retval;
665228
-#endif /* not _WIN32 */
665228
 }
665228
 
665228
 static errcode_t parse_line(char *line, struct parse_state *state,