Blame SOURCES/pam-1.5.1-faillock-load-conf-from-file.patch

e0a759
diff -up Linux-PAM-1.5.1/modules/pam_faillock/faillock.8.xml.faillock-load-conf-from-file Linux-PAM-1.5.1/modules/pam_faillock/faillock.8.xml
e0a759
--- Linux-PAM-1.5.1/modules/pam_faillock/faillock.8.xml.faillock-load-conf-from-file	2020-11-25 17:57:02.000000000 +0100
e0a759
+++ Linux-PAM-1.5.1/modules/pam_faillock/faillock.8.xml	2022-05-25 15:30:33.700518571 +0200
e0a759
@@ -57,12 +57,29 @@
e0a759
          <variablelist>
e0a759
             <varlistentry>
e0a759
               <term>
e0a759
+                <option>--conf <replaceable>/path/to/config-file</replaceable></option>
e0a759
+              </term>
e0a759
+              <listitem>
e0a759
+                <para>
e0a759
+                  The file where the configuration is located. The default is
e0a759
+                  <filename>/etc/security/faillock.conf</filename>.
e0a759
+                </para>
e0a759
+              </listitem>
e0a759
+            </varlistentry>
e0a759
+            <varlistentry>
e0a759
+              <term>
e0a759
                 <option>--dir <replaceable>/path/to/tally-directory</replaceable></option>
e0a759
               </term>
e0a759
               <listitem>
e0a759
                 <para>
e0a759
-                  The directory where the user files with the failure records are kept. The
e0a759
-                  default is <filename>/var/run/faillock</filename>.
e0a759
+                  The directory where the user files with the failure records are kept.
e0a759
+                </para>
e0a759
+                <para>
e0a759
+                  The priority to set this option is to use the value provided
e0a759
+                  from the command line. If this isn't provided, then the value
e0a759
+                  from the configuration file is used. Finally, if neither of
e0a759
+                  them has been provided, then
e0a759
+                  <filename>/var/run/faillock</filename> is used.
e0a759
                 </para>
e0a759
               </listitem>
e0a759
             </varlistentry>
