GeniTicket.pm.in 9.08 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2008 University of Utah and the Flux Group.
# All rights reserved.
#
package GeniTicket;

#
# Some simple ticket stuff.
#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA    = "Exporter";
@EXPORT = qw ( );

# Must come after package declaration!
use lib '@prefix@/lib';
use GeniDB;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
22
use GeniCredential;
23
use GeniCertificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24
use libtestbed;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
25 26
use Experiment;
use libdb qw(TBGetUniqueIndex);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
27
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
28 29
use XML::Simple;
use XML::LibXML;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
30 31 32 33 34 35 36 37 38 39 40
use Data::Dumper;
use File::Temp qw(tempfile);

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
my $SIGNCRED	   = "$TB/sbin/signgenicred";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
my $VERIFYCRED	   = "$TB/sbin/verifygenicred";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42
my $NFREE	   = "$TB/bin/nfree";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
43
my $CMCERT	   = "$TB/etc/genicm.pem";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
44 45

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
46
# Create an unsigned ticket object, to be populated and signed and returned.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
47
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
48
sub Create($$$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
49
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50
    my ($class, $slice, $owner, $rspec) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
51 52

    my $self = {};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53
    $self->{'rspec'}         = $rspec;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54 55 56 57 58
    $self->{'slice_uuid'}    = $slice->uuid();
    $self->{'owner_uuid'}    = $owner->uuid();
    $self->{'slice_cert'}    = $slice->cert();
    $self->{'owner_cert'}    = $owner->cert();
    $self->{'ticket_string'} = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
59
    $self->{'idx'}           = undef;	# Only set when in DB.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
60
    $self->{'component'}     = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61 62 63 64 65 66
    bless($self, $class);

    return $self;
}
# accessors
sub field($$)           { return ($_[0]->{$_[1]}); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
67
sub idx($)		{ return field($_[0], "idx"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
68
sub rspec($)		{ return field($_[0], "rspec"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
69 70 71
sub uuid($)		{ return field($_[0], "slice_uuid"); }
sub slice_uuid($)	{ return field($_[0], "slice_uuid"); }
sub owner_uuid($)	{ return field($_[0], "owner_uuid"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
72 73
sub slice_cert($)	{ return field($_[0], "slice_cert"); }
sub owner_cert($)	{ return field($_[0], "owner_cert"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
74
sub ticket($)		{ return field($_[0], "ticket"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
75
sub asString($)		{ return field($_[0], "ticket_string"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
76
sub ticket_string($)	{ return field($_[0], "ticket_string"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
77 78 79 80 81
sub component($)	{ return field($_[0], "component"); }

#
# Create a ticket object from a signed ticket string.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
82
sub CreateFromSignedTicket($$;$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
83
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
84
    my ($class, $ticket_string, $component, $nosig) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
85

Leigh B. Stoller's avatar
Leigh B. Stoller committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    #
    # This flag is used to avoid verifying the signature since I do not
    # really care if the component gives me a bad ticket; I am not using
    # it locally, just passing it back to the component at some point.
    #
    $nosig = 0
	if (!defined($nosig));

    if (! $nosig) {
	my ($fh, $filename) = tempfile(UNLINK => 0);
	return undef
	    if (!defined($fh));
	print $fh $ticket_string;
	close($fh);
	system("$VERIFYCRED $filename");
	if ($?) {
	    print STDERR "Ticket in $filename did not verify\n";
	    return undef;
	}
	unlink($filename);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
106 107
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
108 109 110 111 112 113 114 115
    # Use XML::Simple to convert to something we can mess with.
    my $parser = XML::LibXML->new;
    my $doc    = $parser->parse_string($ticket_string);

    # Dig out the rspec.
    my ($rspec_node) = $doc->getElementsByTagName("rspec");
    return undef
	if (!defined($rspec_node));
116 117
    my $rspec = XMLin($rspec_node->toString(), ForceArray => ["node",
							      "link"]);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
118 119 120 121 122 123

    # Dig out the slice uuid. Locally, I am not sure if we bother to
    # keep slices in the DB (they are in the DB at geni central).
    my ($uuid_node) = $doc->getElementsByTagName("this_uuid");
    return undef
	if (!defined($uuid_node));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
124 125
    my $slice_cert = $uuid_node->to_literal();
    my $slice_uuid;
126
    GeniCertificate->CertificateInfo($slice_cert, \$slice_uuid) == 0
Leigh B. Stoller's avatar
Leigh B. Stoller committed
127
	or return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
128 129 130 131 132 133 134 135 136 137 138

    if (! ($slice_uuid =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/)) {
	print STDERR "Invalid slice_uuid in ticket\n";
	return undef;
    }

    # Dig out the owner uuid. Locally, I am not sure if we bother to
    # keep users in the DB (they are in the DB at geni central).
    ($uuid_node) = $doc->getElementsByTagName("owner_uuid");
    return undef
	if (!defined($uuid_node));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
139 140
    my $owner_cert = $uuid_node->to_literal();
    my $owner_uuid;
141
    GeniCertificate->CertificateInfo($owner_cert, \$owner_uuid) == 0
Leigh B. Stoller's avatar
Leigh B. Stoller committed
142
	or return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
143 144 145 146 147 148

    if (! ($owner_uuid =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/)) {
	print STDERR "Invalid owner_uuid in ticket\n";
	return undef;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
149 150 151 152
    #
    # We save copies of the tickets we hand out; lets find that record
    # in the DB, just to verify.
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    my $serial;

    if (!$nosig) {
	my ($serial_node) = $doc->getElementsByTagName("serial");
	return undef
	    if (!defined($serial_node));
	$serial = $serial_node->to_literal();

	if (! ($serial =~ /^\w+$/)) {
	    print STDERR "Invalid serial in ticket\n";
	    return undef;
	}
	my $query_result =
	    DBQueryWarn("select * from geni_tickets where idx='$serial'");
	if (!$query_result || !$query_result->numrows) {
	    print STDERR "Could not find the ticket in te DB\n";
	    return undef;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
171 172
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
173 174 175 176
    my $self = {};
    $self->{'rspec'}         = $rspec;
    $self->{'slice_uuid'}    = $slice_uuid;
    $self->{'owner_uuid'}    = $owner_uuid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
177 178
    $self->{'slice_cert'}    = $slice_cert;
    $self->{'owner_cert'}    = $owner_cert;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
179 180
    $self->{'ticket_string'} = $ticket_string;
    $self->{'xmlref'}        = $doc;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
181
    $self->{'component'}     = $component;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
182
    $self->{'idx'}           = $serial;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
183 184 185 186
    bless($self, $class);

    return $self;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
187

Leigh B. Stoller's avatar
Leigh B. Stoller committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
#
# Might have to delete this fron the DB, as with an error handing out a ticket.
#
sub Delete($)
{
    my ($self) = @_;

    return -1
	if (! ref($self));

    if (defined($self->idx())) {
	my $idx = $self->idx();
	
	DBQueryWarn("delete from geni_tickets where idx='$idx'")
	    or return -1;
    }
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
207 208 209 210 211 212 213 214
#
# Populate the ticket with some stuff, which right now is just the
# number of node we are willing to grant.
#
sub Grant($$)
{
    my ($self, $count) = @_;

Leigh B. Stoller's avatar
Leigh B. Stoller committed
215
    return -1
Leigh B. Stoller's avatar
Leigh B. Stoller committed
216 217 218 219 220 221
	if (! ref($self));

    $self->{'count'} = $count;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
222
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
223 224 225
# Store the given ticket in the DB. We only do this for signed tickets,
# so we have a record of them. We store them on the server and the client
# side.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
#
sub Store($$)
{
    my ($self, $idx) = @_;
    my @insert_data  = ();

    my $slice_uuid = $self->slice_uuid();
    my $owner_uuid = $self->owner_uuid();

    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "idx='$idx'");
    push(@insert_data, "slice_uuid='$slice_uuid'");
    push(@insert_data, "owner_uuid='$owner_uuid'");
    
    my $safe_ticket = DBQuoteSpecial($self->ticket_string());
    push(@insert_data, "ticket_string=$safe_ticket");

    # Insert into DB.
    DBQueryWarn("insert into geni_tickets set " . join(",", @insert_data))
	or return -1;

    # If sucessfully stored, set the idx field so we know.
    $self->{'idx'} = $idx;

    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
254 255 256 257 258 259 260 261 262 263
#
# Sign the ticket before returning it. We capture the output, which is
# in XML.
#
sub Sign($)
{
    my ($self) = @_;

    return -1
	if (!ref($self));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
264 265 266 267

    # Every Ticket gets a new unique index, which is used in the xml:id below.
    # Also used for the DB insertion below.
    my $idx = TBGetUniqueIndex('next_ticket', 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
268
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
269 270
    my $slice_cert = $self->slice_cert();
    my $owner_cert = $self->owner_cert();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
271 272
    my $rspec_xml  = XMLout($self->rspec(), "NoAttr" => 1);
    $rspec_xml =~ s/opt\>/rspec\>/g;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
273 274 275 276 277 278

    #
    # Create a template xml file to sign.
    #
    my $template =
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
279
	"<credential xml:id=\"ref1\">\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
280
	" <type>ticket</type>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
281
	" <serial>$idx</serial>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
282 283
	" <owner_uuid>$owner_cert</owner_uuid>\n".
	" <this_uuid>$slice_cert</this_uuid>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
284 285
	" <ticket>\n".
	"  <can_delegate>1</can_delegate>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
286
	"  $rspec_xml\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300
	" </ticket>\n".	
        "</credential>\n";

    my ($fh, $filename) = tempfile(UNLINK => 0);
    return -1
	if (!defined($fh));

    print $fh $template;
    close($fh);

    #
    # Fire up the signer and capture the output. This is the signed ticket
    # that is returned. 
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
301
    if (! open(SIGNER, "$SIGNCRED -c $CMCERT $filename |")) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
302 303 304 305 306 307 308
	print STDERR "Could not sign $filename\n";
	return -1;
    }
    my $ticket = "";
    while (<SIGNER>) {
	$ticket .= $_;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
309 310 311 312
    if (!close(SIGNER)) {
	print STDERR "Could not sign $filename\n";
	return -1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
313
    $self->{'ticket_string'} = $ticket;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
314

Leigh B. Stoller's avatar
Leigh B. Stoller committed
315 316 317
    $self->Store($idx) == 0
	or return -1;

Leigh B. Stoller's avatar
Leigh B. Stoller committed
318
    unlink($filename);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    return 0;
}

#
# Get the experiment for the slice this sliver belongs to.
#
sub GetExperiment($)
{
    my ($self) = @_;

    return undef
	if (! ref($self));

    return Experiment->Lookup($self->slice_uuid());
}

#
# Release a ticket. Need to release the nodes ...
#
sub Release($)
{
    my ($self) = @_;

    return undef
	if (! ref($self));

    my $experiment = Experiment->Lookup($self->slice_uuid());
    my $node_id    = $self->rspec()->{'node_id'};
    my $node       = Node->Lookup($node_id);
    return -1
	if (!defined($node));
    my $reservation = Node->Reservation();
    
    if (defined($reservation) && $reservation->SameExperiment($experiment)) {
	my $pid = $experiment->pid();
	my $eid = $experiment->eid();
	
	system("$NFREE $pid $eid $node_id");
    }
    $self->Delete();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
359 360 361
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
362 363
# _Always_ make sure that this 1 is at the end of the file...
1;