# ACL cache
-my $compile_acl = sub {
- my ($self, $user) = @_;
+my $compile_acl_path = sub {
+ my ($self, $user, $path) = @_;
- my $res = {};
my $cfg = $self->{user_cfg};
return undef if !$cfg->{roles};
- if ($user eq 'root@pam') { # root can do anything
- return {'/' => $cfg->{roles}->{'Administrator'}};
- }
+ die "internal error" if $user eq 'root@pam';
- foreach my $path (sort keys %{$cfg->{acl}}) {
- my @ra = PVE::AccessControl::roles($cfg, $user, $path);
+ my $cache = $self->{aclcache};
+ $cache->{$user} = {} if !$cache->{$user};
+ my $data = $cache->{$user};
- my $privs = {};
- foreach my $role (@ra) {
- if (my $privset = $cfg->{roles}->{$role}) {
- foreach my $p (keys %$privset) {
- $privs->{$p} = 1;
+ if (!$data->{poolroles}) {
+ $data->{poolroles} = {};
+
+ foreach my $pool (keys %{$cfg->{pools}}) {
+ my $d = $cfg->{pools}->{$pool};
+ my @ra = PVE::AccessControl::roles($cfg, $user, "/pool/$pool"); # pool roles
+ next if !scalar(@ra);
+ foreach my $vmid (keys %{$d->{vms}}) {
+ for my $role (@ra) {
+ $data->{poolroles}->{"/vms/$vmid"}->{$role} = 1;
+ }
+ }
+ foreach my $storeid (keys %{$d->{storage}}) {
+ for my $role (@ra) {
+ $data->{poolroles}->{"/storage/$storeid"}->{$role} = 1;
}
}
}
+ }
+
+ my @ra = PVE::AccessControl::roles($cfg, $user, $path);
- $res->{$path} = $privs;
+ # apply roles inherited from pools
+ # Note: assume we do not want to propagate those privs
+ if ($data->{poolroles}->{$path}) {
+ if (!($ra[0] && $ra[0] eq 'NoAccess')) {
+ if ($data->{poolroles}->{$path}->{NoAccess}) {
+ @ra = ('NoAccess');
+ } else {
+ foreach my $role (keys %{$data->{poolroles}->{$path}}) {
+ push @ra, $role;
+ }
+ }
+ }
}
- return $res;
+ $data->{roles}->{$path} = [ @ra ];
+
+ my $privs = {};
+ foreach my $role (@ra) {
+ if (my $privset = $cfg->{roles}->{$role}) {
+ foreach my $p (keys %$privset) {
+ $privs->{$p} = 1;
+ }
+ }
+ }
+ $data->{privs}->{$path} = $privs;
+
+ return $privs;
};
+sub roles {
+ my ($self, $user, $path) = @_;
+
+ if ($user eq 'root@pam') { # root can do anything
+ return ('Administrator');
+ }
+
+ $user = PVE::AccessControl::verify_username($user, 1);
+ return () if !$user;
+
+ my $cache = $self->{aclcache};
+ $cache->{$user} = {} if !$cache->{$user};
+
+ my $acl = $cache->{$user};
+
+ my $roles = $acl->{roles}->{$path};
+ return @$roles if $roles;
+
+ &$compile_acl_path($self, $user, $path);
+ $roles = $acl->{roles}->{$path} || [];
+ return @$roles;
+}
+
sub permissions {
my ($self, $user, $path) = @_;
+ if ($user eq 'root@pam') { # root can do anything
+ my $cfg = $self->{user_cfg};
+ return $cfg->{roles}->{'Administrator'};
+ }
+
$user = PVE::AccessControl::verify_username($user, 1);
return {} if !$user;
my $cache = $self->{aclcache};
+ $cache->{$user} = {} if !$cache->{$user};
my $acl = $cache->{$user};
- if (!$acl) {
- if (!($acl = &$compile_acl($self, $user))) {
- return {};
- }
- $cache->{$user} = $acl;
- }
-
- my $perm;
+ my $perm = $acl->{privs}->{$path};
+ return $perm if $perm;
- if (!($perm = $acl->{$path})) {
- $perm = {};
- foreach my $p (sort keys %$acl) {
- my $final = ($path eq $p);
-
- next if !(($p eq '/') || $final || ($path =~ m|^$p/|));
-
- $perm = $acl->{$p};
- }
- $acl->{$path} = $perm;
- }
-
- return $perm;
+ return &$compile_acl_path($self, $user, $path);
}
sub check {
raise_perm_exc("$path, " . join("|", @$privs));
};
+sub check_full {
+ my ($self, $username, $path, $privs, $any, $noerr) = @_;
+ if ($any) {
+ return $self->check_any($username, $path, $privs, $noerr);
+ } else {
+ return $self->check($username, $path, $privs, $noerr);
+ }
+}
+
sub check_user_enabled {
my ($self, $user, $noerr) = @_;
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};
- }
+ if ($self->check_full($user, $path, $privs, $any, 1)) {
+ $groups->{$group} = $cfg->{groups}->{$group};
}
}
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';
+ } elsif ($path =~ m|^/pool/.+$|) {
+ push @$testperms, 'Pool.Allocate';
+ }
+
+ return $self->check_any($username, $path, $testperms, $noerr);
+}
+
sub exec_api2_perm_check {
my ($self, $check, $username, $param, $noerr) = @_;
my ($t, $tmplpath, $privs, %options) = @$check;
my $any = $options{any};
die "missing parameters" if !($tmplpath && $privs);
- my $path = PVE::Tools::template_replace($tmplpath, $param);
- if ($any) {
- return $self->check_any($username, $path, $privs, $noerr);
- } else {
- return $self->check($username, $path, $privs, $noerr);
+ my $require_param = $options{require_param};
+ if ($require_param && !defined($param->{$require_param})) {
+ return 0 if $noerr;
+ raise_perm_exc();
}
+ my $path = PVE::Tools::template_replace($tmplpath, $param);
+ $path = PVE::AccessControl::normalize_path($path);
+ return $self->check_full($username, $path, $privs, $any, $noerr);
} elsif ($test eq 'userid-group') {
my $userid = $param->{userid};
my ($t, $privs, %options) = @$check;
- return if !$options{groups_param} && !$self->check_user_exist($userid, $noerr);
- if (!$self->check_any($username, "/access", $privs, 1)) {
+ 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, " . join("|", @$privs)) if !scalar(@group_param);
+ 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};
}
return 1;
} elsif ($test eq 'userid-param') {
- my $userid = $param->{userid};
+ 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;
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);
} else {
die "unknown userid-param test";
}
- } else {
+ } 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";
}
};
my $ucdata = PVE::Tools::file_get_contents($userconfig);
my $cfg = PVE::AccessControl::parse_user_config($userconfig, $ucdata);
$self->{user_cfg} = $cfg;
+ #print Dumper($cfg);
} else {
my $ucvers = PVE::Cluster::cfs_file_version('user.cfg');
if (!$self->{aclcache} || !defined($self->{aclversion}) ||