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