]>
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; |
2c3a6c0a DM |
9 | use MIME::Base64; |
10 | use Digest::SHA; | |
2c3a6c0a DM |
11 | use PVE::Tools qw(run_command lock_file file_get_contents split_list safe_print); |
12 | use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file); | |
13 | use PVE::JSONSchema; | |
5bb4e06a DM |
14 | |
15 | use PVE::Auth::Plugin; | |
16 | use PVE::Auth::AD; | |
17 | use PVE::Auth::LDAP; | |
18 | use PVE::Auth::PVE; | |
19 | use PVE::Auth::PAM; | |
2c3a6c0a DM |
20 | |
21 | use Data::Dumper; # fixme: remove | |
22 | ||
5bb4e06a DM |
23 | # load and initialize all plugins |
24 | ||
25 | PVE::Auth::AD->register(); | |
26 | PVE::Auth::LDAP->register(); | |
27 | PVE::Auth::PVE->register(); | |
28 | PVE::Auth::PAM->register(); | |
29 | PVE::Auth::Plugin->init(); | |
30 | ||
2c3a6c0a DM |
31 | # $authdir must be writable by root only! |
32 | my $confdir = "/etc/pve"; | |
33 | my $authdir = "$confdir/priv"; | |
34 | my $authprivkeyfn = "$authdir/authkey.key"; | |
35 | my $authpubkeyfn = "$confdir/authkey.pub"; | |
2c3a6c0a DM |
36 | my $pve_www_key_fn = "$confdir/pve-www.key"; |
37 | ||
38 | my $ticket_lifetime = 3600*2; # 2 hours | |
39 | ||
40 | Crypt::OpenSSL::RSA->import_random_seed(); | |
41 | ||
42 | cfs_register_file('user.cfg', | |
43 | \&parse_user_config, | |
44 | \&write_user_config); | |
45 | ||
2c3a6c0a | 46 | |
5bb4e06a DM |
47 | sub verify_username { |
48 | PVE::Auth::Plugin::verify_username(@_); | |
2c3a6c0a DM |
49 | } |
50 | ||
5bb4e06a DM |
51 | sub pve_verify_realm { |
52 | PVE::Auth::Plugin::pve_verify_realm(@_); | |
2c3a6c0a DM |
53 | } |
54 | ||
5bb4e06a | 55 | sub lock_user_config { |
2c3a6c0a DM |
56 | my ($code, $errmsg) = @_; |
57 | ||
5bb4e06a DM |
58 | cfs_lock_file("user.cfg", undef, $code); |
59 | if (my $err = $@) { | |
2c3a6c0a DM |
60 | $errmsg ? die "$errmsg: $err" : die $err; |
61 | } | |
62 | } | |
63 | ||
64 | my $pve_auth_pub_key; | |
65 | sub get_pubkey { | |
66 | ||
67 | return $pve_auth_pub_key if $pve_auth_pub_key; | |
68 | ||
69 | my $input = PVE::Tools::file_get_contents($authpubkeyfn); | |
70 | ||
71 | $pve_auth_pub_key = Crypt::OpenSSL::RSA->new_public_key($input); | |
72 | ||
73 | return $pve_auth_pub_key; | |
74 | } | |
75 | ||
76 | my $csrf_prevention_secret; | |
77 | my $get_csrfr_secret = sub { | |
78 | if (!$csrf_prevention_secret) { | |
79 | my $input = PVE::Tools::file_get_contents($pve_www_key_fn); | |
80 | $csrf_prevention_secret = Digest::SHA::sha1_base64($input); | |
81 | } | |
82 | return $csrf_prevention_secret; | |
83 | }; | |
84 | ||
85 | sub assemble_csrf_prevention_token { | |
86 | my ($username) = @_; | |
87 | ||
88 | my $timestamp = sprintf("%08X", time()); | |
89 | ||
90 | my $digest = Digest::SHA::sha1_base64("$timestamp:$username", &$get_csrfr_secret()); | |
91 | ||
92 | return "$timestamp:$digest"; | |
93 | } | |
94 | ||
95 | sub verify_csrf_prevention_token { | |
96 | my ($username, $token, $noerr) = @_; | |
97 | ||
98 | if ($token =~ m/^([A-Z0-9]{8}):(\S+)$/) { | |
99 | my $sig = $2; | |
100 | my $timestamp = $1; | |
101 | my $ttime = hex($timestamp); | |
102 | ||
103 | my $digest = Digest::SHA::sha1_base64("$timestamp:$username", &$get_csrfr_secret()); | |
104 | ||
105 | my $age = time() - $ttime; | |
106 | return if ($digest eq $sig) && ($age > -300) && ($age < $ticket_lifetime); | |
107 | } | |
108 | ||
109 | die "Permission denied - invalid csrf token\n" if !$noerr; | |
110 | ||
111 | return undef; | |
112 | } | |
113 | ||
114 | my $pve_auth_priv_key; | |
115 | sub get_privkey { | |
116 | ||
117 | return $pve_auth_priv_key if $pve_auth_priv_key; | |
118 | ||
119 | my $input = PVE::Tools::file_get_contents($authprivkeyfn); | |
120 | ||
121 | $pve_auth_priv_key = Crypt::OpenSSL::RSA->new_private_key($input); | |
122 | ||
123 | return $pve_auth_priv_key; | |
124 | } | |
125 | ||
126 | sub assemble_ticket { | |
127 | my ($username) = @_; | |
128 | ||
129 | my $rsa_priv = get_privkey(); | |
130 | ||
131 | my $timestamp = sprintf("%08X", time()); | |
132 | ||
133 | my $plain = "PVE:$username:$timestamp"; | |
134 | ||
135 | my $ticket = $plain . "::" . encode_base64($rsa_priv->sign($plain), ''); | |
136 | ||
137 | return $ticket; | |
138 | } | |
139 | ||
140 | sub verify_ticket { | |
141 | my ($ticket, $noerr) = @_; | |
142 | ||
adf8d771 | 143 | if ($ticket && $ticket =~ m/^(PVE:\S+)::([^:\s]+)$/) { |
2c3a6c0a DM |
144 | my $plain = $1; |
145 | my $sig = $2; | |
146 | ||
147 | my $rsa_pub = get_pubkey(); | |
148 | if ($rsa_pub->verify($plain, decode_base64($sig))) { | |
533219a1 | 149 | if ($plain =~ m/^PVE:(\S+):([A-Z0-9]{8})$/) { |
2c3a6c0a | 150 | my $username = $1; |
533219a1 | 151 | my $timestamp = $2; |
2c3a6c0a DM |
152 | my $ttime = hex($timestamp); |
153 | ||
154 | my $age = time() - $ttime; | |
155 | ||
5bb4e06a | 156 | if (PVE::Auth::Plugin::verify_username($username, 1) && |
533219a1 | 157 | ($age > -300) && ($age < $ticket_lifetime)) { |
2c3a6c0a DM |
158 | return wantarray ? ($username, $age) : $username; |
159 | } | |
160 | } | |
161 | } | |
162 | } | |
163 | ||
164 | die "permission denied - invalid ticket\n" if !$noerr; | |
165 | ||
166 | return undef; | |
167 | } | |
168 | ||
adf8d771 DM |
169 | # VNC tickets |
170 | # - they do not contain the username in plain text | |
171 | # - they are restricted to a specific resource path (example: '/vms/100') | |
172 | sub assemble_vnc_ticket { | |
173 | my ($username, $path) = @_; | |
174 | ||
175 | my $rsa_priv = get_privkey(); | |
176 | ||
177 | my $timestamp = sprintf("%08X", time()); | |
178 | ||
179 | my $plain = "PVEVNC:$timestamp"; | |
180 | ||
181 | $path = normalize_path($path); | |
182 | ||
183 | my $full = "$plain:$username:$path"; | |
184 | ||
185 | my $ticket = $plain . "::" . encode_base64($rsa_priv->sign($full), ''); | |
186 | ||
187 | return $ticket; | |
188 | } | |
189 | ||
190 | sub verify_vnc_ticket { | |
191 | my ($ticket, $username, $path, $noerr) = @_; | |
192 | ||
193 | if ($ticket && $ticket =~ m/^(PVEVNC:\S+)::([^:\s]+)$/) { | |
194 | my $plain = $1; | |
195 | my $sig = $2; | |
196 | my $full = "$plain:$username:$path"; | |
197 | ||
198 | my $rsa_pub = get_pubkey(); | |
199 | # Note: sign only match if $username and $path is correct | |
200 | if ($rsa_pub->verify($full, decode_base64($sig))) { | |
201 | if ($plain =~ m/^PVEVNC:([A-Z0-9]{8})$/) { | |
202 | my $ttime = hex($1); | |
203 | ||
204 | my $age = time() - $ttime; | |
205 | ||
206 | if (($age > -20) && ($age < 40)) { | |
207 | return 1; | |
208 | } | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
213 | die "permission denied - invalid vnc ticket\n" if !$noerr; | |
214 | ||
215 | return undef; | |
216 | } | |
217 | ||
23b35225 | 218 | sub assemble_spice_ticket { |
bf3e6d31 | 219 | my ($username, $vmid, $node) = @_; |
23b35225 AD |
220 | |
221 | my $rsa_priv = get_privkey(); | |
222 | ||
bf3e6d31 | 223 | my $timestamp = sprintf("%08x", time()); |
23b35225 | 224 | |
bf3e6d31 | 225 | my $randomstr = "PVESPICE:$timestamp:$vmid:$node:" . rand(10); |
23b35225 | 226 | |
bf3e6d31 DM |
227 | # this should be uses as one-time password |
228 | # max length is 60 chars (spice limit) | |
229 | # we pass this to qemu set_pasword and limit lifetime there | |
230 | # keep this secret | |
231 | my $ticket = Digest::SHA::sha1_hex($rsa_priv->sign($randomstr)); | |
23b35225 | 232 | |
bf3e6d31 DM |
233 | # Note: spice proxy connects with HTTP, so $proxyticket is exposed to public |
234 | # we use a signature/timestamp to make sure nobody can fake such ticket | |
235 | # an attacker can use this $proxyticket, but he will fail because $ticket is | |
236 | # private. | |
237 | # The proxy need to be able to extract/verify the ticket | |
238 | # Note: data needs to be lower case only, because virt-viewer needs that | |
3f62bdbe DM |
239 | # Note: RSA signature are too long (>=256 charaters) and makes problems with remote-viewer |
240 | ||
241 | my $secret = &$get_csrfr_secret(); | |
cb442f35 | 242 | my $plain = "pvespiceproxy:$timestamp:$vmid:" . lc($node); |
3f62bdbe DM |
243 | |
244 | # produces 40 characters | |
245 | my $sig = unpack("H*", Digest::SHA::sha1($plain, &$get_csrfr_secret())); | |
246 | ||
247 | #my $sig = unpack("H*", $rsa_priv->sign($plain)); # this produce too long strings (512) | |
23b35225 | 248 | |
bf3e6d31 | 249 | my $proxyticket = $plain . "::" . $sig; |
83d1f13e | 250 | |
bf3e6d31 DM |
251 | return ($ticket, $proxyticket); |
252 | } | |
253 | ||
254 | sub verify_spice_connect_url { | |
255 | my ($connect_str) = @_; | |
256 | ||
257 | # Note: we pass the spice ticket as 'host', so the | |
258 | # spice viewer connects with "$ticket:$port" | |
259 | ||
260 | return undef if !$connect_str; | |
261 | ||
3f62bdbe | 262 | if ($connect_str =~m/^pvespiceproxy:([a-z0-9]{8}):(\d+):(\S+)::([a-z0-9]{40}):(\d+)$/) { |
bf3e6d31 DM |
263 | my ($timestamp, $vmid, $node, $hexsig, $port) = ($1, $2, $3, $4, $5, $6); |
264 | my $ttime = hex($timestamp); | |
265 | my $age = time() - $ttime; | |
266 | ||
267 | # use very limited lifetime - is this enough? | |
268 | return undef if !(($age > -20) && ($age < 40)); | |
269 | ||
bf3e6d31 | 270 | my $plain = "pvespiceproxy:$timestamp:$vmid:$node"; |
3f62bdbe | 271 | my $sig = unpack("H*", Digest::SHA::sha1($plain, &$get_csrfr_secret())); |
bf3e6d31 | 272 | |
3f62bdbe | 273 | if ($sig eq $hexsig) { |
bf3e6d31 DM |
274 | return ($vmid, $node, $port); |
275 | } | |
276 | } | |
277 | ||
278 | return undef; | |
23b35225 AD |
279 | } |
280 | ||
cee5583b DM |
281 | sub read_x509_subject_spice { |
282 | my ($filename) = @_; | |
283 | ||
284 | # read x509 subject | |
285 | my $bio = Net::SSLeay::BIO_new_file($filename, 'r'); | |
286 | my $x509 = Net::SSLeay::PEM_read_bio_X509($bio); | |
287 | Net::SSLeay::BIO_free($bio); | |
288 | my $nameobj = Net::SSLeay::X509_get_subject_name($x509); | |
289 | my $subject = Net::SSLeay::X509_NAME_oneline($nameobj); | |
290 | Net::SSLeay::X509_free($x509); | |
291 | ||
292 | # remote-viewer wants comma as seperator (not '/') | |
293 | $subject =~ s!^/!!; | |
294 | $subject =~ s!/(\w+=)!,$1!g; | |
295 | ||
296 | return $subject; | |
297 | } | |
298 | ||
299 | # helper to generate SPICE remote-viewer configuration | |
300 | sub remote_viewer_config { | |
301 | my ($authuser, $vmid, $node, $proxy, $title, $port) = @_; | |
302 | ||
303 | if (!$proxy) { | |
304 | my $host = `hostname -f` || PVE::INotify::nodename(); | |
305 | chomp $host; | |
306 | $proxy = $host; | |
307 | } | |
308 | ||
309 | my ($ticket, $proxyticket) = assemble_spice_ticket($authuser, $vmid, $node); | |
310 | ||
311 | my $filename = "/etc/pve/local/pve-ssl.pem"; | |
312 | my $subject = read_x509_subject_spice($filename); | |
313 | ||
314 | my $cacert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192); | |
315 | $cacert =~ s/\n/\\n/g; | |
316 | ||
317 | my $config = { | |
318 | type => 'spice', | |
319 | title => $title, | |
320 | host => $proxyticket, # this break tls hostname verification, so we need to use 'host-subject' | |
321 | proxy => "http://$proxy:3128", | |
322 | 'tls-port' => $port, | |
323 | 'host-subject' => $subject, | |
324 | ca => $cacert, | |
325 | password => $ticket, | |
326 | 'delete-this-file' => 1, | |
327 | }; | |
328 | ||
329 | return ($ticket, $proxyticket, $config); | |
330 | } | |
331 | ||
37d45deb | 332 | sub check_user_exist { |
7070c1ae | 333 | my ($usercfg, $username, $noerr) = @_; |
2c3a6c0a | 334 | |
5bb4e06a | 335 | $username = PVE::Auth::Plugin::verify_username($username, $noerr); |
2c3a6c0a DM |
336 | return undef if !$username; |
337 | ||
37d45deb DM |
338 | return $usercfg->{users}->{$username} if $usercfg && $usercfg->{users}->{$username}; |
339 | ||
340 | die "no such user ('$username')\n" if !$noerr; | |
341 | ||
342 | return undef; | |
343 | } | |
344 | ||
345 | sub check_user_enabled { | |
346 | my ($usercfg, $username, $noerr) = @_; | |
347 | ||
348 | my $data = check_user_exist($usercfg, $username, $noerr); | |
349 | return undef if !$data; | |
350 | ||
351 | return 1 if $data->{enable}; | |
2c3a6c0a DM |
352 | |
353 | return 1 if $username eq 'root@pam'; # root is always enabled | |
354 | ||
37d45deb | 355 | die "user '$username' is disabled\n" if !$noerr; |
7070c1ae DM |
356 | |
357 | return undef; | |
2c3a6c0a DM |
358 | } |
359 | ||
360 | # password should be utf8 encoded | |
6126ab75 | 361 | # Note: some pluging delay/sleep if auth fails |
2c3a6c0a DM |
362 | sub authenticate_user { |
363 | my ($username, $password) = @_; | |
364 | ||
365 | die "no username specified\n" if !$username; | |
366 | ||
5bb4e06a | 367 | my ($ruid, $realm); |
2c3a6c0a | 368 | |
5bb4e06a | 369 | ($username, $ruid, $realm) = PVE::Auth::Plugin::verify_username($username); |
2c3a6c0a DM |
370 | |
371 | my $usercfg = cfs_read_file('user.cfg'); | |
372 | ||
6126ab75 | 373 | check_user_enabled($usercfg, $username); |
2c3a6c0a DM |
374 | |
375 | my $ctime = time(); | |
376 | my $expire = $usercfg->{users}->{$username}->{expire}; | |
377 | ||
6126ab75 | 378 | die "account expired\n" if $expire && ($expire < $ctime); |
2c3a6c0a | 379 | |
5bb4e06a | 380 | my $domain_cfg = cfs_read_file('domains.cfg'); |
2c3a6c0a | 381 | |
6126ab75 DM |
382 | my $cfg = $domain_cfg->{ids}->{$realm}; |
383 | die "auth domain '$realm' does not exists\n" if !$cfg; | |
384 | my $plugin = PVE::Auth::Plugin->lookup($cfg->{type}); | |
385 | $plugin->authenticate_user($cfg, $realm, $ruid, $password); | |
2c3a6c0a | 386 | |
5bb4e06a | 387 | return $username; |
2c3a6c0a DM |
388 | } |
389 | ||
390 | sub domain_set_password { | |
5bb4e06a | 391 | my ($realm, $username, $password) = @_; |
2c3a6c0a DM |
392 | |
393 | die "no auth domain specified" if !$realm; | |
394 | ||
5bb4e06a DM |
395 | my $domain_cfg = cfs_read_file('domains.cfg'); |
396 | ||
397 | my $cfg = $domain_cfg->{ids}->{$realm}; | |
398 | die "auth domain '$realm' does not exists\n" if !$cfg; | |
399 | my $plugin = PVE::Auth::Plugin->lookup($cfg->{type}); | |
400 | $plugin->store_password($cfg, $realm, $username, $password); | |
2c3a6c0a DM |
401 | } |
402 | ||
403 | sub add_user_group { | |
404 | ||
405 | my ($username, $usercfg, $group) = @_; | |
406 | $usercfg->{users}->{$username}->{groups}->{$group} = 1; | |
407 | $usercfg->{groups}->{$group}->{users}->{$username} = 1; | |
408 | } | |
409 | ||
410 | sub delete_user_group { | |
411 | ||
412 | my ($username, $usercfg) = @_; | |
413 | ||
414 | foreach my $group (keys %{$usercfg->{groups}}) { | |
415 | ||
416 | delete ($usercfg->{groups}->{$group}->{users}->{$username}) | |
417 | if $usercfg->{groups}->{$group}->{users}->{$username}; | |
418 | } | |
419 | } | |
420 | ||
421 | sub delete_user_acl { | |
422 | ||
423 | my ($username, $usercfg) = @_; | |
424 | ||
425 | foreach my $acl (keys %{$usercfg->{acl}}) { | |
426 | ||
427 | delete ($usercfg->{acl}->{$acl}->{users}->{$username}) | |
428 | if $usercfg->{acl}->{$acl}->{users}->{$username}; | |
429 | } | |
2c3a6c0a | 430 | } |
39c85db8 | 431 | |
2c3a6c0a DM |
432 | sub delete_group_acl { |
433 | ||
434 | my ($group, $usercfg) = @_; | |
435 | ||
436 | foreach my $acl (keys %{$usercfg->{acl}}) { | |
437 | ||
438 | delete ($usercfg->{acl}->{$acl}->{groups}->{$group}) | |
439 | if $usercfg->{acl}->{$acl}->{groups}->{$group}; | |
440 | } | |
39c85db8 DM |
441 | } |
442 | ||
443 | sub delete_pool_acl { | |
444 | ||
445 | my ($pool, $usercfg) = @_; | |
2c3a6c0a | 446 | |
39c85db8 DM |
447 | my $path = "/pool/$pool"; |
448 | ||
449 | foreach my $aclpath (keys %{$usercfg->{acl}}) { | |
450 | delete ($usercfg->{acl}->{$aclpath}) | |
451 | if $usercfg->{acl}->{$aclpath} eq 'path'; | |
452 | } | |
2c3a6c0a DM |
453 | } |
454 | ||
455 | # we automatically create some predefined roles by splitting privs | |
456 | # into 3 groups (per category) | |
457 | # root: only root is allowed to do that | |
458 | # admin: an administrator can to that | |
459 | # user: a normak user/customer can to that | |
460 | my $privgroups = { | |
461 | VM => { | |
462 | root => [], | |
463 | admin => [ | |
c0fead8c | 464 | 'VM.Config.Disk', |
c0fead8c DM |
465 | 'VM.Config.CPU', |
466 | 'VM.Config.Memory', | |
467 | 'VM.Config.Network', | |
468 | 'VM.Config.HWType', | |
469 | 'VM.Config.Options', # covers all other things | |
2c3a6c0a | 470 | 'VM.Allocate', |
7b395f99 | 471 | 'VM.Clone', |
2c3a6c0a | 472 | 'VM.Migrate', |
c0fead8c | 473 | 'VM.Monitor', |
e3e6510c | 474 | 'VM.Snapshot', |
2c3a6c0a DM |
475 | ], |
476 | user => [ | |
cc7bdf33 | 477 | 'VM.Config.CDROM', # change CDROM media |
2c3a6c0a | 478 | 'VM.Console', |
68d5a86d | 479 | 'VM.Backup', |
2c3a6c0a DM |
480 | 'VM.PowerMgmt', |
481 | ], | |
482 | audit => [ | |
82b63965 | 483 | 'VM.Audit', |
2c3a6c0a DM |
484 | ], |
485 | }, | |
486 | Sys => { | |
487 | root => [ | |
488 | 'Sys.PowerMgmt', | |
37d45deb | 489 | 'Sys.Modify', # edit/change node settings |
2c3a6c0a DM |
490 | ], |
491 | admin => [ | |
2e376c58 | 492 | 'Permissions.Modify', |
2c3a6c0a DM |
493 | 'Sys.Console', |
494 | 'Sys.Syslog', | |
495 | ], | |
496 | user => [], | |
497 | audit => [ | |
498 | 'Sys.Audit', | |
499 | ], | |
500 | }, | |
501 | Datastore => { | |
2e376c58 | 502 | root => [], |
19f60b5e DM |
503 | admin => [ |
504 | 'Datastore.Allocate', | |
373cb383 | 505 | 'Datastore.AllocateTemplate', |
19f60b5e | 506 | ], |
2c3a6c0a DM |
507 | user => [ |
508 | 'Datastore.AllocateSpace', | |
509 | ], | |
510 | audit => [ | |
511 | 'Datastore.Audit', | |
512 | ], | |
513 | }, | |
12683df7 | 514 | User => { |
82b63965 DM |
515 | root => [ |
516 | 'Realm.Allocate', | |
517 | ], | |
12683df7 DM |
518 | admin => [ |
519 | 'User.Modify', | |
82b63965 DM |
520 | 'Group.Allocate', # edit/change group settings |
521 | 'Realm.AllocateUser', | |
19f60b5e | 522 | ], |
12683df7 DM |
523 | user => [], |
524 | audit => [], | |
525 | }, | |
dee1c882 DM |
526 | Pool => { |
527 | root => [], | |
528 | admin => [ | |
529 | 'Pool.Allocate', # create/delete pools | |
530 | ], | |
531 | user => [], | |
532 | audit => [], | |
533 | }, | |
2c3a6c0a DM |
534 | }; |
535 | ||
536 | my $valid_privs = {}; | |
537 | ||
538 | my $special_roles = { | |
539 | 'NoAccess' => {}, # no priviledges | |
540 | 'Administrator' => $valid_privs, # all priviledges | |
541 | }; | |
542 | ||
543 | sub create_roles { | |
544 | ||
545 | foreach my $cat (keys %$privgroups) { | |
546 | my $cd = $privgroups->{$cat}; | |
547 | foreach my $p (@{$cd->{root}}, @{$cd->{admin}}, | |
548 | @{$cd->{user}}, @{$cd->{audit}}) { | |
549 | $valid_privs->{$p} = 1; | |
550 | } | |
551 | foreach my $p (@{$cd->{admin}}, @{$cd->{user}}, @{$cd->{audit}}) { | |
552 | ||
553 | $special_roles->{"PVE${cat}Admin"}->{$p} = 1; | |
554 | $special_roles->{"PVEAdmin"}->{$p} = 1; | |
555 | } | |
556 | if (scalar(@{$cd->{user}})) { | |
557 | foreach my $p (@{$cd->{user}}, @{$cd->{audit}}) { | |
558 | $special_roles->{"PVE${cat}User"}->{$p} = 1; | |
559 | } | |
560 | } | |
561 | foreach my $p (@{$cd->{audit}}) { | |
562 | $special_roles->{"PVEAuditor"}->{$p} = 1; | |
563 | } | |
564 | } | |
ff4b2235 | 565 | |
7b395f99 | 566 | $special_roles->{"PVETemplateUser"} = { 'VM.Clone' => 1, 'VM.Audit' => 1 }; |
2c3a6c0a DM |
567 | }; |
568 | ||
569 | create_roles(); | |
570 | ||
2c3a6c0a DM |
571 | sub add_role_privs { |
572 | my ($role, $usercfg, $privs) = @_; | |
573 | ||
574 | return if !$privs; | |
575 | ||
576 | die "role '$role' does not exist\n" if !$usercfg->{roles}->{$role}; | |
577 | ||
578 | foreach my $priv (split_list($privs)) { | |
579 | if (defined ($valid_privs->{$priv})) { | |
580 | $usercfg->{roles}->{$role}->{$priv} = 1; | |
581 | } else { | |
582 | die "invalid priviledge '$priv'\n"; | |
583 | } | |
584 | } | |
585 | } | |
586 | ||
587 | sub normalize_path { | |
588 | my $path = shift; | |
589 | ||
4bc17477 | 590 | $path =~ s|/+|/|g; |
2c3a6c0a DM |
591 | |
592 | $path =~ s|/$||; | |
593 | ||
594 | $path = '/' if !$path; | |
595 | ||
4bc17477 DM |
596 | $path = "/$path" if $path !~ m|^/|; |
597 | ||
e4f8fc2e | 598 | return undef if $path !~ m|^[[:alnum:]\.\-\_\/]+$|; |
2c3a6c0a DM |
599 | |
600 | return $path; | |
601 | } | |
602 | ||
2c3a6c0a DM |
603 | |
604 | PVE::JSONSchema::register_format('pve-groupid', \&verify_groupname); | |
605 | sub verify_groupname { | |
606 | my ($groupname, $noerr) = @_; | |
607 | ||
608 | if ($groupname !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
609 | ||
610 | die "group name '$groupname' contains invalid characters\n" if !$noerr; | |
611 | ||
612 | return undef; | |
613 | } | |
614 | ||
615 | return $groupname; | |
616 | } | |
617 | ||
618 | PVE::JSONSchema::register_format('pve-roleid', \&verify_rolename); | |
619 | sub verify_rolename { | |
620 | my ($rolename, $noerr) = @_; | |
621 | ||
622 | if ($rolename !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
623 | ||
624 | die "role name '$rolename' contains invalid characters\n" if !$noerr; | |
625 | ||
626 | return undef; | |
627 | } | |
628 | ||
629 | return $rolename; | |
630 | } | |
631 | ||
39c85db8 DM |
632 | PVE::JSONSchema::register_format('pve-poolid', \&verify_groupname); |
633 | sub verify_poolname { | |
634 | my ($poolname, $noerr) = @_; | |
635 | ||
636 | if ($poolname !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
637 | ||
638 | die "pool name '$poolname' contains invalid characters\n" if !$noerr; | |
639 | ||
640 | return undef; | |
641 | } | |
642 | ||
643 | return $poolname; | |
644 | } | |
645 | ||
2c3a6c0a DM |
646 | PVE::JSONSchema::register_format('pve-priv', \&verify_privname); |
647 | sub verify_privname { | |
648 | my ($priv, $noerr) = @_; | |
649 | ||
650 | if (!$valid_privs->{$priv}) { | |
651 | die "invalid priviledge '$priv'\n" if !$noerr; | |
652 | ||
653 | return undef; | |
654 | } | |
655 | ||
656 | return $priv; | |
657 | } | |
658 | ||
659 | sub userconfig_force_defaults { | |
660 | my ($cfg) = @_; | |
661 | ||
662 | foreach my $r (keys %$special_roles) { | |
663 | $cfg->{roles}->{$r} = $special_roles->{$r}; | |
664 | } | |
665 | ||
666 | # fixme: remove 'root' group (not required)? | |
667 | ||
668 | # add root user | |
669 | $cfg->{users}->{'root@pam'}->{enable} = 1; | |
670 | } | |
671 | ||
672 | sub parse_user_config { | |
673 | my ($filename, $raw) = @_; | |
674 | ||
675 | my $cfg = {}; | |
676 | ||
677 | userconfig_force_defaults($cfg); | |
678 | ||
679 | while ($raw && $raw =~ s/^(.*?)(\n|$)//) { | |
680 | my $line = $1; | |
681 | ||
682 | next if $line =~ m/^\s*$/; # skip empty lines | |
683 | ||
684 | my @data; | |
685 | ||
686 | foreach my $d (split (/:/, $line)) { | |
687 | $d =~ s/^\s+//; | |
688 | $d =~ s/\s+$//; | |
689 | push @data, $d | |
690 | } | |
691 | ||
692 | my $et = shift @data; | |
693 | ||
694 | if ($et eq 'user') { | |
695 | my ($user, $enable, $expire, $firstname, $lastname, $email, $comment) = @data; | |
696 | ||
5bb4e06a | 697 | my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1); |
2c3a6c0a DM |
698 | if (!$realm) { |
699 | warn "user config - ignore user '$user' - invalid user name\n"; | |
700 | next; | |
701 | } | |
702 | ||
703 | $enable = $enable ? 1 : 0; | |
704 | ||
705 | $expire = 0 if !$expire; | |
706 | ||
707 | if ($expire !~ m/^\d+$/) { | |
708 | warn "user config - ignore user '$user' - (illegal characters in expire '$expire')\n"; | |
709 | next; | |
710 | } | |
711 | $expire = int($expire); | |
712 | ||
713 | #if (!verify_groupname ($group, 1)) { | |
714 | # warn "user config - ignore user '$user' - invalid characters in group name\n"; | |
715 | # next; | |
716 | #} | |
717 | ||
718 | $cfg->{users}->{$user} = { | |
719 | enable => $enable, | |
720 | # group => $group, | |
721 | }; | |
722 | $cfg->{users}->{$user}->{firstname} = PVE::Tools::decode_text($firstname) if $firstname; | |
723 | $cfg->{users}->{$user}->{lastname} = PVE::Tools::decode_text($lastname) if $lastname; | |
724 | $cfg->{users}->{$user}->{email} = $email; | |
725 | $cfg->{users}->{$user}->{comment} = PVE::Tools::decode_text($comment) if $comment; | |
726 | $cfg->{users}->{$user}->{expire} = $expire; | |
727 | ||
728 | #$cfg->{users}->{$user}->{groups}->{$group} = 1; | |
729 | #$cfg->{groups}->{$group}->{$user} = 1; | |
730 | ||
731 | } elsif ($et eq 'group') { | |
732 | my ($group, $userlist, $comment) = @data; | |
733 | ||
734 | if (!verify_groupname($group, 1)) { | |
735 | warn "user config - ignore group '$group' - invalid characters in group name\n"; | |
736 | next; | |
737 | } | |
738 | ||
739 | # make sure to add the group (even if there are no members) | |
740 | $cfg->{groups}->{$group} = { users => {} } if !$cfg->{groups}->{$group}; | |
741 | ||
742 | $cfg->{groups}->{$group}->{comment} = PVE::Tools::decode_text($comment) if $comment; | |
743 | ||
744 | foreach my $user (split_list($userlist)) { | |
745 | ||
5bb4e06a | 746 | if (!PVE::Auth::Plugin::verify_username($user, 1)) { |
2c3a6c0a DM |
747 | warn "user config - ignore invalid group member '$user'\n"; |
748 | next; | |
749 | } | |
750 | ||
751 | if ($cfg->{users}->{$user}) { # user exists | |
752 | $cfg->{users}->{$user}->{groups}->{$group} = 1; | |
753 | $cfg->{groups}->{$group}->{users}->{$user} = 1; | |
754 | } else { | |
755 | warn "user config - ignore invalid group member '$user'\n"; | |
756 | } | |
757 | } | |
758 | ||
759 | } elsif ($et eq 'role') { | |
760 | my ($role, $privlist) = @data; | |
761 | ||
762 | if (!verify_rolename($role, 1)) { | |
763 | warn "user config - ignore role '$role' - invalid characters in role name\n"; | |
764 | next; | |
765 | } | |
766 | ||
767 | # make sure to add the role (even if there are no privileges) | |
768 | $cfg->{roles}->{$role} = {} if !$cfg->{roles}->{$role}; | |
769 | ||
770 | foreach my $priv (split_list($privlist)) { | |
771 | if (defined ($valid_privs->{$priv})) { | |
772 | $cfg->{roles}->{$role}->{$priv} = 1; | |
773 | } else { | |
774 | warn "user config - ignore invalid priviledge '$priv'\n"; | |
775 | } | |
776 | } | |
777 | ||
778 | } elsif ($et eq 'acl') { | |
779 | my ($propagate, $pathtxt, $uglist, $rolelist) = @data; | |
780 | ||
781 | if (my $path = normalize_path($pathtxt)) { | |
782 | foreach my $role (split_list($rolelist)) { | |
783 | ||
784 | if (!verify_rolename($role, 1)) { | |
785 | warn "user config - ignore invalid role name '$role' in acl\n"; | |
786 | next; | |
787 | } | |
788 | ||
789 | foreach my $ug (split_list($uglist)) { | |
9b217226 | 790 | if ($ug =~ m/^@(\S+)$/) { |
2c3a6c0a DM |
791 | my $group = $1; |
792 | if ($cfg->{groups}->{$group}) { # group exists | |
793 | $cfg->{acl}->{$path}->{groups}->{$group}->{$role} = $propagate; | |
794 | } else { | |
795 | warn "user config - ignore invalid acl group '$group'\n"; | |
796 | } | |
5bb4e06a | 797 | } elsif (PVE::Auth::Plugin::verify_username($ug, 1)) { |
2c3a6c0a DM |
798 | if ($cfg->{users}->{$ug}) { # user exists |
799 | $cfg->{acl}->{$path}->{users}->{$ug}->{$role} = $propagate; | |
800 | } else { | |
801 | warn "user config - ignore invalid acl member '$ug'\n"; | |
802 | } | |
803 | } else { | |
804 | warn "user config - invalid user/group '$ug' in acl\n"; | |
805 | } | |
806 | } | |
807 | } | |
808 | } else { | |
809 | warn "user config - ignore invalid path in acl '$pathtxt'\n"; | |
810 | } | |
4bc17477 | 811 | } elsif ($et eq 'pool') { |
39c85db8 | 812 | my ($pool, $comment, $vmlist, $storelist) = @data; |
4bc17477 | 813 | |
39c85db8 DM |
814 | if (!verify_poolname($pool, 1)) { |
815 | warn "user config - ignore pool '$pool' - invalid characters in pool name\n"; | |
816 | next; | |
817 | } | |
4bc17477 | 818 | |
39c85db8 DM |
819 | # make sure to add the pool (even if there are no members) |
820 | $cfg->{pools}->{$pool} = { vms => {}, storage => {} } if !$cfg->{pools}->{$pool}; | |
4bc17477 | 821 | |
39c85db8 | 822 | $cfg->{pools}->{$pool}->{comment} = PVE::Tools::decode_text($comment) if $comment; |
4bc17477 | 823 | |
39c85db8 DM |
824 | foreach my $vmid (split_list($vmlist)) { |
825 | if ($vmid !~ m/^\d+$/) { | |
826 | warn "user config - ignore invalid vmid '$vmid' in pool '$pool'\n"; | |
827 | next; | |
4bc17477 | 828 | } |
39c85db8 | 829 | $vmid = int($vmid); |
4bc17477 | 830 | |
39c85db8 DM |
831 | if ($cfg->{vms}->{$vmid}) { |
832 | warn "user config - ignore duplicate vmid '$vmid' in pool '$pool'\n"; | |
833 | next; | |
4bc17477 DM |
834 | } |
835 | ||
39c85db8 DM |
836 | $cfg->{pools}->{$pool}->{vms}->{$vmid} = 1; |
837 | ||
838 | # record vmid ==> pool relation | |
839 | $cfg->{vms}->{$vmid} = $pool; | |
840 | } | |
841 | ||
842 | foreach my $storeid (split_list($storelist)) { | |
843 | if ($storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) { | |
844 | warn "user config - ignore invalid storage '$storeid' in pool '$pool'\n"; | |
845 | next; | |
846 | } | |
847 | $cfg->{pools}->{$pool}->{storage}->{$storeid} = 1; | |
4bc17477 | 848 | } |
2c3a6c0a DM |
849 | } else { |
850 | warn "user config - ignore config line: $line\n"; | |
851 | } | |
852 | } | |
853 | ||
854 | userconfig_force_defaults($cfg); | |
855 | ||
856 | return $cfg; | |
857 | } | |
858 | ||
2c3a6c0a DM |
859 | sub write_user_config { |
860 | my ($filename, $cfg) = @_; | |
861 | ||
862 | my $data = ''; | |
863 | ||
864 | foreach my $user (keys %{$cfg->{users}}) { | |
2c3a6c0a DM |
865 | my $d = $cfg->{users}->{$user}; |
866 | my $firstname = $d->{firstname} ? PVE::Tools::encode_text($d->{firstname}) : ''; | |
867 | my $lastname = $d->{lastname} ? PVE::Tools::encode_text($d->{lastname}) : ''; | |
868 | my $email = $d->{email} || ''; | |
869 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
5eabc984 | 870 | my $expire = int($d->{expire} || 0); |
2c3a6c0a DM |
871 | my $enable = $d->{enable} ? 1 : 0; |
872 | $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:\n"; | |
873 | } | |
874 | ||
875 | $data .= "\n"; | |
876 | ||
877 | foreach my $group (keys %{$cfg->{groups}}) { | |
878 | my $d = $cfg->{groups}->{$group}; | |
879 | my $list = join (',', keys %{$d->{users}}); | |
880 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
881 | $data .= "group:$group:$list:$comment:\n"; | |
882 | } | |
883 | ||
884 | $data .= "\n"; | |
885 | ||
39c85db8 DM |
886 | foreach my $pool (keys %{$cfg->{pools}}) { |
887 | my $d = $cfg->{pools}->{$pool}; | |
4bc17477 DM |
888 | my $vmlist = join (',', keys %{$d->{vms}}); |
889 | my $storelist = join (',', keys %{$d->{storage}}); | |
890 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
39c85db8 | 891 | $data .= "pool:$pool:$comment:$vmlist:$storelist:\n"; |
4bc17477 DM |
892 | } |
893 | ||
894 | $data .= "\n"; | |
895 | ||
2c3a6c0a DM |
896 | foreach my $role (keys %{$cfg->{roles}}) { |
897 | next if $special_roles->{$role}; | |
898 | ||
899 | my $d = $cfg->{roles}->{$role}; | |
900 | my $list = join (',', keys %$d); | |
901 | $data .= "role:$role:$list:\n"; | |
902 | } | |
903 | ||
904 | $data .= "\n"; | |
905 | ||
906 | foreach my $path (sort keys %{$cfg->{acl}}) { | |
907 | my $d = $cfg->{acl}->{$path}; | |
908 | ||
909 | my $ra = {}; | |
910 | ||
911 | foreach my $group (keys %{$d->{groups}}) { | |
912 | my $l0 = ''; | |
913 | my $l1 = ''; | |
914 | foreach my $role (sort keys %{$d->{groups}->{$group}}) { | |
915 | my $propagate = $d->{groups}->{$group}->{$role}; | |
916 | if ($propagate) { | |
917 | $l1 .= ',' if $l1; | |
918 | $l1 .= $role; | |
919 | } else { | |
920 | $l0 .= ',' if $l0; | |
921 | $l0 .= $role; | |
922 | } | |
923 | } | |
924 | $ra->{0}->{$l0}->{"\@$group"} = 1 if $l0; | |
925 | $ra->{1}->{$l1}->{"\@$group"} = 1 if $l1; | |
926 | } | |
927 | ||
928 | foreach my $user (keys %{$d->{users}}) { | |
929 | # no need to save, because root is always 'Administartor' | |
930 | next if $user eq 'root@pam'; | |
931 | ||
932 | my $l0 = ''; | |
933 | my $l1 = ''; | |
934 | foreach my $role (sort keys %{$d->{users}->{$user}}) { | |
935 | my $propagate = $d->{users}->{$user}->{$role}; | |
936 | if ($propagate) { | |
937 | $l1 .= ',' if $l1; | |
938 | $l1 .= $role; | |
939 | } else { | |
940 | $l0 .= ',' if $l0; | |
941 | $l0 .= $role; | |
942 | } | |
943 | } | |
944 | $ra->{0}->{$l0}->{$user} = 1 if $l0; | |
945 | $ra->{1}->{$l1}->{$user} = 1 if $l1; | |
946 | } | |
947 | ||
948 | foreach my $rolelist (sort keys %{$ra->{0}}) { | |
949 | my $uglist = join (',', keys %{$ra->{0}->{$rolelist}}); | |
950 | $data .= "acl:0:$path:$uglist:$rolelist:\n"; | |
951 | } | |
952 | foreach my $rolelist (sort keys %{$ra->{1}}) { | |
953 | my $uglist = join (',', keys %{$ra->{1}->{$rolelist}}); | |
954 | $data .= "acl:1:$path:$uglist:$rolelist:\n"; | |
955 | } | |
956 | } | |
957 | ||
958 | return $data; | |
959 | } | |
960 | ||
961 | sub roles { | |
962 | my ($cfg, $user, $path) = @_; | |
963 | ||
4bc17477 DM |
964 | # NOTE: we do not consider pools here. |
965 | # You need to use $rpcenv->roles() instead if you want that. | |
966 | ||
2c3a6c0a DM |
967 | return 'Administrator' if $user eq 'root@pam'; # root can do anything |
968 | ||
969 | my $perm = {}; | |
970 | ||
971 | foreach my $p (sort keys %{$cfg->{acl}}) { | |
972 | my $final = ($path eq $p); | |
973 | ||
974 | next if !(($p eq '/') || $final || ($path =~ m|^$p/|)); | |
975 | ||
976 | my $acl = $cfg->{acl}->{$p}; | |
977 | ||
978 | #print "CHECKACL $path $p\n"; | |
979 | #print "ACL $path = " . Dumper ($acl); | |
980 | ||
981 | if (my $ri = $acl->{users}->{$user}) { | |
982 | my $new; | |
983 | foreach my $role (keys %$ri) { | |
984 | my $propagate = $ri->{$role}; | |
985 | if ($final || $propagate) { | |
986 | #print "APPLY ROLE $p $user $role\n"; | |
987 | $new = {} if !$new; | |
988 | $new->{$role} = 1; | |
989 | } | |
990 | } | |
991 | if ($new) { | |
992 | $perm = $new; # overwrite previous settings | |
993 | next; # user privs always override group privs | |
994 | } | |
995 | } | |
996 | ||
997 | my $new; | |
998 | foreach my $g (keys %{$acl->{groups}}) { | |
999 | next if !$cfg->{groups}->{$g}->{users}->{$user}; | |
1000 | if (my $ri = $acl->{groups}->{$g}) { | |
1001 | foreach my $role (keys %$ri) { | |
1002 | my $propagate = $ri->{$role}; | |
1003 | if ($final || $propagate) { | |
1004 | #print "APPLY ROLE $p \@$g $role\n"; | |
1005 | $new = {} if !$new; | |
1006 | $new->{$role} = 1; | |
1007 | } | |
1008 | } | |
1009 | } | |
1010 | } | |
1011 | if ($new) { | |
1012 | $perm = $new; # overwrite previous settings | |
1013 | next; | |
1014 | } | |
2c3a6c0a DM |
1015 | } |
1016 | ||
4bc17477 DM |
1017 | return ('NoAccess') if defined ($perm->{NoAccess}); |
1018 | #return () if defined ($perm->{NoAccess}); | |
2c3a6c0a DM |
1019 | |
1020 | #print "permission $user $path = " . Dumper ($perm); | |
1021 | ||
4bc17477 | 1022 | my @ra = keys %$perm; |
2c3a6c0a DM |
1023 | |
1024 | #print "roles $user $path = " . join (',', @ra) . "\n"; | |
1025 | ||
1026 | return @ra; | |
1027 | } | |
1028 | ||
1029 | sub permission { | |
1030 | my ($cfg, $user, $path) = @_; | |
1031 | ||
5bb4e06a | 1032 | $user = PVE::Auth::Plugin::verify_username($user, 1); |
2c3a6c0a DM |
1033 | return {} if !$user; |
1034 | ||
1035 | my @ra = roles($cfg, $user, $path); | |
1036 | ||
1037 | my $privs = {}; | |
1038 | ||
1039 | foreach my $role (@ra) { | |
1040 | if (my $privset = $cfg->{roles}->{$role}) { | |
1041 | foreach my $p (keys %$privset) { | |
1042 | $privs->{$p} = 1; | |
1043 | } | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | #print "priviledges $user $path = " . Dumper ($privs); | |
1048 | ||
1049 | return $privs; | |
1050 | } | |
1051 | ||
1052 | sub check_permissions { | |
1053 | my ($username, $path, $privlist) = @_; | |
1054 | ||
1055 | $path = normalize_path($path); | |
1056 | my $usercfg = cfs_read_file('user.cfg'); | |
1057 | my $perm = permission($usercfg, $username, $path); | |
1058 | ||
1059 | foreach my $priv (split_list($privlist)) { | |
1060 | return undef if !$perm->{$priv}; | |
1061 | }; | |
1062 | ||
1063 | return 1; | |
1064 | } | |
1065 | ||
018ae3a9 DM |
1066 | sub add_vm_to_pool { |
1067 | my ($vmid, $pool) = @_; | |
1068 | ||
1069 | my $addVMtoPoolFn = sub { | |
1070 | my $usercfg = cfs_read_file("user.cfg"); | |
1071 | if (my $data = $usercfg->{pools}->{$pool}) { | |
1072 | $data->{vms}->{$vmid} = 1; | |
1073 | $usercfg->{vms}->{$vmid} = $pool; | |
1074 | cfs_write_file("user.cfg", $usercfg); | |
1075 | } | |
1076 | }; | |
1077 | ||
1078 | lock_user_config($addVMtoPoolFn, "can't add VM $vmid to pool '$pool'"); | |
1079 | } | |
1080 | ||
1081 | sub remove_vm_from_pool { | |
1082 | my ($vmid) = @_; | |
1083 | ||
1084 | my $delVMfromPoolFn = sub { | |
1085 | my $usercfg = cfs_read_file("user.cfg"); | |
1086 | if (my $pool = $usercfg->{vms}->{$vmid}) { | |
1087 | if (my $data = $usercfg->{pools}->{$pool}) { | |
1088 | delete $data->{vms}->{$vmid}; | |
1089 | delete $usercfg->{vms}->{$vmid}; | |
1090 | cfs_write_file("user.cfg", $usercfg); | |
1091 | } | |
1092 | } | |
1093 | }; | |
1094 | ||
1095 | lock_user_config($delVMfromPoolFn, "pool cleanup for VM $vmid failed"); | |
1096 | } | |
1097 | ||
2c3a6c0a | 1098 | 1; |