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