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