rmacct 6.79 KB
Newer Older
1
#!/usr/local/bin/perl -wT
2
3

### 
Kristin Wright's avatar
Kristin Wright committed
4
### $Id: rmacct,v 1.5 2000-11-28 23:11:12 kwright Exp $
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
###
### rmacct - Removes accounts for all users in given experiment.
###

use Mysql;

## un-taint path
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

##
## arg: @row = {user};
##
sub untaint_user {
    my @row = @_;

21
    if ( @row != 2 ) {                           # expecting only 1 field 
22
23
24
25
26
27
28
29
30
31
32
33
	return 0;    
    }

    if ( $row[0] =~ /^(([a-z]|\d)+)$/ ) {       # user
	$row[0] = $1;
    } else { 
	return 0;
    }

    return 1;
}

Kristin Wright's avatar
Kristin Wright committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
my $eid;
my $savedir = 1;
my $usage = "Usage: rmacct [-t] <eid>\n" .
            "-nt\t(notar) DON'T save user directory in a tar file in /tmp\n";
if ( $#ARGV < 0 or $#ARGV > 1 ) { 
    die($usage);
} elsif ($#ARGV == 0) {
    $eid = $ARGV[0];
} else {
    $eid = $ARGV[1];
    if ( $ARGV[0] =~ /-nt/ ) {
	$savedir = 0;
    } else {
	die($usage);
    }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
}
if ( !($eid =~ /^[A-Za-z0-9\.\-\_ ]+$/) ) { 
    die("Invalid eid '$eid' contains illegal characters.\n"); 
}

##
## Check for valid user
##
open(WHO,"/usr/bin/whoami 2>&1 |");
chop($_ = <WHO>);
close(WHO);
if ($_ ne "root") { die("Must be root.\n"); }

##
## Open database
##
my $dbh = Mysql->connect("localhost","tbdb","script","none");

##
## If the EID doesn't exist, it's invalid.
##
print "Checking that $eid is a valid EID...";
my $cmd = "select eid from experiments where eid='$eid'";
my $sth = $dbh->query($cmd);
my $rows =0;
## Must put in check that $sth defined. May be undefined if
## query command above is syntactically incorrect. -lkw
Kristin Wright's avatar
Kristin Wright committed
76
77
78
if (not defined($sth)) {
    die "Malformed query $cmd.";
}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
while ($sth->fetchrow_array) {
    $rows += 1;
}
if ( $rows != 1 ) { 
    die("Invalid eid '$eid' (not in database).\n"); 
} else {
    print "ok.\n";
}

##
## Select user parameters for users in given experiment.
## Join: exp.pid -> p.pid
##
print "Selecting users...\n";
$cmd = 
94
  "select u.uid, p.pid ".
95
96
97
98
99
100
101
102
  "from experiments as e ".
  "left join projects as p on e.pid = p.pid ".
  "left join proj_memb as pm on p.pid = pm.pid ".
  "left join users as u on u.uid = pm.uid ".
  "where e.eid = '$eid'";
$sth = $dbh->query($cmd);
my @users = ();
while (@row = $sth->fetchrow_array) {
103
104
105
106
107
108
109
110
111
    if ( @row != 2 ) {
	die "User data query did not return correct number of elements.\n";
    } 

    if ( $row[0] =~ /^(([a-z]|\d)+)$/ ) {       # user
	push(@users, $1);
	print "\t$1\n";
    } else { 
	print "Username from database invalid. Row skipped\n";
112
    }
113
114
115
116
117
118
    
    if ( $row[1] =~ /^([a-z0-9]+)$/ ) {          # project/group name
	$group = $1;
    } else {
	die "Group name from database not valid.\n";
    } 
119
120
}

121
122
print "Group: $group\n";

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
##
## Select machines.
##
print "Selecting machines...";
$cmd = 
  "select IP from interfaces as ip ".
  "left join reserved as r on r.node_id=ip.node_id ".
  "left join experiments as e on r.eid = e.eid ".
  "where e.eid = '$eid'";
$sth = $dbh->query($cmd);
my @nodes = ();
while (@row = $sth->fetchrow_array) {
    push(@nodes,$row[0]);
    print " $row[0] ";
}
print "\n";

