diff options
author | nate <nate@FreeBSD.org> | 1998-07-16 17:18:25 +0000 |
---|---|---|
committer | nate <nate@FreeBSD.org> | 1998-07-16 17:18:25 +0000 |
commit | e5267d9c1bf272fca670235d30ab651aaa2ee00f (patch) | |
tree | 49d76e28cd830cb33069b9e21dc9eefd92d50241 /usr.sbin/pw/pw.c | |
parent | 024928fa98398e514a840e1077afa8eedae5268b (diff) | |
download | FreeBSD-src-e5267d9c1bf272fca670235d30ab651aaa2ee00f.zip FreeBSD-src-e5267d9c1bf272fca670235d30ab651aaa2ee00f.tar.gz |
Fix race condition in pw caused by multiple instances of pwd_mkdb being
run at the same time.
Notes:
The fileupdate function is still somewhat broken. Instead of
returning a failure code if it can't modify the original file it
renames the .new file and continues as though nothing is wrong.
This will cause the lock on the original file to be lost and could
lead to a similar race condition. I left that portion of the code
alone since I feel that the maintainer of the code would have a
better concept of how he wants to handle errors in that function
than I do.
PR: bin/6787
Submitted by: Craig Spannring <cts@internetcds.com>
Diffstat (limited to 'usr.sbin/pw/pw.c')
-rw-r--r-- | usr.sbin/pw/pw.c | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/usr.sbin/pw/pw.c b/usr.sbin/pw/pw.c index 634716e..5daa213 100644 --- a/usr.sbin/pw/pw.c +++ b/usr.sbin/pw/pw.c @@ -26,11 +26,12 @@ #ifndef lint static const char rcsid[] = - "$Id$"; + "$Id: pw.c,v 1.7 1997/10/10 06:23:34 charnier Exp $"; #endif /* not lint */ #include "pw.h" #include <err.h> +#include <fcntl.h> #include <paths.h> #include <sys/wait.h> @@ -49,6 +50,7 @@ static struct cargs arglist; static int getindex(const char *words[], const char *word); static void cmdhelp(int mode, int which); +static int filelock(const char *filename); int @@ -147,7 +149,20 @@ main(int argc, char *argv[]) * Now, let's do the common initialisation */ cnf = read_userconfig(getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL); - ch = funcs[which] (cnf, mode, &arglist); + + + /* + * Be pessimistic and lock the master passowrd and group + * files right away. Keep it locked for the duration. + */ + if (-1 == filelock(_PATH_GROUP) || -1 == filelock(_PATH_MASTERPASSWD)) + { + ch = EX_IOERR; + } + else + { + ch = funcs[which] (cnf, mode, &arglist); + } /* * If everything went ok, and we've been asked to update @@ -178,6 +193,12 @@ main(int argc, char *argv[]) } static int +filelock(const char *filename) +{ + return open(filename, O_RDONLY | O_EXLOCK, 0); +} + +static int getindex(const char *words[], const char *word) { int i = 0; |