Annotation of loncom/lcpasswd, revision 1.9
1.1 harris41 1: #!/usr/bin/perl
1.3 harris41 2: #
3: # lcpasswd
4: #
5: # Scott Harrison
1.6 harris41 6: # SH: October 27, 2000
7: # SH: October 28, 2000
1.1 harris41 8:
9: use strict;
10:
11: # This script is a setuid script that should
1.4 harris41 12: # be run by user 'www'. This script allows
13: # for synchronous entry of passwords into
14: # both the /etc/passwd and the /etc/smbpasswd
15: # files.
1.1 harris41 16:
1.5 harris41 17: # This script works under the same process control mechanism
18: # as lcuseradd and lcpasswd, to make sure that only one of these
19: # processes is running at any one time on the system.
20:
1.1 harris41 21: # Standard input usage
22: # First line is USERNAME
23: # Second line is CURRENT PASSWORD
24: # Third line is NEW PASSWORD
25:
1.8 harris41 26: # Valid passwords must consist of the
27: # ascii characters within the inclusive
28: # range of 0x20 (32) to 0x7E (126).
29: # These characters are:
30: # SPACE and
31: # !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO
32: # PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
33:
34: # Valid user names must consist of ascii
35: # characters that are alphabetical characters
36: # (A-Z,a-z), numeric (0-9), or the underscore
37: # mark (_). (Essentially, the perl regex \w).
38:
1.4 harris41 39: # Command-line arguments
40: # Yes, but be very careful here (don't pass shell commands)
41: # and this is only supported to allow perl-system calls.
42:
43: # Usage within code
1.5 harris41 44: # Note: NEVER run as system("/home/httpd/perl/lcpasswd NAME OLDPWD NEWPWD")
1.4 harris41 45: #
1.5 harris41 46: # $exitcode=system("/home/httpd/perl/lcpasswd","NAME","OLDPWD","NEWPWD")/256;
1.4 harris41 47: # print "uh-oh" if $exitcode;
48:
49: # These are the exit codes.
50: # ( (0,"ok"),
1.7 harris41 51: # (1,"User ID mismatch. This program must be run as user 'www'"),
1.4 harris41 52: # (2,"Error. This program does not accept command-line arguments."),
1.6 harris41 53: # (3,"Error. Three lines need to be entered into standard input."),
54: # (4,"Error. Too many other simultaneous password change requests being made."),
55: # (5,"Error. User $username does not exist."),
56: # (6,"Error. Invalid entry of current password."),
1.7 harris41 57: # (7,"Error. Root was not successfully enabled."),
1.6 harris41 58: # (8,"Error. Cannot open /etc/passwd.") )
1.4 harris41 59:
1.1 harris41 60: # Security
1.6 harris41 61: $ENV{'PATH'}='/bin:/usr/bin'; # Nullify path information except for what smbpasswd needs
62: $ENV{'BASH_ENV'}=''; # Nullify shell environment information.
1.1 harris41 63:
1.4 harris41 64: # Do not print error messages if there are command-line arguments
65: my $noprint=0;
66: if (@ARGV) {
67: $noprint=1;
68: }
69:
1.5 harris41 70: # Read in /etc/passwd, and make sure this process is running from user=www
1.6 harris41 71: open (IN, '</etc/passwd');
1.1 harris41 72: my @lines=<IN>;
73: close IN;
74: my $wwwid;
75: for my $l (@lines) {
76: chop $l;
77: my @F=split(/\:/,$l);
78: if ($F[0] eq 'www') {$wwwid=$F[2];}
79: }
80: if ($wwwid!=$<) {
1.4 harris41 81: print("User ID mismatch. This program must be run as user 'www'\n") unless $noprint;
82: exit 1;
1.1 harris41 83: }
1.2 harris41 84: &disable_root_capability;
1.1 harris41 85:
1.5 harris41 86: # Handle case of another lcpasswd process
1.6 harris41 87: unless (&try_to_lock('/tmp/lock_lcpasswd')) {
1.5 harris41 88: print "Error. Too many other simultaneous password change requests being made.\n" unless $noprint;
89: exit 4;
90: }
91:
1.4 harris41 92: # Gather input. Should only be 3 values.
93: my @input;
94: if (@ARGV==3) {
95: @input=@ARGV;
96: }
97: elsif (@ARGV) {
98: print("Error. This program needs 3 command-line arguments (username, old password, new password).\n") unless $noprint;
1.6 harris41 99: unlink('/tmp/lock_lcpasswd');
1.4 harris41 100: exit 2;
101: }
102: else {
103: @input=<>;
104: if (@input!=3) {
105: print("Error. Three lines need to be entered into standard input.\n") unless $noprint;
1.6 harris41 106: unlink('/tmp/lock_lcpasswd');
1.4 harris41 107: exit 3;
108: }
109: map {chop} @input;
1.1 harris41 110: }
111:
1.4 harris41 112: my ($username,$oldpwd,$newpwd)=@input;
1.8 harris41 113: $username=~/^(\w+)$/;
114: my $safeusername=$1;
115: if ($username ne $safeusername) {
116: print "Error. The user name specified has invalid characters.\n";
117: unlink('/tmp/lock_lcpasswd');
118: exit 9;
119: }
1.9 ! harris41 120: my $pbad=0;
! 121: map {if (($_<32)&&($_>126)){$pbad=1;}} (split(//,$oldpwd));
! 122: map {if (($_<32)&&($_>126)){$pbad=1;}} (split(//,$newpwd));
! 123: if ($pbad) {
! 124: print "Error. A password entry had an invalid character.\n";
! 125: unlink('/tmp/lock_lcpasswd');
! 126: exit 10;
! 127: }
1.1 harris41 128:
129: # Grab the line corresponding to username
130: my ($userid,$useroldcryptpwd);
131: my @F; my @U;
132: for my $l (@lines) {
133: @F=split(/\:/,$l);
134: if ($F[0] eq $username) {($userid,$useroldcryptpwd)=($F[2],$F[1]); @U=@F;}
135: }
136:
137: # Verify existence of user
138: if (!defined($userid)) {
1.4 harris41 139: print "Error. User $username does not exist.\n" unless $noprint;
1.6 harris41 140: unlink('/tmp/lock_lcpasswd');
1.4 harris41 141: exit 5;
1.1 harris41 142: }
143:
144: # Verify password entry
145: if (crypt($oldpwd,$useroldcryptpwd) ne $useroldcryptpwd) {
1.4 harris41 146: print "Error. Invalid entry of current password.\n" unless $noprint;
1.6 harris41 147: unlink('/tmp/lock_lcpasswd');
1.4 harris41 148: exit 6;
1.1 harris41 149: }
150:
1.2 harris41 151: # Construct new password entry (random salt)
152: my $newcryptpwd=crypt($newpwd,(join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
1.1 harris41 153: $U[1]=$newcryptpwd;
1.6 harris41 154: my $userline=join(':',@U);
1.2 harris41 155: my $rootid=&enable_root_capability;
156: if ($rootid!=0) {
1.4 harris41 157: print "Error. Root was not successfully enabled.\n" unless $noprint;
1.6 harris41 158: unlink('/tmp/lock_lcpasswd');
1.4 harris41 159: exit 7;
1.2 harris41 160: }
1.6 harris41 161: open PASSWORDFILE, '>/etc/passwd' or (print("Error. Cannot open /etc/passwd.\n") && unlink('/tmp/lock_lcpasswd') && exit(8));
1.1 harris41 162: for my $l (@lines) {
163: @F=split(/\:/,$l);
164: if ($F[0] eq $username) {print PASSWORDFILE "$userline\n";}
165: else {print PASSWORDFILE "$l\n";}
166: }
167: close PASSWORDFILE;
1.8 harris41 168:
1.2 harris41 169: ($>,$<)=(0,0); # fool smbpasswd here to think this is not a setuid environment
1.6 harris41 170: unless (-e '/etc/smbpasswd') {
171: open (OUT,'>/etc/smbpasswd'); close OUT;
1.2 harris41 172: }
173: my $smbexist=0;
1.6 harris41 174: open (IN, '</etc/smbpasswd');
1.2 harris41 175: my @lines=<IN>;
176: close IN;
177: for my $l (@lines) {
178: chop $l;
179: my @F=split(/\:/,$l);
180: if ($F[0] eq $username) {$smbexist=1;}
181: }
182: unless ($smbexist) {
1.6 harris41 183: open(OUT,'>>/etc/smbpasswd');
184: print OUT join(':',($safeusername,$userid,'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX','','/home/'.$safeusername,'/bin/bash')) . "\n";
1.2 harris41 185: close OUT;
186: }
187: open(OUT,"|/usr/bin/smbpasswd -s $safeusername>/dev/null");
188: print OUT $newpwd; print OUT "\n";
189: print OUT $newpwd; print OUT "\n";
190: close OUT;
191: $<=$wwwid; # unfool the program
1.1 harris41 192: &disable_root_capability;
1.6 harris41 193: unlink('/tmp/lock_lcpasswd');
1.4 harris41 194: exit 0;
1.1 harris41 195:
1.4 harris41 196: # ----------------------------------------------------------- have setuid script run as root
1.1 harris41 197: sub enable_root_capability {
1.2 harris41 198: if ($wwwid==$>) {
1.1 harris41 199: ($<,$>)=($>,$<);
200: ($(,$))=($),$();
201: }
202: else {
203: # root capability is already enabled
204: }
1.2 harris41 205: return $>;
1.1 harris41 206: }
207:
1.4 harris41 208: # ----------------------------------------------------------- have setuid script run as www
1.1 harris41 209: sub disable_root_capability {
1.2 harris41 210: if ($wwwid==$<) {
1.1 harris41 211: ($<,$>)=($>,$<);
212: ($(,$))=($),$();
213: }
214: else {
215: # root capability is already disabled
216: }
217: }
218:
1.4 harris41 219: # ----------------------------------- make sure that another lcpasswd process isn't running
1.1 harris41 220: sub try_to_lock {
221: my ($lockfile)=@_;
222: my $currentpid;
223: my $lastpid;
1.5 harris41 224: # Do not manipulate lock file as root
225: if ($>==0) {
226: return 0;
227: }
228: # Try to generate lock file.
229: # Wait 3 seconds. If same process id is in
230: # lock file, then assume lock file is stale, and
231: # go ahead. If process id's fluctuate, try
232: # for a maximum of 10 times.
1.1 harris41 233: for (0..10) {
234: if (-e $lockfile) {
235: open(LOCK,"<$lockfile");
236: $currentpid=<LOCK>;
237: close LOCK;
238: if ($currentpid==$lastpid) {
239: last;
240: }
241: sleep 3;
242: $lastpid=$currentpid;
243: }
244: else {
245: last;
246: }
247: if ($_==10) {
248: return 0;
249: }
250: }
251: open(LOCK,">$lockfile");
252: print LOCK $$;
253: close LOCK;
254: return 1;
255: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>