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

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