Kristin Wright's avatar
Kristin Wright committed
140
141
142
143
144
145
146
147
##
## Save user dirs in tar file unless -nt specified 
##
if ( $savedir ) {
    foreach my $user (@users) {
	if ( -e "/users/$user" ) {
	    print "Creating $user.tar in /tmp\n";
	    open(TAR, "/usr/bin/tar cvf /tmp/$user.tar /users/$user 2>&1 |");
148
    while (<TAR>) { print "$_"; }
Kristin Wright's avatar
Kristin Wright committed
149
150
151
152
153
	    close(TAR);	    
	}
    }
}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
##
## For each machine, ping to see if its alive.
## If alive, determine if its FreeBSD or Linux and
## create the user accounts. 
##
my $last = "";
my $OS = "";
my $IP = "";
foreach $IP (@nodes) {
    ## If this machine is not the same as the last one, get the OS
    ## Check also that the IP address is valid (some interfaces might
    ## not have an IP address). 

    if ( $IP ne $last && $IP =~ /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {
	$IP = $1;
        $last = $IP;
	
         if (-e "/bin/ping") {
            open(PING,"/bin/ping -c 1 $IP 2>&1 |");
        } else {
            open(PING,"/sbin/ping -c 1 $IP 2>&1 |");
        }
        do { } until ( <PING> =~ /transmitted, (\d+) packets received/ );
	close(PING); 

        if ( $1 <= 0 ) {

	    die "Couldn't ping machine $IP\n";

	} else {  

	    print "Checking OS on $IP...";
186
            open(UNAME,"/usr/local/bin/sshtb $IP uname 2>&1 |");
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
            $_ = <UNAME>;
	    print $_;
	    close(UNAME);

	    ## FreeBSD says "155.99.214.129: Connection refused"
	    ##    and can also say "Host key doesn't match..."  
	    ##    and can also say "@@@@@@... WARNING HOST ID CHANGED..."
            if ( ($_=~/^Sorry/) || ($_=~/^$IP/) || ($_=~/^Host key/) 
		 || ($_ =~/^@+/) ) { 
		die("Uname failed on $IP:$_\n"); 
	    } else { 
		$OS = $_;
	    }
        } 

        if ( $OS =~ /Linux/ ) {  
	    
	    ## If the group or user doesn't exist, the '*del' utilities
	    ## will report it. If a user remains in the group after user
	    ## deletions, groupdel will report it and not delete the group.

	    my $i = 1;
	    foreach my $user (@users) {
		print "Removing user $user from $IP.\n";
211
		open(USERDEL, "/usr/local/bin/sshtb $IP /usr/sbin/userdel -r $user 2>&1 |");
212
213
214
215
		while (<USERDEL>) { print "$_"; }
		close(USERDEL);
	    }

Kristin Wright's avatar
Kristin Wright committed
216
	    print "Removing group $eid on $IP.\n";
217
	    open(GROUPDEL, "/usr/local/bin/sshtb $IP /usr/sbin/groupdel $group 2>&1 |");
218
219
220
221
222
223
224
225
226
227
	    while (<GROUPDEL>) { print "$_"; }
	    close(GROUPDEL); 

	} elsif ($OS =~ /FreeBSD/) { 

 	    ## Fetch remote login information

	    foreach my $user (@users) {

		print "Removing $user from $IP\n";
228
		open (RMUSER, "/usr/local/bin/sshtb $IP /usr/sbin/rmuser -y $user 2>&1 |");
229
230
231
		while ( <RMUSER> ) {print "$_"; }
		close(RMUSER);
	    }
232
233
234
235
236
237
238
239
	    
	    print "Removing group $group from $IP\n";
	    open (SED, "/usr/local/bin/sshtb $IP \"/usr/bin/sed '/^$group/d' /etc/group >! /tmp/newgroup\" 2>&1 |");
	    while ( <SED> ) {print "$_";}   
	    close(SED);
	    open (CP, "/usr/local/bin/sshtb $IP /bin/cp /tmp/newgroup /etc/group 2>&1 |");
	    while (<CP>) {print "$_";}
	    close CP;
240
241
242
243
244
245
246
	} else {
	    print "$IP is unreachable or has an unknown OS ($OS).\n";
	}
    }
}


Kristin Wright's avatar
Kristin Wright committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
### Remove any tip line processes that might exist for the users
### on each machine.

##
## Select node_ids
##
print "Selecting node_ids...";
$cmd = 
  "select ip.node_id from interfaces as ip ".
  "left join reserved as r on r.node_id=ip.node_id ".
  "left join experiments as e on r.eid = e.eid ".
  "where e.eid = '$eid'";
$sth = $dbh->query($cmd);
my @node_ids = ();
while (@row = $sth->fetchrow_array) {
    push(@node_ids, $row[0]);
    print " $row[0] ";
}
print "\n";

print "Removing tip line processes on local node:\n";
foreach $user (@users) {
    foreach $node_id (@node_ids) {
	print "$user $node_id\n";
	if ($user =~ /^(([a-z]|\d)+)$/) {
	    $u = $1;
	    if ($node_id =~ /^tb(pc|sh)([0-9])+$/) {
		$n = $1;
		print "\tkilltip $user $node_id\n";
		open (KILLTIP, "/usr/testbed/bin/killtip $u $n 2>&1 |");
		while ( <KILLTIP> ) { print "$_"; }
	    }
	}
    }
}
print "\n";