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