allow dots in access paths
[pve-access-control.git] / PVE / API2 / AccessControl.pm
1 package PVE::API2::AccessControl;
2
3 use strict;
4 use warnings;
5
6 use PVE::Exception qw(raise raise_perm_exc);
7 use PVE::SafeSyslog;
8 use PVE::RPCEnvironment;
9 use PVE::Cluster qw(cfs_read_file);
10 use PVE::RESTHandler;
11 use PVE::AccessControl;
12 use PVE::JSONSchema qw(get_standard_option);
13 use PVE::API2::Domains;
14 use PVE::API2::User;
15 use PVE::API2::Group;
16 use PVE::API2::Role;
17 use PVE::API2::ACL;
18
19 use base qw(PVE::RESTHandler);
20
21 __PACKAGE__->register_method ({
22 subclass => "PVE::API2::User",
23 path => 'users',
24 });
25
26 __PACKAGE__->register_method ({
27 subclass => "PVE::API2::Group",
28 path => 'groups',
29 });
30
31 __PACKAGE__->register_method ({
32 subclass => "PVE::API2::Role",
33 path => 'roles',
34 });
35
36 __PACKAGE__->register_method ({
37 subclass => "PVE::API2::ACL",
38 path => 'acl',
39 });
40
41 __PACKAGE__->register_method ({
42 subclass => "PVE::API2::Domains",
43 path => 'domains',
44 });
45
46 __PACKAGE__->register_method ({
47 name => 'index',
48 path => '',
49 method => 'GET',
50 description => "Directory index.",
51 permissions => {
52 user => 'all',
53 },
54 parameters => {
55 additionalProperties => 0,
56 properties => {},
57 },
58 returns => {
59 type => 'array',
60 items => {
61 type => "object",
62 properties => {
63 subdir => { type => 'string' },
64 },
65 },
66 links => [ { rel => 'child', href => "{subdir}" } ],
67 },
68 code => sub {
69 my ($param) = @_;
70
71 my $res = [];
72
73 my $ma = __PACKAGE__->method_attributes();
74
75 foreach my $info (@$ma) {
76 next if !$info->{subclass};
77
78 my $subpath = $info->{match_re}->[0];
79
80 push @$res, { subdir => $subpath };
81 }
82
83 push @$res, { subdir => 'ticket' };
84 push @$res, { subdir => 'password' };
85
86 return $res;
87 }});
88
89
90 my $verify_auth = sub {
91 my ($rpcenv, $username, $pw_or_ticket, $path, $privs) = @_;
92
93 my $normpath = PVE::AccessControl::normalize_path($path);
94
95 my $ticketuser;
96 if (($ticketuser = PVE::AccessControl::verify_ticket($pw_or_ticket, 1)) &&
97 ($ticketuser eq $username)) {
98 # valid ticket
99 } elsif (PVE::AccessControl::verify_vnc_ticket($pw_or_ticket, $username, $normpath, 1)) {
100 # valid vnc ticket
101 } else {
102 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket);
103 }
104
105 my $privlist = [ PVE::Tools::split_list($privs) ];
106 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
107 die "no permission ($path, $privs)\n";
108 }
109
110 return { username => $username };
111 };
112
113 my $create_ticket = sub {
114 my ($rpcenv, $username, $pw_or_ticket) = @_;
115
116 my $ticketuser;
117 if (($ticketuser = PVE::AccessControl::verify_ticket($pw_or_ticket, 1)) &&
118 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
119 # valid ticket. Note: root@pam can create tickets for other users
120 } else {
121 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket);
122 }
123
124 my $ticket = PVE::AccessControl::assemble_ticket($username);
125 my $csrftoken = PVE::AccessControl::assemble_csrf_prevention_token($username);
126
127 return {
128 ticket => $ticket,
129 username => $username,
130 CSRFPreventionToken => $csrftoken,
131 };
132 };
133
134 my $compute_api_permission = sub {
135 my ($rpcenv, $authuser) = @_;
136
137 my $usercfg = $rpcenv->{user_cfg};
138
139 my $nodelist = PVE::Cluster::get_nodelist();
140 my $vmlist = PVE::Cluster::get_vmlist() || {};
141 my $idlist = $vmlist->{ids} || {};
142
143 my $cfg = PVE::Storage::config();
144 my @sids = PVE::Storage::storage_ids ($cfg);
145
146 my $res = {
147 vms => {},
148 storage => {},
149 access => {},
150 nodes => {},
151 dc => {},
152 };
153
154 my $extract_vm_caps = sub {
155 my ($path) = @_;
156
157 my $perm = $rpcenv->permissions($authuser, $path);
158 foreach my $priv (keys %$perm) {
159 next if !($priv eq 'Permissions.Modify' || $priv =~ m/^VM\./);
160 $res->{vms}->{$priv} = 1;
161 }
162 };
163
164 foreach my $pool (keys %{$usercfg->{pools}}) {
165 &$extract_vm_caps("/pool/$pool");
166 }
167
168 foreach my $vmid (keys %$idlist, '__phantom__') {
169 &$extract_vm_caps("/vms/$vmid");
170 }
171
172 foreach my $storeid (@sids, '__phantom__') {
173 my $perm = $rpcenv->permissions($authuser, "/storage/$storeid");
174 foreach my $priv (keys %$perm) {
175 next if !($priv eq 'Permissions.Modify' || $priv =~ m/^Datastore\./);
176 $res->{storage}->{$priv} = 1;
177 }
178 }
179
180 foreach my $path (('/access/groups')) {
181 my $perm = $rpcenv->permissions($authuser, $path);
182 foreach my $priv (keys %$perm) {
183 next if $priv !~ m/^(User|Group)\./;
184 $res->{access}->{$priv} = 1;
185 }
186 }
187
188 foreach my $group (keys %{$usercfg->{users}->{$authuser}->{groups}}, '__phantom__') {
189 my $perm = $rpcenv->permissions($authuser, "/access/groups/$group");
190 if ($perm->{'User.Modify'}) {
191 $res->{access}->{'User.Modify'} = 1;
192 }
193 }
194
195 foreach my $node (@$nodelist) {
196 my $perm = $rpcenv->permissions($authuser, "/nodes/$node");
197 foreach my $priv (keys %$perm) {
198 next if $priv !~ m/^Sys\./;
199 $res->{nodes}->{$priv} = 1;
200 }
201 }
202
203 my $perm = $rpcenv->permissions($authuser, "/");
204 $res->{dc}->{'Sys.Audit'} = 1 if $perm->{'Sys.Audit'};
205
206 return $res;
207 };
208
209 __PACKAGE__->register_method ({
210 name => 'create_ticket',
211 path => 'ticket',
212 method => 'POST',
213 permissions => {
214 description => "You need to pass valid credientials.",
215 user => 'world'
216 },
217 protected => 1, # else we can't access shadow files
218 description => "Create or verify authentication ticket.",
219 parameters => {
220 additionalProperties => 0,
221 properties => {
222 username => {
223 description => "User name",
224 type => 'string',
225 maxLength => 64,
226 },
227 realm => get_standard_option('realm', {
228 description => "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
229 optional => 1}),
230 password => {
231 description => "The secret password. This can also be a valid ticket.",
232 type => 'string',
233 },
234 path => {
235 description => "Verify ticket, and check if user have access 'privs' on 'path'",
236 type => 'string',
237 requires => 'privs',
238 optional => 1,
239 maxLength => 64,
240 },
241 privs => {
242 description => "Verify ticket, and check if user have access 'privs' on 'path'",
243 type => 'string' , format => 'pve-priv-list',
244 requires => 'path',
245 optional => 1,
246 maxLength => 64,
247 },
248 }
249 },
250 returns => {
251 type => "object",
252 properties => {
253 username => { type => 'string' },
254 ticket => { type => 'string', optional => 1},
255 CSRFPreventionToken => { type => 'string', optional => 1 },
256 }
257 },
258 code => sub {
259 my ($param) = @_;
260
261 my $username = $param->{username};
262 $username .= "\@$param->{realm}" if $param->{realm};
263
264 my $rpcenv = PVE::RPCEnvironment::get();
265
266 my $res;
267 eval {
268 # test if user exists and is enabled
269 $rpcenv->check_user_enabled($username);
270
271 if ($param->{path} && $param->{privs}) {
272 $res = &$verify_auth($rpcenv, $username, $param->{password},
273 $param->{path}, $param->{privs});
274 } else {
275 $res = &$create_ticket($rpcenv, $username, $param->{password});
276 }
277 };
278 if (my $err = $@) {
279 my $clientip = $rpcenv->get_client_ip() || '';
280 syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err");
281 # do not return any info to prevent user enumeration attacks
282 die PVE::Exception->new("authentication failure\n", code => 401);
283 }
284
285 $res->{cap} = &$compute_api_permission($rpcenv, $username);
286
287 PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'");
288
289 return $res;
290 }});
291
292 __PACKAGE__->register_method ({
293 name => 'change_passsword',
294 path => 'password',
295 method => 'PUT',
296 permissions => {
297 description => "Each user is allowed to change his own password. A user can change the password of another user if he has 'Realm.AllocateUser' (on the realm of user <userid>) and 'User.Modify' permission on /access/groups/<group> on a group where user <userid> is member of.",
298 check => [ 'or',
299 ['userid-param', 'self'],
300 [ 'and',
301 [ 'userid-param', 'Realm.AllocateUser'],
302 [ 'userid-group', ['User.Modify']]
303 ]
304 ],
305 },
306 protected => 1, # else we can't access shadow files
307 description => "Change user password.",
308 parameters => {
309 additionalProperties => 0,
310 properties => {
311 userid => get_standard_option('userid'),
312 password => {
313 description => "The new password.",
314 type => 'string',
315 minLength => 5,
316 maxLength => 64,
317 },
318 }
319 },
320 returns => { type => "null" },
321 code => sub {
322 my ($param) = @_;
323
324 my $rpcenv = PVE::RPCEnvironment::get();
325 my $authuser = $rpcenv->get_user();
326
327 my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid});
328
329 $rpcenv->check_user_exist($userid);
330
331 if ($authuser eq 'root@pam') {
332 # OK - root can change anything
333 } else {
334 if ($authuser eq $userid) {
335 $rpcenv->check_user_enabled($userid);
336 # OK - each user can change its own password
337 } else {
338 # only root may change root password
339 raise_perm_exc() if $userid eq 'root@pam';
340 # do not allow to change system user passwords
341 raise_perm_exc() if $realm eq 'pam';
342 }
343 }
344
345 PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password});
346
347 PVE::Cluster::log_msg('info', 'root@pam', "changed password for user '$userid'");
348
349 return undef;
350 }});
351
352 1;