GeniCredential.pm.in 8.61 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 GeniCredential;

#
# Some simple credential 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;
22
use GeniCertificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23 24 25 26 27 28 29 30 31
use libtestbed;
use Experiment;
use libdb qw(TBGetUniqueIndex);
use English;
use XML::Simple;
use XML::LibXML;
use Data::Dumper;
use File::Temp qw(tempfile);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
32 33 34
# Exported variables
use vars qw(@EXPORT_OK $LOCALSA_FLAG $LOCALCM_FLAG);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
35 36 37 38 39 40 41 42 43 44
# 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";
my $VERIFYCRED	   = "$TB/sbin/verifygenicred";
my $NFREE	   = "$TB/bin/nfree";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45 46 47 48 49 50
my $OPENSSL	   = "/usr/bin/openssl";

# Signing flags
$LOCALSA_FLAG	   = 1;
$LOCALCM_FLAG	   = 2;
@EXPORT_OK	   = qw(LOCALSA_FLAG LOCALCM_FLAG);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
51 52 53 54 55 56

#
# Create an empty credential object.
#
sub Create($$$$)
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
57 58 59 60
    my ($class, $this, $owner) = @_;

    return undef
	if (! (ref($this) && ref($owner)));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61 62

    my $self = {};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
63 64 65 66 67 68
    $self->{'this'}          = $this;
    $self->{'owner'}         = $owner;
    $self->{'this_uuid'}     = $this->uuid();
    $self->{'owner_uuid'}    = $owner->uuid();
    $self->{'this_cert'}     = $this->cert();
    $self->{'owner_cert'}    = $owner->cert();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
69 70
    $self->{'string'}        = undef;
    $self->{'capabilities'}  = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
71
    $self->{'idx'}	     = undef;	# Only set when stored to DB.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
