From akimoto@xephion.ne.jp  Tue Dec 12 06:18:17 2000
Return-Path: <akimoto@xephion.ne.jp>
Received: from mayfair.nw.xephion.ne.jp (mayfair.nw.xephion.ne.jp [211.9.226.151])
	by hub.freebsd.org (Postfix) with ESMTP id 66E7137B404
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 12 Dec 2000 06:18:16 -0800 (PST)
Received: (from akimoto@localhost)
	by mayfair.nw.xephion.ne.jp (8.11.1/8.11.1/2000-12-01) id eBCEIEB05177;
	Tue, 12 Dec 2000 23:18:14 +0900 (JST)
	(envelope-from akimoto)
Message-Id: <200012121418.eBCEIEB05177@mayfair.nw.xephion.ne.jp>
Date: Tue, 12 Dec 2000 23:18:14 +0900 (JST)
From: Tomonobu AKIMOTO <akimoto@xephion.ne.jp>
Reply-To: akimoto@xephion.ne.jp
To: FreeBSD-gnats-submit@freebsd.org
Subject: pw command destroy /etc/master.passwd
X-Send-Pr-Version: 3.2

>Number:         23501
>Category:       bin
>Synopsis:       pw destroy /etc/master.passwd when pw executing at the same time
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    kensmith
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Dec 12 06:20:00 PST 2000
>Closed-Date:    Mon Mar 08 12:47:23 PST 2004
>Last-Modified:  Mon Mar 08 12:47:23 PST 2004
>Originator:     Tomonobu AKIMOTO
>Release:        FreeBSD 4.2-RELEASE i386
>Organization:
>Environment:

our server have about 10000 users.

>Description:

/etc/master.passwd is often broken on the servers.
Sometimes,we lost many users from the begining include of root,
sometimes,many users from the bottom.
So,we test.
We make 10000 users on testing server,then we execute 3 pw commands
at the same time.
few seconds later,/etc/master.passwd is broken.

>How-To-Repeat:

	

>Fix:

	


>Release-Note:
>Audit-Trail:

From: Daniel Hagan <dhagan@colltech.com>
To: freebsd-gnats-submit@FreeBSD.org, akimoto@xephion.ne.jp
Cc:  
Subject: Re: bin/23501: pw destroy /etc/master.passwd when pw executing at the 
 same time
Date: Thu, 04 Jan 2001 16:13:50 -0500

 This is almost certainly the fault of fileupdate() in fileupd.c:72.  The
 logic is too byzantine for me to figure out right now, but the comment
 at line 181 seems worrisome.
 
 Daniel
 

From: Tomonobu AKIMOTO <akimoto@xephion.ne.jp>
To: Daniel Hagan <dhagan@colltech.com>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: bin/23501: pw destroy /etc/master.passwd when pw executing at the  same time
Date: Fri, 5 Jan 2001 14:36:09 +0900

 Hello,Daniel
 
 Thank you for your replying.
 But I can't figure out,too.
 
 If anyone knows how to modify concretely,
 please tell me that.
 
 On Thu, 04 Jan 2001 16:13:50 -0500
 Daniel Hagan <dhagan@colltech.com> wrote:
 
 > This is almost certainly the fault of fileupdate() in fileupd.c:72.  The
 > logic is too byzantine for me to figure out right now, but the comment
 > at line 181 seems worrisome.
 > 
 > Daniel
 

