]> git.proxmox.com Git - pve-access-control.git/blob - src/PVE/RPCEnvironment.pm
permissions: add some more comments
[pve-access-control.git] / src / PVE / RPCEnvironment.pm
1 package PVE::RPCEnvironment;
2
3 use strict;
4 use warnings;
5
6 use PVE::AccessControl;
7 use PVE::Cluster;
8 use PVE::Exception qw(raise raise_perm_exc);
9 use PVE::INotify;
10 use PVE::ProcFSTools;
11 use PVE::RESTEnvironment;
12 use PVE::SafeSyslog;
13 use PVE::Tools;
14
15 use base qw(PVE::RESTEnvironment);
16
17 # ACL cache
18
19 my $compile_acl_path = sub {
20 my ($self, $user, $path) = @_;
21
22 my $cfg = $self->{user_cfg};
23
24 return undef if !$cfg->{roles};
25
26 # permissions() has an early return for this case
27 die "internal error" if $user eq 'root@pam';
28
29 my $cache = $self->{aclcache};
30 $cache->{$user} = {} if !$cache->{$user};
31 my $data = $cache->{$user};
32
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});
36
37 # resolve and cache roles of the current user/token for all pool ACL paths
38 if (!$data->{poolroles}) {
39 $data->{poolroles} = {};
40
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;
48 }
49 }
50 foreach my $storeid (keys %{$d->{storage}}) {
51 for my $role (keys %$pool_roles) {
52 $data->{poolroles}->{"/storage/$storeid"}->{$role} = 1;
53 }
54 }
55 }
56 }
57
58 # get roles of current user/token on checked path - this already handles
59 # propagation and NoAccess along the path
60 #
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);
64
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 };
72 } else {
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});
77 }
78 }
79 }
80 }
81
82 # cache roles
83 $data->{roles}->{$path} = $roles;
84
85 # derive privs from set roles - hash mapping privilege name to propagation
86 # flag value, a key being defined means the priv is set
87 my $privs = {};
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};
94 }
95 }
96 }
97
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
105 # flags
106 $privs = { map { $_ => $user_privs->{$_} && $privs->{$_} } @$filtered_privs };
107 }
108
109 foreach my $priv (keys %$privs) {
110 # safeguard, this should never happen anyway
111 delete $privs->{$priv} if !defined($privs->{$priv});
112 }
113
114 # cache privs
115 $data->{privs}->{$path} = $privs;
116
117 return $privs;
118 };
119
120 # this is the method used by permission check helpers below
121 #
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.
126 #
127 # compiled ACLs are cached, so repeated checks for the same path and user are
128 # almost free.
129 #
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
135 sub permissions {
136 my ($self, $user, $path) = @_;
137
138 if ($user eq 'root@pam') { # root can do anything
139 my $cfg = $self->{user_cfg};
140 return { map { $_ => 1 } keys %{$cfg->{roles}->{'Administrator'}} };
141 }
142
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};
147
148 return {} if !$token_info;
149
150 # ensure cache for user is populated
151 my $user_perms = $self->permissions($username, $path);
152
153 # return user privs for non-privsep tokens
154 return $user_perms if !$token_info->{privsep};
155 } else {
156 $user = PVE::AccessControl::verify_username($user, 1);
157 return {} if !$user;
158 }
159
160 my $cache = $self->{aclcache};
161 $cache->{$user} = {} if !$cache->{$user};
162
163 my $acl = $cache->{$user};
164
165 my $perm = $acl->{privs}->{$path};
166 return $perm if $perm;
167
168 return &$compile_acl_path($self, $user, $path);
169 }
170
171 sub compute_api_permission {
172 my ($self, $authuser) = @_;
173
174 my $usercfg = $self->{user_cfg};
175
176 my $res = {};
177 my $priv_re_map = {
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\./,
184 };
185 map { $res->{$_} = {} } keys %$priv_re_map;
186
187 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage', '/sdn'];
188
189 my $checked_paths = {};
190 foreach my $path (@$required_paths, keys %{$usercfg->{acl}}) {
191 next if $checked_paths->{$path};
192 $checked_paths->{$path} = 1;
193
194 my $path_perm = $self->permissions($authuser, $path);
195
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});
200
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;
208 }
209 }
210 } else {
211 my $priv_regex = $priv_re_map->{$toplevel} // next;
212 foreach my $priv (keys %$path_perm) {
213 next if !defined($path_perm->{$priv});
214
215 next if $priv !~ m/^($priv_regex)/;
216 $res->{$toplevel}->{$priv} = 1;
217 }
218 }
219 }
220
221 return $res;
222 }
223
224 sub get_effective_permissions {
225 my ($self, $user) = @_;
226
227 # default / top level paths
228 my $paths = {
229 '/' => 1,
230 '/access' => 1,
231 '/access/groups' => 1,
232 '/nodes' => 1,
233 '/pools' => 1,
234 '/sdn' => 1,
235 '/storage' => 1,
236 '/vms' => 1,
237 };
238
239 my $cfg = $self->{user_cfg};
240
241 # paths explicitly listed in ACLs
242 foreach my $acl_path (keys %{$cfg->{acl}}) {
243 $paths->{$acl_path} = 1;
244 }
245
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;
251 }
252 foreach my $storeid (keys %{$d->{storage}}) {
253 $paths->{"/storage/$storeid"} = 1;
254 }
255 }
256
257 my $perms = {};
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});
262 }
263 # filter paths where user has NO permissions
264 $perms->{$path} = $path_perms if %$path_perms;
265 }
266 return $perms;
267 }
268
269 sub check {
270 my ($self, $user, $path, $privs, $noerr) = @_;
271
272 my $perm = $self->permissions($user, $path);
273
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");
279 }
280 };
281
282 return 1;
283 };
284
285 sub check_any {
286 my ($self, $user, $path, $privs, $noerr) = @_;
287
288 my $perm = $self->permissions($user, $path);
289
290 my $found = 0;
291 foreach my $priv (@$privs) {
292 PVE::AccessControl::verify_privname($priv);
293 if (defined($perm->{$priv})) {
294 $found = 1;
295 last;
296 }
297 };
298
299 return 1 if $found;
300
301 return undef if $noerr;
302
303 raise_perm_exc("$path, " . join("|", @$privs));
304 };
305
306 sub check_full {
307 my ($self, $username, $path, $privs, $any, $noerr) = @_;
308 if ($any) {
309 return $self->check_any($username, $path, $privs, $noerr);
310 } else {
311 return $self->check($username, $path, $privs, $noerr);
312 }
313 }
314
315 sub check_user_enabled {
316 my ($self, $user, $noerr) = @_;
317
318 my $cfg = $self->{user_cfg};
319 return PVE::AccessControl::check_user_enabled($cfg, $user, $noerr);
320 }
321
322 sub check_user_exist {
323 my ($self, $user, $noerr) = @_;
324
325 my $cfg = $self->{user_cfg};
326 return PVE::AccessControl::check_user_exist($cfg, $user, $noerr);
327 }
328
329 sub check_pool_exist {
330 my ($self, $pool, $noerr) = @_;
331
332 my $cfg = $self->{user_cfg};
333
334 return 1 if $cfg->{pools}->{$pool};
335
336 return undef if $noerr;
337
338 raise_perm_exc("pool '$pool' does not exist");
339 }
340
341 sub check_vm_perm {
342 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
343
344 my $cfg = $self->{user_cfg};
345
346 if ($pool) {
347 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
348 }
349 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
350 };
351
352 sub is_group_member {
353 my ($self, $group, $user) = @_;
354
355 my $cfg = $self->{user_cfg};
356
357 return 0 if !$cfg->{groups}->{$group};
358
359 return defined($cfg->{groups}->{$group}->{users}->{$user});
360 }
361
362 sub filter_groups {
363 my ($self, $user, $privs, $any) = @_;
364
365 my $cfg = $self->{user_cfg};
366
367 my $groups = {};
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};
372 }
373 }
374
375 return $groups;
376 }
377
378 sub group_member_join {
379 my ($self, $grouplist) = @_;
380
381 my $users = {};
382
383 my $cfg = $self->{user_cfg};
384 foreach my $group (@$grouplist) {
385 my $data = $cfg->{groups}->{$group};
386 next if !$data;
387 foreach my $user (keys %{$data->{users}}) {
388 $users->{$user} = 1;
389 }
390 }
391
392 return $users;
393 }
394
395 sub check_perm_modify {
396 my ($self, $username, $path, $noerr) = @_;
397
398 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
399
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';
407 }
408
409 return $self->check_any($username, $path, $testperms, $noerr);
410 }
411
412 sub exec_api2_perm_check {
413 my ($self, $check, $username, $param, $noerr) = @_;
414
415 # syslog("info", "CHECK " . join(', ', @$check));
416
417 my $ind = 0;
418 my $test = $check->[$ind++];
419 die "no permission test specified" if !$test;
420
421 if ($test eq 'and') {
422 while (my $subcheck = $check->[$ind++]) {
423 $self->exec_api2_perm_check($subcheck, $username, $param);
424 }
425 return 1;
426 } elsif ($test eq 'or') {
427 while (my $subcheck = $check->[$ind++]) {
428 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
429 }
430 return 0 if $noerr;
431 raise_perm_exc();
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})) {
438 return 0 if $noerr;
439 raise_perm_exc();
440 }
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;
447
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);
450
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))
461 if !$groups->{$pg};
462 }
463 }
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}) {
468 return 0 if $noerr;
469 raise_perm_exc();
470 }
471 }
472 }
473 return 1;
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;
481 return 0 if $noerr;
482 raise_perm_exc();
483 } elsif ($subtest eq 'Realm.AllocateUser') {
484 my $path = "/access/realm/$realm";
485 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
486 } else {
487 die "unknown userid-param test";
488 }
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);
494 } else {
495 die "unknown permission test";
496 }
497 };
498
499 sub check_api2_permissions {
500 my ($self, $perm, $username, $param) = @_;
501
502 return 1 if !$username && $perm->{user} && $perm->{user} eq 'world';
503
504 raise_perm_exc("user != null") if !$username;
505
506 return 1 if $username eq 'root@pam';
507
508 raise_perm_exc('user != root@pam') if !$perm;
509
510 return 1 if $perm->{user} && $perm->{user} eq 'all';
511
512 return $self->exec_api2_perm_check($perm->{check}, $username, $param)
513 if $perm->{check};
514
515 raise_perm_exc();
516 }
517
518 sub log_cluster_msg {
519 my ($self, $pri, $user, $msg) = @_;
520
521 PVE::Cluster::log_msg($pri, $user, $msg);
522 }
523
524 sub broadcast_tasklist {
525 my ($self, $tlist) = @_;
526
527 PVE::Cluster::broadcast_tasklist($tlist);
528 }
529
530 # initialize environment - must be called once at program startup
531 sub init {
532 my ($class, $type, %params) = @_;
533
534 $class = ref($class) || $class;
535
536 my $self = $class->SUPER::init($type, %params);
537
538 $self->{user_cfg} = {};
539 $self->{aclcache} = {};
540 $self->{aclversion} = undef;
541
542 return $self;
543 };
544
545
546 # init_request - must be called before each RPC request
547 sub init_request {
548 my ($self, %params) = @_;
549
550 PVE::Cluster::cfs_update();
551
552 $self->{result_attributes} = {};
553
554 my $userconfig; # we use this for regression tests
555 foreach my $p (keys %params) {
556 if ($p eq 'userconfig') {
557 $userconfig = $params{$p};
558 } else {
559 die "unknown parameter '$p'";
560 }
561 }
562
563 eval {
564 $self->{aclcache} = {};
565 if ($userconfig) {
566 my $ucdata = PVE::Tools::file_get_contents($userconfig);
567 my $cfg = PVE::AccessControl::parse_user_config($userconfig, $ucdata);
568 $self->{user_cfg} = $cfg;
569 } else {
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;
576 }
577 }
578 };
579 if (my $err = $@) {
580 $self->{user_cfg} = {};
581 die "Unable to load access control list: $err";
582 }
583 }
584
585 # hacks: to provide better backwards compatibiliy
586
587 # old code uses PVE::RPCEnvironment::get();
588 # new code should use PVE::RPCEnvironment->get();
589 sub get {
590 return PVE::RESTEnvironment->get();
591 }
592
593 # old code uses PVE::RPCEnvironment::is_worker();
594 # new code should use PVE::RPCEnvironment->is_worker();
595 sub is_worker {
596 return PVE::RESTEnvironment->is_worker();
597 }
598
599 1;