GeniUser.pm.in 19.8 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2008-2015 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
#
# Flush from our little cache.
#
sub Flush($)
{
    my ($self) = @_;

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
# 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 "");

288
    return $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
289 290
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
291 292 293 294 295
#
# 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. 
#
296
sub CheckConflict($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
297
{
298
    my ($class, $certificate) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
299

300 301
    my $safe_hrn   = DBQuoteSpecial($certificate->hrn());
    my $safe_email = DBQuoteSpecial($certificate->email());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
302 303

    my $query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
304
	DBQueryFatal("select idx from geni_users ".
305
		     "where hrn=$safe_hrn or email=$safe_email");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
306 307 308 309 310 311
    return -1
	if (!defined($query_result));

    return $query_result->numrows;
}

312 313 314 315 316 317 318 319 320 321 322 323
#
# 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());
}

324
#
325
# Class function to create new Geni user in the DB and return object. 
326
#
Leigh B Stoller's avatar
Leigh B Stoller committed
327
sub Create($$$$;$$)
328
{
Leigh B Stoller's avatar
Leigh B Stoller committed
329
    my ($class, $certificate, $authority, $info, $unverified) = @_;
330
    my $idx;
331 332
    my @insert_data = ();

Leigh B Stoller's avatar
Leigh B Stoller committed
333 334 335 336
    $unverified = 0
	if (!defined($unverified));
    my $status = ($unverified ? "unverified" : "active");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
337
    # Every user gets a new unique index.
338 339 340 341
    if (defined($main::GENI_ISCLRHOUSE) && $main::GENI_ISCLRHOUSE) {
	$idx = TBGetUniqueIndex('next_geniuser', 1);
    }
    else {
342
	$idx = User->NextIDX();
343
    }	
344

345 346 347 348 349 350
    if (!defined($authority)) {
	print STDERR "Need to specify an authority!\n";
	return undef;
    }
    my $sa_uuid = $authority->uuid();

351 352
    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
353
    push(@insert_data, "idx='$idx'");
Leigh B Stoller's avatar
Leigh B Stoller committed
354
    push(@insert_data, "status='$status'");
355

356 357 358
    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
359 360
    push(@insert_data, "hrn=$safe_hrn");
    push(@insert_data, "uuid=$safe_uuid");
361
    push(@insert_data, "email=$safe_email");
362
    push(@insert_data, "sa_uuid='$sa_uuid'");
363

364
    #
365
    # uid comes from urn, but only if its valid for Emulab.
366
    #
367 368 369
    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
370 371
	return undef;
    }
372 373 374 375 376 377 378 379 380 381 382
    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
383
	if (!$hash || !GeniUser->ValidUserID("u${hash}")) {
384 385 386 387 388 389 390
	    print STDERR "Cannot form a uid from certificate\n".
		$certificate->cert() . "\n";
	    return undef;
	}
	$uid = lc("u${hash}");
	print STDERR "Using $uid instead\n";
    }
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

    #
    # 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";
409 410 411
	return undef;
    }

412
    # Insert into DB.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
413 414 415 416
    if (! DBQueryWarn("insert into geni_users set " .
		      join(",", @insert_data))) {
	return undef;
    }
417

Leigh B. Stoller's avatar
Leigh B. Stoller committed
418
    return GeniUser->Lookup($idx);
419 420
}

421 422 423
#
# Modify a record; only partial.
#
424
sub Modify($$$)
425
{
426
    my ($self, $name, $email) = @_;
427 428 429 430
    my @insert_data = ();

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

434 435 436 437 438 439
    if (defined($name) || defined($email)) {
	return -1
	    if (!DBQueryWarn("update geni_users set ".
			     " name=$safe_name, email=$safe_email ".
			     "where idx='$idx'"));
    }
440 441 442
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
443 444 445 446 447 448 449 450 451 452 453
#
# 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);
}

