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