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 (PVE
::AccessControl
::pve_verify_tokenid
($user, 1)) {
144 my ($username, $token) = PVE
::AccessControl
::split_tokenid
($user);
145 my $cfg = $self->{user_cfg
};
146 my $token_info = $cfg->{users
}->{$username}->{tokens
}->{$token};
148 return {} if !$token_info;
150 # ensure cache for user is populated
151 my $user_perms = $self->permissions($username, $path);
153 # return user privs for non-privsep tokens
154 return $user_perms if !$token_info->{privsep
};
156 $user = PVE
::AccessControl
::verify_username
($user, 1);
160 my $cache = $self->{aclcache
};
161 $cache->{$user} = {} if !$cache->{$user};
163 my $acl = $cache->{$user};
165 my $perm = $acl->{privs
}->{$path};
166 return $perm if $perm;
168 return &$compile_acl_path($self, $user, $path);
171 sub compute_api_permission
{
172 my ($self, $authuser) = @_;
174 my $usercfg = $self->{user_cfg
};
178 vms
=> qr/VM\.|Permissions\.Modify/,
179 access
=> qr/(User|Group)\.|Permissions\.Modify/,
180 storage
=> qr/Datastore\.|Permissions\.Modify/,
181 nodes
=> qr/Sys\.|Permissions\.Modify/,
182 sdn
=> qr/SDN\.|Permissions\.Modify/,
183 dc
=> qr/Sys\.Audit|SDN\./,
185 map { $res->{$_} = {} } keys %$priv_re_map;
187 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage', '/sdn'];
189 my $checked_paths = {};
190 foreach my $path (@$required_paths, keys %{$usercfg->{acl
}}) {
191 next if $checked_paths->{$path};
192 $checked_paths->{$path} = 1;
194 my $path_perm = $self->permissions($authuser, $path);
196 my $toplevel = ($path =~ /^\/(\w
+)/) ?
$1 : 'dc';
197 if ($toplevel eq 'pool') {
198 foreach my $priv (keys %$path_perm) {
199 next if !defined($path_perm->{$priv});
201 if ($priv =~ m/^VM\./) {
202 $res->{vms
}->{$priv} = 1;
203 } elsif ($priv =~ m/^Datastore\./) {
204 $res->{storage
}->{$priv} = 1;
205 } elsif ($priv eq 'Permissions.Modify') {
206 $res->{storage
}->{$priv} = 1;
207 $res->{vms
}->{$priv} = 1;
211 my $priv_regex = $priv_re_map->{$toplevel} // next;
212 foreach my $priv (keys %$path_perm) {
213 next if !defined($path_perm->{$priv});
215 next if $priv !~ m/^($priv_regex)/;
216 $res->{$toplevel}->{$priv} = 1;
224 sub get_effective_permissions
{
225 my ($self, $user) = @_;
227 # default / top level paths
231 '/access/groups' => 1,
239 my $cfg = $self->{user_cfg
};
241 # paths explicitly listed in ACLs
242 foreach my $acl_path (keys %{$cfg->{acl
}}) {
243 $paths->{$acl_path} = 1;
246 # paths referenced by pool definitions
247 foreach my $pool (keys %{$cfg->{pools
}}) {
248 my $d = $cfg->{pools
}->{$pool};
249 foreach my $vmid (keys %{$d->{vms
}}) {
250 $paths->{"/vms/$vmid"} = 1;
252 foreach my $storeid (keys %{$d->{storage
}}) {
253 $paths->{"/storage/$storeid"} = 1;
258 foreach my $path (keys %$paths) {
259 my $path_perms = $self->permissions($user, $path);
260 foreach my $priv (keys %$path_perms) {
261 delete $path_perms->{$priv} if !defined($path_perms->{$priv});
263 # filter paths where user has NO permissions
264 $perms->{$path} = $path_perms if %$path_perms;
270 my ($self, $user, $path, $privs, $noerr) = @_;
272 my $perm = $self->permissions($user, $path);
274 foreach my $priv (@$privs) {
275 PVE
::AccessControl
::verify_privname
($priv);
276 if (!defined($perm->{$priv})) {
277 return undef if $noerr;
278 raise_perm_exc
("$path, $priv");
286 my ($self, $user, $path, $privs, $noerr) = @_;
288 my $perm = $self->permissions($user, $path);
291 foreach my $priv (@$privs) {
292 PVE
::AccessControl
::verify_privname
($priv);
293 if (defined($perm->{$priv})) {
301 return undef if $noerr;
303 raise_perm_exc
("$path, " . join("|", @$privs));
307 my ($self, $username, $path, $privs, $any, $noerr) = @_;
309 return $self->check_any($username, $path, $privs, $noerr);
311 return $self->check($username, $path, $privs, $noerr);
315 sub check_user_enabled
{
316 my ($self, $user, $noerr) = @_;
318 my $cfg = $self->{user_cfg
};
319 return PVE
::AccessControl
::check_user_enabled
($cfg, $user, $noerr);
322 sub check_user_exist
{
323 my ($self, $user, $noerr) = @_;
325 my $cfg = $self->{user_cfg
};
326 return PVE
::AccessControl
::check_user_exist
($cfg, $user, $noerr);
329 sub check_pool_exist
{
330 my ($self, $pool, $noerr) = @_;
332 my $cfg = $self->{user_cfg
};
334 return 1 if $cfg->{pools
}->{$pool};
336 return undef if $noerr;
338 raise_perm_exc
("pool '$pool' does not exist");
342 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
344 my $cfg = $self->{user_cfg
};
347 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
349 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
352 sub is_group_member
{
353 my ($self, $group, $user) = @_;
355 my $cfg = $self->{user_cfg
};
357 return 0 if !$cfg->{groups
}->{$group};
359 return defined($cfg->{groups
}->{$group}->{users
}->{$user});
363 my ($self, $user, $privs, $any) = @_;
365 my $cfg = $self->{user_cfg
};
368 foreach my $group (keys %{$cfg->{groups
}}) {
369 my $path = "/access/groups/$group";
370 if ($self->check_full($user, $path, $privs, $any, 1)) {
371 $groups->{$group} = $cfg->{groups
}->{$group};
378 sub group_member_join
{
379 my ($self, $grouplist) = @_;
383 my $cfg = $self->{user_cfg
};
384 foreach my $group (@$grouplist) {
385 my $data = $cfg->{groups
}->{$group};
387 foreach my $user (keys %{$data->{users
}}) {
395 sub check_perm_modify
{
396 my ($self, $username, $path, $noerr) = @_;
398 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
400 my $testperms = [ 'Permissions.Modify' ];
401 if ($path =~ m
|^/storage/.+$|) {
402 push @$testperms, 'Datastore.Allocate';
403 } elsif ($path =~ m
|^/vms/.+$|) {
404 push @$testperms, 'VM.Allocate';
405 } elsif ($path =~ m
|^/pool/.+$|) {
406 push @$testperms, 'Pool.Allocate';
409 return $self->check_any($username, $path, $testperms, $noerr);
412 sub exec_api2_perm_check
{
413 my ($self, $check, $username, $param, $noerr) = @_;
415 # syslog("info", "CHECK " . join(', ', @$check));
418 my $test = $check->[$ind++];
419 die "no permission test specified" if !$test;
421 if ($test eq 'and') {
422 while (my $subcheck = $check->[$ind++]) {
423 $self->exec_api2_perm_check($subcheck, $username, $param);
426 } elsif ($test eq 'or') {
427 while (my $subcheck = $check->[$ind++]) {
428 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
432 } elsif ($test eq 'perm') {
433 my ($t, $tmplpath, $privs, %options) = @$check;
434 my $any = $options{any
};
435 die "missing parameters" if !($tmplpath && $privs);
436 my $require_param = $options{require_param
};
437 if ($require_param && !defined($param->{$require_param})) {
441 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
442 $path = PVE
::AccessControl
::normalize_path
($path);
443 return $self->check_full($username, $path, $privs, $any, $noerr);
444 } elsif ($test eq 'userid-group') {
445 my $userid = $param->{userid
};
446 my ($t, $privs, %options) = @$check;
448 my $check_existing_user = !$options{groups_param
} || $options{groups_param
} ne 'create';
449 return 0 if $check_existing_user && !$self->check_user_exist($userid, $noerr);
451 # check permission for ALL groups (and thus ALL users)
452 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
453 # list of groups $username has any of $privs on
454 my $groups = $self->filter_groups($username, $privs, 1);
455 if ($options{groups_param
}) {
456 # does $username have any of $privs on all new/updated/.. groups?
457 my @group_param = PVE
::Tools
::split_list
($param->{groups
});
458 raise_perm_exc
("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
459 foreach my $pg (@group_param) {
460 raise_perm_exc
("/access/groups/$pg, " . join("|", @$privs))
464 if ($check_existing_user) {
465 # does $username have any of $privs on any existing group of $userid
466 my $allowed_users = $self->group_member_join([keys %$groups]);
467 if (!$allowed_users->{$userid}) {
474 } elsif ($test eq 'userid-param') {
475 my ($userid, undef, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
476 my ($t, $subtest) = @$check;
477 die "missing parameters" if !$subtest;
478 if ($subtest eq 'self') {
479 return 0 if !$self->check_user_exist($userid, $noerr);
480 return 1 if $username eq $userid;
483 } elsif ($subtest eq 'Realm.AllocateUser') {
484 my $path = "/access/realm/$realm";
485 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
487 die "unknown userid-param test";
489 } elsif ($test eq 'perm-modify') {
490 my ($t, $tmplpath) = @$check;
491 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
492 $path = PVE
::AccessControl
::normalize_path
($path);
493 return $self->check_perm_modify($username, $path, $noerr);
495 die "unknown permission test";
499 sub check_api2_permissions
{
500 my ($self, $perm, $username, $param) = @_;
502 return 1 if !$username && $perm->{user
} && $perm->{user
} eq 'world';
504 raise_perm_exc
("user != null") if !$username;
506 return 1 if $username eq 'root@pam';
508 raise_perm_exc
('user != root@pam') if !$perm;
510 return 1 if $perm->{user
} && $perm->{user
} eq 'all';
512 return $self->exec_api2_perm_check($perm->{check
}, $username, $param)
518 sub log_cluster_msg
{
519 my ($self, $pri, $user, $msg) = @_;
521 PVE
::Cluster
::log_msg
($pri, $user, $msg);
524 sub broadcast_tasklist
{
525 my ($self, $tlist) = @_;
527 PVE
::Cluster
::broadcast_tasklist
($tlist);
530 # initialize environment - must be called once at program startup
532 my ($class, $type, %params) = @_;
534 $class = ref($class) || $class;
536 my $self = $class->SUPER::init
($type, %params);
538 $self->{user_cfg
} = {};
539 $self->{aclcache
} = {};
540 $self->{aclversion
} = undef;
546 # init_request - must be called before each RPC request
548 my ($self, %params) = @_;
550 PVE
::Cluster
::cfs_update
();
552 $self->{result_attributes
} = {};
554 my $userconfig; # we use this for regression tests
555 foreach my $p (keys %params) {
556 if ($p eq 'userconfig') {
557 $userconfig = $params{$p};
559 die "unknown parameter '$p'";
564 $self->{aclcache
} = {};
566 my $ucdata = PVE
::Tools
::file_get_contents
($userconfig);
567 my $cfg = PVE
::AccessControl
::parse_user_config
($userconfig, $ucdata);
568 $self->{user_cfg
} = $cfg;
570 my $ucvers = PVE
::Cluster
::cfs_file_version
('user.cfg');
571 if (!$self->{aclcache
} || !defined($self->{aclversion
}) ||
572 !defined($ucvers) || ($ucvers ne $self->{aclversion
})) {
573 $self->{aclversion
} = $ucvers;
574 my $cfg = PVE
::Cluster
::cfs_read_file
('user.cfg');
575 $self->{user_cfg
} = $cfg;
580 $self->{user_cfg
} = {};
581 die "Unable to load access control list: $err";
585 # hacks: to provide better backwards compatibiliy
587 # old code uses PVE::RPCEnvironment::get();
588 # new code should use PVE::RPCEnvironment->get();
590 return PVE
::RESTEnvironment-
>get();
593 # old code uses PVE::RPCEnvironment::is_worker();
594 # new code should use PVE::RPCEnvironment->is_worker();
596 return PVE
::RESTEnvironment-
>is_worker();