1 package PVE
::API2
::ACL
;
5 use PVE
::Cluster qw
(cfs_read_file cfs_write_file
);
6 use PVE
::Tools
qw(split_list);
7 use PVE
::AccessControl
;
8 use PVE
::Exception
qw(raise_param_exc);
9 use PVE
::JSONSchema
qw(get_standard_option register_standard_option);
15 use base
qw(PVE::RESTHandler);
17 register_standard_option
('acl-propagate', {
18 description
=> "Allow to propagate (inherit) permissions.",
23 register_standard_option
('acl-path', {
24 description
=> "Access control path",
28 __PACKAGE__-
>register_method ({
32 description
=> "Get Access Control List (ACLs).",
34 description
=> "The returned list is restricted to objects where you have rights to modify permissions.",
38 additionalProperties
=> 0,
45 additionalProperties
=> 0,
47 propagate
=> get_standard_option
('acl-propagate'),
48 path
=> get_standard_option
('acl-path'),
49 type
=> { type
=> 'string', enum
=> ['user', 'group', 'token'] },
50 ugid
=> { type
=> 'string' },
51 roleid
=> { type
=> 'string' },
58 my $rpcenv = PVE
::RPCEnvironment
::get
();
59 my $authuser = $rpcenv->get_user();
62 my $usercfg = $rpcenv->{user_cfg
};
63 if (!$usercfg || !$usercfg->{acl
}) {
67 my $audit = $rpcenv->check($authuser, '/access', ['Sys.Audit'], 1);
69 my $acl = $usercfg->{acl
};
70 foreach my $path (keys %$acl) {
71 foreach my $type (qw(user group token)) {
72 my $d = $acl->{$path}->{"${type}s"};
74 next if !($audit || $rpcenv->check_perm_modify($authuser, $path, 1));
75 foreach my $id (keys %$d) {
76 foreach my $role (keys %{$d->{$id}}) {
77 my $propagate = $d->{$id}->{$role};
83 propagate
=> $propagate,
93 __PACKAGE__-
>register_method ({
99 check
=> ['perm-modify', '{path}'],
101 description
=> "Update Access Control List (add or remove permissions).",
103 additionalProperties
=> 0,
105 propagate
=> get_standard_option
('acl-propagate'),
106 path
=> get_standard_option
('acl-path'),
108 description
=> "List of users.",
109 type
=> 'string', format
=> 'pve-userid-list',
113 description
=> "List of groups.",
114 type
=> 'string', format
=> 'pve-groupid-list',
118 description
=> "List of API tokens.",
119 type
=> 'string', format
=> 'pve-tokenid-list',
123 description
=> "List of roles.",
124 type
=> 'string', format
=> 'pve-roleid-list',
127 description
=> "Remove permissions (instead of adding it).",
133 returns
=> { type
=> 'null' },
137 if (!($param->{users
} || $param->{groups
} || $param->{tokens
})) {
138 raise_param_exc
({ map { $_ => "either 'users', 'groups' or 'tokens' is required." } qw(users groups tokens) });
141 my $path = PVE
::AccessControl
::normalize_path
($param->{path
});
142 raise_param_exc
({ path
=> "invalid ACL path '$param->{path}'" }) if !$path;
144 if (!$param->{delete} && !PVE
::AccessControl
::check_path
($path)) {
145 raise_param_exc
({ path
=> "invalid ACL path '$param->{path}'" });
148 PVE
::AccessControl
::lock_user_config
(
151 my $cfg = cfs_read_file
("user.cfg");
155 if (defined($param->{propagate
})) {
156 $propagate = $param->{propagate
} ?
1 : 0;
159 foreach my $role (split_list
($param->{roles
})) {
160 die "role '$role' does not exist\n"
161 if !$cfg->{roles
}->{$role};
163 foreach my $group (split_list
($param->{groups
})) {
165 die "group '$group' does not exist\n"
166 if !$cfg->{groups
}->{$group};
168 if ($param->{delete}) {
169 delete($cfg->{acl
}->{$path}->{groups
}->{$group}->{$role});
171 $cfg->{acl
}->{$path}->{groups
}->{$group}->{$role} = $propagate;
175 foreach my $userid (split_list
($param->{users
})) {
176 my $username = PVE
::AccessControl
::verify_username
($userid);
178 die "user '$username' does not exist\n"
179 if !$cfg->{users
}->{$username};
181 if ($param->{delete}) {
182 delete($cfg->{acl
}->{$path}->{users
}->{$username}->{$role});
184 $cfg->{acl
}->{$path}->{users
}->{$username}->{$role} = $propagate;
188 foreach my $tokenid (split_list
($param->{tokens
})) {
189 my ($username, $token) = PVE
::AccessControl
::split_tokenid
($tokenid);
190 PVE
::AccessControl
::check_token_exist
($cfg, $username, $token);
192 if ($param->{delete}) {
193 delete $cfg->{acl
}->{$path}->{tokens
}->{$tokenid}->{$role};
195 $cfg->{acl
}->{$path}->{tokens
}->{$tokenid}->{$role} = $propagate;
200 cfs_write_file
("user.cfg", $cfg);
201 }, "ACL update failed");