]> git.proxmox.com Git - pve-access-control.git/blobdiff - src/PVE/API2/TFA.pm
use statement fixup
[pve-access-control.git] / src / PVE / API2 / TFA.pm
index 1888699ed8b0c8cdeb27c19474187b0a535389e7..bee4dee7deba40b2dd825debe3189e42acfd3f94 100644 (file)
@@ -3,11 +3,14 @@ package PVE::API2::TFA;
 use strict;
 use warnings;
 
+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
 
@@ -99,7 +102,7 @@ my $TFA_UPDATE_INFO_SCHEMA = {
 my sub root_permission_check : prototype($$$$) {
     my ($rpcenv, $authuser, $userid, $password) = @_;
 
-    ($userid, my $ruid, my $realm) = PVE::AccessControl::verify_username($userid);
+    ($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';
@@ -109,16 +112,46 @@ my sub root_permission_check : prototype($$$$) {
        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}->{$realm};
-       die "auth domain '$realm' does not exist\n" if !$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, $realm, $ruid, $password);
+       $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($$$) {
+    my ($userid, $realm, $tfa_cfg) = @_;
+
+    PVE::AccessControl::lock_user_config(sub {
+       my $user_cfg = cfs_read_file('user.cfg');
+       my $user = $user_cfg->{users}->{$userid};
+       my $keys = $user->{keys};
+       # When enabling, we convert old-old keys,
+       # When disabling, we shouldn't actually have old keys anymore, so if they are there,
+       # they'll be removed.
+       if ($tfa_cfg && $keys && $keys !~ /^x(?:!.*)?$/) {
+           my $domain_cfg = cfs_read_file('domains.cfg');
+           my $realm_cfg = $domain_cfg->{ids}->{$realm};
+           die "auth domain '$realm' does not exist\n" if !$realm_cfg;
+
+           my $realm_tfa = $realm_cfg->{tfa};
+           $realm_tfa = PVE::Auth::Plugin::parse_tfa_config($realm_tfa) if $realm_tfa;
+
+           PVE::AccessControl::add_old_keys_to_realm_tfa($userid, $tfa_cfg, $realm_tfa, $keys);
+       }
+       $user->{keys} = $tfa_cfg ? 'x' : undef;
+       cfs_write_file("user.cfg", $user_cfg);
+    }, "enabling TFA for the user failed");
+}
+
 ### OLD API
 
 __PACKAGE__->register_method({
@@ -156,6 +189,9 @@ __PACKAGE__->register_method({
        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') {
@@ -253,7 +289,7 @@ __PACKAGE__->register_method ({
        my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
        my $id = $param->{id};
        my $entry = $tfa_cfg->api_get_tfa_entry($param->{userid}, $id);
-       raise("No such tfa entry '$id'", 404) if !$entry;
+       raise("No such tfa entry '$id'", code => HTTP::Status::HTTP_NOT_FOUND) if !$entry;
        return $entry;
     }});
 
@@ -291,11 +327,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, undef, undef);
+       }
     }});
 
 __PACKAGE__->register_method ({
@@ -335,10 +375,24 @@ __PACKAGE__->register_method ({
 
        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 ({
@@ -395,20 +449,29 @@ __PACKAGE__->register_method ({
 
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
-       my $userid =
+       my ($userid, $realm) =
            root_permission_check($rpcenv, $authuser, $param->{userid}, $param->{password});
 
+       my $type = delete $param->{type};
+       my $value = delete $param->{value};
+       if ($type eq 'yubico') {
+           $value = validate_yubico_otp($userid, $realm, $value);
+       }
+
        return PVE::AccessControl::lock_tfa_config(sub {
            my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
+
+           set_user_tfa_enabled($userid, $realm, $tfa_cfg);
+
            PVE::AccessControl::configure_u2f_and_wa($tfa_cfg);
 
            my $response = $tfa_cfg->api_add_tfa_entry(
                $userid,
                $param->{description},
                $param->{totp},
-               $param->{value},
+               $value,
                $param->{challenge},
-               $param->{type},
+               $type,
            );
 
            cfs_write_file('priv/tfa.cfg', $tfa_cfg);
@@ -417,6 +480,28 @@ __PACKAGE__->register_method ({
        });
     }});
 
+sub validate_yubico_otp : prototype($$) {
+    my ($userid, $realm, $value) = @_;
+
+    my $domain_cfg = cfs_read_file('domains.cfg');
+    my $realm_cfg = $domain_cfg->{ids}->{$realm};
+    die "auth domain '$realm' does not exist\n" if !$realm_cfg;
+
+    my $realm_tfa = $realm_cfg->{tfa};
+    die "no yubico otp configuration available for realm $realm\n"
+       if !$realm_tfa;
+
+    $realm_tfa = PVE::Auth::Plugin::parse_tfa_config($realm_tfa);
+    die "realm is not setup for Yubico OTP\n"
+       if !$realm_tfa || $realm_tfa->{type} ne 'yubico';
+
+    my $public_key = substr($value, 0, 12);
+
+    PVE::AccessControl::authenticate_yubico_do($value, $public_key, $realm_tfa);
+
+    return $public_key;
+}
+
 __PACKAGE__->register_method ({
     name => 'update_tfa_entry',
     path => '{userid}/{id}',