GeniCM.pm.in 232 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2008-2017 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
42 43 44

use GeniDB;
use Genixmlrpc;
Leigh Stoller's avatar
Leigh Stoller committed
45 46
use GeniResponse;
use GeniTicket;
Leigh Stoller's avatar
Leigh Stoller committed
47
use GeniCredential;
48
use GeniCertificate;
Leigh Stoller's avatar
Leigh Stoller committed
49
use GeniSlice;
Leigh Stoller's avatar
Leigh Stoller committed
50
use GeniAggregate;
51
use GeniAuthority;
Leigh Stoller's avatar
Leigh Stoller committed
52
use GeniSliver;
Leigh Stoller's avatar
Leigh Stoller committed
53
use GeniUser;
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
70 71
use English;
use Data::Dumper;
Leigh Stoller's avatar
Leigh Stoller committed
72
use XML::Simple;
73
use XML::LibXML;
Leigh Stoller's avatar
Leigh Stoller committed
74
use Date::Parse;
75
use POSIX qw(strftime tmpnam ceil);
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
96
my $CREATEEXPT     = "$TB/bin/batchexp";
97
my $ENDEXP         = "$TB/bin/endexp";
98
my $NFREE	   = "$TB/bin/nfree";
Leigh Stoller's avatar
Leigh Stoller committed
99
my $AVAIL	   = "$TB/sbin/avail";
100
my $PTOPGEN	   = "$TB/libexec/ptopgen";
Leigh Stoller's avatar
Leigh Stoller committed
101 102
my $TBSWAP	   = "$TB/bin/tbswap";
my $SWAPEXP	   = "$TB/bin/swapexp";
Leigh Stoller's avatar
Leigh Stoller committed
103 104
my $PLABSLICE	   = "$TB/sbin/plabslicewrapper";
my $NAMEDSETUP     = "$TB/sbin/named_setup";
105
my $EXPORTS_SETUP  = "$TB/sbin/exports_setup";
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
148
#
Leigh Stoller's avatar
Leigh Stoller committed
149
# Respond to a Resolve request. 
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
178 179 180
	return GeniResponse->MalformedArgsResponse();
    }
    # Allow lookup by uuid or hrn.
181
    if (! defined($uuid) && !defined( $hrn ) ) {
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
194 195 196
	my $node;
	
	if (defined($uuid)) {
197
	    $node= GeniUtil::LookupNode($uuid);
Leigh Stoller's avatar
Leigh Stoller committed
198 199
	}
	else {
200
	    $node= GeniUtil::LookupNode($hrn);
Leigh Stoller's avatar
Leigh Stoller committed
201
	}
202
	if (! defined($node)) {
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
212 213
	
	# Return a blob.
214
	my $blob = { "hrn"          => "${PGENIDOMAIN}." . $node->node_id(),
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
225 226 227 228 229 230 231
		   };

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

Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
326
    #
327
    # Acquire the advertisement from ptopgen and compress it if requested.
Leigh Stoller's avatar
Leigh Stoller committed
328
    #
329
    my $xml = GetAdvertisement($available, undef, $version, $experiment);
330
    if (! defined($xml)) {
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
341 342
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $xml);
}
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
441
#
442
# Update a ticket with a new rspec.
Leigh Stoller's avatar
Leigh Stoller committed
443
#
444
sub UpdateTicket($)
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
468

469
    if (! defined($credstr)) {
470
	return GeniResponse->MalformedArgsResponse();
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
507
    #
508
    # Create user from the certificate.
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
517
    #
518
    # Create slice from the certificate.
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
634
    }
635 636 637

    if (!$allow_externalusers && !$isExempted && !$user->IsLocal()) {
	    return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
Leigh Stoller's avatar
Leigh Stoller committed
638 639 640 641
				    "External users temporarily denied");
    }
    
    #
642
    # For now all tickets expire very quickly (minutes), but once the
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
648
    #
649
    my $expires = GeniXML::GetExpires($rspec);
