Annotation of loncom/lcuseradd, revision 1.28

1.1       harris41    1: #!/usr/bin/perl
1.16      harris41    2: 
                      3: # The Learning Online Network with CAPA
1.1       harris41    4: #
1.16      harris41    5: # lcuseradd - LON-CAPA setuid script to coordinate all actions
                      6: #             with adding a user with filesystem privileges (e.g. author)
1.1       harris41    7: #
1.16      harris41    8: #
1.28    ! foxr        9: # $Id: lcuseradd,v 1.27 2004/08/05 20:47:27 albertel Exp $
1.16      harris41   10: ###
1.15      harris41   11: 
                     12: ###############################################################################
                     13: ##                                                                           ##
                     14: ## ORGANIZATION OF THIS PERL SCRIPT                                          ##
                     15: ##                                                                           ##
                     16: ## 1. Description of script                                                  ##
                     17: ## 2. Invoking script (standard input)                                       ##
                     18: ## 3. Example usage inside another piece of code                             ##
                     19: ## 4. Description of functions                                               ##
                     20: ## 5. Exit codes                                                             ##
                     21: ## 6. Initializations                                                        ##
                     22: ## 7. Make sure this process is running from user=www                        ##
                     23: ## 8. Start running script with www permissions                              ##
                     24: ## 9. Handle case of another lcpasswd process (locking)                      ##
                     25: ## 10. Error-check input, need 3 values (user name, password 1, password 2)  ##
                     26: ## 11. Start running script with root permissions                            ##
                     27: ## 12. Add user and make www a member of the user-specific group             ##
                     28: ## 13. Set password                                                          ##
                     29: ## 14. Make final modifications to the user directory                        ##
                     30: ## 15. Exit script (unlock)                                                  ##
                     31: ##                                                                           ##
                     32: ###############################################################################
1.1       harris41   33: 
                     34: use strict;
                     35: 
1.15      harris41   36: # ------------------------------------------------------- Description of script
                     37: #
1.1       harris41   38: # This script is a setuid script that should
1.20      foxr       39: # be run by user 'www'.  It creates a /home/USERNAME directory.
1.15      harris41   40: # It adds a user to the unix system.
1.2       harris41   41: # Passwords are set with lcpasswd.
                     42: # www becomes a member of this user group.
1.1       harris41   43: 
1.15      harris41   44: # -------------- Invoking script (standard input versus command-line arguments)
1.26      foxr       45: #                Otherwise sensitive information will be available to ps-ers for
                     46: #                a small but exploitable time window.
1.15      harris41   47: #
                     48: # Standard input (STDIN) usage
1.1       harris41   49: # First line is USERNAME
                     50: # Second line is PASSWORD
1.3       harris41   51: # Third line is PASSWORD
1.26      foxr       52: # Fouth line is the name of a file to which an error code will be written.
                     53: #            If the fourth line is omitted, no error file will be written.
                     54: #            In either case, the program Exits with the code as its Exit status.
                     55: #            The error file will just be a single line containing an
                     56: #            error code.
                     57: #            
                     58: #  
1.15      harris41   59: #
                     60: # Command-line arguments [USERNAME] [PASSWORD] [PASSWORD]
                     61: # Yes, but be very careful here (don't pass shell commands)
                     62: # and this is only supported to allow perl-system calls.
                     63: #
1.7       harris41   64: # Valid passwords must consist of the
                     65: # ascii characters within the inclusive
                     66: # range of 0x20 (32) to 0x7E (126).
                     67: # These characters are:
                     68: # SPACE and
                     69: # !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO
                     70: # PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
1.15      harris41   71: #
1.7       harris41   72: # Valid user names must consist of ascii
                     73: # characters that are alphabetical characters
                     74: # (A-Z,a-z), numeric (0-9), or the underscore
                     75: # mark (_). (Essentially, the perl regex \w).
1.15      harris41   76: # User names must begin with an alphabetical character
                     77: # (A-Z,a-z).
1.7       harris41   78: 
1.15      harris41   79: # ---------------------------------- Example usage inside another piece of code
1.4       harris41   80: # Usage within code
                     81: #
