rmgroup.in 3.61 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh Stoller's avatar
Leigh Stoller committed
2 3 4 5 6 7 8

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

Leigh Stoller's avatar
Leigh Stoller committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
use English;

#
# Remove a group. We delete the group directory and the we remove the
# group from /etc/group. Actually, the project directory is *renamed*
# since we do not want to be so destructive.
#
# usage: rmgroup <pid> <gid>
#

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $CONTROL  = "@USERNODE@";

my $PROJROOT = "/proj";
27
my $GRPROOT  = "/groups";
Leigh Stoller's avatar
Leigh Stoller committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
my $SSH      = "$TB/bin/sshtb";
my $GROUPDEL = "/usr/sbin/pw groupdel";
my $errors   = 0;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

#
# Check args.
#
if (@ARGV != 2) {
    die("Usage: rmgroup <pid> <gid>\n");
}
my $pid = $ARGV[0];
my $gid = $ARGV[1];

#
# Untaint the argument.
#
if ($pid =~ /^([-\@\w.]+)$/) {
    $pid = $1;
}
else {
    die("*** Invalid pid '$pid' contains illegal characters.\n");
}

if ($gid =~ /^([-\@\w.]+)$/) {
    $gid = $1;
}
else {
    die("*** Invalid gid '$gid' contains illegal characters.\n");
}

#
# Figure out who called us. Only root or people with admin status, or
# project root people, can do this.
#
if ($UID && !TBAdmin($UID)) {
    my $dbuid;

    if (! UNIX2DBUID($UID, \$dbuid)) {
	die("*** You are not a valid emulab user!\n");
    }

    if (! TBMinTrust(TBProjTrust($dbuid, $pid), PROJMEMBERTRUST_PROJROOT)) {
        die("*** Yo do not have permission to remove a group directory\n");
    }
}

#
# Unix info for the group
#
my $unix_gid;
my $unix_name;

if (! TBGroupUnixInfo($pid, $gid, \$unix_gid, \$unix_name)) {
    die("*** No info for project/group $pid/$gid!");
}

#
# Rename the group directory.
# 
105 106 107 108 109 110 111 112
my $groupdir  = "$GRPROOT/$pid";
my $oldname   = "$groupdir/$gid";
my $newname   = "$oldname-" . TBDateTimeFSSafe();
my $grouplink = "$PROJROOT/$pid/groups/$gid";

if (-e $grouplink) {
    unlink($grouplink) or
	die("*** Could not unlink $grouplink: $!\n");
Leigh Stoller's avatar
Leigh Stoller committed
113 114
}

115 116
if (-e $oldname) {
    print "Renaming $oldname to $newname.\n";
117
    
118 119
    if (! rename($oldname, $newname)) {
	die("*** Could not rename $oldname to $newname: $!\n");
Leigh Stoller's avatar
Leigh Stoller committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    }

    #
    # Chown the owner/group to root and set the permissions so no one is
    # allowed to look inside.
    #
    if (! chmod(0700, $newname)) {
	die("*** Could not chmod directory $newname to 0700: $!\n");
    }
    if (! chown(0, 0, $newname)) {
	die("*** Could not chown directory $newname to 0/0: $!\n");
    }
}

#
# Now remove the group from the group file on both plastic and paper.
#
137 138
print "Removing group $unix_name ($unix_gid) on local node.\n";

Leigh Stoller's avatar
Leigh Stoller committed
139 140 141 142 143 144 145 146 147 148 149
if (system("$GROUPDEL $unix_name")) {
    warn("*** WARNING: ".
	 "Could not remove group $unix_name from operations node\n");
    $errors++;
}

#
# Be real root for ssh.
# 
$UID = 0;

150 151
print "Removing group $unix_name ($unix_gid) on $CONTROL.\n";

152
if (system("$SSH -host $CONTROL $GROUPDEL $unix_name")) {
Leigh Stoller's avatar
Leigh Stoller committed
153 154 155 156
    warn("*** WARNING: Could not remove group $unix_name from $CONTROL\n");
    $errors++;
}

157 158 159 160 161 162 163 164 165 166 167 168 169
#
# Remove group on the tip servers. 
#
my @tipservers;

$query_result =
    DBQueryFatal("select * from tipservers");

while (@db_row = $query_result->fetchrow_array() ) {
    push(@tipservers, $db_row[0]);
}

foreach my $tipserver ( @tipservers ) {
170 171
    print "Removing group $unix_name ($unix_gid) on $tipserver.\n";

172
    if (system("$SSH -host $tipserver $GROUPDEL $unix_name")) {
173 174 175 176 177 178 179
	warn("*** WARNING: ".
	     "Could not remove group $unix_name from $tipserver!\n");
	$errors++;
    }
}

exit($errors);
Leigh Stoller's avatar
Leigh Stoller committed
180