454
#
455
# Delete the user.
456 457 458 459 460 461 462 463
#
sub Delete($)
{
    my ($self) = @_;

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

464 465
    my $idx  = $self->idx();
    my $uuid = $self->uuid();
466

467 468
    DBQueryWarn("delete from geni_bindings where user_uuid='$uuid'")
	or return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
469
    DBQueryWarn("delete from geni_userkeys where uuid='$uuid'")
470
	or return -1;
471 472
    DBQueryWarn("delete from geni_certificates where uuid='$uuid'")
	or return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
473
    DBQueryWarn("delete from geni_users where idx='$idx'")
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
	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
489
    my $idx = $self->idx();
490 491

    DBQueryWarn("update geni_users set status='archived' ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
492
		"where idx='$idx'")
493 494 495 496 497
	or return -1;
    
    return 0;
}

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
#
# 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
517 518 519 520 521 522 523 524 525 526 527 528 529
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
530 531 532 533 534 535 536 537 538 539
#
# Is this a local user. 
#
sub IsLocal($)
{
    my ($self) = @_;

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

Leigh B Stoller's avatar
Leigh B Stoller committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
#
# 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;
}

576 577 578 579 580
#
# 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
581
sub GetKeyBundle($$;$)
582
{
Leigh B Stoller's avatar
Leigh B Stoller committed
583
    my ($self, $pref, $ignored) = @_;
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
    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
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
#
# 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;
}

629
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
630
# Delete all users for an authority.
631 632 633 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 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
#
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
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
#
# 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;
}

711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
#
# 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
726 727 728 729 730
############################################################################
#
# Wrapper for local users.
#
package GeniUser::LocalUser;
731 732 733
use vars qw(@ISA);
@ISA = "GeniUser";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
734 735
use English;
use GeniDB;
736
use GeniUser;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
737
use GeniHRN;
738
use GeniCertificate;
739
use User;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
740 741
use overload ('""' => 'Stringify');

742 743 744 745 746 747 748 749 750 751 752
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
753 754 755 756 757 758 759 760 761 762
#
# Create a wrapper, with the same access names.
#
sub Create($$)
{
    my ($class, $user) = @_;

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

763 764
    # And the certificate wrapper.
    my $certificate = GeniCertificate::LocalUser->Create($user);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
765 766 767 768
    if (!defined($certificate)) {
	print STDERR "No certificate found for $user\n";
	return undef;
    }
769
    $self->{'CERT'} = $certificate;
770

Leigh B. Stoller's avatar
Leigh B. Stoller committed
771 772 773 774
    bless($self, $class);
    return $self;
}

775
sub emulab_user()       { return $_[0]->{'USER'}; }
776
sub idx($)		{ return $_[0]->{'USER'}->uid_idx(); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
777 778 779 780 781
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(); }
782 783
sub SSLPassPhrase($$$)  { return $_[0]->{'USER'}->SSLPassPhrase($_[1],$_[2]); }
sub HomeDir($)          { return $_[0]->{'USER'}->HomeDir(); }
784
sub admin($)		{ return $_[0]->{'USER'}->admin(); }
785 786 787
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
788 789

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

792 793 794
# 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
795

796 797 798 799
# 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
800 801 802 803 804 805 806 807
# Return the URN.
sub urn($)
{
    my ($self) = @_;

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

808
#
809 810 811
# 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. 
812
#
Leigh B Stoller's avatar
Leigh B Stoller committed
813
sub GetKeyBundle($$;$)
814
{
Leigh B Stoller's avatar
Leigh B Stoller committed
815
    my ($self, $pref, $internal) = @_;
816 817 818 819 820 821 822
    my @results = ();

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

    my $uuid = $self->uuid();
    my @sshkeys    = ();
823
    $self->emulab_user()->GetSSHKeys(\@sshkeys);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
824
    
825 826 827 828
    foreach my $sshkey (@sshkeys) {
	push(@results, {"type" => 'ssh',
			"key"  => $sshkey});
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
829 830 831 832 833 834 835 836 837 838 839 840 841
    #
    # 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});
	}
    }
842 843 844 845
    @$pref = @results;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
846 847 848 849 850 851
#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
852
    my $user = $self->{'USER'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
853 854 855 856
    
    return "$user";
}

857 858
# _Always_ make sure that this 1 is at the end of the file...
1;