1 package PVE
::API2
::AccessControl
;
9 use PVE
::Exception
qw(raise raise_perm_exc);
11 use PVE
::RPCEnvironment
;
12 use PVE
::Cluster
qw(cfs_read_file);
15 use PVE
::AccessControl
;
16 use PVE
::JSONSchema
qw(get_standard_option);
17 use PVE
::API2
::Domains
;
23 my $u2f_available = 0;
29 use base
qw(PVE::RESTHandler);
31 __PACKAGE__-
>register_method ({
32 subclass
=> "PVE::API2::User",
36 __PACKAGE__-
>register_method ({
37 subclass
=> "PVE::API2::Group",
41 __PACKAGE__-
>register_method ({
42 subclass
=> "PVE::API2::Role",
46 __PACKAGE__-
>register_method ({
47 subclass
=> "PVE::API2::ACL",
51 __PACKAGE__-
>register_method ({
52 subclass
=> "PVE::API2::Domains",
56 __PACKAGE__-
>register_method ({
60 description
=> "Directory index.",
65 additionalProperties
=> 0,
73 subdir
=> { type
=> 'string' },
76 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
83 my $ma = __PACKAGE__-
>method_attributes();
85 foreach my $info (@$ma) {
86 next if !$info->{subclass
};
88 my $subpath = $info->{match_re
}->[0];
90 push @$res, { subdir
=> $subpath };
93 push @$res, { subdir
=> 'ticket' };
94 push @$res, { subdir
=> 'password' };
100 my $verify_auth = sub {
101 my ($rpcenv, $username, $pw_or_ticket, $otp, $path, $privs) = @_;
103 my $normpath = PVE
::AccessControl
::normalize_path
($path);
106 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
107 ($ticketuser eq $username)) {
109 } elsif (PVE
::AccessControl
::verify_vnc_ticket
($pw_or_ticket, $username, $normpath, 1)) {
112 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket, $otp);
115 my $privlist = [ PVE
::Tools
::split_list
($privs) ];
116 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
117 die "no permission ($path, $privs)\n";
120 return { username
=> $username };
123 my $create_ticket = sub {
124 my ($rpcenv, $username, $pw_or_ticket, $otp) = @_;
127 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
128 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
129 # valid ticket. Note: root@pam can create tickets for other users
131 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket, $otp);
134 my $ticket = PVE
::AccessControl
::assemble_ticket
($username);
135 my $csrftoken = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
139 username
=> $username,
140 CSRFPreventionToken
=> $csrftoken,
144 my $compute_api_permission = sub {
145 my ($rpcenv, $authuser) = @_;
147 my $usercfg = $rpcenv->{user_cfg
};
151 vms
=> qr/VM\.|Permissions\.Modify/,
152 access
=> qr/(User|Group)\.|Permissions\.Modify/,
153 storage
=> qr/Datastore\.|Permissions\.Modify/,
154 nodes
=> qr/Sys\.|Permissions\.Modify/,
155 dc
=> qr/Sys\.Audit/,
157 map { $res->{$_} = {} } keys %$priv_re_map;
159 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage'];
161 my $checked_paths = {};
162 foreach my $path (@$required_paths, keys %{$usercfg->{acl
}}) {
163 next if $checked_paths->{$path};
164 $checked_paths->{$path} = 1;
166 my $path_perm = $rpcenv->permissions($authuser, $path);
168 my $toplevel = ($path =~ /^\/(\w
+)/) ?
$1 : 'dc';
169 if ($toplevel eq 'pool') {
170 foreach my $priv (keys %$path_perm) {
171 if ($priv =~ m/^VM\./) {
172 $res->{vms
}->{$priv} = 1;
173 } elsif ($priv =~ m/^Datastore\./) {
174 $res->{storage
}->{$priv} = 1;
175 } elsif ($priv eq 'Permissions.Modify') {
176 $res->{storage
}->{$priv} = 1;
177 $res->{vms
}->{$priv} = 1;
181 my $priv_regex = $priv_re_map->{$toplevel} // next;
182 foreach my $priv (keys %$path_perm) {
183 next if $priv !~ m/^($priv_regex)/;
184 $res->{$toplevel}->{$priv} = 1;
192 __PACKAGE__-
>register_method ({
193 name
=> 'get_ticket',
196 permissions
=> { user
=> 'world' },
197 description
=> "Dummy. Useful for formatters which want to provide a login page.",
199 additionalProperties
=> 0,
201 returns
=> { type
=> "null" },
202 code
=> sub { return undef; }});
204 __PACKAGE__-
>register_method ({
205 name
=> 'create_ticket',
209 description
=> "You need to pass valid credientials.",
212 protected
=> 1, # else we can't access shadow files
213 description
=> "Create or verify authentication ticket.",
215 additionalProperties
=> 0,
218 description
=> "User name",
221 completion
=> \
&PVE
::AccessControl
::complete_username
,
223 realm
=> get_standard_option
('realm', {
224 description
=> "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
226 completion
=> \
&PVE
::AccessControl
::complete_realm
,
229 description
=> "The secret password. This can also be a valid ticket.",
233 description
=> "One-time password for Two-factor authentication.",
238 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
245 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
246 type
=> 'string' , format
=> 'pve-priv-list',
256 username
=> { type
=> 'string' },
257 ticket
=> { type
=> 'string', optional
=> 1},
258 CSRFPreventionToken
=> { type
=> 'string', optional
=> 1 },
259 clustername
=> { type
=> 'string', optional
=> 1 },
265 my $username = $param->{username
};
266 $username .= "\@$param->{realm}" if $param->{realm
};
268 my $rpcenv = PVE
::RPCEnvironment
::get
();
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
}, $param->{otp
},
277 $param->{path
}, $param->{privs
});
279 $res = &$create_ticket($rpcenv, $username, $param->{password
}, $param->{otp
});
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 die PVE
::Exception-
>new("authentication failure\n", code
=> 401);
289 $res->{cap
} = &$compute_api_permission($rpcenv, $username);
291 if (PVE
::Corosync
::check_conf_exists
(1)) {
292 if ($rpcenv->check($username, '/', ['Sys.Audit'], 1)) {
294 my $conf = cfs_read_file
('corosync.conf');
295 my $totem = PVE
::Corosync
::totem_config
($conf);
296 if ($totem->{cluster_name
}) {
297 $res->{clustername
} = $totem->{cluster_name
};
304 PVE
::Cluster
::log_msg
('info', 'root@pam', "successful auth for user '$username'");
309 __PACKAGE__-
>register_method ({
310 name
=> 'change_password',
314 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.",
316 ['userid-param', 'self'],
318 [ 'userid-param', 'Realm.AllocateUser'],
319 [ 'userid-group', ['User.Modify']]
323 protected
=> 1, # else we can't access shadow files
324 description
=> "Change user password.",
326 additionalProperties
=> 0,
328 userid
=> get_standard_option
('userid-completed'),
330 description
=> "The new password.",
337 returns
=> { type
=> "null" },
341 my $rpcenv = PVE
::RPCEnvironment
::get
();
342 my $authuser = $rpcenv->get_user();
344 my ($userid, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
346 $rpcenv->check_user_exist($userid);
348 if ($authuser eq 'root@pam') {
349 # OK - root can change anything
351 if ($authuser eq $userid) {
352 $rpcenv->check_user_enabled($userid);
353 # OK - each user can change its own password
355 # only root may change root password
356 raise_perm_exc
() if $userid eq 'root@pam';
357 # do not allow to change system user passwords
358 raise_perm_exc
() if $realm eq 'pam';
362 PVE
::AccessControl
::domain_set_password
($realm, $ruid, $param->{password
});
364 PVE
::Cluster
::log_msg
('info', 'root@pam', "changed password for user '$userid'");
369 sub get_u2f_config
() {
370 die "u2f support not available\n" if !$u2f_available;
372 my $dc = cfs_read_file
('datacenter.cfg');
373 my $u2f = $dc->{u2f
};
374 die "u2f not configured in datacenter.cfg\n" if !$u2f;
375 $u2f = PVE
::JSONSchema
::parse_property_string
($PVE::Cluster
::u2f_format
, $u2f);
379 sub get_u2f_instance
{
380 my ($rpcenv, $publicKey, $keyHandle) = @_;
382 # We store the public key base64 encoded (as the api provides it in binary)
383 $publicKey = decode_base64
($publicKey) if defined($publicKey);
385 my $u2fconfig = get_u2f_config
();
386 my $u2f = PVE
::U2F-
>new();
388 # via the 'Host' header (in case a node has multiple hosts available).
389 my $origin = $u2fconfig->{origin
};
390 if (!defined($origin)) {
391 $origin = $rpcenv->get_request_host(1);
393 $origin = "https://$origin";
395 die "failed to figure out u2f origin\n";
399 my $appid = $u2fconfig->{appid
} // $origin;
400 $u2f->set_appid($appid);
401 $u2f->set_origin($origin);
402 $u2f->set_publicKey($publicKey) if defined($publicKey);
403 $u2f->set_keyHandle($keyHandle) if defined($keyHandle);
407 __PACKAGE__-
>register_method ({
408 name
=> 'change_tfa',
412 description
=> 'A user can change their own u2f token.',
414 ['userid-param', 'self'],
416 [ 'userid-param', 'Realm.AllocateUser'],
417 [ 'userid-group', ['User.Modify']]
421 protected
=> 1, # else we can't access shadow files
422 description
=> "Change user u2f authentication.",
424 additionalProperties
=> 0,
426 userid
=> get_standard_option
('userid', {
427 completion
=> \
&PVE
::AccessControl
::complete_username
,
430 optional
=> 1, # Only required if not root@pam
431 description
=> "The current password.",
437 description
=> 'The action to perform',
439 enum
=> [qw(delete new confirm)],
443 description
=> 'The response to the current registration challenge.',
448 returns
=> { type
=> 'object' },
452 my $rpcenv = PVE
::RPCEnvironment
::get
();
453 my $authuser = $rpcenv->get_user();
455 my $action = delete $param->{action
};
456 my $response = delete $param->{response
};
457 my $password = delete($param->{password
}) // '';
459 my ($userid, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
460 $rpcenv->check_user_exist($userid);
462 # Only root may modify root
463 raise_perm_exc
() if $userid eq 'root@pam' && $authuser ne 'root@pam';
465 # Regular users need to confirm their password to change u2f settings.
466 if ($authuser ne 'root@pam') {
467 raise_param_exc
('password' => 'password is required to modify u2f data')
468 if !defined($password);
469 my $domain_cfg = cfs_read_file
('domains.cfg');
470 my $cfg = $domain_cfg->{ids
}->{$realm};
471 die "auth domain '$realm' does not exists\n" if !$cfg;
472 my $plugin = PVE
::Auth
::Plugin-
>lookup($cfg->{type
});
473 $plugin->authenticate_user($cfg, $realm, $ruid, $password);
476 if ($action eq 'delete') {
477 PVE
::AccessControl
::user_set_tfa
($userid, $realm, undef, undef);
478 PVE
::Cluster
::log_msg
('info', $authuser, "deleted u2f data for user '$userid'");
479 } elsif ($action eq 'new') {
480 my $u2f = get_u2f_instance
($rpcenv);
481 my $challenge = $u2f->registration_challenge()
482 or raise
("failed to get u2f challenge");
483 $challenge = decode_json
($challenge);
484 PVE
::AccessControl
::user_set_tfa
($userid, $realm, 'u2f', $challenge);
486 } elsif ($action eq 'confirm') {
487 raise_param_exc
('response' => "confirm action requires the 'response' parameter to be set")
488 if !defined($response);
490 my ($type, $u2fdata) = PVE
::AccessControl
::user_get_tfa
($userid, $realm);
491 raise
("no u2f data available")
492 if (!defined($type) || $type ne 'u2f');
494 my $challenge = $u2fdata->{challenge
}
495 or raise
("no active challenge");
497 my $u2f = get_u2f_instance
($rpcenv);
498 $u2f->set_challenge($challenge);
499 my ($keyHandle, $publicKey) = $u2f->registration_verify($response);
500 PVE
::AccessControl
::user_set_tfa
($userid, $realm, 'u2f', {
501 keyHandle
=> $keyHandle,
502 publicKey
=> encode_base64
($publicKey, ''),
505 die "invalid action: $action\n";
511 __PACKAGE__-
>register_method({
512 name
=> 'verify_tfa',
515 permissions
=> { user
=> 'all' },
516 protected
=> 1, # else we can't access shadow files
517 description
=> 'Finish a u2f challenge.',
519 additionalProperties
=> 0,
523 description
=> 'The response to the current authentication challenge.',
530 ticket
=> { type
=> 'string' },
537 my $rpcenv = PVE
::RPCEnvironment
::get
();
538 my $challenge = $rpcenv->get_u2f_challenge()
539 or raise
('no active challenge');
540 my $authuser = $rpcenv->get_user();
541 my ($username, undef, $realm) = PVE
::AccessControl
::verify_username
($authuser);
543 my ($tfa_type, $u2fdata) = PVE
::AccessControl
::user_get_tfa
($username, $realm);
544 if (!defined($tfa_type) || $tfa_type ne 'u2f') {
545 raise
('no u2f data available');
548 my $keyHandle = $u2fdata->{keyHandle
};
549 my $publicKey = $u2fdata->{publicKey
};
550 raise
("incomplete u2f setup")
551 if !defined($keyHandle) || !defined($publicKey);
553 my $u2f = get_u2f_instance
($rpcenv, $publicKey, $keyHandle);
554 $u2f->set_challenge($challenge);
557 my ($counter, $present) = $u2f->auth_verify($param->{response
});
558 # Do we want to do anything with these?
561 my $clientip = $rpcenv->get_client_ip() || '';
562 syslog
('err', "authentication verification failure; rhost=$clientip user=$authuser msg=$err");
563 die PVE
::Exception-
>new("authentication failure\n", code
=> 401);
566 # create a new ticket for the user:
567 my $ticket_data = "u2f!$authuser!verified";
569 ticket
=> PVE
::AccessControl
::assemble_ticket
($ticket_data),
570 cap
=> &$compute_api_permission($rpcenv, $authuser),