]>
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; | |
9 | use Authen::PAM qw(:constants); | |
10 | use Net::LDAP; | |
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; | |
14 | use Encode; | |
15 | ||
16 | use Data::Dumper; # fixme: remove | |
17 | ||
18 | # $authdir must be writable by root only! | |
19 | my $confdir = "/etc/pve"; | |
20 | my $authdir = "$confdir/priv"; | |
21 | my $authprivkeyfn = "$authdir/authkey.key"; | |
22 | my $authpubkeyfn = "$confdir/authkey.pub"; | |
23 | my $shadowconfigfile = "priv/shadow.cfg"; | |
24 | my $domainconfigfile = "domains.cfg"; | |
25 | my $pve_www_key_fn = "$confdir/pve-www.key"; | |
26 | ||
27 | my $ticket_lifetime = 3600*2; # 2 hours | |
28 | ||
29 | Crypt::OpenSSL::RSA->import_random_seed(); | |
30 | ||
31 | cfs_register_file('user.cfg', | |
32 | \&parse_user_config, | |
33 | \&write_user_config); | |
34 | ||
35 | cfs_register_file($shadowconfigfile, | |
36 | \&parse_shadow_passwd, | |
37 | \&write_shadow_config); | |
38 | ||
39 | cfs_register_file($domainconfigfile, | |
40 | \&parse_domains, | |
41 | \&write_domains); | |
42 | ||
43 | ||
44 | sub lock_user_config { | |
45 | my ($code, $errmsg) = @_; | |
46 | ||
47 | cfs_lock_file("user.cfg", undef, $code); | |
48 | my $err = $@; | |
49 | if ($err) { | |
50 | $errmsg ? die "$errmsg: $err" : die $err; | |
51 | } | |
52 | } | |
53 | ||
54 | sub lock_domain_config { | |
55 | my ($code, $errmsg) = @_; | |
56 | ||
57 | cfs_lock_file($domainconfigfile, undef, $code); | |
58 | my $err = $@; | |
59 | if ($err) { | |
60 | $errmsg ? die "$errmsg: $err" : die $err; | |
61 | } | |
62 | } | |
63 | ||
64 | sub lock_shadow_config { | |
65 | my ($code, $errmsg) = @_; | |
66 | ||
67 | cfs_lock_file($shadowconfigfile, undef, $code); | |
68 | my $err = $@; | |
69 | if ($err) { | |
70 | $errmsg ? die "$errmsg: $err" : die $err; | |
71 | } | |
72 | } | |
73 | ||
74 | my $pve_auth_pub_key; | |
75 | sub get_pubkey { | |
76 | ||
77 | return $pve_auth_pub_key if $pve_auth_pub_key; | |
78 | ||
79 | my $input = PVE::Tools::file_get_contents($authpubkeyfn); | |
80 | ||
81 | $pve_auth_pub_key = Crypt::OpenSSL::RSA->new_public_key($input); | |
82 | ||
83 | return $pve_auth_pub_key; | |
84 | } | |
85 | ||
86 | my $csrf_prevention_secret; | |
87 | my $get_csrfr_secret = sub { | |
88 | if (!$csrf_prevention_secret) { | |
89 | my $input = PVE::Tools::file_get_contents($pve_www_key_fn); | |
90 | $csrf_prevention_secret = Digest::SHA::sha1_base64($input); | |
91 | } | |
92 | return $csrf_prevention_secret; | |
93 | }; | |
94 | ||
95 | sub assemble_csrf_prevention_token { | |
96 | my ($username) = @_; | |
97 | ||
98 | my $timestamp = sprintf("%08X", time()); | |
99 | ||
100 | my $digest = Digest::SHA::sha1_base64("$timestamp:$username", &$get_csrfr_secret()); | |
101 | ||
102 | return "$timestamp:$digest"; | |
103 | } | |
104 | ||
105 | sub verify_csrf_prevention_token { | |
106 | my ($username, $token, $noerr) = @_; | |
107 | ||
108 | if ($token =~ m/^([A-Z0-9]{8}):(\S+)$/) { | |
109 | my $sig = $2; | |
110 | my $timestamp = $1; | |
111 | my $ttime = hex($timestamp); | |
112 | ||
113 | my $digest = Digest::SHA::sha1_base64("$timestamp:$username", &$get_csrfr_secret()); | |
114 | ||
115 | my $age = time() - $ttime; | |
116 | return if ($digest eq $sig) && ($age > -300) && ($age < $ticket_lifetime); | |
117 | } | |
118 | ||
119 | die "Permission denied - invalid csrf token\n" if !$noerr; | |
120 | ||
121 | return undef; | |
122 | } | |
123 | ||
124 | my $pve_auth_priv_key; | |
125 | sub get_privkey { | |
126 | ||
127 | return $pve_auth_priv_key if $pve_auth_priv_key; | |
128 | ||
129 | my $input = PVE::Tools::file_get_contents($authprivkeyfn); | |
130 | ||
131 | $pve_auth_priv_key = Crypt::OpenSSL::RSA->new_private_key($input); | |
132 | ||
133 | return $pve_auth_priv_key; | |
134 | } | |
135 | ||
136 | sub assemble_ticket { | |
137 | my ($username) = @_; | |
138 | ||
139 | my $rsa_priv = get_privkey(); | |
140 | ||
141 | my $timestamp = sprintf("%08X", time()); | |
142 | ||
143 | my $plain = "PVE:$username:$timestamp"; | |
144 | ||
145 | my $ticket = $plain . "::" . encode_base64($rsa_priv->sign($plain), ''); | |
146 | ||
147 | return $ticket; | |
148 | } | |
149 | ||
150 | sub verify_ticket { | |
151 | my ($ticket, $noerr) = @_; | |
152 | ||
adf8d771 | 153 | if ($ticket && $ticket =~ m/^(PVE:\S+)::([^:\s]+)$/) { |
2c3a6c0a DM |
154 | my $plain = $1; |
155 | my $sig = $2; | |
156 | ||
157 | my $rsa_pub = get_pubkey(); | |
158 | if ($rsa_pub->verify($plain, decode_base64($sig))) { | |
159 | if ($plain =~ m/^PVE:(([A-Za-z0-9\.\-_]+)(\@([A-Za-z0-9\.\-_]+))?):([A-Z0-9]{8})$/) { | |
160 | my $username = $1; | |
161 | my $timestamp = $5; | |
162 | my $ttime = hex($timestamp); | |
163 | ||
164 | my $age = time() - $ttime; | |
165 | ||
166 | if (($age > -300) && ($age < $ticket_lifetime)) { | |
167 | return wantarray ? ($username, $age) : $username; | |
168 | } | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
173 | die "permission denied - invalid ticket\n" if !$noerr; | |
174 | ||
175 | return undef; | |
176 | } | |
177 | ||
adf8d771 DM |
178 | # VNC tickets |
179 | # - they do not contain the username in plain text | |
180 | # - they are restricted to a specific resource path (example: '/vms/100') | |
181 | sub assemble_vnc_ticket { | |
182 | my ($username, $path) = @_; | |
183 | ||
184 | my $rsa_priv = get_privkey(); | |
185 | ||
186 | my $timestamp = sprintf("%08X", time()); | |
187 | ||
188 | my $plain = "PVEVNC:$timestamp"; | |
189 | ||
190 | $path = normalize_path($path); | |
191 | ||
192 | my $full = "$plain:$username:$path"; | |
193 | ||
194 | my $ticket = $plain . "::" . encode_base64($rsa_priv->sign($full), ''); | |
195 | ||
196 | return $ticket; | |
197 | } | |
198 | ||
199 | sub verify_vnc_ticket { | |
200 | my ($ticket, $username, $path, $noerr) = @_; | |
201 | ||
202 | if ($ticket && $ticket =~ m/^(PVEVNC:\S+)::([^:\s]+)$/) { | |
203 | my $plain = $1; | |
204 | my $sig = $2; | |
205 | my $full = "$plain:$username:$path"; | |
206 | ||
207 | my $rsa_pub = get_pubkey(); | |
208 | # Note: sign only match if $username and $path is correct | |
209 | if ($rsa_pub->verify($full, decode_base64($sig))) { | |
210 | if ($plain =~ m/^PVEVNC:([A-Z0-9]{8})$/) { | |
211 | my $ttime = hex($1); | |
212 | ||
213 | my $age = time() - $ttime; | |
214 | ||
215 | if (($age > -20) && ($age < 40)) { | |
216 | return 1; | |
217 | } | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | die "permission denied - invalid vnc ticket\n" if !$noerr; | |
223 | ||
224 | return undef; | |
225 | } | |
226 | ||
227 | ||
2c3a6c0a DM |
228 | sub authenticate_user_shadow { |
229 | my ($userid, $password) = @_; | |
230 | ||
231 | die "no password\n" if !$password; | |
232 | ||
233 | my $shadow_cfg = cfs_read_file($shadowconfigfile); | |
234 | ||
235 | if ($shadow_cfg->{users}->{$userid}) { | |
236 | my $encpw = crypt($password, $shadow_cfg->{users}->{$userid}->{shadow}); | |
237 | die "invalid credentials\n" if ($encpw ne $shadow_cfg->{users}->{$userid}->{shadow}); | |
238 | } else { | |
239 | die "no password set\n"; | |
240 | } | |
241 | } | |
242 | ||
243 | sub authenticate_user_pam { | |
244 | my ($userid, $password) = @_; | |
245 | ||
246 | # user (www-data) need to be able to read /etc/passwd /etc/shadow | |
247 | ||
248 | die "no password\n" if !$password; | |
249 | ||
250 | my $pamh = new Authen::PAM ('common-auth', $userid, sub { | |
251 | my @res; | |
252 | while(@_) { | |
253 | my $msg_type = shift; | |
254 | my $msg = shift; | |
255 | push @res, (0, $password); | |
256 | } | |
257 | push @res, 0; | |
258 | return @res; | |
259 | }); | |
260 | ||
261 | if (!ref ($pamh)) { | |
262 | my $err = $pamh->pam_strerror($pamh); | |
263 | die "error during PAM init: $err"; | |
264 | } | |
265 | ||
266 | my $res; | |
267 | ||
268 | if (($res = $pamh->pam_authenticate(0)) != PAM_SUCCESS) { | |
269 | my $err = $pamh->pam_strerror($res); | |
270 | die "$err\n"; | |
271 | } | |
272 | ||
273 | if (($res = $pamh->pam_acct_mgmt (0)) != PAM_SUCCESS) { | |
274 | my $err = $pamh->pam_strerror($res); | |
275 | die "$err\n"; | |
276 | } | |
277 | ||
278 | $pamh = 0; # call destructor | |
279 | } | |
280 | ||
281 | sub authenticate_user_ad { | |
282 | ||
283 | my ($entry, $server, $userid, $password) = @_; | |
284 | ||
285 | my $default_port = $entry->{secure} ? 636: 389; | |
286 | my $port = $entry->{port} ? $entry->{port} : $default_port; | |
287 | my $scheme = $entry->{secure} ? 'ldaps' : 'ldap'; | |
288 | my $conn_string = "$scheme://${server}:$port"; | |
289 | ||
290 | my $ldap = Net::LDAP->new($server) || die "$@\n"; | |
291 | ||
292 | $userid = "$userid\@$entry->{domain}" | |
293 | if $userid !~ m/@/ && $entry->{domain}; | |
294 | ||
295 | my $res = $ldap->bind($userid, password => $password); | |
296 | ||
297 | my $code = $res->code(); | |
298 | my $err = $res->error; | |
299 | ||
300 | $ldap->unbind(); | |
301 | ||
302 | die "$err\n" if ($code); | |
303 | } | |
304 | ||
305 | sub authenticate_user_ldap { | |
306 | ||
307 | my ($entry, $server, $userid, $password) = @_; | |
308 | ||
309 | my $default_port = $entry->{secure} ? 636: 389; | |
310 | my $port = $entry->{port} ? $entry->{port} : $default_port; | |
311 | my $scheme = $entry->{secure} ? 'ldaps' : 'ldap'; | |
312 | my $conn_string = "$scheme://${server}:$port"; | |
313 | ||
314 | my $ldap = Net::LDAP->new($conn_string, verify => 'none') || die "$@\n"; | |
315 | my $search = $entry->{user_attr} . "=" . $userid; | |
316 | my $result = $ldap->search( base => "$entry->{base_dn}", | |
317 | scope => "sub", | |
318 | filter => "$search", | |
319 | attrs => ['dn'] | |
320 | ); | |
321 | die "no entries returned\n" if !$result->entries; | |
322 | my @entries = $result->entries; | |
323 | my $res = $ldap->bind($entries[0]->dn, password => $password); | |
324 | ||
325 | my $code = $res->code(); | |
326 | my $err = $res->error; | |
327 | ||
328 | $ldap->unbind(); | |
329 | ||
330 | die "$err\n" if ($code); | |
331 | } | |
332 | ||
333 | sub authenticate_user_domain { | |
334 | my ($realm, $userid, $password) = @_; | |
335 | ||
336 | my $domain_cfg = cfs_read_file($domainconfigfile); | |
337 | ||
338 | die "no auth domain specified" if !$realm; | |
339 | ||
340 | if ($realm eq 'pam') { | |
341 | authenticate_user_pam($userid, $password); | |
342 | return; | |
343 | } | |
344 | ||
345 | eval { | |
346 | if ($realm eq 'pve') { | |
347 | authenticate_user_shadow($userid, $password); | |
348 | } else { | |
349 | ||
350 | my $cfg = $domain_cfg->{$realm}; | |
351 | die "auth domain '$realm' does not exists\n" if !$cfg; | |
352 | ||
353 | if ($cfg->{type} eq 'ad') { | |
354 | eval { authenticate_user_ad($cfg, $cfg->{server1}, $userid, $password); }; | |
355 | my $err = $@; | |
356 | return if !$err; | |
357 | die $err if !$cfg->{server2}; | |
358 | authenticate_user_ad($cfg, $cfg->{server2}, $userid, $password); | |
359 | } elsif ($cfg->{type} eq 'ldap') { | |
360 | eval { authenticate_user_ldap($cfg, $cfg->{server1}, $userid, $password); }; | |
361 | my $err = $@; | |
362 | return if !$err; | |
363 | die $err if !$cfg->{server2}; | |
364 | authenticate_user_ldap($cfg, $cfg->{server2}, $userid, $password); | |
365 | } else { | |
366 | die "unknown auth type '$cfg->{type}'\n"; | |
367 | } | |
368 | } | |
369 | }; | |
370 | if (my $err = $@) { | |
371 | sleep(2); # timeout after failed auth | |
372 | die $err; | |
373 | } | |
374 | } | |
375 | ||
37d45deb | 376 | sub check_user_exist { |
7070c1ae | 377 | my ($usercfg, $username, $noerr) = @_; |
2c3a6c0a | 378 | |
7070c1ae | 379 | $username = verify_username($username, $noerr); |
2c3a6c0a DM |
380 | return undef if !$username; |
381 | ||
37d45deb DM |
382 | return $usercfg->{users}->{$username} if $usercfg && $usercfg->{users}->{$username}; |
383 | ||
384 | die "no such user ('$username')\n" if !$noerr; | |
385 | ||
386 | return undef; | |
387 | } | |
388 | ||
389 | sub check_user_enabled { | |
390 | my ($usercfg, $username, $noerr) = @_; | |
391 | ||
392 | my $data = check_user_exist($usercfg, $username, $noerr); | |
393 | return undef if !$data; | |
394 | ||
395 | return 1 if $data->{enable}; | |
2c3a6c0a DM |
396 | |
397 | return 1 if $username eq 'root@pam'; # root is always enabled | |
398 | ||
37d45deb | 399 | die "user '$username' is disabled\n" if !$noerr; |
7070c1ae DM |
400 | |
401 | return undef; | |
2c3a6c0a DM |
402 | } |
403 | ||
404 | # password should be utf8 encoded | |
405 | sub authenticate_user { | |
406 | my ($username, $password) = @_; | |
407 | ||
408 | die "no username specified\n" if !$username; | |
409 | ||
410 | my ($userid, $realm); | |
411 | ||
412 | ($username, $userid, $realm) = verify_username($username); | |
413 | ||
414 | my $usercfg = cfs_read_file('user.cfg'); | |
415 | ||
7070c1ae DM |
416 | eval { check_user_enabled($usercfg, $username); }; |
417 | if (my $err = $@) { | |
2c3a6c0a | 418 | sleep(2); |
7070c1ae | 419 | die $err; |
2c3a6c0a DM |
420 | } |
421 | ||
422 | my $ctime = time(); | |
423 | my $expire = $usercfg->{users}->{$username}->{expire}; | |
424 | ||
425 | if ($expire && ($expire < $ctime)) { | |
426 | sleep(2); | |
427 | die "account expired\n" | |
428 | } | |
429 | ||
430 | authenticate_user_domain($realm, $userid, $password); | |
431 | ||
432 | return $username; | |
433 | } | |
434 | ||
435 | sub delete_shadow_password { | |
436 | my ($userid) = @_; | |
437 | ||
438 | lock_shadow_config(sub { | |
439 | my $shadow_cfg = cfs_read_file($shadowconfigfile); | |
440 | delete ($shadow_cfg->{users}->{$userid}) | |
441 | if $shadow_cfg->{users}->{$userid}; | |
442 | cfs_write_file($shadowconfigfile, $shadow_cfg); | |
443 | }); | |
444 | } | |
445 | ||
446 | sub store_shadow_password { | |
447 | my ($userid, $password) = @_; | |
448 | ||
449 | lock_shadow_config(sub { | |
450 | my $shadow_cfg = cfs_read_file($shadowconfigfile); | |
451 | $shadow_cfg->{users}->{$userid}->{shadow} = encrypt_pw($password); | |
452 | cfs_write_file($shadowconfigfile, $shadow_cfg); | |
453 | }); | |
454 | } | |
455 | ||
456 | sub encrypt_pw { | |
457 | my ($pw) = @_; | |
458 | ||
459 | my $time = substr (Digest::SHA::sha1_base64 (time), 0, 8); | |
460 | return crypt (encode("utf8", $pw), "\$5\$$time\$"); | |
461 | } | |
462 | ||
463 | sub store_pam_password { | |
464 | my ($userid, $password) = @_; | |
465 | ||
466 | my $cmd = ['/usr/sbin/usermod']; | |
467 | ||
468 | my $epw = encrypt_pw($password); | |
469 | push @$cmd, '-p', $epw; | |
470 | ||
471 | push @$cmd, $userid; | |
472 | ||
473 | run_command($cmd); | |
474 | } | |
475 | ||
476 | sub domain_set_password { | |
477 | my ($realm, $userid, $password) = @_; | |
478 | ||
479 | die "no auth domain specified" if !$realm; | |
480 | ||
481 | if ($realm eq 'pam') { | |
482 | store_pam_password($userid, $password); | |
483 | } elsif ($realm eq 'pve') { | |
484 | store_shadow_password($userid, $password); | |
485 | } else { | |
486 | die "can't set password on auth domain '$realm'\n"; | |
487 | } | |
488 | } | |
489 | ||
490 | sub add_user_group { | |
491 | ||
492 | my ($username, $usercfg, $group) = @_; | |
493 | $usercfg->{users}->{$username}->{groups}->{$group} = 1; | |
494 | $usercfg->{groups}->{$group}->{users}->{$username} = 1; | |
495 | } | |
496 | ||
497 | sub delete_user_group { | |
498 | ||
499 | my ($username, $usercfg) = @_; | |
500 | ||
501 | foreach my $group (keys %{$usercfg->{groups}}) { | |
502 | ||
503 | delete ($usercfg->{groups}->{$group}->{users}->{$username}) | |
504 | if $usercfg->{groups}->{$group}->{users}->{$username}; | |
505 | } | |
506 | } | |
507 | ||
508 | sub delete_user_acl { | |
509 | ||
510 | my ($username, $usercfg) = @_; | |
511 | ||
512 | foreach my $acl (keys %{$usercfg->{acl}}) { | |
513 | ||
514 | delete ($usercfg->{acl}->{$acl}->{users}->{$username}) | |
515 | if $usercfg->{acl}->{$acl}->{users}->{$username}; | |
516 | } | |
2c3a6c0a | 517 | } |
39c85db8 | 518 | |
2c3a6c0a DM |
519 | sub delete_group_acl { |
520 | ||
521 | my ($group, $usercfg) = @_; | |
522 | ||
523 | foreach my $acl (keys %{$usercfg->{acl}}) { | |
524 | ||
525 | delete ($usercfg->{acl}->{$acl}->{groups}->{$group}) | |
526 | if $usercfg->{acl}->{$acl}->{groups}->{$group}; | |
527 | } | |
39c85db8 DM |
528 | } |
529 | ||
530 | sub delete_pool_acl { | |
531 | ||
532 | my ($pool, $usercfg) = @_; | |
2c3a6c0a | 533 | |
39c85db8 DM |
534 | my $path = "/pool/$pool"; |
535 | ||
536 | foreach my $aclpath (keys %{$usercfg->{acl}}) { | |
537 | delete ($usercfg->{acl}->{$aclpath}) | |
538 | if $usercfg->{acl}->{$aclpath} eq 'path'; | |
539 | } | |
2c3a6c0a DM |
540 | } |
541 | ||
542 | # we automatically create some predefined roles by splitting privs | |
543 | # into 3 groups (per category) | |
544 | # root: only root is allowed to do that | |
545 | # admin: an administrator can to that | |
546 | # user: a normak user/customer can to that | |
547 | my $privgroups = { | |
548 | VM => { | |
549 | root => [], | |
550 | admin => [ | |
c0fead8c | 551 | 'VM.Config.Disk', |
373cb383 | 552 | 'VM.Config.CDROM', # change CDROM media |
c0fead8c DM |
553 | 'VM.Config.CPU', |
554 | 'VM.Config.Memory', | |
555 | 'VM.Config.Network', | |
556 | 'VM.Config.HWType', | |
557 | 'VM.Config.Options', # covers all other things | |
2c3a6c0a DM |
558 | 'VM.Allocate', |
559 | 'VM.Migrate', | |
c0fead8c | 560 | 'VM.Monitor', |
2c3a6c0a DM |
561 | ], |
562 | user => [ | |
563 | 'VM.Console', | |
564 | 'VM.PowerMgmt', | |
565 | ], | |
566 | audit => [ | |
82b63965 | 567 | 'VM.Audit', |
2c3a6c0a DM |
568 | ], |
569 | }, | |
570 | Sys => { | |
571 | root => [ | |
572 | 'Sys.PowerMgmt', | |
37d45deb | 573 | 'Sys.Modify', # edit/change node settings |
2c3a6c0a DM |
574 | ], |
575 | admin => [ | |
2e376c58 | 576 | 'Permissions.Modify', |
2c3a6c0a DM |
577 | 'Sys.Console', |
578 | 'Sys.Syslog', | |
579 | ], | |
580 | user => [], | |
581 | audit => [ | |
582 | 'Sys.Audit', | |
583 | ], | |
584 | }, | |
585 | Datastore => { | |
2e376c58 | 586 | root => [], |
19f60b5e DM |
587 | admin => [ |
588 | 'Datastore.Allocate', | |
373cb383 | 589 | 'Datastore.AllocateTemplate', |
19f60b5e | 590 | ], |
2c3a6c0a DM |
591 | user => [ |
592 | 'Datastore.AllocateSpace', | |
593 | ], | |
594 | audit => [ | |
595 | 'Datastore.Audit', | |
596 | ], | |
597 | }, | |
12683df7 | 598 | User => { |
82b63965 DM |
599 | root => [ |
600 | 'Realm.Allocate', | |
601 | ], | |
12683df7 DM |
602 | admin => [ |
603 | 'User.Modify', | |
82b63965 DM |
604 | 'Group.Allocate', # edit/change group settings |
605 | 'Realm.AllocateUser', | |
19f60b5e | 606 | ], |
12683df7 DM |
607 | user => [], |
608 | audit => [], | |
609 | }, | |
dee1c882 DM |
610 | Pool => { |
611 | root => [], | |
612 | admin => [ | |
613 | 'Pool.Allocate', # create/delete pools | |
614 | ], | |
615 | user => [], | |
616 | audit => [], | |
617 | }, | |
2c3a6c0a DM |
618 | }; |
619 | ||
620 | my $valid_privs = {}; | |
621 | ||
622 | my $special_roles = { | |
623 | 'NoAccess' => {}, # no priviledges | |
624 | 'Administrator' => $valid_privs, # all priviledges | |
625 | }; | |
626 | ||
627 | sub create_roles { | |
628 | ||
629 | foreach my $cat (keys %$privgroups) { | |
630 | my $cd = $privgroups->{$cat}; | |
631 | foreach my $p (@{$cd->{root}}, @{$cd->{admin}}, | |
632 | @{$cd->{user}}, @{$cd->{audit}}) { | |
633 | $valid_privs->{$p} = 1; | |
634 | } | |
635 | foreach my $p (@{$cd->{admin}}, @{$cd->{user}}, @{$cd->{audit}}) { | |
636 | ||
637 | $special_roles->{"PVE${cat}Admin"}->{$p} = 1; | |
638 | $special_roles->{"PVEAdmin"}->{$p} = 1; | |
639 | } | |
640 | if (scalar(@{$cd->{user}})) { | |
641 | foreach my $p (@{$cd->{user}}, @{$cd->{audit}}) { | |
642 | $special_roles->{"PVE${cat}User"}->{$p} = 1; | |
643 | } | |
644 | } | |
645 | foreach my $p (@{$cd->{audit}}) { | |
646 | $special_roles->{"PVEAuditor"}->{$p} = 1; | |
647 | } | |
648 | } | |
649 | }; | |
650 | ||
651 | create_roles(); | |
652 | ||
653 | my $valid_attributes = { | |
654 | ad => { | |
655 | server1 => '[\w\d]+(.[\w\d]+)*', | |
656 | server2 => '[\w\d]+(.[\w\d]+)*', | |
657 | domain => '\S+', | |
af4a8a85 | 658 | port => '\d+', |
2c3a6c0a DM |
659 | secure => '', |
660 | comment => '.*', | |
661 | }, | |
662 | ldap => { | |
663 | server1 => '[\w\d]+(.[\w\d]+)*', | |
664 | server2 => '[\w\d]+(.[\w\d]+)*', | |
665 | base_dn => '\w+=[\w\s]+(,\s*\w+=[\w\s]+)*', | |
666 | user_attr => '\S{2,}', | |
667 | secure => '', | |
af4a8a85 | 668 | port => '\d+', |
2c3a6c0a DM |
669 | comment => '.*', |
670 | } | |
671 | }; | |
672 | ||
673 | sub add_role_privs { | |
674 | my ($role, $usercfg, $privs) = @_; | |
675 | ||
676 | return if !$privs; | |
677 | ||
678 | die "role '$role' does not exist\n" if !$usercfg->{roles}->{$role}; | |
679 | ||
680 | foreach my $priv (split_list($privs)) { | |
681 | if (defined ($valid_privs->{$priv})) { | |
682 | $usercfg->{roles}->{$role}->{$priv} = 1; | |
683 | } else { | |
684 | die "invalid priviledge '$priv'\n"; | |
685 | } | |
686 | } | |
687 | } | |
688 | ||
689 | sub normalize_path { | |
690 | my $path = shift; | |
691 | ||
4bc17477 | 692 | $path =~ s|/+|/|g; |
2c3a6c0a DM |
693 | |
694 | $path =~ s|/$||; | |
695 | ||
696 | $path = '/' if !$path; | |
697 | ||
4bc17477 DM |
698 | $path = "/$path" if $path !~ m|^/|; |
699 | ||
2c3a6c0a DM |
700 | return undef if $path !~ m|^[[:alnum:]\-\_\/]+$|; |
701 | ||
702 | return $path; | |
703 | } | |
704 | ||
705 | my $realm_regex = qr/[A-Za-z][A-Za-z0-9\.\-_]+/; | |
706 | ||
707 | sub pve_verify_realm { | |
708 | my ($realm, $noerr) = @_; | |
709 | ||
710 | if ($realm !~ m/^${realm_regex}$/) { | |
711 | return undef if $noerr; | |
712 | die "value does not look like a valid realm\n"; | |
713 | } | |
714 | return $realm; | |
715 | } | |
716 | ||
717 | PVE::JSONSchema::register_format('pve-userid', \&verify_username); | |
718 | sub verify_username { | |
719 | my ($username, $noerr) = @_; | |
720 | ||
721 | $username = '' if !$username; | |
722 | my $len = length($username); | |
723 | if ($len < 3) { | |
724 | die "user name '$username' is too short\n" if !$noerr; | |
725 | return undef; | |
726 | } | |
727 | if ($len > 64) { | |
728 | die "user name '$username' is too long ($len > 64)\n" if !$noerr; | |
729 | return undef; | |
730 | } | |
731 | ||
732 | # we only allow a limited set of characters (colon is not allowed, | |
733 | # because we store usernames in colon separated lists)! | |
734 | if ($username =~ m/^([^\s:]+)\@(${realm_regex})$/) { | |
735 | return wantarray ? ($username, $1, $2) : $username; | |
736 | } | |
737 | ||
738 | die "value '$username' does not look like a valid user name\n" if !$noerr; | |
739 | ||
740 | return undef; | |
741 | } | |
742 | PVE::JSONSchema::register_standard_option('userid', { | |
743 | description => "User ID", | |
744 | type => 'string', format => 'pve-userid', | |
745 | maxLength => 64, | |
746 | }); | |
747 | ||
748 | PVE::JSONSchema::register_standard_option('realm', { | |
749 | description => "Authentication domain ID", | |
750 | type => 'string', format => 'pve-configid', | |
751 | maxLength => 32, | |
752 | }); | |
753 | ||
754 | PVE::JSONSchema::register_format('pve-groupid', \&verify_groupname); | |
755 | sub verify_groupname { | |
756 | my ($groupname, $noerr) = @_; | |
757 | ||
758 | if ($groupname !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
759 | ||
760 | die "group name '$groupname' contains invalid characters\n" if !$noerr; | |
761 | ||
762 | return undef; | |
763 | } | |
764 | ||
765 | return $groupname; | |
766 | } | |
767 | ||
768 | PVE::JSONSchema::register_format('pve-roleid', \&verify_rolename); | |
769 | sub verify_rolename { | |
770 | my ($rolename, $noerr) = @_; | |
771 | ||
772 | if ($rolename !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
773 | ||
774 | die "role name '$rolename' contains invalid characters\n" if !$noerr; | |
775 | ||
776 | return undef; | |
777 | } | |
778 | ||
779 | return $rolename; | |
780 | } | |
781 | ||
39c85db8 DM |
782 | PVE::JSONSchema::register_format('pve-poolid', \&verify_groupname); |
783 | sub verify_poolname { | |
784 | my ($poolname, $noerr) = @_; | |
785 | ||
786 | if ($poolname !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
787 | ||
788 | die "pool name '$poolname' contains invalid characters\n" if !$noerr; | |
789 | ||
790 | return undef; | |
791 | } | |
792 | ||
793 | return $poolname; | |
794 | } | |
795 | ||
2c3a6c0a DM |
796 | PVE::JSONSchema::register_format('pve-priv', \&verify_privname); |
797 | sub verify_privname { | |
798 | my ($priv, $noerr) = @_; | |
799 | ||
800 | if (!$valid_privs->{$priv}) { | |
801 | die "invalid priviledge '$priv'\n" if !$noerr; | |
802 | ||
803 | return undef; | |
804 | } | |
805 | ||
806 | return $priv; | |
807 | } | |
808 | ||
809 | sub userconfig_force_defaults { | |
810 | my ($cfg) = @_; | |
811 | ||
812 | foreach my $r (keys %$special_roles) { | |
813 | $cfg->{roles}->{$r} = $special_roles->{$r}; | |
814 | } | |
815 | ||
816 | # fixme: remove 'root' group (not required)? | |
817 | ||
818 | # add root user | |
819 | $cfg->{users}->{'root@pam'}->{enable} = 1; | |
820 | } | |
821 | ||
822 | sub parse_user_config { | |
823 | my ($filename, $raw) = @_; | |
824 | ||
825 | my $cfg = {}; | |
826 | ||
827 | userconfig_force_defaults($cfg); | |
828 | ||
829 | while ($raw && $raw =~ s/^(.*?)(\n|$)//) { | |
830 | my $line = $1; | |
831 | ||
832 | next if $line =~ m/^\s*$/; # skip empty lines | |
833 | ||
834 | my @data; | |
835 | ||
836 | foreach my $d (split (/:/, $line)) { | |
837 | $d =~ s/^\s+//; | |
838 | $d =~ s/\s+$//; | |
839 | push @data, $d | |
840 | } | |
841 | ||
842 | my $et = shift @data; | |
843 | ||
844 | if ($et eq 'user') { | |
845 | my ($user, $enable, $expire, $firstname, $lastname, $email, $comment) = @data; | |
846 | ||
847 | my (undef, undef, $realm) = verify_username($user, 1); | |
848 | if (!$realm) { | |
849 | warn "user config - ignore user '$user' - invalid user name\n"; | |
850 | next; | |
851 | } | |
852 | ||
853 | $enable = $enable ? 1 : 0; | |
854 | ||
855 | $expire = 0 if !$expire; | |
856 | ||
857 | if ($expire !~ m/^\d+$/) { | |
858 | warn "user config - ignore user '$user' - (illegal characters in expire '$expire')\n"; | |
859 | next; | |
860 | } | |
861 | $expire = int($expire); | |
862 | ||
863 | #if (!verify_groupname ($group, 1)) { | |
864 | # warn "user config - ignore user '$user' - invalid characters in group name\n"; | |
865 | # next; | |
866 | #} | |
867 | ||
868 | $cfg->{users}->{$user} = { | |
869 | enable => $enable, | |
870 | # group => $group, | |
871 | }; | |
872 | $cfg->{users}->{$user}->{firstname} = PVE::Tools::decode_text($firstname) if $firstname; | |
873 | $cfg->{users}->{$user}->{lastname} = PVE::Tools::decode_text($lastname) if $lastname; | |
874 | $cfg->{users}->{$user}->{email} = $email; | |
875 | $cfg->{users}->{$user}->{comment} = PVE::Tools::decode_text($comment) if $comment; | |
876 | $cfg->{users}->{$user}->{expire} = $expire; | |
877 | ||
878 | #$cfg->{users}->{$user}->{groups}->{$group} = 1; | |
879 | #$cfg->{groups}->{$group}->{$user} = 1; | |
880 | ||
881 | } elsif ($et eq 'group') { | |
882 | my ($group, $userlist, $comment) = @data; | |
883 | ||
884 | if (!verify_groupname($group, 1)) { | |
885 | warn "user config - ignore group '$group' - invalid characters in group name\n"; | |
886 | next; | |
887 | } | |
888 | ||
889 | # make sure to add the group (even if there are no members) | |
890 | $cfg->{groups}->{$group} = { users => {} } if !$cfg->{groups}->{$group}; | |
891 | ||
892 | $cfg->{groups}->{$group}->{comment} = PVE::Tools::decode_text($comment) if $comment; | |
893 | ||
894 | foreach my $user (split_list($userlist)) { | |
895 | ||
896 | if (!verify_username($user, 1)) { | |
897 | warn "user config - ignore invalid group member '$user'\n"; | |
898 | next; | |
899 | } | |
900 | ||
901 | if ($cfg->{users}->{$user}) { # user exists | |
902 | $cfg->{users}->{$user}->{groups}->{$group} = 1; | |
903 | $cfg->{groups}->{$group}->{users}->{$user} = 1; | |
904 | } else { | |
905 | warn "user config - ignore invalid group member '$user'\n"; | |
906 | } | |
907 | } | |
908 | ||
909 | } elsif ($et eq 'role') { | |
910 | my ($role, $privlist) = @data; | |
911 | ||
912 | if (!verify_rolename($role, 1)) { | |
913 | warn "user config - ignore role '$role' - invalid characters in role name\n"; | |
914 | next; | |
915 | } | |
916 | ||
917 | # make sure to add the role (even if there are no privileges) | |
918 | $cfg->{roles}->{$role} = {} if !$cfg->{roles}->{$role}; | |
919 | ||
920 | foreach my $priv (split_list($privlist)) { | |
921 | if (defined ($valid_privs->{$priv})) { | |
922 | $cfg->{roles}->{$role}->{$priv} = 1; | |
923 | } else { | |
924 | warn "user config - ignore invalid priviledge '$priv'\n"; | |
925 | } | |
926 | } | |
927 | ||
928 | } elsif ($et eq 'acl') { | |
929 | my ($propagate, $pathtxt, $uglist, $rolelist) = @data; | |
930 | ||
931 | if (my $path = normalize_path($pathtxt)) { | |
932 | foreach my $role (split_list($rolelist)) { | |
933 | ||
934 | if (!verify_rolename($role, 1)) { | |
935 | warn "user config - ignore invalid role name '$role' in acl\n"; | |
936 | next; | |
937 | } | |
938 | ||
939 | foreach my $ug (split_list($uglist)) { | |
940 | if ($ug =~ m/^@(\w+)$/) { | |
941 | my $group = $1; | |
942 | if ($cfg->{groups}->{$group}) { # group exists | |
943 | $cfg->{acl}->{$path}->{groups}->{$group}->{$role} = $propagate; | |
944 | } else { | |
945 | warn "user config - ignore invalid acl group '$group'\n"; | |
946 | } | |
947 | } elsif (verify_username($ug, 1)) { | |
948 | if ($cfg->{users}->{$ug}) { # user exists | |
949 | $cfg->{acl}->{$path}->{users}->{$ug}->{$role} = $propagate; | |
950 | } else { | |
951 | warn "user config - ignore invalid acl member '$ug'\n"; | |
952 | } | |
953 | } else { | |
954 | warn "user config - invalid user/group '$ug' in acl\n"; | |
955 | } | |
956 | } | |
957 | } | |
958 | } else { | |
959 | warn "user config - ignore invalid path in acl '$pathtxt'\n"; | |
960 | } | |
4bc17477 | 961 | } elsif ($et eq 'pool') { |
39c85db8 | 962 | my ($pool, $comment, $vmlist, $storelist) = @data; |
4bc17477 | 963 | |
39c85db8 DM |
964 | if (!verify_poolname($pool, 1)) { |
965 | warn "user config - ignore pool '$pool' - invalid characters in pool name\n"; | |
966 | next; | |
967 | } | |
4bc17477 | 968 | |
39c85db8 DM |
969 | # make sure to add the pool (even if there are no members) |
970 | $cfg->{pools}->{$pool} = { vms => {}, storage => {} } if !$cfg->{pools}->{$pool}; | |
4bc17477 | 971 | |
39c85db8 | 972 | $cfg->{pools}->{$pool}->{comment} = PVE::Tools::decode_text($comment) if $comment; |
4bc17477 | 973 | |
39c85db8 DM |
974 | foreach my $vmid (split_list($vmlist)) { |
975 | if ($vmid !~ m/^\d+$/) { | |
976 | warn "user config - ignore invalid vmid '$vmid' in pool '$pool'\n"; | |
977 | next; | |
4bc17477 | 978 | } |
39c85db8 | 979 | $vmid = int($vmid); |
4bc17477 | 980 | |
39c85db8 DM |
981 | if ($cfg->{vms}->{$vmid}) { |
982 | warn "user config - ignore duplicate vmid '$vmid' in pool '$pool'\n"; | |
983 | next; | |
4bc17477 DM |
984 | } |
985 | ||
39c85db8 DM |
986 | $cfg->{pools}->{$pool}->{vms}->{$vmid} = 1; |
987 | ||
988 | # record vmid ==> pool relation | |
989 | $cfg->{vms}->{$vmid} = $pool; | |
990 | } | |
991 | ||
992 | foreach my $storeid (split_list($storelist)) { | |
993 | if ($storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) { | |
994 | warn "user config - ignore invalid storage '$storeid' in pool '$pool'\n"; | |
995 | next; | |
996 | } | |
997 | $cfg->{pools}->{$pool}->{storage}->{$storeid} = 1; | |
4bc17477 | 998 | } |
2c3a6c0a DM |
999 | } else { |
1000 | warn "user config - ignore config line: $line\n"; | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | userconfig_force_defaults($cfg); | |
1005 | ||
1006 | return $cfg; | |
1007 | } | |
1008 | ||
1009 | sub parse_shadow_passwd { | |
1010 | my ($filename, $raw) = @_; | |
1011 | ||
1012 | my $shadow = {}; | |
1013 | ||
1014 | while ($raw && $raw =~ s/^(.*?)(\n|$)//) { | |
1015 | my $line = $1; | |
1016 | ||
1017 | next if $line =~ m/^\s*$/; # skip empty lines | |
1018 | ||
1019 | if ($line !~ m/^\S+:\S+:$/) { | |
1020 | warn "pve shadow password: ignore invalid line $.\n"; | |
1021 | next; | |
1022 | } | |
1023 | ||
1024 | my ($userid, $crypt_pass) = split (/:/, $line); | |
1025 | $shadow->{users}->{$userid}->{shadow} = $crypt_pass; | |
1026 | } | |
1027 | ||
1028 | return $shadow; | |
1029 | } | |
1030 | ||
1031 | sub write_domains { | |
1032 | my ($filename, $cfg) = @_; | |
1033 | ||
1034 | my $data = ''; | |
1035 | ||
1036 | my $wrote_default; | |
1037 | ||
1038 | foreach my $realm (sort keys %$cfg) { | |
1039 | my $entry = $cfg->{$realm}; | |
1040 | my $type = lc($entry->{type}); | |
1041 | ||
1042 | next if !$type; | |
1043 | ||
1044 | next if ($type eq 'pam') || ($type eq 'pve'); | |
1045 | ||
1046 | my $formats = $valid_attributes->{$type}; | |
1047 | next if !$formats; | |
1048 | ||
1049 | $data .= "$type: $realm\n"; | |
1050 | ||
1051 | foreach my $k (sort keys %$entry) { | |
1052 | next if $k eq 'type'; | |
1053 | my $v = $entry->{$k}; | |
1054 | if ($k eq 'default') { | |
1055 | $data .= "\t$k\n" if $v && !$wrote_default; | |
1056 | $wrote_default = 1; | |
1057 | } elsif (defined($formats->{$k})) { | |
1058 | if (!$formats->{$k}) { | |
0c156363 | 1059 | $data .= "\t$k\n" if $v; |
2c3a6c0a DM |
1060 | } elsif ($v =~ m/^$formats->{$k}$/) { |
1061 | $v = PVE::Tools::encode_text($v) if $k eq 'comment'; | |
1062 | $data .= "\t$k $v\n"; | |
1063 | } else { | |
1064 | die "invalid value '$v' for attribute '$k'\n"; | |
1065 | } | |
1066 | } else { | |
1067 | die "invalid attribute '$k' - not supported\n"; | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | $data .= "\n"; | |
1072 | } | |
1073 | ||
1074 | return $data; | |
1075 | } | |
1076 | ||
1077 | sub parse_domains { | |
1078 | my ($filename, $raw) = @_; | |
1079 | ||
1080 | my $cfg = {}; | |
1081 | ||
1082 | my $default; | |
1083 | ||
1084 | while ($raw && $raw =~ s/^(.*?)(\n|$)//) { | |
1085 | my $line = $1; | |
1086 | ||
1087 | next if $line =~ m/^\#/; # skip comment lines | |
1088 | next if $line =~ m/^\s*$/; # skip empty lines | |
1089 | ||
1090 | if ($line =~ m/^(\S+):\s*(\S+)\s*$/) { | |
1091 | my $realm = $2; | |
1092 | my $type = lc($1); | |
1093 | ||
1094 | my $ignore = 0; | |
1095 | my $entry; | |
1096 | ||
1097 | my $formats = $valid_attributes->{$type}; | |
1098 | if (!$formats) { | |
1099 | $ignore = 1; | |
1100 | warn "ignoring domain '$realm' - (unsupported authentication type '$type')\n"; | |
1101 | } elsif (!pve_verify_realm($realm, 1)) { | |
1102 | $ignore = 1; | |
1103 | warn "ignoring domain '$realm' - (illegal characters)\n"; | |
1104 | } else { | |
1105 | $entry = { type => $type }; | |
1106 | } | |
1107 | ||
1108 | while ($raw && $raw =~ s/^(.*?)(\n|$)//) { | |
1109 | $line = $1; | |
1110 | ||
1111 | next if $line =~ m/^\#/; #skip comment lines | |
1112 | last if $line =~ m/^\s*$/; | |
1113 | ||
1114 | next if $ignore; # skip | |
1115 | ||
1116 | if ($line =~ m/^\s+(default)\s*$/) { | |
1117 | $default = $realm if !$default; | |
1118 | } elsif ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) { | |
1119 | my ($k, $v) = (lc($1), $3); | |
1120 | if (defined($formats->{$k})) { | |
1121 | if (!$formats->{$k} && !defined($v)) { | |
1122 | $entry->{$k} = 1; | |
1123 | } elsif ($formats->{$k} && $v =~ m/^$formats->{$k}$/) { | |
1124 | if (!defined($entry->{$k})) { | |
1125 | $v = PVE::Tools::decode_text($v) if $k eq 'comment'; | |
1126 | $entry->{$k} = $v; | |
1127 | } else { | |
1128 | warn "ignoring duplicate attribute '$k $v'\n"; | |
1129 | } | |
1130 | } else { | |
1131 | warn "ignoring value '$v' for attribute '$k' - invalid format\n"; | |
1132 | } | |
1133 | } else { | |
1134 | warn "ignoring attribute '$k' - not supported\n"; | |
1135 | } | |
1136 | } else { | |
1137 | warn "ignore config line: $line\n"; | |
1138 | } | |
1139 | } | |
1140 | ||
1141 | if ($entry->{server2} && !$entry->{server1}) { | |
1142 | $entry->{server1} = $entry->{server2}; | |
1143 | delete $entry->{server2}; | |
1144 | } | |
1145 | ||
1146 | if ($ignore) { | |
1147 | # do nothing | |
1148 | } elsif (!$entry->{server1}) { | |
1149 | warn "ignoring domain '$realm' - missing server attribute\n"; | |
1150 | } elsif (($entry->{type} eq "ldap") && !$entry->{user_attr}) { | |
1151 | warn "ignoring domain '$realm' - missing user attribute\n"; | |
1152 | } elsif (($entry->{type} eq "ldap") && !$entry->{base_dn}) { | |
1153 | warn "ignoring domain '$realm' - missing base_dn attribute\n"; | |
0c156363 DM |
1154 | } elsif (($entry->{type} eq "ad") && !$entry->{domain}) { |
1155 | warn "ignoring domain '$realm' - missing domain attribute\n"; | |
2c3a6c0a DM |
1156 | } else { |
1157 | $cfg->{$realm} = $entry; | |
1158 | } | |
1159 | ||
1160 | } else { | |
1161 | warn "ignore config line: $line\n"; | |
1162 | } | |
1163 | } | |
1164 | ||
1165 | $cfg->{$default}->{default} = 1 if $default; | |
1166 | ||
1167 | # add default domains | |
1168 | ||
1169 | $cfg->{pve} = { | |
1170 | type => 'builtin', | |
1171 | comment => "Proxmox VE authentication server", | |
1172 | }; | |
1173 | ||
1174 | $cfg->{pam} = { | |
1175 | type => 'builtin', | |
1176 | comment => "Linux PAM standard authentication", | |
1177 | }; | |
1178 | ||
1179 | return $cfg; | |
1180 | } | |
1181 | ||
1182 | sub write_shadow_config { | |
1183 | my ($filename, $cfg) = @_; | |
1184 | ||
1185 | my $data = ''; | |
1186 | foreach my $userid (keys %{$cfg->{users}}) { | |
1187 | my $crypt_pass = $cfg->{users}->{$userid}->{shadow}; | |
1188 | $data .= "$userid:$crypt_pass:\n"; | |
1189 | } | |
1190 | ||
1191 | return $data | |
1192 | } | |
1193 | ||
1194 | sub write_user_config { | |
1195 | my ($filename, $cfg) = @_; | |
1196 | ||
1197 | my $data = ''; | |
1198 | ||
1199 | foreach my $user (keys %{$cfg->{users}}) { | |
2c3a6c0a DM |
1200 | my $d = $cfg->{users}->{$user}; |
1201 | my $firstname = $d->{firstname} ? PVE::Tools::encode_text($d->{firstname}) : ''; | |
1202 | my $lastname = $d->{lastname} ? PVE::Tools::encode_text($d->{lastname}) : ''; | |
1203 | my $email = $d->{email} || ''; | |
1204 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
5eabc984 | 1205 | my $expire = int($d->{expire} || 0); |
2c3a6c0a DM |
1206 | my $enable = $d->{enable} ? 1 : 0; |
1207 | $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:\n"; | |
1208 | } | |
1209 | ||
1210 | $data .= "\n"; | |
1211 | ||
1212 | foreach my $group (keys %{$cfg->{groups}}) { | |
1213 | my $d = $cfg->{groups}->{$group}; | |
1214 | my $list = join (',', keys %{$d->{users}}); | |
1215 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
1216 | $data .= "group:$group:$list:$comment:\n"; | |
1217 | } | |
1218 | ||
1219 | $data .= "\n"; | |
1220 | ||
39c85db8 DM |
1221 | foreach my $pool (keys %{$cfg->{pools}}) { |
1222 | my $d = $cfg->{pools}->{$pool}; | |
4bc17477 DM |
1223 | my $vmlist = join (',', keys %{$d->{vms}}); |
1224 | my $storelist = join (',', keys %{$d->{storage}}); | |
1225 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
39c85db8 | 1226 | $data .= "pool:$pool:$comment:$vmlist:$storelist:\n"; |
4bc17477 DM |
1227 | } |
1228 | ||
1229 | $data .= "\n"; | |
1230 | ||
2c3a6c0a DM |
1231 | foreach my $role (keys %{$cfg->{roles}}) { |
1232 | next if $special_roles->{$role}; | |
1233 | ||
1234 | my $d = $cfg->{roles}->{$role}; | |
1235 | my $list = join (',', keys %$d); | |
1236 | $data .= "role:$role:$list:\n"; | |
1237 | } | |
1238 | ||
1239 | $data .= "\n"; | |
1240 | ||
1241 | foreach my $path (sort keys %{$cfg->{acl}}) { | |
1242 | my $d = $cfg->{acl}->{$path}; | |
1243 | ||
1244 | my $ra = {}; | |
1245 | ||
1246 | foreach my $group (keys %{$d->{groups}}) { | |
1247 | my $l0 = ''; | |
1248 | my $l1 = ''; | |
1249 | foreach my $role (sort keys %{$d->{groups}->{$group}}) { | |
1250 | my $propagate = $d->{groups}->{$group}->{$role}; | |
1251 | if ($propagate) { | |
1252 | $l1 .= ',' if $l1; | |
1253 | $l1 .= $role; | |
1254 | } else { | |
1255 | $l0 .= ',' if $l0; | |
1256 | $l0 .= $role; | |
1257 | } | |
1258 | } | |
1259 | $ra->{0}->{$l0}->{"\@$group"} = 1 if $l0; | |
1260 | $ra->{1}->{$l1}->{"\@$group"} = 1 if $l1; | |
1261 | } | |
1262 | ||
1263 | foreach my $user (keys %{$d->{users}}) { | |
1264 | # no need to save, because root is always 'Administartor' | |
1265 | next if $user eq 'root@pam'; | |
1266 | ||
1267 | my $l0 = ''; | |
1268 | my $l1 = ''; | |
1269 | foreach my $role (sort keys %{$d->{users}->{$user}}) { | |
1270 | my $propagate = $d->{users}->{$user}->{$role}; | |
1271 | if ($propagate) { | |
1272 | $l1 .= ',' if $l1; | |
1273 | $l1 .= $role; | |
1274 | } else { | |
1275 | $l0 .= ',' if $l0; | |
1276 | $l0 .= $role; | |
1277 | } | |
1278 | } | |
1279 | $ra->{0}->{$l0}->{$user} = 1 if $l0; | |
1280 | $ra->{1}->{$l1}->{$user} = 1 if $l1; | |
1281 | } | |
1282 | ||
1283 | foreach my $rolelist (sort keys %{$ra->{0}}) { | |
1284 | my $uglist = join (',', keys %{$ra->{0}->{$rolelist}}); | |
1285 | $data .= "acl:0:$path:$uglist:$rolelist:\n"; | |
1286 | } | |
1287 | foreach my $rolelist (sort keys %{$ra->{1}}) { | |
1288 | my $uglist = join (',', keys %{$ra->{1}->{$rolelist}}); | |
1289 | $data .= "acl:1:$path:$uglist:$rolelist:\n"; | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | return $data; | |
1294 | } | |
1295 | ||
1296 | sub roles { | |
1297 | my ($cfg, $user, $path) = @_; | |
1298 | ||
4bc17477 DM |
1299 | # NOTE: we do not consider pools here. |
1300 | # You need to use $rpcenv->roles() instead if you want that. | |
1301 | ||
2c3a6c0a DM |
1302 | return 'Administrator' if $user eq 'root@pam'; # root can do anything |
1303 | ||
1304 | my $perm = {}; | |
1305 | ||
1306 | foreach my $p (sort keys %{$cfg->{acl}}) { | |
1307 | my $final = ($path eq $p); | |
1308 | ||
1309 | next if !(($p eq '/') || $final || ($path =~ m|^$p/|)); | |
1310 | ||
1311 | my $acl = $cfg->{acl}->{$p}; | |
1312 | ||
1313 | #print "CHECKACL $path $p\n"; | |
1314 | #print "ACL $path = " . Dumper ($acl); | |
1315 | ||
1316 | if (my $ri = $acl->{users}->{$user}) { | |
1317 | my $new; | |
1318 | foreach my $role (keys %$ri) { | |
1319 | my $propagate = $ri->{$role}; | |
1320 | if ($final || $propagate) { | |
1321 | #print "APPLY ROLE $p $user $role\n"; | |
1322 | $new = {} if !$new; | |
1323 | $new->{$role} = 1; | |
1324 | } | |
1325 | } | |
1326 | if ($new) { | |
1327 | $perm = $new; # overwrite previous settings | |
1328 | next; # user privs always override group privs | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | my $new; | |
1333 | foreach my $g (keys %{$acl->{groups}}) { | |
1334 | next if !$cfg->{groups}->{$g}->{users}->{$user}; | |
1335 | if (my $ri = $acl->{groups}->{$g}) { | |
1336 | foreach my $role (keys %$ri) { | |
1337 | my $propagate = $ri->{$role}; | |
1338 | if ($final || $propagate) { | |
1339 | #print "APPLY ROLE $p \@$g $role\n"; | |
1340 | $new = {} if !$new; | |
1341 | $new->{$role} = 1; | |
1342 | } | |
1343 | } | |
1344 | } | |
1345 | } | |
1346 | if ($new) { | |
1347 | $perm = $new; # overwrite previous settings | |
1348 | next; | |
1349 | } | |
2c3a6c0a DM |
1350 | } |
1351 | ||
4bc17477 DM |
1352 | return ('NoAccess') if defined ($perm->{NoAccess}); |
1353 | #return () if defined ($perm->{NoAccess}); | |
2c3a6c0a DM |
1354 | |
1355 | #print "permission $user $path = " . Dumper ($perm); | |
1356 | ||
4bc17477 | 1357 | my @ra = keys %$perm; |
2c3a6c0a DM |
1358 | |
1359 | #print "roles $user $path = " . join (',', @ra) . "\n"; | |
1360 | ||
1361 | return @ra; | |
1362 | } | |
1363 | ||
1364 | sub permission { | |
1365 | my ($cfg, $user, $path) = @_; | |
1366 | ||
1367 | $user = verify_username($user, 1); | |
1368 | return {} if !$user; | |
1369 | ||
1370 | my @ra = roles($cfg, $user, $path); | |
1371 | ||
1372 | my $privs = {}; | |
1373 | ||
1374 | foreach my $role (@ra) { | |
1375 | if (my $privset = $cfg->{roles}->{$role}) { | |
1376 | foreach my $p (keys %$privset) { | |
1377 | $privs->{$p} = 1; | |
1378 | } | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | #print "priviledges $user $path = " . Dumper ($privs); | |
1383 | ||
1384 | return $privs; | |
1385 | } | |
1386 | ||
1387 | sub check_permissions { | |
1388 | my ($username, $path, $privlist) = @_; | |
1389 | ||
1390 | $path = normalize_path($path); | |
1391 | my $usercfg = cfs_read_file('user.cfg'); | |
1392 | my $perm = permission($usercfg, $username, $path); | |
1393 | ||
1394 | foreach my $priv (split_list($privlist)) { | |
1395 | return undef if !$perm->{$priv}; | |
1396 | }; | |
1397 | ||
1398 | return 1; | |
1399 | } | |
1400 | ||
1401 | 1; |