]>
Commit | Line | Data |
---|---|---|
2c3a6c0a DM |
1 | package PVE::API2::AccessControl; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
37d45deb | 6 | use PVE::Exception qw(raise raise_perm_exc); |
2c3a6c0a DM |
7 | use PVE::SafeSyslog; |
8 | use PVE::RPCEnvironment; | |
a427cecb | 9 | use PVE::Cluster qw(cfs_read_file); |
2c3a6c0a DM |
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.", | |
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 | |
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 | ||
dd2cfee0 DM |
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 | foreach my $vmid (keys %$idlist, '__phantom__') { | |
155 | my $perm = $rpcenv->permissions($authuser, "/vms/$vmid"); | |
156 | foreach my $priv (keys %$perm) { | |
157 | next if !($priv eq 'Permissions.Modify' ||$priv =~ m/^VM\./); | |
158 | $res->{vms}->{$priv} = 1; | |
159 | } | |
160 | } | |
161 | ||
162 | foreach my $storeid (@sids, '__phantom__') { | |
163 | my $perm = $rpcenv->permissions($authuser, "/storage/$storeid"); | |
164 | foreach my $priv (keys %$perm) { | |
165 | next if !($priv eq 'Permissions.Modify' || $priv =~ m/^Datastore\./); | |
166 | $res->{storage}->{$priv} = 1; | |
167 | } | |
168 | } | |
169 | ||
170 | foreach my $path (('/access/groups')) { | |
171 | my $perm = $rpcenv->permissions($authuser, $path); | |
172 | foreach my $priv (keys %$perm) { | |
173 | next if $priv !~ m/^(User|Group)\./; | |
174 | $res->{access}->{$priv} = 1; | |
175 | } | |
176 | } | |
177 | ||
178 | foreach my $group (keys %{$usercfg->{users}->{$authuser}->{groups}}, '__phantom__') { | |
179 | my $perm = $rpcenv->permissions($authuser, "/access/groups/$group"); | |
180 | if ($perm->{'User.Modify'}) { | |
181 | $res->{access}->{'User.Modify'} = 1; | |
182 | } | |
183 | } | |
184 | ||
185 | foreach my $node (@$nodelist) { | |
186 | my $perm = $rpcenv->permissions($authuser, "/nodes/$node"); | |
187 | foreach my $priv (keys %$perm) { | |
188 | next if $priv !~ m/^Sys\./; | |
189 | $res->{nodes}->{$priv} = 1; | |
190 | } | |
191 | } | |
192 | ||
193 | my $perm = $rpcenv->permissions($authuser, "/"); | |
194 | $res->{dc}->{'Sys.Audit'} = 1 if $perm->{'Sys.Audit'}; | |
195 | ||
196 | return $res; | |
197 | }; | |
198 | ||
2c3a6c0a DM |
199 | __PACKAGE__->register_method ({ |
200 | name => 'create_ticket', | |
201 | path => 'ticket', | |
202 | method => 'POST', | |
96919234 DM |
203 | permissions => { |
204 | description => "You need to pass valid credientials.", | |
205 | user => 'world' | |
206 | }, | |
2c3a6c0a | 207 | protected => 1, # else we can't access shadow files |
adf8d771 | 208 | description => "Create or verify authentication ticket.", |
2c3a6c0a DM |
209 | parameters => { |
210 | additionalProperties => 0, | |
211 | properties => { | |
212 | username => { | |
213 | description => "User name", | |
214 | type => 'string', | |
215 | maxLength => 64, | |
216 | }, | |
217 | realm => get_standard_option('realm', { | |
218 | description => "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.", | |
219 | optional => 1}), | |
220 | password => { | |
221 | description => "The secret password. This can also be a valid ticket.", | |
222 | type => 'string', | |
223 | }, | |
224 | path => { | |
adf8d771 | 225 | description => "Verify ticket, and check if user have access 'privs' on 'path'", |
2c3a6c0a DM |
226 | type => 'string', |
227 | requires => 'privs', | |
228 | optional => 1, | |
229 | maxLength => 64, | |
230 | }, | |
231 | privs => { | |
adf8d771 | 232 | description => "Verify ticket, and check if user have access 'privs' on 'path'", |
2c3a6c0a DM |
233 | type => 'string' , format => 'pve-priv-list', |
234 | requires => 'path', | |
235 | optional => 1, | |
236 | maxLength => 64, | |
237 | }, | |
238 | } | |
239 | }, | |
240 | returns => { | |
241 | type => "object", | |
242 | properties => { | |
2c3a6c0a | 243 | username => { type => 'string' }, |
adf8d771 DM |
244 | ticket => { type => 'string', optional => 1}, |
245 | CSRFPreventionToken => { type => 'string', optional => 1 }, | |
2c3a6c0a DM |
246 | } |
247 | }, | |
248 | code => sub { | |
249 | my ($param) = @_; | |
250 | ||
251 | my $username = $param->{username}; | |
252 | $username .= "\@$param->{realm}" if $param->{realm}; | |
253 | ||
254 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2c3a6c0a | 255 | |
adf8d771 | 256 | my $res; |
2c3a6c0a | 257 | |
adf8d771 | 258 | eval { |
7070c1ae DM |
259 | # test if user exists and is enabled |
260 | $rpcenv->check_user_enabled($username); | |
261 | ||
2c3a6c0a | 262 | if ($param->{path} && $param->{privs}) { |
adf8d771 DM |
263 | $res = &$verify_auth($rpcenv, $username, $param->{password}, |
264 | $param->{path}, $param->{privs}); | |
2c3a6c0a | 265 | } else { |
adf8d771 | 266 | $res = &$create_ticket($rpcenv, $username, $param->{password}); |
2c3a6c0a | 267 | } |
2c3a6c0a DM |
268 | }; |
269 | if (my $err = $@) { | |
adf8d771 | 270 | my $clientip = $rpcenv->get_client_ip() || ''; |
2c3a6c0a DM |
271 | syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err"); |
272 | die $err; | |
273 | } | |
274 | ||
dd2cfee0 DM |
275 | $res->{cap} = &$compute_api_permission($rpcenv, $username); |
276 | ||
2c3a6c0a DM |
277 | PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'"); |
278 | ||
adf8d771 | 279 | return $res; |
2c3a6c0a DM |
280 | }}); |
281 | ||
37d45deb DM |
282 | __PACKAGE__->register_method ({ |
283 | name => 'change_passsword', | |
284 | path => 'password', | |
285 | method => 'PUT', | |
12683df7 | 286 | permissions => { |
82b63965 | 287 | 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 |
288 | check => [ 'or', |
289 | ['userid-param', 'self'], | |
82b63965 DM |
290 | [ 'and', |
291 | [ 'userid-param', 'Realm.AllocateUser'], | |
292 | [ 'userid-group', ['User.Modify']] | |
293 | ] | |
12683df7 DM |
294 | ], |
295 | }, | |
37d45deb DM |
296 | protected => 1, # else we can't access shadow files |
297 | description => "Change user password.", | |
298 | parameters => { | |
299 | additionalProperties => 0, | |
300 | properties => { | |
301 | userid => get_standard_option('userid'), | |
302 | password => { | |
303 | description => "The new password.", | |
304 | type => 'string', | |
305 | minLength => 5, | |
306 | maxLength => 64, | |
307 | }, | |
308 | } | |
309 | }, | |
310 | returns => { type => "null" }, | |
311 | code => sub { | |
312 | my ($param) = @_; | |
313 | ||
314 | my $rpcenv = PVE::RPCEnvironment::get(); | |
315 | my $authuser = $rpcenv->get_user(); | |
316 | ||
317 | my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid}); | |
318 | ||
12683df7 | 319 | $rpcenv->check_user_exist($userid); |
37d45deb DM |
320 | |
321 | if ($authuser eq 'root@pam') { | |
322 | # OK - root can change anything | |
323 | } else { | |
324 | if ($authuser eq $userid) { | |
325 | $rpcenv->check_user_enabled($userid); | |
326 | # OK - each user can change its own password | |
327 | } else { | |
12683df7 | 328 | # only root may change root password |
37d45deb | 329 | raise_perm_exc() if $userid eq 'root@pam'; |
59321f26 DM |
330 | # do not allow to change system user passwords |
331 | raise_perm_exc() if $realm eq 'pam'; | |
37d45deb DM |
332 | } |
333 | } | |
334 | ||
335 | PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password}); | |
336 | ||
337 | PVE::Cluster::log_msg('info', 'root@pam', "changed password for user '$userid'"); | |
338 | ||
339 | return undef; | |
340 | }}); | |
341 | ||
2c3a6c0a | 342 | 1; |