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