]> git.proxmox.com Git - pve-access-control.git/blobdiff - src/PVE/API2/TFA.pm
bump version to 8.1.4
[pve-access-control.git] / src / PVE / API2 / TFA.pm
index 31e57415169407c9bbc63f47af448c44724c21f1..62ddd959378008ae3caf6ea92e04d43b489fc358 100644 (file)
@@ -7,9 +7,10 @@ use HTTP::Status qw(:constants);
 
 use PVE::AccessControl;
 use PVE::Cluster qw(cfs_read_file cfs_write_file);
-use PVE::JSONSchema qw(get_standard_option);
 use PVE::Exception qw(raise raise_perm_exc raise_param_exc);
+use PVE::JSONSchema qw(get_standard_option);
 use PVE::RPCEnvironment;
+use PVE::SafeSyslog;
 
 use PVE::API2::AccessControl; # for old login api get_u2f_instance method
 
@@ -17,8 +18,8 @@ use PVE::RESTHandler;
 
 use base qw(PVE::RESTHandler);
 
-my $OPTIONAL_PASSWORD_SCHEMA = {
-    description => "The current password.",
+our $OPTIONAL_PASSWORD_SCHEMA = {
+    description => "The current password of the user performing the change.",
     type => 'string',
     optional => 1, # Only required if not root@pam
     minLength => 5,
@@ -94,36 +95,6 @@ my $TFA_UPDATE_INFO_SCHEMA = {
     },
 };
 
-# Only root may modify root, regular users need to specify their password.
-#
-# Returns the userid returned from `verify_username`.
-# Or ($userid, $realm) in list context.
-my sub root_permission_check : prototype($$$$) {
-    my ($rpcenv, $authuser, $userid, $password) = @_;
-
-    ($userid, undef, my $realm) = PVE::AccessControl::verify_username($userid);
-    $rpcenv->check_user_exist($userid);
-
-    raise_perm_exc() if $userid eq 'root@pam' && $authuser ne 'root@pam';
-
-    # Regular users need to confirm their password to change TFA settings.
-    if ($authuser ne 'root@pam') {
-       raise_param_exc({ 'password' => 'password is required to modify TFA data' })
-           if !defined($password);
-
-       ($authuser, my $auth_username, my $auth_realm) =
-           PVE::AccessControl::verify_username($authuser);
-
-       my $domain_cfg = cfs_read_file('domains.cfg');
-       my $cfg = $domain_cfg->{ids}->{$auth_realm};
-       die "auth domain '$auth_realm' does not exist\n" if !$cfg;
-       my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
-       $plugin->authenticate_user($cfg, $auth_realm, $auth_username, $password);
-    }
-
-    return wantarray ? ($userid, $realm) : $userid;
-}
-
 # Set TFA to enabled if $tfa_cfg is passed, or to disabled if $tfa_cfg is undef,
 # When enabling we also merge the old user.cfg keys into the $tfa_cfg.
 my sub set_user_tfa_enabled : prototype($$$) {
@@ -151,83 +122,6 @@ my sub set_user_tfa_enabled : prototype($$$) {
     }, "enabling TFA for the user failed");
 }
 
