GeniCM.pm.in 238 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. 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 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 ceil);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
76
use Time::Local;
77
use Compress::Zlib;
78
use File::Temp qw(tempfile tmpnam);
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
use Errno qw(:POSIX);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
83
84
85
86
87
88

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

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

139
140
141
142
143
144
145
146
147
148
#
# 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
149
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
150
# Respond to a Resolve request. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
151
152
153
154
155
156
#
sub Resolve($)
{
    my ($argref) = @_;
    my $uuid       = $argref->{'uuid'};
    my $cred       = $argref->{'credential'};
157
158
    my $type       = lc( $argref->{'type'} );
    my $hrn        = $argref->{'hrn'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
159
160
161
162

    if (! defined($cred)) {
	return GeniResponse->MalformedArgsResponse();
    }
163
164
165
166
    if (defined($uuid) && GeniHRN::IsValid($uuid)) {
	$hrn  = $uuid;
	$uuid = undef;
    }
167
168
169
170
171
172
173
174
175
176
177
178
    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
179
180
181
	return GeniResponse->MalformedArgsResponse();
    }
    # Allow lookup by uuid or hrn.
182
    if (! defined($uuid) && !defined( $hrn ) ) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
183
184
185
186
187
188
	return GeniResponse->MalformedArgsResponse();
    }
    if (defined($uuid) && !($uuid =~ /^[-\w]*$/)) {
	return GeniResponse->MalformedArgsResponse();
    }

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

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

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

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

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

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

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

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

268
269
270
    # 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.
271
272
273
274
275
276
    if (defined($available) && ref($available) eq 'Frontier::RPC2::Boolean') {
	$available = $available->value;
    }
    if (defined($compress) && ref($compress) eq 'Frontier::RPC2::Boolean') {
	$compress = $compress->value;
    }
277

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

    # 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) {
293
        if (GeniXML::PolicyExists('allow_externalusers', $credential) == 1) {
294
295
296
297
298
299
        $isExempted = 1;
        last;
      }
    }

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

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
    #
    # 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
327
    #
328
    # Acquire the advertisement from ptopgen and compress it if requested.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
329
    #
330
    my $xml = GetAdvertisement($available, undef, $version, $experiment);
331
    if (! defined($xml)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
332
333
334
335
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not start avail");
    }

336
337
338
339
340
341
    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
342
343
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $xml);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
344

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

355
356
357
358
359
360
361
    $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");

362
    my $invocation = "$PTOPGEN -x -g $version -r -f";
363
364
    if (defined($experiment)) {
	my $eid = $experiment->eid();
365
	my $pid = $experiment->pid();
366
	$invocation .= " -p $pid -e $eid";
367
    }
368
369
370
371
    $invocation .= " -a" unless $available;
    if (defined($pc)) {
	$invocation .= " -1 $pc";
    }
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
410
    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;
	}
    }
411
    if (open(AVAIL, "$invocation |")) {
412
	$xml = "";
413
414
415
416
417
	while (<AVAIL>) {
	    $xml .= $_;
	}
	close(AVAIL);
    }
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
    #
    # 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();
    }
439
440
441
    return $xml;
}

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

    return GetTicket($argref, 1);
}

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

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

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

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

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

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

561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
    #
    # 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");
    }

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

585
586
587
588
589
590
    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
591
592
593
594
    #
    # A sitevar controls whether external users can get any nodes.
    #
    my $allow_externalusers = 0;
595
    if (!GetSiteVar('protogeni/allow_externalusers', \$allow_externalusers)){
596
597
598
599
	    # Cannot get the value, say no.
	    $allow_externalusers = 0;
    }

600
601
602
603
604
605
606
607
608
609
610
    # 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);

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

625
626
627
628
629
630
631
632
633
634
    # 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
635
    }
636
637
638

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

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

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

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

    #
    # Mark the experiment locally as coming from the cooked interface.
    # This changes what tmcd returns to the local nodes.
    #
