-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcertbot-lib.pl
265 lines (216 loc) · 6.77 KB
/
certbot-lib.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
#!/usr/bin/perl
BEGIN { push(@INC, ".."); };
use WebminCore;
use File::Basename;
my %wconfig = foreign_config('webmin');
init_config();
sub get_certbot_config
{
my $lref = &read_file_lines($config{'certbot_config'});
my @rv;
my $lnum = 0;
foreach my $line (@$lref) {
my ($n, $v) = split(/\s+/, $line, 2);
if ($n) {
push(@rv, { 'name' => $n, 'value' => $v, 'line' => $lnum });
}
$lnum++;
}
return @rv;
}
sub get_certbot_version{
local %version;
local $out = &backquote_command(get_certbot_cmd()." --version 2>&1");
if ($out =~ /(certbot)\s([0-9\.]+)/i) {
$version{'type'} = $1;
$version{'number'} = $2;
}else {
$version{'type'} = 'unknown';
$version{'number'} = 0.0;
}
return %version;
}
sub get_certbot_cmd(){
my $letsencrypt_cmd = 'not-found';
if ($wconfig{'letsencrypt_cmd'}) {
$letsencrypt_cmd = &has_command($wconfig{'letsencrypt_cmd'});
}else {
$letsencrypt_cmd = &has_command("letsencrypt") ||
&has_command("letsencrypt-auto") ||
&has_command("certbot") ||
&has_command("certbot-auto") ||
'not-installed';
}
return $letsencrypt_cmd;
}
sub get_certbot_group_options(){
my %groups = ('Optional' => ['max-log-backups', 'dry-run'],
'Security' => ['rsa-key-size', 'must-staple'],
'Testing' => ['staging'],
'Automation' => [ 'agree-tos', 'email', 'no-eff-email', 'eff-email',
'disable-hook-validation', 'no-directory-hooks',
'no-autorenew', 'disable-renew-updates'
]);
return %groups;
}
sub get_certbot_options(){
#Default option must be first in array!
my %opts = ('rsa-key-size' => ['2048','4096'],
'dry-run' => ['true', 'false'],
'max-log-backups' => ['0', '10', '20', '50', '100'],
'staging' => ['false', 'true'],
'must-staple' => ['false', 'true'],
'agree-tos' => ['false', 'true'],
'no-eff-email' => ['false', 'true'],
'eff-email' => ['false', 'true'],
'disable-hook-validation' => ['false', 'true'],
'no-directory-hooks' => ['false', 'true'],
'no-autorenew' => ['true', 'false'],
'disable-renew-updates' => ['false', 'true'],
'email' => ['textbox']
);
return %opts;
}
sub get_certbot_logdir(){
return $config{'certbot_logdir'};
}
sub get_certbot_certs_info(){
my @rv;
my %cert_info;
&open_execute_command(CMD, get_certbot_cmd().' certificates', 1);
while(my $line = <CMD>) {
if ($line =~ /\s+Certificate Name: (.*)/i) {
$cert_info{'name'} = $1;
$cert_info{'domains'} = '';
$cert_info{'expiry_date'} = '';
$cert_info{'staging'} = 'no';
$cert_info{'revoked'} = 'no';
$cert_info{'cert_path'} = '';
$cert_info{'key_path'} = '';
}elsif($line =~ /\s+Domains: (.*)/i){
($cert_info{'domains'} = $1) =~ s/ /,/g;
}elsif($line =~ /\s+Expiry Date: ([0-9\-]+ [0-9:\+]+) \(VALID: ([0-9a-z ]+)\)?/){
$cert_info{'expiry_date'} = $1;
$cert_info{'valid_for'} = $2;
}elsif($line =~ /\s+Expiry Date: ([0-9\-]+ [0-9:\+]+) (\(INVALID:[A-Z_ ,]+\))?/){
$cert_info{'expiry_date'} = $1;
my $flags = $2;
$flags =~ s/INVALID://;
$flags =~ s/[\s\(\)]//g;
foreach my $flag (split(',', $flags)){
if($flag eq 'TEST_CERT'){
$cert_info{'staging'} = 'yes';
}
if($flag eq 'REVOKED'){
$cert_info{'revoked'} = 'yes';
}
}
}elsif($line =~ /\s+Certificate Path: (.*)/){
$cert_info{'cert_path'} = $1;
}elsif($line =~ /\s+Private Key Path: (.*)/){
$cert_info{'key_path'} = $1;
push(@rv, {%cert_info});
}
}
close(CMD);
return @rv;
}
sub find_cert_usedin_apache{
my $href = shift;
my %rv = ();
foreign_require('apache', 'apache-lib.pl');
my $conf = &apache::get_config();
my @virt = &apache::find_directive_struct("VirtualHost", $conf);
foreach $v (@virt) { #for each virtual host
my $vm = $v->{'members'};
my $server_name = &apache::find_directive("ServerName", $vm);
if( ($server_name eq $href->{'name'}) ||
($server_name eq $href->{'name'}.':443') ){ #if its our vhost
my $vm_cert_file = &apache::find_directive("SSLCertificateKeyFile", $vm);
if($vm_cert_file eq $href->{'key_path'}){ #if its our cert file
my $virt_idx = &indexof($v, @$conf);
my ($vmembers, $vconf) = &apache::get_virtual_config($virt_idx);
$rv{$virt_idx} = $vconf->{'file'}; #certificate is installed
}
}
}
return %rv;
}
sub get_nginx_configs(){
my @nginx_dirs = ('/etc/nginx', '/etc/nginx/conf.d', '/etc/nginx/default.d');
my @configs = ();
foreach $d (@nginx_dirs){
if (-d $d) {
opendir(DIR, $d) or die $!;
my @d_conf = grep { $_ = "$d/$_"; -f && m/\.conf$/i } readdir(DIR);
closedir(DIR);
push(@configs, @d_conf);
}
}
return sort @configs;
}
sub file_find_pattern{
my $filepath = $_[0];
my $pattern = $_[1];
my $lref = &read_file_lines($filepath);
foreach my $line (@$lref) {
if($line =~ m/$pattern/){ #the line matches
return 1;
}
}
return 0;
}
sub find_cert_usedin_nginx{
my $href = shift;
my $pattern = 'ssl_certificate\s+'.$href->{'cert_path'};
my %rv = ();
$virt_idx = 0;
foreach $filepath (get_nginx_configs()) { #for each config
if(file_find_pattern($filepath, $href->{'cert_path'}) == 1){
$rv{$virt_idx} = $filepath;
$virt_idx++;
}
}
return %rv;
}
sub find_cert_usedin(){
my $href = shift;
my %rv = ();
#check which certificates are installed
if(&has_command('apache2') or &has_command('httpd')){ #if apache is installed and configured
return find_cert_usedin_apache($href);
}elsif(&has_command('nginx')){ #if apache is installed and configured
return find_cert_usedin_nginx($href);
}
return %rv;
}
sub get_cert_info(){
my $name = shift;
my @cert_info = get_certbot_certs_info();
foreach $href (@cert_info) {
if($href->{'name'} eq $name){
return $href;
}
}
return undef;
}
sub get_hooks_dir(){
return '/etc/letsencrypt/renewal-hooks';
}
sub get_certboot_hooks(){
my @all_hooks = ();
my @subdirs = ('pre', 'deploy', 'post');
foreach $subdir (@subdirs){
my $hooks_dir = get_hooks_dir().'/'.$subdir;
opendir(DIR, $hooks_dir) or return @hooks;
@hooks= grep {
! /^\./ # Doesn't begins with a period
&& -f "$hooks_dir/$_" # and is a file
} readdir(DIR);
closedir(DIR);
foreach $hook (@hooks){
push(@all_hooks, $subdir.'/'.$hook);
}
}
return sort @all_hooks;
}