1 package PVE
::RPCEnvironment
;
6 use PVE
::AccessControl
;
8 use PVE
::Exception
qw(raise raise_perm_exc);
11 use PVE
::RESTEnvironment
;
15 use base
qw(PVE::RESTEnvironment);
19 my $compile_acl_path = sub {
20 my ($self, $user, $path) = @_;
22 my $cfg = $self->{user_cfg
};
24 return undef if !$cfg->{roles
};
26 # permissions() has an early return for this case
27 die "internal error" if $user eq 'root@pam';
29 my $cache = $self->{aclcache
};
30 $cache->{$user} = {} if !$cache->{$user};
31 my $data = $cache->{$user};
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});
37 # resolve and cache roles of the current user/token for all pool ACL paths
38 if (!$data->{poolroles
}) {
39 $data->{poolroles
} = {};
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;
50 foreach my $storeid (keys %{$d->{storage
}}) {
51 for my $role (keys %$pool_roles) {
52 $data->{poolroles
}->{"/storage/$storeid"}->{$role} = 1;
58 # get roles of current user/token on checked path - this already handles
59 # propagation and NoAccess along the path
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);
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 };
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});
83 $data->{roles
}->{$path} = $roles;
85 # derive privs from set roles - hash mapping privilege name to propagation
86 # flag value, a key being defined means the priv is set
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};
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
106 $privs = { map { $_ => $user_privs->{$_} && $privs->{$_} } @$filtered_privs };
109 foreach my $priv (keys %$privs) {
110 # safeguard, this should never happen anyway
111 delete $privs->{$priv} if !defined($privs->{$priv});
115 $data->{privs
}->{$path} = $privs;
120 # this is the method used by permission check helpers below
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.
127 # compiled ACLs are cached, so repeated checks for the same path and user are
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
136 my ($self, $user, $path) = @_;
138 if ($user eq 'root@pam') { # root can do anything
139 my $cfg = $self->{user_cfg
};
140 return { map { $_ => 1 } keys %{$cfg->{roles
}->{'Administrator'}} };
143 if (!defined($path)) {
144 # this shouldn't happen!
145 warn "internal error: ACL check called for undefined ACL path!\n";
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};
154 return {} if !$token_info;
156 # ensure cache for user is populated
157 my $user_perms = $self->permissions($username, $path);
159 # return user privs for non-privsep tokens
160 return $user_perms if !$token_info->{privsep
};
162 $user = PVE
::AccessControl
::verify_username
($user, 1);
166 my $cache = $self->{aclcache
};
167 $cache->{$user} = {} if !$cache->{$user};
169 my $acl = $cache->{$user};
171 my $perm = $acl->{privs
}->{$path};
172 return $perm if $perm;
174 return &$compile_acl_path($self, $user, $path);
177 sub compute_api_permission
{
178 my ($self, $authuser) = @_;
180 my $usercfg = $self->{user_cfg
};
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\./,
191 map { $res->{$_} = {} } keys %$priv_re_map;
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;
200 my $checked_paths = {};
201 foreach my $path (@$required_paths, @$defined_paths) {
202 next if $checked_paths->{$path};
203 $checked_paths->{$path} = 1;
205 my $path_perm = $self->permissions($authuser, $path);
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});
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;
222 my $priv_regex = $priv_re_map->{$toplevel} // next;
223 foreach my $priv (keys %$path_perm) {
224 next if !defined($path_perm->{$priv});
226 next if $priv !~ m/^($priv_regex)/;
227 $res->{$toplevel}->{$priv} = 1;
235 sub get_effective_permissions
{
236 my ($self, $user) = @_;
238 # default / top level paths
242 '/access/groups' => 1,
250 my $cfg = $self->{user_cfg
};
252 # paths explicitly listed in ACLs
253 PVE
::AccessControl
::iterate_acl_tree
("/", $cfg->{acl_root
}, sub {
254 my ($path, $node) = @_;
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;
264 foreach my $storeid (keys %{$d->{storage
}}) {
265 $paths->{"/storage/$storeid"} = 1;
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});
275 # filter paths where user has NO permissions
276 $perms->{$path} = $path_perms if %$path_perms;
282 my ($self, $user, $path, $privs, $noerr) = @_;
284 my $perm = $self->permissions($user, $path);
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");
298 my ($self, $user, $path, $privs, $noerr) = @_;
300 my $perm = $self->permissions($user, $path);
303 foreach my $priv (@$privs) {
304 PVE
::AccessControl
::verify_privname
($priv);
305 if (defined($perm->{$priv})) {
313 return undef if $noerr;
315 raise_perm_exc
("$path, " . join("|", @$privs));
319 my ($self, $username, $path, $privs, $any, $noerr) = @_;
321 return $self->check_any($username, $path, $privs, $noerr);
323 return $self->check($username, $path, $privs, $noerr);
327 sub check_user_enabled
{
328 my ($self, $user, $noerr) = @_;
330 my $cfg = $self->{user_cfg
};
331 return PVE
::AccessControl
::check_user_enabled
($cfg, $user, $noerr);
334 sub check_user_exist
{
335 my ($self, $user, $noerr) = @_;
337 my $cfg = $self->{user_cfg
};
338 return PVE
::AccessControl
::check_user_exist
($cfg, $user, $noerr);
341 sub check_pool_exist
{
342 my ($self, $pool, $noerr) = @_;
344 my $cfg = $self->{user_cfg
};
346 return 1 if $cfg->{pools
}->{$pool};
348 return undef if $noerr;
350 raise_perm_exc
("pool '$pool' does not exist");
354 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
356 my $cfg = $self->{user_cfg
};
359 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
361 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
364 sub is_group_member
{
365 my ($self, $group, $user) = @_;
367 my $cfg = $self->{user_cfg
};
369 return 0 if !$cfg->{groups
}->{$group};
371 return defined($cfg->{groups
}->{$group}->{users
}->{$user});
375 my ($self, $user, $privs, $any) = @_;
377 my $cfg = $self->{user_cfg
};
380 foreach my $group (keys %{$cfg->{groups
}}) {
381 my $path = "/access/groups/$group";
382 if ($self->check_full($user, $path, $privs, $any, 1)) {
383 $groups->{$group} = $cfg->{groups
}->{$group};
390 sub group_member_join
{
391 my ($self, $grouplist) = @_;
395 my $cfg = $self->{user_cfg
};
396 foreach my $group (@$grouplist) {
397 my $data = $cfg->{groups
}->{$group};
399 foreach my $user (keys %{$data->{users
}}) {
407 sub check_perm_modify
{
408 my ($self, $username, $path, $noerr) = @_;
410 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
412 my $testperms = [ 'Permissions.Modify' ];
413 if ($path =~ m
|^/storage/.+$|) {
414 push @$testperms, 'Datastore.Allocate';
415 } elsif ($path =~ m
|^/vms/.+$|) {
416 push @$testperms, 'VM.Allocate';
417 } elsif ($path =~ m
|^/pool/.+$|) {
418 push @$testperms, 'Pool.Allocate';
421 return $self->check_any($username, $path, $testperms, $noerr);
424 sub exec_api2_perm_check
{
425 my ($self, $check, $username, $param, $noerr) = @_;
427 # syslog("info", "CHECK " . join(', ', @$check));
430 my $test = $check->[$ind++];
431 die "no permission test specified" if !$test;
433 if ($test eq 'and') {
434 while (my $subcheck = $check->[$ind++]) {
435 $self->exec_api2_perm_check($subcheck, $username, $param);
438 } elsif ($test eq 'or') {
439 while (my $subcheck = $check->[$ind++]) {
440 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
444 } elsif ($test eq 'perm') {
445 my ($t, $tmplpath, $privs, %options) = @$check;
446 my $any = $options{any
};
447 die "missing parameters" if !($tmplpath && $privs);
448 my $require_param = $options{require_param
};
449 if ($require_param && !defined($param->{$require_param})) {
453 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
454 my $normpath = PVE
::AccessControl
::normalize_path
($path);
455 warn "Failed to normalize '$path'\n" if !defined($normpath) && defined($path);
457 return $self->check_full($username, $normpath, $privs, $any, $noerr);
458 } elsif ($test eq 'userid-group') {
459 my $userid = $param->{userid
};
460 my ($t, $privs, %options) = @$check;
462 my $check_existing_user = !$options{groups_param
} || $options{groups_param
} ne 'create';
463 return 0 if $check_existing_user && !$self->check_user_exist($userid, $noerr);
465 # check permission for ALL groups (and thus ALL users)
466 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
467 # list of groups $username has any of $privs on
468 my $groups = $self->filter_groups($username, $privs, 1);
469 if ($options{groups_param
}) {
470 # does $username have any of $privs on all new/updated/.. groups?
471 my @group_param = PVE
::Tools
::split_list
($param->{groups
});
472 raise_perm_exc
("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
473 foreach my $pg (@group_param) {
474 raise_perm_exc
("/access/groups/$pg, " . join("|", @$privs))
478 if ($check_existing_user) {
479 # does $username have any of $privs on any existing group of $userid
480 my $allowed_users = $self->group_member_join([keys %$groups]);
481 if (!$allowed_users->{$userid}) {
488 } elsif ($test eq 'userid-param') {
489 my ($userid, undef, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
490 my ($t, $subtest) = @$check;
491 die "missing parameters" if !$subtest;
492 if ($subtest eq 'self') {
493 return 0 if !$self->check_user_exist($userid, $noerr);
494 return 1 if $username eq $userid;
497 } elsif ($subtest eq 'Realm.AllocateUser') {
498 my $path = "/access/realm/$realm";
499 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
501 die "unknown userid-param test";
503 } elsif ($test eq 'perm-modify') {
504 my ($t, $tmplpath) = @$check;
505 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
506 $path = PVE
::AccessControl
::normalize_path
($path);
507 return 0 if !defined($path); # should already die in API2::ACL
508 return $self->check_perm_modify($username, $path, $noerr);
510 die "unknown permission test";
514 sub check_api2_permissions
{
515 my ($self, $perm, $username, $param) = @_;
517 return 1 if !$username && $perm->{user
} && $perm->{user
} eq 'world';
519 raise_perm_exc
("user != null") if !$username;
521 return 1 if $username eq 'root@pam';
523 raise_perm_exc
('user != root@pam') if !$perm;
525 return 1 if $perm->{user
} && $perm->{user
} eq 'all';
527 return $self->exec_api2_perm_check($perm->{check
}, $username, $param)
533 sub log_cluster_msg
{
534 my ($self, $pri, $user, $msg) = @_;
536 PVE
::Cluster
::log_msg
($pri, $user, $msg);
539 sub broadcast_tasklist
{
540 my ($self, $tlist) = @_;
542 PVE
::Cluster
::broadcast_tasklist
($tlist);
545 # initialize environment - must be called once at program startup
547 my ($class, $type, %params) = @_;
549 $class = ref($class) || $class;
551 my $self = $class->SUPER::init
($type, %params);
553 $self->{user_cfg
} = {};
554 $self->{aclcache
} = {};
555 $self->{aclversion
} = undef;
561 # init_request - must be called before each RPC request
563 my ($self, %params) = @_;
565 PVE
::Cluster
::cfs_update
();
567 $self->{result_attributes
} = {};
569 my $userconfig; # we use this for regression tests
570 foreach my $p (keys %params) {
571 if ($p eq 'userconfig') {
572 $userconfig = $params{$p};
574 die "unknown parameter '$p'";
579 $self->{aclcache
} = {};
581 my $ucdata = PVE
::Tools
::file_get_contents
($userconfig);
582 my $cfg = PVE
::AccessControl
::parse_user_config
($userconfig, $ucdata);
583 $self->{user_cfg
} = $cfg;
585 my $ucvers = PVE
::Cluster
::cfs_file_version
('user.cfg');
586 if (!$self->{aclcache
} || !defined($self->{aclversion
}) ||
587 !defined($ucvers) || ($ucvers ne $self->{aclversion
})) {
588 $self->{aclversion
} = $ucvers;
589 my $cfg = PVE
::Cluster
::cfs_read_file
('user.cfg');
590 $self->{user_cfg
} = $cfg;
595 $self->{user_cfg
} = {};
596 die "Unable to load access control list: $err";
600 # hacks: to provide better backwards compatibility
602 # old code uses PVE::RPCEnvironment::get();
603 # new code should use PVE::RPCEnvironment->get();
605 return PVE
::RESTEnvironment-
>get();
608 # old code uses PVE::RPCEnvironment::is_worker();
609 # new code should use PVE::RPCEnvironment->is_worker();
611 return PVE
::RESTEnvironment-
>is_worker();