sharevlan.in 11 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-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/>.
# 
# }}}
23 24 25 26
#
use strict;
use English;
use Getopt::Std;
27
use Data::Dumper;
28 29 30 31 32 33 34

#
# Grant and revoke shared vlan access.
#
sub usage()
{
    print STDERR "Usage: sharevlan [-o] eid lanname token\n";
35
    print STDERR "       sharevlan -r | -i | -c token\n";
36
    print STDERR "       sharevlan -r eid lanname\n";
37
    print STDERR " -r    Revoke sharing instead\n";
38
    print STDERR " -i    Show members.\n";
39
    print STDERR " -o    Sharing is open to everyone.\n";
40
    print STDERR " -l    List all shared vlans\n";
41
    print STDERR " -c    Create a credential for private vlan\n";
42 43
    exit(-1);
}
44
my $optlist  = "hrRolifc";
45
my $revoke   = 0;
46
my $docred   = 0;
47
my $open     = 0;
48
my $list     = 0;
49 50
my $info     = 0;
my $force    = 0;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

# Protos
sub fatal($);

#
# Please do not run as root. Hard to track what has happened.
#
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root!\n");
}

#
# Configure variables
#
my $TB		 = "@prefix@";
67 68 69 70
my $TBOPS        = "@TBOPSEMAIL@";
my $OURDOMAIN    = "@OURDOMAIN@";
my $PGENISUPPORT = @PROTOGENI_SUPPORT@;
my $PGENIDOMAIN  = "@PROTOGENI_DOMAIN@";
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 105 106

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use libtestbed;
use Experiment;
use EmulabConstants;
use Lan;
use User;

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'h'})) {
    usage();
}
if (defined($options{'o'})) {
    $open = 1;
}
107 108 109 110 111 112 113 114 115 116
if (defined($options{'f'})) {
    $force = 1;
}
if (defined($options{'i'})) {
    $info = 1;

    usage()
	if (@ARGV != 1);
}
elsif (defined($options{'l'})) {
117 118 119
    $list = 1;
}
elsif (defined($options{'r'})) {
120 121
    $revoke = 1;

122 123 124
    if (defined($options{'R'})) {
	$force = 1;
    }
125
}
126 127 128
elsif (defined($options{'c'})) {
    $docred = 1;
}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
else {
    usage()
	if (@ARGV != 3);
}

#
# Verify user, must be admin.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}
if (!$this_user->IsAdmin()) {
    fatal("You are not a testbed administrator!");
}

145 146
if ($list) {
    my $query_result =
147 148 149
	DBQueryFatal("select * from shared_vlans as s ".
		     "left join vlans as v on v.id=s.lanid ".
		     "order by s.token");
150 151 152
    exit(0)
	if (! $query_result->numrows);

153 154 155 156
    printf("%-15s %-20s %-20s %-20s %-5s %s\n",
	   "PID", "EID", "Token", "Lan", "Tag", "Who");
    print "-------------------------------------------------------------";
    print "------------------------------\n";
157 158 159 160 161 162 163 164
	
    while (my $row = $query_result->fetchrow_hashref()) {
	my $pid      = $row->{'pid'};
	my $eid      = $row->{'eid'};
	my $token    = $row->{'token'};
	my $lan      = $row->{'vname'};
	my $created  = $row->{'created'};
	my $creator  = $row->{'creator'};
165
	my $tag      = $row->{'tag'} || "";
166

167 168
	printf("%-15s %-20s %-20s %-20s %-5s %s\n",
	       $pid, $eid, $token, $lan, "$tag", $creator);
169 170 171 172
    }
    exit(0);
}

