]> git.proxmox.com Git - pve-access-control.git/blame - src/PVE/AccessControl.pm
bump version to 8.1.4
[pve-access-control.git] / src / PVE / AccessControl.pm
CommitLineData
2c3a6c0a
DM
1package PVE::AccessControl;
2
3use strict;
7c410d63 4use warnings;
2c3a6c0a
DM
5use Encode;
6use Crypt::OpenSSL::Random;
7use Crypt::OpenSSL::RSA;
cee5583b 8use Net::SSLeay;
25167526 9use Net::IP;
0fe62fa8 10use MIME::Base32;
2c3a6c0a
DM
11use MIME::Base64;
12use Digest::SHA;
21800a71
FG
13use IO::File;
14use File::stat;
fda8ca85 15use JSON;
2ee46655 16use Scalar::Util 'weaken';
0fe62fa8 17use URI::Escape;
a1f8aaae 18
060941d4 19use PVE::Exception qw(raise_perm_exc raise_param_exc);
972859d1 20use PVE::OTP;
a1f8aaae 21use PVE::Ticket;
2c3a6c0a
DM
22use PVE::Tools qw(run_command lock_file file_get_contents split_list safe_print);
23use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
ab7b19b5 24use PVE::JSONSchema qw(register_standard_option get_standard_option);
5bb4e06a 25
57098eb8
WB
26use PVE::RS::TFA;
27
5bb4e06a
DM
28use PVE::Auth::Plugin;
29use PVE::Auth::AD;
30use PVE::Auth::LDAP;
31use PVE::Auth::PVE;
32use PVE::Auth::PAM;
52d1c1b9 33use PVE::Auth::OpenId;
2c3a6c0a 34
5bb4e06a
DM
35# load and initialize all plugins
36
37PVE::Auth::AD->register();
38PVE::Auth::LDAP->register();
39PVE::Auth::PVE->register();
40PVE::Auth::PAM->register();
52d1c1b9 41PVE::Auth::OpenId->register();
5bb4e06a
DM
42PVE::Auth::Plugin->init();
43
2c3a6c0a
DM
44# $authdir must be writable by root only!
45my $confdir = "/etc/pve";
46my $authdir = "$confdir/priv";
21800a71 47
2c3a6c0a
DM
48my $pve_www_key_fn = "$confdir/pve-www.key";
49
21800a71
FG
50my $pve_auth_key_files = {
51 priv => "$authdir/authkey.key",
52 pub => "$confdir/authkey.pub",
53 pubold => "$confdir/authkey.pub.old",
54};
55
56my $pve_auth_key_cache = {};
57
243262f1 58my $ticket_lifetime = 3600 * 2; # 2 hours
8304b226 59my $auth_graceperiod = 60 * 5; # 5 minutes
243262f1 60my $authkey_lifetime = 3600 * 24; # rotate every 24 hours
2c3a6c0a
DM
61
62Crypt::OpenSSL::RSA->import_random_seed();
63
d7e8d24e
TL
64cfs_register_file('user.cfg', \&parse_user_config, \&write_user_config);
65cfs_register_file('priv/tfa.cfg', \&parse_priv_tfa_config, \&write_priv_tfa_config);
2c3a6c0a 66
5bb4e06a
DM
67sub verify_username {
68 PVE::Auth::Plugin::verify_username(@_);
2c3a6c0a
DM
69}
70
5bb4e06a
DM
71sub pve_verify_realm {
72 PVE::Auth::Plugin::pve_verify_realm(@_);
2c3a6c0a
DM
73}
74
2ee46655
WB
75# Locking both config files together is only ever allowed in one order:
76# 1) tfa config
77# 2) user config
78# If we permit the other way round, too, we might end up deadlocking!
79my $user_config_locked;
5bb4e06a 80sub lock_user_config {
2c3a6c0a
DM
81 my ($code, $errmsg) = @_;
82
2ee46655
WB
83 my $locked = 1;
84 $user_config_locked = \$locked;
85 weaken $user_config_locked; # make this scope guard signal safe...
86
5bb4e06a 87 cfs_lock_file("user.cfg", undef, $code);
2ee46655 88 $user_config_locked = undef;
5bb4e06a 89 if (my $err = $@) {
2c3a6c0a
DM
90 $errmsg ? die "$errmsg: $err" : die $err;
91 }
92}
93
07692c72
WB
94sub lock_tfa_config {
95 my ($code, $errmsg) = @_;
96
2ee46655
WB
97 die "tfa config lock cannot be acquired while holding user config lock\n"
98 if ($user_config_locked && $$user_config_locked);
99
07692c72
WB
100 my $res = cfs_lock_file("priv/tfa.cfg", undef, $code);
101 if (my $err = $@) {
102 $errmsg ? die "$errmsg: $err" : die $err;
103 }
104
105 return $res;
106}
107
21800a71
FG
108my $cache_read_key = sub {
109 my ($type) = @_;
110
111 my $path = $pve_auth_key_files->{$type};
112
113 my $read_key_and_mtime = sub {
114 my $fh = IO::File->new($path, "r");
115
116 return undef if !defined($fh);
117
118 my $st = stat($fh);
119 my $pem = PVE::Tools::safe_read_from($fh, 0, 0, $path);
120
121 close $fh;
122
123 my $key;
124 if ($type eq 'pub' || $type eq 'pubold') {
125 $key = eval { Crypt::OpenSSL::RSA->new_public_key($pem); };
126 } elsif ($type eq 'priv') {
127 $key = eval { Crypt::OpenSSL::RSA->new_private_key($pem); };
128 } else {
129 die "Invalid authkey type '$type'\n";
130 }
131
132 return { key => $key, mtime => $st->mtime };
133 };
134
135 if (!defined($pve_auth_key_cache->{$type})) {
136 $pve_auth_key_cache->{$type} = $read_key_and_mtime->();
137 } else {
138 my $st = stat($path);
139 if (!$st || $st->mtime != $pve_auth_key_cache->{$type}->{mtime}) {
140 $pve_auth_key_cache->{$type} = $read_key_and_mtime->();
141 }
142 }
143
144 return $pve_auth_key_cache->{$type};
145};
146
66931b11 147sub get_pubkey {
21800a71
FG
148 my ($old) = @_;
149
150 my $type = $old ? 'pubold' : 'pub';
151
152 my $res = $cache_read_key->($type);
153 return undef if !defined($res);
154
155 return wantarray ? ($res->{key}, $res->{mtime}) : $res->{key};
156}
157
158sub get_privkey {
159 my $res = $cache_read_key->('priv');
2c3a6c0a 160
21800a71
FG
161 if (!defined($res) || !check_authkey(1)) {
162 rotate_authkey();
163 $res = $cache_read_key->('priv');
164 }
2c3a6c0a 165
21800a71
FG
166 return wantarray ? ($res->{key}, $res->{mtime}) : $res->{key};
167}
2c3a6c0a 168
21800a71
FG
169sub check_authkey {
170 my ($quiet) = @_;
171
172 # skip check if non-quorate, as rotation is not possible anyway
173 return 1 if !PVE::Cluster::check_cfs_quorum(1);
174
175 my ($pub_key, $mtime) = get_pubkey();
176 if (!$pub_key) {
177 warn "auth key pair missing, generating new one..\n" if !$quiet;
178 return 0;
179 } else {
9de25de8
TL
180 my $now = time();
181 if ($now - $mtime >= $authkey_lifetime) {
21800a71
FG
182 warn "auth key pair too old, rotating..\n" if !$quiet;;
183 return 0;
9de25de8
TL
184 } elsif ($mtime > $now + $auth_graceperiod) {
185 # a nodes RTC had a time set in the future during key generation -> ticket
186 # validity is clamped to 0+5 min grace period until now >= mtime again
187 my (undef, $old_mtime) = get_pubkey(1);
188 if ($old_mtime && $mtime >= $old_mtime && $mtime - $old_mtime < $ticket_lifetime) {
189 warn "auth key pair generated in the future (key $mtime > host $now),"
190 ." but old key still exists and in valid grace period so avoid automatic"
191 ." fixup. Cluster time not in sync?\n" if !$quiet;
192 return 1;
193 }
194 warn "auth key pair generated in the future (key $mtime > host $now), rotating..\n" if !$quiet;
195 return 0;
21800a71
FG
196 } else {
197 warn "auth key new enough, skipping rotation\n" if !$quiet;;
198 return 1;
199 }
200 }
201}
2c3a6c0a 202
21800a71
FG
203sub rotate_authkey {
204 return if $authkey_lifetime == 0;
205
03593f3d 206 PVE::Cluster::cfs_lock_authkey(undef, sub {
a74d5080
FG
207 # stat() calls might be answered from the kernel page cache for up to
208 # 1s, so this special dance is needed to avoid a double rotation in
209 # clusters *despite* the cfs_lock context..
210
211 # drop in-process cache hash
212 $pve_auth_key_cache = {};
213 # force open/close of file to invalidate page cache entry
214 get_pubkey();
215 # now re-check with lock held and page cache invalidated so that stat()
216 # does the right thing, and any key updates by other nodes are visible.
21800a71
FG
217 return if check_authkey();
218
219 my $old = get_pubkey();
e770e667 220 my $new = Crypt::OpenSSL::RSA->generate_key(2048);
21800a71
FG
221
222 if ($old) {
223 eval {
224 my $pem = $old->get_public_key_x509_string();
b8055a4f 225 # mtime is used for caching and ticket age range calculation
21800a71
FG
226 PVE::Tools::file_set_contents($pve_auth_key_files->{pubold}, $pem);
227 };
228 die "Failed to store old auth key: $@\n" if $@;
229 }
230
21800a71
FG
231 eval {
232 my $pem = $new->get_public_key_x509_string();
b8055a4f
FG
233 # mtime is used for caching and ticket age range calculation,
234 # should be close to that of pubold above
21800a71
FG
235 PVE::Tools::file_set_contents($pve_auth_key_files->{pub}, $pem);
236 };
237 if ($@) {
238 if ($old) {
239 warn "Failed to store new auth key - $@\n";
240 warn "Reverting to previous auth key\n";
241 eval {
242 my $pem = $old->get_public_key_x509_string();
243 PVE::Tools::file_set_contents($pve_auth_key_files->{pub}, $pem);
244 };
245 die "Failed to restore old auth key: $@\n" if $@;
246 } else {
247 die "Failed to store new auth key - $@\n";
248 }
249 }
250
251 eval {
252 my $pem = $new->get_private_key_string();
253 PVE::Tools::file_set_contents($pve_auth_key_files->{priv}, $pem);
254 };
255 if ($@) {
256 warn "Failed to store new auth key - $@\n";
257 warn "Deleting auth key to force regeneration\n";
258 unlink $pve_auth_key_files->{pub};
259 unlink $pve_auth_key_files->{priv};
260 }
261 });
262 die $@ if $@;
2c3a6c0a
DM
263}
264
571e9d06
FG
265PVE::JSONSchema::register_standard_option('tokenid', {
266 description => "API token identifier.",
267 type => "string",
268 format => "pve-tokenid",
269});
270
28e3dc05
FG
271our $token_subid_regex = $PVE::Auth::Plugin::realm_regex;
272
273# username@realm username realm tokenid
274our $token_full_regex = qr/((${PVE::Auth::Plugin::user_regex})\@(${PVE::Auth::Plugin::realm_regex}))!(${token_subid_regex})/;
275
276our $userid_or_token_regex = qr/^$PVE::Auth::Plugin::user_regex\@$PVE::Auth::Plugin::realm_regex(?:!$token_subid_regex)?$/;
277
278sub split_tokenid {
279 my ($tokenid, $noerr) = @_;
280
281 if ($tokenid =~ /^${token_full_regex}$/) {
282 return ($1, $4);
283 }
284
285 die "'$tokenid' is not a valid token ID - not able to split into user and token parts\n" if !$noerr;
286
287 return undef;
288}
289
290sub join_tokenid {
291 my ($username, $tokensubid) = @_;
292
293 my $joined = "${username}!${tokensubid}";
294
295 return pve_verify_tokenid($joined);
296}
297
298PVE::JSONSchema::register_format('pve-tokenid', \&pve_verify_tokenid);
299sub pve_verify_tokenid {
300 my ($tokenid, $noerr) = @_;
301
302 if ($tokenid =~ /^${token_full_regex}$/) {
303 return wantarray ? ($tokenid, $2, $3, $4) : $tokenid;
304 }
305
306 die "value '$tokenid' does not look like a valid token ID\n" if !$noerr;
307
308 return undef;
309}
310
311
2c3a6c0a 312my $csrf_prevention_secret;
e149b1c6 313my $csrf_prevention_secret_legacy;
2c3a6c0a
DM
314my $get_csrfr_secret = sub {
315 if (!$csrf_prevention_secret) {
66931b11 316 my $input = PVE::Tools::file_get_contents($pve_www_key_fn);
51e6f56d 317 $csrf_prevention_secret = Digest::SHA::hmac_sha256_base64($input);
e149b1c6 318 $csrf_prevention_secret_legacy = Digest::SHA::sha1_base64($input);
2c3a6c0a
DM
319 }
320 return $csrf_prevention_secret;
321};
322
323sub assemble_csrf_prevention_token {
324 my ($username) = @_;
325
a1f8aaae 326 my $secret = &$get_csrfr_secret();
2c3a6c0a 327
a1f8aaae 328 return PVE::Ticket::assemble_csrf_prevention_token ($secret, $username);
2c3a6c0a
DM
329}
330
331sub verify_csrf_prevention_token {
332 my ($username, $token, $noerr) = @_;
333
e149b1c6
TL
334 my $secret = $get_csrfr_secret->();
335
336 # FIXME: remove with PVE 7 and/or refactor all into PVE::Ticket ?
337 if ($token =~ m/^([A-Z0-9]{8}):(\S+)$/) {
338 my $sig = $2;
339 if (length($sig) == 27) {
340 # the legacy secret got populated by above get_csrfr_secret call
341 $secret = $csrf_prevention_secret_legacy;
342 }
343 }
2c3a6c0a 344
a1f8aaae 345 return PVE::Ticket::verify_csrf_prevention_token(
8304b226 346 $secret, $username, $token, -$auth_graceperiod, $ticket_lifetime, $noerr);
2c3a6c0a
DM
347}
348
21800a71
FG
349my $get_ticket_age_range = sub {
350 my ($now, $mtime, $rotated) = @_;
351
352 my $key_age = $now - $mtime;
353 $key_age = 0 if $key_age < 0;
354
8304b226 355 my $min = -$auth_graceperiod;
21800a71
FG
356 my $max = $ticket_lifetime;
357
358 if ($rotated) {
359 # ticket creation after rotation is not allowed
8304b226 360 $min = $key_age - $auth_graceperiod;
21800a71
FG
361 } else {
362 if ($key_age > $authkey_lifetime && $authkey_lifetime > 0) {
363 if (PVE::Cluster::check_cfs_quorum(1)) {
364 # key should have been rotated, clamp range accordingly
365 $min = $key_age - $authkey_lifetime;
366 } else {
367 warn "Cluster not quorate - extending auth key lifetime!\n";
368 }
369 }
370
8304b226 371 $max = $key_age + $auth_graceperiod if $key_age < $ticket_lifetime;
21800a71 372 }
2c3a6c0a 373
21800a71
FG
374 return undef if $min > $ticket_lifetime;
375 return ($min, $max);
376};
2c3a6c0a 377
afb10353
WB
378sub assemble_ticket : prototype($;$) {
379 my ($data, $aad) = @_;
2c3a6c0a
DM
380
381 my $rsa_priv = get_privkey();
382
afb10353 383 return PVE::Ticket::assemble_rsa_ticket($rsa_priv, 'PVE', $data, $aad);
2c3a6c0a
DM
384}
385
afb10353
WB
386# Returns the username, "age" and tfa info.
387#
388# Note that for the new-style outh, tfa info is never set, as it only uses the `/ticket` api call
389# via the new 'tfa-challenge' parameter, so this part can go with PVE-8.
390#
391# New-style auth still uses this function, but sets `$tfa_ticket` to true when validating the tfa
392# ticket.
393sub verify_ticket : prototype($;$$) {
394 my ($ticket, $noerr, $tfa_ticket_aad) = @_;
2c3a6c0a 395
21800a71
FG
396 my $now = time();
397
398 my $check = sub {
399 my ($old) = @_;
400
401 my ($rsa_pub, $rsa_mtime) = get_pubkey($old);
402 return undef if !$rsa_pub;
403
404 my ($min, $max) = $get_ticket_age_range->($now, $rsa_mtime, $old);
5bb966fe 405 return undef if !defined($min);
21800a71
FG
406
407 return PVE::Ticket::verify_rsa_ticket(
afb10353 408 $rsa_pub, 'PVE', $ticket, $tfa_ticket_aad, $min, $max, 1);
21800a71 409 };
a1f8aaae 410
18f8ba18 411 my ($data, $age) = $check->();
a1f8aaae 412
21800a71 413 # check with old, rotated key if current key failed
18f8ba18 414 ($data, $age) = $check->(1) if !defined($data);
21800a71 415
18f8ba18 416 my $auth_failure = sub {
21800a71
FG
417 if ($noerr) {
418 return undef;
419 } else {
420 # raise error via undef ticket
421 PVE::Ticket::verify_rsa_ticket(undef, 'PVE');
422 }
18f8ba18
WB
423 };
424
425 if (!defined($data)) {
426 return $auth_failure->();
427 }
428
afb10353
WB
429 if ($tfa_ticket_aad) {
430 # We're validating a ticket-call's 'tfa-challenge' parameter, so just return its data.
431 if ($data =~ /^!tfa!(.*)$/) {
432 return $1;
433 }
434 die "bad ticket\n";
435 }
436
f25628d3 437 my ($username, $tfa_info);
afb10353
WB
438 if ($data =~ /^!tfa!(.*)$/) {
439 # PBS style half-authenticated ticket, contains a json string form of a `TfaChallenge`
440 # object.
441 # This type of ticket does not contain the user name.
442 return { type => 'new', data => $1 };
443 }
18f8ba18
WB
444 if ($data =~ m{^u2f!([^!]+)!([0-9a-zA-Z/.=_\-+]+)$}) {
445 # Ticket for u2f-users:
f25628d3 446 ($username, my $challenge) = ($1, $2);
18f8ba18
WB
447 if ($challenge eq 'verified') {
448 # u2f challenge was completed
449 $challenge = undef;
450 } elsif (!wantarray) {
451 # The caller is not aware there could be an ongoing challenge,
452 # so we treat this ticket as invalid:
453 return $auth_failure->();
454 }
f25628d3
WB
455 $tfa_info = {
456 type => 'u2f',
457 challenge => $challenge,
458 };
459 } elsif ($data =~ /^tfa!(.*)$/) {
460 # TOTP and Yubico don't require a challenge so this is the generic
461 # 'missing 2nd factor ticket'
462 $username = $1;
463 $tfa_info = { type => 'tfa' };
18f8ba18
WB
464 } else {
465 # Regular ticket (full access)
466 $username = $data;
21800a71 467 }
2c3a6c0a 468
a1f8aaae 469 return undef if !PVE::Auth::Plugin::verify_username($username, $noerr);
2c3a6c0a 470
f25628d3 471 return wantarray ? ($username, $age, $tfa_info) : $username;
2c3a6c0a
DM
472}
473
35c3ca0f
FG
474sub verify_token {
475 my ($api_token) = @_;
476
477 die "no API token specified\n" if !$api_token;
478
479 my ($tokenid, $value);
480 if ($api_token =~ /^(.*)=(.*)$/) {
481 $tokenid = $1;
482 $value = $2;
483 } else {
484 die "no tokenid specified\n";
485 }
486
487 my ($username, $token) = split_tokenid($tokenid);
488
489 my $usercfg = cfs_read_file('user.cfg');
490 check_user_enabled($usercfg, $username);
491 check_token_exist($usercfg, $username, $token);
492
35c3ca0f 493 my $user = $usercfg->{users}->{$username};
35c3ca0f 494 my $token_info = $user->{tokens}->{$token};
8a724f7b
DM
495
496 my $ctime = time();
aaacf4c3 497 die "token '$token' access expired\n" if $token_info->{expire} && ($token_info->{expire} < $ctime);
35c3ca0f
FG
498
499 die "invalid token value!\n" if !PVE::Cluster::verify_token($tokenid, $value);
500
501 return wantarray ? ($tokenid) : $tokenid;
502}
503
3760a33c
FG
504my $assemble_short_lived_ticket = sub {
505 my ($prefix, $username, $path) = @_;
adf8d771
DM
506
507 my $rsa_priv = get_privkey();
508
adf8d771
DM
509 $path = normalize_path($path);
510
37d3c16b
FG
511 die "invalid ticket path\n" if !defined($path);
512
a1f8aaae 513 my $secret_data = "$username:$path";
adf8d771 514
a1f8aaae 515 return PVE::Ticket::assemble_rsa_ticket(
3760a33c
FG
516 $rsa_priv, $prefix, undef, $secret_data);
517};
adf8d771 518
3760a33c
FG
519my $verify_short_lived_ticket = sub {
520 my ($ticket, $prefix, $username, $path, $noerr) = @_;
adf8d771 521
06b4a3b3
FG
522 $path = normalize_path($path);
523
37d3c16b
FG
524 die "invalid ticket path\n" if !defined($path);
525
a1f8aaae 526 my $secret_data = "$username:$path";
adf8d771 527
21800a71 528 my ($rsa_pub, $rsa_mtime) = get_pubkey();
5efff6c1 529 if (!$rsa_pub || (time() - $rsa_mtime > $authkey_lifetime && $authkey_lifetime > 0)) {
21800a71
FG
530 if ($noerr) {
531 return undef;
532 } else {
533 # raise error via undef ticket
3760a33c 534 PVE::Ticket::verify_rsa_ticket($rsa_pub, $prefix);
21800a71
FG
535 }
536 }
537
a1f8aaae 538 return PVE::Ticket::verify_rsa_ticket(
3760a33c
FG
539 $rsa_pub, $prefix, $ticket, $secret_data, -20, 40, $noerr);
540};
541
542# VNC tickets
543# - they do not contain the username in plain text
544# - they are restricted to a specific resource path (example: '/vms/100')
545sub assemble_vnc_ticket {
546 my ($username, $path) = @_;
547
548 return $assemble_short_lived_ticket->('PVEVNC', $username, $path);
549}
550
551sub verify_vnc_ticket {
552 my ($ticket, $username, $path, $noerr) = @_;
553
554 return $verify_short_lived_ticket->($ticket, 'PVEVNC', $username, $path, $noerr);
555}
556
557# Tunnel tickets
558# - they do not contain the username in plain text
559# - they are restricted to a specific resource path (example: '/vms/100', '/socket/run/qemu-server/123.storage')
560sub assemble_tunnel_ticket {
561 my ($username, $path) = @_;
562
563 return $assemble_short_lived_ticket->('PVETUNNEL', $username, $path);
564}
565
566sub verify_tunnel_ticket {
567 my ($ticket, $username, $path, $noerr) = @_;
568
569 return $verify_short_lived_ticket->($ticket, 'PVETUNNEL', $username, $path, $noerr);
adf8d771
DM
570}
571
23b35225 572sub assemble_spice_ticket {
bf3e6d31 573 my ($username, $vmid, $node) = @_;
23b35225 574
3f62bdbe 575 my $secret = &$get_csrfr_secret();
23b35225 576
a1f8aaae
DM
577 return PVE::Ticket::assemble_spice_ticket(
578 $secret, $username, $vmid, $node);
bf3e6d31
DM
579}
580
581sub verify_spice_connect_url {
582 my ($connect_str) = @_;
583
a1f8aaae 584 my $secret = &$get_csrfr_secret();
bf3e6d31 585
a1f8aaae 586 return PVE::Ticket::verify_spice_connect_url($secret, $connect_str);
23b35225
AD
587}
588
cee5583b
DM
589sub read_x509_subject_spice {
590 my ($filename) = @_;
591
592 # read x509 subject
593 my $bio = Net::SSLeay::BIO_new_file($filename, 'r');
44903703
FG
594 die "Could not open $filename using OpenSSL\n"
595 if !$bio;
596
cee5583b
DM
597 my $x509 = Net::SSLeay::PEM_read_bio_X509($bio);
598 Net::SSLeay::BIO_free($bio);
44903703
FG
599
600 die "Could not parse X509 certificate in $filename\n"
601 if !$x509;
602
cee5583b
DM
603 my $nameobj = Net::SSLeay::X509_get_subject_name($x509);
604 my $subject = Net::SSLeay::X509_NAME_oneline($nameobj);
605 Net::SSLeay::X509_free($x509);
66931b11 606
7d23b7ca 607 # remote-viewer wants comma as separator (not '/')
cee5583b
DM
608 $subject =~ s!^/!!;
609 $subject =~ s!/(\w+=)!,$1!g;
610
611 return $subject;
612}
613
614# helper to generate SPICE remote-viewer configuration
615sub remote_viewer_config {
616 my ($authuser, $vmid, $node, $proxy, $title, $port) = @_;
617
618 if (!$proxy) {
619 my $host = `hostname -f` || PVE::INotify::nodename();
620 chomp $host;
621 $proxy = $host;
622 }
623
624 my ($ticket, $proxyticket) = assemble_spice_ticket($authuser, $vmid, $node);
625
626 my $filename = "/etc/pve/local/pve-ssl.pem";
627 my $subject = read_x509_subject_spice($filename);
628
629 my $cacert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192);
630 $cacert =~ s/\n/\\n/g;
63691fc6 631
25167526 632 $proxy = "[$proxy]" if Net::IP::ip_is_ipv6($proxy);
cee5583b 633 my $config = {
63691fc6
DM
634 'secure-attention' => "Ctrl+Alt+Ins",
635 'toggle-fullscreen' => "Shift+F11",
636 'release-cursor' => "Ctrl+Alt+R",
cee5583b
DM
637 type => 'spice',
638 title => $title,
1075c589 639 host => $proxyticket, # this breaks tls hostname verification, so we need to use 'host-subject'
cee5583b
DM
640 proxy => "http://$proxy:3128",
641 'tls-port' => $port,
642 'host-subject' => $subject,
643 ca => $cacert,
644 password => $ticket,
645 'delete-this-file' => 1,
646 };
647
648 return ($ticket, $proxyticket, $config);
649}
650
37d45deb 651sub check_user_exist {
7070c1ae 652 my ($usercfg, $username, $noerr) = @_;
2c3a6c0a 653
5bb4e06a 654 $username = PVE::Auth::Plugin::verify_username($username, $noerr);
2c3a6c0a 655 return undef if !$username;
66931b11 656
37d45deb
DM
657 return $usercfg->{users}->{$username} if $usercfg && $usercfg->{users}->{$username};
658
659 die "no such user ('$username')\n" if !$noerr;
66931b11 660
37d45deb
DM
661 return undef;
662}
663
664sub check_user_enabled {
665 my ($usercfg, $username, $noerr) = @_;
666
667 my $data = check_user_exist($usercfg, $username, $noerr);
668 return undef if !$data;
669
5ff516de
TL
670 if (!$data->{enable}) {
671 die "user '$username' is disabled\n" if !$noerr;
672 return undef;
673 }
674
8a724f7b
DM
675 my $ctime = time();
676 my $expire = $usercfg->{users}->{$username}->{expire};
677
5ff516de 678 if ($expire && $expire < $ctime) {
aaacf4c3 679 die "user '$username' access expired\n" if !$noerr;
5ff516de
TL
680 return undef;
681 }
d0cce79f 682
5ff516de 683 return 1; # enabled and not expired
2c3a6c0a
DM
684}
685
571e9d06
FG
686sub check_token_exist {
687 my ($usercfg, $username, $tokenid, $noerr) = @_;
688
689 my $user = check_user_exist($usercfg, $username, $noerr);
690 return undef if !$user;
691
692 return $user->{tokens}->{$tokenid}
693 if defined($user->{tokens}) && $user->{tokens}->{$tokenid};
694
695 die "no such token '$tokenid' for user '$username'\n" if !$noerr;
696
697 return undef;
698}
699
6adcb18c 700# deprecated
96f8ebd6 701sub verify_one_time_pw {
fda8ca85 702 my ($type, $username, $keys, $tfa_cfg, $otp) = @_;
96f8ebd6 703
1075c589 704 die "missing one time password for two-factor authentication '$type'\n" if !$otp;
96f8ebd6
DM
705
706 # fixme: proxy support?
707 my $proxy;
708
709 if ($type eq 'yubico') {
972859d1
DM
710 PVE::OTP::yubico_verify_otp($otp, $keys, $tfa_cfg->{url},
711 $tfa_cfg->{id}, $tfa_cfg->{key}, $proxy);
1abc2c0a 712 } elsif ($type eq 'oath') {
972859d1 713 PVE::OTP::oath_verify_otp($otp, $keys, $tfa_cfg->{step}, $tfa_cfg->{digits});
96f8ebd6
DM
714 } else {
715 die "unknown tfa type '$type'\n";
716 }
96f8ebd6
DM
717}
718
2c3a6c0a 719# password should be utf8 encoded
1075c589 720# Note: some plugins delay/sleep if auth fails
cfd8636b
WB
721sub authenticate_user : prototype($$$;$) {
722 my ($username, $password, $otp, $tfa_challenge) = @_;
2c3a6c0a
DM
723
724 die "no username specified\n" if !$username;
66931b11 725
5bb4e06a 726 my ($ruid, $realm);
2c3a6c0a 727
5bb4e06a 728 ($username, $ruid, $realm) = PVE::Auth::Plugin::verify_username($username);
2c3a6c0a
DM
729
730 my $usercfg = cfs_read_file('user.cfg');
731
6126ab75 732 check_user_enabled($usercfg, $username);
2c3a6c0a 733
5bb4e06a 734 my $domain_cfg = cfs_read_file('domains.cfg');
2c3a6c0a 735
6126ab75 736 my $cfg = $domain_cfg->{ids}->{$realm};
3443faca 737 die "auth domain '$realm' does not exist\n" if !$cfg;
6126ab75 738 my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
afb10353
WB
739
740 if ($tfa_challenge) {
741 # This is the 2nd factor, use the password for the OTP response.
742 my $tfa_challenge = authenticate_2nd_new($username, $realm, $password, $tfa_challenge);
743 return wantarray ? ($username, $tfa_challenge) : $username;
744 }
745
6126ab75 746 $plugin->authenticate_user($cfg, $realm, $ruid, $password);
2c3a6c0a 747
cfd8636b
WB
748 # This is the first factor with an optional immediate 2nd factor for TOTP:
749 $tfa_challenge = authenticate_2nd_new($username, $realm, $otp, undef);
750 return wantarray ? ($username, $tfa_challenge) : $username;
2c3a6c0a
DM
751}
752
68095371 753sub authenticate_2nd_new_do : prototype($$$$) {
61565fb2 754 my ($username, $realm, $tfa_response, $tfa_challenge) = @_;
cfd8636b 755 my ($tfa_cfg, $realm_tfa) = user_get_tfa($username, $realm);
afb10353 756
72950c1d
WB
757 # FIXME: `$tfa_cfg` is now usually never undef - use cheap check for
758 # whether the user has *any* entries here instead whe it is available in
759 # pve-rs
68095371
DC
760 if (!defined($tfa_cfg)) {
761 return undef;
762 }
6adcb18c 763
68095371
DC
764 my $realm_type = $realm_tfa && $realm_tfa->{type};
765 # verify realm type unless using recovery keys:
766 if (defined($realm_type)) {
767 $realm_type = 'totp' if $realm_type eq 'oath'; # we used to call it that
768 if ($realm_type eq 'yubico') {
769 # Yubico auth will not be supported in rust for now...
770 if (!defined($tfa_challenge)) {
771 my $challenge = { yubico => JSON::true };
772 # Even with yubico auth we do allow recovery keys to be used:
773 if (my $recovery = $tfa_cfg->recovery_state($username)) {
774 $challenge->{recovery} = $recovery;
a0374ad0 775 }
68095371 776 return to_json($challenge);
6adcb18c
WB
777 }
778
61565fb2
DC
779 if ($tfa_response =~ /^yubico:(.*)$/) {
780 $tfa_response = $1;
68095371
DC
781 # Defer to after unlocking the TFA config:
782 return sub {
783 authenticate_yubico_new(
61565fb2 784 $tfa_cfg, $username, $realm_tfa, $tfa_challenge, $tfa_response,
68095371
DC
785 );
786 };
6adcb18c 787 }
68095371 788 }
a0374ad0 789
68095371 790 my $response_type;
61565fb2
DC
791 if (defined($tfa_response)) {
792 if ($tfa_response !~ /^([^:]+):/) {
68095371
DC
793 die "bad otp response\n";
794 }
795 $response_type = $1;
afb10353
WB
796 }
797
68095371
DC
798 die "realm requires $realm_type authentication\n"
799 if $response_type && $response_type ne 'recovery' && $response_type ne $realm_type;
800 }
afb10353 801
68095371
DC
802 configure_u2f_and_wa($tfa_cfg);
803
d9f02efe 804 my ($result, $tfa_done);
68095371 805 if (defined($tfa_challenge)) {
d9f02efe 806 $tfa_done = 1;
68095371 807 $tfa_challenge = verify_ticket($tfa_challenge, 0, $username);
d9f02efe 808 $result = $tfa_cfg->authentication_verify2($username, $tfa_challenge, $tfa_response);
68095371
DC
809 $tfa_challenge = undef;
810 } else {
811 $tfa_challenge = $tfa_cfg->authentication_challenge($username);
032e7d6d
FW
812
813 die "missing required 2nd keys\n"
814 if $realm_tfa && !defined($tfa_challenge);
815
61565fb2 816 if (defined($tfa_response)) {
68095371 817 if (defined($tfa_challenge)) {
d9f02efe
WB
818 $tfa_done = 1;
819 $result = $tfa_cfg->authentication_verify2($username, $tfa_challenge, $tfa_response);
68095371
DC
820 } else {
821 die "no such challenge\n";
afb10353
WB
822 }
823 }
68095371 824 }
afb10353 825
d9f02efe
WB
826 if ($tfa_done) {
827 if (!$result) {
828 # authentication_verify2 somehow returned undef - should be unreachable
829 die "2nd factor failed\n";
830 }
831
d9f02efe
WB
832 if ($result->{'needs-saving'}) {
833 cfs_write_file('priv/tfa.cfg', $tfa_cfg);
834 }
9036621e
WB
835 if ($result->{'totp-limit-reached'}) {
836 # FIXME: send mail to the user (or admin/root if no email configured)
837 die "failed 2nd factor: TOTP limit reached, locked\n";
838 }
839 if ($result->{'tfa-limit-reached'}) {
840 # FIXME: send mail to the user (or admin/root if no email configured)
841 die "failed 1nd factor: TFA limit reached, user locked out\n";
842 }
843 if (!$result->{result}) {
844 die "failed 2nd factor\n";
845 }
68095371 846 }
afb10353 847
68095371
DC
848 return $tfa_challenge;
849}
850
851# Returns a tfa challenge or undef.
852sub authenticate_2nd_new : prototype($$$$) {
61565fb2 853 my ($username, $realm, $tfa_response, $tfa_challenge) = @_;
68095371
DC
854
855 my $result;
856
61565fb2 857 if (defined($tfa_response) && $tfa_response =~ m/^recovery:/) {
68095371 858 $result = lock_tfa_config(sub {
61565fb2 859 authenticate_2nd_new_do($username, $realm, $tfa_response, $tfa_challenge);
68095371
DC
860 });
861 } else {
61565fb2 862 $result = authenticate_2nd_new_do($username, $realm, $tfa_response, $tfa_challenge);
68095371 863 }
6adcb18c
WB
864
865 # Yubico auth returns the authentication sub:
866 if (ref($result) eq 'CODE') {
867 $result = $result->();
868 }
869
870 return $result;
871}
872
873sub authenticate_yubico_new : prototype($$$) {
874 my ($tfa_cfg, $username, $realm, $tfa_challenge, $otp) = @_;
875
876 $tfa_challenge = verify_ticket($tfa_challenge, 0, $username);
877 $tfa_challenge = from_json($tfa_challenge);
878
879 if (!$tfa_challenge->{yubico}) {
880 die "no such challenge\n";
881 }
882
883 my $keys = $tfa_cfg->get_yubico_keys($username);
884 die "no keys configured\n" if !defined($keys) || !length($keys);
885
8c1e3ab3 886 authenticate_yubico_do($otp, $keys, $realm);
6adcb18c
WB
887
888 # return `undef` to clear the tfa challenge.
889 return undef;
afb10353
WB
890}
891
8c1e3ab3
WB
892sub authenticate_yubico_do : prototype($$$) {
893 my ($value, $keys, $realm) = @_;
894
895 # fixme: proxy support?
896 my $proxy = undef;
897
898 PVE::OTP::yubico_verify_otp($value, $keys, $realm->{url}, $realm->{id}, $realm->{key}, $proxy);
899}
900
afb10353
WB
901sub configure_u2f_and_wa : prototype($) {
902 my ($tfa_cfg) = @_;
903
d12f247e
WB
904 my $rpc_origin;
905 my $get_origin = sub {
906 return $rpc_origin if defined($rpc_origin);
907 my $rpcenv = PVE::RPCEnvironment::get();
908 if (my $origin = $rpcenv->get_request_host(1)) {
909 $rpc_origin = "https://$origin";
910 return $rpc_origin;
911 }
912 die "failed to figure out origin\n";
913 };
914
afb10353
WB
915 my $dc = cfs_read_file('datacenter.cfg');
916 if (my $u2f = $dc->{u2f}) {
280d0edd
WB
917 eval {
918 $tfa_cfg->set_u2f_config({
919 origin => $u2f->{origin} // $get_origin->(),
920 appid => $u2f->{appid},
921 });
922 };
923 warn "u2f unavailable, configuration error: $@\n" if $@;
afb10353
WB
924 }
925 if (my $wa = $dc->{webauthn}) {
28ec8972
WB
926 $wa->{origin} //= $get_origin->();
927 eval { $tfa_cfg->set_webauthn_config({%$wa}) };
280d0edd 928 warn "webauthn unavailable, configuration error: $@\n" if $@;
afb10353
WB
929 }
930}
931
2c3a6c0a 932sub domain_set_password {
5bb4e06a 933 my ($realm, $username, $password) = @_;
2c3a6c0a
DM
934
935 die "no auth domain specified" if !$realm;
936
5bb4e06a
DM
937 my $domain_cfg = cfs_read_file('domains.cfg');
938
939 my $cfg = $domain_cfg->{ids}->{$realm};
1075c589 940 die "auth domain '$realm' does not exist\n" if !$cfg;
5bb4e06a
DM
941 my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
942 $plugin->store_password($cfg, $realm, $username, $password);
2c3a6c0a
DM
943}
944
170cf17b
FG
945sub iterate_acl_tree {
946 my ($path, $node, $code) = @_;
947
948 $code->($path, $node);
949
950 $path = '' if $path eq '/'; # avoid leading '//'
951
952 my $children = $node->{children};
953
cca4c000 954 foreach my $child (sort keys %$children) {
170cf17b
FG
955 iterate_acl_tree("$path/$child", $children->{$child}, $code);
956 }
957}
958
959# find ACL node corresponding to normalized $path under $root
960sub find_acl_tree_node {
961 my ($root, $path) = @_;
962
963 my $split_path = [ split("/", $path) ];
964
965 if (!$split_path) {
966 return $root;
967 }
968
969 my $node = $root;
970 for my $p (@$split_path) {
971 next if !$p;
972
973 $node->{children} = {} if !$node->{children};
974 $node->{children}->{$p} = {} if !$node->{children}->{$p};
975
976 $node = $node->{children}->{$p};
977 }
978
979 return $node;
980}
981
2c3a6c0a 982sub add_user_group {
2c3a6c0a 983 my ($username, $usercfg, $group) = @_;
66931b11 984
2c3a6c0a
DM
985 $usercfg->{users}->{$username}->{groups}->{$group} = 1;
986 $usercfg->{groups}->{$group}->{users}->{$username} = 1;
987}
988
989sub delete_user_group {
2c3a6c0a 990 my ($username, $usercfg) = @_;
66931b11 991
2c3a6c0a
DM
992 foreach my $group (keys %{$usercfg->{groups}}) {
993
66931b11 994 delete ($usercfg->{groups}->{$group}->{users}->{$username})
2c3a6c0a
DM
995 if $usercfg->{groups}->{$group}->{users}->{$username};
996 }
997}
998
999sub delete_user_acl {
2c3a6c0a
DM
1000 my ($username, $usercfg) = @_;
1001
170cf17b
FG
1002 my $code = sub {
1003 my ($path, $acl_node) = @_;
2c3a6c0a 1004
170cf17b
FG
1005 delete ($acl_node->{users}->{$username})
1006 if $acl_node->{users}->{$username};
1007 };
1008
1009 iterate_acl_tree("/", $usercfg->{acl_root}, $code);
2c3a6c0a 1010}
39c85db8 1011
2c3a6c0a 1012sub delete_group_acl {
2c3a6c0a
DM
1013 my ($group, $usercfg) = @_;
1014
170cf17b
FG
1015 my $code = sub {
1016 my ($path, $acl_node) = @_;
2c3a6c0a 1017
170cf17b
FG
1018 delete ($acl_node->{groups}->{$group})
1019 if $acl_node->{groups}->{$group};
1020 };
1021
1022 iterate_acl_tree("/", $usercfg->{acl_root}, $code);
39c85db8
DM
1023}
1024
1025sub delete_pool_acl {
39c85db8 1026 my ($pool, $usercfg) = @_;
2c3a6c0a 1027
170cf17b 1028 delete ($usercfg->{acl_root}->{children}->{pool}->{children}->{$pool});
2c3a6c0a
DM
1029}
1030
1031# we automatically create some predefined roles by splitting privs
1032# into 3 groups (per category)
1033# root: only root is allowed to do that
1034# admin: an administrator can to that
1075c589 1035# user: a normal user/customer can to that
2c3a6c0a
DM
1036my $privgroups = {
1037 VM => {
1038 root => [],
66931b11
DM
1039 admin => [
1040 'VM.Config.Disk',
1041 'VM.Config.CPU',
1042 'VM.Config.Memory',
1043 'VM.Config.Network',
c0fead8c 1044 'VM.Config.HWType',
66931b11
DM
1045 'VM.Config.Options', # covers all other things
1046 'VM.Allocate',
1047 'VM.Clone',
2c3a6c0a 1048 'VM.Migrate',
66931b11
DM
1049 'VM.Monitor',
1050 'VM.Snapshot',
aad513f6 1051 'VM.Snapshot.Rollback',
2c3a6c0a
DM
1052 ],
1053 user => [
cc7bdf33 1054 'VM.Config.CDROM', # change CDROM media
cb381abc 1055 'VM.Config.Cloudinit',
66931b11 1056 'VM.Console',
68d5a86d 1057 'VM.Backup',
2c3a6c0a
DM
1058 'VM.PowerMgmt',
1059 ],
66931b11 1060 audit => [
82b63965 1061 'VM.Audit',
2c3a6c0a
DM
1062 ],
1063 },
1064 Sys => {
1065 root => [
66931b11 1066 'Sys.PowerMgmt',
37d45deb 1067 'Sys.Modify', # edit/change node settings
881dce13 1068 'Sys.Incoming', # incoming storage/guest migrations
36c18144 1069 'Sys.AccessNetwork', # for, e.g., downloading ISOs from any URL
2c3a6c0a
DM
1070 ],
1071 admin => [
66931b11 1072 'Sys.Console',
2c3a6c0a
DM
1073 'Sys.Syslog',
1074 ],
1075 user => [],
1076 audit => [
1077 'Sys.Audit',
1078 ],
1079 },
1080 Datastore => {
2e376c58 1081 root => [],
19f60b5e
DM
1082 admin => [
1083 'Datastore.Allocate',
373cb383 1084 'Datastore.AllocateTemplate',
19f60b5e 1085 ],
2c3a6c0a
DM
1086 user => [
1087 'Datastore.AllocateSpace',
1088 ],
1089 audit => [
1090 'Datastore.Audit',
1091 ],
1092 },
40672671
AD
1093 SDN => {
1094 root => [],
1095 admin => [
1096 'SDN.Allocate',
1097 'SDN.Audit',
1098 ],
a62d78db
AD
1099 user => [
1100 'SDN.Use',
1101 ],
40672671
AD
1102 audit => [
1103 'SDN.Audit',
1104 ],
1105 },
12683df7 1106 User => {
82b63965
DM
1107 root => [
1108 'Realm.Allocate',
1109 ],
12683df7
DM
1110 admin => [
1111 'User.Modify',
82b63965 1112 'Group.Allocate', # edit/change group settings
66931b11 1113 'Realm.AllocateUser',
19f60b5e 1114 ],
12683df7
DM
1115 user => [],
1116 audit => [],
1117 },
dee1c882
DM
1118 Pool => {
1119 root => [],
1120 admin => [
1121 'Pool.Allocate', # create/delete pools
1122 ],
6d048ad6
LS
1123 user => [
1124 'Pool.Audit',
1125 ],
1126 audit => [
1127 'Pool.Audit',
1128 ],
dee1c882 1129 },
8b5fd2e6
DC
1130 Mapping => {
1131 root => [],
1132 admin => [
1133 'Mapping.Modify',
1134 ],
1135 user => [
1136 'Mapping.Use',
1137 ],
1138 audit => [
1139 'Mapping.Audit',
1140 ],
1141 },
2c3a6c0a
DM
1142};
1143
df619a8d
FG
1144my $valid_privs = {
1145 'Permissions.Modify' => 1, # not contained in a group
1146};
2c3a6c0a
DM
1147
1148my $special_roles = {
1075c589
FG
1149 'NoAccess' => {}, # no privileges
1150 'Administrator' => $valid_privs, # all privileges
2c3a6c0a
DM
1151};
1152
1153sub create_roles {
1154
0aa00d13 1155 for my $cat (keys %$privgroups) {
2c3a6c0a 1156 my $cd = $privgroups->{$cat};
0aa00d13
TL
1157 # create map to easily check if a privilege is valid
1158 for my $priv (@{$cd->{root}}, @{$cd->{admin}}, @{$cd->{user}}, @{$cd->{audit}}) {
1159 $valid_privs->{$priv} = 1;
2c3a6c0a 1160 }
0aa00d13
TL
1161 # create grouped admin roles and PVEAdmin
1162 for my $priv (@{$cd->{admin}}, @{$cd->{user}}, @{$cd->{audit}}) {
1163 $special_roles->{"PVE${cat}Admin"}->{$priv} = 1;
1164 $special_roles->{"PVEAdmin"}->{$priv} = 1;
2c3a6c0a 1165 }
0aa00d13 1166 # create grouped user and audit roles
2c3a6c0a 1167 if (scalar(@{$cd->{user}})) {
0aa00d13
TL
1168 for my $priv (@{$cd->{user}}, @{$cd->{audit}}) {
1169 $special_roles->{"PVE${cat}User"}->{$priv} = 1;
2c3a6c0a
DM
1170 }
1171 }
0aa00d13
TL
1172 for my $priv (@{$cd->{audit}}) {
1173 $special_roles->{"PVEAuditor"}->{$priv} = 1;
2c3a6c0a
DM
1174 }
1175 }
ff4b2235 1176
8b5fd2e6
DC
1177 # remove Mapping.Modify from PVEAdmin, only Administrator, root@pam and
1178 # PVEMappingAdmin should be able to use that for now
1179 delete $special_roles->{"PVEAdmin"}->{"Mapping.Modify"};
1180
7b395f99 1181 $special_roles->{"PVETemplateUser"} = { 'VM.Clone' => 1, 'VM.Audit' => 1 };
2c3a6c0a
DM
1182};
1183
1184create_roles();
1185
0fea3f16
DC
1186sub create_priv_properties {
1187 my $properties = {};
1188 foreach my $priv (keys %$valid_privs) {
1189 $properties->{$priv} = {
1190 type => 'boolean',
1191 optional => 1,
1192 };
1193 }
1194 return $properties;
1195}
1196
894e6f0c
PA
1197sub role_is_special {
1198 my ($role) = @_;
b7ba86d4 1199 return (exists $special_roles->{$role}) ? 1 : 0;
894e6f0c
PA
1200}
1201
2c3a6c0a
DM
1202sub add_role_privs {
1203 my ($role, $usercfg, $privs) = @_;
1204
1205 return if !$privs;
1206
1207 die "role '$role' does not exist\n" if !$usercfg->{roles}->{$role};
1208
1209 foreach my $priv (split_list($privs)) {
1210 if (defined ($valid_privs->{$priv})) {
1211 $usercfg->{roles}->{$role}->{$priv} = 1;
1212 } else {
1075c589 1213 die "invalid privilege '$priv'\n";
66931b11
DM
1214 }
1215 }
2c3a6c0a
DM
1216}
1217
eb41d200 1218sub lookup_username {
f335d265 1219 my ($username, $noerr) = @_;
eb41d200
WL
1220
1221 $username =~ m!^(${PVE::Auth::Plugin::user_regex})\@(${PVE::Auth::Plugin::realm_regex})$!;
1222
1223 my $realm = $2;
1224 my $domain_cfg = cfs_read_file("domains.cfg");
1225 my $casesensitive = $domain_cfg->{ids}->{$realm}->{'case-sensitive'} // 1;
1226 my $usercfg = cfs_read_file('user.cfg');
1227
1228 if (!$casesensitive) {
1229 my @matches = grep { lc $username eq lc $_ } (keys %{$usercfg->{users}});
1230
1231 die "ambiguous case insensitive match of username '$username', cannot safely grant access!\n"
f335d265 1232 if scalar @matches > 1 && !$noerr;
eb41d200
WL
1233
1234 return $matches[0]
1235 }
1236
1237 return $username;
1238}
1239
2c3a6c0a
DM
1240sub normalize_path {
1241 my $path = shift;
1242
37d3c16b
FG
1243 return undef if !$path;
1244
4bc17477 1245 $path =~ s|/+|/|g;
2c3a6c0a
DM
1246
1247 $path =~ s|/$||;
1248
1249 $path = '/' if !$path;
1250
4bc17477
DM
1251 $path = "/$path" if $path !~ m|^/|;
1252
e4f8fc2e 1253 return undef if $path !~ m|^[[:alnum:]\.\-\_\/]+$|;
2c3a6c0a
DM
1254
1255 return $path;
66931b11 1256}
2c3a6c0a 1257
20c60513 1258sub check_path {
91c30089
TL
1259 my ($path) = @_;
1260 return $path =~ m!^(
20c60513
LS
1261 /
1262 |/access
1263 |/access/groups
8737ff37 1264 |/access/groups/[[:alnum:]\.\-\_]+
20c60513 1265 |/access/realm
8737ff37 1266 |/access/realm/[[:alnum:]\.\-\_]+
20c60513
LS
1267 |/nodes
1268 |/nodes/[[:alnum:]\.\-\_]+
1269 |/pool
e7224f6e 1270 |/pool/[A-Za-z0-9\.\-_]+(?:/[A-Za-z0-9\.\-_]+){0,2}
20c60513 1271 |/sdn
7b5d2abd
FG
1272 |/sdn/controllers
1273 |/sdn/controllers/[[:alnum:]\_\-]+
1274 |/sdn/dns
1275 |/sdn/dns/[[:alnum:]]+
1276 |/sdn/ipams
1277 |/sdn/ipams/[[:alnum:]]+
4d5b0937 1278 |/sdn/zones
0a06acb1 1279 |/sdn/zones/[[:alnum:]\.\-\_]+
4d5b0937
AD
1280 |/sdn/zones/[[:alnum:]\.\-\_]+/[[:alnum:]\.\-\_]+
1281 |/sdn/zones/[[:alnum:]\.\-\_]+/[[:alnum:]\.\-\_]+/[1-9][0-9]{0,3}
20c60513
LS
1282 |/storage
1283 |/storage/[[:alnum:]\.\-\_]+
1284 |/vms
ad1ef9fc 1285 |/vms/[1-9][0-9]{2,}
8b5fd2e6
DC
1286 |/mapping
1287 |/mapping/[[:alnum:]\.\-\_]+
1288 |/mapping/[[:alnum:]\.\-\_]+/[[:alnum:]\.\-\_]+
20c60513
LS
1289 )$!xs;
1290}
1291
2c3a6c0a
DM
1292PVE::JSONSchema::register_format('pve-groupid', \&verify_groupname);
1293sub verify_groupname {
1294 my ($groupname, $noerr) = @_;
1295
1296 if ($groupname !~ m/^[A-Za-z0-9\.\-_]+$/) {
1297
1298 die "group name '$groupname' contains invalid characters\n" if !$noerr;
1299
1300 return undef;
1301 }
66931b11 1302
2c3a6c0a
DM
1303 return $groupname;
1304}
1305
1306PVE::JSONSchema::register_format('pve-roleid', \&verify_rolename);
1307sub verify_rolename {
1308 my ($rolename, $noerr) = @_;
1309
1310 if ($rolename !~ m/^[A-Za-z0-9\.\-_]+$/) {
1311
1312 die "role name '$rolename' contains invalid characters\n" if !$noerr;
1313
1314 return undef;
1315 }
66931b11 1316
2c3a6c0a
DM
1317 return $rolename;
1318}
1319
16e50b59 1320PVE::JSONSchema::register_format('pve-poolid', \&verify_poolname);
39c85db8
DM
1321sub verify_poolname {
1322 my ($poolname, $noerr) = @_;
1323
e7224f6e
FG
1324 if (split("/", $poolname) > 3) {
1325 die "pool name '$poolname' nested too deeply (max levels = 3)\n" if !$noerr;
39c85db8 1326
e7224f6e
FG
1327 return undef;
1328 }
1329
1330 # also adapt check_path above if changed!
1331 if ($poolname !~ m!^[A-Za-z0-9\.\-_]+(?:/[A-Za-z0-9\.\-_]+){0,2}$!) {
39c85db8
DM
1332 die "pool name '$poolname' contains invalid characters\n" if !$noerr;
1333
1334 return undef;
1335 }
66931b11 1336
39c85db8
DM
1337 return $poolname;
1338}
1339
2c3a6c0a
DM
1340PVE::JSONSchema::register_format('pve-priv', \&verify_privname);
1341sub verify_privname {
1342 my ($priv, $noerr) = @_;
1343
1344 if (!$valid_privs->{$priv}) {
1075c589 1345 die "invalid privilege '$priv'\n" if !$noerr;
2c3a6c0a
DM
1346
1347 return undef;
1348 }
66931b11 1349
2c3a6c0a
DM
1350 return $priv;
1351}
1352
1353sub userconfig_force_defaults {
1354 my ($cfg) = @_;
1355
1356 foreach my $r (keys %$special_roles) {
1357 $cfg->{roles}->{$r} = $special_roles->{$r};
1358 }
1359
7279f31c
WL
1360 # add root user if not exists
1361 if (!$cfg->{users}->{'root@pam'}) {
66931b11 1362 $cfg->{users}->{'root@pam'}->{enable} = 1;
7279f31c 1363 }
170cf17b
FG
1364
1365 # add (empty) ACL tree root node
1366 if (!$cfg->{acl_root}) {
1367 $cfg->{acl_root} = {};
1368 }
2c3a6c0a
DM
1369}
1370
1371sub parse_user_config {
1372 my ($filename, $raw) = @_;
1373
1374 my $cfg = {};
1375
1376 userconfig_force_defaults($cfg);
1377
d6eb6621 1378 $raw = '' if !defined($raw);
62af314a 1379 while ($raw =~ /^\s*(.+?)\s*$/gm) {
2c3a6c0a 1380 my $line = $1;
2c3a6c0a
DM
1381 my @data;
1382
1383 foreach my $d (split (/:/, $line)) {
66931b11 1384 $d =~ s/^\s+//;
2c3a6c0a
DM
1385 $d =~ s/\s+$//;
1386 push @data, $d
1387 }
1388
1389 my $et = shift @data;
1390
1391 if ($et eq 'user') {
96f8ebd6 1392 my ($user, $enable, $expire, $firstname, $lastname, $email, $comment, $keys) = @data;
2c3a6c0a 1393
5bb4e06a 1394 my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1);
2c3a6c0a
DM
1395 if (!$realm) {
1396 warn "user config - ignore user '$user' - invalid user name\n";
1397 next;
1398 }
1399
1400 $enable = $enable ? 1 : 0;
1401
1402 $expire = 0 if !$expire;
1403
1404 if ($expire !~ m/^\d+$/) {
1405 warn "user config - ignore user '$user' - (illegal characters in expire '$expire')\n";
1406 next;
1407 }
1408 $expire = int($expire);
1409
1410 #if (!verify_groupname ($group, 1)) {
1411 # warn "user config - ignore user '$user' - invalid characters in group name\n";
1412 # next;
1413 #}
1414
1415 $cfg->{users}->{$user} = {
1416 enable => $enable,
1417 # group => $group,
1418 };
1419 $cfg->{users}->{$user}->{firstname} = PVE::Tools::decode_text($firstname) if $firstname;
1420 $cfg->{users}->{$user}->{lastname} = PVE::Tools::decode_text($lastname) if $lastname;
1421 $cfg->{users}->{$user}->{email} = $email;
1422 $cfg->{users}->{$user}->{comment} = PVE::Tools::decode_text($comment) if $comment;
1423 $cfg->{users}->{$user}->{expire} = $expire;
1abc2c0a 1424 # keys: allowed yubico key ids or oath secrets (base32 encoded)
66931b11 1425 $cfg->{users}->{$user}->{keys} = $keys if $keys;
2c3a6c0a
DM
1426
1427 #$cfg->{users}->{$user}->{groups}->{$group} = 1;
1428 #$cfg->{groups}->{$group}->{$user} = 1;
1429
1430 } elsif ($et eq 'group') {
1431 my ($group, $userlist, $comment) = @data;
1432
1433 if (!verify_groupname($group, 1)) {
1434 warn "user config - ignore group '$group' - invalid characters in group name\n";
1435 next;
1436 }
1437
1438 # make sure to add the group (even if there are no members)
1439 $cfg->{groups}->{$group} = { users => {} } if !$cfg->{groups}->{$group};
1440
1441 $cfg->{groups}->{$group}->{comment} = PVE::Tools::decode_text($comment) if $comment;
1442
1443 foreach my $user (split_list($userlist)) {
1444
5bb4e06a 1445 if (!PVE::Auth::Plugin::verify_username($user, 1)) {
2c3a6c0a
DM
1446 warn "user config - ignore invalid group member '$user'\n";
1447 next;
1448 }
1449
66931b11 1450 if ($cfg->{users}->{$user}) { # user exists
2c3a6c0a 1451 $cfg->{users}->{$user}->{groups}->{$group} = 1;
2c3a6c0a
DM
1452 } else {
1453 warn "user config - ignore invalid group member '$user'\n";
1454 }
5654260e 1455 $cfg->{groups}->{$group}->{users}->{$user} = 1;
2c3a6c0a
DM
1456 }
1457
1458 } elsif ($et eq 'role') {
1459 my ($role, $privlist) = @data;
66931b11 1460
2c3a6c0a
DM
1461 if (!verify_rolename($role, 1)) {
1462 warn "user config - ignore role '$role' - invalid characters in role name\n";
1463 next;
1464 }
1465
1466 # make sure to add the role (even if there are no privileges)
1467 $cfg->{roles}->{$role} = {} if !$cfg->{roles}->{$role};
1468
1469 foreach my $priv (split_list($privlist)) {
1470 if (defined ($valid_privs->{$priv})) {
1471 $cfg->{roles}->{$role}->{$priv} = 1;
1472 } else {
1516bfa0 1473 warn "user config - ignore invalid privilege '$priv'\n";
66931b11 1474 }
2c3a6c0a 1475 }
66931b11 1476
2c3a6c0a
DM
1477 } elsif ($et eq 'acl') {
1478 my ($propagate, $pathtxt, $uglist, $rolelist) = @data;
1479
733371da
FG
1480 $propagate = $propagate ? 1 : 0;
1481
2c3a6c0a 1482 if (my $path = normalize_path($pathtxt)) {
170cf17b 1483 my $acl_node;
2c3a6c0a 1484 foreach my $role (split_list($rolelist)) {
66931b11 1485
2c3a6c0a
DM
1486 if (!verify_rolename($role, 1)) {
1487 warn "user config - ignore invalid role name '$role' in acl\n";
1488 next;
1489 }
1490
21f523a5
FG
1491 if (!$cfg->{roles}->{$role}) {
1492 warn "user config - ignore invalid acl role '$role'\n";
1493 next;
1494 }
1495
2c3a6c0a 1496 foreach my $ug (split_list($uglist)) {
508e11f1
FG
1497 my ($group) = $ug =~ m/^@(\S+)$/;
1498
1499 if ($group && verify_groupname($group, 1)) {
5654260e 1500 if (!$cfg->{groups}->{$group}) { # group does not exist
2c3a6c0a
DM
1501 warn "user config - ignore invalid acl group '$group'\n";
1502 }
170cf17b
FG
1503 $acl_node = find_acl_tree_node($cfg->{acl_root}, $path) if !$acl_node;
1504 $acl_node->{groups}->{$group}->{$role} = $propagate;
5bb4e06a 1505 } elsif (PVE::Auth::Plugin::verify_username($ug, 1)) {
5654260e 1506 if (!$cfg->{users}->{$ug}) { # user does not exist
2c3a6c0a
DM
1507 warn "user config - ignore invalid acl member '$ug'\n";
1508 }
170cf17b
FG
1509 $acl_node = find_acl_tree_node($cfg->{acl_root}, $path) if !$acl_node;
1510 $acl_node->{users}->{$ug}->{$role} = $propagate;
28e3dc05 1511 } elsif (my ($user, $token) = split_tokenid($ug, 1)) {
571e9d06 1512 if (check_token_exist($cfg, $user, $token, 1)) {
170cf17b
FG
1513 $acl_node = find_acl_tree_node($cfg->{acl_root}, $path) if !$acl_node;
1514 $acl_node->{tokens}->{$ug}->{$role} = $propagate;
28e3dc05
FG
1515 } else {
1516 warn "user config - ignore invalid acl token '$ug'\n";
1517 }
2c3a6c0a
DM
1518 } else {
1519 warn "user config - invalid user/group '$ug' in acl\n";
1520 }
1521 }
1522 }
1523 } else {
1524 warn "user config - ignore invalid path in acl '$pathtxt'\n";
1525 }
4bc17477 1526 } elsif ($et eq 'pool') {
39c85db8 1527 my ($pool, $comment, $vmlist, $storelist) = @data;
4bc17477 1528
39c85db8
DM
1529 if (!verify_poolname($pool, 1)) {
1530 warn "user config - ignore pool '$pool' - invalid characters in pool name\n";
1531 next;
1532 }
4bc17477 1533
39c85db8 1534 # make sure to add the pool (even if there are no members)
4418b06b
FG
1535 $cfg->{pools}->{$pool} = { vms => {}, storage => {}, pools => {} }
1536 if !$cfg->{pools}->{$pool};
1537
1538 if ($pool =~ m!/!) {
1539 my $curr = $pool;
1540 while ($curr =~ m!^(.+)/[^/]+$!) {
1541 # ensure nested pool info is correctly recorded
1542 my $parent = $1;
1543 $cfg->{pools}->{$curr}->{parent} = $parent;
1544 $cfg->{pools}->{$parent} = { vms => {}, storage => {}, pools => {} }
1545 if !$cfg->{pools}->{$parent};
1546 $cfg->{pools}->{$parent}->{pools}->{$curr} = 1;
1547 $curr = $parent;
1548 }
1549 }
4bc17477 1550
39c85db8 1551 $cfg->{pools}->{$pool}->{comment} = PVE::Tools::decode_text($comment) if $comment;
4bc17477 1552
39c85db8
DM
1553 foreach my $vmid (split_list($vmlist)) {
1554 if ($vmid !~ m/^\d+$/) {
1555 warn "user config - ignore invalid vmid '$vmid' in pool '$pool'\n";
1556 next;
4bc17477 1557 }
39c85db8 1558 $vmid = int($vmid);
4bc17477 1559
39c85db8
DM
1560 if ($cfg->{vms}->{$vmid}) {
1561 warn "user config - ignore duplicate vmid '$vmid' in pool '$pool'\n";
1562 next;
4bc17477
DM
1563 }
1564
39c85db8 1565 $cfg->{pools}->{$pool}->{vms}->{$vmid} = 1;
66931b11 1566
39c85db8
DM
1567 # record vmid ==> pool relation
1568 $cfg->{vms}->{$vmid} = $pool;
1569 }
1570
1571 foreach my $storeid (split_list($storelist)) {
1572 if ($storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
1573 warn "user config - ignore invalid storage '$storeid' in pool '$pool'\n";
1574 next;
1575 }
1576 $cfg->{pools}->{$pool}->{storage}->{$storeid} = 1;
4bc17477 1577 }
28e3dc05
FG
1578 } elsif ($et eq 'token') {
1579 my ($tokenid, $expire, $privsep, $comment) = @data;
1580
1581 my ($user, $token) = split_tokenid($tokenid, 1);
1582 if (!($user && $token)) {
1583 warn "user config - ignore invalid tokenid '$tokenid'\n";
1584 next;
1585 }
1586
1587 $privsep = $privsep ? 1 : 0;
1588
1589 $expire = 0 if !$expire;
1590
1591 if ($expire !~ m/^\d+$/) {
1592 warn "user config - ignore token '$tokenid' - (illegal characters in expire '$expire')\n";
1593 next;
1594 }
1595 $expire = int($expire);
1596
1597 if (my $user_cfg = $cfg->{users}->{$user}) { # user exists
1598 $user_cfg->{tokens}->{$token} = {} if !$user_cfg->{tokens}->{$token};
1599 my $token_cfg = $user_cfg->{tokens}->{$token};
1600 $token_cfg->{privsep} = $privsep;
1601 $token_cfg->{expire} = $expire;
1602 $token_cfg->{comment} = PVE::Tools::decode_text($comment) if $comment;
1603 } else {
1604 warn "user config - ignore token '$tokenid' - user does not exist\n";
1605 }
2c3a6c0a
DM
1606 } else {
1607 warn "user config - ignore config line: $line\n";
1608 }
1609 }
1610
1611 userconfig_force_defaults($cfg);
1612
1613 return $cfg;
1614}
1615
2c3a6c0a
DM
1616sub write_user_config {
1617 my ($filename, $cfg) = @_;
1618
1619 my $data = '';
1620
93c7e9c3 1621 foreach my $user (sort keys %{$cfg->{users}}) {
2c3a6c0a
DM
1622 my $d = $cfg->{users}->{$user};
1623 my $firstname = $d->{firstname} ? PVE::Tools::encode_text($d->{firstname}) : '';
1624 my $lastname = $d->{lastname} ? PVE::Tools::encode_text($d->{lastname}) : '';
1625 my $email = $d->{email} || '';
1626 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
5eabc984 1627 my $expire = int($d->{expire} || 0);
2c3a6c0a 1628 my $enable = $d->{enable} ? 1 : 0;
96f8ebd6
DM
1629 my $keys = $d->{keys} ? $d->{keys} : '';
1630 $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:$keys:\n";
28e3dc05
FG
1631
1632 my $user_tokens = $d->{tokens};
1633 foreach my $token (sort keys %$user_tokens) {
1634 my $td = $user_tokens->{$token};
1635 my $full_tokenid = join_tokenid($user, $token);
1636 my $comment = $td->{comment} ? PVE::Tools::encode_text($td->{comment}) : '';
1637 my $expire = int($td->{expire} || 0);
1638 my $privsep = $td->{privsep} ? 1 : 0;
1639 $data .= "token:$full_tokenid:$expire:$privsep:$comment:\n";
1640 }
2c3a6c0a
DM
1641 }
1642
1643 $data .= "\n";
1644
93c7e9c3 1645 foreach my $group (sort keys %{$cfg->{groups}}) {
2c3a6c0a 1646 my $d = $cfg->{groups}->{$group};
a5ec58ea 1647 my $list = join (',', sort keys %{$d->{users}});
66931b11 1648 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
2c3a6c0a
DM
1649 $data .= "group:$group:$list:$comment:\n";
1650 }
1651
1652 $data .= "\n";
1653
93c7e9c3 1654 foreach my $pool (sort keys %{$cfg->{pools}}) {
39c85db8 1655 my $d = $cfg->{pools}->{$pool};
a5ec58ea
FG
1656 my $vmlist = join (',', sort keys %{$d->{vms}});
1657 my $storelist = join (',', sort keys %{$d->{storage}});
66931b11 1658 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
39c85db8 1659 $data .= "pool:$pool:$comment:$vmlist:$storelist:\n";
4bc17477
DM
1660 }
1661
1662 $data .= "\n";
1663
93c7e9c3 1664 foreach my $role (sort keys %{$cfg->{roles}}) {
2c3a6c0a
DM
1665 next if $special_roles->{$role};
1666
1667 my $d = $cfg->{roles}->{$role};
a5ec58ea 1668 my $list = join (',', sort keys %$d);
2c3a6c0a
DM
1669 $data .= "role:$role:$list:\n";
1670 }
1671
1672 $data .= "\n";
1673
9a12a08c
FG
1674 my $collect_rolelist_members = sub {
1675 my ($acl_members, $result, $prefix, $exclude) = @_;
2c3a6c0a 1676
9a12a08c
FG
1677 foreach my $member (keys %$acl_members) {
1678 next if $exclude && $member eq $exclude;
2c3a6c0a 1679
2c3a6c0a
DM
1680 my $l0 = '';
1681 my $l1 = '';
9a12a08c
FG
1682 foreach my $role (sort keys %{$acl_members->{$member}}) {
1683 my $propagate = $acl_members->{$member}->{$role};
2c3a6c0a
DM
1684 if ($propagate) {
1685 $l1 .= ',' if $l1;
1686 $l1 .= $role;
1687 } else {
1688 $l0 .= ',' if $l0;
1689 $l0 .= $role;
1690 }
1691 }
9a12a08c
FG
1692 $result->{0}->{$l0}->{"${prefix}${member}"} = 1 if $l0;
1693 $result->{1}->{$l1}->{"${prefix}${member}"} = 1 if $l1;
2c3a6c0a 1694 }
9a12a08c 1695 };
2c3a6c0a 1696
170cf17b
FG
1697 iterate_acl_tree("/", $cfg->{acl_root}, sub {
1698 my ($path, $d) = @_;
2c3a6c0a 1699
9a12a08c 1700 my $rolelist_members = {};
2c3a6c0a 1701
9a12a08c
FG
1702 $collect_rolelist_members->($d->{'groups'}, $rolelist_members, '@');
1703
1704 # no need to save 'root@pam', it is always 'Administrator'
1705 $collect_rolelist_members->($d->{'users'}, $rolelist_members, '', 'root@pam');
1706
28e3dc05
FG
1707 $collect_rolelist_members->($d->{'tokens'}, $rolelist_members, '');
1708
9a12a08c
FG
1709 foreach my $propagate (0,1) {
1710 my $filtered = $rolelist_members->{$propagate};
1711 foreach my $rolelist (sort keys %$filtered) {
1712 my $uglist = join (',', sort keys %{$filtered->{$rolelist}});
1713 $data .= "acl:$propagate:$path:$uglist:$rolelist:\n";
1714 }
28e3dc05 1715
2c3a6c0a 1716 }
170cf17b 1717 });
2c3a6c0a
DM
1718
1719 return $data;
1720}
1721
57098eb8
WB
1722# Creates a `PVE::RS::TFA` instance from the raw config data.
1723# Its contained hash will also support the legacy functionality.
fda8ca85
WB
1724sub parse_priv_tfa_config {
1725 my ($filename, $raw) = @_;
1726
fda8ca85 1727 $raw = '' if !defined($raw);
57098eb8 1728 my $cfg = PVE::RS::TFA->new($raw);
fda8ca85 1729
57098eb8
WB
1730 # Purge invalid users:
1731 foreach my $user ($cfg->users()->@*) {
fda8ca85
WB
1732 my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1);
1733 if (!$realm) {
1734 warn "user tfa config - ignore user '$user' - invalid user name\n";
57098eb8 1735 $cfg->remove_user($user);
fda8ca85 1736 }
fda8ca85
WB
1737 }
1738
1739 return $cfg;
1740}
1741
1742sub write_priv_tfa_config {
1743 my ($filename, $cfg) = @_;
1744
57098eb8 1745 return $cfg->write();
fda8ca85
WB
1746}
1747
2c3a6c0a
DM
1748sub roles {
1749 my ($cfg, $user, $path) = @_;
1750
66931b11 1751 # NOTE: we do not consider pools here.
e915e9e4
FG
1752 # NOTE: for privsep tokens, this does not filter roles by those that the
1753 # corresponding user has.
a31f1d85 1754 # Use $rpcenv->permission() for any actual permission checks!
4bc17477 1755
2c3a6c0a
DM
1756 return 'Administrator' if $user eq 'root@pam'; # root can do anything
1757
37d3c16b
FG
1758 if (!defined($path)) {
1759 # this shouldn't happen!
1760 warn "internal error: ACL check called for undefined ACL path!\n";
1761 return {};
1762 }
1763
e915e9e4
FG
1764 if (pve_verify_tokenid($user, 1)) {
1765 my $tokenid = $user;
1766 my ($username, $token) = split_tokenid($tokenid);
1767
1768 my $token_info = $cfg->{users}->{$username}->{tokens}->{$token};
1769 return () if !$token_info;
1770
7e8bcaa7 1771 my $user_roles = roles($cfg, $username, $path);
e915e9e4
FG
1772
1773 # return full user privileges
7e8bcaa7 1774 return $user_roles if !$token_info->{privsep};
e915e9e4
FG
1775 }
1776
7e8bcaa7 1777 my $roles = {};
2c3a6c0a 1778
170cf17b
FG
1779 my $split = [ split("/", $path) ];
1780 if ($path eq '/') {
1781 $split = [ '' ];
1782 }
2c3a6c0a 1783
170cf17b
FG
1784 my $acl = $cfg->{acl_root};
1785 my $i = 0;
2c3a6c0a 1786
170cf17b
FG
1787 while (@$split) {
1788 my $p = shift @$split;
1789 my $final = !@$split;
1790 if ($p ne '') {
1791 $acl = $acl->{children}->{$p};
1792 }
2c3a6c0a
DM
1793
1794 #print "CHECKACL $path $p\n";
1795 #print "ACL $path = " . Dumper ($acl);
e915e9e4
FG
1796 if (my $ri = $acl->{tokens}->{$user}) {
1797 my $new;
1798 foreach my $role (keys %$ri) {
1799 my $propagate = $ri->{$role};
1800 if ($final || $propagate) {
1801 #print "APPLY ROLE $p $user $role\n";
1802 $new = {} if !$new;
7e8bcaa7 1803 $new->{$role} = $propagate;
e915e9e4
FG
1804 }
1805 }
1806 if ($new) {
7e8bcaa7 1807 $roles = $new; # overwrite previous settings
e915e9e4
FG
1808 next;
1809 }
1810 }
2c3a6c0a
DM
1811
1812 if (my $ri = $acl->{users}->{$user}) {
1813 my $new;
1814 foreach my $role (keys %$ri) {
1815 my $propagate = $ri->{$role};
1816 if ($final || $propagate) {
1817 #print "APPLY ROLE $p $user $role\n";
1818 $new = {} if !$new;
7e8bcaa7 1819 $new->{$role} = $propagate;
2c3a6c0a
DM
1820 }
1821 }
1822 if ($new) {
7e8bcaa7 1823 $roles = $new; # overwrite previous settings
2c3a6c0a
DM
1824 next; # user privs always override group privs
1825 }
1826 }
1827
1828 my $new;
1829 foreach my $g (keys %{$acl->{groups}}) {
1830 next if !$cfg->{groups}->{$g}->{users}->{$user};
1831 if (my $ri = $acl->{groups}->{$g}) {
1832 foreach my $role (keys %$ri) {
1833 my $propagate = $ri->{$role};
1834 if ($final || $propagate) {
1835 #print "APPLY ROLE $p \@$g $role\n";
1836 $new = {} if !$new;
7e8bcaa7 1837 $new->{$role} = $propagate;
2c3a6c0a
DM
1838 }
1839 }
1840 }
1841 }
1842 if ($new) {
7e8bcaa7 1843 $roles = $new; # overwrite previous settings
2c3a6c0a
DM
1844 next;
1845 }
2c3a6c0a
DM
1846 }
1847
7e8bcaa7
FG
1848 return { 'NoAccess' => $roles->{NoAccess} } if defined ($roles->{NoAccess});
1849 #return () if defined ($roles->{NoAccess});
66931b11 1850
7e8bcaa7 1851 #print "permission $user $path = " . Dumper ($roles);
2c3a6c0a
DM
1852
1853 #print "roles $user $path = " . join (',', @ra) . "\n";
1854
7e8bcaa7 1855 return $roles;
2c3a6c0a 1856}
66931b11 1857
3b4a3f94
AG
1858sub remove_vm_access {
1859 my ($vmid) = @_;
1860 my $delVMaccessFn = sub {
170cf17b 1861 my $usercfg = cfs_read_file("user.cfg");
57a70473 1862 my $modified;
3b4a3f94 1863
170cf17b
FG
1864 if (my $acl = $usercfg->{acl_root}->{children}->{vms}->{children}->{$vmid}) {
1865 delete $usercfg->{acl_root}->{children}->{vms}->{children}->{$vmid};
57a70473 1866 $modified = 1;
170cf17b
FG
1867 }
1868 if (my $pool = $usercfg->{vms}->{$vmid}) {
1869 if (my $data = $usercfg->{pools}->{$pool}) {
1870 delete $data->{vms}->{$vmid};
1871 delete $usercfg->{vms}->{$vmid};
57a70473 1872 $modified = 1;
170cf17b
FG
1873 }
1874 }
57a70473 1875 cfs_write_file("user.cfg", $usercfg) if $modified;
3b4a3f94
AG
1876 };
1877
1878 lock_user_config($delVMaccessFn, "access permissions cleanup for VM $vmid failed");
1879}
1880
60844761
AG
1881sub remove_storage_access {
1882 my ($storeid) = @_;
1883
1884 my $deleteStorageAccessFn = sub {
170cf17b 1885 my $usercfg = cfs_read_file("user.cfg");
60844761
AG
1886 my $modified;
1887
170cf17b
FG
1888 if (my $acl = $usercfg->{acl_root}->{children}->{storage}->{children}->{$storeid}) {
1889 delete $usercfg->{acl_root}->{children}->{storage}->{children}->{$storeid};
1890 $modified = 1;
1891 }
60844761
AG
1892 foreach my $pool (keys %{$usercfg->{pools}}) {
1893 delete $usercfg->{pools}->{$pool}->{storage}->{$storeid};
1894 $modified = 1;
1895 }
170cf17b 1896 cfs_write_file("user.cfg", $usercfg) if $modified;
60844761
AG
1897 };
1898
1899 lock_user_config($deleteStorageAccessFn,
1900 "access permissions cleanup for storage $storeid failed");
1901}
1902
018ae3a9
DM
1903sub add_vm_to_pool {
1904 my ($vmid, $pool) = @_;
1905
1906 my $addVMtoPoolFn = sub {
1907 my $usercfg = cfs_read_file("user.cfg");
1908 if (my $data = $usercfg->{pools}->{$pool}) {
1909 $data->{vms}->{$vmid} = 1;
1910 $usercfg->{vms}->{$vmid} = $pool;
1911 cfs_write_file("user.cfg", $usercfg);
1912 }
1913 };
1914
1915 lock_user_config($addVMtoPoolFn, "can't add VM $vmid to pool '$pool'");
1916}
1917
1918sub remove_vm_from_pool {
1919 my ($vmid) = @_;
66931b11 1920
018ae3a9
DM
1921 my $delVMfromPoolFn = sub {
1922 my $usercfg = cfs_read_file("user.cfg");
1923 if (my $pool = $usercfg->{vms}->{$vmid}) {
1924 if (my $data = $usercfg->{pools}->{$pool}) {
1925 delete $data->{vms}->{$vmid};
1926 delete $usercfg->{vms}->{$vmid};
1927 cfs_write_file("user.cfg", $usercfg);
1928 }
1929 }
1930 };
1931
1932 lock_user_config($delVMfromPoolFn, "pool cleanup for VM $vmid failed");
1933}
1934
49b15310 1935my $USER_CONTROLLED_TFA_TYPES = {
fda8ca85
WB
1936 u2f => 1,
1937 oath => 1,
1938};
1939
d168ab34
WB
1940sub user_remove_tfa : prototype($) {
1941 my ($userid) = @_;
1942
d168ab34
WB
1943 my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
1944 $tfa_cfg->remove_user($userid);
1945 cfs_write_file('priv/tfa.cfg', $tfa_cfg);
1946}
1947
0fe62fa8
WB
1948my sub add_old_yubico_keys : prototype($$$) {
1949 my ($userid, $tfa_cfg, $keys) = @_;
1950
1951 my $count = 0;
1952 foreach my $key (split_list($keys)) {
1953 my $description = "<old userconfig key $count>";
1954 ++$count;
1955 $tfa_cfg->add_yubico_entry($userid, $description, $key);
1956 }
1957}
1958
1959my sub normalize_totp_secret : prototype($) {
1960 my ($key) = @_;
1961
1962 my $binkey;
1963 # See PVE::OTP::oath_verify_otp:
1964 if ($key =~ /^v2-0x([0-9a-fA-F]+)$/) {
1965 # v2, hex
1966 $binkey = pack('H*', $1);
1967 } elsif ($key =~ /^v2-([A-Z2-7=]+)$/) {
1968 # v2, base32
1969 $binkey = MIME::Base32::decode_rfc3548($1);
1970 } elsif ($key =~ /^[A-Z2-7=]{16}$/) {
1971 $binkey = MIME::Base32::decode_rfc3548($key);
1972 } elsif ($key =~ /^[A-Fa-f0-9]{40}$/) {
1973 $binkey = pack('H*', $key);
1974 } else {
1975 return undef;
1976 }
1977
1978 return MIME::Base32::encode_rfc3548($binkey);
1979}
1980
1981my sub add_old_totp_keys : prototype($$$$) {
1982 my ($userid, $tfa_cfg, $realm_tfa, $keys) = @_;
1983
1984 my $issuer = 'Proxmox%20VE';
1985 my $account = uri_escape("Old key for $userid");
1986 my $digits = $realm_tfa->{digits} || 6;
1987 my $step = $realm_tfa->{step} || 30;
1988 my $uri = "otpauth://totp/$issuer:$account?digits=$digits&period=$step&algorithm=SHA1&secret=";
1989
1990 my $count = 0;
1991 foreach my $key (split_list($keys)) {
1992 $key = normalize_totp_secret($key);
1993 # and just skip invalid keys:
1994 next if !defined($key);
1995
1996 my $description = "<old userconfig key $count>";
1997 ++$count;
1998 eval { $tfa_cfg->add_totp_entry($userid, $description, $uri . $key) };
1999 warn $@ if $@;
2000 }
2001}
2002
2003sub add_old_keys_to_realm_tfa : prototype($$$$) {
2004 my ($userid, $tfa_cfg, $realm_tfa, $keys) = @_;
2005
2006 # if there's no realm tfa configured, we don't know what the keys mean, so we just ignore
2007 # them...
2008 return if !$realm_tfa;
2009
2010 my $type = $realm_tfa->{type};
2011 if ($type eq 'oath') {
2012 add_old_totp_keys($userid, $tfa_cfg, $realm_tfa, $keys);
2013 } elsif ($type eq 'yubico') {
2014 add_old_yubico_keys($userid, $tfa_cfg, $keys);
2015 } else {
2016 # invalid keys, we'll just drop them now...
2017 }
2018}
2019
afb10353 2020sub user_get_tfa : prototype($$$) {
cfd8636b 2021 my ($username, $realm) = @_;
fda8ca85
WB
2022
2023 my $user_cfg = cfs_read_file('user.cfg');
2024 my $user = $user_cfg->{users}->{$username}
2025 or die "user '$username' not found\n";
2026
2027 my $keys = $user->{keys};
fda8ca85
WB
2028
2029 my $domain_cfg = cfs_read_file('domains.cfg');
2030 my $realm_cfg = $domain_cfg->{ids}->{$realm};
2031 die "auth domain '$realm' does not exist\n" if !$realm_cfg;
2032
2033 my $realm_tfa = $realm_cfg->{tfa};
2034 $realm_tfa = PVE::Auth::Plugin::parse_tfa_config($realm_tfa)
2035 if $realm_tfa;
2036
cfd8636b
WB
2037 my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
2038 if (defined($keys) && $keys !~ /^x(?:!.*)$/) {
2039 add_old_keys_to_realm_tfa($username, $tfa_cfg, $realm_tfa, $keys);
fda8ca85 2040 }
0f3d14d6 2041
cfd8636b 2042 return ($tfa_cfg, $realm_tfa);
fda8ca85
WB
2043}
2044
3e5bfdf6
DM
2045# bash completion helpers
2046
ab7b19b5
SI
2047register_standard_option('userid-completed',
2048 get_standard_option('userid', { completion => \&complete_username}),
2049);
2050
3e5bfdf6
DM
2051sub complete_username {
2052
2053 my $user_cfg = cfs_read_file('user.cfg');
2054
2055 return [ keys %{$user_cfg->{users}} ];
2056}
2057
2058sub complete_group {
2059
2060 my $user_cfg = cfs_read_file('user.cfg');
2061
2062 return [ keys %{$user_cfg->{groups}} ];
2063}
2064
2065sub complete_realm {
2066
2067 my $domain_cfg = cfs_read_file('domains.cfg');
2068
2069 return [ keys %{$domain_cfg->{ids}} ];
2070}
2071
2c3a6c0a 20721;