]> git.proxmox.com Git - pve-access-control.git/blame - src/PVE/RPCEnvironment.pm
bump version to 8.1.4
[pve-access-control.git] / src / PVE / RPCEnvironment.pm
CommitLineData
2c3a6c0a
DM
1package PVE::RPCEnvironment;
2
3use strict;
4use warnings;
c104e4ab 5
5e868938
TL
6use PVE::AccessControl;
7use PVE::Cluster;
060941d4 8use PVE::Exception qw(raise raise_param_exc raise_perm_exc);
2c3a6c0a 9use PVE::INotify;
2c3a6c0a 10use PVE::ProcFSTools;
5e868938
TL
11use PVE::RESTEnvironment;
12use PVE::SafeSyslog;
13use PVE::Tools;
2c3a6c0a 14
c104e4ab 15use base qw(PVE::RESTEnvironment);
2c3a6c0a 16
2c3a6c0a
DM
17# ACL cache
18
4bc17477
DM
19my $compile_acl_path = sub {
20 my ($self, $user, $path) = @_;
2c3a6c0a 21
2c3a6c0a
DM
22 my $cfg = $self->{user_cfg};
23
24 return undef if !$cfg->{roles};
25
b8c4218b 26 # permissions() has an early return for this case
4bc17477 27 die "internal error" if $user eq 'root@pam';
2c3a6c0a 28
4bc17477
DM
29 my $cache = $self->{aclcache};
30 $cache->{$user} = {} if !$cache->{$user};
31 my $data = $cache->{$user};
2c3a6c0a 32
b8c4218b 33 # permissions() will always prime the cache for the owning user
e915e9e4
FG
34 my ($username, undef) = PVE::AccessControl::split_tokenid($user, 1);
35 die "internal error" if $username && $username ne 'root@pam' && !defined($cache->{$username});
36
b8c4218b 37 # resolve and cache roles of the current user/token for all pool ACL paths
4bc17477 38 if (!$data->{poolroles}) {
c104e4ab
DM
39 $data->{poolroles} = {};
40
39c85db8
DM
41 foreach my $pool (keys %{$cfg->{pools}}) {
42 my $d = $cfg->{pools}->{$pool};
7e8bcaa7
FG
43 my $pool_roles = PVE::AccessControl::roles($cfg, $user, "/pool/$pool"); # pool roles
44 next if !scalar(keys %$pool_roles);
4bc17477 45 foreach my $vmid (keys %{$d->{vms}}) {
7e8bcaa7 46 for my $role (keys %$pool_roles) {
4bc17477 47 $data->{poolroles}->{"/vms/$vmid"}->{$role} = 1;
2c3a6c0a
DM
48 }
49 }
4bc17477 50 foreach my $storeid (keys %{$d->{storage}}) {
7e8bcaa7 51 for my $role (keys %$pool_roles) {
4bc17477
DM
52 $data->{poolroles}->{"/storage/$storeid"}->{$role} = 1;
53 }
54 }
55 }
56 }
57
b8c4218b
FG
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
7e8bcaa7 63 my $roles = PVE::AccessControl::roles($cfg, $user, $path);
4bc17477
DM
64
65 # apply roles inherited from pools
4bc17477 66 if ($data->{poolroles}->{$path}) {
b8c4218b 67 # NoAccess must not be trumped by pool ACLs
7e8bcaa7 68 if (!defined($roles->{NoAccess})) {
8ade28e6 69 if ($data->{poolroles}->{$path}->{NoAccess}) {
b8c4218b 70 # but pool ACL NoAccess trumps regular ACL
7e8bcaa7 71 $roles = { 'NoAccess' => 0 };
8ade28e6
DM
72 } else {
73 foreach my $role (keys %{$data->{poolroles}->{$path}}) {
b8c4218b
FG
74 # only use role from pool ACL if regular ACL didn't already
75 # set it, and never set propagation for pool-derived ACLs
7e8bcaa7 76 $roles->{$role} = 0 if !defined($roles->{$role});
8ade28e6 77 }
4bc17477 78 }
2c3a6c0a 79 }
4bc17477 80 }
2c3a6c0a 81
b8c4218b 82 # cache roles
7e8bcaa7 83 $data->{roles}->{$path} = $roles;
c104e4ab 84
b8c4218b
FG
85 # derive privs from set roles - hash mapping privilege name to propagation
86 # flag value, a key being defined means the priv is set
4bc17477 87 my $privs = {};
7e8bcaa7 88 foreach my $role (keys %$roles) {
4bc17477
DM
89 if (my $privset = $cfg->{roles}->{$role}) {
90 foreach my $p (keys %$privset) {
b8c4218b
FG
91 # set priv '$p' to propagated iff any of the set roles
92 # containing it have the propagated flag set
b55e33f4 93 $privs->{$p} ||= $roles->{$role};
4bc17477
DM
94 }
95 }
2c3a6c0a 96 }
e915e9e4 97
b8c4218b 98 # intersect user and token permissions
e915e9e4 99 if ($username && $username ne 'root@pam') {
b8c4218b 100 # map of set privs to their propagation flag value, for the owning user
e915e9e4 101 my $user_privs = $cache->{$username}->{privs}->{$path};
b8c4218b 102 # list of privs set both for token and owning user
0716a56b 103 my $filtered_privs = [ grep { defined($user_privs->{$_}) } keys %$privs ];
b8c4218b
FG
104 # intersection of privs using filtered list, combining both propagation
105 # flags
e8a0cee4 106 $privs = { map { $_ => $user_privs->{$_} && $privs->{$_} } @$filtered_privs };
e915e9e4
FG
107 }
108
9aa49a8d
FG
109 foreach my $priv (keys %$privs) {
110 # safeguard, this should never happen anyway
111 delete $privs->{$priv} if !defined($privs->{$priv});
112 }
113
b8c4218b 114 # cache privs
4bc17477 115 $data->{privs}->{$path} = $privs;
2c3a6c0a 116
4bc17477 117 return $privs;
2c3a6c0a
DM
118};
119
b8c4218b
FG
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
2c3a6c0a
DM
135sub permissions {
136 my ($self, $user, $path) = @_;
137
4bc17477
DM
138 if ($user eq 'root@pam') { # root can do anything
139 my $cfg = $self->{user_cfg};
7e8bcaa7 140 return { map { $_ => 1 } keys %{$cfg->{roles}->{'Administrator'}} };
c104e4ab 141 }
4bc17477 142
37d3c16b
FG
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
e915e9e4
FG
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};
7e8bcaa7 153
e915e9e4
FG
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 }
2c3a6c0a
DM
165
166 my $cache = $self->{aclcache};
4bc17477 167 $cache->{$user} = {} if !$cache->{$user};
2c3a6c0a
DM
168
169 my $acl = $cache->{$user};
170
4bc17477
DM
171 my $perm = $acl->{privs}->{$path};
172 return $perm if $perm;
2c3a6c0a 173
4bc17477 174 return &$compile_acl_path($self, $user, $path);
2c3a6c0a
DM
175}
176
ac344d7d
DM
177sub 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/,
c27eb245 189 dc => qr/Sys\.Audit|Sys\.Modify|SDN\./,
8b5fd2e6 190 mapping => qr/Mapping\.|Permissions.Modify/,
ac344d7d
DM
191 };
192 map { $res->{$_} = {} } keys %$priv_re_map;
193
8b5fd2e6 194 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage', '/sdn', '/mapping'];
170cf17b
FG
195 my $defined_paths = [];
196 PVE::AccessControl::iterate_acl_tree("/", $usercfg->{acl_root}, sub {
197 my ($path, $node) = @_;
198 push @$defined_paths, $path;
199 });
ac344d7d
DM
200
201 my $checked_paths = {};
170cf17b 202 foreach my $path (@$required_paths, @$defined_paths) {
ac344d7d
DM
203 next if $checked_paths->{$path};
204 $checked_paths->{$path} = 1;
205
206 my $path_perm = $self->permissions($authuser, $path);
207
208 my $toplevel = ($path =~ /^\/(\w+)/) ? $1 : 'dc';
209 if ($toplevel eq 'pool') {
210 foreach my $priv (keys %$path_perm) {
0786c1e5
FG
211 next if !defined($path_perm->{$priv});
212
ac344d7d
DM
213 if ($priv =~ m/^VM\./) {
214 $res->{vms}->{$priv} = 1;
215 } elsif ($priv =~ m/^Datastore\./) {
216 $res->{storage}->{$priv} = 1;
217 } elsif ($priv eq 'Permissions.Modify') {
218 $res->{storage}->{$priv} = 1;
219 $res->{vms}->{$priv} = 1;
220 }
221 }
222 } else {
223 my $priv_regex = $priv_re_map->{$toplevel} // next;
224 foreach my $priv (keys %$path_perm) {
0786c1e5
FG
225 next if !defined($path_perm->{$priv});
226
ac344d7d
DM
227 next if $priv !~ m/^($priv_regex)/;
228 $res->{$toplevel}->{$priv} = 1;
229 }
230 }
231 }
232
233 return $res;
234}
235
c3fa8a36
FG
236sub get_effective_permissions {
237 my ($self, $user) = @_;
238
239 # default / top level paths
240 my $paths = {
241 '/' => 1,
242 '/access' => 1,
243 '/access/groups' => 1,
244 '/nodes' => 1,
3d7afd6f 245 '/pool' => 1,
92f571d9 246 '/sdn' => 1,
c3fa8a36
FG
247 '/storage' => 1,
248 '/vms' => 1,
249 };
250
251 my $cfg = $self->{user_cfg};
252
253 # paths explicitly listed in ACLs
170cf17b
FG
254 PVE::AccessControl::iterate_acl_tree("/", $cfg->{acl_root}, sub {
255 my ($path, $node) = @_;
256 $paths->{$path} = 1;
257 });
c3fa8a36
FG
258
259 # paths referenced by pool definitions
260 foreach my $pool (keys %{$cfg->{pools}}) {
261 my $d = $cfg->{pools}->{$pool};
262 foreach my $vmid (keys %{$d->{vms}}) {
263 $paths->{"/vms/$vmid"} = 1;
264 }
265 foreach my $storeid (keys %{$d->{storage}}) {
266 $paths->{"/storage/$storeid"} = 1;
267 }
268 }
269
270 my $perms = {};
271 foreach my $path (keys %$paths) {
272 my $path_perms = $self->permissions($user, $path);
0786c1e5
FG
273 foreach my $priv (keys %$path_perms) {
274 delete $path_perms->{$priv} if !defined($path_perms->{$priv});
275 }
c3fa8a36
FG
276 # filter paths where user has NO permissions
277 $perms->{$path} = $path_perms if %$path_perms;
278 }
279 return $perms;
280}
281
2c3a6c0a 282sub check {
37d45deb 283 my ($self, $user, $path, $privs, $noerr) = @_;
2c3a6c0a
DM
284
285 my $perm = $self->permissions($user, $path);
286
287 foreach my $priv (@$privs) {
37d45deb 288 PVE::AccessControl::verify_privname($priv);
7e8bcaa7 289 if (!defined($perm->{$priv})) {
37d45deb 290 return undef if $noerr;
c104e4ab 291 raise_perm_exc("$path, $priv");
37d45deb 292 }
2c3a6c0a
DM
293 };
294
295 return 1;
296};
297
37d45deb
DM
298sub check_any {
299 my ($self, $user, $path, $privs, $noerr) = @_;
300
301 my $perm = $self->permissions($user, $path);
efce1d57 302
37d45deb
DM
303 my $found = 0;
304 foreach my $priv (@$privs) {
305 PVE::AccessControl::verify_privname($priv);
7e8bcaa7 306 if (defined($perm->{$priv})) {
37d45deb
DM
307 $found = 1;
308 last;
309 }
310 };
311
312 return 1 if $found;
313
314 return undef if $noerr;
315
c104e4ab 316 raise_perm_exc("$path, " . join("|", @$privs));
37d45deb
DM
317};
318
c4a776a6
DM
319sub check_full {
320 my ($self, $username, $path, $privs, $any, $noerr) = @_;
321 if ($any) {
322 return $self->check_any($username, $path, $privs, $noerr);
323 } else {
324 return $self->check($username, $path, $privs, $noerr);
325 }
326}
327
3c97bee5 328# check for any fashion of access to vnet/bridge
a5616d5c
AD
329sub check_sdn_bridge {
330 my ($self, $username, $zone, $bridge, $privs, $noerr) = @_;
331
332 my $path = "/sdn/zones/$zone/$bridge";
e1ea58c8
FG
333 # check access to bridge itself
334 return 1 if $self->check_any($username, $path, $privs, 1);
335
a5616d5c
AD
336 my $cfg = $self->{user_cfg};
337 my $bridge_acl = PVE::AccessControl::find_acl_tree_node($cfg->{acl_root}, $path);
338 if ($bridge_acl) {
3c97bee5 339 # check access to VLANs
a5616d5c
AD
340 my $vlans = $bridge_acl->{children};
341 for my $vlan (keys %$vlans) {
342 my $vlanpath = "$path/$vlan";
3c97bee5 343 return 1 if $self->check_any($username, $vlanpath, $privs, 1);
a5616d5c 344 }
a5616d5c 345 }
3c97bee5
FG
346
347 # repeat check, but fatal
348 $self->check_any($username, $path, $privs, 0) if !$noerr;
349
a5616d5c
AD
350 return;
351}
352
7070c1ae
DM
353sub check_user_enabled {
354 my ($self, $user, $noerr) = @_;
c104e4ab 355
2c3a6c0a 356 my $cfg = $self->{user_cfg};
7070c1ae 357 return PVE::AccessControl::check_user_enabled($cfg, $user, $noerr);
2c3a6c0a
DM
358}
359
37d45deb
DM
360sub check_user_exist {
361 my ($self, $user, $noerr) = @_;
c104e4ab 362
37d45deb
DM
363 my $cfg = $self->{user_cfg};
364 return PVE::AccessControl::check_user_exist($cfg, $user, $noerr);
365}
366
a23cec1f
DM
367sub check_pool_exist {
368 my ($self, $pool, $noerr) = @_;
369
370 my $cfg = $self->{user_cfg};
371
372 return 1 if $cfg->{pools}->{$pool};
373
374 return undef if $noerr;
375
c104e4ab 376 raise_perm_exc("pool '$pool' does not exist");
a23cec1f
DM
377}
378
379sub check_vm_perm {
380 my ($self, $user, $vmid, $pool, $privs, $any, $noerr) = @_;
381
382 my $cfg = $self->{user_cfg};
c104e4ab 383
a23cec1f
DM
384 if ($pool) {
385 return if $self->check_full($user, "/pool/$pool", $privs, $any, 1);
386 }
387 return $self->check_full($user, "/vms/$vmid", $privs, $any, $noerr);
388};
389
37d45deb
DM
390sub is_group_member {
391 my ($self, $group, $user) = @_;
392
393 my $cfg = $self->{user_cfg};
394
395 return 0 if !$cfg->{groups}->{$group};
396
397 return defined($cfg->{groups}->{$group}->{users}->{$user});
398}
399
400sub filter_groups {
b9180ed2 401 my ($self, $user, $privs, $any) = @_;
37d45deb
DM
402
403 my $cfg = $self->{user_cfg};
404
405 my $groups = {};
406 foreach my $group (keys %{$cfg->{groups}}) {
b9180ed2 407 my $path = "/access/groups/$group";
c4a776a6
DM
408 if ($self->check_full($user, $path, $privs, $any, 1)) {
409 $groups->{$group} = $cfg->{groups}->{$group};
37d45deb
DM
410 }
411 }
412
413 return $groups;
414}
415
416sub group_member_join {
417 my ($self, $grouplist) = @_;
418
419 my $users = {};
420
421 my $cfg = $self->{user_cfg};
422 foreach my $group (@$grouplist) {
423 my $data = $cfg->{groups}->{$group};
424 next if !$data;
425 foreach my $user (keys %{$data->{users}}) {
426 $users->{$user} = 1;
427 }
428 }
429
430 return $users;
431}
432
e3a3a0d7
DM
433sub check_perm_modify {
434 my ($self, $username, $path, $noerr) = @_;
435
436 return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
437
438 my $testperms = [ 'Permissions.Modify' ];
439 if ($path =~ m|^/storage/.+$|) {
440 push @$testperms, 'Datastore.Allocate';
441 } elsif ($path =~ m|^/vms/.+$|) {
442 push @$testperms, 'VM.Allocate';
7a7a517a
DM
443 } elsif ($path =~ m|^/pool/.+$|) {
444 push @$testperms, 'Pool.Allocate';
e3a3a0d7
DM
445 }
446
447 return $self->check_any($username, $path, $testperms, $noerr);
448}
449
f8cc5a5f
DM
450sub exec_api2_perm_check {
451 my ($self, $check, $username, $param, $noerr) = @_;
452
453 # syslog("info", "CHECK " . join(', ', @$check));
454
455 my $ind = 0;
456 my $test = $check->[$ind++];
457 die "no permission test specified" if !$test;
c104e4ab 458
f8cc5a5f
DM
459 if ($test eq 'and') {
460 while (my $subcheck = $check->[$ind++]) {
c104e4ab 461 $self->exec_api2_perm_check($subcheck, $username, $param);
f8cc5a5f
DM
462 }
463 return 1;
464 } elsif ($test eq 'or') {
465 while (my $subcheck = $check->[$ind++]) {
c104e4ab 466 return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
f8cc5a5f
DM
467 }
468 return 0 if $noerr;
469 raise_perm_exc();
470 } elsif ($test eq 'perm') {
471 my ($t, $tmplpath, $privs, %options) = @$check;
472 my $any = $options{any};
473 die "missing parameters" if !($tmplpath && $privs);
c4a776a6
DM
474 my $require_param = $options{require_param};
475 if ($require_param && !defined($param->{$require_param})) {
476 return 0 if $noerr;
477 raise_perm_exc();
478 }
f8cc5a5f 479 my $path = PVE::Tools::template_replace($tmplpath, $param);
37d3c16b
FG
480 my $normpath = PVE::AccessControl::normalize_path($path);
481 warn "Failed to normalize '$path'\n" if !defined($normpath) && defined($path);
482
483 return $self->check_full($username, $normpath, $privs, $any, $noerr);
f8cc5a5f
DM
484 } elsif ($test eq 'userid-group') {
485 my $userid = $param->{userid};
486 my ($t, $privs, %options) = @$check;
aee071ad
FG
487
488 my $check_existing_user = !$options{groups_param} || $options{groups_param} ne 'create';
489 return 0 if $check_existing_user && !$self->check_user_exist($userid, $noerr);
490
491 # check permission for ALL groups (and thus ALL users)
82b63965 492 if (!$self->check_any($username, "/access/groups", $privs, 1)) {
aee071ad 493 # list of groups $username has any of $privs on
f8cc5a5f
DM
494 my $groups = $self->filter_groups($username, $privs, 1);
495 if ($options{groups_param}) {
aee071ad 496 # does $username have any of $privs on all new/updated/.. groups?
f8cc5a5f 497 my @group_param = PVE::Tools::split_list($param->{groups});
82b63965 498 raise_perm_exc("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
f8cc5a5f
DM
499 foreach my $pg (@group_param) {
500 raise_perm_exc("/access/groups/$pg, " . join("|", @$privs))
501 if !$groups->{$pg};
502 }
aee071ad
FG
503 }
504 if ($check_existing_user) {
505 # does $username have any of $privs on any existing group of $userid
f8cc5a5f
DM
506 my $allowed_users = $self->group_member_join([keys %$groups]);
507 if (!$allowed_users->{$userid}) {
508 return 0 if $noerr;
509 raise_perm_exc();
510 }
511 }
512 }
513 return 1;
514 } elsif ($test eq 'userid-param') {
09d27058 515 my ($userid, undef, $realm) = PVE::AccessControl::verify_username($param->{userid});
f8cc5a5f
DM
516 my ($t, $subtest) = @$check;
517 die "missing parameters" if !$subtest;
518 if ($subtest eq 'self') {
a69bbe2e 519 return 0 if !$self->check_user_exist($userid, $noerr);
1cf154b7 520 return 1 if $username eq $userid;
f8cc5a5f
DM
521 return 0 if $noerr;
522 raise_perm_exc();
82b63965
DM
523 } elsif ($subtest eq 'Realm.AllocateUser') {
524 my $path = "/access/realm/$realm";
525 return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
f8cc5a5f
DM
526 } else {
527 die "unknown userid-param test";
528 }
c870b202 529 } elsif ($test eq 'perm-modify') {
e3a3a0d7
DM
530 my ($t, $tmplpath) = @$check;
531 my $path = PVE::Tools::template_replace($tmplpath, $param);
532 $path = PVE::AccessControl::normalize_path($path);
37d3c16b 533 return 0 if !defined($path); # should already die in API2::ACL
e3a3a0d7 534 return $self->check_perm_modify($username, $path, $noerr);
c870b202 535 } else {
f8cc5a5f
DM
536 die "unknown permission test";
537 }
538};
539
540sub check_api2_permissions {
541 my ($self, $perm, $username, $param) = @_;
542
d146e520 543 return 1 if !$username && $perm->{user} && $perm->{user} eq 'world';
f8cc5a5f
DM
544
545 raise_perm_exc("user != null") if !$username;
546
547 return 1 if $username eq 'root@pam';
548
549 raise_perm_exc('user != root@pam') if !$perm;
550
551 return 1 if $perm->{user} && $perm->{user} eq 'all';
552
c104e4ab 553 return $self->exec_api2_perm_check($perm->{check}, $username, $param)
f8cc5a5f
DM
554 if $perm->{check};
555
556 raise_perm_exc();
557}
558
c104e4ab
DM
559sub log_cluster_msg {
560 my ($self, $pri, $user, $msg) = @_;
2c3a6c0a 561
c104e4ab
DM
562 PVE::Cluster::log_msg($pri, $user, $msg);
563}
2c3a6c0a 564
c104e4ab
DM
565sub broadcast_tasklist {
566 my ($self, $tlist) = @_;
2c3a6c0a 567
c104e4ab
DM
568 PVE::Cluster::broadcast_tasklist($tlist);
569}
2c3a6c0a 570
c104e4ab
DM
571# initialize environment - must be called once at program startup
572sub init {
573 my ($class, $type, %params) = @_;
86c4f1e6
DM
574
575 $class = ref($class) || $class;
5ae5900d 576
c104e4ab 577 my $self = $class->SUPER::init($type, %params);
5ae5900d 578
c104e4ab
DM
579 $self->{user_cfg} = {};
580 $self->{aclcache} = {};
581 $self->{aclversion} = undef;
2c3a6c0a 582
c104e4ab
DM
583 return $self;
584};
2c3a6c0a 585
2c3a6c0a
DM
586
587# init_request - must be called before each RPC request
588sub init_request {
589 my ($self, %params) = @_;
590
591 PVE::Cluster::cfs_update();
592
be6ea723 593 $self->{result_attributes} = {};
272fe9ff 594
2c3a6c0a
DM
595 my $userconfig; # we use this for regression tests
596 foreach my $p (keys %params) {
597 if ($p eq 'userconfig') {
598 $userconfig = $params{$p};
599 } else {
600 die "unknown parameter '$p'";
601 }
602 }
603
604 eval {
605 $self->{aclcache} = {};
606 if ($userconfig) {
607 my $ucdata = PVE::Tools::file_get_contents($userconfig);
608 my $cfg = PVE::AccessControl::parse_user_config($userconfig, $ucdata);
609 $self->{user_cfg} = $cfg;
610 } else {
c104e4ab
DM
611 my $ucvers = PVE::Cluster::cfs_file_version('user.cfg');
612 if (!$self->{aclcache} || !defined($self->{aclversion}) ||
2c3a6c0a
DM
613 !defined($ucvers) || ($ucvers ne $self->{aclversion})) {
614 $self->{aclversion} = $ucvers;
615 my $cfg = PVE::Cluster::cfs_read_file('user.cfg');
616 $self->{user_cfg} = $cfg;
617 }
618 }
619 };
620 if (my $err = $@) {
621 $self->{user_cfg} = {};
622 die "Unable to load access control list: $err";
623 }
624}
625
7d23b7ca 626# hacks: to provide better backwards compatibility
2c3a6c0a 627
c104e4ab
DM
628# old code uses PVE::RPCEnvironment::get();
629# new code should use PVE::RPCEnvironment->get();
630sub get {
631 return PVE::RESTEnvironment->get();
2c3a6c0a
DM
632}
633
c104e4ab
DM
634# old code uses PVE::RPCEnvironment::is_worker();
635# new code should use PVE::RPCEnvironment->is_worker();
7b6dfe82 636sub is_worker {
c104e4ab 637 return PVE::RESTEnvironment->is_worker();
2c3a6c0a
DM
638}
639
90faf488 640# Permission helper for TFA and password API endpoints modifying users.
060941d4
WB
641# Only root may modify root, regular users need to specify their password.
642#
90faf488
WB
643# Returns the same as `verify_username` in list context (userid, ruid, realm),
644# or just the userid in scalar context.
645sub reauth_user_for_user_modification : prototype($$$$;$) {
646 my ($rpcenv, $authuser, $userid, $password, $param_name) = @_;
060941d4 647
90faf488
WB
648 $param_name //= 'password';
649
650 ($userid, my $ruid, my $realm) = PVE::AccessControl::verify_username($userid);
060941d4
WB
651 $rpcenv->check_user_exist($userid);
652
653 raise_perm_exc() if $userid eq 'root@pam' && $authuser ne 'root@pam';
654
655 # Regular users need to confirm their password to change TFA settings.
656 if ($authuser ne 'root@pam') {
90faf488 657 raise_param_exc({ $param_name => 'password is required to modify user' })
060941d4
WB
658 if !defined($password);
659
660 ($authuser, my $auth_username, my $auth_realm) =
661 PVE::AccessControl::verify_username($authuser);
662
663 my $domain_cfg = PVE::Cluster::cfs_read_file('domains.cfg');
664 my $cfg = $domain_cfg->{ids}->{$auth_realm};
665 die "auth domain '$auth_realm' does not exist\n" if !$cfg;
666 my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
667 $plugin->authenticate_user($cfg, $auth_realm, $auth_username, $password);
668 }
669
90faf488 670 return wantarray ? ($userid, $ruid, $realm) : $userid;
060941d4
WB
671}
672
2c3a6c0a 6731;