]> git.proxmox.com Git - pve-access-control.git/blobdiff - src/PVE/API2/User.pm
update tfa cleanup when deleting users
[pve-access-control.git] / src / PVE / API2 / User.pm
index c6065964d46c1a242e3ae1b28804d4c54916e427..244264e88e4136d52bbc27eb36e47b411eae6624 100644 (file)
@@ -10,6 +10,7 @@ use PVE::JSONSchema qw(get_standard_option register_standard_option);
 use PVE::SafeSyslog;
 
 use PVE::AccessControl;
+use PVE::Auth::Plugin;
 use PVE::TokenConfig;
 
 use PVE::RESTHandler;
@@ -197,9 +198,9 @@ __PACKAGE__->register_method ({
                ];
            }
 
-           my (undef, undef, $realm) = PVE::AccessControl::verify_username($user, 1);
-           if (defined($realm) && exists($domainids->{$realm})) {
-               $entry->{'realm-type'} = $domainids->{$realm}->{type};
+           if ($user =~ /($PVE::Auth::Plugin::realm_regex)$/) {
+               my $realm = $1;
+               $entry->{'realm-type'} = $domainids->{$realm}->{type} if exists $domainids->{$realm};
            }
 
            $entry->{userid} = $user;
@@ -217,10 +218,11 @@ __PACKAGE__->register_method ({
     method => 'POST',
     permissions => {
        description => "You need 'Realm.AllocateUser' on '/access/realm/<realm>' on the realm of user <userid>, and 'User.Modify' permissions to '/access/groups/<group>' for any group specified (or 'User.Modify' on '/access/groups' if you pass no groups.",
-       check => [ 'and',
-                  [ 'userid-param', 'Realm.AllocateUser'],
-                  [ 'userid-group', ['User.Modify'], groups_param => 1],
-           ],
+       check => [
+           'and',
+           [ 'userid-param', 'Realm.AllocateUser'],
+           [ 'userid-group', ['User.Modify'], groups_param => 1],
+       ],
     },
     description => "Create new user.",
     parameters => {
@@ -328,8 +330,7 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my ($username, undef, $domain) =
-           PVE::AccessControl::verify_username($param->{userid});
+       my ($username, undef, $domain) = PVE::AccessControl::verify_username($param->{userid});
 
        my $usercfg = cfs_read_file("user.cfg");
 
@@ -370,41 +371,37 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       my ($username, $ruid, $realm) =
-           PVE::AccessControl::verify_username($param->{userid});
-
-       PVE::AccessControl::lock_user_config(
-           sub {
-
-               my $usercfg = cfs_read_file("user.cfg");
+       my ($username, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid});
 
-               PVE::AccessControl::check_user_exist($usercfg, $username);
+       PVE::AccessControl::lock_user_config(sub {
+           my $usercfg = cfs_read_file("user.cfg");
 
-               $usercfg->{users}->{$username}->{enable} = $param->{enable} if defined($param->{enable});
+           PVE::AccessControl::check_user_exist($usercfg, $username);
 
-               $usercfg->{users}->{$username}->{expire} = $param->{expire} if defined($param->{expire});
+           $usercfg->{users}->{$username}->{enable} = $param->{enable} if defined($param->{enable});
+           $usercfg->{users}->{$username}->{expire} = $param->{expire} if defined($param->{expire});
 
-               PVE::AccessControl::delete_user_group($username, $usercfg)
-                   if (!$param->{append} && defined($param->{groups}));
+           PVE::AccessControl::delete_user_group($username, $usercfg)
+               if (!$param->{append} && defined($param->{groups}));
 
-               if ($param->{groups}) {
-                   foreach my $group (split_list($param->{groups})) {
-                       if ($usercfg->{groups}->{$group}) {
-                           PVE::AccessControl::add_user_group($username, $usercfg, $group);
-                       } else {
-                           die "no such group '$group'\n";
-                       }
+           if ($param->{groups}) {
+               foreach my $group (split_list($param->{groups})) {
+                   if ($usercfg->{groups}->{$group}) {
+                       PVE::AccessControl::add_user_group($username, $usercfg, $group);
+                   } else {
+                       die "no such group '$group'\n";
                    }
                }
+           }
 
-               $usercfg->{users}->{$username}->{firstname} = $param->{firstname} if defined($param->{firstname});
-               $usercfg->{users}->{$username}->{lastname} = $param->{lastname} if defined($param->{lastname});
-               $usercfg->{users}->{$username}->{email} = $param->{email} if defined($param->{email});
-               $usercfg->{users}->{$username}->{comment} = $param->{comment} if defined($param->{comment});
-               $usercfg->{users}->{$username}->{keys} = $param->{keys} if defined($param->{keys});
+           $usercfg->{users}->{$username}->{firstname} = $param->{firstname} if defined($param->{firstname});
+           $usercfg->{users}->{$username}->{lastname} = $param->{lastname} if defined($param->{lastname});
+           $usercfg->{users}->{$username}->{email} = $param->{email} if defined($param->{email});
+           $usercfg->{users}->{$username}->{comment} = $param->{comment} if defined($param->{comment});
+           $usercfg->{users}->{$username}->{keys} = $param->{keys} if defined($param->{keys});
 
-               cfs_write_file("user.cfg", $usercfg);
-           }, "update user failed");
+           cfs_write_file("user.cfg", $usercfg);
+       }, "update user failed");
 
        return undef;
     }});
@@ -417,9 +414,9 @@ __PACKAGE__->register_method ({
     description => "Delete user.",
     permissions => {
        check => [ 'and',
-                  [ 'userid-param', 'Realm.AllocateUser'],
-                  [ 'userid-group', ['User.Modify']],
-           ],
+           [ 'userid-param', 'Realm.AllocateUser'],
+           [ 'userid-group', ['User.Modify']],
+       ],
     },
     parameters => {
        additionalProperties => 0,
@@ -434,30 +431,40 @@ __PACKAGE__->register_method ({
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
 
-       my ($userid, $ruid, $realm) =
-           PVE::AccessControl::verify_username($param->{userid});
+       my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid});
 
-       PVE::AccessControl::lock_user_config(
-           sub {
+       PVE::AccessControl::lock_user_config(sub {
+           my $usercfg = cfs_read_file("user.cfg");
 
-               my $usercfg = cfs_read_file("user.cfg");
+           # NOTE: disable the user first (transaction like), so if (e.g.) we fail in the middle of
+           # TFA deletion the user will be still disabled and not just without TFA protection.
+           $usercfg->{users}->{$userid}->{enable} = 0;
+           cfs_write_file("user.cfg", $usercfg);
 
-               my $domain_cfg = cfs_read_file('domains.cfg');
-               if (my $cfg = $domain_cfg->{ids}->{$realm}) {
-                   my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
-                   $plugin->delete_user($cfg, $realm, $ruid);
-               }
+           my $domain_cfg = cfs_read_file('domains.cfg');
+           if (my $cfg = $domain_cfg->{ids}->{$realm}) {
+               my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
+               $plugin->delete_user($cfg, $realm, $ruid);
+           }
 
-               # Remove TFA data before removing the user entry as the user entry tells us whether
-               # we need ot update priv/tfa.cfg.
-               PVE::AccessControl::user_set_tfa($userid, $realm, undef, undef, $usercfg, $domain_cfg);
+           # Remove user from cache before removing the TFA entry so realms with TFA-enforcement
+           # know that it's OK to drop any TFA entry in that case.
+           delete $usercfg->{users}->{$userid};
 
-               delete $usercfg->{users}->{$userid};
+           my $partial_deletion = '';
+           eval {
+               PVE::AccessControl::user_remove_tfa($userid);
+               $partial_deletion = ' - but deleted related TFA';
 
                PVE::AccessControl::delete_user_group($userid, $usercfg);
+               $partial_deletion .= ', Groups';
                PVE::AccessControl::delete_user_acl($userid, $usercfg);
+               $partial_deletion .= ', ACLs';
+
                cfs_write_file("user.cfg", $usercfg);
-           }, "delete user failed");
+           };
+           die "$@$partial_deletion\n" if $@;
+       }, "delete user failed");
 
        return undef;
     }});
@@ -478,6 +485,12 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => {
            userid => get_standard_option('userid-completed'),
+           multiple => {
+               type => 'boolean',
+               description => 'Request all entries as an array.',
+               optional => 1,
+               default => 0,
+           },
        },
     },
     returns => {
@@ -492,9 +505,23 @@ __PACKAGE__->register_method ({
            user => {
                type => 'string',
                enum => [qw(oath u2f)],
-               description => "The type of TFA the user has set, if any.",
+               description =>
+                   "The type of TFA the user has set, if any."
+                   . " Only set if 'multiple' was not passed.",
                optional => 1,
            },
+           types => {
+               type => 'array',
+               description =>
+                   "Array of the user configured TFA types, if any."
+                   . " Only available if 'multiple' was not passed.",
+               optional => 1,
+               items => {
+                   type => 'string',
+                   enum => [qw(totp u2f yubico webauthn recovedry)],
+                   description => 'A TFA type.',
+               },
+           },
        },
        type => "object"
     },
@@ -503,21 +530,28 @@ __PACKAGE__->register_method ({
 
        my ($username, undef, $realm) = PVE::AccessControl::verify_username($param->{userid});
 
-
        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 $res = {};
        my $realm_tfa = {};
-       $realm_tfa = PVE::Auth::Plugin::parse_tfa_config($realm_cfg->{tfa})
-           if $realm_cfg->{tfa};
+       $realm_tfa = PVE::Auth::Plugin::parse_tfa_config($realm_cfg->{tfa}) if $realm_cfg->{tfa};
+       $res->{realm} = $realm_tfa->{type} if $realm_tfa->{type};
 
        my $tfa_cfg = cfs_read_file('priv/tfa.cfg');
-       my $tfa = $tfa_cfg->{users}->{$username};
-
-       my $res = {};
-       $res->{realm} = $realm_tfa->{type} if $realm_tfa->{type};
-       $res->{user} = $tfa->{type} if $tfa->{type};
+       if ($param->{multiple}) {
+           my $tfa = $tfa_cfg->get_user($username);
+           my $user = [];
+           foreach my $type (keys %$tfa) {
+               next if !scalar($tfa->{$type}->@*);
+               push @$user, $type;
+           }
+           $res->{user} = $user;
+       } else {
+           my $tfa = $tfa_cfg->{users}->{$username};
+           $res->{user} = $tfa->{type} if $tfa->{type};
+       }
        return $res;
     }});
 