763
    my $generated_by = GeniXML::GetText("generated_by", $rspec);
764
765
    if (defined($generated_by) &&
	$generated_by eq "libvtop") {
766
767
768
769
	$slice_experiment->Update({"geniflags" =>
				       $Experiment::EXPT_GENIFLAGS_EXPT|
				       $Experiment::EXPT_GENIFLAGS_COOKED});
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
770
771
772
773
774
775
776
777
778
779
780
781
782
    
    #
    # 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);
783
784
785
786

    #
    # Allow user to control the multiplex factor. Note that ptopgen
    # will not allow the mfactor to be more then what we set as the
787
788
789
790
791
792
793
794
795
796
797
798
799
800
    # max for the node, but in general we set it low here. Also note
    # that to support Emulab Classic NS file conversion, zero means to
    # not change it.
    #
    my $mfactor = GeniXML::MultiplexFactor($rspec);
    if (defined($mfactor)) {
	if ($mfactor != 0) {
	    $virtexperiment->multiplex_factor($mfactor);
	}
    }
    else {
	# Odd geni default dating back to the beginning of time.
	$virtexperiment->multiplex_factor(10);
    }
801
802
803
804
805
    #
    # The packing strategy is sorta independent; the user can specify either
    # pack or balance (load balance).
    #
    my $packing_option = GeniXML::PackingStrategy($rspec);
806

807
    #
808
    # User can turn off routing.
809
    #
810
811
812
813
814
815
816
817
818
819
820
821
    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;
    }
822
    
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
    #
    # 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);
    }
849

850
851
852
853
854
855
856
857
858
859
    #
    # 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()) {
860
	$virtexperiment->NewTableRow("virt_vtypes",
861
862
863
864
				     {"name"     => $row->{'vtype'},
				      "members"  => $row->{'types'},
				      "weight"   => $row->{'weight'}
				     });
865
    }
866

867
868
869
870
871
    # Need to move this someplace else; the parser adds a bunch.
    $virtexperiment->NewTableRow("virt_agents",
				 {"vnode"      => "*",
				  "vname"      => "ns",
				  "objecttype" => "6"});
872
873
874
875
    $virtexperiment->NewTableRow("virt_agents",
				 {"vnode"      => "*",
				  "vname"      => "linktest",
				  "objecttype" => "7"});
876

877
878
879
    #
    # Look for toplevel address pools
    #
880
881
882
883
884
885
886
    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") {
887
		    $response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
888
889
890
891
892
		     "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",
893
894
895
896
					 {"pool_id" => $pool->{'client_id'},
					  "count" => $pool->{'count'},
					  "restriction" => $pool->{'type'},
					  "version" => "ipv4" });
897
	    }
898
899
900
	}
    }

901
902
903
904
905
906
907
908
909
910
911
912
    #
    # 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);
	}
913
    }
914
915
916
917
    #
    # Note that we set the mounts to "genidefault" when we create the
    # container experiment. So this overrides.
    #
918
919
    if ($PROTOGENI_NONFSMOUNTS) {
	$virtexperiment->nonfsmounts(1);
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
	# 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");
	}
937
938
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
939
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
940
941
    # 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
942
943
    # cannot be granted.
    #
944
945
946
    my %namemap  = ();
    my %colomap  = ();
    my %ifacemap = ();
Jonathon Duerig's avatar
Jonathon Duerig committed
947
    my %iface2node = ();
948
    my %vportmap = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
949
    my %nodemap  = ();
950
    my %bridgemap= ();
951
    my @nodeids  = ();
952
    my %lannodes = ();
953
    my %allnodes = ();
954
955
    # Extra nodes (like XEN vhosts).
    my %internal_nodemap  = ();
956
    # For stitching, keep track of external nodes and links.
957
958
959
    my %external_nodemap  = ();
    my %external_linkmap  = ();
    my %external_vportmap = ();
