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