X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=PVE%2FRPCEnvironment.pm;h=30415ebe4f3ca85d99652426a913f0ad59b831c0;hb=5ae5900d26ffa7e158d4847ebeb537714fb9439d;hp=a4ea2ef7ec7432725a38c0d830dbdd64fbf07b00;hpb=09d270580b09668432292e959dff2b4afb70bf74;p=pve-access-control.git diff --git a/PVE/RPCEnvironment.pm b/PVE/RPCEnvironment.pm index a4ea2ef..30415eb 100644 --- a/PVE/RPCEnvironment.pm +++ b/PVE/RPCEnvironment.pm @@ -14,8 +14,6 @@ use PVE::INotify; use PVE::Cluster; use PVE::ProcFSTools; use PVE::AccessControl; -use Cwd 'abs_path'; -use CGI; # we use this singleton class to pass RPC related environment values @@ -30,6 +28,7 @@ my $pve_env; # that (also see perlipc) my $WORKER_PIDS; +my $WORKER_FLAG = 0; my $log_task_result = sub { my ($upid, $user, $status) = @_; @@ -288,24 +287,26 @@ sub check_volume_access { # test if we have read access to volid - my $path; my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1); if ($sid) { - my ($ownervm, $vtype); - ($path, $ownervm, $vtype) = PVE::Storage::path($storecfg, $volid); + my ($vtype, undef, $ownervm) = PVE::Storage::parse_volname($storecfg, $volid); if ($vtype eq 'iso' || $vtype eq 'vztmpl') { # we simply allow access - } elsif (!$ownervm || ($ownervm != $vmid)) { + } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) { + # we are owner - allow access + } elsif ($vtype eq 'backup' && $ownervm) { + $self->check($user, "/storage/$sid", ['Datastore.AllocateSpace']); + $self->check($user, "/vms/$ownervm", ['VM.Backup']); + } else { # allow if we are Datastore administrator $self->check($user, "/storage/$sid", ['Datastore.Allocate']); } } else { die "Only root can pass arbitrary filesystem paths." if $user ne 'root@pam'; - - $path = abs_path($volid); } - return $path; + + return undef; } sub is_group_member { @@ -424,11 +425,11 @@ sub exec_api2_perm_check { return 1; } elsif ($test eq 'userid-param') { my ($userid, undef, $realm) = PVE::AccessControl::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 !$self->check_user_exist($userid, $noerr); + return 1 if $username eq $userid; return 0 if $noerr; raise_perm_exc(); } elsif ($subtest eq 'Realm.AllocateUser') { @@ -512,57 +513,29 @@ sub init { return $self; }; -# get the singleton -sub get { +# convenience function for command line tools +sub setup_default_cli_env { + my ($username) = @_; - die "not initialized" if !$pve_env; + $username //= 'root@pam'; - return $pve_env; -} + PVE::INotify::inotify_init(); -sub parse_params { - my ($self, $enable_upload) = @_; + my $rpcenv = PVE::RPCEnvironment->init('cli'); + $rpcenv->init_request(); + $rpcenv->set_language($ENV{LANG}); + $rpcenv->set_user($username); - if ($self->{request_rec}) { - my $cgi; - if ($enable_upload) { - $cgi = CGI->new($self->{request_rec}); - } else { - # disable upload using empty upload_hook - $cgi = CGI->new($self->{request_rec}, sub {}, undef, 0); - } - $self->{cgi} = $cgi; - my $params = $cgi->Vars(); - return PVE::Tools::decode_utf8_parameters($params); - } elsif ($self->{params}) { - return $self->{params}; - } else { - die "no parameters registered"; - } + die "please run as root\n" + if ($username eq 'root@pam') && ($> != 0); } -sub get_upload_info { - my ($self, $param) = @_; - - my $cgi = $self->{cgi}; - die "CGI not initialized" if !$cgi; - - my $pd = $cgi->param($param); - die "unable to get cgi parameter info\n" if !$pd; - my $info = $cgi->uploadInfo($pd); - die "unable to get cgi upload info\n" if !$info; - - my $res = { %$info }; - - my $tmpfilename = $cgi->tmpFileName($pd); - die "unable to get cgi upload file name\n" if !$tmpfilename; - $res->{tmpfilename} = $tmpfilename; +# get the singleton +sub get { - #my $hndl = $cgi->upload($param); - #die "unable to get cgi upload handle\n" if !$hndl; - #$res->{handle} = $hndl->handle; + die "not initialized" if !$pve_env; - return $res; + return $pve_env; } # init_request - must be called before each RPC request @@ -577,11 +550,6 @@ sub init_request { foreach my $p (keys %params) { if ($p eq 'userconfig') { $userconfig = $params{$p}; - } elsif ($p eq 'request_rec') { - # pass Apache2::RequestRec - $self->{request_rec} = $params{$p}; - } elsif ($p eq 'params') { - $self->{params} = $params{$p}; } else { die "unknown parameter '$p'"; } @@ -664,6 +632,10 @@ sub get_user { return $self->{user}; } +sub is_worker { + return $WORKER_FLAG; +} + # read/update list of active workers # we move all finished tasks to the archive index, # but keep aktive and most recent task in the active file. @@ -726,7 +698,7 @@ sub active_workers { my @arlist = (); foreach my $task (@ta) { if (!$task->{saved}) { - $archive .= sprintf("$task->{upid} %08X $task->{status}\n", $task->{endtime}); + $archive .= sprintf("%s %08X %s\n", $task->{upid}, $task->{endtime}, $task->{status}); $save = 1; push @arlist, $task; $task->{saved} = 1; @@ -820,14 +792,14 @@ sub check_worker { # STDOUT,STDERR are redirected to the filename returned by upid_decode # NOTE: we simulate running in foreground if ($self->{type} eq 'cli') sub fork_worker { - my ($self, $dtype, $id, $user, $function) = @_; + my ($self, $dtype, $id, $user, $function, $background) = @_; $dtype = 'unknown' if !defined ($dtype); $id = '' if !defined ($id); $user = 'root@pve' if !defined ($user); - my $sync = $self->{type} eq 'cli' ? 1 : 0; + my $sync = ($self->{type} eq 'cli' && !$background) ? 1 : 0; local $SIG{INT} = local $SIG{QUIT} = @@ -858,6 +830,7 @@ sub fork_worker { if (!$cpid) { # child $0 = "task $upid"; + $WORKER_FLAG = 1; $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub { die "received interrupt\n"; };