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