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