add more privileges, improve docs
[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
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.",
51 permissions => {
52 user => 'all',
53 },
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' };
84 push @$res, { subdir => 'password' };
85
86 return $res;
87 }});
88
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
134 __PACKAGE__->register_method ({
135 name => 'create_ticket',
136 path => 'ticket',
137 method => 'POST',
138 permissions => {
139 description => "You need to pass valid credientials.",
140 user => 'world'
141 },
142 protected => 1, # else we can't access shadow files
143 description => "Create or verify authentication ticket.",
144 parameters => {
145 additionalProperties => 0,
146 properties => {
147 username => {
148 description => "User name",
149 type => 'string',
150 maxLength => 64,
151 },
152 realm => get_standard_option('realm', {
153 description => "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
154 optional => 1}),
155 password => {
156 description => "The secret password. This can also be a valid ticket.",
157 type => 'string',
158 },
159 path => {
160 description => "Verify ticket, and check if user have access 'privs' on 'path'",
161 type => 'string',
162 requires => 'privs',
163 optional => 1,
164 maxLength => 64,
165 },
166 privs => {
167 description => "Verify ticket, and check if user have access 'privs' on 'path'",
168 type => 'string' , format => 'pve-priv-list',
169 requires => 'path',
170 optional => 1,
171 maxLength => 64,
172 },
173 }
174 },
175 returns => {
176 type => "object",
177 properties => {
178 username => { type => 'string' },
179 ticket => { type => 'string', optional => 1},
180 CSRFPreventionToken => { type => 'string', optional => 1 },
181 }
182 },
183 code => sub {
184 my ($param) = @_;
185
186 my $username = $param->{username};
187 $username .= "\@$param->{realm}" if $param->{realm};
188
189 my $rpcenv = PVE::RPCEnvironment::get();
190
191 my $res;
192
193 eval {
194 # test if user exists and is enabled
195 $rpcenv->check_user_enabled($username);
196
197 if ($param->{path} && $param->{privs}) {
198 $res = &$verify_auth($rpcenv, $username, $param->{password},
199 $param->{path}, $param->{privs});
200 } else {
201 $res = &$create_ticket($rpcenv, $username, $param->{password});
202 }
203 };
204 if (my $err = $@) {
205 my $clientip = $rpcenv->get_client_ip() || '';
206 syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err");
207 die $err;
208 }
209
210 PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'");
211
212 return $res;
213 }});
214
215 __PACKAGE__->register_method ({
216 name => 'change_passsword',
217 path => 'password',
218 method => 'PUT',
219 permissions => {
220 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.",
221 check => [ 'or',
222 ['userid-param', 'self'],
223 [ 'and',
224 [ 'userid-param', 'Realm.AllocateUser'],
225 [ 'userid-group', ['User.Modify']]
226 ]
227 ],
228 },
229 protected => 1, # else we can't access shadow files
230 description => "Change user password.",
231 parameters => {
232 additionalProperties => 0,
233 properties => {
234 userid => get_standard_option('userid'),
235 password => {
236 description => "The new password.",
237 type => 'string',
238 minLength => 5,
239 maxLength => 64,
240 },
241 }
242 },
243 returns => { type => "null" },
244 code => sub {
245 my ($param) = @_;
246
247 my $rpcenv = PVE::RPCEnvironment::get();
248 my $authuser = $rpcenv->get_user();
249
250 my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid});
251
252 $rpcenv->check_user_exist($userid);
253
254 if ($authuser eq 'root@pam') {
255 # OK - root can change anything
256 } else {
257 if ($authuser eq $userid) {
258 $rpcenv->check_user_enabled($userid);
259 # OK - each user can change its own password
260 } else {
261 # only root may change root password
262 raise_perm_exc() if $userid eq 'root@pam';
263 }
264 }
265
266 PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password});
267
268 PVE::Cluster::log_msg('info', 'root@pam', "changed password for user '$userid'");
269
270 return undef;
271 }});
272
273 1;