| 1 | # ex:ts=8 sw=4: | ||
| 2 | # $OpenBSD$ | ||
| 3 | # | ||
| 4 | # Copyright (c) 2026 Dick Olsson <hi@dickolsson.com> | ||
| 5 | # | ||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||
| 8 | # copyright notice and this permission notice appear in all copies. | ||
| 9 | # | ||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 17 | |||
| 18 | use v5.36; | ||
| 19 | |||
| 20 | # App::FuguVM::Autoinstall - serve one autoinstall(8) response file. | ||
| 21 | # | ||
| 22 | # The module holds the response file and the responder: a small HTTP | ||
| 23 | # child that answers that one file to one guest. The child binds the | ||
| 24 | # loopback address only, because a response file can hold the root | ||
| 25 | # password, and the guest reaches a loopback listener through the QEMU | ||
| 26 | # gateway. The module is FuguVM policy over generic parts: the file it | ||
| 27 | # serves is an installer artifact, and the address it serves to is a | ||
| 28 | # QEMU gateway. Fugu::Proxy stays a proxy, and it gains no | ||
| 29 | # origin-server mode here. | ||
| 30 | |||
| 31 | package App::FuguVM::Autoinstall; | ||
| 32 | our $VERSION = '0.3.1'; | ||
| 33 | |||
| 34 | use IO::Socket::INET; | ||
| 35 | use Fugu::File; | ||
| 36 | use Fugu::Log; | ||
| 37 | use Fugu::Process; | ||
| 38 | use Fugu::Timeout; | ||
| 39 | use App::FuguVM::Proxy; | ||
| 40 | |||
| 41 | use constant { | ||
| 42 | RESPONSE_PATH => '/install.conf', | ||
| 43 | |||
| 44 | # One range above the proxy range, so the two children cannot | ||
| 45 | # contend for one port. | ||
| 46 | PORTS => [ 8181, 8280 ], | ||
| 47 | |||
| 48 | BIND_ADDRESS => '127.0.0.1', | ||
| 49 | PROXY_TOKEN => '@PROXY_URL@', | ||
| 50 | READY_TIMEOUT => 10, | ||
| 51 | |||
| 52 | # The bound on one request line. The guest sends one short GET, | ||
| 53 | # so a longer line is not a request from the installer. | ||
| 54 | MAX_REQUEST_LINE => 8192, | ||
| 55 | }; | ||
| 56 | |||
| 57 | # App::FuguVM::Autoinstall->new(%args): | ||
| 58 | # file => $path the response file (required) | ||
| 59 | # pidfile => $pidfile a Fugu::Pidfile for the child (required) | ||
| 60 | # store => $store a Fugu::StateFile that holds the port (required) | ||
| 61 | # proxy_url => $url replaces each PROXY_TOKEN, or undef | ||
| 62 | # logfile => $path where the output of the child goes | ||
| 63 | # log => $logger default: Fugu::Log->default | ||
| 64 | # | ||
| 65 | # The constructor opens nothing. | ||
| 66 | sub new ( $class, %args ) | ||
| 67 | { | ||
| 68 | for my $required (qw(pidfile store)) { | ||
| 69 | die "$required parameter required" | ||
| 70 | unless defined $args{$required}; | ||
| 71 | } | ||
| 72 | |||
| 73 | return bless { | ||
| 74 | file => $args{file}, | ||
| 75 | pidfile => $args{pidfile}, | ||
| 76 | store => $args{store}, | ||
| 77 | proxy_url => $args{proxy_url}, | ||
| 78 | logfile => $args{logfile} // '/dev/null', | ||
| 79 | log => $args{log} // Fugu::Log->default, | ||
| 80 | error => undef, | ||
| 81 | }, $class; | ||
| 82 | } | ||
| 83 | |||
| 84 | # $self->path: | ||
| 85 | # Return the response-file path. | ||
| 86 | sub path ($self) | ||
| 87 | { | ||
| 88 | return $self->{file}; | ||
| 89 | } | ||
| 90 | |||
| 91 | # $self->error: | ||
| 92 | # Return the reason of the last failed start, or undef. | ||
| 93 | sub error ($self) | ||
| 94 | { | ||
| 95 | return $self->{error}; | ||
| 96 | } | ||
| 97 | |||
| 98 | # $self->port: | ||
| 99 | # Return the recorded port, or undef. | ||
| 100 | sub port ($self) | ||
| 101 | { | ||
| 102 | return $self->{store}->get('autoinstall_port'); | ||
| 103 | } | ||
| 104 | |||
| 105 | # $self->is_running: | ||
| 106 | # Report whether the child is alive. The check reaps first, so a | ||
| 107 | # child that became a zombie reads as stopped. | ||
| 108 | sub is_running ($self) | ||
| 109 | { | ||
| 110 | return $self->{pidfile}->is_running ? 1 : 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | # $self->guest_url: | ||
| 114 | # Return the response-file URL as the guest reaches it, through | ||
| 115 | # the QEMU gateway. The gateway routes the request to the | ||
| 116 | # loopback listener of the host, so no port forward is needed. | ||
| 117 | sub guest_url ($self) | ||
| 118 | { | ||
| 119 | my $port = $self->port; | ||
| 120 | return if !defined $port; | ||
| 121 | |||
| 122 | return | ||
| 123 | 'http://' | ||
| 124 | . App::FuguVM::Proxy::HOST_GATEWAY() | ||
| 125 | . ":$port" | ||
| 126 | . RESPONSE_PATH; | ||
| 127 | } | ||
| 128 | |||
| 129 | # $self->start: | ||
| 130 | # Take a free port, spawn the child, record the PID and the | ||
| 131 | # port, and wait until the port answers. Return the port, or | ||
| 132 | # undef with the reason in error(). A responder that already | ||
| 133 | # runs returns its port and starts nothing. | ||
| 134 | sub start ($self) | ||
| 135 | { | ||
| 136 | $self->{error} = undef; | ||
| 137 | |||
| 138 | return $self->port if $self->is_running; | ||
| 139 | |||
| 140 | if ( !defined $self->{file} || !-r $self->{file} ) { | ||
| 141 | $self->{error} = 'the response file is not readable: ' | ||
| 142 | . ( $self->path // '(none)' ); | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | my $port = $self->_find_free_port; | ||
| 147 | unless ( defined $port ) { | ||
| 148 | $self->{error} = sprintf 'no free port in %d-%d', @{ +PORTS }; | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | my $child = ref $self; | ||
| 153 | my $result = Fugu::Process->spawn_perl( | ||
| 154 | code => "use $child; $child->run_child(\@ARGV)", | ||
| 155 | args => [ $port, $self->{file}, $self->{proxy_url} // '' ], | ||
| 156 | daemonize => 1, | ||
| 157 | stdout => $self->{logfile}, | ||
| 158 | stderr => $self->{logfile}, | ||
| 159 | ); | ||
| 160 | unless ( $result->{success} ) { | ||
| 161 | $self->{error} = "cannot start the responder: $result->{error}"; | ||
| 162 | return; | ||
| 163 | } | ||
| 164 | |||
| 165 | $self->{pidfile}->write_pid( $result->{pid} ); | ||
| 166 | $self->{store}->set( autoinstall_port => $port ); | ||
| 167 | |||
| 168 | unless ( $self->_wait_ready ) { | ||
| 169 | $self->{error} = 'the responder did not take connections'; | ||
| 170 | $self->stop; | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | return $port; | ||
| 175 | } | ||
| 176 | |||
| 177 | # $self->stop: | ||
| 178 | # Stop the child and forget the port. The method returns 1. | ||
| 179 | sub stop ($self) | ||
| 180 | { | ||
| 181 | my $pid = $self->{pidfile}->read_pid; | ||
| 182 | Fugu::Process->terminate( $pid, grace_period => 5 ) | ||
| 183 | if defined $pid; | ||
| 184 | |||
| 185 | $self->{pidfile}->remove; | ||
| 186 | $self->{store}->delete('autoinstall_port'); | ||
| 187 | |||
| 188 | return 1; | ||
| 189 | } | ||
| 190 | |||
| 191 | # $class->run_child($port, $file, $proxy_url): | ||
| 192 | # The entry point of the spawned child. The child reads the | ||
| 193 | # response file, renders it, and serves it until a SIGTERM. It | ||
| 194 | # logs each request line, and it never logs the file content. | ||
| 195 | sub run_child ( $class, $port, $file, $proxy_url ) | ||
| 196 | { | ||
| 197 | my $log = Fugu::Log->new( mode => 'stderr', level => 'debug' ); | ||
| 198 | |||
| 199 | my $bytes = Fugu::File->read($file); | ||
| 200 | die "Cannot read the response file: $file\n" if !defined $bytes; | ||
| 201 | |||
| 202 | my $body = $class->render( $bytes, $proxy_url ); | ||
| 203 | |||
| 204 | my $listener = IO::Socket::INET->new( | ||
| 205 | LocalAddr => BIND_ADDRESS, | ||
| 206 | LocalPort => $port, | ||
| 207 | Proto => 'tcp', | ||
| 208 | ReuseAddr => 1, | ||
| 209 | Listen => 5, | ||
| 210 | ) or die 'Cannot listen on ' . BIND_ADDRESS . ":$port: $!\n"; | ||
| 211 | |||
| 212 | $log->info( 'Responder listening on %s:%d', BIND_ADDRESS, $port ); | ||
| 213 | |||
| 214 | # A client can disconnect in the middle of an answer | ||
| 215 | local $SIG{PIPE} = 'IGNORE'; | ||
| 216 | |||
| 217 | # The self-pipe makes the signal safe: the handler writes one | ||
| 218 | # byte, and the select loop notices it between requests instead | ||
| 219 | # of inside one. | ||
| 220 | require IO::Select; | ||
| 221 | pipe my $sig_read, my $sig_write or die "pipe: $!"; | ||
| 222 | $sig_read->blocking(0); | ||
| 223 | $sig_write->blocking(0); | ||
| 224 | |||
| 225 | my $running = 1; | ||
| 226 | local $SIG{TERM} = sub { | ||
| 227 | $running = 0; | ||
| 228 | syswrite $sig_write, 'x', 1; | ||
| 229 | }; | ||
| 230 | |||
| 231 | my $select = IO::Select->new( $listener, $sig_read ); | ||
| 232 | |||
| 233 | while ($running) { | ||
| 234 | my @ready = $select->can_read; | ||
| 235 | last if !$running; | ||
| 236 | |||
| 237 | for my $fh (@ready) { | ||
| 238 | if ( $fh == $sig_read ) { | ||
| 239 | sysread $sig_read, my $drain, 100; | ||
| 240 | next; | ||
| 241 | } | ||
| 242 | |||
| 243 | my $client = $listener->accept or next; | ||
| 244 | _answer( $client, $body, $log ); | ||
| 245 | close $client; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | $log->info('Responder shutting down'); | ||
| 250 | close $sig_read; | ||
| 251 | close $sig_write; | ||
| 252 | close $listener; | ||
| 253 | |||
| 254 | return 1; | ||
| 255 | } | ||
| 256 | |||
| 257 | # $class->render($bytes, $proxy_url): | ||
| 258 | # Return the bytes with every PROXY_TOKEN replaced by | ||
| 259 | # $proxy_url. The method is pure, so a test proves it with no | ||
| 260 | # socket. With an empty proxy URL the bytes return unchanged: a | ||
| 261 | # token that nothing replaces stays visible, and the installer | ||
| 262 | # then diagnoses it. | ||
| 263 | sub render ( $, $bytes, $proxy_url ) | ||
| 264 | { | ||
| 265 | return $bytes if !defined $proxy_url || $proxy_url eq ''; | ||
| 266 | |||
| 267 | my $token = PROXY_TOKEN; | ||
| 268 | $bytes =~ s/\Q$token\E/$proxy_url/g; | ||
| 269 | |||
| 270 | return $bytes; | ||
| 271 | } | ||
| 272 | |||
| 273 | # _answer($client, $body, $log): | ||
| 274 | # Read one request, answer it, and log the request line with the | ||
| 275 | # status code. The child parses the request line and nothing | ||
| 276 | # else: the headers carry nothing the answer depends on. It | ||
| 277 | # still reads to the end of the request head. Without that | ||
| 278 | # drain, the close after the answer can reset the connection | ||
| 279 | # before the guest reads it. | ||
| 280 | sub _answer ( $client, $body, $log ) | ||
| 281 | { | ||
| 282 | my $line = _request_line($client); | ||
| 283 | |||
| 284 | # A connection that sends nothing is the readiness probe of | ||
| 285 | # start, not a request. It gets no answer and no log line. | ||
| 286 | return if defined $line && $line eq ''; | ||
| 287 | |||
| 288 | if ( !defined $line ) { | ||
| 289 | $log->warning('400 (request line too long or unreadable)'); | ||
| 290 | _respond( $client, 400, 'Bad Request', '' ); | ||
| 291 | return; | ||
| 292 | } | ||
| 293 | |||
| 294 | my ( $method, $path ) = split ' ', $line; | ||
| 295 | $method //= ''; | ||
| 296 | $path //= ''; | ||
| 297 | |||
| 298 | my ( $code, $text, $answer ) = | ||
| 299 | $method ne 'GET' | ||
| 300 | && $method ne 'HEAD' ? ( 405, 'Method Not Allowed', '' ) | ||
| 301 | : $path ne RESPONSE_PATH ? ( 404, 'Not Found', '' ) | ||
| 302 | : ( 200, 'OK', $body ); | ||
| 303 | |||
| 304 | $log->info( '%s %s -> %d', $method, $path, $code ); | ||
| 305 | _respond( $client, $code, $text, $answer, $method eq 'HEAD' ); | ||
| 306 | |||
| 307 | return; | ||
| 308 | } | ||
| 309 | |||
| 310 | # _request_line($client): | ||
| 311 | # Return the request line, without its line ending. The read | ||
| 312 | # continues to the blank line that ends the request head, under | ||
| 313 | # one bound for the whole head. Return the empty string for a | ||
| 314 | # peer that sent nothing. Return undef for a head above the | ||
| 315 | # bound, and for a read failure before the first line ends. | ||
| 316 | sub _request_line ($client) | ||
| 317 | { | ||
| 318 | my $head = ''; | ||
| 319 | while ( length($head) < MAX_REQUEST_LINE ) { | ||
| 320 | my $read = sysread( $client, my $chunk, 1024 ); | ||
| 321 | last if !$read; | ||
| 322 | $head .= $chunk; | ||
| 323 | last if index( $head, "\r\n\r\n" ) >= 0; | ||
| 324 | last if index( $head, "\n\n" ) >= 0; | ||
| 325 | } | ||
| 326 | return '' if $head eq ''; | ||
| 327 | return if length($head) >= MAX_REQUEST_LINE; | ||
| 328 | |||
| 329 | my $end = index( $head, "\n" ); | ||
| 330 | return if $end < 0; | ||
| 331 | |||
| 332 | my $line = substr( $head, 0, $end ); | ||
| 333 | $line =~ s/\r\z//; | ||
| 334 | |||
| 335 | return $line; | ||
| 336 | } | ||
| 337 | |||
| 338 | # _respond($client, $code, $text, $body, $head_only): | ||
| 339 | # Write one complete answer. Every answer carries Content-Length | ||
| 340 | # and Connection: close, and a HEAD answer carries the headers | ||
| 341 | # only. | ||
| 342 | sub _respond ( $client, $code, $text, $body, $head_only = 0 ) | ||
| 343 | { | ||
| 344 | my $head = | ||
| 345 | "HTTP/1.1 $code $text\r\n" | ||
| 346 | . "Content-Type: text/plain\r\n" | ||
| 347 | . 'Content-Length: ' | ||
| 348 | . length($body) . "\r\n" | ||
| 349 | . "Connection: close\r\n\r\n"; | ||
| 350 | $head .= $body unless $head_only; | ||
| 351 | |||
| 352 | return Fugu::File->_write_all( $client, $head, 'client socket' ); | ||
| 353 | } | ||
| 354 | |||
| 355 | # $self->_find_free_port: | ||
| 356 | # Return the first port of the range that binds on the loopback | ||
| 357 | # address, or undef. | ||
| 358 | sub _find_free_port ($self) | ||
| 359 | { | ||
| 360 | my ( $first, $last ) = @{ +PORTS }; | ||
| 361 | |||
| 362 | for my $port ( $first .. $last ) { | ||
| 363 | my $sock = IO::Socket::INET->new( | ||
| 364 | LocalAddr => BIND_ADDRESS, | ||
| 365 | LocalPort => $port, | ||
| 366 | Proto => 'tcp', | ||
| 367 | ReuseAddr => 1, | ||
| 368 | Listen => 1, | ||
| 369 | ) or next; | ||
| 370 | close $sock; | ||
| 371 | return $port; | ||
| 372 | } | ||
| 373 | |||
| 374 | return; | ||
| 375 | } | ||
| 376 | |||
| 377 | # $self->_wait_ready: | ||
| 378 | # Wait until the responder takes a connection. | ||
| 379 | sub _wait_ready ($self) | ||
| 380 | { | ||
| 381 | my $port = $self->port; | ||
| 382 | return 0 if !defined $port; | ||
| 383 | |||
| 384 | my $ready = Fugu::Timeout::wait_until( | ||
| 385 | READY_TIMEOUT, | ||
| 386 | 0.2, | ||
| 387 | sub { | ||
| 388 | my $sock = IO::Socket::INET->new( | ||
| 389 | PeerAddr => BIND_ADDRESS, | ||
| 390 | PeerPort => $port, | ||
| 391 | Proto => 'tcp', | ||
| 392 | Timeout => 1, | ||
| 393 | ) or return 0; | ||
| 394 | close $sock; | ||
| 395 | return 1; | ||
| 396 | } ); | ||
| 397 | |||
| 398 | return $ready ? 1 : 0; | ||
| 399 | } | ||
| 400 | |||
| 401 | 1; |