]> git.proxmox.com Git - pve-access-control.git/blobdiff - PVE/AccessControl.pm
api/users: catch existing user also on case insensitive realm
[pve-access-control.git] / PVE / AccessControl.pm
index 6a85c1a20b21722bf6cad45a231721919d807a19..8b5be1e03c570eab4c381c75ccac3111ba19ffdd 100644 (file)
@@ -891,6 +891,28 @@ sub add_role_privs {
     }
 }
 
+sub lookup_username {
+    my ($username, $noerr) = @_;
+
+    $username =~ m!^(${PVE::Auth::Plugin::user_regex})\@(${PVE::Auth::Plugin::realm_regex})$!;
+
+    my $realm = $2;
+    my $domain_cfg = cfs_read_file("domains.cfg");
+    my $casesensitive = $domain_cfg->{ids}->{$realm}->{'case-sensitive'} // 1;
+    my $usercfg = cfs_read_file('user.cfg');
+
+    if (!$casesensitive) {
+       my @matches = grep { lc $username eq lc $_ } (keys %{$usercfg->{users}});
+
+       die "ambiguous case insensitive match of username '$username', cannot safely grant access!\n"
+           if scalar @matches > 1 && !$noerr;
+
+       return $matches[0]
+    }
+
+    return $username;
+}
+
 sub normalize_path {
     my $path = shift;