Blame SOURCES/pam-1.3.1-pam-motd-support-multiple-motd-paths.patch

b51b82
diff -up Linux-PAM-1.3.1/modules/pam_motd/pam_motd.8.xml.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/modules/pam_motd/pam_motd.8.xml
b51b82
--- Linux-PAM-1.3.1/modules/pam_motd/pam_motd.8.xml.pam_motd-support-multiple-motd-paths1	2022-04-25 12:32:36.939663167 +0200
b51b82
+++ Linux-PAM-1.3.1/modules/pam_motd/pam_motd.8.xml	2022-04-25 12:34:43.506582206 +0200
b51b82
@@ -21,6 +21,9 @@
b51b82
       <arg choice="opt">
b51b82
         motd=<replaceable>/path/filename</replaceable>
b51b82
       </arg>
b51b82
+      <arg choice="opt">
b51b82
+        motd_dir=<replaceable>/path/dirname.d</replaceable>
b51b82
+      </arg>
b51b82
     </cmdsynopsis>
b51b82
   </refsynopsisdiv>
b51b82
 
b51b82
@@ -31,18 +34,49 @@
b51b82
     <para>
b51b82
       pam_motd is a PAM module that can be used to display
b51b82
       arbitrary motd (message of the day) files after a successful
b51b82
-      login. By default the <filename>/etc/motd</filename> file and
b51b82
-      all files from <filename>/etc/motd.d</filename> are
b51b82
-      shown. The message size is limited to 64KB.
b51b82
+      login. By default, pam_motd shows files in the
b51b82
+      following locations:
b51b82
+    </para>
b51b82
+    <para>
b51b82
+      <simplelist type='vert'>
b51b82
+        <member><filename>/etc/motd</filename></member>
b51b82
+        <member><filename>/run/motd</filename></member>
b51b82
+        <member><filename>/usr/lib/motd</filename></member>
b51b82
+        <member><filename>/etc/motd.d/</filename></member>
b51b82
+        <member><filename>/run/motd.d/</filename></member>
b51b82
+        <member><filename>/usr/lib/motd.d/</filename></member>
b51b82
+      </simplelist>
b51b82
+    </para>
b51b82
+    <para>
b51b82
+      Each message size is limited to 64KB.
b51b82
+    </para>
b51b82
+    <para>
b51b82
+      If <filename>/etc/motd</filename> does not exist,
b51b82
+      then <filename>/run/motd</filename> is shown. If
b51b82
+      <filename>/run/motd</filename> does not exist, then
b51b82
+      <filename>/usr/lib/motd</filename> is shown.
b51b82
+    </para>
b51b82
+    <para>
b51b82
+      Similar overriding behavior applies to the directories.
b51b82
+      Files in <filename>/etc/motd.d/</filename> override files
b51b82
+      with the same name in <filename>/run/motd.d/</filename> and
b51b82
+      <filename>/usr/lib/motd.d/</filename>. Files in <filename>/run/motd.d/</filename>
b51b82
+      override files with the same name in <filename>/usr/lib/motd.d/</filename>.
b51b82
+    </para>
b51b82
+    <para>
b51b82
+      Files in the directories listed above are displayed in lexicographic
b51b82
+      order by name. Moreover, the files are filtered by reading them with the
b51b82
+      credentials of the target user authenticating on the system.
b51b82
     </para>
b51b82
     <para>
b51b82
       To silence a message,
b51b82
       a symbolic link with target <filename>/dev/null</filename>
b51b82
       may be placed in <filename>/etc/motd.d</filename> with
b51b82
       the same filename as the message to be silenced. Example:
b51b82
+      Creating a symbolic link as follows silences <filename>/usr/lib/motd.d/my_motd</filename>.
b51b82
     </para>
b51b82
     <para>
b51b82
-      <command>ln -sfn /dev/null /etc/motd.d/my_motd</command>
b51b82
+      <command>ln -s /dev/null /etc/motd.d/my_motd</command>
b51b82
     </para>
b51b82
   </refsect1>
b51b82
 
b51b82
@@ -56,8 +90,10 @@
b51b82
         </term>
b51b82
         <listitem>
b51b82
           <para>
b51b82
-	    The <filename>/path/filename</filename> file is displayed
b51b82
-            as message of the day.
b51b82
+            The <filename>/path/filename</filename> file is displayed
b51b82
+            as message of the day. Multiple paths to try can be
b51b82
+            specified as a colon-separated list. By default this option
b51b82
+            is set to <filename>/etc/motd:/run/motd:/usr/lib/motd</filename>.
b51b82
           </para>
b51b82
         </listitem>
b51b82
       </varlistentry>
b51b82
@@ -68,16 +104,17 @@
b51b82
         <listitem>
b51b82
           <para>
b51b82
             The <filename>/path/dirname.d</filename> directory is scanned