From: Alex Kapranoff <alex@kapran.bitmcnit.bryansk.su>
To: akimoto@xephion.ne.jp
Cc: dhagan@colltech.com, freebsd-gnats-submit@freebsd.org
Subject: Re: bin/23501: pw destroy /etc/master.passwd when pw executing at the
Date: Fri, 12 Jan 2001 20:47:34 +0300

 >  If anyone knows how to modify concretely,
 >  please tell me that.
 >  
 >  On Thu, 04 Jan 2001 16:13:50 -0500
 >  Daniel Hagan <dhagan@colltech.com> wrote:
 >  
 >  > This is almost certainly the fault of fileupdate() in fileupd.c:72.  The
 >  > logic is too byzantine for me to figure out right now, but the comment
 >  > at line 181 seems worrisome.
 >  > 
 >  > Daniel
 
 Tomonobo,
 try the following patch and see if it helps.
 
 diff -ru /usr/src/usr.sbin/pw/edgroup.c ./edgroup.c
 --- /usr/src/usr.sbin/pw/edgroup.c	Thu Dec 14 22:13:45 2000
 +++ ./edgroup.c	Fri Jan 12 20:21:42 2001
 @@ -68,7 +68,7 @@
  	strcpy(grouptmp, groupfile);
  	strcat(grouptmp, ".new");
  
 -	if ((infd = open(groupfile, O_RDWR | O_CREAT, 0644)) != -1) {
 +	if ((infd = open(groupfile, O_RDWR | O_CREAT | O_EXLOCK, 0644)) != -1) {
  		FILE           *infp;
  
  		if ((infp = fdopen(infd, "r+")) == NULL)
 @@ -76,7 +76,7 @@
  		else {
  			int             outfd;
  
 -			if ((outfd = open(grouptmp, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, 0644)) != -1) {
 +			if ((outfd = open(grouptmp, O_RDWR | O_CREAT | O_TRUNC, 0644)) != -1) {
  				FILE           *outfp;
  
  				if ((outfp = fdopen(outfd, "w+")) == NULL)
 @@ -207,8 +207,7 @@
  
  							/*
  							 * This is a gross hack, but we may have corrupted the
 -							 * original file. Unfortunately, it will lose preservation
 -							 * of the inode.
 +							 * original file.
  							 */
  							if (fflush(infp) == EOF || ferror(infp))
  								rc = rename(grouptmp, groupfile) == 0;
 diff -ru /usr/src/usr.sbin/pw/fileupd.c ./fileupd.c
 --- /usr/src/usr.sbin/pw/fileupd.c	Thu Dec 14 22:13:45 2000
 +++ ./fileupd.c	Fri Jan 12 20:20:44 2001
 @@ -76,7 +76,7 @@
  	if (pfxlen <= 1)
  		rc = EINVAL;
  	else {
 -		int    infd = open(filename, O_RDWR | O_CREAT, fmode);
 +		int    infd = open(filename, O_RDWR | O_CREAT | O_EXLOCK, fmode);
  
  		if (infd == -1)
  			rc = errno;
 @@ -92,7 +92,7 @@
  
  				strcpy(file, filename);
  				strcat(file, ".new");
 -				outfd = open(file, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, fmode);
 +				outfd = open(file, O_RDWR | O_CREAT | O_TRUNC, fmode);
  				if (outfd == -1)
  					rc = errno;
  				else {
 @@ -183,8 +183,6 @@
  								 * to 'file'.
  								 * This is a gross hack, but we may have
  								 * corrupted the original file
 -								 * Unfortunately, it will lose the inode
 -								 * and hence the lock.
  								 */
  								if (fflush(infp) == EOF || ferror(infp))
  									rename(file, filename);
 
 -- 
 Alex Kapranoff,                              Voice: +7(0832)791845
 We've lived 11 days in the brand new millenium...
 

From: Tomonobu AKIMOTO <akimoto@xephion.ne.jp>
To: Alex Kapranoff <alex@kapran.bitmcnit.bryansk.su>
Cc: dhagan@colltech.com, freebsd-gnats-submit@freebsd.org
Subject: Re: bin/23501: pw destroy /etc/master.passwd when pw executing at the
Date: Wed, 17 Jan 2001 16:11:31 +0900

 Hello,Alex.
 
 I tried your patch and tested it.
 Then the problem is solved.
 
 Thanks a lot.
 
 > try the following patch and see if it helps.
 

From: Jonathan Towne <jontow@twcny.rr.com>
To: freebsd-gnats-submit@freebsd.org
Cc:  
Subject: Re: bin/23501: pw destroy /etc/master.passwd when pw executing at the same time
Date: Fri, 22 Jun 2001 10:48:07 -0400

 This PR can be closed
 
 -- 
 
 - Jonathan Towne/jontow@twcny.rr.com
State-Changed-From-To: open->closed 
State-Changed-By: phk 
State-Changed-When: Fri Jun 22 08:43:09 PDT 2001 
State-Changed-Why:  
Done. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=23501 

From: "Stephen D. Bechard" <steve@destek.net>
To: <freebsd-gnats-submit@FreeBSD.org>, <akimoto@xephion.ne.jp>
Cc:  
Subject: Re: bin/23501: pw destroy /etc/master.passwd when pw executing at the same time
Date: Tue, 30 Dec 2003 15:39:13 -0500

 I was wondering if someone could please confirm that this patch was
 added to FreeBSD 4.9-RELEASE, as I am still seeing the same issues.
 
 Thanks,
 Steve

From: Andy Farkas <andyf@speednet.com.au>
To: "Stephen D. Bechard" <steve@destek.net>
Cc: freebsd-bugs@freebsd.org, freebsd-gnats-submit@freebsd.org
Subject: Re: bin/23501: pw destroy /etc/master.passwd when pw executing at
 the same time
Date: Wed, 31 Dec 2003 07:36:03 +1000 (EST)

 This PR was closed for no reason.
 
 The patch was not applied.
 
State-Changed-From-To: closed->open 
State-Changed-By: simon 
State-Changed-When: Tue Dec 30 14:20:46 PST 2003 
State-Changed-Why:  
Reopen PR, since users report that the problem still exists, and from 
looking in the cvs logs the patch in the PR has not been applied. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=23501 
Responsible-Changed-From-To: freebsd-bugs->kensmith 
Responsible-Changed-By: kensmith 
Responsible-Changed-When: Sun Feb 22 18:36:33 PST 2004 
Responsible-Changed-Why:  

I'll give this one a try.  The submitted patch looks like it solves 
what might be a file library routine buffering issue/file locking 
issue but pw(8) might need more work than just that.  I looked it over 
a bit and can't see where it is using normal password file locking 
to avoid problems with vipw(8) running at the same time, etc. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=23501 
State-Changed-From-To: open->closed 
State-Changed-By: kensmith 
State-Changed-When: Mon Mar 8 12:44:37 PST 2004 
State-Changed-Why:  

Patch was committed.  As part of checking over the patch I verified that 
shifting the locking to the source file instead of the temp file does 
also seem to bring pw(8) into agreement with other master.passwd related 
things like PAM, vipw(8), etc. - they all seem to lock master.passwd itself. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=23501 
>Unformatted:
