/[pdpsoft]/nl.nikhef.pdp.fetchcrl/trunk/fetch-crl3.pl.cin
ViewVC logotype

Annotation of /nl.nikhef.pdp.fetchcrl/trunk/fetch-crl3.pl.cin

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2691 - (hide annotations) (download)
Tue Mar 4 16:33:30 2014 UTC (7 years, 7 months ago) by davidg
File size: 15540 byte(s)
rcmode status log added

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

Properties

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

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