| 1 |
#!/usr/bin/perl -T |
| 2 |
|
| 3 |
#---------------------------------------------------------------------- |
| 4 |
# ACME client written with process isolation and minimal privileges in mind |
| 5 |
# (webserver component) |
| 6 |
# Copyright © 2015-2021 Guilhem Moulin <[email protected]> |
| 7 |
# |
| 8 |
# This program is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# This program is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
#---------------------------------------------------------------------- |
| 21 |
|
| 22 |
use v5.14.2; |
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
|
| 26 |
# Usage: webserver FD |
| 27 |
# |
| 28 |
# fdopen(3) the file descriptor FD (corresponding to a listening |
| 29 |
# socket), then keep accept(2)'ing connections and consider each |
| 30 |
# connection as the verification of an ACME challenge, i.e., serve the |
| 31 |
# requested challenge token. |
| 32 |
# |
| 33 |
# NOTE: one needs to chdir(2) to an appropriate ACME challenge directory |
| 34 |
# before running this program, since challenge tokens are (on purpose) |
| 35 |
# only looked for in the current directory. |
| 36 |
# |
| 37 |
# NOTE: one should run this program as an unprivileged user:group such |
| 38 |
# as "www-data:www-data"; bind(2)'ing to a privileged port such as 80 is |
| 39 |
# not a problem since FD can be bound as root prior to the execve(2). |
| 40 |
|
| 41 |
use Errno qw/EINTR ENOENT/; |
| 42 |
use Fcntl qw/O_NOFOLLOW O_RDONLY/; |
| 43 |
use Socket qw/AF_UNIX AF_INET AF_INET6/; |
| 44 |
|
| 45 |
# Untaint and fdopen(3) the listening socket |
| 46 |
(shift @ARGV // die) =~ /\A(\d+)\z/ or die; |
| 47 |
open (my $S, '+<&=', $1+0) or die "fdopen $1: $!"; |
| 48 |
my $ROOT = '/.well-known/acme-challenge'; |
| 49 |
|
| 50 |
close STDIN or die "close: $!"; |
| 51 |
close STDOUT or die "close: $!"; |
| 52 |
|
| 53 |
sub info($$$) { |
| 54 |
my ($sockaddr, $msg, $req) = @_; |
| 55 |
$req =~ s/\r\n\z// if defined $req; |
| 56 |
|
| 57 |
# get a string representation of the peer's address |
| 58 |
my $fam = Socket::sockaddr_family($sockaddr); |
| 59 |
my $peer; |
| 60 |
|
| 61 |
if ($fam == AF_UNIX) { |
| 62 |
$peer = Socket::unpack_sockaddr_un($sockaddr); |
| 63 |
} else { |
| 64 |
my (undef, $ip) = |
| 65 |
$fam == AF_INET ? Socket::unpack_sockaddr_in($sockaddr) : |
| 66 |
$fam == AF_INET6 ? Socket::unpack_sockaddr_in6($sockaddr) : |
| 67 |
die; |
| 68 |
$peer = Socket::inet_ntop($fam, $ip); |
| 69 |
} |
| 70 |
|
| 71 |
$msg .= " from [$peer]" if defined $peer and $peer ne ''; |
| 72 |
$msg .= ": $req" if defined $req; |
| 73 |
|
| 74 |
print STDERR $msg, "\n"; |
| 75 |
} |
| 76 |
|
| 77 |
while (1) { |
| 78 |
my $sockaddr = accept(my $conn, $S) or do { |
| 79 |
next if $! == EINTR; # try again if accept(2) was interrupted by a signal |
| 80 |
die "accept: $!"; |
| 81 |
}; |
| 82 |
my $req = $conn->getline(); |
| 83 |
info($sockaddr, "[$$] Incoming connection", $req) if $ENV{DEBUG}; |
| 84 |
next unless defined $req; |
| 85 |
|
| 86 |
if ($req !~ s/\A(GET|HEAD) (.*) HTTP\/(1\.[01])\r\n\z/$2/) { |
| 87 |
info($sockaddr, "Error: Bad request", $req); |
| 88 |
next; |
| 89 |
} |
| 90 |
my ($method, $proto) = ($1, $3); |
| 91 |
|
| 92 |
# Consume (and ignore) the headers |
| 93 |
while (defined (my $h = $conn->getline())) { last if $h eq "\r\n" }; |
| 94 |
|
| 95 |
my ($status_line, $content_type, $content); |
| 96 |
if ($req =~ /\A\Q$ROOT\E\/([A-Za-z0-9_\-]+)\z/) { |
| 97 |
# only serve base64-encoded filenames (tokens) in the cwd |
| 98 |
# XXX stat(2) followed by open(2) is racy; open(2) would hang if |
| 99 |
# an attacker manages to replace a regular file with a FIFO |
| 100 |
# between the syscalls, leading to denial of service; however |
| 101 |
# there shouldn't be any risk of information disclosure as we're |
| 102 |
# not following symlinks (and the cwd is not owned by us) |
| 103 |
if (-f $1 and sysopen(my $fh, $1, O_NOFOLLOW|O_RDONLY)) { |
| 104 |
($status_line, $content_type) = ('200 OK', 'application/jose+json'); |
| 105 |
$content = do { local $/ = undef; $fh->getline() }; |
| 106 |
$fh->close() or die "close: $!"; |
| 107 |
} elsif ($! == ENOENT) { # errno from either stat(2) or open(2) |
| 108 |
$status_line = '404 Not Found'; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
$conn->print( "HTTP/$proto ", ($status_line // '403 Forbidden'), "\r\n" ); |
| 113 |
$conn->print( "Content-Type: $content_type\r\n" ) if defined $content_type; |
| 114 |
$conn->print( "Content-Length: ".length($content)."\r\n" ) if defined $content; |
| 115 |
$conn->print( "Connection: close\r\n" ); |
| 116 |
$conn->print( "\r\n" ); |
| 117 |
$conn->print( $content ) if defined $content and $method eq 'GET'; |
| 118 |
|
| 119 |
$conn->close() or die "close: $!"; |
| 120 |
} |