]> git.proxmox.com Git - pve-access-control.git/blob - src/PVE/RPCEnvironment.pm
privs: add Sys.Incoming
[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 (!defined($path)) {
144 # this shouldn't happen!
145 warn "internal error: ACL check called for undefined ACL path!\n";
146 return {};
147 }
148
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};
153
154 return {} if !$token_info;
155
156 # ensure cache for user is populated
157 my $user_perms = $self->permissions($username, $path);
158
159 # return user privs for non-privsep tokens
160 return $user_perms if !$token_info->{privsep};
161 } else {
162 $user = PVE::AccessControl::verify_username($user, 1);
163 return {} if !$user;
164 }
165
166 my $cache = $self->{aclcache};
167 $cache->{$user} = {} if !$cache->{$user};
168
169 my $acl = $cache->{$user};
170
171 my $perm = $acl->{privs}->{$path};
172 return $perm if $perm;
173
174 return &$compile_acl_path($self, $user, $path);
175 }
176
177 sub compute_api_permission {
178 my ($self, $authuser) = @_;
179
180 my $usercfg = $self->{user_cfg};
181
182 my $res = {};
183 my $priv_re_map = {
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|SDN\./,
190 };
191 map { $res->{$_} = {} } keys %$priv_re_map;
192
193 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage', '/sdn'];
194
195 my $checked_paths = {};
196 foreach my $path (@$required_paths, keys %{$usercfg->{acl}}) {
197 next if $checked_paths->{$path};
198 $checked_paths->{$path} = 1;
199
200 my $path_perm = $self->permissions($authuser, $path);
201
202 my $toplevel = ($path =~ /^\/(\w+)/) ? $1 : 'dc';
203 if ($toplevel eq 'pool') {
204 foreach my $priv (keys %$path_perm) {
205 next if !defined($path_perm->{$priv});
206
207 if ($priv =~ m/^VM\./) {
208 $res->{vms}->{$priv} = 1;
209 } elsif ($priv =~ m/^Datastore\./) {
210 $res->{storage}->{$priv} = 1;
211 } elsif ($priv eq 'Permissions.Modify') {
212 $res->{storage}->{$priv} = 1;
213 $res->{vms}->{$priv} = 1;
214 }
215 }
216 } else {
217 my $priv_regex = $priv_re_map->{$toplevel} // next;
218 foreach my $priv (keys %$path_perm) {
219 next if !defined($path_perm->{$priv});
220
221 next if $priv !~ m/^($priv_regex)/;
222 $res->{$toplevel}->{$priv} = 1;
223 }
224 }
225 }
226
227 return $res;
228 }
229
230 sub get_effective_permissions {
231 my ($self, $user) = @_;
232
233 # default / top level paths
234 my $paths = {
235 '/' => 1,
236 '/access' => 1,
237 '/access/groups' => 1,
238 '/nodes' => 1,
239 '/pools' => 1,
240 '/sdn' => 1,
241 '/storage' => 1,
242 '/vms' => 1,
243 };
244
245 my $cfg = $self->{user_cfg};
246
247 # paths explicitly listed in ACLs
248 foreach my $acl_path (keys %{$cfg->{acl}}) {
249 $paths->{$acl_path} = 1;
250 }
251
252 # paths referenced by pool definitions
253 foreach my $pool (keys %{$cfg->{pools}}) {
254 my $d = $cfg->{pools}->{$pool};
255 foreach my $vmid (keys %{$d->{vms}}) {
256 $paths->{"/vms/$vmid"} = 1;
257 }
258 foreach my $storeid (keys %{$d->{storage}}) {
259 $paths->{"/storage/$storeid"} = 1;
260 }
261 }
262
263 my $perms = {};
264 foreach my $path (keys %$paths) {
265 my $path_perms = $self->permissions($user, $path);
266 foreach my $priv (keys %$path_perms) {
267 delete $path_perms->{$priv} if !defined($path_perms->{$priv});
268 }
269 # filter paths where user has NO permissions
270 $perms->{$path} = $path_perms if %$path_perms;
271 }
272 return $perms;
273 }
274
275 sub check {
276 my ($self, $user, $path, $privs, $noerr) = @_;
277
278 my $perm = $self->permissions($user, $path);
279
280 foreach my $priv (@$privs) {
281 PVE::AccessControl::verify_privname($priv);
282 if (!defined($perm->{$priv})) {
283 return undef if $noerr;
284 raise_perm_exc("$path, $priv");
285 }
286 };
287
288 return 1;
289 };
290
291 sub check_any {
292 my ($self, $user, $path, $privs, $noerr) = @_;
293
294 my $perm = $self->permissions($user, $path);
295
296 my $found = 0;
297 foreach my $priv (@$privs) {
298 PVE::AccessControl::verify_privname($priv);
299 if (defined($perm->{$priv})) {
300 $found = 1;
301 last;
302 }
303 };
304
305 return 1 if $found;
306
307 return undef if $noerr;
308
309 raise_perm_exc("$path, " . join("|", @$privs));
310 };
311
312 sub check_full {
313 my ($self, $username, $path, $privs, $any, $noerr) = @_;
314 if ($any) {
315 return $self->check_any($username, $path, $privs, $noerr);
316 } else {
317 return $self->check($username, $path, $privs, $noerr);
318 }
319 }
320
321 sub check_user_enabled {
322 my ($self, $user, $noerr) = @_;
323
324 my $cfg = $self->{user_cfg};
325 return PVE::AccessControl::check_user_enabled($cfg, $user, $noerr);
326 }
327
328 sub check_user_exist {
329 my ($self, $user, $noerr) = @_;
330
331 my $cfg = $self->{user_cfg};
332 return PVE::AccessControl::check_user_exist($cfg, $user, $noerr);
333 }
334
335 sub check_pool_exist {
336 my ($self, $pool, $noerr) = @_;
337
338 my $cfg = $self->{user_cfg};
339
340 return 1 if $cfg->{pools}->{$pool};
341
342 return undef if $noerr;
343
344 raise_perm_exc("pool '$pool' does not exist");
345 }
346
347 sub check_vm_perm {
348 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
349
350 my $cfg = $self->{user_cfg};
351
352 if ($pool) {
353 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
354 }
355 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
356 };
357
358 sub is_group_member {
359 my ($self, $group, $user) = @_;
360
361 my $cfg = $self->{user_cfg};
362
363 return 0 if !$cfg->{groups}->{$group};
364
365 return defined($cfg->{groups}->{$group}->{users}->{$user});
366 }
367
368 sub filter_groups {
369 my ($self, $user, $privs, $any) = @_;
370
371 my $cfg = $self->{user_cfg};
372
373 my $groups = {};
374 foreach my $group (keys %{$cfg->{groups}}) {
375 my $path = "/access/groups/$group";
376 if ($self->check_full($user, $path, $privs, $any, 1)) {
377 $groups->{$group} = $cfg->{groups}->{$group};
378 }
379 }
380
381 return $groups;
382 }
383
384 sub group_member_join {
385 my ($self, $grouplist) = @_;
386
387 my $users = {};
388
389 my $cfg = $self->{user_cfg};
390 foreach my $group (@$grouplist) {
391 my $data = $cfg->{groups}->{$group};
392 next if !$data;
393 foreach my $user (keys %{$data->{users}}) {
394 $users->{$user} = 1;
395 }
396 }
397
398 return $users;
399 }
400
401 sub check_perm_modify {
402 my ($self, $username, $path, $noerr) = @_;
403
404 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
405
406 my $testperms = [ 'Permissions.Modify' ];
407 if ($path =~ m|^/storage/.+$|) {
408 push @$testperms, 'Datastore.Allocate';
409 } elsif ($path =~ m|^/vms/.+$|) {
410 push @$testperms, 'VM.Allocate';
411 } elsif ($path =~ m|^/pool/.+$|) {
412 push @$testperms, 'Pool.Allocate';
413 }
414
415 return $self->check_any($username, $path, $testperms, $noerr);
416 }
417
418 sub exec_api2_perm_check {
419 my ($self, $check, $username, $param, $noerr) = @_;
420
421 # syslog("info", "CHECK " . join(', ', @$check));
422
423 my $ind = 0;
424 my $test = $check->[$ind++];
425 die "no permission test specified" if !$test;
426
427 if ($test eq 'and') {
428 while (my $subcheck = $check->[$ind++]) {
429 $self->exec_api2_perm_check($subcheck, $username, $param);
430 }
431 return 1;
432 } elsif ($test eq 'or') {
433 while (my $subcheck = $check->[$ind++]) {
434 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
435 }
436 return 0 if $noerr;
437 raise_perm_exc();
438 } elsif ($test eq 'perm') {
439 my ($t, $tmplpath, $privs, %options) = @$check;
440 my $any = $options{any};
441 die "missing parameters" if !($tmplpath && $privs);
442 my $require_param = $options{require_param};
443 if ($require_param && !defined($param->{$require_param})) {
444 return 0 if $noerr;
445 raise_perm_exc();
446 }
447 my $path = PVE::Tools::template_replace($tmplpath, $param);
448 my $normpath = PVE::AccessControl::normalize_path($path);
449 warn "Failed to normalize '$path'\n" if !defined($normpath) && defined($path);
450
451 return $self->check_full($username, $normpath, $privs, $any, $noerr);
452 } elsif ($test eq 'userid-group') {
453 my $userid = $param->{userid};
454 my ($t, $privs, %options) = @$check;
455
456 my $check_existing_user = !$options{groups_param} || $options{groups_param} ne 'create';
457 return 0 if $check_existing_user && !$self->check_user_exist($userid, $noerr);
458
459 # check permission for ALL groups (and thus ALL users)
460 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
461 # list of groups $username has any of $privs on
462 my $groups = $self->filter_groups($username, $privs, 1);
463 if ($options{groups_param}) {
464 # does $username have any of $privs on all new/updated/.. groups?
465 my @group_param = PVE::Tools::split_list($param->{groups});
466 raise_perm_exc("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
467 foreach my $pg (@group_param) {
468 raise_perm_exc("/access/groups/$pg, " . join("|", @$privs))
469 if !$groups->{$pg};
470 }
471 }
472 if ($check_existing_user) {
473 # does $username have any of $privs on any existing group of $userid
474 my $allowed_users = $self->group_member_join([keys %$groups]);
475 if (!$allowed_users->{$userid}) {
476 return 0 if $noerr;
477 raise_perm_exc();
478 }
479 }
480 }
481 return 1;
482 } elsif ($test eq 'userid-param') {
483 my ($userid, undef, $realm) = PVE::AccessControl::verify_username($param->{userid});
484 my ($t, $subtest) = @$check;
485 die "missing parameters" if !$subtest;
486 if ($subtest eq 'self') {
487 return 0 if !$self->check_user_exist($userid, $noerr);
488 return 1 if $username eq $userid;
489 return 0 if $noerr;
490 raise_perm_exc();
491 } elsif ($subtest eq 'Realm.AllocateUser') {
492 my $path = "/access/realm/$realm";
493 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
494 } else {
495 die "unknown userid-param test";
496 }
497 } elsif ($test eq 'perm-modify') {
498 my ($t, $tmplpath) = @$check;
499 my $path = PVE::Tools::template_replace($tmplpath, $param);
500 $path = PVE::AccessControl::normalize_path($path);
501 return 0 if !defined($path); # should already die in API2::ACL
502 return $self->check_perm_modify($username, $path, $noerr);
503 } else {
504 die "unknown permission test";
505 }
506 };
507
508 sub check_api2_permissions {
509 my ($self, $perm, $username, $param) = @_;
510
511 return 1 if !$username && $perm->{user} && $perm->{user} eq 'world';
512
513 raise_perm_exc("user != null") if !$username;
514
515 return 1 if $username eq 'root@pam';
516
517 raise_perm_exc('user != root@pam') if !$perm;
518
519 return 1 if $perm->{user} && $perm->{user} eq 'all';
520
521 return $self->exec_api2_perm_check($perm->{check}, $username, $param)
522 if $perm->{check};
523
524 raise_perm_exc();
525 }
526
527 sub log_cluster_msg {
528 my ($self, $pri, $user, $msg) = @_;
529
530 PVE::Cluster::log_msg($pri, $user, $msg);
531 }
532
533 sub broadcast_tasklist {
534 my ($self, $tlist) = @_;
535
536 PVE::Cluster::broadcast_tasklist($tlist);
537 }
538
539 # initialize environment - must be called once at program startup
540 sub init {
541 my ($class, $type, %params) = @_;
542
543 $class = ref($class) || $class;
544
545 my $self = $class->SUPER::init($type, %params);
546
547 $self->{user_cfg} = {};
548 $self->{aclcache} = {};
549 $self->{aclversion} = undef;
550
551 return $self;
552 };
553
554
555 # init_request - must be called before each RPC request
556 sub init_request {
557 my ($self, %params) = @_;
558
559 PVE::Cluster::cfs_update();
560
561 $self->{result_attributes} = {};
562
563 my $userconfig; # we use this for regression tests
564 foreach my $p (keys %params) {
565 if ($p eq 'userconfig') {
566 $userconfig = $params{$p};
567 } else {
568 die "unknown parameter '$p'";
569 }
570 }
571
572 eval {
573 $self->{aclcache} = {};
574 if ($userconfig) {
575 my $ucdata = PVE::Tools::file_get_contents($userconfig);
576 my $cfg = PVE::AccessControl::parse_user_config($userconfig, $ucdata);
577 $self->{user_cfg} = $cfg;
578 } else {
579 my $ucvers = PVE::Cluster::cfs_file_version('user.cfg');
580 if (!$self->{aclcache} || !defined($self->{aclversion}) ||
581 !defined($ucvers) || ($ucvers ne $self->{aclversion})) {
582 $self->{aclversion} = $ucvers;
583 my $cfg = PVE::Cluster::cfs_read_file('user.cfg');
584 $self->{user_cfg} = $cfg;
585 }
586 }
587 };
588 if (my $err = $@) {
589 $self->{user_cfg} = {};
590 die "Unable to load access control list: $err";
591 }
592 }
593
594 # hacks: to provide better backwards compatibility
595
596 # old code uses PVE::RPCEnvironment::get();
597 # new code should use PVE::RPCEnvironment->get();
598 sub get {
599 return PVE::RESTEnvironment->get();
600 }
601
602 # old code uses PVE::RPCEnvironment::is_worker();
603 # new code should use PVE::RPCEnvironment->is_worker();
604 sub is_worker {
605 return PVE::RESTEnvironment->is_worker();
606 }
607
608 1;