]> git.proxmox.com Git - pve-access-control.git/blob - src/PVE/AccessControl.pm
bump version to 8.1.4
[pve-access-control.git] / src / PVE / AccessControl.pm
1 package PVE::AccessControl;
2
3 use strict;
4 use warnings;
5 use Encode;
6 use Crypt::OpenSSL::Random;
7 use Crypt::OpenSSL::RSA;
8 use Net::SSLeay;
9 use Net::IP;
10 use MIME::Base32;
11 use MIME::Base64;
12 use Digest::SHA;
13 use IO::File;
14 use File::stat;
15 use JSON;
16 use Scalar::Util 'weaken';
17 use URI::Escape;
18
19 use PVE::Exception qw(raise_perm_exc raise_param_exc);
20 use PVE::OTP;
21 use PVE::Ticket;
22 use PVE::Tools qw(run_command lock_file file_get_contents split_list safe_print);
23 use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
24 use PVE::JSONSchema qw(register_standard_option get_standard_option);
25
26 use PVE::RS::TFA;
27
28 use PVE::Auth::Plugin;
29 use PVE::Auth::AD;
30 use PVE::Auth::LDAP;
31 use PVE::Auth::PVE;
32 use PVE::Auth::PAM;
33 use PVE::Auth::OpenId;
34
35 # load and initialize all plugins
36
37 PVE::Auth::AD->register();
38 PVE::Auth::LDAP->register();
39 PVE::Auth::PVE->register();
40 PVE::Auth::PAM->register();
41 PVE::Auth::OpenId->register();
42 PVE::Auth::Plugin->init();
43
44 # $authdir must be writable by root only!
45 my $confdir = "/etc/pve";
46 my $authdir = "$confdir/priv";
47
48 my $pve_www_key_fn = "$confdir/pve-www.key";
49
50 my $pve_auth_key_files = {
51 priv => "$authdir/authkey.key",
52 pub => "$confdir/authkey.pub",
53 pubold => "$confdir/authkey.pub.old",
54 };
55
56 my $pve_auth_key_cache = {};
57
58 my $ticket_lifetime = 3600 * 2; # 2 hours
59 my $auth_graceperiod = 60 * 5; # 5 minutes
60 my $authkey_lifetime = 3600 * 24; # rotate every 24 hours
61
62 Crypt::OpenSSL::RSA->import_random_seed();
63
64 cfs_register_file('user.cfg', \&parse_user_config, \&write_user_config);
65 cfs_register_file('priv/tfa.cfg', \&parse_priv_tfa_config, \&write_priv_tfa_config);
66
67 sub verify_username {
68 PVE::Auth::Plugin::verify_username(@_);
69 }
70
71 sub pve_verify_realm {
72 PVE::Auth::Plugin::pve_verify_realm(@_);
73 }
74
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!
79 my $user_config_locked;
80 sub lock_user_config {
81 my ($code, $errmsg) = @_;
82
83 my $locked = 1;
84 $user_config_locked = \$locked;
85 weaken $user_config_locked; # make this scope guard signal safe...
86
87 cfs_lock_file("user.cfg", undef, $code);
88 $user_config_locked = undef;
89 if (my $err = $@) {
90 $errmsg ? die "$errmsg: $err" : die $err;
91 }
92 }
93
94 sub lock_tfa_config {
95 my ($code, $errmsg) = @_;
96
97 die "tfa config lock cannot be acquired while holding user config lock\n"
98 if ($user_config_locked && $$user_config_locked);
99
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
108 my $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
147 sub get_pubkey {
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
158 sub get_privkey {
159 my $res = $cache_read_key->('priv');
160
161 if (!defined($res) || !check_authkey(1)) {
162 rotate_authkey();
163 $res = $cache_read_key->('priv');
164 }
165
166 return wantarray ? ($res->{key}, $res->{mtime}) : $res->{key};
167 }
168
169 sub 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 {
180 my $now = time();
181 if ($now - $mtime >= $authkey_lifetime) {
182 warn "auth key pair too old, rotating..\n" if !$quiet;;
183 return 0;
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;
196 } else {
197 warn "auth key new enough, skipping rotation\n" if !$quiet;;
198 return 1;
199 }
200 }
201 }
202
203 sub rotate_authkey {
204 return if $authkey_lifetime == 0;
205
206 PVE::Cluster::cfs_lock_authkey(undef, sub {
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.
217 return if check_authkey();
218
219 my $old = get_pubkey();
220 my $new = Crypt::OpenSSL::RSA->generate_key(2048);
221
222 if ($old) {
223 eval {
224 my $pem = $old->get_public_key_x509_string();
225 # mtime is used for caching and ticket age range calculation
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
231 eval {
232 my $pem = $new->get_public_key_x509_string();
233 # mtime is used for caching and ticket age range calculation,
234 # should be close to that of pubold above
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 $@;
263 }
264
265 PVE::JSONSchema::register_standard_option('tokenid', {
266 description => "API token identifier.",
267 type => "string",
268 format => "pve-tokenid",
269 });
270
271 our $token_subid_regex = $PVE::Auth::Plugin::realm_regex;
272
273 # username@realm username realm tokenid
274 our $token_full_regex = qr/((${PVE::Auth::Plugin::user_regex})\@(${PVE::Auth::Plugin::realm_regex}))!(${token_subid_regex})/;
275
276 our $userid_or_token_regex = qr/^$PVE::Auth::Plugin::user_regex\@$PVE::Auth::Plugin::realm_regex(?:!$token_subid_regex)?$/;
277
278 sub 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
290 sub join_tokenid {
291 my ($username, $tokensubid) = @_;
292
293 my $joined = "${username}!${tokensubid}";
294
295 return pve_verify_tokenid($joined);
296 }
297
298 PVE::JSONSchema::register_format('pve-tokenid', \&pve_verify_tokenid);
299 sub 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
312 my $csrf_prevention_secret;
313 my $csrf_prevention_secret_legacy;
314 my $get_csrfr_secret = sub {
315 if (!$csrf_prevention_secret) {
316 my $input = PVE::Tools::file_get_contents($pve_www_key_fn);
317 $csrf_prevention_secret = Digest::SHA::hmac_sha256_base64($input);
318 $csrf_prevention_secret_legacy = Digest::SHA::sha1_base64($input);
319 }
320 return $csrf_prevention_secret;
321 };
322
323 sub assemble_csrf_prevention_token {
324 my ($username) = @_;
325
326 my $secret = &$get_csrfr_secret();
327
328 return PVE::Ticket::assemble_csrf_prevention_token ($secret, $username);
329 }
330
331 sub verify_csrf_prevention_token {
332 my ($username, $token, $noerr) = @_;
333
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 }
344
345 return PVE::Ticket::verify_csrf_prevention_token(
346 $secret, $username, $token, -$auth_graceperiod, $ticket_lifetime, $noerr);
347 }
348
349 my $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
355 my $min = -$auth_graceperiod;
356 my $max = $ticket_lifetime;
357
358 if ($rotated) {
359 # ticket creation after rotation is not allowed
360 $min = $key_age - $auth_graceperiod;
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
371 $max = $key_age + $auth_graceperiod if $key_age < $ticket_lifetime;
372 }
373
374 return undef if $min > $ticket_lifetime;
375 return ($min, $max);
376 };
377
378 sub assemble_ticket : prototype($;$) {
379 my ($data, $aad) = @_;
380
381 my $rsa_priv = get_privkey();
382
383 return PVE::Ticket::assemble_rsa_ticket($rsa_priv, 'PVE', $data, $aad);
384 }
385
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.
393 sub verify_ticket : prototype($;$$) {
394 my ($ticket, $noerr, $tfa_ticket_aad) = @_;
395
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);
405 return undef if !defined($min);
406
407 return PVE::Ticket::verify_rsa_ticket(
408 $rsa_pub, 'PVE', $ticket, $tfa_ticket_aad, $min, $max, 1);
409 };
410
411 my ($data, $age) = $check->();
412
413 # check with old, rotated key if current key failed
414 ($data, $age) = $check->(1) if !defined($data);
415
416 my $auth_failure = sub {
417 if ($noerr) {
418 return undef;
419 } else {
420 # raise error via undef ticket
421 PVE::Ticket::verify_rsa_ticket(undef, 'PVE');
422 }
423 };
424
425 if (!defined($data)) {
426 return $auth_failure->();
427 }
428
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
437 my ($username, $tfa_info);
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 }
444 if ($data =~ m{^u2f!([^!]+)!([0-9a-zA-Z/.=_\-+]+)$}) {
445 # Ticket for u2f-users:
446 ($username, my $challenge) = ($1, $2);
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 }
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' };
464 } else {
465 # Regular ticket (full access)
466 $username = $data;
467 }
468
469 return undef if !PVE::Auth::Plugin::verify_username($username, $noerr);
470
471 return wantarray ? ($username, $age, $tfa_info) : $username;
472 }
473
474 sub 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
493 my $user = $usercfg->{users}->{$username};
494 my $token_info = $user->{tokens}->{$token};
495
496 my $ctime = time();
497 die "token '$token' access expired\n" if $token_info->{expire} && ($token_info->{expire} < $ctime);
498
499 die "invalid token value!\n" if !PVE::Cluster::verify_token($tokenid, $value);
500
501 return wantarray ? ($tokenid) : $tokenid;
502 }
503
504 my $assemble_short_lived_ticket = sub {
505 my ($prefix, $username, $path) = @_;
506
507 my $rsa_priv = get_privkey();
508
509 $path = normalize_path($path);
510
511 die "invalid ticket path\n" if !defined($path);
512
513 my $secret_data = "$username:$path";
514
515 return PVE::Ticket::assemble_rsa_ticket(
516 $rsa_priv, $prefix, undef, $secret_data);
517 };
518
519 my $verify_short_lived_ticket = sub {
520 my ($ticket, $prefix, $username, $path, $noerr) = @_;
521
522 $path = normalize_path($path);
523
524 die "invalid ticket path\n" if !defined($path);
525
526 my $secret_data = "$username:$path";
527
528 my ($rsa_pub, $rsa_mtime) = get_pubkey();
529 if (!$rsa_pub || (time() - $rsa_mtime > $authkey_lifetime && $authkey_lifetime > 0)) {
530 if ($noerr) {
531 return undef;
532 } else {
533 # raise error via undef ticket
534 PVE::Ticket::verify_rsa_ticket($rsa_pub, $prefix);
535 }
536 }
537
538 return PVE::Ticket::verify_rsa_ticket(
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')
545 sub assemble_vnc_ticket {
546 my ($username, $path) = @_;
547
548 return $assemble_short_lived_ticket->('PVEVNC', $username, $path);
549 }
550
551 sub 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')
560 sub assemble_tunnel_ticket {
561 my ($username, $path) = @_;
562
563 return $assemble_short_lived_ticket->('PVETUNNEL', $username, $path);
564 }
565
566 sub verify_tunnel_ticket {
567 my ($ticket, $username, $path, $noerr) = @_;
568
569 return $verify_short_lived_ticket->($ticket, 'PVETUNNEL', $username, $path, $noerr);
570 }
571
572 sub assemble_spice_ticket {
573 my ($username, $vmid, $node) = @_;
574
575 my $secret = &$get_csrfr_secret();
576
577 return PVE::Ticket::assemble_spice_ticket(
578 $secret, $username, $vmid, $node);
579 }
580
581 sub verify_spice_connect_url {
582 my ($connect_str) = @_;
583
584 my $secret = &$get_csrfr_secret();
585
586 return PVE::Ticket::verify_spice_connect_url($secret, $connect_str);
587 }
588
589 sub read_x509_subject_spice {
590 my ($filename) = @_;
591
592 # read x509 subject
593 my $bio = Net::SSLeay::BIO_new_file($filename, 'r');
594 die "Could not open $filename using OpenSSL\n"
595 if !$bio;
596
597 my $x509 = Net::SSLeay::PEM_read_bio_X509($bio);
598 Net::SSLeay::BIO_free($bio);
599
600 die "Could not parse X509 certificate in $filename\n"
601 if !$x509;
602
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);
606
607 # remote-viewer wants comma as separator (not '/')
608 $subject =~ s!^/!!;
609 $subject =~ s!/(\w+=)!,$1!g;
610
611 return $subject;
612 }
613
614 # helper to generate SPICE remote-viewer configuration
615 sub 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;
631
632 $proxy = "[$proxy]" if Net::IP::ip_is_ipv6($proxy);
633 my $config = {
634 'secure-attention' => "Ctrl+Alt+Ins",
635 'toggle-fullscreen' => "Shift+F11",
636 'release-cursor' => "Ctrl+Alt+R",
637 type => 'spice',
638 title => $title,
639 host => $proxyticket, # this breaks tls hostname verification, so we need to use 'host-subject'
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
651 sub check_user_exist {
652 my ($usercfg, $username, $noerr) = @_;
653
654 $username = PVE::Auth::Plugin::verify_username($username, $noerr);
655 return undef if !$username;
656
657 return $usercfg->{users}->{$username} if $usercfg && $usercfg->{users}->{$username};
658
659 die "no such user ('$username')\n" if !$noerr;
660
661 return undef;
662 }
663
664 sub check_user_enabled {
665 my ($usercfg, $username, $noerr) = @_;
666
667 my $data = check_user_exist($usercfg, $username, $noerr);
668 return undef if !$data;
669
670 if (!$data->{enable}) {
671 die "user '$username' is disabled\n" if !$noerr;
672 return undef;
673 }
674
675 my $ctime = time();
676 my $expire = $usercfg->{users}->{$username}->{expire};
677
678 if ($expire && $expire < $ctime) {
679 die "user '$username' access expired\n" if !$noerr;
680 return undef;
681 }
682
683 return 1; # enabled and not expired
684 }
685
686 sub 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
700 # deprecated
701 sub verify_one_time_pw {
702 my ($type, $username, $keys, $tfa_cfg, $otp) = @_;
703
704 die "missing one time password for two-factor authentication '$type'\n" if !$otp;
705
706 # fixme: proxy support?
707 my $proxy;
708
709 if ($type eq 'yubico') {
710 PVE::OTP::yubico_verify_otp($otp, $keys, $tfa_cfg->{url},
711 $tfa_cfg->{id}, $tfa_cfg->{key}, $proxy);
712 } elsif ($type eq 'oath') {
713 PVE::OTP::oath_verify_otp($otp, $keys, $tfa_cfg->{step}, $tfa_cfg->{digits});
714 } else {
715 die "unknown tfa type '$type'\n";
716 }
717 }
718
719 # password should be utf8 encoded
720 # Note: some plugins delay/sleep if auth fails
721 sub authenticate_user : prototype($$$;$) {
722 my ($username, $password, $otp, $tfa_challenge) = @_;
723
724 die "no username specified\n" if !$username;
725
726 my ($ruid, $realm);
727
728 ($username, $ruid, $realm) = PVE::Auth::Plugin::verify_username($username);
729
730 my $usercfg = cfs_read_file('user.cfg');
731
732 check_user_enabled($usercfg, $username);
733
734 my $domain_cfg = cfs_read_file('domains.cfg');
735
736 my $cfg = $domain_cfg->{ids}->{$realm};
737 die "auth domain '$realm' does not exist\n" if !$cfg;
738 my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
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
746 $plugin->authenticate_user($cfg, $realm, $ruid, $password);
747
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;
751 }
752
753 sub authenticate_2nd_new_do : prototype($$$$) {
754 my ($username, $realm, $tfa_response, $tfa_challenge) = @_;
755 my ($tfa_cfg, $realm_tfa) = user_get_tfa($username, $realm);
756
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
760 if (!defined($tfa_cfg)) {
761 return undef;
762 }
763
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;
775 }
776 return to_json($challenge);
777 }
778
779 if ($tfa_response =~ /^yubico:(.*)$/) {
780 $tfa_response = $1;
781 # Defer to after unlocking the TFA config:
782 return sub {
783 authenticate_yubico_new(
784 $tfa_cfg, $username, $realm_tfa, $tfa_challenge, $tfa_response,
785 );
786 };
787 }
788 }
789
790 my $response_type;
791 if (defined($tfa_response)) {
792 if ($tfa_response !~ /^([^:]+):/) {
793 die "bad otp response\n";
794 }
795 $response_type = $1;
796 }
797
798 die "realm requires $realm_type authentication\n"
799 if $response_type && $response_type ne 'recovery' && $response_type ne $realm_type;
800 }
801
802 configure_u2f_and_wa($tfa_cfg);
803
804 my ($result, $tfa_done);
805 if (defined($tfa_challenge)) {
806 $tfa_done = 1;
807 $tfa_challenge = verify_ticket($tfa_challenge, 0, $username);
808 $result = $tfa_cfg->authentication_verify2($username, $tfa_challenge, $tfa_response);
809 $tfa_challenge = undef;
810 } else {
811 $tfa_challenge = $tfa_cfg->authentication_challenge($username);
812
813 die "missing required 2nd keys\n"
814 if $realm_tfa && !defined($tfa_challenge);
815
816 if (defined($tfa_response)) {
817 if (defined($tfa_challenge)) {
818 $tfa_done = 1;
819 $result = $tfa_cfg->authentication_verify2($username, $tfa_challenge, $tfa_response);
820 } else {
821 die "no such challenge\n";
822 }
823 }
824 }
825
826 if ($tfa_done) {
827 if (!$result) {
828 # authentication_verify2 somehow returned undef - should be unreachable
829 die "2nd factor failed\n";
830 }
831
832 if ($result->{'needs-saving'}) {
833 cfs_write_file('priv/tfa.cfg', $tfa_cfg);
834 }
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 }
846 }
847
848 return $tfa_challenge;
849 }
850
851 # Returns a tfa challenge or undef.
852 sub authenticate_2nd_new : prototype($$$$) {
853 my ($username, $realm, $tfa_response, $tfa_challenge) = @_;
854
855 my $result;
856
857 if (defined($tfa_response) && $tfa_response =~ m/^recovery:/) {
858 $result = lock_tfa_config(sub {
859 authenticate_2nd_new_do($username, $realm, $tfa_response, $tfa_challenge);
860 });
861 } else {
862 $result = authenticate_2nd_new_do($username, $realm, $tfa_response, $tfa_challenge);
863 }
864
865 # Yubico auth returns the authentication sub:
866 if (ref($result) eq 'CODE') {
867 $result = $result->();
868 }
869
870 return $result;
871 }
872
873 sub 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
886 authenticate_yubico_do($otp, $keys, $realm);
887
888 # return `undef` to clear the tfa challenge.
889 return undef;
890 }
891
892 sub 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
901 sub configure_u2f_and_wa : prototype($) {
902 my ($tfa_cfg) = @_;
903
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
915 my $dc = cfs_read_file('datacenter.cfg');
916 if (my $u2f = $dc->{u2f}) {
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 $@;
924 }
925 if (my $wa = $dc->{webauthn}) {
926 $wa->{origin} //= $get_origin->();
927 eval { $tfa_cfg->set_webauthn_config({%$wa}) };
928 warn "webauthn unavailable, configuration error: $@\n" if $@;
929 }
930 }
931
932 sub domain_set_password {
933 my ($realm, $username, $password) = @_;
934
935 die "no auth domain specified" if !$realm;
936
937 my $domain_cfg = cfs_read_file('domains.cfg');
938
939 my $cfg = $domain_cfg->{ids}->{$realm};
940 die "auth domain '$realm' does not exist\n" if !$cfg;
941 my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
942 $plugin->store_password($cfg, $realm, $username, $password);
943 }
944
945 sub 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
954 foreach my $child (sort keys %$children) {
955 iterate_acl_tree("$path/$child", $children->{$child}, $code);
956 }
957 }
958
959 # find ACL node corresponding to normalized $path under $root
960 sub 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
982 sub add_user_group {
983 my ($username, $usercfg, $group) = @_;
984
985 $usercfg->{users}->{$username}->{groups}->{$group} = 1;
986 $usercfg->{groups}->{$group}->{users}->{$username} = 1;
987 }
988
989 sub delete_user_group {
990 my ($username, $usercfg) = @_;
991
992 foreach my $group (keys %{$usercfg->{groups}}) {
993
994 delete ($usercfg->{groups}->{$group}->{users}->{$username})
995 if $usercfg->{groups}->{$group}->{users}->{$username};
996 }
997 }
998
999 sub delete_user_acl {
1000 my ($username, $usercfg) = @_;
1001
1002 my $code = sub {
1003 my ($path, $acl_node) = @_;
1004
1005 delete ($acl_node->{users}->{$username})
1006 if $acl_node->{users}->{$username};
1007 };
1008
1009 iterate_acl_tree("/", $usercfg->{acl_root}, $code);
1010 }
1011
1012 sub delete_group_acl {
1013 my ($group, $usercfg) = @_;
1014
1015 my $code = sub {
1016 my ($path, $acl_node) = @_;
1017
1018 delete ($acl_node->{groups}->{$group})
1019 if $acl_node->{groups}->{$group};
1020 };
1021
1022 iterate_acl_tree("/", $usercfg->{acl_root}, $code);
1023 }
1024
1025 sub delete_pool_acl {
1026 my ($pool, $usercfg) = @_;
1027
1028 delete ($usercfg->{acl_root}->{children}->{pool}->{children}->{$pool});
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
1035 # user: a normal user/customer can to that
1036 my $privgroups = {
1037 VM => {
1038 root => [],
1039 admin => [
1040 'VM.Config.Disk',
1041 'VM.Config.CPU',
1042 'VM.Config.Memory',
1043 'VM.Config.Network',
1044 'VM.Config.HWType',
1045 'VM.Config.Options', # covers all other things
1046 'VM.Allocate',
1047 'VM.Clone',
1048 'VM.Migrate',
1049 'VM.Monitor',
1050 'VM.Snapshot',
1051 'VM.Snapshot.Rollback',
1052 ],
1053 user => [
1054 'VM.Config.CDROM', # change CDROM media
1055 'VM.Config.Cloudinit',
1056 'VM.Console',
1057 'VM.Backup',
1058 'VM.PowerMgmt',
1059 ],
1060 audit => [
1061 'VM.Audit',
1062 ],
1063 },
1064 Sys => {
1065 root => [
1066 'Sys.PowerMgmt',
1067 'Sys.Modify', # edit/change node settings
1068 'Sys.Incoming', # incoming storage/guest migrations
1069 'Sys.AccessNetwork', # for, e.g., downloading ISOs from any URL
1070 ],
1071 admin => [
1072 'Sys.Console',
1073 'Sys.Syslog',
1074 ],
1075 user => [],
1076 audit => [
1077 'Sys.Audit',
1078 ],
1079 },
1080 Datastore => {
1081 root => [],
1082 admin => [
1083 'Datastore.Allocate',
1084 'Datastore.AllocateTemplate',
1085 ],
1086 user => [
1087 'Datastore.AllocateSpace',
1088 ],
1089 audit => [
1090 'Datastore.Audit',
1091 ],
1092 },
1093 SDN => {
1094 root => [],
1095 admin => [
1096 'SDN.Allocate',
1097 'SDN.Audit',
1098 ],
1099 user => [
1100 'SDN.Use',
1101 ],
1102 audit => [
1103 'SDN.Audit',
1104 ],
1105 },
1106 User => {
1107 root => [
1108 'Realm.Allocate',
1109 ],
1110 admin => [
1111 'User.Modify',
1112 'Group.Allocate', # edit/change group settings
1113 'Realm.AllocateUser',
1114 ],
1115 user => [],
1116 audit => [],
1117 },
1118 Pool => {
1119 root => [],
1120 admin => [
1121 'Pool.Allocate', # create/delete pools
1122 ],
1123 user => [
1124 'Pool.Audit',
1125 ],
1126 audit => [
1127 'Pool.Audit',
1128 ],
1129 },
1130 Mapping => {
1131 root => [],
1132 admin => [
1133 'Mapping.Modify',
1134 ],
1135 user => [
1136 'Mapping.Use',
1137 ],
1138 audit => [
1139 'Mapping.Audit',
1140 ],
1141 },
1142 };
1143
1144 my $valid_privs = {
1145 'Permissions.Modify' => 1, # not contained in a group
1146 };
1147
1148 my $special_roles = {
1149 'NoAccess' => {}, # no privileges
1150 'Administrator' => $valid_privs, # all privileges
1151 };
1152
1153 sub create_roles {
1154
1155 for my $cat (keys %$privgroups) {
1156 my $cd = $privgroups->{$cat};
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;
1160 }
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;
1165 }
1166 # create grouped user and audit roles
1167 if (scalar(@{$cd->{user}})) {
1168 for my $priv (@{$cd->{user}}, @{$cd->{audit}}) {
1169 $special_roles->{"PVE${cat}User"}->{$priv} = 1;
1170 }
1171 }
1172 for my $priv (@{$cd->{audit}}) {
1173 $special_roles->{"PVEAuditor"}->{$priv} = 1;
1174 }
1175 }
1176
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
1181 $special_roles->{"PVETemplateUser"} = { 'VM.Clone' => 1, 'VM.Audit' => 1 };
1182 };
1183
1184 create_roles();
1185
1186 sub 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
1197 sub role_is_special {
1198 my ($role) = @_;
1199 return (exists $special_roles->{$role}) ? 1 : 0;
1200 }
1201
1202 sub 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 {
1213 die "invalid privilege '$priv'\n";
1214 }
1215 }
1216 }
1217
1218 sub lookup_username {
1219 my ($username, $noerr) = @_;
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"
1232 if scalar @matches > 1 && !$noerr;
1233
1234 return $matches[0]
1235 }
1236
1237 return $username;
1238 }
1239
1240 sub normalize_path {
1241 my $path = shift;
1242
1243 return undef if !$path;
1244
1245 $path =~ s|/+|/|g;
1246
1247 $path =~ s|/$||;
1248
1249 $path = '/' if !$path;
1250
1251 $path = "/$path" if $path !~ m|^/|;
1252
1253 return undef if $path !~ m|^[[:alnum:]\.\-\_\/]+$|;
1254
1255 return $path;
1256 }
1257
1258 sub check_path {
1259 my ($path) = @_;
1260 return $path =~ m!^(
1261 /
1262 |/access
1263 |/access/groups
1264 |/access/groups/[[:alnum:]\.\-\_]+
1265 |/access/realm
1266 |/access/realm/[[:alnum:]\.\-\_]+
1267 |/nodes
1268 |/nodes/[[:alnum:]\.\-\_]+
1269 |/pool
1270 |/pool/[A-Za-z0-9\.\-_]+(?:/[A-Za-z0-9\.\-_]+){0,2}
1271 |/sdn
1272 |/sdn/controllers
1273 |/sdn/controllers/[[:alnum:]\_\-]+
1274 |/sdn/dns
1275 |/sdn/dns/[[:alnum:]]+
1276 |/sdn/ipams
1277 |/sdn/ipams/[[:alnum:]]+
1278 |/sdn/zones
1279 |/sdn/zones/[[:alnum:]\.\-\_]+
1280 |/sdn/zones/[[:alnum:]\.\-\_]+/[[:alnum:]\.\-\_]+
1281 |/sdn/zones/[[:alnum:]\.\-\_]+/[[:alnum:]\.\-\_]+/[1-9][0-9]{0,3}
1282 |/storage
1283 |/storage/[[:alnum:]\.\-\_]+
1284 |/vms
1285 |/vms/[1-9][0-9]{2,}
1286 |/mapping
1287 |/mapping/[[:alnum:]\.\-\_]+
1288 |/mapping/[[:alnum:]\.\-\_]+/[[:alnum:]\.\-\_]+
1289 )$!xs;
1290 }
1291
1292 PVE::JSONSchema::register_format('pve-groupid', \&verify_groupname);
1293 sub 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 }
1302
1303 return $groupname;
1304 }
1305
1306 PVE::JSONSchema::register_format('pve-roleid', \&verify_rolename);
1307 sub 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 }
1316
1317 return $rolename;
1318 }
1319
1320 PVE::JSONSchema::register_format('pve-poolid', \&verify_poolname);
1321 sub verify_poolname {
1322 my ($poolname, $noerr) = @_;
1323
1324 if (split("/", $poolname) > 3) {
1325 die "pool name '$poolname' nested too deeply (max levels = 3)\n" if !$noerr;
1326
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}$!) {
1332 die "pool name '$poolname' contains invalid characters\n" if !$noerr;
1333
1334 return undef;
1335 }
1336
1337 return $poolname;
1338 }
1339
1340 PVE::JSONSchema::register_format('pve-priv', \&verify_privname);
1341 sub verify_privname {
1342 my ($priv, $noerr) = @_;
1343
1344 if (!$valid_privs->{$priv}) {
1345 die "invalid privilege '$priv'\n" if !$noerr;
1346
1347 return undef;
1348 }
1349
1350 return $priv;
1351 }
1352
1353 sub userconfig_force_defaults {
1354 my ($cfg) = @_;
1355
1356 foreach my $r (keys %$special_roles) {
1357 $cfg->{roles}->{$r} = $special_roles->{$r};
1358 }
1359
1360 # add root user if not exists
1361 if (!$cfg->{users}->{'root@pam'}) {
1362 $cfg->{users}->{'root@pam'}->{enable} = 1;
1363 }
1364
1365 # add (empty) ACL tree root node
1366 if (!$cfg->{acl_root}) {
1367 $cfg->{acl_root} = {};
1368 }
1369 }
1370
1371 sub parse_user_config {
1372 my ($filename, $raw) = @_;
1373
1374 my $cfg = {};
1375
1376 userconfig_force_defaults($cfg);
1377
1378 $raw = '' if !defined($raw);
1379 while ($raw =~ /^\s*(.+?)\s*$/gm) {
1380 my $line = $1;
1381 my @data;
1382
1383 foreach my $d (split (/:/, $line)) {
1384 $d =~ s/^\s+//;
1385 $d =~ s/\s+$//;
1386 push @data, $d
1387 }
1388
1389 my $et = shift @data;
1390
1391 if ($et eq 'user') {
1392 my ($user, $enable, $expire, $firstname, $lastname, $email, $comment, $keys) = @data;
1393
1394 my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1);
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;
1424 # keys: allowed yubico key ids or oath secrets (base32 encoded)
1425 $cfg->{users}->{$user}->{keys} = $keys if $keys;
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
1445 if (!PVE::Auth::Plugin::verify_username($user, 1)) {
1446 warn "user config - ignore invalid group member '$user'\n";
1447 next;
1448 }
1449
1450 if ($cfg->{users}->{$user}) { # user exists
1451 $cfg->{users}->{$user}->{groups}->{$group} = 1;
1452 } else {
1453 warn "user config - ignore invalid group member '$user'\n";
1454 }
1455 $cfg->{groups}->{$group}->{users}->{$user} = 1;
1456 }
1457
1458 } elsif ($et eq 'role') {
1459 my ($role, $privlist) = @data;
1460
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 {
1473 warn "user config - ignore invalid privilege '$priv'\n";
1474 }
1475 }
1476
1477 } elsif ($et eq 'acl') {
1478 my ($propagate, $pathtxt, $uglist, $rolelist) = @data;
1479
1480 $propagate = $propagate ? 1 : 0;
1481
1482 if (my $path = normalize_path($pathtxt)) {
1483 my $acl_node;
1484 foreach my $role (split_list($rolelist)) {
1485
1486 if (!verify_rolename($role, 1)) {
1487 warn "user config - ignore invalid role name '$role' in acl\n";
1488 next;
1489 }
1490
1491 if (!$cfg->{roles}->{$role}) {
1492 warn "user config - ignore invalid acl role '$role'\n";
1493 next;
1494 }
1495
1496 foreach my $ug (split_list($uglist)) {
1497 my ($group) = $ug =~ m/^@(\S+)$/;
1498
1499 if ($group && verify_groupname($group, 1)) {
1500 if (!$cfg->{groups}->{$group}) { # group does not exist
1501 warn "user config - ignore invalid acl group '$group'\n";
1502 }
1503 $acl_node = find_acl_tree_node($cfg->{acl_root}, $path) if !$acl_node;
1504 $acl_node->{groups}->{$group}->{$role} = $propagate;
1505 } elsif (PVE::Auth::Plugin::verify_username($ug, 1)) {
1506 if (!$cfg->{users}->{$ug}) { # user does not exist
1507 warn "user config - ignore invalid acl member '$ug'\n";
1508 }
1509 $acl_node = find_acl_tree_node($cfg->{acl_root}, $path) if !$acl_node;
1510 $acl_node->{users}->{$ug}->{$role} = $propagate;
1511 } elsif (my ($user, $token) = split_tokenid($ug, 1)) {
1512 if (check_token_exist($cfg, $user, $token, 1)) {
1513 $acl_node = find_acl_tree_node($cfg->{acl_root}, $path) if !$acl_node;
1514 $acl_node->{tokens}->{$ug}->{$role} = $propagate;
1515 } else {
1516 warn "user config - ignore invalid acl token '$ug'\n";
1517 }
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 }
1526 } elsif ($et eq 'pool') {
1527 my ($pool, $comment, $vmlist, $storelist) = @data;
1528
1529 if (!verify_poolname($pool, 1)) {
1530 warn "user config - ignore pool '$pool' - invalid characters in pool name\n";
1531 next;
1532 }
1533
1534 # make sure to add the pool (even if there are no members)
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 }
1550
1551 $cfg->{pools}->{$pool}->{comment} = PVE::Tools::decode_text($comment) if $comment;
1552
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;
1557 }
1558 $vmid = int($vmid);
1559
1560 if ($cfg->{vms}->{$vmid}) {
1561 warn "user config - ignore duplicate vmid '$vmid' in pool '$pool'\n";
1562 next;
1563 }
1564
1565 $cfg->{pools}->{$pool}->{vms}->{$vmid} = 1;
1566
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;
1577 }
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 }
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
1616 sub write_user_config {
1617 my ($filename, $cfg) = @_;
1618
1619 my $data = '';
1620
1621 foreach my $user (sort keys %{$cfg->{users}}) {
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}) : '';
1627 my $expire = int($d->{expire} || 0);
1628 my $enable = $d->{enable} ? 1 : 0;
1629 my $keys = $d->{keys} ? $d->{keys} : '';
1630 $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:$keys:\n";
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 }
1641 }
1642
1643 $data .= "\n";
1644
1645 foreach my $group (sort keys %{$cfg->{groups}}) {
1646 my $d = $cfg->{groups}->{$group};
1647 my $list = join (',', sort keys %{$d->{users}});
1648 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
1649 $data .= "group:$group:$list:$comment:\n";
1650 }
1651
1652 $data .= "\n";
1653
1654 foreach my $pool (sort keys %{$cfg->{pools}}) {
1655 my $d = $cfg->{pools}->{$pool};
1656 my $vmlist = join (',', sort keys %{$d->{vms}});
1657 my $storelist = join (',', sort keys %{$d->{storage}});
1658 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
1659 $data .= "pool:$pool:$comment:$vmlist:$storelist:\n";
1660 }
1661
1662 $data .= "\n";
1663
1664 foreach my $role (sort keys %{$cfg->{roles}}) {
1665 next if $special_roles->{$role};
1666
1667 my $d = $cfg->{roles}->{$role};
1668 my $list = join (',', sort keys %$d);
1669 $data .= "role:$role:$list:\n";
1670 }
1671
1672 $data .= "\n";
1673
1674 my $collect_rolelist_members = sub {
1675 my ($acl_members, $result, $prefix, $exclude) = @_;
1676
1677 foreach my $member (keys %$acl_members) {
1678 next if $exclude && $member eq $exclude;
1679
1680 my $l0 = '';
1681 my $l1 = '';
1682 foreach my $role (sort keys %{$acl_members->{$member}}) {
1683 my $propagate = $acl_members->{$member}->{$role};
1684 if ($propagate) {
1685 $l1 .= ',' if $l1;
1686 $l1 .= $role;
1687 } else {
1688 $l0 .= ',' if $l0;
1689 $l0 .= $role;
1690 }
1691 }
1692 $result->{0}->{$l0}->{"${prefix}${member}"} = 1 if $l0;
1693 $result->{1}->{$l1}->{"${prefix}${member}"} = 1 if $l1;
1694 }
1695 };
1696
1697 iterate_acl_tree("/", $cfg->{acl_root}, sub {
1698 my ($path, $d) = @_;
1699
1700 my $rolelist_members = {};
1701
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
1707 $collect_rolelist_members->($d->{'tokens'}, $rolelist_members, '');
1708
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 }
1715
1716 }
1717 });
1718
1719 return $data;
1720 }
1721
1722 # Creates a `PVE::RS::TFA` instance from the raw config data.
1723 # Its contained hash will also support the legacy functionality.
1724 sub parse_priv_tfa_config {
1725 my ($filename, $raw) = @_;
1726
1727 $raw = '' if !defined($raw);
1728 my $cfg = PVE::RS::TFA->new($raw);
1729
1730 # Purge invalid users:
1731 foreach my $user ($cfg->users()->@*) {
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";
1735 $cfg->remove_user($user);
1736 }
1737 }
1738
1739 return $cfg;
1740 }
1741
1742 sub write_priv_tfa_config {
1743 my ($filename, $cfg) = @_;
1744
1745 return $cfg->write();
1746 }
1747
1748 sub roles {
1749 my ($cfg, $user, $path) = @_;
1750
1751 # NOTE: we do not consider pools here.
1752 # NOTE: for privsep tokens, this does not filter roles by those that the
1753 # corresponding user has.
1754 # Use $rpcenv->permission() for any actual permission checks!
1755
1756 return 'Administrator' if $user eq 'root@pam'; # root can do anything
1757
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
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
1771 my $user_roles = roles($cfg, $username, $path);
1772
1773 # return full user privileges
1774 return $user_roles if !$token_info->{privsep};
1775 }
1776
1777 my $roles = {};
1778
1779 my $split = [ split("/", $path) ];
1780 if ($path eq '/') {
1781 $split = [ '' ];
1782 }
1783
1784 my $acl = $cfg->{acl_root};
1785 my $i = 0;
1786
1787 while (@$split) {
1788 my $p = shift @$split;
1789 my $final = !@$split;
1790 if ($p ne '') {
1791 $acl = $acl->{children}->{$p};
1792 }
1793
1794 #print "CHECKACL $path $p\n";
1795 #print "ACL $path = " . Dumper ($acl);
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;
1803 $new->{$role} = $propagate;
1804 }
1805 }
1806 if ($new) {
1807 $roles = $new; # overwrite previous settings
1808 next;
1809 }
1810 }
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;
1819 $new->{$role} = $propagate;
1820 }
1821 }
1822 if ($new) {
1823 $roles = $new; # overwrite previous settings
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;
1837 $new->{$role} = $propagate;
1838 }
1839 }
1840 }
1841 }
1842 if ($new) {
1843 $roles = $new; # overwrite previous settings
1844 next;
1845 }
1846 }
1847
1848 return { 'NoAccess' => $roles->{NoAccess} } if defined ($roles->{NoAccess});
1849 #return () if defined ($roles->{NoAccess});
1850
1851 #print "permission $user $path = " . Dumper ($roles);
1852
1853 #print "roles $user $path = " . join (',', @ra) . "\n";
1854
1855 return $roles;
1856 }
1857
1858 sub remove_vm_access {
1859 my ($vmid) = @_;
1860 my $delVMaccessFn = sub {
1861 my $usercfg = cfs_read_file("user.cfg");
1862 my $modified;
1863
1864 if (my $acl = $usercfg->{acl_root}->{children}->{vms}->{children}->{$vmid}) {
1865 delete $usercfg->{acl_root}->{children}->{vms}->{children}->{$vmid};
1866 $modified = 1;
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};
1872 $modified = 1;
1873 }
1874 }
1875 cfs_write_file("user.cfg", $usercfg) if $modified;
1876 };
1877
1878 lock_user_config($delVMaccessFn, "access permissions cleanup for VM $vmid failed");
1879 }
1880
1881 sub remove_storage_access {
1882 my ($storeid) = @_;
1883
1884 my $deleteStorageAccessFn = sub {
1885 my $usercfg = cfs_read_file("user.cfg");
1886 my $modified;
1887
1888 if (my $acl = $usercfg->{acl_root}->{children}->{storage}->{children}->{$storeid}) {
1889 delete $usercfg->{acl_root}->{children}->{storage}->{children}->{$storeid};
1890 $modified = 1;
1891 }
1892 foreach my $pool (keys %{$usercfg->{pools}}) {
1893 delete $usercfg->{pools}->{$pool}->{storage}->{$storeid};
1894 $modified = 1;
1895 }
1896 cfs_write_file("user.cfg", $usercfg) if $modified;
1897 };
1898
1899 lock_user_config($deleteStorageAccessFn,
1900 "access permissions cleanup for storage $storeid failed");
1901 }
1902
1903 sub 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
1918 sub remove_vm_from_pool {
1919 my ($vmid) = @_;
1920
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
1935 my $USER_CONTROLLED_TFA_TYPES = {
1936 u2f => 1,
1937 oath => 1,
1938 };
1939
1940 sub user_remove_tfa : prototype($) {
1941 my ($userid) = @_;
1942
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
1948 my 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
1959 my 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
1981 my 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
2003 sub 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
2020 sub user_get_tfa : prototype($$$) {
2021 my ($username, $realm) = @_;
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};
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
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);
2040 }
2041
2042 return ($tfa_cfg, $realm_tfa);
2043 }
2044
2045 # bash completion helpers
2046
2047 register_standard_option('userid-completed',
2048 get_standard_option('userid', { completion => \&complete_username}),
2049 );
2050
2051 sub complete_username {
2052
2053 my $user_cfg = cfs_read_file('user.cfg');
2054
2055 return [ keys %{$user_cfg->{users}} ];
2056 }
2057
2058 sub complete_group {
2059
2060 my $user_cfg = cfs_read_file('user.cfg');
2061
2062 return [ keys %{$user_cfg->{groups}} ];
2063 }
2064
2065 sub complete_realm {
2066
2067 my $domain_cfg = cfs_read_file('domains.cfg');
2068
2069 return [ keys %{$domain_cfg->{ids}} ];
2070 }
2071
2072 1;