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) = @_;
126 my ($ticketuser, $u2fdata);
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, $u2fdata) = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket, $otp);
135 my $ticket_data = $username;
136 if (defined($u2fdata)) {
137 my $u2f = get_u2f_instance
($rpcenv, $u2fdata->@{qw(publicKey keyHandle)});
138 my $challenge = $u2f->auth_challenge()
139 or die "failed to get u2f challenge\n";
140 $challenge = decode_json
($challenge);
141 $extra{U2FChallenge
} = $challenge;
142 $ticket_data = "u2f!$username!$challenge->{challenge}";
145 my $ticket = PVE
::AccessControl
::assemble_ticket
($ticket_data);
146 my $csrftoken = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
150 username
=> $username,
151 CSRFPreventionToken
=> $csrftoken,
156 my $compute_api_permission = sub {
157 my ($rpcenv, $authuser) = @_;
159 my $usercfg = $rpcenv->{user_cfg
};
163 vms
=> qr/VM\.|Permissions\.Modify/,
164 access
=> qr/(User|Group)\.|Permissions\.Modify/,
165 storage
=> qr/Datastore\.|Permissions\.Modify/,
166 nodes
=> qr/Sys\.|Permissions\.Modify/,
167 dc
=> qr/Sys\.Audit/,
169 map { $res->{$_} = {} } keys %$priv_re_map;
171 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage'];
173 my $checked_paths = {};
174 foreach my $path (@$required_paths, keys %{$usercfg->{acl
}}) {
175 next if $checked_paths->{$path};
176 $checked_paths->{$path} = 1;
178 my $path_perm = $rpcenv->permissions($authuser, $path);
180 my $toplevel = ($path =~ /^\/(\w
+)/) ?
$1 : 'dc';
181 if ($toplevel eq 'pool') {
182 foreach my $priv (keys %$path_perm) {
183 if ($priv =~ m/^VM\./) {
184 $res->{vms
}->{$priv} = 1;
185 } elsif ($priv =~ m/^Datastore\./) {
186 $res->{storage
}->{$priv} = 1;
187 } elsif ($priv eq 'Permissions.Modify') {
188 $res->{storage
}->{$priv} = 1;
189 $res->{vms
}->{$priv} = 1;
193 my $priv_regex = $priv_re_map->{$toplevel} // next;
194 foreach my $priv (keys %$path_perm) {
195 next if $priv !~ m/^($priv_regex)/;
196 $res->{$toplevel}->{$priv} = 1;
204 __PACKAGE__-
>register_method ({
205 name
=> 'get_ticket',
208 permissions
=> { user
=> 'world' },
209 description
=> "Dummy. Useful for formatters which want to provide a login page.",
211 additionalProperties
=> 0,
213 returns
=> { type
=> "null" },
214 code
=> sub { return undef; }});
216 __PACKAGE__-
>register_method ({
217 name
=> 'create_ticket',
221 description
=> "You need to pass valid credientials.",
224 protected
=> 1, # else we can't access shadow files
225 description
=> "Create or verify authentication ticket.",
227 additionalProperties
=> 0,
230 description
=> "User name",
233 completion
=> \
&PVE
::AccessControl
::complete_username
,
235 realm
=> get_standard_option
('realm', {
236 description
=> "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
238 completion
=> \
&PVE
::AccessControl
::complete_realm
,
241 description
=> "The secret password. This can also be a valid ticket.",
245 description
=> "One-time password for Two-factor authentication.",
250 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
257 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
258 type
=> 'string' , format
=> 'pve-priv-list',
268 username
=> { type
=> 'string' },
269 ticket
=> { type
=> 'string', optional
=> 1},
270 CSRFPreventionToken
=> { type
=> 'string', optional
=> 1 },
271 clustername
=> { type
=> 'string', optional
=> 1 },
272 # cap => computed api permissions, unless there's a u2f challenge
278 my $username = $param->{username
};
279 $username .= "\@$param->{realm}" if $param->{realm
};
281 my $rpcenv = PVE
::RPCEnvironment
::get
();
285 # test if user exists and is enabled
286 $rpcenv->check_user_enabled($username);
288 if ($param->{path
} && $param->{privs
}) {
289 $res = &$verify_auth($rpcenv, $username, $param->{password
}, $param->{otp
},
290 $param->{path
}, $param->{privs
});
292 $res = &$create_ticket($rpcenv, $username, $param->{password
}, $param->{otp
});
296 my $clientip = $rpcenv->get_client_ip() || '';
297 syslog
('err', "authentication failure; rhost=$clientip user=$username msg=$err");
298 # do not return any info to prevent user enumeration attacks
299 die PVE
::Exception-
>new("authentication failure\n", code
=> 401);
302 $res->{cap
} = &$compute_api_permission($rpcenv, $username)
303 if !defined($res->{U2FChallenge
});
305 if (PVE
::Corosync
::check_conf_exists
(1)) {
306 if ($rpcenv->check($username, '/', ['Sys.Audit'], 1)) {
308 my $conf = cfs_read_file
('corosync.conf');
309 my $totem = PVE
::Corosync
::totem_config
($conf);
310 if ($totem->{cluster_name
}) {
311 $res->{clustername
} = $totem->{cluster_name
};
318 PVE
::Cluster
::log_msg
('info', 'root@pam', "successful auth for user '$username'");
323 __PACKAGE__-
>register_method ({
324 name
=> 'change_password',
328 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.",
330 ['userid-param', 'self'],
332 [ 'userid-param', 'Realm.AllocateUser'],
333 [ 'userid-group', ['User.Modify']]
337 protected
=> 1, # else we can't access shadow files
338 description
=> "Change user password.",
340 additionalProperties
=> 0,
342 userid
=> get_standard_option
('userid-completed'),
344 description
=> "The new password.",
351 returns
=> { type
=> "null" },
355 my $rpcenv = PVE
::RPCEnvironment
::get
();
356 my $authuser = $rpcenv->get_user();
358 my ($userid, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
360 $rpcenv->check_user_exist($userid);
362 if ($authuser eq 'root@pam') {
363 # OK - root can change anything
365 if ($authuser eq $userid) {
366 $rpcenv->check_user_enabled($userid);
367 # OK - each user can change its own password
369 # only root may change root password
370 raise_perm_exc
() if $userid eq 'root@pam';
371 # do not allow to change system user passwords
372 raise_perm_exc
() if $realm eq 'pam';
376 PVE
::AccessControl
::domain_set_password
($realm, $ruid, $param->{password
});
378 PVE
::Cluster
::log_msg
('info', 'root@pam', "changed password for user '$userid'");
383 sub get_u2f_config
() {
384 die "u2f support not available\n" if !$u2f_available;
386 my $dc = cfs_read_file
('datacenter.cfg');
387 my $u2f = $dc->{u2f
};
388 die "u2f not configured in datacenter.cfg\n" if !$u2f;
389 $u2f = PVE
::JSONSchema
::parse_property_string
($PVE::Cluster
::u2f_format
, $u2f);
393 sub get_u2f_instance
{
394 my ($rpcenv, $publicKey, $keyHandle) = @_;
396 # We store the public key base64 encoded (as the api provides it in binary)
397 $publicKey = decode_base64
($publicKey) if defined($publicKey);
399 my $u2fconfig = get_u2f_config
();
400 my $u2f = PVE
::U2F-
>new();
402 # via the 'Host' header (in case a node has multiple hosts available).
403 my $origin = $u2fconfig->{origin
};
404 if (!defined($origin)) {
405 $origin = $rpcenv->get_request_host(1);
407 $origin = "https://$origin";
409 die "failed to figure out u2f origin\n";
413 my $appid = $u2fconfig->{appid
} // $origin;
414 $u2f->set_appid($appid);
415 $u2f->set_origin($origin);
416 $u2f->set_publicKey($publicKey) if defined($publicKey);
417 $u2f->set_keyHandle($keyHandle) if defined($keyHandle);
421 __PACKAGE__-
>register_method ({
422 name
=> 'change_tfa',
426 description
=> 'A user can change their own u2f token.',
428 ['userid-param', 'self'],
430 [ 'userid-param', 'Realm.AllocateUser'],
431 [ 'userid-group', ['User.Modify']]
435 protected
=> 1, # else we can't access shadow files
436 description
=> "Change user u2f authentication.",
438 additionalProperties
=> 0,
440 userid
=> get_standard_option
('userid', {
441 completion
=> \
&PVE
::AccessControl
::complete_username
,
444 optional
=> 1, # Only required if not root@pam
445 description
=> "The current password.",
451 description
=> 'The action to perform',
453 enum
=> [qw(delete new confirm)],
457 description
=> 'The response to the current registration challenge.',
462 returns
=> { type
=> 'object' },
466 my $rpcenv = PVE
::RPCEnvironment
::get
();
467 my $authuser = $rpcenv->get_user();
469 my $action = delete $param->{action
};
470 my $response = delete $param->{response
};
471 my $password = delete($param->{password
}) // '';
473 my ($userid, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
474 $rpcenv->check_user_exist($userid);
476 # Only root may modify root
477 raise_perm_exc
() if $userid eq 'root@pam' && $authuser ne 'root@pam';
479 # Regular users need to confirm their password to change u2f settings.
480 if ($authuser ne 'root@pam') {
481 raise_param_exc
('password' => 'password is required to modify u2f data')
482 if !defined($password);
483 my $domain_cfg = cfs_read_file
('domains.cfg');
484 my $cfg = $domain_cfg->{ids
}->{$realm};
485 die "auth domain '$realm' does not exists\n" if !$cfg;
486 my $plugin = PVE
::Auth
::Plugin-
>lookup($cfg->{type
});
487 $plugin->authenticate_user($cfg, $realm, $ruid, $password);
490 if ($action eq 'delete') {
491 PVE
::AccessControl
::user_set_tfa
($userid, $realm, undef, undef);
492 PVE
::Cluster
::log_msg
('info', $authuser, "deleted u2f data for user '$userid'");
493 } elsif ($action eq 'new') {
494 my $u2f = get_u2f_instance
($rpcenv);
495 my $challenge = $u2f->registration_challenge()
496 or raise
("failed to get u2f challenge");
497 $challenge = decode_json
($challenge);
498 PVE
::AccessControl
::user_set_tfa
($userid, $realm, 'u2f', $challenge);
500 } elsif ($action eq 'confirm') {
501 raise_param_exc
('response' => "confirm action requires the 'response' parameter to be set")
502 if !defined($response);
504 my ($type, $u2fdata) = PVE
::AccessControl
::user_get_tfa
($userid, $realm);
505 raise
("no u2f data available")
506 if (!defined($type) || $type ne 'u2f');
508 my $challenge = $u2fdata->{challenge
}
509 or raise
("no active challenge");
511 my $u2f = get_u2f_instance
($rpcenv);
512 $u2f->set_challenge($challenge);
513 my ($keyHandle, $publicKey) = $u2f->registration_verify($response);
514 PVE
::AccessControl
::user_set_tfa
($userid, $realm, 'u2f', {
515 keyHandle
=> $keyHandle,
516 publicKey
=> encode_base64
($publicKey, ''),
519 die "invalid action: $action\n";
525 __PACKAGE__-
>register_method({
526 name
=> 'verify_tfa',
529 permissions
=> { user
=> 'all' },
530 protected
=> 1, # else we can't access shadow files
531 description
=> 'Finish a u2f challenge.',
533 additionalProperties
=> 0,
537 description
=> 'The response to the current authentication challenge.',
544 ticket
=> { type
=> 'string' },
551 my $rpcenv = PVE
::RPCEnvironment
::get
();
552 my $challenge = $rpcenv->get_u2f_challenge()
553 or raise
('no active challenge');
554 my $authuser = $rpcenv->get_user();
555 my ($username, undef, $realm) = PVE
::AccessControl
::verify_username
($authuser);
557 my ($tfa_type, $u2fdata) = PVE
::AccessControl
::user_get_tfa
($username, $realm);
558 if (!defined($tfa_type) || $tfa_type ne 'u2f') {
559 raise
('no u2f data available');
562 my $keyHandle = $u2fdata->{keyHandle
};
563 my $publicKey = $u2fdata->{publicKey
};
564 raise
("incomplete u2f setup")
565 if !defined($keyHandle) || !defined($publicKey);
567 my $u2f = get_u2f_instance
($rpcenv, $publicKey, $keyHandle);
568 $u2f->set_challenge($challenge);
571 my ($counter, $present) = $u2f->auth_verify($param->{response
});
572 # Do we want to do anything with these?
575 my $clientip = $rpcenv->get_client_ip() || '';
576 syslog
('err', "authentication verification failure; rhost=$clientip user=$authuser msg=$err");
577 die PVE
::Exception-
>new("authentication failure\n", code
=> 401);
580 # create a new ticket for the user:
581 my $ticket_data = "u2f!$authuser!verified";
583 ticket
=> PVE
::AccessControl
::assemble_ticket
($ticket_data),
584 cap
=> &$compute_api_permission($rpcenv, $authuser),