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