1.26      foxr       82: # $Exitcode=
1.15      harris41   83: #      system("/home/httpd/perl/lcuseradd","NAME","PASSWORD1","PASSWORD2")/256;
1.26      foxr       84: # print "uh-oh" if $Exitcode;
1.4       harris41   85: 
1.15      harris41   86: # ---------------------------------------------------- Description of functions
                     87: # enable_root_capability() : have setuid script run as root
                     88: # disable_root_capability() : have setuid script run as www
                     89: # try_to_lock() : make sure that another lcpasswd process isn't running
                     90: 
                     91: # ------------------------------------------------------------------ Exit codes
1.26      foxr       92: # These are the Exit codes.
1.13      harris41   93: # ( (0,"ok"),
                     94: # (1,"User ID mismatch.  This program must be run as user 'www'"),
1.15      harris41   95: # (2,"Error. This program needs 3 command-line arguments (username, ".
                     96: #    "password 1, password 2)."),
1.13      harris41   97: # (3,"Error. Three lines should be entered into standard input."),
1.15      harris41   98: # (4,"Error. Too many other simultaneous password change requests being ".
                     99: #    "made."),
1.13      harris41  100: # (5,"Error. User $username does not exist."),
                    101: # (6,"Error. Could not make www a member of the group \"$safeusername\"."),
                    102: # (7,"Error. Root was not successfully enabled.),
1.15      harris41  103: # (8,"Error. Cannot set password."),
1.13      harris41  104: # (9,"Error. The user name specified has invalid characters."),
                    105: # (10,"Error. A password entry had an invalid character."),
                    106: # (11,"Error. User already exists.),
1.15      harris41  107: # (12,"Error. Something went wrong with the addition of user ".
                    108: #     "\"$safeusername\"."),
1.13      harris41  109: # (13,"Error. Password mismatch."),
1.26      foxr      110: # (14, "Error filename is invalid")
1.4       harris41  111: 
1.15      harris41  112: # ------------------------------------------------------------- Initializations
1.1       harris41  113: # Security
1.15      harris41  114: $ENV{'PATH'}='/bin/:/usr/bin:/usr/local/sbin:/home/httpd/perl'; # Nullify path
                    115:                                                                 # information
1.16      harris41  116: delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # nullify potential taints
1.2       harris41  117: 
1.15      harris41  118: # Do not print error messages.
                    119: my $noprint=1;
                    120: 
1.26      foxr      121: #  Error file:
                    122: 
                    123: my $error_file;			# This is either the error file name or undef.
                    124: 
1.23      foxr      125: print "In lcuseradd\n" unless $noprint;
                    126: 
1.15      harris41  127: # ----------------------------- Make sure this process is running from user=www
                    128: my $wwwid=getpwnam('www');
                    129: &disable_root_capability;
                    130: if ($wwwid!=$>) {
                    131:     print("User ID mismatch.  This program must be run as user 'www'\n")
                    132: 	unless $noprint;
1.26      foxr      133:     &Exit(1);
1.4       harris41  134: }
1.15      harris41  135: 
                    136: # ----------------------------------- Start running script with www permissions
1.4       harris41  137: &disable_root_capability;
                    138: 
1.15      harris41  139: # --------------------------- Handle case of another lcpasswd process (locking)
1.4       harris41  140: unless (&try_to_lock("/tmp/lock_lcpasswd")) {
1.15      harris41  141:     print "Error. Too many other simultaneous password change requests being ".
                    142: 	"made.\n" unless $noprint;
1.26      foxr      143:     &Exit(4);
1.4       harris41  144: }
                    145: 
1.15      harris41  146: # ------- Error-check input, need 3 values (user name, password 1, password 2).
1.4       harris41  147: my @input;
1.26      foxr      148: if (@ARGV>=3) {
1.4       harris41  149:     @input=@ARGV;
1.28    ! foxr      150: } elsif (@ARGV) {
1.26      foxr      151:     print("Error. This program needs at least 3 command-line arguments (username, ".
                    152: 	  "password 1, password 2 [errorfile]).\n") unless $noprint;
1.4       harris41  153:     unlink('/tmp/lock_lcpasswd');
1.26      foxr      154:     &Exit(2);
1.28    ! foxr      155: } else {
1.4       harris41  156:     @input=<>;
1.26      foxr      157:     if (@input < 3) {
                    158: 	print("Error. At least three lines should be entered into standard input.\n")
1.15      harris41  159: 	    unless $noprint;
1.4       harris41  160: 	unlink('/tmp/lock_lcpasswd');
1.26      foxr      161: 	&Exit(3);
1.4       harris41  162:     }
1.19      harris41  163:     foreach (@input) {chomp;}
1.4       harris41  164: }
                    165: 
