GeniUser.pm.in 20.7 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2008-2016 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.
# 
# }}}
29 30 31 32 33 34 35 36 37 38 39
#
package GeniUser;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

use GeniDB;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
40
use GeniRegistry;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
use GeniAuthority;
42
use GeniCertificate;
43
use GeniHRN;
44
use emutil;
45 46 47 48
use English;
use overload ('""' => 'Stringify');
use vars qw();

49 50 51 52
# Do not load this for the Clearinghouse XML server.
BEGIN { 
    if (! defined($main::GENI_ISCLRHOUSE)) {
	require User;
53
	require emdb;
54 55 56
    }
}

57 58 59 60 61 62 63 64
# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $CONTROL	   = "@USERNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
65
my $PGENIDOMAIN    = "@PROTOGENI_DOMAIN@";
66
my $PROTOGENI_LOCALUSER= @PROTOGENI_LOCALUSER@;
67 68 69

# Cache of instances to avoid regenerating them.
my %users      = ();
70
BEGIN { use GeniUtil; GeniUtil::AddCache(\%users); }
71 72 73 74 75 76 77 78 79 80 81 82 83
my $debug      = 0;

# Little helper and debug function.
sub mysystem($)
{
    my ($command) = @_;

    print STDERR "Running '$command'\n"
	if ($debug);
    return system($command);
}

