a4d4d78
diff -up Linux-PAM-1.1.3/modules/pam_faillock/faillock.c.screensaver Linux-PAM-1.1.3/modules/pam_faillock/faillock.c
5310fec
--- Linux-PAM-1.1.3/modules/pam_faillock/faillock.c.screensaver	2010-11-10 11:46:07.000000000 +0100
5310fec
+++ Linux-PAM-1.1.3/modules/pam_faillock/faillock.c	2010-11-10 11:46:07.000000000 +0100
a4d4d78
@@ -41,13 +41,14 @@
a4d4d78
 #include <sys/types.h>
a4d4d78
 #include <sys/stat.h>
a4d4d78
 #include <sys/file.h>
a4d4d78
+#include <sys/stat.h>
a4d4d78
 #include <fcntl.h>
a4d4d78
 #include <security/pam_modutil.h>
a4d4d78
 
a4d4d78
 #include "faillock.h"
a4d4d78
 
a4d4d78
 int
a4d4d78
-open_tally (const char *dir, const char *user, int create)
a4d4d78
+open_tally (const char *dir, const char *user, uid_t uid, int create)
a4d4d78
 {
a4d4d78
 	char *path;
a4d4d78
 	int flags = O_RDWR;
fdfa166
@@ -69,8 +70,18 @@ open_tally (const char *dir, const char 
a4d4d78
 
a4d4d78
 	fd = open(path, flags, 0600);
a4d4d78
 
a4d4d78
-	if (fd != -1)
fdfa166
+	free(path);
fdfa166
+
a4d4d78
+	if (fd != -1) {
a4d4d78
+		struct stat st;
a4d4d78
+
a4d4d78
 		while (flock(fd, LOCK_EX) == -1 && errno == EINTR);
a4d4d78
+		if (fstat(fd, &st) == 0) {
a4d4d78
+			if (st.st_uid != uid) {
a4d4d78
+				fchown(fd, uid, -1);
a4d4d78
+			}
a4d4d78
+		}
a4d4d78
+	}
a4d4d78
 
a4d4d78
 	return fd;
a4d4d78
 }
a4d4d78
diff -up Linux-PAM-1.1.3/modules/pam_faillock/faillock.h.screensaver Linux-PAM-1.1.3/modules/pam_faillock/faillock.h
5310fec
--- Linux-PAM-1.1.3/modules/pam_faillock/faillock.h.screensaver	2010-11-10 11:46:07.000000000 +0100
5310fec
+++ Linux-PAM-1.1.3/modules/pam_faillock/faillock.h	2010-11-10 11:46:07.000000000 +0100
a4d4d78
@@ -45,6 +45,7 @@
a4d4d78
 #define _FAILLOCK_H
a4d4d78
 
a4d4d78
 #include <stdint.h>
a4d4d78
+#include <sys/types.h>
a4d4d78
 
a4d4d78
 #define TALLY_STATUS_VALID     0x1       /* the tally file entry is valid */
a4d4d78
 #define TALLY_STATUS_RHOST     0x2       /* the source is rhost */
a4d4d78
@@ -65,7 +66,7 @@ struct tally_data {
a4d4d78
 
a4d4d78
 #define FAILLOCK_DEFAULT_TALLYDIR "/var/run/faillock"
a4d4d78
 
a4d4d78
-int open_tally(const char *dir, const char *user, int create);
a4d4d78
+int open_tally(const char *dir, const char *user, uid_t uid, int create);
a4d4d78
 int read_tally(int fd, struct tally_data *tallies);
a4d4d78
 int update_tally(int fd, struct tally_data *tallies);
a4d4d78
 #endif
a4d4d78
diff -up Linux-PAM-1.1.3/modules/pam_faillock/main.c.screensaver Linux-PAM-1.1.3/modules/pam_faillock/main.c
5310fec
--- Linux-PAM-1.1.3/modules/pam_faillock/main.c.screensaver	2010-11-10 11:46:07.000000000 +0100
5310fec
+++ Linux-PAM-1.1.3/modules/pam_faillock/main.c	2010-11-10 11:46:07.000000000 +0100
a4d4d78
@@ -106,8 +106,11 @@ do_user(struct options *opts, const char
a4d4d78
 	int fd;
a4d4d78
 	int rv;
a4d4d78
 	struct tally_data tallies;
a4d4d78
+	struct passwd *pwd;
a4d4d78
 
a4d4d78
-	fd = open_tally(opts->dir, user, 0);
a4d4d78
+	pwd = getpwnam(user);
a4d4d78
+
a4d4d78
+	fd = open_tally(opts->dir, user, pwd != NULL ? pwd->pw_uid : 0, 0);
a4d4d78
 
a4d4d78
 	if (fd == -1) {
a4d4d78
 		if (errno == ENOENT) {
a4d4d78
@@ -134,9 +137,8 @@ do_user(struct options *opts, const char
a4d4d78
 #ifdef HAVE_LIBAUDIT
a4d4d78
 		}
a4d4d78
 		if ((audit_fd=audit_open()) >= 0) {
a4d4d78
-			struct passwd *pwd;
a4d4d78
 
a4d4d78
-			if ((pwd=getpwnam(user)) != NULL) {
a4d4d78
+			if (pwd != NULL) {
a4d4d78
 				snprintf(buf, sizeof(buf), "faillock reset uid=%u",
a4d4d78
 					pwd->pw_uid);
a4d4d78
 				audit_log_user_message(audit_fd, AUDIT_USER_ACCT,
a4d4d78
diff -up Linux-PAM-1.1.3/modules/pam_faillock/pam_faillock.c.screensaver Linux-PAM-1.1.3/modules/pam_faillock/pam_faillock.c
5310fec
--- Linux-PAM-1.1.3/modules/pam_faillock/pam_faillock.c.screensaver	2010-11-10 11:46:07.000000000 +0100
5310fec
+++ Linux-PAM-1.1.3/modules/pam_faillock/pam_faillock.c	2010-11-10 11:46:07.000000000 +0100
a4d4d78
@@ -213,7 +213,7 @@ check_tally(pam_handle_t *pamh, struct o
a4d4d78
 
a4d4d78
 	opts->now = time(NULL);
a4d4d78
 
a4d4d78
-	tfd = open_tally(opts->dir, opts->user, 0);
a4d4d78
+	tfd = open_tally(opts->dir, opts->user, opts->uid, 0);
a4d4d78
 
a4d4d78
 	*fd = tfd;
a4d4d78
 
a4d4d78
@@ -289,9 +289,14 @@ reset_tally(pam_handle_t *pamh, struct o
a4d4d78
 {
a4d4d78
 	int rv;
a4d4d78
 
a4d4d78
-	while ((rv=ftruncate(*fd, 0)) == -1 && errno == EINTR);
a4d4d78
-	if (rv == -1) {
a4d4d78
-		pam_syslog(pamh, LOG_ERR, "Error clearing the tally file for %s: %m", opts->user);
a4d4d78
+	if (*fd == -1) {
a4d4d78
+		*fd = open_tally(opts->dir, opts->user, opts->uid, 1);
a4d4d78
+	}
a4d4d78
+	else {
a4d4d78
+		while ((rv=ftruncate(*fd, 0)) == -1 && errno == EINTR);
a4d4d78
+		if (rv == -1) {
a4d4d78
+			pam_syslog(pamh, LOG_ERR, "Error clearing the tally file for %s: %m", opts->user);
a4d4d78
+		}
a4d4d78
 	}
a4d4d78
 }
a4d4d78
 
a4d4d78
@@ -306,7 +311,7 @@ write_tally(pam_handle_t *pamh, struct o
a4d4d78
 	const void *source = NULL;
a4d4d78
 
a4d4d78
 	if (*fd == -1) {
a4d4d78
-		*fd = open_tally(opts->dir, opts->user, 1);
a4d4d78
+		*fd = open_tally(opts->dir, opts->user, opts->uid, 1);
a4d4d78
 	}
a4d4d78
 	if (*fd == -1) {
a4d4d78
 		if (errno == EACCES) {
5310fec
@@ -463,7 +468,7 @@ pam_sm_authenticate(pam_handle_t *pamh, 
a4d4d78
 
a4d4d78
 		case FAILLOCK_ACTION_AUTHSUCC:
a4d4d78
 			rv = check_tally(pamh, &opts, &tallies, &fd;;
a4d4d78
-			if (rv == PAM_SUCCESS && fd != -1) {
a4d4d78
+			if (rv == PAM_SUCCESS) {
a4d4d78
 				reset_tally(pamh, &opts, &fd;;
a4d4d78
 			}
a4d4d78
                         break;
5310fec
@@ -511,10 +516,8 @@ pam_sm_acct_mgmt(pam_handle_t *pamh, int
a4d4d78
 		return rv;
a4d4d78
 	}
a4d4d78
 
a4d4d78
-	check_tally(pamh, &opts, &tallies, &fd;;
a4d4d78
-	if (fd != -1) {
a4d4d78
-		reset_tally(pamh, &opts, &fd;;
a4d4d78
-	}
a4d4d78
+	check_tally(pamh, &opts, &tallies, &fd;; /* for auditing */
a4d4d78
+	reset_tally(pamh, &opts, &fd;;
a4d4d78
 
a4d4d78
 	tally_cleanup(&tallies, fd);
a4d4d78
 
a4d4d78
diff -up Linux-PAM-1.1.3/modules/pam_faillock/pam_faillock.8.xml.screensaver Linux-PAM-1.1.3/modules/pam_faillock/pam_faillock.8.xml
5310fec
--- Linux-PAM-1.1.3/modules/pam_faillock/pam_faillock.8.xml.screensaver	2010-11-10 11:46:07.000000000 +0100
5310fec
+++ Linux-PAM-1.1.3/modules/pam_faillock/pam_faillock.8.xml	2010-11-10 11:47:14.000000000 +0100
a4d4d78
@@ -277,13 +277,9 @@
a4d4d78
       from the <emphasis>pam_tally2</emphasis> module setup.
a4d4d78
     </para>
a4d4d78
     <para>
a4d4d78
-      There is no setuid wrapper for access to the data file such as when the
a4d4d78
-      <emphasis remap='B'>pam_faillock.so</emphasis> module is called from
a4d4d78
-      a screensaver. As this would make it impossible to share PAM configuration
a4d4d78
-      with such services the following workaround is used: If the data file
a4d4d78
-      cannot be opened because of insufficient permissions
a4d4d78
-      (<errorcode>EACCES</errorcode>) the module returns
a4d4d78
-      <errorcode>PAM_SUCCESS</errorcode>.
a4d4d78
+      The individual files with the failure records are created as owned by
a4d4d78
+      the user. This allows <emphasis remap='B'>pam_faillock.so</emphasis> module
a4d4d78
+      to work correctly when it is called from a screensaver.
a4d4d78
     </para>
5310fec
     <para>
5310fec
       Note that using the module in <option>preauth</option> without the