File:  [LON-CAPA] / loncom / lcuserdel
Revision 1.10: download - view: text, annotated - select for diffs
Mon Oct 30 02:31:45 2000 UTC (23 years, 6 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
more error catching and commenting.  lcuseradd should be complete, but
I've yet to test the script or give a careful look-through. -Scott

    1: #!/usr/bin/perl
    2: #
    3: # lcuserdel
    4: #
    5: # Scott Harrison
    6: # SH: October 27, 2000
    7: # SH: October 28, 2000
    8: # SH: October 29, 2000
    9: 
   10: use strict;
   11: 
   12: # This script is a setuid script (chmod 6755) that should
   13: # be run by user 'www'.  It DOES NOT delete directories.
   14: # All it does is remove a user's entries from
   15: # /etc/passwd, /etc/groups, and /etc/smbpasswd.
   16: # It also disables user directory access by making the directory
   17: # to be owned by user=www (as opposed to the former "username").
   18: # This command only returns an error if it is
   19: # invoked incorrectly (by passing bad command-line arguments, etc).
   20: 
   21: # This script works under the same process control mechanism
   22: # as lcuseradd and lcpasswd, to make sure that only one of these
   23: # processes is running at any one time on the system.
   24: 
   25: # Standard input usage
   26: # First line is USERNAME
   27: 
   28: # Valid user names must consist of ascii
   29: # characters that are alphabetical characters
   30: # (A-Z,a-z), numeric (0-9), or the underscore
   31: # mark (_). (Essentially, the perl regex \w).
   32: 
   33: # Command-line arguments [USERNAME]
   34: # Yes, but be very careful here (don't pass shell commands)
   35: # and this is only supported to allow perl-system calls.
   36: 
   37: # Usage within code
   38: #
   39: # $exitcode=system("/home/httpd/perl/lcuserdel","NAME")/256;
   40: # print "uh-oh" if $exitcode;
   41: 
   42: # These are the exit codes.
   43: # ( (0,"ok"),
   44: #   (1,"User ID mismatch.  This program must be run as user 'www'"),
   45: #   (2,"Error. Too many other simultaneous password change requests being made."),
   46: #   (3,"Error. Only one line should be entered into standard input."),
   47: #   (4,"Error. This program needs just 1 command-line argument (username).") )
   48: 
   49: # Security
   50: $ENV{'PATH'}=""; # Nullify path information.
   51: $ENV{'BASH_ENV'}=""; # Nullify shell environment information.
   52: 
   53: # Do not print error messages if there are command-line arguments
   54: my $noprint=0;
   55: if (@ARGV) {
   56:     $noprint=1;
   57: }
   58: 
   59: # Read in /etc/passwd, and make sure this process is running from user=www
   60: open (IN, "</etc/passwd");
   61: my @lines=<IN>;
   62: close IN;
   63: my $wwwid;
   64: for my $l (@lines) {
   65:     chop $l;
   66:     my @F=split(/\:/,$l);
   67:     if ($F[0] eq 'www') {$wwwid=$F[2];}
   68: }
   69: if ($wwwid!=$<) {
   70:     print("User ID mismatch.  This program must be run as user 'www'\n") unless $noprint;
   71:     exit 1;
   72: }
   73: &disable_root_capability;
   74: 
   75: # Handle case of another lcpasswd process
   76: unless (&try_to_lock("/tmp/lock_lcpasswd")) {
   77:     print "Error. Too many other simultaneous password change requests being made.\n" unless $noprint;
   78:     exit 4;
   79: }
   80: 
   81: # Gather input.  Should only be 1 value (user name).
   82: my @input;
   83: if (@ARGV==1) {
   84:     @input=@ARGV;
   85: }
   86: elsif (@ARGV) {
   87:     print("Error. This program needs just 1 command-line argument (username).\n") unless $noprint;
   88:     unlink('/tmp/lock_lcpasswd');
   89:     exit 2;
   90: }
   91: else {
   92:     @input=<>;
   93:     if (@input!=1) {
   94: 	print("Error. Only one line should be entered into standard input.\n") unless $noprint;
   95: 	unlink('/tmp/lock_lcpasswd');
   96: 	exit 3;
   97:     }
   98:     map {chop} @input;
   99: }
  100: 
  101: my ($username)=@input;
  102: $username=~/^(\w+)$/;
  103: my $safeusername=$1;
  104: if ($username ne $safeusername) {
  105:     print "Error. The user name specified has invalid characters.\n";
  106:     unlink('/tmp/lock_lcpasswd');
  107:     exit 9;
  108: }
  109: 
  110: &enable_root_capability;
  111: 
  112: # By using the system userdel command:
  113: # Remove entry from /etc/passwd if it exists
  114: # Remove entry from /etc/groups if it exists
  115: # I surround with groupdel command to make absolutely sure the group definition disappears.
  116: system('/usr/sbin/groupdel 2>/dev/null',$safeusername); # ignore error message
  117: system('/usr/sbin/userdel 2>/dev/null',$safeusername); # ignore error message
  118: system('/usr/sbin/groupdel 2>/dev/null',$safeusername); # ignore error message
  119: 
  120: # Remove entry from /etc/smbpasswd if it exists
  121: my $oldsmbpasswd=`/bin/cat /etc/smbpasswd`;
  122: my $newsmbpasswd=`/bin/grep -v '^${safeusername}:' /etc/smbpasswd`;
  123: 
  124: if ($oldsmbpasswd ne $newsmbpasswd) {
  125:     open OUT,">/etc/smbpasswd";
  126:     print OUT $newsmbpasswd;
  127:     close OUT;
  128: }
  129: 
  130: # Change ownership on directory from username:username to www:www
  131: # This prevents subsequently added users from having access.
  132: 
  133: system('/bin/chown','-R','www:www',"/home/$safeusername");
  134: 
  135: &disable_root_capability;
  136: unlink("/tmp/lock_lcpasswd");
  137: exit 0;
  138: 
  139: # ----------------------------------------------------------- have setuid script run as root
  140: sub enable_root_capability {
  141:     if ($wwwid==$>) {
  142: 	($<,$>)=($>,$<);
  143: 	($(,$))=($),$();
  144:     }
  145:     else {
  146: 	# root capability is already enabled
  147:     }
  148:     return $>;
  149: }
  150: 
  151: # ----------------------------------------------------------- have setuid script run as www
  152: sub disable_root_capability {
  153:     if ($wwwid==$<) {
  154: 	($<,$>)=($>,$<);
  155: 	($(,$))=($),$();
  156:     }
  157:     else {
  158: 	# root capability is already disabled
  159:     }
  160: }
  161: 
  162: # ----------------------------------- make sure that another lcpasswd process isn't running
  163: sub try_to_lock {
  164:     my ($lockfile)=@_;
  165:     my $currentpid;
  166:     my $lastpid;
  167:     # Do not manipulate lock file as root
  168:     if ($>==0) {
  169: 	return 0;
  170:     }
  171:     # Try to generate lock file.
  172:     # Wait 3 seconds.  If same process id is in
  173:     # lock file, then assume lock file is stale, and
  174:     # go ahead.  If process id's fluctuate, try
  175:     # for a maximum of 10 times.
  176:     for (0..10) {
  177: 	if (-e $lockfile) {
  178: 	    open(LOCK,"<$lockfile");
  179: 	    $currentpid=<LOCK>;
  180: 	    close LOCK;
  181: 	    if ($currentpid==$lastpid) {
  182: 		last;
  183: 	    }
  184: 	    sleep 3;
  185: 	    $lastpid=$currentpid;
  186: 	}
  187: 	else {
  188: 	    last;
  189: 	}
  190: 	if ($_==10) {
  191: 	    return 0;
  192: 	}
  193:     }
  194:     open(LOCK,">$lockfile");
  195:     print LOCK $$;
  196:     close LOCK;
  197:     return 1;
  198: }
  199: 
  200: 

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