updategeniuser.in 12.7 KB
Newer Older
1
2
#!/usr/bin/perl -w
#
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
29
30
31
32
33
34
35
36
37
38
39
# 
# {{{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.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use Data::Dumper;

#
# Update a geni user.
#
sub usage()
{
40
    print STDERR "Usage: $0 [-c <credfile> -e <certfile>] [-s] [-p] <user>\n";
41
42
    exit(1);
}
43
my $optlist   = "c:se:np";
44
my $dosshkeys = 0;
45
my $doprojects= 0;
46
my $impotent  = 0;
47
my $debug     = 0;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
my $credfile;
my $certfile;

# Configure ...
my $TB		  = "@prefix@";
my $SACERT	  = "$TB/etc/genisa.pem";
my $ADDPUBKEY     = "$TB/sbin/addpubkey";

# Do this early so that we talk to the right DB. 
use vars qw($GENI_DBNAME);
BEGIN { $GENI_DBNAME = "geni"; }

use lib '@prefix@/lib';
use libtestbed;
use emutil;
use User;
64
use Group;
65
66
67
68
69
70
71
72
73
74
use GeniCertificate;
use GeniCredential;
use GeniAuthority;
use Genixmlrpc;
use GeniResponse;
use GeniHRN;

# Protos
sub UpdateCredential();
sub UpdateSSHKeys();
75
sub ProjectMembership();
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

sub fatal($)
{
    my ($msg) = @_;

    die("*** $0:\n".
	"    $msg\n");
}

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"s"})) {
    $dosshkeys = 1;
}
96
97
98
if (defined($options{"p"})) {
    $doprojects = 1;
}
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
if (defined($options{"n"})) {
    $impotent = 1;
}
if (defined($options{"c"})) {
    $credfile = $options{"c"};
}
if (defined($options{"e"})) {
    $certfile = $options{"e"};
}
usage()
    if (@ARGV != 1);

my $target_user = User->Lookup($ARGV[0]);
if (!defined($target_user)) {
    fatal("No such user");
}
my $target_uid = $target_user->uid();

my $this_user = User->ThisUser();
if (!defined($this_user)) {
    fatal("Who are you?");
}
if (! ($this_user->SameUser($target_user) || $this_user->IsAdmin())) {
    fatal("Not allowed to update user; must be an admin");
}

if (defined($credfile)) {
    usage()
	if (!defined($certfile));
}
if (defined($certfile)) {
    usage()
	if (!defined($credfile));
    UpdateCredential()
}
if ($dosshkeys) {
    UpdateSSHKeys();
}
137
if ($doprojects) {
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    #
    # If user is a member of a local project, then we can skip this
    # check, we have vetted them ourselves.
    #
    my @projects;
    if ($target_user->ProjectMembershipList(\@projects)) {
	fatal("Error getting local project list");
    }
    # XXX All nonlocal cloudlab users are a member of the holding project,
    # but that does not count as project membership.
    if (@projects > 1) {
	print "Local membership: @projects\n";
	$target_user->Update({"webonly" => 0})
	    if (!$impotent);
	exit(0);
    }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    my @geniprojects = ProjectMembership();
    if (@geniprojects) {
	print "Portal membership: @geniprojects\n";
    }
    else {
	print "Not a member of any projects!\n";
    }
    exit(0)
	if ($impotent);

    #
    # Update user. No projects means no permission to do anything.
    #
    if (@geniprojects) {
	$target_user->Update({"webonly" => 0});
    }
    else {
	$target_user->Update({"webonly" => 1});
    }
173
    if (0) {
174
175
176
177
178
179
180
181
182
183
    my @projects;
    $target_user->ProjectMembershipList(\@projects);
    foreach my $project (@projects) {
	my $membership = $project->GetProjectGroup()->LookupUser($target_user);
	next
	    if (!defined($membership));
	$membership->ModifyTrust((@geniprojects ?
				  $Group::MemberShip::TRUSTSTRING_LOCALROOT :
				  $Group::MemberShip::TRUSTSTRING_USER));
    }
184
    }
185
}
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
exit(0);

#
# Update the speaksfor credential for the user.
#
sub UpdateCredential()
{
    fatal("No such file: $credfile")
	if (! -e $credfile);
    fatal("No such file: $certfile")
	if (! -e $certfile);
    my $credential = GeniCredential->LoadFromFile($credfile);
    if (!defined($credential)) {
	fatal("Could not parse credential from file");
    }
    fatal("Not a speaksfor credential")
	if (! ($credential->type() eq "speaksfor" ||
	       $credential->type() eq "abac"));

    my $certificate = GeniCertificate->LoadFromFile($certfile);
    if (!defined($certificate)) {
	fatal("Could not parse certificate from file");
    }
    $target_user->StoreCredential($credential->asString(),
				  $credential->expires(),
				  $certificate->cert())
	== 0 or fatal("Could not store credential for user");

    return 0;
}