#
84
# Lookup by URN, idx, or uuid.
85
#
86
sub Lookup($$;$)
87
{
88
    my ($class, $token, $includelocal) = @_;
89
    my $query_result;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
90
    my $idx;
91

92 93 94
    $includelocal = 0
	if (!defined($includelocal));

95
    if( GeniHRN::IsValid( $token ) ) {
96
	$token = GeniHRN::Normalise( $token );
97
	my $safe_token = DBQuoteSpecial($token);
98 99 100 101
	my ($authority, $type, $id) = GeniHRN::Parse( $token );

	return undef if $type ne "user";

102
	if( GeniHRN::Authoritative( $token, "@OURDOMAIN@" ) ) {
103 104
	    my $safe_hrn = DBQuoteSpecial("${PGENIDOMAIN}.$id");
	    
105 106 107
	    # A local name, so look only for local users...
	    $query_result =
		DBQueryWarn("select idx from geni_users ".
108
			    "where hrn=$safe_hrn " .
Leigh B Stoller's avatar
Leigh B Stoller committed
109
			    "and (status='active' or status='unverified')");
110 111

	    return undef
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		if (!$query_result);

	    if (!$query_result->numrows) {
		return undef
		    if (!$includelocal);

		#
		# Check Emulab users table. 
		#
		my $user = User->Lookup($id);
		return undef
		    if (!defined($user));
		return GeniUser->CreateFromLocal($user);
	    }
	} else {
127 128 129 130 131 132 133 134 135
	    # A foreign name.
	    if ($PROTOGENI_LOCALUSER) {
		# See if a local account for a nonlocal user.
		my $user = User->LookupNonLocal($token);
		if (defined($user)) {
		    return GeniUser->CreateFromLocal($user);
		}
	    }
	    # Check against names in certificates.
136 137 138
	    $query_result = DBQueryWarn(
		"SELECT geni_users.idx FROM geni_users, geni_certificates " .
		"WHERE geni_users.uuid = geni_certificates.uuid AND " .
139
		"geni_certificates.urn = $safe_token;" );
140
	    return undef unless $query_result and $query_result->numrows;
141 142 143 144
	}
	($idx) = $query_result->fetchrow_array();	
    }
    elsif ($token =~ /^\d+$/) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
145
	$idx = $token;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
146 147 148
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	$query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
149
	    DBQueryWarn("select idx from geni_users ".
Leigh B Stoller's avatar
Leigh B Stoller committed
150 151
			"where uuid='$token' and ".
			"      (status='active' or status='unverified')");
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

	return undef
	    if (!$query_result);

	if (!$query_result->numrows) {
	    return undef
		if (!$includelocal);

	    #
	    # Check Emulab users table. 
	    #
	    my $user = User->LookupByUUID($token);
	    return undef
		if (!defined($user));
	    return GeniUser->CreateFromLocal($user);
	}
	($idx) = $query_result->fetchrow_array();
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
170
    elsif ($token =~ /^[-\w\.]*$/) {
171 172
	$query_result =
	    DBQueryWarn("select idx from geni_users ".
Leigh B Stoller's avatar
Leigh B Stoller committed
173 174
			"where hrn='$token' and ".
			"(status='active' or status='unverified')");
175 176 177 178 179

	return undef
	    if (!$query_result);

	if (!$query_result->numrows) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
180
	    return undef
181
		if (!$includelocal);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
182

183 184 185 186 187 188 189 190 191 192 193
	    #
	    # Check Emulab users table for last part of hrn.
	    #
	    ($token) = ($token =~ /\.(\w*)$/);

	    my $user = User->Lookup($token);
	    return undef
		if (!defined($user));
	    return GeniUser->CreateFromLocal($user);
	}
	($idx) = $query_result->fetchrow_array();
194 195 196 197 198
    }
    else {
	return undef;
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
199 200 201 202 203 204
    # Look in cache first
    return $users{"$idx"}
        if (exists($users{"$idx"}));

    $query_result =
	DBQueryWarn("select * from geni_users where idx='$idx'");
205
    
206 207 208 209 210 211
    return undef
	if (!$query_result || !$query_result->numrows);

    my $self         = {};
    $self->{'USER'}  = $query_result->fetchrow_hashref();
    bless($self, $class);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
212 213 214 215 216

    #
    # Grab the certificate, since we will probably want it.
    #
    my $uuid = $self->{'USER'}->{'uuid'};
217 218 219
    my $certificate = GeniCertificate->Lookup($uuid);
    if (!defined($certificate)) {
	print STDERR "Could not find certificate for user $idx ($uuid)\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
220 221
	return undef;
    }
222
    $self->{'CERT'} = $certificate;
223 224
    
    # Add to cache. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
225
    $users{$self->{'USER'}->{'idx'}} = $self;
226 227 228 229 230
    
    return $self;
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'USER'}->{$_[1]}); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
231
sub idx($)		{ return field($_[0], "idx"); }
232 233
sub uid_idx($)		{ return field($_[0], "idx"); }
sub dbid($)		{ return field($_[0], "idx"); }
234
sub uid($)		{ return field($_[0], "uid"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
235
sub hrn($)		{ return field($_[0], "hrn"); }
236
sub uuid($)		{ return field($_[0], "uuid"); }
237
sub expires($)		{ return field($_[0], "expires"); }
238
sub last_activity($)	{ return field($_[0], "last_activity"); }
239 240 241 242 243
sub status($)		{ return field($_[0], "status"); }
sub created($)		{ return field($_[0], "created"); }
sub archived($)		{ return field($_[0], "archived"); }
sub name($)		{ return field($_[0], "name"); }
sub email($)		{ return field($_[0], "email"); }
244
sub cert($)		{ return $_[0]->{'CERT'}->cert(); }
245
sub sa_uuid($)		{ return field($_[0], "sa_uuid"); }
Leigh B Stoller's avatar
Leigh B Stoller committed
246
sub auth_token($)	{ return field($_[0], "auth_token"); }
247
sub GetCertificate($)   { return $_[0]->{'CERT'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
248
sub IsActive($)         { return ($_[0]->status() eq "active" ? 1 : 0); }
249

Leigh B. Stoller's avatar
Leigh B. Stoller committed
250 251 252 253 254 255 256
#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
257 258
    my $hrn = $self->hrn();
    my $idx = $self->idx();
259

Leigh B. Stoller's avatar
Leigh B. Stoller committed
260
    return "[GeniUser: $hrn, IDX: $idx]";
261 262
}

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
#
# This lookup is needed to deal with a design choice mistake; we are using
# the geni-sa DB for both portal guest users and for Geni nonlocal users
# (PROTOGENI_LOCALUSER=1). This causes a conflict, the portal guest users
# should be someplace else. 
#
sub LookupGuestOnly($$)
{
    my ($class, $urn) = @_;

    return undef
	if (!GeniHRN::IsValid($urn));

    my ($authority, $type, $id) = GeniHRN::Parse($urn);
    return undef
	if ($type ne "user");

    my $safe_urn = DBQuoteSpecial($urn);
	    
    my $query_result =
	DBQueryWarn("SELECT geni_users.idx FROM ".
		    "  geni_users, geni_certificates " .
		    "WHERE geni_users.uuid = geni_certificates.uuid AND " .
		    "geni_certificates.urn = $safe_urn;" );

    return undef
	if (! ($query_result && $query_result->numrows));

    my ($idx) = $query_result->fetchrow_array();
    return GeniUser->Lookup($idx);
}

295 296 297 298 299 300 301 302 303 304
#
# Flush from our little cache.
#
sub Flush($)
{
    my ($self) = @_;

    delete($users{$self->idx()});
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
# Return the URN.
#
# Return the URN. This is complicated by the fact that the DB does
# not store the urn, but is in the certificate. Further, it might
# be a slice from an SA not doing URNs yet, in which case set it to
# the uuid and hope for the best.
#
sub urn($)
{
    my ($self) = @_;
    my $urn = $self->GetCertificate()->urn();

    return $urn
	if (defined($urn) && $urn ne "");

320
    return $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
321 322
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
323 324 325 326 327
#
# Class method to check for an existing user that has the same
# uid/email. Lets not allow this for now. Return the number of
# users that match or -1 if an error. 
#
328
sub CheckConflict($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
329
{
330
    my ($class, $certificate) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
331

332 333
    my $safe_hrn   = DBQuoteSpecial($certificate->hrn());
    my $safe_email = DBQuoteSpecial($certificate->email());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
334 335

    my $query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
336
	DBQueryFatal("select idx from geni_users ".
337
		     "where hrn=$safe_hrn or email=$safe_email");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
338 339 340 341 342 343
    return -1
	if (!defined($query_result));

    return $query_result->numrows;
}

344 345 346 347 348 349 350 351 352 353 354 355
#
# Check to see if a uid is valid (and okay for a password file).
#
sub ValidUserID($$)
{
    my ($class, $uid) = @_;

    return TBcheck_dbslot($uid, "users", "uid",
			  TBDB_CHECKDBSLOT_WARN()|
			  TBDB_CHECKDBSLOT_ERROR());
}

356
#
357
# Class function to create new Geni user in the DB and return object. 
358
#
Leigh B Stoller's avatar
Leigh B Stoller committed
359
sub Create($$$$;$$)
360
{
Leigh B Stoller's avatar
Leigh B Stoller committed
361
    my ($class, $certificate, $authority, $info, $unverified) = @_;
362
    my $idx;
363 364
    my @insert_data = ();

Leigh B Stoller's avatar
Leigh B Stoller committed
365 366 367 368
    $unverified = 0
	if (!defined($unverified));
    my $status = ($unverified ? "unverified" : "active");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
369
    # Every user gets a new unique index.
370 371 372 373
    if (defined($main::GENI_ISCLRHOUSE) && $main::GENI_ISCLRHOUSE) {
	$idx = TBGetUniqueIndex('next_geniuser', 1);
    }
    else {
374
	$idx = User->NextIDX();
375
    }	
376

377 378 379 380 381 382
    if (!defined($authority)) {
	print STDERR "Need to specify an authority!\n";
	return undef;
    }
    my $sa_uuid = $authority->uuid();

383 384
    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
385
    push(@insert_data, "idx='$idx'");
Leigh B Stoller's avatar
Leigh B Stoller committed
386
    push(@insert_data, "status='$status'");
387

388 389 390
    my $safe_hrn   = DBQuoteSpecial($certificate->hrn());
    my $safe_uuid  = DBQuoteSpecial($certificate->uuid());
    my $safe_email = DBQuoteSpecial($certificate->email());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
391 392
    push(@insert_data, "hrn=$safe_hrn");
    push(@insert_data, "uuid=$safe_uuid");
393
    push(@insert_data, "email=$safe_email");
394
    push(@insert_data, "sa_uuid='$sa_uuid'");
395

396
    #
397
    # uid comes from urn, but only if its valid for Emulab.
398
    #
399 400 401
    my (undef,$type,$uid) = GeniHRN::Parse($certificate->urn());
    if (!defined($uid) || $type ne "user") {
	print STDERR "URN is not well formed: " . $certificate->urn() . "\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
402 403
	return undef;
    }
404 405 406 407 408 409 410 411 412 413 414
    if (! GeniUser->ValidUserID($uid)) {
	print STDERR
	    "UID is not valid for Emulab: " . $certificate->urn() . "\n";
	#
	# Create something reasonable and consistent.
	# It gets returned in the manifest.
	#
	my $hash = $certificate->sha1();
	return undef
	    if (!defined($hash));
	$hash = substr($hash, 0, 7);
Leigh B Stoller's avatar
Leigh B Stoller committed
415
	if (!$hash || !GeniUser->ValidUserID("u${hash}")) {
416 417 418 419 420 421 422
	    print STDERR "Cannot form a uid from certificate\n".
		$certificate->cert() . "\n";
	    return undef;
	}
	$uid = lc("u${hash}");
	print STDERR "Using $uid instead\n";
    }
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

    #
    # This comes from either an info record or the cert.
    #
    my $safe_name;
    if (defined($info) && exists($info->{'name'})) {
	$safe_name = DBQuoteSpecial($info->{'name'});
    }
    else {
	$safe_name = DBQuoteSpecial($uid);
    }
    my $safe_uid = DBQuoteSpecial($uid);

    push(@insert_data, "uid=$safe_uid");
    push(@insert_data, "name=$safe_name");

    if ($certificate->Store() != 0) {
	print STDERR "Could not store certificate for new user.\n";
441 442 443
	return undef;
    }

444
    # Insert into DB.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
445 446 447 448
    if (! DBQueryWarn("insert into geni_users set " .
		      join(",", @insert_data))) {
	return undef;
    }
449

Leigh B. Stoller's avatar
Leigh B. Stoller committed
450
    return GeniUser->Lookup($idx);
451 452
}

453 454 455
#
# Modify a record; only partial.
#
456
sub Modify($$$)
457
{
458
    my ($self, $name, $email) = @_;
459 460 461 462
    my @insert_data = ();

    my $idx  = $self->idx();
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
463 464
    my $safe_name  = DBQuoteSpecial($name || $self->name());
    my $safe_email = DBQuoteSpecial($email || $self->email());
465

466 467 468 469 470 471
    if (defined($name) || defined($email)) {
	return -1
	    if (!DBQueryWarn("update geni_users set ".
			     " name=$safe_name, email=$safe_email ".
			     "where idx='$idx'"));
    }
472 473 474
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
475 476 477 478 479 480 481 482 483 484 485
#
# We wrap up local users so that the interface to them is consistent, but
# do not want to duplicate any data, so use a different class wrapper.
#
sub CreateFromLocal($$)
{
    my ($class, $user) = @_;

    return GeniUser::LocalUser->Create($user);
}

486
#
487
# Delete the user.
488 489 490 491 492 493 494 495
#
sub Delete($)
{
    my ($self) = @_;

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

496 497
    my $idx  = $self->idx();
    my $uuid = $self->uuid();
498

499 500
    DBQueryWarn("delete from geni_bindings where user_uuid='$uuid'")
	or return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
501
    DBQueryWarn("delete from geni_userkeys where uuid='$uuid'")
502
	or return -1;
503 504
    DBQueryWarn("delete from geni_certificates where uuid='$uuid'")
	or return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
505
    DBQueryWarn("delete from geni_users where idx='$idx'")
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	or return -1;
    
    return 0;
}

#
# Archive user. 
#
sub Archive($)
{
    my ($self) = @_;

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
521
    my $idx = $self->idx();
522 523

    DBQueryWarn("update geni_users set status='archived' ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
524
		"where idx='$idx'")
525 526 527 528 529
	or return -1;
    
    return 0;
}

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
#
# Bump activity.
#
sub BumpActivity($)
{
    my ($self) = @_;

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

    my $idx = $self->idx();

    DBQueryWarn("update geni_users set last_activity=now() ".
		"where idx='$idx'")
	or return -1;
    
    return 0;
}

Leigh B Stoller's avatar
Leigh B Stoller committed
549 550 551 552 553 554 555 556 557 558 559 560 561
sub SetAuthToken($$)
{
    my ($self, $token) = @_;
    my $safe_token = DBQuoteSpecial($token);
    my $idx = $self->idx();

    DBQueryWarn("update geni_users set auth_token=$safe_token ".
		"where idx='$idx'")
	or return -1;
    
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
562 563 564 565 566 567 568 569 570 571
#
# Is this a local user. 
#
sub IsLocal($)
{
    my ($self) = @_;

    return ref($self) eq "GeniUser::LocalUser";
}

Leigh B Stoller's avatar
Leigh B Stoller committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
#
# Add an sshkey for a user. 
#
sub AddKeyAux($$$)
{
    my ($self, $internal, $key) = @_;
    my $uuid = $self->uuid();
    my $safe_key = DBQuoteSpecial($key);
    $internal = ($internal ? 1 : 0);

    DBQueryWarn("insert into geni_userkeys set internal='$internal', ".
		" `key`=$safe_key, type='ssh', uuid='$uuid', created=now()")
	or return -1;

    return 0;
}
sub AddKey($$)          {  return AddKeyAux($_[0], 0, $_[1]); }
sub AddInternalKey($$)  {  return AddKeyAux($_[0], 1, $_[1]); }

sub GetKeys($)
{
    my ($self) = @_;
    my $uuid = $self->uuid();
    my @keys = ();

    my $query_result =
	DBQueryWarn("select `key` from geni_userkeys where uuid='$uuid'");
    return ()
	if (!defined($query_result) || !$query_result->numrows);
    
    while (my ($key) = $query_result->fetchrow_array()) {
	push(@keys, $key);
    }
    return @keys;
}

608 609 610 611 612
#
# Get the key bundle for a local user, which are just sshkeys. 
# This function is intended to be used only by the SA to get the
# key bundle from the emulab ssh keys for the local user. 
#
Leigh B Stoller's avatar
Leigh B Stoller committed
613
sub GetKeyBundle($$;$)
614
{
Leigh B Stoller's avatar
Leigh B Stoller committed
615
    my ($self, $pref, $ignored) = @_;
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
    my @results = ();

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

    my @tmp = $self->GetKeys();
    goto done
	if (!@tmp);

    foreach my $sshkey (@tmp) {
	push(@results, {"type" => 'ssh',
			"key"  => $sshkey});
    }
  done:
    @$pref = @results;
    return 0;
}

Leigh B Stoller's avatar
Leigh B Stoller committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
#
# Delete sshkeys for a user.
#
sub DeleteKeys($)
{
    my ($self) = @_;
    my $uuid = $self->uuid();

    DBQueryWarn("delete from geni_userkeys where ".
		"  internal=0 and uuid='$uuid'")
	or return -1;
    
    return 0;
}

sub DeleteInternalKeys($)
{
    my ($self) = @_;
    my $uuid = $self->uuid();

    DBQueryWarn("delete from geni_userkeys where ".
		"  internal=1 and uuid='$uuid'")
	or return -1;
    
    return 0;
}

661
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
662
# Delete all users for an authority.
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
#
sub DeleteAll($$)
{
    my ($class, $authority) = @_;

    my $uuid = $authority->uuid();
    my $query_result =
	DBQueryWarn("select uuid from geni_users ".
		    "where sa_uuid='$uuid'");

    return -1
	if (! $query_result);
    return 0
	if (!$query_result->numrows);

    while (my ($uuid) = $query_result->fetchrow_array()) {
	my $user = GeniUser->Lookup($uuid);
	if (!defined($user)) {
	    print STDERR "Could not lookup user $uuid\n";
	    return -1;
	}
	#
	# Do not allow users with active slices to be deleted.
	#
	my $active_result =
	    DBQueryWarn("select uuid from geni_slices ".
			"where creator_uuid='$uuid'");
	return -1
	    if (!$active_result);
	if ($active_result->numrows()) {
	    print STDERR "$user is heading up active slices\n";
	}

	$active_result =
	    DBQueryWarn("select uuid from geni_slivers ".
			"where creator_uuid='$uuid'");
	return -1
	    if (!$active_result);
	if ($active_result->numrows()) {
	    print STDERR "$user is heading up active slivers\n";
	}
	
	if ($user->Delete() != 0) {
	    print STDERR "Could not delete $user\n";
	    return -1;
	}
    }

    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
#
# List All users.
#
sub ListAll($$)
{
    my ($class, $pref) = @_;
    my @result = ();
    @$pref = ();

    my $query_result =
	DBQueryWarn("select uuid from geni_users");

    return -1
	if (! $query_result);
    return 0
	if (!$query_result->numrows);

    while (my ($uuid) = $query_result->fetchrow_array()) {
	my $user = GeniUser->Lookup($uuid);
	if (!defined($user)) {
	    print STDERR "Could not lookup user $uuid\n";
	    return -1;
	}
	push(@result, $user);
    }
    @$pref = @result;
    return 0;
}

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
#
# Register this local user at the ClearingHouse.
#
sub Register($)
{
    my ($self) = @_;

    my $clearinghouse = GeniRegistry::ClearingHouse->Create();
    return -1
	if (!defined($clearinghouse));

    return $clearinghouse->RegisterUser($self->name(), $self->email(),
					$self->cert());
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
758 759 760 761 762
############################################################################
#
# Wrapper for local users.
#
package GeniUser::LocalUser;
763 764 765
use vars qw(@ISA);
@ISA = "GeniUser";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
766 767
use English;
use GeniDB;
768
use GeniUser;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
769
use GeniHRN;
770
use GeniCertificate;
771
use User;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
772 773
use overload ('""' => 'Stringify');

774 775 776 777 778 779 780 781 782 783 784
sub Lookup($$)
{
    my ($class, $uid) = @_;

    my $user = User->Lookup($uid);
    return undef
	if (!defined($user));
    
    return GeniUser::LocalUser->Create($user);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
785 786 787 788 789 790 791 792 793 794
#
# Create a wrapper, with the same access names.
#
sub Create($$)
{
    my ($class, $user) = @_;

    my $self         = {};
    $self->{'USER'}  = $user;

795 796
    # And the certificate wrapper.
    my $certificate = GeniCertificate::LocalUser->Create($user);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
797 798 799 800
    if (!defined($certificate)) {
	print STDERR "No certificate found for $user\n";
	return undef;
    }
801
    $self->{'CERT'} = $certificate;
802

Leigh B. Stoller's avatar
Leigh B. Stoller committed
803 804 805 806
    bless($self, $class);
    return $self;
}

807
sub emulab_user()       { return $_[0]->{'USER'}; }
808
sub idx($)		{ return $_[0]->{'USER'}->uid_idx(); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
809 810 811 812 813
sub uid($)		{ return $_[0]->{'USER'}->uid(); }
sub uuid                { return $_[0]->{'USER'}->uuid(); }
sub created($)		{ return $_[0]->{'USER'}->created(); }
sub name($)		{ return $_[0]->{'USER'}->name(); }
sub email($)		{ return $_[0]->{'USER'}->email(); }
814 815
sub SSLPassPhrase($$$)  { return $_[0]->{'USER'}->SSLPassPhrase($_[1],$_[2]); }
sub HomeDir($)          { return $_[0]->{'USER'}->HomeDir(); }
816
sub admin($)		{ return $_[0]->{'USER'}->admin(); }
817 818 819
sub BumpActivity($)     { return $_[0]->{'USER'}->BumpActivity(); }
sub DefaultProject($)   { return $_[0]->{'USER'}->DefaultProject(); }
sub FlipTo($$)          { return $_[0]->{'USER'}->FlipTo($_[1]); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
820 821

# Need to construct this since not in User structure.
822
sub hrn($)		{ return "${PGENIDOMAIN}." . $_[0]->uid(); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
823

824 825 826
# And this is in another structure.
sub cert($)             { return $_[0]->{'CERT'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERT'}; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
827

828 829 830 831
# This should never be called on a local user since we do not want
# to change the main emulab info.
sub Modify($$$)		{ return -1; }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
832 833 834 835 836 837 838 839
# Return the URN.
sub urn($)
{
    my ($self) = @_;

    return GeniHRN::Generate("@OURDOMAIN@", "user", $self->uid());
}

840
#
841 842 843
# Get the key bundle for a local user, which are just sshkeys. 
# This function is intended to be used only by the SA to get the
# key bundle from the emulab ssh keys for the local user. 
844
#
Leigh B Stoller's avatar
Leigh B Stoller committed
845
sub GetKeyBundle($$;$)
846
{
Leigh B Stoller's avatar
Leigh B Stoller committed
847
    my ($self, $pref, $internal) = @_;
848 849 850 851 852 853 854
    my @results = ();

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

    my $uuid = $self->uuid();
    my @sshkeys    = ();
855
    $self->emulab_user()->GetSSHKeys(\@sshkeys);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
856
    
857 858 859 860
    foreach my $sshkey (@sshkeys) {
	push(@results, {"type" => 'ssh',
			"key"  => $sshkey});
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
861 862 863 864 865 866 867 868 869 870 871 872 873
    #
    # Add in unencrypted keys if requested. Be careful with this, only
    # from APT at the moment since we know those resources are local.
    #
    if (defined($internal) && $internal) {
	@sshkeys = ();
	$self->emulab_user()->GetDefaultSSHKeys(\@sshkeys);
    
	foreach my $sshkey (@sshkeys) {
	    push(@results, {"type" => 'ssh',
			    "key"  => $sshkey});
	}
    }
874 875 876 877
    @$pref = @results;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
878 879 880 881 882 883
#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
884
    my $user = $self->{'USER'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
885 886 887 888
    
    return "$user";
}

889 890
# _Always_ make sure that this 1 is at the end of the file...
1;