]>
git.proxmox.com Git - pve-access-control.git/blob - PVE/RPCEnvironment.pm
8384376f99e0207212129058f72e34e78a607690
1 package PVE
::RPCEnvironment
;
6 use PVE
::RESTEnvironment
;
8 use PVE
::Exception
qw(raise raise_perm_exc);
14 use PVE
::AccessControl
;
16 use base
qw(PVE::RESTEnvironment);
20 my $compile_acl_path = sub {
21 my ($self, $user, $path) = @_;
23 my $cfg = $self->{user_cfg
};
25 return undef if !$cfg->{roles
};
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 my ($username, undef) = PVE
::AccessControl
::split_tokenid
($user, 1);
34 die "internal error" if $username && $username ne 'root@pam' && !defined($cache->{$username});
36 if (!$data->{poolroles
}) {
37 $data->{poolroles
} = {};
39 foreach my $pool (keys %{$cfg->{pools
}}) {
40 my $d = $cfg->{pools
}->{$pool};
41 my $pool_roles = PVE
::AccessControl
::roles
($cfg, $user, "/pool/$pool"); # pool roles
42 next if !scalar(keys %$pool_roles);
43 foreach my $vmid (keys %{$d->{vms
}}) {
44 for my $role (keys %$pool_roles) {
45 $data->{poolroles
}->{"/vms/$vmid"}->{$role} = 1;
48 foreach my $storeid (keys %{$d->{storage
}}) {
49 for my $role (keys %$pool_roles) {
50 $data->{poolroles
}->{"/storage/$storeid"}->{$role} = 1;
56 my $roles = PVE
::AccessControl
::roles
($cfg, $user, $path);
58 # apply roles inherited from pools
59 # Note: assume we do not want to propagate those privs
60 if ($data->{poolroles
}->{$path}) {
61 if (!defined($roles->{NoAccess
})) {
62 if ($data->{poolroles
}->{$path}->{NoAccess
}) {
63 $roles = { 'NoAccess' => 0 };
65 foreach my $role (keys %{$data->{poolroles
}->{$path}}) {
66 $roles->{$role} = 0 if !defined($roles->{$role});
72 $data->{roles
}->{$path} = $roles;
75 foreach my $role (keys %$roles) {
76 if (my $privset = $cfg->{roles
}->{$role}) {
77 foreach my $p (keys %$privset) {
78 $privs->{$p} = $roles->{$role};
83 if ($username && $username ne 'root@pam') {
84 # intersect user and token permissions
85 my $user_privs = $cache->{$username}->{privs
}->{$path};
86 $privs = { map { $_ => $user_privs->{$_} && $privs->{$_} } keys %$privs };
89 $data->{privs
}->{$path} = $privs;
95 my ($self, $user, $path) = @_;
97 if ($user eq 'root@pam') { # root can do anything
98 my $cfg = $self->{user_cfg
};
99 return { map { $_ => 1 } keys %{$cfg->{roles
}->{'Administrator'}} };
102 if (PVE
::AccessControl
::pve_verify_tokenid
($user, 1)) {
103 my ($username, $token) = PVE
::AccessControl
::split_tokenid
($user);
104 my $cfg = $self->{user_cfg
};
105 my $token_info = $cfg->{users
}->{$username}->{tokens
}->{$token};
107 return {} if !$token_info;
109 # ensure cache for user is populated
110 my $user_perms = $self->permissions($username, $path);
112 # return user privs for non-privsep tokens
113 return $user_perms if !$token_info->{privsep
};
115 $user = PVE
::AccessControl
::verify_username
($user, 1);
119 my $cache = $self->{aclcache
};
120 $cache->{$user} = {} if !$cache->{$user};
122 my $acl = $cache->{$user};
124 my $perm = $acl->{privs
}->{$path};
125 return $perm if $perm;
127 return &$compile_acl_path($self, $user, $path);
131 my ($self, $user, $path, $privs, $noerr) = @_;
133 my $perm = $self->permissions($user, $path);
135 foreach my $priv (@$privs) {
136 PVE
::AccessControl
::verify_privname
($priv);
137 if (!defined($perm->{$priv})) {
138 return undef if $noerr;
139 raise_perm_exc
("$path, $priv");
147 my ($self, $user, $path, $privs, $noerr) = @_;
149 my $perm = $self->permissions($user, $path);
152 foreach my $priv (@$privs) {
153 PVE
::AccessControl
::verify_privname
($priv);
154 if (defined($perm->{$priv})) {
162 return undef if $noerr;
164 raise_perm_exc
("$path, " . join("|", @$privs));
168 my ($self, $username, $path, $privs, $any, $noerr) = @_;
170 return $self->check_any($username, $path, $privs, $noerr);
172 return $self->check($username, $path, $privs, $noerr);
176 sub check_user_enabled
{
177 my ($self, $user, $noerr) = @_;
179 my $cfg = $self->{user_cfg
};
180 return PVE
::AccessControl
::check_user_enabled
($cfg, $user, $noerr);
183 sub check_user_exist
{
184 my ($self, $user, $noerr) = @_;
186 my $cfg = $self->{user_cfg
};
187 return PVE
::AccessControl
::check_user_exist
($cfg, $user, $noerr);
190 sub check_pool_exist
{
191 my ($self, $pool, $noerr) = @_;
193 my $cfg = $self->{user_cfg
};
195 return 1 if $cfg->{pools
}->{$pool};
197 return undef if $noerr;
199 raise_perm_exc
("pool '$pool' does not exist");
203 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
205 my $cfg = $self->{user_cfg
};
208 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
210 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
213 sub is_group_member
{
214 my ($self, $group, $user) = @_;
216 my $cfg = $self->{user_cfg
};
218 return 0 if !$cfg->{groups
}->{$group};
220 return defined($cfg->{groups
}->{$group}->{users
}->{$user});
224 my ($self, $user, $privs, $any) = @_;
226 my $cfg = $self->{user_cfg
};
229 foreach my $group (keys %{$cfg->{groups
}}) {
230 my $path = "/access/groups/$group";
231 if ($self->check_full($user, $path, $privs, $any, 1)) {
232 $groups->{$group} = $cfg->{groups
}->{$group};
239 sub group_member_join
{
240 my ($self, $grouplist) = @_;
244 my $cfg = $self->{user_cfg
};
245 foreach my $group (@$grouplist) {
246 my $data = $cfg->{groups
}->{$group};
248 foreach my $user (keys %{$data->{users
}}) {
256 sub check_perm_modify
{
257 my ($self, $username, $path, $noerr) = @_;
259 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
261 my $testperms = [ 'Permissions.Modify' ];
262 if ($path =~ m
|^/storage/.+$|) {
263 push @$testperms, 'Datastore.Allocate';
264 } elsif ($path =~ m
|^/vms/.+$|) {
265 push @$testperms, 'VM.Allocate';
266 } elsif ($path =~ m
|^/pool/.+$|) {
267 push @$testperms, 'Pool.Allocate';
270 return $self->check_any($username, $path, $testperms, $noerr);
273 sub exec_api2_perm_check
{
274 my ($self, $check, $username, $param, $noerr) = @_;
276 # syslog("info", "CHECK " . join(', ', @$check));
279 my $test = $check->[$ind++];
280 die "no permission test specified" if !$test;
282 if ($test eq 'and') {
283 while (my $subcheck = $check->[$ind++]) {
284 $self->exec_api2_perm_check($subcheck, $username, $param);
287 } elsif ($test eq 'or') {
288 while (my $subcheck = $check->[$ind++]) {
289 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
293 } elsif ($test eq 'perm') {
294 my ($t, $tmplpath, $privs, %options) = @$check;
295 my $any = $options{any
};
296 die "missing parameters" if !($tmplpath && $privs);
297 my $require_param = $options{require_param
};
298 if ($require_param && !defined($param->{$require_param})) {
302 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
303 $path = PVE
::AccessControl
::normalize_path
($path);
304 return $self->check_full($username, $path, $privs, $any, $noerr);
305 } elsif ($test eq 'userid-group') {
306 my $userid = $param->{userid
};
307 my ($t, $privs, %options) = @$check;
308 return 0 if !$options{groups_param
} && !$self->check_user_exist($userid, $noerr);
309 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
310 my $groups = $self->filter_groups($username, $privs, 1);
311 if ($options{groups_param
}) {
312 my @group_param = PVE
::Tools
::split_list
($param->{groups
});
313 raise_perm_exc
("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
314 foreach my $pg (@group_param) {
315 raise_perm_exc
("/access/groups/$pg, " . join("|", @$privs))
319 my $allowed_users = $self->group_member_join([keys %$groups]);
320 if (!$allowed_users->{$userid}) {
327 } elsif ($test eq 'userid-param') {
328 my ($userid, undef, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
329 my ($t, $subtest) = @$check;
330 die "missing parameters" if !$subtest;
331 if ($subtest eq 'self') {
332 return 0 if !$self->check_user_exist($userid, $noerr);
333 return 1 if $username eq $userid;
336 } elsif ($subtest eq 'Realm.AllocateUser') {
337 my $path = "/access/realm/$realm";
338 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
340 die "unknown userid-param test";
342 } elsif ($test eq 'perm-modify') {
343 my ($t, $tmplpath) = @$check;
344 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
345 $path = PVE
::AccessControl
::normalize_path
($path);
346 return $self->check_perm_modify($username, $path, $noerr);
348 die "unknown permission test";
352 sub check_api2_permissions
{
353 my ($self, $perm, $username, $param) = @_;
355 return 1 if !$username && $perm->{user
} && $perm->{user
} eq 'world';
357 raise_perm_exc
("user != null") if !$username;
359 return 1 if $username eq 'root@pam';
361 raise_perm_exc
('user != root@pam') if !$perm;
363 return 1 if $perm->{user
} && $perm->{user
} eq 'all';
365 return $self->exec_api2_perm_check($perm->{check
}, $username, $param)
371 sub log_cluster_msg
{
372 my ($self, $pri, $user, $msg) = @_;
374 PVE
::Cluster
::log_msg
($pri, $user, $msg);
377 sub broadcast_tasklist
{
378 my ($self, $tlist) = @_;
380 PVE
::Cluster
::broadcast_tasklist
($tlist);
383 # initialize environment - must be called once at program startup
385 my ($class, $type, %params) = @_;
387 $class = ref($class) || $class;
389 my $self = $class->SUPER::init
($type, %params);
391 $self->{user_cfg
} = {};
392 $self->{aclcache
} = {};
393 $self->{aclversion
} = undef;
399 # init_request - must be called before each RPC request
401 my ($self, %params) = @_;
403 PVE
::Cluster
::cfs_update
();
405 $self->{result_attributes
} = {};
407 my $userconfig; # we use this for regression tests
408 foreach my $p (keys %params) {
409 if ($p eq 'userconfig') {
410 $userconfig = $params{$p};
412 die "unknown parameter '$p'";
417 $self->{aclcache
} = {};
419 my $ucdata = PVE
::Tools
::file_get_contents
($userconfig);
420 my $cfg = PVE
::AccessControl
::parse_user_config
($userconfig, $ucdata);
421 $self->{user_cfg
} = $cfg;
423 my $ucvers = PVE
::Cluster
::cfs_file_version
('user.cfg');
424 if (!$self->{aclcache
} || !defined($self->{aclversion
}) ||
425 !defined($ucvers) || ($ucvers ne $self->{aclversion
})) {
426 $self->{aclversion
} = $ucvers;
427 my $cfg = PVE
::Cluster
::cfs_read_file
('user.cfg');
428 $self->{user_cfg
} = $cfg;
433 $self->{user_cfg
} = {};
434 die "Unable to load access control list: $err";
438 # hacks: to provide better backwards compatibiliy
440 # old code uses PVE::RPCEnvironment::get();
441 # new code should use PVE::RPCEnvironment->get();
443 return PVE
::RESTEnvironment-
>get();
446 # old code uses PVE::RPCEnvironment::is_worker();
447 # new code should use PVE::RPCEnvironment->is_worker();
449 return PVE
::RESTEnvironment-
>is_worker();