/[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 2597 - (hide annotations) (download) (as text)
Wed Jan 30 08:22:08 2013 UTC (8 years, 9 months ago) by davidg
Original Path: nl.nikhef.pdp.fetchcrl/trunk/fetch-crl3.pl
File MIME type: text/x-prolog
File size: 14592 byte(s)
allow override -quiet

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

Properties

Name Value
svn:executable *

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