| 1 | package App::Acmeman::Source::Apache; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | use feature 'state'; | ||
| 7 | use File::Path qw(make_path); | ||
| 8 | use File::Spec; | ||
| 9 | use App::Acmeman::Apache::Layout; | ||
| 10 | use App::Acmeman::Log qw(:all); | ||
| 11 | use parent 'App::Acmeman::Source'; | ||
| 12 | use Getopt::Long qw(GetOptionsFromArray :config gnu_getopt no_ignore_case); | ||
| 13 | use Apache::Defaults; | ||
| 14 | use Apache::Config::Preproc; | ||
| 15 | use Text::ParseWords; | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my $class = shift; | ||
| 19 | my $server_root; | ||
| 20 | GetOptionsFromArray(\@_, | ||
| 21 | 'server-root=s' => \$server_root); | ||
| 22 | my $layout = detect App::Acmeman::Apache::Layout(@_); | ||
| 23 | my $self = bless { _layout => $layout }, $class; | ||
| 24 | $self->server_root($server_root) if $server_root; | ||
| 25 | return $self; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub layout { shift->{_layout} } | ||
| 29 | |||
| 30 | sub scan { | ||
| 31 | my ($self) = @_; | ||
| 32 | debug(2, 'assuming Apache layout "'.$self->layout->name.'"'); | ||
| 33 | $self->set(qw(core postrenew), $self->layout->restart_command) | ||
| 34 | unless $self->is_set(qw(core postrenew)); | ||
| 35 | return $self->examine_http_config($self->layout->config_file); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub dequote { | ||
| 39 | my ($self, $arg) = @_; | ||
| 40 | if (defined($arg) && $arg =~ s{^"(.*?)"$}{$1}) { | ||
| 41 | $arg =~ s{\\([\\"])}{$1}g; | ||
| 42 | } | ||
| 43 | return $arg; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub examine_http_config { | ||
| 47 | my ($self, $file) = @_; | ||
| 48 | |||
| 49 | my $app = new Apache::Config::Preproc( | ||
| 50 | $file, | ||
| 51 | -expand => [ 'compact', | ||
| 52 | { 'include' => [ server_root => $self->server_root ] }, | ||
| 53 | { 'macro' => [ | ||
| 54 | 'keep' => [ qw(LetsEncryptChallenge | ||
| 55 | LetsEncryptReference | ||
| 56 | LetsEncryptSSL) | ||
| 57 | ] | ||
| 58 | ] | ||
| 59 | } | ||
| 60 | ]) | ||
| 61 | or do { | ||
| 62 | error($Apache::Admin::Config::ERROR); | ||
| 63 | return 0; | ||
| 64 | }; | ||
| 65 | |||
| 66 | foreach my $sect ($app->section(-name => "macro")) { | ||
| 67 | if ($sect->value =~ m{^(?ix)letsencryptssl | ||
| 68 | \s+ | ||
| 69 | (.+)} | ||
| 70 | && ! $self->is_set(qw(files apache))) { | ||
| 71 | $self->set(qw(files apache argument), $1); | ||
| 72 | map { | ||
| 73 | if ($_->name =~ m{^(?ix) | ||
| 74 | SSLCertificate((?:Key)|(?:Chain))?File}) { | ||
| 75 | my %t = ( | ||
| 76 | '' => 'certificate-file', | ||
| 77 | key => 'key-file', | ||
| 78 | chain => 'ca-file' | ||
| 79 | ); | ||
| 80 | $self->set(qw(files apache), $t{lc($1||'')}, | ||
| 81 | $self->dequote($_->value)); | ||
| 82 | } | ||
| 83 | } $sect->directive(); | ||
| 84 | } elsif ($sect->value =~ m{^(?ix)letsencryptchallenge$}) { | ||
| 85 | foreach my $alias ($sect->directive('alias')) { | ||
| 86 | if ($alias->value =~ m{^/.well-known/acme-challenge\s+(.+)}) { | ||
| 87 | my $dir = $self->dequote($1); | ||
| 88 | $dir =~ s{/.well-known/acme-challenge$}{}; | ||
| 89 | $self->set(qw(core rootdir), $dir); | ||
| 90 | debug(3, "ACME challenge root dir: $dir"); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | foreach my $sect ($app->section(-name => "virtualhost")) { | ||
| 97 | my ($server_name) = (map { | ||
| 98 | (my $s = $self->dequote($_->value)) =~ s{^https?://}{}; | ||
| 99 | $s | ||
| 100 | } $sect->directive('servername')); | ||
| 101 | my @server_aliases = map { quotewords('\s+', 0, | ||
| 102 | $self->dequote($_->value)) } | ||
| 103 | $sect->directive('serveralias'); | ||
| 104 | my @d = map { | ||
| 105 | if ($_->value =~ m{^(?ix) | ||
| 106 | (?:letsencrypt(challenge|ssl|reference)) | ||
| 107 | (?:\s+(.+))?}) { | ||
| 108 | [lc($1),$self->dequote($2)] | ||
| 109 | } else { | ||
| 110 | () | ||
| 111 | } | ||
| 112 | } $sect->directive('use'); | ||
| 113 | |||
| 114 | if (grep { $_->[0] eq 'challenge' } @d) { | ||
| 115 | unless ($server_name) { | ||
| 116 | $server_name = shift @server_aliases; | ||
| 117 | } | ||
| 118 | $self->define_domain($server_name); | ||
| 119 | $self->define_alias($server_name, @server_aliases); | ||
| 120 | debug(3, "will handle ".join(',', $server_name, @server_aliases)); | ||
| 121 | } elsif (my ($ref) = map { $_->[1] } | ||
| 122 | grep { $_->[0] eq 'reference' } @d) { | ||
| 123 | $self->set('domain', $ref, 'files', 'apache'); | ||
| 124 | $self->define_alias($ref, $server_name, @server_aliases); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub server_root { | ||
| 131 | my $self = shift; | ||
| 132 | if (my $v = shift) { | ||
| 133 | croak "too many arguments" if $@; | ||
| 134 | $self->{_server_root} = $v; | ||
| 135 | } | ||
| 136 | return $self->{_server_root} || $self->layout->apache->server_root; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub mkpath { | ||
| 140 | my ($self, $dir) = @_; | ||
| 141 | my @created = make_path("$dir", { error => \my $err } ); | ||
| 142 | if (@$err) { | ||
| 143 | for my $diag (@$err) { | ||
| 144 | my ($file, $message) = %$diag; | ||
| 145 | if ($file eq '') { | ||
| 146 | error($message); | ||
| 147 | } else { | ||
| 148 | error("mkdir $file: $message"); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | return 0; | ||
| 152 | } | ||
| 153 | return 1; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub setup { | ||
| 157 | my ($self, %args) = @_; | ||
| 158 | my $filename = $self->layout->incdir() . "/httpd-letsencrypt.conf"; | ||
| 159 | if (-e $filename) { | ||
| 160 | if ($args{force}) { | ||
| 161 | error("the file \"$filename\" already exists", | ||
| 162 | prefix => 'warning'); | ||
| 163 | } else { | ||
| 164 | error("the file \"$filename\" already exists"); | ||
| 165 | error("use --force to continue"); | ||
| 166 | return 0; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | my $www_root = $self->get(qw(core rootdir)); | ||
| 170 | debug(2, "writing $filename"); | ||
| 171 | unless ($args{dry_run}) { | ||
| 172 | my $challenge_dir = "$www_root/.well-known/acme-challenge"; | ||
| 173 | my $acme_dir = $App::Acmeman::acme_dir; | ||
| 174 | |||
| 175 | foreach my $dir ($self->layout->incdir(), $challenge_dir, $acme_dir) { | ||
| 176 | unless ($self->mkpath($dir)) { | ||
| 177 | return 0; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | $self->layout->pre_setup; | ||
| 182 | |||
| 183 | open(my $fd, '>', $filename) | ||
| 184 | or croak "can't open \"$filename\" for writing: $!"; | ||
| 185 | print $fd <<EOT; | ||
| 186 | <Macro LetsEncryptChallenge> | ||
| 187 | Alias /.well-known/acme-challenge $challenge_dir | ||
| 188 | <Directory $challenge_dir> | ||
| 189 | Options None | ||
| 190 | Require all granted | ||
| 191 | </Directory> | ||
| 192 | <IfModule mod_rewrite.c> | ||
| 193 | RewriteEngine On | ||
| 194 | RewriteRule /.well-known/acme-challenge - [L] | ||
| 195 | </IfModule> | ||
| 196 | </Macro> | ||
| 197 | |||
| 198 | <Macro LetsEncryptReference \$domain> | ||
| 199 | Use LetsEncryptChallenge | ||
| 200 | Alias /.dummy/\$domain /dev/null | ||
| 201 | </Macro> | ||
| 202 | |||
| 203 | <Macro LetsEncryptSSL \$domain> | ||
| 204 | SSLEngine on | ||
| 205 | SSLProtocol all -SSLv2 -SSLv3 | ||
| 206 | SSLHonorCipherOrder on | ||
| 207 | SSLCipherSuite ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:!DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA | ||
| 208 | SSLCertificateFile $acme_dir/\$domain/cert.pem | ||
| 209 | SSLCertificateKeyFile $acme_dir/\$domain/privkey.pem | ||
| 210 | SSLCACertificateFile $acme_dir/$App::Acmeman::letsencrypt_root_cert_basename | ||
| 211 | </Macro> | ||
| 212 | |||
| 213 | <Macro LetsEncryptServer \$domain> | ||
| 214 | ServerName \$domain | ||
| 215 | Use LetsEncryptSSL \$domain | ||
| 216 | </Macro> | ||
| 217 | |||
| 218 | EOT | ||
| 219 | ; | ||
| 220 | close $fd; | ||
| 221 | error("created file \"$filename\"", prefix => 'note'); | ||
| 222 | |||
| 223 | $self->layout->post_setup($filename); | ||
| 224 | } | ||
| 225 | |||
| 226 | my $pfx = "please,"; | ||
| 227 | unless ($self->is_letsencryptssl_defined) { | ||
| 228 | error("please, make sure your Apache configuration includes this file", | ||
| 229 | prefix => 'note'); | ||
| 230 | $pfx = "and"; | ||
| 231 | } | ||
| 232 | |||
| 233 | unless ($self->layout->apache_modules('macro')) { | ||
| 234 | error("$pfx enable mod_macro", | ||
| 235 | prefix => 'note'); | ||
| 236 | } | ||
| 237 | return 1; | ||
| 238 | } | ||
| 239 | |||
| 240 | # Check if LetsEncryptSSL macro is defined | ||
| 241 | sub is_letsencryptssl_defined { | ||
| 242 | my ($self) = @_; | ||
| 243 | my $app = new Apache::Config::Preproc( | ||
| 244 | $self->layout->config_file, | ||
| 245 | -expand => [ 'compact', | ||
| 246 | { 'include' => [ server_root => $self->server_root ] }, | ||
| 247 | { 'macro' => [ | ||
| 248 | 'keep' => [ qw(LetsEncryptChallenge | ||
| 249 | LetsEncryptReference | ||
| 250 | LetsEncryptSSL) | ||
| 251 | ] | ||
| 252 | ] | ||
| 253 | } | ||
| 254 | ]); | ||
| 255 | if ($app) { | ||
| 256 | return grep { $_->value =~ m{^(?i)letsencryptssl\s+} } | ||
| 257 | $app->section(-name => "macro"); | ||
| 258 | } else { | ||
| 259 | debug(1, $Apache::Admin::Config::ERROR); | ||
| 260 | return undef | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | 1; |