-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathletsencrypt.cgi
executable file
·252 lines (229 loc) · 7.44 KB
/
letsencrypt.cgi
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
#!/usr/local/bin/perl
# Request and install a cert and key from Let's Encrypt
require './virtual-server-lib.pl';
&ReadParse();
&error_setup($text{'letsencrypt_err'});
$d = &get_domain($in{'dom'});
&can_edit_domain($d) && &can_edit_ssl() && &can_edit_letsencrypt() &&
(&domain_has_website($d) || $d->{'dns'}) ||
&error($text{'edit_ecannot'});
$d->{'disabled'} && &error($text{'letsencrypt_eenabled'});
if ($in{'dname_def'}) {
@dnames = &get_hostnames_for_ssl($d);
$custom_dname = undef;
}
else {
foreach my $dname (split(/\s+/, $in{'dname'})) {
$dname = lc(&parse_domain_name($dname));
my $checkname = $dname;
$checkname =~ s/^www\.//;
$checkname =~ s/^\*\.//;
$err = &valid_domain_name($checkname);
&error($err) if ($err);
push(@dnames, $dname);
}
$custom_dname = join(" ", @dnames);
}
@dnames || &error($text{'letsencrypt_ednames'});
push(@dnames, "*.".$d->{'dom'}) if ($in{'dwild'});
# Filter wildcard to prevent redundancy
my $fdnames = &filter_ssl_wildcards(\@dnames);
@dnames = @$fdnames;
if ($in{'only'}) {
# Just update renewal date and domains
$d->{'letsencrypt_dname'} = $custom_dname;
$d->{'letsencrypt_dwild'} = $in{'dwild'};
$d->{'letsencrypt_renew'} = $in{'renew'};
$d->{'letsencrypt_nodnscheck'} = !$in{'dnscheck'};
$d->{'letsencrypt_subset'} = $in{'subset'};
$d->{'letsencrypt_email'} = $in{'email'};
$d->{'letsencrypt_id'} = $in{'acme'} if (defined($in{'acme'}));
&save_domain($d);
&redirect("cert_form.cgi?dom=$d->{'id'}");
}
else {
&ui_print_unbuffered_header(&domain_in($d),
$text{'letsencrypt_title'}, "");
# Build list of domains
my @cdoms = ( $d );
if (!$d->{'alias'} && $in{'dname_def'}) {
push(@cdoms, grep { &domain_has_website($_) }
&get_domain_by("alias", $d->{'id'}));
}
# Validate connectivity
if ($in{'connectivity'} == 2) {
&$first_print(&text('letsencrypt_conncheck',
join(" ", map { &show_domain_name($_) } @cdoms)));
my @errs;
foreach my $cd (@cdoms) {
push(@errs, &check_domain_connectivity($cd,
{ 'mail' => 1, 'ssl' => 1 }));
}
if (@errs) {
&$second_print($text{'letsencrypt_connerrs'});
print "<ul>\n";
foreach my $e (@errs) {
print "<li>",$e->{'desc'}," : ",
$e->{'error'},"\n";
}
print "</ul>\n";
&ui_print_footer(&domain_footer_link($d),
"", $text{'index_return'});
return;
}
else {
&$second_print($text{'letsencrypt_connok'});
}
}
# Validate config
if ($in{'connectivity'} >= 1) {
&$first_print(&text('letsencrypt_validcheck',
join(" ", map { &show_domain_name($_) } @cdoms)));
my $vcheck = $in{'dwild'} ? ['dns'] : ['web'];
my @errs = map { &validate_letsencrypt_config($_, $vcheck) } @cdoms;
if (@errs) {
&$second_print($text{'letsencrypt_connerrs'});
print "<ul>\n";
foreach my $e (@errs) {
print "<li>",$e->{'desc'}," : ",
$e->{'error'},"\n";
}
print "</ul>\n";
&ui_print_footer(&domain_footer_link($d),
"", $text{'index_return'});
return;
}
else {
&$second_print($text{'letsencrypt_connok'});
}
}
# Filter down hostnames to those that can be resolved
if ($in{'dnscheck'}) {
&$first_print($text{'letsencrypt_dnscheck'});
my @badnames;
my $fok = &filter_external_dns(\@dnames, \@badnames);
if ($fok < 0) {
&$second_print($text{'letsencrypt_ednscheck'});
}
elsif ($fok) {
&$second_print($text{'letsencrypt_dnscheckok'});
}
elsif (!@dnames) {
&$second_print($text{'letsencrypt_dnscheckall'});
goto FAILED;
}
else {
&$second_print(&text('letsencrypt_dnscheckbad',
join(', ', map { "<tt>$_</tt>" } @badnames)));
}
}
# Run the before command
&set_domain_envs($d, "SSL_DOMAIN");
$merr = &making_changes();
&reset_domain_envs($d);
&error(&text('setup_emaking', "<tt>$merr</tt>")) if (defined($merr));
$dlist = join(", ", map { "<tt>$_</tt>" } @dnames);
if (defined($in{'acme'})) {
($acme) = grep { $_->{'id'} eq $in{'acme'} }
&list_acme_providers();
$acme || &error($text{'letsencrypt_eacme'});
&can_acme_provider($acme) ||
&error($text{'letsencrypt_eacme2'});
if ($acme->{'type'}) {
($prov) = grep { $_->{'id'} eq $acme->{'type'} }
&list_known_acme_providers();
}
&$first_print(&text('letsencrypt_doing2a', $dlist,
$prov ? $prov->{'desc'} : $acme->{'desc'}));
}
else {
&$first_print(&text('letsencrypt_doing2', $dlist));
}
&foreign_require("webmin");
$phd = &public_html_dir($d);
$before = &before_letsencrypt_website($d);
($ok, $cert, $key, $chain) = &request_domain_letsencrypt_cert(
$d, \@dnames, 0, undef, undef,
$in{'ctype'}, $acme, $in{'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(&text('letsencrypt_failed', $cert));
}
else {
$info = &cert_file_info($cert);
@gotnames = &unique($info->{'cn'}, @{$info->{'alt'}});
if (scalar(@gotnames) == scalar(@dnames)) {
&$second_print(&text('letsencrypt_done'));
}
else {
&$second_print(&text('letsencrypt_done2',
join(", ", map { "<tt>$_</tt>" } @gotnames)));
}
# Figure out which services (webmin, postfix, etc)
# were using the old cert
@beforecerts = &get_all_domain_service_ssl_certs($d);
# Worked .. copy to the domain
&obtain_lock_ssl($d);
&$first_print($text{'newkey_apache'});
&install_letsencrypt_cert($d, $cert, $key, $chain);
# Save renewal state
$d->{'letsencrypt_dname'} = $custom_dname;
$d->{'letsencrypt_dwild'} = $in{'dwild'};
$d->{'letsencrypt_renew'} = $in{'renew'};
$d->{'letsencrypt_ctype'} = $in{'ctype'} =~ /^ec/ ? "ecdsa" : "rsa";
$d->{'letsencrypt_last'} = time();
$d->{'letsencrypt_last_success'} = time();
$d->{'letsencrypt_nodnscheck'} = !$in{'dnscheck'};
$d->{'letsencrypt_subset'} = $in{'subset'};
$d->{'letsencrypt_email'} = $in{'email'};
$d->{'letsencrypt_id'} = $acme->{'id'} if ($acme);
delete($d->{'letsencrypt_last_err'});
&refresh_ssl_cert_expiry($d);
&save_domain($d);
&$second_print($text{'setup_done'});
# Update other services using the cert
&$first_print($text{'cert_updatesvcs'});
&update_all_domain_service_ssl_certs($d, \@beforecerts);
&$second_print($text{'setup_done'});
# 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);
# Run the after command
&set_domain_envs($d, "SSL_DOMAIN");
local $merr = &made_changes();
&$second_print(&text('setup_emade', "<tt>$merr</tt>"))
if (defined($merr));
&reset_domain_envs($d);
&run_post_actions();
&webmin_log("letsencrypt", "domain", $d->{'dom'}, $d);
}
FAILED:
&ui_print_footer("cert_form.cgi?dom=$in{'dom'}", $text{'cert_return'},
&domain_footer_link($d),
"", $text{'index_return'});
}