]>
git.proxmox.com Git - pve-access-control.git/blob - PVE/RPCEnvironment.pm
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);
18 # FIXME: remove dependency to PVE::Storage;
22 my $compile_acl_path = sub {
23 my ($self, $user, $path) = @_;
25 my $cfg = $self->{user_cfg
};
27 return undef if !$cfg->{roles
};
29 die "internal error" if $user eq 'root@pam';
31 my $cache = $self->{aclcache
};
32 $cache->{$user} = {} if !$cache->{$user};
33 my $data = $cache->{$user};
35 if (!$data->{poolroles
}) {
36 $data->{poolroles
} = {};
38 foreach my $pool (keys %{$cfg->{pools
}}) {
39 my $d = $cfg->{pools
}->{$pool};
40 my @ra = PVE
::AccessControl
::roles
($cfg, $user, "/pool/$pool"); # pool roles
42 foreach my $vmid (keys %{$d->{vms
}}) {
44 $data->{poolroles
}->{"/vms/$vmid"}->{$role} = 1;
47 foreach my $storeid (keys %{$d->{storage
}}) {
49 $data->{poolroles
}->{"/storage/$storeid"}->{$role} = 1;
55 my @ra = PVE
::AccessControl
::roles
($cfg, $user, $path);
57 # apply roles inherited from pools
58 # Note: assume we do not want to propagate those privs
59 if ($data->{poolroles
}->{$path}) {
60 if (!($ra[0] && $ra[0] eq 'NoAccess')) {
61 if ($data->{poolroles
}->{$path}->{NoAccess
}) {
64 foreach my $role (keys %{$data->{poolroles
}->{$path}}) {
71 $data->{roles
}->{$path} = [ @ra ];
74 foreach my $role (@ra) {
75 if (my $privset = $cfg->{roles
}->{$role}) {
76 foreach my $p (keys %$privset) {
81 $data->{privs
}->{$path} = $privs;
87 my ($self, $user, $path) = @_;
89 if ($user eq 'root@pam') { # root can do anything
90 return ('Administrator');
93 $user = PVE
::AccessControl
::verify_username
($user, 1);
96 my $cache = $self->{aclcache
};
97 $cache->{$user} = {} if !$cache->{$user};
99 my $acl = $cache->{$user};
101 my $roles = $acl->{roles
}->{$path};
102 return @$roles if $roles;
104 &$compile_acl_path($self, $user, $path);
105 $roles = $acl->{roles
}->{$path} || [];
110 my ($self, $user, $path) = @_;
112 if ($user eq 'root@pam') { # root can do anything
113 my $cfg = $self->{user_cfg
};
114 return $cfg->{roles
}->{'Administrator'};
117 $user = PVE
::AccessControl
::verify_username
($user, 1);
120 my $cache = $self->{aclcache
};
121 $cache->{$user} = {} if !$cache->{$user};
123 my $acl = $cache->{$user};
125 my $perm = $acl->{privs
}->{$path};
126 return $perm if $perm;
128 return &$compile_acl_path($self, $user, $path);
132 my ($self, $user, $path, $privs, $noerr) = @_;
134 my $perm = $self->permissions($user, $path);
136 foreach my $priv (@$privs) {
137 PVE
::AccessControl
::verify_privname
($priv);
138 if (!$perm->{$priv}) {
139 return undef if $noerr;
140 raise_perm_exc
("$path, $priv");
148 my ($self, $user, $path, $privs, $noerr) = @_;
150 my $perm = $self->permissions($user, $path);
153 foreach my $priv (@$privs) {
154 PVE
::AccessControl
::verify_privname
($priv);
155 if ($perm->{$priv}) {
163 return undef if $noerr;
165 raise_perm_exc
("$path, " . join("|", @$privs));
169 my ($self, $username, $path, $privs, $any, $noerr) = @_;
171 return $self->check_any($username, $path, $privs, $noerr);
173 return $self->check($username, $path, $privs, $noerr);
177 sub check_user_enabled
{
178 my ($self, $user, $noerr) = @_;
180 my $cfg = $self->{user_cfg
};
181 return PVE
::AccessControl
::check_user_enabled
($cfg, $user, $noerr);
184 sub check_user_exist
{
185 my ($self, $user, $noerr) = @_;
187 my $cfg = $self->{user_cfg
};
188 return PVE
::AccessControl
::check_user_exist
($cfg, $user, $noerr);
191 sub check_pool_exist
{
192 my ($self, $pool, $noerr) = @_;
194 my $cfg = $self->{user_cfg
};
196 return 1 if $cfg->{pools
}->{$pool};
198 return undef if $noerr;
200 raise_perm_exc
("pool '$pool' does not exist");
204 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
206 my $cfg = $self->{user_cfg
};
209 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
211 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
214 sub check_volume_access
{
215 my ($self, $user, $storecfg, $vmid, $volid) = @_;
217 # test if we have read access to volid
219 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
221 my ($vtype, undef, $ownervm) = PVE
::Storage
::parse_volname
($storecfg, $volid);
222 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
223 # we simply allow access
224 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
225 # we are owner - allow access
226 } elsif ($vtype eq 'backup' && $ownervm) {
227 $self->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
228 $self->check($user, "/vms/$ownervm", ['VM.Backup']);
230 # allow if we are Datastore administrator
231 $self->check($user, "/storage/$sid", ['Datastore.Allocate']);
234 die "Only root can pass arbitrary filesystem paths."
235 if $user ne 'root@pam';
241 sub is_group_member
{
242 my ($self, $group, $user) = @_;
244 my $cfg = $self->{user_cfg
};
246 return 0 if !$cfg->{groups
}->{$group};
248 return defined($cfg->{groups
}->{$group}->{users
}->{$user});
252 my ($self, $user, $privs, $any) = @_;
254 my $cfg = $self->{user_cfg
};
257 foreach my $group (keys %{$cfg->{groups
}}) {
258 my $path = "/access/groups/$group";
259 if ($self->check_full($user, $path, $privs, $any, 1)) {
260 $groups->{$group} = $cfg->{groups
}->{$group};
267 sub group_member_join
{
268 my ($self, $grouplist) = @_;
272 my $cfg = $self->{user_cfg
};
273 foreach my $group (@$grouplist) {
274 my $data = $cfg->{groups
}->{$group};
276 foreach my $user (keys %{$data->{users
}}) {
284 sub check_perm_modify
{
285 my ($self, $username, $path, $noerr) = @_;
287 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
289 my $testperms = [ 'Permissions.Modify' ];
290 if ($path =~ m
|^/storage/.+$|) {
291 push @$testperms, 'Datastore.Allocate';
292 } elsif ($path =~ m
|^/vms/.+$|) {
293 push @$testperms, 'VM.Allocate';
294 } elsif ($path =~ m
|^/pool/.+$|) {
295 push @$testperms, 'Pool.Allocate';
298 return $self->check_any($username, $path, $testperms, $noerr);
301 sub exec_api2_perm_check
{
302 my ($self, $check, $username, $param, $noerr) = @_;
304 # syslog("info", "CHECK " . join(', ', @$check));
307 my $test = $check->[$ind++];
308 die "no permission test specified" if !$test;
310 if ($test eq 'and') {
311 while (my $subcheck = $check->[$ind++]) {
312 $self->exec_api2_perm_check($subcheck, $username, $param);
315 } elsif ($test eq 'or') {
316 while (my $subcheck = $check->[$ind++]) {
317 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
321 } elsif ($test eq 'perm') {
322 my ($t, $tmplpath, $privs, %options) = @$check;
323 my $any = $options{any
};
324 die "missing parameters" if !($tmplpath && $privs);
325 my $require_param = $options{require_param
};
326 if ($require_param && !defined($param->{$require_param})) {
330 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
331 $path = PVE
::AccessControl
::normalize_path
($path);
332 return $self->check_full($username, $path, $privs, $any, $noerr);
333 } elsif ($test eq 'userid-group') {
334 my $userid = $param->{userid
};
335 my ($t, $privs, %options) = @$check;
336 return 0 if !$options{groups_param
} && !$self->check_user_exist($userid, $noerr);
337 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
338 my $groups = $self->filter_groups($username, $privs, 1);
339 if ($options{groups_param
}) {
340 my @group_param = PVE
::Tools
::split_list
($param->{groups
});
341 raise_perm_exc
("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
342 foreach my $pg (@group_param) {
343 raise_perm_exc
("/access/groups/$pg, " . join("|", @$privs))
347 my $allowed_users = $self->group_member_join([keys %$groups]);
348 if (!$allowed_users->{$userid}) {
355 } elsif ($test eq 'userid-param') {
356 my ($userid, undef, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
357 my ($t, $subtest) = @$check;
358 die "missing parameters" if !$subtest;
359 if ($subtest eq 'self') {
360 return 0 if !$self->check_user_exist($userid, $noerr);
361 return 1 if $username eq $userid;
364 } elsif ($subtest eq 'Realm.AllocateUser') {
365 my $path = "/access/realm/$realm";
366 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
368 die "unknown userid-param test";
370 } elsif ($test eq 'perm-modify') {
371 my ($t, $tmplpath) = @$check;
372 my $path = PVE
::Tools
::template_replace
($tmplpath, $param);
373 $path = PVE
::AccessControl
::normalize_path
($path);
374 return $self->check_perm_modify($username, $path, $noerr);
376 die "unknown permission test";
380 sub check_api2_permissions
{
381 my ($self, $perm, $username, $param) = @_;
383 return 1 if !$username && $perm->{user
} eq 'world';
385 raise_perm_exc
("user != null") if !$username;
387 return 1 if $username eq 'root@pam';
389 raise_perm_exc
('user != root@pam') if !$perm;
391 return 1 if $perm->{user
} && $perm->{user
} eq 'all';
393 return $self->exec_api2_perm_check($perm->{check
}, $username, $param)
399 sub log_cluster_msg
{
400 my ($self, $pri, $user, $msg) = @_;
402 PVE
::Cluster
::log_msg
($pri, $user, $msg);
405 sub broadcast_tasklist
{
406 my ($self, $tlist) = @_;
408 PVE
::Cluster
::broadcast_tasklist
($tlist);
411 # initialize environment - must be called once at program startup
413 my ($class, $type, %params) = @_;
415 $class = ref($class) || $class;
417 my $self = $class->SUPER::init
($type, %params);
419 $self->{user_cfg
} = {};
420 $self->{aclcache
} = {};
421 $self->{aclversion
} = undef;
427 # init_request - must be called before each RPC request
429 my ($self, %params) = @_;
431 PVE
::Cluster
::cfs_update
();
433 $self->{result_attributes
} = {};
435 my $userconfig; # we use this for regression tests
436 foreach my $p (keys %params) {
437 if ($p eq 'userconfig') {
438 $userconfig = $params{$p};
440 die "unknown parameter '$p'";
445 $self->{aclcache
} = {};
447 my $ucdata = PVE
::Tools
::file_get_contents
($userconfig);
448 my $cfg = PVE
::AccessControl
::parse_user_config
($userconfig, $ucdata);
449 $self->{user_cfg
} = $cfg;
451 my $ucvers = PVE
::Cluster
::cfs_file_version
('user.cfg');
452 if (!$self->{aclcache
} || !defined($self->{aclversion
}) ||
453 !defined($ucvers) || ($ucvers ne $self->{aclversion
})) {
454 $self->{aclversion
} = $ucvers;
455 my $cfg = PVE
::Cluster
::cfs_read_file
('user.cfg');
456 $self->{user_cfg
} = $cfg;
461 $self->{user_cfg
} = {};
462 die "Unable to load access control list: $err";
466 # hacks: to provide better backwards compatibiliy
468 # old code uses PVE::RPCEnvironment::get();
469 # new code should use PVE::RPCEnvironment->get();
471 return PVE
::RESTEnvironment-
>get();
474 # old code uses PVE::RPCEnvironment::is_worker();
475 # new code should use PVE::RPCEnvironment->is_worker();
477 return PVE
::RESTEnvironment-
>is_worker();