173 174 175 176
if ($info) {
    my $token = $ARGV[0];
    my $query_result =
	DBQueryFatal("select lanid from shared_vlans where token='$token'");
177
    if (!$query_result->numrows) {
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	fatal("No such shared vlan");
    }
    my ($lanid) = $query_result->fetchrow_array();
    my $vlan = VLan->Lookup($lanid);
    if (!defined($vlan)) {
	fatal("No such vlan $lanid");
    }
    my @members;
    if ($vlan->MemberList(\@members) != 0) {
	fatal("Could not get member list for $vlan");
    }
    foreach my $member (@members) {
	my $node;
	my $iface;
	$member->GetNodeIface(\$node, \$iface);
	print $node->node_id() . ":$iface";

	my $portlan_exptidx;
	my $portlan_lanname;
	my $experiment;
	$member->GetAttribute("portlan_exptidx", \$portlan_exptidx);
	$member->GetAttribute("portlan_lanname", \$portlan_lanname);
	
	if (defined($portlan_exptidx) && defined($portlan_lanname)) {
	    $experiment = Experiment->Lookup($portlan_exptidx);
	    if (defined($experiment)) {
		print " " . $experiment->pid() . ":" . $experiment->eid();
	    }
206 207 208
	    else {
		print " oops";
	    }
209 210 211 212 213 214 215 216 217 218 219 220
	}
	my $reservation = $node->Reservation();
	if (defined($reservation) &&
	    (!defined($experiment) ||
	     !$reservation->SameExperiment($experiment))) {
	    print " (" . $reservation->pid() . ":" . $reservation->eid() . ")";
	}
	print "\n";
    }
    exit(0);
}

