Annotation of loncom/lcuseradd, revision 1.12
1.1 harris41 1: #!/usr/bin/perl
2: #
3: # lcuseradd
4: #
5: # Scott Harrison
6: # October 27, 2000
7:
8: use strict;
9:
10: # This script is a setuid script that should
11: # be run by user 'www'. It creates a /home/USERNAME directory
12: # as well as a /home/USERNAME/public_html directory.
13: # It adds user entries to
14: # /etc/passwd and /etc/groups.
1.2 harris41 15: # Passwords are set with lcpasswd.
16: # www becomes a member of this user group.
1.1 harris41 17:
18: # Standard input usage
19: # First line is USERNAME
20: # Second line is PASSWORD
1.3 harris41 21: # Third line is PASSWORD
1.1 harris41 22:
1.7 harris41 23: # Valid passwords must consist of the
24: # ascii characters within the inclusive
25: # range of 0x20 (32) to 0x7E (126).
26: # These characters are:
27: # SPACE and
28: # !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO
29: # PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
30:
31: # Valid user names must consist of ascii
32: # characters that are alphabetical characters
33: # (A-Z,a-z), numeric (0-9), or the underscore
34: # mark (_). (Essentially, the perl regex \w).
35:
1.3 harris41 36: # Command-line arguments [USERNAME] [PASSWORD] [PASSWORD]
1.1 harris41 37: # Yes, but be very careful here (don't pass shell commands)
38: # and this is only supported to allow perl-system calls.
39:
1.4 harris41 40: # Usage within code
41: #
42: # $exitcode=system("/home/httpd/perl/lcuseradd","NAME","PASSWORD1","PASSWORD2")/256;
43: # print "uh-oh" if $exitcode;
44:
45: # These are the exit codes.
46:
1.1 harris41 47: # Security
48: $ENV{'PATH'}=""; # Nullify path information.
49: $ENV{'BASH_ENV'}=""; # Nullify shell environment information.
1.2 harris41 50:
1.4 harris41 51: # Do not print error messages if there are command-line arguments
52: my $noprint=0;
53: if (@ARGV) {
54: $noprint=1;
55: }
56:
57: # Read in /etc/passwd, and make sure this process is running from user=www
58: open (IN, "</etc/passwd");
59: my @lines=<IN>;
60: close IN;
61: my $wwwid;
62: for my $l (@lines) {
63: chop $l;
64: my @F=split(/\:/,$l);
65: if ($F[0] eq 'www') {$wwwid=$F[2];}
66: }
67: if ($wwwid!=$<) {
68: print("User ID mismatch. This program must be run as user 'www'\n") unless $noprint;
69: exit 1;
70: }
71: &disable_root_capability;
72:
73: # Handle case of another lcpasswd process
74: unless (&try_to_lock("/tmp/lock_lcpasswd")) {
75: print "Error. Too many other simultaneous password change requests being made.\n" unless $noprint;
76: exit 4;
77: }
78:
79: # Gather input. Should be 3 values (user name, password 1, password 2).
80: my @input;
1.5 harris41 81: if (@ARGV==3) {
1.4 harris41 82: @input=@ARGV;
83: }
84: elsif (@ARGV) {
85: print("Error. This program needs 3 command-line arguments (username, password 1, password 2).\n") unless $noprint;
86: unlink('/tmp/lock_lcpasswd');
87: exit 2;
88: }
89: else {
90: @input=<>;
1.5 harris41 91: if (@input!=3) {
1.4 harris41 92: print("Error. Three lines should be entered into standard input.\n") unless $noprint;
93: unlink('/tmp/lock_lcpasswd');
94: exit 3;
95: }
96: map {chop} @input;
97: }
98:
99: my ($username,$password1,$password2)=@input;
100: $username=~/^(\w+)$/;
101: my $safeusername=$1;
1.8 harris41 102: if ($username ne $safeusername) {
103: print "Error. The user name specified has invalid characters.\n";
104: unlink('/tmp/lock_lcpasswd');
105: exit 9;
106: }
107: my $pbad=0;
1.9 harris41 108: map {if ((ord($_)<32)||(ord($_)>126)){$pbad=1;}} (split(//,$password1));
109: map {if ((ord($_)<32)||(ord($_)>126)){$pbad=1;}} (split(//,$password2));
1.8 harris41 110: if ($pbad) {
111: print "Error. A password entry had an invalid character.\n";
112: unlink('/tmp/lock_lcpasswd');
113: exit 10;
114: }
1.5 harris41 115:
1.7 harris41 116: # Only add user if we can create a brand new home directory (/home/username).
117: if (-e "/home/$safeusername") {
118: print "Error. User already exists.\n" unless $noprint;
119: unlink('/tmp/lock_lcpasswd');
120: exit 8;
121: }
122:
123: # Only add user if the two password arguments match.
1.5 harris41 124: if ($password1 ne $password2) {
1.6 harris41 125: print "Error. Password mismatch.\n" unless $noprint;
1.5 harris41 126: unlink('/tmp/lock_lcpasswd');
127: exit 7;
128: }
1.4 harris41 129:
130: &enable_root_capability;
131:
1.3 harris41 132: # Add user entry to /etc/passwd and /etc/groups in such
133: # a way that www is a member of the user-specific group
134:
1.5 harris41 135: if (system('/usr/sbin/useradd','-c','LON-CAPA user',$safeusername)) {
1.6 harris41 136: print "Error. Something went wrong with the addition of user \"$safeusername\".\n" unless $noprint;
1.4 harris41 137: unlink('/tmp/lock_lcpasswd');
138: exit 5;
139: }
1.7 harris41 140:
141: # Make www a member of that user group.
1.5 harris41 142: if (system('/usr/sbin/usermod','-G',$safeusername,'www')) {
1.6 harris41 143: print "Error. Could not make www a member of the group \"$safeusername\".\n" unless $noprint;
1.5 harris41 144: unlink('/tmp/lock_lcpasswd');
145: exit 6;
146: }
147:
148: # Set password with lcpasswd-style algorithm (which creates smbpasswd entry).
149: # I cannot place a direct shell call to lcpasswd since I have to allow
150: # for crazy characters in the password, and the setuid environment of perl
151: # requires me to make everything safe.
1.7 harris41 152:
1.8 harris41 153: # Grab the line corresponding to username
1.9 harris41 154: open (IN, "</etc/passwd");
155: @lines=<IN>;
156: close IN;
1.8 harris41 157: my ($userid,$useroldcryptpwd);
158: my @F; my @U;
159: for my $l (@lines) {
1.11 harris41 160: chop $l;
1.8 harris41 161: @F=split(/\:/,$l);
162: if ($F[0] eq $username) {($userid,$useroldcryptpwd)=($F[2],$F[1]); @U=@F;}
163: }
1.5 harris41 164:
1.8 harris41 165: # Verify existence of user
166: if (!defined($userid)) {
167: print "Error. User $username does not exist.\n" unless $noprint;
168: unlink('/tmp/lock_lcpasswd');
169: exit 5;
170: }
1.2 harris41 171:
1.8 harris41 172: # Construct new password entry (random salt)
1.10 harris41 173: my $newcryptpwd=crypt($password1,(join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
1.8 harris41 174: $U[1]=$newcryptpwd;
175: my $userline=join(':',@U);
176: my $rootid=&enable_root_capability;
177: if ($rootid!=0) {
178: print "Error. Root was not successfully enabled.\n" unless $noprint;
179: unlink('/tmp/lock_lcpasswd');
180: exit 7;
181: }
182: open PASSWORDFILE, '>/etc/passwd' or (print("Error. Cannot open /etc/passwd.\n") && unlink('/tmp/lock_lcpasswd') && exit(8));
183: for my $l (@lines) {
184: @F=split(/\:/,$l);
185: if ($F[0] eq $username) {print PASSWORDFILE "$userline\n";}
186: else {print PASSWORDFILE "$l\n";}
187: }
188: close PASSWORDFILE;
189:
190: ($>,$<)=(0,0); # fool smbpasswd here to think this is not a setuid environment
191: unless (-e '/etc/smbpasswd') {
192: open (OUT,'>/etc/smbpasswd'); close OUT;
193: }
194: my $smbexist=0;
195: open (IN, '</etc/smbpasswd');
196: my @lines=<IN>;
197: close IN;
198: for my $l (@lines) {
199: chop $l;
200: my @F=split(/\:/,$l);
201: if ($F[0] eq $username) {$smbexist=1;}
202: }
203: unless ($smbexist) {
204: open(OUT,'>>/etc/smbpasswd');
205: print OUT join(':',($safeusername,$userid,'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX','','/home/'.$safeusername,'/bin/bash')) . "\n";
206: close OUT;
207: }
208: open(OUT,"|/usr/bin/smbpasswd -s $safeusername>/dev/null");
1.10 harris41 209: print OUT $password1; print OUT "\n";
210: print OUT $password1; print OUT "\n";
1.8 harris41 211: close OUT;
212: $<=$wwwid; # unfool the program
213:
214: # Make final modifications to the user directory.
215: # Add a public_html file with a stand-in index.html file.
216:
1.12 ! harris41 217: system('/bin/chmod','-R','0660',"/home/$safeusername");
! 218: system('/bin/chmod','0770',"/home/$safeusername");
! 219: mkdir "/home/$safeusername/public_html",0770;
1.11 harris41 220: open OUT,">/home/$safeusername/public_html/index.html";
1.8 harris41 221: print OUT<<END;
222: <HTML>
223: <HEAD>
224: <TITLE>$safeusername</TITLE>
225: </HEAD>
226: <BODY>
227: <H1>$safeusername</H1>
228: <P>
229: Learning Online Network
230: </P>
231: <P>
232: This area provides for:
233: </P>
234: <UL>
235: <LI>resource construction
236: <LI>resource publication
237: <LI>record-keeping
238: </UL>
239: </BODY>
240: </HTML>
241: END
242: close OUT;
1.11 harris41 243: system('/bin/chown','-R',"$safeusername:$safeusername","/home/$safeusername");
1.8 harris41 244: &disable_root_capability;
245: unlink('/tmp/lock_lcpasswd');
246: exit 0;
1.1 harris41 247:
1.5 harris41 248: # ----------------------------------------------------------- have setuid script run as root
249: sub enable_root_capability {
250: if ($wwwid==$>) {
251: ($<,$>)=($>,$<);
252: ($(,$))=($),$();
253: }
254: else {
255: # root capability is already enabled
256: }
257: return $>;
258: }
259:
260: # ----------------------------------------------------------- have setuid script run as www
261: sub disable_root_capability {
262: if ($wwwid==$<) {
263: ($<,$>)=($>,$<);
264: ($(,$))=($),$();
265: }
266: else {
267: # root capability is already disabled
268: }
269: }
270:
271: # ----------------------------------- make sure that another lcpasswd process isn't running
272: sub try_to_lock {
273: my ($lockfile)=@_;
274: my $currentpid;
275: my $lastpid;
276: # Do not manipulate lock file as root
277: if ($>==0) {
278: return 0;
279: }
280: # Try to generate lock file.
281: # Wait 3 seconds. If same process id is in
282: # lock file, then assume lock file is stale, and
283: # go ahead. If process id's fluctuate, try
284: # for a maximum of 10 times.
285: for (0..10) {
286: if (-e $lockfile) {
287: open(LOCK,"<$lockfile");
288: $currentpid=<LOCK>;
289: close LOCK;
290: if ($currentpid==$lastpid) {
291: last;
292: }
293: sleep 3;
294: $lastpid=$currentpid;
295: }
296: else {
297: last;
298: }
299: if ($_==10) {
300: return 0;
301: }
302: }
303: open(LOCK,">$lockfile");
304: print LOCK $$;
305: close LOCK;
306: return 1;
307: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>