960
    my %external_lanrefs  = ();
Leigh B Stoller's avatar
Leigh B Stoller committed
961
    my $routable_ip_count = 0;
962
    my $sync_server = ($isupdate ? $slice_experiment->sync_server() : undef);
963

964
965
    # Always do this to avoid buildup.
    $slice_experiment->ClearBackupState();
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990

    #
    # 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;
    }
991
    
992
993
994
995
996
997
998
    #
    # 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
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
	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;
	}
1016
1017
1018
1019
1020
1021
1022
	my $oldrspec;
	if ($v2 && defined($aggregate)) {
	    $oldrspec = $aggregate->GetManifest(0);
	}
	else {
	    $oldrspec = $ticket->rspec();
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1023
	
1024
	foreach my $ref (GeniXML::FindNodes("n:node",
1025
					    $oldrspec)->get_nodelist()) {
1026
	    # Let remote nodes pass through.
1027
	    next
1028
		if (!GeniXML::IsLocalNode($ref));
1029

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

1034
	    my $node_nickname = GeniXML::GetVirtualId($ref);
Jonathon Duerig's avatar
Jonathon Duerig committed
1035
	    my $colocate      = GeniXML::GetColocate($ref);
1036
	    my $component_id  = GeniXML::GetNodeId($ref);
1037
1038
	    my $vnode_id      = GeniXML::GetVnodeId($ref);
	    my $node = GeniUtil::LookupNode($vnode_id);
1039
1040
	    if (!defined($node)) {
		$response = GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
1041
				 "Bad resource $component_id in ticket");
1042
1043
		goto bad;
	    }
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
	    #
	    # 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;
	    }
1059
1060
1061
1062
1063
	    $namemap{$node_nickname} = $node;
	    $colomap{$colocate} = $node
		if (defined($colocate));
	}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1064

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

1067
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
1068
	my $component_id  = GeniXML::GetNodeId($ref);
1069
	my $vnode_id      = GeniXML::GetVnodeId($ref);
1070
	my $manager_id    = GeniXML::GetManagerId($ref);
1071
	my $node_nickname = GeniXML::GetVirtualId($ref);
Jonathon Duerig's avatar
Jonathon Duerig committed
1072
1073
	my $colocate      = GeniXML::GetColocate($ref);
	my $subnode_of    = GeniXML::GetSubnodeOf($ref);
1074
	my $instantiate_on= GeniXML::GetInstantiateOn($ref);
Jonathon Duerig's avatar
Jonathon Duerig committed
1075
1076
	my $virtualization_type = GeniXML::GetVirtualizationType($ref);
	
1077
	my $virtualization_subtype
Jonathon Duerig's avatar
Jonathon Duerig committed
1078
1079
                          = GeniXML::GetVirtualizationSubtype($ref);
	my $exclusive     = GeniXML::GetExclusive($ref);
1080
	my $tarfiles      = GeniXML::GetTarball($ref);
1081
	my $pctype;
1082
	my ($osname, $osinfo);
1083
	my $parent_osname;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1084
	my $node;
1085
1086
	my $isbridge    = 0;
	my $isfirewall  = 0;
Leigh B Stoller's avatar
Leigh B Stoller committed
1087
	my $xensettings;
1088
	my $fwsettings;
1089
	
1090
1091
1092
1093
1094
	# 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);
1095
1096
1097
1098
1099
1100
1101
	    if (exists($iface2node{$virtual_id})) {
		$response =
		    GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Duplicate interface $virtual_id on ".
					 "node $node_nickname");
		goto bad;
	    }
1102
1103
1104
	    $iface2node{$virtual_id} = $node_nickname;
	}

1105
	# Let remote nodes pass through.
1106
1107
1108
1109
	if (! GeniXML::IsLocalNode($ref)) {
	    $external_nodemap{$node_nickname} = $ref;
	    next;
	}