b51b82
-            and each file contained inside of it is displayed.
b51b82
+            and each file contained inside of it is displayed. Multiple
b51b82
+            directories to scan can be specified as a colon-separated list.
b51b82
+            By default this option is set to <filename>/etc/motd.d:/run/motd.d:/usr/lib/motd.d</filename>.
b51b82
           </para>
b51b82
         </listitem>
b51b82
       </varlistentry>
b51b82
     </variablelist>
b51b82
     <para>
b51b82
-      When no options are given, the default is to display both
b51b82
-      <filename>/etc/motd</filename> and the contents of
b51b82
-      <filename>/etc/motd.d</filename>.  Specifying either option (or both)
b51b82
-      will disable this default behavior.
b51b82
+      When no options are given, the default behavior applies for both
b51b82
+      options. Specifying either option (or both) will disable the
b51b82
+      default behavior for both options.
b51b82
     </para>
b51b82
   </refsect1>
b51b82
 
b51b82
diff -up Linux-PAM-1.3.1/modules/pam_motd/pam_motd.c.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/modules/pam_motd/pam_motd.c
b51b82
--- Linux-PAM-1.3.1/modules/pam_motd/pam_motd.c.pam_motd-support-multiple-motd-paths1	2018-05-18 11:50:46.000000000 +0200
b51b82
+++ Linux-PAM-1.3.1/modules/pam_motd/pam_motd.c	2022-04-25 12:32:36.947663225 +0200
b51b82
@@ -33,8 +33,8 @@
b51b82
  */
b51b82
 
b51b82
 #define PAM_SM_SESSION
b51b82
-#define DEFAULT_MOTD	"/etc/motd"
b51b82
-#define DEFAULT_MOTD_D	"/etc/motd.d"
b51b82
+#define DEFAULT_MOTD	"/etc/motd:/run/motd:/usr/lib/motd"
b51b82
+#define DEFAULT_MOTD_D	"/etc/motd.d:/run/motd.d:/usr/lib/motd.d"
b51b82
 
b51b82
 #include <security/pam_modules.h>
b51b82
 #include <security/pam_modutil.h>
b51b82
@@ -97,12 +97,235 @@ static void try_to_display_directory(pam
b51b82
     }
b51b82
 }
b51b82
 
