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

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
6 7 8
# All rights reserved.
#

Leigh Stoller's avatar
Leigh Stoller committed
9 10 11 12
use English;

#
# Remove a group. We delete the group directory and the we remove the
13 14 15 16 17 18 19 20 21 22
# group from /etc/group. Actually, the group directory is *renamed*
# since we do not want to be so destructive. This scrip can be called
# with pid=gid, but in that case it has to be a tbadmin, and the
# directory is not renamed. The caller (rmproj) is responsible for that.
#
sub usage()
{
    print STDOUT "Usage: rmgroup <pid> <gid>\n";
    exit(-1);
}
23
sub fatal($);
24 25
my  $optlist = "";

Leigh Stoller's avatar
Leigh Stoller committed
26
#
27
# We don't want to run this script unless its the real version.
Leigh Stoller's avatar
Leigh Stoller committed
28
#
29 30 31 32 33 34 35 36 37 38 39 40 41
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be setuid! Maybe its a development version?\n");
}

#
# This script is setuid, so please do not run it as root. Hard to track
# what has happened.
# 
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root! Its already setuid!\n");
}
Leigh Stoller's avatar
Leigh Stoller committed
42 43 44 45 46 47 48

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $CONTROL  = "@USERNODE@";
49
my $BOSSNODE = "@BOSSNODE@";
50
my $ELABINELAB    = @ELABINELAB@;
51
my $MAILMANSUPPORT= @MAILMANSUPPORT@;
52 53
my $BUGDBSUPPORT  = @BUGDBSUPPORT@;
my $OPSDBSUPPORT  = @OPSDBSUPPORT@;
Leigh Stoller's avatar
Leigh Stoller committed
54 55 56

my $SSH      = "$TB/bin/sshtb";
my $GROUPDEL = "/usr/sbin/pw groupdel";
57
my $DELMMLIST= "$TB/sbin/delmmlist";
58
my $MODGROUPS= "$TB/sbin/modgroups";
59
my $OPSDBCONTROL= "$TB/sbin/opsdb_control";
Leigh Stoller's avatar
Leigh Stoller committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

#
# 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";
76
use libaudit;
Leigh Stoller's avatar
Leigh Stoller committed
77 78 79
use libdb;
use libtestbed;

80 81 82
my $PROJROOT    = PROJROOT();
my $GRPROOT     = GROUPROOT();

Leigh Stoller's avatar
Leigh Stoller committed
83 84 85 86
#
# Check args.
#
if (@ARGV != 2) {
87
    usage();
Leigh Stoller's avatar
Leigh Stoller committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
}
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");
}

#
110
# Figure out who called us. 
Leigh Stoller's avatar
Leigh Stoller committed
111
#
112 113 114 115 116 117 118 119 120 121
if (!TBAdmin($UID)) {
    #
    # Only admin types can remove the project group. Typically, you won't
    # do this directly, but use rmproj instead.
    #
    if ($pid eq $gid) {
	die("*** $0:\n".
	    "    You do not have permission to remove a project group!\n");
    }
    
Leigh Stoller's avatar
Leigh Stoller committed
122 123 124
    my $dbuid;

    if (! UNIX2DBUID($UID, \$dbuid)) {
125 126
	die("*** $0:\n".
	    "    You are not a valid emulab user!\n");
Leigh Stoller's avatar
Leigh Stoller committed
127 128
    }

129
    if (! TBProjAccessCheck($dbuid, $pid, $pid, TB_PROJECT_DELGROUP())) {
130
	die("*** $0:\n".
131
	    "    You do not have permission to remove a project group!\n");
Leigh Stoller's avatar
Leigh Stoller committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145
    }
}

#
# 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!");
}

#
146 147 148 149 150 151 152
# This script is always audited. Mail is sent automatically upon exit.
#
if (AuditStart(0)) {
    #
    # Parent exits normally
    #
    exit(0);
Leigh Stoller's avatar
Leigh Stoller committed
153 154
}

