]> git.proxmox.com Git - pve-access-control.git/blobdiff - PVE/RPCEnvironment.pm
fix NoAccess when inheritred from pool
[pve-access-control.git] / PVE / RPCEnvironment.pm
index 19276badcadd46c95c24935186493f65a9612adb..0df71dca594099544d2cdc5961c31831927947ff 100644 (file)
@@ -7,6 +7,7 @@ use IO::Handle;
 use IO::File;
 use IO::Select;
 use Fcntl qw(:flock);
+use PVE::Exception qw(raise raise_perm_exc);
 use PVE::SafeSyslog;
 use PVE::Tools;
 use PVE::INotify;
@@ -89,80 +90,150 @@ my $register_worker = sub {
 
 # 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};
+
+    if (!$data->{poolroles}) {
+       $data->{poolroles} = {}; 
+       foreach my $poolpath (keys %{$cfg->{pools}}) {
+           my $d = $cfg->{pools}->{$poolpath};
+           my @ra = PVE::AccessControl::roles($cfg, $user, "/pool$poolpath"); # 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);
 
-       my $privs = {};
-       foreach my $role (@ra) {
-           if (my $privset = $cfg->{roles}->{$role}) {
-               foreach my $p (keys %$privset) {
-                   $privs->{$p} = 1;
+    # 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;
                }
            }
        }
+    }
 
-       $res->{$path} = $privs;
+    $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 $res;
+    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 = $acl->{privs}->{$path};
+    return $perm if $perm;
 
-    my $perm;
+    return &$compile_acl_path($self, $user, $path);
+}
 
-    if (!($perm = $acl->{$path})) {
-       $perm = {};
-       foreach my $p (sort keys %$acl) {
-           my $final = ($path eq $p);
-           
-           next if !(($p eq '/') || $final || ($path =~ m|^$p/|));
+sub check {
+    my ($self, $user, $path, $privs, $noerr) = @_;
 
-           $perm = $acl->{$p};
+    my $perm = $self->permissions($user, $path);
+
+    foreach my $priv (@$privs) {
+       PVE::AccessControl::verify_privname($priv);
+       if (!$perm->{$priv}) {
+           return undef if $noerr;
+           raise_perm_exc("$path, $priv"); 
        }
-       $acl->{$path} = $perm;
-    }
+    };
 
-    return $perm;
-}
+    return 1;
+};
 
-sub check {
-    my ($self, $user, $path, $privs) = @_;
+sub check_any {
+    my ($self, $user, $path, $privs, $noerr) = @_;
 
     my $perm = $self->permissions($user, $path);
 
+    my $found = 0;
     foreach my $priv (@$privs) {
-       return undef if !$perm->{$priv};
+       PVE::AccessControl::verify_privname($priv);
+       if ($perm->{$priv}) {
+           $found = 1;
+           last;
+       }
     };
 
-    return 1;
+    return 1 if $found;
+
+    return undef if $noerr;
+
+    raise_perm_exc("$path, " . join("|", @$privs)); 
 };
 
 sub check_user_enabled {
@@ -172,6 +243,150 @@ sub check_user_enabled {
     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 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);
+       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 if !$options{groups_param} && !$self->check_user_exist($userid, $noerr);
+       if (!$self->check_any($username, "/access", $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);
+               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 = $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();
+       } else {
+           die "unknown userid-param test";
+       }
+    } 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();
+}
+
 # initialize environment - must be called once at program startup
 sub init {
     my ($class, $type, %params) = @_;
@@ -239,7 +454,7 @@ sub parse_params {
        }
        $self->{cgi} = $cgi;
        my $params = $cgi->Vars();
-       return $params;
+       return PVE::Tools::decode_utf8_parameters($params);
     } elsif ($self->{params}) {
        return $self->{params};
     } else {
@@ -299,6 +514,7 @@ sub init_request {
            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}) ||