b51b82
+/*
b51b82
+ * Split a DELIM-separated string ARG into an array.
b51b82
+ * Outputs a newly allocated array of strings OUT_ARG_SPLIT
b51b82
+ * and the number of strings OUT_NUM_STRS.
b51b82
+ * Returns 0 in case of error, 1 in case of success.
b51b82
+ */
b51b82
+static int pam_split_string(const pam_handle_t *pamh, char *arg, char delim,
b51b82
+			    char ***out_arg_split, uint *out_num_strs)
b51b82
+{
b51b82
+    char *arg_extracted = NULL;
b51b82
+    const char *arg_ptr = arg;
b51b82
+    char **arg_split = NULL;
b51b82
+    char delim_str[2];
b51b82
+    int i = 0;
b51b82
+    uint num_strs = 0;
b51b82
+    int retval = 0;
b51b82
+
b51b82
+    delim_str[0] = delim;
b51b82
+    delim_str[1] = '\0';
b51b82
+
b51b82
+    if (arg == NULL) {
b51b82
+	goto out;
b51b82
+    }
b51b82
+
b51b82
+    while (arg_ptr != NULL) {
b51b82
+	num_strs++;
b51b82
+	arg_ptr = strchr(arg_ptr + sizeof(const char), delim);
b51b82
+    }
b51b82
+
b51b82
+    arg_split = (char **)calloc(num_strs, sizeof(char *));
b51b82
+    if (arg_split == NULL) {
b51b82
+	pam_syslog(pamh, LOG_CRIT, "pam_motd: failed to allocate string array");
b51b82
+	goto out;
b51b82
+    }
b51b82
+
b51b82
+
b51b82
+    arg_extracted = strtok_r(arg, delim_str, &arg;;
b51b82
+    while (arg_extracted != NULL && i < num_strs) {
b51b82
+	arg_split[i++] = arg_extracted;
b51b82
+	arg_extracted = strtok_r(NULL, delim_str, &arg;;
b51b82
+    }
b51b82
+
b51b82
+    retval = 1;
b51b82
+
b51b82
+  out:
b51b82
+    *out_num_strs = num_strs;
b51b82
+    *out_arg_split = arg_split;
b51b82
+
b51b82
+    return retval;
b51b82
+}
b51b82
+
b51b82
+/* Join A_STR and B_STR, inserting a "/" between them if one is not already trailing
b51b82
+ * in A_STR or beginning B_STR. A pointer to a newly allocated string holding the
b51b82
+ * joined string is returned in STRP_OUT.
b51b82
+ * Returns -1 in case of error, or the number of bytes in the joined string in
b51b82
+ * case of success. */
b51b82
+static int join_dir_strings(char **strp_out, const char *a_str, const char *b_str)
b51b82
+{
b51b82
+    int has_sep = 0;
b51b82
+    int retval = -1;
b51b82
+    char *join_strp = NULL;
b51b82
+    
b51b82
+    if (strp_out == NULL || a_str == NULL || b_str == NULL) {
b51b82
+	goto out;
b51b82
+    }
b51b82
+    if (strlen(a_str) == 0) {
b51b82
+	goto out;
b51b82
+    }
b51b82
+
b51b82
+    has_sep = (a_str[strlen(a_str) - 1] == '/') || (b_str[0] == '/');
b51b82
+
b51b82
+    retval = asprintf(&join_strp, "%s%s%s", a_str,
b51b82
+	(has_sep == 1) ? "" : "/", b_str);
b51b82
+
b51b82
+    if (retval < 0) {
b51b82
+	goto out;
b51b82
+    }
b51b82
+
b51b82
+    *strp_out = join_strp;
b51b82
+
b51b82
+  out:
b51b82
+    return retval;
b51b82
+}
b51b82
+
b51b82
+static int compare_strings(const void * a, const void * b)
b51b82
+{
b51b82
+    const char *a_str = *(char **)a;
b51b82
+    const char *b_str = *(char **)b;
b51b82
+
b51b82
+    if (a_str == NULL && b_str == NULL) {
b51b82
+        return 0;
b51b82
+    }
b51b82
+    else if (a_str == NULL) {
b51b82
+	return -1;
b51b82
+    }
b51b82
+    else if (b_str == NULL) {
b51b82
+	return 1;
b51b82
+    }
b51b82
+    else {
b51b82
+	return strcmp(a_str, b_str);
b51b82
+    }
b51b82
+}
b51b82
+
b51b82
+static int filter_dirents(const struct dirent *d)
b51b82
+{
b51b82
+    return (d->d_type == DT_REG || d->d_type == DT_LNK);
b51b82
+}
b51b82
+
b51b82
+static void try_to_display_directories_with_overrides(pam_handle_t *pamh,
b51b82
+	char **motd_dir_path_split, int num_motd_dirs)
b51b82
+{
b51b82
+    struct dirent ***dirscans = NULL;
b51b82
+    int *dirscans_sizes = NULL;
b51b82
+    int dirscans_size_total = 0;
b51b82
+    char **dirnames_all = NULL;
b51b82
+    int i;
b51b82
+    int i_dirnames = 0;
b51b82
+
b51b82
+    if (pamh == NULL || motd_dir_path_split == NULL) {
b51b82
+	goto out;
b51b82
+    }
b51b82
+    if (num_motd_dirs < 1) {
b51b82
+	goto out;
b51b82
+    }
b51b82
+
b51b82
+    if ((dirscans = (struct dirent ***)calloc(num_motd_dirs,
b51b82
+	    sizeof(struct dirent **))) == NULL) {
b51b82
+	pam_syslog(pamh, LOG_CRIT, "pam_motd: failed to allocate dirent arrays");
b51b82
+	goto out;
b51b82
+    }
b51b82
+    if ((dirscans_sizes = (int *)calloc(num_motd_dirs, sizeof(int))) == NULL) {
b51b82
+	pam_syslog(pamh, LOG_CRIT, "pam_motd: failed to allocate dirent array sizes");
b51b82
+	goto out;
b51b82
+    }
b51b82
+
b51b82
+    for (i = 0; i < num_motd_dirs; i++) {
b51b82
+	dirscans_sizes[i] = scandir(motd_dir_path_split[i], &(dirscans[i]),
b51b82
+		filter_dirents, alphasort);
b51b82
+	if (dirscans_sizes[i] < 0) {
b51b82
+	    pam_syslog(pamh, LOG_ERR, "pam_motd: error scanning directory %s", motd_dir_path_split[i]);
b51b82
+	    dirscans_sizes[i] = 0;
b51b82
+	}
b51b82
+	dirscans_size_total += dirscans_sizes[i];
b51b82
+    }
b51b82
+
b51b82
+    /* Allocate space for all file names found in the directories, including duplicates. */
b51b82
+    if ((dirnames_all = (char **)calloc(dirscans_size_total,
b51b82
+	    sizeof(char *))) == NULL) {
b51b82
+	pam_syslog(pamh, LOG_CRIT, "pam_motd: failed to allocate dirname array");
b51b82
+	goto out;
b51b82
+    }
b51b82
+
b51b82
+    for (i = 0; i < dirscans_size_total; i++) {
b51b82
+	dirnames_all[i] = NULL;
b51b82
+    }
b51b82
+
b51b82
+    for (i = 0; i < num_motd_dirs; i++) {
b51b82
+	int j;
b51b82
+
b51b82
+	for (j = 0; j < dirscans_sizes[i]; j++) {
b51b82
+	    dirnames_all[i_dirnames] = dirscans[i][j]->d_name;
b51b82
+	    i_dirnames++;
b51b82
+	}
b51b82
+    }
b51b82
+
b51b82
+    qsort(dirnames_all, dirscans_size_total,
b51b82
+	    sizeof(const char *), compare_strings);
b51b82
+
b51b82
+    for (i = 0; i < dirscans_size_total; i++) {
b51b82
+	int j;
b51b82
+
b51b82
+	if (dirnames_all[i] == NULL) {
b51b82
+	    continue;
b51b82
+	}
b51b82
+
b51b82
+	/* Skip duplicate file names. */
b51b82
+	if (i > 0 && strcmp(dirnames_all[i], dirnames_all[i - 1]) == 0) {
b51b82
+	    continue;
b51b82
+	}
b51b82
+
b51b82
+	for (j = 0; j < num_motd_dirs; j++) {
b51b82
+	    char *abs_path = NULL;
b51b82
+
b51b82
+	    if (join_dir_strings(&abs_path, motd_dir_path_split[j],
b51b82
+		    dirnames_all[i]) < 0) {
b51b82
+		continue;
b51b82
+	    }
b51b82
+
b51b82
+	    if (abs_path != NULL) {
b51b82
+		int fd = open(abs_path, O_RDONLY, 0);
b51b82
+		if (fd >= 0) {
b51b82
+		    try_to_display_fd(pamh, fd);
b51b82
+		    close(fd);
b51b82
+
b51b82
+		    /* We displayed a file, skip to the next file name. */
b51b82
+		    break;
b51b82
+		}
b51b82
+	    }
b51b82
+	    _pam_drop(abs_path);
b51b82
+	}
b51b82
+    }
b51b82
+
b51b82
+  out:
b51b82
+    _pam_drop(dirnames_all);
b51b82
+    for (i = 0; i < num_motd_dirs; i++) {
b51b82
+	int j;
b51b82
+	for (j = 0; j < dirscans_sizes[i]; j++) {
b51b82
+	    _pam_drop(dirscans[i][j]);
b51b82
+	}
b51b82
+	_pam_drop(dirscans[i]);
b51b82
+    }
b51b82
+    _pam_drop(dirscans_sizes);
b51b82
+    _pam_drop(dirscans);
b51b82
+
b51b82
+    return;
b51b82
+}
b51b82
+
b51b82
 int pam_sm_open_session(pam_handle_t *pamh, int flags,
b51b82
 			int argc, const char **argv)
b51b82
 {
b51b82
     int retval = PAM_IGNORE;
b51b82
     const char *motd_path = NULL;
b51b82
+    char *motd_path_copy = NULL;
b51b82
+    int num_motd_paths = 0;
b51b82
+    char **motd_path_split = NULL;
b51b82
     const char *motd_dir_path = NULL;
b51b82
+    char *motd_dir_path_copy = NULL;
b51b82
+    int num_motd_dir_paths = 0;
b51b82
+    char **motd_dir_path_split = NULL;
b51b82
 
b51b82
     if (flags & PAM_SILENT) {
b51b82
 	return retval;
b51b82
@@ -140,17 +363,47 @@ int pam_sm_open_session(pam_handle_t *pa
b51b82
 	motd_dir_path = default_motd_dir;
b51b82
     }
b51b82
 
b51b82
-    if (motd_path != NULL) {
b51b82
-	int fd = open(motd_path, O_RDONLY, 0);
b51b82
+    motd_path_copy = strdup(motd_path);
b51b82
+    if (motd_path_copy != NULL) {
b51b82
+	if (pam_split_string(pamh, motd_path_copy, ':', &motd_path_split,
b51b82
+		&num_motd_paths) == 0) {
b51b82
+	    goto out;
b51b82
+	}
b51b82
+    }
b51b82
+
b51b82
+    motd_dir_path_copy = strdup(motd_dir_path);
b51b82
+    if (motd_dir_path_copy != NULL) {
b51b82
+	if (pam_split_string(pamh, motd_dir_path_copy, ':',
b51b82
+		&motd_dir_path_split, &num_motd_dir_paths) == 0) {
b51b82
+	    goto out;
b51b82
+	}
b51b82
+    }
b51b82
+
b51b82
+    if (motd_path_split != NULL) {
b51b82
+	int i;
b51b82
+
b51b82
+	for (i = 0; i < num_motd_paths; i++) {
b51b82
+	    int fd = open(motd_path_split[i], O_RDONLY, 0);
b51b82
 
b51b82
-	if (fd >= 0) {
b51b82
-	    try_to_display_fd(pamh, fd);
b51b82
-	    close(fd);
b51b82
+	    if (fd >= 0) {
b51b82
+		try_to_display_fd(pamh, fd);
b51b82
+		close(fd);
b51b82
+
b51b82
+		/* We found and displayed a file, move onto next filename. */
b51b82
+		break;
b51b82
+	    }
b51b82
 	}
b51b82
     }
b51b82
 
b51b82
-    if (motd_dir_path != NULL)
b51b82
-	try_to_display_directory(pamh, motd_dir_path);
b51b82
+    if (motd_dir_path_split != NULL)
b51b82
+	try_to_display_directories_with_overrides(pamh, motd_dir_path_split,
b51b82
+		num_motd_dir_paths);
b51b82
+
b51b82
+  out:
b51b82
+    _pam_drop(motd_path_copy);
b51b82
+    _pam_drop(motd_path_split);
b51b82
+    _pam_drop(motd_dir_path_copy);
b51b82
+    _pam_drop(motd_dir_path_split);
b51b82
 
b51b82
     return retval;
b51b82
 }
b51b82
diff -up Linux-PAM-1.3.1/xtests/Makefile.am.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/xtests/Makefile.am
b51b82
--- Linux-PAM-1.3.1/xtests/Makefile.am.pam_motd-support-multiple-motd-paths1	2017-02-10 11:10:15.000000000 +0100
b51b82
+++ Linux-PAM-1.3.1/xtests/Makefile.am	2022-04-25 12:32:36.947663225 +0200
b51b82
@@ -32,7 +32,10 @@ EXTRA_DIST = run-xtests.sh tst-pam_dispa
b51b82
 	tst-pam_substack5.pamd tst-pam_substack5a.pamd tst-pam_substack5.sh \
b51b82
 	tst-pam_assemble_line1.pamd tst-pam_assemble_line1.sh \
b51b82
 	tst-pam_pwhistory1.pamd tst-pam_pwhistory1.sh \
b51b82
-	tst-pam_time1.pamd time.conf
b51b82
+	tst-pam_time1.pamd time.conf \
b51b82
+	tst-pam_motd.sh tst-pam_motd1.sh tst-pam_motd2.sh \
b51b82
+	tst-pam_motd3.sh tst-pam_motd1.pamd \
b51b82
+	tst-pam_motd2.pamd tst-pam_motd3.pamd
b51b82
 
b51b82
 XTESTS = tst-pam_dispatch1 tst-pam_dispatch2 tst-pam_dispatch3 \
b51b82
 	tst-pam_dispatch4 tst-pam_dispatch5 \
b51b82
@@ -41,7 +44,7 @@ XTESTS = tst-pam_dispatch1 tst-pam_dispa
b51b82
 	tst-pam_access1 tst-pam_access2 tst-pam_access3 \
b51b82
 	tst-pam_access4 tst-pam_limits1 tst-pam_succeed_if1 \
b51b82
 	tst-pam_group1 tst-pam_authfail tst-pam_authsucceed \
b51b82
-	tst-pam_pwhistory1 tst-pam_time1
b51b82
+	tst-pam_pwhistory1 tst-pam_time1 tst-pam_motd
b51b82
 
b51b82
 NOSRCTESTS = tst-pam_substack1 tst-pam_substack2 tst-pam_substack3 \
b51b82
 	tst-pam_substack4 tst-pam_substack5 tst-pam_assemble_line1
b51b82
diff -up Linux-PAM-1.3.1/xtests/tst-pam_motd1.pamd.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/xtests/tst-pam_motd1.pamd
b51b82
--- Linux-PAM-1.3.1/xtests/tst-pam_motd1.pamd.pam_motd-support-multiple-motd-paths1	2022-04-25 12:32:36.947663225 +0200
b51b82
+++ Linux-PAM-1.3.1/xtests/tst-pam_motd1.pamd	2022-04-25 12:32:36.947663225 +0200
b51b82
@@ -0,0 +1,3 @@
b51b82
+#%PAM-1.0
b51b82
+session    required    pam_permit.so
b51b82
+session    optional    pam_motd.so motd=tst-pam_motd1.d/etc/motd motd_dir=tst-pam_motd1.d/etc/motd.d
b51b82
diff -up Linux-PAM-1.3.1/xtests/tst-pam_motd1.sh.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/xtests/tst-pam_motd1.sh
b51b82
--- Linux-PAM-1.3.1/xtests/tst-pam_motd1.sh.pam_motd-support-multiple-motd-paths1	2022-04-25 12:32:36.947663225 +0200
b51b82
+++ Linux-PAM-1.3.1/xtests/tst-pam_motd1.sh	2022-04-25 12:32:36.947663225 +0200
b51b82
@@ -0,0 +1,36 @@
b51b82
+#!/bin/bash
b51b82
+
b51b82
+TST_DIR="tst-pam_motd1.d"
b51b82
+
b51b82
+function tst_cleanup() {
b51b82
+    rm -rf "${TST_DIR}"
b51b82
+    rm -f tst-pam_motd1.out
b51b82
+}
b51b82
+
b51b82
+mkdir -p ${TST_DIR}
b51b82
+mkdir -p ${TST_DIR}/etc/motd.d
b51b82
+
b51b82
+# Verify the case of single motd and motd.d directory works
b51b82
+echo "motd: /etc/motd" > ${TST_DIR}/etc/motd
b51b82
+echo "motd: /etc/motd.d/test" > ${TST_DIR}/etc/motd.d/test
b51b82
+
b51b82
+./tst-pam_motd tst-pam_motd1 > tst-pam_motd1.out
b51b82
+
b51b82
+RET=$?
b51b82
+
b51b82
+motd_to_show_output=$(cat tst-pam_motd1.out | grep "motd: /etc/motd")
b51b82
+if [ -z "${motd_to_show_output}" ];
b51b82
+then
b51b82
+    tst_cleanup
b51b82
+    exit 1
b51b82
+fi
b51b82
+
b51b82
+motd_dir_to_show_output=$(cat tst-pam_motd1.out | grep "motd: /etc/motd.d/test")
b51b82
+if [ -z "${motd_dir_to_show_output}" ];
b51b82
+then
b51b82
+    tst_cleanup
b51b82
+    exit 1
b51b82
+fi
b51b82
+
b51b82
+tst_cleanup
b51b82
+exit $RET
b51b82
diff -up Linux-PAM-1.3.1/xtests/tst-pam_motd2.pamd.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/xtests/tst-pam_motd2.pamd
b51b82
--- Linux-PAM-1.3.1/xtests/tst-pam_motd2.pamd.pam_motd-support-multiple-motd-paths1	2022-04-25 12:32:36.947663225 +0200
b51b82
+++ Linux-PAM-1.3.1/xtests/tst-pam_motd2.pamd	2022-04-25 12:32:36.947663225 +0200
b51b82
@@ -0,0 +1,3 @@
b51b82
+#%PAM-1.0
b51b82
+session    required    pam_permit.so
b51b82
+session optional    pam_motd.so    motd=tst-pam_motd2.d/etc/motd:tst-pam_motd2.d/run/motd:tst-pam_motd2.d/usr/lib/motd motd_dir=tst-pam_motd2.d/etc/motd.d:tst-pam_motd2.d/run/motd.d:tst-pam_motd2.d/usr/lib/motd.d
b51b82
diff -up Linux-PAM-1.3.1/xtests/tst-pam_motd2.sh.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/xtests/tst-pam_motd2.sh
b51b82
--- Linux-PAM-1.3.1/xtests/tst-pam_motd2.sh.pam_motd-support-multiple-motd-paths1	2022-04-25 12:32:36.947663225 +0200
b51b82
+++ Linux-PAM-1.3.1/xtests/tst-pam_motd2.sh	2022-04-25 12:32:36.947663225 +0200
b51b82
@@ -0,0 +1,53 @@
b51b82
+#!/bin/bash
b51b82
+
b51b82
+TST_DIR="tst-pam_motd2.d"
b51b82
+
b51b82
+function tst_cleanup() {
b51b82
+    rm -rf "${TST_DIR}"
b51b82
+    rm -f tst-pam_motd2.out
b51b82
+}
b51b82
+
b51b82
+mkdir -p ${TST_DIR}
b51b82
+mkdir -p ${TST_DIR}/etc/motd.d
b51b82
+mkdir -p ${TST_DIR}/run/motd.d
b51b82
+mkdir -p ${TST_DIR}/usr/lib/motd.d
b51b82
+
b51b82
+echo "motd: /etc/motd" > ${TST_DIR}/etc/motd
b51b82
+echo "motd: /run/motd" > ${TST_DIR}/run/motd
b51b82
+echo "motd: /usr/lib/motd" > ${TST_DIR}/usr/lib/motd
b51b82
+
b51b82
+# Drop a motd file in test directories such that every overriding
b51b82
+# condition (for 3 directories in this case) will be seen.
b51b82
+echo "motd: e0r0u1 in usr/lib - will show" > ${TST_DIR}/usr/lib/motd.d/e0r0u1.motd
b51b82
+echo "motd: e0r1u0 in run - will show" > ${TST_DIR}/run/motd.d/e0r1u0.motd
b51b82
+echo "motd: e0r1u1 in usr/lib - not show" > ${TST_DIR}/usr/lib/motd.d/e0r1u1.motd
b51b82
+echo "motd: e0r1u1 in run - will show" > ${TST_DIR}/run/motd.d/e0r1u1.motd
b51b82
+echo "motd: e1r0u0 in etc - will show" > ${TST_DIR}/etc/motd.d/e1r0u0.motd
b51b82
+echo "motd: e1r0u1 in usr/lib - not show" > ${TST_DIR}/usr/lib/motd.d/e1r0u1.motd
b51b82
+echo "motd: e1r0u1 in etc - will show" > ${TST_DIR}/etc/motd.d/e1r0u1.motd
b51b82
+echo "motd: e1r1u0 in run - not show" > ${TST_DIR}/run/motd.d/e1r1u0.motd
b51b82
+echo "motd: e1r1u0 in etc - will show" > ${TST_DIR}/etc/motd.d/e1r1u0.motd
b51b82
+echo "motd: e1r1u1 in usr/lib - not show" > ${TST_DIR}/usr/lib/motd.d/e1r1u1.motd
b51b82
+echo "motd: e1r1u1 in run - not show" > ${TST_DIR}/run/motd.d/e1r1u1.motd
b51b82
+echo "motd: e1r1u1 in etc - will show" > ${TST_DIR}/etc/motd.d/e1r1u1.motd
b51b82
+
b51b82
+./tst-pam_motd tst-pam_motd2 > tst-pam_motd2.out
b51b82
+
b51b82
+RET=$?
b51b82
+
b51b82
+motd_to_show_output=$(cat tst-pam_motd2.out | grep "motd: /etc/motd")
b51b82
+if [ -z "${motd_to_show_output}" ];
b51b82
+then
b51b82
+    tst_cleanup
b51b82
+    exit 1
b51b82
+fi
b51b82
+
b51b82
+motd_dir_not_show_output=$(cat tst-pam_motd2.out | grep "not show")
b51b82
+if [ -n "${motd_dir_not_show_output}" ];
b51b82
+then
b51b82
+    tst_cleanup
b51b82
+    exit 1
b51b82
+fi
b51b82
+
b51b82
+tst_cleanup
b51b82
+exit $RET
b51b82
diff -up Linux-PAM-1.3.1/xtests/tst-pam_motd3.pamd.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/xtests/tst-pam_motd3.pamd
b51b82
--- Linux-PAM-1.3.1/xtests/tst-pam_motd3.pamd.pam_motd-support-multiple-motd-paths1	2022-04-25 12:32:36.947663225 +0200
b51b82
+++ Linux-PAM-1.3.1/xtests/tst-pam_motd3.pamd	2022-04-25 12:32:36.947663225 +0200
b51b82
@@ -0,0 +1,3 @@
b51b82
+#%PAM-1.0
b51b82
+session    required    pam_permit.so
b51b82
+session optional    pam_motd.so    motd=tst-pam_motd3.d/etc/motd:tst-pam_motd3.d/run/motd:tst-pam_motd3.d/usr/lib/motd motd_dir=tst-pam_motd3.d/etc/motd.d:tst-pam_motd3.d/run/motd.d:tst-pam_motd3.d/usr/lib/motd.d
b51b82
diff -up Linux-PAM-1.3.1/xtests/tst-pam_motd3.sh.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/xtests/tst-pam_motd3.sh
b51b82
--- Linux-PAM-1.3.1/xtests/tst-pam_motd3.sh.pam_motd-support-multiple-motd-paths1	2022-04-25 12:32:36.947663225 +0200
b51b82
+++ Linux-PAM-1.3.1/xtests/tst-pam_motd3.sh	2022-04-25 12:32:36.947663225 +0200
b51b82
@@ -0,0 +1,53 @@
b51b82
+#!/bin/bash
b51b82
+
b51b82
+TST_DIR="tst-pam_motd3.d"
b51b82
+
b51b82
+function tst_cleanup() {
b51b82
+    rm -rf "${TST_DIR}"
b51b82
+    rm -f tst-pam_motd3.out
b51b82
+}
b51b82
+
b51b82
+mkdir -p ${TST_DIR}
b51b82
+mkdir -p ${TST_DIR}/etc/motd.d
b51b82
+mkdir -p ${TST_DIR}/run/motd.d
b51b82
+mkdir -p ${TST_DIR}/usr/lib/motd.d
b51b82
+
b51b82
+# Verify motd is still displayed when not overridden
b51b82
+echo "motd: test-show in run - show" > ${TST_DIR}/run/motd.d/test-show.motd
b51b82
+
b51b82
+# Test overridden by a symlink to a file that isn't /dev/null; symlink target should show
b51b82
+echo "motd: hidden-by-symlink in usr/lib - not show" > ${TST_DIR}/usr/lib/motd.d/hidden-by-symlink.motd
b51b82
+echo "motd: test-from-symlink - show" > ${TST_DIR}/test-from-symlink.motd
b51b82
+ln -sr ${TST_DIR}/test-from-symlink.motd ${TST_DIR}/run/motd.d/hidden-by-symlink.motd
b51b82
+
b51b82
+# Test hidden by a null symlink
b51b82
+echo "motd: hidden-by-null-symlink in run - not show" > ${TST_DIR}/run/motd.d/hidden-by-null-symlink.motd
b51b82
+ln -s /dev/null ${TST_DIR}/etc/motd.d/hidden-by-null-symlink.motd
b51b82
+
b51b82
+./tst-pam_motd tst-pam_motd3 > tst-pam_motd3.out
b51b82
+
b51b82
+RET=$?
b51b82
+
b51b82
+motd_dir_not_show_output=$(cat tst-pam_motd3.out | grep "not show")
b51b82
+if [ -n "${motd_dir_not_show_output}" ];
b51b82
+then
b51b82
+    tst_cleanup
b51b82
+    exit 1
b51b82
+fi
b51b82
+
b51b82
+motd_test_show_output=$(cat tst-pam_motd3.out | grep "test-show.*- show")
b51b82
+if [ -z "${motd_test_show_output}" ];
b51b82
+then
b51b82
+    tst_cleanup
b51b82
+    exit 1
b51b82
+fi
b51b82
+
b51b82
+motd_general_symlink_show_output=$(cat tst-pam_motd3.out | grep "test-from-symlink.*- show")
b51b82
+if [ -z "${motd_general_symlink_show_output}" ];
b51b82
+then
b51b82
+    tst_cleanup
b51b82
+    exit 1
b51b82
+fi
b51b82
+
b51b82
+tst_cleanup
b51b82
+exit $RET
b51b82
diff -up Linux-PAM-1.3.1/xtests/tst-pam_motd.c.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/xtests/tst-pam_motd.c
b51b82
--- Linux-PAM-1.3.1/xtests/tst-pam_motd.c.pam_motd-support-multiple-motd-paths1	2022-04-25 12:32:36.947663225 +0200
b51b82
+++ Linux-PAM-1.3.1/xtests/tst-pam_motd.c	2022-04-25 12:32:36.947663225 +0200
b51b82
@@ -0,0 +1,69 @@
b51b82
+/*
b51b82
+ * Redistribution and use in source and binary forms, with or without
b51b82
+ * modification, are permitted provided that the following conditions
b51b82
+ * are met:
b51b82
+ * 1. Redistributions of source code must retain the above copyright
b51b82
+ *    notice, and the entire permission notice in its entirety,
b51b82
+ *    including the disclaimer of warranties.
b51b82
+ * 2. Redistributions in binary form must reproduce the above copyright
b51b82
+ *    notice, this list of conditions and the following disclaimer in the
b51b82
+ *    documentation and/or other materials provided with the distribution.
b51b82
+ * 3. The name of the author may not be used to endorse or promote
b51b82
+ *    products derived from this software without specific prior
b51b82
+ *    written permission.
b51b82
+ *
b51b82
+ * ALTERNATIVELY, this product may be distributed under the terms of
b51b82
+ * the GNU Public License, in which case the provisions of the GPL are
b51b82
+ * required INSTEAD OF the above restrictions.  (This clause is
b51b82
+ * necessary due to a potential bad interaction between the GPL and
b51b82
+ * the restrictions contained in a BSD-style copyright.)
b51b82
+ *
b51b82
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
b51b82
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
b51b82
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
b51b82
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
b51b82
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
b51b82
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
b51b82
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
b51b82
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
b51b82
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
b51b82
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
b51b82
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
b51b82
+ */
b51b82
+
b51b82
+#ifdef HAVE_CONFIG_H
b51b82
+#include <config.h>
b51b82
+#endif
b51b82
+
b51b82
+#include <stdio.h>
b51b82
+#include <stdlib.h>
b51b82
+#include <security/pam_appl.h>
b51b82
+#include <security/pam_misc.h>
b51b82
+
b51b82
+static struct pam_conv conv = {
b51b82
+    misc_conv,
b51b82
+    NULL
b51b82
+};
b51b82
+
b51b82
+int main(int argc, char *argv[])
b51b82
+{
b51b82
+    pam_handle_t *pamh=NULL;
b51b82
+    char *tst_arg = NULL;
b51b82
+    int retval;
b51b82
+
b51b82
+    if (argc > 1)
b51b82
+	tst_arg = argv[1];
b51b82
+
b51b82
+    retval = pam_start(tst_arg, NULL, &conv, &pamh);
b51b82
+
b51b82
+    retval = pam_open_session(pamh, 0);
b51b82
+
b51b82
+    retval = pam_close_session(pamh, 0);
b51b82
+
b51b82
+    if (pam_end(pamh,retval) != PAM_SUCCESS) {     /* close Linux-PAM */
b51b82
+	pamh = NULL;
b51b82
+	exit(1);
b51b82
+    }
b51b82
+
b51b82
+    return ( retval == PAM_SUCCESS ? 0:1 );       /* indicate success */
b51b82
+}
b51b82
diff -up Linux-PAM-1.3.1/xtests/tst-pam_motd.sh.pam_motd-support-multiple-motd-paths1 Linux-PAM-1.3.1/xtests/tst-pam_motd.sh
b51b82
--- Linux-PAM-1.3.1/xtests/tst-pam_motd.sh.pam_motd-support-multiple-motd-paths1	2022-04-25 12:32:36.947663225 +0200
b51b82
+++ Linux-PAM-1.3.1/xtests/tst-pam_motd.sh	2022-04-25 12:32:36.947663225 +0200
b51b82
@@ -0,0 +1,7 @@
b51b82
+#!/bin/bash
b51b82
+
b51b82
+set -e
b51b82
+
b51b82
+./tst-pam_motd1.sh
b51b82
+./tst-pam_motd2.sh
b51b82
+./tst-pam_motd3.sh