1.26      foxr      166: my ($username,$password1,$password2, $error_file)=@input;
1.23      foxr      167: print "Username = ".$username."\n" unless $noprint;
1.4       harris41  168: $username=~/^(\w+)$/;
1.22      foxr      169: print "Username after substitution - ".$username unless $noprint;
1.4       harris41  170: my $safeusername=$1;
1.23      foxr      171: print "Safe username = $safeusername \n" unless $noprint;
1.22      foxr      172: 
1.15      harris41  173: if (($username ne $safeusername) or ($safeusername!~/^[A-Za-z]/)) {
1.22      foxr      174:     print "Error. The user name specified $username $safeusername  has invalid characters.\n"
1.15      harris41  175: 	unless $noprint;
1.8       harris41  176:     unlink('/tmp/lock_lcpasswd');
1.26      foxr      177:     &Exit(9);
1.8       harris41  178: }
                    179: my $pbad=0;
1.19      harris41  180: foreach (split(//,$password1)) {if ((ord($_)<32)||(ord($_)>126)){$pbad=1;}}
                    181: foreach (split(//,$password2)) {if ((ord($_)<32)||(ord($_)>126)){$pbad=1;}}
1.8       harris41  182: if ($pbad) {
1.26      foxr      183:     print "Error. A password entry had an invalid character.\n" unless $noprint;
1.8       harris41  184:     unlink('/tmp/lock_lcpasswd');
1.26      foxr      185:     &Exit(10);
1.8       harris41  186: }
1.5       harris41  187: 
1.26      foxr      188: #
                    189: #   Safe the filename.  For our case, it must only have alpha, numeric, period
                    190: #   and path sparators..
                    191: #
                    192: 
                    193: print "Error file is $error_file \n" unless $noprint;
                    194: 
                    195: if($error_file) {
                    196:     if($error_file =~ /^([(\w)(\d)\.\/]+)$/) {
                    197: 	print "Error file matched pattern $error_file : $1\n" unless $noprint;
                    198: 	my $safe_error_file = $1;	# Untainted I think.
                    199: 	print "Error file after transform $safe_error_file\n"
                    200: 	    unless $noprint;
                    201: 	if($error_file == $safe_error_file) {
                    202: 	    $error_file = $safe_error_file; # untainted error_file.
                    203: 	} else {
                    204: 	    $error_file ="";
                    205: 	    print "Invalid error filename\n" unless $noprint;
                    206: 	    Exit(14);
                    207: 	}
                    208: 
1.28    ! foxr      209:     } else {
1.26      foxr      210: 	$error_file="";
                    211: 	print "Invalid error filename\n" unless $noprint;
                    212: 	Exit(14);
                    213:     }
                    214: }
                    215: 
                    216: 
1.15      harris41  217: # -- Only add user if we can create a brand new home directory (/home/username)
1.7       harris41  218: if (-e "/home/$safeusername") {
                    219:     print "Error. User already exists.\n" unless $noprint;
                    220:     unlink('/tmp/lock_lcpasswd');
1.26      foxr      221:     &Exit(11);
1.7       harris41  222: }
                    223: 
1.15      harris41  224: # -- Only add user if the two password arguments match.
1.23      foxr      225: 
1.5       harris41  226: if ($password1 ne $password2) {
1.6       harris41  227:     print "Error. Password mismatch.\n" unless $noprint;
1.5       harris41  228:     unlink('/tmp/lock_lcpasswd');
1.26      foxr      229:     &Exit(13);
1.5       harris41  230: }
1.23      foxr      231: print "enabling root\n" unless $noprint;
1.15      harris41  232: # ---------------------------------- Start running script with root permissions
1.4       harris41  233: &enable_root_capability;
                    234: 
1.15      harris41  235: # ------------------- Add user and make www a member of the user-specific group
                    236: # -- Add user
1.23      foxr      237: 
                    238: print "adding user: $safeusername \n" unless $noprint;
                    239: my $status = system('/usr/sbin/useradd','-c','LON-CAPA user',$safeusername);
                    240: if ($status) {
1.15      harris41  241:     print "Error.  Something went wrong with the addition of user ".
                    242: 	  "\"$safeusername\".\n" unless $noprint;
1.23      foxr      243:     print "Final status of useradd = $status";
1.4       harris41  244:     unlink('/tmp/lock_lcpasswd');
1.26      foxr      245:     &Exit(12);
1.4       harris41  246: }
1.23      foxr      247: print "Done adding user\n" unless $noprint;
1.7       harris41  248: # Make www a member of that user group.
1.26      foxr      249: my $groups=`/usr/bin/groups www` or &Exit(6);
1.27      albertel  250: # untaint
                    251: my ($safegroups)=($groups=~/([\s\w]+)/);
                    252: $groups=$safegroups;
1.17      harris41  253: chomp $groups; $groups=~s/^\S+\s+\:\s+//;
                    254: my @grouplist=split(/\s+/,$groups);
                    255: my @ugrouplist=grep {!/www|$safeusername/} @grouplist;
                    256: my $gl=join(',',(@ugrouplist,$safeusername));
1.23      foxr      257: print "Putting user in its own group\n" unless $noprint;
1.17      harris41  258: if (system('/usr/sbin/usermod','-G',$gl,'www')) {
1.15      harris41  259:     print "Error. Could not make www a member of the group ".
                    260: 	  "\"$safeusername\".\n" unless $noprint;
1.5       harris41  261:     unlink('/tmp/lock_lcpasswd');
1.26      foxr      262:     &Exit(6);
1.5       harris41  263: }
                    264: 
1.15      harris41  265: # ---------------------------------------------------------------- Set password
                    266: # Set password with lcpasswd (which creates smbpasswd entry).
1.2       harris41  267: 
1.15      harris41  268: unlink('/tmp/lock_lcpasswd');
                    269: &disable_root_capability;
1.16      harris41  270: ($>,$<)=($wwwid,$wwwid);
1.23      foxr      271: print "Opening lcpasswd pipeline\n" unless $noprint;
1.16      harris41  272: open OUT,"|/home/httpd/perl/lcpasswd";
1.15      harris41  273: print OUT $safeusername;
                    274: print OUT "\n";
                    275: print OUT $password1;
                    276: print OUT "\n";
                    277: print OUT $password1;
                    278: print OUT "\n";
                    279: close OUT;
                    280: if ($?) {
1.26      foxr      281:     print "abnormal Exit from close lcpasswd\n" unless $noprint;
                    282:     &Exit(8);
1.8       harris41  283: }
1.18      harris41  284: ($>,$<)=($wwwid,0);
1.15      harris41  285: &enable_root_capability;
1.8       harris41  286: 
1.20      foxr      287: # -- Don't add public_html... that can be added either by the user
                    288: #    or by lchtmldir when the user is granted an authorship role.
                    289: 
1.15      harris41  290: # ------------------------------ Make final modifications to the user directory
                    291: # -- Add a public_html file with a stand-in index.html file
1.8       harris41  292: 
1.21      foxr      293:  system('/bin/chmod','-R','0660',"/home/$safeusername");
                    294: system('/bin/chmod','0710',"/home/$safeusername");
                    295: mkdir "/home/$safeusername/public_html",0755;
                    296: system('/bin/chmod','02770',"/home/$safeusername/public_html");
                    297: open OUT,">/home/$safeusername/public_html/index.html";
                    298: print OUT<<END;
                    299: <html>
                    300: <head>
                    301: <title>$safeusername</title>
                    302: </head>
                    303: <body>
1.24      www       304: <h1>Construction Space</h1>
                    305: <h3>$safeusername</h3>
1.21      foxr      306: </body>
                    307: </html>
                    308: END
                    309: close OUT;
1.20      foxr      310: 
1.24      www       311: print "lcuseradd ownership\n" unless $noprint;
1.11      harris41  312: system('/bin/chown','-R',"$safeusername:$safeusername","/home/$safeusername");
1.24      www       313: # ---------------------------------------------------- Gracefull Apache Restart
                    314: if (-e '/var/run/httpd.pid') {
                    315:     print "lcuseradd Apache restart\n" unless $noprint;
                    316:     open(PID,'/var/run/httpd.pid');
                    317:     my $pid=<PID>;
                    318:     close(PID);
1.27      albertel  319:     my ($safepid)=($pid=~s/(\D+)//g);
1.24      www       320:     if ($pid) {
1.27      albertel  321: 	system('kill','-USR1',"$safepid");
1.24      www       322:     }
                    323: }
1.15      harris41  324: # -------------------------------------------------------- Exit script
1.26      foxr      325: print "lcuseradd Exiting\n" unless $noprint;
1.8       harris41  326: &disable_root_capability;
1.26      foxr      327: &Exit(0);
1.1       harris41  328: 
1.15      harris41  329: # ---------------------------------------------- Have setuid script run as root
1.5       harris41  330: sub enable_root_capability {
                    331:     if ($wwwid==$>) {
1.23      foxr      332: 	($<,$>)=($>,0);
                    333: 	($(,$))=($),0);
1.28    ! foxr      334:     } else {
1.5       harris41  335: 	# root capability is already enabled
                    336:     }
                    337:     return $>;
                    338: }
                    339: 
1.15      harris41  340: # ----------------------------------------------- Have setuid script run as www
1.5       harris41  341: sub disable_root_capability {
                    342:     if ($wwwid==$<) {
                    343: 	($<,$>)=($>,$<);
                    344: 	($(,$))=($),$();
1.28    ! foxr      345:     } else {
1.5       harris41  346: 	# root capability is already disabled
                    347:     }
                    348: }
                    349: 
1.15      harris41  350: # ----------------------- Make sure that another lcpasswd process isn't running
1.5       harris41  351: sub try_to_lock {
                    352:     my ($lockfile)=@_;
                    353:     my $currentpid;
                    354:     my $lastpid;
                    355:     # Do not manipulate lock file as root
                    356:     if ($>==0) {
                    357: 	return 0;
                    358:     }
                    359:     # Try to generate lock file.
                    360:     # Wait 3 seconds.  If same process id is in
                    361:     # lock file, then assume lock file is stale, and
                    362:     # go ahead.  If process id's fluctuate, try
                    363:     # for a maximum of 10 times.
                    364:     for (0..10) {
                    365: 	if (-e $lockfile) {
                    366: 	    open(LOCK,"<$lockfile");
                    367: 	    $currentpid=<LOCK>;
                    368: 	    close LOCK;
                    369: 	    if ($currentpid==$lastpid) {
                    370: 		last;
                    371: 	    }
                    372: 	    sleep 3;
                    373: 	    $lastpid=$currentpid;
1.28    ! foxr      374: 	} else {
1.5       harris41  375: 	    last;
                    376: 	}
                    377: 	if ($_==10) {
                    378: 	    return 0;
                    379: 	}
                    380:     }
                    381:     open(LOCK,">$lockfile");
                    382:     print LOCK $$;
                    383:     close LOCK;
                    384:     return 1;
                    385: }
1.26      foxr      386: #-------------------------- Exit...
                    387: #
                    388: #   Write the file if the error_file is defined.  Regardless
                    389: #   Exit with the status code.
                    390: #
                    391: sub Exit {
                    392:     my ($code) = @_;		# Status code.
                    393: 
                    394:     print "Exiting with status $code error file is $error_file\n" unless $noprint;
                    395:     if($error_file) {
                    396: 	open(FH, ">$error_file");
                    397: 	print FH  "$code\n";
                    398: 	close(FH);
                    399:     }
                    400:     exit $code;
                    401: }
1.16      harris41  402: 
                    403: =head1 NAME
                    404: 
                    405: lcuseradd - LON-CAPA setuid script to coordinate all actions
                    406:             with adding a user with filesystem privileges (e.g. author)
                    407: 
                    408: =head1 DESCRIPTION
                    409: 
                    410: lcuseradd - LON-CAPA setuid script to coordinate all actions
                    411:             with adding a user with filesystem privileges (e.g. author)
                    412: 
                    413: =head1 README
                    414: 
                    415: lcuseradd - LON-CAPA setuid script to coordinate all actions
                    416:             with adding a user with filesystem privileges (e.g. author)
                    417: 
                    418: =head1 PREREQUISITES
                    419: 
                    420: =head1 COREQUISITES
                    421: 
                    422: =pod OSNAMES
                    423: 
                    424: linux
                    425: 
                    426: =pod SCRIPT CATEGORIES
                    427: 
                    428: LONCAPA/Administrative
                    429: 
                    430: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>