650
    if (defined($expires)) {
651 652
	if (GeniResponse::IsResponse($expires)) {
	    return $expires;
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
697

698
    #
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
717
    # We need this now so we can form a virtual topo.
718
    #
719 720 721 722 723 724
    my $slice_experiment = GeniExperiment($slice, $user);
    if (GeniResponse::IsResponse($slice_experiment)) {
	$response = $slice_experiment;
	$slice_experiment = undef;
	goto bad;
    }
725 726 727 728 729 730 731 732 733 734 735
    #
    # Mark the expires slot in the experiment, for admission control
    # during the mapper run. 
    #
    if ($slice_experiment->SetExpiration(defined($expires) ? $expires :
					 $slice->expires())) {
	$response = GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					 "set experiment expiration");
	print STDERR "Could not set experiment expiration\n";
	goto bad;
    }
736 737 738 739 740
    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 Stoller's avatar
Leigh Stoller committed
741 742 743 744
	goto bad;
    }
    my $pid = $slice_experiment->pid();
    my $eid = $slice_experiment->eid();
745 746 747 748 749

    #
    # Mark the experiment locally as coming from the cooked interface.
    # This changes what tmcd returns to the local nodes.
    #
750
    my $generated_by = GeniXML::GetText("generated_by", $rspec);
751 752
    if (defined($generated_by) &&
	$generated_by eq "libvtop") {
753 754 755 756
	$slice_experiment->Update({"geniflags" =>
				       $Experiment::EXPT_GENIFLAGS_EXPT|
				       $Experiment::EXPT_GENIFLAGS_COOKED});
    }
Leigh Stoller's avatar
Leigh Stoller committed
757 758 759 760 761 762 763 764 765 766 767 768 769
    
    #
    # 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);
770 771 772 773 774 775 776

    #
    # 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);
777 778 779 780 781
    #
    # The packing strategy is sorta independent; the user can specify either
    # pack or balance (load balance).
    #
    my $packing_option = GeniXML::PackingStrategy($rspec);
782

783
    #
784
    # User can turn off routing.
785
    #
786 787 788 789 790 791 792 793 794 795 796 797
    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;
    }
798
    
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
    #
    # 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);
    }
825

826 827 828 829 830 831 832 833 834 835
    #
    # 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()) {
836
	$virtexperiment->NewTableRow("virt_vtypes",
837 838 839 840
				     {"name"     => $row->{'vtype'},
				      "members"  => $row->{'types'},
				      "weight"   => $row->{'weight'}
				     });
841
    }
842

843 844 845 846 847
    # Need to move this someplace else; the parser adds a bunch.
    $virtexperiment->NewTableRow("virt_agents",
				 {"vnode"      => "*",
				  "vname"      => "ns",
				  "objecttype" => "6"});
848 849 850 851
    $virtexperiment->NewTableRow("virt_agents",
				 {"vnode"      => "*",
				  "vname"      => "linktest",
				  "objecttype" => "7"});
852

853 854 855
    #
    # Look for toplevel address pools
    #
856 857 858 859 860 861 862
    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") {
863
		    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
864 865 866 867 868
		     "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",
869 870 871 872
					 {"pool_id" => $pool->{'client_id'},
					  "count" => $pool->{'count'},
					  "restriction" => $pool->{'type'},
					  "version" => "ipv4" });
873
	    }
874 875 876
	}
    }

877 878 879 880 881 882 883 884 885 886 887 888
    #
    # 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);
	}
889
    }
890 891 892 893
    #
    # Note that we set the mounts to "genidefault" when we create the
    # container experiment. So this overrides.
    #
894 895
    if ($PROTOGENI_NONFSMOUNTS) {
	$virtexperiment->nonfsmounts(1);
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
	# 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");
	}
913 914
    }

Leigh Stoller's avatar
Leigh Stoller committed
915
    #
Leigh Stoller's avatar
Leigh Stoller committed
916 917
    # An rspec is a structure that requests specific nodes. If those
    # nodes are available, then reserve it. Otherwise the ticket
Leigh Stoller's avatar
Leigh Stoller committed
918 919
    # cannot be granted.
    #
920 921 922
    my %namemap  = ();
    my %colomap  = ();
    my %ifacemap = ();
923
    my %iface2node = ();
924
    my %vportmap = ();
Leigh Stoller's avatar
Leigh Stoller committed
925
    my %nodemap  = ();
