[comp.sys.sgi] How do I lock the passwd file?

rickert@mp.cs.niu.edu (Neil Rickert) (01/09/91)

In article <1991Jan9.003046.12975@ccu1.aukuni.ac.nz> russell@ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) writes:
>I am writing a set of scripts in perl to create and delete users on our 
>SGI 4D system running Irix 3.3.1.
>
>The man page for passwd(4) suggests that adequate locking MUST be performed
>but gives no hint of how. There is a file /etc/.pwd.lock which is presumably
>used by the passwd command.
>
 If you have 'vipw', it handles locking for you.  Define the EDITOR
environment variable to be the editor (or perl or shell or whatever) which
will actually do the editing, then invoke 'vipw'.  If you can use this,
it should be relatively robust in the sense that you won't have to
modify your code every time your vendor changes his approach to locking.

-- 
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  Neil W. Rickert, Computer Science               <rickert@cs.niu.edu>
  Northern Illinois Univ.
  DeKalb, IL 60115                                   +1-815-753-6940

blbates@AERO4.LARC.NASA.GOV ("Brent L. Bates AAD/TAB MS361 x42854") (01/09/91)

    The SGI's under 3.3.1 don't have vipw.  (Our system administrator, who
is also in charge of our SUN's which DO have vipw, is rather annoyed that
the SGI's don't have vipw)
--

	Brent L. Bates
	NASA-Langley Research Center
	M.S. 361
	Hampton, Virginia  23665-5225
	(804) 864-2854
	E-mail: blbates@aero4.larc.nasa.gov or blbates@aero8.larc.nasa.gov

fsfacca@AVELON.LERC.NASA.GOV (Tony Facca) (01/09/91)

> 
> These scripts (obviously) need to modify the password file /etc/passwd. The
> way our system will operate it is quite possible (likely ??) that two or more
> people may attempt modification at the same time with erroneous results.
> 
> The man page for passwd(4) suggests that adequate locking MUST be performed
> but gives no hint of how. There is a file /etc/.pwd.lock which is presumably
> used by the passwd command.
> 

The man page is probably suggesting that YOU enforce a locking scheme if you
plan to modify the passwd file.  

A method which works well for us is:

    if [lock file exists] then
	exit
    else
	[create the lock file]
	[make a backup copy of the passwd file]
	[copy the password file to a temp file]
	[make modifications to the temp file]
	[disable interrupts]
	[replace the password file with the temp file]
	[remove the lock file]
	[enable interrupts]
    endif


In C-Shell code:

  set lockfile = /etc/passwd.lock
  set tempfile = /etc/passwd.temp
  set savefile = /etc/passwd.save

  if (-e $lockfile) then
      echo "Try again later"
      exit
  else
      cp /dev/null $lockfile
      cp /etc/passwd $tempfile
      cp /etc/passwd $savefile

	   [ code to modify the temp file ]

      # this is the critical section

      onintr -
      mv $tempfile /etc/passwd		
      # you may want to check $status here 
      rm $lockfile
      onintr

  endif 


The same thing can be done using the Bourne Shell, I don't know about perl.
Be careful that you set the proper umask before you start and that the file
modes are read-only.

Good Luck!
--
-----------------------------------------------------------------------------
Tony Facca   |   fsfacca@avelon.lerc.nasa.gov      |     phone: 216-433-8318
-----------------------------------------------------------------------------
      You are at Witt's end.  Passages lead off in *all* directions.