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

b51b82
diff -up Linux-PAM-1.3.1/modules/pam_faillock/faillock.8.xml.faillock-load-conf-from-file Linux-PAM-1.3.1/modules/pam_faillock/faillock.8.xml
b51b82
--- Linux-PAM-1.3.1/modules/pam_faillock/faillock.8.xml.faillock-load-conf-from-file	2022-05-26 10:57:11.713067506 +0200
b51b82
+++ Linux-PAM-1.3.1/modules/pam_faillock/faillock.8.xml	2022-05-26 10:57:11.747067749 +0200
b51b82
@@ -57,12 +57,29 @@
b51b82
          <variablelist>
b51b82
             <varlistentry>
b51b82
               <term>
b51b82
+                <option>--conf <replaceable>/path/to/config-file</replaceable></option>
b51b82
+              </term>
b51b82
+              <listitem>
b51b82
+                <para>
b51b82
+                  The file where the configuration is located. The default is
b51b82
+                  <filename>/etc/security/faillock.conf</filename>.
b51b82
+                </para>
b51b82
+              </listitem>
b51b82
+            </varlistentry>
b51b82
+            <varlistentry>
b51b82
+              <term>
b51b82
                 <option>--dir <replaceable>/path/to/tally-directory</replaceable></option>
b51b82
               </term>
b51b82
               <listitem>
b51b82
                 <para>
b51b82
-                  The directory where the user files with the failure records are kept. The
b51b82
-                  default is <filename>/var/run/faillock</filename>.
b51b82
+                  The directory where the user files with the failure records are kept.
b51b82
+                </para>
b51b82
+                <para>
b51b82
+                  The priority to set this option is to use the value provided
b51b82
+                  from the command line. If this isn't provided, then the value
b51b82
+                  from the configuration file is used. Finally, if neither of
b51b82
+                  them has been provided, then
b51b82
+                  <filename>/var/run/faillock</filename> is used.
b51b82
                 </para>
b51b82
               </listitem>
b51b82
             </varlistentry>
b51b82
diff -up Linux-PAM-1.3.1/modules/pam_faillock/faillock_config.c.faillock-load-conf-from-file Linux-PAM-1.3.1/modules/pam_faillock/faillock_config.c
b51b82
--- Linux-PAM-1.3.1/modules/pam_faillock/faillock_config.c.faillock-load-conf-from-file	2022-05-26 10:57:11.747067749 +0200
b51b82
+++ Linux-PAM-1.3.1/modules/pam_faillock/faillock_config.c	2022-05-26 10:57:11.747067749 +0200
b51b82
@@ -0,0 +1,266 @@
b51b82
+/*
b51b82
+ * Copyright (c) 2022 Tomas Mraz <tm@t8m.info>
b51b82
+ * Copyright (c) 2022 Iker Pedrosa <ipedrosa@redhat.com>
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
+#include "config.h"
b51b82
+
b51b82
+#include <ctype.h>
b51b82
+#include <errno.h>
b51b82
+#include <stdio.h>
b51b82
+#include <stdlib.h>
b51b82
+#include <string.h>
b51b82
+#include <syslog.h>
b51b82
+
b51b82
+#include <security/pam_modules.h>
b51b82
+
b51b82
+#include "faillock_config.h"
b51b82
+#include "faillock.h"
b51b82
+
b51b82
+#define FAILLOCK_DEFAULT_CONF "/etc/security/faillock.conf"
b51b82
+#ifdef VENDOR_SCONFIGDIR
b51b82
+#define VENDOR_FAILLOCK_DEFAULT_CONF VENDOR_SCONFIGDIR "/faillock.conf"
b51b82
+#endif
b51b82
+
b51b82
+static void PAM_FORMAT((printf, 3, 4)) PAM_NONNULL((3))
b51b82
+config_log(const pam_handle_t *pamh, int priority, const char *fmt, ...)
b51b82
+{
b51b82
+	va_list args;
b51b82
+
b51b82
+	va_start(args, fmt);
b51b82
+	if (pamh) {
b51b82
+		pam_vsyslog(pamh, priority, fmt, args);
b51b82
+	} else {
b51b82
+		char *buf = NULL;
b51b82
+
b51b82
+		if (vasprintf(&buf, fmt, args) < 0) {
b51b82
+			fprintf(stderr, "vasprintf: %m");
b51b82
+			va_end(args);
b51b82
+			return;
b51b82
+		}
b51b82
+		fprintf(stderr, "%s\n", buf);
b51b82
+		free(buf);
b51b82
+	}
b51b82
+	va_end(args);
b51b82
+}
b51b82
+
b51b82
+/* parse a single configuration file */
b51b82
+int
b51b82
+read_config_file(pam_handle_t *pamh, struct options *opts, const char *cfgfile)
b51b82
+{
b51b82
+	char linebuf[FAILLOCK_CONF_MAX_LINELEN+1];
b51b82
+	const char *fname = (cfgfile != NULL) ? cfgfile : FAILLOCK_DEFAULT_CONF;
b51b82
+	FILE *f = fopen(fname, "r");
b51b82
+
b51b82
+#ifdef VENDOR_FAILLOCK_DEFAULT_CONF
b51b82
+	if (f == NULL && errno == ENOENT && cfgfile == NULL) {
b51b82
+		/*
b51b82
+		 * If the default configuration file in /etc does not exist,
b51b82
+		 * try the vendor configuration file as fallback.
b51b82
+		 */
b51b82
+		f = fopen(VENDOR_FAILLOCK_DEFAULT_CONF, "r");
b51b82
+	}
b51b82
+#endif /* VENDOR_FAILLOCK_DEFAULT_CONF */
b51b82
+
b51b82
+	if (f == NULL) {
b51b82
+		/* ignore non-existent default config file */
b51b82
+		if (errno == ENOENT && cfgfile == NULL)
b51b82
+			return PAM_SUCCESS;
b51b82
+		return PAM_SERVICE_ERR;
b51b82
+	}
b51b82
+
b51b82
+	while (fgets(linebuf, sizeof(linebuf), f) != NULL) {
b51b82
+		size_t len;
b51b82
+		char *ptr;
b51b82
+		char *name;
b51b82
+		int eq;
b51b82
+
b51b82
+		len = strlen(linebuf);
b51b82
+		/* len cannot be 0 unless there is a bug in fgets */
b51b82
+		if (len && linebuf[len - 1] != '\n' && !feof(f)) {
b51b82
+			(void) fclose(f);
b51b82
+			return PAM_SERVICE_ERR;
b51b82
+		}
b51b82
+
b51b82
+		if ((ptr=strchr(linebuf, '#')) != NULL) {
b51b82
+			*ptr = '\0';
b51b82
+		} else {
b51b82
+			ptr = linebuf + len;
b51b82
+		}
b51b82
+
b51b82
+		/* drop terminating whitespace including the \n */
b51b82
+		while (ptr > linebuf) {
b51b82
+			if (!isspace(*(ptr-1))) {
b51b82
+				*ptr = '\0';
b51b82
+				break;
b51b82
+			}
b51b82
+			--ptr;
b51b82
+		}
b51b82
+
b51b82
+		/* skip initial whitespace */
b51b82
+		for (ptr = linebuf; isspace(*ptr); ptr++);
b51b82
+		if (*ptr == '\0')
b51b82
+			continue;
b51b82
+
b51b82
+		/* grab the key name */
b51b82
+		eq = 0;
b51b82
+		name = ptr;
b51b82
+		while (*ptr != '\0') {
b51b82
+			if (isspace(*ptr) || *ptr == '=') {
b51b82
+				eq = *ptr == '=';
b51b82
+				*ptr = '\0';
b51b82
+				++ptr;
b51b82
+				break;
b51b82
+			}
b51b82
+			++ptr;
b51b82
+		}
b51b82
+
b51b82
+		/* grab the key value */
b51b82
+		while (*ptr != '\0') {
b51b82
+			if (*ptr != '=' || eq) {
b51b82
+				if (!isspace(*ptr)) {
b51b82
+					break;
b51b82
+				}
b51b82
+			} else {
b51b82
+				eq = 1;
b51b82
+			}
b51b82
+			++ptr;
b51b82
+		}
b51b82
+
b51b82
+		/* set the key:value pair on opts */
b51b82
+		set_conf_opt(pamh, opts, name, ptr);
b51b82
+	}
b51b82
+
b51b82
+	(void)fclose(f);
b51b82
+	return PAM_SUCCESS;
b51b82
+}
b51b82
+
b51b82
+void
b51b82
+set_conf_opt(pam_handle_t *pamh, struct options *opts, const char *name,
b51b82
+			 const char *value)
b51b82
+{
b51b82
+	if (strcmp(name, "dir") == 0) {
b51b82
+		if (value[0] != '/') {
b51b82
+			config_log(pamh, LOG_ERR,
b51b82
+					"Tally directory is not absolute path (%s); keeping value",
b51b82
+					value);
b51b82
+		} else {
b51b82
+			free(opts->dir);
b51b82
+			opts->dir = strdup(value);
b51b82
+			if (opts->dir == NULL) {
b51b82
+				opts->fatal_error = 1;
b51b82
+				config_log(pamh, LOG_CRIT, "Error allocating memory: %m");
b51b82
+			}
b51b82
+		}
b51b82
+	}
b51b82
+	else if (strcmp(name, "deny") == 0) {
b51b82
+		if (sscanf(value, "%hu", &opts->deny) != 1) {
b51b82
+			config_log(pamh, LOG_ERR,
b51b82
+				"Bad number supplied for deny argument");
b51b82
+		}
b51b82
+	}
b51b82
+	else if (strcmp(name, "fail_interval") == 0) {
b51b82
+		unsigned int temp;
b51b82
+		if (sscanf(value, "%u", &temp) != 1 ||
b51b82
+			temp > MAX_TIME_INTERVAL) {
b51b82
+			config_log(pamh, LOG_ERR,
b51b82
+				"Bad number supplied for fail_interval argument");
b51b82
+		} else {
b51b82
+			opts->fail_interval = temp;
b51b82
+		}
b51b82
+	}
b51b82
+	else if (strcmp(name, "unlock_time") == 0) {
b51b82
+		unsigned int temp;
b51b82
+
b51b82
+		if (strcmp(value, "never") == 0) {
b51b82
+			opts->unlock_time = 0;
b51b82
+		}
b51b82
+		else if (sscanf(value, "%u", &temp) != 1 ||
b51b82
+			temp > MAX_TIME_INTERVAL) {
b51b82
+			config_log(pamh, LOG_ERR,
b51b82
+				"Bad number supplied for unlock_time argument");
b51b82
+		}
b51b82
+		else {
b51b82
+			opts->unlock_time = temp;
b51b82
+		}
b51b82
+	}
b51b82
+	else if (strcmp(name, "root_unlock_time") == 0) {
b51b82
+		unsigned int temp;
b51b82
+
b51b82
+		if (strcmp(value, "never") == 0) {
b51b82
+			opts->root_unlock_time = 0;
b51b82
+		}
b51b82
+		else if (sscanf(value, "%u", &temp) != 1 ||
b51b82
+			temp > MAX_TIME_INTERVAL) {
b51b82
+			config_log(pamh, LOG_ERR,
b51b82
+				"Bad number supplied for root_unlock_time argument");
b51b82
+		} else {
b51b82
+			opts->root_unlock_time = temp;
b51b82
+		}
b51b82
+	}
b51b82
+	else if (strcmp(name, "admin_group") == 0) {
b51b82
+		free(opts->admin_group);
b51b82
+		opts->admin_group = strdup(value);
b51b82
+		if (opts->admin_group == NULL) {
b51b82
+			opts->fatal_error = 1;
b51b82
+			config_log(pamh, LOG_CRIT, "Error allocating memory: %m");
b51b82
+		}
b51b82
+	}
b51b82
+	else if (strcmp(name, "even_deny_root") == 0) {
b51b82
+		opts->flags |= FAILLOCK_FLAG_DENY_ROOT;
b51b82
+	}
b51b82
+	else if (strcmp(name, "audit") == 0) {
b51b82
+		opts->flags |= FAILLOCK_FLAG_AUDIT;
b51b82
+	}
b51b82
+	else if (strcmp(name, "silent") == 0) {
b51b82
+		opts->flags |= FAILLOCK_FLAG_SILENT;
b51b82
+	}
b51b82
+	else if (strcmp(name, "no_log_info") == 0) {
b51b82
+		opts->flags |= FAILLOCK_FLAG_NO_LOG_INFO;
b51b82
+	}
b51b82
+	else if (strcmp(name, "local_users_only") == 0) {
b51b82
+		opts->flags |= FAILLOCK_FLAG_LOCAL_ONLY;
b51b82
+	}
b51b82
+	else if (strcmp(name, "nodelay") == 0) {
b51b82
+		opts->flags |= FAILLOCK_FLAG_NO_DELAY;
b51b82
+	}
b51b82
+	else {
b51b82
+		config_log(pamh, LOG_ERR, "Unknown option: %s", name);
b51b82
+	}
b51b82
+}
b51b82
+
b51b82
+const char *get_tally_dir(const struct options *opts)
b51b82
+{
b51b82
+	return (opts->dir != NULL) ? opts->dir : FAILLOCK_DEFAULT_TALLYDIR;
b51b82
+}
b51b82
diff -up Linux-PAM-1.3.1/modules/pam_faillock/faillock_config.h.faillock-load-conf-from-file Linux-PAM-1.3.1/modules/pam_faillock/faillock_config.h
b51b82
--- Linux-PAM-1.3.1/modules/pam_faillock/faillock_config.h.faillock-load-conf-from-file	2022-05-26 10:57:11.747067749 +0200
b51b82
+++ Linux-PAM-1.3.1/modules/pam_faillock/faillock_config.h	2022-05-26 10:57:11.747067749 +0200
b51b82
@@ -0,0 +1,89 @@
b51b82
+/*
b51b82
+ * Copyright (c) 2022 Tomas Mraz <tm@t8m.info>
b51b82
+ * Copyright (c) 2022 Iker Pedrosa <ipedrosa@redhat.com>
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
+/*
b51b82
+ * faillock_config.h - load configuration options from file
b51b82
+ *
b51b82
+ */
b51b82
+
b51b82
+#ifndef _FAILLOCK_CONFIG_H
b51b82
+#define _FAILLOCK_CONFIG_H
b51b82
+
b51b82
+#include <limits.h>
b51b82
+#include <stdint.h>
b51b82
+#include <sys/types.h>
b51b82
+
b51b82
+#include <security/pam_ext.h>
b51b82
+
b51b82
+#define FAILLOCK_FLAG_DENY_ROOT		0x1
b51b82
+#define FAILLOCK_FLAG_AUDIT			0x2
b51b82
+#define FAILLOCK_FLAG_SILENT		0x4
b51b82
+#define FAILLOCK_FLAG_NO_LOG_INFO	0x8
b51b82
+#define FAILLOCK_FLAG_UNLOCKED		0x10
b51b82
+#define FAILLOCK_FLAG_LOCAL_ONLY	0x20
b51b82
+#define FAILLOCK_FLAG_NO_DELAY		0x40
b51b82
+
b51b82
+#define FAILLOCK_CONF_MAX_LINELEN 	1023
b51b82
+#define MAX_TIME_INTERVAL			604800 /* 7 days */
b51b82
+
b51b82
+struct options {
b51b82
+	unsigned int action;
b51b82
+	unsigned int flags;
b51b82
+	unsigned short deny;
b51b82
+	unsigned int fail_interval;
b51b82
+	unsigned int unlock_time;
b51b82
+	unsigned int root_unlock_time;
b51b82
+	char *dir;
b51b82
+	const char *user;
b51b82
+	char *admin_group;
b51b82
+	int failures;
b51b82
+	uint64_t latest_time;
b51b82
+	uid_t uid;
b51b82
+	int is_admin;
b51b82
+	uint64_t now;
b51b82
+	int fatal_error;
b51b82
+
b51b82
+	unsigned int reset;
b51b82
+	const char *progname;
b51b82
+};
b51b82
+
b51b82
+int read_config_file(pam_handle_t *pamh, struct options *opts,
b51b82
+					 const char *cfgfile);
b51b82
+void set_conf_opt(pam_handle_t *pamh, struct options *opts, const char *name,
b51b82
+		  const char *value);
b51b82
+const char *get_tally_dir(const struct options *opts);
b51b82
+
b51b82
+#endif /* _FAILLOCK_CONFIG_H */
b51b82
diff -up Linux-PAM-1.3.1/modules/pam_faillock/main.c.faillock-load-conf-from-file Linux-PAM-1.3.1/modules/pam_faillock/main.c
b51b82
--- Linux-PAM-1.3.1/modules/pam_faillock/main.c.faillock-load-conf-from-file	2022-05-26 10:57:11.713067506 +0200
b51b82
+++ Linux-PAM-1.3.1/modules/pam_faillock/main.c	2022-05-26 10:57:11.747067749 +0200
b51b82
@@ -48,43 +48,50 @@
b51b82
 #include <libaudit.h>
