1 package PVE
::API2
::AccessControl
;
6 use PVE
::Exception
qw(raise raise_perm_exc);
8 use PVE
::RPCEnvironment
;
9 use PVE
::Cluster
qw(cfs_read_file);
11 use PVE
::AccessControl
;
12 use PVE
::JSONSchema
qw(get_standard_option);
13 use PVE
::API2
::Domains
;
19 use base
qw(PVE::RESTHandler);
21 __PACKAGE__-
>register_method ({
22 subclass
=> "PVE::API2::User",
26 __PACKAGE__-
>register_method ({
27 subclass
=> "PVE::API2::Group",
31 __PACKAGE__-
>register_method ({
32 subclass
=> "PVE::API2::Role",
36 __PACKAGE__-
>register_method ({
37 subclass
=> "PVE::API2::ACL",
41 __PACKAGE__-
>register_method ({
42 subclass
=> "PVE::API2::Domains",
46 __PACKAGE__-
>register_method ({
50 description
=> "Directory index.",
55 additionalProperties
=> 0,
63 subdir
=> { type
=> 'string' },
66 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
73 my $ma = __PACKAGE__-
>method_attributes();
75 foreach my $info (@$ma) {
76 next if !$info->{subclass
};
78 my $subpath = $info->{match_re
}->[0];
80 push @$res, { subdir
=> $subpath };
83 push @$res, { subdir
=> 'ticket' };
84 push @$res, { subdir
=> 'password' };
90 my $verify_auth = sub {
91 my ($rpcenv, $username, $pw_or_ticket, $otp, $path, $privs) = @_;
93 my $normpath = PVE
::AccessControl
::normalize_path
($path);
96 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
97 ($ticketuser eq $username)) {
99 } elsif (PVE
::AccessControl
::verify_vnc_ticket
($pw_or_ticket, $username, $normpath, 1)) {
102 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket, $otp);
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";
110 return { username
=> $username };
113 my $create_ticket = sub {
114 my ($rpcenv, $username, $pw_or_ticket, $otp) = @_;
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
121 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket, $otp);
124 my $ticket = PVE
::AccessControl
::assemble_ticket
($username);
125 my $csrftoken = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
129 username
=> $username,
130 CSRFPreventionToken
=> $csrftoken,
134 my $compute_api_permission = sub {
135 my ($rpcenv, $authuser) = @_;
137 my $usercfg = $rpcenv->{user_cfg
};
139 my $nodelist = PVE
::Cluster
::get_nodelist
();
140 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
141 my $idlist = $vmlist->{ids
} || {};
143 my $cfg = PVE
::Storage
::config
();
144 my @sids = PVE
::Storage
::storage_ids
($cfg);
154 my $extract_vm_caps = sub {
157 my $perm = $rpcenv->permissions($authuser, $path);
158 foreach my $priv (keys %$perm) {
159 next if !($priv eq 'Permissions.Modify' || $priv =~ m/^VM\./);
160 $res->{vms
}->{$priv} = 1;
164 foreach my $pool (keys %{$usercfg->{pools
}}) {
165 &$extract_vm_caps("/pool/$pool");
168 foreach my $vmid (keys %$idlist, '__phantom__') {
169 &$extract_vm_caps("/vms/$vmid");
172 foreach my $storeid (@sids, '__phantom__') {
173 my $perm = $rpcenv->permissions($authuser, "/storage/$storeid");
174 foreach my $priv (keys %$perm) {
175 next if !($priv eq 'Permissions.Modify' || $priv =~ m/^Datastore\./);
176 $res->{storage
}->{$priv} = 1;
180 foreach my $path (('/access/groups')) {
181 my $perm = $rpcenv->permissions($authuser, $path);
182 foreach my $priv (keys %$perm) {
183 next if $priv !~ m/^(User|Group)\./;
184 $res->{access
}->{$priv} = 1;
188 foreach my $group (keys %{$usercfg->{users
}->{$authuser}->{groups
}}, '__phantom__') {
189 my $perm = $rpcenv->permissions($authuser, "/access/groups/$group");
190 if ($perm->{'User.Modify'}) {
191 $res->{access
}->{'User.Modify'} = 1;
195 foreach my $node (@$nodelist) {
196 my $perm = $rpcenv->permissions($authuser, "/nodes/$node");
197 foreach my $priv (keys %$perm) {
198 next if $priv !~ m/^Sys\./;
199 $res->{nodes
}->{$priv} = 1;
203 my $perm = $rpcenv->permissions($authuser, "/");
204 $res->{dc
}->{'Sys.Audit'} = 1 if $perm->{'Sys.Audit'};
209 __PACKAGE__-
>register_method ({
210 name
=> 'get_ticket',
213 permissions
=> { user
=> 'world' },
214 description
=> "Dummy. Useful for formaters which want to priovde a login page.",
216 additionalProperties
=> 0,
218 returns
=> { type
=> "null" },
219 code
=> sub { return undef; }});
221 __PACKAGE__-
>register_method ({
222 name
=> 'create_ticket',
226 description
=> "You need to pass valid credientials.",
229 protected
=> 1, # else we can't access shadow files
230 description
=> "Create or verify authentication ticket.",
232 additionalProperties
=> 0,
235 description
=> "User name",
238 completion
=> \
&PVE
::AccessControl
::complete_username
,
240 realm
=> get_standard_option
('realm', {
241 description
=> "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
243 completion
=> \
&PVE
::AccessControl
::complete_realm
,
246 description
=> "The secret password. This can also be a valid ticket.",
250 description
=> "One-time password for Two-factor authentication.",
255 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
262 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
263 type
=> 'string' , format
=> 'pve-priv-list',
273 username
=> { type
=> 'string' },
274 ticket
=> { type
=> 'string', optional
=> 1},
275 CSRFPreventionToken
=> { type
=> 'string', optional
=> 1 },
281 my $username = $param->{username
};
282 $username .= "\@$param->{realm}" if $param->{realm
};
284 my $rpcenv = PVE
::RPCEnvironment
::get
();
288 # test if user exists and is enabled
289 $rpcenv->check_user_enabled($username);
291 if ($param->{path
} && $param->{privs
}) {
292 $res = &$verify_auth($rpcenv, $username, $param->{password
}, $param->{otp
},
293 $param->{path
}, $param->{privs
});
295 $res = &$create_ticket($rpcenv, $username, $param->{password
}, $param->{otp
});
299 my $clientip = $rpcenv->get_client_ip() || '';
300 syslog
('err', "authentication failure; rhost=$clientip user=$username msg=$err");
301 # do not return any info to prevent user enumeration attacks
302 die PVE
::Exception-
>new("authentication failure\n", code
=> 401);
305 $res->{cap
} = &$compute_api_permission($rpcenv, $username);
307 PVE
::Cluster
::log_msg
('info', 'root@pam', "successful auth for user '$username'");
312 __PACKAGE__-
>register_method ({
313 name
=> 'change_passsword',
317 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.",
319 ['userid-param', 'self'],
321 [ 'userid-param', 'Realm.AllocateUser'],
322 [ 'userid-group', ['User.Modify']]
326 protected
=> 1, # else we can't access shadow files
327 description
=> "Change user password.",
329 additionalProperties
=> 0,
331 userid
=> get_standard_option
('userid', {
332 completion
=> \
&PVE
::AccessControl
::complete_username
,
335 description
=> "The new password.",
342 returns
=> { type
=> "null" },
346 my $rpcenv = PVE
::RPCEnvironment
::get
();
347 my $authuser = $rpcenv->get_user();
349 my ($userid, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
351 $rpcenv->check_user_exist($userid);
353 if ($authuser eq 'root@pam') {
354 # OK - root can change anything
356 if ($authuser eq $userid) {
357 $rpcenv->check_user_enabled($userid);
358 # OK - each user can change its own password
360 # only root may change root password
361 raise_perm_exc
() if $userid eq 'root@pam';
362 # do not allow to change system user passwords
363 raise_perm_exc
() if $realm eq 'pam';
367 PVE
::AccessControl
::domain_set_password
($realm, $ruid, $param->{password
});
369 PVE
::Cluster
::log_msg
('info', 'root@pam', "changed password for user '$userid'");