155 156 157 158 159 160 161 162
#
# Rename the group directory as long as its not the proj directory!
#
if ($pid ne $gid) {
    my $groupdir  = "$GRPROOT/$pid";
    my $oldname   = "$groupdir/$gid";
    my $newname   = "$oldname-" . TBDateTimeFSSafe();
    my $grouplink = "$PROJROOT/$pid/groups/$gid";
Leigh Stoller's avatar
Leigh Stoller committed
163

164 165
    if (-e $grouplink && !unlink($grouplink)) {
	fatal("Could not unlink $grouplink: $!");
Leigh Stoller's avatar
Leigh Stoller committed
166
    }
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

    if (-e $oldname) {
	print "Renaming $oldname to $newname.\n";
    
	if (rename($oldname, $newname)) {
	    #
	    # Chown the owner/group to root and set the permissions so no
	    # one is allowed to look inside.
	    #
	    if (! chmod(0700, $newname)) {
		fatal("Could not chmod directory $newname to 0700: $!");
	    }
	    if (! chown(0, 0, $newname)) {
		fatal("Could not chown directory $newname to 0/0: $!");
	    }
	}
	else {
	    fatal("Could not rename $oldname to $newname: $!");
	}
Leigh Stoller's avatar
Leigh Stoller committed
186 187 188
    }
}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
#
# Remove all members from the group.
# If there was an error, the DB state is left so that this script can
# be run again!
#
# Must find out what users/groups to delete so that we can pass off to
# modgroups.
#
my $query_result =
    DBQueryFatal("select uid from group_membership ".
		 "where pid='$pid' and gid='$gid'");

while (my ($uid) = $query_result->fetchrow_array()) {
    # For perl
    $EUID = $UID;
    if (system("$MODGROUPS -r $pid:$gid $uid")) {
	fatal("$MODGROUPS -r $pid:$gid $uid failed!");
    }
    $EUID = 0;
}

Leigh Stoller's avatar
Leigh Stoller committed
210 211 212
#
# Now remove the group from the group file on both plastic and paper.
#
213 214
print "Removing group $unix_name ($unix_gid) on local node.\n";

Leigh Stoller's avatar
Leigh Stoller committed
215
if (system("$GROUPDEL $unix_name")) {
216 217 218
    if (($? >> 8) != 65) {
	fatal("Could not remove group $unix_name from local node!");
    }
Leigh Stoller's avatar
Leigh Stoller committed
219 220
}

221
if ($MAILMANSUPPORT && !$ELABINELAB) {
222 223
    my $listname = ($pid eq $gid ? "${pid}-users" : "${pid}-${gid}-users");
    
224 225
    # For perl
    $EUID = $UID;
226 227
    system("$DELMMLIST -a $listname") == 0 or
	fatal("$DELMMLIST -a $listname failed!");
228 229 230
    $EUID = 0;
}

231 232 233 234 235 236 237 238
if ($OPSDBSUPPORT && !$ELABINELAB) {
    # For perl
    $EUID = $UID;
    system("$OPSDBCONTROL delgroup $pid $gid") == 0 or
	fatal("$OPSDBCONTROL delgroup $pid $gid failed!");
    $EUID = 0;
}

Leigh Stoller's avatar
Leigh Stoller committed
239 240 241 242 243
#
# Be real root for ssh.
# 
$UID = 0;

244 245
if ($CONTROL ne $BOSSNODE) {
    print "Removing group $unix_name ($unix_gid) on $CONTROL.\n";
246

247 248 249 250 251
    if (system("$SSH -host $CONTROL $GROUPDEL $unix_name")) {
	if (($? >> 8) != 65) {
	    fatal("Could not remove group $unix_name from $CONTROL!");
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
252 253
}

254 255 256
#
# Remove group on the tip servers. 
#
257
foreach my $tipserver ( TBTipServers() ) {
258 259
    print "Removing group $unix_name ($unix_gid) on $tipserver.\n";

260
    if (system("$SSH -host $tipserver $GROUPDEL $unix_name")) {
261 262 263
	if (($? >> 8) != 65) {
	    fatal("Could not remove group $unix_name from $tipserver!");
	}
264 265 266
    }
}

267 268 269
DBQueryFatal("delete from group_stats ".
	     "where pid='$pid' and gid='$gid'");

270 271 272 273 274
DBQueryFatal("delete from groups ".
	     "where pid='$pid' and gid='$gid'");

print "Group $pid/$gid has been removed!\n";
exit(0);
Leigh Stoller's avatar
Leigh Stoller committed
275

276 277 278 279 280 281
sub fatal($) {
    my($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}