| 1 |
#!/usr/bin/perl |
| 2 |
package IkiWiki::Hosting; |
| 3 |
|
| 4 |
no lib '.'; |
| 5 |
use warnings; |
| 6 |
use strict; |
| 7 |
use IkiWiki; |
| 8 |
use IkiWiki::Hosting; |
| 9 |
|
| 10 |
sub meta_createkey { |
| 11 |
required => [qw{client-hostname}], |
| 12 |
options => [qw{}], |
| 13 |
description => "creates key for a client, and outputs the key filenames", |
| 14 |
section => "primary", |
| 15 |
} |
| 16 |
sub createkey { |
| 17 |
my $client=shift; |
| 18 |
|
| 19 |
assert_root(); |
| 20 |
assert_client_haskey($client, 0); |
| 21 |
|
| 22 |
shell("mkdir", "-p", "$config{keydir}/dns"); |
| 23 |
shell("chmod", "700", $config{keydir}); |
| 24 |
chdir "$config{keydir}/dns" || die "chdir $config{keydir}/dns: $!"; |
| 25 |
my $keyid=`dnssec-keygen -b 512 -a HMAC-MD5 -n HOST "$client."`; |
| 26 |
chomp $keyid; |
| 27 |
if (! length $keyid) { |
| 28 |
error "dnssec-keygen failed"; |
| 29 |
} |
| 30 |
setupbind(); |
| 31 |
|
| 32 |
return "$config{keydir}/dns/$keyid.key", "$config{keydir}/dns/$keyid.private"; |
| 33 |
} |
| 34 |
|
| 35 |
sub meta_deletekey { |
| 36 |
required => [qw{client-hostname}], |
| 37 |
options => [qw{}], |
| 38 |
description => "removes key files for a client", |
| 39 |
section => "primary", |
| 40 |
} |
| 41 |
sub deletekey { |
| 42 |
my $client=shift; |
| 43 |
|
| 44 |
assert_root(); |
| 45 |
assert_client_haskey($client, 1); |
| 46 |
|
| 47 |
my @keys=(dns_private_keys($client), dns_keys($client)); |
| 48 |
shell("rm", "-f", @keys); |
| 49 |
setupbind(); |
| 50 |
} |
| 51 |
|
| 52 |
sub meta_setupbind { |
| 53 |
required => [qw{}], |
| 54 |
options => [qw{}], |
| 55 |
description => "generates bind config files that allow updates using keys", |
| 56 |
section => "utility", |
| 57 |
} |
| 58 |
sub setupbind { |
| 59 |
assert_root(); |
| 60 |
|
| 61 |
# use template to generate named.conf file |
| 62 |
my $named_conf="/etc/bind/ikisite.conf"; |
| 63 |
my @keys = map { { |
| 64 |
key => dns_keyid($_), |
| 65 |
secret => dns_keysecret($_), |
| 66 |
} } dns_private_keys(); |
| 67 |
|
| 68 |
my @domains; |
| 69 |
foreach my $domain (split ' ', $config{domains}) { |
| 70 |
my $basedir="/etc/bind/$domain"; |
| 71 |
my $zonefile="$basedir/zonefile"; |
| 72 |
|
| 73 |
shell("mkdir", "-p", $basedir); |
| 74 |
|
| 75 |
# bind needs permission to write to the basedir |
| 76 |
shell("chown", "root:bind", $basedir); |
| 77 |
shell("chmod", "770", $basedir); |
| 78 |
|
| 79 |
if (! -e $zonefile) { |
| 80 |
# use template to generate stub zone file |
| 81 |
outtemplate($zonefile, "zonefile.tmpl", |
| 82 |
domain => $domain, |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
push @domains, { |
| 87 |
domain => $domain, |
| 88 |
zonefile => $zonefile, |
| 89 |
keys => \@keys, |
| 90 |
}; |
| 91 |
} |
| 92 |
my $template=IkiWiki::Hosting::ikisite_template("named.conf.tmpl"); |
| 93 |
$template->param( |
| 94 |
keys => \@keys, |
| 95 |
dns_domains => \@domains, |
| 96 |
); |
| 97 |
open(OUT, ">", $named_conf) || error "$named_conf: $!"; |
| 98 |
# contains keys, so needs to be only readable by bind |
| 99 |
shell("chown", "root:bind", $named_conf); |
| 100 |
shell("chmod", "640", $named_conf); |
| 101 |
print OUT $template->output; |
| 102 |
close OUT; |
| 103 |
|
| 104 |
# add include to named.conf.local |
| 105 |
my $named_conf_comment="// automatically added by ikidns"; |
| 106 |
open(NAMED_CONF_IN, "<", "/etc/bind/named.conf.local") || error("named.conf.local: $!"); |
| 107 |
open(NAMED_CONF_OUT, ">", "/etc/bind/named.conf.local.tmp") || error("named.conf.local.tmp: $!"); |
| 108 |
while (<NAMED_CONF_IN>) { |
| 109 |
chomp; |
| 110 |
print NAMED_CONF_OUT "$_\n" |
| 111 |
unless /^include\s+\".*\";\s*\Q$named_conf_comment\E/; |
| 112 |
} |
| 113 |
close NAMED_CONF_IN; |
| 114 |
print NAMED_CONF_OUT "include \"$named_conf\"; $named_conf_comment\n"; |
| 115 |
close NAMED_CONF_OUT || error "close named.conf.tmp: $!"; |
| 116 |
shell("mv", "-f", "/etc/bind/named.conf.local.tmp", "/etc/bind/named.conf.local"); |
| 117 |
|
| 118 |
shell("service", "bind9", "restart"); |
| 119 |
} |
| 120 |
|
| 121 |
sub meta_letsencrypt { |
| 122 |
required => [qw{}], |
| 123 |
options => [qw{}], |
| 124 |
description => "use Lets Encrypt to get wildcard https certificates", |
| 125 |
section => "utility", |
| 126 |
} |
| 127 |
sub letsencrypt { |
| 128 |
assert_root(); |
| 129 |
|
| 130 |
my $certdir=$config{wildcard_ssl_cert_dir}; |
| 131 |
if (! defined $certdir || ! length $certdir) { |
| 132 |
error "ikiwiki-hosting.conf does not have wildcard_ssl_cert_dir configured"; |
| 133 |
} |
| 134 |
|
| 135 |
# Use a separate bind key for certbot. |
| 136 |
my @keys=dns_private_keys("certbot"); |
| 137 |
if (! @keys) { |
| 138 |
createkey("certbot"); |
| 139 |
setupbind(); |
| 140 |
} |
| 141 |
@keys=dns_private_keys("certbot"); |
| 142 |
my $secret = dns_keysecret($keys[0]); |
| 143 |
|
| 144 |
# This file needs to continue to exist so certbot can use it for |
| 145 |
# renewals. |
| 146 |
my $certbot_credfile="$config{keydir}/dns/certbot.credentials"; |
| 147 |
open(OUT, ">", $certbot_credfile) || error "$certbot_credfile: $!"; |
| 148 |
# contains keys, so needs to be only readable by root |
| 149 |
shell("chown", "root:root", $certbot_credfile); |
| 150 |
shell("chmod", "600", $certbot_credfile); |
| 151 |
print OUT "dns_rfc2136_server = 127.0.0.1\n"; |
| 152 |
print OUT "dns_rfc2136_port = 53\n"; |
| 153 |
print OUT "dns_rfc2136_name = certbot.\n"; |
| 154 |
print OUT "dns_rfc2136_secret = $secret\n"; |
| 155 |
print OUT "dns_rfc2136_algorithm = HMAC-MD5\n"; |
| 156 |
close OUT; |
| 157 |
|
| 158 |
foreach my $domain (split ' ', $config{domains}) { |
| 159 |
shell("certbot", "certonly", |
| 160 |
"--text", "--noninteractive", "--quiet", "--keep-until-expiring", |
| 161 |
"--agree-tos", "--email=$config{adminemail}", |
| 162 |
"--dns-rfc2136", |
| 163 |
"--dns-rfc2136-credentials", $certbot_credfile, |
| 164 |
# use ACMEv2 endpoint |
| 165 |
"--server", "https://acme-v02.api.letsencrypt.org/directory", |
| 166 |
# request cert for top level domain |
| 167 |
"--domain=$domain", |
| 168 |
# request wildcard cert |
| 169 |
"--domain=*.$domain"); |
| 170 |
my $livedir="/etc/letsencrypt/live/$domain"; |
| 171 |
|
| 172 |
# Symlink into place as wildcard cert, so ikisite will use it. |
| 173 |
shell("mkdir", "-p", "$certdir/$domain"); |
| 174 |
shell("ln", "-sf", "$livedir/cert.pem", "$certdir/$domain/cert.pem"); |
| 175 |
shell("ln", "-sf", "$livedir/privkey.pem", "$certdir/$domain/privkey.pem"); |
| 176 |
shell("ln", "-sf", "$livedir/fullchain.pem", "$certdir/$domain/fullchain.pem"); |
| 177 |
} |
| 178 |
|
| 179 |
print "Wildcard cert(s) installed, and will be used by ikisite when configuring sites\n"; |
| 180 |
} |
| 181 |
|
| 182 |
############################################################################# |
| 183 |
|
| 184 |
sub assert_client_haskey { |
| 185 |
my $client=shift; |
| 186 |
my $true=shift; |
| 187 |
|
| 188 |
my @keys=(dns_private_keys($client), dns_keys($client)); |
| 189 |
if (! $true && @keys) { |
| 190 |
error("dns key already exists for $client"); |
| 191 |
} |
| 192 |
elsif ($true && ! @keys) { |
| 193 |
error("dns key does not exist for $client"); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
sub dns_private_keys { |
| 198 |
my $client=shift || "*"; |
| 199 |
|
| 200 |
my @list=glob("$config{keydir}/dns/K$client.+*.private"); |
| 201 |
return @list; |
| 202 |
} |
| 203 |
|
| 204 |
sub dns_keys { |
| 205 |
my $client=shift || "*"; |
| 206 |
|
| 207 |
my @list=glob("$config{keydir}/dns/K$client.+*.key"); |
| 208 |
return @list; |
| 209 |
} |
| 210 |
|
| 211 |
sub dns_keyid { |
| 212 |
my $keyfile=shift; |
| 213 |
|
| 214 |
$keyfile=IkiWiki::basename($keyfile); |
| 215 |
$keyfile=~s/^K//; |
| 216 |
$keyfile=~s/\+.*//; |
| 217 |
return $keyfile; |
| 218 |
} |
| 219 |
|
| 220 |
sub dns_keysecret { |
| 221 |
my $keyfile=shift; |
| 222 |
|
| 223 |
my $file=readfile($keyfile); |
| 224 |
my ($key)=$file=~/^Key:\s+(.*)$/m; |
| 225 |
return $key; |
| 226 |
} |
| 227 |
|
| 228 |
main(); |