1110

1111
	#
1112
1113
1114
1115
1116
	#
	# 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. 
	#
1117
	if (GeniXML::IsLanNode($ref)) {
1118
1119
1120
1121
	    $lannodes{$node_nickname} = $ref;
	    next;
	}

1122
1123
1124
1125
1126
1127
1128
1129
	#
	# 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}";
1130
	if (0 && length($fullhostname) > 63) {
1131
1132
1133
1134
	    $response = GeniResponse->Create(GENIRESPONSE_TOOBIG, undef,
				     "Hostname > 63 characters: $fullhostname");
	    goto bad;
	}
1135
1136
1137
	#
	# Check for disk_image request. Specified as a URN. 
	#
1138
	my $diskref = GeniXML::GetDiskImage($ref);
1139
1140
	if (defined($diskref)) {
	    my $dname = GeniXML::GetText("name", $diskref);
1141
	    my $url   = GeniXML::GetText("url", $diskref);
1142

1143
1144
1145
1146
1147
	    # url is deprecated; name can be anything.
	    if (defined($dname) && $dname =~ /^http/) {
		$url = $dname;
	    }

1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
	    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)) {
1163
1164
1165
1166
1167
1168
1169
		if (! GeniHRN::IsValid($dname)) {
		    $response =
			GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Malformed image URN: $dname");
		    goto bad;
		}
		my ($auth,$type,$id) = GeniHRN::Parse($dname);
1170
1171
		my ($ospid,$os,undef,$vers) =
		    ($id =~ m{([^/]+)//([^/]+)(//(\d+))?});		
1172
1173
1174
1175
1176
1177
		if ($type ne "image" || !defined($ospid) || !defined($os)){
		    $response =
			GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					 "Malformed image URN: $dname");
		    goto bad;
		}
1178
		$osinfo = OSImage->Lookup($ospid, $os, $vers);
1179
		if (!defined($osinfo)) {
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
		    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;
		    }
1196
		}
1197
1198
1199
		else {
		    #
		    # The OS must be in the current project, or it must
1200
		    # be global.
1201
		    #
1202
		    if (! ($osinfo->global() ||
1203
			   $osinfo->pid() eq $slice_experiment->pid())) {
1204
			if ($PROTOGENI_LOCALUSER && $user->IsLocal()) {
1205
			    my $image = OSImage->Lookup($ospid,$os,$vers);
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
			    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;			
			}
1224
1225
1226
1227
1228
1229
1230
		    }
		    #
		    # This is only going to be used in raw mode. 
		    #
		    $osname  = "$ospid/$os";
		    $osname .= ":${vers}" if (defined($vers));
		}
1231
1232
1233
	    }
	}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1234
1235
1236
	if (defined($virtualization_type)) {
	    if ($virtualization_type eq "emulab-vnode") {
		if (defined($virtualization_subtype)) {
1237
1238
		    $pctype = "pcvm";
		    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1239
1240
1241
1242
		    if ($virtualization_subtype eq "emulab-jail") {
			$osname = "FBSD-JAIL";
		    }
		    elsif ($virtualization_subtype eq "emulab-openvz") {
1243
1244
1245
1246
1247
1248
1249
1250
			# 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";
			    }
			}
1251
1252
			elsif (!defined($osname)) {
			    # Allow for url above.
1253
1254
			    $osname = "OPENVZ-STD";
			}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1255
		    }
1256
1257
		    elsif ($virtualization_subtype eq "emulab-xen" ||
			   $virtualization_subtype eq "default-vm") {
1258
1259
1260
1261
1262
			# Allow caller to set the image to use, but also
			# trick to set the parent. 
			if (defined($osinfo)) {
			    if (! $osinfo->IsSubOS()) {
				$parent_osname = $osname;
1263
				$osname = "UBUNTU14-64-STD";
1264
1265
			    }
			}
1266
			# If not set, we will pick up the default_image below.