926
    my %bridgemap= ();
927
    my @nodeids  = ();
928
    my %lannodes = ();
929
    my %allnodes = ();
930 931
    # Extra nodes (like XEN vhosts).
    my %internal_nodemap  = ();
932
    # For stitching, keep track of external nodes and links.
933 934 935
    my %external_nodemap  = ();
    my %external_linkmap  = ();
    my %external_vportmap = ();
936
    my %external_lanrefs  = ();
Leigh Stoller's avatar
Leigh Stoller committed
937
    my $routable_ip_count = 0;
938
    my $sync_server = ($isupdate ? $slice_experiment->sync_server() : undef);
939

940 941
    # Always do this to avoid buildup.
    $slice_experiment->ClearBackupState();
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966

    #
    # 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;
    }
967
    
968 969 970 971 972 973 974
    #
    # 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 Stoller's avatar
Leigh Stoller committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
	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;
	}
992 993 994 995 996 997 998
	my $oldrspec;
	if ($v2 && defined($aggregate)) {
	    $oldrspec = $aggregate->GetManifest(0);
	}
	else {
	    $oldrspec = $ticket->rspec();
	}
Leigh Stoller's avatar
Leigh Stoller committed
999
	
1000
	foreach my $ref (GeniXML::FindNodes("n:node",
1001
					    $oldrspec)->get_nodelist()) {
1002
	    # Let remote nodes pass through.
1003
	    next
1004
		if (!GeniXML::IsLocalNode($ref));
1005

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

1010
	    my $node_nickname = GeniXML::GetVirtualId($ref);
1011
	    my $colocate      = GeniXML::GetColocate($ref);
1012
	    my $component_id  = GeniXML::GetNodeId($ref);
1013 1014
	    my $vnode_id      = GeniXML::GetVnodeId($ref);
	    my $node = GeniUtil::LookupNode($vnode_id);
1015 1016
	    if (!defined($node)) {
		$response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
1017
				 "Bad resource $component_id in ticket");
1018 1019
		goto bad;
	    }
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	    #
	    # 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;
	    }
1035 1036 1037 1038 1039
	    $namemap{$node_nickname} = $node;
	    $colomap{$colocate} = $node
		if (defined($colocate));
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
1040

1041
    print GeniXML::Serialize($rspec);
Leigh Stoller's avatar
Leigh Stoller committed
1042

1043
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
1044
	my $component_id  = GeniXML::GetNodeId($ref);
1045
	my $vnode_id      = GeniXML::GetVnodeId($ref);
1046
	my $manager_id    = GeniXML::GetManagerId($ref);
1047
	my $node_nickname = GeniXML::GetVirtualId($ref);
1048 1049
	my $colocate      = GeniXML::GetColocate($ref);
	my $subnode_of    = GeniXML::GetSubnodeOf($ref);
1050
	my $instantiate_on= GeniXML::GetInstantiateOn($ref);
1051 1052
	my $virtualization_type = GeniXML::GetVirtualizationType($ref);
	
1053
	my $virtualization_subtype
1054 1055
                          = GeniXML::GetVirtualizationSubtype($ref);
	my $exclusive     = GeniXML::GetExclusive($ref);
1056
	my $tarfiles      = GeniXML::GetTarball($ref);
1057
	my $pctype;
1058
	my ($osname, $osinfo);
1059
	my $parent_osname;
Leigh Stoller's avatar
Leigh Stoller committed
1060
	my $node;
1061 1062
	my $isbridge    = 0;
	my $isfirewall  = 0;
Leigh Stoller's avatar
Leigh Stoller committed
1063
	my $xensettings;
1064
	my $fwsettings;
1065
	
1066 1067 1068 1069 1070
	# 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);
1071 1072 1073 1074 1075 1076 1077
	    if (exists($iface2node{$virtual_id})) {
		$response =
		    GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Duplicate interface $virtual_id on ".
					 "node $node_nickname");
		goto bad;
	    }
1078 1079 1080
	    $iface2node{$virtual_id} = $node_nickname;
	}

1081
	# Let remote nodes pass through.
