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