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