rmgroup.in 6.17 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh Stoller's avatar
Leigh Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
5 6
# All rights reserved.
#
Leigh Stoller's avatar
Leigh Stoller committed
7
use English;
8
use strict;
Leigh Stoller's avatar
Leigh Stoller committed
9 10 11

#
# Remove a group. We delete the group directory and the we remove the
12 13 14 15 16 17 18
# 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()
{
19
    print STDOUT "Usage: rmgroup <gid>\n";
20 21
    exit(-1);
}
22
sub fatal($);
23 24
my  $optlist = "";

Leigh Stoller's avatar
Leigh Stoller committed
25
#
26
# We don't want to run this script unless its the real version.
Leigh Stoller's avatar
Leigh Stoller committed
27
#
28 29 30 31 32 33 34 35 36 37 38 39 40
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
41 42 43 44

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

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

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

79
# Defined in libtestbed;
80 81 82
my $PROJROOT    = PROJROOT();
my $GRPROOT     = GROUPROOT();

Leigh Stoller's avatar
Leigh Stoller committed
83 84 85
#
# Check args.
#
86
if (@ARGV != 1) {
87
    usage();
Leigh Stoller's avatar
Leigh Stoller committed
88 89
}

90 91 92 93
# Map invoking user to object.
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
Leigh Stoller's avatar
Leigh Stoller committed
94
}
95 96 97 98 99 100 101 102
my $user_name  = $this_user->name();
my $user_email = $this_user->email();
my $user_uid   = $this_user->uid();

# Map the group to its object.
my $group = Group->Lookup($ARGV[0]);
if (!defined($group)) {
    fatal("Could not lookup group object for $ARGV[0]");
Leigh Stoller's avatar
Leigh Stoller committed
103
}
104 105 106 107 108
my $unix_gid   = $group->unix_gid();
my $unix_name  = $group->unix_name();
my $pid        = $group->pid();
my $gid        = $group->gid();
my $gid_idx    = $group->gid_idx();
Leigh Stoller's avatar
Leigh Stoller committed
109 110

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

123
    if (! $group->AccessCheck($this_user, TB_PROJECT_DELGROUP())) {
124
	die("*** $0:\n".
125
	    "    You do not have permission to remove group $group!\n");
Leigh Stoller's avatar
Leigh Stoller committed
126 127 128 129
    }
}

#
130 131 132 133 134 135 136
# 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
137 138
}

139
#
140
# Rename the group directory as long as its not the proj directory.
141
#
142
if (! $group->IsProjectGroup()) {
143 144
    my $groupdir  = "$GRPROOT/$pid";
    my $oldname   = "$groupdir/$gid";
145
    my $newname   = "$groupdir/$gid_idx";
146
    my $grouplink = "$PROJROOT/$pid/groups/$gid";
Leigh Stoller's avatar
Leigh Stoller committed
147

148 149
    if (-e $grouplink && !unlink($grouplink)) {
	fatal("Could not unlink $grouplink: $!");
Leigh Stoller's avatar
Leigh Stoller committed
150
    }
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

    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
170 171 172
    }
}

173 174 175
#
# Remove all members from the group.
# If there was an error, the DB state is left so that this script can
176
# be run again.
177 178 179 180
#
# Must find out what users/groups to delete so that we can pass off to
# modgroups.
#
181 182 183 184
my @userlist = ();
$group->MemberList(\@userlist,
		   $Group::MEMBERLIST_FLAGS_ALLUSERS |
		   $Group::MEMBERLIST_FLAGS_UIDSONLY);
185

186
foreach my $uid (@userlist) {
187 188 189 190 191 192 193 194
    # For perl
    $EUID = $UID;
    if (system("$MODGROUPS -r $pid:$gid $uid")) {
	fatal("$MODGROUPS -r $pid:$gid $uid failed!");
    }
    $EUID = 0;
}

195 196 197 198 199 200
# If the group isn't in /etc/group yet, it wasn't approved and created.
if (system("grep -q '^${unix_gid}:' /etc/group")) {
    #
    # Now remove the group from the group file on both plastic and paper.
    #
    print "Removing group $unix_name ($unix_gid) on local node.\n";
201

202 203 204 205
    if (system("$GROUPDEL $unix_name")) {
	if (($? >> 8) != 65) {
	    fatal("Could not remove group $unix_name from local node!");
	}
206
    }
Leigh Stoller's avatar
Leigh Stoller committed
207

208 209
    if ($MAILMANSUPPORT && !$ELABINELAB) {
	my $listname = ($pid eq $gid ? "${pid}-users" : "${pid}-${gid}-users");
210

211 212 213 214 215 216
	# For perl
	$EUID = $UID;
	system("$DELMMLIST -a $listname") == 0 or
	    fatal("$DELMMLIST -a $listname failed!");
	$EUID = 0;
    }
217

218 219 220 221 222 223 224
    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
225

226 227 228 229
    #
    # Be real root for ssh.
    # 
    $UID = 0;
230

231 232 233 234 235 236 237
    if ($CONTROL ne $BOSSNODE) {
	print "Removing group $unix_name ($unix_gid) on $CONTROL.\n";

	if (system("$SSH -host $CONTROL $GROUPDEL $unix_name")) {
	    if (($? >> 8) != 65) {
		fatal("Could not remove group $unix_name from $CONTROL!");
	    }
238 239
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
240

241 242 243 244 245
    #
    # Remove group on the tip servers. 
    #
    foreach my $tipserver ( TBTipServers() ) {
	print "Removing group $unix_name ($unix_gid) on $tipserver.\n";
246

247 248 249 250
	if (system("$SSH -host $tipserver $GROUPDEL $unix_name")) {
	    if (($? >> 8) != 65) {
		fatal("Could not remove group $unix_name from $tipserver!");
	    }
251
	}
252 253 254
    }
}

255 256 257
# Last step, so we can repeat above actions on failures.
$group->Delete() == 0 or
    fatal("Could not delete group $group");
258

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

262 263 264 265 266 267
sub fatal($) {
    my($mesg) = $_[0];

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