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