GeniCM.pm.in 226 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2008-2016 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 23 24 25 26 27 28
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29 30 31 32 33 34 35 36 37 38 39
#
package GeniCM;

#
# The server side of the CM interface on remote sites. Also communicates
# with the GMC interface at Geni Central as a client.
#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

40 41
@ISA    = qw(Exporter);
@EXPORT = qw();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42 43 44

use GeniDB;
use Genixmlrpc;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45 46
use GeniResponse;
use GeniTicket;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
47
use GeniCredential;
48
use GeniCertificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
49
use GeniSlice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50
use GeniAggregate;
51
use GeniAuthority;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
52
use GeniSliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53
use GeniUser;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54
use GeniRegistry;
55
use GeniUtil;
56
use GeniHRN;
57
use GeniXML;
58
use GeniStitch;
59
use GeniUsage;
60
use GeniImage;
61
use libtestbed;
62
use emutil;
63 64
use EmulabConstants;
use libEmulab;
65
use Lan;
66
use User;
67
use Experiment;
68
use NodeType;
69
use Node;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
70 71
use English;
use Data::Dumper;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
72
use XML::Simple;
73
use XML::LibXML;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
74
use Date::Parse;
75
use POSIX qw(strftime tmpnam ceil);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
76
use Time::Local;
77
use Compress::Zlib;
78
use File::Temp qw(tempfile);
79
use MIME::Base64;
80
use Digest::SHA1 qw(sha1_hex);
81
use POSIX ":sys_wait_h";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
82 83 84 85 86 87

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
88 89
my $TBBASE         = "@TBBASE@";
my $TBDOCBASE      = "@TBDOCBASE@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
90 91
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
92
my $MAINSITE       = @TBMAINSITE@;
93
my $ELABINELAB     = @ELABINELAB@;
94
my $PGENIDOMAIN    = "@PROTOGENI_DOMAIN@";
95
my $PROTOUSER 	   = "elabman";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
96
my $CREATEEXPT     = "$TB/bin/batchexp";
97
my $ENDEXP         = "$TB/bin/endexp";
98
my $NFREE	   = "$TB/bin/nfree";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
99
my $AVAIL	   = "$TB/sbin/avail";
100
my $PTOPGEN	   = "$TB/libexec/ptopgen";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
101 102
my $TBSWAP	   = "$TB/bin/tbswap";
my $SWAPEXP	   = "$TB/bin/swapexp";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
103 104
my $PLABSLICE	   = "$TB/sbin/plabslicewrapper";
my $NAMEDSETUP     = "$TB/sbin/named_setup";
105
my $EXPORTS_SETUP  = "$TB/sbin/exports_setup";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
106 107
my $VNODESETUP     = "$TB/sbin/vnode_setup";
my $GENTOPOFILE    = "$TB/libexec/gentopofile";
108
my $IPASSIGN       = "$TB/libexec/ipassign_wrapper";
109
my $TARFILES_SETUP = "$TB/bin/tarfiles_setup";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
110 111
my $MAPPER         = "$TB/bin/mapper";
my $VTOPGEN        = "$TB/bin/vtopgen";
112
my $SNMPIT         = "$TB/bin/snmpit";
113
my $RESERVEVLANS   = "$TB/sbin/protogeni/reservevlans";
114
my $NEWGROUP       = "$TB/bin/newgroup";
115 116
my $NEWPROJECT     = "$TB/sbin/newproj";
my $MAKEPROJECT    = "$TB/sbin/mkproj";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
117
my $PRERENDER      = "$TB/libexec/vis/prerender";
118 119
my $SUDO           = "/usr/local/bin/sudo";
my $WAP            = "$TB/sbin/withadminprivs";
120
my $XMLLINT	   = "/usr/local/bin/xmllint";
121
my $ADDAUTHORITY   = "$TB/sbin/protogeni/addauthority";
122
my $EMULAB_PEMFILE = "@prefix@/etc/genicm.pem";
123
my $TARINSTALL     = "/usr/local/bin/install-tarfile";
124
my $IMAGE_SETUP    = "$TB/sbin/image_setup";
125
my $IMAGE_IMPORT   = "$TB/sbin/image_import";
126
my $SHAREVLAN      = "$TB/sbin/sharevlan";
127
my $RFLINKS	   = "$TB/bin/rflinks";
128
my $FWNAME	   = "fw";
129
my $API_VERSION    = 1;
130 131
my $PROTOGENI_LOCALUSER   = @PROTOGENI_LOCALUSER@;
my $PROTOGENI_NONFSMOUNTS = @PROTOGENI_NONFSMOUNTS@;
132

133 134 135 136 137
# For location info.
my $default_longitude = undef;
my $default_latitude  = undef;
my $default_country   = undef;

