]> git.proxmox.com Git - pve-access-control.git/blame - PVE/API2/AccessControl.pm
white space cleanup
[pve-access-control.git] / PVE / API2 / AccessControl.pm
CommitLineData
2c3a6c0a
DM
1package PVE::API2::AccessControl;
2
3use strict;
4use warnings;
5
37d45deb 6use PVE::Exception qw(raise raise_perm_exc);
2c3a6c0a
DM
7use PVE::SafeSyslog;
8use PVE::RPCEnvironment;
a427cecb 9use PVE::Cluster qw(cfs_read_file);
2c3a6c0a
DM
10use PVE::RESTHandler;
11use PVE::AccessControl;
12use PVE::JSONSchema qw(get_standard_option);
13use PVE::API2::Domains;
14use PVE::API2::User;
15use PVE::API2::Group;
16use PVE::API2::Role;
17use PVE::API2::ACL;
18
19use 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.",
82b63965
DM
51 permissions => {
52 user => 'all',
53 },
2c3a6c0a
DM
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' };
37d45deb 84 push @$res, { subdir => 'password' };
2c3a6c0a
DM
85
86 return $res;
87 }});
88
adf8d771
DM
89
90my $verify_auth = sub {
96f8ebd6 91 my ($rpcenv, $username, $pw_or_ticket, $otp, $path, $privs) = @_;
adf8d771
DM
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 {
96f8ebd6 102 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket, $otp);
adf8d771
DM
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
113my $create_ticket = sub {
96f8ebd6 114 my ($rpcenv, $username, $pw_or_ticket, $otp) = @_;
adf8d771
DM
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 {
96f8ebd6 121 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket, $otp);
adf8d771
DM
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
dd2cfee0
DM
134my $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
437be042
DM
154 my $extract_vm_caps = sub {
155 my ($path) = @_;
156
157 my $perm = $rpcenv->permissions($authuser, $path);
dd2cfee0 158 foreach my $priv (keys %$perm) {
437be042 159 next if !($priv eq 'Permissions.Modify' || $priv =~ m/^VM\./);
dd2cfee0
DM
160 $res->{vms}->{$priv} = 1;
161 }
437be042
DM
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");
dd2cfee0
DM
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
39e4e363
DM
209__PACKAGE__->register_method ({
210 name => 'get_ticket',
211 path => 'ticket',
212 method => 'GET',
213 permissions => { user => 'world' },
214 description => "Dummy. Useful for formaters which want to priovde a login page.",
215 parameters => {
216 additionalProperties => 0,
217 },
218 returns => { type => "null" },
219 code => sub { return undef; }});
220
2c3a6c0a
DM
221__PACKAGE__->register_method ({
222 name => 'create_ticket',
223 path => 'ticket',
224 method => 'POST',
96919234
DM
225 permissions => {
226 description => "You need to pass valid credientials.",
227 user => 'world'
228 },
2c3a6c0a 229 protected => 1, # else we can't access shadow files
adf8d771 230 description => "Create or verify authentication ticket.",
2c3a6c0a
DM
231 parameters => {
232 additionalProperties => 0,
233 properties => {
234 username => {
235 description => "User name",
236 type => 'string',
237 maxLength => 64,
238 },
239 realm => get_standard_option('realm', {
240 description => "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
241 optional => 1}),
242 password => {
243 description => "The secret password. This can also be a valid ticket.",
244 type => 'string',
245 },
96f8ebd6
DM
246 otp => {
247 description => "One-time password for Two-factor authentication.",
248 type => 'string',
249 optional => 1,
250 },
2c3a6c0a 251 path => {
adf8d771 252 description => "Verify ticket, and check if user have access 'privs' on 'path'",
2c3a6c0a
DM
253 type => 'string',
254 requires => 'privs',
255 optional => 1,
256 maxLength => 64,
257 },
258 privs => {
adf8d771 259 description => "Verify ticket, and check if user have access 'privs' on 'path'",
2c3a6c0a
DM
260 type => 'string' , format => 'pve-priv-list',
261 requires => 'path',
262 optional => 1,
263 maxLength => 64,
264 },
265 }
266 },
267 returns => {
268 type => "object",
269 properties => {
2c3a6c0a 270 username => { type => 'string' },
adf8d771
DM
271 ticket => { type => 'string', optional => 1},
272 CSRFPreventionToken => { type => 'string', optional => 1 },
2c3a6c0a
DM
273 }
274 },
275 code => sub {
276 my ($param) = @_;
277
278 my $username = $param->{username};
279 $username .= "\@$param->{realm}" if $param->{realm};
280
281 my $rpcenv = PVE::RPCEnvironment::get();
2c3a6c0a 282
adf8d771 283 my $res;
adf8d771 284 eval {
7070c1ae
DM
285 # test if user exists and is enabled
286 $rpcenv->check_user_enabled($username);
287
2c3a6c0a 288 if ($param->{path} && $param->{privs}) {
96f8ebd6 289 $res = &$verify_auth($rpcenv, $username, $param->{password}, $param->{otp},
adf8d771 290 $param->{path}, $param->{privs});
2c3a6c0a 291 } else {
96f8ebd6 292 $res = &$create_ticket($rpcenv, $username, $param->{password}, $param->{otp});
2c3a6c0a 293 }
2c3a6c0a
DM
294 };
295 if (my $err = $@) {
adf8d771 296 my $clientip = $rpcenv->get_client_ip() || '';
2c3a6c0a 297 syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err");
6126ab75 298 # do not return any info to prevent user enumeration attacks
fe2defd9 299 die PVE::Exception->new("authentication failure\n", code => 401);
2c3a6c0a
DM
300 }
301
dd2cfee0
DM
302 $res->{cap} = &$compute_api_permission($rpcenv, $username);
303
2c3a6c0a
DM
304 PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'");
305
adf8d771 306 return $res;
2c3a6c0a
DM
307 }});
308
37d45deb
DM
309__PACKAGE__->register_method ({
310 name => 'change_passsword',
311 path => 'password',
312 method => 'PUT',
12683df7 313 permissions => {
82b63965 314 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.",
12683df7
DM
315 check => [ 'or',
316 ['userid-param', 'self'],
82b63965
DM
317 [ 'and',
318 [ 'userid-param', 'Realm.AllocateUser'],
319 [ 'userid-group', ['User.Modify']]
320 ]
12683df7
DM
321 ],
322 },
37d45deb
DM
323 protected => 1, # else we can't access shadow files
324 description => "Change user password.",
325 parameters => {
326 additionalProperties => 0,
327 properties => {
328 userid => get_standard_option('userid'),
329 password => {
330 description => "The new password.",
331 type => 'string',
332 minLength => 5,
333 maxLength => 64,
334 },
335 }
336 },
337 returns => { type => "null" },
338 code => sub {
339 my ($param) = @_;
340
341 my $rpcenv = PVE::RPCEnvironment::get();
342 my $authuser = $rpcenv->get_user();
343
344 my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid});
345
12683df7 346 $rpcenv->check_user_exist($userid);
37d45deb
DM
347
348 if ($authuser eq 'root@pam') {
349 # OK - root can change anything
350 } else {
351 if ($authuser eq $userid) {
352 $rpcenv->check_user_enabled($userid);
353 # OK - each user can change its own password
354 } else {
12683df7 355 # only root may change root password
37d45deb 356 raise_perm_exc() if $userid eq 'root@pam';
59321f26
DM
357 # do not allow to change system user passwords
358 raise_perm_exc() if $realm eq 'pam';
37d45deb
DM
359 }
360 }
361
362 PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password});
363
364 PVE::Cluster::log_msg('info', 'root@pam', "changed password for user '$userid'");
365
366 return undef;
367 }});
368
2c3a6c0a 3691;