GeniTicket.pm.in 9.03 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;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23
use libtestbed;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24 25
use Experiment;
use libdb qw(TBGetUniqueIndex);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
27 28
use XML::Simple;
use XML::LibXML;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29 30 31 32 33 34 35 36 37 38 39
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
40
my $VERIFYCRED	   = "$TB/sbin/verifygenicred";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
my $NFREE	   = "$TB/bin/nfree";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42
my $CMCERT	   = "$TB/etc/genicm.pem";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
43 44

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

    my $self = {};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
52
    $self->{'rspec'}         = $rspec;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53 54 55 56 57
    $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
58
    $self->{'idx'}           = undef;	# Only set when in DB.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
59
    $self->{'component'}     = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
60 61 62 63 64 65
    bless($self, $class);

    return $self;
}
# accessors
sub field($$)           { return ($_[0]->{$_[1]}); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66
sub idx($)		{ return field($_[0], "idx"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
67
sub rspec($)		{ return field($_[0], "rspec"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
68 69 70
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
71 72
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
73
sub ticket($)		{ return field($_[0], "ticket"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
74
sub asString($)		{ return field($_[0], "ticket_string"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
75
sub ticket_string($)	{ return field($_[0], "ticket_string"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
76 77 78 79 80
sub component($)	{ return field($_[0], "component"); }

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    #
    # 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
105 106
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    # 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));
    my $rspec = XMLin($rspec_node->toString(), ForceArray => 0);

    # 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
122 123 124 125
    my $slice_cert = $uuid_node->to_literal();
    my $slice_uuid;
    GeniCredential->CertificateInfo($slice_cert, \$slice_uuid) == 0
	or return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
126 127 128 129 130 131 132 133 134 135 136

    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
137 138 139 140
    my $owner_cert = $uuid_node->to_literal();
    my $owner_uuid;
    GeniCredential->CertificateInfo($owner_cert, \$owner_uuid) == 0
	or return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
141 142 143 144 145 146

    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
147 148 149 150
    #
    # 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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    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
169 170
    }

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

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
#
# 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
205 206 207 208 209 210 211 212
#
# 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
213
    return -1
Leigh B. Stoller's avatar
Leigh B. Stoller committed
214 215 216 217 218 219
	if (! ref($self));

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
220
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
221 222 223
# 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
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
#
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
252 253 254 255 256 257 258 259 260 261
#
# 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
262 263 264 265

    # 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
266
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
267 268
    my $slice_cert = $self->slice_cert();
    my $owner_cert = $self->owner_cert();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
269 270
    my $rspec_xml  = XMLout($self->rspec(), "NoAttr" => 1);
    $rspec_xml =~ s/opt\>/rspec\>/g;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
271 272 273 274 275 276

    #
    # 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
277
	"<credential xml:id=\"ref1\">\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
278
	" <type>ticket</type>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
279
	" <serial>$idx</serial>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
280 281
	" <owner_uuid>$owner_cert</owner_uuid>\n".
	" <this_uuid>$slice_cert</this_uuid>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
282 283
	" <ticket>\n".
	"  <can_delegate>1</can_delegate>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
284
	"  $rspec_xml\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298
	" </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
299
    if (! open(SIGNER, "$SIGNCRED -c $CMCERT $filename |")) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
300 301 302 303 304 305 306
	print STDERR "Could not sign $filename\n";
	return -1;
    }
    my $ticket = "";
    while (<SIGNER>) {
	$ticket .= $_;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
307 308 309 310
    if (!close(SIGNER)) {
	print STDERR "Could not sign $filename\n";
	return -1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
311
    $self->{'ticket_string'} = $ticket;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
312

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
316
    unlink($filename);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
317 318 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
    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
357 358 359
    return 0;
}

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