/[pdpsoft]/nl.nikhef.pdp.fetchcrl/tags/fetch-crl-3.0.13-0.RC20140305/fetch-crl3.pl.cin
ViewVC logotype

Contents of /nl.nikhef.pdp.fetchcrl/tags/fetch-crl-3.0.13-0.RC20140305/fetch-crl3.pl.cin

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2646 - (show annotations) (download)
Tue Jul 2 18:50:08 2013 UTC (8 years, 5 months ago) by davidg
Original Path: nl.nikhef.pdp.fetchcrl/trunk/fetch-crl3.pl.cin
File size: 15265 byte(s)
Add version info to fetch-crl via -V and sccsid

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

Properties

Name Value
svn:executable *
svn:keywords Date Author Revision Id

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28