b51b82
 #endif
b51b82
 
b51b82
+#include "pam_inline.h"
b51b82
 #include "faillock.h"
b51b82
-
b51b82
-struct options {
b51b82
-	unsigned int reset;
b51b82
-	const char *dir;
b51b82
-	const char *user;
b51b82
-	const char *progname;
b51b82
-};
b51b82
+#include "faillock_config.h"
b51b82
 
b51b82
 static int
b51b82
 args_parse(int argc, char **argv, struct options *opts)
b51b82
 {
b51b82
 	int i;
b51b82
+	int rv;
b51b82
+	const char *dir = NULL;
b51b82
+	const char *conf = NULL;
b51b82
+
b51b82
 	memset(opts, 0, sizeof(*opts));
b51b82
 
b51b82
-	opts->dir = FAILLOCK_DEFAULT_TALLYDIR;
b51b82
 	opts->progname = argv[0];
b51b82
 
b51b82
 	for (i = 1; i < argc; ++i) {
b51b82
-
b51b82
-		if (strcmp(argv[i], "--dir") == 0) {
b51b82
+		if (strcmp(argv[i], "--conf") == 0) {
b51b82
+			++i;
b51b82
+			if (i >= argc || strlen(argv[i]) == 0) {
b51b82
+				fprintf(stderr, "%s: No configuration file supplied.\n",
b51b82
+						argv[0]);
b51b82
+				return -1;
b51b82
+			}
b51b82
+			conf = argv[i];
b51b82
+		}
b51b82
+		else if (strcmp(argv[i], "--dir") == 0) {
b51b82
 			++i;
b51b82
 			if (i >= argc || strlen(argv[i]) == 0) {
b51b82
-				fprintf(stderr, "%s: No directory supplied.\n", argv[0]);				
b51b82
+				fprintf(stderr, "%s: No records directory supplied.\n",
b51b82
+						argv[0]);
b51b82
 				return -1;
b51b82
 			}
b51b82
-		        opts->dir = argv[i];
b51b82
-		} 
b51b82
+			dir = argv[i];
b51b82
+		}
b51b82
 		else if (strcmp(argv[i], "--user") == 0) {
b51b82
 			++i;
b51b82
 			if (i >= argc || strlen(argv[i]) == 0) {
b51b82
-				fprintf(stderr, "%s: No user name supplied.\n", argv[0]);				
b51b82
+				fprintf(stderr, "%s: No user name supplied.\n", argv[0]);
b51b82
 				return -1;
b51b82
 			}
b51b82
-		        opts->user = argv[i];
b51b82
+			opts->user = argv[i];
b51b82
 		}
b51b82
- 		else if (strcmp(argv[i], "--reset") == 0) {
b51b82
+		else if (strcmp(argv[i], "--reset") == 0) {
b51b82
 			opts->reset = 1;
b51b82
 		}
b51b82
 		else {
b51b82
@@ -92,6 +99,21 @@ args_parse(int argc, char **argv, struct
b51b82
 			return -1;
b51b82
 		}
b51b82
 	}
b51b82
+
b51b82
+	if ((rv = read_config_file(NULL, opts, conf)) != PAM_SUCCESS) {
b51b82
+		fprintf(stderr, "Configuration file missing or broken");
b51b82
+		return rv;
b51b82
+	}
b51b82
+
b51b82
+	if (dir != NULL) {
b51b82
+		free(opts->dir);
b51b82
+		opts->dir = strdup(dir);
b51b82
+		if (opts->dir == NULL) {
b51b82
+			fprintf(stderr, "Error allocating memory: %m");
b51b82
+			return -1;
b51b82
+		}
b51b82
+	}
b51b82
+
b51b82
 	return 0;
b51b82
 }
b51b82
 
b51b82
@@ -109,10 +131,11 @@ do_user(struct options *opts, const char
b51b82
 	int rv;
b51b82
 	struct tally_data tallies;
b51b82
 	struct passwd *pwd;
b51b82
+	const char *dir = get_tally_dir(opts);
b51b82
 
b51b82
 	pwd = getpwnam(user);
b51b82
 
b51b82
-	fd = open_tally(opts->dir, user, pwd != NULL ? pwd->pw_uid : 0, 0);
b51b82
+	fd = open_tally(dir, user, pwd != NULL ? pwd->pw_uid : 0, 0);
b51b82
 
b51b82
 	if (fd == -1) {
b51b82
 		if (errno == ENOENT) {
b51b82
@@ -189,11 +212,11 @@ do_allusers(struct options *opts)
b51b82
 {
b51b82
 	struct dirent **userlist;
b51b82
 	int rv, i;
b51b82
+	const char *dir = get_tally_dir(opts);
b51b82
 
b51b82
-	rv = scandir(opts->dir, &userlist, NULL, alphasort);
b51b82
+	rv = scandir(dir, &userlist, NULL, alphasort);
b51b82
 	if (rv < 0) {
b51b82
-		fprintf(stderr, "%s: Error reading tally directory: ", opts->progname);
b51b82
-		perror(NULL);
b51b82
+		fprintf(stderr, "%s: Error reading tally directory: %m\n", opts->progname);
b51b82
 		return 2;
b51b82
 	}
b51b82
 
b51b82
diff -up Linux-PAM-1.3.1/modules/pam_faillock/Makefile.am.faillock-load-conf-from-file Linux-PAM-1.3.1/modules/pam_faillock/Makefile.am
b51b82
--- Linux-PAM-1.3.1/modules/pam_faillock/Makefile.am.faillock-load-conf-from-file	2022-05-26 10:57:11.727067606 +0200
b51b82
+++ Linux-PAM-1.3.1/modules/pam_faillock/Makefile.am	2022-05-26 10:57:59.032406450 +0200
b51b82
@@ -17,7 +17,7 @@ TESTS = tst-pam_faillock
b51b82
 securelibdir = $(SECUREDIR)
b51b82
 secureconfdir = $(SCONFIGDIR)
b51b82
 
b51b82
-noinst_HEADERS = faillock.h
b51b82
+noinst_HEADERS = faillock.h faillock_config.h
b51b82
 
b51b82
 faillock_CFLAGS = -I$(top_srcdir)/libpam/include -I$(top_srcdir)/libpamc/include @PIE_CFLAGS@
b51b82
 pam_faillock_la_CFLAGS = -I$(top_srcdir)/libpam/include -I$(top_srcdir)/libpamc/include
b51b82
@@ -36,8 +36,8 @@ secureconf_DATA = faillock.conf
b51b82
 securelib_LTLIBRARIES = pam_faillock.la
b51b82
 sbin_PROGRAMS = faillock
b51b82
 
b51b82
-pam_faillock_la_SOURCES = pam_faillock.c faillock.c
b51b82
-faillock_SOURCES = main.c faillock.c
b51b82
+pam_faillock_la_SOURCES = pam_faillock.c faillock.c faillock_config.c
b51b82
+faillock_SOURCES = main.c faillock.c faillock_config.c
b51b82
 
b51b82
 if ENABLE_REGENERATE_MAN
b51b82
 noinst_DATA = README
b51b82
diff -up Linux-PAM-1.3.1/modules/pam_faillock/pam_faillock.c.faillock-load-conf-from-file Linux-PAM-1.3.1/modules/pam_faillock/pam_faillock.c
b51b82
--- Linux-PAM-1.3.1/modules/pam_faillock/pam_faillock.c.faillock-load-conf-from-file	2022-05-26 10:57:11.727067606 +0200
b51b82
+++ Linux-PAM-1.3.1/modules/pam_faillock/pam_faillock.c	2022-05-26 10:57:11.748067756 +0200
b51b82
@@ -38,7 +38,6 @@
b51b82
 #include <stdio.h>
b51b82
 #include <string.h>
b51b82
 #include <unistd.h>
b51b82
-#include <stdint.h>
b51b82
 #include <stdlib.h>
b51b82
 #include <errno.h>
b51b82
 #include <time.h>
b51b82
@@ -54,83 +53,50 @@
b51b82
 #include <security/pam_modutil.h>
b51b82
 #include <security/pam_ext.h>
b51b82
 
b51b82
+#include "pam_inline.h"
b51b82
 #include "faillock.h"
b51b82
-
b51b82
-#define PAM_SM_AUTH
b51b82
-#define PAM_SM_ACCOUNT
b51b82
+#include "faillock_config.h"
b51b82
 
b51b82
 #define FAILLOCK_ACTION_PREAUTH  0
b51b82
 #define FAILLOCK_ACTION_AUTHSUCC 1
b51b82
 #define FAILLOCK_ACTION_AUTHFAIL 2
b51b82
 
b51b82
-#define FAILLOCK_FLAG_DENY_ROOT		0x1
b51b82
-#define FAILLOCK_FLAG_AUDIT		0x2
b51b82
-#define FAILLOCK_FLAG_SILENT		0x4
b51b82
-#define FAILLOCK_FLAG_NO_LOG_INFO	0x8
b51b82
-#define FAILLOCK_FLAG_UNLOCKED		0x10
b51b82
-#define FAILLOCK_FLAG_LOCAL_ONLY	0x20
b51b82
-
b51b82
-#define MAX_TIME_INTERVAL 604800 /* 7 days */
b51b82
-#define FAILLOCK_CONF_MAX_LINELEN 1023
b51b82
-#define FAILLOCK_ERROR_CONF_OPEN -3
b51b82
-#define FAILLOCK_ERROR_CONF_MALFORMED -4
b51b82
-
b51b82
-#define PATH_PASSWD "/etc/passwd"
b51b82
-
b51b82
-struct options {
b51b82
-	unsigned int action;
b51b82
-	unsigned int flags;
b51b82
-	unsigned short deny;
b51b82
-	unsigned int fail_interval;
b51b82
-	unsigned int unlock_time;
b51b82
-	unsigned int root_unlock_time;
b51b82
-	char *dir;
b51b82
-	const char *conf;
b51b82
-	const char *user;
b51b82
-	char *admin_group;
b51b82
-	int failures;
b51b82
-	uint64_t latest_time;
b51b82
-	uid_t uid;
b51b82
-	int is_admin;
b51b82
-	uint64_t now;
b51b82
-	int fatal_error;
b51b82
-};
b51b82
-
b51b82
-int read_config_file(
b51b82
-	pam_handle_t *pamh,
b51b82
-	struct options *opts,
b51b82
-	const char *cfgfile
b51b82
-);
b51b82
-
b51b82
-void set_conf_opt(
b51b82
-	pam_handle_t *pamh,
b51b82
-	struct options *opts,
b51b82
-	const char *name,
b51b82
-	const char *value
b51b82
-);
b51b82
-
b51b82
-static void
b51b82
+static int
b51b82
 args_parse(pam_handle_t *pamh, int argc, const char **argv,
b51b82
 		int flags, struct options *opts)
b51b82
 {
b51b82
 	int i;
b51b82
+	int config_arg_index = -1;
b51b82
 	int rv;
b51b82
+	const char *conf = NULL;
b51b82
+
b51b82
 	memset(opts, 0, sizeof(*opts));
b51b82
 
b51b82
-	opts->dir = strdup(FAILLOCK_DEFAULT_TALLYDIR);
b51b82
-	opts->conf = FAILLOCK_DEFAULT_CONF;
b51b82
 	opts->deny = 3;
b51b82
 	opts->fail_interval = 900;
b51b82
 	opts->unlock_time = 600;
b51b82
 	opts->root_unlock_time = MAX_TIME_INTERVAL+1;
b51b82
 
b51b82
-	if ((rv=read_config_file(pamh, opts, opts->conf)) != PAM_SUCCESS) {
b51b82
-		pam_syslog(pamh, LOG_DEBUG,
b51b82
-					"Configuration file missing");
b51b82
+	for (i = 0; i < argc; ++i) {
b51b82
+		const char *str = pam_str_skip_prefix(argv[i], "conf=");
b51b82
+
b51b82
+		if (str != NULL) {
b51b82
+			conf = str;
b51b82
+			config_arg_index = i;
b51b82
+		}
b51b82
+	}
b51b82
+
b51b82
+	if ((rv = read_config_file(pamh, opts, conf)) != PAM_SUCCESS) {
b51b82
+		pam_syslog(pamh, LOG_ERR,
b51b82
+					"Configuration file missing or broken");
b51b82
+		return rv;
b51b82
 	}
b51b82
 
b51b82
 	for (i = 0; i < argc; ++i) {
b51b82
-		if (strcmp(argv[i], "preauth") == 0) {
b51b82
+		if (i == config_arg_index) {
b51b82
+			continue;
b51b82
+		}
b51b82
+		else if (strcmp(argv[i], "preauth") == 0) {
b51b82
 			opts->action = FAILLOCK_ACTION_PREAUTH;
b51b82
 		}
b51b82
 		else if (strcmp(argv[i], "authfail") == 0) {
b51b82
@@ -163,226 +129,26 @@ args_parse(pam_handle_t *pamh, int argc,
b51b82
 	if (flags & PAM_SILENT)
b51b82
 		opts->flags |= FAILLOCK_FLAG_SILENT;
b51b82
 
b51b82
-	if (opts->dir == NULL) {
b51b82
-		pam_syslog(pamh, LOG_CRIT, "Error allocating memory: %m");
b51b82
-		opts->fatal_error = 1;
b51b82
-	}
b51b82
-}
b51b82
-
b51b82
-/* parse a single configuration file */
b51b82
-int
b51b82
-read_config_file(pam_handle_t *pamh, struct options *opts, const char *cfgfile)
b51b82
-{
b51b82
-	FILE *f;
b51b82
-	char linebuf[FAILLOCK_CONF_MAX_LINELEN+1];
b51b82
-
b51b82
-	f = fopen(cfgfile, "r");
b51b82
-	if (f == NULL) {
b51b82
-		/* ignore non-existent default config file */
b51b82
-		if (errno == ENOENT && strcmp(cfgfile, FAILLOCK_DEFAULT_CONF) == 0)
b51b82
-			return 0;
b51b82
-		return FAILLOCK_ERROR_CONF_OPEN;
b51b82
-	}
b51b82
-
b51b82
-	while (fgets(linebuf, sizeof(linebuf), f) != NULL) {
b51b82
-		size_t len;
b51b82
-		char *ptr;
b51b82
-		char *name;
b51b82
-		int eq;
b51b82
-
b51b82
-		len = strlen(linebuf);
b51b82
-		/* len cannot be 0 unless there is a bug in fgets */
b51b82
-		if (len && linebuf[len - 1] != '\n' && !feof(f)) {
b51b82
-			(void) fclose(f);
b51b82
-			return FAILLOCK_ERROR_CONF_MALFORMED;
b51b82
-		}
b51b82
-
b51b82
-		if ((ptr=strchr(linebuf, '#')) != NULL) {
b51b82
-			*ptr = '\0';
b51b82
-		} else {
b51b82
-			ptr = linebuf + len;
b51b82
-		}
b51b82
-
b51b82
-		/* drop terminating whitespace including the \n */
b51b82
-		while (ptr > linebuf) {
b51b82
-			if (!isspace(*(ptr-1))) {
b51b82
-				*ptr = '\0';
b51b82
-				break;
b51b82
-			}
b51b82
-			--ptr;
b51b82
-		}
b51b82
-
b51b82
-		/* skip initial whitespace */
b51b82
-		for (ptr = linebuf; isspace(*ptr); ptr++);
b51b82
-		if (*ptr == '\0')
b51b82
-			continue;
b51b82
-
b51b82
-		/* grab the key name */
b51b82
-		eq = 0;
b51b82
-		name = ptr;
b51b82
-		while (*ptr != '\0') {
b51b82
-			if (isspace(*ptr) || *ptr == '=') {
b51b82
-				eq = *ptr == '=';
b51b82
-				*ptr = '\0';
b51b82
-				++ptr;
b51b82
-				break;
b51b82
-			}
b51b82
-			++ptr;
b51b82
-		}
b51b82
-
b51b82
-		/* grab the key value */
b51b82
-		while (*ptr != '\0') {
b51b82
-			if (*ptr != '=' || eq) {
b51b82
-				if (!isspace(*ptr)) {
b51b82
-					break;
b51b82
-				}
b51b82
-			} else {
b51b82
-				eq = 1;
b51b82
-			}
b51b82
-			++ptr;
b51b82
-		}
b51b82
-
b51b82
-		/* set the key:value pair on opts */
b51b82
-		set_conf_opt(pamh, opts, name, ptr);
b51b82
-	}
b51b82
-
b51b82
-	(void)fclose(f);
b51b82
+	if (opts->fatal_error)
b51b82
+		return PAM_BUF_ERR;
b51b82
 	return PAM_SUCCESS;
b51b82
 }
b51b82
 
b51b82
-void set_conf_opt(pam_handle_t *pamh, struct options *opts, const char *name, const char *value)
b51b82
-{
b51b82
-	if (strcmp(name, "dir") == 0) {
b51b82
-		if (value[0] != '/') {
b51b82
-			pam_syslog(pamh, LOG_ERR,
b51b82
-				"Tally directory is not absolute path (%s); keeping default", value);
b51b82
-		} else {
b51b82
-			free(opts->dir);
b51b82
-			opts->dir = strdup(value);
b51b82
-		}
b51b82
-	}
b51b82
-	else if (strcmp(name, "deny") == 0) {
b51b82
-		if (sscanf(value, "%hu", &opts->deny) != 1) {
b51b82
-			pam_syslog(pamh, LOG_ERR,
b51b82
-				"Bad number supplied for deny argument");
b51b82
-		}
b51b82
-	}
b51b82
-	else if (strcmp(name, "fail_interval") == 0) {
b51b82
-		unsigned int temp;
b51b82
-		if (sscanf(value, "%u", &temp) != 1 ||
b51b82
-			temp > MAX_TIME_INTERVAL) {
b51b82
-			pam_syslog(pamh, LOG_ERR,
b51b82
-				"Bad number supplied for fail_interval argument");
b51b82
-		} else {
b51b82
-			opts->fail_interval = temp;
b51b82
-		}
b51b82
-	}
b51b82
-	else if (strcmp(name, "unlock_time") == 0) {
b51b82
-		unsigned int temp;
b51b82
-
b51b82
-		if (strcmp(value, "never") == 0) {
b51b82
-			opts->unlock_time = 0;
b51b82
-		}
b51b82
-		else if (sscanf(value, "%u", &temp) != 1 ||
b51b82
-			temp > MAX_TIME_INTERVAL) {
b51b82
-			pam_syslog(pamh, LOG_ERR,
b51b82
-				"Bad number supplied for unlock_time argument");
b51b82
-		}
b51b82
-		else {
b51b82
-			opts->unlock_time = temp;
b51b82
-		}
b51b82
-	}
b51b82
-	else if (strcmp(name, "root_unlock_time") == 0) {
b51b82
-		unsigned int temp;
b51b82
-
b51b82
-		if (strcmp(value, "never") == 0) {
b51b82
-			opts->root_unlock_time = 0;
b51b82
-		}
b51b82
-		else if (sscanf(value, "%u", &temp) != 1 ||
b51b82
-			temp > MAX_TIME_INTERVAL) {
b51b82
-			pam_syslog(pamh, LOG_ERR,
b51b82
-				"Bad number supplied for root_unlock_time argument");
b51b82
-		} else {
b51b82
-			opts->root_unlock_time = temp;
b51b82
-		}
b51b82
-	}
b51b82
-	else if (strcmp(name, "admin_group") == 0) {
b51b82
-		free(opts->admin_group);
b51b82
-		opts->admin_group = strdup(value);
b51b82
-		if (opts->admin_group == NULL) {
b51b82
-			opts->fatal_error = 1;
b51b82
-			pam_syslog(pamh, LOG_CRIT, "Error allocating memory: %m");
b51b82
-		}
b51b82
-	}
b51b82
-	else if (strcmp(name, "even_deny_root") == 0) {
b51b82
-		opts->flags |= FAILLOCK_FLAG_DENY_ROOT;
b51b82
-	}
b51b82
-	else if (strcmp(name, "audit") == 0) {
b51b82
-		opts->flags |= FAILLOCK_FLAG_AUDIT;
b51b82
-	}
b51b82
-	else if (strcmp(name, "silent") == 0) {
b51b82
-		opts->flags |= FAILLOCK_FLAG_SILENT;
b51b82
-	}
b51b82
-	else if (strcmp(name, "no_log_info") == 0) {
b51b82
-		opts->flags |= FAILLOCK_FLAG_NO_LOG_INFO;
b51b82
-	}
b51b82
-	else if (strcmp(name, "local_users_only") == 0) {
b51b82
-		opts->flags |= FAILLOCK_FLAG_LOCAL_ONLY;
b51b82
-	}
b51b82
-	else {
b51b82
-		pam_syslog(pamh, LOG_ERR, "Unknown option: %s", name);
b51b82
-	}
b51b82
-}
b51b82
-
b51b82
-static int check_local_user (pam_handle_t *pamh, const char *user)
b51b82
+static int
b51b82
+check_local_user (pam_handle_t *pamh, const char *user)
b51b82
 {
b51b82
-	struct passwd pw, *pwp;
b51b82
-	char buf[4096];
b51b82
-	int found = 0;
b51b82
-	FILE *fp;
b51b82
-	int errn;
b51b82
-
b51b82
-	fp = fopen(PATH_PASSWD, "r");
b51b82
-	if (fp == NULL) {
b51b82
-		pam_syslog(pamh, LOG_ERR, "unable to open %s: %m",
b51b82
-			   PATH_PASSWD);
b51b82
-		return -1;
b51b82
-	}
b51b82
-
b51b82
-	for (;;) {
b51b82
-		errn = fgetpwent_r(fp, &pw, buf, sizeof (buf), &pwp);
b51b82
-		if (errn == ERANGE) {
b51b82
-			pam_syslog(pamh, LOG_WARNING, "%s contains very long lines; corrupted?",
b51b82
-				   PATH_PASSWD);
b51b82
-			/* we can continue here as next call will read further */
b51b82
-			continue;
b51b82
-		}
b51b82
-		if (errn != 0)
b51b82
-			break;
b51b82
-		if (strcmp(pwp->pw_name, user) == 0) {
b51b82
-			found = 1;
b51b82
-			break;
b51b82
-		}
b51b82
-	}
b51b82
-
b51b82
-	fclose (fp);
b51b82
-
b51b82
-	if (errn != 0 && errn != ENOENT) {
b51b82
-		pam_syslog(pamh, LOG_ERR, "unable to enumerate local accounts: %m");
b51b82
-		return -1;
b51b82
-	} else {
b51b82
-		return found;
b51b82
-	}
b51b82
+	return pam_modutil_check_user_in_passwd(pamh, user, NULL) == PAM_SUCCESS;
b51b82
 }
b51b82
 
b51b82
-static int get_pam_user(pam_handle_t *pamh, struct options *opts)
b51b82
+static int
b51b82
+get_pam_user(pam_handle_t *pamh, struct options *opts)
b51b82
 {
b51b82
 	const char *user;
b51b82
 	int rv;
b51b82
 	struct passwd *pwd;
b51b82
 
b51b82
 	if ((rv=pam_get_user(pamh, &user, NULL)) != PAM_SUCCESS) {
b51b82
-		return rv;
b51b82
+		return rv == PAM_CONV_AGAIN ? PAM_INCOMPLETE : rv;
b51b82
 	}
b51b82
 
b51b82
 	if (*user == '\0') {
b51b82
@@ -391,10 +157,10 @@ static int get_pam_user(pam_handle_t *pa
b51b82
 
b51b82
 	if ((pwd=pam_modutil_getpwnam(pamh, user)) == NULL) {
b51b82
 		if (opts->flags & FAILLOCK_FLAG_AUDIT) {
b51b82
-			pam_syslog(pamh, LOG_ERR, "User unknown: %s", user);
b51b82
+			pam_syslog(pamh, LOG_NOTICE, "User unknown: %s", user);
b51b82
 		}
b51b82
 		else {
b51b82
-			pam_syslog(pamh, LOG_ERR, "User unknown");
b51b82
+			pam_syslog(pamh, LOG_NOTICE, "User unknown");
b51b82
 		}
b51b82
 		return PAM_IGNORE;
b51b82
 	}
b51b82
@@ -421,10 +187,11 @@ check_tally(pam_handle_t *pamh, struct o
b51b82
 	unsigned int i;
b51b82
 	uint64_t latest_time;
b51b82
 	int failures;
b51b82
+	const char *dir = get_tally_dir(opts);
b51b82
 
b51b82
 	opts->now = time(NULL);
b51b82
 
b51b82
-	tfd = open_tally(opts->dir, opts->user, opts->uid, 0);
b51b82
+	tfd = open_tally(dir, opts->user, opts->uid, 0);
b51b82
 
b51b82
 	*fd = tfd;
b51b82
 
b51b82
@@ -446,7 +213,7 @@ check_tally(pam_handle_t *pamh, struct o
b51b82
 	}
b51b82
 
b51b82
 	latest_time = 0;
b51b82
-	for(i = 0; i < tallies->count; i++) {
b51b82
+	for (i = 0; i < tallies->count; i++) {
b51b82
 		if ((tallies->records[i].status & TALLY_STATUS_VALID) &&
b51b82
 			tallies->records[i].time > latest_time)
b51b82
 			latest_time = tallies->records[i].time;
b51b82
@@ -455,7 +222,7 @@ check_tally(pam_handle_t *pamh, struct o
b51b82
 	opts->latest_time = latest_time;
b51b82
 
b51b82
 	failures = 0;
b51b82
-	for(i = 0; i < tallies->count; i++) {
b51b82
+	for (i = 0; i < tallies->count; i++) {
b51b82
 		if ((tallies->records[i].status & TALLY_STATUS_VALID) &&
b51b82
 			latest_time - tallies->records[i].time < opts->fail_interval) {
b51b82
 			++failures;
b51b82
@@ -498,9 +265,10 @@ static void
b51b82
 reset_tally(pam_handle_t *pamh, struct options *opts, int *fd)
b51b82
 {
b51b82
 	int rv;
b51b82
+	const char *dir = get_tally_dir(opts);
b51b82
 
b51b82
 	if (*fd == -1) {
b51b82
-		*fd = open_tally(opts->dir, opts->user, opts->uid, 1);
b51b82
+		*fd = open_tally(dir, opts->user, opts->uid, 1);
b51b82
 	}
b51b82
 	else {
b51b82
 		while ((rv=ftruncate(*fd, 0)) == -1 && errno == EINTR);
b51b82
@@ -519,9 +287,10 @@ write_tally(pam_handle_t *pamh, struct o
b51b82
 	unsigned int oldest;
b51b82
 	uint64_t oldtime;
b51b82
 	const void *source = NULL;
b51b82
+	const char *dir = get_tally_dir(opts);
b51b82
 
b51b82
 	if (*fd == -1) {
b51b82
-		*fd = open_tally(opts->dir, opts->user, opts->uid, 1);
b51b82
+		*fd = open_tally(dir, opts->user, opts->uid, 1);
b51b82
 	}
b51b82
 	if (*fd == -1) {
b51b82
 		if (errno == EACCES) {
b51b82
@@ -536,7 +305,7 @@ write_tally(pam_handle_t *pamh, struct o
b51b82
 	failures = 0;
b51b82
 
b51b82
 	for (i = 0; i < tallies->count; ++i) {
b51b82
-		if (tallies->records[i].time < oldtime) {
b51b82
+		if (oldtime == 0 || tallies->records[i].time < oldtime) {
b51b82
 			oldtime = tallies->records[i].time;
b51b82
 			oldest = i;
b51b82
 		}
b51b82
@@ -630,16 +399,26 @@ faillock_message(pam_handle_t *pamh, str
b51b82
 			left = opts->latest_time + opts->unlock_time - opts->now;
b51b82
 		}
b51b82
 
b51b82
+		pam_info(pamh, _("The account is locked due to %u failed logins."),
b51b82
+			(unsigned int)opts->failures);
b51b82
 		if (left > 0) {
b51b82
 			left = (left + 59)/60; /* minutes */
b51b82
 
b51b82
-			pam_info(pamh, _("Account temporarily locked due to %d failed logins"),
b51b82
-				opts->failures);
b51b82
-			pam_info(pamh, _("(%d minutes left to unlock)"), (int)left);
b51b82
-		}
b51b82
-		else {
b51b82
-			pam_info(pamh, _("Account locked due to %d failed logins"),
b51b82
-				opts->failures);
b51b82
+#if defined HAVE_DNGETTEXT && defined ENABLE_NLS
b51b82
+			pam_info(
b51b82
+				pamh,
b51b82
+				dngettext(PACKAGE,
b51b82
+					"(%d minute left to unlock)",
b51b82
+					"(%d minutes left to unlock)",
b51b82
+					(int)left),
b51b82
+				(int)left);
b51b82
+#else
b51b82
+			if (left == 1)
b51b82
+				pam_info(pamh, _("(%d minute left to unlock)"), (int)left);
b51b82
+			else
b51b82
+				/* TRANSLATORS: only used if dngettext is not supported. */
b51b82
+				pam_info(pamh, _("(%d minutes left to unlock)"), (int)left);
b51b82
+#endif
b51b82
 		}
b51b82
 	}
b51b82
 }
b51b82
@@ -663,7 +442,7 @@ opts_cleanup(struct options *opts)
b51b82
 
b51b82
 /*---------------------------------------------------------------------*/
b51b82
 
b51b82
-PAM_EXTERN int
b51b82
+int
b51b82
 pam_sm_authenticate(pam_handle_t *pamh, int flags,
b51b82
 		    int argc, const char **argv)
b51b82
 {
b51b82
@@ -673,13 +452,13 @@ pam_sm_authenticate(pam_handle_t *pamh,
b51b82
 
b51b82
 	memset(&tallies, 0, sizeof(tallies));
b51b82
 
b51b82
-	args_parse(pamh, argc, argv, flags, &opts);
b51b82
-	if (opts.fatal_error) {
b51b82
-		rv = PAM_BUF_ERR;
b51b82
+	rv = args_parse(pamh, argc, argv, flags, &opts);
b51b82
+	if (rv != PAM_SUCCESS)
b51b82
 		goto err;
b51b82
-	}
b51b82
 
b51b82
-	pam_fail_delay(pamh, 2000000);	/* 2 sec delay for on failure */
b51b82
+	if (!(opts.flags & FAILLOCK_FLAG_NO_DELAY)) {
b51b82
+		pam_fail_delay(pamh, 2000000);	/* 2 sec delay on failure */
b51b82
+	}
b51b82
 
b51b82
 	if ((rv=get_pam_user(pamh, &opts)) != PAM_SUCCESS) {
b51b82
 		goto err;
b51b82
@@ -722,7 +501,7 @@ err:
b51b82
 
b51b82
 /*---------------------------------------------------------------------*/
b51b82
 
b51b82
-PAM_EXTERN int
b51b82
+int
b51b82
 pam_sm_setcred(pam_handle_t *pamh UNUSED, int flags UNUSED,
b51b82
 	       int argc UNUSED, const char **argv UNUSED)
b51b82
 {
b51b82
@@ -731,7 +510,7 @@ pam_sm_setcred(pam_handle_t *pamh UNUSED
b51b82
 
b51b82
 /*---------------------------------------------------------------------*/
b51b82
 
b51b82
-PAM_EXTERN int
b51b82
+int
b51b82
 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
b51b82
 		 int argc, const char **argv)
b51b82
 {
b51b82
@@ -741,12 +520,10 @@ pam_sm_acct_mgmt(pam_handle_t *pamh, int
b51b82
 
b51b82
 	memset(&tallies, 0, sizeof(tallies));
b51b82
 
b51b82
-	args_parse(pamh, argc, argv, flags, &opts);
b51b82
+	rv = args_parse(pamh, argc, argv, flags, &opts);
b51b82
 
b51b82
-	if (opts.fatal_error) {
b51b82
-		rv = PAM_BUF_ERR;
b51b82
+	if (rv != PAM_SUCCESS)
b51b82
 		goto err;
b51b82
-	}
b51b82
 
b51b82
 	opts.action = FAILLOCK_ACTION_AUTHSUCC;
b51b82
 
b51b82
@@ -770,28 +547,3 @@ err:
b51b82
 
b51b82
 /*-----------------------------------------------------------------------*/
b51b82
 
b51b82
-#ifdef PAM_STATIC
b51b82
-
b51b82
-/* static module data */
b51b82
-
b51b82
-struct pam_module _pam_faillock_modstruct = {
b51b82
-     MODULE_NAME,
b51b82
-#ifdef PAM_SM_AUTH
b51b82
-     pam_sm_authenticate,
b51b82
-     pam_sm_setcred,
b51b82
-#else
b51b82
-     NULL,
b51b82
-     NULL,
b51b82
-#endif
b51b82
-#ifdef PAM_SM_ACCOUNT
b51b82
-     pam_sm_acct_mgmt,
b51b82
-#else
b51b82
-     NULL,
b51b82
-#endif
b51b82
-     NULL,
b51b82
-     NULL,
b51b82
-     NULL,
b51b82
-};
b51b82
-
b51b82
-#endif   /* #ifdef PAM_STATIC */
b51b82
-
b51b82
diff --git a/modules/pam_faillock/faillock.h b/modules/pam_faillock/faillock.h
b51b82
index b22a9dfb..0ea0ffba 100644
b51b82
--- a/modules/pam_faillock/faillock.h
b51b82
+++ b/modules/pam_faillock/faillock.h
b51b82
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
b51b82
--- Linux-PAM-1.5.1/modules/pam_faillock/faillock.h.faillock-load-conf-from-file	2020-11-25 17:57:02.000000000 +0100
b51b82
+++ Linux-PAM-1.5.1/modules/pam_faillock/faillock.h	2022-05-25 15:33:03.885551825 +0200
b51b82
@@ -67,7 +67,6 @@ struct tally_data {
b51b82
 };
b51b82
 
b51b82
 #define FAILLOCK_DEFAULT_TALLYDIR "/var/run/faillock"
b51b82
-#define FAILLOCK_DEFAULT_CONF "/etc/security/faillock.conf"
b51b82
 
b51b82
 int open_tally(const char *dir, const char *user, uid_t uid, int create);
b51b82
 int read_tally(int fd, struct tally_data *tallies);
b51b82
diff --git a/modules/pam_faillock/pam_faillock.c b/modules/pam_faillock/pam_faillock.c
b51b82
index ddbb90e7..f46fca99 100644
b51b82
--- a/modules/pam_faillock/pam_faillock.c
b51b82
+++ b/modules/pam_faillock/pam_faillock.c
b51b82
@@ -134,10 +134,96 @@ args_parse(pam_handle_t *pamh, int argc, const char **argv,
b51b82
 	return PAM_SUCCESS;
b51b82
 }
b51b82
 
b51b82
+static int
b51b82
+check_user_in_passwd(pam_handle_t *pamh,
b51b82
+				 const char *user_name,
b51b82
+				 const char *file_name)
b51b82
+{
b51b82
+	int rc;
b51b82
+	size_t user_len;
b51b82
+	FILE *fp;
b51b82
+	char line[BUFSIZ];
b51b82
+
b51b82
+	/* Validate the user name.  */
b51b82
+	if ((user_len = strlen(user_name)) == 0) {
b51b82
+		pam_syslog(pamh, LOG_NOTICE, "user name is not valid");
b51b82
+		return PAM_SERVICE_ERR;
b51b82
+	}
b51b82
+
b51b82
+	if (user_len > sizeof(line) - sizeof(":")) {
b51b82
+		pam_syslog(pamh, LOG_NOTICE, "user name is too long");
b51b82
+		return PAM_SERVICE_ERR;
b51b82
+	}
b51b82
+
b51b82
+	if (strchr(user_name, ':') != NULL) {
b51b82
+		/*
b51b82
+		 * "root:x" is not a local user name even if the passwd file
b51b82
+		 * contains a line starting with "root:x:".
b51b82
+		 */
b51b82
+		return PAM_PERM_DENIED;
b51b82
+	}
b51b82
+
b51b82
+	/* Open the passwd file.  */
b51b82
+	if (file_name == NULL) {
b51b82
+		file_name = "/etc/passwd";
b51b82
+	}
b51b82
+	if ((fp = fopen(file_name, "r")) == NULL) {
b51b82
+		pam_syslog(pamh, LOG_ERR, "error opening %s: %m", file_name);
b51b82
+		return PAM_SERVICE_ERR;
b51b82
+	}
b51b82
+
b51b82
+	/*
b51b82
+	 * Scan the file using fgets() instead of fgetpwent_r() because
b51b82
+	 * the latter is not flexible enough in handling long lines
b51b82
+	 * in passwd files.
b51b82
+	 */
b51b82
+	rc = PAM_PERM_DENIED;
b51b82
+	while (fgets(line, sizeof(line), fp) != NULL) {
b51b82
+		size_t line_len;
b51b82
+		const char *str;
b51b82
+
b51b82
+		/*
b51b82
+		 * Does this line start with the user name
b51b82
+		 * followed by a colon?
b51b82
+		 */
b51b82
+		if (strncmp(user_name, line, user_len) == 0 &&
b51b82
+		    line[user_len] == ':') {
b51b82
+			rc = PAM_SUCCESS;
b51b82
+			/*
b51b82
+			 * Continue reading the file to avoid timing attacks.
b51b82
+			 */
b51b82
+		}
b51b82
+		/* Has a newline been read?  */
b51b82
+		line_len = strlen(line);
b51b82
+		if (line_len < sizeof(line) - 1 ||
b51b82
+		    line[line_len - 1] == '\n') {
b51b82
+			/* Yes, continue with the next line.  */
b51b82
+			continue;
b51b82
+		}
b51b82
+
b51b82
+		/* No, read till the end of this line first.  */
b51b82
+		while ((str = fgets(line, sizeof(line), fp)) != NULL) {
b51b82
+			line_len = strlen(line);
b51b82
+			if (line_len == 0 ||
b51b82
+			    line[line_len - 1] == '\n') {
b51b82
+				break;
b51b82
+			}
b51b82
+		}
b51b82
+		if (str == NULL) {
b51b82
+			/* fgets returned NULL, we are done.  */
b51b82
+			break;
b51b82
+		}
b51b82
+		/* Continue with the next line.  */
b51b82
+	}
b51b82
+
b51b82
+	fclose(fp);
b51b82
+	return rc;
b51b82
+}
b51b82
+
b51b82
 static int
b51b82
 check_local_user (pam_handle_t *pamh, const char *user)
b51b82
 {
b51b82
-	return pam_modutil_check_user_in_passwd(pamh, user, NULL) == PAM_SUCCESS;
b51b82
+	return check_user_in_passwd(pamh, user, NULL) == PAM_SUCCESS;
b51b82
 }
b51b82
 
b51b82
 static int