+ return PVE::AccessControl::check_user_enabled($cfg, $user, $noerr);
+}
+
+sub check_user_exist {
+ my ($self, $user, $noerr) = @_;
+
+ my $cfg = $self->{user_cfg};
+ return PVE::AccessControl::check_user_exist($cfg, $user, $noerr);
+}
+
+sub is_group_member {
+ my ($self, $group, $user) = @_;
+
+ my $cfg = $self->{user_cfg};
+
+ return 0 if !$cfg->{groups}->{$group};
+
+ return defined($cfg->{groups}->{$group}->{users}->{$user});
+}
+
+sub filter_groups {
+ my ($self, $user, $privs, $any) = @_;
+
+ my $cfg = $self->{user_cfg};
+
+ my $groups = {};
+ foreach my $group (keys %{$cfg->{groups}}) {
+ my $path = "/access/groups/$group";
+ if ($any) {
+ if ($self->check_any($user, $path, $privs, 1)) {
+ $groups->{$group} = $cfg->{groups}->{$group};
+ }
+ } else {
+ if ($self->check($user, $path, $privs, 1)) {
+ $groups->{$group} = $cfg->{groups}->{$group};
+ }
+ }
+ }
+
+ return $groups;
+}
+
+sub group_member_join {
+ my ($self, $grouplist) = @_;
+
+ my $users = {};
+
+ my $cfg = $self->{user_cfg};
+ foreach my $group (@$grouplist) {
+ my $data = $cfg->{groups}->{$group};
+ next if !$data;
+ foreach my $user (keys %{$data->{users}}) {
+ $users->{$user} = 1;
+ }
+ }
+
+ return $users;
+}
+
+sub check_perm_modify {
+ my ($self, $username, $path, $noerr) = @_;
+
+ return $self->check($username, '/access', [ 'Permissions.Modify' ], $noerr) if !$path;
+
+ my $testperms = [ 'Permissions.Modify' ];
+ if ($path =~ m|^/storage/.+$|) {
+ push @$testperms, 'Datastore.Allocate';
+ } elsif ($path =~ m|^/vms/.+$|) {
+ push @$testperms, 'VM.Allocate';
+ }
+
+ return $self->check_any($username, $path, $testperms, $noerr);
+}
+
+sub exec_api2_perm_check {
+ my ($self, $check, $username, $param, $noerr) = @_;
+
+ # syslog("info", "CHECK " . join(', ', @$check));
+
+ my $ind = 0;
+ my $test = $check->[$ind++];
+ die "no permission test specified" if !$test;
+
+ if ($test eq 'and') {
+ while (my $subcheck = $check->[$ind++]) {
+ $self->exec_api2_perm_check($subcheck, $username, $param);
+ }
+ return 1;
+ } elsif ($test eq 'or') {
+ while (my $subcheck = $check->[$ind++]) {
+ return 1 if $self->exec_api2_perm_check($subcheck, $username, $param, 1);
+ }
+ return 0 if $noerr;
+ raise_perm_exc();
+ } elsif ($test eq 'perm') {
+ my ($t, $tmplpath, $privs, %options) = @$check;
+ my $any = $options{any};
+ die "missing parameters" if !($tmplpath && $privs);
+ my $path = PVE::Tools::template_replace($tmplpath, $param);
+ $path = PVE::AccessControl::normalize_path($path);
+ if ($any) {
+ return $self->check_any($username, $path, $privs, $noerr);
+ } else {
+ return $self->check($username, $path, $privs, $noerr);
+ }
+ } elsif ($test eq 'userid-group') {
+ my $userid = $param->{userid};
+ my ($t, $privs, %options) = @$check;
+ return 0 if !$options{groups_param} && !$self->check_user_exist($userid, $noerr);
+ if (!$self->check_any($username, "/access/groups", $privs, 1)) {
+ my $groups = $self->filter_groups($username, $privs, 1);
+ if ($options{groups_param}) {
+ my @group_param = PVE::Tools::split_list($param->{groups});
+ raise_perm_exc("/access/groups, " . join("|", @$privs)) if !scalar(@group_param);
+ foreach my $pg (@group_param) {
+ raise_perm_exc("/access/groups/$pg, " . join("|", @$privs))
+ if !$groups->{$pg};
+ }
+ } else {
+ my $allowed_users = $self->group_member_join([keys %$groups]);
+ if (!$allowed_users->{$userid}) {
+ return 0 if $noerr;
+ raise_perm_exc();
+ }
+ }
+ }
+ return 1;
+ } elsif ($test eq 'userid-param') {
+ my ($userid, undef, $realm) = verify_username($param->{userid});
+ return if !$self->check_user_exist($userid, $noerr);
+ my ($t, $subtest) = @$check;
+ die "missing parameters" if !$subtest;
+ if ($subtest eq 'self') {
+ return 1 if $username eq 'userid';
+ return 0 if $noerr;
+ raise_perm_exc();
+ } elsif ($subtest eq 'Realm.AllocateUser') {
+ my $path = "/access/realm/$realm";
+ return $self->check($username, $path, ['Realm.AllocateUser'], $noerr);
+ return 0 if $noerr;
+ raise_perm_exc("$path, 'Realm.AllocateUser'");
+ } else {
+ die "unknown userid-param test";
+ }
+ } elsif ($test eq 'perm-modify') {
+ my ($t, $tmplpath) = @$check;
+ my $path = PVE::Tools::template_replace($tmplpath, $param);
+ $path = PVE::AccessControl::normalize_path($path);
+ return $self->check_perm_modify($username, $path, $noerr);
+ } else {
+ die "unknown permission test";
+ }
+};
+
+sub check_api2_permissions {
+ my ($self, $perm, $username, $param) = @_;
+
+ return 1 if !$username && $perm->{user} eq 'world';
+
+ raise_perm_exc("user != null") if !$username;
+
+ return 1 if $username eq 'root@pam';
+
+ raise_perm_exc('user != root@pam') if !$perm;
+
+ return 1 if $perm->{user} && $perm->{user} eq 'all';
+
+ return $self->exec_api2_perm_check($perm->{check}, $username, $param)
+ if $perm->{check};
+
+ raise_perm_exc();