138 139 140 141 142 143 144 145 146 147
#
# Tell the client what API revision we support.  The correspondence
# between revision numbers and API features is to be specified elsewhere.
# No credentials are required.
#
sub GetVersion()
{
    return GeniResponse->Create( GENIRESPONSE_SUCCESS, $API_VERSION );
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
148
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
149
# Respond to a Resolve request. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
150 151 152 153 154 155
#
sub Resolve($)
{
    my ($argref) = @_;
    my $uuid       = $argref->{'uuid'};
    my $cred       = $argref->{'credential'};
156 157
    my $type       = lc( $argref->{'type'} );
    my $hrn        = $argref->{'hrn'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
158 159 160 161

    if (! defined($cred)) {
	return GeniResponse->MalformedArgsResponse();
    }
162 163 164 165
    if (defined($uuid) && GeniHRN::IsValid($uuid)) {
	$hrn  = $uuid;
	$uuid = undef;
    }
166 167 168 169 170 171 172 173 174 175 176 177
    if( defined( $hrn ) && GeniHRN::IsValid( $hrn ) ) {
	my ($auth,$t,$id) = GeniHRN::Parse( $hrn );

	return GeniResponse->Create( GENIRESPONSE_ERROR, undef,
				     "Authority mismatch" )
	    if( $auth ne $OURDOMAIN );

	$type = lc( $t );
	
	$hrn = $id;	
    }
    if (! (defined($type) && ($type =~ /^(node)$/))) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
178 179 180
	return GeniResponse->MalformedArgsResponse();
    }
    # Allow lookup by uuid or hrn.
181
    if (! defined($uuid) && !defined( $hrn ) ) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
182 183 184 185 186 187
	return GeniResponse->MalformedArgsResponse();
    }
    if (defined($uuid) && !($uuid =~ /^[-\w]*$/)) {
	return GeniResponse->MalformedArgsResponse();
    }

188
    my $credential = GeniCredential::CheckCredential($cred);
189 190 191
    return $credential
	if (GeniResponse::IsResponse($credential));

192
    if ($type eq "node") {
193
	require Interface;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
194 195 196
	my $node;
	
	if (defined($uuid)) {
197
	    $node= GeniUtil::LookupNode($uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
198 199
	}
	else {
200
	    $node= GeniUtil::LookupNode($hrn);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
201
	}
202
	if (! defined($node)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
203 204 205
	    return GeniResponse->Create(GENIRESPONSE_SEARCHFAILED,
					undef, "Nothing here by that name");
	}
206

207
	my $rspec = GetAdvertisement(0, $node->node_id(), "0.1", undef);
208 209 210 211
	if (! defined($rspec)) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Could not start avail");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
212 213
	
	# Return a blob.
214
	my $blob = { "hrn"          => "${PGENIDOMAIN}." . $node->node_id(),
Leigh B. Stoller's avatar
Leigh B. Stoller committed
215
		     "uuid"         => $node->uuid(),
216
		     "role"	    => $node->role(),
217 218
		     "hostname"     =>
			 GeniUtil::FindHostname($node->node_id()),
219 220
		     "physctrl"     => 
			 Interface->LookupControl( $node->phys_nodeid() )->IP(),
221 222
		     "urn"          => GeniHRN::Generate( $OURDOMAIN,
							  "node",
223 224
							  $node->node_id() ),
		     "rspec"        => $rspec
Leigh B. Stoller's avatar
Leigh B. Stoller committed
225 226 227 228 229 230 231
		   };

	return GeniResponse->Create(GENIRESPONSE_SUCCESS, $blob);
    }
    return GeniResponse->Create(GENIRESPONSE_UNSUPPORTED);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
232 233 234 235 236 237
#
# Discover resources on this component, returning a resource availablity spec
#
sub DiscoverResources($)
{
    my ($argref) = @_;
238
    my $credstr   = $argref->{'credential'};
239
    my $available = $argref->{'available'} || 0;
240
    my $compress  = $argref->{'compress'} || 0;
241
    my $version   = $argref->{'rspec_version'} || undef;
242

243
    my $credential = GeniCredential::CheckCredential($credstr);
244 245
    return $credential
	if (GeniResponse::IsResponse($credential));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
246

247 248
    return DiscoverResourcesAux($available,
				$compress, $version, [$credential]);
249 250
}
# Helper function for V2.
251
sub DiscoverResourcesAux($$$$)
252
{
253
    my ($available, $compress, $version, $credentials) = @_;
254
    my $user_urn  = $ENV{'GENIRN'};
255
    $version   = "2"
256 257 258
	if (!defined($version));

    # Sanity check since this can come from client.
259
    if (! ($version eq "0.1" || $version eq "0.2" || $version eq "2"
260 261 262
	   || $version eq "3"
	   || $version eq "PG 0.1" || $version eq "PG 0.2"
	   || $version eq "PG 2")) {
263 264 265
	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "Improper version request");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
266

267 268 269
    # Oh, for $*%(s sake.  Frontier::RPC2 insists on representing a
    # Boolean as its own object type -- which Perl always interprets as
    # true, regardless of the object's value.  Undo all of that silliness.
270 271 272 273 274 275
    if (defined($available) && ref($available) eq 'Frontier::RPC2::Boolean') {
	$available = $available->value;
    }
    if (defined($compress) && ref($compress) eq 'Frontier::RPC2::Boolean') {
	$compress = $compress->value;
    }
276

Leigh B. Stoller's avatar
Leigh B. Stoller committed
277 278 279 280
    #
    # A sitevar controls whether external users can get any nodes.
    #
    my $allow_externalusers = 0;
281
    if (!GetSiteVar('protogeni/allow_externalusers', \$allow_externalusers)){
282 283
	      # Cannot get the value, say no.
	      $allow_externalusers = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
284
    }
285 286 287 288 289 290 291

    # Figure out if user has a credentials that exempts him
    # from the following policy. If external users are blocked access
    # and he presents a credential that exempts him from it, 
    # then he should get access.
    my $isExempted = 0;
    foreach my $credential (@$credentials) {
292
        if (GeniXML::PolicyExists('allow_externalusers', $credential) == 1) {
293 294 295 296 297 298
        $isExempted = 1;
        last;
      }
    }

    if (!$allow_externalusers && !$isExempted) {
299
	my $user = GeniUser->Lookup($user_urn, 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
300 301 302 303 304 305 306
	# No record means the user is remote.
	if (!defined($user) || !$user->IsLocal()) {
	    return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
					"External users temporarily denied");
	}
    }

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    #
    # See if one of the credentials is a slice credential. If it is, and
    # that slice is active, pass it to ptopgen so that it includes the current
    # resources as available.
    #
    my $experiment = undef;
    foreach my $credential (@$credentials) {
	my ($auth, $type, $id) = GeniHRN::Parse($credential->target_urn());
	if ($type eq "slice") {
	    # Might not exist here yet.
	    my $slice = GeniSlice->Lookup($credential->target_urn());
	    if (defined($slice)) {
		# See if the local experiment exists yet.
		$experiment = Experiment->Lookup($slice->uuid());
	    }
	    last;
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
326
    #
327
    # Acquire the advertisement from ptopgen and compress it if requested.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
328
    #
329
    my $xml = GetAdvertisement($available, undef, $version, $experiment);
330
    if (! defined($xml)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
331 332 333 334
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not start avail");
    }

335 336 337 338 339 340
    if( $compress ) {
	my $coder = Frontier::RPC2->new();
	my $base64 = encode_base64( compress( $xml ) );
	$xml = $coder->base64( $base64 );	
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
341 342
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $xml);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
343

344 345 346
#
# Use ptopgen in xml mode to spit back an xml file. 
#
347
sub GetAdvertisement($$$$)
348
{
349
    my ($available, $pc, $version, $experiment) = @_;
350
    my $xml = undef;
351 352
    my $gotlock = 0;
    my $filename = "/var/tmp/protogeni_resources.xml";
353

354 355 356 357 358 359 360
    $version = "0.1"
	if ($version eq "PG 0.1");
    $version = "0.2"
	if ($version eq "PG 0.2");
    $version = "2"
	if ($version eq "PG 2");

361
    my $invocation = "$PTOPGEN -x -g $version -r -f";
362 363
    if (defined($experiment)) {
	my $eid = $experiment->eid();
364
	my $pid = $experiment->pid();
365
	$invocation .= " -p $pid -e $eid";
366
    }
367 368 369 370
    $invocation .= " -a" unless $available;
    if (defined($pc)) {
	$invocation .= " -1 $pc";
    }
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    if (!defined($pc)) {
      again:
	#
	# Grab a global script lock. This will ensure that only one ptopgen
	# runs at a time, and everyone else who comes along while that first
	# one is running, will share the same results file.
	#
	# Need to use a well known name, unless we want to share that name
	# via the DB. Lets be simple about it for now.
	#
	if ((my $locked = TBScriptLock("discover", 1)) != TBSCRIPTLOCK_OKAY()) {
	    if ($locked == TBSCRIPTLOCK_IGNORE) {
		#
		# Previous locker finished ptopgen.
		# Grab the file if it exists (small race), otherwise
		# try again from the top.
		#
		if (open(AVAIL, "$filename")) {
		    $xml = "";
		    while (<AVAIL>) {
			$xml .= $_;
		    }
		    close(AVAIL);
		    return $xml;
		}
		goto again;
	    }
	    else {
		print STDERR "Could not get ptopgen lockfile\n";
		return undef;
	    }
	}
	else {
	    #
	    # We got the lock so we get to run ptopgen.
	    #
	    $gotlock = 1;
	}
    }
410
    if (open(AVAIL, "$invocation |")) {
411
	$xml = "";
412 413 414 415 416
	while (<AVAIL>) {
	    $xml .= $_;
	}
	close(AVAIL);
    }
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    #
    # The lock holder has to create the new version of the file for
    # anyone waiting. Need to do this atomically so that anyone still
    # reading the previous version does not get inconsistent data.
    #
    if ($gotlock) {
	my ($fh, $tempname) = tempfile(UNLINK => 0, DIR => "/var/tmp");
	if (!defined($fh)) {
	    print STDERR "Could not create temporary file: $!\n";
	    $xml = undef;
	}
	else {
	    print $fh $xml;
	    close($fh);
	    if (! rename($tempname, $filename)) {
		print STDERR "Could not rename temporary file: $!\n";
		$xml = undef;
	    }
	}
	TBScriptUnlock();
    }
438 439 440
    return $xml;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
441
#
442
# Update a ticket with a new rspec.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
443
#
444
sub UpdateTicket($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
445 446
{
    my ($argref) = @_;
447 448 449 450 451 452 453 454 455 456

    return GetTicket($argref, 1);
}

#
# Respond to a GetTicket request. 
#
sub GetTicket($;$)
{
    my ($argref, $isupdate) = @_;
457
    my $rspecstr   = $argref->{'rspec'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
458
    my $impotent   = $argref->{'impotent'};
459 460
    my $credstr    = $argref->{'credential'};
    my $tickstr    = $argref->{'ticket'};
461 462 463 464 465 466 467
    my $ticket;

    # Default to no update
    $isupdate = 0
	if (!defined($isupdate));
    $impotent = 0
	if (!defined($impotent));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
468

469
    if (! defined($credstr)) {
470
	return GeniResponse->MalformedArgsResponse();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
471
    }
472
    if (!defined($rspecstr)) {
473 474
	return GeniResponse->MalformedArgsResponse();
    }
475 476 477 478
    if (! ($rspecstr =~ /^[\040-\176\012\015\011]+$/)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "Improper characters in rspec");
    }
479
    my $credential = GeniCredential::CheckCredential($credstr);
480 481 482
    return $credential
	if (GeniResponse::IsResponse($credential));

483
    if ($isupdate) {
484
	$ticket = CheckTicket($tickstr, $credential->target_urn());
485 486
	return $ticket
	    if (GeniResponse::IsResponse($ticket));
487
    }
488
    return GetTicketAux($credential,
489
			$rspecstr, $isupdate, $impotent, 0, 1, 0, $ticket);
490
}
491

492
sub GetTicketAux($$$$$$$$$@)
493
{
494
    my ($credential, $rspecstr, $isupdate, $impotent, $v2, $level, $usetracker,
495
	$ticket, $speaksfor, @morecreds) = @_;
496
    
497 498 499 500 501 502
    defined($credential) &&
	($credential->HasPrivilege( "pi" ) or
	 $credential->HasPrivilege( "instantiate" ) or
	 $credential->HasPrivilege( "bind" ) or
	 return GeniResponse->Create( GENIRESPONSE_FORBIDDEN, undef,
				      "Insufficient privilege" ));
503
    
504 505
    my $slice_urn = $credential->target_urn();
    my $user_urn  = $credential->owner_urn();
506
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
507
    #
508
    # Create user from the certificate.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
509
    #
510
    my $user = CreateUserFromCertificate($credential);
511 512
    return $user
	if (GeniResponse::IsResponse($user));
513 514 515

    # Bump activity. Does not matter if request fails ...
    $user->BumpActivity();
516
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
517
    #
518
    # Create slice from the certificate.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
519
    #
520 521
    my $slice = GeniSlice->Lookup($slice_urn);
    if (!defined($slice)) {
522
	if ($isupdate) {
523
	    print STDERR "Could not locate slice $slice_urn for Update\n";
524
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
525 526 527
					"No slice found for UpdateTicket");
	}
	$slice = CreateSliceFromCertificate($credential, $user);
528 529
	return $slice
	    if (GeniResponse::IsResponse($slice));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
530
    }
531 532 533
    $slice->SetSpeaksFor($speaksfor)
	if (defined($speaksfor));
	
534
    main::AddLogfileMetaDataFromSlice($slice);
535
    
536
    return GetTicketAuxAux($slice, $user, $rspecstr,
537 538
			   $isupdate, $impotent, $v2, $level, $usetracker,
			   $ticket, [$credential, @morecreds], $speaksfor);
539
}
540
sub GetTicketAuxAux($$$$$$$$$$$)
541
{
542
    my ($slice, $user, $rspecstr, $isupdate,
543 544
	$impotent, $v2, $level, $usetracker,
	$ticket, $credentials, $speaksfor) = @_;
545 546 547
    my $response    = undef;
    my $restorevirt = 0;	# Flag to restore virtual state
    my $restorephys = 0;	# Flag to restore physical state
548
    require OSImage;
549
    require VirtExperiment;
550 551 552 553 554 555

    #
    # We need this below to sign the ticket.
    #
    my $authority = GeniCertificate->LoadFromFile($EMULAB_PEMFILE);
    if (!defined($authority)) {
556
	print STDERR " Could not load authority for $EMULAB_PEMFILE\n";
557 558 559
	return GeniResponse->Create(GENIRESPONSE_ERROR);
    }

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    #
    # Run xmllint on the rspec to catch format errors.
    #
    my ($fh, $filename) = tempfile(UNLINK => 0);
    if (!defined($fh)) {
	print STDERR "Could not create temp file for rspec\n";
	return GeniResponse->Create(GENIRESPONSE_ERROR);
    }
    print $fh $rspecstr;
    close($fh);
    my $xmlerrors = `$XMLLINT --noout $filename 2>&1`;
    unlink($filename);
    if ($?) {
	return GeniResponse->Create(GENIRESPONSE_ERROR,
				    $xmlerrors,
				    "rspec is not well formed");
    }

578 579
    my $rspec = GeniXML::Parse($rspecstr);
    if (! defined($rspec)) {
580
	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
581
				    "Error Parsing rspec XML");
582
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
583

584 585 586 587 588 589
    my $rspecVersion = GeniXML::GetXmlVersion($rspec);
    if (! defined($rspecVersion)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "Unknown RSpec Version");
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
590 591 592 593
    #
    # A sitevar controls whether external users can get any nodes.
    #
    my $allow_externalusers = 0;
594
    if (!GetSiteVar('protogeni/allow_externalusers', \$allow_externalusers)){
595 596 597 598
	    # Cannot get the value, say no.
	    $allow_externalusers = 0;
    }

599 600 601 602 603 604 605 606 607 608 609
    # Image tracker.
    my $use_imagetracker;
    if (!GetSiteVar('protogeni/use_imagetracker', \$use_imagetracker)) {
	# Cannot get the value, say no.
	$use_imagetracker = 0;
    }
    # But the Portal is currently the one telling us to use the tracker
    # for specific slices.
    $use_imagetracker = 1
	if ($use_imagetracker && $usetracker);

610 611 612 613
    #
    # Watch for sites that do not support openflow, we want to fail early.
    #
    my $no_openflow = 0;
614
    my $ignore_openflow = 0;
615 616 617 618
    if (!GetSiteVar('general/no_openflow', \$no_openflow)){
	# Cannot get the value, say no.
	$no_openflow = 1;
    }
619 620 621 622
    if ($no_openflow =~ /ignore/) {
	$no_openflow = 1;
	$ignore_openflow = 1;
    }
623

624 625 626 627 628 629 630 631 632 633
    # Figure out if user has a credentials that exempts him
    # from the following policy. If external users are blocked access
    # and he presents a credential that exempts him from it, 
    # then he should get access.
    my $isExempted = 0;
    foreach my $credential (@$credentials) {
      if (1 == GeniXML::PolicyExists('allow_externalusers', $credential)) {
        $isExempted = 1;
        last;
      }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
634
    }
635 636 637

    if (!$allow_externalusers && !$isExempted && !$user->IsLocal()) {
	    return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
Leigh B. Stoller's avatar
Leigh B. Stoller committed
638 639 640 641
				    "External users temporarily denied");
    }
    
    #
642
    # For now all tickets expire very quickly (minutes), but once the
Leigh B. Stoller's avatar
Leigh B. Stoller committed
643
    # ticket is redeemed, it will expire according to the rspec request.
644 645
    # If nothing specified in the rspec, then it will expire when the
    # slice record expires, which was given by the expiration time of the
646 647
    # slice credential, or the local policy max_sliver_lifetime. See
    # CreateSliceFromCertificate() in this file.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
648
    #
649
    my $expires = GeniXML::GetExpires($rspec);
650
    if (defined($expires)) {
651 652
	if (GeniResponse::IsResponse($expires)) {
	    return $expires;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
653
	}
654 655
	# Note "checkonly" flag; we do not actually change the slice
	# until the ticket is redeemed. 
656
	my $tmp = SetSliceExpiration($slice, $expires, 1, 0, @{ $credentials });
657 658
	if (GeniResponse::IsResponse($tmp)) {
	    return $tmp;
659
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
660
    }
661 662 663 664 665 666 667

    #
    # Lock the ticket so it cannot be released.
    #
    if (defined($ticket) && $ticket->stored() && $ticket->Lock() != 0) {
	return GeniResponse->BusyResponse("ticket");
    }
668 669 670
    if (defined($ticket)) {
	$ticket->SetSlice($slice);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
671 672 673 674 675 676
    
    #
    #
    # Lock the slice from further access.
    #
    if ($slice->Lock() != 0) {
677 678 679
	$ticket->UnLock()
	    if (defined($ticket) && $ticket->stored());
	return GeniResponse->BusyResponse("slice");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
680
    }
681 682 683
    # Shutdown slices get nothing.
    if ($slice->shutdown()) {
	$slice->UnLock();
684 685
	$ticket->UnLock()
	    if (defined($ticket) && $ticket->stored());
686 687 688
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Slice has been shutdown");
    }
689 690 691 692 693 694 695 696
    # Ditto for expired.
    if ($slice->IsExpired()) {
	$slice->UnLock();
	$ticket->UnLock()
	    if (defined($ticket) && $ticket->stored());
	return GeniResponse->Create(GENIRESPONSE_REFUSED, undef,
				    "Slice has expired");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
697

698
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
699
    # For now, there can be only a single toplevel aggregate per slice.
700
    # The existence of an aggregate means the slice is active here. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
701
    #
702
    my $aggregate = GeniAggregate->SliceAggregate($slice);
703 704 705 706 707
    if (!$isupdate) {
	if (defined($aggregate)) {
	    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				     "Already have an aggregate for slice");
	    goto bad;
708 709
	}
    }
710
    elsif ($v2 && $level > 0 && !defined($ticket) && !defined($aggregate)) {
711 712 713 714
	print STDERR "No aggregate for $slice in version two API\n";
	$response = GeniResponse->Create(GENIRESPONSE_ERROR);
	goto bad;
    }
715

716
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
717
    # We need this now so we can form a virtual topo.
718
    #
719 720 721 722 723 724 725 726 727 728 729
    my $slice_experiment = GeniExperiment($slice, $user);
    if (GeniResponse::IsResponse($slice_experiment)) {
	$response = $slice_experiment;
	$slice_experiment = undef;
	goto bad;
    }
    my $realuser = FlipToUser($slice, $user);
    if (! (defined($realuser) && $realuser)) {
	$response = GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					 "FlipToUser Error");
	print STDERR "Error flipping to real user\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
730 731 732 733
	goto bad;
    }
    my $pid = $slice_experiment->pid();
    my $eid = $slice_experiment->eid();
734 735 736 737 738

    #
    # Mark the experiment locally as coming from the cooked interface.
    # This changes what tmcd returns to the local nodes.
    #
739
    my $generated_by = GeniXML::GetText("generated_by", $rspec);
740 741
    if (defined($generated_by) &&
	$generated_by eq "libvtop") {
742 743 744 745
	$slice_experiment->Update({"geniflags" =>
				       $Experiment::EXPT_GENIFLAGS_EXPT|
				       $Experiment::EXPT_GENIFLAGS_COOKED});
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
746 747 748 749 750 751 752 753 754 755 756 757 758
    
    #
    # Create a virt topology object. We are going to load this up as we
    # process the rspec.
    #
    my $virtexperiment = VirtExperiment->CreateNew($slice_experiment);
    if (!defined($virtexperiment)) {
	print STDERR "Could not create VirtExperiment object!\n";
	$response = GeniResponse->Create(GENIRESPONSE_ERROR);
	goto bad;
    }
    # Turn off fixnode; we will control this on the commandline.
    $virtexperiment->allowfixnode(0);
759 760 761 762 763 764 765

    #
    # Allow user to control the multiplex factor. Note that ptopgen
    # will not allow the mfactor to be more then what we set as the
    # max for the node, but in general we set it low here.
    #
    $virtexperiment->multiplex_factor(GeniXML::MultiplexFactor($rspec) || 10);
766 767 768 769 770
    #
    # The packing strategy is sorta independent; the user can specify either
    # pack or balance (load balance).
    #
    my $packing_option = GeniXML::PackingStrategy($rspec);
771

772
    #
773
    # User can turn off routing.
774
    #
775 776 777 778 779 780 781 782 783 784 785 786
    my $routertype = GeniXML::RoutingStyle($rspec);
    if (!defined($routertype)) {
	$routertype = "static-ddijk";
    }
    elsif ($routertype eq "static") {
	$routertype = "static-ddijk";
    }
    elsif ($routertype ne "none") {
	$response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Bad routing style: $routertype");
	goto bad;
    }
787
    
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
    #
    # User can set the delay image. 
    #
    if (defined(GeniXML::DelayImage($rspec))) {
	my $delayurn = GeniXML::DelayImage($rspec);
	
	if (!GeniHRN::IsValid($delayurn)) {
	    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					     "Invalid URN: $delayurn");
	    goto bad;
	}
	my $hrn = GeniHRN->new($delayurn);
	my (undef,$ospid,$os,$vers) = $hrn->ParseImage();
	if ($hrn->type() ne "image" || !defined($ospid) || !defined($os)) {
	    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					     "Malformed image URN: $delayurn");
	    goto bad;
	}
	my $delayname = $os;
	$delayname = "${ospid}/" . $delayname
	    if (defined($ospid));
	$delayname .= ":${vers}"
	    if (defined($vers));
	
	$virtexperiment->delay_osname($delayname);
    }
814

815 816 817 818 819 820 821 822 823 824
    #
    # Add global vtypes.
    #
    my $vtypes_result =
	emdb::DBQueryWarn("select * from global_vtypes");
    if (!$vtypes_result) {
	$response = GeniResponse->Create(GENIRESPONSE_ERROR);
	goto bad;
    }
    while (my $row = $vtypes_result->fetchrow_hashref()) {
825
	$virtexperiment->NewTableRow("virt_vtypes",
826 827 828 829
				     {"name"     => $row->{'vtype'},
				      "members"  => $row->{'types'},
				      "weight"   => $row->{'weight'}
				     });
830
    }
831

832 833 834 835 836
    # Need to move this someplace else; the parser adds a bunch.
    $virtexperiment->NewTableRow("virt_agents",
				 {"vnode"      => "*",
				  "vname"      => "ns",
				  "objecttype" => "6"});
837 838 839 840
    $virtexperiment->NewTableRow("virt_agents",
				 {"vnode"      => "*",
				  "vname"      => "linktest",
				  "objecttype" => "7"});
841

842 843 844
    #
    # Look for toplevel address pools
    #
845 846 847 848 849 850 851
    if (Node::HaveExperimentNodes()) {
	my $address_pools = GeniXML::GetAddressPools($rspec);
	foreach my $pool (@{ $address_pools }) {
	    if (! defined($pool->{'cmurn'}) ||
		$pool->{'cmurn'} eq $ENV{'MYURN'})
	    {
		if ($pool->{'type'} ne "any") {
852
		    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
853 854 855 856 857
		     "Only public address pools of type any are supported");
		    goto bad;
		}
		print STDERR 'DEBUG: Adding row to virt_address_allocation';
		$virtexperiment->NewTableRow("virt_address_allocation",
858 859 860 861
					 {"pool_id" => $pool->{'client_id'},
					  "count" => $pool->{'count'},
					  "restriction" => $pool->{'type'},
					  "version" => "ipv4" });
862
	    }
863 864 865
	}
    }

866 867 868 869 870 871 872 873 874 875 876 877
    #
    # Look for toplevel elabinelab section.
    #
    my $elabinelab_settings = GeniXML::GetElabInElabSettings($rspec);
    if (defined($elabinelab_settings)) {
	$virtexperiment->elab_in_elab(1);
	if (exists($elabinelab_settings->{'singlenet'})) {
	    $virtexperiment->elabinelab_singlenet(1);
	}
	if (exists($elabinelab_settings->{'xen'})) {
	    $virtexperiment->multiplex_factor(2);
	}
878
    }
879 880 881 882
    #
    # Note that we set the mounts to "genidefault" when we create the
    # container experiment. So this overrides.
    #
883 884
    if ($PROTOGENI_NONFSMOUNTS) {
	$virtexperiment->nonfsmounts(1);
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
	# This is the new way of doing things.
	$virtexperiment->nfsmounts("none");
    }
    elsif (GeniXML::FromEmulabPortal($rspec) && $PROTOGENI_LOCALUSER) {
	#
	# By not setting, we get standard emulab mounts which include
	# /users. Not sure if I want to explictly set this.
	#
	# But we do not let anyone do this, at the moment only the local
	# SA can do that, which means the authority that contacted us
	# must be our SA. Not even users from our SA can specify this.
	#
	my $hrn = GeniHRN->new($ENV{"GENIURN"});
	if (defined($hrn) &&
	    $hrn->domain() eq $OURDOMAIN && $hrn->IsSA()) {
	    $virtexperiment->nfsmounts("emulabdefault");
	}
902 903
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
904
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
905 906
    # An rspec is a structure that requests specific nodes. If those
    # nodes are available, then reserve it. Otherwise the ticket
Leigh B. Stoller's avatar
Leigh B. Stoller committed
907 908
    # cannot be granted.
    #
909 910 911
    my %namemap  = ();
    my %colomap  = ();
    my %ifacemap = ();
912
    my %iface2node = ();
913
    my %vportmap = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
914
    my %nodemap  = ();
915
    my %bridgemap= ();
916
    my @nodeids  = ();
917
    my %lannodes = ();
918
    my %allnodes = ();
919
    # For stitching, keep track of external nodes and links.
920 921 922
    my %external_nodemap  = ();
    my %external_linkmap  = ();
    my %external_vportmap = ();
923
    my %external_lanrefs  = ();
Leigh B Stoller's avatar
Leigh B Stoller committed
924
    my $routable_ip_count = 0;
925
    my $sync_server = ($isupdate ? $slice_experiment->sync_server() : undef);
926

927 928
    # Always do this to avoid buildup.
    $slice_experiment->ClearBackupState();
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953

    #
    # Find all the node client ids, so we can support binding VMs to other
    # nodes in the topology. We could enforce that the rspec has to put
    # those nodes first, but that would be annoying to users. And me.
    #
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
	# Let remote nodes pass through.
	next
	    if (!GeniXML::IsLocalNode($ref));

	# Skip lan nodes; they are fake.
	next
	    if (GeniXML::IsLanNode($ref));

	my $node_nickname = GeniXML::GetVirtualId($ref);

	# Might as well do a duplicate check while we are here.
	if (exists($allnodes{lc($node_nickname)})) {
	    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					     "Duplicate node $node_nickname");
	    goto bad;
	}
	$allnodes{lc($node_nickname)} = 1;
    }
954
    
955 956 957 958 959 960 961
    #
    # If this is a ticket update, we want to seed the namemap with
    # existing nodes. This is cause the rspec might refer to wildcards
    # that were already bound in a previous call. We also want to know
    # what nodes are currently reserved in case we have to release some.
    #
    if ($isupdate) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
	if ($slice_experiment->BackupVirtualState()) {
	    print STDERR "Could not backup virtual state!\n";
	    $response = GeniResponse->Create(GENIRESPONSE_ERROR);
	    goto bad;
	}
	if ($slice_experiment->RemoveVirtualState()) {
	    print STDERR "Could not remove virtual state!\n";
	    $response = GeniResponse->Create(GENIRESPONSE_ERROR);
	    goto bad;
	}
	$restorevirt = 1;

	if ($slice_experiment->BackupPhysicalState()) {
	    print STDERR "Could not backup physical state!\n";
	    $response = GeniResponse->Create(GENIRESPONSE_ERROR);
	    goto bad;
	}
979 980 981 982 983 984 985
	my $oldrspec;
	if ($v2 && defined($aggregate)) {
	    $oldrspec = $aggregate->GetManifest(0);
	}
	else {
	    $oldrspec = $ticket->rspec();
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
986
	
987
	foreach my $ref (GeniXML::FindNodes("n:node",
988
					    $oldrspec)->get_nodelist()) {
989
	    # Let remote nodes pass through.
990
	    next
991
		if (!GeniXML::IsLocalNode($ref));
992

993 994
	    # Skip lan nodes; they are fake.
	    next
995
		if (GeniXML::IsLanNode($ref));
996

997
	    my $node_nickname = GeniXML::GetVirtualId($ref);
998
	    my $colocate      = GeniXML::GetColocate($ref);
999
	    my $component_id  = GeniXML::GetNodeId($ref);
1000 1001
	    my $vnode_id      = GeniXML::GetVnodeId($ref);
	    my $node = GeniUtil::LookupNode($vnode_id);
1002 1003
	    if (!defined($node)) {
		$response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
1004
				 "Bad resource $component_id in ticket");
1005 1006
		goto bad;
	    }
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	    #
	    # Is the node a virtual node? Must be an update to an
	    # existing sliver/ticket, since we now return the node_id
	    # of the allocated virtual node, not the physical node.
	    #
	    if ($node->isvirtnode()) {
		my $pnode = Node->Lookup($node->phys_nodeid());
		if (!defined($pnode)) {
		    $response =
			GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				     "No physical resource for $component_id");
		    goto bad;
		}
		$node = $pnode;
	    }
1022 1023 1024 1025 1026
	    $namemap{$node_nickname} = $node;
	    $colomap{$colocate} = $node
		if (defined($colocate));
	}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1027

1028
    print GeniXML::Serialize($rspec);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1029

1030
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
1031
	my $component_id  = GeniXML::GetNodeId($ref);
1032
	my $vnode_id      = GeniXML::GetVnodeId($ref);
1033
	my $manager_id    = GeniXML::GetManagerId($ref);
1034
	my $node_nickname = GeniXML::GetVirtualId($ref);
1035 1036
	my $colocate      = GeniXML::GetColocate($ref);
	my $subnode_of    = GeniXML::GetSubnodeOf($ref);
1037
	my $instantiate_on= GeniXML::GetInstantiateOn($ref);
1038 1039
	my $virtualization_type = GeniXML::GetVirtualizationType($ref);
	
1040
	my $virtualization_subtype
1041 1042
                          = GeniXML::GetVirtualizationSubtype($ref);
	my $exclusive     = GeniXML::GetExclusive($ref);
1043
	my $tarfiles      = GeniXML::GetTarball($ref);
1044
	my $pctype;
1045
	my ($osname, $osinfo);
1046
	my $parent_osname;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1047
	my $node;
1048 1049
	my $isbridge    = 0;
	my $isfirewall  = 0;
Leigh B Stoller's avatar
Leigh B Stoller committed
1050
	my $xensettings;
1051
	my $fwsettings;
1052
	
1053 1054 1055 1056 1057
	# Always populate iface2node mapping, even if we let the node
	# pass through.
	foreach my $linkref (GeniXML::FindNodes("n:interface",
						$ref)->get_nodelist()) {
	    my $virtual_id   = GeniXML::GetInterfaceId($linkref);
1058 1059 1060 1061 1062 1063 1064
	    if (exists($iface2node{$virtual_id})) {
		$response =
		    GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Duplicate interface $virtual_id on ".
					 "node $node_nickname");
		goto bad;
	    }
1065 1066 1067
	    $iface2node{$virtual_id} = $node_nickname;
	}

1068
	# Let remote nodes pass through.
1069 1070 1071 1072
	if (! GeniXML::IsLocalNode($ref)) {
	    $external_nodemap{$node_nickname} = $ref;
	    next;
	}
1073

1074
	#
1075 1076 1077 1078 1079
	#
	# Lan nodes are fake and do not go into the virt topo. Need
	# to remember them though, for when we do the links below.
	# They are still in the returned ticket though. 
	#
1080
	if (GeniXML::IsLanNode($ref)) {
1081 1082 1083 1084
	    $lannodes{$node_nickname} = $ref;
	    next;
	}

1085 1086 1087 1088 1089 1090 1091 1092
	#
	# Check for total hostname length > 63 characters. This
	# breaks a lot of clients. Do this until we have a plan
	# for dealing with it on the clients. Why 63 instead of 64?
	# Cause of a bug in the event library code, that is now fixed,
	# but will not make it out to all images for a while. 
	#
	my $fullhostname = $node_nickname . ".${eid}.${pid}.${OURDOMAIN}";
1093
	if (0 && length($fullhostname) > 63) {
1094 1095 1096 1097
	    $response = GeniResponse->Create(GENIRESPONSE_TOOBIG, undef,
				     "Hostname > 63 characters: $fullhostname");
	    goto bad;
	}
1098 1099 1100
	#
	# Check for disk_image request. Specified as a URN. 
	#
1101
	my $diskref = GeniXML::GetDiskImage($ref);
1102 1103
	if (defined($diskref)) {
	    my $dname = GeniXML::GetText("name", $diskref);
1104
	    my $url   = GeniXML::GetText("url", $diskref);
1105

1106 1107 1108 1109 1110
	    # url is deprecated; name can be anything.
	    if (defined($dname) && $dname =~ /^http/) {
		$url = $dname;
	    }

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	    if (defined($url)) {
		if (! TBcheck_dbslot($url, "virt_nodes", "osname",
				 TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)){
		    $response =
			GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					     "Invalid disk image url: $url");
		    goto bad;
		}
		#
		# Pass it right through; we load them later.
		# There is no osinfo, but see below. 
		#
		$osname = $url;
	    }
	    elsif (defined($dname)) {
1126 1127 1128 1129 1130 1131 1132
		if (! GeniHRN::IsValid($dname)) {
		    $response =
			GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Malformed image URN: $dname");
		    goto bad;
		}
		my ($auth,$type,$id) = GeniHRN::Parse($dname);
1133 1134
		my ($ospid,$os,undef,$vers) =
		    ($id =~ m{([^/]+)//([^/]+)(//(\d+))?});		
1135 1136 1137 1138 1139 1140
		if ($type ne "image" || !defined($ospid) || !defined($os)){
		    $response =
			GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Malformed image URN: $dname");
		    goto bad;
		}
1141
		$osinfo = OSImage->Lookup($ospid, $os, $vers);
1142
		if (!defined($osinfo)) {
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
		    if ($use_imagetracker) {
			my $image = GeniImage::MapToLocalImage($dname, $pid);
			if (GeniResponse::IsError($image)) {
			    $response = $image;
			    goto bad;
			}
			$osname = ($image->IsLocal() ?
				   $image->versname() :
				   $image->metadata_url());
		    }
		    else {
			$response =
			    GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
						 "Unknown image URN: $dname");
			goto bad;
		    }
1159
		}
1160 1161 1162 1163 1164 1165 1166
		else {
		    #
		    # The OS must be in the current project, or it must
		    # be global (okay, shared).
		    #
		    if (! ($osinfo->shared() ||
			   $osinfo->pid() eq $slice_experiment->pid())) {
1167
			if ($PROTOGENI_LOCALUSER && $user->IsLocal()) {
1168
			    my $image = OSImage->Lookup($ospid,$os,$vers);
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
			    if (!defined($image)) {
				$response =
				    GeniResponse->Create(GENIRESPONSE_BADARGS,
							 undef,
							 "No image: $dname");
				goto bad;
			    }
			    goto noperm
				if (!$image->AccessCheck($user->emulab_user(),
							 TB_IMAGEID_READINFO));
			}
			else {
			  noperm:
			    $response =
				GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				 "Insufficient permission to use $dname");
			    goto bad;			
			}
1187 1188 1189 1190 1191 1192 1193
		    }
		    #
		    # This is only going to be used in raw mode. 
		    #
		    $osname  = "$ospid/$os";
		    $osname .= ":${vers}" if (defined($vers));
		}
1194 1195 1196
	    }
	}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1197 1198 1199
	if (defined($virtualization_type)) {
	    if ($virtualization_type eq "emulab-vnode") {
		if (defined($virtualization_subtype)) {
1200 1201
		    $pctype = "pcvm";
		    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1202 1203 1204 1205
		    if ($virtualization_subtype eq "emulab-jail") {
			$osname = "FBSD-JAIL";
		    }
		    elsif ($virtualization_subtype eq "emulab-openvz") {
1206 1207 1208 1209 1210 1211 1212 1213
			# Allow caller to set the image to use, but also
			# trick to set the parent. 
			if (defined($osinfo)) {
			    if (! $osinfo->IsSubOS()) {
				$parent_osname = $osname;
				$osname = "OPENVZ-STD";
			    }
			}
1214 1215
			elsif (!defined($osname)) {
			    # Allow for url above.
1216 1217
			    $osname = "OPENVZ-STD";
			}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1218
		    }
1219 1220
		    elsif ($virtualization_subtype eq "emulab-xen" ||
			   $virtualization_subtype eq "default-vm") {
1221 1222 1223 1224 1225
			# Allow caller to set the image to use, but also
			# trick to set the parent. 
			if (defined($osinfo)) {
			    if (! $osinfo->IsSubOS()) {
				$parent_osname = $osname;
1226
				$osname = "UBUNTU14-64-STD";
1227 1228
			    }
			}
1229
			# If not set, we will pick up the default_image below.
Leigh B Stoller's avatar
Leigh B Stoller committed
1230 1231 1232 1233 1234 1235
			#
			# Look for the knobs
			#
			if (GeniXML::HasXenSettings($ref)) {
			    $xensettings = GeniXML::GetXenSettings($ref);
			}
1236 1237 1238
			my $ptype = GeniXML::XenPtype($ref);
			$pctype = $ptype
			    if (defined($ptype));
1239
			$virtexperiment->encap_style("vlan");
1240 1241 1242 1243 1244 1245 1246

			#
			# Per-vnode firewall options.
			#
			if (GeniXML::HasFirewallSettings($ref)) {
			    $fwsettings = GeniXML::GetFirewallSettings($ref);
			}
1247
		    }
1248 1249 1250 1251
		    elsif ($virtualization_subtype eq "emulab-spp") {
			$osname = "SPPVM-FAKE";
			$pctype = "sppvm";
			# Lets force to shared node.
1252
			if (! GeniXML::SetExclusive($ref, 0)) {
1253 1254 1255
			    $response
				= GeniResponse->Create(GENIRESPONSE_BADARGS,
						       undef,
1256 1257
				       "Malformed rspec: ".
				       "Cannot set exclusive tag to false");
1258 1259 1260
			    goto bad;
			}
			$exclusive = 0;
1261 1262
			# Kludge for libvtop.
			$virtexperiment->multiplex_factor(1);
1263
			$virtexperiment->encap_style("vlan");
1264
		    }
1265 1266 1267 1268 1269 1270
		    elsif ($virtualization_subtype eq "emulab-bbg") {
			$osname = "BBGENIVM-FAKE";
			$pctype = "bbgenivm";
			# Lets force to shared node.
			GeniXML::SetExclusive($ref, 0);
			$exclusive = 0;
1271
			$virtexperiment->multiplex_factor(5);
1272 1273
			$virtexperiment->encap_style("vlan");
		    }
1274 1275 1276 1277
		    elsif ($virtualization_subtype eq "emulab-blockstore") {
			$osname = "BLOCKSTORE-VM";
			$pctype = "blockstore";
		    }
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
		    elsif ($virtualization_subtype eq "emulab-connect") {
			$osname = "GENERICDEV-VM";
			$pctype = "interconnect-vm";
			# Lets force to shared node.
			GeniXML::SetExclusive($ref, 0);
			$exclusive = 0;
			# Kludge for libvtop.
			$virtexperiment->multiplex_factor(5);
			$virtexperiment->encap_style("vlan");
		    }
1288 1289
		    elsif ($virtualization_subtype eq "raw"
			   || $virtualization_subtype eq "raw-pc") {
1290 1291 1292
			$pctype = undef;
			goto raw;
		    }
1293 1294 1295 1296
		    elsif ($virtualization_subtype eq "delay") {
			$isbridge = 1;
			$pctype   = undef;
		    }
1297 1298 1299 1300 1301 1302
		    elsif ($virtualization_subtype eq "firewall") {
			$isfirewall = 1;
			$osname     = "FW-IPFW2";
			$pctype     = "pc";
			goto raw;
		    }
1303 1304 1305 1306 1307 1308 1309 1310
		    else {
			$response
			    = GeniResponse->Create(GENIRESPONSE_BADARGS,
						   undef,
				       "Malformed rspec: ".
				       "Unknown virtualization_subtype");
			goto bad;
		    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1311 1312 1313 1314 1315 1316 1317 1318
		}
		else {
		    goto raw;
		}
	    }
	    else {
	      raw:
		# Lets force to exclusive real node.
1319
		if (! GeniXML::SetExclusive($ref, 1)) {
1320 1321
		    $response = GeniResponse->Create(GENIRESPONSE_BADARGS,
						     undef,
1322
			"Malformed rspec: Cannot set exclusive tag to true");
1323 1324 1325
		    goto bad;
		}
		$exclusive = 1;
1326 1327 1328 1329 1330
		my $subtype = "raw-pc";
		if (GeniXML::IsVersion0($ref)) {
		    $subtype = "raw";
		}
		if (! GeniXML::SetVirtualizationSubtype($ref, $subtype)) {
1331 1332
		    $response = GeniResponse->Create(GENIRESPONSE_BADARGS,
						     undef,
1333
			"Malformed rspec: Cannot set virtualization_type to raw");
1334 1335
		    goto bad;
		}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1336 1337 1338 1339
	    }
	}
	else {
	    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
1340
				     "Must provide a virtualization_type");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1341 1342 1343
	    goto bad;

	}
1344 1345 1346 1347 1348 1349
	if (!defined($node_nickname)) {
	    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				     "Must provide a virtual_id for nodes");
	    goto bad;
	}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1350
	#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1351
	# Allow wildcarding.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1352
	#
