]>
Commit | Line | Data |
---|---|---|
d08808bc | 1 | package PVE::APIServer::AnyEvent; |
d8218001 | 2 | |
8d88baec DM |
3 | # Note 1: interactions with Crypt::OpenSSL::RSA |
4 | # | |
5 | # Some handlers (auth_handler) use Crypt::OpenSSL::RSA, which seems to | |
6 | # set the openssl error variable. We need to clear that here, else | |
7 | # AnyEvent::TLS aborts the connection. | |
8 | # Net::SSLeay::ERR_clear_error(); | |
9 | ||
d8218001 DM |
10 | use strict; |
11 | use warnings; | |
5cef57de TL |
12 | |
13 | use AnyEvent::HTTP; | |
d8218001 | 14 | use AnyEvent::Handle; |
d8218001 | 15 | use AnyEvent::IO; |
5cef57de TL |
16 | use AnyEvent::Socket; |
17 | # use AnyEvent::Strict; # only use this for debugging | |
18 | use AnyEvent::TLS; | |
19 | use AnyEvent::Util qw(guard fh_nonblocking WSAEWOULDBLOCK WSAEINPROGRESS); | |
20 | ||
d8218001 | 21 | use Compress::Zlib; |
5cef57de TL |
22 | use Digest::MD5; |
23 | use Digest::SHA; | |
256da581 | 24 | use Encode; |
5cef57de TL |
25 | use Fcntl (); |
26 | use Fcntl; | |
27 | use File::Find; | |
28 | use File::stat qw(); | |
29 | use IO::File; | |
30 | use MIME::Base64; | |
31 | use Net::SSLeay; | |
32 | use POSIX qw(strftime EINTR EAGAIN); | |
33 | use Socket qw(IPPROTO_TCP TCP_NODELAY SOMAXCONN); | |
34 | use Time::HiRes qw(usleep ualarm gettimeofday tv_interval); | |
d8218001 | 35 | |
5cef57de | 36 | #use Data::Dumper; # FIXME: remove, just use: print to_json([$var], {pretty => 1}) ."\n"; |
d8218001 DM |
37 | use HTTP::Date; |
38 | use HTTP::Headers; | |
39 | use HTTP::Request; | |
40 | use HTTP::Response; | |
5cef57de | 41 | use HTTP::Status qw(:constants); |
3e05f166 | 42 | use JSON; |
5cef57de TL |
43 | use Net::IP; |
44 | use URI::Escape; | |
45 | use URI; | |
46 | ||
47 | use PVE::INotify; | |
48 | use PVE::SafeSyslog; | |
a66b77d8 | 49 | use PVE::Tools qw(trim); |
5cef57de TL |
50 | |
51 | use PVE::APIServer::Formatter; | |
52 | use PVE::APIServer::Utils; | |
d8218001 | 53 | |
e7ee5e3f | 54 | my $limit_max_headers = 64; |
d8218001 | 55 | my $limit_max_header_size = 8*1024; |
967dcb76 | 56 | my $limit_max_post = 64*1024; |
d8218001 | 57 | |
d8218001 DM |
58 | my $known_methods = { |
59 | GET => 1, | |
60 | POST => 1, | |
61 | PUT => 1, | |
62 | DELETE => 1, | |
63 | }; | |
64 | ||
65 | my $split_abs_uri = sub { | |
66 | my ($abs_uri, $base_uri) = @_; | |
67 | ||
68 | my ($format, $rel_uri) = $abs_uri =~ m/^\Q$base_uri\E\/+([a-z][a-z0-9]+)(\/.*)?$/; | |
69 | $rel_uri = '/' if !$rel_uri; | |
70 | ||
71 | return wantarray ? ($rel_uri, $format) : $rel_uri; | |
72 | }; | |
73 | ||
26e73e6b SI |
74 | sub dprint { |
75 | my ($self, $message) = @_; | |
76 | ||
77 | return if !$self->{debug}; | |
78 | ||
79 | my ($pkg, $pkgfile, $line, $sub) = caller(1); | |
80 | $sub =~ s/^(?:.+::)+//; | |
81 | print "worker[$$]: $pkg +$line: $sub: $message\n"; | |
82 | } | |
83 | ||
d8218001 DM |
84 | sub log_request { |
85 | my ($self, $reqstate) = @_; | |
86 | ||
87 | my $loginfo = $reqstate->{log}; | |
88 | ||
89 | # like apache2 common log format | |
90 | # LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" | |
91 | ||
92 | return if $loginfo->{written}; # avoid duplicate logs | |
93 | $loginfo->{written} = 1; | |
94 | ||
95 | my $peerip = $reqstate->{peer_host} || '-'; | |
96 | my $userid = $loginfo->{userid} || '-'; | |
97 | my $content_length = defined($loginfo->{content_length}) ? $loginfo->{content_length} : '-'; | |
98 | my $code = $loginfo->{code} || 500; | |
99 | my $requestline = $loginfo->{requestline} || '-'; | |
2fd728c9 | 100 | my $timestr = strftime("%d/%m/%Y:%H:%M:%S %z", localtime()); |
d8218001 DM |
101 | |
102 | my $msg = "$peerip - $userid [$timestr] \"$requestline\" $code $content_length\n"; | |
103 | ||
104 | $self->write_log($msg); | |
105 | } | |
106 | ||
107 | sub log_aborted_request { | |
108 | my ($self, $reqstate, $error) = @_; | |
109 | ||
110 | my $r = $reqstate->{request}; | |
111 | return if !$r; # no active request | |
112 | ||
113 | if ($error) { | |
114 | syslog("err", "problem with client $reqstate->{peer_host}; $error"); | |
115 | } | |
116 | ||
117 | $self->log_request($reqstate); | |
118 | } | |
119 | ||
d8218001 | 120 | sub cleanup_reqstate { |
7fb356b7 | 121 | my ($reqstate, $deletetmpfile) = @_; |
d8218001 DM |
122 | |
123 | delete $reqstate->{log}; | |
124 | delete $reqstate->{request}; | |
125 | delete $reqstate->{proto}; | |
126 | delete $reqstate->{accept_gzip}; | |
127 | delete $reqstate->{starttime}; | |
128 | ||
129 | if ($reqstate->{tmpfilename}) { | |
7fb356b7 | 130 | unlink $reqstate->{tmpfilename} if $deletetmpfile; |
d8218001 DM |
131 | delete $reqstate->{tmpfilename}; |
132 | } | |
133 | } | |
134 | ||
135 | sub client_do_disconnect { | |
136 | my ($self, $reqstate) = @_; | |
137 | ||
7fb356b7 | 138 | cleanup_reqstate($reqstate, 1); |
d8218001 DM |
139 | |
140 | my $shutdown_hdl = sub { | |
141 | my $hdl = shift; | |
142 | ||
143 | shutdown($hdl->{fh}, 1); | |
144 | # clear all handlers | |
145 | $hdl->on_drain(undef); | |
146 | $hdl->on_read(undef); | |
147 | $hdl->on_eof(undef); | |
148 | }; | |
149 | ||
150 | if (my $proxyhdl = delete $reqstate->{proxyhdl}) { | |
b1364a3c FG |
151 | &$shutdown_hdl($proxyhdl) |
152 | if !$proxyhdl->{block_disconnect}; | |
d8218001 DM |
153 | } |
154 | ||
155 | my $hdl = delete $reqstate->{hdl}; | |
156 | ||
157 | if (!$hdl) { | |
158 | syslog('err', "detected empty handle"); | |
159 | return; | |
160 | } | |
161 | ||
26e73e6b | 162 | $self->dprint("close connection $hdl"); |
d8218001 DM |
163 | |
164 | &$shutdown_hdl($hdl); | |
165 | ||
c82d1707 SI |
166 | warn "connection count <= 0!\n" if $self->{conn_count} <= 0; |
167 | ||
d8218001 DM |
168 | $self->{conn_count}--; |
169 | ||
26e73e6b | 170 | $self->dprint("CLOSE FH" . $hdl->{fh}->fileno() . " CONN$self->{conn_count}"); |
d8218001 DM |
171 | } |
172 | ||
173 | sub finish_response { | |
174 | my ($self, $reqstate) = @_; | |
175 | ||
7fb356b7 | 176 | cleanup_reqstate($reqstate, 0); |
d8218001 | 177 | |
133e0586 DM |
178 | my $hdl = $reqstate->{hdl}; |
179 | return if !$hdl; # already disconnected | |
180 | ||
d8218001 DM |
181 | if (!$self->{end_loop} && $reqstate->{keep_alive} > 0) { |
182 | # print "KEEPALIVE $reqstate->{keep_alive}\n" if $self->{debug}; | |
183 | $hdl->on_read(sub { | |
184 | eval { $self->push_request_header($reqstate); }; | |
185 | warn $@ if $@; | |
186 | }); | |
187 | } else { | |
188 | $hdl->on_drain (sub { | |
189 | eval { | |
190 | $self->client_do_disconnect($reqstate); | |
191 | }; | |
192 | warn $@ if $@; | |
193 | }); | |
194 | } | |
195 | } | |
196 | ||
389ad881 SR |
197 | sub response_stream { |
198 | my ($self, $reqstate, $stream_fh) = @_; | |
199 | ||
200 | # disable timeout, we don't know how big the data is | |
201 | $reqstate->{hdl}->timeout(0); | |
202 | ||
203 | my $buf_size = 4*1024*1024; | |
204 | ||
205 | my $on_read; | |
206 | $on_read = sub { | |
207 | my ($hdl) = @_; | |
208 | my $reqhdl = $reqstate->{hdl}; | |
209 | return if !$reqhdl; | |
210 | ||
211 | my $wbuf_len = length($reqhdl->{wbuf}); | |
212 | my $rbuf_len = length($hdl->{rbuf}); | |
213 | # TODO: Take into account $reqhdl->{wbuf_max} ? Right now | |
214 | # that's unbounded, so just assume $buf_size | |
215 | my $to_read = $buf_size - $wbuf_len; | |
216 | $to_read = $rbuf_len if $rbuf_len < $to_read; | |
217 | if ($to_read > 0) { | |
218 | my $data = substr($hdl->{rbuf}, 0, $to_read, ''); | |
219 | $reqhdl->push_write($data); | |
220 | $rbuf_len -= $to_read; | |
221 | } elsif ($hdl->{_eof}) { | |
222 | # workaround: AnyEvent gives us a fake EPIPE if we don't consume | |
223 | # any data when called at EOF, so unregister ourselves - data is | |
224 | # flushed by on_eof anyway | |
225 | # see: https://sources.debian.org/src/libanyevent-perl/7.170-2/lib/AnyEvent/Handle.pm/#L1329 | |
226 | $hdl->on_read(); | |
227 | return; | |
228 | } | |
229 | ||
230 | # apply backpressure so we don't accept any more data into | |
231 | # buffer if the client isn't downloading fast enough | |
232 | # note: read_size can double upon read, and we also need to | |
233 | # account for one more read after start_read, so *4 | |
234 | if ($rbuf_len + $hdl->{read_size}*4 > $buf_size) { | |
235 | # stop reading until write buffer is empty | |
236 | $hdl->on_read(); | |
237 | my $prev_on_drain = $reqhdl->{on_drain}; | |
238 | $reqhdl->on_drain(sub { | |
239 | my ($wrhdl) = @_; | |
240 | # on_drain called because write buffer is empty, continue reading | |
241 | $hdl->on_read($on_read); | |
242 | if ($prev_on_drain) { | |
243 | $wrhdl->on_drain($prev_on_drain); | |
244 | $prev_on_drain->($wrhdl); | |
245 | } | |
246 | }); | |
247 | } | |
248 | }; | |
249 | ||
250 | $reqstate->{proxyhdl} = AnyEvent::Handle->new( | |
251 | fh => $stream_fh, | |
252 | rbuf_max => $buf_size, | |
253 | timeout => 0, | |
254 | on_read => $on_read, | |
255 | on_eof => sub { | |
256 | my ($hdl) = @_; | |
257 | eval { | |
258 | if (my $reqhdl = $reqstate->{hdl}) { | |
259 | $self->log_aborted_request($reqstate); | |
260 | # write out any remaining data | |
261 | $reqhdl->push_write($hdl->{rbuf}) if length($hdl->{rbuf}) > 0; | |
262 | $hdl->{rbuf} = ""; | |
263 | $reqhdl->push_shutdown(); | |
264 | $self->finish_response($reqstate); | |
265 | } | |
266 | }; | |
267 | if (my $err = $@) { syslog('err', "$err"); } | |
268 | $on_read = undef; | |
269 | }, | |
270 | on_error => sub { | |
271 | my ($hdl, $fatal, $message) = @_; | |
272 | eval { | |
273 | $self->log_aborted_request($reqstate, $message); | |
274 | $self->client_do_disconnect($reqstate); | |
275 | }; | |
276 | if (my $err = $@) { syslog('err', "$err"); } | |
277 | $on_read = undef; | |
278 | }, | |
279 | ); | |
280 | } | |
281 | ||
d8218001 | 282 | sub response { |
389ad881 | 283 | my ($self, $reqstate, $resp, $mtime, $nocomp, $delay, $stream_fh) = @_; |
d8218001 DM |
284 | |
285 | #print "$$: send response: " . Dumper($resp); | |
286 | ||
287 | # activate timeout | |
288 | $reqstate->{hdl}->timeout_reset(); | |
289 | $reqstate->{hdl}->timeout($self->{timeout}); | |
290 | ||
025b3038 | 291 | $nocomp = 1 if !$self->{compression}; |
d8218001 DM |
292 | $nocomp = 1 if !$reqstate->{accept_gzip}; |
293 | ||
294 | my $code = $resp->code; | |
295 | my $msg = $resp->message || HTTP::Status::status_message($code); | |
d8218001 DM |
296 | my $content = $resp->content; |
297 | ||
936007ae TL |
298 | # multiline mode only checks \n for $, so explicitly check for any \n or \r afterwards |
299 | ($msg) = $msg =~ m/^(.*)$/m; | |
300 | if ($msg =~ /[\r\n]/) { | |
301 | $code = 400; # bad request from user | |
302 | $msg = HTTP::Status::status_message($code); | |
303 | $content = ''; | |
304 | } | |
305 | ||
d8218001 | 306 | if ($code =~ /^(1\d\d|[23]04)$/) { |
b9a7fd54 | 307 | # make sure informational, no content and not modified response send no content |
d8218001 DM |
308 | $content = ""; |
309 | } | |
310 | ||
311 | $reqstate->{keep_alive} = 0 if ($code >= 400) || $self->{end_loop}; | |
312 | ||
313 | $reqstate->{log}->{code} = $code; | |
314 | ||
315 | my $proto = $reqstate->{proto} ? $reqstate->{proto}->{str} : 'HTTP/1.0'; | |
316 | my $res = "$proto $code $msg\015\012"; | |
317 | ||
318 | my $ctime = time(); | |
319 | my $date = HTTP::Date::time2str($ctime); | |
320 | $resp->header('Date' => $date); | |
321 | if ($mtime) { | |
322 | $resp->header('Last-Modified' => HTTP::Date::time2str($mtime)); | |
323 | } else { | |
324 | $resp->header('Expires' => $date); | |
325 | $resp->header('Cache-Control' => "max-age=0"); | |
326 | $resp->header("Pragma", "no-cache"); | |
327 | } | |
328 | ||
329 | $resp->header('Server' => "pve-api-daemon/3.0"); | |
330 | ||
331 | my $content_length; | |
389ad881 | 332 | if ($content && !$stream_fh) { |
d8218001 DM |
333 | |
334 | $content_length = length($content); | |
335 | ||
336 | if (!$nocomp && ($content_length > 1024)) { | |
337 | my $comp = Compress::Zlib::memGzip($content); | |
338 | $resp->header('Content-Encoding', 'gzip'); | |
339 | $content = $comp; | |
340 | $content_length = length($content); | |
341 | } | |
342 | $resp->header("Content-Length" => $content_length); | |
343 | $reqstate->{log}->{content_length} = $content_length; | |
344 | ||
345 | } else { | |
346 | $resp->remove_header("Content-Length"); | |
347 | } | |
348 | ||
349 | if ($reqstate->{keep_alive} > 0) { | |
350 | $resp->push_header('Connection' => 'Keep-Alive'); | |
351 | } else { | |
352 | $resp->header('Connection' => 'close'); | |
353 | } | |
354 | ||
355 | $res .= $resp->headers_as_string("\015\012"); | |
356 | #print "SEND(without content) $res\n" if $self->{debug}; | |
357 | ||
358 | $res .= "\015\012"; | |
389ad881 | 359 | $res .= $content if $content && !$stream_fh; |
d8218001 DM |
360 | |
361 | $self->log_request($reqstate, $reqstate->{request}); | |
5f14e56e | 362 | |
389ad881 SR |
363 | if ($stream_fh) { |
364 | # write headers and preamble... | |
365 | $reqstate->{hdl}->push_write($res); | |
366 | # ...then stream data via an AnyEvent::Handle | |
367 | $self->response_stream($reqstate, $stream_fh); | |
368 | } elsif ($delay && $delay > 0) { | |
d8218001 DM |
369 | my $w; $w = AnyEvent->timer(after => $delay, cb => sub { |
370 | undef $w; # delete reference | |
e12e46c9 | 371 | return if !$reqstate->{hdl}; # already disconnected |
d8218001 DM |
372 | $reqstate->{hdl}->push_write($res); |
373 | $self->finish_response($reqstate); | |
374 | }); | |
375 | } else { | |
376 | $reqstate->{hdl}->push_write($res); | |
377 | $self->finish_response($reqstate); | |
378 | } | |
379 | } | |
380 | ||
381 | sub error { | |
382 | my ($self, $reqstate, $code, $msg, $hdr, $content) = @_; | |
383 | ||
384 | eval { | |
385 | my $resp = HTTP::Response->new($code, $msg, $hdr, $content); | |
386 | $self->response($reqstate, $resp); | |
387 | }; | |
388 | warn $@ if $@; | |
389 | } | |
390 | ||
391 | my $file_extension_info = { | |
392 | css => { ct => 'text/css' }, | |
393 | html => { ct => 'text/html' }, | |
394 | js => { ct => 'application/javascript' }, | |
3d12de14 | 395 | json => { ct => 'application/json' }, |
b1e84aa3 | 396 | map => { ct => 'application/json' }, |
d8218001 DM |
397 | png => { ct => 'image/png' , nocomp => 1 }, |
398 | ico => { ct => 'image/x-icon', nocomp => 1}, | |
399 | gif => { ct => 'image/gif', nocomp => 1}, | |
3d12de14 | 400 | svg => { ct => 'image/svg+xml' }, |
d8218001 DM |
401 | jar => { ct => 'application/java-archive', nocomp => 1}, |
402 | woff => { ct => 'application/font-woff', nocomp => 1}, | |
403 | woff2 => { ct => 'application/font-woff2', nocomp => 1}, | |
404 | ttf => { ct => 'application/font-snft', nocomp => 1}, | |
405 | pdf => { ct => 'application/pdf', nocomp => 1}, | |
406 | epub => { ct => 'application/epub+zip', nocomp => 1}, | |
3d12de14 DC |
407 | mp3 => { ct => 'audio/mpeg', nocomp => 1}, |
408 | oga => { ct => 'audio/ogg', nocomp => 1}, | |
80d61dd6 | 409 | tgz => { ct => 'application/x-compressed-tar', nocomp => 1}, |
d8218001 DM |
410 | }; |
411 | ||
412 | sub send_file_start { | |
05e777bc | 413 | my ($self, $reqstate, $download) = @_; |
d8218001 DM |
414 | |
415 | eval { | |
416 | # print "SEND FILE $filename\n"; | |
417 | # Note: aio_load() this is not really async unless we use IO::AIO! | |
418 | eval { | |
419 | ||
420 | my $r = $reqstate->{request}; | |
421 | ||
05e777bc DC |
422 | my $fh; |
423 | my $nocomp; | |
424 | my $mime; | |
425 | ||
426 | if (ref($download) eq 'HASH') { | |
05e777bc | 427 | $mime = $download->{'content-type'}; |
127dbe7c | 428 | my $encoding = $download->{'content-encoding'}; |
9c1388da | 429 | my $disposition = $download->{'content-disposition'}; |
389ad881 | 430 | |
51841e98 SR |
431 | if ($download->{path} && $download->{stream} && |
432 | $reqstate->{request}->header('PVEDisableProxy')) | |
433 | { | |
434 | # avoid double stream from a file, let the proxy handle it | |
435 | die "internal error: file proxy streaming only available for pvedaemon\n" | |
436 | if !$self->{trusted_env}; | |
437 | my $header = HTTP::Headers->new( | |
438 | pvestreamfile => $download->{path}, | |
439 | Content_Type => $mime, | |
440 | ); | |
127dbe7c | 441 | $header->header('Content-Encoding' => $encoding) if defined($encoding); |
9c1388da | 442 | $header->header('Content-Disposition' => $disposition) if defined($disposition); |
51841e98 SR |
443 | # we need some data so Content-Length gets set correctly and |
444 | # the proxy doesn't wait for more data - place a canary | |
445 | my $resp = HTTP::Response->new(200, "OK", $header, "error canary"); | |
446 | $self->response($reqstate, $resp); | |
447 | return; | |
448 | } | |
449 | ||
450 | if (!($fh = $download->{fh})) { | |
451 | my $path = $download->{path}; | |
452 | die "internal error: {download} returned but neither fh not path given\n" | |
453 | if !$path; | |
454 | sysopen($fh, "$path", O_NONBLOCK | O_RDONLY) | |
455 | or die "open stream path '$path' for reading failed: $!\n"; | |
456 | } | |
457 | ||
389ad881 SR |
458 | if ($download->{stream}) { |
459 | my $header = HTTP::Headers->new(Content_Type => $mime); | |
127dbe7c | 460 | $header->header('Content-Encoding' => $encoding) if defined($encoding); |
9c1388da | 461 | $header->header('Content-Disposition' => $disposition) if defined($disposition); |
389ad881 SR |
462 | my $resp = HTTP::Response->new(200, "OK", $header); |
463 | $self->response($reqstate, $resp, undef, 1, 0, $fh); | |
464 | return; | |
465 | } | |
05e777bc DC |
466 | } else { |
467 | my $filename = $download; | |
468 | $fh = IO::File->new($filename, '<') || | |
469 | die "unable to open file '$filename' - $!\n"; | |
470 | ||
471 | my ($ext) = $filename =~ m/\.([^.]*)$/; | |
472 | my $ext_info = $file_extension_info->{$ext}; | |
473 | ||
474 | die "unable to detect content type" if !$ext_info; | |
475 | $mime = $ext_info->{ct}; | |
476 | $nocomp = $ext_info->{nocomp}; | |
477 | } | |
478 | ||
d8218001 DM |
479 | my $stat = File::stat::stat($fh) || |
480 | die "$!\n"; | |
5f14e56e | 481 | |
d8218001 DM |
482 | my $mtime = $stat->mtime; |
483 | ||
484 | if (my $ifmod = $r->header('if-modified-since')) { | |
485 | my $iftime = HTTP::Date::str2time($ifmod); | |
486 | if ($mtime <= $iftime) { | |
487 | my $resp = HTTP::Response->new(304, "NOT MODIFIED"); | |
488 | $self->response($reqstate, $resp, $mtime); | |
489 | return; | |
490 | } | |
491 | } | |
492 | ||
493 | my $data; | |
494 | my $len = sysread($fh, $data, $stat->size); | |
495 | die "got short file\n" if !defined($len) || $len != $stat->size; | |
496 | ||
05e777bc | 497 | my $header = HTTP::Headers->new(Content_Type => $mime); |
d8218001 | 498 | my $resp = HTTP::Response->new(200, "OK", $header, $data); |
05e777bc | 499 | $self->response($reqstate, $resp, $mtime, $nocomp); |
d8218001 DM |
500 | }; |
501 | if (my $err = $@) { | |
502 | $self->error($reqstate, 501, $err); | |
503 | } | |
504 | }; | |
505 | ||
506 | warn $@ if $@; | |
507 | } | |
508 | ||
509 | sub websocket_proxy { | |
510 | my ($self, $reqstate, $wsaccept, $wsproto, $param) = @_; | |
511 | ||
512 | eval { | |
513 | my $remhost; | |
514 | my $remport; | |
515 | ||
ed8d2557 | 516 | my $max_payload_size = 128*1024; |
d8218001 | 517 | |
d8218001 DM |
518 | if ($param->{port}) { |
519 | $remhost = 'localhost'; | |
520 | $remport = $param->{port}; | |
521 | } elsif ($param->{socket}) { | |
522 | $remhost = 'unix/'; | |
523 | $remport = $param->{socket}; | |
524 | } else { | |
525 | die "websocket_proxy: missing port or socket\n"; | |
526 | } | |
527 | ||
c3bd22ec FG |
528 | my $encode = sub { |
529 | my ($data, $opcode) = @_; | |
530 | ||
531 | my $string; | |
532 | my $payload; | |
12014359 DC |
533 | |
534 | $string = $opcode ? $opcode : "\x82"; # binary frame | |
535 | $payload = $$data; | |
c3bd22ec FG |
536 | |
537 | my $payload_len = length($payload); | |
538 | if ($payload_len <= 125) { | |
539 | $string .= pack 'C', $payload_len; | |
540 | } elsif ($payload_len <= 0xffff) { | |
541 | $string .= pack 'C', 126; | |
542 | $string .= pack 'n', $payload_len; | |
543 | } else { | |
544 | $string .= pack 'C', 127; | |
545 | $string .= pack 'Q>', $payload_len; | |
546 | } | |
547 | $string .= $payload; | |
548 | return $string; | |
549 | }; | |
550 | ||
d8218001 | 551 | tcp_connect $remhost, $remport, sub { |
5f14e56e | 552 | my ($fh) = @_ |
d8218001 | 553 | or die "connect to '$remhost:$remport' failed: $!"; |
5f14e56e | 554 | |
26e73e6b | 555 | $self->dprint("CONNECTed to '$remhost:$remport'"); |
d8218001 DM |
556 | |
557 | $reqstate->{proxyhdl} = AnyEvent::Handle->new( | |
558 | fh => $fh, | |
cdb69327 | 559 | rbuf_max => $max_payload_size, |
ed8d2557 | 560 | wbuf_max => $max_payload_size*5, |
d8218001 DM |
561 | timeout => 5, |
562 | on_eof => sub { | |
563 | my ($hdl) = @_; | |
564 | eval { | |
565 | $self->log_aborted_request($reqstate); | |
566 | $self->client_do_disconnect($reqstate); | |
567 | }; | |
568 | if (my $err = $@) { syslog('err', $err); } | |
569 | }, | |
570 | on_error => sub { | |
571 | my ($hdl, $fatal, $message) = @_; | |
572 | eval { | |
573 | $self->log_aborted_request($reqstate, $message); | |
574 | $self->client_do_disconnect($reqstate); | |
575 | }; | |
576 | if (my $err = $@) { syslog('err', "$err"); } | |
577 | }); | |
578 | ||
579 | my $proxyhdlreader = sub { | |
580 | my ($hdl) = @_; | |
5f14e56e | 581 | |
d8218001 | 582 | my $len = length($hdl->{rbuf}); |
cdb69327 | 583 | my $data = substr($hdl->{rbuf}, 0, $len > $max_payload_size ? $max_payload_size : $len, ''); |
d8218001 | 584 | |
c3bd22ec | 585 | my $string = $encode->(\$data); |
d8218001 DM |
586 | |
587 | $reqstate->{hdl}->push_write($string) if $reqstate->{hdl}; | |
588 | }; | |
589 | ||
590 | my $hdlreader = sub { | |
591 | my ($hdl) = @_; | |
592 | ||
ed979f7a RJ |
593 | while (my $len = length($hdl->{rbuf})) { |
594 | return if $len < 2; | |
d8218001 | 595 | |
ed979f7a RJ |
596 | my $hdr = unpack('C', substr($hdl->{rbuf}, 0, 1)); |
597 | my $opcode = $hdr & 0b00001111; | |
598 | my $fin = $hdr & 0b10000000; | |
d8218001 | 599 | |
ed979f7a | 600 | die "received fragmented websocket frame\n" if !$fin; |
d8218001 | 601 | |
ed979f7a RJ |
602 | my $rsv = $hdr & 0b01110000; |
603 | die "received websocket frame with RSV flags\n" if $rsv; | |
d8218001 | 604 | |
ed979f7a | 605 | my $payload_len = unpack 'C', substr($hdl->{rbuf}, 1, 1); |
d8218001 | 606 | |
ed979f7a RJ |
607 | my $masked = $payload_len & 0b10000000; |
608 | die "received unmasked websocket frame from client\n" if !$masked; | |
d8218001 | 609 | |
ed979f7a RJ |
610 | my $offset = 2; |
611 | $payload_len = $payload_len & 0b01111111; | |
612 | if ($payload_len == 126) { | |
613 | return if $len < 4; | |
614 | $payload_len = unpack('n', substr($hdl->{rbuf}, $offset, 2)); | |
615 | $offset += 2; | |
616 | } elsif ($payload_len == 127) { | |
617 | return if $len < 10; | |
618 | $payload_len = unpack('Q>', substr($hdl->{rbuf}, $offset, 8)); | |
619 | $offset += 8; | |
620 | } | |
d8218001 | 621 | |
ed979f7a RJ |
622 | die "received too large websocket frame (len = $payload_len)\n" |
623 | if ($payload_len > $max_payload_size) || ($payload_len < 0); | |
d8218001 | 624 | |
ed979f7a | 625 | return if $len < ($offset + 4 + $payload_len); |
d8218001 | 626 | |
ed979f7a | 627 | my $data = substr($hdl->{rbuf}, 0, $offset + 4 + $payload_len, ''); # now consume data |
5f14e56e | 628 | |
c55702c3 | 629 | my $mask = substr($data, $offset, 4); |
ed979f7a | 630 | $offset += 4; |
d8218001 | 631 | |
ed979f7a | 632 | my $payload = substr($data, $offset, $payload_len); |
d8218001 | 633 | |
c55702c3 FG |
634 | # NULL-mask might be used over TLS, skip to increase performance |
635 | if ($mask ne pack('N', 0)) { | |
636 | # repeat 4 byte mask to payload length + up to 4 byte | |
637 | $mask = $mask x (int($payload_len / 4) + 1); | |
638 | # truncate mask to payload length | |
639 | substr($mask, $payload_len) = ""; | |
640 | # (un-)apply mask | |
641 | $payload ^= $mask; | |
ed979f7a | 642 | } |
d8218001 | 643 | |
ed979f7a RJ |
644 | if ($opcode == 1 || $opcode == 2) { |
645 | $reqstate->{proxyhdl}->push_write($payload) if $reqstate->{proxyhdl}; | |
646 | } elsif ($opcode == 8) { | |
647 | my $statuscode = unpack ("n", $payload); | |
26e73e6b | 648 | $self->dprint("websocket received close. status code: '$statuscode'"); |
b1364a3c | 649 | if (my $proxyhdl = $reqstate->{proxyhdl}) { |
6277311e FG |
650 | $proxyhdl->{block_disconnect} = 1 if length $proxyhdl->{wbuf}; |
651 | ||
b1364a3c FG |
652 | $proxyhdl->push_shutdown(); |
653 | } | |
ed979f7a | 654 | $hdl->push_shutdown(); |
e6cb79b5 FG |
655 | } elsif ($opcode == 9) { |
656 | # ping received, schedule pong | |
657 | $reqstate->{hdl}->push_write($encode->(\$payload, "\x8A")) if $reqstate->{hdl}; | |
658 | } elsif ($opcode == 0xA) { | |
659 | # pong received, continue | |
ed979f7a RJ |
660 | } else { |
661 | die "received unhandled websocket opcode $opcode\n"; | |
d8218001 | 662 | } |
d8218001 DM |
663 | } |
664 | }; | |
665 | ||
666 | my $proto = $reqstate->{proto} ? $reqstate->{proto}->{str} : 'HTTP/1.1'; | |
667 | ||
668 | $reqstate->{proxyhdl}->timeout(0); | |
669 | $reqstate->{proxyhdl}->on_read($proxyhdlreader); | |
670 | $reqstate->{hdl}->on_read($hdlreader); | |
671 | ||
672 | # todo: use stop_read/start_read if write buffer grows to much | |
673 | ||
1d7da936 FG |
674 | # FIXME: remove protocol in PVE/PMG 8.x |
675 | # | |
e7eb7165 DC |
676 | # for backwards, compatibility, we have to reply with the websocket |
677 | # subprotocol from the request | |
d8218001 DM |
678 | my $res = "$proto 101 Switching Protocols\015\012" . |
679 | "Upgrade: websocket\015\012" . | |
680 | "Connection: upgrade\015\012" . | |
681 | "Sec-WebSocket-Accept: $wsaccept\015\012" . | |
e7eb7165 | 682 | ($wsproto ne "" ? "Sec-WebSocket-Protocol: $wsproto\015\012" : "") . |
d8218001 DM |
683 | "\015\012"; |
684 | ||
26e73e6b | 685 | $self->dprint($res); |
d8218001 DM |
686 | |
687 | $reqstate->{hdl}->push_write($res); | |
688 | ||
689 | # log early | |
690 | $reqstate->{log}->{code} = 101; | |
691 | $self->log_request($reqstate); | |
692 | }; | |
693 | ||
694 | }; | |
695 | if (my $err = $@) { | |
696 | warn $err; | |
697 | $self->log_aborted_request($reqstate, $err); | |
698 | $self->client_do_disconnect($reqstate); | |
699 | } | |
700 | } | |
701 | ||
702 | sub proxy_request { | |
362dd8eb | 703 | my ($self, $reqstate, $clientip, $host, $node, $method, $uri, $auth, $params) = @_; |
d8218001 DM |
704 | |
705 | eval { | |
706 | my $target; | |
707 | my $keep_alive = 1; | |
580d540e TL |
708 | |
709 | # stringify URI object and verify it starts with a slash | |
710 | $uri = "$uri"; | |
711 | if ($uri !~ m@^/@) { | |
712 | $self->error($reqstate, 400, "invalid proxy uri"); | |
713 | return; | |
714 | } | |
715 | ||
e9df8a6e | 716 | my $may_stream_file; |
d8218001 DM |
717 | if ($host eq 'localhost') { |
718 | $target = "http://$host:85$uri"; | |
719 | # keep alive for localhost is not worth (connection setup is about 0.2ms) | |
720 | $keep_alive = 0; | |
e9df8a6e | 721 | $may_stream_file = 1; |
d8218001 DM |
722 | } elsif (Net::IP::ip_is_ipv6($host)) { |
723 | $target = "https://[$host]:8006$uri"; | |
724 | } else { | |
725 | $target = "https://$host:8006$uri"; | |
726 | } | |
727 | ||
728 | my $headers = { | |
729 | PVEDisableProxy => 'true', | |
730 | PVEClientIP => $clientip, | |
731 | }; | |
732 | ||
362dd8eb FG |
733 | $headers->{'cookie'} = PVE::APIServer::Formatter::create_auth_cookie($auth->{ticket}, $self->{cookie_name}) |
734 | if $auth->{ticket}; | |
735 | $headers->{'Authorization'} = PVE::APIServer::Formatter::create_auth_header($auth->{api_token}, $self->{apitoken_name}) | |
736 | if $auth->{api_token}; | |
737 | $headers->{'CSRFPreventionToken'} = $auth->{token} | |
738 | if $auth->{token}; | |
23dfd1b7 | 739 | $headers->{'Accept-Encoding'} = 'gzip' if ($reqstate->{accept_gzip} && $self->{compression}); |
d8218001 | 740 | |
090019fa WB |
741 | if (defined(my $host = $reqstate->{request}->header('Host'))) { |
742 | $headers->{Host} = $host; | |
743 | } | |
744 | ||
d8218001 DM |
745 | my $content; |
746 | ||
747 | if ($method eq 'POST' || $method eq 'PUT') { | |
748 | $headers->{'Content-Type'} = 'application/x-www-form-urlencoded'; | |
749 | # use URI object to format application/x-www-form-urlencoded content. | |
750 | my $url = URI->new('http:'); | |
751 | $url->query_form(%$params); | |
752 | $content = $url->query; | |
753 | if (defined($content)) { | |
754 | $headers->{'Content-Length'} = length($content); | |
755 | } | |
756 | } | |
757 | ||
758 | my $tls = { | |
759 | # TLS 1.x only, with certificate pinning | |
760 | method => 'any', | |
761 | sslv2 => 0, | |
762 | sslv3 => 0, | |
763 | verify => 1, | |
764 | verify_cb => sub { | |
765 | my (undef, undef, undef, $depth, undef, undef, $cert) = @_; | |
766 | # we don't care about intermediate or root certificates | |
767 | return 1 if $depth != 0; | |
768 | # check server certificate against cache of pinned FPs | |
769 | return $self->check_cert_fingerprint($cert); | |
770 | }, | |
771 | }; | |
772 | ||
773 | # load and cache cert fingerprint if first time we proxy to this node | |
774 | $self->initialize_cert_cache($node); | |
775 | ||
776 | my $w; $w = http_request( | |
777 | $method => $target, | |
778 | headers => $headers, | |
779 | timeout => 30, | |
780 | recurse => 0, | |
781 | proxy => undef, # avoid use of $ENV{HTTP_PROXY} | |
782 | keepalive => $keep_alive, | |
783 | body => $content, | |
784 | tls_ctx => AnyEvent::TLS->new(%{$tls}), | |
785 | sub { | |
786 | my ($body, $hdr) = @_; | |
787 | ||
788 | undef $w; | |
789 | ||
790 | if (!$reqstate->{hdl}) { | |
791 | warn "proxy detected vanished client connection\n"; | |
792 | return; | |
793 | } | |
794 | ||
795 | eval { | |
796 | my $code = delete $hdr->{Status}; | |
797 | my $msg = delete $hdr->{Reason}; | |
51841e98 | 798 | my $stream = delete $hdr->{pvestreamfile}; |
d8218001 DM |
799 | delete $hdr->{URL}; |
800 | delete $hdr->{HTTPVersion}; | |
801 | my $header = HTTP::Headers->new(%$hdr); | |
802 | if (my $location = $header->header('Location')) { | |
803 | $location =~ s|^http://localhost:85||; | |
804 | $header->header(Location => $location); | |
805 | } | |
51841e98 | 806 | if ($stream) { |
e9df8a6e TL |
807 | if (!$may_stream_file) { |
808 | $self->error($reqstate, 403, 'streaming denied'); | |
809 | return; | |
810 | } | |
51841e98 SR |
811 | sysopen(my $fh, "$stream", O_NONBLOCK | O_RDONLY) |
812 | or die "open stream path '$stream' for forwarding failed: $!\n"; | |
813 | my $resp = HTTP::Response->new($code, $msg, $header, undef); | |
814 | $self->response($reqstate, $resp, undef, 1, 0, $fh); | |
815 | } else { | |
816 | my $resp = HTTP::Response->new($code, $msg, $header, $body); | |
817 | # Note: disable compression, because body is already compressed | |
818 | $self->response($reqstate, $resp, undef, 1); | |
819 | } | |
d8218001 DM |
820 | }; |
821 | warn $@ if $@; | |
822 | }); | |
823 | }; | |
824 | warn $@ if $@; | |
825 | } | |
826 | ||
827 | # return arrays as \0 separated strings (like CGI.pm) | |
256da581 | 828 | # assume data is UTF8 encoded |
d8218001 DM |
829 | sub decode_urlencoded { |
830 | my ($data) = @_; | |
831 | ||
832 | my $res = {}; | |
833 | ||
834 | return $res if !$data; | |
835 | ||
836 | foreach my $kv (split(/[\&\;]/, $data)) { | |
837 | my ($k, $v) = split(/=/, $kv); | |
838 | $k =~s/\+/ /g; | |
839 | $k =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg; | |
d8218001 | 840 | |
4a09623b TL |
841 | if (defined($v)) { |
842 | $v =~s/\+/ /g; | |
843 | $v =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg; | |
256da581 | 844 | |
4a09623b TL |
845 | $v = Encode::decode('utf8', $v); |
846 | ||
847 | if (defined(my $old = $res->{$k})) { | |
848 | $v = "$old\0$v"; | |
849 | } | |
5f14e56e | 850 | } |
4a09623b TL |
851 | |
852 | $res->{$k} = $v; | |
d8218001 DM |
853 | } |
854 | return $res; | |
855 | } | |
856 | ||
857 | sub extract_params { | |
858 | my ($r, $method) = @_; | |
859 | ||
860 | my $params = {}; | |
861 | ||
862 | if ($method eq 'PUT' || $method eq 'POST') { | |
3e05f166 DC |
863 | my $ct; |
864 | if (my $ctype = $r->header('Content-Type')) { | |
865 | $ct = parse_content_type($ctype); | |
866 | } | |
867 | if (defined($ct) && $ct eq 'application/json') { | |
868 | $params = decode_json($r->content); | |
869 | } else { | |
870 | $params = decode_urlencoded($r->content); | |
871 | } | |
d8218001 DM |
872 | } |
873 | ||
874 | my $query_params = decode_urlencoded($r->url->query()); | |
875 | ||
876 | foreach my $k (keys %{$query_params}) { | |
877 | $params->{$k} = $query_params->{$k}; | |
878 | } | |
879 | ||
256da581 | 880 | return $params; |
d8218001 DM |
881 | } |
882 | ||
883 | sub handle_api2_request { | |
884 | my ($self, $reqstate, $auth, $method, $path, $upload_state) = @_; | |
885 | ||
886 | eval { | |
887 | my $r = $reqstate->{request}; | |
888 | ||
889 | my ($rel_uri, $format) = &$split_abs_uri($path, $self->{base_uri}); | |
890 | ||
c7154375 | 891 | my $formatter = PVE::APIServer::Formatter::get_formatter($format, $method, $rel_uri); |
d8218001 DM |
892 | |
893 | if (!defined($formatter)) { | |
555a5ab3 | 894 | $self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no formatter for uri $rel_uri, $format"); |
d8218001 DM |
895 | return; |
896 | } | |
897 | ||
898 | #print Dumper($upload_state) if $upload_state; | |
899 | ||
d8218001 DM |
900 | my $params; |
901 | ||
902 | if ($upload_state) { | |
903 | $params = $upload_state->{params}; | |
904 | } else { | |
905 | $params = extract_params($r, $method); | |
906 | } | |
907 | ||
d9b3838f | 908 | delete $params->{_dc} if $params; # remove disable cache parameter |
d8218001 DM |
909 | |
910 | my $clientip = $reqstate->{peer_host}; | |
911 | ||
66a7a3a3 | 912 | my $res = $self->rest_handler($clientip, $method, $rel_uri, $auth, $params, $format); |
d8218001 | 913 | |
8d88baec DM |
914 | # HACK: see Note 1 |
915 | Net::SSLeay::ERR_clear_error(); | |
916 | ||
d8218001 DM |
917 | AnyEvent->now_update(); # in case somebody called sleep() |
918 | ||
d8218001 DM |
919 | my $upgrade = $r->header('upgrade'); |
920 | $upgrade = lc($upgrade) if $upgrade; | |
921 | ||
922 | if (my $host = $res->{proxy}) { | |
923 | ||
924 | if ($self->{trusted_env}) { | |
925 | $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy not allowed"); | |
926 | return; | |
927 | } | |
928 | ||
929 | if ($host ne 'localhost' && $r->header('PVEDisableProxy')) { | |
930 | $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy loop detected"); | |
931 | return; | |
932 | } | |
933 | ||
934 | $res->{proxy_params}->{tmpfilename} = $reqstate->{tmpfilename} if $upload_state; | |
935 | ||
936 | $self->proxy_request($reqstate, $clientip, $host, $res->{proxynode}, $method, | |
362dd8eb | 937 | $r->uri, $auth, $res->{proxy_params}); |
d8218001 DM |
938 | return; |
939 | ||
940 | } elsif ($upgrade && ($method eq 'GET') && ($path =~ m|websocket$|)) { | |
941 | die "unable to upgrade to protocol '$upgrade'\n" if !$upgrade || ($upgrade ne 'websocket'); | |
942 | my $wsver = $r->header('sec-websocket-version'); | |
943 | die "unsupported websocket-version '$wsver'\n" if !$wsver || ($wsver ne '13'); | |
e7eb7165 | 944 | my $wsproto = $r->header('sec-websocket-protocol') // ""; |
d8218001 DM |
945 | my $wskey = $r->header('sec-websocket-key'); |
946 | die "missing websocket-key\n" if !$wskey; | |
947 | # Note: Digest::SHA::sha1_base64 has wrong padding | |
948 | my $wsaccept = Digest::SHA::sha1_base64("${wskey}258EAFA5-E914-47DA-95CA-C5AB0DC85B11") . "="; | |
949 | if ($res->{status} == HTTP_OK) { | |
950 | $self->websocket_proxy($reqstate, $wsaccept, $wsproto, $res->{data}); | |
951 | return; | |
952 | } | |
953 | } | |
954 | ||
955 | my $delay = 0; | |
956 | if ($res->{status} == HTTP_UNAUTHORIZED) { | |
957 | # always delay unauthorized calls by 3 seconds | |
958 | $delay = 3 - tv_interval($reqstate->{starttime}); | |
959 | $delay = 0 if $delay < 0; | |
960 | } | |
961 | ||
6d832dbc SR |
962 | my $download = $res->{download}; |
963 | $download //= $res->{data}->{download} | |
964 | if defined($res->{data}) && ref($res->{data}) eq 'HASH'; | |
965 | if (defined($download)) { | |
05e777bc | 966 | send_file_start($self, $reqstate, $download); |
20a61215 DM |
967 | return; |
968 | } | |
969 | ||
ca304f91 DM |
970 | my ($raw, $ct, $nocomp) = $formatter->($res, $res->{data}, $params, $path, |
971 | $auth, $self->{formatter_config}); | |
d8218001 DM |
972 | |
973 | my $resp; | |
974 | if (ref($raw) && (ref($raw) eq 'HTTP::Response')) { | |
975 | $resp = $raw; | |
976 | } else { | |
977 | $resp = HTTP::Response->new($res->{status}, $res->{message}); | |
978 | $resp->header("Content-Type" => $ct); | |
979 | $resp->content($raw); | |
980 | } | |
981 | $self->response($reqstate, $resp, undef, $nocomp, $delay); | |
982 | }; | |
983 | if (my $err = $@) { | |
984 | $self->error($reqstate, 501, $err); | |
985 | } | |
986 | } | |
987 | ||
988 | sub handle_spice_proxy_request { | |
989 | my ($self, $reqstate, $connect_str, $vmid, $node, $spiceport) = @_; | |
990 | ||
991 | eval { | |
992 | ||
542f4c3a | 993 | my ($minport, $maxport) = PVE::Tools::spice_port_range(); |
9789c451 TL |
994 | if ($spiceport < $minport || $spiceport > $maxport) { |
995 | die "SPICE Port $spiceport is not in allowed range ($minport, $maxport)\n"; | |
996 | } | |
d8218001 | 997 | |
d8218001 DM |
998 | my $clientip = $reqstate->{peer_host}; |
999 | my $r = $reqstate->{request}; | |
1000 | ||
1001 | my $remip; | |
1002 | ||
1003 | if ($node ne 'localhost' && PVE::INotify::nodename() !~ m/^$node$/i) { | |
1004 | $remip = $self->remote_node_ip($node); | |
26e73e6b | 1005 | $self->dprint("REMOTE CONNECT $vmid, $remip, $connect_str"); |
d8218001 | 1006 | } else { |
26e73e6b | 1007 | $self->dprint("CONNECT $vmid, $node, $spiceport"); |
d8218001 DM |
1008 | } |
1009 | ||
1010 | if ($remip && $r->header('PVEDisableProxy')) { | |
1011 | $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy loop detected"); | |
1012 | return; | |
1013 | } | |
1014 | ||
1015 | $reqstate->{hdl}->timeout(0); | |
1016 | $reqstate->{hdl}->wbuf_max(64*10*1024); | |
1017 | ||
1018 | my $remhost = $remip ? $remip : "localhost"; | |
1019 | my $remport = $remip ? 3128 : $spiceport; | |
1020 | ||
1021 | tcp_connect $remhost, $remport, sub { | |
5f14e56e | 1022 | my ($fh) = @_ |
d8218001 DM |
1023 | or die "connect to '$remhost:$remport' failed: $!"; |
1024 | ||
26e73e6b | 1025 | $self->dprint("CONNECTed to '$remhost:$remport'"); |
d8218001 DM |
1026 | $reqstate->{proxyhdl} = AnyEvent::Handle->new( |
1027 | fh => $fh, | |
1028 | rbuf_max => 64*1024, | |
1029 | wbuf_max => 64*10*1024, | |
1030 | timeout => 5, | |
1031 | on_eof => sub { | |
1032 | my ($hdl) = @_; | |
1033 | eval { | |
1034 | $self->log_aborted_request($reqstate); | |
1035 | $self->client_do_disconnect($reqstate); | |
1036 | }; | |
1037 | if (my $err = $@) { syslog('err', $err); } | |
1038 | }, | |
1039 | on_error => sub { | |
1040 | my ($hdl, $fatal, $message) = @_; | |
1041 | eval { | |
1042 | $self->log_aborted_request($reqstate, $message); | |
1043 | $self->client_do_disconnect($reqstate); | |
1044 | }; | |
1045 | if (my $err = $@) { syslog('err', "$err"); } | |
1046 | }); | |
1047 | ||
1048 | ||
1049 | my $proxyhdlreader = sub { | |
1050 | my ($hdl) = @_; | |
1051 | ||
1052 | my $len = length($hdl->{rbuf}); | |
1053 | my $data = substr($hdl->{rbuf}, 0, $len, ''); | |
1054 | ||
1055 | #print "READ1 $len\n"; | |
1056 | $reqstate->{hdl}->push_write($data) if $reqstate->{hdl}; | |
1057 | }; | |
1058 | ||
1059 | my $hdlreader = sub { | |
1060 | my ($hdl) = @_; | |
1061 | ||
1062 | my $len = length($hdl->{rbuf}); | |
1063 | my $data = substr($hdl->{rbuf}, 0, $len, ''); | |
1064 | ||
1065 | #print "READ0 $len\n"; | |
1066 | $reqstate->{proxyhdl}->push_write($data) if $reqstate->{proxyhdl}; | |
1067 | }; | |
1068 | ||
1069 | my $proto = $reqstate->{proto} ? $reqstate->{proto}->{str} : 'HTTP/1.0'; | |
1070 | ||
1071 | my $startproxy = sub { | |
1072 | $reqstate->{proxyhdl}->timeout(0); | |
1073 | $reqstate->{proxyhdl}->on_read($proxyhdlreader); | |
1074 | $reqstate->{hdl}->on_read($hdlreader); | |
1075 | ||
1076 | # todo: use stop_read/start_read if write buffer grows to much | |
1077 | ||
a4d8bbaf DC |
1078 | # a response must be followed by an empty line |
1079 | my $res = "$proto 200 OK\015\012\015\012"; | |
d8218001 DM |
1080 | $reqstate->{hdl}->push_write($res); |
1081 | ||
1082 | # log early | |
1083 | $reqstate->{log}->{code} = 200; | |
1084 | $self->log_request($reqstate); | |
1085 | }; | |
1086 | ||
1087 | if ($remip) { | |
1088 | my $header = "CONNECT ${connect_str} $proto\015\012" . | |
1089 | "Host: ${connect_str}\015\012" . | |
1090 | "Proxy-Connection: keep-alive\015\012" . | |
1091 | "User-Agent: spiceproxy\015\012" . | |
1092 | "PVEDisableProxy: true\015\012" . | |
1093 | "PVEClientIP: $clientip\015\012" . | |
1094 | "\015\012"; | |
1095 | ||
1096 | $reqstate->{proxyhdl}->push_write($header); | |
1097 | $reqstate->{proxyhdl}->push_read(line => sub { | |
1098 | my ($hdl, $line) = @_; | |
5f14e56e | 1099 | |
d8218001 | 1100 | if ($line =~ m!^$proto 200 OK$!) { |
6c31d13e DC |
1101 | # read the empty line after the 200 OK |
1102 | $reqstate->{proxyhdl}->unshift_read(line => sub{ | |
1103 | &$startproxy(); | |
1104 | }); | |
d8218001 DM |
1105 | } else { |
1106 | $reqstate->{hdl}->push_write($line); | |
1107 | $self->client_do_disconnect($reqstate); | |
1108 | } | |
1109 | }); | |
1110 | } else { | |
1111 | &$startproxy(); | |
1112 | } | |
1113 | ||
1114 | }; | |
1115 | }; | |
1116 | if (my $err = $@) { | |
1117 | warn $err; | |
1118 | $self->log_aborted_request($reqstate, $err); | |
1119 | $self->client_do_disconnect($reqstate); | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | sub handle_request { | |
1124 | my ($self, $reqstate, $auth, $method, $path) = @_; | |
1125 | ||
1126 | my $base_uri = $self->{base_uri}; | |
1127 | ||
1128 | eval { | |
1129 | my $r = $reqstate->{request}; | |
5f14e56e | 1130 | |
d8218001 DM |
1131 | # disable timeout on handle (we already have all data we need) |
1132 | # we re-enable timeout in response() | |
1133 | $reqstate->{hdl}->timeout(0); | |
1134 | ||
1135 | if ($path =~ m/^\Q$base_uri\E/) { | |
1136 | $self->handle_api2_request($reqstate, $auth, $method, $path); | |
1137 | return; | |
1138 | } | |
1139 | ||
1140 | if ($self->{pages} && ($method eq 'GET') && (my $handler = $self->{pages}->{$path})) { | |
1141 | if (ref($handler) eq 'CODE') { | |
1142 | my $params = decode_urlencoded($r->url->query()); | |
1143 | my ($resp, $userid) = &$handler($self, $reqstate->{request}, $params); | |
8d88baec DM |
1144 | # HACK: see Note 1 |
1145 | Net::SSLeay::ERR_clear_error(); | |
d8218001 DM |
1146 | $self->response($reqstate, $resp); |
1147 | } elsif (ref($handler) eq 'HASH') { | |
1148 | if (my $filename = $handler->{file}) { | |
1149 | my $fh = IO::File->new($filename) || | |
1150 | die "unable to open file '$filename' - $!\n"; | |
1151 | send_file_start($self, $reqstate, $filename); | |
1152 | } else { | |
1153 | die "internal error - no handler"; | |
1154 | } | |
1155 | } else { | |
1156 | die "internal error - no handler"; | |
1157 | } | |
1158 | return; | |
1159 | } | |
1160 | ||
1161 | if ($self->{dirs} && ($method eq 'GET')) { | |
1162 | # we only allow simple names | |
1163 | if ($path =~ m!^(/\S+/)([a-zA-Z0-9\-\_\.]+)$!) { | |
1164 | my ($subdir, $file) = ($1, $2); | |
1165 | if (my $dir = $self->{dirs}->{$subdir}) { | |
1166 | my $filename = "$dir$file"; | |
1167 | my $fh = IO::File->new($filename) || | |
1168 | die "unable to open file '$filename' - $!\n"; | |
1169 | send_file_start($self, $reqstate, $filename); | |
1170 | return; | |
1171 | } | |
1172 | } | |
1173 | } | |
1174 | ||
666d3ae1 | 1175 | die "no such file '$path'\n"; |
d8218001 DM |
1176 | }; |
1177 | if (my $err = $@) { | |
1178 | $self->error($reqstate, 501, $err); | |
1179 | } | |
1180 | } | |
1181 | ||
59128f6b TL |
1182 | my sub assert_form_disposition { |
1183 | die "wrong Content-Disposition '$_[0]' in multipart, expected 'form-data'\n" if $_[0] ne 'form-data'; | |
1184 | } | |
1185 | ||
d8218001 DM |
1186 | sub file_upload_multipart { |
1187 | my ($self, $reqstate, $auth, $method, $path, $rstate) = @_; | |
1188 | ||
1189 | eval { | |
1190 | my $boundary = $rstate->{boundary}; | |
1191 | my $hdl = $reqstate->{hdl}; | |
d8218001 DM |
1192 | my $startlen = length($hdl->{rbuf}); |
1193 | ||
59128f6b TL |
1194 | my $newline_re = qr/\015?\012/; |
1195 | my $delim_re = qr/--\Q$boundary\E${newline_re}/; | |
44791210 | 1196 | my $close_delim_re = qr/--\Q$boundary\E--/; |
d8218001 | 1197 | |
0fbcbc26 | 1198 | # Phase 0 - preserve boundary, but remove everything before |
59128f6b | 1199 | if ($rstate->{phase} == 0 && $hdl->{rbuf} =~ s/^.*?($delim_re)/$1/s) { |
0fbcbc26 MH |
1200 | $rstate->{read} += $startlen - length($hdl->{rbuf}); |
1201 | $rstate->{phase} = 1; | |
1202 | } | |
d8218001 | 1203 | |
e3295acc MH |
1204 | my $remove_until_data = sub { |
1205 | my ($hdl) = @_; | |
1206 | # remove any remaining multipart "headers" like Content-Type | |
1207 | $hdl->{rbuf} =~ s/^.*?${newline_re}{2}//s; | |
1208 | }; | |
1209 | ||
59128f6b TL |
1210 | my $extract_form_disposition = sub { |
1211 | my ($name) = @_; | |
e3295acc | 1212 | if ($hdl->{rbuf} =~ s/^${delim_re}.*?Content-Disposition: (.*?); name="$name"(.*?${delim_re})/$2/s) { |
59128f6b | 1213 | assert_form_disposition($1); |
e3295acc MH |
1214 | $remove_until_data->($hdl); |
1215 | $hdl->{rbuf} =~ s/^(.*?)(${delim_re})/$2/s; | |
1216 | $rstate->{params}->{$name} = trim($1); | |
d8218001 | 1217 | } |
59128f6b | 1218 | }; |
0fbcbc26 | 1219 | |
59128f6b TL |
1220 | if ($rstate->{phase} == 1) { # Phase 1 - parse payload without file data |
1221 | $extract_form_disposition->('content'); | |
1222 | $extract_form_disposition->('checksum-algorithm'); | |
1223 | $extract_form_disposition->('checksum'); | |
0fbcbc26 | 1224 | |
0b6b3b37 | 1225 | if ($hdl->{rbuf} =~ s/^${delim_re}Content-Disposition: (.*?); name="(.*?)"; filename="([^"]+)"${newline_re}//s) { |
59128f6b | 1226 | assert_form_disposition($1); |
aad755eb | 1227 | die "wrong field name '$2' for file upload, expected 'filename'" if $2 ne "filename"; |
0fbcbc26 | 1228 | $rstate->{phase} = 2; |
a66b77d8 | 1229 | $rstate->{params}->{filename} = trim($3); |
435dbe0c | 1230 | $remove_until_data->($hdl); # any remaining multipart "headers" like Content-Type |
0fbcbc26 MH |
1231 | } |
1232 | } | |
1233 | ||
aad755eb | 1234 | if ($rstate->{phase} == 2) { # Phase 2 - dump content into file |
dafe4416 | 1235 | my ($data, $write_length); |
59128f6b | 1236 | if ($hdl->{rbuf} =~ s/^(.*?)${newline_re}?+${close_delim_re}.*$//s) { |
dafe4416 TL |
1237 | $data = $1; |
1238 | $write_length = length($data); | |
0fbcbc26 | 1239 | $rstate->{phase} = 100; |
d8218001 | 1240 | } else { |
dafe4416 TL |
1241 | $write_length = length($hdl->{rbuf}) - $rstate->{boundlen}; |
1242 | $data = substr($hdl->{rbuf}, 0, $write_length, '') if $write_length > 0; | |
1243 | } | |
1244 | ||
1245 | if ($write_length > 0) { | |
1246 | syswrite($rstate->{outfh}, $data) == $write_length or die "write to temporary file failed - $!\n"; | |
1247 | $rstate->{bytes} += $write_length; | |
1248 | $rstate->{ctx}->add($data); | |
d8218001 | 1249 | } |
0fbcbc26 | 1250 | } |
d8218001 | 1251 | |
aad755eb | 1252 | if ($rstate->{phase} == 100) { # Phase 100 - transfer finished |
dafe4416 | 1253 | $rstate->{md5sum} = $rstate->{ctx}->hexdigest; |
0fbcbc26 | 1254 | my $elapsed = tv_interval($rstate->{starttime}); |
32163b8e | 1255 | syslog('info', "multipart upload complete (size: %dB time: %.3fs rate: %.2fMiB/s md5sum: %s)", |
aad755eb | 1256 | $rstate->{bytes}, $elapsed, $rstate->{bytes} / ($elapsed * 1024 * 1024), $rstate->{md5sum} |
0fbcbc26 MH |
1257 | ); |
1258 | $self->handle_api2_request($reqstate, $auth, $method, $path, $rstate); | |
d8218001 DM |
1259 | } |
1260 | ||
91b86f4e | 1261 | $rstate->{read} += $startlen - length($hdl->{rbuf}); |
d8218001 | 1262 | |
aad755eb | 1263 | if ($rstate->{read} + length($hdl->{rbuf}) >= $rstate->{size} && $rstate->{phase} != 100) { |
0fbcbc26 | 1264 | die "upload failed"; |
d8218001 DM |
1265 | } |
1266 | }; | |
1267 | if (my $err = $@) { | |
1268 | syslog('err', $err); | |
1269 | $self->error($reqstate, 501, $err); | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | sub parse_content_type { | |
1274 | my ($ctype) = @_; | |
1275 | ||
1276 | my ($ct, @params) = split(/\s*[;,]\s*/o, $ctype); | |
5f14e56e | 1277 | |
d8218001 DM |
1278 | foreach my $v (@params) { |
1279 | if ($v =~ m/^\s*boundary\s*=\s*(\S+?)\s*$/o) { | |
1280 | return wantarray ? ($ct, $1) : $ct; | |
1281 | } | |
1282 | } | |
5f14e56e | 1283 | |
d8218001 DM |
1284 | return wantarray ? ($ct) : $ct; |
1285 | } | |
1286 | ||
d8218001 DM |
1287 | my $tmpfile_seq_no = 0; |
1288 | ||
1289 | sub get_upload_filename { | |
1290 | # choose unpredictable tmpfile name | |
5f14e56e | 1291 | |
d8218001 DM |
1292 | $tmpfile_seq_no++; |
1293 | return "/var/tmp/pveupload-" . Digest::MD5::md5_hex($tmpfile_seq_no . time() . $$); | |
1294 | } | |
1295 | ||
1296 | sub unshift_read_header { | |
1297 | my ($self, $reqstate, $state) = @_; | |
1298 | ||
1299 | $state = { size => 0, count => 0 } if !$state; | |
1300 | ||
1301 | $reqstate->{hdl}->unshift_read(line => sub { | |
1302 | my ($hdl, $line) = @_; | |
1303 | ||
1304 | eval { | |
1305 | # print "$$: got header: $line\n" if $self->{debug}; | |
1306 | ||
e7ee5e3f | 1307 | die "too many http header lines (> $limit_max_headers)\n" if ++$state->{count} >= $limit_max_headers; |
d8218001 DM |
1308 | die "http header too large\n" if ($state->{size} += length($line)) >= $limit_max_header_size; |
1309 | ||
1310 | my $r = $reqstate->{request}; | |
1311 | if ($line eq '') { | |
1312 | ||
1313 | my $path = uri_unescape($r->uri->path()); | |
1314 | my $method = $r->method(); | |
1315 | ||
1316 | $r->push_header($state->{key}, $state->{val}) | |
1317 | if $state->{key}; | |
1318 | ||
1319 | if (!$known_methods->{$method}) { | |
1320 | my $resp = HTTP::Response->new(HTTP_NOT_IMPLEMENTED, "method '$method' not available"); | |
1321 | $self->response($reqstate, $resp); | |
1322 | return; | |
1323 | } | |
1324 | ||
1325 | my $conn = $r->header('Connection'); | |
1326 | my $accept_enc = $r->header('Accept-Encoding'); | |
1327 | $reqstate->{accept_gzip} = ($accept_enc && $accept_enc =~ m/gzip/) ? 1 : 0; | |
1328 | ||
1329 | if ($conn) { | |
1330 | $reqstate->{keep_alive} = 0 if $conn =~ m/close/oi; | |
1331 | } else { | |
1332 | if ($reqstate->{proto}->{ver} < 1001) { | |
1333 | $reqstate->{keep_alive} = 0; | |
1334 | } | |
1335 | } | |
1336 | ||
1337 | my $te = $r->header('Transfer-Encoding'); | |
1338 | if ($te && lc($te) eq 'chunked') { | |
1339 | # Handle chunked transfer encoding | |
1340 | $self->error($reqstate, 501, "chunked transfer encoding not supported"); | |
1341 | return; | |
1342 | } elsif ($te) { | |
1343 | $self->error($reqstate, 501, "Unknown transfer encoding '$te'"); | |
1344 | return; | |
1345 | } | |
1346 | ||
1347 | my $pveclientip = $r->header('PVEClientIP'); | |
1348 | my $base_uri = $self->{base_uri}; | |
1349 | ||
1350 | # fixme: how can we make PVEClientIP header trusted? | |
1351 | if ($self->{trusted_env} && $pveclientip) { | |
1352 | $reqstate->{peer_host} = $pveclientip; | |
1353 | } else { | |
1354 | $r->header('PVEClientIP', $reqstate->{peer_host}); | |
1355 | } | |
1356 | ||
1357 | my $len = $r->header('Content-Length'); | |
1358 | ||
87821486 | 1359 | my $host_header = $r->header('Host'); |
e52d9400 TL |
1360 | if (my $rpcenv = $self->{rpcenv}) { |
1361 | $rpcenv->set_request_host($host_header); | |
1362 | } | |
87821486 | 1363 | |
d8218001 DM |
1364 | # header processing complete - authenticate now |
1365 | ||
1366 | my $auth = {}; | |
1367 | if ($self->{spiceproxy}) { | |
87821486 | 1368 | my $connect_str = $host_header; |
403964f2 | 1369 | my ($vmid, $node, $port) = $self->verify_spice_connect_url($connect_str); |
d8218001 DM |
1370 | if (!(defined($vmid) && $node && $port)) { |
1371 | $self->error($reqstate, HTTP_UNAUTHORIZED, "invalid ticket"); | |
1372 | return; | |
1373 | } | |
1374 | $self->handle_spice_proxy_request($reqstate, $connect_str, $vmid, $node, $port); | |
1375 | return; | |
1376 | } elsif ($path =~ m/^\Q$base_uri\E/) { | |
1377 | my $token = $r->header('CSRFPreventionToken'); | |
1378 | my $cookie = $r->header('Cookie'); | |
e045d070 TM |
1379 | my $auth_header = $r->header('Authorization'); |
1380 | ||
1381 | # prefer actual cookie | |
1382 | my $ticket = PVE::APIServer::Formatter::extract_auth_value($cookie, $self->{cookie_name}); | |
1383 | ||
1384 | # fallback to cookie in 'Authorization' header | |
1385 | $ticket = PVE::APIServer::Formatter::extract_auth_value($auth_header, $self->{cookie_name}) | |
1386 | if !$ticket; | |
d8218001 | 1387 | |
362dd8eb FG |
1388 | # finally, fallback to API token if no ticket has been provided so far |
1389 | my $api_token; | |
1390 | $api_token = PVE::APIServer::Formatter::extract_auth_value($auth_header, $self->{apitoken_name}) | |
1391 | if !$ticket; | |
1392 | ||
d8218001 DM |
1393 | my ($rel_uri, $format) = &$split_abs_uri($path, $self->{base_uri}); |
1394 | if (!$format) { | |
1395 | $self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no such uri"); | |
1396 | return; | |
1397 | } | |
1398 | ||
d8218001 | 1399 | eval { |
362dd8eb | 1400 | $auth = $self->auth_handler($method, $rel_uri, $ticket, $token, $api_token, |
58ddb769 | 1401 | $reqstate->{peer_host}); |
d8218001 DM |
1402 | }; |
1403 | if (my $err = $@) { | |
8d88baec | 1404 | # HACK: see Note 1 |
63890886 | 1405 | Net::SSLeay::ERR_clear_error(); |
d8218001 DM |
1406 | # always delay unauthorized calls by 3 seconds |
1407 | my $delay = 3; | |
d8327719 TL |
1408 | |
1409 | if (ref($err) eq "PVE::Exception") { | |
1410 | ||
1411 | $err->{code} ||= HTTP_INTERNAL_SERVER_ERROR, | |
1412 | my $resp = HTTP::Response->new($err->{code}, $err->{msg}); | |
1413 | $self->response($reqstate, $resp, undef, 0, $delay); | |
1414 | ||
1415 | } elsif (my $formatter = PVE::APIServer::Formatter::get_login_formatter($format)) { | |
ca304f91 DM |
1416 | my ($raw, $ct, $nocomp) = |
1417 | $formatter->($path, $auth, $self->{formatter_config}); | |
d8218001 DM |
1418 | my $resp; |
1419 | if (ref($raw) && (ref($raw) eq 'HTTP::Response')) { | |
1420 | $resp = $raw; | |
1421 | } else { | |
1422 | $resp = HTTP::Response->new(HTTP_UNAUTHORIZED, "Login Required"); | |
1423 | $resp->header("Content-Type" => $ct); | |
1424 | $resp->content($raw); | |
1425 | } | |
63890886 | 1426 | $self->response($reqstate, $resp, undef, $nocomp, $delay); |
d8218001 DM |
1427 | } else { |
1428 | my $resp = HTTP::Response->new(HTTP_UNAUTHORIZED, $err); | |
1429 | $self->response($reqstate, $resp, undef, 0, $delay); | |
1430 | } | |
1431 | return; | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | $reqstate->{log}->{userid} = $auth->{userid}; | |
1436 | ||
1437 | if ($len) { | |
1438 | ||
1439 | if (!($method eq 'PUT' || $method eq 'POST')) { | |
1440 | $self->error($reqstate, 501, "Unexpected content for method '$method'"); | |
1441 | return; | |
1442 | } | |
1443 | ||
1444 | my $ctype = $r->header('Content-Type'); | |
5339ae14 | 1445 | my ($ct, $boundary) = $ctype ? parse_content_type($ctype) : (); |
d8218001 DM |
1446 | |
1447 | if ($auth->{isUpload} && !$self->{trusted_env}) { | |
5f14e56e | 1448 | die "upload 'Content-Type '$ctype' not implemented\n" |
d8218001 DM |
1449 | if !($boundary && $ct && ($ct eq 'multipart/form-data')); |
1450 | ||
1451 | die "upload without content length header not supported" if !$len; | |
1452 | ||
1453 | die "upload without content length header not supported" if !$len; | |
1454 | ||
26e73e6b | 1455 | $self->dprint("start upload $path $ct $boundary"); |
d8218001 DM |
1456 | |
1457 | my $tmpfilename = get_upload_filename(); | |
1458 | my $outfh = IO::File->new($tmpfilename, O_RDWR|O_CREAT|O_EXCL, 0600) || | |
1459 | die "unable to create temporary upload file '$tmpfilename'"; | |
1460 | ||
1461 | $reqstate->{keep_alive} = 0; | |
1462 | ||
1463 | my $boundlen = length($boundary) + 8; # \015?\012--$boundary--\015?\012 | |
1464 | ||
1465 | my $state = { | |
1466 | size => $len, | |
1467 | boundary => $boundary, | |
1468 | ctx => Digest::MD5->new, | |
1469 | boundlen => $boundlen, | |
1470 | maxheader => 2048 + $boundlen, # should be large enough | |
1471 | params => decode_urlencoded($r->url->query()), | |
1472 | phase => 0, | |
1473 | read => 0, | |
1474 | post_size => 0, | |
1475 | starttime => [gettimeofday], | |
1476 | outfh => $outfh, | |
1477 | }; | |
1478 | $reqstate->{tmpfilename} = $tmpfilename; | |
baf8c8dc TL |
1479 | $reqstate->{hdl}->on_read(sub { |
1480 | $self->file_upload_multipart($reqstate, $auth, $method, $path, $state); | |
1481 | }); | |
d8218001 DM |
1482 | return; |
1483 | } | |
1484 | ||
1485 | if ($len > $limit_max_post) { | |
1486 | $self->error($reqstate, 501, "for data too large"); | |
1487 | return; | |
1488 | } | |
1489 | ||
3e05f166 | 1490 | if (!$ct || $ct eq 'application/x-www-form-urlencoded' || $ct eq 'application/json') { |
d8218001 DM |
1491 | $reqstate->{hdl}->unshift_read(chunk => $len, sub { |
1492 | my ($hdl, $data) = @_; | |
1493 | $r->content($data); | |
1494 | $self->handle_request($reqstate, $auth, $method, $path); | |
1495 | }); | |
1496 | } else { | |
1497 | $self->error($reqstate, 506, "upload 'Content-Type '$ctype' not implemented"); | |
1498 | } | |
1499 | } else { | |
1500 | $self->handle_request($reqstate, $auth, $method, $path); | |
1501 | } | |
1502 | } elsif ($line =~ /^([^:\s]+)\s*:\s*(.*)/) { | |
1503 | $r->push_header($state->{key}, $state->{val}) if $state->{key}; | |
1504 | ($state->{key}, $state->{val}) = ($1, $2); | |
1505 | $self->unshift_read_header($reqstate, $state); | |
1506 | } elsif ($line =~ /^\s+(.*)/) { | |
1507 | $state->{val} .= " $1"; | |
1508 | $self->unshift_read_header($reqstate, $state); | |
1509 | } else { | |
1510 | $self->error($reqstate, 506, "unable to parse request header"); | |
1511 | } | |
1512 | }; | |
1513 | warn $@ if $@; | |
1514 | }); | |
1515 | }; | |
1516 | ||
1517 | sub push_request_header { | |
1518 | my ($self, $reqstate) = @_; | |
1519 | ||
1520 | eval { | |
1521 | $reqstate->{hdl}->push_read(line => sub { | |
1522 | my ($hdl, $line) = @_; | |
1523 | ||
1524 | eval { | |
1525 | # print "got request header: $line\n" if $self->{debug}; | |
1526 | ||
1527 | $reqstate->{keep_alive}--; | |
1528 | ||
1529 | if ($line =~ /(\S+)\040(\S+)\040HTTP\/(\d+)\.(\d+)/o) { | |
1530 | my ($method, $url, $maj, $min) = ($1, $2, $3, $4); | |
1531 | ||
1532 | if ($maj != 1) { | |
1533 | $self->error($reqstate, 506, "http protocol version $maj.$min not supported"); | |
1534 | return; | |
1535 | } | |
c2bd69c7 TL |
1536 | if ($url =~ m|^[^/]*@|) { |
1537 | # if an '@' comes before the first slash proxy forwarding might consider | |
1538 | # the frist part of the url to be part of an authority... | |
1539 | $self->error($reqstate, 400, "invalid url"); | |
4099febe | 1540 | return; |
c2bd69c7 | 1541 | } |
d8218001 DM |
1542 | |
1543 | $self->{request_count}++; # only count valid request headers | |
1544 | if ($self->{request_count} >= $self->{max_requests}) { | |
1545 | $self->{end_loop} = 1; | |
1546 | } | |
1547 | $reqstate->{log} = { requestline => $line }; | |
1548 | $reqstate->{proto}->{str} = "HTTP/$maj.$min"; | |
1549 | $reqstate->{proto}->{maj} = $maj; | |
1550 | $reqstate->{proto}->{min} = $min; | |
1551 | $reqstate->{proto}->{ver} = $maj*1000+$min; | |
1552 | $reqstate->{request} = HTTP::Request->new($method, $url); | |
1553 | $reqstate->{starttime} = [gettimeofday], | |
1554 | ||
1555 | $self->unshift_read_header($reqstate); | |
1556 | } elsif ($line eq '') { | |
1557 | # ignore empty lines before requests (browser bugs?) | |
1558 | $self->push_request_header($reqstate); | |
1559 | } else { | |
1560 | $self->error($reqstate, 400, 'bad request'); | |
1561 | } | |
1562 | }; | |
1563 | warn $@ if $@; | |
1564 | }); | |
1565 | }; | |
1566 | warn $@ if $@; | |
1567 | } | |
1568 | ||
1569 | sub accept { | |
1570 | my ($self) = @_; | |
1571 | ||
1572 | my $clientfh; | |
1573 | ||
1574 | return if $self->{end_loop}; | |
1575 | ||
1576 | # we need to m make sure that only one process calls accept | |
1577 | while (!flock($self->{lockfh}, Fcntl::LOCK_EX())) { | |
1578 | next if $! == EINTR; | |
1579 | die "could not get lock on file '$self->{lockfile}' - $!\n"; | |
1580 | } | |
1581 | ||
1582 | my $again = 0; | |
1583 | my $errmsg; | |
1584 | eval { | |
1585 | while (!$self->{end_loop} && | |
1586 | !defined($clientfh = $self->{socket}->accept()) && | |
1587 | ($! == EINTR)) {}; | |
1588 | ||
1589 | if ($self->{end_loop}) { | |
1590 | $again = 0; | |
1591 | } else { | |
1592 | $again = ($! == EAGAIN || $! == WSAEWOULDBLOCK); | |
1593 | if (!defined($clientfh)) { | |
1594 | $errmsg = "failed to accept connection: $!\n"; | |
1595 | } | |
1596 | } | |
1597 | }; | |
1598 | warn $@ if $@; | |
1599 | ||
1600 | flock($self->{lockfh}, Fcntl::LOCK_UN()); | |
1601 | ||
1602 | if (!defined($clientfh)) { | |
1603 | return if $again; | |
1604 | die $errmsg if $errmsg; | |
1605 | } | |
1606 | ||
1607 | fh_nonblocking $clientfh, 1; | |
1608 | ||
d8218001 DM |
1609 | return $clientfh; |
1610 | } | |
1611 | ||
1612 | sub wait_end_loop { | |
1613 | my ($self) = @_; | |
1614 | ||
1615 | $self->{end_loop} = 1; | |
1616 | ||
1617 | undef $self->{socket_watch}; | |
1618 | ||
1619 | $0 = "$0 (shutdown)" if $0 !~ m/\(shutdown\)$/; | |
1620 | ||
1621 | if ($self->{conn_count} <= 0) { | |
1622 | $self->{end_cond}->send(1); | |
1623 | return; | |
1624 | } | |
1625 | ||
1626 | # fork and exit, so that parent starts a new worker | |
1627 | if (fork()) { | |
1628 | exit(0); | |
1629 | } | |
1630 | ||
1631 | # else we need to wait until all open connections gets closed | |
1632 | my $w; $w = AnyEvent->timer (after => 1, interval => 1, cb => sub { | |
1633 | eval { | |
1634 | # todo: test for active connections instead (we can abort idle connections) | |
1635 | if ($self->{conn_count} <= 0) { | |
1636 | undef $w; | |
1637 | $self->{end_cond}->send(1); | |
1638 | } | |
1639 | }; | |
1640 | warn $@ if $@; | |
1641 | }); | |
1642 | } | |
1643 | ||
1644 | ||
1645 | sub check_host_access { | |
1646 | my ($self, $clientip) = @_; | |
5f14e56e | 1647 | |
c6de5b3f | 1648 | $clientip = PVE::APIServer::Utils::normalize_v4_in_v6($clientip); |
d8218001 DM |
1649 | my $cip = Net::IP->new($clientip); |
1650 | ||
ebcaaf46 SI |
1651 | if (!$cip) { |
1652 | $self->dprint("client IP not parsable: $@"); | |
1653 | return 0; | |
1654 | } | |
1655 | ||
d8218001 DM |
1656 | my $match_allow = 0; |
1657 | my $match_deny = 0; | |
1658 | ||
1659 | if ($self->{allow_from}) { | |
1660 | foreach my $t (@{$self->{allow_from}}) { | |
1661 | if ($t->overlaps($cip)) { | |
1662 | $match_allow = 1; | |
1e169242 | 1663 | $self->dprint("client IP allowed: ". $t->prefix()); |
d8218001 DM |
1664 | last; |
1665 | } | |
1666 | } | |
1667 | } | |
1668 | ||
1669 | if ($self->{deny_from}) { | |
1670 | foreach my $t (@{$self->{deny_from}}) { | |
1671 | if ($t->overlaps($cip)) { | |
1e169242 | 1672 | $self->dprint("client IP denied: ". $t->prefix()); |
d8218001 DM |
1673 | $match_deny = 1; |
1674 | last; | |
1675 | } | |
1676 | } | |
1677 | } | |
1678 | ||
1679 | if ($match_allow == $match_deny) { | |
1680 | # match both allow and deny, or no match | |
1681 | return $self->{policy} && $self->{policy} eq 'allow' ? 1 : 0; | |
1682 | } | |
1683 | ||
1684 | return $match_allow; | |
1685 | } | |
1686 | ||
1687 | sub accept_connections { | |
1688 | my ($self) = @_; | |
1689 | ||
ebcaaf46 | 1690 | my ($clientfh, $handle_creation); |
d8218001 DM |
1691 | eval { |
1692 | ||
ebcaaf46 | 1693 | while ($clientfh = $self->accept()) { |
d8218001 DM |
1694 | |
1695 | my $reqstate = { keep_alive => $self->{keep_alive} }; | |
1696 | ||
1697 | # stop keep-alive when there are many open connections | |
c82d1707 | 1698 | if ($self->{conn_count} + 1 >= $self->{max_conn_soft_limit}) { |
d8218001 DM |
1699 | $reqstate->{keep_alive} = 0; |
1700 | } | |
1701 | ||
1702 | if (my $sin = getpeername($clientfh)) { | |
1703 | my ($pfamily, $pport, $phost) = PVE::Tools::unpack_sockaddr_in46($sin); | |
1704 | ($reqstate->{peer_port}, $reqstate->{peer_host}) = ($pport, Socket::inet_ntop($pfamily, $phost)); | |
ebcaaf46 | 1705 | } else { |
1e169242 | 1706 | $self->dprint("getpeername failed: $!"); |
ebcaaf46 SI |
1707 | close($clientfh); |
1708 | next; | |
d8218001 DM |
1709 | } |
1710 | ||
1711 | if (!$self->{trusted_env} && !$self->check_host_access($reqstate->{peer_host})) { | |
26e73e6b | 1712 | $self->dprint("ABORT request from $reqstate->{peer_host} - access denied"); |
d8218001 DM |
1713 | $reqstate->{log}->{code} = 403; |
1714 | $self->log_request($reqstate); | |
ebcaaf46 | 1715 | close($clientfh); |
d8218001 DM |
1716 | next; |
1717 | } | |
1718 | ||
c82d1707 SI |
1719 | # Increment conn_count before creating new handle, since creation |
1720 | # triggers callbacks, which can potentialy decrement (e.g. | |
1721 | # on_error) conn_count before AnyEvent::Handle->new() returns. | |
1722 | $handle_creation = 1; | |
1723 | $self->{conn_count}++; | |
d8218001 DM |
1724 | $reqstate->{hdl} = AnyEvent::Handle->new( |
1725 | fh => $clientfh, | |
1726 | rbuf_max => 64*1024, | |
1727 | timeout => $self->{timeout}, | |
1728 | linger => 0, # avoid problems with ssh - really needed ? | |
1729 | on_eof => sub { | |
1730 | my ($hdl) = @_; | |
1731 | eval { | |
1732 | $self->log_aborted_request($reqstate); | |
1733 | $self->client_do_disconnect($reqstate); | |
1734 | }; | |
1735 | if (my $err = $@) { syslog('err', $err); } | |
1736 | }, | |
1737 | on_error => sub { | |
1738 | my ($hdl, $fatal, $message) = @_; | |
1739 | eval { | |
1740 | $self->log_aborted_request($reqstate, $message); | |
1741 | $self->client_do_disconnect($reqstate); | |
1742 | }; | |
1743 | if (my $err = $@) { syslog('err', "$err"); } | |
1744 | }, | |
1745 | ($self->{tls_ctx} ? (tls => "accept", tls_ctx => $self->{tls_ctx}) : ())); | |
c82d1707 | 1746 | $handle_creation = 0; |
d8218001 | 1747 | |
26e73e6b | 1748 | $self->dprint("ACCEPT FH" . $clientfh->fileno() . " CONN$self->{conn_count}"); |
d8218001 DM |
1749 | |
1750 | $self->push_request_header($reqstate); | |
1751 | } | |
1752 | }; | |
1753 | ||
1754 | if (my $err = $@) { | |
1755 | syslog('err', $err); | |
1e169242 | 1756 | $self->dprint("connection accept error: $err"); |
ebcaaf46 | 1757 | close($clientfh); |
c82d1707 SI |
1758 | if ($handle_creation) { |
1759 | if ($self->{conn_count} <= 0) { | |
1760 | warn "connection count <= 0 not decrementing!\n"; | |
1761 | } else { | |
1762 | $self->{conn_count}--; | |
1763 | } | |
1764 | } | |
d8218001 DM |
1765 | $self->{end_loop} = 1; |
1766 | } | |
1767 | ||
1768 | $self->wait_end_loop() if $self->{end_loop}; | |
1769 | } | |
1770 | ||
1771 | # Note: We can't open log file in non-blocking mode and use AnyEvent::Handle, | |
1772 | # because we write from multiple processes, and that would arbitrarily mix output | |
1773 | # of all processes. | |
1774 | sub open_access_log { | |
1775 | my ($self, $filename) = @_; | |
1776 | ||
1777 | my $old_mask = umask(0137);; | |
1778 | my $logfh = IO::File->new($filename, ">>") || | |
1779 | die "unable to open log file '$filename' - $!\n"; | |
1780 | umask($old_mask); | |
1781 | ||
1782 | $logfh->autoflush(1); | |
1783 | ||
1784 | $self->{logfh} = $logfh; | |
1785 | } | |
1786 | ||
1787 | sub write_log { | |
1788 | my ($self, $data) = @_; | |
1789 | ||
1790 | return if !defined($self->{logfh}) || !$data; | |
1791 | ||
1792 | my $res = $self->{logfh}->print($data); | |
1793 | ||
1794 | if (!$res) { | |
1795 | delete $self->{logfh}; | |
1796 | syslog('err', "error writing access log"); | |
1797 | $self->{end_loop} = 1; # terminate asap | |
1798 | } | |
1799 | } | |
1800 | ||
1801 | sub atfork_handler { | |
1802 | my ($self) = @_; | |
1803 | ||
1804 | eval { | |
1805 | # something else do to ? | |
1806 | close($self->{socket}); | |
1807 | }; | |
1808 | warn $@ if $@; | |
1809 | } | |
1810 | ||
fbc42b33 DM |
1811 | sub run { |
1812 | my ($self) = @_; | |
1813 | ||
1814 | $self->{end_cond}->recv; | |
1815 | } | |
1816 | ||
d8218001 DM |
1817 | sub new { |
1818 | my ($this, %args) = @_; | |
1819 | ||
1820 | my $class = ref($this) || $this; | |
1821 | ||
af76fd78 | 1822 | foreach my $req (qw(socket lockfh lockfile)) { |
d8218001 DM |
1823 | die "misssing required argument '$req'" if !defined($args{$req}); |
1824 | } | |
1825 | ||
1826 | my $self = bless { %args }, $class; | |
1827 | ||
1828 | $self->{cookie_name} //= 'PVEAuthCookie'; | |
362dd8eb | 1829 | $self->{apitoken_name} //= 'PVEAPIToken'; |
d8218001 | 1830 | $self->{base_uri} //= "/api2"; |
911ede9b | 1831 | $self->{dirs} //= {}; |
fc87cd5e | 1832 | $self->{title} //= 'API Inspector'; |
23dfd1b7 | 1833 | $self->{compression} //= 1; |
d8218001 | 1834 | |
ca304f91 DM |
1835 | # formatter_config: we pass some configuration values to the Formatter |
1836 | $self->{formatter_config} = {}; | |
362dd8eb | 1837 | foreach my $p (qw(apitoken_name cookie_name base_uri title)) { |
ca304f91 DM |
1838 | $self->{formatter_config}->{$p} = $self->{$p}; |
1839 | } | |
1840 | $self->{formatter_config}->{csrfgen_func} = | |
1841 | $self->can('generate_csrf_prevention_token'); | |
1842 | ||
1843 | # add default dirs which includes jquery and bootstrap | |
e52d9400 TL |
1844 | my $jsbase = '/usr/share/javascript'; |
1845 | add_dirs($self->{dirs}, '/js/' => "$jsbase/"); | |
1846 | # libjs-bootstrap uses symlinks for this, which we do not want to allow.. | |
1847 | my $glyphicons = '/usr/share/fonts/truetype/glyphicons/'; | |
1848 | add_dirs($self->{dirs}, '/js/bootstrap/fonts/' => "$glyphicons"); | |
6edb39f6 | 1849 | |
d8218001 DM |
1850 | # init inotify |
1851 | PVE::INotify::inotify_init(); | |
1852 | ||
d8218001 DM |
1853 | fh_nonblocking($self->{socket}, 1); |
1854 | ||
1855 | $self->{end_loop} = 0; | |
1856 | $self->{conn_count} = 0; | |
1857 | $self->{request_count} = 0; | |
1858 | $self->{timeout} = 5 if !$self->{timeout}; | |
1859 | $self->{keep_alive} = 0 if !defined($self->{keep_alive}); | |
1860 | $self->{max_conn} = 800 if !$self->{max_conn}; | |
1861 | $self->{max_requests} = 8000 if !$self->{max_requests}; | |
1862 | ||
1863 | $self->{policy} = 'allow' if !$self->{policy}; | |
1864 | ||
1865 | $self->{end_cond} = AnyEvent->condvar; | |
1866 | ||
1867 | if ($self->{ssl}) { | |
fac83ab2 SI |
1868 | my $ssl_defaults = { |
1869 | # Note: older versions are considered insecure, for example | |
1870 | # search for "Poodle"-Attack | |
1871 | method => 'any', | |
1872 | sslv2 => 0, | |
1873 | sslv3 => 0, | |
1874 | cipher_list => 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256', | |
1875 | honor_cipher_order => 1, | |
1876 | }; | |
1877 | ||
95fde1f7 FG |
1878 | # workaround until anyevent supports TLS 1.3 ciphersuites directly |
1879 | my $ciphersuites = delete $self->{ssl}->{ciphersuites}; | |
1880 | ||
fac83ab2 SI |
1881 | foreach my $k (keys %$ssl_defaults) { |
1882 | $self->{ssl}->{$k} //= $ssl_defaults->{$k}; | |
1883 | } | |
1884 | ||
1885 | if (!defined($self->{ssl}->{dh_file})) { | |
1886 | $self->{ssl}->{dh} = 'skip2048'; | |
1887 | } | |
1888 | ||
7599c478 TL |
1889 | my $tls_ctx_flags = 0; |
1890 | $tls_ctx_flags |= &Net::SSLeay::OP_NO_COMPRESSION; | |
1891 | $tls_ctx_flags |= &Net::SSLeay::OP_SINGLE_ECDH_USE; | |
1892 | $tls_ctx_flags |= &Net::SSLeay::OP_SINGLE_DH_USE; | |
1893 | $tls_ctx_flags |= &Net::SSLeay::OP_NO_RENEGOTIATION; | |
1894 | if (delete $self->{ssl}->{honor_cipher_order}) { | |
54c0ead9 SI |
1895 | $tls_ctx_flags |= &Net::SSLeay::OP_CIPHER_SERVER_PREFERENCE; |
1896 | } | |
e9022485 FG |
1897 | # workaround until anyevent supports disabling TLS 1.3 directly |
1898 | if (exists($self->{ssl}->{tlsv1_3}) && !$self->{ssl}->{tlsv1_3}) { | |
1899 | $tls_ctx_flags |= &Net::SSLeay::OP_NO_TLSv1_3; | |
1900 | } | |
1901 | ||
54c0ead9 | 1902 | |
d8218001 | 1903 | $self->{tls_ctx} = AnyEvent::TLS->new(%{$self->{ssl}}); |
54c0ead9 | 1904 | Net::SSLeay::CTX_set_options($self->{tls_ctx}->{ctx}, $tls_ctx_flags); |
34f20af2 FG |
1905 | if (defined($ciphersuites)) { |
1906 | warn "Failed to set TLS 1.3 ciphersuites '$ciphersuites'\n" | |
1907 | if !Net::SSLeay::CTX_set_ciphersuites($self->{tls_ctx}->{ctx}, $ciphersuites); | |
1908 | } | |
d8218001 DM |
1909 | } |
1910 | ||
1911 | if ($self->{spiceproxy}) { | |
1912 | $known_methods = { CONNECT => 1 }; | |
1913 | } | |
1914 | ||
1915 | $self->open_access_log($self->{logfile}) if $self->{logfile}; | |
1916 | ||
1917 | $self->{max_conn_soft_limit} = $self->{max_conn} > 100 ? $self->{max_conn} - 20 : $self->{max_conn}; | |
1918 | ||
1919 | $self->{socket_watch} = AnyEvent->io(fh => $self->{socket}, poll => 'r', cb => sub { | |
1920 | eval { | |
1921 | if ($self->{conn_count} >= $self->{max_conn}) { | |
1922 | my $w; $w = AnyEvent->timer (after => 1, interval => 1, cb => sub { | |
1923 | if ($self->{conn_count} < $self->{max_conn}) { | |
1924 | undef $w; | |
1925 | $self->accept_connections(); | |
1926 | } | |
1927 | }); | |
1928 | } else { | |
1929 | $self->accept_connections(); | |
1930 | } | |
1931 | }; | |
1932 | warn $@ if $@; | |
1933 | }); | |
1934 | ||
1935 | $self->{term_watch} = AnyEvent->signal(signal => "TERM", cb => sub { | |
1936 | undef $self->{term_watch}; | |
1937 | $self->wait_end_loop(); | |
1938 | }); | |
1939 | ||
1940 | $self->{quit_watch} = AnyEvent->signal(signal => "QUIT", cb => sub { | |
1941 | undef $self->{quit_watch}; | |
1942 | $self->wait_end_loop(); | |
1943 | }); | |
1944 | ||
1945 | $self->{inotify_poll} = AnyEvent->timer(after => 5, interval => 5, cb => sub { | |
1946 | PVE::INotify::poll(); # read inotify events | |
1947 | }); | |
1948 | ||
1949 | return $self; | |
1950 | } | |
1951 | ||
911ede9b DM |
1952 | # static helper to add directory including all subdirs |
1953 | # This can be used to setup $self->{dirs} | |
1954 | sub add_dirs { | |
1955 | my ($result_hash, $alias, $subdir) = @_; | |
1956 | ||
1957 | $result_hash->{$alias} = $subdir; | |
1958 | ||
1959 | my $wanted = sub { | |
1960 | my $dir = $File::Find::dir; | |
1961 | if ($dir =~m!^$subdir(.*)$!) { | |
1962 | my $name = "$alias$1/"; | |
1963 | $result_hash->{$name} = "$dir/"; | |
1964 | } | |
1965 | }; | |
1966 | ||
1967 | find({wanted => $wanted, follow => 0, no_chdir => 1}, $subdir); | |
1968 | } | |
1969 | ||
fbc42b33 DM |
1970 | # abstract functions - subclass should overwrite/implement them |
1971 | ||
403964f2 DM |
1972 | sub verify_spice_connect_url { |
1973 | my ($self, $connect_str) = @_; | |
1974 | ||
1975 | die "implement me"; | |
1976 | ||
1977 | #return ($vmid, $node, $port); | |
1978 | } | |
1979 | ||
a3bb6070 DM |
1980 | # formatters can call this when the generate a new page |
1981 | sub generate_csrf_prevention_token { | |
1982 | my ($username) = @_; | |
1983 | ||
1984 | return undef; # do nothing by default | |
1985 | } | |
1986 | ||
d8218001 | 1987 | sub auth_handler { |
362dd8eb | 1988 | my ($self, $method, $rel_uri, $ticket, $token, $api_token, $peer_host) = @_; |
d8218001 | 1989 | |
b639f458 | 1990 | die "implement me"; |
d8218001 | 1991 | |
b639f458 DM |
1992 | # return { |
1993 | # ticket => $ticket, | |
1994 | # token => $token, | |
1995 | # userid => $username, | |
1996 | # age => $age, | |
1997 | # isUpload => $isUpload, | |
362dd8eb | 1998 | # api_token => $api_token, |
b639f458 | 1999 | #}; |
d8218001 DM |
2000 | } |
2001 | ||
d8218001 | 2002 | sub rest_handler { |
66a7a3a3 | 2003 | my ($self, $clientip, $method, $rel_uri, $auth, $params, $format) = @_; |
d8218001 | 2004 | |
c5f0a96f DM |
2005 | # please do not raise exceptions here (always return a result). |
2006 | ||
b639f458 DM |
2007 | return { |
2008 | status => HTTP_NOT_IMPLEMENTED, | |
2009 | message => "Method '$method $rel_uri' not implemented", | |
d8218001 | 2010 | }; |
c5f0a96f DM |
2011 | |
2012 | # this should return the following properties, which | |
2013 | # are then passed to the Formatter | |
2014 | ||
2015 | # status: HTTP status code | |
2016 | # message: Error message | |
2017 | # errors: more detailed error hash (per parameter) | |
2018 | # info: reference to JSON schema definition - useful to format output | |
2019 | # data: result data | |
2020 | ||
2021 | # total: additional info passed to output | |
2022 | # changes: additional info passed to output | |
2023 | ||
2024 | # if you want to proxy the request to another node return this | |
2025 | # { proxy => $remip, proxynode => $node, proxy_params => $params }; | |
2026 | ||
2027 | # to pass the request to the local priviledged daemon use: | |
2028 | # { proxy => 'localhost' , proxy_params => $params }; | |
20a61215 DM |
2029 | |
2030 | # to download aspecific file use: | |
2031 | # { download => "/path/to/file" }; | |
d8218001 DM |
2032 | } |
2033 | ||
2034 | sub check_cert_fingerprint { | |
2035 | my ($self, $cert) = @_; | |
2036 | ||
b639f458 DM |
2037 | die "implement me"; |
2038 | } | |
d8218001 DM |
2039 | |
2040 | sub initialize_cert_cache { | |
2041 | my ($self, $node) = @_; | |
2042 | ||
b639f458 | 2043 | die "implement me"; |
d8218001 DM |
2044 | } |
2045 | ||
2046 | sub remote_node_ip { | |
2047 | my ($self, $node) = @_; | |
2048 | ||
b639f458 | 2049 | die "implement me"; |
d8218001 | 2050 | |
b639f458 | 2051 | # return $remip; |
d8218001 DM |
2052 | } |
2053 | ||
d8218001 DM |
2054 | |
2055 | 1; |