updategeniuser.in 7.79 KB
Newer Older
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
222
223
224
225
226
227
228
229
#!/usr/bin/perl -w
#
# Copyright (c) 2008-2014 University of Utah and the Flux Group.
# 
# {{{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()
{
    print STDERR "Usage: $0 [-c <credfile> -e <certfile>] [-s] <user>\n";
    exit(1);
}
my $optlist   = "c:se:n";
my $dosshkeys = 0;
my $impotent  = 0;
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;
use GeniCertificate;
use GeniCredential;
use GeniAuthority;
use Genixmlrpc;
use GeniResponse;
use GeniHRN;

# Protos
sub UpdateCredential();
sub UpdateSSHKeys();

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;
}
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();
}
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()
{
    #
    # 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);
    }
    else {
	$url = $certificate->url();
	$url =~ s/sa$/geni-ma/;
Leigh B Stoller's avatar
Leigh B Stoller committed
230
#	$url = "https://www.emulab.net:12369/protogeni/stoller/xmlrpc/geni-ma";
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
	$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) {
	fatal("Could not get keys: " . $response->output());
    }
    if (! (ref($response->value()) &&
	   exists($response->value()->{$user_urn}) &&
	   ref($response->value()->{$user_urn}))) {
	fatal("Returned keys do not look right");
    }
    my @keys = @{ $response->value()->{$user_urn} };
    if (!@keys) {
	fatal("No keys returned for user!");
    }
    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;
}
exit(0);