1353
	if (!defined($component_id) || $component_id eq "*") {
1354 1355 1356 1357 1358
	    if (defined($colocate) && exists($colomap{$colocate})) {
		$node = $colomap{$colocate};
	    }
	    elsif ($isupdate && exists($namemap{$node_nickname})) {
		$node = $namemap{$node_nickname};
1359
	    }
1360 1361
	    # If the node still isn't bound and doesn't have a pctype,
	    # use the user-specified one.
1362 1363 1364 1365 1366 1367 1368 1369 1370
	    if (GeniXML::IsVersion0($ref)) {
		if (! defined($node) && ! defined($pctype)) {
		    my $usertype = GeniXML::FindFirst("n:node_type", $ref);
		    if (defined($usertype)) {
			$pctype = GeniXML::GetText("type_name", $usertype);
		    }
		}
	    } else {
		my $usertype = GeniXML::FindFirst("n:hardware_type", $ref);
1371
		if (defined($usertype)) {
1372 1373 1374 1375 1376 1377
		    #
		    # Watch for pcvm type set above. If the user specified
		    # a hardware type for their VMs, then form a proper
		    # hardware specific pcvm type. 
		    #
		    my $pt = GeniXML::GetText("name", $usertype);
Leigh B Stoller's avatar
Leigh B Stoller committed
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
		    if (!defined($pt)) {
			$response =
			    GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
						 "Bad hardware_type");
			goto bad;
		    }
		    if (defined($pctype) && $pctype eq "pcvm") {
			$pctype = "${pt}-vm";
		    }
		    else {
			$pctype = $pt;
		    }
1390 1391
		}
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1392 1393
	}
	else {
1394
	    $node = GeniUtil::LookupNode($vnode_id);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1395 1396 1397 1398

	    if (!defined($node)) {
		$response =
		    GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
1399
					 "Bad resource $component_id");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1400 1401
		goto bad;
	    }
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
	    #
	    # Is the node a virtual node? Must be an update to an
	    # existing sliver/ticket, since we now return the node_id
	    # of the allocated virtual node, not the physical node.
	    #
	    if ($node->isvirtnode()) {
		if (!$isupdate ||
		    !exists($namemap{$node_nickname})) {
		    $response =
			GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				     "Bad resource for $node_nickname");
		    goto bad;
		}
		$node = $namemap{$node_nickname};
	    }
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
	    #
	    # Watch for pcvm type set above. If the user specified
	    # a hardware type for their VMs, then form a proper
	    # hardware specific pcvm type.
	    #
	    if (defined($pctype) && $pctype eq "pcvm" &&
		(my $usertype =
		 GeniXML::FindFirst("n:hardware_type", $ref))) {
		my $htype = GeniXML::GetText("name", $usertype);
		if (defined($htype)) {
		    $pctype = "${htype}-vm";
		}
	    }
