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