pw and file locking

From: Odhiambo Washington (wash_at_wananchi.com)
Date: 04/26/04

  • Next message: Odhiambo Washington: "Sending Mass mail to users on a system"
    Date: Mon, 26 Apr 2004 13:49:37 +0300
    To: freebsd-isp@freebsd.org
    
    

    Hi,

    I am using pw to process users on my box from a small script.
    What happens is that one script on a different machines executes
    the script (below) on another machine with the required variables.
    NB: It is quite possible that the script on the remote box can be
    executed by two processes simultaneously, so my concern here would
    be about locking.
    Variously I have ended up with a corrupted master.passwd file and
    I had to restore from backup (Thanks to FreeBSD periodic-daily scripts).

    Here is the script, very simplistic, but it works:

    <quote>
    #!/bin/sh

    # -- useradd

    # Script to ADD a mail account
    # This script essentially requires the following data:
    # username = $1
    # realname = $2
    # password = $3
    #

    PW=`which pw`
    OPERATION='useradd'

    # Note: I am not quite sure about how to handle the initial password entry..
    # So I am doing that on the 2nd step ..

    # Add the user account
    $PW $OPERATION $1 -c "$2" -g users -m

    # Now fix the password
    echo $1 $2 $3 >> /script/WBS/useradd.log
    echo "$3" | pw usermod $1 -h 0

    My questions:

    1. It it possible (or even necessary) to implement locking when this
       script runs to take care of any race condition that might lead to
       master.passwd file corruption? If so, clues/suggestions are welcome.
    2. I'll need to capture stderr to the logfile. At what juncture is this
       suitable?

    Thanks
     

    -Wash

    http://www.netmeister.org/news/learn2quote.html

    --
    +======================================================================+
        |\      _,,,---,,_     | Odhiambo Washington    <wash@wananchi.com>
    Zzz /,`.-'`'    -.  ;-;;,_ | Wananchi Online Ltd.   www.wananchi.com
       |,4-  ) )-,_. ,\ (  `'-'| Tel: +254 20 313985-9  +254 20 313922
      '---''(_/--'  `-'\_)     | GSM: +254 722 743223   +254 733 744121
    +======================================================================+
    Aleph-null bottles of beer on the wall,
    Aleph-null bottles of beer,
    	You take one down, and pass it around,
    Aleph-null bottles of beer on the wall.
    _______________________________________________
    freebsd-isp@freebsd.org mailing list
    http://lists.freebsd.org/mailman/listinfo/freebsd-isp
    To unsubscribe, send any mail to "freebsd-isp-unsubscribe@freebsd.org"
    

  • Next message: Odhiambo Washington: "Sending Mass mail to users on a system"