]> git.proxmox.com Git - pve-access-control.git/blob - PVE/API2/AccessControl.pm
cleanup permission checks
[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 use PVE::API2::Pool;
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 subclass => "PVE::API2::Pool",
49 path => 'pools',
50 });
51
52 __PACKAGE__->register_method ({
53 name => 'index',
54 path => '',
55 method => 'GET',
56 description => "Directory index.",
57 permissions => {
58 user => 'all',
59 },
60 parameters => {
61 additionalProperties => 0,
62 properties => {},
63 },
64 returns => {
65 type => 'array',
66 items => {
67 type => "object",
68 properties => {
69 subdir => { type => 'string' },
70 },
71 },
72 links => [ { rel => 'child', href => "{subdir}" } ],
73 },
74 code => sub {
75 my ($param) = @_;
76
77 my $res = [];
78
79 my $ma = __PACKAGE__->method_attributes();
80
81 foreach my $info (@$ma) {
82 next if !$info->{subclass};
83
84 my $subpath = $info->{match_re}->[0];
85
86 push @$res, { subdir => $subpath };
87 }
88
89 push @$res, { subdir => 'ticket' };
90 push @$res, { subdir => 'password' };
91
92 return $res;
93 }});
94
95
96 my $verify_auth = sub {
97 my ($rpcenv, $username, $pw_or_ticket, $path, $privs) = @_;
98
99 my $normpath = PVE::AccessControl::normalize_path($path);
100
101 my $ticketuser;
102 if (($ticketuser = PVE::AccessControl::verify_ticket($pw_or_ticket, 1)) &&
103 ($ticketuser eq $username)) {
104 # valid ticket
105 } elsif (PVE::AccessControl::verify_vnc_ticket($pw_or_ticket, $username, $normpath, 1)) {
106 # valid vnc ticket
107 } else {
108 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket);
109 }
110
111 my $privlist = [ PVE::Tools::split_list($privs) ];
112 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
113 die "no permission ($path, $privs)\n";
114 }
115
116 return { username => $username };
117 };
118
119 my $create_ticket = sub {
120 my ($rpcenv, $username, $pw_or_ticket) = @_;
121
122 my $ticketuser;
123 if (($ticketuser = PVE::AccessControl::verify_ticket($pw_or_ticket, 1)) &&
124 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
125 # valid ticket. Note: root@pam can create tickets for other users
126 } else {
127 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket);
128 }
129
130 my $ticket = PVE::AccessControl::assemble_ticket($username);
131 my $csrftoken = PVE::AccessControl::assemble_csrf_prevention_token($username);
132
133 return {
134 ticket => $ticket,
135 username => $username,
136 CSRFPreventionToken => $csrftoken,
137 };
138 };
139
140 __PACKAGE__->register_method ({
141 name => 'create_ticket',
142 path => 'ticket',
143 method => 'POST',
144 permissions => {
145 description => "You need to pass valid credientials.",
146 user => 'world'
147 },
148 protected => 1, # else we can't access shadow files
149 description => "Create or verify authentication ticket.",
150 parameters => {
151 additionalProperties => 0,
152 properties => {
153 username => {
154 description => "User name",
155 type => 'string',
156 maxLength => 64,
157 },
158 realm => get_standard_option('realm', {
159 description => "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
160 optional => 1}),
161 password => {
162 description => "The secret password. This can also be a valid ticket.",
163 type => 'string',
164 },
165 path => {
166 description => "Verify ticket, and check if user have access 'privs' on 'path'",
167 type => 'string',
168 requires => 'privs',
169 optional => 1,
170 maxLength => 64,
171 },
172 privs => {
173 description => "Verify ticket, and check if user have access 'privs' on 'path'",
174 type => 'string' , format => 'pve-priv-list',
175 requires => 'path',
176 optional => 1,
177 maxLength => 64,
178 },
179 }
180 },
181 returns => {
182 type => "object",
183 properties => {
184 username => { type => 'string' },
185 ticket => { type => 'string', optional => 1},
186 CSRFPreventionToken => { type => 'string', optional => 1 },
187 }
188 },
189 code => sub {
190 my ($param) = @_;
191
192 my $username = $param->{username};
193 $username .= "\@$param->{realm}" if $param->{realm};
194
195 my $rpcenv = PVE::RPCEnvironment::get();
196
197 my $res;
198
199 eval {
200 # test if user exists and is enabled
201 $rpcenv->check_user_enabled($username);
202
203 if ($param->{path} && $param->{privs}) {
204 $res = &$verify_auth($rpcenv, $username, $param->{password},
205 $param->{path}, $param->{privs});
206 } else {
207 $res = &$create_ticket($rpcenv, $username, $param->{password});
208 }
209 };
210 if (my $err = $@) {
211 my $clientip = $rpcenv->get_client_ip() || '';
212 syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err");
213 die $err;
214 }
215
216 PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'");
217
218 return $res;
219 }});
220
221 __PACKAGE__->register_method ({
222 name => 'change_passsword',
223 path => 'password',
224 method => 'PUT',
225 permissions => {
226 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.",
227 check => [ 'or',
228 ['userid-param', 'self'],
229 [ 'and',
230 [ 'userid-param', 'Realm.AllocateUser'],
231 [ 'userid-group', ['User.Modify']]
232 ]
233 ],
234 },
235 protected => 1, # else we can't access shadow files
236 description => "Change user password.",
237 parameters => {
238 additionalProperties => 0,
239 properties => {
240 userid => get_standard_option('userid'),
241 password => {
242 description => "The new password.",
243 type => 'string',
244 minLength => 5,
245 maxLength => 64,
246 },
247 }
248 },
249 returns => { type => "null" },
250 code => sub {
251 my ($param) = @_;
252
253 my $rpcenv = PVE::RPCEnvironment::get();
254 my $authuser = $rpcenv->get_user();
255
256 my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid});
257
258 $rpcenv->check_user_exist($userid);
259
260 if ($authuser eq 'root@pam') {
261 # OK - root can change anything
262 } else {
263 if ($authuser eq $userid) {
264 $rpcenv->check_user_enabled($userid);
265 # OK - each user can change its own password
266 } else {
267 # only root may change root password
268 raise_perm_exc() if $userid eq 'root@pam';
269 }
270 }
271
272 PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password});
273
274 PVE::Cluster::log_msg('info', 'root@pam', "changed password for user '$userid'");
275
276 return undef;
277 }});
278
279 1;