1 package PVE
::HTTPServer
;
9 use PVE
::APIServer
::AnyEvent
;
10 use PVE
::Exception
qw(raise_param_exc raise_perm_exc raise);
12 use PVE
::RPCEnvironment
;
13 use PVE
::AccessControl
;
20 use base
('PVE::APIServer::AnyEvent');
22 use HTTP
::Status
qw(:constants);
25 my ($this, %args) = @_;
27 my $class = ref($this) || $this;
29 my $self = $class->SUPER::new
(%args);
31 $self->{rpcenv
} = PVE
::RPCEnvironment-
>init(
32 $self->{trusted_env
} ?
'priv' : 'pub', atfork
=> sub { $self-> atfork_handler
() });
37 sub verify_spice_connect_url
{
38 my ($self, $connect_str) = @_;
40 my $rpcenv = $self->{rpcenv
};
42 $rpcenv->init_request();
44 my ($vmid, $node, $port) = PVE
::AccessControl
::verify_spice_connect_url
($connect_str);
46 return ($vmid, $node, $port);
49 sub generate_csrf_prevention_token
{
52 return PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
56 my ($self, $method, $rel_uri, $ticket, $token, $api_token, $peer_host) = @_;
58 my $rpcenv = $self->{rpcenv
};
60 # set environment variables
61 $rpcenv->set_user(undef);
62 $rpcenv->set_language('C');
63 $rpcenv->set_client_ip($peer_host);
65 eval { $rpcenv->init_request() };
66 raise
("RPCEnvironment init request failed: $@\n") if $@;
70 # explicitly allow some calls without auth
71 if (($rel_uri eq '/access/domains' && $method eq 'GET') ||
72 ($rel_uri eq '/access/ticket' && ($method eq 'GET' || $method eq 'POST'))) {
82 $username = PVE
::AccessControl
::verify_token
($api_token);
83 $rpcenv->set_user($username); #actually tokenid in this case
85 die "No ticket\n" if !$ticket;
87 ($username, $age, my $tfa_info) = PVE
::AccessControl
::verify_ticket
($ticket);
89 if (defined($tfa_info)) {
90 if (defined(my $challenge = $tfa_info->{challenge
})) {
91 $rpcenv->set_u2f_challenge($challenge);
94 if ($rel_uri ne '/access/tfa' || $method ne 'POST');
97 $rpcenv->set_user($username);
99 if ($method eq 'POST' && $rel_uri =~ m
|^/nodes/([^/]+)/storage
/([^/]+)/upload
$|) {
100 my ($node, $storeid) = ($1, $2);
101 # we disable CSRF checks if $isUpload is set,
102 # to improve security we check user upload permission here
103 my $perm = { check
=> ['perm', "/storage/$storeid", ['Datastore.AllocateTemplate']] };
104 $rpcenv->check_api2_permissions($perm, $username, {});
108 # we skip CSRF check for file upload, because it is
109 # difficult to pass CSRF HTTP headers with native html forms,
110 # and it should not be necessary at all.
112 PVE
::AccessControl
::verify_csrf_prevention_token
($username, $token)
113 if !$isUpload && ($euid != 0) && ($method ne 'GET');
122 isUpload
=> $isUpload,
123 api_token
=> $api_token,
128 my ($self, $clientip, $method, $rel_uri, $auth, $params) = @_;
130 my $rpcenv = $self->{rpcenv
};
133 status
=> HTTP_NOT_IMPLEMENTED
,
134 message
=> "Method '$method $rel_uri' not implemented",
137 my ($handler, $info);
141 ($handler, $info) = PVE
::API2-
>find_handler($method, $rel_uri, $uri_param);
142 return if !$handler || !$info;
144 foreach my $p (keys %{$params}) {
145 if (defined($uri_param->{$p})) {
146 raise_param_exc
({$p => "duplicate parameter (already defined in URI)"});
148 $uri_param->{$p} = $params->{$p};
151 raise_perm_exc
("URI '$rel_uri' not available with API token, need proper ticket.\n")
152 if $auth->{api_token
} && !$info->{allowtoken
};
154 # check access permissions
155 $rpcenv->check_api2_permissions($info->{permissions
}, $auth->{userid
}, $uri_param);
157 if ($info->{proxyto
} || $info->{proxyto_callback
}) {
158 my $node = PVE
::API2Tools
::resolve_proxyto
(
159 $rpcenv, $info->{proxyto_callback
}, $info->{proxyto
}, $uri_param);
161 if ($node ne 'localhost' && $node ne PVE
::INotify
::nodename
()) {
162 die "unable to proxy file uploads" if $auth->{isUpload
};
163 my $remip = $self->remote_node_ip($node);
164 $resp = { proxy
=> $remip, proxynode
=> $node, proxy_params
=> $params };
170 if ($info->{protected
} && ($euid != 0)) {
171 $resp = { proxy
=> 'localhost' , proxy_params
=> $params };
176 data
=> $handler->handle($info, $uri_param),
177 info
=> $info, # useful to format output
181 if (my $count = $rpcenv->get_result_attrib('total')) {
182 $resp->{total
} = $count;
185 if (my $diff = $rpcenv->get_result_attrib('changes')) {
186 $resp->{changes
} = $diff;
191 $rpcenv->set_user(undef); # clear after request
194 $resp = { info
=> $info };
195 if (ref($err) eq "PVE::Exception") {
196 $resp->{status
} = $err->{code
} || HTTP_INTERNAL_SERVER_ERROR
;
197 $resp->{errors
} = $err->{errors
} if $err->{errors
};
198 $resp->{message
} = $err->{msg
};
200 $resp->{status
} = HTTP_INTERNAL_SERVER_ERROR
;
201 $resp->{message
} = $err;
208 sub check_cert_fingerprint
{
209 my ($self, $cert) = @_;
211 return PVE
::CertCache
::check_cert_fingerprint
($cert);
214 sub initialize_cert_cache
{
215 my ($self, $node) = @_;
217 PVE
::CertCache
::initialize_cert_cache
($node);
221 my ($self, $node) = @_;
223 my $remip = PVE
::Cluster
::remote_node_ip
($node);
225 die "unable to get remote IP address for node '$node'\n" if !$remip;