]>
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 ({ | |
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.", | |
82b63965 DM |
64 | permissions => { |
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) = @_; | |
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' }; | |
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 TL |
178 | nodes => qr/Sys\.|Permissions\.Modify/, |
179 | dc => qr/Sys\.Audit/, | |
dd2cfee0 | 180 | }; |
a2c18811 TL |
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 | } | |
dd2cfee0 DM |
210 | } |
211 | } | |
212 | ||
dd2cfee0 DM |
213 | return $res; |
214 | }; | |
215 | ||
39e4e363 DM |
216 | __PACKAGE__->register_method ({ |
217 | name => 'get_ticket', | |
218 | path => 'ticket', | |
219 | method => 'GET', | |
220 | permissions => { user => 'world' }, | |
36dd9dbd | 221 | description => "Dummy. Useful for formatters which want to provide a login page.", |
39e4e363 DM |
222 | parameters => { |
223 | additionalProperties => 0, | |
224 | }, | |
225 | returns => { type => "null" }, | |
226 | code => sub { return undef; }}); | |
227 | ||
2c3a6c0a DM |
228 | __PACKAGE__->register_method ({ |
229 | name => 'create_ticket', | |
230 | path => 'ticket', | |
231 | method => 'POST', | |
96919234 DM |
232 | permissions => { |
233 | description => "You need to pass valid credientials.", | |
234 | user => 'world' | |
235 | }, | |
2c3a6c0a | 236 | protected => 1, # else we can't access shadow files |
adf8d771 | 237 | description => "Create or verify authentication ticket.", |
2c3a6c0a DM |
238 | parameters => { |
239 | additionalProperties => 0, | |
240 | properties => { | |
241 | username => { | |
3a5ae7a0 SI |
242 | description => "User name", |
243 | type => 'string', | |
244 | maxLength => 64, | |
245 | completion => \&PVE::AccessControl::complete_username, | |
2c3a6c0a DM |
246 | }, |
247 | realm => get_standard_option('realm', { | |
248 | description => "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.", | |
3e5bfdf6 DM |
249 | optional => 1, |
250 | completion => \&PVE::AccessControl::complete_realm, | |
251 | }), | |
2c3a6c0a DM |
252 | password => { |
253 | description => "The secret password. This can also be a valid ticket.", | |
254 | type => 'string', | |
255 | }, | |
96f8ebd6 DM |
256 | otp => { |
257 | description => "One-time password for Two-factor authentication.", | |
258 | type => 'string', | |
259 | optional => 1, | |
260 | }, | |
2c3a6c0a | 261 | path => { |
adf8d771 | 262 | description => "Verify ticket, and check if user have access 'privs' on 'path'", |
2c3a6c0a DM |
263 | type => 'string', |
264 | requires => 'privs', | |
265 | optional => 1, | |
266 | maxLength => 64, | |
267 | }, | |
268 | privs => { | |
adf8d771 | 269 | description => "Verify ticket, and check if user have access 'privs' on 'path'", |
2c3a6c0a DM |
270 | type => 'string' , format => 'pve-priv-list', |
271 | requires => 'path', | |
272 | optional => 1, | |
273 | maxLength => 64, | |
274 | }, | |
275 | } | |
276 | }, | |
277 | returns => { | |
278 | type => "object", | |
279 | properties => { | |
2c3a6c0a | 280 | username => { type => 'string' }, |
adf8d771 DM |
281 | ticket => { type => 'string', optional => 1}, |
282 | CSRFPreventionToken => { type => 'string', optional => 1 }, | |
e842fec5 | 283 | clustername => { type => 'string', optional => 1 }, |
18f8ba18 | 284 | # cap => computed api permissions, unless there's a u2f challenge |
2c3a6c0a DM |
285 | } |
286 | }, | |
287 | code => sub { | |
288 | my ($param) = @_; | |
289 | ||
290 | my $username = $param->{username}; | |
291 | $username .= "\@$param->{realm}" if $param->{realm}; | |
292 | ||
293 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2c3a6c0a | 294 | |
adf8d771 | 295 | my $res; |
adf8d771 | 296 | eval { |
7070c1ae DM |
297 | # test if user exists and is enabled |
298 | $rpcenv->check_user_enabled($username); | |
299 | ||
2c3a6c0a | 300 | if ($param->{path} && $param->{privs}) { |
96f8ebd6 | 301 | $res = &$verify_auth($rpcenv, $username, $param->{password}, $param->{otp}, |
adf8d771 | 302 | $param->{path}, $param->{privs}); |
2c3a6c0a | 303 | } else { |
96f8ebd6 | 304 | $res = &$create_ticket($rpcenv, $username, $param->{password}, $param->{otp}); |
2c3a6c0a | 305 | } |
2c3a6c0a DM |
306 | }; |
307 | if (my $err = $@) { | |
adf8d771 | 308 | my $clientip = $rpcenv->get_client_ip() || ''; |
2c3a6c0a | 309 | syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err"); |
6126ab75 | 310 | # do not return any info to prevent user enumeration attacks |
fe2defd9 | 311 | die PVE::Exception->new("authentication failure\n", code => 401); |
2c3a6c0a DM |
312 | } |
313 | ||
18f8ba18 | 314 | $res->{cap} = &$compute_api_permission($rpcenv, $username) |
f25628d3 | 315 | if !defined($res->{NeedTFA}); |
dd2cfee0 | 316 | |
0fb0c62d FG |
317 | my $clinfo = PVE::Cluster::get_clinfo(); |
318 | if ($clinfo->{cluster}->{name} && $rpcenv->check($username, '/', ['Sys.Audit'], 1)) { | |
319 | $res->{clustername} = $clinfo->{cluster}->{name}; | |
e842fec5 TL |
320 | } |
321 | ||
2c3a6c0a DM |
322 | PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'"); |
323 | ||
adf8d771 | 324 | return $res; |
2c3a6c0a DM |
325 | }}); |
326 | ||
37d45deb | 327 | __PACKAGE__->register_method ({ |
765305e2 | 328 | name => 'change_password', |
37d45deb DM |
329 | path => 'password', |
330 | method => 'PUT', | |
12683df7 | 331 | permissions => { |
82b63965 | 332 | 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.", |
12683df7 DM |
333 | check => [ 'or', |
334 | ['userid-param', 'self'], | |
82b63965 DM |
335 | [ 'and', |
336 | [ 'userid-param', 'Realm.AllocateUser'], | |
337 | [ 'userid-group', ['User.Modify']] | |
338 | ] | |
12683df7 DM |
339 | ], |
340 | }, | |
37d45deb DM |
341 | protected => 1, # else we can't access shadow files |
342 | description => "Change user password.", | |
343 | parameters => { | |
344 | additionalProperties => 0, | |
345 | properties => { | |
3a5ae7a0 | 346 | userid => get_standard_option('userid-completed'), |
37d45deb DM |
347 | password => { |
348 | description => "The new password.", | |
349 | type => 'string', | |
350 | minLength => 5, | |
351 | maxLength => 64, | |
352 | }, | |
353 | } | |
354 | }, | |
355 | returns => { type => "null" }, | |
356 | code => sub { | |
357 | my ($param) = @_; | |
358 | ||
359 | my $rpcenv = PVE::RPCEnvironment::get(); | |
360 | my $authuser = $rpcenv->get_user(); | |
361 | ||
362 | my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
363 | ||
12683df7 | 364 | $rpcenv->check_user_exist($userid); |
37d45deb DM |
365 | |
366 | if ($authuser eq 'root@pam') { | |
367 | # OK - root can change anything | |
368 | } else { | |
369 | if ($authuser eq $userid) { | |
370 | $rpcenv->check_user_enabled($userid); | |
371 | # OK - each user can change its own password | |
372 | } else { | |
12683df7 | 373 | # only root may change root password |
37d45deb | 374 | raise_perm_exc() if $userid eq 'root@pam'; |
59321f26 DM |
375 | # do not allow to change system user passwords |
376 | raise_perm_exc() if $realm eq 'pam'; | |
37d45deb DM |
377 | } |
378 | } | |
379 | ||
380 | PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password}); | |
381 | ||
382 | PVE::Cluster::log_msg('info', 'root@pam', "changed password for user '$userid'"); | |
383 | ||
384 | return undef; | |
385 | }}); | |
386 | ||
2b4c98ab WB |
387 | sub get_u2f_config() { |
388 | die "u2f support not available\n" if !$u2f_available; | |
389 | ||
390 | my $dc = cfs_read_file('datacenter.cfg'); | |
391 | my $u2f = $dc->{u2f}; | |
392 | die "u2f not configured in datacenter.cfg\n" if !$u2f; | |
158514a8 | 393 | $u2f = PVE::JSONSchema::parse_property_string($PVE::DataCenterConfig::u2f_format, $u2f); |
2b4c98ab WB |
394 | return $u2f; |
395 | } | |
396 | ||
397 | sub get_u2f_instance { | |
398 | my ($rpcenv, $publicKey, $keyHandle) = @_; | |
399 | ||
400 | # We store the public key base64 encoded (as the api provides it in binary) | |
401 | $publicKey = decode_base64($publicKey) if defined($publicKey); | |
402 | ||
403 | my $u2fconfig = get_u2f_config(); | |
404 | my $u2f = PVE::U2F->new(); | |
405 | ||
406 | # via the 'Host' header (in case a node has multiple hosts available). | |
407 | my $origin = $u2fconfig->{origin}; | |
408 | if (!defined($origin)) { | |
409 | $origin = $rpcenv->get_request_host(1); | |
410 | if ($origin) { | |
411 | $origin = "https://$origin"; | |
412 | } else { | |
413 | die "failed to figure out u2f origin\n"; | |
414 | } | |
415 | } | |
416 | ||
417 | my $appid = $u2fconfig->{appid} // $origin; | |
418 | $u2f->set_appid($appid); | |
419 | $u2f->set_origin($origin); | |
420 | $u2f->set_publicKey($publicKey) if defined($publicKey); | |
421 | $u2f->set_keyHandle($keyHandle) if defined($keyHandle); | |
422 | return $u2f; | |
423 | } | |
424 | ||
47d731c7 WB |
425 | sub verify_user_tfa_config { |
426 | my ($type, $tfa_cfg, $value) = @_; | |
427 | ||
428 | if (!defined($type)) { | |
429 | die "missing tfa 'type'\n"; | |
430 | } | |
431 | ||
432 | if ($type ne 'oath') { | |
433 | die "invalid type for custom tfa authentication\n"; | |
434 | } | |
435 | ||
436 | my $secret = $tfa_cfg->{keys} | |
437 | or die "missing TOTP secret\n"; | |
438 | $tfa_cfg = $tfa_cfg->{config}; | |
439 | # Copy the hash to verify that we have no unexpected keys without modifying the original hash. | |
440 | $tfa_cfg = {%$tfa_cfg}; | |
441 | ||
442 | # We can only verify 1 secret but oath_verify_otp allows multiple: | |
443 | if (scalar(PVE::Tools::split_list($secret)) != 1) { | |
444 | die "only exactly one secret key allowed\n"; | |
445 | } | |
446 | ||
447 | my $digits = delete($tfa_cfg->{digits}) // 6; | |
448 | my $step = delete($tfa_cfg->{step}) // 30; | |
449 | # Maybe also this? | |
450 | # my $algorithm = delete($tfa_cfg->{algorithm}) // 'sha1'; | |
451 | ||
452 | if (length(my $more = join(', ', keys %$tfa_cfg))) { | |
453 | die "unexpected tfa config keys: $more\n"; | |
454 | } | |
455 | ||
456 | PVE::OTP::oath_verify_otp($value, $secret, $step, $digits); | |
457 | } | |
458 | ||
2b4c98ab WB |
459 | __PACKAGE__->register_method ({ |
460 | name => 'change_tfa', | |
461 | path => 'tfa', | |
462 | method => 'PUT', | |
463 | permissions => { | |
47d731c7 | 464 | description => 'A user can change their own u2f or totp token.', |
2b4c98ab WB |
465 | check => [ 'or', |
466 | ['userid-param', 'self'], | |
467 | [ 'and', | |
468 | [ 'userid-param', 'Realm.AllocateUser'], | |
469 | [ 'userid-group', ['User.Modify']] | |
470 | ] | |
471 | ], | |
472 | }, | |
473 | protected => 1, # else we can't access shadow files | |
474 | description => "Change user u2f authentication.", | |
475 | parameters => { | |
476 | additionalProperties => 0, | |
477 | properties => { | |
478 | userid => get_standard_option('userid', { | |
479 | completion => \&PVE::AccessControl::complete_username, | |
480 | }), | |
481 | password => { | |
482 | optional => 1, # Only required if not root@pam | |
483 | description => "The current password.", | |
484 | type => 'string', | |
485 | minLength => 5, | |
486 | maxLength => 64, | |
487 | }, | |
488 | action => { | |
489 | description => 'The action to perform', | |
490 | type => 'string', | |
491 | enum => [qw(delete new confirm)], | |
492 | }, | |
493 | response => { | |
494 | optional => 1, | |
47d731c7 WB |
495 | description => |
496 | 'Either the the response to the current u2f registration challenge,' | |
497 | .' or, when adding TOTP, the currently valid TOTP value.', | |
498 | type => 'string', | |
499 | }, | |
500 | key => { | |
501 | optional => 1, | |
502 | description => 'When adding TOTP, the shared secret value.', | |
2b4c98ab | 503 | type => 'string', |
0bf114df | 504 | format => 'pve-tfa-secret', |
47d731c7 WB |
505 | }, |
506 | config => { | |
507 | optional => 1, | |
508 | description => 'A TFA configuration. This must currently be of type TOTP of not set at all.', | |
509 | type => 'string', | |
510 | format => 'pve-tfa-config', | |
511 | maxLength => 128, | |
2b4c98ab WB |
512 | }, |
513 | } | |
514 | }, | |
515 | returns => { type => 'object' }, | |
516 | code => sub { | |
517 | my ($param) = @_; | |
518 | ||
519 | my $rpcenv = PVE::RPCEnvironment::get(); | |
520 | my $authuser = $rpcenv->get_user(); | |
521 | ||
522 | my $action = delete $param->{action}; | |
523 | my $response = delete $param->{response}; | |
524 | my $password = delete($param->{password}) // ''; | |
47d731c7 WB |
525 | my $key = delete($param->{key}); |
526 | my $config = delete($param->{config}); | |
2b4c98ab WB |
527 | |
528 | my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
529 | $rpcenv->check_user_exist($userid); | |
530 | ||
531 | # Only root may modify root | |
532 | raise_perm_exc() if $userid eq 'root@pam' && $authuser ne 'root@pam'; | |
533 | ||
534 | # Regular users need to confirm their password to change u2f settings. | |
535 | if ($authuser ne 'root@pam') { | |
e240695b | 536 | raise_param_exc({ 'password' => 'password is required to modify u2f data' }) |
2b4c98ab WB |
537 | if !defined($password); |
538 | my $domain_cfg = cfs_read_file('domains.cfg'); | |
539 | my $cfg = $domain_cfg->{ids}->{$realm}; | |
540 | die "auth domain '$realm' does not exists\n" if !$cfg; | |
541 | my $plugin = PVE::Auth::Plugin->lookup($cfg->{type}); | |
542 | $plugin->authenticate_user($cfg, $realm, $ruid, $password); | |
543 | } | |
544 | ||
545 | if ($action eq 'delete') { | |
546 | PVE::AccessControl::user_set_tfa($userid, $realm, undef, undef); | |
547 | PVE::Cluster::log_msg('info', $authuser, "deleted u2f data for user '$userid'"); | |
548 | } elsif ($action eq 'new') { | |
47d731c7 WB |
549 | if (defined($config)) { |
550 | $config = PVE::Auth::Plugin::parse_tfa_config($config); | |
551 | my $type = delete($config->{type}); | |
552 | my $tfa_cfg = { | |
553 | keys => $key, | |
554 | config => $config, | |
555 | }; | |
556 | verify_user_tfa_config($type, $tfa_cfg, $response); | |
557 | PVE::AccessControl::user_set_tfa($userid, $realm, $type, $tfa_cfg); | |
558 | } else { | |
559 | # The default is U2F: | |
560 | my $u2f = get_u2f_instance($rpcenv); | |
561 | my $challenge = $u2f->registration_challenge() | |
562 | or raise("failed to get u2f challenge"); | |
563 | $challenge = decode_json($challenge); | |
564 | PVE::AccessControl::user_set_tfa($userid, $realm, 'u2f', $challenge); | |
565 | return $challenge; | |
566 | } | |
2b4c98ab | 567 | } elsif ($action eq 'confirm') { |
e240695b | 568 | raise_param_exc({ 'response' => "confirm action requires the 'response' parameter to be set" }) |
2b4c98ab WB |
569 | if !defined($response); |
570 | ||
571 | my ($type, $u2fdata) = PVE::AccessControl::user_get_tfa($userid, $realm); | |
572 | raise("no u2f data available") | |
573 | if (!defined($type) || $type ne 'u2f'); | |
574 | ||
575 | my $challenge = $u2fdata->{challenge} | |
576 | or raise("no active challenge"); | |
577 | ||
578 | my $u2f = get_u2f_instance($rpcenv); | |
579 | $u2f->set_challenge($challenge); | |
580 | my ($keyHandle, $publicKey) = $u2f->registration_verify($response); | |
581 | PVE::AccessControl::user_set_tfa($userid, $realm, 'u2f', { | |
582 | keyHandle => $keyHandle, | |
eb25cbaf | 583 | publicKey => $publicKey, # already base64 encoded |
2b4c98ab WB |
584 | }); |
585 | } else { | |
586 | die "invalid action: $action\n"; | |
587 | } | |
588 | ||
589 | return {}; | |
590 | }}); | |
591 | ||
592 | __PACKAGE__->register_method({ | |
593 | name => 'verify_tfa', | |
594 | path => 'tfa', | |
595 | method => 'POST', | |
596 | permissions => { user => 'all' }, | |
597 | protected => 1, # else we can't access shadow files | |
598 | description => 'Finish a u2f challenge.', | |
599 | parameters => { | |
600 | additionalProperties => 0, | |
601 | properties => { | |
602 | response => { | |
603 | type => 'string', | |
604 | description => 'The response to the current authentication challenge.', | |
605 | }, | |
606 | } | |
607 | }, | |
608 | returns => { | |
609 | type => 'object', | |
610 | properties => { | |
611 | ticket => { type => 'string' }, | |
612 | # cap | |
613 | } | |
614 | }, | |
615 | code => sub { | |
616 | my ($param) = @_; | |
617 | ||
618 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2b4c98ab WB |
619 | my $authuser = $rpcenv->get_user(); |
620 | my ($username, undef, $realm) = PVE::AccessControl::verify_username($authuser); | |
621 | ||
f25628d3 WB |
622 | my ($tfa_type, $tfa_data) = PVE::AccessControl::user_get_tfa($username, $realm); |
623 | if (!defined($tfa_type)) { | |
2b4c98ab WB |
624 | raise('no u2f data available'); |
625 | } | |
626 | ||
f25628d3 WB |
627 | eval { |
628 | if ($tfa_type eq 'u2f') { | |
629 | my $challenge = $rpcenv->get_u2f_challenge() | |
630 | or raise('no active challenge'); | |
2b4c98ab | 631 | |
f25628d3 WB |
632 | my $keyHandle = $tfa_data->{keyHandle}; |
633 | my $publicKey = $tfa_data->{publicKey}; | |
634 | raise("incomplete u2f setup") | |
635 | if !defined($keyHandle) || !defined($publicKey); | |
2b4c98ab | 636 | |
f25628d3 WB |
637 | my $u2f = get_u2f_instance($rpcenv, $publicKey, $keyHandle); |
638 | $u2f->set_challenge($challenge); | |
639 | ||
640 | my ($counter, $present) = $u2f->auth_verify($param->{response}); | |
641 | # Do we want to do anything with these? | |
642 | } else { | |
643 | # sanity check before handing off to the verification code: | |
644 | my $keys = $tfa_data->{keys} or die "missing tfa keys\n"; | |
645 | my $config = $tfa_data->{config} or die "bad tfa entry\n"; | |
646 | PVE::AccessControl::verify_one_time_pw($tfa_type, $authuser, $keys, $config, $param->{response}); | |
647 | } | |
2b4c98ab WB |
648 | }; |
649 | if (my $err = $@) { | |
650 | my $clientip = $rpcenv->get_client_ip() || ''; | |
651 | syslog('err', "authentication verification failure; rhost=$clientip user=$authuser msg=$err"); | |
652 | die PVE::Exception->new("authentication failure\n", code => 401); | |
653 | } | |
654 | ||
2b4c98ab | 655 | return { |
f25628d3 | 656 | ticket => PVE::AccessControl::assemble_ticket($authuser), |
2b4c98ab WB |
657 | cap => &$compute_api_permission($rpcenv, $authuser), |
658 | } | |
659 | }}); | |
660 | ||
2c3a6c0a | 661 | 1; |