[pve-devel] [PATCH access-control 10/10] set/remove 'x' for tfa keys in user.cfg in new api

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Nov 9 12:27:05 CET 2021


Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 src/PVE/API2/TFA.pm | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/src/PVE/API2/TFA.pm b/src/PVE/API2/TFA.pm
index 2fbc7a8..53f57fc 100644
--- a/src/PVE/API2/TFA.pm
+++ b/src/PVE/API2/TFA.pm
@@ -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);
-- 
2.30.2






More information about the pve-devel mailing list