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