1082 1083 1084 1085
	if (! GeniXML::IsLocalNode($ref)) {
	    $external_nodemap{$node_nickname} = $ref;
	    next;
	}
1086

1087
	#
1088 1089 1090 1091 1092
	#
	# 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. 
	#
1093
	if (GeniXML::IsLanNode($ref)) {
1094 1095 1096 1097
	    $lannodes{$node_nickname} = $ref;
	    next;
	}

1098 1099 1100 1101 1102 1103 1104 1105
	#
	# 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}";
1106
	if (0 && length($fullhostname) > 63) {
1107 1108 1109 1110
	    $response = GeniResponse->Create(GENIRESPONSE_TOOBIG, undef,
				     "Hostname > 63 characters: $fullhostname");
	    goto bad;
	}
1111 1112 1113
	#
	# Check for disk_image request. Specified as a URN. 
	#
1114
	my $diskref = GeniXML::GetDiskImage($ref);
1115 1116
	if (defined($diskref)) {
	    my $dname = GeniXML::GetText("name", $diskref);
1117
	    my $url   = GeniXML::GetText("url", $diskref);
1118

1119 1120 1121 1122 1123
	    # url is deprecated; name can be anything.
	    if (defined($dname) && $dname =~ /^http/) {
		$url = $dname;
	    }

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	    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)) {
1139 1140 1141 1142 1143 1144 1145
		if (! GeniHRN::IsValid($dname)) {
		    $response =
			GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Malformed image URN: $dname");
		    goto bad;
		}
		my ($auth,$type,$id) = GeniHRN::Parse($dname);
1146 1147
		my ($ospid,$os,undef,$vers) =
		    ($id =~ m{([^/]+)//([^/]+)(//(\d+))?});		
1148 1149 1150 1151 1152 1153
		if ($type ne "image" || !defined($ospid) || !defined($os)){
		    $response =
			GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Malformed image URN: $dname");
		    goto bad;
		}
1154
		$osinfo = OSImage->Lookup($ospid, $os, $vers);
1155
		if (!defined($osinfo)) {
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
		    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;
		    }
1172
		}
1173 1174 1175
		else {
		    #
		    # The OS must be in the current project, or it must
1176
		    # be global.
1177
		    #
1178
		    if (! ($osinfo->global() ||
1179
			   $osinfo->pid() eq $slice_experiment->pid())) {
1180
			if ($PROTOGENI_LOCALUSER && $user->IsLocal()) {
1181
			    my $image = OSImage->Lookup($ospid,$os,$vers);
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
			    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;			
			}
1200 1201 1202 1203 1204 1205 1206
		    }
		    #
		    # This is only going to be used in raw mode. 
		    #
		    $osname  = "$ospid/$os";
		    $osname .= ":${vers}" if (defined($vers));
		}
1207 1208 1209
	    }
	}

Leigh Stoller's avatar
Leigh Stoller committed
1210 1211 1212
	if (defined($virtualization_type)) {
	    if ($virtualization_type eq "emulab-vnode") {
		if (defined($virtualization_subtype)) {
1213 1214
		    $pctype = "pcvm";
		    
Leigh Stoller's avatar
Leigh Stoller committed
1215 1216 1217 1218
		    if ($virtualization_subtype eq "emulab-jail") {
			$osname = "FBSD-JAIL";
		    }
		    elsif ($virtualization_subtype eq "emulab-openvz") {
1219 1220 1221 1222 1223 1224 1225 1226
			# 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";
			    }
			}
1227 1228
			elsif (!defined($osname)) {
			    # Allow for url above.
1229 1230
			    $osname = "OPENVZ-STD";
			}
Leigh Stoller's avatar
Leigh Stoller committed
1231
		    }
1232 1233
		    elsif ($virtualization_subtype eq "emulab-xen" ||
			   $virtualization_subtype eq "default-vm") {
1234 1235 1236 1237 1238
			# Allow caller to set the image to use, but also
			# trick to set the parent. 
			if (defined($osinfo)) {
			    if (! $osinfo->IsSubOS()) {
				$parent_osname = $osname;
1239
				$osname = "UBUNTU14-64-STD";
1240 1241
			    }
			}
1242
			# If not set, we will pick up the default_image below.
Leigh Stoller's avatar
Leigh Stoller committed
1243 1244 1245 1246 1247 1248
			#
			# Look for the knobs
			#
			if (GeniXML::HasXenSettings($ref)) {
			    $xensettings = GeniXML::GetXenSettings($ref);
			}
1249 1250 1251
			my $ptype = GeniXML::XenPtype($ref);
			$pctype = $ptype
			    if (defined($ptype));
1252
			$virtexperiment->encap_style("vlan");
1253 1254 1255 1256 1257 1258 1259

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

	}
1357 1358 1359 1360 1361 1362
	if (!defined($node_nickname)) {
	    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				     "Must provide a virtual_id for nodes");
	    goto bad;
	}

