]> git.proxmox.com Git - pve-access-control.git/commitdiff
set/remove 'x' for tfa keys in user.cfg in new api
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 9 Nov 2021 11:27:05 +0000 (12:27 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 10 Nov 2021 10:13:21 +0000 (11:13 +0100)
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/PVE/API2/TFA.pm

index 2fbc7a8483bf7c452f9d4121864fb401d9492b3a..53f57fcafc146bf13a747e26b675b8134e28783d 100644 (file)
@@ -119,6 +119,22 @@ my sub root_permission_check : prototype($$$$) {
     return wantarray ? ($userid, $realm) : $userid;
 }
 
+my sub set_user_tfa_enabled : prototype($$) {
+    my ($userid, $enabled) = @_;
+
+    PVE::AccessControl::lock_user_config(sub {
+       my $user_cfg = cfs_read_file('user.cfg');
+       my $user = $user_cfg->{users}->{$userid};
+       my $keys = $user->{keys};
+       if ($keys && $keys !~ /^x(?:!.*)?$/) {
+           die "user contains tfa keys directly in user.cfg,"
+               ." please remove them and add them via the TFA panel instead\n";
+       }
+       $user->{keys} = $enabled ? 'x' : undef;
+       cfs_write_file("user.cfg", $user_cfg);
+    }, "enabling TFA for the user failed");
+}
+
 ### OLD API
 
 __PACKAGE__->register_method({
@@ -291,11 +307,15 @@ __PACKAGE__->register_method ({
        my $userid =
            root_permission_check($rpcenv, $authuser, $param->{userid}, $param->{password});
 
-       return PVE::AccessControl::lock_tfa_config(sub {
+       my $has_entries_left = PVE::AccessControl::lock_tfa_config(sub {
            my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
-           $tfa_cfg->api_delete_tfa($userid, $param->{id});
+           my $has_entries_left = $tfa_cfg->api_delete_tfa($userid, $param->{id});
            cfs_write_file('priv/tfa.cfg', $tfa_cfg);
+           return $has_entries_left;
        });
+       if (!$has_entries_left) {
+           set_user_tfa_enabled($userid, 0);
+       }
     }});
 
 __PACKAGE__->register_method ({
@@ -404,6 +424,8 @@ __PACKAGE__->register_method ({
            $value = validate_yubico_otp($userid, $realm, $value);
        }
 
+       set_user_tfa_enabled($userid, 1);
+
        return PVE::AccessControl::lock_tfa_config(sub {
            my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
            PVE::AccessControl::configure_u2f_and_wa($tfa_cfg);