1430 1431
	    $pctype = $node->type()
		if (!defined($pctype));
1432
	}
1433

1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
	#
	# Check to see if a XEN pcvm is bound to another node in the
	# virtual topology (not a testbed physical node). This becomes
	# the "fixed" slot in the virt_nodes table, even on an update
	# (we bound $node above) cause this is how libvtop wants it.
	#
	if (defined($instantiate_on)) {
	    if (defined($virtualization_subtype) &&
		$virtualization_subtype eq "emulab-xen") {

		if (!exists($allnodes{$instantiate_on})) {
		    $response =
			GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
			     "Bad binding of $node_nickname to $subnode_of");
		    goto bad;
		}
	    }
	    else {
		$response =
		    GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
			     "Only Xen nodes can use 'instantiate_on'");
		goto bad;
	    }
	}
1458
	#
1459
	# If no osname, check for protogeni default osname
1460
	#
1461
	if (! defined($osname) && !GeniXML::UseTypeDefaultImage($ref)) {
1462
	    my $protogeni_os = undef;
1463
	    if (GetSiteVar('protogeni/default_osname', \$protogeni_os)
1464 1465
		&& $protogeni_os ne ""
		&& (! defined($pctype) || $pctype ne "bbgeni")) {
1466
		$osname = $protogeni_os;
1467 1468 1469 1470 1471 1472 1473 1474
	    }
	}

	#
	# If no osname by this point, try for the default.
	#
	if (defined($node) && !defined($osname)) {
	    if (defined($node->default_osid())) {
1475
		my $default_osinfo = OSImage->Lookup($node->default_osid());
1476 1477
		$osname = $default_osinfo->osname()
		    if (defined($default_osinfo));
1478
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1479
	}
1480 1481 1482
	# The slot does not like to be NULL.
	$osname = ""
	    if (!defined($osname));
1483
	
1484 1485 1486
	# Need some kind of default.
	$pctype = "pc"
	    if (!defined($pctype));