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