]> git.proxmox.com Git - pve-access-control.git/blob - src/PVE/RPCEnvironment.pm
check_sdn_bridge: check bridge first
[pve-access-control.git] / src / PVE / RPCEnvironment.pm
1 package PVE::RPCEnvironment;
2
3 use strict;
4 use warnings;
5
6 use PVE::AccessControl;
7 use PVE::Cluster;
8 use PVE::Exception qw(raise raise_perm_exc);
9 use PVE::INotify;
10 use PVE::ProcFSTools;
11 use PVE::RESTEnvironment;
12 use PVE::SafeSyslog;
13 use PVE::Tools;
14
15 use base qw(PVE::RESTEnvironment);
16
17 # ACL cache
18
19 my $compile_acl_path = sub {
20 my ($self, $user, $path) = @_;
21
22 my $cfg = $self->{user_cfg};
23
24 return undef if !$cfg->{roles};
25
26 # permissions() has an early return for this case
27 die "internal error" if $user eq 'root@pam';
28
29 my $cache = $self->{aclcache};
30 $cache->{$user} = {} if !$cache->{$user};
31 my $data = $cache->{$user};
32
33 # permissions() will always prime the cache for the owning user
34 my ($username, undef) = PVE::AccessControl::split_tokenid($user, 1);
35 die "internal error" if $username && $username ne 'root@pam' && !defined($cache->{$username});
36
37 # resolve and cache roles of the current user/token for all pool ACL paths
38 if (!$data->{poolroles}) {
39 $data->{poolroles} = {};
40
41 foreach my $pool (keys %{$cfg->{pools}}) {
42 my $d = $cfg->{pools}->{$pool};
43 my $pool_roles = PVE::AccessControl::roles($cfg, $user, "/pool/$pool"); # pool roles
44 next if !scalar(keys %$pool_roles);
45 foreach my $vmid (keys %{$d->{vms}}) {
46 for my $role (keys %$pool_roles) {
47 $data->{poolroles}->{"/vms/$vmid"}->{$role} = 1;
48 }
49 }
50 foreach my $storeid (keys %{$d->{storage}}) {
51 for my $role (keys %$pool_roles) {
52 $data->{poolroles}->{"/storage/$storeid"}->{$role} = 1;
53 }
54 }
55 }
56 }
57
58 # get roles of current user/token on checked path - this already handles
59 # propagation and NoAccess along the path
60 #
61 # hash mapping role name to propagation flag value, a key being defined
62 # means the role is set
63 my $roles = PVE::AccessControl::roles($cfg, $user, $path);
64
65 # apply roles inherited from pools
66 if ($data->{poolroles}->{$path}) {
67 # NoAccess must not be trumped by pool ACLs
68 if (!defined($roles->{NoAccess})) {
69 if ($data->{poolroles}->{$path}->{NoAccess}) {
70 # but pool ACL NoAccess trumps regular ACL
71 $roles = { 'NoAccess' => 0 };
72 } else {
73 foreach my $role (keys %{$data->{poolroles}->{$path}}) {
74 # only use role from pool ACL if regular ACL didn't already
75 # set it, and never set propagation for pool-derived ACLs
76 $roles->{$role} = 0 if !defined($roles->{$role});
77 }
78 }
79 }
80 }
81
82 # cache roles
83 $data->{roles}->{$path} = $roles;
84
85 # derive privs from set roles - hash mapping privilege name to propagation
86 # flag value, a key being defined means the priv is set
87 my $privs = {};
88 foreach my $role (keys %$roles) {
89 if (my $privset = $cfg->{roles}->{$role}) {
90 foreach my $p (keys %$privset) {
91 # set priv '$p' to propagated iff any of the set roles
92 # containing it have the propagated flag set
93 $privs->{$p} ||= $roles->{$role};
94 }
95 }
96 }
97
98 # intersect user and token permissions
99 if ($username && $username ne 'root@pam') {
100 # map of set privs to their propagation flag value, for the owning user
101 my $user_privs = $cache->{$username}->{privs}->{$path};
102 # list of privs set both for token and owning user
103 my $filtered_privs = [ grep { defined($user_privs->{$_}) } keys %$privs ];
104 # intersection of privs using filtered list, combining both propagation
105 # flags
106 $privs = { map { $_ => $user_privs->{$_} && $privs->{$_} } @$filtered_privs };
107 }
108
109 foreach my $priv (keys %$privs) {
110 # safeguard, this should never happen anyway
111 delete $privs->{$priv} if !defined($privs->{$priv});
112 }
113
114 # cache privs
115 $data->{privs}->{$path} = $privs;
116
117 return $privs;
118 };
119
120 # this is the method used by permission check helpers below
121 #
122 # returned value is a hash mapping all set privileges on $path to their
123 # respective propagation flag. the propagation flag is informational only -
124 # actual propagation is handled in PVE::AccessControl::roles(). to determine
125 # whether a privilege is set, check for definedness in the returned hash.
126 #
127 # compiled ACLs are cached, so repeated checks for the same path and user are
128 # almost free.
129 #
130 # if $user is a tokenid, permissions are calculated depending on the
131 # privilege-separation flag value:
132 # - non-priv-separated: permissions for owning user are returned
133 # - priv-separated: permissions for owning user are calculated and intersected
134 # with those of token
135 sub permissions {
136 my ($self, $user, $path) = @_;
137
138 if ($user eq 'root@pam') { # root can do anything
139 my $cfg = $self->{user_cfg};
140 return { map { $_ => 1 } keys %{$cfg->{roles}->{'Administrator'}} };
141 }
142
143 if (!defined($path)) {
144 # this shouldn't happen!
145 warn "internal error: ACL check called for undefined ACL path!\n";
146 return {};
147 }
148
149 if (PVE::AccessControl::pve_verify_tokenid($user, 1)) {
150 my ($username, $token) = PVE::AccessControl::split_tokenid($user);
151 my $cfg = $self->{user_cfg};
152 my $token_info = $cfg->{users}->{$username}->{tokens}->{$token};
153
154 return {} if !$token_info;
155
156 # ensure cache for user is populated
157 my $user_perms = $self->permissions($username, $path);
158
159 # return user privs for non-privsep tokens
160 return $user_perms if !$token_info->{privsep};
161 } else {
162 $user = PVE::AccessControl::verify_username($user, 1);
163 return {} if !$user;
164 }
165
166 my $cache = $self->{aclcache};
167 $cache->{$user} = {} if !$cache->{$user};
168
169 my $acl = $cache->{$user};
170
171 my $perm = $acl->{privs}->{$path};
172 return $perm if $perm;
173
174 return &$compile_acl_path($self, $user, $path);
175 }
176
177 sub compute_api_permission {
178 my ($self, $authuser) = @_;
179
180 my $usercfg = $self->{user_cfg};
181
182 my $res = {};
183 my $priv_re_map = {
184 vms => qr/VM\.|Permissions\.Modify/,
185 access => qr/(User|Group)\.|Permissions\.Modify/,
186 storage => qr/Datastore\.|Permissions\.Modify/,
187 nodes => qr/Sys\.|Permissions\.Modify/,
188 sdn => qr/SDN\.|Permissions\.Modify/,
189 dc => qr/Sys\.Audit|Sys\.Modify|SDN\./,
190 };
191 map { $res->{$_} = {} } keys %$priv_re_map;
192
193 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage', '/sdn'];
194 my $defined_paths = [];
195 PVE::AccessControl::iterate_acl_tree("/", $usercfg->{acl_root}, sub {
196 my ($path, $node) = @_;
197 push @$defined_paths, $path;
198 });
199
200 my $checked_paths = {};
201 foreach my $path (@$required_paths, @$defined_paths) {
202 next if $checked_paths->{$path};
203 $checked_paths->{$path} = 1;
204
205 my $path_perm = $self->permissions($authuser, $path);
206
207 my $toplevel = ($path =~ /^\/(\w+)/) ? $1 : 'dc';
208 if ($toplevel eq 'pool') {
209 foreach my $priv (keys %$path_perm) {
210 next if !defined($path_perm->{$priv});
211
212 if ($priv =~ m/^VM\./) {
213 $res->{vms}->{$priv} = 1;
214 } elsif ($priv =~ m/^Datastore\./) {
215 $res->{storage}->{$priv} = 1;
216 } elsif ($priv eq 'Permissions.Modify') {
217 $res->{storage}->{$priv} = 1;
218 $res->{vms}->{$priv} = 1;
219 }
220 }
221 } else {
222 my $priv_regex = $priv_re_map->{$toplevel} // next;
223 foreach my $priv (keys %$path_perm) {
224 next if !defined($path_perm->{$priv});
225
226 next if $priv !~ m/^($priv_regex)/;
227 $res->{$toplevel}->{$priv} = 1;
228 }
229 }
230 }
231
232 return $res;
233 }
234
235 sub get_effective_permissions {
236 my ($self, $user) = @_;
237
238 # default / top level paths
239 my $paths = {
240 '/' => 1,
241 '/access' => 1,
242 '/access/groups' => 1,
243 '/nodes' => 1,
244 '/pools' => 1,
245 '/sdn' => 1,
246 '/storage' => 1,
247 '/vms' => 1,
248 };
249
250 my $cfg = $self->{user_cfg};
251
252 # paths explicitly listed in ACLs
253 PVE::AccessControl::iterate_acl_tree("/", $cfg->{acl_root}, sub {
254 my ($path, $node) = @_;
255 $paths->{$path} = 1;
256 });
257
258 # paths referenced by pool definitions
259 foreach my $pool (keys %{$cfg->{pools}}) {
260 my $d = $cfg->{pools}->{$pool};
261 foreach my $vmid (keys %{$d->{vms}}) {
262 $paths->{"/vms/$vmid"} = 1;
263 }
264 foreach my $storeid (keys %{$d->{storage}}) {
265 $paths->{"/storage/$storeid"} = 1;
266 }
267 }
268
269 my $perms = {};
270 foreach my $path (keys %$paths) {
271 my $path_perms = $self->permissions($user, $path);
272 foreach my $priv (keys %$path_perms) {
273 delete $path_perms->{$priv} if !defined($path_perms->{$priv});
274 }
275 # filter paths where user has NO permissions
276 $perms->{$path} = $path_perms if %$path_perms;
277 }
278 return $perms;
279 }
280
281 sub check {
282 my ($self, $user, $path, $privs, $noerr) = @_;
283
284 my $perm = $self->permissions($user, $path);
285
286 foreach my $priv (@$privs) {
287 PVE::AccessControl::verify_privname($priv);
288 if (!defined($perm->{$priv})) {
289 return undef if $noerr;
290 raise_perm_exc("$path, $priv");
291 }
292 };
293
294 return 1;
295 };
296
297 sub check_any {
298 my ($self, $user, $path, $privs, $noerr) = @_;
299
300 my $perm = $self->permissions($user, $path);
301
302 my $found = 0;
303 foreach my $priv (@$privs) {
304 PVE::AccessControl::verify_privname($priv);
305 if (defined($perm->{$priv})) {
306 $found = 1;
307 last;
308 }
309 };
310
311 return 1 if $found;
312
313 return undef if $noerr;
314
315 raise_perm_exc("$path, " . join("|", @$privs));
316 };
317
318 sub check_full {
319 my ($self, $username, $path, $privs, $any, $noerr) = @_;
320 if ($any) {
321 return $self->check_any($username, $path, $privs, $noerr);
322 } else {
323 return $self->check($username, $path, $privs, $noerr);
324 }
325 }
326
327 # check for any fashion of access to vnet/bridge
328 sub check_sdn_bridge {
329 my ($self, $username, $zone, $bridge, $privs, $noerr) = @_;
330
331 my $path = "/sdn/zones/$zone/$bridge";
332 # check access to bridge itself
333 return 1 if $self->check_any($username, $path, $privs, 1);
334
335 my $cfg = $self->{user_cfg};
336 my $bridge_acl = PVE::AccessControl::find_acl_tree_node($cfg->{acl_root}, $path);
337 if ($bridge_acl) {
338 # check access to VLANs
339 my $vlans = $bridge_acl->{children};
340 for my $vlan (keys %$vlans) {
341 my $vlanpath = "$path/$vlan";
342 return 1 if $self->check_any($username, $vlanpath, $privs, 1);
343 }
344 }
345
346 # repeat check, but fatal
347 $self->check_any($username, $path, $privs, 0) if !$noerr;
348
349 return;
350 }
351
352 sub check_user_enabled {
353 my ($self, $user, $noerr) = @_;
354
355 my $cfg = $self->{user_cfg};
356 return PVE::AccessControl::check_user_enabled($cfg, $user, $noerr);
357 }
358
359 sub check_user_exist {
360 my ($self, $user, $noerr) = @_;
361
362 my $cfg = $self->{user_cfg};
363 return PVE::AccessControl::check_user_exist($cfg, $user, $noerr);
364 }
365
366 sub check_pool_exist {
367 my ($self, $pool, $noerr) = @_;
368
369 my $cfg = $self->{user_cfg};
370
371 return 1 if $cfg->{pools}->{$pool};
372
373 return undef if $noerr;
374
375 raise_perm_exc("pool '$pool' does not exist");
376 }
377
378 sub check_vm_perm {
379 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
380
381 my $cfg = $self->{user_cfg};
382
383 if ($pool) {
384 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
385 }
386 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
387 };
388
389 sub is_group_member {
390 my ($self, $group, $user) = @_;
391
392 my $cfg = $self->{user_cfg};
393
394 return 0 if !$cfg->{groups}->{$group};
395
396 return defined($cfg->{groups}->{$group}->{users}->{$user});
397 }
398
399 sub filter_groups {
400 my ($self, $user, $privs, $any) = @_;
401
402 my $cfg = $self->{user_cfg};
403
404 my $groups = {};
405 foreach my $group (keys %{$cfg->{groups}}) {
406 my $path = "/access/groups/$group";
407 if ($self->check_full($user, $path, $privs, $any, 1)) {
408 $groups->{$group} = $cfg->{groups}->{$group};
409 }
410 }
411
412 return $groups;
413 }
414
415 sub group_member_join {
416 my ($self, $grouplist) = @_;
417
418 my $users = {};
419
420 my $cfg = $self->{user_cfg};
421 foreach my $group (@$grouplist) {
422 my $data = $cfg->{groups}->{$group};
423 next if !$data;
424 foreach my $user (keys %{$data->{users}}) {
425 $users->{$user} = 1;
426 }
427 }
428
429 return $users;
430 }
431
432 sub check_perm_modify {
433 my ($self, $username, $path, $noerr) = @_;
434
435 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
436
437 my $testperms = [ 'Permissions.Modify' ];
438 if ($path =~ m|^/storage/.+$|) {
439 push @$testperms, 'Datastore.Allocate';
440 } elsif ($path =~ m|^/vms/.+$|) {
441 push @$testperms, 'VM.Allocate';
442 } elsif ($path =~ m|^/pool/.+$|) {
443 push @$testperms, 'Pool.Allocate';
444 }
445
446 return $self->check_any($username, $path, $testperms, $noerr);
447 }
448
449 sub exec_api2_perm_check {
450 my ($self, $check, $username, $param, $noerr) = @_;
451
452 # syslog("info", "CHECK " . join(', ', @$check));
453
454 my $ind = 0;
455 my $test = $check->[$ind++];
456 die "no permission test specified" if !$test;
457
458 if ($test eq 'and') {
459 while (my $subcheck = $check->[$ind++]) {
460 $self->exec_api2_perm_check($subcheck, $username, $param);
461 }
462 return 1;
463 } elsif ($test eq 'or') {
464 while (my $subcheck = $check->[$ind++]) {
465 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
466 }
467 return 0 if $noerr;
468 raise_perm_exc();
469 } elsif ($test eq 'perm') {
470 my ($t, $tmplpath, $privs, %options) = @$check;
471 my $any = $options{any};
472 die "missing parameters" if !($tmplpath && $privs);
473 my $require_param = $options{require_param};
474 if ($require_param && !defined($param->{$require_param})) {
475 return 0 if $noerr;
476 raise_perm_exc();
477 }
478 my $path = PVE::Tools::template_replace($tmplpath, $param);
479 my $normpath = PVE::AccessControl::normalize_path($path);
480 warn "Failed to normalize '$path'\n" if !defined($normpath) && defined($path);
481
482 return $self->check_full($username, $normpath, $privs, $any, $noerr);
483 } elsif ($test eq 'userid-group') {
484 my $userid = $param->{userid};
485 my ($t, $privs, %options) = @$check;
486
487 my $check_existing_user = !$options{groups_param} || $options{groups_param} ne 'create';
488 return 0 if $check_existing_user && !$self->check_user_exist($userid, $noerr);
489
490 # check permission for ALL groups (and thus ALL users)
491 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
492 # list of groups $username has any of $privs on
493 my $groups = $self->filter_groups($username, $privs, 1);
494 if ($options{groups_param}) {
495 # does $username have any of $privs on all new/updated/.. groups?
496 my @group_param = PVE::Tools::split_list($param->{groups});
497 raise_perm_exc("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
498 foreach my $pg (@group_param) {
499 raise_perm_exc("/access/groups/$pg, " . join("|", @$privs))
500 if !$groups->{$pg};
501 }
502 }
503 if ($check_existing_user) {
504 # does $username have any of $privs on any existing group of $userid
505 my $allowed_users = $self->group_member_join([keys %$groups]);
506 if (!$allowed_users->{$userid}) {
507 return 0 if $noerr;
508 raise_perm_exc();
509 }
510 }
511 }
512 return 1;
513 } elsif ($test eq 'userid-param') {
514 my ($userid, undef, $realm) = PVE::AccessControl::verify_username($param->{userid});
515 my ($t, $subtest) = @$check;
516 die "missing parameters" if !$subtest;
517 if ($subtest eq 'self') {
518 return 0 if !$self->check_user_exist($userid, $noerr);
519 return 1 if $username eq $userid;
520 return 0 if $noerr;
521 raise_perm_exc();
522 } elsif ($subtest eq 'Realm.AllocateUser') {
523 my $path = "/access/realm/$realm";
524 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
525 } else {
526 die "unknown userid-param test";
527 }
528 } elsif ($test eq 'perm-modify') {
529 my ($t, $tmplpath) = @$check;
530 my $path = PVE::Tools::template_replace($tmplpath, $param);
531 $path = PVE::AccessControl::normalize_path($path);
532 return 0 if !defined($path); # should already die in API2::ACL
533 return $self->check_perm_modify($username, $path, $noerr);
534 } else {
535 die "unknown permission test";
536 }
537 };
538
539 sub check_api2_permissions {
540 my ($self, $perm, $username, $param) = @_;
541
542 return 1 if !$username && $perm->{user} && $perm->{user} eq 'world';
543
544 raise_perm_exc("user != null") if !$username;
545
546 return 1 if $username eq 'root@pam';
547
548 raise_perm_exc('user != root@pam') if !$perm;
549
550 return 1 if $perm->{user} && $perm->{user} eq 'all';
551
552 return $self->exec_api2_perm_check($perm->{check}, $username, $param)
553 if $perm->{check};
554
555 raise_perm_exc();
556 }
557
558 sub log_cluster_msg {
559 my ($self, $pri, $user, $msg) = @_;
560
561 PVE::Cluster::log_msg($pri, $user, $msg);
562 }
563
564 sub broadcast_tasklist {
565 my ($self, $tlist) = @_;
566
567 PVE::Cluster::broadcast_tasklist($tlist);
568 }
569
570 # initialize environment - must be called once at program startup
571 sub init {
572 my ($class, $type, %params) = @_;
573
574 $class = ref($class) || $class;
575
576 my $self = $class->SUPER::init($type, %params);
577
578 $self->{user_cfg} = {};
579 $self->{aclcache} = {};
580 $self->{aclversion} = undef;
581
582 return $self;
583 };
584
585
586 # init_request - must be called before each RPC request
587 sub init_request {
588 my ($self, %params) = @_;
589
590 PVE::Cluster::cfs_update();
591
592 $self->{result_attributes} = {};
593
594 my $userconfig; # we use this for regression tests
595 foreach my $p (keys %params) {
596 if ($p eq 'userconfig') {
597 $userconfig = $params{$p};
598 } else {
599 die "unknown parameter '$p'";
600 }
601 }
602
603 eval {
604 $self->{aclcache} = {};
605 if ($userconfig) {
606 my $ucdata = PVE::Tools::file_get_contents($userconfig);
607 my $cfg = PVE::AccessControl::parse_user_config($userconfig, $ucdata);
608 $self->{user_cfg} = $cfg;
609 } else {
610 my $ucvers = PVE::Cluster::cfs_file_version('user.cfg');
611 if (!$self->{aclcache} || !defined($self->{aclversion}) ||
612 !defined($ucvers) || ($ucvers ne $self->{aclversion})) {
613 $self->{aclversion} = $ucvers;
614 my $cfg = PVE::Cluster::cfs_read_file('user.cfg');
615 $self->{user_cfg} = $cfg;
616 }
617 }
618 };
619 if (my $err = $@) {
620 $self->{user_cfg} = {};
621 die "Unable to load access control list: $err";
622 }
623 }
624
625 # hacks: to provide better backwards compatibility
626
627 # old code uses PVE::RPCEnvironment::get();
628 # new code should use PVE::RPCEnvironment->get();
629 sub get {
630 return PVE::RESTEnvironment->get();
631 }
632
633 # old code uses PVE::RPCEnvironment::is_worker();
634 # new code should use PVE::RPCEnvironment->is_worker();
635 sub is_worker {
636 return PVE::RESTEnvironment->is_worker();
637 }
638
639 1;