]> git.proxmox.com Git - pve-access-control.git/blob - src/PVE/AccessControl.pm
447f96091e98ef568e8e8d9a791ca0891807f676
[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::Auth::Plugin;
23 use PVE::Auth::AD;
24 use PVE::Auth::LDAP;
25 use PVE::Auth::PVE;
26 use PVE::Auth::PAM;
27
28 # load and initialize all plugins
29
30 PVE::Auth::AD->register();
31 PVE::Auth::LDAP->register();
32 PVE::Auth::PVE->register();
33 PVE::Auth::PAM->register();
34 PVE::Auth::Plugin->init();
35
36 # $authdir must be writable by root only!
37 my $confdir = "/etc/pve";
38 my $authdir = "$confdir/priv";
39
40 my $pve_www_key_fn = "$confdir/pve-www.key";
41
42 my $pve_auth_key_files = {
43 priv => "$authdir/authkey.key",
44 pub => "$confdir/authkey.pub",
45 pubold => "$confdir/authkey.pub.old",
46 };
47
48 my $pve_auth_key_cache = {};
49
50 my $ticket_lifetime = 3600 * 2; # 2 hours
51 my $auth_graceperiod = 60 * 5; # 5 minutes
52 my $authkey_lifetime = 3600 * 24; # rotate every 24 hours
53
54 Crypt::OpenSSL::RSA->import_random_seed();
55
56 cfs_register_file('user.cfg',
57 \&parse_user_config,
58 \&write_user_config);
59 cfs_register_file('priv/tfa.cfg',
60 \&parse_priv_tfa_config,
61 \&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 {
342 my ($data) = @_;
343
344 my $rsa_priv = get_privkey();
345
346 return PVE::Ticket::assemble_rsa_ticket($rsa_priv, 'PVE', $data);
347 }
348
349 sub verify_ticket {
350 my ($ticket, $noerr) = @_;
351
352 my $now = time();
353
354 my $check = sub {
355 my ($old) = @_;
356
357 my ($rsa_pub, $rsa_mtime) = get_pubkey($old);
358 return undef if !$rsa_pub;
359
360 my ($min, $max) = $get_ticket_age_range->($now, $rsa_mtime, $old);
361 return undef if !defined($min);
362
363 return PVE::Ticket::verify_rsa_ticket(
364 $rsa_pub, 'PVE', $ticket, undef, $min, $max, 1);
365 };
366
367 my ($data, $age) = $check->();
368
369 # check with old, rotated key if current key failed
370 ($data, $age) = $check->(1) if !defined($data);
371
372 my $auth_failure = sub {
373 if ($noerr) {
374 return undef;
375 } else {
376 # raise error via undef ticket
377 PVE::Ticket::verify_rsa_ticket(undef, 'PVE');
378 }
379 };
380
381 if (!defined($data)) {
382 return $auth_failure->();
383 }
384
385 my ($username, $tfa_info);
386 if ($data =~ m{^u2f!([^!]+)!([0-9a-zA-Z/.=_\-+]+)$}) {
387 # Ticket for u2f-users:
388 ($username, my $challenge) = ($1, $2);
389 if ($challenge eq 'verified') {
390 # u2f challenge was completed
391 $challenge = undef;
392 } elsif (!wantarray) {
393 # The caller is not aware there could be an ongoing challenge,
394 # so we treat this ticket as invalid:
395 return $auth_failure->();
396 }
397 $tfa_info = {
398 type => 'u2f',
399 challenge => $challenge,
400 };
401 } elsif ($data =~ /^tfa!(.*)$/) {
402 # TOTP and Yubico don't require a challenge so this is the generic
403 # 'missing 2nd factor ticket'
404 $username = $1;
405 $tfa_info = { type => 'tfa' };
406 } else {
407 # Regular ticket (full access)
408 $username = $data;
409 }
410
411 return undef if !PVE::Auth::Plugin::verify_username($username, $noerr);
412
413 return wantarray ? ($username, $age, $tfa_info) : $username;
414 }
415
416 sub verify_token {
417 my ($api_token) = @_;
418
419 die "no API token specified\n" if !$api_token;
420
421 my ($tokenid, $value);
422 if ($api_token =~ /^(.*)=(.*)$/) {
423 $tokenid = $1;
424 $value = $2;
425 } else {
426 die "no tokenid specified\n";
427 }
428
429 my ($username, $token) = split_tokenid($tokenid);
430
431 my $usercfg = cfs_read_file('user.cfg');
432 check_user_enabled($usercfg, $username);
433 check_token_exist($usercfg, $username, $token);
434
435 my $ctime = time();
436
437 my $user = $usercfg->{users}->{$username};
438 die "account expired\n" if $user->{expire} && ($user->{expire} < $ctime);
439
440 my $token_info = $user->{tokens}->{$token};
441 die "token expired\n" if $token_info->{expire} && ($token_info->{expire} < $ctime);
442
443 die "invalid token value!\n" if !PVE::Cluster::verify_token($tokenid, $value);
444
445 return wantarray ? ($tokenid) : $tokenid;
446 }
447
448
449 # VNC tickets
450 # - they do not contain the username in plain text
451 # - they are restricted to a specific resource path (example: '/vms/100')
452 sub assemble_vnc_ticket {
453 my ($username, $path) = @_;
454
455 my $rsa_priv = get_privkey();
456
457 $path = normalize_path($path);
458
459 my $secret_data = "$username:$path";
460
461 return PVE::Ticket::assemble_rsa_ticket(
462 $rsa_priv, 'PVEVNC', undef, $secret_data);
463 }
464
465 sub verify_vnc_ticket {
466 my ($ticket, $username, $path, $noerr) = @_;
467
468 my $secret_data = "$username:$path";
469
470 my ($rsa_pub, $rsa_mtime) = get_pubkey();
471 if (!$rsa_pub || (time() - $rsa_mtime > $authkey_lifetime && $authkey_lifetime > 0)) {
472 if ($noerr) {
473 return undef;
474 } else {
475 # raise error via undef ticket
476 PVE::Ticket::verify_rsa_ticket($rsa_pub, 'PVEVNC');
477 }
478 }
479
480 return PVE::Ticket::verify_rsa_ticket(
481 $rsa_pub, 'PVEVNC', $ticket, $secret_data, -20, 40, $noerr);
482 }
483
484 sub assemble_spice_ticket {
485 my ($username, $vmid, $node) = @_;
486
487 my $secret = &$get_csrfr_secret();
488
489 return PVE::Ticket::assemble_spice_ticket(
490 $secret, $username, $vmid, $node);
491 }
492
493 sub verify_spice_connect_url {
494 my ($connect_str) = @_;
495
496 my $secret = &$get_csrfr_secret();
497
498 return PVE::Ticket::verify_spice_connect_url($secret, $connect_str);
499 }
500
501 sub read_x509_subject_spice {
502 my ($filename) = @_;
503
504 # read x509 subject
505 my $bio = Net::SSLeay::BIO_new_file($filename, 'r');
506 die "Could not open $filename using OpenSSL\n"
507 if !$bio;
508
509 my $x509 = Net::SSLeay::PEM_read_bio_X509($bio);
510 Net::SSLeay::BIO_free($bio);
511
512 die "Could not parse X509 certificate in $filename\n"
513 if !$x509;
514
515 my $nameobj = Net::SSLeay::X509_get_subject_name($x509);
516 my $subject = Net::SSLeay::X509_NAME_oneline($nameobj);
517 Net::SSLeay::X509_free($x509);
518
519 # remote-viewer wants comma as seperator (not '/')
520 $subject =~ s!^/!!;
521 $subject =~ s!/(\w+=)!,$1!g;
522
523 return $subject;
524 }
525
526 # helper to generate SPICE remote-viewer configuration
527 sub remote_viewer_config {
528 my ($authuser, $vmid, $node, $proxy, $title, $port) = @_;
529
530 if (!$proxy) {
531 my $host = `hostname -f` || PVE::INotify::nodename();
532 chomp $host;
533 $proxy = $host;
534 }
535
536 my ($ticket, $proxyticket) = assemble_spice_ticket($authuser, $vmid, $node);
537
538 my $filename = "/etc/pve/local/pve-ssl.pem";
539 my $subject = read_x509_subject_spice($filename);
540
541 my $cacert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192);
542 $cacert =~ s/\n/\\n/g;
543
544 $proxy = "[$proxy]" if Net::IP::ip_is_ipv6($proxy);
545 my $config = {
546 'secure-attention' => "Ctrl+Alt+Ins",
547 'toggle-fullscreen' => "Shift+F11",
548 'release-cursor' => "Ctrl+Alt+R",
549 type => 'spice',
550 title => $title,
551 host => $proxyticket, # this breaks tls hostname verification, so we need to use 'host-subject'
552 proxy => "http://$proxy:3128",
553 'tls-port' => $port,
554 'host-subject' => $subject,
555 ca => $cacert,
556 password => $ticket,
557 'delete-this-file' => 1,
558 };
559
560 return ($ticket, $proxyticket, $config);
561 }
562
563 sub check_user_exist {
564 my ($usercfg, $username, $noerr) = @_;
565
566 $username = PVE::Auth::Plugin::verify_username($username, $noerr);
567 return undef if !$username;
568
569 return $usercfg->{users}->{$username} if $usercfg && $usercfg->{users}->{$username};
570
571 die "no such user ('$username')\n" if !$noerr;
572
573 return undef;
574 }
575
576 sub check_user_enabled {
577 my ($usercfg, $username, $noerr) = @_;
578
579 my $data = check_user_exist($usercfg, $username, $noerr);
580 return undef if !$data;
581
582 return 1 if $data->{enable};
583
584 die "user '$username' is disabled\n" if !$noerr;
585
586 return undef;
587 }
588
589 sub check_token_exist {
590 my ($usercfg, $username, $tokenid, $noerr) = @_;
591
592 my $user = check_user_exist($usercfg, $username, $noerr);
593 return undef if !$user;
594
595 return $user->{tokens}->{$tokenid}
596 if defined($user->{tokens}) && $user->{tokens}->{$tokenid};
597
598 die "no such token '$tokenid' for user '$username'\n" if !$noerr;
599
600 return undef;
601 }
602
603 sub verify_one_time_pw {
604 my ($type, $username, $keys, $tfa_cfg, $otp) = @_;
605
606 die "missing one time password for two-factor authentication '$type'\n" if !$otp;
607
608 # fixme: proxy support?
609 my $proxy;
610
611 if ($type eq 'yubico') {
612 PVE::OTP::yubico_verify_otp($otp, $keys, $tfa_cfg->{url},
613 $tfa_cfg->{id}, $tfa_cfg->{key}, $proxy);
614 } elsif ($type eq 'oath') {
615 PVE::OTP::oath_verify_otp($otp, $keys, $tfa_cfg->{step}, $tfa_cfg->{digits});
616 } else {
617 die "unknown tfa type '$type'\n";
618 }
619 }
620
621 # password should be utf8 encoded
622 # Note: some plugins delay/sleep if auth fails
623 sub authenticate_user {
624 my ($username, $password, $otp) = @_;
625
626 die "no username specified\n" if !$username;
627
628 my ($ruid, $realm);
629
630 ($username, $ruid, $realm) = PVE::Auth::Plugin::verify_username($username);
631
632 my $usercfg = cfs_read_file('user.cfg');
633
634 check_user_enabled($usercfg, $username);
635
636 my $ctime = time();
637 my $expire = $usercfg->{users}->{$username}->{expire};
638
639 die "account expired\n" if $expire && ($expire < $ctime);
640
641 my $domain_cfg = cfs_read_file('domains.cfg');
642
643 my $cfg = $domain_cfg->{ids}->{$realm};
644 die "auth domain '$realm' does not exist\n" if !$cfg;
645 my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
646 $plugin->authenticate_user($cfg, $realm, $ruid, $password);
647
648 my ($type, $tfa_data) = user_get_tfa($username, $realm);
649 if ($type) {
650 if ($type eq 'u2f') {
651 # Note that if the user did not manage to complete the initial u2f registration
652 # challenge we have a hash containing a 'challenge' entry in the user's tfa.cfg entry:
653 $tfa_data = undef if exists $tfa_data->{challenge};
654 } elsif (!defined($otp)) {
655 # The user requires a 2nd factor but has not provided one. Return success but
656 # don't clear $tfa_data.
657 } else {
658 my $keys = $tfa_data->{keys};
659 my $tfa_cfg = $tfa_data->{config};
660 verify_one_time_pw($type, $username, $keys, $tfa_cfg, $otp);
661 $tfa_data = undef;
662 }
663
664 # Return the type along with the rest:
665 if ($tfa_data) {
666 $tfa_data = {
667 type => $type,
668 data => $tfa_data,
669 };
670 }
671 }
672
673 return wantarray ? ($username, $tfa_data) : $username;
674 }
675
676 sub domain_set_password {
677 my ($realm, $username, $password) = @_;
678
679 die "no auth domain specified" if !$realm;
680
681 my $domain_cfg = cfs_read_file('domains.cfg');
682
683 my $cfg = $domain_cfg->{ids}->{$realm};
684 die "auth domain '$realm' does not exist\n" if !$cfg;
685 my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
686 $plugin->store_password($cfg, $realm, $username, $password);
687 }
688
689 sub add_user_group {
690 my ($username, $usercfg, $group) = @_;
691
692 $usercfg->{users}->{$username}->{groups}->{$group} = 1;
693 $usercfg->{groups}->{$group}->{users}->{$username} = 1;
694 }
695
696 sub delete_user_group {
697 my ($username, $usercfg) = @_;
698
699 foreach my $group (keys %{$usercfg->{groups}}) {
700
701 delete ($usercfg->{groups}->{$group}->{users}->{$username})
702 if $usercfg->{groups}->{$group}->{users}->{$username};
703 }
704 }
705
706 sub delete_user_acl {
707 my ($username, $usercfg) = @_;
708
709 foreach my $acl (keys %{$usercfg->{acl}}) {
710
711 delete ($usercfg->{acl}->{$acl}->{users}->{$username})
712 if $usercfg->{acl}->{$acl}->{users}->{$username};
713 }
714 }
715
716 sub delete_group_acl {
717 my ($group, $usercfg) = @_;
718
719 foreach my $acl (keys %{$usercfg->{acl}}) {
720
721 delete ($usercfg->{acl}->{$acl}->{groups}->{$group})
722 if $usercfg->{acl}->{$acl}->{groups}->{$group};
723 }
724 }
725
726 sub delete_pool_acl {
727 my ($pool, $usercfg) = @_;
728
729 my $path = "/pool/$pool";
730
731 delete ($usercfg->{acl}->{$path})
732 }
733
734 # we automatically create some predefined roles by splitting privs
735 # into 3 groups (per category)
736 # root: only root is allowed to do that
737 # admin: an administrator can to that
738 # user: a normal user/customer can to that
739 my $privgroups = {
740 VM => {
741 root => [],
742 admin => [
743 'VM.Config.Disk',
744 'VM.Config.CPU',
745 'VM.Config.Memory',
746 'VM.Config.Network',
747 'VM.Config.HWType',
748 'VM.Config.Options', # covers all other things
749 'VM.Allocate',
750 'VM.Clone',
751 'VM.Migrate',
752 'VM.Monitor',
753 'VM.Snapshot',
754 'VM.Snapshot.Rollback',
755 ],
756 user => [
757 'VM.Config.CDROM', # change CDROM media
758 'VM.Config.Cloudinit',
759 'VM.Console',
760 'VM.Backup',
761 'VM.PowerMgmt',
762 ],
763 audit => [
764 'VM.Audit',
765 ],
766 },
767 Sys => {
768 root => [
769 'Sys.PowerMgmt',
770 'Sys.Modify', # edit/change node settings
771 ],
772 admin => [
773 'Permissions.Modify',
774 'Sys.Console',
775 'Sys.Syslog',
776 ],
777 user => [],
778 audit => [
779 'Sys.Audit',
780 ],
781 },
782 Datastore => {
783 root => [],
784 admin => [
785 'Datastore.Allocate',
786 'Datastore.AllocateTemplate',
787 ],
788 user => [
789 'Datastore.AllocateSpace',
790 ],
791 audit => [
792 'Datastore.Audit',
793 ],
794 },
795 SDN => {
796 root => [],
797 admin => [
798 'SDN.Allocate',
799 'SDN.Audit',
800 ],
801 audit => [
802 'SDN.Audit',
803 ],
804 },
805 User => {
806 root => [
807 'Realm.Allocate',
808 ],
809 admin => [
810 'User.Modify',
811 'Group.Allocate', # edit/change group settings
812 'Realm.AllocateUser',
813 ],
814 user => [],
815 audit => [],
816 },
817 Pool => {
818 root => [],
819 admin => [
820 'Pool.Allocate', # create/delete pools
821 ],
822 user => [
823 'Pool.Audit',
824 ],
825 audit => [
826 'Pool.Audit',
827 ],
828 },
829 };
830
831 my $valid_privs = {};
832
833 my $special_roles = {
834 'NoAccess' => {}, # no privileges
835 'Administrator' => $valid_privs, # all privileges
836 };
837
838 sub create_roles {
839
840 foreach my $cat (keys %$privgroups) {
841 my $cd = $privgroups->{$cat};
842 foreach my $p (@{$cd->{root}}, @{$cd->{admin}},
843 @{$cd->{user}}, @{$cd->{audit}}) {
844 $valid_privs->{$p} = 1;
845 }
846 foreach my $p (@{$cd->{admin}}, @{$cd->{user}}, @{$cd->{audit}}) {
847
848 $special_roles->{"PVE${cat}Admin"}->{$p} = 1;
849 $special_roles->{"PVEAdmin"}->{$p} = 1;
850 }
851 if (scalar(@{$cd->{user}})) {
852 foreach my $p (@{$cd->{user}}, @{$cd->{audit}}) {
853 $special_roles->{"PVE${cat}User"}->{$p} = 1;
854 }
855 }
856 foreach my $p (@{$cd->{audit}}) {
857 $special_roles->{"PVEAuditor"}->{$p} = 1;
858 }
859 }
860
861 $special_roles->{"PVETemplateUser"} = { 'VM.Clone' => 1, 'VM.Audit' => 1 };
862 };
863
864 create_roles();
865
866 sub create_priv_properties {
867 my $properties = {};
868 foreach my $priv (keys %$valid_privs) {
869 $properties->{$priv} = {
870 type => 'boolean',
871 optional => 1,
872 };
873 }
874 return $properties;
875 }
876
877 sub role_is_special {
878 my ($role) = @_;
879 return (exists $special_roles->{$role}) ? 1 : 0;
880 }
881
882 sub add_role_privs {
883 my ($role, $usercfg, $privs) = @_;
884
885 return if !$privs;
886
887 die "role '$role' does not exist\n" if !$usercfg->{roles}->{$role};
888
889 foreach my $priv (split_list($privs)) {
890 if (defined ($valid_privs->{$priv})) {
891 $usercfg->{roles}->{$role}->{$priv} = 1;
892 } else {
893 die "invalid privilege '$priv'\n";
894 }
895 }
896 }
897
898 sub lookup_username {
899 my ($username, $noerr) = @_;
900
901 $username =~ m!^(${PVE::Auth::Plugin::user_regex})\@(${PVE::Auth::Plugin::realm_regex})$!;
902
903 my $realm = $2;
904 my $domain_cfg = cfs_read_file("domains.cfg");
905 my $casesensitive = $domain_cfg->{ids}->{$realm}->{'case-sensitive'} // 1;
906 my $usercfg = cfs_read_file('user.cfg');
907
908 if (!$casesensitive) {
909 my @matches = grep { lc $username eq lc $_ } (keys %{$usercfg->{users}});
910
911 die "ambiguous case insensitive match of username '$username', cannot safely grant access!\n"
912 if scalar @matches > 1 && !$noerr;
913
914 return $matches[0]
915 }
916
917 return $username;
918 }
919
920 sub normalize_path {
921 my $path = shift;
922
923 $path =~ s|/+|/|g;
924
925 $path =~ s|/$||;
926
927 $path = '/' if !$path;
928
929 $path = "/$path" if $path !~ m|^/|;
930
931 return undef if $path !~ m|^[[:alnum:]\.\-\_\/]+$|;
932
933 return $path;
934 }
935
936 sub check_path {
937 my ($path) = @_;
938 return $path =~ m!^(
939 /
940 |/access
941 |/access/groups
942 |/access/groups/[[:alnum:]\.\-\_]+
943 |/access/realm
944 |/access/realm/[[:alnum:]\.\-\_]+
945 |/nodes
946 |/nodes/[[:alnum:]\.\-\_]+
947 |/pool
948 |/pool/[[:alnum:]\.\-\_]+
949 |/sdn
950 |/sdn/zones/[[:alnum:]\.\-\_]+
951 |/storage
952 |/storage/[[:alnum:]\.\-\_]+
953 |/vms
954 |/vms/[1-9][0-9]{2,}
955 )$!xs;
956 }
957
958 PVE::JSONSchema::register_format('pve-groupid', \&verify_groupname);
959 sub verify_groupname {
960 my ($groupname, $noerr) = @_;
961
962 if ($groupname !~ m/^[A-Za-z0-9\.\-_]+$/) {
963
964 die "group name '$groupname' contains invalid characters\n" if !$noerr;
965
966 return undef;
967 }
968
969 return $groupname;
970 }
971
972 PVE::JSONSchema::register_format('pve-roleid', \&verify_rolename);
973 sub verify_rolename {
974 my ($rolename, $noerr) = @_;
975
976 if ($rolename !~ m/^[A-Za-z0-9\.\-_]+$/) {
977
978 die "role name '$rolename' contains invalid characters\n" if !$noerr;
979
980 return undef;
981 }
982
983 return $rolename;
984 }
985
986 PVE::JSONSchema::register_format('pve-poolid', \&verify_poolname);
987 sub verify_poolname {
988 my ($poolname, $noerr) = @_;
989
990 if ($poolname !~ m/^[A-Za-z0-9\.\-_]+$/) {
991
992 die "pool name '$poolname' contains invalid characters\n" if !$noerr;
993
994 return undef;
995 }
996
997 return $poolname;
998 }
999
1000 PVE::JSONSchema::register_format('pve-priv', \&verify_privname);
1001 sub verify_privname {
1002 my ($priv, $noerr) = @_;
1003
1004 if (!$valid_privs->{$priv}) {
1005 die "invalid privilege '$priv'\n" if !$noerr;
1006
1007 return undef;
1008 }
1009
1010 return $priv;
1011 }
1012
1013 sub userconfig_force_defaults {
1014 my ($cfg) = @_;
1015
1016 foreach my $r (keys %$special_roles) {
1017 $cfg->{roles}->{$r} = $special_roles->{$r};
1018 }
1019
1020 # add root user if not exists
1021 if (!$cfg->{users}->{'root@pam'}) {
1022 $cfg->{users}->{'root@pam'}->{enable} = 1;
1023 }
1024 }
1025
1026 sub parse_user_config {
1027 my ($filename, $raw) = @_;
1028
1029 my $cfg = {};
1030
1031 userconfig_force_defaults($cfg);
1032
1033 $raw = '' if !defined($raw);
1034 while ($raw =~ /^\s*(.+?)\s*$/gm) {
1035 my $line = $1;
1036 my @data;
1037
1038 foreach my $d (split (/:/, $line)) {
1039 $d =~ s/^\s+//;
1040 $d =~ s/\s+$//;
1041 push @data, $d
1042 }
1043
1044 my $et = shift @data;
1045
1046 if ($et eq 'user') {
1047 my ($user, $enable, $expire, $firstname, $lastname, $email, $comment, $keys) = @data;
1048
1049 my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1);
1050 if (!$realm) {
1051 warn "user config - ignore user '$user' - invalid user name\n";
1052 next;
1053 }
1054
1055 $enable = $enable ? 1 : 0;
1056
1057 $expire = 0 if !$expire;
1058
1059 if ($expire !~ m/^\d+$/) {
1060 warn "user config - ignore user '$user' - (illegal characters in expire '$expire')\n";
1061 next;
1062 }
1063 $expire = int($expire);
1064
1065 #if (!verify_groupname ($group, 1)) {
1066 # warn "user config - ignore user '$user' - invalid characters in group name\n";
1067 # next;
1068 #}
1069
1070 $cfg->{users}->{$user} = {
1071 enable => $enable,
1072 # group => $group,
1073 };
1074 $cfg->{users}->{$user}->{firstname} = PVE::Tools::decode_text($firstname) if $firstname;
1075 $cfg->{users}->{$user}->{lastname} = PVE::Tools::decode_text($lastname) if $lastname;
1076 $cfg->{users}->{$user}->{email} = $email;
1077 $cfg->{users}->{$user}->{comment} = PVE::Tools::decode_text($comment) if $comment;
1078 $cfg->{users}->{$user}->{expire} = $expire;
1079 # keys: allowed yubico key ids or oath secrets (base32 encoded)
1080 $cfg->{users}->{$user}->{keys} = $keys if $keys;
1081
1082 #$cfg->{users}->{$user}->{groups}->{$group} = 1;
1083 #$cfg->{groups}->{$group}->{$user} = 1;
1084
1085 } elsif ($et eq 'group') {
1086 my ($group, $userlist, $comment) = @data;
1087
1088 if (!verify_groupname($group, 1)) {
1089 warn "user config - ignore group '$group' - invalid characters in group name\n";
1090 next;
1091 }
1092
1093 # make sure to add the group (even if there are no members)
1094 $cfg->{groups}->{$group} = { users => {} } if !$cfg->{groups}->{$group};
1095
1096 $cfg->{groups}->{$group}->{comment} = PVE::Tools::decode_text($comment) if $comment;
1097
1098 foreach my $user (split_list($userlist)) {
1099
1100 if (!PVE::Auth::Plugin::verify_username($user, 1)) {
1101 warn "user config - ignore invalid group member '$user'\n";
1102 next;
1103 }
1104
1105 if ($cfg->{users}->{$user}) { # user exists
1106 $cfg->{users}->{$user}->{groups}->{$group} = 1;
1107 } else {
1108 warn "user config - ignore invalid group member '$user'\n";
1109 }
1110 $cfg->{groups}->{$group}->{users}->{$user} = 1;
1111 }
1112
1113 } elsif ($et eq 'role') {
1114 my ($role, $privlist) = @data;
1115
1116 if (!verify_rolename($role, 1)) {
1117 warn "user config - ignore role '$role' - invalid characters in role name\n";
1118 next;
1119 }
1120
1121 # make sure to add the role (even if there are no privileges)
1122 $cfg->{roles}->{$role} = {} if !$cfg->{roles}->{$role};
1123
1124 foreach my $priv (split_list($privlist)) {
1125 if (defined ($valid_privs->{$priv})) {
1126 $cfg->{roles}->{$role}->{$priv} = 1;
1127 } else {
1128 warn "user config - ignore invalid privilege '$priv'\n";
1129 }
1130 }
1131
1132 } elsif ($et eq 'acl') {
1133 my ($propagate, $pathtxt, $uglist, $rolelist) = @data;
1134
1135 $propagate = $propagate ? 1 : 0;
1136
1137 if (my $path = normalize_path($pathtxt)) {
1138 foreach my $role (split_list($rolelist)) {
1139
1140 if (!verify_rolename($role, 1)) {
1141 warn "user config - ignore invalid role name '$role' in acl\n";
1142 next;
1143 }
1144
1145 if (!$cfg->{roles}->{$role}) {
1146 warn "user config - ignore invalid acl role '$role'\n";
1147 next;
1148 }
1149
1150 foreach my $ug (split_list($uglist)) {
1151 my ($group) = $ug =~ m/^@(\S+)$/;
1152
1153 if ($group && verify_groupname($group, 1)) {
1154 if (!$cfg->{groups}->{$group}) { # group does not exist
1155 warn "user config - ignore invalid acl group '$group'\n";
1156 }
1157 $cfg->{acl}->{$path}->{groups}->{$group}->{$role} = $propagate;
1158 } elsif (PVE::Auth::Plugin::verify_username($ug, 1)) {
1159 if (!$cfg->{users}->{$ug}) { # user does not exist
1160 warn "user config - ignore invalid acl member '$ug'\n";
1161 }
1162 $cfg->{acl}->{$path}->{users}->{$ug}->{$role} = $propagate;
1163 } elsif (my ($user, $token) = split_tokenid($ug, 1)) {
1164 if (check_token_exist($cfg, $user, $token, 1)) {
1165 $cfg->{acl}->{$path}->{tokens}->{$ug}->{$role} = $propagate;
1166 } else {
1167 warn "user config - ignore invalid acl token '$ug'\n";
1168 }
1169 } else {
1170 warn "user config - invalid user/group '$ug' in acl\n";
1171 }
1172 }
1173 }
1174 } else {
1175 warn "user config - ignore invalid path in acl '$pathtxt'\n";
1176 }
1177 } elsif ($et eq 'pool') {
1178 my ($pool, $comment, $vmlist, $storelist) = @data;
1179
1180 if (!verify_poolname($pool, 1)) {
1181 warn "user config - ignore pool '$pool' - invalid characters in pool name\n";
1182 next;
1183 }
1184
1185 # make sure to add the pool (even if there are no members)
1186 $cfg->{pools}->{$pool} = { vms => {}, storage => {} } if !$cfg->{pools}->{$pool};
1187
1188 $cfg->{pools}->{$pool}->{comment} = PVE::Tools::decode_text($comment) if $comment;
1189
1190 foreach my $vmid (split_list($vmlist)) {
1191 if ($vmid !~ m/^\d+$/) {
1192 warn "user config - ignore invalid vmid '$vmid' in pool '$pool'\n";
1193 next;
1194 }
1195 $vmid = int($vmid);
1196
1197 if ($cfg->{vms}->{$vmid}) {
1198 warn "user config - ignore duplicate vmid '$vmid' in pool '$pool'\n";
1199 next;
1200 }
1201
1202 $cfg->{pools}->{$pool}->{vms}->{$vmid} = 1;
1203
1204 # record vmid ==> pool relation
1205 $cfg->{vms}->{$vmid} = $pool;
1206 }
1207
1208 foreach my $storeid (split_list($storelist)) {
1209 if ($storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
1210 warn "user config - ignore invalid storage '$storeid' in pool '$pool'\n";
1211 next;
1212 }
1213 $cfg->{pools}->{$pool}->{storage}->{$storeid} = 1;
1214 }
1215 } elsif ($et eq 'token') {
1216 my ($tokenid, $expire, $privsep, $comment) = @data;
1217
1218 my ($user, $token) = split_tokenid($tokenid, 1);
1219 if (!($user && $token)) {
1220 warn "user config - ignore invalid tokenid '$tokenid'\n";
1221 next;
1222 }
1223
1224 $privsep = $privsep ? 1 : 0;
1225
1226 $expire = 0 if !$expire;
1227
1228 if ($expire !~ m/^\d+$/) {
1229 warn "user config - ignore token '$tokenid' - (illegal characters in expire '$expire')\n";
1230 next;
1231 }
1232 $expire = int($expire);
1233
1234 if (my $user_cfg = $cfg->{users}->{$user}) { # user exists
1235 $user_cfg->{tokens}->{$token} = {} if !$user_cfg->{tokens}->{$token};
1236 my $token_cfg = $user_cfg->{tokens}->{$token};
1237 $token_cfg->{privsep} = $privsep;
1238 $token_cfg->{expire} = $expire;
1239 $token_cfg->{comment} = PVE::Tools::decode_text($comment) if $comment;
1240 } else {
1241 warn "user config - ignore token '$tokenid' - user does not exist\n";
1242 }
1243 } else {
1244 warn "user config - ignore config line: $line\n";
1245 }
1246 }
1247
1248 userconfig_force_defaults($cfg);
1249
1250 return $cfg;
1251 }
1252
1253 sub write_user_config {
1254 my ($filename, $cfg) = @_;
1255
1256 my $data = '';
1257
1258 foreach my $user (sort keys %{$cfg->{users}}) {
1259 my $d = $cfg->{users}->{$user};
1260 my $firstname = $d->{firstname} ? PVE::Tools::encode_text($d->{firstname}) : '';
1261 my $lastname = $d->{lastname} ? PVE::Tools::encode_text($d->{lastname}) : '';
1262 my $email = $d->{email} || '';
1263 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
1264 my $expire = int($d->{expire} || 0);
1265 my $enable = $d->{enable} ? 1 : 0;
1266 my $keys = $d->{keys} ? $d->{keys} : '';
1267 $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:$keys:\n";
1268
1269 my $user_tokens = $d->{tokens};
1270 foreach my $token (sort keys %$user_tokens) {
1271 my $td = $user_tokens->{$token};
1272 my $full_tokenid = join_tokenid($user, $token);
1273 my $comment = $td->{comment} ? PVE::Tools::encode_text($td->{comment}) : '';
1274 my $expire = int($td->{expire} || 0);
1275 my $privsep = $td->{privsep} ? 1 : 0;
1276 $data .= "token:$full_tokenid:$expire:$privsep:$comment:\n";
1277 }
1278 }
1279
1280 $data .= "\n";
1281
1282 foreach my $group (sort keys %{$cfg->{groups}}) {
1283 my $d = $cfg->{groups}->{$group};
1284 my $list = join (',', sort keys %{$d->{users}});
1285 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
1286 $data .= "group:$group:$list:$comment:\n";
1287 }
1288
1289 $data .= "\n";
1290
1291 foreach my $pool (sort keys %{$cfg->{pools}}) {
1292 my $d = $cfg->{pools}->{$pool};
1293 my $vmlist = join (',', sort keys %{$d->{vms}});
1294 my $storelist = join (',', sort keys %{$d->{storage}});
1295 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
1296 $data .= "pool:$pool:$comment:$vmlist:$storelist:\n";
1297 }
1298
1299 $data .= "\n";
1300
1301 foreach my $role (sort keys %{$cfg->{roles}}) {
1302 next if $special_roles->{$role};
1303
1304 my $d = $cfg->{roles}->{$role};
1305 my $list = join (',', sort keys %$d);
1306 $data .= "role:$role:$list:\n";
1307 }
1308
1309 $data .= "\n";
1310
1311 my $collect_rolelist_members = sub {
1312 my ($acl_members, $result, $prefix, $exclude) = @_;
1313
1314 foreach my $member (keys %$acl_members) {
1315 next if $exclude && $member eq $exclude;
1316
1317 my $l0 = '';
1318 my $l1 = '';
1319 foreach my $role (sort keys %{$acl_members->{$member}}) {
1320 my $propagate = $acl_members->{$member}->{$role};
1321 if ($propagate) {
1322 $l1 .= ',' if $l1;
1323 $l1 .= $role;
1324 } else {
1325 $l0 .= ',' if $l0;
1326 $l0 .= $role;
1327 }
1328 }
1329 $result->{0}->{$l0}->{"${prefix}${member}"} = 1 if $l0;
1330 $result->{1}->{$l1}->{"${prefix}${member}"} = 1 if $l1;
1331 }
1332 };
1333
1334 foreach my $path (sort keys %{$cfg->{acl}}) {
1335 my $d = $cfg->{acl}->{$path};
1336
1337 my $rolelist_members = {};
1338
1339 $collect_rolelist_members->($d->{'groups'}, $rolelist_members, '@');
1340
1341 # no need to save 'root@pam', it is always 'Administrator'
1342 $collect_rolelist_members->($d->{'users'}, $rolelist_members, '', 'root@pam');
1343
1344 $collect_rolelist_members->($d->{'tokens'}, $rolelist_members, '');
1345
1346 foreach my $propagate (0,1) {
1347 my $filtered = $rolelist_members->{$propagate};
1348 foreach my $rolelist (sort keys %$filtered) {
1349 my $uglist = join (',', sort keys %{$filtered->{$rolelist}});
1350 $data .= "acl:$propagate:$path:$uglist:$rolelist:\n";
1351 }
1352
1353 }
1354 }
1355
1356 return $data;
1357 }
1358
1359 # The TFA configuration in priv/tfa.cfg format contains one line per user of
1360 # the form:
1361 # USER:TYPE:DATA
1362 # DATA is a base64 encoded json string and its format depends on the type.
1363 sub parse_priv_tfa_config {
1364 my ($filename, $raw) = @_;
1365
1366 my $users = {};
1367 my $cfg = { users => $users };
1368
1369 $raw = '' if !defined($raw);
1370 while ($raw =~ /^\s*(.+?)\s*$/gm) {
1371 my $line = $1;
1372 my ($user, $type, $data) = split(/:/, $line, 3);
1373
1374 my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1);
1375 if (!$realm) {
1376 warn "user tfa config - ignore user '$user' - invalid user name\n";
1377 next;
1378 }
1379
1380 $data = decode_json(decode_base64($data));
1381
1382 $users->{$user} = {
1383 type => $type,
1384 data => $data,
1385 };
1386 }
1387
1388 return $cfg;
1389 }
1390
1391 sub write_priv_tfa_config {
1392 my ($filename, $cfg) = @_;
1393
1394 my $output = '';
1395
1396 my $users = $cfg->{users};
1397 foreach my $user (sort keys %$users) {
1398 my $info = $users->{$user};
1399 next if !%$info; # skip empty entries
1400
1401 $info = {%$info}; # copy to verify contents:
1402
1403 my $type = delete $info->{type};
1404 my $data = delete $info->{data};
1405
1406 if (my @keys = keys %$info) {
1407 die "invalid keys in TFA config for user $user: " . join(', ', @keys) . "\n";
1408 }
1409
1410 $data = encode_base64(encode_json($data), '');
1411 $output .= "${user}:${type}:${data}\n";
1412 }
1413
1414 return $output;
1415 }
1416
1417 sub roles {
1418 my ($cfg, $user, $path) = @_;
1419
1420 # NOTE: we do not consider pools here.
1421 # NOTE: for privsep tokens, this does not filter roles by those that the
1422 # corresponding user has.
1423 # Use $rpcenv->permission() for any actual permission checks!
1424
1425 return 'Administrator' if $user eq 'root@pam'; # root can do anything
1426
1427 if (pve_verify_tokenid($user, 1)) {
1428 my $tokenid = $user;
1429 my ($username, $token) = split_tokenid($tokenid);
1430
1431 my $token_info = $cfg->{users}->{$username}->{tokens}->{$token};
1432 return () if !$token_info;
1433
1434 my $user_roles = roles($cfg, $username, $path);
1435
1436 # return full user privileges
1437 return $user_roles if !$token_info->{privsep};
1438 }
1439
1440 my $roles = {};
1441
1442 foreach my $p (sort keys %{$cfg->{acl}}) {
1443 my $final = ($path eq $p);
1444
1445 next if !(($p eq '/') || $final || ($path =~ m|^$p/|));
1446
1447 my $acl = $cfg->{acl}->{$p};
1448
1449 #print "CHECKACL $path $p\n";
1450 #print "ACL $path = " . Dumper ($acl);
1451 if (my $ri = $acl->{tokens}->{$user}) {
1452 my $new;
1453 foreach my $role (keys %$ri) {
1454 my $propagate = $ri->{$role};
1455 if ($final || $propagate) {
1456 #print "APPLY ROLE $p $user $role\n";
1457 $new = {} if !$new;
1458 $new->{$role} = $propagate;
1459 }
1460 }
1461 if ($new) {
1462 $roles = $new; # overwrite previous settings
1463 next;
1464 }
1465 }
1466
1467 if (my $ri = $acl->{users}->{$user}) {
1468 my $new;
1469 foreach my $role (keys %$ri) {
1470 my $propagate = $ri->{$role};
1471 if ($final || $propagate) {
1472 #print "APPLY ROLE $p $user $role\n";
1473 $new = {} if !$new;
1474 $new->{$role} = $propagate;
1475 }
1476 }
1477 if ($new) {
1478 $roles = $new; # overwrite previous settings
1479 next; # user privs always override group privs
1480 }
1481 }
1482
1483 my $new;
1484 foreach my $g (keys %{$acl->{groups}}) {
1485 next if !$cfg->{groups}->{$g}->{users}->{$user};
1486 if (my $ri = $acl->{groups}->{$g}) {
1487 foreach my $role (keys %$ri) {
1488 my $propagate = $ri->{$role};
1489 if ($final || $propagate) {
1490 #print "APPLY ROLE $p \@$g $role\n";
1491 $new = {} if !$new;
1492 $new->{$role} = $propagate;
1493 }
1494 }
1495 }
1496 }
1497 if ($new) {
1498 $roles = $new; # overwrite previous settings
1499 next;
1500 }
1501 }
1502
1503 return { 'NoAccess' => $roles->{NoAccess} } if defined ($roles->{NoAccess});
1504 #return () if defined ($roles->{NoAccess});
1505
1506 #print "permission $user $path = " . Dumper ($roles);
1507
1508 #print "roles $user $path = " . join (',', @ra) . "\n";
1509
1510 return $roles;
1511 }
1512
1513 sub remove_vm_access {
1514 my ($vmid) = @_;
1515 my $delVMaccessFn = sub {
1516 my $usercfg = cfs_read_file("user.cfg");
1517 my $modified;
1518
1519 if (my $acl = $usercfg->{acl}->{"/vms/$vmid"}) {
1520 delete $usercfg->{acl}->{"/vms/$vmid"};
1521 $modified = 1;
1522 }
1523 if (my $pool = $usercfg->{vms}->{$vmid}) {
1524 if (my $data = $usercfg->{pools}->{$pool}) {
1525 delete $data->{vms}->{$vmid};
1526 delete $usercfg->{vms}->{$vmid};
1527 $modified = 1;
1528 }
1529 }
1530 cfs_write_file("user.cfg", $usercfg) if $modified;
1531 };
1532
1533 lock_user_config($delVMaccessFn, "access permissions cleanup for VM $vmid failed");
1534 }
1535
1536 sub remove_storage_access {
1537 my ($storeid) = @_;
1538
1539 my $deleteStorageAccessFn = sub {
1540 my $usercfg = cfs_read_file("user.cfg");
1541 my $modified;
1542
1543 if (my $storage = $usercfg->{acl}->{"/storage/$storeid"}) {
1544 delete $usercfg->{acl}->{"/storage/$storeid"};
1545 $modified = 1;
1546 }
1547 foreach my $pool (keys %{$usercfg->{pools}}) {
1548 delete $usercfg->{pools}->{$pool}->{storage}->{$storeid};
1549 $modified = 1;
1550 }
1551 cfs_write_file("user.cfg", $usercfg) if $modified;
1552 };
1553
1554 lock_user_config($deleteStorageAccessFn,
1555 "access permissions cleanup for storage $storeid failed");
1556 }
1557
1558 sub add_vm_to_pool {
1559 my ($vmid, $pool) = @_;
1560
1561 my $addVMtoPoolFn = sub {
1562 my $usercfg = cfs_read_file("user.cfg");
1563 if (my $data = $usercfg->{pools}->{$pool}) {
1564 $data->{vms}->{$vmid} = 1;
1565 $usercfg->{vms}->{$vmid} = $pool;
1566 cfs_write_file("user.cfg", $usercfg);
1567 }
1568 };
1569
1570 lock_user_config($addVMtoPoolFn, "can't add VM $vmid to pool '$pool'");
1571 }
1572
1573 sub remove_vm_from_pool {
1574 my ($vmid) = @_;
1575
1576 my $delVMfromPoolFn = sub {
1577 my $usercfg = cfs_read_file("user.cfg");
1578 if (my $pool = $usercfg->{vms}->{$vmid}) {
1579 if (my $data = $usercfg->{pools}->{$pool}) {
1580 delete $data->{vms}->{$vmid};
1581 delete $usercfg->{vms}->{$vmid};
1582 cfs_write_file("user.cfg", $usercfg);
1583 }
1584 }
1585 };
1586
1587 lock_user_config($delVMfromPoolFn, "pool cleanup for VM $vmid failed");
1588 }
1589
1590 my $USER_CONTROLLED_TFA_TYPES = {
1591 u2f => 1,
1592 oath => 1,
1593 };
1594
1595 # Delete an entry by setting $data=undef in which case $type is ignored.
1596 # Otherwise both must be valid.
1597 sub user_set_tfa {
1598 my ($userid, $realm, $type, $data, $cached_usercfg, $cached_domaincfg) = @_;
1599
1600 if (defined($data) && !defined($type)) {
1601 # This is an internal usage error and should not happen
1602 die "cannot set tfa data without a type\n";
1603 }
1604
1605 my $user_cfg = $cached_usercfg || cfs_read_file('user.cfg');
1606 my $user = $user_cfg->{users}->{$userid}
1607 or die "user '$userid' not found\n";
1608
1609 my $domain_cfg = $cached_domaincfg || cfs_read_file('domains.cfg');
1610 my $realm_cfg = $domain_cfg->{ids}->{$realm};
1611 die "auth domain '$realm' does not exist\n" if !$realm_cfg;
1612
1613 my $realm_tfa = $realm_cfg->{tfa};
1614 if (defined($realm_tfa)) {
1615 $realm_tfa = PVE::Auth::Plugin::parse_tfa_config($realm_tfa);
1616 # If the realm has a TFA setting, we're only allowed to use that.
1617 if (defined($data)) {
1618 my $required_type = $realm_tfa->{type};
1619 if ($required_type ne $type) {
1620 die "realm '$realm' only allows TFA of type '$required_type\n";
1621 }
1622
1623 if (defined($data->{config})) {
1624 # XXX: Is it enough if the type matches? Or should the configuration also match?
1625 }
1626
1627 # realm-configured tfa always uses a simple key list, so use the user.cfg
1628 $user->{keys} = $data->{keys};
1629 } else {
1630 die "realm '$realm' does not allow removing the 2nd factor\n";
1631 }
1632 } else {
1633 # Without a realm-enforced TFA setting the user can add a u2f or totp entry by themselves.
1634 # The 'yubico' type requires yubico server settings, which have to be configured on the
1635 # realm, so this is not supported here:
1636 die "domain '$realm' does not support TFA type '$type'\n"
1637 if defined($data) && !$USER_CONTROLLED_TFA_TYPES->{$type};
1638 }
1639
1640 # Custom TFA entries are stored in priv/tfa.cfg as they can be more complet: u2f uses a
1641 # public key and a key handle, TOTP requires the usual totp settings...
1642
1643 my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
1644 my $tfa = ($tfa_cfg->{users}->{$userid} //= {});
1645
1646 if (defined($data)) {
1647 $tfa->{type} = $type;
1648 $tfa->{data} = $data;
1649 cfs_write_file('priv/tfa.cfg', $tfa_cfg);
1650
1651 $user->{keys} = "x!$type";
1652 } else {
1653 delete $tfa_cfg->{users}->{$userid};
1654 cfs_write_file('priv/tfa.cfg', $tfa_cfg);
1655
1656 delete $user->{keys};
1657 }
1658
1659 cfs_write_file('user.cfg', $user_cfg);
1660 }
1661
1662 sub user_get_tfa {
1663 my ($username, $realm) = @_;
1664
1665 my $user_cfg = cfs_read_file('user.cfg');
1666 my $user = $user_cfg->{users}->{$username}
1667 or die "user '$username' not found\n";
1668
1669 my $keys = $user->{keys};
1670
1671 my $domain_cfg = cfs_read_file('domains.cfg');
1672 my $realm_cfg = $domain_cfg->{ids}->{$realm};
1673 die "auth domain '$realm' does not exist\n" if !$realm_cfg;
1674
1675 my $realm_tfa = $realm_cfg->{tfa};
1676 $realm_tfa = PVE::Auth::Plugin::parse_tfa_config($realm_tfa)
1677 if $realm_tfa;
1678
1679 if (!$keys) {
1680 return if !$realm_tfa;
1681 die "missing required 2nd keys\n";
1682 }
1683
1684 # new style config starts with an 'x' and optionally contains a !<type> suffix
1685 if ($keys !~ /^x(?:!.*)?$/) {
1686 # old style config, find the type via the realm
1687 return if !$realm_tfa;
1688 return ($realm_tfa->{type}, {
1689 keys => $keys,
1690 config => $realm_tfa,
1691 });
1692 } else {
1693 my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
1694 my $tfa = $tfa_cfg->{users}->{$username};
1695 return if !$tfa; # should not happen (user.cfg wasn't cleaned up?)
1696
1697 if ($realm_tfa) {
1698 # if the realm has a tfa setting we need to verify the type:
1699 die "auth domain '$realm' and user have mismatching TFA settings\n"
1700 if $realm_tfa && $realm_tfa->{type} ne $tfa->{type};
1701 }
1702
1703 return ($tfa->{type}, $tfa->{data});
1704 }
1705 }
1706
1707 # bash completion helpers
1708
1709 register_standard_option('userid-completed',
1710 get_standard_option('userid', { completion => \&complete_username}),
1711 );
1712
1713 sub complete_username {
1714
1715 my $user_cfg = cfs_read_file('user.cfg');
1716
1717 return [ keys %{$user_cfg->{users}} ];
1718 }
1719
1720 sub complete_group {
1721
1722 my $user_cfg = cfs_read_file('user.cfg');
1723
1724 return [ keys %{$user_cfg->{groups}} ];
1725 }
1726
1727 sub complete_realm {
1728
1729 my $domain_cfg = cfs_read_file('domains.cfg');
1730
1731 return [ keys %{$domain_cfg->{ids}} ];
1732 }
1733
1734 1;