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