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