e0a759
diff -up Linux-PAM-1.5.1/modules/pam_faillock/faillock_config.c.faillock-load-conf-from-file Linux-PAM-1.5.1/modules/pam_faillock/faillock_config.c
e0a759
--- Linux-PAM-1.5.1/modules/pam_faillock/faillock_config.c.faillock-load-conf-from-file	2022-05-25 15:30:33.699518564 +0200
e0a759
+++ Linux-PAM-1.5.1/modules/pam_faillock/faillock_config.c	2022-05-25 15:30:33.700518571 +0200
e0a759
@@ -0,0 +1,263 @@
e0a759
+/*
e0a759
+ * Copyright (c) 2022 Tomas Mraz <tm@t8m.info>
e0a759
+ * Copyright (c) 2022 Iker Pedrosa <ipedrosa@redhat.com>
e0a759
+ *
e0a759
+ * Redistribution and use in source and binary forms, with or without
e0a759
+ * modification, are permitted provided that the following conditions
e0a759
+ * are met:
e0a759
+ * 1. Redistributions of source code must retain the above copyright
e0a759
+ *    notice, and the entire permission notice in its entirety,
e0a759
+ *    including the disclaimer of warranties.
e0a759
+ * 2. Redistributions in binary form must reproduce the above copyright
e0a759
+ *    notice, this list of conditions and the following disclaimer in the
e0a759
+ *    documentation and/or other materials provided with the distribution.
e0a759
+ * 3. The name of the author may not be used to endorse or promote
e0a759
+ *    products derived from this software without specific prior
e0a759
+ *    written permission.
e0a759
+ *
e0a759
+ * ALTERNATIVELY, this product may be distributed under the terms of
e0a759
+ * the GNU Public License, in which case the provisions of the GPL are
e0a759
+ * required INSTEAD OF the above restrictions.  (This clause is
e0a759
+ * necessary due to a potential bad interaction between the GPL and
e0a759
+ * the restrictions contained in a BSD-style copyright.)
e0a759
+ *
e0a759
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
e0a759
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
e0a759
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
e0a759
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
e0a759
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
e0a759
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
e0a759
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
e0a759
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
e0a759
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
e0a759
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
e0a759
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
e0a759
+ */
e0a759
+
e0a759
+#include "config.h"
e0a759
+
e0a759
+#include <ctype.h>
e0a759
+#include <errno.h>
e0a759
+#include <stdio.h>
e0a759
+#include <stdlib.h>
e0a759
+#include <string.h>
e0a759
+#include <syslog.h>
e0a759
+
e0a759
+#include <security/pam_modules.h>
e0a759
+
e0a759
+#include "faillock_config.h"
e0a759
+#include "faillock.h"
e0a759
+
e0a759
+#define FAILLOCK_DEFAULT_CONF "/etc/security/faillock.conf"
e0a759
+
e0a759
+static void PAM_FORMAT((printf, 3, 4)) PAM_NONNULL((3))
e0a759
+config_log(const pam_handle_t *pamh, int priority, const char *fmt, ...)
e0a759
+{
e0a759
+	va_list args;
e0a759
+
e0a759
+	va_start(args, fmt);
e0a759
+	if (pamh) {
e0a759
+		pam_vsyslog(pamh, priority, fmt, args);
e0a759
+	} else {
e0a759
+		char *buf = NULL;
e0a759
+
e0a759
+		if (vasprintf(&buf, fmt, args) < 0) {
e0a759
+			fprintf(stderr, "vasprintf: %m");
e0a759
+			va_end(args);
e0a759
+			return;
e0a759
+		}
e0a759
+		fprintf(stderr, "%s\n", buf);
e0a759
+		free(buf);
e0a759
+	}
e0a759
+	va_end(args);
e0a759
+}
e0a759
+
e0a759
+/* parse a single configuration file */
e0a759
+int
e0a759
+read_config_file(pam_handle_t *pamh, struct options *opts, const char *cfgfile)
e0a759
+{
e0a759
+	char linebuf[FAILLOCK_CONF_MAX_LINELEN+1];
e0a759
+	const char *fname = (cfgfile != NULL) ? cfgfile : FAILLOCK_DEFAULT_CONF;
e0a759
+	FILE *f = fopen(fname, "r");
e0a759
+
e0a759
+#ifdef VENDOR_FAILLOCK_DEFAULT_CONF
e0a759
+	if (f == NULL && errno == ENOENT && cfgfile == NULL) {
e0a759
+		/*
e0a759
+		 * If the default configuration file in /etc does not exist,
e0a759
+		 * try the vendor configuration file as fallback.
e0a759
+		 */
e0a759
+		f = fopen(VENDOR_FAILLOCK_DEFAULT_CONF, "r");
e0a759
+	}
e0a759
+#endif /* VENDOR_FAILLOCK_DEFAULT_CONF */
e0a759
+
e0a759
+	if (f == NULL) {
e0a759
+		/* ignore non-existent default config file */
e0a759
+		if (errno == ENOENT && cfgfile == NULL)
e0a759
+			return PAM_SUCCESS;
e0a759
+		return PAM_SERVICE_ERR;
e0a759
+	}
e0a759
+
e0a759
+	while (fgets(linebuf, sizeof(linebuf), f) != NULL) {
e0a759
+		size_t len;
e0a759
+		char *ptr;
e0a759
+		char *name;
e0a759
+		int eq;
e0a759
+
e0a759
+		len = strlen(linebuf);
e0a759
+		/* len cannot be 0 unless there is a bug in fgets */
e0a759
+		if (len && linebuf[len - 1] != '\n' && !feof(f)) {
e0a759
+			(void) fclose(f);
e0a759
+			return PAM_SERVICE_ERR;
e0a759
+		}
e0a759
+
e0a759
+		if ((ptr=strchr(linebuf, '#')) != NULL) {
e0a759
+			*ptr = '\0';
e0a759
+		} else {
e0a759
+			ptr = linebuf + len;
e0a759
+		}
e0a759
+
e0a759
+		/* drop terminating whitespace including the \n */
e0a759
+		while (ptr > linebuf) {
e0a759
+			if (!isspace(*(ptr-1))) {
e0a759
+				*ptr = '\0';
e0a759
+				break;
e0a759
+			}
e0a759
+			--ptr;
e0a759
+		}
e0a759
+
e0a759
+		/* skip initial whitespace */
e0a759
+		for (ptr = linebuf; isspace(*ptr); ptr++);
e0a759
+		if (*ptr == '\0')
e0a759
+			continue;
e0a759
+
e0a759
+		/* grab the key name */
e0a759
+		eq = 0;
e0a759
+		name = ptr;
e0a759
+		while (*ptr != '\0') {
e0a759
+			if (isspace(*ptr) || *ptr == '=') {
e0a759
+				eq = *ptr == '=';
e0a759
+				*ptr = '\0';
e0a759
+				++ptr;
e0a759
+				break;
e0a759
+			}
e0a759
+			++ptr;
e0a759
+		}
e0a759
+
e0a759
+		/* grab the key value */
e0a759
+		while (*ptr != '\0') {
e0a759
+			if (*ptr != '=' || eq) {
e0a759
+				if (!isspace(*ptr)) {
e0a759
+					break;
e0a759
+				}
e0a759
+			} else {
e0a759
+				eq = 1;
e0a759
+			}
e0a759
+			++ptr;
e0a759
+		}
e0a759
+
e0a759
+		/* set the key:value pair on opts */
e0a759
+		set_conf_opt(pamh, opts, name, ptr);
e0a759
+	}
e0a759
+
e0a759
+	(void)fclose(f);
e0a759
+	return PAM_SUCCESS;
e0a759
+}
e0a759
+
e0a759
+void
e0a759
+set_conf_opt(pam_handle_t *pamh, struct options *opts, const char *name,
e0a759
+			 const char *value)
e0a759
+{
e0a759
+	if (strcmp(name, "dir") == 0) {
e0a759
+		if (value[0] != '/') {
e0a759
+			config_log(pamh, LOG_ERR,
e0a759
+					"Tally directory is not absolute path (%s); keeping value",
e0a759
+					value);
e0a759
+		} else {
e0a759
+			free(opts->dir);
e0a759
+			opts->dir = strdup(value);
e0a759
+			if (opts->dir == NULL) {
e0a759
+				opts->fatal_error = 1;
e0a759
+				config_log(pamh, LOG_CRIT, "Error allocating memory: %m");
e0a759
+			}
e0a759
+		}
e0a759
+	}
e0a759
+	else if (strcmp(name, "deny") == 0) {
e0a759
+		if (sscanf(value, "%hu", &opts->deny) != 1) {
e0a759
+			config_log(pamh, LOG_ERR,
e0a759
+				"Bad number supplied for deny argument");
e0a759
+		}
e0a759
+	}
e0a759
+	else if (strcmp(name, "fail_interval") == 0) {
e0a759
+		unsigned int temp;
e0a759
+		if (sscanf(value, "%u", &temp) != 1 ||
e0a759
+			temp > MAX_TIME_INTERVAL) {
e0a759
+			config_log(pamh, LOG_ERR,
e0a759
+				"Bad number supplied for fail_interval argument");
e0a759
+		} else {
e0a759
+			opts->fail_interval = temp;
e0a759
+		}
e0a759
+	}
e0a759
+	else if (strcmp(name, "unlock_time") == 0) {
e0a759
+		unsigned int temp;
e0a759
+
e0a759
+		if (strcmp(value, "never") == 0) {
e0a759
+			opts->unlock_time = 0;
e0a759
+		}
e0a759
+		else if (sscanf(value, "%u", &temp) != 1 ||
e0a759
+			temp > MAX_TIME_INTERVAL) {
e0a759
+			config_log(pamh, LOG_ERR,
e0a759
+				"Bad number supplied for unlock_time argument");
e0a759
+		}
e0a759
+		else {
e0a759
+			opts->unlock_time = temp;
e0a759
+		}
e0a759
+	}
e0a759
+	else if (strcmp(name, "root_unlock_time") == 0) {
e0a759
+		unsigned int temp;
e0a759
+
e0a759
+		if (strcmp(value, "never") == 0) {
e0a759
+			opts->root_unlock_time = 0;
e0a759
+		}
e0a759
+		else if (sscanf(value, "%u", &temp) != 1 ||
e0a759
+			temp > MAX_TIME_INTERVAL) {
e0a759
+			config_log(pamh, LOG_ERR,
e0a759
+				"Bad number supplied for root_unlock_time argument");
e0a759
+		} else {
e0a759
+			opts->root_unlock_time = temp;
e0a759
+		}
e0a759
+	}
e0a759
+	else if (strcmp(name, "admin_group") == 0) {
e0a759
+		free(opts->admin_group);
e0a759
+		opts->admin_group = strdup(value);
e0a759
+		if (opts->admin_group == NULL) {
e0a759
+			opts->fatal_error = 1;
e0a759
+			config_log(pamh, LOG_CRIT, "Error allocating memory: %m");
e0a759
+		}
e0a759
+	}
e0a759
+	else if (strcmp(name, "even_deny_root") == 0) {
e0a759
+		opts->flags |= FAILLOCK_FLAG_DENY_ROOT;
e0a759
+	}
e0a759
+	else if (strcmp(name, "audit") == 0) {
e0a759
+		opts->flags |= FAILLOCK_FLAG_AUDIT;
e0a759
+	}
e0a759
+	else if (strcmp(name, "silent") == 0) {
e0a759
+		opts->flags |= FAILLOCK_FLAG_SILENT;
e0a759
+	}
e0a759
+	else if (strcmp(name, "no_log_info") == 0) {
e0a759
+		opts->flags |= FAILLOCK_FLAG_NO_LOG_INFO;
e0a759
+	}
e0a759
+	else if (strcmp(name, "local_users_only") == 0) {
e0a759
+		opts->flags |= FAILLOCK_FLAG_LOCAL_ONLY;
e0a759
+	}
e0a759
+	else if (strcmp(name, "nodelay") == 0) {
e0a759
+		opts->flags |= FAILLOCK_FLAG_NO_DELAY;
e0a759
+	}
e0a759
+	else {
e0a759
+		config_log(pamh, LOG_ERR, "Unknown option: %s", name);
e0a759
+	}
e0a759
+}
e0a759
+
e0a759
+const char *get_tally_dir(const struct options *opts)
e0a759
+{
e0a759
+	return (opts->dir != NULL) ? opts->dir : FAILLOCK_DEFAULT_TALLYDIR;
e0a759
+}
e0a759
diff -up Linux-PAM-1.5.1/modules/pam_faillock/faillock_config.h.faillock-load-conf-from-file Linux-PAM-1.5.1/modules/pam_faillock/faillock_config.h
e0a759
--- Linux-PAM-1.5.1/modules/pam_faillock/faillock_config.h.faillock-load-conf-from-file	2022-05-25 15:30:33.699518564 +0200
e0a759
+++ Linux-PAM-1.5.1/modules/pam_faillock/faillock_config.h	2022-05-25 15:30:33.700518571 +0200
e0a759
@@ -0,0 +1,89 @@
e0a759
+/*
e0a759
+ * Copyright (c) 2022 Tomas Mraz <tm@t8m.info>
e0a759
+ * Copyright (c) 2022 Iker Pedrosa <ipedrosa@redhat.com>
e0a759
+ *
e0a759
+ * Redistribution and use in source and binary forms, with or without
e0a759
+ * modification, are permitted provided that the following conditions
e0a759
+ * are met:
e0a759
+ * 1. Redistributions of source code must retain the above copyright
e0a759
+ *    notice, and the entire permission notice in its entirety,
e0a759
+ *    including the disclaimer of warranties.
e0a759
+ * 2. Redistributions in binary form must reproduce the above copyright
e0a759
+ *    notice, this list of conditions and the following disclaimer in the
e0a759
+ *    documentation and/or other materials provided with the distribution.
e0a759
+ * 3. The name of the author may not be used to endorse or promote
e0a759
+ *    products derived from this software without specific prior
e0a759
+ *    written permission.
e0a759
+ *
e0a759
+ * ALTERNATIVELY, this product may be distributed under the terms of
e0a759
+ * the GNU Public License, in which case the provisions of the GPL are
e0a759
+ * required INSTEAD OF the above restrictions.  (This clause is
e0a759
+ * necessary due to a potential bad interaction between the GPL and
e0a759
+ * the restrictions contained in a BSD-style copyright.)
e0a759
+ *
e0a759
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
e0a759
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
e0a759
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
e0a759
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
e0a759
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
e0a759
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
e0a759
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
e0a759
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
e0a759
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
e0a759
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
e0a759
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
e0a759
+ */
e0a759
+
e0a759
+/*
e0a759
+ * faillock_config.h - load configuration options from file
e0a759
+ *
e0a759
+ */
e0a759
+
e0a759
+#ifndef _FAILLOCK_CONFIG_H
e0a759
+#define _FAILLOCK_CONFIG_H
e0a759
+
e0a759
+#include <limits.h>
e0a759
+#include <stdint.h>
e0a759
+#include <sys/types.h>
e0a759
+
e0a759
+#include <security/pam_ext.h>
e0a759
+
e0a759
+#define FAILLOCK_FLAG_DENY_ROOT		0x1
e0a759
+#define FAILLOCK_FLAG_AUDIT			0x2
e0a759
+#define FAILLOCK_FLAG_SILENT		0x4
e0a759
+#define FAILLOCK_FLAG_NO_LOG_INFO	0x8
e0a759
+#define FAILLOCK_FLAG_UNLOCKED		0x10
e0a759
+#define FAILLOCK_FLAG_LOCAL_ONLY	0x20
e0a759
+#define FAILLOCK_FLAG_NO_DELAY		0x40
e0a759
+
e0a759
+#define FAILLOCK_CONF_MAX_LINELEN 	1023
e0a759
+#define MAX_TIME_INTERVAL			604800 /* 7 days */
e0a759
+
e0a759
+struct options {
e0a759
+	unsigned int action;
e0a759
+	unsigned int flags;
e0a759
+	unsigned short deny;
e0a759
+	unsigned int fail_interval;
e0a759
+	unsigned int unlock_time;
e0a759
+	unsigned int root_unlock_time;
e0a759
+	char *dir;
e0a759
+	const char *user;
e0a759
+	char *admin_group;
e0a759
+	int failures;
e0a759
+	uint64_t latest_time;
e0a759
+	uid_t uid;
e0a759
+	int is_admin;
e0a759
+	uint64_t now;
e0a759
+	int fatal_error;
e0a759
+
e0a759
+	unsigned int reset;
e0a759
+	const char *progname;
e0a759
+};
e0a759
+
e0a759
+int read_config_file(pam_handle_t *pamh, struct options *opts,
e0a759
+					 const char *cfgfile);
e0a759
+void set_conf_opt(pam_handle_t *pamh, struct options *opts, const char *name,
e0a759
+		  const char *value);
e0a759
+const char *get_tally_dir(const struct options *opts);
e0a759
+
e0a759
+#endif /* _FAILLOCK_CONFIG_H */
e0a759
diff -up Linux-PAM-1.5.1/modules/pam_faillock/main.c.faillock-load-conf-from-file Linux-PAM-1.5.1/modules/pam_faillock/main.c
e0a759
--- Linux-PAM-1.5.1/modules/pam_faillock/main.c.faillock-load-conf-from-file	2020-11-25 17:57:02.000000000 +0100
e0a759
+++ Linux-PAM-1.5.1/modules/pam_faillock/main.c	2022-05-25 15:37:05.801216176 +0200
e0a759
@@ -51,33 +51,40 @@
e0a759
 #define AUDIT_NO_ID     ((unsigned int) -1)
