1 package PVE
::HTTPServer
;
9 use PVE
::APIServer
::AnyEvent
;
10 use PVE
::Exception
qw(raise_param_exc);
12 use PVE
::RPCEnvironment
;
13 use PVE
::AccessControl
;
18 use base
('PVE::APIServer::AnyEvent');
20 use HTTP
::Status
qw(:constants);
23 my ($this, %args) = @_;
25 my $class = ref($this) || $this;
27 my $self = $class->SUPER::new
(%args);
29 $self->{rpcenv
} = PVE
::RPCEnvironment-
>init(
30 $self->{trusted_env
} ?
'priv' : 'pub', atfork
=> sub { $self-> atfork_handler
() });
35 sub verify_spice_connect_url
{
36 my ($self, $connect_str) = @_;
38 my $rpcenv = $self->{rpcenv
};
40 $rpcenv->init_request();
42 my ($vmid, $node, $port) = PVE
::AccessControl
::verify_spice_connect_url
($connect_str);
44 return ($vmid, $node, $port);
47 sub generate_csrf_prevention_token
{
50 return PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
54 my ($self, $method, $rel_uri, $ticket, $token, $peer_host) = @_;
56 my $rpcenv = $self->{rpcenv
};
58 # set environment variables
59 $rpcenv->set_user(undef);
60 $rpcenv->set_language('C');
61 $rpcenv->set_client_ip($peer_host);
63 $rpcenv->init_request();
67 # explicitly allow some calls without auth
68 if (($rel_uri eq '/access/domains' && $method eq 'GET') ||
69 ($rel_uri eq '/access/ticket' && ($method eq 'GET' || $method eq 'POST'))) {
79 die "No ticket\n" if !$ticket;
81 ($username, $age) = PVE
::AccessControl
::verify_ticket
($ticket);
83 $rpcenv->set_user($username);
85 if ($method eq 'POST' && $rel_uri =~ m
|^/nodes/([^/]+)/storage
/([^/]+)/upload
$|) {
86 my ($node, $storeid) = ($1, $2);
87 # we disable CSRF checks if $isUpload is set,
88 # to improve security we check user upload permission here
89 my $perm = { check
=> ['perm', "/storage/$storeid", ['Datastore.AllocateTemplate']] };
90 $rpcenv->check_api2_permissions($perm, $username, {});
94 # we skip CSRF check for file upload, because it is
95 # difficult to pass CSRF HTTP headers with native html forms,
96 # and it should not be necessary at all.
98 PVE
::AccessControl
::verify_csrf_prevention_token
($username, $token)
99 if !$isUpload && ($euid != 0) && ($method ne 'GET');
107 isUpload
=> $isUpload,
112 my ($self, $clientip, $method, $rel_uri, $auth, $params) = @_;
114 my $rpcenv = $self->{rpcenv
};
117 status
=> HTTP_NOT_IMPLEMENTED
,
118 message
=> "Method '$method $rel_uri' not implemented",
121 my ($handler, $info);
125 ($handler, $info) = PVE
::API2-
>find_handler($method, $rel_uri, $uri_param);
126 return if !$handler || !$info;
128 foreach my $p (keys %{$params}) {
129 if (defined($uri_param->{$p})) {
130 raise_param_exc
({$p => "duplicate parameter (already defined in URI)"});
132 $uri_param->{$p} = $params->{$p};
135 # check access permissions
136 $rpcenv->check_api2_permissions($info->{permissions
}, $auth->{userid
}, $uri_param);
138 if ($info->{proxyto
}) {
139 my $pn = $info->{proxyto
};
140 my $node = $uri_param->{$pn};
142 raise_param_exc
({$pn => "proxy parameter '$pn' does not exists"}) if !$node;
144 if ($node ne 'localhost' && $node ne PVE
::INotify
::nodename
()) {
145 die "unable to proxy file uploads" if $auth->{isUpload
};
146 my $remip = $self->remote_node_ip($node);
147 $resp = { proxy
=> $remip, proxynode
=> $node, proxy_params
=> $params };
153 if ($info->{protected
} && ($euid != 0)) {
154 $resp = { proxy
=> 'localhost' , proxy_params
=> $params };
159 data
=> $handler->handle($info, $uri_param),
160 info
=> $info, # useful to format output
164 if (my $count = $rpcenv->get_result_attrib('total')) {
165 $resp->{total
} = $count;
168 if (my $diff = $rpcenv->get_result_attrib('changes')) {
169 $resp->{changes
} = $diff;
174 $rpcenv->set_user(undef); # clear after request
177 $resp = { info
=> $info };
178 if (ref($err) eq "PVE::Exception") {
179 $resp->{status
} = $err->{code
} || HTTP_INTERNAL_SERVER_ERROR
;
180 $resp->{errors
} = $err->{errors
} if $err->{errors
};
181 $resp->{message
} = $err->{msg
};
183 $resp->{status
} = HTTP_INTERNAL_SERVER_ERROR
;
184 $resp->{message
} = $err;
191 sub check_cert_fingerprint
{
192 my ($self, $cert) = @_;
194 return PVE
::Cluster
::check_cert_fingerprint
($cert);
197 sub initialize_cert_cache
{
198 my ($self, $node) = @_;
200 PVE
::Cluster
::initialize_cert_cache
($node);
204 my ($self, $node) = @_;
206 my $remip = PVE
::Cluster
::remote_node_ip
($node);
208 die "unable to get remote IP address for node '$node'\n" if !$remip;