1 package PVE
::API2
::AccessControl
;
5 use Time
::HiRes
qw(usleep gettimeofday tv_interval);
7 use PVE
::Exception
qw(raise raise_perm_exc);
9 use PVE
::RPCEnvironment
;
10 use PVE
::Cluster
qw(cfs_read_file);
12 use PVE
::AccessControl
;
13 use PVE
::JSONSchema
qw(get_standard_option);
14 use PVE
::API2
::Domains
;
20 use base
qw(PVE::RESTHandler);
22 __PACKAGE__-
>register_method ({
23 subclass
=> "PVE::API2::User",
27 __PACKAGE__-
>register_method ({
28 subclass
=> "PVE::API2::Group",
32 __PACKAGE__-
>register_method ({
33 subclass
=> "PVE::API2::Role",
37 __PACKAGE__-
>register_method ({
38 subclass
=> "PVE::API2::ACL",
42 __PACKAGE__-
>register_method ({
43 subclass
=> "PVE::API2::Domains",
47 __PACKAGE__-
>register_method ({
51 description
=> "Directory index.",
56 additionalProperties
=> 0,
64 subdir
=> { type
=> 'string' },
67 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
74 my $ma = __PACKAGE__-
>method_attributes();
76 foreach my $info (@$ma) {
77 next if !$info->{subclass
};
79 my $subpath = $info->{match_re
}->[0];
81 push @$res, { subdir
=> $subpath };
84 push @$res, { subdir
=> 'ticket' };
85 push @$res, { subdir
=> 'password' };
91 my $verify_auth = sub {
92 my ($rpcenv, $username, $pw_or_ticket, $path, $privs) = @_;
94 my $normpath = PVE
::AccessControl
::normalize_path
($path);
97 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
98 ($ticketuser eq $username)) {
100 } elsif (PVE
::AccessControl
::verify_vnc_ticket
($pw_or_ticket, $username, $normpath, 1)) {
103 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket);
106 my $privlist = [ PVE
::Tools
::split_list
($privs) ];
107 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
108 die "no permission ($path, $privs)\n";
111 return { username
=> $username };
114 my $create_ticket = sub {
115 my ($rpcenv, $username, $pw_or_ticket) = @_;
118 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
119 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
120 # valid ticket. Note: root@pam can create tickets for other users
122 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket);
125 my $ticket = PVE
::AccessControl
::assemble_ticket
($username);
126 my $csrftoken = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
130 username
=> $username,
131 CSRFPreventionToken
=> $csrftoken,
135 my $compute_api_permission = sub {
136 my ($rpcenv, $authuser) = @_;
138 my $usercfg = $rpcenv->{user_cfg
};
140 my $nodelist = PVE
::Cluster
::get_nodelist
();
141 my $vmlist = PVE
::Cluster
::get_vmlist
() || {};
142 my $idlist = $vmlist->{ids
} || {};
144 my $cfg = PVE
::Storage
::config
();
145 my @sids = PVE
::Storage
::storage_ids
($cfg);
155 my $extract_vm_caps = sub {
158 my $perm = $rpcenv->permissions($authuser, $path);
159 foreach my $priv (keys %$perm) {
160 next if !($priv eq 'Permissions.Modify' || $priv =~ m/^VM\./);
161 $res->{vms
}->{$priv} = 1;
165 foreach my $pool (keys %{$usercfg->{pools
}}) {
166 &$extract_vm_caps("/pool/$pool");
169 foreach my $vmid (keys %$idlist, '__phantom__') {
170 &$extract_vm_caps("/vms/$vmid");
173 foreach my $storeid (@sids, '__phantom__') {
174 my $perm = $rpcenv->permissions($authuser, "/storage/$storeid");
175 foreach my $priv (keys %$perm) {
176 next if !($priv eq 'Permissions.Modify' || $priv =~ m/^Datastore\./);
177 $res->{storage
}->{$priv} = 1;
181 foreach my $path (('/access/groups')) {
182 my $perm = $rpcenv->permissions($authuser, $path);
183 foreach my $priv (keys %$perm) {
184 next if $priv !~ m/^(User|Group)\./;
185 $res->{access
}->{$priv} = 1;
189 foreach my $group (keys %{$usercfg->{users
}->{$authuser}->{groups
}}, '__phantom__') {
190 my $perm = $rpcenv->permissions($authuser, "/access/groups/$group");
191 if ($perm->{'User.Modify'}) {
192 $res->{access
}->{'User.Modify'} = 1;
196 foreach my $node (@$nodelist) {
197 my $perm = $rpcenv->permissions($authuser, "/nodes/$node");
198 foreach my $priv (keys %$perm) {
199 next if $priv !~ m/^Sys\./;
200 $res->{nodes
}->{$priv} = 1;
204 my $perm = $rpcenv->permissions($authuser, "/");
205 $res->{dc
}->{'Sys.Audit'} = 1 if $perm->{'Sys.Audit'};
210 __PACKAGE__-
>register_method ({
211 name
=> 'create_ticket',
215 description
=> "You need to pass valid credientials.",
218 protected
=> 1, # else we can't access shadow files
219 description
=> "Create or verify authentication ticket.",
221 additionalProperties
=> 0,
224 description
=> "User name",
228 realm
=> get_standard_option
('realm', {
229 description
=> "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
232 description
=> "The secret password. This can also be a valid ticket.",
236 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
243 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
244 type
=> 'string' , format
=> 'pve-priv-list',
254 username
=> { type
=> 'string' },
255 ticket
=> { type
=> 'string', optional
=> 1},
256 CSRFPreventionToken
=> { type
=> 'string', optional
=> 1 },
262 my $username = $param->{username
};
263 $username .= "\@$param->{realm}" if $param->{realm
};
265 my $rpcenv = PVE
::RPCEnvironment
::get
();
269 my $starttime = [gettimeofday
];
272 # test if user exists and is enabled
273 $rpcenv->check_user_enabled($username);
275 if ($param->{path
} && $param->{privs
}) {
276 $res = &$verify_auth($rpcenv, $username, $param->{password
},
277 $param->{path
}, $param->{privs
});
279 $res = &$create_ticket($rpcenv, $username, $param->{password
});
283 my $clientip = $rpcenv->get_client_ip() || '';
284 syslog
('err', "authentication failure; rhost=$clientip user=$username msg=$err");
285 # do not return any info to prevent user enumeration attacks
286 # always try to delay exactly 3 seconds to prevent timing attacks
288 while (($elapsed = tv_interval
($starttime)) < 3) {
289 usleep
(int((3 - $elapsed)*1000000));
291 die "authentication failure\n";
294 $res->{cap
} = &$compute_api_permission($rpcenv, $username);
296 PVE
::Cluster
::log_msg
('info', 'root@pam', "successful auth for user '$username'");
301 __PACKAGE__-
>register_method ({
302 name
=> 'change_passsword',
306 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.",
308 ['userid-param', 'self'],
310 [ 'userid-param', 'Realm.AllocateUser'],
311 [ 'userid-group', ['User.Modify']]
315 protected
=> 1, # else we can't access shadow files
316 description
=> "Change user password.",
318 additionalProperties
=> 0,
320 userid
=> get_standard_option
('userid'),
322 description
=> "The new password.",
329 returns
=> { type
=> "null" },
333 my $rpcenv = PVE
::RPCEnvironment
::get
();
334 my $authuser = $rpcenv->get_user();
336 my ($userid, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
338 $rpcenv->check_user_exist($userid);
340 if ($authuser eq 'root@pam') {
341 # OK - root can change anything
343 if ($authuser eq $userid) {
344 $rpcenv->check_user_enabled($userid);
345 # OK - each user can change its own password
347 # only root may change root password
348 raise_perm_exc
() if $userid eq 'root@pam';
349 # do not allow to change system user passwords
350 raise_perm_exc
() if $realm eq 'pam';
354 PVE
::AccessControl
::domain_set_password
($realm, $ruid, $param->{password
});
356 PVE
::Cluster
::log_msg
('info', 'root@pam', "changed password for user '$userid'");