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