--- loncom/lcpasswd 2000/10/27 23:32:24 1.4 +++ loncom/lcpasswd 2000/10/28 18:25:47 1.5 @@ -13,6 +13,10 @@ use strict; # both the /etc/passwd and the /etc/smbpasswd # files. +# This script works under the same process control mechanism +# as lcuseradd and lcpasswd, to make sure that only one of these +# processes is running at any one time on the system. + # Standard input usage # First line is USERNAME # Second line is CURRENT PASSWORD @@ -23,9 +27,9 @@ use strict; # and this is only supported to allow perl-system calls. # Usage within code -# Note: NEVER run as system("NAME OLDPWD NEWPWD") +# Note: NEVER run as system("/home/httpd/perl/lcpasswd NAME OLDPWD NEWPWD") # -# $exitcode=system("NAME","OLDPWD","NEWPWD")/256; +# $exitcode=system("/home/httpd/perl/lcpasswd","NAME","OLDPWD","NEWPWD")/256; # print "uh-oh" if $exitcode; # These are the exit codes. @@ -48,6 +52,7 @@ if (@ARGV) { $noprint=1; } +# Read in /etc/passwd, and make sure this process is running from user=www open (IN, "; close IN; @@ -59,10 +64,17 @@ for my $l (@lines) { } if ($wwwid!=$<) { print("User ID mismatch. This program must be run as user 'www'\n") unless $noprint; + unlink("/tmp/lock_lcpasswd"); exit 1; } &disable_root_capability; +# Handle case of another lcpasswd process +unless (&try_to_lock("/tmp/lock_lcpasswd")) { + print "Error. Too many other simultaneous password change requests being made.\n" unless $noprint; + exit 4; +} + # Gather input. Should only be 3 values. my @input; if (@ARGV==3) { @@ -70,21 +82,18 @@ if (@ARGV==3) { } elsif (@ARGV) { print("Error. This program needs 3 command-line arguments (username, old password, new password).\n") unless $noprint; + unlink("/tmp/lock_lcpasswd"); exit 2; } else { @input=<>; if (@input!=3) { print("Error. Three lines need to be entered into standard input.\n") unless $noprint; + unlink("/tmp/lock_lcpasswd"); exit 3; } map {chop} @input; } -# Handle case of another lcpasswd process -unless (&try_to_lock("/tmp/lock_lcpasswd")) { - print "Error. Too many other simultaneous password change requests being made.\n" unless $noprint; - exit 4; -} my ($username,$oldpwd,$newpwd)=@input; @@ -184,6 +193,15 @@ sub try_to_lock { my ($lockfile)=@_; my $currentpid; my $lastpid; + # Do not manipulate lock file as root + if ($>==0) { + return 0; + } + # Try to generate lock file. + # Wait 3 seconds. If same process id is in + # lock file, then assume lock file is stale, and + # go ahead. If process id's fluctuate, try + # for a maximum of 10 times. for (0..10) { if (-e $lockfile) { open(LOCK,"<$lockfile");