-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathgenerate-acme-cert.pl
executable file
·368 lines (336 loc) · 10.9 KB
/
generate-acme-cert.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/local/bin/perl
=head1 generate-acme-cert.pl
Requests and installs an SSL cert for a virtual server.
The server must be specified with the C<--domain> flag, followed by a domain
name. By default the certificate will be the for either previously used
hostnames for a cert request, or the default SSL hostnames for the domain.
However, you can specify an alternate list of hostnames with the C<--host>
flag, which can be given multiple times. Or you can force use of the default
SSL hostname list with C<--default-hosts>.
The ACME SSL provider can be set with the C<--acme> flag followed by a numeric
ID or a provider type like C<zerossl>. If not set, the cert will be requested
from Let's Encrypt.
If the optional C<--renew> flag is given, automatic renewal will be configured
to occur when the certificate is close to expiry. You can also choose if email
is sent on every renewal with the C<--email-always> flag, only if renewal failed
with C<--email-error>, or never with C<--email-never>.
To have Virtualmin attempt to verify external Internet connectivity to your
domain before requesting the certificate, use the C<--check-first> flag. This
will detect common errors before your ACME provider service quota is consumed.
To have Virtualmin perform a local validation check of the domain, use the
C<--validate-first> flag. This is automatically enabled when C<--check-first>
is set.
By default Virtualmin will attempt to perform an external DNS lookup of all
domain names that the certificate is requested for, to make sure they can be
resolved by the SSL provider. To disable this check, use the
C<--skip-dns-check> flag. Or to forcible enable it because it was disabled
for the domain in the UI, use the C<--dns-check> flag.
Alternately, you can use the C<--allow-subset> flag to have the SSL provider
exclude any hostnames that cannot be resolved or validated from the certificate.
Otherwise, failure of any hostname will block the entire request.
By default both web and DNS validation will be attempted by the SSL provider for
domain ownership, but you can select just one with either the C<--web> or
C<--dns> flags.
=cut
package virtual_server;
if (!$module_name) {
$main::no_acl_check++;
$ENV{'WEBMIN_CONFIG'} ||= "/etc/webmin";
$ENV{'WEBMIN_VAR'} ||= "/var/webmin";
if ($0 =~ /^(.*)\/[^\/]+$/) {
chdir($pwd = $1);
}
else {
chop($pwd = `pwd`);
}
$0 = "$pwd/generate-acme-cert.pl";
require './virtual-server-lib.pl';
$< == 0 || die "generate-acme-cert.pl must be run as root";
}
@OLDARGV = @ARGV;
&set_all_text_print();
# Parse command-line args
while(@ARGV > 0) {
local $a = shift(@ARGV);
if ($a eq "--domain") {
$dname = shift(@ARGV);
}
elsif ($a eq "--host") {
push(@dnames, lc(shift(@ARGV)));
}
elsif ($a eq "--default-hosts") {
$defdnames = 1;
}
elsif ($a eq "--multiline") {
$multiline = 1;
}
elsif ($a eq "--renew") {
if ($ARGV[0] =~ /^[0-9\.]+$/) {
# Ignore months flag now
shift(@ARGV);
}
$renew = 1;
}
elsif ($a eq "--size") {
$size = shift(@ARGV);
$size =~ /^\d+$/ ||
&usage("--size must be followed by a number of bits");
}
elsif ($a eq "--staging") {
$staging = 1;
}
elsif ($a eq "--check-first") {
$connectivity = 1;
}
elsif ($a eq "--validate-first") {
$validation = 1;
}
elsif ($a eq "--skip-dns-check") {
$nodnscheck = 1;
}
elsif ($a eq "--dns-check") {
$nodnscheck = 0;
}
elsif ($a eq "--allow-subset") {
$subset = 1;
}
elsif ($a =~ /^--(web|dns)$/) {
$mode = $1;
}
elsif ($a =~ /^--(sha1|sha2|rsa|ec)$/) {
$ctype = $1 eq "sha1" || $1 eq "sha2" ? "rsa" : $1;
}
elsif ($a eq "--email-always") {
$email = 0;
}
elsif ($a eq "--email-never") {
$email = 2;
}
elsif ($a eq "--email-error") {
$email = 1;
}
elsif ($a eq "--acme") {
$acmeid = shift(@ARGV);
}
elsif ($a eq "--help") {
&usage();
}
else {
&usage("Unknown parameter $a");
}
}
# Validate inputs
$dname || &usage("Missing --domain parameter");
$d = &get_domain_by("dom", $dname);
$d || &usage("No virtual server named $dname found");
$d->{'ssl_same'} && &usage("This server shares it's SSL certificate ".
"with another domain");
if ($ctype =~ /^ec/) {
&letsencrypt_supports_ec() ||
&usage("The ACME client on your system does ".
"not support EC certificates");
}
$ctype ||= ($d->{'letsencrypt_ctype'} || "rsa");
if (!@dnames) {
# No hostnames specified
if ($defdnames || !$d->{'letsencrypt_dname'}) {
# Use default hostnames
@dnames = &get_hostnames_for_ssl($d);
$custom_dname = undef;
}
else {
# Use hostnames from last time
@dnames = split(/\s+/, $d->{'letsencrypt_dname'});
$custom_dname = $d->{'letsencrypt_dname'};
}
push(@dnames, "*.".$d->{'dom'}) if ($d->{'letsencrypt_dwild'});
}
else {
# Hostnames given
foreach my $dname (@dnames) {
my $checkname = $dname;
$checkname =~ s/^www\.//;
$checkname =~ s/^\*\.//;
$err = &valid_domain_name($checkname);
&usage($err) if ($err);
}
$custom_dname = join(" ", @dnames);
}
if ($acmeid) {
defined(&list_acme_providers) ||
&usage("The --acme flag is only available in Virtualmin Pro");
($acme) = grep { $_->{'id'} eq $acmeid ||
$_->{'type'} eq $acmeid } &list_acme_providers();
$acme || &usage("No ACME provider with ID of type $acmeid found");
}
elsif (defined(&list_acme_providers)) {
($acme) = grep { $_->{'id'} eq $d->{'letsencrypt_id'} }
&list_acme_providers();
}
# Build a list of the domains being validated
my @cdoms = ( $d );
if (!$d->{'alias'} && !$custom_dname) {
push(@cdoms, grep { &domain_has_website($_) }
&get_domain_by("alias", $d->{'id'}));
}
# Check for external connectivity first
if ($connectivity && defined(&check_domain_connectivity)) {
my @errs;
foreach my $cd (@cdoms) {
push(@errs, &check_domain_connectivity($cd,
{ 'mail' => 1, 'ssl' => 1 }));
}
if (@errs) {
print "Connectivity check failed :\n";
foreach my $e (@errs) {
print "ERROR: $e->{'desc'} : $e->{'error'}\n";
}
exit(1);
}
}
# If doing a connectivity check, also do web and DNS validation
if ($connectivity || $validation) {
my $vcheck = ['web'];
foreach my $dn (@dnames) {
$vcheck = ['dns'] if ($dn =~ /\*/);
}
my @errs = map { &validate_letsencrypt_config($_, $vcheck) } @cdoms;
if (@errs) {
print "Validation check failed :\n";
foreach my $e (@errs) {
print "ERROR: $e->{'desc'} : $e->{'error'}\n";
}
exit(1);
}
}
# Filter hostnames down to those that can be resolved
$nodnscheck = $d->{'letsencrypt_nodnscheck'} if (!defined($nodnscheck));
if (!$nodnscheck) {
&$first_print("Checking hostnames for resolvability ..");
my @badnames;
my $fok = &filter_external_dns(\@dnames, \@badnames);
if ($fok < 0) {
&$second_print(".. check could not be performed!");
}
elsif ($fok) {
&$second_print(".. all hostnames can be resolved");
}
elsif (!@dnames) {
&$second_print(".. none of the hostnames could be resolved!");
exit(1);
}
else {
&$second_print(".. some hostnames were removed : ".
join(', ', map { "<tt>$_</tt>" } @badnames));
}
}
# Run the before command
&set_domain_envs($d, "SSL_DOMAIN");
my $merr = &making_changes();
&usage($merr) if ($merr);
&reset_domain_envs($d);
# Request the cert
&foreign_require("webmin");
$phd = &public_html_dir($d);
if ($acme) {
&$first_print("Requesting SSL certificate for ".join(" ", @dnames).
" from $acme->{'desc'} ..");
}
else {
&$first_print("Requesting SSL certificate for ".join(" ", @dnames)." ..");
}
$before = &before_letsencrypt_website($d);
@beforecerts = &get_all_domain_service_ssl_certs($d);
($ok, $cert, $key, $chain) = &request_domain_letsencrypt_cert(
$d, \@dnames, $staging, $size, $mode, $ctype, $acme, $subset);
&after_letsencrypt_website($d, $before);
if (!$ok) {
# Always store last Certbot error
&lock_domain($d);
$d->{'letsencrypt_last_failure'} = time();
$d->{'letsencrypt_last_err'} = $cert;
$d->{'letsencrypt_last_err'} =~ s/\r?\n/\t/g;
&save_domain($d);
&unlock_domain($d);
&$second_print(".. failed : $cert");
exit(1);
}
else {
$info = &cert_file_info($cert);
@gotnames = &unique($info->{'cn'}, @{$info->{'alt'}});
if (scalar(@gotnames) == scalar(@dnames)) {
&$second_print(".. done for all hostnames");
}
else {
&$second_print(".. done for ".join(", ", @gotnames));
}
# Worked .. copy to the domain
&obtain_lock_ssl($d);
&$first_print("Copying to server configuration ..");
&install_letsencrypt_cert($d, $cert, $key, $chain);
# Save renewal state
$d->{'letsencrypt_dname'} = $custom_dname;
$d->{'letsencrypt_last'} = time();
$d->{'letsencrypt_last_success'} = time();
$d->{'letsencrypt_renew'} = $renew;
$d->{'letsencrypt_ctype'} = $ctype =~ /^ec/ ? "ecdsa" : "rsa";
$d->{'letsencrypt_size'} = $size;
$d->{'letsencrypt_id'} = $acme ? $acme->{'id'} : undef;
$d->{'letsencrypt_nodnscheck'} = $nodnscheck;
$d->{'letsencrypt_subset'} = $subset;
$d->{'letsencrypt_email'} = $email;
delete($d->{'letsencrypt_last_err'});
&refresh_ssl_cert_expiry($d);
&save_domain($d);
# Update other services using the cert
&update_all_domain_service_ssl_certs($d, \@beforecerts);
# For domains that were using the SSL cert on this domain originally but
# can no longer due to the cert hostname changing, break the linkage
&break_invalid_ssl_linkages($d);
# Copy SSL directives to domains using same cert
foreach $od (&get_domain_by("ssl_same", $d->{'id'})) {
next if (!&domain_has_ssl_cert($od));
$od->{'ssl_cert'} = $d->{'ssl_cert'};
$od->{'ssl_key'} = $d->{'ssl_key'};
$od->{'ssl_newkey'} = $d->{'ssl_newkey'};
$od->{'ssl_csr'} = $d->{'ssl_csr'};
$od->{'ssl_pass'} = $d->{'ssl_pass'};
&save_domain_passphrase($od);
&save_domain($od);
}
# Update DANE DNS records
&sync_domain_tlsa_records($d);
foreach $od (&get_domain_by("ssl_same", $d->{'id'})) {
&sync_domain_tlsa_records($od);
}
&release_lock_ssl($d);
&$second_print(".. done");
&run_post_actions();
# Call the post command
&set_domain_envs($d, "SSL_DOMAIN");
&made_changes();
&reset_domain_envs($d);
&virtualmin_api_log(\@OLDARGV, $d);
}
sub usage
{
print "$_[0]\n\n" if ($_[0]);
print "Requests and installs an SSL cert for a virtual server.\n";
print "\n";
print "virtualmin generate-acme-cert --domain name\n";
print " [--host hostname]*\n";
print " [--default-hosts]\n";
print " [--renew]\n";
print " [--size bits]\n";
print " [--staging]\n";
print " [--check-first | --validate-first]\n";
print " [--skip-dns-check | --dns-check]\n";
print " [--allow-subset]\n";
print " [--email-always |\n";
print " --email-never |\n";
print " --email-error]\n";
print " [--web | --dns]\n";
print " [--rsa | --ec]\n";
print " [--acme id|provider]\n";
exit(1);
}