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