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