e0a759
 #endif
e0a759
 
e0a759
+#include "pam_inline.h"
e0a759
 #include "faillock.h"
e0a759
-
e0a759
-struct options {
e0a759
-	unsigned int reset;
e0a759
-	const char *dir;
e0a759
-	const char *user;
e0a759
-	const char *progname;
e0a759
-};
e0a759
+#include "faillock_config.h"
e0a759
 
e0a759
 static int
e0a759
 args_parse(int argc, char **argv, struct options *opts)
e0a759
 {
e0a759
 	int i;
e0a759
+	int rv;
e0a759
+	const char *dir = NULL;
e0a759
+	const char *conf = NULL;
e0a759
+
e0a759
 	memset(opts, 0, sizeof(*opts));
e0a759
 
e0a759
-	opts->dir = FAILLOCK_DEFAULT_TALLYDIR;
e0a759
 	opts->progname = argv[0];
e0a759
 
e0a759
 	for (i = 1; i < argc; ++i) {
e0a759
-
e0a759
-		if (strcmp(argv[i], "--dir") == 0) {
e0a759
+		if (strcmp(argv[i], "--conf") == 0) {
e0a759
+			++i;
e0a759
+			if (i >= argc || strlen(argv[i]) == 0) {
e0a759
+				fprintf(stderr, "%s: No configuration file supplied.\n",
e0a759
+						argv[0]);
e0a759
+				return -1;
e0a759
+			}
e0a759
+			conf = argv[i];
e0a759
+		}
e0a759
+		else if (strcmp(argv[i], "--dir") == 0) {
e0a759
 			++i;
e0a759
 			if (i >= argc || strlen(argv[i]) == 0) {
e0a759
-				fprintf(stderr, "%s: No directory supplied.\n", argv[0]);
e0a759
+				fprintf(stderr, "%s: No records directory supplied.\n",
e0a759
+						argv[0]);
e0a759
 				return -1;
e0a759
 			}
e0a759
-		        opts->dir = argv[i];
e0a759
+			dir = argv[i];
e0a759
 		}
e0a759
 		else if (strcmp(argv[i], "--user") == 0) {
e0a759
 			++i;
e0a759
@@ -85,7 +92,7 @@ args_parse(int argc, char **argv, struct
e0a759
 				fprintf(stderr, "%s: No user name supplied.\n", argv[0]);
e0a759
 				return -1;
e0a759
 			}
e0a759
-		        opts->user = argv[i];
e0a759
+			opts->user = argv[i];
e0a759
 		}
e0a759
 		else if (strcmp(argv[i], "--reset") == 0) {
e0a759
 			opts->reset = 1;
e0a759
@@ -95,6 +102,21 @@ args_parse(int argc, char **argv, struct
e0a759
 			return -1;
e0a759
 		}
e0a759
 	}
e0a759
+
e0a759
+	if ((rv = read_config_file(NULL, opts, conf)) != PAM_SUCCESS) {
e0a759
+		fprintf(stderr, "Configuration file missing or broken");
e0a759
+		return rv;
e0a759
+	}
e0a759
+
e0a759
+	if (dir != NULL) {
e0a759
+		free(opts->dir);
e0a759
+		opts->dir = strdup(dir);
e0a759
+		if (opts->dir == NULL) {
e0a759
+			fprintf(stderr, "Error allocating memory: %m");
e0a759
+			return -1;
e0a759
+		}
e0a759
+	}
e0a759
+
e0a759
 	return 0;
e0a759
 }
e0a759
 
e0a759
@@ -112,10 +134,11 @@ do_user(struct options *opts, const char
e0a759
 	int rv;
e0a759
 	struct tally_data tallies;
e0a759
 	struct passwd *pwd;
e0a759
+	const char *dir = get_tally_dir(opts);
e0a759
 
e0a759
 	pwd = getpwnam(user);
e0a759
 
e0a759
-	fd = open_tally(opts->dir, user, pwd != NULL ? pwd->pw_uid : 0, 0);
e0a759
+	fd = open_tally(dir, user, pwd != NULL ? pwd->pw_uid : 0, 0);
e0a759
 
e0a759
 	if (fd == -1) {
e0a759
 		if (errno == ENOENT) {
e0a759
@@ -191,8 +214,9 @@ do_allusers(struct options *opts)
e0a759
 {
e0a759
 	struct dirent **userlist;
e0a759
 	int rv, i;
e0a759
+	const char *dir = get_tally_dir(opts);
e0a759
 
e0a759
-	rv = scandir(opts->dir, &userlist, NULL, alphasort);
e0a759
+	rv = scandir(dir, &userlist, NULL, alphasort);
e0a759
 	if (rv < 0) {
e0a759
 		fprintf(stderr, "%s: Error reading tally directory: %m\n", opts->progname);
e0a759
 		return 2;
e0a759
diff -up Linux-PAM-1.5.1/modules/pam_faillock/Makefile.am.faillock-load-conf-from-file Linux-PAM-1.5.1/modules/pam_faillock/Makefile.am
e0a759
--- Linux-PAM-1.5.1/modules/pam_faillock/Makefile.am.faillock-load-conf-from-file	2020-11-25 17:57:02.000000000 +0100
e0a759
+++ Linux-PAM-1.5.1/modules/pam_faillock/Makefile.am	2022-05-25 15:30:33.700518571 +0200
e0a759
@@ -20,7 +20,7 @@ TESTS = $(dist_check_SCRIPTS)
e0a759
 securelibdir = $(SECUREDIR)
e0a759
 secureconfdir = $(SCONFIGDIR)
e0a759
 
e0a759
-noinst_HEADERS = faillock.h
e0a759
+noinst_HEADERS = faillock.h faillock_config.h
e0a759
 
e0a759
 AM_CFLAGS = -I$(top_srcdir)/libpam/include -I$(top_srcdir)/libpamc/include \
e0a759
 	$(WARN_CFLAGS)
e0a759
@@ -41,8 +41,8 @@ dist_secureconf_DATA = faillock.conf
e0a759
 securelib_LTLIBRARIES = pam_faillock.la
e0a759
 sbin_PROGRAMS = faillock
e0a759
 
e0a759
-pam_faillock_la_SOURCES = pam_faillock.c faillock.c
e0a759
-faillock_SOURCES = main.c faillock.c
e0a759
+pam_faillock_la_SOURCES = pam_faillock.c faillock.c faillock_config.c
e0a759
+faillock_SOURCES = main.c faillock.c faillock_config.c
e0a759
 
e0a759
 if ENABLE_REGENERATE_MAN
e0a759
 dist_noinst_DATA = README
e0a759
diff -up Linux-PAM-1.5.1/modules/pam_faillock/pam_faillock.c.faillock-load-conf-from-file Linux-PAM-1.5.1/modules/pam_faillock/pam_faillock.c
e0a759
--- Linux-PAM-1.5.1/modules/pam_faillock/pam_faillock.c.faillock-load-conf-from-file	2020-11-25 17:57:02.000000000 +0100
e0a759
+++ Linux-PAM-1.5.1/modules/pam_faillock/pam_faillock.c	2022-05-25 15:33:03.885551825 +0200
e0a759
@@ -38,7 +38,6 @@
e0a759
 #include <stdio.h>
e0a759
 #include <string.h>
e0a759
 #include <unistd.h>
e0a759
-#include <stdint.h>
e0a759
 #include <stdlib.h>
e0a759
 #include <errno.h>
e0a759
 #include <time.h>
e0a759
@@ -56,55 +55,12 @@
e0a759
 
e0a759
 #include "pam_inline.h"
e0a759
 #include "faillock.h"
e0a759
+#include "faillock_config.h"
e0a759
 
e0a759
 #define FAILLOCK_ACTION_PREAUTH  0
e0a759
 #define FAILLOCK_ACTION_AUTHSUCC 1
e0a759
 #define FAILLOCK_ACTION_AUTHFAIL 2
e0a759
 
e0a759
-#define FAILLOCK_FLAG_DENY_ROOT		0x1
e0a759
-#define FAILLOCK_FLAG_AUDIT		0x2
e0a759
-#define FAILLOCK_FLAG_SILENT		0x4
e0a759
-#define FAILLOCK_FLAG_NO_LOG_INFO	0x8
e0a759
-#define FAILLOCK_FLAG_UNLOCKED		0x10
e0a759
-#define FAILLOCK_FLAG_LOCAL_ONLY	0x20
e0a759
-#define FAILLOCK_FLAG_NO_DELAY		0x40
e0a759
-
e0a759
-#define MAX_TIME_INTERVAL 604800 /* 7 days */
e0a759
-#define FAILLOCK_CONF_MAX_LINELEN 1023
e0a759
-
e0a759
-static const char default_faillock_conf[] = FAILLOCK_DEFAULT_CONF;
e0a759
-
e0a759
-struct options {
e0a759
-	unsigned int action;
e0a759
-	unsigned int flags;
e0a759
-	unsigned short deny;
e0a759
-	unsigned int fail_interval;
e0a759
-	unsigned int unlock_time;
e0a759
-	unsigned int root_unlock_time;
e0a759
-	char *dir;
e0a759
-	const char *user;
e0a759
-	char *admin_group;
e0a759
-	int failures;
e0a759
-	uint64_t latest_time;
e0a759
-	uid_t uid;
e0a759
-	int is_admin;
e0a759
-	uint64_t now;
e0a759
-	int fatal_error;
e0a759
-};
e0a759
-
e0a759
-static int read_config_file(
e0a759
-	pam_handle_t *pamh,
e0a759
-	struct options *opts,
e0a759
-	const char *cfgfile
e0a759
-);
e0a759
-
e0a759
-static void set_conf_opt(
e0a759
-	pam_handle_t *pamh,
e0a759
-	struct options *opts,
e0a759
-	const char *name,
e0a759
-	const char *value
e0a759
-);
e0a759
-
e0a759
 static int
e0a759
 args_parse(pam_handle_t *pamh, int argc, const char **argv,
e0a759
 		int flags, struct options *opts)
e0a759
@@ -112,11 +68,10 @@ args_parse(pam_handle_t *pamh, int argc,
e0a759
 	int i;
e0a759
 	int config_arg_index = -1;
e0a759
 	int rv;
e0a759
-	const char *conf = default_faillock_conf;
e0a759
+	const char *conf = NULL;
e0a759
 
e0a759
 	memset(opts, 0, sizeof(*opts));
e0a759
 
e0a759
-	opts->dir = strdup(FAILLOCK_DEFAULT_TALLYDIR);
e0a759
 	opts->deny = 3;
e0a759
 	opts->fail_interval = 900;
e0a759
 	opts->unlock_time = 600;
e0a759
@@ -174,185 +129,11 @@ args_parse(pam_handle_t *pamh, int argc,
e0a759
 	if (flags & PAM_SILENT)
e0a759
 		opts->flags |= FAILLOCK_FLAG_SILENT;
e0a759
 
e0a759
-	if (opts->dir == NULL) {
e0a759
-		pam_syslog(pamh, LOG_CRIT, "Error allocating memory: %m");
e0a759
-		opts->fatal_error = 1;
e0a759
-	}
e0a759
-
e0a759
 	if (opts->fatal_error)
e0a759
 		return PAM_BUF_ERR;
e0a759
 	return PAM_SUCCESS;
e0a759
 }
e0a759
 
e0a759
-/* parse a single configuration file */
e0a759
-static int
e0a759
-read_config_file(pam_handle_t *pamh, struct options *opts, const char *cfgfile)
e0a759
-{
e0a759
-	FILE *f;
e0a759
-	char linebuf[FAILLOCK_CONF_MAX_LINELEN+1];
e0a759
-
e0a759
-	f = fopen(cfgfile, "r");
e0a759
-	if (f == NULL) {
e0a759
-		/* ignore non-existent default config file */
e0a759
-		if (errno == ENOENT && cfgfile == default_faillock_conf)
e0a759
-			return PAM_SUCCESS;
e0a759
-		return PAM_SERVICE_ERR;
e0a759
-	}
e0a759
-
e0a759
-	while (fgets(linebuf, sizeof(linebuf), f) != NULL) {
e0a759
-		size_t len;
e0a759
-		char *ptr;
e0a759
-		char *name;
e0a759
-		int eq;
e0a759
-
e0a759
-		len = strlen(linebuf);
e0a759
-		/* len cannot be 0 unless there is a bug in fgets */
e0a759
-		if (len && linebuf[len - 1] != '\n' && !feof(f)) {
e0a759
-			(void) fclose(f);
e0a759
-			return PAM_SERVICE_ERR;
e0a759
-		}
e0a759
-
e0a759
-		if ((ptr=strchr(linebuf, '#')) != NULL) {
e0a759
-			*ptr = '\0';
e0a759
-		} else {
e0a759
-			ptr = linebuf + len;
e0a759
-		}
e0a759
-
e0a759
-		/* drop terminating whitespace including the \n */
e0a759
-		while (ptr > linebuf) {
e0a759
-			if (!isspace(*(ptr-1))) {
e0a759
-				*ptr = '\0';
e0a759
-				break;
e0a759
-			}
e0a759
-			--ptr;
e0a759
-		}
e0a759
-
e0a759
-		/* skip initial whitespace */
e0a759
-		for (ptr = linebuf; isspace(*ptr); ptr++);
e0a759
-		if (*ptr == '\0')
e0a759
-			continue;
e0a759
-
e0a759
-		/* grab the key name */
e0a759
-		eq = 0;
e0a759
-		name = ptr;
e0a759
-		while (*ptr != '\0') {
e0a759
-			if (isspace(*ptr) || *ptr == '=') {
e0a759
-				eq = *ptr == '=';
e0a759
-				*ptr = '\0';
e0a759
-				++ptr;
e0a759
-				break;
e0a759
-			}
e0a759
-			++ptr;
e0a759
-		}
e0a759
-
e0a759
-		/* grab the key value */
e0a759
-		while (*ptr != '\0') {
e0a759
-			if (*ptr != '=' || eq) {
e0a759
-				if (!isspace(*ptr)) {
e0a759
-					break;
e0a759
-				}
e0a759
-			} else {
e0a759
-				eq = 1;
e0a759
-			}
e0a759
-			++ptr;
e0a759
-		}
e0a759
-
e0a759
-		/* set the key:value pair on opts */
e0a759
-		set_conf_opt(pamh, opts, name, ptr);
e0a759
-	}
e0a759
-
e0a759
-	(void)fclose(f);
e0a759
-	return PAM_SUCCESS;
e0a759
-}
e0a759
-
e0a759
-static void
e0a759
-set_conf_opt(pam_handle_t *pamh, struct options *opts, const char *name, const char *value)
e0a759
-{
e0a759
-	if (strcmp(name, "dir") == 0) {
e0a759
-		if (value[0] != '/') {
e0a759
-			pam_syslog(pamh, LOG_ERR,
e0a759
-				"Tally directory is not absolute path (%s); keeping default", value);
e0a759
-		} else {
e0a759
-			free(opts->dir);
e0a759
-			opts->dir = strdup(value);
e0a759
-		}
e0a759
-	}
e0a759
-	else if (strcmp(name, "deny") == 0) {
e0a759
-		if (sscanf(value, "%hu", &opts->deny) != 1) {
e0a759
-			pam_syslog(pamh, LOG_ERR,
e0a759
-				"Bad number supplied for deny argument");
e0a759
-		}
e0a759
-	}
e0a759
-	else if (strcmp(name, "fail_interval") == 0) {
e0a759
-		unsigned int temp;
e0a759
-		if (sscanf(value, "%u", &temp) != 1 ||
e0a759
-			temp > MAX_TIME_INTERVAL) {
e0a759
-			pam_syslog(pamh, LOG_ERR,
e0a759
-				"Bad number supplied for fail_interval argument");
e0a759
-		} else {
e0a759
-			opts->fail_interval = temp;
e0a759
-		}
e0a759
-	}
e0a759
-	else if (strcmp(name, "unlock_time") == 0) {
e0a759
-		unsigned int temp;
e0a759
-
e0a759
-		if (strcmp(value, "never") == 0) {
e0a759
-			opts->unlock_time = 0;
e0a759
-		}
e0a759
-		else if (sscanf(value, "%u", &temp) != 1 ||
e0a759
-			temp > MAX_TIME_INTERVAL) {
e0a759
-			pam_syslog(pamh, LOG_ERR,
e0a759
-				"Bad number supplied for unlock_time argument");
e0a759
-		}
e0a759
-		else {
e0a759
-			opts->unlock_time = temp;
e0a759
-		}
e0a759
-	}
e0a759
-	else if (strcmp(name, "root_unlock_time") == 0) {
e0a759
-		unsigned int temp;
e0a759
-
e0a759
-		if (strcmp(value, "never") == 0) {
e0a759
-			opts->root_unlock_time = 0;
e0a759
-		}
e0a759
-		else if (sscanf(value, "%u", &temp) != 1 ||
e0a759
-			temp > MAX_TIME_INTERVAL) {
e0a759
-			pam_syslog(pamh, LOG_ERR,
e0a759
-				"Bad number supplied for root_unlock_time argument");
e0a759
-		} else {
e0a759
-			opts->root_unlock_time = temp;
e0a759
-		}
e0a759
-	}
e0a759
-	else if (strcmp(name, "admin_group") == 0) {
e0a759
-		free(opts->admin_group);
e0a759
-		opts->admin_group = strdup(value);
e0a759
-		if (opts->admin_group == NULL) {
e0a759
-			opts->fatal_error = 1;
e0a759
-			pam_syslog(pamh, LOG_CRIT, "Error allocating memory: %m");
e0a759
-		}
e0a759
-	}
e0a759
-	else if (strcmp(name, "even_deny_root") == 0) {
e0a759
-		opts->flags |= FAILLOCK_FLAG_DENY_ROOT;
e0a759
-	}
e0a759
-	else if (strcmp(name, "audit") == 0) {
e0a759
-		opts->flags |= FAILLOCK_FLAG_AUDIT;
e0a759
-	}
e0a759
-	else if (strcmp(name, "silent") == 0) {
e0a759
-		opts->flags |= FAILLOCK_FLAG_SILENT;
e0a759
-	}
e0a759
-	else if (strcmp(name, "no_log_info") == 0) {
e0a759
-		opts->flags |= FAILLOCK_FLAG_NO_LOG_INFO;
e0a759
-	}
e0a759
-	else if (strcmp(name, "local_users_only") == 0) {
e0a759
-		opts->flags |= FAILLOCK_FLAG_LOCAL_ONLY;
e0a759
-	}
e0a759
-	else if (strcmp(name, "nodelay") == 0) {
e0a759
-		opts->flags |= FAILLOCK_FLAG_NO_DELAY;
e0a759
-	}
e0a759
-	else {
e0a759
-		pam_syslog(pamh, LOG_ERR, "Unknown option: %s", name);
e0a759
-	}
e0a759
-}
e0a759
-
e0a759
 static int
e0a759
 check_local_user (pam_handle_t *pamh, const char *user)
e0a759
 {
e0a759
@@ -406,10 +187,11 @@ check_tally(pam_handle_t *pamh, struct o
e0a759
 	unsigned int i;
e0a759
 	uint64_t latest_time;
e0a759
 	int failures;
e0a759
+	const char *dir = get_tally_dir(opts);
e0a759
 
e0a759
 	opts->now = time(NULL);
e0a759
 
e0a759
-	tfd = open_tally(opts->dir, opts->user, opts->uid, 0);
e0a759
+	tfd = open_tally(dir, opts->user, opts->uid, 0);
e0a759
 
e0a759
 	*fd = tfd;
e0a759
 
e0a759
@@ -483,9 +265,10 @@ static void
e0a759
 reset_tally(pam_handle_t *pamh, struct options *opts, int *fd)
e0a759
 {
e0a759
 	int rv;
e0a759
+	const char *dir = get_tally_dir(opts);
e0a759
 
e0a759
 	if (*fd == -1) {
e0a759
-		*fd = open_tally(opts->dir, opts->user, opts->uid, 1);
e0a759
+		*fd = open_tally(dir, opts->user, opts->uid, 1);
e0a759
 	}
e0a759
 	else {
e0a759
 		while ((rv=ftruncate(*fd, 0)) == -1 && errno == EINTR);
e0a759
@@ -504,9 +287,10 @@ write_tally(pam_handle_t *pamh, struct o
e0a759
 	unsigned int oldest;
e0a759
 	uint64_t oldtime;
e0a759
 	const void *source = NULL;
e0a759
+	const char *dir = get_tally_dir(opts);
e0a759
 
e0a759
 	if (*fd == -1) {
e0a759
-		*fd = open_tally(opts->dir, opts->user, opts->uid, 1);
e0a759
+		*fd = open_tally(dir, opts->user, opts->uid, 1);
e0a759
 	}
e0a759
 	if (*fd == -1) {
e0a759
 		if (errno == EACCES) {
e0a759
diff --git a/modules/pam_faillock/faillock.h b/modules/pam_faillock/faillock.h
e0a759
index b22a9dfb..0ea0ffba 100644
e0a759
--- a/modules/pam_faillock/faillock.h
e0a759
+++ b/modules/pam_faillock/faillock.h
e0a759
diff -up Linux-PAM-1.5.1/modules/pam_faillock/faillock.h.faillock-load-conf-from-file Linux-PAM-1.5.1/modules/pam_faillock/faillock.h
e0a759
--- Linux-PAM-1.5.1/modules/pam_faillock/faillock.h.faillock-load-conf-from-file	2020-11-25 17:57:02.000000000 +0100
e0a759
+++ Linux-PAM-1.5.1/modules/pam_faillock/faillock.h	2022-05-25 15:33:03.885551825 +0200
e0a759
@@ -67,7 +67,6 @@ struct tally_data {
e0a759
 };
e0a759
 
e0a759
 #define FAILLOCK_DEFAULT_TALLYDIR "/var/run/faillock"
e0a759
-#define FAILLOCK_DEFAULT_CONF "/etc/security/faillock.conf"
e0a759
 
e0a759
 int open_tally(const char *dir, const char *user, uid_t uid, int create);
e0a759
 int read_tally(int fd, struct tally_data *tallies);