]> git.proxmox.com Git - pve-manager.git/blob - PVE/HTTPServer.pm
update shipped appliance info index
[pve-manager.git] / PVE / HTTPServer.pm
1 package PVE::HTTPServer;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::INotify;
8 use PVE::Tools;
9 use PVE::APIServer::AnyEvent;
10 use PVE::Exception qw(raise_param_exc raise);
11
12 use PVE::RPCEnvironment;
13 use PVE::AccessControl;
14 use PVE::Cluster;
15 use PVE::API2Tools;
16
17 use Data::Dumper;
18
19 use base('PVE::APIServer::AnyEvent');
20
21 use HTTP::Status qw(:constants);
22
23 sub new {
24 my ($this, %args) = @_;
25
26 my $class = ref($this) || $this;
27
28 my $self = $class->SUPER::new(%args);
29
30 $self->{rpcenv} = PVE::RPCEnvironment->init(
31 $self->{trusted_env} ? 'priv' : 'pub', atfork => sub { $self-> atfork_handler() });
32
33 return $self;
34 }
35
36 sub verify_spice_connect_url {
37 my ($self, $connect_str) = @_;
38
39 my $rpcenv = $self->{rpcenv};
40
41 $rpcenv->init_request();
42
43 my ($vmid, $node, $port) = PVE::AccessControl::verify_spice_connect_url($connect_str);
44
45 return ($vmid, $node, $port);
46 }
47
48 sub generate_csrf_prevention_token {
49 my ($username) = @_;
50
51 return PVE::AccessControl::assemble_csrf_prevention_token($username);
52 }
53
54 sub auth_handler {
55 my ($self, $method, $rel_uri, $ticket, $token, $peer_host) = @_;
56
57 my $rpcenv = $self->{rpcenv};
58
59 # set environment variables
60 $rpcenv->set_user(undef);
61 $rpcenv->set_language('C');
62 $rpcenv->set_client_ip($peer_host);
63
64 eval { $rpcenv->init_request() };
65 raise("RPCEnvironment init request failed: $@\n") if $@;
66
67 my $require_auth = 1;
68
69 # explicitly allow some calls without auth
70 if (($rel_uri eq '/access/domains' && $method eq 'GET') ||
71 ($rel_uri eq '/access/ticket' && ($method eq 'GET' || $method eq 'POST'))) {
72 $require_auth = 0;
73 }
74
75 my ($username, $age);
76
77 my $isUpload = 0;
78
79 if ($require_auth) {
80
81 die "No ticket\n" if !$ticket;
82
83 ($username, $age, my $tfa_info) = PVE::AccessControl::verify_ticket($ticket);
84
85 if (defined($tfa_info)) {
86 if (defined(my $challenge = $tfa_info->{challenge})) {
87 $rpcenv->set_u2f_challenge($challenge);
88 }
89 die "No ticket\n"
90 if ($rel_uri ne '/access/tfa' || $method ne 'POST');
91 }
92
93 $rpcenv->set_user($username);
94
95 if ($method eq 'POST' && $rel_uri =~ m|^/nodes/([^/]+)/storage/([^/]+)/upload$|) {
96 my ($node, $storeid) = ($1, $2);
97 # we disable CSRF checks if $isUpload is set,
98 # to improve security we check user upload permission here
99 my $perm = { check => ['perm', "/storage/$storeid", ['Datastore.AllocateTemplate']] };
100 $rpcenv->check_api2_permissions($perm, $username, {});
101 $isUpload = 1;
102 }
103
104 # we skip CSRF check for file upload, because it is
105 # difficult to pass CSRF HTTP headers with native html forms,
106 # and it should not be necessary at all.
107 my $euid = $>;
108 PVE::AccessControl::verify_csrf_prevention_token($username, $token)
109 if !$isUpload && ($euid != 0) && ($method ne 'GET');
110 }
111
112 return {
113 ticket => $ticket,
114 token => $token,
115 userid => $username,
116 age => $age,
117 isUpload => $isUpload,
118 };
119 }
120
121 sub rest_handler {
122 my ($self, $clientip, $method, $rel_uri, $auth, $params) = @_;
123
124 my $rpcenv = $self->{rpcenv};
125
126 my $resp = {
127 status => HTTP_NOT_IMPLEMENTED,
128 message => "Method '$method $rel_uri' not implemented",
129 };
130
131 my ($handler, $info);
132
133 eval {
134 my $uri_param = {};
135 ($handler, $info) = PVE::API2->find_handler($method, $rel_uri, $uri_param);
136 return if !$handler || !$info;
137
138 foreach my $p (keys %{$params}) {
139 if (defined($uri_param->{$p})) {
140 raise_param_exc({$p => "duplicate parameter (already defined in URI)"});
141 }
142 $uri_param->{$p} = $params->{$p};
143 }
144
145 # check access permissions
146 $rpcenv->check_api2_permissions($info->{permissions}, $auth->{userid}, $uri_param);
147
148 if ($info->{proxyto} || $info->{proxyto_callback}) {
149 my $node = PVE::API2Tools::resolve_proxyto(
150 $rpcenv, $info->{proxyto_callback}, $info->{proxyto}, $uri_param);
151
152 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
153 die "unable to proxy file uploads" if $auth->{isUpload};
154 my $remip = $self->remote_node_ip($node);
155 $resp = { proxy => $remip, proxynode => $node, proxy_params => $params };
156 return;
157 }
158 }
159
160 my $euid = $>;
161 if ($info->{protected} && ($euid != 0)) {
162 $resp = { proxy => 'localhost' , proxy_params => $params };
163 return;
164 }
165
166 $resp = {
167 data => $handler->handle($info, $uri_param),
168 info => $info, # useful to format output
169 status => HTTP_OK,
170 };
171
172 if (my $count = $rpcenv->get_result_attrib('total')) {
173 $resp->{total} = $count;
174 }
175
176 if (my $diff = $rpcenv->get_result_attrib('changes')) {
177 $resp->{changes} = $diff;
178 }
179 };
180 my $err = $@;
181
182 $rpcenv->set_user(undef); # clear after request
183
184 if ($err) {
185 $resp = { info => $info };
186 if (ref($err) eq "PVE::Exception") {
187 $resp->{status} = $err->{code} || HTTP_INTERNAL_SERVER_ERROR;
188 $resp->{errors} = $err->{errors} if $err->{errors};
189 $resp->{message} = $err->{msg};
190 } else {
191 $resp->{status} = HTTP_INTERNAL_SERVER_ERROR;
192 $resp->{message} = $err;
193 }
194 }
195
196 return $resp;
197 }
198
199 sub check_cert_fingerprint {
200 my ($self, $cert) = @_;
201
202 return PVE::Cluster::check_cert_fingerprint($cert);
203 }
204
205 sub initialize_cert_cache {
206 my ($self, $node) = @_;
207
208 PVE::Cluster::initialize_cert_cache($node);
209 }
210
211 sub remote_node_ip {
212 my ($self, $node) = @_;
213
214 my $remip = PVE::Cluster::remote_node_ip($node);
215
216 die "unable to get remote IP address for node '$node'\n" if !$remip;
217
218 return $remip;
219 }
220
221 1;