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