Leigh Stoller's avatar
Leigh Stoller committed
1363
	#
Leigh Stoller's avatar
Leigh Stoller committed
1364
	# Allow wildcarding.
Leigh Stoller's avatar
Leigh Stoller committed
1365
	#
1366
	if (!defined($component_id) || $component_id eq "*") {
1367 1368 1369 1370 1371
	    if (defined($colocate) && exists($colomap{$colocate})) {
		$node = $colomap{$colocate};
	    }
	    elsif ($isupdate && exists($namemap{$node_nickname})) {
		$node = $namemap{$node_nickname};
1372
	    }
1373 1374
	    # If the node still isn't bound and doesn't have a pctype,
	    # use the user-specified one.
1375 1376 1377 1378 1379 1380 1381 1382 1383
	    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);
1384
		if (defined($usertype)) {
1385 1386 1387 1388 1389 1390
		    #
		    # 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 Stoller's avatar
Leigh Stoller committed
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
		    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;
		    }
1403 1404
		}
	    }
Leigh Stoller's avatar
Leigh Stoller committed
1405 1406
	}
	else {
1407
	    $node = GeniUtil::LookupNode($vnode_id);
Leigh Stoller's avatar
Leigh Stoller committed
1408 1409 1410 1411

	    if (!defined($node)) {
		$response =
		    GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
1412
					 "Bad resource $component_id");
Leigh Stoller's avatar
Leigh Stoller committed
1413 1414
		goto bad;
	    }
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
	    #
	    # 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};
	    }
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
	    #
	    # 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";
		}
	    }
1443 1444
	    $pctype = $node->type()
		if (!defined($pctype));
1445
	}
1446

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
	#
	# 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;
	    }
	}
1471
	#
1472
	# If no osname, check for protogeni default osname
1473
	#
1474 1475
	if (!defined($osname) &&
	    !GeniXML::UseTypeDefaultImage($ref) && !$isbridge) {
1476
	    my $protogeni_os = undef;
1477
	    if (GetSiteVar('protogeni/default_osname', \$protogeni_os)
1478 1479
		&& $protogeni_os ne ""
		&& (! defined($pctype) || $pctype ne "bbgeni")) {
1480
		$osname = $protogeni_os;
1481 1482 1483 1484 1485 1486
	    }
	}

	#
	# If no osname by this point, try for the default.
	#
1487
	if (defined($node) && !defined($osname) && !$isbridge) {
1488
	    if (defined($node->default_osid())) {
1489
		my $default_osinfo = OSImage->Lookup($node->default_osid());
1490 1491
		$osname = $default_osinfo->osname()
		    if (defined($default_osinfo));
1492
	    }
Leigh Stoller's avatar
Leigh Stoller committed
1493
	}
1494 1495 1496
	# The slot does not like to be NULL.
	$osname = ""
	    if (!defined($osname));
1497
	
1498 1499 1500
	# Need some kind of default.
	$pctype = "pc"
	    if (!defined($pctype));
1501
	
1502 1503 1504 1505 1506
	my $nodeblob = {"vname"   => $node_nickname,
			"type"    => $pctype,
			"osname"  => $osname,
			"ips"     => '', # deprecated
			"cmd_line"=> '', # bogus
1507
			"routertype" => $routertype,
1508 1509 1510 1511
			"fixed"   =>
			    (defined($subnode_of) ? $subnode_of :
			     defined($instantiate_on) ? $instantiate_on :
			     defined($node) ? $node->node_id() : ""),