@@ -527,9 +561,10 @@ __PACKAGE__->register_method ({
     method => 'GET',
     description => "Get user API tokens.",
     permissions => {
-       check => ['or',
-                   ['userid-param', 'self'],
-                   ['perm', '/access/users/{userid}', ['User.Modify']],
+       check => [
+           'or',
+           ['userid-param', 'self'],
+           ['perm', '/access/users/{userid}', ['User.Modify']],
        ],
     },
     parameters => {
@@ -563,9 +598,10 @@ __PACKAGE__->register_method ({
     method => 'GET',
     description => "Get specific API token information.",
     permissions => {
-       check => ['or',
-                   ['userid-param', 'self'],
-                   ['perm', '/access/users/{userid}', ['User.Modify']],
+       check => [
+           'or',
+           ['userid-param', 'self'],
+           ['perm', '/access/users/{userid}', ['User.Modify']],
        ],
     },
     parameters => {
@@ -594,9 +630,10 @@ __PACKAGE__->register_method ({
     description => "Generate a new API token for a specific user. NOTE: returns API token value, which needs to be stored as it cannot be retrieved afterwards!",
     protected => 1,
     permissions => {
-       check => ['or',
-                   ['userid-param', 'self'],
-                   ['perm', '/access/users/{userid}', ['User.Modify']],
+       check => [
+           'or',
+           ['userid-param', 'self'],
+           ['perm', '/access/users/{userid}', ['User.Modify']],
        ],
     },
     parameters => {
@@ -673,9 +710,10 @@ __PACKAGE__->register_method ({
     description => "Update API token for a specific user.",
     protected => 1,
     permissions => {
-       check => ['or',
-                   ['userid-param', 'self'],
-                   ['perm', '/access/users/{userid}', ['User.Modify']],
+       check => [
+           'or',
+           ['userid-param', 'self'],
+           ['perm', '/access/users/{userid}', ['User.Modify']],
        ],
     },
     parameters => {
@@ -698,7 +736,7 @@ __PACKAGE__->register_method ({
        my $usercfg = cfs_read_file("user.cfg");
        my $token = PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid);
 
-       my $update_token = sub {
+       PVE::AccessControl::lock_user_config(sub {
            $usercfg = cfs_read_file("user.cfg");
            $token = PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid);
 
@@ -710,9 +748,7 @@ __PACKAGE__->register_method ({
 
            $usercfg->{users}->{$userid}->{tokens}->{$tokenid} = $token;
            cfs_write_file("user.cfg", $usercfg);
-       };
-
-       PVE::AccessControl::lock_user_config($update_token, 'updating token info failed');
+       }, 'updating token info failed');
 
        return $token;
     }});
@@ -725,9 +761,10 @@ __PACKAGE__->register_method ({
     description => "Remove API token for a specific user.",
     protected => 1,
     permissions => {
-       check => ['or',
-                   ['userid-param', 'self'],
-                   ['perm', '/access/users/{userid}', ['User.Modify']],
+       check => [
+           'or',
+           ['userid-param', 'self'],
+           ['perm', '/access/users/{userid}', ['User.Modify']],
        ],
     },
     parameters => {
@@ -747,7 +784,7 @@ __PACKAGE__->register_method ({
        my $usercfg = cfs_read_file("user.cfg");
        my $token = PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid);
 
-       my $update_token = sub {
+       PVE::AccessControl::lock_user_config(sub {
            $usercfg = cfs_read_file("user.cfg");
 
            PVE::AccessControl::check_token_exist($usercfg, $userid, $tokenid);
@@ -757,9 +794,7 @@ __PACKAGE__->register_method ({
            delete $usercfg->{users}->{$userid}->{tokens}->{$tokenid};
 
            cfs_write_file("user.cfg", $usercfg);
-       };
-
-       PVE::AccessControl::lock_user_config($update_token, 'deleting token failed');
+       }, 'deleting token failed');
 
        return;
     }});