1 package PVE
::RPCEnvironment
;
6 use PVE
::AccessControl
;
8 use PVE
::Exception
qw(raise raise_param_exc 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\./,
190 mapping
=> qr/Mapping\.|Permissions.Modify/,
192 map { $res->{$_} = {} } keys %$priv_re_map;
194 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage', '/sdn', '/mapping'];
195 my $defined_paths = [];
196 PVE
::AccessControl
::iterate_acl_tree
("/", $usercfg->{acl_root
}, sub {
197 my ($path, $node) = @_;
198 push @$defined_paths, $path;
201 my $checked_paths = {};
202 foreach my $path (@$required_paths, @$defined_paths) {
203 next if $checked_paths->{$path};
204 $checked_paths->{$path} = 1;
206 my $path_perm = $self->permissions($authuser, $path);
208 my $toplevel = ($path =~ /^\/(\w
+)/) ?
$1 : 'dc';
209 if ($toplevel eq 'pool') {
210 foreach my $priv (keys %$path_perm) {
211 next if !defined($path_perm->{$priv});
213 if ($priv =~ m/^VM\./) {
214 $res->{vms
}->{$priv} = 1;
215 } elsif ($priv =~ m/^Datastore\./) {
216 $res->{storage
}->{$priv} = 1;
217 } elsif ($priv eq 'Permissions.Modify') {
218 $res->{storage
}->{$priv} = 1;
219 $res->{vms
}->{$priv} = 1;
223 my $priv_regex = $priv_re_map->{$toplevel} // next;
224 foreach my $priv (keys %$path_perm) {
225 next if !defined($path_perm->{$priv});
227 next if $priv !~ m/^($priv_regex)/;
228 $res->{$toplevel}->{$priv} = 1;
236 sub get_effective_permissions
{
237 my ($self, $user) = @_;
239 # default / top level paths
243 '/access/groups' => 1,
251 my $cfg = $self->{user_cfg
};
253 # paths explicitly listed in ACLs
254 PVE
::AccessControl
::iterate_acl_tree
("/", $cfg->{acl_root
}, sub {
255 my ($path, $node) = @_;
259 # paths referenced by pool definitions
260 foreach my $pool (keys %{$cfg->{pools
}}) {
261 my $d = $cfg->{pools
}->{$pool};
262 foreach my $vmid (keys %{$d->{vms
}}) {
263 $paths->{"/vms/$vmid"} = 1;
265 foreach my $storeid (keys %{$d->{storage
}}) {
266 $paths->{"/storage/$storeid"} = 1;
271 foreach my $path (keys %$paths) {
272 my $path_perms = $self->permissions($user, $path);
273 foreach my $priv (keys %$path_perms) {
274 delete $path_perms->{$priv} if !defined($path_perms->{$priv});
276 # filter paths where user has NO permissions
277 $perms->{$path} = $path_perms if %$path_perms;
283 my ($self, $user, $path, $privs, $noerr) = @_;
285 my $perm = $self->permissions($user, $path);
287 foreach my $priv (@$privs) {
288 PVE
::AccessControl
::verify_privname
($priv);
289 if (!defined($perm->{$priv})) {
290 return undef if $noerr;
291 raise_perm_exc
("$path, $priv");
299 my ($self, $user, $path, $privs, $noerr) = @_;
301 my $perm = $self->permissions($user, $path);
304 foreach my $priv (@$privs) {
305 PVE
::AccessControl
::verify_privname
($priv);
306 if (defined($perm->{$priv})) {
314 return undef if $noerr;
316 raise_perm_exc
("$path, " . join("|", @$privs));
320 my ($self, $username, $path, $privs, $any, $noerr) = @_;
322 return $self->check_any($username, $path, $privs, $noerr);
324 return $self->check($username, $path, $privs, $noerr);
328 # check for any fashion of access to vnet/bridge
329 sub check_sdn_bridge
{
330 my ($self, $username, $zone, $bridge, $privs, $noerr) = @_;
332 my $path = "/sdn/zones/$zone/$bridge";
333 # check access to bridge itself
334 return 1 if $self->check_any($username, $path, $privs, 1);
336 my $cfg = $self->{user_cfg
};
337 my $bridge_acl = PVE
::AccessControl
::find_acl_tree_node
($cfg->{acl_root
}, $path);
339 # check access to VLANs
340 my $vlans = $bridge_acl->{children
};
341 for my $vlan (keys %$vlans) {
342 my $vlanpath = "$path/$vlan";
343 return 1 if $self->check_any($username, $vlanpath, $privs, 1);
347 # repeat check, but fatal
348 $self->check_any($username, $path, $privs, 0) if !$noerr;
353 sub check_user_enabled
{
354 my ($self, $user, $noerr) = @_;
356 my $cfg = $self->{user_cfg
};
357 return PVE
::AccessControl
::check_user_enabled
($cfg, $user, $noerr);
360 sub check_user_exist
{
361 my ($self, $user, $noerr) = @_;
363 my $cfg = $self->{user_cfg
};
364 return PVE
::AccessControl
::check_user_exist
($cfg, $user, $noerr);
367 sub check_pool_exist
{
368 my ($self, $pool, $noerr) = @_;
370 my $cfg = $self->{user_cfg
};
372 return 1 if $cfg->{pools
}->{$pool};
374 return undef if $noerr;
376 raise_perm_exc
("pool '$pool' does not exist");
380 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
382 my $cfg = $self->{user_cfg
};
385 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
387 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
390 sub is_group_member
{
391 my ($self, $group, $user) = @_;
393 my $cfg = $self->{user_cfg
};
395 return 0 if !$cfg->{groups
}->{$group};
397 return defined($cfg->{groups
}->{$group}->{users
}->{$user});
401 my ($self, $user, $privs, $any) = @_;
403 my $cfg = $self->{user_cfg
};
406 foreach my $group (keys %{$cfg->{groups
}}) {
407 my $path = "/access/groups/$group";
408 if ($self->check_full($user, $path, $privs, $any, 1)) {
409 $groups->{$group} = $cfg->{groups
}->{$group};
416 sub group_member_join
{
417 my ($self, $grouplist) = @_;
421 my $cfg = $self->{user_cfg
};
422 foreach my $group (@$grouplist) {
423 my $data = $cfg->{groups
}->{$group};
425 foreach my $user (keys %{$data->{users
}}) {
433 sub check_perm_modify
{
434 my ($self, $username, $path, $noerr) = @_;
436 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
438 my $testperms = [ 'Permissions.Modify' ];
439 if ($path =~ m
|^/storage/.+$|) {
440 push @$testperms, 'Datastore.Allocate';
441 } elsif ($path =~ m
|^/vms/.+$|) {
442 push @$testperms, 'VM.Allocate';
443 } elsif ($path =~ m
|^/pool/.+$|) {
444 push @$testperms, 'Pool.Allocate';
447 return $self->check_any($username, $path, $testperms, $noerr);
450 sub exec_api2_perm_check
{
451 my ($self, $check, $username, $param, $noerr) = @_;
453 # syslog("info", "CHECK " . join(', ', @$check));
456 my $test = $check->[$ind++];
457 die "no permission test specified" if !$test;
459 if ($test eq 'and') {
460 while (my $subcheck = $check->[$ind++]) {
461 $self->exec_api2_perm_check($subcheck, $username, $param);
464 } elsif ($test eq 'or') {
465 while (my $subcheck = $check->[$ind++]) {
466 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
470 } elsif ($test eq 'perm') {
471 my ($t, $tmplpath, $privs, %options) = @$check;
472 my $any = $options{any
};
473 die "missing parameters" if !($tmplpath && $privs);
474 my $require_param = $options{require_param
};
475 if ($require_param && !defined($param->{$require_param})) {
479 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
480 my $normpath = PVE
::AccessControl
::normalize_path
($path);
481 warn "Failed to normalize '$path'\n" if !defined($normpath) && defined($path);
483 return $self->check_full($username, $normpath, $privs, $any, $noerr);
484 } elsif ($test eq 'userid-group') {
485 my $userid = $param->{userid
};
486 my ($t, $privs, %options) = @$check;
488 my $check_existing_user = !$options{groups_param
} || $options{groups_param
} ne 'create';
489 return 0 if $check_existing_user && !$self->check_user_exist($userid, $noerr);
491 # check permission for ALL groups (and thus ALL users)
492 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
493 # list of groups $username has any of $privs on
494 my $groups = $self->filter_groups($username, $privs, 1);
495 if ($options{groups_param
}) {
496 # does $username have any of $privs on all new/updated/.. groups?
497 my @group_param = PVE
::Tools
::split_list
($param->{groups
});
498 raise_perm_exc
("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
499 foreach my $pg (@group_param) {
500 raise_perm_exc
("/access/groups/$pg, " . join("|", @$privs))
504 if ($check_existing_user) {
505 # does $username have any of $privs on any existing group of $userid
506 my $allowed_users = $self->group_member_join([keys %$groups]);
507 if (!$allowed_users->{$userid}) {
514 } elsif ($test eq 'userid-param') {
515 my ($userid, undef, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
516 my ($t, $subtest) = @$check;
517 die "missing parameters" if !$subtest;
518 if ($subtest eq 'self') {
519 return 0 if !$self->check_user_exist($userid, $noerr);
520 return 1 if $username eq $userid;
523 } elsif ($subtest eq 'Realm.AllocateUser') {
524 my $path = "/access/realm/$realm";
525 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
527 die "unknown userid-param test";
529 } elsif ($test eq 'perm-modify') {
530 my ($t, $tmplpath) = @$check;
531 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
532 $path = PVE
::AccessControl
::normalize_path
($path);
533 return 0 if !defined($path); # should already die in API2::ACL
534 return $self->check_perm_modify($username, $path, $noerr);
536 die "unknown permission test";
540 sub check_api2_permissions
{
541 my ($self, $perm, $username, $param) = @_;
543 return 1 if !$username && $perm->{user
} && $perm->{user
} eq 'world';
545 raise_perm_exc
("user != null") if !$username;
547 return 1 if $username eq 'root@pam';
549 raise_perm_exc
('user != root@pam') if !$perm;
551 return 1 if $perm->{user
} && $perm->{user
} eq 'all';
553 return $self->exec_api2_perm_check($perm->{check
}, $username, $param)
559 sub log_cluster_msg
{
560 my ($self, $pri, $user, $msg) = @_;
562 PVE
::Cluster
::log_msg
($pri, $user, $msg);
565 sub broadcast_tasklist
{
566 my ($self, $tlist) = @_;
568 PVE
::Cluster
::broadcast_tasklist
($tlist);
571 # initialize environment - must be called once at program startup
573 my ($class, $type, %params) = @_;
575 $class = ref($class) || $class;
577 my $self = $class->SUPER::init
($type, %params);
579 $self->{user_cfg
} = {};
580 $self->{aclcache
} = {};
581 $self->{aclversion
} = undef;
587 # init_request - must be called before each RPC request
589 my ($self, %params) = @_;
591 PVE
::Cluster
::cfs_update
();
593 $self->{result_attributes
} = {};
595 my $userconfig; # we use this for regression tests
596 foreach my $p (keys %params) {
597 if ($p eq 'userconfig') {
598 $userconfig = $params{$p};
600 die "unknown parameter '$p'";
605 $self->{aclcache
} = {};
607 my $ucdata = PVE
::Tools
::file_get_contents
($userconfig);
608 my $cfg = PVE
::AccessControl
::parse_user_config
($userconfig, $ucdata);
609 $self->{user_cfg
} = $cfg;
611 my $ucvers = PVE
::Cluster
::cfs_file_version
('user.cfg');
612 if (!$self->{aclcache
} || !defined($self->{aclversion
}) ||
613 !defined($ucvers) || ($ucvers ne $self->{aclversion
})) {
614 $self->{aclversion
} = $ucvers;
615 my $cfg = PVE
::Cluster
::cfs_read_file
('user.cfg');
616 $self->{user_cfg
} = $cfg;
621 $self->{user_cfg
} = {};
622 die "Unable to load access control list: $err";
626 # hacks: to provide better backwards compatibility
628 # old code uses PVE::RPCEnvironment::get();
629 # new code should use PVE::RPCEnvironment->get();
631 return PVE
::RESTEnvironment-
>get();
634 # old code uses PVE::RPCEnvironment::is_worker();
635 # new code should use PVE::RPCEnvironment->is_worker();
637 return PVE
::RESTEnvironment-
>is_worker();
640 # Permission helper for TFA and password API endpoints modifying users.
641 # Only root may modify root, regular users need to specify their password.
643 # Returns the same as `verify_username` in list context (userid, ruid, realm),
644 # or just the userid in scalar context.
645 sub reauth_user_for_user_modification
: prototype($$$$;$) {
646 my ($rpcenv, $authuser, $userid, $password, $param_name) = @_;
648 $param_name //= 'password';
650 ($userid, my $ruid, my $realm) = PVE
::AccessControl
::verify_username
($userid);
651 $rpcenv->check_user_exist($userid);
653 raise_perm_exc
() if $userid eq 'root@pam' && $authuser ne 'root@pam';
655 # Regular users need to confirm their password to change TFA settings.
656 if ($authuser ne 'root@pam') {
657 raise_param_exc
({ $param_name => 'password is required to modify user' })
658 if !defined($password);
660 ($authuser, my $auth_username, my $auth_realm) =
661 PVE
::AccessControl
::verify_username
($authuser);
663 my $domain_cfg = PVE
::Cluster
::cfs_read_file
('domains.cfg');
664 my $cfg = $domain_cfg->{ids
}->{$auth_realm};
665 die "auth domain '$auth_realm' does not exist\n" if !$cfg;
666 my $plugin = PVE
::Auth
::Plugin-
>lookup($cfg->{type
});
667 $plugin->authenticate_user($cfg, $auth_realm, $auth_username, $password);
670 return wantarray ?
($userid, $ruid, $realm) : $userid;