]> git.proxmox.com Git - pve-access-control.git/blob - PVE/AccessControl.pm
typo fixup
[pve-access-control.git] / 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 # TODO: set to 24h for PVE 6.0
52 my $authkey_lifetime = 3600*0; # rotation disabled
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 if (time() - $mtime >= $authkey_lifetime) {
153 warn "auth key pair too old, rotating..\n" if !$quiet;;
154 return 0;
155 } else {
156 warn "auth key new enough, skipping rotation\n" if !$quiet;;
157 return 1;
158 }
159 }
160 }
161
162 sub rotate_authkey {
163 return if $authkey_lifetime == 0;
164
165 PVE::Cluster::cfs_lock_authkey(undef, sub {
166 # re-check with lock to avoid double rotation in clusters
167 return if check_authkey();
168
169 my $old = get_pubkey();
170
171 if ($old) {
172 eval {
173 my $pem = $old->get_public_key_x509_string();
174 PVE::Tools::file_set_contents($pve_auth_key_files->{pubold}, $pem);
175 };
176 die "Failed to store old auth key: $@\n" if $@;
177 }
178
179 my $new = Crypt::OpenSSL::RSA->generate_key(2048);
180 eval {
181 my $pem = $new->get_public_key_x509_string();
182 PVE::Tools::file_set_contents($pve_auth_key_files->{pub}, $pem);
183 };
184 if ($@) {
185 if ($old) {
186 warn "Failed to store new auth key - $@\n";
187 warn "Reverting to previous auth key\n";
188 eval {
189 my $pem = $old->get_public_key_x509_string();
190 PVE::Tools::file_set_contents($pve_auth_key_files->{pub}, $pem);
191 };
192 die "Failed to restore old auth key: $@\n" if $@;
193 } else {
194 die "Failed to store new auth key - $@\n";
195 }
196 }
197
198 eval {
199 my $pem = $new->get_private_key_string();
200 PVE::Tools::file_set_contents($pve_auth_key_files->{priv}, $pem);
201 };
202 if ($@) {
203 warn "Failed to store new auth key - $@\n";
204 warn "Deleting auth key to force regeneration\n";
205 unlink $pve_auth_key_files->{pub};
206 unlink $pve_auth_key_files->{priv};
207 }
208 });
209 die $@ if $@;
210 }
211
212 my $csrf_prevention_secret;
213 my $get_csrfr_secret = sub {
214 if (!$csrf_prevention_secret) {
215 my $input = PVE::Tools::file_get_contents($pve_www_key_fn);
216 $csrf_prevention_secret = Digest::SHA::sha1_base64($input);
217 }
218 return $csrf_prevention_secret;
219 };
220
221 sub assemble_csrf_prevention_token {
222 my ($username) = @_;
223
224 my $secret = &$get_csrfr_secret();
225
226 return PVE::Ticket::assemble_csrf_prevention_token ($secret, $username);
227 }
228
229 sub verify_csrf_prevention_token {
230 my ($username, $token, $noerr) = @_;
231
232 my $secret = &$get_csrfr_secret();
233
234 return PVE::Ticket::verify_csrf_prevention_token(
235 $secret, $username, $token, -300, $ticket_lifetime, $noerr);
236 }
237
238 my $get_ticket_age_range = sub {
239 my ($now, $mtime, $rotated) = @_;
240
241 my $key_age = $now - $mtime;
242 $key_age = 0 if $key_age < 0;
243
244 my $min = -300;
245 my $max = $ticket_lifetime;
246
247 if ($rotated) {
248 # ticket creation after rotation is not allowed
249 $min = $key_age - 300;
250 } else {
251 if ($key_age > $authkey_lifetime && $authkey_lifetime > 0) {
252 if (PVE::Cluster::check_cfs_quorum(1)) {
253 # key should have been rotated, clamp range accordingly
254 $min = $key_age - $authkey_lifetime;
255 } else {
256 warn "Cluster not quorate - extending auth key lifetime!\n";
257 }
258 }
259
260 $max = $key_age + 300 if $key_age < $ticket_lifetime;
261 }
262
263 return undef if $min > $ticket_lifetime;
264 return ($min, $max);
265 };
266
267 sub assemble_ticket {
268 my ($data) = @_;
269
270 my $rsa_priv = get_privkey();
271
272 return PVE::Ticket::assemble_rsa_ticket($rsa_priv, 'PVE', $data);
273 }
274
275 sub verify_ticket {
276 my ($ticket, $noerr) = @_;
277
278 my $now = time();
279
280 my $check = sub {
281 my ($old) = @_;
282
283 my ($rsa_pub, $rsa_mtime) = get_pubkey($old);
284 return undef if !$rsa_pub;
285
286 my ($min, $max) = $get_ticket_age_range->($now, $rsa_mtime, $old);
287 return undef if !$min;
288
289 return PVE::Ticket::verify_rsa_ticket(
290 $rsa_pub, 'PVE', $ticket, undef, $min, $max, 1);
291 };
292
293 my ($data, $age) = $check->();
294
295 # check with old, rotated key if current key failed
296 ($data, $age) = $check->(1) if !defined($data);
297
298 my $auth_failure = sub {
299 if ($noerr) {
300 return undef;
301 } else {
302 # raise error via undef ticket
303 PVE::Ticket::verify_rsa_ticket(undef, 'PVE');
304 }
305 };
306
307 if (!defined($data)) {
308 return $auth_failure->();
309 }
310
311 my ($username, $tfa_info);
312 if ($data =~ m{^u2f!([^!]+)!([0-9a-zA-Z/.=_\-+]+)$}) {
313 # Ticket for u2f-users:
314 ($username, my $challenge) = ($1, $2);
315 if ($challenge eq 'verified') {
316 # u2f challenge was completed
317 $challenge = undef;
318 } elsif (!wantarray) {
319 # The caller is not aware there could be an ongoing challenge,
320 # so we treat this ticket as invalid:
321 return $auth_failure->();
322 }
323 $tfa_info = {
324 type => 'u2f',
325 challenge => $challenge,
326 };
327 } elsif ($data =~ /^tfa!(.*)$/) {
328 # TOTP and Yubico don't require a challenge so this is the generic
329 # 'missing 2nd factor ticket'
330 $username = $1;
331 $tfa_info = { type => 'tfa' };
332 } else {
333 # Regular ticket (full access)
334 $username = $data;
335 }
336
337 return undef if !PVE::Auth::Plugin::verify_username($username, $noerr);
338
339 return wantarray ? ($username, $age, $tfa_info) : $username;
340 }
341
342 # VNC tickets
343 # - they do not contain the username in plain text
344 # - they are restricted to a specific resource path (example: '/vms/100')
345 sub assemble_vnc_ticket {
346 my ($username, $path) = @_;
347
348 my $rsa_priv = get_privkey();
349
350 $path = normalize_path($path);
351
352 my $secret_data = "$username:$path";
353
354 return PVE::Ticket::assemble_rsa_ticket(
355 $rsa_priv, 'PVEVNC', undef, $secret_data);
356 }
357
358 sub verify_vnc_ticket {
359 my ($ticket, $username, $path, $noerr) = @_;
360
361 my $secret_data = "$username:$path";
362
363 my ($rsa_pub, $rsa_mtime) = get_pubkey();
364 if (!$rsa_pub || (time() - $rsa_mtime > $authkey_lifetime && $authkey_lifetime > 0)) {
365 if ($noerr) {
366 return undef;
367 } else {
368 # raise error via undef ticket
369 PVE::Ticket::verify_rsa_ticket($rsa_pub, 'PVEVNC');
370 }
371 }
372
373 return PVE::Ticket::verify_rsa_ticket(
374 $rsa_pub, 'PVEVNC', $ticket, $secret_data, -20, 40, $noerr);
375 }
376
377 sub assemble_spice_ticket {
378 my ($username, $vmid, $node) = @_;
379
380 my $secret = &$get_csrfr_secret();
381
382 return PVE::Ticket::assemble_spice_ticket(
383 $secret, $username, $vmid, $node);
384 }
385
386 sub verify_spice_connect_url {
387 my ($connect_str) = @_;
388
389 my $secret = &$get_csrfr_secret();
390
391 return PVE::Ticket::verify_spice_connect_url($secret, $connect_str);
392 }
393
394 sub read_x509_subject_spice {
395 my ($filename) = @_;
396
397 # read x509 subject
398 my $bio = Net::SSLeay::BIO_new_file($filename, 'r');
399 die "Could not open $filename using OpenSSL\n"
400 if !$bio;
401
402 my $x509 = Net::SSLeay::PEM_read_bio_X509($bio);
403 Net::SSLeay::BIO_free($bio);
404
405 die "Could not parse X509 certificate in $filename\n"
406 if !$x509;
407
408 my $nameobj = Net::SSLeay::X509_get_subject_name($x509);
409 my $subject = Net::SSLeay::X509_NAME_oneline($nameobj);
410 Net::SSLeay::X509_free($x509);
411
412 # remote-viewer wants comma as seperator (not '/')
413 $subject =~ s!^/!!;
414 $subject =~ s!/(\w+=)!,$1!g;
415
416 return $subject;
417 }
418
419 # helper to generate SPICE remote-viewer configuration
420 sub remote_viewer_config {
421 my ($authuser, $vmid, $node, $proxy, $title, $port) = @_;
422
423 if (!$proxy) {
424 my $host = `hostname -f` || PVE::INotify::nodename();
425 chomp $host;
426 $proxy = $host;
427 }
428
429 my ($ticket, $proxyticket) = assemble_spice_ticket($authuser, $vmid, $node);
430
431 my $filename = "/etc/pve/local/pve-ssl.pem";
432 my $subject = read_x509_subject_spice($filename);
433
434 my $cacert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192);
435 $cacert =~ s/\n/\\n/g;
436
437 $proxy = "[$proxy]" if Net::IP::ip_is_ipv6($proxy);
438 my $config = {
439 'secure-attention' => "Ctrl+Alt+Ins",
440 'toggle-fullscreen' => "Shift+F11",
441 'release-cursor' => "Ctrl+Alt+R",
442 type => 'spice',
443 title => $title,
444 host => $proxyticket, # this breaks tls hostname verification, so we need to use 'host-subject'
445 proxy => "http://$proxy:3128",
446 'tls-port' => $port,
447 'host-subject' => $subject,
448 ca => $cacert,
449 password => $ticket,
450 'delete-this-file' => 1,
451 };
452
453 return ($ticket, $proxyticket, $config);
454 }
455
456 sub check_user_exist {
457 my ($usercfg, $username, $noerr) = @_;
458
459 $username = PVE::Auth::Plugin::verify_username($username, $noerr);
460 return undef if !$username;
461
462 return $usercfg->{users}->{$username} if $usercfg && $usercfg->{users}->{$username};
463
464 die "no such user ('$username')\n" if !$noerr;
465
466 return undef;
467 }
468
469 sub check_user_enabled {
470 my ($usercfg, $username, $noerr) = @_;
471
472 my $data = check_user_exist($usercfg, $username, $noerr);
473 return undef if !$data;
474
475 return 1 if $data->{enable};
476
477 die "user '$username' is disabled\n" if !$noerr;
478
479 return undef;
480 }
481
482 sub verify_one_time_pw {
483 my ($type, $username, $keys, $tfa_cfg, $otp) = @_;
484
485 die "missing one time password for two-factor authentication '$type'\n" if !$otp;
486
487 # fixme: proxy support?
488 my $proxy;
489
490 if ($type eq 'yubico') {
491 PVE::OTP::yubico_verify_otp($otp, $keys, $tfa_cfg->{url},
492 $tfa_cfg->{id}, $tfa_cfg->{key}, $proxy);
493 } elsif ($type eq 'oath') {
494 PVE::OTP::oath_verify_otp($otp, $keys, $tfa_cfg->{step}, $tfa_cfg->{digits});
495 } else {
496 die "unknown tfa type '$type'\n";
497 }
498 }
499
500 # password should be utf8 encoded
501 # Note: some plugins delay/sleep if auth fails
502 sub authenticate_user {
503 my ($username, $password, $otp) = @_;
504
505 die "no username specified\n" if !$username;
506
507 my ($ruid, $realm);
508
509 ($username, $ruid, $realm) = PVE::Auth::Plugin::verify_username($username);
510
511 my $usercfg = cfs_read_file('user.cfg');
512
513 check_user_enabled($usercfg, $username);
514
515 my $ctime = time();
516 my $expire = $usercfg->{users}->{$username}->{expire};
517
518 die "account expired\n" if $expire && ($expire < $ctime);
519
520 my $domain_cfg = cfs_read_file('domains.cfg');
521
522 my $cfg = $domain_cfg->{ids}->{$realm};
523 die "auth domain '$realm' does not exists\n" if !$cfg;
524 my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
525 $plugin->authenticate_user($cfg, $realm, $ruid, $password);
526
527 my ($type, $tfa_data) = user_get_tfa($username, $realm);
528 if ($type) {
529 if ($type eq 'u2f') {
530 # Note that if the user did not manage to complete the initial u2f registration
531 # challenge we have a hash containing a 'challenge' entry in the user's tfa.cfg entry:
532 $tfa_data = undef if exists $tfa_data->{challenge};
533 } elsif (!defined($otp)) {
534 # The user requires a 2nd factor but has not provided one. Return success but
535 # don't clear $tfa_data.
536 } else {
537 my $keys = $tfa_data->{keys};
538 my $tfa_cfg = $tfa_data->{config};
539 verify_one_time_pw($type, $username, $keys, $tfa_cfg, $otp);
540 $tfa_data = undef;
541 }
542
543 # Return the type along with the rest:
544 if ($tfa_data) {
545 $tfa_data = {
546 type => $type,
547 data => $tfa_data,
548 };
549 }
550 }
551
552 return wantarray ? ($username, $tfa_data) : $username;
553 }
554
555 sub domain_set_password {
556 my ($realm, $username, $password) = @_;
557
558 die "no auth domain specified" if !$realm;
559
560 my $domain_cfg = cfs_read_file('domains.cfg');
561
562 my $cfg = $domain_cfg->{ids}->{$realm};
563 die "auth domain '$realm' does not exist\n" if !$cfg;
564 my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
565 $plugin->store_password($cfg, $realm, $username, $password);
566 }
567
568 sub add_user_group {
569 my ($username, $usercfg, $group) = @_;
570
571 $usercfg->{users}->{$username}->{groups}->{$group} = 1;
572 $usercfg->{groups}->{$group}->{users}->{$username} = 1;
573 }
574
575 sub delete_user_group {
576 my ($username, $usercfg) = @_;
577
578 foreach my $group (keys %{$usercfg->{groups}}) {
579
580 delete ($usercfg->{groups}->{$group}->{users}->{$username})
581 if $usercfg->{groups}->{$group}->{users}->{$username};
582 }
583 }
584
585 sub delete_user_acl {
586 my ($username, $usercfg) = @_;
587
588 foreach my $acl (keys %{$usercfg->{acl}}) {
589
590 delete ($usercfg->{acl}->{$acl}->{users}->{$username})
591 if $usercfg->{acl}->{$acl}->{users}->{$username};
592 }
593 }
594
595 sub delete_group_acl {
596 my ($group, $usercfg) = @_;
597
598 foreach my $acl (keys %{$usercfg->{acl}}) {
599
600 delete ($usercfg->{acl}->{$acl}->{groups}->{$group})
601 if $usercfg->{acl}->{$acl}->{groups}->{$group};
602 }
603 }
604
605 sub delete_pool_acl {
606 my ($pool, $usercfg) = @_;
607
608 my $path = "/pool/$pool";
609
610 delete ($usercfg->{acl}->{$path})
611 }
612
613 # we automatically create some predefined roles by splitting privs
614 # into 3 groups (per category)
615 # root: only root is allowed to do that
616 # admin: an administrator can to that
617 # user: a normal user/customer can to that
618 my $privgroups = {
619 VM => {
620 root => [],
621 admin => [
622 'VM.Config.Disk',
623 'VM.Config.CPU',
624 'VM.Config.Memory',
625 'VM.Config.Network',
626 'VM.Config.HWType',
627 'VM.Config.Options', # covers all other things
628 'VM.Allocate',
629 'VM.Clone',
630 'VM.Migrate',
631 'VM.Monitor',
632 'VM.Snapshot',
633 'VM.Snapshot.Rollback',
634 ],
635 user => [
636 'VM.Config.CDROM', # change CDROM media
637 'VM.Console',
638 'VM.Backup',
639 'VM.PowerMgmt',
640 ],
641 audit => [
642 'VM.Audit',
643 ],
644 },
645 Sys => {
646 root => [
647 'Sys.PowerMgmt',
648 'Sys.Modify', # edit/change node settings
649 ],
650 admin => [
651 'Permissions.Modify',
652 'Sys.Console',
653 'Sys.Syslog',
654 ],
655 user => [],
656 audit => [
657 'Sys.Audit',
658 ],
659 },
660 Datastore => {
661 root => [],
662 admin => [
663 'Datastore.Allocate',
664 'Datastore.AllocateTemplate',
665 ],
666 user => [
667 'Datastore.AllocateSpace',
668 ],
669 audit => [
670 'Datastore.Audit',
671 ],
672 },
673 User => {
674 root => [
675 'Realm.Allocate',
676 ],
677 admin => [
678 'User.Modify',
679 'Group.Allocate', # edit/change group settings
680 'Realm.AllocateUser',
681 ],
682 user => [],
683 audit => [],
684 },
685 Pool => {
686 root => [],
687 admin => [
688 'Pool.Allocate', # create/delete pools
689 ],
690 user => [],
691 audit => [],
692 },
693 };
694
695 my $valid_privs = {};
696
697 my $special_roles = {
698 'NoAccess' => {}, # no privileges
699 'Administrator' => $valid_privs, # all privileges
700 };
701
702 sub create_roles {
703
704 foreach my $cat (keys %$privgroups) {
705 my $cd = $privgroups->{$cat};
706 foreach my $p (@{$cd->{root}}, @{$cd->{admin}},
707 @{$cd->{user}}, @{$cd->{audit}}) {
708 $valid_privs->{$p} = 1;
709 }
710 foreach my $p (@{$cd->{admin}}, @{$cd->{user}}, @{$cd->{audit}}) {
711
712 $special_roles->{"PVE${cat}Admin"}->{$p} = 1;
713 $special_roles->{"PVEAdmin"}->{$p} = 1;
714 }
715 if (scalar(@{$cd->{user}})) {
716 foreach my $p (@{$cd->{user}}, @{$cd->{audit}}) {
717 $special_roles->{"PVE${cat}User"}->{$p} = 1;
718 }
719 }
720 foreach my $p (@{$cd->{audit}}) {
721 $special_roles->{"PVEAuditor"}->{$p} = 1;
722 }
723 }
724
725 $special_roles->{"PVETemplateUser"} = { 'VM.Clone' => 1, 'VM.Audit' => 1 };
726 };
727
728 create_roles();
729
730 sub create_priv_properties {
731 my $properties = {};
732 foreach my $priv (keys %$valid_privs) {
733 $properties->{$priv} = {
734 type => 'boolean',
735 optional => 1,
736 };
737 }
738 return $properties;
739 }
740
741 sub role_is_special {
742 my ($role) = @_;
743 return (exists $special_roles->{$role}) ? 1 : 0;
744 }
745
746 sub add_role_privs {
747 my ($role, $usercfg, $privs) = @_;
748
749 return if !$privs;
750
751 die "role '$role' does not exist\n" if !$usercfg->{roles}->{$role};
752
753 foreach my $priv (split_list($privs)) {
754 if (defined ($valid_privs->{$priv})) {
755 $usercfg->{roles}->{$role}->{$priv} = 1;
756 } else {
757 die "invalid privilege '$priv'\n";
758 }
759 }
760 }
761
762 sub normalize_path {
763 my $path = shift;
764
765 $path =~ s|/+|/|g;
766
767 $path =~ s|/$||;
768
769 $path = '/' if !$path;
770
771 $path = "/$path" if $path !~ m|^/|;
772
773 return undef if $path !~ m|^[[:alnum:]\.\-\_\/]+$|;
774
775 return $path;
776 }
777
778 PVE::JSONSchema::register_format('pve-groupid', \&verify_groupname);
779 sub verify_groupname {
780 my ($groupname, $noerr) = @_;
781
782 if ($groupname !~ m/^[A-Za-z0-9\.\-_]+$/) {
783
784 die "group name '$groupname' contains invalid characters\n" if !$noerr;
785
786 return undef;
787 }
788
789 return $groupname;
790 }
791
792 PVE::JSONSchema::register_format('pve-roleid', \&verify_rolename);
793 sub verify_rolename {
794 my ($rolename, $noerr) = @_;
795
796 if ($rolename !~ m/^[A-Za-z0-9\.\-_]+$/) {
797
798 die "role name '$rolename' contains invalid characters\n" if !$noerr;
799
800 return undef;
801 }
802
803 return $rolename;
804 }
805
806 PVE::JSONSchema::register_format('pve-poolid', \&verify_poolname);
807 sub verify_poolname {
808 my ($poolname, $noerr) = @_;
809
810 if ($poolname !~ m/^[A-Za-z0-9\.\-_]+$/) {
811
812 die "pool name '$poolname' contains invalid characters\n" if !$noerr;
813
814 return undef;
815 }
816
817 return $poolname;
818 }
819
820 PVE::JSONSchema::register_format('pve-priv', \&verify_privname);
821 sub verify_privname {
822 my ($priv, $noerr) = @_;
823
824 if (!$valid_privs->{$priv}) {
825 die "invalid privilege '$priv'\n" if !$noerr;
826
827 return undef;
828 }
829
830 return $priv;
831 }
832
833 sub userconfig_force_defaults {
834 my ($cfg) = @_;
835
836 foreach my $r (keys %$special_roles) {
837 $cfg->{roles}->{$r} = $special_roles->{$r};
838 }
839
840 # add root user if not exists
841 if (!$cfg->{users}->{'root@pam'}) {
842 $cfg->{users}->{'root@pam'}->{enable} = 1;
843 }
844 }
845
846 sub parse_user_config {
847 my ($filename, $raw) = @_;
848
849 my $cfg = {};
850
851 userconfig_force_defaults($cfg);
852
853 $raw = '' if !defined($raw);
854 while ($raw =~ /^\s*(.+?)\s*$/gm) {
855 my $line = $1;
856 my @data;
857
858 foreach my $d (split (/:/, $line)) {
859 $d =~ s/^\s+//;
860 $d =~ s/\s+$//;
861 push @data, $d
862 }
863
864 my $et = shift @data;
865
866 if ($et eq 'user') {
867 my ($user, $enable, $expire, $firstname, $lastname, $email, $comment, $keys) = @data;
868
869 my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1);
870 if (!$realm) {
871 warn "user config - ignore user '$user' - invalid user name\n";
872 next;
873 }
874
875 $enable = $enable ? 1 : 0;
876
877 $expire = 0 if !$expire;
878
879 if ($expire !~ m/^\d+$/) {
880 warn "user config - ignore user '$user' - (illegal characters in expire '$expire')\n";
881 next;
882 }
883 $expire = int($expire);
884
885 #if (!verify_groupname ($group, 1)) {
886 # warn "user config - ignore user '$user' - invalid characters in group name\n";
887 # next;
888 #}
889
890 $cfg->{users}->{$user} = {
891 enable => $enable,
892 # group => $group,
893 };
894 $cfg->{users}->{$user}->{firstname} = PVE::Tools::decode_text($firstname) if $firstname;
895 $cfg->{users}->{$user}->{lastname} = PVE::Tools::decode_text($lastname) if $lastname;
896 $cfg->{users}->{$user}->{email} = $email;
897 $cfg->{users}->{$user}->{comment} = PVE::Tools::decode_text($comment) if $comment;
898 $cfg->{users}->{$user}->{expire} = $expire;
899 # keys: allowed yubico key ids or oath secrets (base32 encoded)
900 $cfg->{users}->{$user}->{keys} = $keys if $keys;
901
902 #$cfg->{users}->{$user}->{groups}->{$group} = 1;
903 #$cfg->{groups}->{$group}->{$user} = 1;
904
905 } elsif ($et eq 'group') {
906 my ($group, $userlist, $comment) = @data;
907
908 if (!verify_groupname($group, 1)) {
909 warn "user config - ignore group '$group' - invalid characters in group name\n";
910 next;
911 }
912
913 # make sure to add the group (even if there are no members)
914 $cfg->{groups}->{$group} = { users => {} } if !$cfg->{groups}->{$group};
915
916 $cfg->{groups}->{$group}->{comment} = PVE::Tools::decode_text($comment) if $comment;
917
918 foreach my $user (split_list($userlist)) {
919
920 if (!PVE::Auth::Plugin::verify_username($user, 1)) {
921 warn "user config - ignore invalid group member '$user'\n";
922 next;
923 }
924
925 if ($cfg->{users}->{$user}) { # user exists
926 $cfg->{users}->{$user}->{groups}->{$group} = 1;
927 $cfg->{groups}->{$group}->{users}->{$user} = 1;
928 } else {
929 warn "user config - ignore invalid group member '$user'\n";
930 }
931 }
932
933 } elsif ($et eq 'role') {
934 my ($role, $privlist) = @data;
935
936 if (!verify_rolename($role, 1)) {
937 warn "user config - ignore role '$role' - invalid characters in role name\n";
938 next;
939 }
940
941 # make sure to add the role (even if there are no privileges)
942 $cfg->{roles}->{$role} = {} if !$cfg->{roles}->{$role};
943
944 foreach my $priv (split_list($privlist)) {
945 if (defined ($valid_privs->{$priv})) {
946 $cfg->{roles}->{$role}->{$priv} = 1;
947 } else {
948 warn "user config - ignore invalid priviledge '$priv'\n";
949 }
950 }
951
952 } elsif ($et eq 'acl') {
953 my ($propagate, $pathtxt, $uglist, $rolelist) = @data;
954
955 if (my $path = normalize_path($pathtxt)) {
956 foreach my $role (split_list($rolelist)) {
957
958 if (!verify_rolename($role, 1)) {
959 warn "user config - ignore invalid role name '$role' in acl\n";
960 next;
961 }
962
963 foreach my $ug (split_list($uglist)) {
964 if ($ug =~ m/^@(\S+)$/) {
965 my $group = $1;
966 if ($cfg->{groups}->{$group}) { # group exists
967 $cfg->{acl}->{$path}->{groups}->{$group}->{$role} = $propagate;
968 } else {
969 warn "user config - ignore invalid acl group '$group'\n";
970 }
971 } elsif (PVE::Auth::Plugin::verify_username($ug, 1)) {
972 if ($cfg->{users}->{$ug}) { # user exists
973 $cfg->{acl}->{$path}->{users}->{$ug}->{$role} = $propagate;
974 } else {
975 warn "user config - ignore invalid acl member '$ug'\n";
976 }
977 } else {
978 warn "user config - invalid user/group '$ug' in acl\n";
979 }
980 }
981 }
982 } else {
983 warn "user config - ignore invalid path in acl '$pathtxt'\n";
984 }
985 } elsif ($et eq 'pool') {
986 my ($pool, $comment, $vmlist, $storelist) = @data;
987
988 if (!verify_poolname($pool, 1)) {
989 warn "user config - ignore pool '$pool' - invalid characters in pool name\n";
990 next;
991 }
992
993 # make sure to add the pool (even if there are no members)
994 $cfg->{pools}->{$pool} = { vms => {}, storage => {} } if !$cfg->{pools}->{$pool};
995
996 $cfg->{pools}->{$pool}->{comment} = PVE::Tools::decode_text($comment) if $comment;
997
998 foreach my $vmid (split_list($vmlist)) {
999 if ($vmid !~ m/^\d+$/) {
1000 warn "user config - ignore invalid vmid '$vmid' in pool '$pool'\n";
1001 next;
1002 }
1003 $vmid = int($vmid);
1004
1005 if ($cfg->{vms}->{$vmid}) {
1006 warn "user config - ignore duplicate vmid '$vmid' in pool '$pool'\n";
1007 next;
1008 }
1009
1010 $cfg->{pools}->{$pool}->{vms}->{$vmid} = 1;
1011
1012 # record vmid ==> pool relation
1013 $cfg->{vms}->{$vmid} = $pool;
1014 }
1015
1016 foreach my $storeid (split_list($storelist)) {
1017 if ($storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
1018 warn "user config - ignore invalid storage '$storeid' in pool '$pool'\n";
1019 next;
1020 }
1021 $cfg->{pools}->{$pool}->{storage}->{$storeid} = 1;
1022 }
1023 } else {
1024 warn "user config - ignore config line: $line\n";
1025 }
1026 }
1027
1028 userconfig_force_defaults($cfg);
1029
1030 return $cfg;
1031 }
1032
1033 sub write_user_config {
1034 my ($filename, $cfg) = @_;
1035
1036 my $data = '';
1037
1038 foreach my $user (keys %{$cfg->{users}}) {
1039 my $d = $cfg->{users}->{$user};
1040 my $firstname = $d->{firstname} ? PVE::Tools::encode_text($d->{firstname}) : '';
1041 my $lastname = $d->{lastname} ? PVE::Tools::encode_text($d->{lastname}) : '';
1042 my $email = $d->{email} || '';
1043 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
1044 my $expire = int($d->{expire} || 0);
1045 my $enable = $d->{enable} ? 1 : 0;
1046 my $keys = $d->{keys} ? $d->{keys} : '';
1047 $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:$keys:\n";
1048 }
1049
1050 $data .= "\n";
1051
1052 foreach my $group (keys %{$cfg->{groups}}) {
1053 my $d = $cfg->{groups}->{$group};
1054 my $list = join (',', keys %{$d->{users}});
1055 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
1056 $data .= "group:$group:$list:$comment:\n";
1057 }
1058
1059 $data .= "\n";
1060
1061 foreach my $pool (keys %{$cfg->{pools}}) {
1062 my $d = $cfg->{pools}->{$pool};
1063 my $vmlist = join (',', keys %{$d->{vms}});
1064 my $storelist = join (',', keys %{$d->{storage}});
1065 my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
1066 $data .= "pool:$pool:$comment:$vmlist:$storelist:\n";
1067 }
1068
1069 $data .= "\n";
1070
1071 foreach my $role (keys %{$cfg->{roles}}) {
1072 next if $special_roles->{$role};
1073
1074 my $d = $cfg->{roles}->{$role};
1075 my $list = join (',', keys %$d);
1076 $data .= "role:$role:$list:\n";
1077 }
1078
1079 $data .= "\n";
1080
1081 foreach my $path (sort keys %{$cfg->{acl}}) {
1082 my $d = $cfg->{acl}->{$path};
1083
1084 my $ra = {};
1085
1086 foreach my $group (keys %{$d->{groups}}) {
1087 my $l0 = '';
1088 my $l1 = '';
1089 foreach my $role (sort keys %{$d->{groups}->{$group}}) {
1090 my $propagate = $d->{groups}->{$group}->{$role};
1091 if ($propagate) {
1092 $l1 .= ',' if $l1;
1093 $l1 .= $role;
1094 } else {
1095 $l0 .= ',' if $l0;
1096 $l0 .= $role;
1097 }
1098 }
1099 $ra->{0}->{$l0}->{"\@$group"} = 1 if $l0;
1100 $ra->{1}->{$l1}->{"\@$group"} = 1 if $l1;
1101 }
1102
1103 foreach my $user (keys %{$d->{users}}) {
1104 # no need to save, because root is always 'Administrator'
1105 next if $user eq 'root@pam';
1106
1107 my $l0 = '';
1108 my $l1 = '';
1109 foreach my $role (sort keys %{$d->{users}->{$user}}) {
1110 my $propagate = $d->{users}->{$user}->{$role};
1111 if ($propagate) {
1112 $l1 .= ',' if $l1;
1113 $l1 .= $role;
1114 } else {
1115 $l0 .= ',' if $l0;
1116 $l0 .= $role;
1117 }
1118 }
1119 $ra->{0}->{$l0}->{$user} = 1 if $l0;
1120 $ra->{1}->{$l1}->{$user} = 1 if $l1;
1121 }
1122
1123 foreach my $rolelist (sort keys %{$ra->{0}}) {
1124 my $uglist = join (',', keys %{$ra->{0}->{$rolelist}});
1125 $data .= "acl:0:$path:$uglist:$rolelist:\n";
1126 }
1127 foreach my $rolelist (sort keys %{$ra->{1}}) {
1128 my $uglist = join (',', keys %{$ra->{1}->{$rolelist}});
1129 $data .= "acl:1:$path:$uglist:$rolelist:\n";
1130 }
1131 }
1132
1133 return $data;
1134 }
1135
1136 # The TFA configuration in priv/tfa.cfg format contains one line per user of
1137 # the form:
1138 # USER:TYPE:DATA
1139 # DATA is a base64 encoded json string and its format depends on the type.
1140 sub parse_priv_tfa_config {
1141 my ($filename, $raw) = @_;
1142
1143 my $users = {};
1144 my $cfg = { users => $users };
1145
1146 $raw = '' if !defined($raw);
1147 while ($raw =~ /^\s*(.+?)\s*$/gm) {
1148 my $line = $1;
1149 my ($user, $type, $data) = split(/:/, $line, 3);
1150
1151 my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1);
1152 if (!$realm) {
1153 warn "user tfa config - ignore user '$user' - invalid user name\n";
1154 next;
1155 }
1156
1157 $data = decode_json(decode_base64($data));
1158
1159 $users->{$user} = {
1160 type => $type,
1161 data => $data,
1162 };
1163 }
1164
1165 return $cfg;
1166 }
1167
1168 sub write_priv_tfa_config {
1169 my ($filename, $cfg) = @_;
1170
1171 my $output = '';
1172
1173 my $users = $cfg->{users};
1174 foreach my $user (sort keys %$users) {
1175 my $info = $users->{$user};
1176 next if !%$info; # skip empty entries
1177
1178 $info = {%$info}; # copy to verify contents:
1179
1180 my $type = delete $info->{type};
1181 my $data = delete $info->{data};
1182
1183 if (my @keys = keys %$info) {
1184 die "invalid keys in TFA config for user $user: " . join(', ', @keys) . "\n";
1185 }
1186
1187 $data = encode_base64(encode_json($data), '');
1188 $output .= "${user}:${type}:${data}\n";
1189 }
1190
1191 return $output;
1192 }
1193
1194 sub roles {
1195 my ($cfg, $user, $path) = @_;
1196
1197 # NOTE: we do not consider pools here.
1198 # You need to use $rpcenv->roles() instead if you want that.
1199
1200 return 'Administrator' if $user eq 'root@pam'; # root can do anything
1201
1202 my $perm = {};
1203
1204 foreach my $p (sort keys %{$cfg->{acl}}) {
1205 my $final = ($path eq $p);
1206
1207 next if !(($p eq '/') || $final || ($path =~ m|^$p/|));
1208
1209 my $acl = $cfg->{acl}->{$p};
1210
1211 #print "CHECKACL $path $p\n";
1212 #print "ACL $path = " . Dumper ($acl);
1213
1214 if (my $ri = $acl->{users}->{$user}) {
1215 my $new;
1216 foreach my $role (keys %$ri) {
1217 my $propagate = $ri->{$role};
1218 if ($final || $propagate) {
1219 #print "APPLY ROLE $p $user $role\n";
1220 $new = {} if !$new;
1221 $new->{$role} = 1;
1222 }
1223 }
1224 if ($new) {
1225 $perm = $new; # overwrite previous settings
1226 next; # user privs always override group privs
1227 }
1228 }
1229
1230 my $new;
1231 foreach my $g (keys %{$acl->{groups}}) {
1232 next if !$cfg->{groups}->{$g}->{users}->{$user};
1233 if (my $ri = $acl->{groups}->{$g}) {
1234 foreach my $role (keys %$ri) {
1235 my $propagate = $ri->{$role};
1236 if ($final || $propagate) {
1237 #print "APPLY ROLE $p \@$g $role\n";
1238 $new = {} if !$new;
1239 $new->{$role} = 1;
1240 }
1241 }
1242 }
1243 }
1244 if ($new) {
1245 $perm = $new; # overwrite previous settings
1246 next;
1247 }
1248 }
1249
1250 return ('NoAccess') if defined ($perm->{NoAccess});
1251 #return () if defined ($perm->{NoAccess});
1252
1253 #print "permission $user $path = " . Dumper ($perm);
1254
1255 my @ra = keys %$perm;
1256
1257 #print "roles $user $path = " . join (',', @ra) . "\n";
1258
1259 return @ra;
1260 }
1261
1262 sub permission {
1263 my ($cfg, $user, $path) = @_;
1264
1265 $user = PVE::Auth::Plugin::verify_username($user, 1);
1266 return {} if !$user;
1267
1268 my @ra = roles($cfg, $user, $path);
1269
1270 my $privs = {};
1271
1272 foreach my $role (@ra) {
1273 if (my $privset = $cfg->{roles}->{$role}) {
1274 foreach my $p (keys %$privset) {
1275 $privs->{$p} = 1;
1276 }
1277 }
1278 }
1279
1280 #print "priviledges $user $path = " . Dumper ($privs);
1281
1282 return $privs;
1283 }
1284
1285 sub check_permissions {
1286 my ($username, $path, $privlist) = @_;
1287
1288 $path = normalize_path($path);
1289 my $usercfg = cfs_read_file('user.cfg');
1290 my $perm = permission($usercfg, $username, $path);
1291
1292 foreach my $priv (split_list($privlist)) {
1293 return undef if !$perm->{$priv};
1294 };
1295
1296 return 1;
1297 }
1298
1299 sub remove_vm_access {
1300 my ($vmid) = @_;
1301 my $delVMaccessFn = sub {
1302 my $usercfg = cfs_read_file("user.cfg");
1303 my $modified;
1304
1305 if (my $acl = $usercfg->{acl}->{"/vms/$vmid"}) {
1306 delete $usercfg->{acl}->{"/vms/$vmid"};
1307 $modified = 1;
1308 }
1309 if (my $pool = $usercfg->{vms}->{$vmid}) {
1310 if (my $data = $usercfg->{pools}->{$pool}) {
1311 delete $data->{vms}->{$vmid};
1312 delete $usercfg->{vms}->{$vmid};
1313 $modified = 1;
1314 }
1315 }
1316 cfs_write_file("user.cfg", $usercfg) if $modified;
1317 };
1318
1319 lock_user_config($delVMaccessFn, "access permissions cleanup for VM $vmid failed");
1320 }
1321
1322 sub remove_storage_access {
1323 my ($storeid) = @_;
1324
1325 my $deleteStorageAccessFn = sub {
1326 my $usercfg = cfs_read_file("user.cfg");
1327 my $modified;
1328
1329 if (my $storage = $usercfg->{acl}->{"/storage/$storeid"}) {
1330 delete $usercfg->{acl}->{"/storage/$storeid"};
1331 $modified = 1;
1332 }
1333 foreach my $pool (keys %{$usercfg->{pools}}) {
1334 delete $usercfg->{pools}->{$pool}->{storage}->{$storeid};
1335 $modified = 1;
1336 }
1337 cfs_write_file("user.cfg", $usercfg) if $modified;
1338 };
1339
1340 lock_user_config($deleteStorageAccessFn,
1341 "access permissions cleanup for storage $storeid failed");
1342 }
1343
1344 sub add_vm_to_pool {
1345 my ($vmid, $pool) = @_;
1346
1347 my $addVMtoPoolFn = sub {
1348 my $usercfg = cfs_read_file("user.cfg");
1349 if (my $data = $usercfg->{pools}->{$pool}) {
1350 $data->{vms}->{$vmid} = 1;
1351 $usercfg->{vms}->{$vmid} = $pool;
1352 cfs_write_file("user.cfg", $usercfg);
1353 }
1354 };
1355
1356 lock_user_config($addVMtoPoolFn, "can't add VM $vmid to pool '$pool'");
1357 }
1358
1359 sub remove_vm_from_pool {
1360 my ($vmid) = @_;
1361
1362 my $delVMfromPoolFn = sub {
1363 my $usercfg = cfs_read_file("user.cfg");
1364 if (my $pool = $usercfg->{vms}->{$vmid}) {
1365 if (my $data = $usercfg->{pools}->{$pool}) {
1366 delete $data->{vms}->{$vmid};
1367 delete $usercfg->{vms}->{$vmid};
1368 cfs_write_file("user.cfg", $usercfg);
1369 }
1370 }
1371 };
1372
1373 lock_user_config($delVMfromPoolFn, "pool cleanup for VM $vmid failed");
1374 }
1375
1376 my $USER_CONTROLLED_TFA_TYPES = {
1377 u2f => 1,
1378 oath => 1,
1379 };
1380
1381 # Delete an entry by setting $data=undef in which case $type is ignored.
1382 # Otherwise both must be valid.
1383 sub user_set_tfa {
1384 my ($userid, $realm, $type, $data, $cached_usercfg, $cached_domaincfg) = @_;
1385
1386 if (defined($data) && !defined($type)) {
1387 # This is an internal usage error and should not happen
1388 die "cannot set tfa data without a type\n";
1389 }
1390
1391 my $user_cfg = $cached_usercfg || cfs_read_file('user.cfg');
1392 my $user = $user_cfg->{users}->{$userid}
1393 or die "user '$userid' not found\n";
1394
1395 my $domain_cfg = $cached_domaincfg || cfs_read_file('domains.cfg');
1396 my $realm_cfg = $domain_cfg->{ids}->{$realm};
1397 die "auth domain '$realm' does not exist\n" if !$realm_cfg;
1398
1399 my $realm_tfa = $realm_cfg->{tfa};
1400 if (defined($realm_tfa)) {
1401 $realm_tfa = PVE::Auth::Plugin::parse_tfa_config($realm_tfa);
1402 # If the realm has a TFA setting, we're only allowed to use that.
1403 if (defined($data)) {
1404 my $required_type = $realm_tfa->{type};
1405 if ($required_type ne $type) {
1406 die "realm '$realm' only allows TFA of type '$required_type\n";
1407 }
1408
1409 if (defined($data->{config})) {
1410 # XXX: Is it enough if the type matches? Or should the configuration also match?
1411 }
1412
1413 # realm-configured tfa always uses a simple key list, so use the user.cfg
1414 $user->{keys} = $data->{keys};
1415 } else {
1416 die "realm '$realm' does not allow removing the 2nd factor\n";
1417 }
1418 } else {
1419 # Without a realm-enforced TFA setting the user can add a u2f or totp entry by themselves.
1420 # The 'yubico' type requires yubico server settings, which have to be configured on the
1421 # realm, so this is not supported here:
1422 die "domain '$realm' does not support TFA type '$type'\n"
1423 if defined($data) && !$USER_CONTROLLED_TFA_TYPES->{$type};
1424 }
1425
1426 # Custom TFA entries are stored in priv/tfa.cfg as they can be more complet: u2f uses a
1427 # public key and a key handle, TOTP requires the usual totp settings...
1428
1429 my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
1430 my $tfa = ($tfa_cfg->{users}->{$userid} //= {});
1431
1432 if (defined($data)) {
1433 $tfa->{type} = $type;
1434 $tfa->{data} = $data;
1435 cfs_write_file('priv/tfa.cfg', $tfa_cfg);
1436
1437 $user->{keys} = "x!$type";
1438 } else {
1439 delete $tfa_cfg->{users}->{$userid};
1440 cfs_write_file('priv/tfa.cfg', $tfa_cfg);
1441
1442 delete $user->{keys};
1443 }
1444
1445 cfs_write_file('user.cfg', $user_cfg);
1446 }
1447
1448 sub user_get_tfa {
1449 my ($username, $realm) = @_;
1450
1451 my $user_cfg = cfs_read_file('user.cfg');
1452 my $user = $user_cfg->{users}->{$username}
1453 or die "user '$username' not found\n";
1454
1455 my $keys = $user->{keys};
1456 return if !$keys;
1457
1458 my $domain_cfg = cfs_read_file('domains.cfg');
1459 my $realm_cfg = $domain_cfg->{ids}->{$realm};
1460 die "auth domain '$realm' does not exist\n" if !$realm_cfg;
1461
1462 my $realm_tfa = $realm_cfg->{tfa};
1463 $realm_tfa = PVE::Auth::Plugin::parse_tfa_config($realm_tfa)
1464 if $realm_tfa;
1465
1466 # new style config starts with an 'x' and optionally contains a !<type> suffix
1467 if ($keys !~ /^x(?:!.*)?$/) {
1468 # old style config, find the type via the realm
1469 return if !$realm_tfa;
1470 return ($realm_tfa->{type}, {
1471 keys => $keys,
1472 config => $realm_tfa,
1473 });
1474 } else {
1475 my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
1476 my $tfa = $tfa_cfg->{users}->{$username};
1477 return if !$tfa; # should not happen (user.cfg wasn't cleaned up?)
1478
1479 if ($realm_tfa) {
1480 # if the realm has a tfa setting we need to verify the type:
1481 die "auth domain '$realm' and user have mismatching TFA settings\n"
1482 if $realm_tfa && $realm_tfa->{type} ne $tfa->{type};
1483 }
1484
1485 return ($tfa->{type}, $tfa->{data});
1486 }
1487 }
1488
1489 # bash completion helpers
1490
1491 register_standard_option('userid-completed',
1492 get_standard_option('userid', { completion => \&complete_username}),
1493 );
1494
1495 sub complete_username {
1496
1497 my $user_cfg = cfs_read_file('user.cfg');
1498
1499 return [ keys %{$user_cfg->{users}} ];
1500 }
1501
1502 sub complete_group {
1503
1504 my $user_cfg = cfs_read_file('user.cfg');
1505
1506 return [ keys %{$user_cfg->{groups}} ];
1507 }
1508
1509 sub complete_realm {
1510
1511 my $domain_cfg = cfs_read_file('domains.cfg');
1512
1513 return [ keys %{$domain_cfg->{ids}} ];
1514 }
1515
1516 1;