1 package PVE
::API2
::AccessControl
;
9 use PVE
::Exception
qw(raise raise_perm_exc raise_param_exc);
11 use PVE
::RPCEnvironment
;
12 use PVE
::Cluster
qw(cfs_read_file);
13 use PVE
::DataCenterConfig
;
15 use PVE
::AccessControl
;
16 use PVE
::JSONSchema
qw(get_standard_option);
17 use PVE
::API2
::Domains
;
22 use PVE
::API2
::OpenId
;
24 use PVE
::Auth
::Plugin
;
27 my $u2f_available = 0;
33 use base
qw(PVE::RESTHandler);
35 __PACKAGE__-
>register_method ({
36 subclass
=> "PVE::API2::User",
40 __PACKAGE__-
>register_method ({
41 subclass
=> "PVE::API2::Group",
45 __PACKAGE__-
>register_method ({
46 subclass
=> "PVE::API2::Role",
50 __PACKAGE__-
>register_method ({
51 subclass
=> "PVE::API2::ACL",
55 __PACKAGE__-
>register_method ({
56 subclass
=> "PVE::API2::Domains",
60 __PACKAGE__-
>register_method ({
61 subclass
=> "PVE::API2::OpenId",
65 __PACKAGE__-
>register_method ({
66 subclass
=> "PVE::API2::TFA",
70 __PACKAGE__-
>register_method ({
74 description
=> "Directory index.",
79 additionalProperties
=> 0,
87 subdir
=> { type
=> 'string' },
90 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
97 my $ma = __PACKAGE__-
>method_attributes();
99 foreach my $info (@$ma) {
100 next if !$info->{subclass
};
102 my $subpath = $info->{match_re
}->[0];
104 push @$res, { subdir
=> $subpath };
107 push @$res, { subdir
=> 'ticket' };
108 push @$res, { subdir
=> 'password' };
114 my sub verify_auth
: prototype($$$$$$) {
115 my ($rpcenv, $username, $pw_or_ticket, $otp, $path, $privs) = @_;
117 my $normpath = PVE
::AccessControl
::normalize_path
($path);
118 die "invalid path - $path\n" if defined($path) && !defined($normpath);
121 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
122 ($ticketuser eq $username)) {
124 } elsif (PVE
::AccessControl
::verify_vnc_ticket
($pw_or_ticket, $username, $normpath, 1)) {
127 $username = PVE
::AccessControl
::authenticate_user
(
134 my $privlist = [ PVE
::Tools
::split_list
($privs) ];
135 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
136 die "no permission ($path, $privs)\n";
139 return { username
=> $username };
142 my sub create_ticket_do
: prototype($$$$$) {
143 my ($rpcenv, $username, $pw_or_ticket, $otp, $tfa_challenge) = @_;
145 die "TFA response should be in 'password', not 'otp' when 'tfa-challenge' is set\n"
146 if defined($otp) && defined($tfa_challenge);
148 my ($ticketuser, undef, $tfa_info);
149 if (!defined($tfa_challenge)) {
150 # We only verify this ticket if we're not responding to a TFA challenge, as in that case
151 # it is a TFA-data ticket and will be verified by `authenticate_user`.
153 ($ticketuser, undef, $tfa_info) = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1);
156 if (defined($ticketuser) && ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
157 if (defined($tfa_info)) {
158 die "incomplete ticket\n";
160 # valid ticket. Note: root@pam can create tickets for other users
162 ($username, $tfa_info) = PVE
::AccessControl
::authenticate_user
(
171 my $ticket_data = $username;
173 if (defined($tfa_info)) {
175 $ticket_data = "!tfa!$tfa_info";
179 my $ticket = PVE
::AccessControl
::assemble_ticket
($ticket_data, $aad);
180 my $csrftoken = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
184 username
=> $username,
185 CSRFPreventionToken
=> $csrftoken,
190 __PACKAGE__-
>register_method ({
191 name
=> 'get_ticket',
194 permissions
=> { user
=> 'world' },
195 description
=> "Dummy. Useful for formatters which want to provide a login page.",
197 additionalProperties
=> 0,
199 returns
=> { type
=> "null" },
200 code
=> sub { return undef; }});
202 __PACKAGE__-
>register_method ({
203 name
=> 'create_ticket',
207 description
=> "You need to pass valid credientials.",
210 protected
=> 1, # else we can't access shadow files
211 allowtoken
=> 0, # we don't want tokens to create tickets
212 description
=> "Create or verify authentication ticket.",
214 additionalProperties
=> 0,
217 description
=> "User name",
220 completion
=> \
&PVE
::AccessControl
::complete_username
,
222 realm
=> get_standard_option
('realm', {
223 description
=> "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
225 completion
=> \
&PVE
::AccessControl
::complete_realm
,
228 description
=> "The secret password. This can also be a valid ticket.",
232 description
=> "One-time password for Two-factor authentication.",
237 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
244 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
245 type
=> 'string' , format
=> 'pve-priv-list',
252 description
=> 'This parameter is now ignored and assumed to be 1.',
258 description
=> "The signed TFA challenge string the user wants to respond to.",
266 username
=> { type
=> 'string' },
267 ticket
=> { type
=> 'string', optional
=> 1},
268 CSRFPreventionToken
=> { type
=> 'string', optional
=> 1 },
269 clustername
=> { type
=> 'string', optional
=> 1 },
270 # cap => computed api permissions, unless there's a u2f challenge
276 my $username = $param->{username
};
277 $username .= "\@$param->{realm}" if $param->{realm
};
279 $username = PVE
::AccessControl
::lookup_username
($username);
280 my $rpcenv = PVE
::RPCEnvironment
::get
();
284 # test if user exists and is enabled
285 $rpcenv->check_user_enabled($username);
287 if ($param->{path
} && $param->{privs
}) {
288 $res = verify_auth
($rpcenv, $username, $param->{password
}, $param->{otp
},
289 $param->{path
}, $param->{privs
});
291 $res = create_ticket_do
(
296 $param->{'tfa-challenge'},
301 my $clientip = $rpcenv->get_client_ip() || '';
302 syslog
('err', "authentication failure; rhost=$clientip user=$username msg=$err");
303 # do not return any info to prevent user enumeration attacks
304 die PVE
::Exception-
>new("authentication failure\n", code
=> 401);
307 $res->{cap
} = $rpcenv->compute_api_permission($username)
308 if !defined($res->{NeedTFA
});
310 my $clinfo = PVE
::Cluster
::get_clinfo
();
311 if ($clinfo->{cluster
}->{name
} && $rpcenv->check($username, '/', ['Sys.Audit'], 1)) {
312 $res->{clustername
} = $clinfo->{cluster
}->{name
};
315 PVE
::Cluster
::log_msg
('info', 'root@pam', "successful auth for user '$username'");
320 __PACKAGE__-
>register_method ({
321 name
=> 'change_password',
325 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.",
327 ['userid-param', 'self'],
329 [ 'userid-param', 'Realm.AllocateUser'],
330 [ 'userid-group', ['User.Modify']]
334 protected
=> 1, # else we can't access shadow files
335 allowtoken
=> 0, # we don't want tokens to change the regular user password
336 description
=> "Change user password.",
338 additionalProperties
=> 0,
340 userid
=> get_standard_option
('userid-completed'),
342 description
=> "The new password.",
347 'confirmation-password' => $PVE::API2
::TFA
::OPTIONAL_PASSWORD_SCHEMA
,
350 returns
=> { type
=> "null" },
354 my $rpcenv = PVE
::RPCEnvironment
::get
();
355 my $authuser = $rpcenv->get_user();
357 my ($userid, $ruid, $realm) = $rpcenv->reauth_user_for_user_modification(
360 $param->{'confirmation-password'},
361 'confirmation-password',
364 if ($authuser eq 'root@pam') {
365 # OK - root can change anything
367 if ($authuser eq $userid) {
368 $rpcenv->check_user_enabled($userid);
369 # OK - each user can change its own password
371 # only root may change root password
372 raise_perm_exc
() if $userid eq 'root@pam';
373 # do not allow to change system user passwords
374 raise_perm_exc
() if $realm eq 'pam';
378 PVE
::AccessControl
::domain_set_password
($realm, $ruid, $param->{password
});
380 PVE
::Cluster
::log_msg
('info', 'root@pam', "changed password for user '$userid'");
385 sub get_u2f_config
() {
386 die "u2f support not available\n" if !$u2f_available;
388 my $dc = cfs_read_file
('datacenter.cfg');
389 my $u2f = $dc->{u2f
};
390 die "u2f not configured in datacenter.cfg\n" if !$u2f;
394 sub get_u2f_instance
{
395 my ($rpcenv, $publicKey, $keyHandle) = @_;
397 # We store the public key base64 encoded (as the api provides it in binary)
398 $publicKey = decode_base64
($publicKey) if defined($publicKey);
400 my $u2fconfig = get_u2f_config
();
401 my $u2f = PVE
::U2F-
>new();
403 # via the 'Host' header (in case a node has multiple hosts available).
404 my $origin = $u2fconfig->{origin
};
405 if (!defined($origin)) {
406 $origin = $rpcenv->get_request_host(1);
408 $origin = "https://$origin";
410 die "failed to figure out u2f origin\n";
414 my $appid = $u2fconfig->{appid
} // $origin;
415 $u2f->set_appid($appid);
416 $u2f->set_origin($origin);
417 $u2f->set_publicKey($publicKey) if defined($publicKey);
418 $u2f->set_keyHandle($keyHandle) if defined($keyHandle);
422 sub verify_user_tfa_config
{
423 my ($type, $tfa_cfg, $value) = @_;
425 if (!defined($type)) {
426 die "missing tfa 'type'\n";
429 if ($type ne 'oath') {
430 die "invalid type for custom tfa authentication\n";
433 my $secret = $tfa_cfg->{keys}
434 or die "missing TOTP secret\n";
435 $tfa_cfg = $tfa_cfg->{config
};
436 # Copy the hash to verify that we have no unexpected keys without modifying the original hash.
437 $tfa_cfg = {%$tfa_cfg};
439 # We can only verify 1 secret but oath_verify_otp allows multiple:
440 if (scalar(PVE
::Tools
::split_list
($secret)) != 1) {
441 die "only exactly one secret key allowed\n";
444 my $digits = delete($tfa_cfg->{digits
}) // 6;
445 my $step = delete($tfa_cfg->{step
}) // 30;
447 # my $algorithm = delete($tfa_cfg->{algorithm}) // 'sha1';
449 if (length(my $more = join(', ', keys %$tfa_cfg))) {
450 die "unexpected tfa config keys: $more\n";
453 PVE
::OTP
::oath_verify_otp
($value, $secret, $step, $digits);
457 __PACKAGE__-
>register_method({
458 name
=> 'permissions',
459 path
=> 'permissions',
461 description
=> 'Retrieve effective permissions of given user/token.',
463 description
=> "Each user/token is allowed to dump their own permissions. A user can dump the permissions of another user if they have 'Sys.Audit' permission on /access.",
467 additionalProperties
=> 0,
471 description
=> "User ID or full API token ID",
472 pattern
=> $PVE::AccessControl
::userid_or_token_regex
,
475 path
=> get_standard_option
('acl-path', {
476 description
=> "Only dump this specific path, not the whole tree.",
483 description
=> 'Map of "path" => (Map of "privilege" => "propagate boolean").',
488 my $rpcenv = PVE
::RPCEnvironment
::get
();
490 my $userid = $param->{userid
};
491 if (defined($userid)) {
492 $rpcenv->check($rpcenv->get_user(), '/access', ['Sys.Audit']);
494 $userid = $rpcenv->get_user();
499 if (my $path = $param->{path
}) {
500 my $perms = $rpcenv->permissions($userid, $path);
502 $res = { $path => $perms };
507 $res = $rpcenv->get_effective_permissions($userid);