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