]>
Commit | Line | Data |
---|---|---|
2c3a6c0a DM |
1 | package PVE::API2::AccessControl; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
2b4c98ab WB |
6 | use JSON; |
7 | use MIME::Base64; | |
8 | ||
e240695b | 9 | use PVE::Exception qw(raise raise_perm_exc raise_param_exc); |
2c3a6c0a DM |
10 | use PVE::SafeSyslog; |
11 | use PVE::RPCEnvironment; | |
a427cecb | 12 | use PVE::Cluster qw(cfs_read_file); |
158514a8 | 13 | use PVE::DataCenterConfig; |
2c3a6c0a DM |
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; | |
8967f86f | 22 | use PVE::Auth::Plugin; |
47d731c7 WB |
23 | use PVE::OTP; |
24 | use PVE::Tools; | |
2c3a6c0a | 25 | |
2b4c98ab WB |
26 | my $u2f_available = 0; |
27 | eval { | |
28 | require PVE::U2F; | |
29 | $u2f_available = 1; | |
30 | }; | |
31 | ||
2c3a6c0a DM |
32 | use 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 | |
103 | my $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 | ||
126 | my $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 |
168 | my $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/, |
b920d59c | 179 | sdn => qr/SDN\.|Permissions\.Modify/, |
a1281512 | 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 | ||
eb41d200 | 295 | $username = PVE::AccessControl::lookup_username($username); |
2c3a6c0a | 296 | my $rpcenv = PVE::RPCEnvironment::get(); |
2c3a6c0a | 297 | |
adf8d771 | 298 | my $res; |
adf8d771 | 299 | eval { |
7070c1ae DM |
300 | # test if user exists and is enabled |
301 | $rpcenv->check_user_enabled($username); | |
302 | ||
2c3a6c0a | 303 | if ($param->{path} && $param->{privs}) { |
96f8ebd6 | 304 | $res = &$verify_auth($rpcenv, $username, $param->{password}, $param->{otp}, |
adf8d771 | 305 | $param->{path}, $param->{privs}); |
2c3a6c0a | 306 | } else { |
96f8ebd6 | 307 | $res = &$create_ticket($rpcenv, $username, $param->{password}, $param->{otp}); |
2c3a6c0a | 308 | } |
2c3a6c0a DM |
309 | }; |
310 | if (my $err = $@) { | |
adf8d771 | 311 | my $clientip = $rpcenv->get_client_ip() || ''; |
2c3a6c0a | 312 | syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err"); |
6126ab75 | 313 | # do not return any info to prevent user enumeration attacks |
fe2defd9 | 314 | die PVE::Exception->new("authentication failure\n", code => 401); |
2c3a6c0a DM |
315 | } |
316 | ||
18f8ba18 | 317 | $res->{cap} = &$compute_api_permission($rpcenv, $username) |
f25628d3 | 318 | if !defined($res->{NeedTFA}); |
dd2cfee0 | 319 | |
0fb0c62d FG |
320 | my $clinfo = PVE::Cluster::get_clinfo(); |
321 | if ($clinfo->{cluster}->{name} && $rpcenv->check($username, '/', ['Sys.Audit'], 1)) { | |
322 | $res->{clustername} = $clinfo->{cluster}->{name}; | |
e842fec5 TL |
323 | } |
324 | ||
2c3a6c0a DM |
325 | PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'"); |
326 | ||
adf8d771 | 327 | return $res; |
2c3a6c0a DM |
328 | }}); |
329 | ||
37d45deb | 330 | __PACKAGE__->register_method ({ |
765305e2 | 331 | name => 'change_password', |
30dd4869 | 332 | path => 'password', |
37d45deb | 333 | method => 'PUT', |
30dd4869 | 334 | permissions => { |
82b63965 | 335 | 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 | 336 | check => [ 'or', |
12683df7 | 337 | ['userid-param', 'self'], |
82b63965 DM |
338 | [ 'and', |
339 | [ 'userid-param', 'Realm.AllocateUser'], | |
340 | [ 'userid-group', ['User.Modify']] | |
341 | ] | |
12683df7 DM |
342 | ], |
343 | }, | |
37d45deb | 344 | protected => 1, # else we can't access shadow files |
49372390 | 345 | allowtoken => 0, # we don't want tokens to change the regular user password |
37d45deb DM |
346 | description => "Change user password.", |
347 | parameters => { | |
348 | additionalProperties => 0, | |
349 | properties => { | |
3a5ae7a0 | 350 | userid => get_standard_option('userid-completed'), |
30dd4869 | 351 | password => { |
37d45deb DM |
352 | description => "The new password.", |
353 | type => 'string', | |
30dd4869 | 354 | minLength => 5, |
37d45deb DM |
355 | maxLength => 64, |
356 | }, | |
357 | } | |
358 | }, | |
359 | returns => { type => "null" }, | |
360 | code => sub { | |
361 | my ($param) = @_; | |
362 | ||
363 | my $rpcenv = PVE::RPCEnvironment::get(); | |
364 | my $authuser = $rpcenv->get_user(); | |
365 | ||
366 | my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
367 | ||
12683df7 | 368 | $rpcenv->check_user_exist($userid); |
37d45deb DM |
369 | |
370 | if ($authuser eq 'root@pam') { | |
371 | # OK - root can change anything | |
372 | } else { | |
373 | if ($authuser eq $userid) { | |
374 | $rpcenv->check_user_enabled($userid); | |
375 | # OK - each user can change its own password | |
376 | } else { | |
12683df7 | 377 | # only root may change root password |
37d45deb | 378 | raise_perm_exc() if $userid eq 'root@pam'; |
59321f26 DM |
379 | # do not allow to change system user passwords |
380 | raise_perm_exc() if $realm eq 'pam'; | |
37d45deb DM |
381 | } |
382 | } | |
383 | ||
384 | PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password}); | |
385 | ||
386 | PVE::Cluster::log_msg('info', 'root@pam', "changed password for user '$userid'"); | |
387 | ||
388 | return undef; | |
389 | }}); | |
390 | ||
2b4c98ab WB |
391 | sub get_u2f_config() { |
392 | die "u2f support not available\n" if !$u2f_available; | |
393 | ||
394 | my $dc = cfs_read_file('datacenter.cfg'); | |
395 | my $u2f = $dc->{u2f}; | |
396 | die "u2f not configured in datacenter.cfg\n" if !$u2f; | |
2b4c98ab WB |
397 | return $u2f; |
398 | } | |
399 | ||
400 | sub get_u2f_instance { | |
401 | my ($rpcenv, $publicKey, $keyHandle) = @_; | |
402 | ||
403 | # We store the public key base64 encoded (as the api provides it in binary) | |
404 | $publicKey = decode_base64($publicKey) if defined($publicKey); | |
405 | ||
406 | my $u2fconfig = get_u2f_config(); | |
407 | my $u2f = PVE::U2F->new(); | |
408 | ||
409 | # via the 'Host' header (in case a node has multiple hosts available). | |
410 | my $origin = $u2fconfig->{origin}; | |
411 | if (!defined($origin)) { | |
412 | $origin = $rpcenv->get_request_host(1); | |
413 | if ($origin) { | |
414 | $origin = "https://$origin"; | |
415 | } else { | |
416 | die "failed to figure out u2f origin\n"; | |
417 | } | |
418 | } | |
419 | ||
420 | my $appid = $u2fconfig->{appid} // $origin; | |
421 | $u2f->set_appid($appid); | |
422 | $u2f->set_origin($origin); | |
423 | $u2f->set_publicKey($publicKey) if defined($publicKey); | |
424 | $u2f->set_keyHandle($keyHandle) if defined($keyHandle); | |
425 | return $u2f; | |
426 | } | |
427 | ||
47d731c7 WB |
428 | sub verify_user_tfa_config { |
429 | my ($type, $tfa_cfg, $value) = @_; | |
430 | ||
431 | if (!defined($type)) { | |
432 | die "missing tfa 'type'\n"; | |
433 | } | |
434 | ||
435 | if ($type ne 'oath') { | |
436 | die "invalid type for custom tfa authentication\n"; | |
437 | } | |
438 | ||
439 | my $secret = $tfa_cfg->{keys} | |
440 | or die "missing TOTP secret\n"; | |
441 | $tfa_cfg = $tfa_cfg->{config}; | |
442 | # Copy the hash to verify that we have no unexpected keys without modifying the original hash. | |
443 | $tfa_cfg = {%$tfa_cfg}; | |
444 | ||
445 | # We can only verify 1 secret but oath_verify_otp allows multiple: | |
446 | if (scalar(PVE::Tools::split_list($secret)) != 1) { | |
447 | die "only exactly one secret key allowed\n"; | |
448 | } | |
449 | ||
450 | my $digits = delete($tfa_cfg->{digits}) // 6; | |
451 | my $step = delete($tfa_cfg->{step}) // 30; | |
452 | # Maybe also this? | |
453 | # my $algorithm = delete($tfa_cfg->{algorithm}) // 'sha1'; | |
454 | ||
455 | if (length(my $more = join(', ', keys %$tfa_cfg))) { | |
456 | die "unexpected tfa config keys: $more\n"; | |
457 | } | |
458 | ||
459 | PVE::OTP::oath_verify_otp($value, $secret, $step, $digits); | |
460 | } | |
461 | ||
2b4c98ab WB |
462 | __PACKAGE__->register_method ({ |
463 | name => 'change_tfa', | |
464 | path => 'tfa', | |
465 | method => 'PUT', | |
466 | permissions => { | |
47d731c7 | 467 | description => 'A user can change their own u2f or totp token.', |
2b4c98ab WB |
468 | check => [ 'or', |
469 | ['userid-param', 'self'], | |
470 | [ 'and', | |
471 | [ 'userid-param', 'Realm.AllocateUser'], | |
472 | [ 'userid-group', ['User.Modify']] | |
473 | ] | |
474 | ], | |
475 | }, | |
476 | protected => 1, # else we can't access shadow files | |
49372390 | 477 | allowtoken => 0, # we don't want tokens to change the regular user's TFA settings |
2b4c98ab WB |
478 | description => "Change user u2f authentication.", |
479 | parameters => { | |
480 | additionalProperties => 0, | |
481 | properties => { | |
482 | userid => get_standard_option('userid', { | |
483 | completion => \&PVE::AccessControl::complete_username, | |
484 | }), | |
485 | password => { | |
486 | optional => 1, # Only required if not root@pam | |
487 | description => "The current password.", | |
488 | type => 'string', | |
489 | minLength => 5, | |
490 | maxLength => 64, | |
491 | }, | |
492 | action => { | |
493 | description => 'The action to perform', | |
494 | type => 'string', | |
495 | enum => [qw(delete new confirm)], | |
496 | }, | |
497 | response => { | |
498 | optional => 1, | |
47d731c7 WB |
499 | description => |
500 | 'Either the the response to the current u2f registration challenge,' | |
501 | .' or, when adding TOTP, the currently valid TOTP value.', | |
502 | type => 'string', | |
503 | }, | |
504 | key => { | |
505 | optional => 1, | |
506 | description => 'When adding TOTP, the shared secret value.', | |
2b4c98ab | 507 | type => 'string', |
0bf114df | 508 | format => 'pve-tfa-secret', |
47d731c7 WB |
509 | }, |
510 | config => { | |
511 | optional => 1, | |
512 | description => 'A TFA configuration. This must currently be of type TOTP of not set at all.', | |
513 | type => 'string', | |
514 | format => 'pve-tfa-config', | |
515 | maxLength => 128, | |
2b4c98ab WB |
516 | }, |
517 | } | |
518 | }, | |
519 | returns => { type => 'object' }, | |
520 | code => sub { | |
521 | my ($param) = @_; | |
522 | ||
523 | my $rpcenv = PVE::RPCEnvironment::get(); | |
524 | my $authuser = $rpcenv->get_user(); | |
525 | ||
526 | my $action = delete $param->{action}; | |
527 | my $response = delete $param->{response}; | |
528 | my $password = delete($param->{password}) // ''; | |
47d731c7 WB |
529 | my $key = delete($param->{key}); |
530 | my $config = delete($param->{config}); | |
2b4c98ab WB |
531 | |
532 | my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
533 | $rpcenv->check_user_exist($userid); | |
534 | ||
535 | # Only root may modify root | |
536 | raise_perm_exc() if $userid eq 'root@pam' && $authuser ne 'root@pam'; | |
537 | ||
538 | # Regular users need to confirm their password to change u2f settings. | |
539 | if ($authuser ne 'root@pam') { | |
e240695b | 540 | raise_param_exc({ 'password' => 'password is required to modify u2f data' }) |
2b4c98ab WB |
541 | if !defined($password); |
542 | my $domain_cfg = cfs_read_file('domains.cfg'); | |
543 | my $cfg = $domain_cfg->{ids}->{$realm}; | |
3443faca | 544 | die "auth domain '$realm' does not exist\n" if !$cfg; |
2b4c98ab WB |
545 | my $plugin = PVE::Auth::Plugin->lookup($cfg->{type}); |
546 | $plugin->authenticate_user($cfg, $realm, $ruid, $password); | |
547 | } | |
548 | ||
549 | if ($action eq 'delete') { | |
550 | PVE::AccessControl::user_set_tfa($userid, $realm, undef, undef); | |
551 | PVE::Cluster::log_msg('info', $authuser, "deleted u2f data for user '$userid'"); | |
552 | } elsif ($action eq 'new') { | |
47d731c7 WB |
553 | if (defined($config)) { |
554 | $config = PVE::Auth::Plugin::parse_tfa_config($config); | |
555 | my $type = delete($config->{type}); | |
556 | my $tfa_cfg = { | |
557 | keys => $key, | |
558 | config => $config, | |
559 | }; | |
560 | verify_user_tfa_config($type, $tfa_cfg, $response); | |
561 | PVE::AccessControl::user_set_tfa($userid, $realm, $type, $tfa_cfg); | |
562 | } else { | |
563 | # The default is U2F: | |
564 | my $u2f = get_u2f_instance($rpcenv); | |
565 | my $challenge = $u2f->registration_challenge() | |
566 | or raise("failed to get u2f challenge"); | |
567 | $challenge = decode_json($challenge); | |
568 | PVE::AccessControl::user_set_tfa($userid, $realm, 'u2f', $challenge); | |
569 | return $challenge; | |
570 | } | |
2b4c98ab | 571 | } elsif ($action eq 'confirm') { |
e240695b | 572 | raise_param_exc({ 'response' => "confirm action requires the 'response' parameter to be set" }) |
2b4c98ab WB |
573 | if !defined($response); |
574 | ||
575 | my ($type, $u2fdata) = PVE::AccessControl::user_get_tfa($userid, $realm); | |
576 | raise("no u2f data available") | |
577 | if (!defined($type) || $type ne 'u2f'); | |
578 | ||
579 | my $challenge = $u2fdata->{challenge} | |
580 | or raise("no active challenge"); | |
581 | ||
582 | my $u2f = get_u2f_instance($rpcenv); | |
583 | $u2f->set_challenge($challenge); | |
584 | my ($keyHandle, $publicKey) = $u2f->registration_verify($response); | |
585 | PVE::AccessControl::user_set_tfa($userid, $realm, 'u2f', { | |
586 | keyHandle => $keyHandle, | |
eb25cbaf | 587 | publicKey => $publicKey, # already base64 encoded |
2b4c98ab WB |
588 | }); |
589 | } else { | |
590 | die "invalid action: $action\n"; | |
591 | } | |
592 | ||
593 | return {}; | |
594 | }}); | |
595 | ||
596 | __PACKAGE__->register_method({ | |
597 | name => 'verify_tfa', | |
598 | path => 'tfa', | |
599 | method => 'POST', | |
600 | permissions => { user => 'all' }, | |
601 | protected => 1, # else we can't access shadow files | |
49372390 | 602 | allowtoken => 0, # we don't want tokens to access TFA information |
2b4c98ab WB |
603 | description => 'Finish a u2f challenge.', |
604 | parameters => { | |
605 | additionalProperties => 0, | |
606 | properties => { | |
607 | response => { | |
608 | type => 'string', | |
609 | description => 'The response to the current authentication challenge.', | |
610 | }, | |
611 | } | |
612 | }, | |
613 | returns => { | |
614 | type => 'object', | |
615 | properties => { | |
616 | ticket => { type => 'string' }, | |
617 | # cap | |
618 | } | |
619 | }, | |
620 | code => sub { | |
621 | my ($param) = @_; | |
622 | ||
623 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2b4c98ab WB |
624 | my $authuser = $rpcenv->get_user(); |
625 | my ($username, undef, $realm) = PVE::AccessControl::verify_username($authuser); | |
626 | ||
f25628d3 WB |
627 | my ($tfa_type, $tfa_data) = PVE::AccessControl::user_get_tfa($username, $realm); |
628 | if (!defined($tfa_type)) { | |
2b4c98ab WB |
629 | raise('no u2f data available'); |
630 | } | |
631 | ||
f25628d3 WB |
632 | eval { |
633 | if ($tfa_type eq 'u2f') { | |
634 | my $challenge = $rpcenv->get_u2f_challenge() | |
635 | or raise('no active challenge'); | |
2b4c98ab | 636 | |
f25628d3 WB |
637 | my $keyHandle = $tfa_data->{keyHandle}; |
638 | my $publicKey = $tfa_data->{publicKey}; | |
639 | raise("incomplete u2f setup") | |
640 | if !defined($keyHandle) || !defined($publicKey); | |
2b4c98ab | 641 | |
f25628d3 WB |
642 | my $u2f = get_u2f_instance($rpcenv, $publicKey, $keyHandle); |
643 | $u2f->set_challenge($challenge); | |
644 | ||
645 | my ($counter, $present) = $u2f->auth_verify($param->{response}); | |
646 | # Do we want to do anything with these? | |
647 | } else { | |
648 | # sanity check before handing off to the verification code: | |
649 | my $keys = $tfa_data->{keys} or die "missing tfa keys\n"; | |
650 | my $config = $tfa_data->{config} or die "bad tfa entry\n"; | |
651 | PVE::AccessControl::verify_one_time_pw($tfa_type, $authuser, $keys, $config, $param->{response}); | |
652 | } | |
2b4c98ab WB |
653 | }; |
654 | if (my $err = $@) { | |
655 | my $clientip = $rpcenv->get_client_ip() || ''; | |
656 | syslog('err', "authentication verification failure; rhost=$clientip user=$authuser msg=$err"); | |
657 | die PVE::Exception->new("authentication failure\n", code => 401); | |
658 | } | |
659 | ||
2b4c98ab | 660 | return { |
f25628d3 | 661 | ticket => PVE::AccessControl::assemble_ticket($authuser), |
2b4c98ab WB |
662 | cap => &$compute_api_permission($rpcenv, $authuser), |
663 | } | |
664 | }}); | |
665 | ||
c3fa8a36 FG |
666 | __PACKAGE__->register_method({ |
667 | name => 'permissions', | |
668 | path => 'permissions', | |
669 | method => 'GET', | |
670 | description => 'Retrieve effective permissions of given user/token.', | |
671 | permissions => { | |
672 | 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.", | |
673 | user => 'all', | |
674 | }, | |
675 | parameters => { | |
676 | additionalProperties => 0, | |
677 | properties => { | |
678 | userid => { | |
679 | type => 'string', | |
680 | description => "User ID or full API token ID", | |
681 | pattern => $PVE::AccessControl::userid_or_token_regex, | |
682 | optional => 1, | |
683 | }, | |
684 | path => get_standard_option('acl-path', { | |
685 | description => "Only dump this specific path, not the whole tree.", | |
686 | optional => 1, | |
687 | }), | |
688 | }, | |
689 | }, | |
690 | returns => { | |
691 | type => 'object', | |
692 | description => 'Map of "path" => (Map of "privilege" => "propagate boolean").', | |
693 | }, | |
694 | code => sub { | |
695 | my ($param) = @_; | |
696 | ||
697 | my $rpcenv = PVE::RPCEnvironment::get(); | |
698 | ||
699 | my $userid = $param->{userid}; | |
700 | if (defined($userid)) { | |
701 | $rpcenv->check($rpcenv->get_user(), '/access', ['Sys.Audit']); | |
702 | } else { | |
703 | $userid = $rpcenv->get_user(); | |
704 | } | |
705 | ||
706 | my $res; | |
707 | ||
708 | if (my $path = $param->{path}) { | |
709 | my $perms = $rpcenv->permissions($userid, $path); | |
710 | if ($perms) { | |
711 | $res = { $path => $perms }; | |
712 | } else { | |
713 | $res = {}; | |
714 | } | |
715 | } else { | |
716 | $res = $rpcenv->get_effective_permissions($userid); | |
717 | } | |
718 | ||
719 | return $res; | |
720 | }}); | |
721 | ||
2c3a6c0a | 722 | 1; |