rmgroup.in 7.89 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh Stoller's avatar
Leigh Stoller committed
2
#
3
# Copyright (c) 2000-2014 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
# Rename the group directory as long as its not the proj directory.
162
#
163
if (! $group->IsProjectGroup()) {
164 165
    my $groupdir  = "$GRPROOT/$pid";
    my $oldname   = "$groupdir/$gid";
166
    my $newname   = "$groupdir/_ARCHIVED-${gid}-${gid_idx}";
167
    my $grouplink = "$PROJROOT/$pid/groups/$gid";
Leigh Stoller's avatar
Leigh Stoller committed
168

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

    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
191 192
    }
}
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
#
# If this is the project group directory, then rename the /groups directory.
# We should only call this when removing the project!
#
else {
    my $oldname   = "$GRPROOT/$gid";
    my $newname   = "$GRPROOT/_ARCHIVED-${gid}-${gid_idx}";

    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
221

222 223 224
#
# Remove all members from the group.
# If there was an error, the DB state is left so that this script can
225
# be run again.
226 227 228 229
#
# Must find out what users/groups to delete so that we can pass off to
# modgroups.
#
230 231 232 233
my @userlist = ();
$group->MemberList(\@userlist,
		   $Group::MEMBERLIST_FLAGS_ALLUSERS |
		   $Group::MEMBERLIST_FLAGS_UIDSONLY);
234

235
foreach my $uid (@userlist) {
236 237 238 239 240 241 242 243
    # For perl
    $EUID = $UID;
    if (system("$MODGROUPS -r $pid:$gid $uid")) {
	fatal("$MODGROUPS -r $pid:$gid $uid failed!");
    }
    $EUID = 0;
}

244 245 246 247 248 249
# 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";
250

251 252 253 254
    if (system("$GROUPDEL $unix_name")) {
	if (($? >> 8) != 65) {
	    fatal("Could not remove group $unix_name from local node!");
	}
255
    }
Leigh Stoller's avatar
Leigh Stoller committed
256

257 258
    if ($MAILMANSUPPORT && !$ELABINELAB) {
	my $listname = ($pid eq $gid ? "${pid}-users" : "${pid}-${gid}-users");
259

260 261 262 263 264 265
	# For perl
	$EUID = $UID;
	system("$DELMMLIST -a $listname") == 0 or
	    fatal("$DELMMLIST -a $listname failed!");
	$EUID = 0;
    }
266

267 268 269 270 271 272 273
    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
274

275 276 277 278
    #
    # Be real root for ssh.
    # 
    $UID = 0;
279

280
    if ($CONTROL ne $BOSSNODE) {
281 282
	my $proxy_command = ($pid eq $gid ? "delproject" : "delgroup");
	
283 284
	print "Removing group $unix_name ($unix_gid) on $CONTROL.\n";

285 286 287
	if (system("$SSH -host $CONTROL ".
		   "   $ACCOUNTPROXY $proxy_command $gid $unix_name")) {
	    if ($?) {
288 289
		fatal("Could not remove group $unix_name from $CONTROL!");
	    }
290 291
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
292

293 294 295 296 297
    #
    # Remove group on the tip servers. 
    #
    foreach my $tipserver ( TBTipServers() ) {
	print "Removing group $unix_name ($unix_gid) on $tipserver.\n";
298

299 300 301 302
	if (system("$SSH -host $tipserver $GROUPDEL $unix_name")) {
	    if (($? >> 8) != 65) {
		fatal("Could not remove group $unix_name from $tipserver!");
	    }
303
	}
304 305 306
    }
}

307 308 309 310 311 312
#
# Kill any features
#
EmulabFeatures->DeleteAll($group) == 0 or
    fatal("Could not delete all features for $group");

313 314 315
# Last step, so we can repeat above actions on failures.
$group->Delete() == 0 or
    fatal("Could not delete group $group");
316

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

320 321 322 323 324 325
sub fatal($) {
    my($mesg) = $_[0];

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