]> git.proxmox.com Git - pve-access-control.git/blame - PVE/API2/AccessControl.pm
fix #233: return cluster name on successful login
[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);
e842fec5 10use PVE::Corosync;
2c3a6c0a
DM
11use PVE::RESTHandler;
12use PVE::AccessControl;
13use PVE::JSONSchema qw(get_standard_option);
14use PVE::API2::Domains;
15use PVE::API2::User;
16use PVE::API2::Group;
17use PVE::API2::Role;
18use PVE::API2::ACL;
19
20use 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.",
82b63965
DM
52 permissions => {
53 user => 'all',
54 },
2c3a6c0a
DM
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' };
37d45deb 85 push @$res, { subdir => 'password' };
2c3a6c0a
DM
86
87 return $res;
88 }});
89
adf8d771
DM
90
91my $verify_auth = sub {
96f8ebd6 92 my ($rpcenv, $username, $pw_or_ticket, $otp, $path, $privs) = @_;
adf8d771
DM
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 {
96f8ebd6 103 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket, $otp);
adf8d771
DM
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
114my $create_ticket = sub {
96f8ebd6 115 my ($rpcenv, $username, $pw_or_ticket, $otp) = @_;
adf8d771
DM
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 {
96f8ebd6 122 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket, $otp);
adf8d771
DM
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
dd2cfee0
DM
135my $compute_api_permission = sub {
136 my ($rpcenv, $authuser) = @_;
137
138 my $usercfg = $rpcenv->{user_cfg};
139
a2c18811
TL
140 my $res = {};
141 my $priv_re_map = {
142 vms => qr/VM\.|Permissions\.Modify/,
143 access => qr/(User|Group)\.|Permissions\.Modify/,
f5848089 144 storage => qr/Datastore\.|Permissions\.Modify/,
a2c18811
TL
145 nodes => qr/Sys\.|Permissions\.Modify/,
146 dc => qr/Sys\.Audit/,
dd2cfee0 147 };
a2c18811
TL
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 }
dd2cfee0
DM
177 }
178 }
179
dd2cfee0
DM
180 return $res;
181};
182
39e4e363
DM
183__PACKAGE__->register_method ({
184 name => 'get_ticket',
185 path => 'ticket',
186 method => 'GET',
187 permissions => { user => 'world' },
36dd9dbd 188 description => "Dummy. Useful for formatters which want to provide a login page.",
39e4e363
DM
189 parameters => {
190 additionalProperties => 0,
191 },
192 returns => { type => "null" },
193 code => sub { return undef; }});
194
2c3a6c0a
DM
195__PACKAGE__->register_method ({
196 name => 'create_ticket',
197 path => 'ticket',
198 method => 'POST',
96919234
DM
199 permissions => {
200 description => "You need to pass valid credientials.",
201 user => 'world'
202 },
2c3a6c0a 203 protected => 1, # else we can't access shadow files
adf8d771 204 description => "Create or verify authentication ticket.",
2c3a6c0a
DM
205 parameters => {
206 additionalProperties => 0,
207 properties => {
208 username => {
3a5ae7a0
SI
209 description => "User name",
210 type => 'string',
211 maxLength => 64,
212 completion => \&PVE::AccessControl::complete_username,
2c3a6c0a
DM
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>.",
3e5bfdf6
DM
216 optional => 1,
217 completion => \&PVE::AccessControl::complete_realm,
218 }),
2c3a6c0a
DM
219 password => {
220 description => "The secret password. This can also be a valid ticket.",
221 type => 'string',
222 },
96f8ebd6
DM
223 otp => {
224 description => "One-time password for Two-factor authentication.",
225 type => 'string',
226 optional => 1,
227 },
2c3a6c0a 228 path => {
adf8d771 229 description => "Verify ticket, and check if user have access 'privs' on 'path'",
2c3a6c0a
DM
230 type => 'string',
231 requires => 'privs',
232 optional => 1,
233 maxLength => 64,
234 },
235 privs => {
adf8d771 236 description => "Verify ticket, and check if user have access 'privs' on 'path'",
2c3a6c0a
DM
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 => {
2c3a6c0a 247 username => { type => 'string' },
adf8d771
DM
248 ticket => { type => 'string', optional => 1},
249 CSRFPreventionToken => { type => 'string', optional => 1 },
e842fec5 250 clustername => { type => 'string', optional => 1 },
2c3a6c0a
DM
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();
2c3a6c0a 260
adf8d771 261 my $res;
adf8d771 262 eval {
7070c1ae
DM
263 # test if user exists and is enabled
264 $rpcenv->check_user_enabled($username);
265
2c3a6c0a 266 if ($param->{path} && $param->{privs}) {
96f8ebd6 267 $res = &$verify_auth($rpcenv, $username, $param->{password}, $param->{otp},
adf8d771 268 $param->{path}, $param->{privs});
2c3a6c0a 269 } else {
96f8ebd6 270 $res = &$create_ticket($rpcenv, $username, $param->{password}, $param->{otp});
2c3a6c0a 271 }
2c3a6c0a
DM
272 };
273 if (my $err = $@) {
adf8d771 274 my $clientip = $rpcenv->get_client_ip() || '';
2c3a6c0a 275 syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err");
6126ab75 276 # do not return any info to prevent user enumeration attacks
fe2defd9 277 die PVE::Exception->new("authentication failure\n", code => 401);
2c3a6c0a
DM
278 }
279
dd2cfee0
DM
280 $res->{cap} = &$compute_api_permission($rpcenv, $username);
281
e842fec5
TL
282 if (PVE::Corosync::check_conf_exists(1)) {
283 if ($rpcenv->check($username, '/', ['Sys.Audit'], 1)) {
284 my $conf = cfs_read_file('corosync.conf');
285 my $totem = PVE::Corosync::totem_config($conf);
286 if ($totem->{cluster_name}) {
287 $res->{clustername} = $totem->{cluster_name};
288 }
289 }
290 }
291
2c3a6c0a
DM
292 PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'");
293
adf8d771 294 return $res;
2c3a6c0a
DM
295 }});
296
37d45deb 297__PACKAGE__->register_method ({
765305e2 298 name => 'change_password',
37d45deb
DM
299 path => 'password',
300 method => 'PUT',
12683df7 301 permissions => {
82b63965 302 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
303 check => [ 'or',
304 ['userid-param', 'self'],
82b63965
DM
305 [ 'and',
306 [ 'userid-param', 'Realm.AllocateUser'],
307 [ 'userid-group', ['User.Modify']]
308 ]
12683df7
DM
309 ],
310 },
37d45deb
DM
311 protected => 1, # else we can't access shadow files
312 description => "Change user password.",
313 parameters => {
314 additionalProperties => 0,
315 properties => {
3a5ae7a0 316 userid => get_standard_option('userid-completed'),
37d45deb
DM
317 password => {
318 description => "The new password.",
319 type => 'string',
320 minLength => 5,
321 maxLength => 64,
322 },
323 }
324 },
325 returns => { type => "null" },
326 code => sub {
327 my ($param) = @_;
328
329 my $rpcenv = PVE::RPCEnvironment::get();
330 my $authuser = $rpcenv->get_user();
331
332 my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid});
333
12683df7 334 $rpcenv->check_user_exist($userid);
37d45deb
DM
335
336 if ($authuser eq 'root@pam') {
337 # OK - root can change anything
338 } else {
339 if ($authuser eq $userid) {
340 $rpcenv->check_user_enabled($userid);
341 # OK - each user can change its own password
342 } else {
12683df7 343 # only root may change root password
37d45deb 344 raise_perm_exc() if $userid eq 'root@pam';
59321f26
DM
345 # do not allow to change system user passwords
346 raise_perm_exc() if $realm eq 'pam';
37d45deb
DM
347 }
348 }
349
350 PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password});
351
352 PVE::Cluster::log_msg('info', 'root@pam', "changed password for user '$userid'");
353
354 return undef;
355 }});
356
2c3a6c0a 3571;