]>
Commit | Line | Data |
---|---|---|
d08808bc | 1 | package PVE::APIServer::AnyEvent; |
d8218001 DM |
2 | |
3 | use strict; | |
4 | use warnings; | |
5 | use Time::HiRes qw(usleep ualarm gettimeofday tv_interval); | |
6 | use Socket qw(IPPROTO_TCP TCP_NODELAY SOMAXCONN); | |
7 | use POSIX qw(strftime EINTR EAGAIN); | |
8 | use Fcntl; | |
9 | use IO::File; | |
10 | use File::stat qw(); | |
11 | use MIME::Base64; | |
12 | use Digest::MD5; | |
13 | use Digest::SHA; | |
14 | # use AnyEvent::Strict; # only use this for debugging | |
15 | use AnyEvent::Util qw(guard fh_nonblocking WSAEWOULDBLOCK WSAEINPROGRESS); | |
16 | use AnyEvent::Socket; | |
17 | use AnyEvent::Handle; | |
18 | use Net::SSLeay; | |
19 | use AnyEvent::TLS; | |
20 | use AnyEvent::IO; | |
21 | use AnyEvent::HTTP; | |
22 | use Fcntl (); | |
23 | use Compress::Zlib; | |
24 | use PVE::SafeSyslog; | |
25 | use PVE::INotify; | |
26 | use PVE::Tools; | |
63307beb | 27 | use PVE::APIServer::Formatter; |
d8218001 DM |
28 | |
29 | use Net::IP; | |
30 | use URI; | |
31 | use URI::Escape; | |
32 | use HTTP::Status qw(:constants); | |
33 | use HTTP::Date; | |
34 | use HTTP::Headers; | |
35 | use HTTP::Request; | |
36 | use HTTP::Response; | |
37 | use Data::Dumper; | |
38 | ||
39 | my $limit_max_headers = 30; | |
40 | my $limit_max_header_size = 8*1024; | |
41 | my $limit_max_post = 16*1024; | |
42 | ||
d8218001 DM |
43 | my $known_methods = { |
44 | GET => 1, | |
45 | POST => 1, | |
46 | PUT => 1, | |
47 | DELETE => 1, | |
48 | }; | |
49 | ||
50 | my $split_abs_uri = sub { | |
51 | my ($abs_uri, $base_uri) = @_; | |
52 | ||
53 | my ($format, $rel_uri) = $abs_uri =~ m/^\Q$base_uri\E\/+([a-z][a-z0-9]+)(\/.*)?$/; | |
54 | $rel_uri = '/' if !$rel_uri; | |
55 | ||
56 | return wantarray ? ($rel_uri, $format) : $rel_uri; | |
57 | }; | |
58 | ||
d8218001 DM |
59 | sub log_request { |
60 | my ($self, $reqstate) = @_; | |
61 | ||
62 | my $loginfo = $reqstate->{log}; | |
63 | ||
64 | # like apache2 common log format | |
65 | # LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" | |
66 | ||
67 | return if $loginfo->{written}; # avoid duplicate logs | |
68 | $loginfo->{written} = 1; | |
69 | ||
70 | my $peerip = $reqstate->{peer_host} || '-'; | |
71 | my $userid = $loginfo->{userid} || '-'; | |
72 | my $content_length = defined($loginfo->{content_length}) ? $loginfo->{content_length} : '-'; | |
73 | my $code = $loginfo->{code} || 500; | |
74 | my $requestline = $loginfo->{requestline} || '-'; | |
75 | my $timestr = strftime("%d/%b/%Y:%H:%M:%S %z", localtime()); | |
76 | ||
77 | my $msg = "$peerip - $userid [$timestr] \"$requestline\" $code $content_length\n"; | |
78 | ||
79 | $self->write_log($msg); | |
80 | } | |
81 | ||
82 | sub log_aborted_request { | |
83 | my ($self, $reqstate, $error) = @_; | |
84 | ||
85 | my $r = $reqstate->{request}; | |
86 | return if !$r; # no active request | |
87 | ||
88 | if ($error) { | |
89 | syslog("err", "problem with client $reqstate->{peer_host}; $error"); | |
90 | } | |
91 | ||
92 | $self->log_request($reqstate); | |
93 | } | |
94 | ||
d8218001 DM |
95 | sub cleanup_reqstate { |
96 | my ($reqstate) = @_; | |
97 | ||
98 | delete $reqstate->{log}; | |
99 | delete $reqstate->{request}; | |
100 | delete $reqstate->{proto}; | |
101 | delete $reqstate->{accept_gzip}; | |
102 | delete $reqstate->{starttime}; | |
103 | ||
104 | if ($reqstate->{tmpfilename}) { | |
105 | unlink $reqstate->{tmpfilename}; | |
106 | delete $reqstate->{tmpfilename}; | |
107 | } | |
108 | } | |
109 | ||
110 | sub client_do_disconnect { | |
111 | my ($self, $reqstate) = @_; | |
112 | ||
113 | cleanup_reqstate($reqstate); | |
114 | ||
115 | my $shutdown_hdl = sub { | |
116 | my $hdl = shift; | |
117 | ||
118 | shutdown($hdl->{fh}, 1); | |
119 | # clear all handlers | |
120 | $hdl->on_drain(undef); | |
121 | $hdl->on_read(undef); | |
122 | $hdl->on_eof(undef); | |
123 | }; | |
124 | ||
125 | if (my $proxyhdl = delete $reqstate->{proxyhdl}) { | |
126 | &$shutdown_hdl($proxyhdl); | |
127 | } | |
128 | ||
129 | my $hdl = delete $reqstate->{hdl}; | |
130 | ||
131 | if (!$hdl) { | |
132 | syslog('err', "detected empty handle"); | |
133 | return; | |
134 | } | |
135 | ||
136 | print "close connection $hdl\n" if $self->{debug}; | |
137 | ||
138 | &$shutdown_hdl($hdl); | |
139 | ||
140 | $self->{conn_count}--; | |
141 | ||
142 | print "$$: CLOSE FH" . $hdl->{fh}->fileno() . " CONN$self->{conn_count}\n" if $self->{debug}; | |
143 | } | |
144 | ||
145 | sub finish_response { | |
146 | my ($self, $reqstate) = @_; | |
147 | ||
148 | my $hdl = $reqstate->{hdl}; | |
149 | ||
150 | cleanup_reqstate($reqstate); | |
151 | ||
152 | if (!$self->{end_loop} && $reqstate->{keep_alive} > 0) { | |
153 | # print "KEEPALIVE $reqstate->{keep_alive}\n" if $self->{debug}; | |
154 | $hdl->on_read(sub { | |
155 | eval { $self->push_request_header($reqstate); }; | |
156 | warn $@ if $@; | |
157 | }); | |
158 | } else { | |
159 | $hdl->on_drain (sub { | |
160 | eval { | |
161 | $self->client_do_disconnect($reqstate); | |
162 | }; | |
163 | warn $@ if $@; | |
164 | }); | |
165 | } | |
166 | } | |
167 | ||
168 | sub response { | |
169 | my ($self, $reqstate, $resp, $mtime, $nocomp, $delay) = @_; | |
170 | ||
171 | #print "$$: send response: " . Dumper($resp); | |
172 | ||
173 | # activate timeout | |
174 | $reqstate->{hdl}->timeout_reset(); | |
175 | $reqstate->{hdl}->timeout($self->{timeout}); | |
176 | ||
177 | $nocomp = 1 if !$reqstate->{accept_gzip}; | |
178 | ||
179 | my $code = $resp->code; | |
180 | my $msg = $resp->message || HTTP::Status::status_message($code); | |
181 | ($msg) = $msg =~m/^(.*)$/m; | |
182 | my $content = $resp->content; | |
183 | ||
184 | if ($code =~ /^(1\d\d|[23]04)$/) { | |
185 | # make sure content we have no content | |
186 | $content = ""; | |
187 | } | |
188 | ||
189 | $reqstate->{keep_alive} = 0 if ($code >= 400) || $self->{end_loop}; | |
190 | ||
191 | $reqstate->{log}->{code} = $code; | |
192 | ||
193 | my $proto = $reqstate->{proto} ? $reqstate->{proto}->{str} : 'HTTP/1.0'; | |
194 | my $res = "$proto $code $msg\015\012"; | |
195 | ||
196 | my $ctime = time(); | |
197 | my $date = HTTP::Date::time2str($ctime); | |
198 | $resp->header('Date' => $date); | |
199 | if ($mtime) { | |
200 | $resp->header('Last-Modified' => HTTP::Date::time2str($mtime)); | |
201 | } else { | |
202 | $resp->header('Expires' => $date); | |
203 | $resp->header('Cache-Control' => "max-age=0"); | |
204 | $resp->header("Pragma", "no-cache"); | |
205 | } | |
206 | ||
207 | $resp->header('Server' => "pve-api-daemon/3.0"); | |
208 | ||
209 | my $content_length; | |
210 | if ($content) { | |
211 | ||
212 | $content_length = length($content); | |
213 | ||
214 | if (!$nocomp && ($content_length > 1024)) { | |
215 | my $comp = Compress::Zlib::memGzip($content); | |
216 | $resp->header('Content-Encoding', 'gzip'); | |
217 | $content = $comp; | |
218 | $content_length = length($content); | |
219 | } | |
220 | $resp->header("Content-Length" => $content_length); | |
221 | $reqstate->{log}->{content_length} = $content_length; | |
222 | ||
223 | } else { | |
224 | $resp->remove_header("Content-Length"); | |
225 | } | |
226 | ||
227 | if ($reqstate->{keep_alive} > 0) { | |
228 | $resp->push_header('Connection' => 'Keep-Alive'); | |
229 | } else { | |
230 | $resp->header('Connection' => 'close'); | |
231 | } | |
232 | ||
233 | $res .= $resp->headers_as_string("\015\012"); | |
234 | #print "SEND(without content) $res\n" if $self->{debug}; | |
235 | ||
236 | $res .= "\015\012"; | |
237 | $res .= $content if $content; | |
238 | ||
239 | $self->log_request($reqstate, $reqstate->{request}); | |
240 | ||
241 | if ($delay && $delay > 0) { | |
242 | my $w; $w = AnyEvent->timer(after => $delay, cb => sub { | |
243 | undef $w; # delete reference | |
244 | $reqstate->{hdl}->push_write($res); | |
245 | $self->finish_response($reqstate); | |
246 | }); | |
247 | } else { | |
248 | $reqstate->{hdl}->push_write($res); | |
249 | $self->finish_response($reqstate); | |
250 | } | |
251 | } | |
252 | ||
253 | sub error { | |
254 | my ($self, $reqstate, $code, $msg, $hdr, $content) = @_; | |
255 | ||
256 | eval { | |
257 | my $resp = HTTP::Response->new($code, $msg, $hdr, $content); | |
258 | $self->response($reqstate, $resp); | |
259 | }; | |
260 | warn $@ if $@; | |
261 | } | |
262 | ||
263 | my $file_extension_info = { | |
264 | css => { ct => 'text/css' }, | |
265 | html => { ct => 'text/html' }, | |
266 | js => { ct => 'application/javascript' }, | |
267 | png => { ct => 'image/png' , nocomp => 1 }, | |
268 | ico => { ct => 'image/x-icon', nocomp => 1}, | |
269 | gif => { ct => 'image/gif', nocomp => 1}, | |
270 | jar => { ct => 'application/java-archive', nocomp => 1}, | |
271 | woff => { ct => 'application/font-woff', nocomp => 1}, | |
272 | woff2 => { ct => 'application/font-woff2', nocomp => 1}, | |
273 | ttf => { ct => 'application/font-snft', nocomp => 1}, | |
274 | pdf => { ct => 'application/pdf', nocomp => 1}, | |
275 | epub => { ct => 'application/epub+zip', nocomp => 1}, | |
276 | }; | |
277 | ||
278 | sub send_file_start { | |
279 | my ($self, $reqstate, $filename) = @_; | |
280 | ||
281 | eval { | |
282 | # print "SEND FILE $filename\n"; | |
283 | # Note: aio_load() this is not really async unless we use IO::AIO! | |
284 | eval { | |
285 | ||
286 | my $r = $reqstate->{request}; | |
287 | ||
288 | my $fh = IO::File->new($filename, '<') || | |
289 | die "$!\n"; | |
290 | my $stat = File::stat::stat($fh) || | |
291 | die "$!\n"; | |
292 | ||
293 | my $mtime = $stat->mtime; | |
294 | ||
295 | if (my $ifmod = $r->header('if-modified-since')) { | |
296 | my $iftime = HTTP::Date::str2time($ifmod); | |
297 | if ($mtime <= $iftime) { | |
298 | my $resp = HTTP::Response->new(304, "NOT MODIFIED"); | |
299 | $self->response($reqstate, $resp, $mtime); | |
300 | return; | |
301 | } | |
302 | } | |
303 | ||
304 | my $data; | |
305 | my $len = sysread($fh, $data, $stat->size); | |
306 | die "got short file\n" if !defined($len) || $len != $stat->size; | |
307 | ||
308 | my ($ext) = $filename =~ m/\.([^.]*)$/; | |
309 | my $ext_info = $file_extension_info->{$ext}; | |
310 | ||
311 | die "unable to detect content type" if !$ext_info; | |
312 | ||
313 | my $header = HTTP::Headers->new(Content_Type => $ext_info->{ct}); | |
314 | my $resp = HTTP::Response->new(200, "OK", $header, $data); | |
315 | $self->response($reqstate, $resp, $mtime, $ext_info->{nocomp}); | |
316 | }; | |
317 | if (my $err = $@) { | |
318 | $self->error($reqstate, 501, $err); | |
319 | } | |
320 | }; | |
321 | ||
322 | warn $@ if $@; | |
323 | } | |
324 | ||
325 | sub websocket_proxy { | |
326 | my ($self, $reqstate, $wsaccept, $wsproto, $param) = @_; | |
327 | ||
328 | eval { | |
329 | my $remhost; | |
330 | my $remport; | |
331 | ||
332 | my $max_payload_size = 65536; | |
333 | ||
334 | my $binary; | |
335 | if ($wsproto eq 'binary') { | |
336 | $binary = 1; | |
337 | } elsif ($wsproto eq 'base64') { | |
338 | $binary = 0; | |
339 | } else { | |
340 | die "websocket_proxy: unsupported protocol '$wsproto'\n"; | |
341 | } | |
342 | ||
343 | if ($param->{port}) { | |
344 | $remhost = 'localhost'; | |
345 | $remport = $param->{port}; | |
346 | } elsif ($param->{socket}) { | |
347 | $remhost = 'unix/'; | |
348 | $remport = $param->{socket}; | |
349 | } else { | |
350 | die "websocket_proxy: missing port or socket\n"; | |
351 | } | |
352 | ||
353 | tcp_connect $remhost, $remport, sub { | |
354 | my ($fh) = @_ | |
355 | or die "connect to '$remhost:$remport' failed: $!"; | |
356 | ||
357 | print "$$: CONNECTed to '$remhost:$remport'\n" if $self->{debug}; | |
358 | ||
359 | $reqstate->{proxyhdl} = AnyEvent::Handle->new( | |
360 | fh => $fh, | |
361 | rbuf_max => 64*1024, | |
362 | wbuf_max => 64*10*1024, | |
363 | timeout => 5, | |
364 | on_eof => sub { | |
365 | my ($hdl) = @_; | |
366 | eval { | |
367 | $self->log_aborted_request($reqstate); | |
368 | $self->client_do_disconnect($reqstate); | |
369 | }; | |
370 | if (my $err = $@) { syslog('err', $err); } | |
371 | }, | |
372 | on_error => sub { | |
373 | my ($hdl, $fatal, $message) = @_; | |
374 | eval { | |
375 | $self->log_aborted_request($reqstate, $message); | |
376 | $self->client_do_disconnect($reqstate); | |
377 | }; | |
378 | if (my $err = $@) { syslog('err', "$err"); } | |
379 | }); | |
380 | ||
381 | my $proxyhdlreader = sub { | |
382 | my ($hdl) = @_; | |
383 | ||
384 | my $len = length($hdl->{rbuf}); | |
385 | my $data = substr($hdl->{rbuf}, 0, $len, ''); | |
386 | ||
387 | my $string; | |
388 | my $payload; | |
389 | ||
390 | if ($binary) { | |
391 | $string = "\x82"; # binary frame | |
392 | $payload = $data; | |
393 | } else { | |
394 | $string = "\x81"; # text frame | |
395 | $payload = encode_base64($data, ''); | |
396 | } | |
397 | ||
398 | my $payload_len = length($payload); | |
399 | if ($payload_len <= 125) { | |
400 | $string .= pack 'C', $payload_len; | |
401 | } elsif ($payload_len <= 0xffff) { | |
402 | $string .= pack 'C', 126; | |
403 | $string .= pack 'n', $payload_len; | |
404 | } else { | |
405 | $string .= pack 'C', 127; | |
406 | $string .= pack 'Q>', $payload_len; | |
407 | } | |
408 | $string .= $payload; | |
409 | ||
410 | $reqstate->{hdl}->push_write($string) if $reqstate->{hdl}; | |
411 | }; | |
412 | ||
413 | my $hdlreader = sub { | |
414 | my ($hdl) = @_; | |
415 | ||
416 | my $len = length($hdl->{rbuf}); | |
417 | return if $len < 2; | |
418 | ||
419 | my $hdr = unpack('C', substr($hdl->{rbuf}, 0, 1)); | |
420 | my $opcode = $hdr & 0b00001111; | |
421 | my $fin = $hdr & 0b10000000; | |
422 | ||
423 | die "received fragmented websocket frame\n" if !$fin; | |
424 | ||
425 | my $rsv = $hdr & 0b01110000; | |
426 | die "received websocket frame with RSV flags\n" if $rsv; | |
427 | ||
428 | my $payload_len = unpack 'C', substr($hdl->{rbuf}, 1, 1); | |
429 | ||
430 | my $masked = $payload_len & 0b10000000; | |
431 | die "received unmasked websocket frame from client\n" if !$masked; | |
432 | ||
433 | my $offset = 2; | |
434 | $payload_len = $payload_len & 0b01111111; | |
435 | if ($payload_len == 126) { | |
436 | return if $len < 4; | |
437 | $payload_len = unpack('n', substr($hdl->{rbuf}, $offset, 2)); | |
438 | $offset += 2; | |
439 | } elsif ($payload_len == 127) { | |
440 | return if $len < 10; | |
441 | $payload_len = unpack('Q>', substr($hdl->{rbuf}, $offset, 8)); | |
442 | $offset += 8; | |
443 | } | |
444 | ||
445 | die "received too large websocket frame (len = $payload_len)\n" | |
446 | if ($payload_len > $max_payload_size) || ($payload_len < 0); | |
447 | ||
448 | return if $len < ($offset + 4 + $payload_len); | |
449 | ||
450 | my $data = substr($hdl->{rbuf}, 0, $len, ''); # now consume data | |
451 | ||
452 | my @mask = (unpack('C', substr($data, $offset+0, 1)), | |
453 | unpack('C', substr($data, $offset+1, 1)), | |
454 | unpack('C', substr($data, $offset+2, 1)), | |
455 | unpack('C', substr($data, $offset+3, 1))); | |
456 | ||
457 | $offset += 4; | |
458 | ||
459 | my $payload = substr($data, $offset, $payload_len); | |
460 | ||
461 | for (my $i = 0; $i < $payload_len; $i++) { | |
462 | my $d = unpack('C', substr($payload, $i, 1)); | |
463 | my $n = $d ^ $mask[$i % 4]; | |
464 | substr($payload, $i, 1, pack('C', $n)); | |
465 | } | |
466 | ||
467 | $payload = decode_base64($payload) if !$binary; | |
468 | ||
469 | if ($opcode == 1 || $opcode == 2) { | |
470 | $reqstate->{proxyhdl}->push_write($payload) if $reqstate->{proxyhdl}; | |
471 | } elsif ($opcode == 8) { | |
472 | print "websocket received close\n" if $self->{debug}; | |
473 | if ($reqstate->{proxyhdl}) { | |
474 | $reqstate->{proxyhdl}->push_write($payload); | |
475 | $reqstate->{proxyhdl}->push_shutdown(); | |
476 | } | |
477 | $hdl->push_shutdown(); | |
478 | } else { | |
479 | die "received unhandled websocket opcode $opcode\n"; | |
480 | } | |
481 | }; | |
482 | ||
483 | my $proto = $reqstate->{proto} ? $reqstate->{proto}->{str} : 'HTTP/1.1'; | |
484 | ||
485 | $reqstate->{proxyhdl}->timeout(0); | |
486 | $reqstate->{proxyhdl}->on_read($proxyhdlreader); | |
487 | $reqstate->{hdl}->on_read($hdlreader); | |
488 | ||
489 | # todo: use stop_read/start_read if write buffer grows to much | |
490 | ||
491 | my $res = "$proto 101 Switching Protocols\015\012" . | |
492 | "Upgrade: websocket\015\012" . | |
493 | "Connection: upgrade\015\012" . | |
494 | "Sec-WebSocket-Accept: $wsaccept\015\012" . | |
495 | "Sec-WebSocket-Protocol: $wsproto\015\012" . | |
496 | "\015\012"; | |
497 | ||
498 | print $res if $self->{debug}; | |
499 | ||
500 | $reqstate->{hdl}->push_write($res); | |
501 | ||
502 | # log early | |
503 | $reqstate->{log}->{code} = 101; | |
504 | $self->log_request($reqstate); | |
505 | }; | |
506 | ||
507 | }; | |
508 | if (my $err = $@) { | |
509 | warn $err; | |
510 | $self->log_aborted_request($reqstate, $err); | |
511 | $self->client_do_disconnect($reqstate); | |
512 | } | |
513 | } | |
514 | ||
515 | sub proxy_request { | |
516 | my ($self, $reqstate, $clientip, $host, $node, $method, $uri, $ticket, $token, $params) = @_; | |
517 | ||
518 | eval { | |
519 | my $target; | |
520 | my $keep_alive = 1; | |
521 | if ($host eq 'localhost') { | |
522 | $target = "http://$host:85$uri"; | |
523 | # keep alive for localhost is not worth (connection setup is about 0.2ms) | |
524 | $keep_alive = 0; | |
525 | } elsif (Net::IP::ip_is_ipv6($host)) { | |
526 | $target = "https://[$host]:8006$uri"; | |
527 | } else { | |
528 | $target = "https://$host:8006$uri"; | |
529 | } | |
530 | ||
531 | my $headers = { | |
532 | PVEDisableProxy => 'true', | |
533 | PVEClientIP => $clientip, | |
534 | }; | |
535 | ||
63307beb | 536 | $headers->{'cookie'} = PVE::APIServer::Formatter::create_auth_cookie($ticket, $self->{cookie_name}) if $ticket; |
d8218001 DM |
537 | $headers->{'CSRFPreventionToken'} = $token if $token; |
538 | $headers->{'Accept-Encoding'} = 'gzip' if $reqstate->{accept_gzip}; | |
539 | ||
540 | my $content; | |
541 | ||
542 | if ($method eq 'POST' || $method eq 'PUT') { | |
543 | $headers->{'Content-Type'} = 'application/x-www-form-urlencoded'; | |
544 | # use URI object to format application/x-www-form-urlencoded content. | |
545 | my $url = URI->new('http:'); | |
546 | $url->query_form(%$params); | |
547 | $content = $url->query; | |
548 | if (defined($content)) { | |
549 | $headers->{'Content-Length'} = length($content); | |
550 | } | |
551 | } | |
552 | ||
553 | my $tls = { | |
554 | # TLS 1.x only, with certificate pinning | |
555 | method => 'any', | |
556 | sslv2 => 0, | |
557 | sslv3 => 0, | |
558 | verify => 1, | |
559 | verify_cb => sub { | |
560 | my (undef, undef, undef, $depth, undef, undef, $cert) = @_; | |
561 | # we don't care about intermediate or root certificates | |
562 | return 1 if $depth != 0; | |
563 | # check server certificate against cache of pinned FPs | |
564 | return $self->check_cert_fingerprint($cert); | |
565 | }, | |
566 | }; | |
567 | ||
568 | # load and cache cert fingerprint if first time we proxy to this node | |
569 | $self->initialize_cert_cache($node); | |
570 | ||
571 | my $w; $w = http_request( | |
572 | $method => $target, | |
573 | headers => $headers, | |
574 | timeout => 30, | |
575 | recurse => 0, | |
576 | proxy => undef, # avoid use of $ENV{HTTP_PROXY} | |
577 | keepalive => $keep_alive, | |
578 | body => $content, | |
579 | tls_ctx => AnyEvent::TLS->new(%{$tls}), | |
580 | sub { | |
581 | my ($body, $hdr) = @_; | |
582 | ||
583 | undef $w; | |
584 | ||
585 | if (!$reqstate->{hdl}) { | |
586 | warn "proxy detected vanished client connection\n"; | |
587 | return; | |
588 | } | |
589 | ||
590 | eval { | |
591 | my $code = delete $hdr->{Status}; | |
592 | my $msg = delete $hdr->{Reason}; | |
593 | delete $hdr->{URL}; | |
594 | delete $hdr->{HTTPVersion}; | |
595 | my $header = HTTP::Headers->new(%$hdr); | |
596 | if (my $location = $header->header('Location')) { | |
597 | $location =~ s|^http://localhost:85||; | |
598 | $header->header(Location => $location); | |
599 | } | |
600 | my $resp = HTTP::Response->new($code, $msg, $header, $body); | |
601 | # Note: disable compression, because body is already compressed | |
602 | $self->response($reqstate, $resp, undef, 1); | |
603 | }; | |
604 | warn $@ if $@; | |
605 | }); | |
606 | }; | |
607 | warn $@ if $@; | |
608 | } | |
609 | ||
610 | # return arrays as \0 separated strings (like CGI.pm) | |
611 | sub decode_urlencoded { | |
612 | my ($data) = @_; | |
613 | ||
614 | my $res = {}; | |
615 | ||
616 | return $res if !$data; | |
617 | ||
618 | foreach my $kv (split(/[\&\;]/, $data)) { | |
619 | my ($k, $v) = split(/=/, $kv); | |
620 | $k =~s/\+/ /g; | |
621 | $k =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg; | |
622 | $v =~s/\+/ /g; | |
623 | $v =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg; | |
624 | ||
625 | if (defined(my $old = $res->{$k})) { | |
626 | $res->{$k} = "$old\0$v"; | |
627 | } else { | |
628 | $res->{$k} = $v; | |
629 | } | |
630 | } | |
631 | return $res; | |
632 | } | |
633 | ||
634 | sub extract_params { | |
635 | my ($r, $method) = @_; | |
636 | ||
637 | my $params = {}; | |
638 | ||
639 | if ($method eq 'PUT' || $method eq 'POST') { | |
640 | $params = decode_urlencoded($r->content); | |
641 | } | |
642 | ||
643 | my $query_params = decode_urlencoded($r->url->query()); | |
644 | ||
645 | foreach my $k (keys %{$query_params}) { | |
646 | $params->{$k} = $query_params->{$k}; | |
647 | } | |
648 | ||
649 | return PVE::Tools::decode_utf8_parameters($params); | |
650 | } | |
651 | ||
652 | sub handle_api2_request { | |
653 | my ($self, $reqstate, $auth, $method, $path, $upload_state) = @_; | |
654 | ||
655 | eval { | |
656 | my $r = $reqstate->{request}; | |
657 | ||
658 | my ($rel_uri, $format) = &$split_abs_uri($path, $self->{base_uri}); | |
659 | ||
63307beb | 660 | my $formatter = PVE::APIServer::Formatter::get_formatter($format); |
d8218001 DM |
661 | |
662 | if (!defined($formatter)) { | |
663 | $self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no such uri $rel_uri, $format"); | |
664 | return; | |
665 | } | |
666 | ||
667 | #print Dumper($upload_state) if $upload_state; | |
668 | ||
669 | my $rpcenv = $self->{rpcenv}; | |
670 | ||
671 | my $params; | |
672 | ||
673 | if ($upload_state) { | |
674 | $params = $upload_state->{params}; | |
675 | } else { | |
676 | $params = extract_params($r, $method); | |
677 | } | |
678 | ||
679 | delete $params->{_dc}; # remove disable cache parameter | |
680 | ||
681 | my $clientip = $reqstate->{peer_host}; | |
682 | ||
683 | $rpcenv->init_request(); | |
684 | ||
685 | my $res = $self->rest_handler($clientip, $method, $rel_uri, $auth, $params); | |
686 | ||
687 | AnyEvent->now_update(); # in case somebody called sleep() | |
688 | ||
689 | $rpcenv->set_user(undef); # clear after request | |
690 | ||
691 | my $upgrade = $r->header('upgrade'); | |
692 | $upgrade = lc($upgrade) if $upgrade; | |
693 | ||
694 | if (my $host = $res->{proxy}) { | |
695 | ||
696 | if ($self->{trusted_env}) { | |
697 | $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy not allowed"); | |
698 | return; | |
699 | } | |
700 | ||
701 | if ($host ne 'localhost' && $r->header('PVEDisableProxy')) { | |
702 | $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy loop detected"); | |
703 | return; | |
704 | } | |
705 | ||
706 | $res->{proxy_params}->{tmpfilename} = $reqstate->{tmpfilename} if $upload_state; | |
707 | ||
708 | $self->proxy_request($reqstate, $clientip, $host, $res->{proxynode}, $method, | |
709 | $r->uri, $auth->{ticket}, $auth->{token}, $res->{proxy_params}, $res->{proxynode}); | |
710 | return; | |
711 | ||
712 | } elsif ($upgrade && ($method eq 'GET') && ($path =~ m|websocket$|)) { | |
713 | die "unable to upgrade to protocol '$upgrade'\n" if !$upgrade || ($upgrade ne 'websocket'); | |
714 | my $wsver = $r->header('sec-websocket-version'); | |
715 | die "unsupported websocket-version '$wsver'\n" if !$wsver || ($wsver ne '13'); | |
716 | my $wsproto_str = $r->header('sec-websocket-protocol'); | |
717 | die "missing websocket-protocol header" if !$wsproto_str; | |
718 | my $wsproto; | |
719 | foreach my $p (PVE::Tools::split_list($wsproto_str)) { | |
720 | $wsproto = $p if !$wsproto && $p eq 'base64'; | |
721 | $wsproto = $p if $p eq 'binary'; | |
722 | } | |
723 | die "unsupported websocket-protocol protocol '$wsproto_str'\n" if !$wsproto; | |
724 | my $wskey = $r->header('sec-websocket-key'); | |
725 | die "missing websocket-key\n" if !$wskey; | |
726 | # Note: Digest::SHA::sha1_base64 has wrong padding | |
727 | my $wsaccept = Digest::SHA::sha1_base64("${wskey}258EAFA5-E914-47DA-95CA-C5AB0DC85B11") . "="; | |
728 | if ($res->{status} == HTTP_OK) { | |
729 | $self->websocket_proxy($reqstate, $wsaccept, $wsproto, $res->{data}); | |
730 | return; | |
731 | } | |
732 | } | |
733 | ||
734 | my $delay = 0; | |
735 | if ($res->{status} == HTTP_UNAUTHORIZED) { | |
736 | # always delay unauthorized calls by 3 seconds | |
737 | $delay = 3 - tv_interval($reqstate->{starttime}); | |
738 | $delay = 0 if $delay < 0; | |
739 | } | |
740 | ||
741 | if ($res->{info} && $res->{info}->{formatter}) { | |
742 | if (defined(my $func = $res->{info}->{formatter}->{$format})) { | |
743 | $formatter = $func; | |
744 | } | |
745 | } | |
746 | ||
747 | my ($raw, $ct, $nocomp) = &$formatter($res, $res->{data}, $params, $path, $auth); | |
748 | ||
749 | my $resp; | |
750 | if (ref($raw) && (ref($raw) eq 'HTTP::Response')) { | |
751 | $resp = $raw; | |
752 | } else { | |
753 | $resp = HTTP::Response->new($res->{status}, $res->{message}); | |
754 | $resp->header("Content-Type" => $ct); | |
755 | $resp->content($raw); | |
756 | } | |
757 | $self->response($reqstate, $resp, undef, $nocomp, $delay); | |
758 | }; | |
759 | if (my $err = $@) { | |
760 | $self->error($reqstate, 501, $err); | |
761 | } | |
762 | } | |
763 | ||
764 | sub handle_spice_proxy_request { | |
765 | my ($self, $reqstate, $connect_str, $vmid, $node, $spiceport) = @_; | |
766 | ||
767 | eval { | |
768 | ||
769 | die "Port $spiceport is not allowed" if ($spiceport < 61000 || $spiceport > 61099); | |
770 | ||
771 | my $rpcenv = $self->{rpcenv}; | |
772 | $rpcenv->init_request(); | |
773 | ||
774 | my $clientip = $reqstate->{peer_host}; | |
775 | my $r = $reqstate->{request}; | |
776 | ||
777 | my $remip; | |
778 | ||
779 | if ($node ne 'localhost' && PVE::INotify::nodename() !~ m/^$node$/i) { | |
780 | $remip = $self->remote_node_ip($node); | |
781 | print "REMOTE CONNECT $vmid, $remip, $connect_str\n" if $self->{debug}; | |
782 | } else { | |
783 | print "$$: CONNECT $vmid, $node, $spiceport\n" if $self->{debug}; | |
784 | } | |
785 | ||
786 | if ($remip && $r->header('PVEDisableProxy')) { | |
787 | $self->error($reqstate, HTTP_INTERNAL_SERVER_ERROR, "proxy loop detected"); | |
788 | return; | |
789 | } | |
790 | ||
791 | $reqstate->{hdl}->timeout(0); | |
792 | $reqstate->{hdl}->wbuf_max(64*10*1024); | |
793 | ||
794 | my $remhost = $remip ? $remip : "localhost"; | |
795 | my $remport = $remip ? 3128 : $spiceport; | |
796 | ||
797 | tcp_connect $remhost, $remport, sub { | |
798 | my ($fh) = @_ | |
799 | or die "connect to '$remhost:$remport' failed: $!"; | |
800 | ||
801 | print "$$: CONNECTed to '$remhost:$remport'\n" if $self->{debug}; | |
802 | $reqstate->{proxyhdl} = AnyEvent::Handle->new( | |
803 | fh => $fh, | |
804 | rbuf_max => 64*1024, | |
805 | wbuf_max => 64*10*1024, | |
806 | timeout => 5, | |
807 | on_eof => sub { | |
808 | my ($hdl) = @_; | |
809 | eval { | |
810 | $self->log_aborted_request($reqstate); | |
811 | $self->client_do_disconnect($reqstate); | |
812 | }; | |
813 | if (my $err = $@) { syslog('err', $err); } | |
814 | }, | |
815 | on_error => sub { | |
816 | my ($hdl, $fatal, $message) = @_; | |
817 | eval { | |
818 | $self->log_aborted_request($reqstate, $message); | |
819 | $self->client_do_disconnect($reqstate); | |
820 | }; | |
821 | if (my $err = $@) { syslog('err', "$err"); } | |
822 | }); | |
823 | ||
824 | ||
825 | my $proxyhdlreader = sub { | |
826 | my ($hdl) = @_; | |
827 | ||
828 | my $len = length($hdl->{rbuf}); | |
829 | my $data = substr($hdl->{rbuf}, 0, $len, ''); | |
830 | ||
831 | #print "READ1 $len\n"; | |
832 | $reqstate->{hdl}->push_write($data) if $reqstate->{hdl}; | |
833 | }; | |
834 | ||
835 | my $hdlreader = sub { | |
836 | my ($hdl) = @_; | |
837 | ||
838 | my $len = length($hdl->{rbuf}); | |
839 | my $data = substr($hdl->{rbuf}, 0, $len, ''); | |
840 | ||
841 | #print "READ0 $len\n"; | |
842 | $reqstate->{proxyhdl}->push_write($data) if $reqstate->{proxyhdl}; | |
843 | }; | |
844 | ||
845 | my $proto = $reqstate->{proto} ? $reqstate->{proto}->{str} : 'HTTP/1.0'; | |
846 | ||
847 | my $startproxy = sub { | |
848 | $reqstate->{proxyhdl}->timeout(0); | |
849 | $reqstate->{proxyhdl}->on_read($proxyhdlreader); | |
850 | $reqstate->{hdl}->on_read($hdlreader); | |
851 | ||
852 | # todo: use stop_read/start_read if write buffer grows to much | |
853 | ||
854 | my $res = "$proto 200 OK\015\012"; # hope this is the right answer? | |
855 | $reqstate->{hdl}->push_write($res); | |
856 | ||
857 | # log early | |
858 | $reqstate->{log}->{code} = 200; | |
859 | $self->log_request($reqstate); | |
860 | }; | |
861 | ||
862 | if ($remip) { | |
863 | my $header = "CONNECT ${connect_str} $proto\015\012" . | |
864 | "Host: ${connect_str}\015\012" . | |
865 | "Proxy-Connection: keep-alive\015\012" . | |
866 | "User-Agent: spiceproxy\015\012" . | |
867 | "PVEDisableProxy: true\015\012" . | |
868 | "PVEClientIP: $clientip\015\012" . | |
869 | "\015\012"; | |
870 | ||
871 | $reqstate->{proxyhdl}->push_write($header); | |
872 | $reqstate->{proxyhdl}->push_read(line => sub { | |
873 | my ($hdl, $line) = @_; | |
874 | ||
875 | if ($line =~ m!^$proto 200 OK$!) { | |
876 | &$startproxy(); | |
877 | } else { | |
878 | $reqstate->{hdl}->push_write($line); | |
879 | $self->client_do_disconnect($reqstate); | |
880 | } | |
881 | }); | |
882 | } else { | |
883 | &$startproxy(); | |
884 | } | |
885 | ||
886 | }; | |
887 | }; | |
888 | if (my $err = $@) { | |
889 | warn $err; | |
890 | $self->log_aborted_request($reqstate, $err); | |
891 | $self->client_do_disconnect($reqstate); | |
892 | } | |
893 | } | |
894 | ||
895 | sub handle_request { | |
896 | my ($self, $reqstate, $auth, $method, $path) = @_; | |
897 | ||
898 | my $base_uri = $self->{base_uri}; | |
899 | ||
900 | eval { | |
901 | my $r = $reqstate->{request}; | |
902 | ||
903 | # disable timeout on handle (we already have all data we need) | |
904 | # we re-enable timeout in response() | |
905 | $reqstate->{hdl}->timeout(0); | |
906 | ||
907 | if ($path =~ m/^\Q$base_uri\E/) { | |
908 | $self->handle_api2_request($reqstate, $auth, $method, $path); | |
909 | return; | |
910 | } | |
911 | ||
912 | if ($self->{pages} && ($method eq 'GET') && (my $handler = $self->{pages}->{$path})) { | |
913 | if (ref($handler) eq 'CODE') { | |
914 | my $params = decode_urlencoded($r->url->query()); | |
915 | my ($resp, $userid) = &$handler($self, $reqstate->{request}, $params); | |
916 | $self->response($reqstate, $resp); | |
917 | } elsif (ref($handler) eq 'HASH') { | |
918 | if (my $filename = $handler->{file}) { | |
919 | my $fh = IO::File->new($filename) || | |
920 | die "unable to open file '$filename' - $!\n"; | |
921 | send_file_start($self, $reqstate, $filename); | |
922 | } else { | |
923 | die "internal error - no handler"; | |
924 | } | |
925 | } else { | |
926 | die "internal error - no handler"; | |
927 | } | |
928 | return; | |
929 | } | |
930 | ||
931 | if ($self->{dirs} && ($method eq 'GET')) { | |
932 | # we only allow simple names | |
933 | if ($path =~ m!^(/\S+/)([a-zA-Z0-9\-\_\.]+)$!) { | |
934 | my ($subdir, $file) = ($1, $2); | |
935 | if (my $dir = $self->{dirs}->{$subdir}) { | |
936 | my $filename = "$dir$file"; | |
937 | my $fh = IO::File->new($filename) || | |
938 | die "unable to open file '$filename' - $!\n"; | |
939 | send_file_start($self, $reqstate, $filename); | |
940 | return; | |
941 | } | |
942 | } | |
943 | } | |
944 | ||
945 | die "no such file '$path'"; | |
946 | }; | |
947 | if (my $err = $@) { | |
948 | $self->error($reqstate, 501, $err); | |
949 | } | |
950 | } | |
951 | ||
952 | sub file_upload_multipart { | |
953 | my ($self, $reqstate, $auth, $method, $path, $rstate) = @_; | |
954 | ||
955 | eval { | |
956 | my $boundary = $rstate->{boundary}; | |
957 | my $hdl = $reqstate->{hdl}; | |
958 | ||
959 | my $startlen = length($hdl->{rbuf}); | |
960 | ||
961 | if ($rstate->{phase} == 0) { # skip everything until start | |
962 | if ($hdl->{rbuf} =~ s/^.*?--\Q$boundary\E \015?\012 | |
963 | ((?:[^\015]+\015\012)* ) \015?\012//xs) { | |
964 | my $header = $1; | |
965 | my ($ct, $disp, $name, $filename); | |
966 | foreach my $line (split(/\015?\012/, $header)) { | |
967 | # assume we have single line headers | |
968 | if ($line =~ m/^Content-Type\s*:\s*(.*)/i) { | |
969 | $ct = parse_content_type($1); | |
970 | } elsif ($line =~ m/^Content-Disposition\s*:\s*(.*)/i) { | |
971 | ($disp, $name, $filename) = parse_content_disposition($1); | |
972 | } | |
973 | } | |
974 | ||
975 | if (!($disp && $disp eq 'form-data' && $name)) { | |
976 | syslog('err', "wrong content disposition in multipart - abort upload"); | |
977 | $rstate->{phase} = -1; | |
978 | } else { | |
979 | ||
980 | $rstate->{fieldname} = $name; | |
981 | ||
982 | if ($filename) { | |
983 | if ($name eq 'filename') { | |
984 | # found file upload data | |
985 | $rstate->{phase} = 1; | |
986 | $rstate->{filename} = $filename; | |
987 | } else { | |
988 | syslog('err', "wrong field name for file upload - abort upload"); | |
989 | $rstate->{phase} = -1; | |
990 | } | |
991 | } else { | |
992 | # found form data for field $name | |
993 | $rstate->{phase} = 2; | |
994 | } | |
995 | } | |
996 | } else { | |
997 | my $len = length($hdl->{rbuf}); | |
998 | substr($hdl->{rbuf}, 0, $len - $rstate->{maxheader}, '') | |
999 | if $len > $rstate->{maxheader}; # skip garbage | |
1000 | } | |
1001 | } elsif ($rstate->{phase} == 1) { # inside file - dump until end marker | |
1002 | if ($hdl->{rbuf} =~ s/^(.*?)\015?\012(--\Q$boundary\E(--)? \015?\012(.*))$/$2/xs) { | |
1003 | my ($rest, $eof) = ($1, $3); | |
1004 | my $len = length($rest); | |
1005 | die "write to temporary file failed - $!" | |
1006 | if syswrite($rstate->{outfh}, $rest) != $len; | |
1007 | $rstate->{ctx}->add($rest); | |
1008 | $rstate->{params}->{filename} = $rstate->{filename}; | |
1009 | $rstate->{md5sum} = $rstate->{ctx}->hexdigest; | |
1010 | $rstate->{bytes} += $len; | |
1011 | $rstate->{phase} = $eof ? 100 : 0; | |
1012 | } else { | |
1013 | my $len = length($hdl->{rbuf}); | |
1014 | my $wlen = $len - $rstate->{boundlen}; | |
1015 | if ($wlen > 0) { | |
1016 | my $data = substr($hdl->{rbuf}, 0, $wlen, ''); | |
1017 | die "write to temporary file failed - $!" | |
1018 | if syswrite($rstate->{outfh}, $data) != $wlen; | |
1019 | $rstate->{bytes} += $wlen; | |
1020 | $rstate->{ctx}->add($data); | |
1021 | } | |
1022 | } | |
1023 | } elsif ($rstate->{phase} == 2) { # inside normal field | |
1024 | ||
1025 | if ($hdl->{rbuf} =~ s/^(.*?)\015?\012(--\Q$boundary\E(--)? \015?\012(.*))$/$2/xs) { | |
1026 | my ($rest, $eof) = ($1, $3); | |
1027 | my $len = length($rest); | |
1028 | $rstate->{post_size} += $len; | |
1029 | if ($rstate->{post_size} < $limit_max_post) { | |
1030 | $rstate->{params}->{$rstate->{fieldname}} = $rest; | |
1031 | $rstate->{phase} = $eof ? 100 : 0; | |
1032 | } else { | |
1033 | syslog('err', "form data to large - abort upload"); | |
1034 | $rstate->{phase} = -1; # skip | |
1035 | } | |
1036 | } | |
1037 | } else { # skip | |
1038 | my $len = length($hdl->{rbuf}); | |
1039 | substr($hdl->{rbuf}, 0, $len, ''); # empty rbuf | |
1040 | } | |
1041 | ||
1042 | $rstate->{read} += ($startlen - length($hdl->{rbuf})); | |
1043 | ||
1044 | if (!$rstate->{done} && ($rstate->{read} + length($hdl->{rbuf})) >= $rstate->{size}) { | |
1045 | $rstate->{done} = 1; # make sure we dont get called twice | |
1046 | if ($rstate->{phase} < 0 || !$rstate->{md5sum}) { | |
1047 | die "upload failed\n"; | |
1048 | } else { | |
1049 | my $elapsed = tv_interval($rstate->{starttime}); | |
1050 | ||
1051 | my $rate = int($rstate->{bytes}/($elapsed*1024*1024)); | |
1052 | syslog('info', "multipart upload complete " . | |
1053 | "(size: %d time: %ds rate: %.2fMiB/s md5sum: $rstate->{md5sum})", | |
1054 | $rstate->{bytes}, $elapsed, $rate); | |
1055 | $self->handle_api2_request($reqstate, $auth, $method, $path, $rstate); | |
1056 | } | |
1057 | } | |
1058 | }; | |
1059 | if (my $err = $@) { | |
1060 | syslog('err', $err); | |
1061 | $self->error($reqstate, 501, $err); | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | sub parse_content_type { | |
1066 | my ($ctype) = @_; | |
1067 | ||
1068 | my ($ct, @params) = split(/\s*[;,]\s*/o, $ctype); | |
1069 | ||
1070 | foreach my $v (@params) { | |
1071 | if ($v =~ m/^\s*boundary\s*=\s*(\S+?)\s*$/o) { | |
1072 | return wantarray ? ($ct, $1) : $ct; | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | return wantarray ? ($ct) : $ct; | |
1077 | } | |
1078 | ||
1079 | sub parse_content_disposition { | |
1080 | my ($line) = @_; | |
1081 | ||
1082 | my ($disp, @params) = split(/\s*[;,]\s*/o, $line); | |
1083 | my $name; | |
1084 | my $filename; | |
1085 | ||
1086 | foreach my $v (@params) { | |
1087 | if ($v =~ m/^\s*name\s*=\s*(\S+?)\s*$/o) { | |
1088 | $name = $1; | |
1089 | $name =~ s/^"(.*)"$/$1/; | |
1090 | } elsif ($v =~ m/^\s*filename\s*=\s*(.+?)\s*$/o) { | |
1091 | $filename = $1; | |
1092 | $filename =~ s/^"(.*)"$/$1/; | |
1093 | } | |
1094 | } | |
1095 | ||
1096 | return wantarray ? ($disp, $name, $filename) : $disp; | |
1097 | } | |
1098 | ||
1099 | my $tmpfile_seq_no = 0; | |
1100 | ||
1101 | sub get_upload_filename { | |
1102 | # choose unpredictable tmpfile name | |
1103 | ||
1104 | $tmpfile_seq_no++; | |
1105 | return "/var/tmp/pveupload-" . Digest::MD5::md5_hex($tmpfile_seq_no . time() . $$); | |
1106 | } | |
1107 | ||
1108 | sub unshift_read_header { | |
1109 | my ($self, $reqstate, $state) = @_; | |
1110 | ||
1111 | $state = { size => 0, count => 0 } if !$state; | |
1112 | ||
1113 | $reqstate->{hdl}->unshift_read(line => sub { | |
1114 | my ($hdl, $line) = @_; | |
1115 | ||
1116 | eval { | |
1117 | # print "$$: got header: $line\n" if $self->{debug}; | |
1118 | ||
1119 | die "to many http header lines\n" if ++$state->{count} >= $limit_max_headers; | |
1120 | die "http header too large\n" if ($state->{size} += length($line)) >= $limit_max_header_size; | |
1121 | ||
1122 | my $r = $reqstate->{request}; | |
1123 | if ($line eq '') { | |
1124 | ||
1125 | my $path = uri_unescape($r->uri->path()); | |
1126 | my $method = $r->method(); | |
1127 | ||
1128 | $r->push_header($state->{key}, $state->{val}) | |
1129 | if $state->{key}; | |
1130 | ||
1131 | if (!$known_methods->{$method}) { | |
1132 | my $resp = HTTP::Response->new(HTTP_NOT_IMPLEMENTED, "method '$method' not available"); | |
1133 | $self->response($reqstate, $resp); | |
1134 | return; | |
1135 | } | |
1136 | ||
1137 | my $conn = $r->header('Connection'); | |
1138 | my $accept_enc = $r->header('Accept-Encoding'); | |
1139 | $reqstate->{accept_gzip} = ($accept_enc && $accept_enc =~ m/gzip/) ? 1 : 0; | |
1140 | ||
1141 | if ($conn) { | |
1142 | $reqstate->{keep_alive} = 0 if $conn =~ m/close/oi; | |
1143 | } else { | |
1144 | if ($reqstate->{proto}->{ver} < 1001) { | |
1145 | $reqstate->{keep_alive} = 0; | |
1146 | } | |
1147 | } | |
1148 | ||
1149 | my $te = $r->header('Transfer-Encoding'); | |
1150 | if ($te && lc($te) eq 'chunked') { | |
1151 | # Handle chunked transfer encoding | |
1152 | $self->error($reqstate, 501, "chunked transfer encoding not supported"); | |
1153 | return; | |
1154 | } elsif ($te) { | |
1155 | $self->error($reqstate, 501, "Unknown transfer encoding '$te'"); | |
1156 | return; | |
1157 | } | |
1158 | ||
1159 | my $pveclientip = $r->header('PVEClientIP'); | |
1160 | my $base_uri = $self->{base_uri}; | |
1161 | ||
1162 | # fixme: how can we make PVEClientIP header trusted? | |
1163 | if ($self->{trusted_env} && $pveclientip) { | |
1164 | $reqstate->{peer_host} = $pveclientip; | |
1165 | } else { | |
1166 | $r->header('PVEClientIP', $reqstate->{peer_host}); | |
1167 | } | |
1168 | ||
1169 | my $len = $r->header('Content-Length'); | |
1170 | ||
1171 | # header processing complete - authenticate now | |
1172 | ||
1173 | my $auth = {}; | |
1174 | if ($self->{spiceproxy}) { | |
1175 | my $connect_str = $r->header('Host'); | |
403964f2 | 1176 | my ($vmid, $node, $port) = $self->verify_spice_connect_url($connect_str); |
d8218001 DM |
1177 | if (!(defined($vmid) && $node && $port)) { |
1178 | $self->error($reqstate, HTTP_UNAUTHORIZED, "invalid ticket"); | |
1179 | return; | |
1180 | } | |
1181 | $self->handle_spice_proxy_request($reqstate, $connect_str, $vmid, $node, $port); | |
1182 | return; | |
1183 | } elsif ($path =~ m/^\Q$base_uri\E/) { | |
1184 | my $token = $r->header('CSRFPreventionToken'); | |
1185 | my $cookie = $r->header('Cookie'); | |
63307beb | 1186 | my $ticket = PVE::APIServer::Formatter::extract_auth_cookie($cookie, $self->{cookie_name}); |
d8218001 DM |
1187 | |
1188 | my ($rel_uri, $format) = &$split_abs_uri($path, $self->{base_uri}); | |
1189 | if (!$format) { | |
1190 | $self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no such uri"); | |
1191 | return; | |
1192 | } | |
1193 | ||
1194 | my $rpcenv = $self->{rpcenv}; | |
1195 | # set environment variables | |
1196 | $rpcenv->set_user(undef); | |
1197 | $rpcenv->set_language('C'); | |
1198 | $rpcenv->set_client_ip($reqstate->{peer_host}); | |
1199 | ||
1200 | eval { | |
1201 | $auth = $self->auth_handler($method, $rel_uri, $ticket, $token); | |
1202 | }; | |
1203 | if (my $err = $@) { | |
1204 | # always delay unauthorized calls by 3 seconds | |
1205 | my $delay = 3; | |
63307beb | 1206 | if (my $formatter = PVE::APIServer::Formatter::get_login_formatter($format)) { |
d8218001 DM |
1207 | my ($raw, $ct, $nocomp) = &$formatter($path, $auth); |
1208 | my $resp; | |
1209 | if (ref($raw) && (ref($raw) eq 'HTTP::Response')) { | |
1210 | $resp = $raw; | |
1211 | } else { | |
1212 | $resp = HTTP::Response->new(HTTP_UNAUTHORIZED, "Login Required"); | |
1213 | $resp->header("Content-Type" => $ct); | |
1214 | $resp->content($raw); | |
1215 | } | |
1216 | $self->response($reqstate, $resp, undef, $nocomp, 3); | |
1217 | } else { | |
1218 | my $resp = HTTP::Response->new(HTTP_UNAUTHORIZED, $err); | |
1219 | $self->response($reqstate, $resp, undef, 0, $delay); | |
1220 | } | |
1221 | return; | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | $reqstate->{log}->{userid} = $auth->{userid}; | |
1226 | ||
1227 | if ($len) { | |
1228 | ||
1229 | if (!($method eq 'PUT' || $method eq 'POST')) { | |
1230 | $self->error($reqstate, 501, "Unexpected content for method '$method'"); | |
1231 | return; | |
1232 | } | |
1233 | ||
1234 | my $ctype = $r->header('Content-Type'); | |
1235 | my ($ct, $boundary) = parse_content_type($ctype) if $ctype; | |
1236 | ||
1237 | if ($auth->{isUpload} && !$self->{trusted_env}) { | |
1238 | die "upload 'Content-Type '$ctype' not implemented\n" | |
1239 | if !($boundary && $ct && ($ct eq 'multipart/form-data')); | |
1240 | ||
1241 | die "upload without content length header not supported" if !$len; | |
1242 | ||
1243 | die "upload without content length header not supported" if !$len; | |
1244 | ||
1245 | print "start upload $path $ct $boundary\n" if $self->{debug}; | |
1246 | ||
1247 | my $tmpfilename = get_upload_filename(); | |
1248 | my $outfh = IO::File->new($tmpfilename, O_RDWR|O_CREAT|O_EXCL, 0600) || | |
1249 | die "unable to create temporary upload file '$tmpfilename'"; | |
1250 | ||
1251 | $reqstate->{keep_alive} = 0; | |
1252 | ||
1253 | my $boundlen = length($boundary) + 8; # \015?\012--$boundary--\015?\012 | |
1254 | ||
1255 | my $state = { | |
1256 | size => $len, | |
1257 | boundary => $boundary, | |
1258 | ctx => Digest::MD5->new, | |
1259 | boundlen => $boundlen, | |
1260 | maxheader => 2048 + $boundlen, # should be large enough | |
1261 | params => decode_urlencoded($r->url->query()), | |
1262 | phase => 0, | |
1263 | read => 0, | |
1264 | post_size => 0, | |
1265 | starttime => [gettimeofday], | |
1266 | outfh => $outfh, | |
1267 | }; | |
1268 | $reqstate->{tmpfilename} = $tmpfilename; | |
1269 | $reqstate->{hdl}->on_read(sub { $self->file_upload_multipart($reqstate, $auth, $method, $path, $state); }); | |
1270 | return; | |
1271 | } | |
1272 | ||
1273 | if ($len > $limit_max_post) { | |
1274 | $self->error($reqstate, 501, "for data too large"); | |
1275 | return; | |
1276 | } | |
1277 | ||
1278 | if (!$ct || $ct eq 'application/x-www-form-urlencoded') { | |
1279 | $reqstate->{hdl}->unshift_read(chunk => $len, sub { | |
1280 | my ($hdl, $data) = @_; | |
1281 | $r->content($data); | |
1282 | $self->handle_request($reqstate, $auth, $method, $path); | |
1283 | }); | |
1284 | } else { | |
1285 | $self->error($reqstate, 506, "upload 'Content-Type '$ctype' not implemented"); | |
1286 | } | |
1287 | } else { | |
1288 | $self->handle_request($reqstate, $auth, $method, $path); | |
1289 | } | |
1290 | } elsif ($line =~ /^([^:\s]+)\s*:\s*(.*)/) { | |
1291 | $r->push_header($state->{key}, $state->{val}) if $state->{key}; | |
1292 | ($state->{key}, $state->{val}) = ($1, $2); | |
1293 | $self->unshift_read_header($reqstate, $state); | |
1294 | } elsif ($line =~ /^\s+(.*)/) { | |
1295 | $state->{val} .= " $1"; | |
1296 | $self->unshift_read_header($reqstate, $state); | |
1297 | } else { | |
1298 | $self->error($reqstate, 506, "unable to parse request header"); | |
1299 | } | |
1300 | }; | |
1301 | warn $@ if $@; | |
1302 | }); | |
1303 | }; | |
1304 | ||
1305 | sub push_request_header { | |
1306 | my ($self, $reqstate) = @_; | |
1307 | ||
1308 | eval { | |
1309 | $reqstate->{hdl}->push_read(line => sub { | |
1310 | my ($hdl, $line) = @_; | |
1311 | ||
1312 | eval { | |
1313 | # print "got request header: $line\n" if $self->{debug}; | |
1314 | ||
1315 | $reqstate->{keep_alive}--; | |
1316 | ||
1317 | if ($line =~ /(\S+)\040(\S+)\040HTTP\/(\d+)\.(\d+)/o) { | |
1318 | my ($method, $url, $maj, $min) = ($1, $2, $3, $4); | |
1319 | ||
1320 | if ($maj != 1) { | |
1321 | $self->error($reqstate, 506, "http protocol version $maj.$min not supported"); | |
1322 | return; | |
1323 | } | |
1324 | ||
1325 | $self->{request_count}++; # only count valid request headers | |
1326 | if ($self->{request_count} >= $self->{max_requests}) { | |
1327 | $self->{end_loop} = 1; | |
1328 | } | |
1329 | $reqstate->{log} = { requestline => $line }; | |
1330 | $reqstate->{proto}->{str} = "HTTP/$maj.$min"; | |
1331 | $reqstate->{proto}->{maj} = $maj; | |
1332 | $reqstate->{proto}->{min} = $min; | |
1333 | $reqstate->{proto}->{ver} = $maj*1000+$min; | |
1334 | $reqstate->{request} = HTTP::Request->new($method, $url); | |
1335 | $reqstate->{starttime} = [gettimeofday], | |
1336 | ||
1337 | $self->unshift_read_header($reqstate); | |
1338 | } elsif ($line eq '') { | |
1339 | # ignore empty lines before requests (browser bugs?) | |
1340 | $self->push_request_header($reqstate); | |
1341 | } else { | |
1342 | $self->error($reqstate, 400, 'bad request'); | |
1343 | } | |
1344 | }; | |
1345 | warn $@ if $@; | |
1346 | }); | |
1347 | }; | |
1348 | warn $@ if $@; | |
1349 | } | |
1350 | ||
1351 | sub accept { | |
1352 | my ($self) = @_; | |
1353 | ||
1354 | my $clientfh; | |
1355 | ||
1356 | return if $self->{end_loop}; | |
1357 | ||
1358 | # we need to m make sure that only one process calls accept | |
1359 | while (!flock($self->{lockfh}, Fcntl::LOCK_EX())) { | |
1360 | next if $! == EINTR; | |
1361 | die "could not get lock on file '$self->{lockfile}' - $!\n"; | |
1362 | } | |
1363 | ||
1364 | my $again = 0; | |
1365 | my $errmsg; | |
1366 | eval { | |
1367 | while (!$self->{end_loop} && | |
1368 | !defined($clientfh = $self->{socket}->accept()) && | |
1369 | ($! == EINTR)) {}; | |
1370 | ||
1371 | if ($self->{end_loop}) { | |
1372 | $again = 0; | |
1373 | } else { | |
1374 | $again = ($! == EAGAIN || $! == WSAEWOULDBLOCK); | |
1375 | if (!defined($clientfh)) { | |
1376 | $errmsg = "failed to accept connection: $!\n"; | |
1377 | } | |
1378 | } | |
1379 | }; | |
1380 | warn $@ if $@; | |
1381 | ||
1382 | flock($self->{lockfh}, Fcntl::LOCK_UN()); | |
1383 | ||
1384 | if (!defined($clientfh)) { | |
1385 | return if $again; | |
1386 | die $errmsg if $errmsg; | |
1387 | } | |
1388 | ||
1389 | fh_nonblocking $clientfh, 1; | |
1390 | ||
1391 | $self->{conn_count}++; | |
1392 | ||
1393 | return $clientfh; | |
1394 | } | |
1395 | ||
1396 | sub wait_end_loop { | |
1397 | my ($self) = @_; | |
1398 | ||
1399 | $self->{end_loop} = 1; | |
1400 | ||
1401 | undef $self->{socket_watch}; | |
1402 | ||
1403 | $0 = "$0 (shutdown)" if $0 !~ m/\(shutdown\)$/; | |
1404 | ||
1405 | if ($self->{conn_count} <= 0) { | |
1406 | $self->{end_cond}->send(1); | |
1407 | return; | |
1408 | } | |
1409 | ||
1410 | # fork and exit, so that parent starts a new worker | |
1411 | if (fork()) { | |
1412 | exit(0); | |
1413 | } | |
1414 | ||
1415 | # else we need to wait until all open connections gets closed | |
1416 | my $w; $w = AnyEvent->timer (after => 1, interval => 1, cb => sub { | |
1417 | eval { | |
1418 | # todo: test for active connections instead (we can abort idle connections) | |
1419 | if ($self->{conn_count} <= 0) { | |
1420 | undef $w; | |
1421 | $self->{end_cond}->send(1); | |
1422 | } | |
1423 | }; | |
1424 | warn $@ if $@; | |
1425 | }); | |
1426 | } | |
1427 | ||
1428 | ||
1429 | sub check_host_access { | |
1430 | my ($self, $clientip) = @_; | |
1431 | ||
1432 | my $cip = Net::IP->new($clientip); | |
1433 | ||
1434 | my $match_allow = 0; | |
1435 | my $match_deny = 0; | |
1436 | ||
1437 | if ($self->{allow_from}) { | |
1438 | foreach my $t (@{$self->{allow_from}}) { | |
1439 | if ($t->overlaps($cip)) { | |
1440 | $match_allow = 1; | |
1441 | last; | |
1442 | } | |
1443 | } | |
1444 | } | |
1445 | ||
1446 | if ($self->{deny_from}) { | |
1447 | foreach my $t (@{$self->{deny_from}}) { | |
1448 | if ($t->overlaps($cip)) { | |
1449 | $match_deny = 1; | |
1450 | last; | |
1451 | } | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | if ($match_allow == $match_deny) { | |
1456 | # match both allow and deny, or no match | |
1457 | return $self->{policy} && $self->{policy} eq 'allow' ? 1 : 0; | |
1458 | } | |
1459 | ||
1460 | return $match_allow; | |
1461 | } | |
1462 | ||
1463 | sub accept_connections { | |
1464 | my ($self) = @_; | |
1465 | ||
1466 | eval { | |
1467 | ||
1468 | while (my $clientfh = $self->accept()) { | |
1469 | ||
1470 | my $reqstate = { keep_alive => $self->{keep_alive} }; | |
1471 | ||
1472 | # stop keep-alive when there are many open connections | |
1473 | if ($self->{conn_count} >= $self->{max_conn_soft_limit}) { | |
1474 | $reqstate->{keep_alive} = 0; | |
1475 | } | |
1476 | ||
1477 | if (my $sin = getpeername($clientfh)) { | |
1478 | my ($pfamily, $pport, $phost) = PVE::Tools::unpack_sockaddr_in46($sin); | |
1479 | ($reqstate->{peer_port}, $reqstate->{peer_host}) = ($pport, Socket::inet_ntop($pfamily, $phost)); | |
1480 | } | |
1481 | ||
1482 | if (!$self->{trusted_env} && !$self->check_host_access($reqstate->{peer_host})) { | |
1483 | print "$$: ABORT request from $reqstate->{peer_host} - access denied\n" if $self->{debug}; | |
1484 | $reqstate->{log}->{code} = 403; | |
1485 | $self->log_request($reqstate); | |
1486 | next; | |
1487 | } | |
1488 | ||
1489 | $reqstate->{hdl} = AnyEvent::Handle->new( | |
1490 | fh => $clientfh, | |
1491 | rbuf_max => 64*1024, | |
1492 | timeout => $self->{timeout}, | |
1493 | linger => 0, # avoid problems with ssh - really needed ? | |
1494 | on_eof => sub { | |
1495 | my ($hdl) = @_; | |
1496 | eval { | |
1497 | $self->log_aborted_request($reqstate); | |
1498 | $self->client_do_disconnect($reqstate); | |
1499 | }; | |
1500 | if (my $err = $@) { syslog('err', $err); } | |
1501 | }, | |
1502 | on_error => sub { | |
1503 | my ($hdl, $fatal, $message) = @_; | |
1504 | eval { | |
1505 | $self->log_aborted_request($reqstate, $message); | |
1506 | $self->client_do_disconnect($reqstate); | |
1507 | }; | |
1508 | if (my $err = $@) { syslog('err', "$err"); } | |
1509 | }, | |
1510 | ($self->{tls_ctx} ? (tls => "accept", tls_ctx => $self->{tls_ctx}) : ())); | |
1511 | ||
1512 | print "$$: ACCEPT FH" . $clientfh->fileno() . " CONN$self->{conn_count}\n" if $self->{debug}; | |
1513 | ||
1514 | $self->push_request_header($reqstate); | |
1515 | } | |
1516 | }; | |
1517 | ||
1518 | if (my $err = $@) { | |
1519 | syslog('err', $err); | |
1520 | $self->{end_loop} = 1; | |
1521 | } | |
1522 | ||
1523 | $self->wait_end_loop() if $self->{end_loop}; | |
1524 | } | |
1525 | ||
1526 | # Note: We can't open log file in non-blocking mode and use AnyEvent::Handle, | |
1527 | # because we write from multiple processes, and that would arbitrarily mix output | |
1528 | # of all processes. | |
1529 | sub open_access_log { | |
1530 | my ($self, $filename) = @_; | |
1531 | ||
1532 | my $old_mask = umask(0137);; | |
1533 | my $logfh = IO::File->new($filename, ">>") || | |
1534 | die "unable to open log file '$filename' - $!\n"; | |
1535 | umask($old_mask); | |
1536 | ||
1537 | $logfh->autoflush(1); | |
1538 | ||
1539 | $self->{logfh} = $logfh; | |
1540 | } | |
1541 | ||
1542 | sub write_log { | |
1543 | my ($self, $data) = @_; | |
1544 | ||
1545 | return if !defined($self->{logfh}) || !$data; | |
1546 | ||
1547 | my $res = $self->{logfh}->print($data); | |
1548 | ||
1549 | if (!$res) { | |
1550 | delete $self->{logfh}; | |
1551 | syslog('err', "error writing access log"); | |
1552 | $self->{end_loop} = 1; # terminate asap | |
1553 | } | |
1554 | } | |
1555 | ||
1556 | sub atfork_handler { | |
1557 | my ($self) = @_; | |
1558 | ||
1559 | eval { | |
1560 | # something else do to ? | |
1561 | close($self->{socket}); | |
1562 | }; | |
1563 | warn $@ if $@; | |
1564 | } | |
1565 | ||
fbc42b33 DM |
1566 | sub run { |
1567 | my ($self) = @_; | |
1568 | ||
1569 | $self->{end_cond}->recv; | |
1570 | } | |
1571 | ||
d8218001 DM |
1572 | sub new { |
1573 | my ($this, %args) = @_; | |
1574 | ||
1575 | my $class = ref($this) || $this; | |
1576 | ||
1577 | foreach my $req (qw(base_handler_class socket lockfh lockfile)) { | |
1578 | die "misssing required argument '$req'" if !defined($args{$req}); | |
1579 | } | |
1580 | ||
1581 | my $self = bless { %args }, $class; | |
1582 | ||
1583 | $self->{cookie_name} //= 'PVEAuthCookie'; | |
1584 | $self->{base_uri} //= "/api2"; | |
1585 | ||
1586 | # init inotify | |
1587 | PVE::INotify::inotify_init(); | |
1588 | ||
d8218001 DM |
1589 | fh_nonblocking($self->{socket}, 1); |
1590 | ||
1591 | $self->{end_loop} = 0; | |
1592 | $self->{conn_count} = 0; | |
1593 | $self->{request_count} = 0; | |
1594 | $self->{timeout} = 5 if !$self->{timeout}; | |
1595 | $self->{keep_alive} = 0 if !defined($self->{keep_alive}); | |
1596 | $self->{max_conn} = 800 if !$self->{max_conn}; | |
1597 | $self->{max_requests} = 8000 if !$self->{max_requests}; | |
1598 | ||
1599 | $self->{policy} = 'allow' if !$self->{policy}; | |
1600 | ||
1601 | $self->{end_cond} = AnyEvent->condvar; | |
1602 | ||
1603 | if ($self->{ssl}) { | |
1604 | $self->{tls_ctx} = AnyEvent::TLS->new(%{$self->{ssl}}); | |
1605 | # TODO : openssl >= 1.0.2 supports SSL_CTX_set_ecdh_auto to select a curve depending on | |
1606 | # server and client availability from SSL_CTX_set1_curves. | |
1607 | # that way other curves like 25519 can be used. | |
1608 | # openssl 1.0.1 can only support 1 curve at a time. | |
1609 | my $curve = Net::SSLeay::OBJ_txt2nid('prime256v1'); | |
1610 | my $ecdh = Net::SSLeay::EC_KEY_new_by_curve_name($curve); | |
1611 | Net::SSLeay::CTX_set_options($self->{tls_ctx}->{ctx}, &Net::SSLeay::OP_NO_COMPRESSION | &Net::SSLeay::OP_SINGLE_ECDH_USE | &Net::SSLeay::OP_SINGLE_DH_USE); | |
1612 | Net::SSLeay::CTX_set_tmp_ecdh($self->{tls_ctx}->{ctx}, $ecdh); | |
1613 | Net::SSLeay::EC_KEY_free($ecdh); | |
1614 | } | |
1615 | ||
1616 | if ($self->{spiceproxy}) { | |
1617 | $known_methods = { CONNECT => 1 }; | |
1618 | } | |
1619 | ||
1620 | $self->open_access_log($self->{logfile}) if $self->{logfile}; | |
1621 | ||
1622 | $self->{max_conn_soft_limit} = $self->{max_conn} > 100 ? $self->{max_conn} - 20 : $self->{max_conn}; | |
1623 | ||
1624 | $self->{socket_watch} = AnyEvent->io(fh => $self->{socket}, poll => 'r', cb => sub { | |
1625 | eval { | |
1626 | if ($self->{conn_count} >= $self->{max_conn}) { | |
1627 | my $w; $w = AnyEvent->timer (after => 1, interval => 1, cb => sub { | |
1628 | if ($self->{conn_count} < $self->{max_conn}) { | |
1629 | undef $w; | |
1630 | $self->accept_connections(); | |
1631 | } | |
1632 | }); | |
1633 | } else { | |
1634 | $self->accept_connections(); | |
1635 | } | |
1636 | }; | |
1637 | warn $@ if $@; | |
1638 | }); | |
1639 | ||
1640 | $self->{term_watch} = AnyEvent->signal(signal => "TERM", cb => sub { | |
1641 | undef $self->{term_watch}; | |
1642 | $self->wait_end_loop(); | |
1643 | }); | |
1644 | ||
1645 | $self->{quit_watch} = AnyEvent->signal(signal => "QUIT", cb => sub { | |
1646 | undef $self->{quit_watch}; | |
1647 | $self->wait_end_loop(); | |
1648 | }); | |
1649 | ||
1650 | $self->{inotify_poll} = AnyEvent->timer(after => 5, interval => 5, cb => sub { | |
1651 | PVE::INotify::poll(); # read inotify events | |
1652 | }); | |
1653 | ||
1654 | return $self; | |
1655 | } | |
1656 | ||
fbc42b33 DM |
1657 | # abstract functions - subclass should overwrite/implement them |
1658 | ||
403964f2 DM |
1659 | sub verify_spice_connect_url { |
1660 | my ($self, $connect_str) = @_; | |
1661 | ||
1662 | die "implement me"; | |
1663 | ||
1664 | #return ($vmid, $node, $port); | |
1665 | } | |
1666 | ||
d8218001 DM |
1667 | sub auth_handler { |
1668 | my ($self, $method, $rel_uri, $ticket, $token) = @_; | |
1669 | ||
b639f458 | 1670 | die "implement me"; |
d8218001 | 1671 | |
b639f458 DM |
1672 | # return { |
1673 | # ticket => $ticket, | |
1674 | # token => $token, | |
1675 | # userid => $username, | |
1676 | # age => $age, | |
1677 | # isUpload => $isUpload, | |
1678 | # cookie_name => $self->{cookie_name}, | |
1679 | #}; | |
d8218001 DM |
1680 | } |
1681 | ||
d8218001 DM |
1682 | |
1683 | sub rest_handler { | |
1684 | my ($self, $clientip, $method, $rel_uri, $auth, $params) = @_; | |
1685 | ||
b639f458 DM |
1686 | return { |
1687 | status => HTTP_NOT_IMPLEMENTED, | |
1688 | message => "Method '$method $rel_uri' not implemented", | |
d8218001 | 1689 | }; |
d8218001 DM |
1690 | } |
1691 | ||
1692 | sub check_cert_fingerprint { | |
1693 | my ($self, $cert) = @_; | |
1694 | ||
b639f458 DM |
1695 | die "implement me"; |
1696 | } | |
d8218001 DM |
1697 | |
1698 | sub initialize_cert_cache { | |
1699 | my ($self, $node) = @_; | |
1700 | ||
b639f458 | 1701 | die "implement me"; |
d8218001 DM |
1702 | } |
1703 | ||
1704 | sub remote_node_ip { | |
1705 | my ($self, $node) = @_; | |
1706 | ||
b639f458 | 1707 | die "implement me"; |
d8218001 | 1708 | |
b639f458 | 1709 | # return $remip; |
d8218001 DM |
1710 | } |
1711 | ||
d8218001 DM |
1712 | |
1713 | 1; |