]> git.proxmox.com Git - pve-access-control.git/blame - PVE/API2/AccessControl.pm
add pool API
[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);
2c3a6c0a
DM
10use PVE::RESTHandler;
11use PVE::AccessControl;
12use PVE::JSONSchema qw(get_standard_option);
13use PVE::API2::Domains;
14use PVE::API2::User;
15use PVE::API2::Group;
16use PVE::API2::Role;
17use PVE::API2::ACL;
39c85db8 18use PVE::API2::Pool;
2c3a6c0a
DM
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
39c85db8
DM
47__PACKAGE__->register_method ({
48 subclass => "PVE::API2::Pool",
49 path => 'pools',
50});
51
2c3a6c0a
DM
52__PACKAGE__->register_method ({
53 name => 'index',
54 path => '',
55 method => 'GET',
56 description => "Directory index.",
57 parameters => {
58 additionalProperties => 0,
59 properties => {},
60 },
61 returns => {
62 type => 'array',
63 items => {
64 type => "object",
65 properties => {
66 subdir => { type => 'string' },
67 },
68 },
69 links => [ { rel => 'child', href => "{subdir}" } ],
70 },
71 code => sub {
72 my ($param) = @_;
73
74 my $res = [];
75
76 my $ma = __PACKAGE__->method_attributes();
77
78 foreach my $info (@$ma) {
79 next if !$info->{subclass};
80
81 my $subpath = $info->{match_re}->[0];
82
83 push @$res, { subdir => $subpath };
84 }
85
86 push @$res, { subdir => 'ticket' };
37d45deb 87 push @$res, { subdir => 'password' };
2c3a6c0a
DM
88
89 return $res;
90 }});
91
adf8d771
DM
92
93my $verify_auth = sub {
94 my ($rpcenv, $username, $pw_or_ticket, $path, $privs) = @_;
95
96 my $normpath = PVE::AccessControl::normalize_path($path);
97
98 my $ticketuser;
99 if (($ticketuser = PVE::AccessControl::verify_ticket($pw_or_ticket, 1)) &&
100 ($ticketuser eq $username)) {
101 # valid ticket
102 } elsif (PVE::AccessControl::verify_vnc_ticket($pw_or_ticket, $username, $normpath, 1)) {
103 # valid vnc ticket
104 } else {
105 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket);
106 }
107
108 my $privlist = [ PVE::Tools::split_list($privs) ];
109 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
110 die "no permission ($path, $privs)\n";
111 }
112
113 return { username => $username };
114};
115
116my $create_ticket = sub {
117 my ($rpcenv, $username, $pw_or_ticket) = @_;
118
119 my $ticketuser;
120 if (($ticketuser = PVE::AccessControl::verify_ticket($pw_or_ticket, 1)) &&
121 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
122 # valid ticket. Note: root@pam can create tickets for other users
123 } else {
124 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket);
125 }
126
127 my $ticket = PVE::AccessControl::assemble_ticket($username);
128 my $csrftoken = PVE::AccessControl::assemble_csrf_prevention_token($username);
129
130 return {
131 ticket => $ticket,
132 username => $username,
133 CSRFPreventionToken => $csrftoken,
134 };
135};
136
2c3a6c0a
DM
137__PACKAGE__->register_method ({
138 name => 'create_ticket',
139 path => 'ticket',
140 method => 'POST',
96919234
DM
141 permissions => {
142 description => "You need to pass valid credientials.",
143 user => 'world'
144 },
2c3a6c0a 145 protected => 1, # else we can't access shadow files
adf8d771 146 description => "Create or verify authentication ticket.",
2c3a6c0a
DM
147 parameters => {
148 additionalProperties => 0,
149 properties => {
150 username => {
151 description => "User name",
152 type => 'string',
153 maxLength => 64,
154 },
155 realm => get_standard_option('realm', {
156 description => "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
157 optional => 1}),
158 password => {
159 description => "The secret password. This can also be a valid ticket.",
160 type => 'string',
161 },
162 path => {
adf8d771 163 description => "Verify ticket, and check if user have access 'privs' on 'path'",
2c3a6c0a
DM
164 type => 'string',
165 requires => 'privs',
166 optional => 1,
167 maxLength => 64,
168 },
169 privs => {
adf8d771 170 description => "Verify ticket, and check if user have access 'privs' on 'path'",
2c3a6c0a
DM
171 type => 'string' , format => 'pve-priv-list',
172 requires => 'path',
173 optional => 1,
174 maxLength => 64,
175 },
176 }
177 },
178 returns => {
179 type => "object",
180 properties => {
2c3a6c0a 181 username => { type => 'string' },
adf8d771
DM
182 ticket => { type => 'string', optional => 1},
183 CSRFPreventionToken => { type => 'string', optional => 1 },
2c3a6c0a
DM
184 }
185 },
186 code => sub {
187 my ($param) = @_;
188
189 my $username = $param->{username};
190 $username .= "\@$param->{realm}" if $param->{realm};
191
192 my $rpcenv = PVE::RPCEnvironment::get();
2c3a6c0a 193
adf8d771 194 my $res;
2c3a6c0a 195
adf8d771 196 eval {
7070c1ae
DM
197 # test if user exists and is enabled
198 $rpcenv->check_user_enabled($username);
199
2c3a6c0a 200 if ($param->{path} && $param->{privs}) {
adf8d771
DM
201 $res = &$verify_auth($rpcenv, $username, $param->{password},
202 $param->{path}, $param->{privs});
2c3a6c0a 203 } else {
adf8d771 204 $res = &$create_ticket($rpcenv, $username, $param->{password});
2c3a6c0a 205 }
2c3a6c0a
DM
206 };
207 if (my $err = $@) {
adf8d771 208 my $clientip = $rpcenv->get_client_ip() || '';
2c3a6c0a
DM
209 syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err");
210 die $err;
211 }
212
213 PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'");
214
adf8d771 215 return $res;
2c3a6c0a
DM
216 }});
217
37d45deb
DM
218__PACKAGE__->register_method ({
219 name => 'change_passsword',
220 path => 'password',
221 method => 'PUT',
12683df7
DM
222 permissions => {
223 description => "Each user is allowed to change his own password. A user can change the password of another user if he has modify permission on /access/groups/<group> on a group where user <userid> is member of.",
224 check => [ 'or',
225 ['userid-param', 'self'],
226 ['userid-group', ['User.Modify']],
227 ],
228 },
37d45deb
DM
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
12683df7 252 $rpcenv->check_user_exist($userid);
37d45deb
DM
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 {
12683df7 261 # only root may change root password
37d45deb 262 raise_perm_exc() if $userid eq 'root@pam';
37d45deb
DM
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
2c3a6c0a 2731;