#
# Update ssh keys. 
#
sub UpdateSSHKeys()
{
Leigh B Stoller's avatar
Leigh B Stoller committed
222
223
    my $isportal = 0;
    
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
252
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
278
279
280
281
282
283
    #
    # Load the SA cert to act as caller context.
    #
    my $sa_certificate = GeniCertificate->LoadFromFile($SACERT);
    if (!defined($sa_certificate)) {
	fatal("Could not load certificate from $SACERT\n");
    }
    my $context = Genixmlrpc->Context($sa_certificate);
    if (!defined($context)) {
	fatal("Could not create context to talk to MA");
    }
    
    #
    # Need the credential and the certificate. The certificate allows us
    # to figure out who to talk to, to get the keys. For protogeni it is
    # the URL in the certificate. For the GCF, well just hardwire it to
    # the common federation api URL. 
    #
    my ($cred,$cert) = $target_user->GetStoredCredential();
    fatal("No stored credential for $target_user")
	if (!defined($cred) || !defined($cert));
    
    my $speaksfor = GeniCredential->CreateFromSigned($cred);
    if (!defined($speaksfor)) {
	fatal("Could not parse credential from string");
    }
    my $geni_type = ($speaksfor->type() eq "abac") ? "geni_abac" : "geni_sfa";
    my $geni_vers = ($speaksfor->type() eq "abac") ? 1 : 3;
	
    my $certificate = GeniCertificate->LoadFromString($cert);
    if (!defined($certificate)) {
	fatal("Could not parse certificate from string");
    }
    my $user_urn = $certificate->urn();

    #
    # We need a URL to make the RPC. IG certs have that url in
    # the certificate (clever people that we are), but GPO certs refer
    # to a nonexistent SA. So just hardwire it, just like flack
    # does.
    #
    # We are going to use the FED API. 
    #
    my @params = ([{"geni_type" => $geni_type,
		    "geni_version" => $geni_vers,
		    "geni_value" => $speaksfor->asString()}
		  ],
		  # Options array.
		  {"speaking_for" => $user_urn,
		   "geni_speaking_for" => $user_urn,
		   "match"   => {'KEY_MEMBER' => $user_urn},
		   "filter"  => ['KEY_PUBLIC'],
		  });
    my $method;
    my $url;
    my ($auth,$type,$id) = GeniHRN::Parse($user_urn);
    if ($auth =~ /geni\.net/) {
	$url = "https://ch.geni.net/MA";
	$method = "lookup";
	@params = ("KEY", @params);
Leigh B Stoller's avatar
Leigh B Stoller committed
284
	$isportal = 1;
285
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
286
287
288
289
290
291
292
293
    elsif ($auth =~ /iminds\.be/) {
	$url = $certificate->url();
	$url =~ s/sa$/geni-ma/;
	$url .= "/2";
	$method = "lookup";
	@params = ("KEY", @params);
	$isportal = 1;
    }
294
295
296
    else {
	$url = $certificate->url();
	$url =~ s/sa$/geni-ma/;
Leigh B Stoller's avatar
Leigh B Stoller committed
297
#	$url = "https://www.emulab.net:12369/protogeni/stoller/xmlrpc/geni-ma";
298
299
300
301
302
303
304
305
	$method = "lookup_keys";
    }
    my $response =
	Genixmlrpc::CallMethod($url, $context, $method, @params);
    if (!defined($response)) {
	fatal("Internal error getting self credential");
    }
    if ($response->code() != GENIRESPONSE_SUCCESS) {
306
307
	fatal("Could not get keys: " . $response->code() . ": " .
	      $response->output());
308
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
309
310
311
    if (! ref($response->value())) {
	fatal("No key array returned in response");
    }
312
    
Leigh B Stoller's avatar
Leigh B Stoller committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
    # Dig out the key list.
    my @keys = ();
    
    if ($isportal) {
	foreach my $id (keys(%{ $response->value() })) {
	    my $ref = $response->value()->{$id};
	    push(@keys, $ref);
	}
    }
    else {
	if (!exists($response->value()->{$user_urn}) &&
	    ref($response->value()->{$user_urn})) {
	    print STDERR Dumper($response);
	    fatal("Returned keys do not look right");
	}
	@keys = @{ $response->value()->{$user_urn} };
329
330
    }
    if (!@keys) {
331
332
	print STDERR "No keys returned for user!\n";
	return 0;
Leigh B Stoller's avatar
Leigh B Stoller committed
333
    }    
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
    my $filename = TBMakeTempFile("geniuserkey");

    #
    # First loop and verify all the keys. 
    #
    foreach my $ref (@keys) {
	fatal("Bad format in key array; no KEY_PUBLIC")
	    if (!exists($ref->{'KEY_PUBLIC'}));
	my $key = $ref->{'KEY_PUBLIC'};
	next
	    if ($key =~ /sslcert/);
	open(KEY, ">$filename") or
	    fatal("Could not open $filename for writing");
	print KEY $key . "\n";
	close(KEY);

	system("$ADDPUBKEY -n -f $filename");
	if ($?) {
	    fatal("Key does not verify: $key");
	}
    }
    if ($impotent) {
	print "Exiting without doing anything ...\n";
	exit(0);
    }
    
    #
    # Delete current keys and add all of the new ones.
    #
    $target_user->DeleteSSHKeys() == 0
	or fatal("Could not delete current ssh keys");

    foreach my $ref (@keys) {
	my $key = $ref->{'KEY_PUBLIC'};
	next
	    if ($key =~ /sslcert/);
	open(KEY, ">$filename") or
	    fatal("Could not open $filename for writing");
	print KEY $key . "\n";
	close(KEY);

	system("$ADDPUBKEY -s -u $target_uid -f $filename");
	if ($?) {
	    fatal("Could not add key: $key");
	}
    }
    unlink($filename);

    #
    # Regenerate the authkeys file,
    #
    system("$ADDPUBKEY -w $target_uid");
    if ($?) {
	fatal("Could not regenerate authorized_keys file");
    }
    return 0;
}
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447

#
# Request project membership from GPO Portal. 
#
sub ProjectMembership()
{
    #
    # Load the SA cert to act as caller context.
    #
    my $sa_certificate = GeniCertificate->LoadFromFile($SACERT);
    if (!defined($sa_certificate)) {
	fatal("Could not load certificate from $SACERT\n");
    }
    my $context = Genixmlrpc->Context($sa_certificate);
    if (!defined($context)) {
	fatal("Could not create context to talk to MA");
    }
    
    #
    # Need the credential and the certificate. The certificate allows us
    # to figure out who to talk to, to get the keys. For protogeni it is
    # the URL in the certificate. For the GCF, well just hardwire it to
    # the common federation api URL. 
    #
    my ($cred,$cert) = $target_user->GetStoredCredential();
    fatal("No stored credential for $target_user")
	if (!defined($cred) || !defined($cert));
    
    my $speaksfor = GeniCredential->CreateFromSigned($cred);
    if (!defined($speaksfor)) {
	fatal("Could not parse credential from string");
    }
    my $geni_type = ($speaksfor->type() eq "abac") ? "geni_abac" : "geni_sfa";
    my $geni_vers = ($speaksfor->type() eq "abac") ? 1 : 3;
	
    my $certificate = GeniCertificate->LoadFromString($cert);
    if (!defined($certificate)) {
	fatal("Could not parse certificate from string");
    }
    my $user_urn = $certificate->urn();

    #
    # We need a URL to make the RPC. IG certs have that url in
    # the certificate (clever people that we are), but GPO certs refer
    # to a nonexistent SA. So just hardwire it, just like flack
    # does.
    #
    # We are going to use the FED API. 
    #
    my @params = ([{"geni_type" => $geni_type,
		    "geni_version" => $geni_vers,
		    "geni_value" => $speaksfor->asString()}
		  ],
		  # Options array.
		  {"speaking_for" => $user_urn,
		   "geni_speaking_for" => $user_urn,
		  });
Leigh B Stoller's avatar
Leigh B Stoller committed
448
    my $method = "lookup_projects_for_member";
449
450
    my $url;
    my ($auth,$type,$id) = GeniHRN::Parse($user_urn);
Leigh B Stoller's avatar
Leigh B Stoller committed
451
452
453
    #
    # XXX We need to ask for the version ... 
    #
454
455
456
    if ($auth =~ /geni\.net/) {
	$url = "https://ch.geni.net/SA";
	@params = ($user_urn, @params);
Leigh B Stoller's avatar
Leigh B Stoller committed
457
458
459
460
461
462
    }
    elsif ($auth =~ /iminds\.be/) {
	$url = $certificate->url();
	$url =~ s/sa$/geni-sa/;
	$url .= "/1";
	@params = ($user_urn, @params);
463
464
465
466
467
468
469
470
471
472
473
    }
    else {
	$url = $certificate->url();
	$url =~ s/sa$/geni-sa/;
    }
    my $response =
	Genixmlrpc::CallMethod($url, $context, $method, @params);
    if (!defined($response)) {
	fatal("Internal error getting self credential");
    }
    if ($response->code() != GENIRESPONSE_SUCCESS) {
Leigh B Stoller's avatar
Leigh B Stoller committed
474
	print STDERR Dumper(\@params);
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
	fatal("Could not get project membership: " . $response->output());
    }
    if (! ref($response->value())) {
	fatal("No project list returned in response");
    }
    my @projects = ();

    print Dumper($response->value())
	if ($debug);

    foreach my $ref (@{ $response->value() }) {
	push(@projects, $ref->{'PROJECT_URN'})
	    if (!$ref->{'EXPIRED'});
    }
    return @projects;
}

492
exit(0);