-### OLD API
-
-__PACKAGE__->register_method({
-    name => 'verify_tfa',
-    path => '',
-    method => 'POST',
-    permissions => { user => 'all' },
-    protected => 1, # else we can't access shadow files
-    allowtoken => 0, # we don't want tokens to access TFA information
-    description => 'Finish a u2f challenge.',
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           response => {
-               type => 'string',
-               description => 'The response to the current authentication challenge.',
-           },
-       }
-    },
-    returns => {
-       type => 'object',
-       properties => {
-           ticket => { type => 'string' },
-           # cap
-       }
-    },
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-       my $authuser = $rpcenv->get_user();
-       my ($username, undef, $realm) = PVE::AccessControl::verify_username($authuser);
-
-       my ($tfa_type, $tfa_data) = PVE::AccessControl::user_get_tfa($username, $realm, 0);
-       if (!defined($tfa_type)) {
-           raise('no u2f data available');
-       }
-       if ($tfa_type eq 'incompatible') {
-           raise('tfa entries incompatible with old login api');
-       }
-
-       eval {
-           if ($tfa_type eq 'u2f') {
-               my $challenge = $rpcenv->get_u2f_challenge()
-                  or raise('no active challenge');
-
-               my $keyHandle = $tfa_data->{keyHandle};
-               my $publicKey = $tfa_data->{publicKey};
-               raise("incomplete u2f setup")
-                   if !defined($keyHandle) || !defined($publicKey);
-
-               my $u2f = PVE::API2::AccessControl::get_u2f_instance($rpcenv, $publicKey, $keyHandle);
-               $u2f->set_challenge($challenge);
-
-               my ($counter, $present) = $u2f->auth_verify($param->{response});
-               # Do we want to do anything with these?
-           } else {
-               # sanity check before handing off to the verification code:
-               my $keys = $tfa_data->{keys} or die "missing tfa keys\n";
-               my $config = $tfa_data->{config} or die "bad tfa entry\n";
-               PVE::AccessControl::verify_one_time_pw($tfa_type, $authuser, $keys, $config, $param->{response});
-           }
-       };
-       if (my $err = $@) {
-           my $clientip = $rpcenv->get_client_ip() || '';
-           syslog('err', "authentication verification failure; rhost=$clientip user=$authuser msg=$err");
-           die PVE::Exception->new("authentication failure\n", code => 401);
-       }
-
-       return {
-           ticket => PVE::AccessControl::assemble_ticket($authuser),
-           cap => $rpcenv->compute_api_permission($authuser),
-       }
-    }});
-
-### END OLD API
-
 __PACKAGE__->register_method ({
     name => 'list_user_tfa',
     path => '{userid}',
@@ -239,7 +133,6 @@ __PACKAGE__->register_method ({
        ],
     },
     protected => 1, # else we can't access shadow files
-    allowtoken => 0, # we don't want tokens to change the regular user's TFA settings
     description => 'List TFA configurations of users.',
     parameters => {
        additionalProperties => 0,
@@ -253,6 +146,7 @@ __PACKAGE__->register_method ({
        description => "A list of the user's TFA entries.",
        type => 'array',
        items => $TYPED_TFA_ENTRY_SCHEMA,
+       links => [ { rel => 'child', href => "{id}" } ],
     },
     code => sub {
        my ($param) = @_;
@@ -271,7 +165,6 @@ __PACKAGE__->register_method ({
        ],
     },
     protected => 1, # else we can't access shadow files
-    allowtoken => 0, # we don't want tokens to change the regular user's TFA settings
     description => 'Fetch a requested TFA entry if present.',
     parameters => {
        additionalProperties => 0,
@@ -319,12 +212,13 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       PVE::AccessControl::assert_new_tfa_config_available();
-       
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
-       my $userid =
-           root_permission_check($rpcenv, $authuser, $param->{userid}, $param->{password});
+       my $userid = $rpcenv->reauth_user_for_user_modification(
+           $authuser,
+           $param->{userid},
+           $param->{password},
+       );
 
        my $has_entries_left = PVE::AccessControl::lock_tfa_config(sub {
            my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
@@ -346,7 +240,6 @@ __PACKAGE__->register_method ({
        user => 'all',
     },
     protected => 1, # else we can't access shadow files
-    allowtoken => 0, # we don't want tokens to change the regular user's TFA settings
     description => 'List TFA configurations of users.',
     parameters => {
        additionalProperties => 0,
@@ -366,18 +259,44 @@ __PACKAGE__->register_method ({
                    type => 'array',
                    items => $TYPED_TFA_ENTRY_SCHEMA,
                },
+               'totp-locked' => {
+                   type => 'boolean',
+                   optional => 1,
+                   description => 'True if the user is currently locked out of TOTP factors.',
+               },
+               'tfa-locked-until' => {
+                   type => 'integer',
+                   optional => 1,
+                   description =>
+                       'Contains a timestamp until when a user is locked out of 2nd factors.',
+               },
            },
        },
+       links => [ { rel => 'child', href => "{userid}" } ],
     },
     code => sub {
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
-       my $top_level_allowed = ($authuser eq 'root@pam');
 
        my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
-       return $tfa_cfg->api_list_tfa($authuser, $top_level_allowed);
+       my $entries = $tfa_cfg->api_list_tfa($authuser, 1);
+
+       my $privs = [ 'User.Modify', 'Sys.Audit' ];
+       if ($rpcenv->check_any($authuser, "/access/groups", $privs, 1)) {
+           # can modify all
+           return $entries;
+       }
+
+       my $groups = $rpcenv->filter_groups($authuser, $privs, 1);
+       my $allowed_users = $rpcenv->group_member_join([keys %$groups]);
+       return [
+           grep {
+               my $userid = $_->{userid};
+               $userid eq $authuser || $allowed_users->{$userid}
+           } $entries->@*
+       ];
     }});
 
 __PACKAGE__->register_method ({
@@ -430,12 +349,13 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       PVE::AccessControl::assert_new_tfa_config_available();
-
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
-       my ($userid, $realm) =
-           root_permission_check($rpcenv, $authuser, $param->{userid}, $param->{password});
+       my ($userid, undef, $realm) = $rpcenv->reauth_user_for_user_modification(
+           $authuser,
+           $param->{userid},
+           $param->{password},
+       );
 
        my $type = delete $param->{type};
        my $value = delete $param->{value};
@@ -465,7 +385,7 @@ __PACKAGE__->register_method ({
        });
     }});
 
-sub validate_yubico_otp : prototype($$) {
+sub validate_yubico_otp : prototype($$$) {
     my ($userid, $realm, $value) = @_;
 
     my $domain_cfg = cfs_read_file('domains.cfg');
@@ -525,12 +445,13 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       PVE::AccessControl::assert_new_tfa_config_available();
-
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
-       my $userid =
-           root_permission_check($rpcenv, $authuser, $param->{userid}, $param->{password});
+       my $userid = $rpcenv->reauth_user_for_user_modification(
+           $authuser,
+           $param->{userid},
+           $param->{password},
+       );
 
        PVE::AccessControl::lock_tfa_config(sub {
            my $tfa_cfg = cfs_read_file('priv/tfa.cfg');