GeniCredential.pm.in 8.87 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 22 23 24 25 26 27 28 29 30
#!/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;
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
31 32 33
# Exported variables
use vars qw(@EXPORT_OK $LOCALSA_FLAG $LOCALCM_FLAG);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
34 35 36 37 38 39 40 41 42 43
# 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
44 45 46 47 48 49
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
50 51 52 53 54 55

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

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

    my $self = {};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
62 63 64 65 66 67
    $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
68 69
    $self->{'string'}        = undef;
    $self->{'capabilities'}  = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
70
    $self->{'idx'}	     = undef;	# Only set when stored to DB.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
71 72 73 74 75 76
    bless($self, $class);

    return $self;
}
# accessors
sub field($$)           { return ($_[0]->{$_[1]}); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
77
sub idx($)		{ return field($_[0], "idx"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
78 79
sub this($)		{ return field($_[0], "this"); }
sub owner($)		{ return field($_[0], "owner"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
80 81
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
82 83
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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
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.
#
sub CreateFromSigned($$)
{
    my ($class, $string) = @_;

    # First verify the credential
    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);

    # 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
139 140 141 142
    my $this_cert = $uuid_node->to_literal();
    my $this_uuid;
    GeniCredential->CertificateInfo($this_cert, \$this_uuid) == 0
	or return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
143 144 145 146 147 148 149 150 151 152 153

    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
154 155 156 157
    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
158 159 160 161 162 163 164 165 166 167

    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
168 169
    $self->{'this_cert'}     = $this_cert;
    $self->{'owner_cert'}    = $owner_cert;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
170 171
    $self->{'string'}        = $string;
    $self->{'xmlref'}        = $doc;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
172
    $self->{'idx'}	     = undef;	# Only set when stored to DB.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    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
188 189 190 191 192 193
    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
194 195 196 197 198 199
    return 0;
}

#
# Sign the credential.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
200
sub Sign($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
201
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
202
    my ($self, $flag) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216

    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
217 218
    my $this_cert  = $self->this_cert();
    my $owner_cert = $self->owner_cert();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
219 220 221 222 223 224 225 226 227 228 229
    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
230 231
	" <owner_uuid>$owner_cert</owner_uuid>\n".
	" <this_uuid>$this_cert</this_uuid>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	"  $cap_xml\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 credential
    # that is returned. 
    #
    if (! open(SIGNER, "$SIGNCRED $filename |")) {
	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
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
#
# 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
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
#
# Create a certificate pair, which gives us a uuid to use for an object.
# We need a file to store the cert/key in.
#
sub CreateCertificate($$$$)
{
    my ($class, $what, $certfile, $pref) = @_;

    system("$MKCERT -o $certfile $what $uuid");
    if ($?) {
	print STDERR "Could not start $MKCERT\n";
	return -1;
    }
    my $cert;
    open(CERT, $certfile) or
	return -1;
    while (<CERT>) {
	if ($_ =~ /^-----BEGIN CERT/) {
	    $cert = "";
	    next;
	}
	last
	    if ($_ =~ /^-----END CERT/);
	next
	    if (!defined($cert));
	$cert .= $_;
    }
    close(CERT);
    $$pref = $cert;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
333 334 335 336 337 338 339 340
#
# Convert a certificate to its uuid.
#
sub CertificateInfo($$$)
{
    my ($class, $string, $pref) = @_;

    # Deleted when scope is left.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
341
    my $tempfile = new File::Temp(UNLINK => 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    my $filename = $tempfile->filename;    
    print $tempfile "-----BEGIN CERTIFICATE-----\n";
    print $tempfile "$string";
    print $tempfile "-----END CERTIFICATE-----\n";

    my $dn = `$OPENSSL x509 -in $filename -noout -subject`;
    if ($?) {
	print STDERR "Cailed to convert $filename to uuid!\n";
	return -1;
    }

    if ($dn =~ /CN=([-\w]*)$/) {
	$$pref = $1;
    }
    else {
	return -1;
    }
    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;