72 73 74 75 76 77
    bless($self, $class);

    return $self;
}
# accessors
sub field($$)           { return ($_[0]->{$_[1]}); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
78
sub idx($)		{ return field($_[0], "idx"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
79 80
sub this($)		{ return field($_[0], "this"); }
sub owner($)		{ return field($_[0], "owner"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
81 82
sub this_uuid($)	{ return field($_[0], "this_uuid"); }
sub owner_uuid($)	{ return field($_[0], "owner_uuid"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
83 84
sub this_cert($)	{ return field($_[0], "this_cert"); }
sub owner_cert($)	{ return field($_[0], "owner_cert"); }
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 105 106 107
sub asString($)		{ return field($_[0], "string"); }
sub capabilities($)	{ return field($_[0], "capabilities"); }

#
# Add a capability to the array.
#
sub AddCapability($$$)
{
    my ($self, $name, $delegate) = @_;

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

    if (!defined($self->capabilities())) {
	$self->{'capabilities'} = {};
    }
    $self->{'capabilities'}->{$name} = {"can_delegate" => $delegate};
    return 0;
}

#
# Create a credential object from a signed credential string.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
108
sub CreateFromSigned($$;$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
109
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
110 111 112 113 114 115 116 117 118
    my ($class, $string, $nosig) = @_;

    #
    # 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));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
119 120

    # First verify the credential
Leigh B. Stoller's avatar
Leigh B. Stoller committed
121 122 123 124 125 126 127 128 129 130 131 132
    if (! $nosig) {
	my ($fh, $filename) = tempfile(UNLINK => 0);
	return undef
	    if (!defined($fh));
	print $fh $string;
	close($fh);
	system("$VERIFYCRED $filename");
	if ($?) {
	    print STDERR "Credential in $filename did not verify\n";
	    return undef;
	}
	unlink($filename);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    }

    # Use XML::Simple to convert to something we can mess with.
    my $parser = XML::LibXML->new;
    my $doc    = $parser->parse_string($string);

    # Dig out the capabilities
    my ($cap_node) = $doc->getElementsByTagName("capabilities");
    return undef
	if (!defined($cap_node));
    my $capabilities = XMLin($cap_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
150 151
    my $this_cert = $uuid_node->to_literal();
    my $this_uuid;
152
    GeniCertificate->CertificateInfo($this_cert, \$this_uuid) == 0
Leigh B. Stoller's avatar
Leigh B. Stoller committed
153
	or return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
154 155 156 157 158 159 160 161 162 163 164

    if (! ($this_uuid =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/)) {
	print STDERR "Invalid this_uuid in credential\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
165 166
    my $owner_cert = $uuid_node->to_literal();
    my $owner_uuid;
167
    GeniCertificate->CertificateInfo($owner_cert, \$owner_uuid) == 0
Leigh B. Stoller's avatar
Leigh B. Stoller committed
168
	or return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
169 170 171 172 173 174 175 176 177 178

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

    my $self = {};
    $self->{'capabilities'}  = $capabilities;
    $self->{'this_uuid'}     = $this_uuid;
    $self->{'owner_uuid'}    = $owner_uuid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
179 180
    $self->{'this_cert'}     = $this_cert;
    $self->{'owner_cert'}    = $owner_cert;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
181 182
    $self->{'string'}        = $string;
    $self->{'xmlref'}        = $doc;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
183
    $self->{'idx'}	     = undef;	# Only set when stored to DB.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    bless($self, $class);

    return $self;
}

#
# Might have to delete this from the DB.
#
sub Delete($)
{
    my ($self) = @_;

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
199 200 201 202 203 204
    if (defined($self->idx())) {
	my $idx = $self->idx();
	
	DBQueryWarn("delete from geni_credentials where idx='$idx'")
	    or return -1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
205 206 207 208 209 210
    return 0;
}

#
# Sign the credential.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
211
sub Sign($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
212
{
213
    my ($self, $how) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227

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

    # If no capabilities, then allow all rights, no delegation.
    if (!defined($self->capabilities())) {
	$self->AddCapability("*", 0);
    }
    # This little wrapup is for xmlout.
    my $capabilities = {"capability" => $self->capabilities()};

    # Every one gets a new unique index, which is used in the xml:id below.
    my $idx = TBGetUniqueIndex('next_ticket', 1);
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
228 229
    my $this_cert  = $self->this_cert();
    my $owner_cert = $self->owner_cert();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
230 231 232 233 234 235 236 237 238 239 240
    my $cap_xml    = XMLout($capabilities, "NoAttr" => 1);
    $cap_xml =~ s/opt\>/capabilities\>/g;

    #
    # Create a template xml file to sign.
    #
    my $template =
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n".
	"<credential xml:id=\"ref1\">\n".
	" <type>capability</type>\n".
	" <serial>$idx</serial>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
241 242
	" <owner_uuid>$owner_cert</owner_uuid>\n".
	" <this_uuid>$this_cert</this_uuid>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
243 244 245 246 247 248 249 250 251 252
	"  $cap_xml\n".
        "</credential>\n";

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

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

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    #
    # Who signs the credential? $how is either a flag (CM or SA) or its
    # a certificate object in the DB. 
    #
    my $certificate;
    if (ref($how)) {
	# This will auto delete too.
	my $certfile = $how->WriteToFile();
	if (!defined($certfile)) {
	    print STDERR "Could not write $certificate to temp file\n";
	    return -1;
	}
	$certificate = "-c $certfile";
    }
    elsif ($how == $LOCALSA_FLAG) {
	$certificate = "-c $TB/etc/genisa.pem";
    }
    elsif ($how == $LOCALCM_FLAG) {
	$certificate = "-c $TB/etc/genicm.pem";
    }
    else {
	print STDERR "Invalid 'how' argument to Sign()\n";
	return -1;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
278 279 280 281
    #
    # Fire up the signer and capture the output. This is the signed credential
    # that is returned. 
    #
282
    if (! open(SIGNER, "$SIGNCRED $certificate $filename |")) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	print STDERR "Could not start $SIGNCRED on $filename\n";
	return -1;
    }
    my $credential = "";
    while (<SIGNER>) {
	$credential .= $_;
    }
    if (!close(SIGNER)) {
	print STDERR "Could not sign $filename\n";
	return -1;
    }
    $self->{'string'} = $credential;
    unlink($filename);
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
#
# Store the given signed credential in the DB. Client side only for now;
# does the CM need to save all the credentials it hands out?
#
sub Store($)
{
    my ($self) = @_;
    my @insert_data  = ();

    # Do not store again.
    return 0
	if (defined($self->idx()));

    # Every credential store gets a new unique index.
    my $idx = TBGetUniqueIndex('next_ticket', 1);
    
    my $this_uuid  = $self->this_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, "this_uuid='$this_uuid'");
    push(@insert_data, "owner_uuid='$owner_uuid'");
    
    my $safe_credential = DBQuoteSpecial($self->asString());
    push(@insert_data, "credential_string=$safe_credential");

    # Insert into DB.
    DBQueryWarn("insert into geni_credentials 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
337 338
# _Always_ make sure that this 1 is at the end of the file...
1;