]> git.proxmox.com Git - pve-access-control.git/blob - src/PVE/RPCEnvironment.pm
rpcenv: skip undef propagation flag in more places
[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 die "internal error" if $user eq 'root@pam';
27
28 my $cache = $self->{aclcache};
29 $cache->{$user} = {} if !$cache->{$user};
30 my $data = $cache->{$user};
31
32 my ($username, undef) = PVE::AccessControl::split_tokenid($user, 1);
33 die "internal error" if $username && $username ne 'root@pam' && !defined($cache->{$username});
34
35 if (!$data->{poolroles}) {
36 $data->{poolroles} = {};
37
38 foreach my $pool (keys %{$cfg->{pools}}) {
39 my $d = $cfg->{pools}->{$pool};
40 my $pool_roles = PVE::AccessControl::roles($cfg, $user, "/pool/$pool"); # pool roles
41 next if !scalar(keys %$pool_roles);
42 foreach my $vmid (keys %{$d->{vms}}) {
43 for my $role (keys %$pool_roles) {
44 $data->{poolroles}->{"/vms/$vmid"}->{$role} = 1;
45 }
46 }
47 foreach my $storeid (keys %{$d->{storage}}) {
48 for my $role (keys %$pool_roles) {
49 $data->{poolroles}->{"/storage/$storeid"}->{$role} = 1;
50 }
51 }
52 }
53 }
54
55 my $roles = PVE::AccessControl::roles($cfg, $user, $path);
56
57 # apply roles inherited from pools
58 # Note: assume we do not want to propagate those privs
59 if ($data->{poolroles}->{$path}) {
60 if (!defined($roles->{NoAccess})) {
61 if ($data->{poolroles}->{$path}->{NoAccess}) {
62 $roles = { 'NoAccess' => 0 };
63 } else {
64 foreach my $role (keys %{$data->{poolroles}->{$path}}) {
65 $roles->{$role} = 0 if !defined($roles->{$role});
66 }
67 }
68 }
69 }
70
71 $data->{roles}->{$path} = $roles;
72
73 my $privs = {};
74 foreach my $role (keys %$roles) {
75 if (my $privset = $cfg->{roles}->{$role}) {
76 foreach my $p (keys %$privset) {
77 $privs->{$p} = $roles->{$role};
78 }
79 }
80 }
81
82 if ($username && $username ne 'root@pam') {
83 # intersect user and token permissions
84 my $user_privs = $cache->{$username}->{privs}->{$path};
85 my $filtered_privs = [ grep { $user_privs->{$_} } keys %$privs ];
86 $privs = { map { $_ => $user_privs->{$_} && $privs->{$_} } @$filtered_privs };
87 }
88
89 $data->{privs}->{$path} = $privs;
90
91 return $privs;
92 };
93
94 sub permissions {
95 my ($self, $user, $path) = @_;
96
97 if ($user eq 'root@pam') { # root can do anything
98 my $cfg = $self->{user_cfg};
99 return { map { $_ => 1 } keys %{$cfg->{roles}->{'Administrator'}} };
100 }
101
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};
106
107 return {} if !$token_info;
108
109 # ensure cache for user is populated
110 my $user_perms = $self->permissions($username, $path);
111
112 # return user privs for non-privsep tokens
113 return $user_perms if !$token_info->{privsep};
114 } else {
115 $user = PVE::AccessControl::verify_username($user, 1);
116 return {} if !$user;
117 }
118
119 my $cache = $self->{aclcache};
120 $cache->{$user} = {} if !$cache->{$user};
121
122 my $acl = $cache->{$user};
123
124 my $perm = $acl->{privs}->{$path};
125 return $perm if $perm;
126
127 return &$compile_acl_path($self, $user, $path);
128 }
129
130 sub compute_api_permission {
131 my ($self, $authuser) = @_;
132
133 my $usercfg = $self->{user_cfg};
134
135 my $res = {};
136 my $priv_re_map = {
137 vms => qr/VM\.|Permissions\.Modify/,
138 access => qr/(User|Group)\.|Permissions\.Modify/,
139 storage => qr/Datastore\.|Permissions\.Modify/,
140 nodes => qr/Sys\.|Permissions\.Modify/,
141 sdn => qr/SDN\.|Permissions\.Modify/,
142 dc => qr/Sys\.Audit|SDN\./,
143 };
144 map { $res->{$_} = {} } keys %$priv_re_map;
145
146 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage', '/sdn'];
147
148 my $checked_paths = {};
149 foreach my $path (@$required_paths, keys %{$usercfg->{acl}}) {
150 next if $checked_paths->{$path};
151 $checked_paths->{$path} = 1;
152
153 my $path_perm = $self->permissions($authuser, $path);
154
155 my $toplevel = ($path =~ /^\/(\w+)/) ? $1 : 'dc';
156 if ($toplevel eq 'pool') {
157 foreach my $priv (keys %$path_perm) {
158 next if !defined($path_perm->{$priv});
159
160 if ($priv =~ m/^VM\./) {
161 $res->{vms}->{$priv} = 1;
162 } elsif ($priv =~ m/^Datastore\./) {
163 $res->{storage}->{$priv} = 1;
164 } elsif ($priv eq 'Permissions.Modify') {
165 $res->{storage}->{$priv} = 1;
166 $res->{vms}->{$priv} = 1;
167 }
168 }
169 } else {
170 my $priv_regex = $priv_re_map->{$toplevel} // next;
171 foreach my $priv (keys %$path_perm) {
172 next if !defined($path_perm->{$priv});
173
174 next if $priv !~ m/^($priv_regex)/;
175 $res->{$toplevel}->{$priv} = 1;
176 }
177 }
178 }
179
180 return $res;
181 }
182
183 sub get_effective_permissions {
184 my ($self, $user) = @_;
185
186 # default / top level paths
187 my $paths = {
188 '/' => 1,
189 '/access' => 1,
190 '/access/groups' => 1,
191 '/nodes' => 1,
192 '/pools' => 1,
193 '/sdn' => 1,
194 '/storage' => 1,
195 '/vms' => 1,
196 };
197
198 my $cfg = $self->{user_cfg};
199
200 # paths explicitly listed in ACLs
201 foreach my $acl_path (keys %{$cfg->{acl}}) {
202 $paths->{$acl_path} = 1;
203 }
204
205 # paths referenced by pool definitions
206 foreach my $pool (keys %{$cfg->{pools}}) {
207 my $d = $cfg->{pools}->{$pool};
208 foreach my $vmid (keys %{$d->{vms}}) {
209 $paths->{"/vms/$vmid"} = 1;
210 }
211 foreach my $storeid (keys %{$d->{storage}}) {
212 $paths->{"/storage/$storeid"} = 1;
213 }
214 }
215
216 my $perms = {};
217 foreach my $path (keys %$paths) {
218 my $path_perms = $self->permissions($user, $path);
219 foreach my $priv (keys %$path_perms) {
220 delete $path_perms->{$priv} if !defined($path_perms->{$priv});
221 }
222 # filter paths where user has NO permissions
223 $perms->{$path} = $path_perms if %$path_perms;
224 }
225 return $perms;
226 }
227
228 sub check {
229 my ($self, $user, $path, $privs, $noerr) = @_;
230
231 my $perm = $self->permissions($user, $path);
232
233 foreach my $priv (@$privs) {
234 PVE::AccessControl::verify_privname($priv);
235 if (!defined($perm->{$priv})) {
236 return undef if $noerr;
237 raise_perm_exc("$path, $priv");
238 }
239 };
240
241 return 1;
242 };
243
244 sub check_any {
245 my ($self, $user, $path, $privs, $noerr) = @_;
246
247 my $perm = $self->permissions($user, $path);
248
249 my $found = 0;
250 foreach my $priv (@$privs) {
251 PVE::AccessControl::verify_privname($priv);
252 if (defined($perm->{$priv})) {
253 $found = 1;
254 last;
255 }
256 };
257
258 return 1 if $found;
259
260 return undef if $noerr;
261
262 raise_perm_exc("$path, " . join("|", @$privs));
263 };
264
265 sub check_full {
266 my ($self, $username, $path, $privs, $any, $noerr) = @_;
267 if ($any) {
268 return $self->check_any($username, $path, $privs, $noerr);
269 } else {
270 return $self->check($username, $path, $privs, $noerr);
271 }
272 }
273
274 sub check_user_enabled {
275 my ($self, $user, $noerr) = @_;
276
277 my $cfg = $self->{user_cfg};
278 return PVE::AccessControl::check_user_enabled($cfg, $user, $noerr);
279 }
280
281 sub check_user_exist {
282 my ($self, $user, $noerr) = @_;
283
284 my $cfg = $self->{user_cfg};
285 return PVE::AccessControl::check_user_exist($cfg, $user, $noerr);
286 }
287
288 sub check_pool_exist {
289 my ($self, $pool, $noerr) = @_;
290
291 my $cfg = $self->{user_cfg};
292
293 return 1 if $cfg->{pools}->{$pool};
294
295 return undef if $noerr;
296
297 raise_perm_exc("pool '$pool' does not exist");
298 }
299
300 sub check_vm_perm {
301 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
302
303 my $cfg = $self->{user_cfg};
304
305 if ($pool) {
306 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
307 }
308 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
309 };
310
311 sub is_group_member {
312 my ($self, $group, $user) = @_;
313
314 my $cfg = $self->{user_cfg};
315
316 return 0 if !$cfg->{groups}->{$group};
317
318 return defined($cfg->{groups}->{$group}->{users}->{$user});
319 }
320
321 sub filter_groups {
322 my ($self, $user, $privs, $any) = @_;
323
324 my $cfg = $self->{user_cfg};
325
326 my $groups = {};
327 foreach my $group (keys %{$cfg->{groups}}) {
328 my $path = "/access/groups/$group";
329 if ($self->check_full($user, $path, $privs, $any, 1)) {
330 $groups->{$group} = $cfg->{groups}->{$group};
331 }
332 }
333
334 return $groups;
335 }
336
337 sub group_member_join {
338 my ($self, $grouplist) = @_;
339
340 my $users = {};
341
342 my $cfg = $self->{user_cfg};
343 foreach my $group (@$grouplist) {
344 my $data = $cfg->{groups}->{$group};
345 next if !$data;
346 foreach my $user (keys %{$data->{users}}) {
347 $users->{$user} = 1;
348 }
349 }
350
351 return $users;
352 }
353
354 sub check_perm_modify {
355 my ($self, $username, $path, $noerr) = @_;
356
357 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
358
359 my $testperms = [ 'Permissions.Modify' ];
360 if ($path =~ m|^/storage/.+$|) {
361 push @$testperms, 'Datastore.Allocate';
362 } elsif ($path =~ m|^/vms/.+$|) {
363 push @$testperms, 'VM.Allocate';
364 } elsif ($path =~ m|^/pool/.+$|) {
365 push @$testperms, 'Pool.Allocate';
366 }
367
368 return $self->check_any($username, $path, $testperms, $noerr);
369 }
370
371 sub exec_api2_perm_check {
372 my ($self, $check, $username, $param, $noerr) = @_;
373
374 # syslog("info", "CHECK " . join(', ', @$check));
375
376 my $ind = 0;
377 my $test = $check->[$ind++];
378 die "no permission test specified" if !$test;
379
380 if ($test eq 'and') {
381 while (my $subcheck = $check->[$ind++]) {
382 $self->exec_api2_perm_check($subcheck, $username, $param);
383 }
384 return 1;
385 } elsif ($test eq 'or') {
386 while (my $subcheck = $check->[$ind++]) {
387 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
388 }
389 return 0 if $noerr;
390 raise_perm_exc();
391 } elsif ($test eq 'perm') {
392 my ($t, $tmplpath, $privs, %options) = @$check;
393 my $any = $options{any};
394 die "missing parameters" if !($tmplpath && $privs);
395 my $require_param = $options{require_param};
396 if ($require_param && !defined($param->{$require_param})) {
397 return 0 if $noerr;
398 raise_perm_exc();
399 }
400 my $path = PVE::Tools::template_replace($tmplpath, $param);
401 $path = PVE::AccessControl::normalize_path($path);
402 return $self->check_full($username, $path, $privs, $any, $noerr);
403 } elsif ($test eq 'userid-group') {
404 my $userid = $param->{userid};
405 my ($t, $privs, %options) = @$check;
406 return 0 if !$options{groups_param} && !$self->check_user_exist($userid, $noerr);
407 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
408 my $groups = $self->filter_groups($username, $privs, 1);
409 if ($options{groups_param}) {
410 my @group_param = PVE::Tools::split_list($param->{groups});
411 raise_perm_exc("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
412 foreach my $pg (@group_param) {
413 raise_perm_exc("/access/groups/$pg, " . join("|", @$privs))
414 if !$groups->{$pg};
415 }
416 } else {
417 my $allowed_users = $self->group_member_join([keys %$groups]);
418 if (!$allowed_users->{$userid}) {
419 return 0 if $noerr;
420 raise_perm_exc();
421 }
422 }
423 }
424 return 1;
425 } elsif ($test eq 'userid-param') {
426 my ($userid, undef, $realm) = PVE::AccessControl::verify_username($param->{userid});
427 my ($t, $subtest) = @$check;
428 die "missing parameters" if !$subtest;
429 if ($subtest eq 'self') {
430 return 0 if !$self->check_user_exist($userid, $noerr);
431 return 1 if $username eq $userid;
432 return 0 if $noerr;
433 raise_perm_exc();
434 } elsif ($subtest eq 'Realm.AllocateUser') {
435 my $path = "/access/realm/$realm";
436 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
437 } else {
438 die "unknown userid-param test";
439 }
440 } elsif ($test eq 'perm-modify') {
441 my ($t, $tmplpath) = @$check;
442 my $path = PVE::Tools::template_replace($tmplpath, $param);
443 $path = PVE::AccessControl::normalize_path($path);
444 return $self->check_perm_modify($username, $path, $noerr);
445 } else {
446 die "unknown permission test";
447 }
448 };
449
450 sub check_api2_permissions {
451 my ($self, $perm, $username, $param) = @_;
452
453 return 1 if !$username && $perm->{user} && $perm->{user} eq 'world';
454
455 raise_perm_exc("user != null") if !$username;
456
457 return 1 if $username eq 'root@pam';
458
459 raise_perm_exc('user != root@pam') if !$perm;
460
461 return 1 if $perm->{user} && $perm->{user} eq 'all';
462
463 return $self->exec_api2_perm_check($perm->{check}, $username, $param)
464 if $perm->{check};
465
466 raise_perm_exc();
467 }
468
469 sub log_cluster_msg {
470 my ($self, $pri, $user, $msg) = @_;
471
472 PVE::Cluster::log_msg($pri, $user, $msg);
473 }
474
475 sub broadcast_tasklist {
476 my ($self, $tlist) = @_;
477
478 PVE::Cluster::broadcast_tasklist($tlist);
479 }
480
481 # initialize environment - must be called once at program startup
482 sub init {
483 my ($class, $type, %params) = @_;
484
485 $class = ref($class) || $class;
486
487 my $self = $class->SUPER::init($type, %params);
488
489 $self->{user_cfg} = {};
490 $self->{aclcache} = {};
491 $self->{aclversion} = undef;
492
493 return $self;
494 };
495
496
497 # init_request - must be called before each RPC request
498 sub init_request {
499 my ($self, %params) = @_;
500
501 PVE::Cluster::cfs_update();
502
503 $self->{result_attributes} = {};
504
505 my $userconfig; # we use this for regression tests
506 foreach my $p (keys %params) {
507 if ($p eq 'userconfig') {
508 $userconfig = $params{$p};
509 } else {
510 die "unknown parameter '$p'";
511 }
512 }
513
514 eval {
515 $self->{aclcache} = {};
516 if ($userconfig) {
517 my $ucdata = PVE::Tools::file_get_contents($userconfig);
518 my $cfg = PVE::AccessControl::parse_user_config($userconfig, $ucdata);
519 $self->{user_cfg} = $cfg;
520 } else {
521 my $ucvers = PVE::Cluster::cfs_file_version('user.cfg');
522 if (!$self->{aclcache} || !defined($self->{aclversion}) ||
523 !defined($ucvers) || ($ucvers ne $self->{aclversion})) {
524 $self->{aclversion} = $ucvers;
525 my $cfg = PVE::Cluster::cfs_read_file('user.cfg');
526 $self->{user_cfg} = $cfg;
527 }
528 }
529 };
530 if (my $err = $@) {
531 $self->{user_cfg} = {};
532 die "Unable to load access control list: $err";
533 }
534 }
535
536 # hacks: to provide better backwards compatibiliy
537
538 # old code uses PVE::RPCEnvironment::get();
539 # new code should use PVE::RPCEnvironment->get();
540 sub get {
541 return PVE::RESTEnvironment->get();
542 }
543
544 # old code uses PVE::RPCEnvironment::is_worker();
545 # new code should use PVE::RPCEnvironment->is_worker();
546 sub is_worker {
547 return PVE::RESTEnvironment->is_worker();
548 }
549
550 1;