rmgroup.in 6.38 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh Stoller's avatar
Leigh Stoller committed
2
#
3
# Copyright (c) 2000-2015 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
23
#
Leigh Stoller's avatar
Leigh Stoller committed
24
use English;
25
use strict;
Leigh Stoller's avatar
Leigh Stoller committed
26 27 28

#
# Remove a group. We delete the group directory and the we remove the
29 30 31 32 33 34 35
# 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()
{
36
    print STDOUT "Usage: rmgroup <gid>\n";
37 38
    exit(-1);
}
39
sub fatal($);
40 41
my  $optlist = "";

Leigh Stoller's avatar
Leigh Stoller committed
42
#
43
# We don't want to run this script unless its the real version.
Leigh Stoller's avatar
Leigh Stoller committed
44
#
45 46 47 48 49 50 51 52 53 54 55 56 57
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
58 59 60 61

#
# Configure variables
#
62 63 64 65
my $TB            = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
my $CONTROL       = "@USERNODE@";
my $BOSSNODE      = "@BOSSNODE@";
66
my $ELABINELAB    = @ELABINELAB@;
67
my $MAILMANSUPPORT= @MAILMANSUPPORT@;
68 69
my $BUGDBSUPPORT  = @BUGDBSUPPORT@;
my $OPSDBSUPPORT  = @OPSDBSUPPORT@;
Leigh Stoller's avatar
Leigh Stoller committed
70

71 72 73 74 75
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";
76
my $ACCOUNTPROXY  = "$TB/sbin/accountsetup";
Leigh Stoller's avatar
Leigh Stoller committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

#
# 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";
93
use libaudit;
Leigh Stoller's avatar
Leigh Stoller committed
94 95
use libdb;
use libtestbed;
96 97
use User;
use Group;
98
use EmulabFeatures;
Leigh Stoller's avatar
Leigh Stoller committed
99

100
# Defined in libtestbed;
101 102 103
my $PROJROOT    = PROJROOT();
my $GRPROOT     = GROUPROOT();

Leigh Stoller's avatar
Leigh Stoller committed
104 105 106
#
# Check args.
#
107
if (@ARGV != 1) {
108
    usage();
Leigh Stoller's avatar
Leigh Stoller committed
109 110
}

111 112 113 114
# 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
115
}
116 117 118 119 120 121 122 123
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
124
}
125 126 127 128 129
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
130 131

#
132
# Figure out who called us. 
Leigh Stoller's avatar
Leigh Stoller committed
133
#
134
if (!TBAdmin()) {
135 136 137 138
    #
    # Only admin types can remove the project group. Typically, you won't
    # do this directly, but use rmproj instead.
    #
139
    if ($group->IsProjectGroup()) {
140 141 142
	die("*** $0:\n".
	    "    You do not have permission to remove a project group!\n");
    }
Leigh Stoller's avatar
Leigh Stoller committed
143

144
    if (! $group->AccessCheck($this_user, TB_PROJECT_DELGROUP())) {
145
	die("*** $0:\n".
146
	    "    You do not have permission to remove group $group!\n");
Leigh Stoller's avatar
Leigh Stoller committed
147 148 149 150
    }
}

#
151 152 153 154 155 156 157
# 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
158 159
}

160
#
161
# Removing group-related directories is handled by accountsetup on ops.
162
#
Leigh Stoller's avatar
Leigh Stoller committed
163

164 165 166
#
# Remove all members from the group.
# If there was an error, the DB state is left so that this script can
167
# be run again.
168 169 170 171
#
# Must find out what users/groups to delete so that we can pass off to
# modgroups.
#
172 173 174 175
my @userlist = ();
$group->MemberList(\@userlist,
		   $Group::MEMBERLIST_FLAGS_ALLUSERS |
		   $Group::MEMBERLIST_FLAGS_UIDSONLY);
176

177
foreach my $uid (@userlist) {
178 179 180 181 182 183 184 185
    # For perl
    $EUID = $UID;
    if (system("$MODGROUPS -r $pid:$gid $uid")) {
	fatal("$MODGROUPS -r $pid:$gid $uid failed!");
    }
    $EUID = 0;
}

186 187 188 189 190 191
# 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";
192

193 194 195 196
    if (system("$GROUPDEL $unix_name")) {
	if (($? >> 8) != 65) {
	    fatal("Could not remove group $unix_name from local node!");
	}
197
    }
Leigh Stoller's avatar
Leigh Stoller committed
198

199 200
    if ($MAILMANSUPPORT && !$ELABINELAB) {
	my $listname = ($pid eq $gid ? "${pid}-users" : "${pid}-${gid}-users");
201

202 203 204 205 206 207
	# For perl
	$EUID = $UID;
	system("$DELMMLIST -a $listname") == 0 or
	    fatal("$DELMMLIST -a $listname failed!");
	$EUID = 0;
    }
208

209 210 211 212 213 214 215
    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
216

217 218 219 220
    #
    # Be real root for ssh.
    # 
    $UID = 0;
221

222
    if ($CONTROL ne $BOSSNODE) {
223 224 225 226 227 228
	my $cmdstr;
	if ($pid eq $gid) {
	    $cmdstr = "delproject $gid $unix_name";
	} else {
	    $cmdstr = "delgroup $gid $unix_name $pid";
	}
229

230 231
	print "Removing group $unix_name ($unix_gid) on $CONTROL.\n";
	if (system("$SSH -host $CONTROL $ACCOUNTPROXY $cmdstr")) {
232
	    fatal("Could not remove group $unix_name from $CONTROL!");
233 234
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
235

236 237 238 239 240
    #
    # Remove group on the tip servers. 
    #
    foreach my $tipserver ( TBTipServers() ) {
	print "Removing group $unix_name ($unix_gid) on $tipserver.\n";
241

242 243 244 245
	if (system("$SSH -host $tipserver $GROUPDEL $unix_name")) {
	    if (($? >> 8) != 65) {
		fatal("Could not remove group $unix_name from $tipserver!");
	    }
246
	}
247 248 249
    }
}

250 251 252 253 254 255
#
# Kill any features
#
EmulabFeatures->DeleteAll($group) == 0 or
    fatal("Could not delete all features for $group");

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

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

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

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