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