221
if ($revoke) {
222
    my ($vlan,$lanid);
223 224
    my $busy  = 0;

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    if (@ARGV == 1) {
	my $token = $ARGV[0];

	my $query_result =
	    DBQueryFatal("select lanid from shared_vlans where token='$token'");
	if (!$query_result->numrows) {
	    fatal("No such shared vlan");
	}
	($lanid) = $query_result->fetchrow_array();
	$vlan = VLan->Lookup($lanid);
	if (!defined($vlan)) {
	    fatal("No such vlan $lanid");
	}
    }
    elsif (@ARGV == 2) {
	my $eid     = $ARGV[0];
	my $lanname = $ARGV[1];

	my $experiment = Experiment->Lookup($eid);
	if (!defined($experiment)) {
	    fatal("No such experiment $eid");
	}
	$vlan = VLan->Lookup($experiment, $lanname);
	if (!defined($vlan)) {
	    fatal("No such lan in $experiment");
	}
	$lanid = $vlan->lanid();
    }
    else {
	usage();
    }
    if (!$vlan->IsShared()) {
	fatal("Lan is not shared");
    }

260
    #
261 262 263 264
    # The lan is obviously shared, so we have to lock it.
    # It should not spend much time locked though, so the
    # timeout should not be too long; indicates an error if
    # it is.
265
    #
266 267
    if ($vlan->Lock(180) != 0) {
	fatal("Could not lock vlan $lanid");
268
    }
269 270 271 272 273
    
    #
    # Do not allow if the lan is actually being used by other experiments.
    #

274 275
    my @members;
    if ($vlan->MemberList(\@members) != 0) {
276
	$vlan->Unlock();
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	fatal("Could not get member list for $vlan");
    }
    foreach my $member (@members) {
	my $node;
	my $iface;
	$member->GetNodeIface(\$node, \$iface);

	my $portlan_exptidx;
	my $portlan_lanname;
	my $experiment;
	$member->GetAttribute("portlan_exptidx", \$portlan_exptidx);
	$member->GetAttribute("portlan_lanname", \$portlan_lanname);
	
	if (defined($portlan_exptidx) && defined($portlan_lanname)) {
	    $experiment = Experiment->Lookup($portlan_exptidx);
	    if (defined($experiment)) {
		print $node->node_id() . ":$iface in ";
		print $experiment->pid() . "/" . $experiment->eid() . " ";
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
		print "is using this vlan";
		if ($force) {
		    print ", but we are going to forcibly revoke it!\n";

		    my $portlan = Lan->Lookup($experiment, $portlan_lanname);
		    if (!defined($portlan)) {
			$vlan->Unlock();
			fatal("Could not revoke sharing of $vlan by ".
			      "$portlan_lanname in $experiment cause ".
			      "portlan_lanname does not exist!\n");
		    }
		    if ($experiment->ClearPortLans(1, $portlan)) {
			$vlan->Unlock();
			fatal("Could not revoke sharing of $vlan by ".
			      "$portlan_lanname in $experiment!\n");
		    }
		}
		else {
		    print "!\n";
		    $busy++;
		}
316 317
	    }
	    else {
318 319
		print $node->node_id() . ":$iface is marked as shared in this ";
		print "vlan but the experiment is gone!\n";
320 321 322 323 324
	    }
	}
    }
    if ($busy) {
	print "*** Refusing to delete shared vlan until sharers give it up!\n";
325
	$vlan->Unlock();
326 327
	exit(1);
    }
328
    
329 330
    $vlan->Unlock();
    DBQueryFatal("delete from shared_vlans where lanid='$lanid'");
331 332 333
    exit(0);
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
if ($docred) {
    if (!$PGENISUPPORT) {
	fatal("Credentials not supported");
    }
    our $LOCALCM_FLAG;
    use vars qw($GENI_DBNAME);
    $GENI_DBNAME = "geni-cm";
    require GeniHRN;
    require GeniCertificate;
    require GeniCredential;
    require GeniSlice;
    require GeniUser;
    
    my $token = $ARGV[0];
    my $query_result =
	DBQueryFatal("select lanid from shared_vlans where token='$token'");
    if (!$query_result->numrows) {
	fatal("No such shared vlan");
    }
    my ($lanid) = $query_result->fetchrow_array();
    my $vlan = VLan->Lookup($lanid);
    if (!defined($vlan)) {
	fatal("No such vlan $lanid");
    }
    #
    # This part is a pain, we need the geni user that created the lan.
    #
    my $experiment = $vlan->GetExperiment();
    my $slice      = GeniSlice->LookupByExperiment($experiment);
    my $geniuser   = GeniUser->Lookup($slice->creator_uuid(), 1);
    my $lan_urn    = GeniHRN::Generate($OURDOMAIN, "vlan", $vlan->lanid());
    my $lan_cert   = GeniCertificate->Lookup($lan_urn);
    if (!defined($lan_cert)) {
	my $lan_hrn = "${PGENIDOMAIN}.vlan" . "." . $vlan->lanid();
	$lan_cert =
	    GeniCertificate->Create({"urn"   => $lan_urn,
				     "hrn"   => $lan_hrn,
				     'email' => $TBOPS});
	if (!defined($lan_cert)) {
	    fatal("Could not generate certificate");
	}
    }
    my $lan_cred = GeniCredential->CreateSigned($lan_cert, $geniuser,
						$LOCALCM_FLAG);
    if (!defined($lan_cred)) {
	fatal("Could not generate credential");
    }
    print $lan_cred->asString();
    exit(0);
}

385 386 387 388
my $eid     = $ARGV[0];
my $lanname = $ARGV[1];
my $token   = $ARGV[2];

389 390 391 392
my $experiment = Experiment->Lookup($eid);
if (!defined($experiment)) {
    fatal("No such experiment $eid");
}
393 394
if ($experiment->state() ne EXPTSTATE_ACTIVE() && !$force) {
    fatal("Experiment is not active; use -f option to override");
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
}
my $vlan = VLan->Lookup($experiment, $lanname);
if (!defined($vlan)) {
    fatal("No such lan in $experiment");
}
my $lanid = $vlan->lanid();

my $query_result =
    DBQueryFatal("select * from shared_vlans where lanid='$lanid'");
if ($query_result->numrows) {
    fatal("$lanname is already shared");
}
if (! ($token =~ /^[-\w]*$/)) {
    fatal("Illegal characters in token"); 
}
$query_result =
    DBQueryFatal("select * from shared_vlans where token='token'");
if ($query_result->numrows) {
    fatal("$token is already used in the shared vlans table");
}
my $pid = $experiment->pid();
$eid    = $experiment->eid();
my $idx = $experiment->idx();
my $uid = $this_user->uid();
my $uid_idx = $this_user->uid_idx();

DBQueryFatal("insert into shared_vlans set open='$open', ".
	     "  pid='$pid', eid='$eid', exptidx='$idx', ".
	     "  token='$token', lanid='$lanid', vname='$lanname', ".
	     "  created=now(), creator='$uid', creator_idx='$uid_idx'");
425 426 427 428 429 430 431 432 433

#
# Since that worked, clear the switch path. Whatever the mapper
# computed will be wrong as soon as we start adding ports. By
# clearing the path, we are telling snmpit to compute a new path
# as ports are added or removed from the vlan.
#
$vlan->ClrSwitchPath();

434 435 436 437 438 439 440 441 442 443
exit(0);

sub fatal($)
{
    my ($mesg) = $_[0];

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