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