1 |
#! /usr/bin/perl -w |
2 |
# |
3 |
# @(#)$Id$ |
4 |
# |
5 |
# Copyright 2010 David Groep, Nationaal instituut voor |
6 |
# subatomaire fysica NIKHEF |
7 |
# |
8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
9 |
# you may not use this file except in compliance with the License. |
10 |
# You may obtain a copy of the License at |
11 |
# |
12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
13 |
# |
14 |
# Unless required by applicable law or agreed to in writing, software |
15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 |
# See the License for the specific language governing permissions and |
18 |
# limitations under the License. |
19 |
# |
20 |
# |
21 |
package main; |
22 |
|
23 |
use strict; |
24 |
use Getopt::Long qw(:config no_ignore_case bundling); |
25 |
use POSIX; |
26 |
eval { require LWP or die; }; $@ and die "Please install libwww-perl (LWP)\n"; |
27 |
|
28 |
# import modules that are needed but still external |
29 |
# (the installed version may have these packages embedded in-line) |
30 |
# |
31 |
require ConfigTiny and import ConfigTiny unless defined &ConfigTiny::new; |
32 |
require TrustAnchor and import TrustAnchor unless defined &TrustAnchor::new; |
33 |
require CRLWriter and import CRLWriter unless defined &CRLWriter::new; |
34 |
require FCLog and import FCLog unless defined &FCLog::new; |
35 |
require OSSL and import OSSL unless defined &OSSL::new; |
36 |
require CRL and import CRL unless defined &CRL::new; |
37 |
|
38 |
my $use_DataDumper = eval { require Data::Dumper; }; |
39 |
my $use_IOSelect = eval { require IO::Select; }; |
40 |
|
41 |
use vars qw/ $log $cnf /; |
42 |
|
43 |
|
44 |
# ########################################################################### |
45 |
# |
46 |
# |
47 |
($cnf,$log) = &init_configuration(); |
48 |
|
49 |
# verify local installation sanity for loaded modules |
50 |
$::log->getverbose > 6 and ! $use_DataDumper and |
51 |
$::log->err("Cannot set verbosity higher than 6 without Data::Dumper") and |
52 |
exit(1); |
53 |
$::cnf->{_}->{parallelism} and ! $use_IOSelect and |
54 |
$::log->err("Cannot use parallel retrieval without IO::Select") and |
55 |
exit(1); |
56 |
|
57 |
$use_DataDumper and $::log->verb(7,Data::Dumper::Dumper($cnf)); |
58 |
|
59 |
# set safe path if so requested |
60 |
$cnf->{_}->{path} and $ENV{"PATH"} = $cnf->{_}->{path} and |
61 |
$::log->verb(5,"Set PATH to",$ENV{"PATH"}); |
62 |
|
63 |
# wait up to randomwait seconds to spread download load |
64 |
$cnf->{_}->{randomwait} and do { |
65 |
my $wtime = int(rand($cnf->{_}->{randomwait})); |
66 |
$::log->verb(2,"Sleeping $wtime seconds before continuing"); |
67 |
sleep($wtime); |
68 |
}; |
69 |
|
70 |
|
71 |
# the list of trust anchors to process comes from the command line and |
72 |
# all files in the infodir that are metadata or crl urls |
73 |
# in the next phase, the suffix will be stripped and the info file |
74 |
# when present preferred over the crlurl |
75 |
# |
76 |
my @metafiles = @ARGV; |
77 |
$::cnf->{_}->{"infodir"} and do { |
78 |
foreach my $fn ( |
79 |
map { glob ( $::cnf->{_}->{"infodir"} . "/$_" ); } "*.info", "*.crl_url" |
80 |
) { |
81 |
next if $::cnf->{_}->{nosymlinks} and -l $fn; |
82 |
$fn =~ /.*\/([^\/]+)(\.crl_url|\.info)$/; |
83 |
push @metafiles, $1 unless grep /^$1$/,@metafiles or not defined $1; |
84 |
} |
85 |
}; |
86 |
|
87 |
@metafiles or |
88 |
$log->err("No trust anchors to process") and exit($log->exitstatus); |
89 |
|
90 |
if ( $::cnf->{_}->{parallelism} ) { |
91 |
¶llel_metafiles($::cnf->{_}->{parallelism}, @metafiles); |
92 |
} else { |
93 |
&process_metafiles( @metafiles ); |
94 |
} |
95 |
|
96 |
$log->flush; |
97 |
exit($log->exitstatus); |
98 |
|
99 |
|
100 |
# ########################################################################### |
101 |
# |
102 |
# |
103 |
sub init_configuration() { |
104 |
my ($cnf,$log); |
105 |
|
106 |
my ($configfile,$agingtolerance,$infodir,$statedir,$cadir,$httptimeout); |
107 |
my ($output); |
108 |
my @formats; |
109 |
my $verbosity; |
110 |
my $quiet=0; |
111 |
my $help=0; |
112 |
my $debuglevel; |
113 |
my $parallelism=0; |
114 |
my $randomwait; |
115 |
my $nosymlinks; |
116 |
my $cfgdir; |
117 |
|
118 |
$log = FCLog->new("qualified"); |
119 |
|
120 |
&GetOptions( |
121 |
"c|config=s" => \$configfile, |
122 |
"l|infodir=s" => \$infodir, |
123 |
"cadir=s" => \$cadir, |
124 |
"s|statedir=s" => \$statedir, |
125 |
"cfgdir=s" => \$cfgdir, |
126 |
"T|httptimeout=i" => \$httptimeout, |
127 |
"o|output=s" => \$output, |
128 |
"format=s@" => \@formats, |
129 |
"v|verbose+" => \$verbosity, |
130 |
"h|help+" => \$help, |
131 |
"q|quiet+" => \$quiet, |
132 |
"d|debug+" => \$debuglevel, |
133 |
"p|parallelism=i" => \$parallelism, |
134 |
"nosymlinks+" => \$nosymlinks, |
135 |
"a|agingtolerance=i" => \$agingtolerance, |
136 |
"r|randomwait=i" => \$randomwait, |
137 |
) or &help and exit(1); |
138 |
|
139 |
$help and &help and exit(0); |
140 |
|
141 |
$configfile ||= ( -e "/etc/fetch-crl.conf" and "/etc/fetch-crl.conf" ); |
142 |
$configfile ||= ( -e "/etc/fetch-crl.cnf" and "/etc/fetch-crl.cnf" ); |
143 |
($quiet > 0) and $verbosity = -$quiet; |
144 |
|
145 |
$cnf = ConfigTiny->new(); |
146 |
$configfile and |
147 |
$cnf->read($configfile) || die "Invalid config file $configfile:\n " . |
148 |
$cnf->errstr . "\n"; |
149 |
|
150 |
( defined $cnf->{_}->{cfgdir} and $cfgdir = $cnf->{_}->{cfgdir} ) |
151 |
unless defined $cfgdir; |
152 |
$cfgdir ||= "/etc/fetch-crl.d"; |
153 |
if ( defined $cfgdir and -d $cfgdir and opendir(my $dh,$cfgdir) ) { |
154 |
while ( my $fn = readdir $dh ) { |
155 |
-f "$cfgdir/$fn" and -r "$cfgdir/$fn" and $cnf->read("$cfgdir/$fn"); |
156 |
} |
157 |
close $dh; |
158 |
} |
159 |
|
160 |
# command-line option overrides |
161 |
$cnf->{_}->{agingtolerance} = $agingtolerance if defined $agingtolerance; |
162 |
$cnf->{_}->{infodir} = $infodir if defined $infodir; |
163 |
$cnf->{_}->{cadir} = $cadir if defined $cadir; |
164 |
$cnf->{_}->{statedir} = $statedir if defined $statedir; |
165 |
$cnf->{_}->{httptimeout} = $httptimeout if defined $httptimeout; |
166 |
$cnf->{_}->{verbosity} = $verbosity if defined $verbosity; |
167 |
$cnf->{_}->{debuglevel} = $debuglevel if defined $debuglevel; |
168 |
$cnf->{_}->{output} = $output if defined $output; |
169 |
$cnf->{_}->{formats} = join "\001",@formats if @formats; |
170 |
$cnf->{_}->{parallelism} = $parallelism if $parallelism; |
171 |
$cnf->{_}->{randomwait} = $randomwait if defined $randomwait; |
172 |
$cnf->{_}->{nosymlinks} = $nosymlinks if defined $nosymlinks; |
173 |
|
174 |
# key default values |
175 |
defined $cnf->{_}->{version} or $cnf->{_}->{version} = "3+"; |
176 |
defined $cnf->{_}->{packager} or $cnf->{_}->{packager} = "EUGridPMA"; |
177 |
defined $cnf->{_}->{openssl} or $cnf->{_}->{openssl} = "openssl"; |
178 |
defined $cnf->{_}->{agingtolerance} or $cnf->{_}->{agingtolerance} ||= 24; |
179 |
defined $cnf->{_}->{infodir} or $cnf->{_}->{infodir} = '/etc/grid-security/certificates'; |
180 |
defined $cnf->{_}->{output} or $cnf->{_}->{output} = $cnf->{_}->{infodir}; |
181 |
defined $cnf->{_}->{cadir} or $cnf->{_}->{cadir} = $cnf->{_}->{infodir}; |
182 |
defined $cnf->{_}->{statedir} or $cnf->{_}->{statedir} = "/var/cache/fetch-crl" if -d "/var/cache/fetch-crl" and -w "/var/cache/fetch-crl"; |
183 |
defined $cnf->{_}->{formats} or $cnf->{_}->{formats} = "openssl"; |
184 |
defined $cnf->{_}->{opensslmode} or $cnf->{_}->{opensslmode} = "dual"; |
185 |
defined $cnf->{_}->{httptimeout} or $cnf->{_}->{httptimeout} = 120; |
186 |
defined $cnf->{_}->{nametemplate_der} or |
187 |
$cnf->{_}->{nametemplate_der} = "\@ANCHORNAME\@.\@R\@.crl"; |
188 |
defined $cnf->{_}->{nametemplate_pem} or |
189 |
$cnf->{_}->{nametemplate_pem} = "\@ANCHORNAME\@.\@R\@.crl.pem"; |
190 |
defined $cnf->{_}->{catemplate} or |
191 |
$cnf->{_}->{catemplate} = "\@ALIAS\@.pem\001". |
192 |
"\@ALIAS\@.\@R\@\001\@ANCHORNAME\@.\@R\@"; |
193 |
|
194 |
$cnf->{_}->{nonssverify} ||= 0; |
195 |
$cnf->{_}->{nocache} ||= 0; |
196 |
$cnf->{_}->{nosymlinks} ||= 0; |
197 |
$cnf->{_}->{verbosity} ||= 0; |
198 |
$cnf->{_}->{debuglevel} ||= 0; |
199 |
|
200 |
$cnf->{_}->{stateless} and delete $cnf->{_}->{statedir}; |
201 |
|
202 |
# expand array keys in config |
203 |
defined $cnf->{_}->{formats} and |
204 |
@{$cnf->{_}->{formats_}} = split(/[\001;,\s]+/,$cnf->{_}->{formats}); |
205 |
|
206 |
# sanity check on configuration |
207 |
$cnf->{_}->{statedir} and ! -d $cnf->{_}->{statedir} and |
208 |
die "Invalid state directory " . $cnf->{_}->{statedir} . "\n"; |
209 |
$cnf->{_}->{infodir} and ! -d $cnf->{_}->{infodir} and |
210 |
die "Invalid meta-data directory ".$cnf->{_}->{infodir}."\n"; |
211 |
|
212 |
# initialize logging |
213 |
$log->flush; |
214 |
$cnf->{_}->{logmode} and $log->destremove("qualified") and do { |
215 |
foreach ( split(/[,\001]+/,$cnf->{_}->{logmode}) ) { |
216 |
if ( /^syslog$/ ) { $log->destadd($_,$cnf->{_}->{syslogfacility}); } |
217 |
elsif ( /^(direct|qualified|cache)$/ ) { $log->destadd($_); } |
218 |
else { die "Invalid log destination $_, exiting.\n"; } |
219 |
} |
220 |
}; |
221 |
$log->setverbose($cnf->{_}->{verbosity}); |
222 |
$log->setdebug($cnf->{_}->{debuglevel}); |
223 |
|
224 |
return ($cnf,$log); |
225 |
} |
226 |
|
227 |
# ########################################################################### |
228 |
# |
229 |
# |
230 |
sub help() { |
231 |
(my $name = $0) =~ s/.*\///; |
232 |
print <<EOHELP; |
233 |
The fetch-crl utility will retrieve certificate revocation lists (CRLs) for |
234 |
a set of installed trust anchors, based on crl_url files or IGTF-style info |
235 |
files. It will install these for use with OpenSSL, NSS or third-party tools. |
236 |
|
237 |
Usage: $name [-c|--config configfile] [-l|--infodir path] |
238 |
[--cadir path] [-s|--statedir path] [-o|--output path] [--format \@formats] |
239 |
[-T|--httptimeout seconds] [-p|--parallelism n] [--nosymlinks] |
240 |
[-a|--agingtolerance hours] [-r|--randomwait seconds] |
241 |
[-v|--verbose] [-h|--help] [-q|--quiet] [-d|--debug level] |
242 |
|
243 |
Options: |
244 |
-c | --config path |
245 |
Read configuration data from path, default: /etc/fetch-crl.conf |
246 |
-l | --infodir path |
247 |
Location of the trust anchor meta-data files (crl_url or info), |
248 |
default: /etc/grid-security/certificates |
249 |
--cadir path |
250 |
Location of the trust anchors (default to infodir) |
251 |
-s | --statedir path |
252 |
Location of the historic state data (for caching and delayed-warning) |
253 |
-T | --httptimeout sec |
254 |
Maximum time in seconds to wait for retrieval or a single URL |
255 |
-o | --output path |
256 |
Location of the CRLs written (global default, defaults to infodir |
257 |
--format \@formats |
258 |
Format(s) in which the CRLs will be written (openssl, pem, der, nss) |
259 |
--nosymlinks |
260 |
Do not include meta-data files that are symlinks |
261 |
-v | --verbose |
262 |
Become more talkative |
263 |
-q | --quiet |
264 |
Become really quiet (overrides verbosity) |
265 |
-p | --parallelism n |
266 |
Run up to n parallel trust anchor retrieval processes |
267 |
-a | --agingtolerance hours |
268 |
Be quiet for up to hours hours before raising an error. Until |
269 |
the tolerance has passed, only warnings are raised |
270 |
-r | --randomwait seconds |
271 |
Introduce a random delay of up to seconds seconds before starting |
272 |
any retrieval processes |
273 |
-h | --help |
274 |
This help text |
275 |
|
276 |
EOHELP |
277 |
|
278 |
return 1; |
279 |
} |
280 |
|
281 |
# ########################################################################### |
282 |
# |
283 |
# |
284 |
sub process_metafiles(@) { |
285 |
my @metafiles = @_; |
286 |
|
287 |
foreach my $f ( @metafiles ) { |
288 |
my $ta = TrustAnchor->new(); |
289 |
$cnf->{_}->{"infodir"} and $ta->setInfodir($cnf->{_}->{"infodir"}); |
290 |
$ta->loadAnchor($f) or next; |
291 |
$ta->saveLogMode() and $ta->setLogMode(); |
292 |
$ta->loadState() or next; |
293 |
$ta->retrieve or next; |
294 |
$ta->loadCAfiles() or next; |
295 |
$ta->verifyAndConvertCRLs or next; |
296 |
|
297 |
my $writer = CRLWriter->new($ta); |
298 |
$writer->writeall() or next; |
299 |
$ta->saveState() or next; |
300 |
$ta->restoreLogMode(); |
301 |
} |
302 |
|
303 |
return 1; |
304 |
} |
305 |
|
306 |
sub parallel_metafiles($@) { |
307 |
my $parallelism = shift; |
308 |
my @metafiles = @_; |
309 |
|
310 |
my %pids = (); # file handle by processID |
311 |
my %metafile_by_fh = (); # reverse map |
312 |
my $readset = new IO::Select(); |
313 |
my %logoutput = (); |
314 |
|
315 |
$| = 1; |
316 |
|
317 |
$::log->verb(2,"starting up to $parallelism worker processes"); |
318 |
|
319 |
while ( @metafiles or scalar keys %pids ) { |
320 |
# loop until we have started all possible retrievals AND have |
321 |
# collected all possible output |
322 |
|
323 |
( @metafiles and (scalar keys %pids < $parallelism) ) and do { |
324 |
# we have metafiles left, and have spare process slots |
325 |
my $metafile = shift @metafiles; |
326 |
|
327 |
|
328 |
$logoutput{$metafile} = ""; |
329 |
|
330 |
my $cout; |
331 |
my $cpid = open $cout, "-|"; |
332 |
defined $cpid and defined $cout or |
333 |
$::log->err("Cannot fork ($metafile): $!") and next; |
334 |
|
335 |
$::log->verb(5,"LOOP: starting process $cpid for $metafile"); |
336 |
|
337 |
if ( $cpid == 0 ) { # I'm the child that should care for $metafile |
338 |
$0 = "fetch-crl worker $metafile"; |
339 |
$::log->cleanse(); |
340 |
$::log->destadd("qualified"); |
341 |
&process_metafiles($metafile); |
342 |
$::log->flush; |
343 |
exit($::log->exitstatus); |
344 |
} else { # parent |
345 |
$pids{$cpid} = $cout; |
346 |
$readset->add($cout); |
347 |
$metafile_by_fh{$cout} = $metafile; |
348 |
} |
349 |
}; |
350 |
|
351 |
# do a select loop over the outstanding requests to collect messages |
352 |
# if we are in the process of starting more processes, we just |
353 |
# briefly poll out pending output so as not to have blocking |
354 |
# children, but if we have started as many children as we ought to |
355 |
# we put in a longer timeout -- any output on a handle will |
356 |
# get us out of the select and into flushing mode again |
357 |
my $timeout = (@metafiles && (scalar keys %pids < $parallelism) ? 0.1:1); |
358 |
|
359 |
$::log->verb(6,"PLOOP: select with timeout $timeout"); |
360 |
my ( $rh_set ) = IO::Select->select($readset, undef, undef, $timeout); |
361 |
|
362 |
foreach my $fh ( @$rh_set ) { |
363 |
my $metafile = $metafile_by_fh{$fh}; |
364 |
# we know there is at least one byte to read, but also that |
365 |
# any client sends complete |
366 |
while (1) { |
367 |
my $char; |
368 |
my $length = sysread $fh, $char, 1; |
369 |
if ( $length ) { |
370 |
$logoutput{$metafile} .= $char; |
371 |
$char eq "\n" and last; |
372 |
} else { |
373 |
#expected a char but got eof |
374 |
$readset->remove($fh); |
375 |
close($fh); |
376 |
map { |
377 |
$pids{$_} == $fh and |
378 |
waitpid($_,WNOHANG) and |
379 |
delete $pids{$_} and |
380 |
$::log->verb(5,"Collected pid $_ (rc=$?),", |
381 |
length($logoutput{$metafile}),"bytes log output"); |
382 |
} keys %pids; |
383 |
last; |
384 |
} |
385 |
} |
386 |
} |
387 |
} |
388 |
|
389 |
# log out all collected log data from our children |
390 |
foreach my $metafile ( sort keys %logoutput ) { |
391 |
foreach my $line ( split(/\n/,$logoutput{$metafile}) ) { |
392 |
$line =~ /^ERROR\s+(.*)$/ and $::log->err($1); |
393 |
$line =~ /^WARN\s+(.*)$/ and $::log->warn($1); |
394 |
$line =~ /^VERBOSE\((\d+)\)\s+(.*)$/ and $::log->verb($1,$2); |
395 |
$line =~ /^DEBUG\((\d+)\)\s+(.*)$/ and $::log->debug($1,$2); |
396 |
} |
397 |
} |
398 |
|
399 |
return 1; |
400 |
} |