]>
Commit | Line | Data |
---|---|---|
1 | package PVE::API2::AccessControl; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use JSON; | |
7 | use MIME::Base64; | |
8 | ||
9 | use PVE::Exception qw(raise raise_perm_exc raise_param_exc); | |
10 | use PVE::SafeSyslog; | |
11 | use PVE::RPCEnvironment; | |
12 | use PVE::Cluster qw(cfs_read_file); | |
13 | use PVE::DataCenterConfig; | |
14 | use PVE::RESTHandler; | |
15 | use PVE::AccessControl; | |
16 | use PVE::JSONSchema qw(get_standard_option); | |
17 | use PVE::API2::Domains; | |
18 | use PVE::API2::User; | |
19 | use PVE::API2::Group; | |
20 | use PVE::API2::Role; | |
21 | use PVE::API2::ACL; | |
22 | use PVE::Auth::Plugin; | |
23 | use PVE::OTP; | |
24 | use PVE::Tools; | |
25 | ||
26 | my $u2f_available = 0; | |
27 | eval { | |
28 | require PVE::U2F; | |
29 | $u2f_available = 1; | |
30 | }; | |
31 | ||
32 | use base qw(PVE::RESTHandler); | |
33 | ||
34 | __PACKAGE__->register_method ({ | |
35 | subclass => "PVE::API2::User", | |
36 | path => 'users', | |
37 | }); | |
38 | ||
39 | __PACKAGE__->register_method ({ | |
40 | subclass => "PVE::API2::Group", | |
41 | path => 'groups', | |
42 | }); | |
43 | ||
44 | __PACKAGE__->register_method ({ | |
45 | subclass => "PVE::API2::Role", | |
46 | path => 'roles', | |
47 | }); | |
48 | ||
49 | __PACKAGE__->register_method ({ | |
50 | subclass => "PVE::API2::ACL", | |
51 | path => 'acl', | |
52 | }); | |
53 | ||
54 | __PACKAGE__->register_method ({ | |
55 | subclass => "PVE::API2::Domains", | |
56 | path => 'domains', | |
57 | }); | |
58 | ||
59 | __PACKAGE__->register_method ({ | |
60 | name => 'index', | |
61 | path => '', | |
62 | method => 'GET', | |
63 | description => "Directory index.", | |
64 | permissions => { | |
65 | user => 'all', | |
66 | }, | |
67 | parameters => { | |
68 | additionalProperties => 0, | |
69 | properties => {}, | |
70 | }, | |
71 | returns => { | |
72 | type => 'array', | |
73 | items => { | |
74 | type => "object", | |
75 | properties => { | |
76 | subdir => { type => 'string' }, | |
77 | }, | |
78 | }, | |
79 | links => [ { rel => 'child', href => "{subdir}" } ], | |
80 | }, | |
81 | code => sub { | |
82 | my ($param) = @_; | |
83 | ||
84 | my $res = []; | |
85 | ||
86 | my $ma = __PACKAGE__->method_attributes(); | |
87 | ||
88 | foreach my $info (@$ma) { | |
89 | next if !$info->{subclass}; | |
90 | ||
91 | my $subpath = $info->{match_re}->[0]; | |
92 | ||
93 | push @$res, { subdir => $subpath }; | |
94 | } | |
95 | ||
96 | push @$res, { subdir => 'ticket' }; | |
97 | push @$res, { subdir => 'password' }; | |
98 | ||
99 | return $res; | |
100 | }}); | |
101 | ||
102 | ||
103 | my $verify_auth = sub { | |
104 | my ($rpcenv, $username, $pw_or_ticket, $otp, $path, $privs) = @_; | |
105 | ||
106 | my $normpath = PVE::AccessControl::normalize_path($path); | |
107 | ||
108 | my $ticketuser; | |
109 | if (($ticketuser = PVE::AccessControl::verify_ticket($pw_or_ticket, 1)) && | |
110 | ($ticketuser eq $username)) { | |
111 | # valid ticket | |
112 | } elsif (PVE::AccessControl::verify_vnc_ticket($pw_or_ticket, $username, $normpath, 1)) { | |
113 | # valid vnc ticket | |
114 | } else { | |
115 | $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket, $otp); | |
116 | } | |
117 | ||
118 | my $privlist = [ PVE::Tools::split_list($privs) ]; | |
119 | if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) { | |
120 | die "no permission ($path, $privs)\n"; | |
121 | } | |
122 | ||
123 | return { username => $username }; | |
124 | }; | |
125 | ||
126 | my $create_ticket = sub { | |
127 | my ($rpcenv, $username, $pw_or_ticket, $otp) = @_; | |
128 | ||
129 | my ($ticketuser, undef, $tfa_info) = PVE::AccessControl::verify_ticket($pw_or_ticket, 1); | |
130 | if (defined($ticketuser) && ($ticketuser eq 'root@pam' || $ticketuser eq $username)) { | |
131 | if (defined($tfa_info)) { | |
132 | die "incomplete ticket\n"; | |
133 | } | |
134 | # valid ticket. Note: root@pam can create tickets for other users | |
135 | } else { | |
136 | ($username, $tfa_info) = PVE::AccessControl::authenticate_user($username, $pw_or_ticket, $otp); | |
137 | } | |
138 | ||
139 | my %extra; | |
140 | my $ticket_data = $username; | |
141 | if (defined($tfa_info)) { | |
142 | $extra{NeedTFA} = 1; | |
143 | if ($tfa_info->{type} eq 'u2f') { | |
144 | my $u2finfo = $tfa_info->{data}; | |
145 | my $u2f = get_u2f_instance($rpcenv, $u2finfo->@{qw(publicKey keyHandle)}); | |
146 | my $challenge = $u2f->auth_challenge() | |
147 | or die "failed to get u2f challenge\n"; | |
148 | $challenge = decode_json($challenge); | |
149 | $extra{U2FChallenge} = $challenge; | |
150 | $ticket_data = "u2f!$username!$challenge->{challenge}"; | |
151 | } else { | |
152 | # General half-login / 'missing 2nd factor' ticket: | |
153 | $ticket_data = "tfa!$username"; | |
154 | } | |
155 | } | |
156 | ||
157 | my $ticket = PVE::AccessControl::assemble_ticket($ticket_data); | |
158 | my $csrftoken = PVE::AccessControl::assemble_csrf_prevention_token($username); | |
159 | ||
160 | return { | |
161 | ticket => $ticket, | |
162 | username => $username, | |
163 | CSRFPreventionToken => $csrftoken, | |
164 | %extra, | |
165 | }; | |
166 | }; | |
167 | ||
168 | my $compute_api_permission = sub { | |
169 | my ($rpcenv, $authuser) = @_; | |
170 | ||
171 | my $usercfg = $rpcenv->{user_cfg}; | |
172 | ||
173 | my $res = {}; | |
174 | my $priv_re_map = { | |
175 | vms => qr/VM\.|Permissions\.Modify/, | |
176 | access => qr/(User|Group)\.|Permissions\.Modify/, | |
177 | storage => qr/Datastore\.|Permissions\.Modify/, | |
178 | nodes => qr/Sys\.|Permissions\.Modify/, | |
179 | dc => qr/Sys\.Audit/, | |
180 | }; | |
181 | map { $res->{$_} = {} } keys %$priv_re_map; | |
182 | ||
183 | my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage']; | |
184 | ||
185 | my $checked_paths = {}; | |
186 | foreach my $path (@$required_paths, keys %{$usercfg->{acl}}) { | |
187 | next if $checked_paths->{$path}; | |
188 | $checked_paths->{$path} = 1; | |
189 | ||
190 | my $path_perm = $rpcenv->permissions($authuser, $path); | |
191 | ||
192 | my $toplevel = ($path =~ /^\/(\w+)/) ? $1 : 'dc'; | |
193 | if ($toplevel eq 'pool') { | |
194 | foreach my $priv (keys %$path_perm) { | |
195 | if ($priv =~ m/^VM\./) { | |
196 | $res->{vms}->{$priv} = 1; | |
197 | } elsif ($priv =~ m/^Datastore\./) { | |
198 | $res->{storage}->{$priv} = 1; | |
199 | } elsif ($priv eq 'Permissions.Modify') { | |
200 | $res->{storage}->{$priv} = 1; | |
201 | $res->{vms}->{$priv} = 1; | |
202 | } | |
203 | } | |
204 | } else { | |
205 | my $priv_regex = $priv_re_map->{$toplevel} // next; | |
206 | foreach my $priv (keys %$path_perm) { | |
207 | next if $priv !~ m/^($priv_regex)/; | |
208 | $res->{$toplevel}->{$priv} = 1; | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
213 | return $res; | |
214 | }; | |
215 | ||
216 | __PACKAGE__->register_method ({ | |
217 | name => 'get_ticket', | |
218 | path => 'ticket', | |
219 | method => 'GET', | |
220 | permissions => { user => 'world' }, | |
221 | description => "Dummy. Useful for formatters which want to provide a login page.", | |
222 | parameters => { | |
223 | additionalProperties => 0, | |
224 | }, | |
225 | returns => { type => "null" }, | |
226 | code => sub { return undef; }}); | |
227 | ||
228 | __PACKAGE__->register_method ({ | |
229 | name => 'create_ticket', | |
230 | path => 'ticket', | |
231 | method => 'POST', | |
232 | permissions => { | |
233 | description => "You need to pass valid credientials.", | |
234 | user => 'world' | |
235 | }, | |
236 | protected => 1, # else we can't access shadow files | |
237 | allowtoken => 0, # we don't want tokens to create tickets | |
238 | description => "Create or verify authentication ticket.", | |
239 | parameters => { | |
240 | additionalProperties => 0, | |
241 | properties => { | |
242 | username => { | |
243 | description => "User name", | |
244 | type => 'string', | |
245 | maxLength => 64, | |
246 | completion => \&PVE::AccessControl::complete_username, | |
247 | }, | |
248 | realm => get_standard_option('realm', { | |
249 | description => "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.", | |
250 | optional => 1, | |
251 | completion => \&PVE::AccessControl::complete_realm, | |
252 | }), | |
253 | password => { | |
254 | description => "The secret password. This can also be a valid ticket.", | |
255 | type => 'string', | |
256 | }, | |
257 | otp => { | |
258 | description => "One-time password for Two-factor authentication.", | |
259 | type => 'string', | |
260 | optional => 1, | |
261 | }, | |
262 | path => { | |
263 | description => "Verify ticket, and check if user have access 'privs' on 'path'", | |
264 | type => 'string', | |
265 | requires => 'privs', | |
266 | optional => 1, | |
267 | maxLength => 64, | |
268 | }, | |
269 | privs => { | |
270 | description => "Verify ticket, and check if user have access 'privs' on 'path'", | |
271 | type => 'string' , format => 'pve-priv-list', | |
272 | requires => 'path', | |
273 | optional => 1, | |
274 | maxLength => 64, | |
275 | }, | |
276 | } | |
277 | }, | |
278 | returns => { | |
279 | type => "object", | |
280 | properties => { | |
281 | username => { type => 'string' }, | |
282 | ticket => { type => 'string', optional => 1}, | |
283 | CSRFPreventionToken => { type => 'string', optional => 1 }, | |
284 | clustername => { type => 'string', optional => 1 }, | |
285 | # cap => computed api permissions, unless there's a u2f challenge | |
286 | } | |
287 | }, | |
288 | code => sub { | |
289 | my ($param) = @_; | |
290 | ||
291 | my $username = $param->{username}; | |
292 | $username .= "\@$param->{realm}" if $param->{realm}; | |
293 | ||
294 | my $rpcenv = PVE::RPCEnvironment::get(); | |
295 | ||
296 | my $res; | |
297 | eval { | |
298 | # test if user exists and is enabled | |
299 | $rpcenv->check_user_enabled($username); | |
300 | ||
301 | if ($param->{path} && $param->{privs}) { | |
302 | $res = &$verify_auth($rpcenv, $username, $param->{password}, $param->{otp}, | |
303 | $param->{path}, $param->{privs}); | |
304 | } else { | |
305 | $res = &$create_ticket($rpcenv, $username, $param->{password}, $param->{otp}); | |
306 | } | |
307 | }; | |
308 | if (my $err = $@) { | |
309 | my $clientip = $rpcenv->get_client_ip() || ''; | |
310 | syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err"); | |
311 | # do not return any info to prevent user enumeration attacks | |
312 | die PVE::Exception->new("authentication failure\n", code => 401); | |
313 | } | |
314 | ||
315 | $res->{cap} = &$compute_api_permission($rpcenv, $username) | |
316 | if !defined($res->{NeedTFA}); | |
317 | ||
318 | my $clinfo = PVE::Cluster::get_clinfo(); | |
319 | if ($clinfo->{cluster}->{name} && $rpcenv->check($username, '/', ['Sys.Audit'], 1)) { | |
320 | $res->{clustername} = $clinfo->{cluster}->{name}; | |
321 | } | |
322 | ||
323 | PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'"); | |
324 | ||
325 | return $res; | |
326 | }}); | |
327 | ||
328 | __PACKAGE__->register_method ({ | |
329 | name => 'change_password', | |
330 | path => 'password', | |
331 | method => 'PUT', | |
332 | permissions => { | |
333 | description => "Each user is allowed to change his own password. A user can change the password of another user if he has 'Realm.AllocateUser' (on the realm of user <userid>) and 'User.Modify' permission on /access/groups/<group> on a group where user <userid> is member of.", | |
334 | check => [ 'or', | |
335 | ['userid-param', 'self'], | |
336 | [ 'and', | |
337 | [ 'userid-param', 'Realm.AllocateUser'], | |
338 | [ 'userid-group', ['User.Modify']] | |
339 | ] | |
340 | ], | |
341 | }, | |
342 | protected => 1, # else we can't access shadow files | |
343 | allowtoken => 0, # we don't want tokens to change the regular user password | |
344 | description => "Change user password.", | |
345 | parameters => { | |
346 | additionalProperties => 0, | |
347 | properties => { | |
348 | userid => get_standard_option('userid-completed'), | |
349 | password => { | |
350 | description => "The new password.", | |
351 | type => 'string', | |
352 | minLength => 5, | |
353 | maxLength => 64, | |
354 | }, | |
355 | } | |
356 | }, | |
357 | returns => { type => "null" }, | |
358 | code => sub { | |
359 | my ($param) = @_; | |
360 | ||
361 | my $rpcenv = PVE::RPCEnvironment::get(); | |
362 | my $authuser = $rpcenv->get_user(); | |
363 | ||
364 | my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
365 | ||
366 | $rpcenv->check_user_exist($userid); | |
367 | ||
368 | if ($authuser eq 'root@pam') { | |
369 | # OK - root can change anything | |
370 | } else { | |
371 | if ($authuser eq $userid) { | |
372 | $rpcenv->check_user_enabled($userid); | |
373 | # OK - each user can change its own password | |
374 | } else { | |
375 | # only root may change root password | |
376 | raise_perm_exc() if $userid eq 'root@pam'; | |
377 | # do not allow to change system user passwords | |
378 | raise_perm_exc() if $realm eq 'pam'; | |
379 | } | |
380 | } | |
381 | ||
382 | PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password}); | |
383 | ||
384 | PVE::Cluster::log_msg('info', 'root@pam', "changed password for user '$userid'"); | |
385 | ||
386 | return undef; | |
387 | }}); | |
388 | ||
389 | sub get_u2f_config() { | |
390 | die "u2f support not available\n" if !$u2f_available; | |
391 | ||
392 | my $dc = cfs_read_file('datacenter.cfg'); | |
393 | my $u2f = $dc->{u2f}; | |
394 | die "u2f not configured in datacenter.cfg\n" if !$u2f; | |
395 | return $u2f; | |
396 | } | |
397 | ||
398 | sub get_u2f_instance { | |
399 | my ($rpcenv, $publicKey, $keyHandle) = @_; | |
400 | ||
401 | # We store the public key base64 encoded (as the api provides it in binary) | |
402 | $publicKey = decode_base64($publicKey) if defined($publicKey); | |
403 | ||
404 | my $u2fconfig = get_u2f_config(); | |
405 | my $u2f = PVE::U2F->new(); | |
406 | ||
407 | # via the 'Host' header (in case a node has multiple hosts available). | |
408 | my $origin = $u2fconfig->{origin}; | |
409 | if (!defined($origin)) { | |
410 | $origin = $rpcenv->get_request_host(1); | |
411 | if ($origin) { | |
412 | $origin = "https://$origin"; | |
413 | } else { | |
414 | die "failed to figure out u2f origin\n"; | |
415 | } | |
416 | } | |
417 | ||
418 | my $appid = $u2fconfig->{appid} // $origin; | |
419 | $u2f->set_appid($appid); | |
420 | $u2f->set_origin($origin); | |
421 | $u2f->set_publicKey($publicKey) if defined($publicKey); | |
422 | $u2f->set_keyHandle($keyHandle) if defined($keyHandle); | |
423 | return $u2f; | |
424 | } | |
425 | ||
426 | sub verify_user_tfa_config { | |
427 | my ($type, $tfa_cfg, $value) = @_; | |
428 | ||
429 | if (!defined($type)) { | |
430 | die "missing tfa 'type'\n"; | |
431 | } | |
432 | ||
433 | if ($type ne 'oath') { | |
434 | die "invalid type for custom tfa authentication\n"; | |
435 | } | |
436 | ||
437 | my $secret = $tfa_cfg->{keys} | |
438 | or die "missing TOTP secret\n"; | |
439 | $tfa_cfg = $tfa_cfg->{config}; | |
440 | # Copy the hash to verify that we have no unexpected keys without modifying the original hash. | |
441 | $tfa_cfg = {%$tfa_cfg}; | |
442 | ||
443 | # We can only verify 1 secret but oath_verify_otp allows multiple: | |
444 | if (scalar(PVE::Tools::split_list($secret)) != 1) { | |
445 | die "only exactly one secret key allowed\n"; | |
446 | } | |
447 | ||
448 | my $digits = delete($tfa_cfg->{digits}) // 6; | |
449 | my $step = delete($tfa_cfg->{step}) // 30; | |
450 | # Maybe also this? | |
451 | # my $algorithm = delete($tfa_cfg->{algorithm}) // 'sha1'; | |
452 | ||
453 | if (length(my $more = join(', ', keys %$tfa_cfg))) { | |
454 | die "unexpected tfa config keys: $more\n"; | |
455 | } | |
456 | ||
457 | PVE::OTP::oath_verify_otp($value, $secret, $step, $digits); | |
458 | } | |
459 | ||
460 | __PACKAGE__->register_method ({ | |
461 | name => 'change_tfa', | |
462 | path => 'tfa', | |
463 | method => 'PUT', | |
464 | permissions => { | |
465 | description => 'A user can change their own u2f or totp token.', | |
466 | check => [ 'or', | |
467 | ['userid-param', 'self'], | |
468 | [ 'and', | |
469 | [ 'userid-param', 'Realm.AllocateUser'], | |
470 | [ 'userid-group', ['User.Modify']] | |
471 | ] | |
472 | ], | |
473 | }, | |
474 | protected => 1, # else we can't access shadow files | |
475 | allowtoken => 0, # we don't want tokens to change the regular user's TFA settings | |
476 | description => "Change user u2f authentication.", | |
477 | parameters => { | |
478 | additionalProperties => 0, | |
479 | properties => { | |
480 | userid => get_standard_option('userid', { | |
481 | completion => \&PVE::AccessControl::complete_username, | |
482 | }), | |
483 | password => { | |
484 | optional => 1, # Only required if not root@pam | |
485 | description => "The current password.", | |
486 | type => 'string', | |
487 | minLength => 5, | |
488 | maxLength => 64, | |
489 | }, | |
490 | action => { | |
491 | description => 'The action to perform', | |
492 | type => 'string', | |
493 | enum => [qw(delete new confirm)], | |
494 | }, | |
495 | response => { | |
496 | optional => 1, | |
497 | description => | |
498 | 'Either the the response to the current u2f registration challenge,' | |
499 | .' or, when adding TOTP, the currently valid TOTP value.', | |
500 | type => 'string', | |
501 | }, | |
502 | key => { | |
503 | optional => 1, | |
504 | description => 'When adding TOTP, the shared secret value.', | |
505 | type => 'string', | |
506 | format => 'pve-tfa-secret', | |
507 | }, | |
508 | config => { | |
509 | optional => 1, | |
510 | description => 'A TFA configuration. This must currently be of type TOTP of not set at all.', | |
511 | type => 'string', | |
512 | format => 'pve-tfa-config', | |
513 | maxLength => 128, | |
514 | }, | |
515 | } | |
516 | }, | |
517 | returns => { type => 'object' }, | |
518 | code => sub { | |
519 | my ($param) = @_; | |
520 | ||
521 | my $rpcenv = PVE::RPCEnvironment::get(); | |
522 | my $authuser = $rpcenv->get_user(); | |
523 | ||
524 | my $action = delete $param->{action}; | |
525 | my $response = delete $param->{response}; | |
526 | my $password = delete($param->{password}) // ''; | |
527 | my $key = delete($param->{key}); | |
528 | my $config = delete($param->{config}); | |
529 | ||
530 | my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
531 | $rpcenv->check_user_exist($userid); | |
532 | ||
533 | # Only root may modify root | |
534 | raise_perm_exc() if $userid eq 'root@pam' && $authuser ne 'root@pam'; | |
535 | ||
536 | # Regular users need to confirm their password to change u2f settings. | |
537 | if ($authuser ne 'root@pam') { | |
538 | raise_param_exc({ 'password' => 'password is required to modify u2f data' }) | |
539 | if !defined($password); | |
540 | my $domain_cfg = cfs_read_file('domains.cfg'); | |
541 | my $cfg = $domain_cfg->{ids}->{$realm}; | |
542 | die "auth domain '$realm' does not exist\n" if !$cfg; | |
543 | my $plugin = PVE::Auth::Plugin->lookup($cfg->{type}); | |
544 | $plugin->authenticate_user($cfg, $realm, $ruid, $password); | |
545 | } | |
546 | ||
547 | if ($action eq 'delete') { | |
548 | PVE::AccessControl::user_set_tfa($userid, $realm, undef, undef); | |
549 | PVE::Cluster::log_msg('info', $authuser, "deleted u2f data for user '$userid'"); | |
550 | } elsif ($action eq 'new') { | |
551 | if (defined($config)) { | |
552 | $config = PVE::Auth::Plugin::parse_tfa_config($config); | |
553 | my $type = delete($config->{type}); | |
554 | my $tfa_cfg = { | |
555 | keys => $key, | |
556 | config => $config, | |
557 | }; | |
558 | verify_user_tfa_config($type, $tfa_cfg, $response); | |
559 | PVE::AccessControl::user_set_tfa($userid, $realm, $type, $tfa_cfg); | |
560 | } else { | |
561 | # The default is U2F: | |
562 | my $u2f = get_u2f_instance($rpcenv); | |
563 | my $challenge = $u2f->registration_challenge() | |
564 | or raise("failed to get u2f challenge"); | |
565 | $challenge = decode_json($challenge); | |
566 | PVE::AccessControl::user_set_tfa($userid, $realm, 'u2f', $challenge); | |
567 | return $challenge; | |
568 | } | |
569 | } elsif ($action eq 'confirm') { | |
570 | raise_param_exc({ 'response' => "confirm action requires the 'response' parameter to be set" }) | |
571 | if !defined($response); | |
572 | ||
573 | my ($type, $u2fdata) = PVE::AccessControl::user_get_tfa($userid, $realm); | |
574 | raise("no u2f data available") | |
575 | if (!defined($type) || $type ne 'u2f'); | |
576 | ||
577 | my $challenge = $u2fdata->{challenge} | |
578 | or raise("no active challenge"); | |
579 | ||
580 | my $u2f = get_u2f_instance($rpcenv); | |
581 | $u2f->set_challenge($challenge); | |
582 | my ($keyHandle, $publicKey) = $u2f->registration_verify($response); | |
583 | PVE::AccessControl::user_set_tfa($userid, $realm, 'u2f', { | |
584 | keyHandle => $keyHandle, | |
585 | publicKey => $publicKey, # already base64 encoded | |
586 | }); | |
587 | } else { | |
588 | die "invalid action: $action\n"; | |
589 | } | |
590 | ||
591 | return {}; | |
592 | }}); | |
593 | ||
594 | __PACKAGE__->register_method({ | |
595 | name => 'verify_tfa', | |
596 | path => 'tfa', | |
597 | method => 'POST', | |
598 | permissions => { user => 'all' }, | |
599 | protected => 1, # else we can't access shadow files | |
600 | allowtoken => 0, # we don't want tokens to access TFA information | |
601 | description => 'Finish a u2f challenge.', | |
602 | parameters => { | |
603 | additionalProperties => 0, | |
604 | properties => { | |
605 | response => { | |
606 | type => 'string', | |
607 | description => 'The response to the current authentication challenge.', | |
608 | }, | |
609 | } | |
610 | }, | |
611 | returns => { | |
612 | type => 'object', | |
613 | properties => { | |
614 | ticket => { type => 'string' }, | |
615 | # cap | |
616 | } | |
617 | }, | |
618 | code => sub { | |
619 | my ($param) = @_; | |
620 | ||
621 | my $rpcenv = PVE::RPCEnvironment::get(); | |
622 | my $authuser = $rpcenv->get_user(); | |
623 | my ($username, undef, $realm) = PVE::AccessControl::verify_username($authuser); | |
624 | ||
625 | my ($tfa_type, $tfa_data) = PVE::AccessControl::user_get_tfa($username, $realm); | |
626 | if (!defined($tfa_type)) { | |
627 | raise('no u2f data available'); | |
628 | } | |
629 | ||
630 | eval { | |
631 | if ($tfa_type eq 'u2f') { | |
632 | my $challenge = $rpcenv->get_u2f_challenge() | |
633 | or raise('no active challenge'); | |
634 | ||
635 | my $keyHandle = $tfa_data->{keyHandle}; | |
636 | my $publicKey = $tfa_data->{publicKey}; | |
637 | raise("incomplete u2f setup") | |
638 | if !defined($keyHandle) || !defined($publicKey); | |
639 | ||
640 | my $u2f = get_u2f_instance($rpcenv, $publicKey, $keyHandle); | |
641 | $u2f->set_challenge($challenge); | |
642 | ||
643 | my ($counter, $present) = $u2f->auth_verify($param->{response}); | |
644 | # Do we want to do anything with these? | |
645 | } else { | |
646 | # sanity check before handing off to the verification code: | |
647 | my $keys = $tfa_data->{keys} or die "missing tfa keys\n"; | |
648 | my $config = $tfa_data->{config} or die "bad tfa entry\n"; | |
649 | PVE::AccessControl::verify_one_time_pw($tfa_type, $authuser, $keys, $config, $param->{response}); | |
650 | } | |
651 | }; | |
652 | if (my $err = $@) { | |
653 | my $clientip = $rpcenv->get_client_ip() || ''; | |
654 | syslog('err', "authentication verification failure; rhost=$clientip user=$authuser msg=$err"); | |
655 | die PVE::Exception->new("authentication failure\n", code => 401); | |
656 | } | |
657 | ||
658 | return { | |
659 | ticket => PVE::AccessControl::assemble_ticket($authuser), | |
660 | cap => &$compute_api_permission($rpcenv, $authuser), | |
661 | } | |
662 | }}); | |
663 | ||
664 | __PACKAGE__->register_method({ | |
665 | name => 'permissions', | |
666 | path => 'permissions', | |
667 | method => 'GET', | |
668 | description => 'Retrieve effective permissions of given user/token.', | |
669 | permissions => { | |
670 | description => "Each user/token is allowed to dump their own permissions. A user can dump the permissions of another user if they have 'Sys.Audit' permission on /access.", | |
671 | user => 'all', | |
672 | }, | |
673 | parameters => { | |
674 | additionalProperties => 0, | |
675 | properties => { | |
676 | userid => { | |
677 | type => 'string', | |
678 | description => "User ID or full API token ID", | |
679 | pattern => $PVE::AccessControl::userid_or_token_regex, | |
680 | optional => 1, | |
681 | }, | |
682 | path => get_standard_option('acl-path', { | |
683 | description => "Only dump this specific path, not the whole tree.", | |
684 | optional => 1, | |
685 | }), | |
686 | }, | |
687 | }, | |
688 | returns => { | |
689 | type => 'object', | |
690 | description => 'Map of "path" => (Map of "privilege" => "propagate boolean").', | |
691 | }, | |
692 | code => sub { | |
693 | my ($param) = @_; | |
694 | ||
695 | my $rpcenv = PVE::RPCEnvironment::get(); | |
696 | ||
697 | my $userid = $param->{userid}; | |
698 | if (defined($userid)) { | |
699 | $rpcenv->check($rpcenv->get_user(), '/access', ['Sys.Audit']); | |
700 | } else { | |
701 | $userid = $rpcenv->get_user(); | |
702 | } | |
703 | ||
704 | my $res; | |
705 | ||
706 | if (my $path = $param->{path}) { | |
707 | my $perms = $rpcenv->permissions($userid, $path); | |
708 | if ($perms) { | |
709 | $res = { $path => $perms }; | |
710 | } else { | |
711 | $res = {}; | |
712 | } | |
713 | } else { | |
714 | $res = $rpcenv->get_effective_permissions($userid); | |
715 | } | |
716 | ||
717 | return $res; | |
718 | }}); | |
719 | ||
720 | 1; |