/[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 2649 - (hide annotations) (download)
Tue Jul 2 18:55:45 2013 UTC (8 years, 5 months ago) by davidg
File size: 15226 byte(s)
Allow getting info with the what command

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    
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 davidg 1878 next if $::cnf->{_}->{nosymlinks} and -l $fn;
90 davidg 1758 $fn =~ /.*\/([^\/]+)(\.crl_url|\.info)$/;
91 davidg 2188 push @metafiles, $1 unless grep /^$1$/,@metafiles or not defined $1;
92 davidg 1758 }
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 davidg 2646 my $showversion=0;
121 davidg 1758 my $debuglevel;
122     my $parallelism=0;
123     my $randomwait;
124 davidg 1878 my $nosymlinks;
125 davidg 2084 my $cfgdir;
126 davidg 2604 my $inet6glue=0;
127 davidg 1758
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 davidg 2084 "cfgdir=s" => \$cfgdir,
136 davidg 1758 "T|httptimeout=i" => \$httptimeout,
137     "o|output=s" => \$output,
138     "format=s@" => \@formats,
139     "v|verbose+" => \$verbosity,
140     "h|help+" => \$help,
141 davidg 2646 "V|version+" => \$showversion,
142 davidg 1758 "q|quiet+" => \$quiet,
143     "d|debug+" => \$debuglevel,
144     "p|parallelism=i" => \$parallelism,
145 davidg 1878 "nosymlinks+" => \$nosymlinks,
146 davidg 1758 "a|agingtolerance=i" => \$agingtolerance,
147     "r|randomwait=i" => \$randomwait,
148 davidg 2604 "inet6glue+" => \$inet6glue,
149 davidg 1758 ) or &help and exit(1);
150    
151     $help and &help and exit(0);
152 davidg 2646 $showversion and &showversion and exit(0);
153 davidg 1758
154 davidg 1878 $configfile ||= ( -e "/etc/fetch-crl.conf" and "/etc/fetch-crl.conf" );
155 davidg 1758 $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 davidg 2084 ( 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 davidg 1758 # 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 davidg 2305 $cnf->{_}->{formats} = join "\001",@formats if @formats;
182 davidg 1758 $cnf->{_}->{parallelism} = $parallelism if $parallelism;
183     $cnf->{_}->{randomwait} = $randomwait if defined $randomwait;
184 davidg 1878 $cnf->{_}->{nosymlinks} = $nosymlinks if defined $nosymlinks;
185 davidg 2604 $cnf->{_}->{inet6glue} = $inet6glue if $inet6glue;
186 davidg 1758
187 davidg 2597 # 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 davidg 1758 # 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 davidg 2305 $cnf->{_}->{catemplate} = "\@ALIAS\@.pem\001".
213     "\@ALIAS\@.\@R\@\001\@ANCHORNAME\@.\@R\@";
214 davidg 1758
215     $cnf->{_}->{nonssverify} ||= 0;
216     $cnf->{_}->{nocache} ||= 0;
217 davidg 1878 $cnf->{_}->{nosymlinks} ||= 0;
218 davidg 1758 $cnf->{_}->{verbosity} ||= 0;
219     $cnf->{_}->{debuglevel} ||= 0;
220 davidg 2604 $cnf->{_}->{inet6glue} ||= 0;
221 davidg 1758
222     $cnf->{_}->{stateless} and delete $cnf->{_}->{statedir};
223    
224     # expand array keys in config
225     defined $cnf->{_}->{formats} and
226 davidg 2305 @{$cnf->{_}->{formats_}} = split(/[\001;,\s]+/,$cnf->{_}->{formats});
227 davidg 1758
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 davidg 2305 foreach ( split(/[,\001]+/,$cnf->{_}->{logmode}) ) {
238 davidg 1758 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 davidg 2646 sub showversion() {
253     (my $name = $0) =~ s/.*\///;
254     print "$name version @VERSION@\n";
255     return 1;
256     }
257    
258 davidg 1758 sub help() {
259     (my $name = $0) =~ s/.*\///;
260     print <<EOHELP;
261     The fetch-crl utility will retrieve certificate revocation lists (CRLs) for
262     a set of installed trust anchors, based on crl_url files or IGTF-style info
263     files. It will install these for use with OpenSSL, NSS or third-party tools.
264    
265     Usage: $name [-c|--config configfile] [-l|--infodir path]
266     [--cadir path] [-s|--statedir path] [-o|--output path] [--format \@formats]
267 davidg 1878 [-T|--httptimeout seconds] [-p|--parallelism n] [--nosymlinks]
268 davidg 1758 [-a|--agingtolerance hours] [-r|--randomwait seconds]
269     [-v|--verbose] [-h|--help] [-q|--quiet] [-d|--debug level]
270    
271     Options:
272     -c | --config path
273 davidg 1878 Read configuration data from path, default: /etc/fetch-crl.conf
274 davidg 1758 -l | --infodir path
275     Location of the trust anchor meta-data files (crl_url or info),
276     default: /etc/grid-security/certificates
277     --cadir path
278     Location of the trust anchors (default to infodir)
279     -s | --statedir path
280     Location of the historic state data (for caching and delayed-warning)
281     -T | --httptimeout sec
282     Maximum time in seconds to wait for retrieval or a single URL
283     -o | --output path
284     Location of the CRLs written (global default, defaults to infodir
285     --format \@formats
286     Format(s) in which the CRLs will be written (openssl, pem, der, nss)
287 davidg 1878 --nosymlinks
288     Do not include meta-data files that are symlinks
289 davidg 1758 -v | --verbose
290     Become more talkative
291     -q | --quiet
292     Become really quiet (overrides verbosity)
293     -p | --parallelism n
294     Run up to n parallel trust anchor retrieval processes
295     -a | --agingtolerance hours
296     Be quiet for up to hours hours before raising an error. Until
297     the tolerance has passed, only warnings are raised
298     -r | --randomwait seconds
299     Introduce a random delay of up to seconds seconds before starting
300     any retrieval processes
301     -h | --help
302     This help text
303    
304 davidg 2646 Version: @VERSION@
305 davidg 1758 EOHELP
306    
307     return 1;
308     }
309    
310     # ###########################################################################
311     #
312     #
313     sub process_metafiles(@) {
314     my @metafiles = @_;
315    
316     foreach my $f ( @metafiles ) {
317     my $ta = TrustAnchor->new();
318     $cnf->{_}->{"infodir"} and $ta->setInfodir($cnf->{_}->{"infodir"});
319     $ta->loadAnchor($f) or next;
320     $ta->saveLogMode() and $ta->setLogMode();
321     $ta->loadState() or next;
322 davidg 2421
323     # using the HASH in the CA filename templates requires the CRL
324     # is retrieved first to determinte the hash
325     if ( $cnf->{_}->{"catemplate"} =~ /\@HASH\@/ ) {
326     $ta->retrieve or next;
327     $ta->loadCAfiles() or next;
328     } else {
329     $ta->loadCAfiles() or next;
330     $ta->retrieve or next;
331     }
332    
333 davidg 1758 $ta->verifyAndConvertCRLs or next;
334    
335     my $writer = CRLWriter->new($ta);
336     $writer->writeall() or next;
337     $ta->saveState() or next;
338     $ta->restoreLogMode();
339     }
340    
341     return 1;
342     }
343    
344     sub parallel_metafiles($@) {
345     my $parallelism = shift;
346     my @metafiles = @_;
347    
348     my %pids = (); # file handle by processID
349     my %metafile_by_fh = (); # reverse map
350     my $readset = new IO::Select();
351     my %logoutput = ();
352    
353     $| = 1;
354    
355     $::log->verb(2,"starting up to $parallelism worker processes");
356    
357     while ( @metafiles or scalar keys %pids ) {
358     # loop until we have started all possible retrievals AND have
359     # collected all possible output
360    
361     ( @metafiles and (scalar keys %pids < $parallelism) ) and do {
362     # we have metafiles left, and have spare process slots
363     my $metafile = shift @metafiles;
364    
365    
366     $logoutput{$metafile} = "";
367    
368     my $cout;
369     my $cpid = open $cout, "-|";
370     defined $cpid and defined $cout or
371     $::log->err("Cannot fork ($metafile): $!") and next;
372    
373     $::log->verb(5,"LOOP: starting process $cpid for $metafile");
374    
375     if ( $cpid == 0 ) { # I'm the child that should care for $metafile
376     $0 = "fetch-crl worker $metafile";
377     $::log->cleanse();
378     $::log->destadd("qualified");
379     &process_metafiles($metafile);
380     $::log->flush;
381     exit($::log->exitstatus);
382     } else { # parent
383     $pids{$cpid} = $cout;
384     $readset->add($cout);
385     $metafile_by_fh{$cout} = $metafile;
386     }
387     };
388    
389     # do a select loop over the outstanding requests to collect messages
390     # if we are in the process of starting more processes, we just
391     # briefly poll out pending output so as not to have blocking
392     # children, but if we have started as many children as we ought to
393     # we put in a longer timeout -- any output on a handle will
394     # get us out of the select and into flushing mode again
395     my $timeout = (@metafiles && (scalar keys %pids < $parallelism) ? 0.1:1);
396    
397     $::log->verb(6,"PLOOP: select with timeout $timeout");
398     my ( $rh_set ) = IO::Select->select($readset, undef, undef, $timeout);
399    
400     foreach my $fh ( @$rh_set ) {
401     my $metafile = $metafile_by_fh{$fh};
402     # we know there is at least one byte to read, but also that
403     # any client sends complete
404     while (1) {
405     my $char;
406     my $length = sysread $fh, $char, 1;
407     if ( $length ) {
408     $logoutput{$metafile} .= $char;
409     $char eq "\n" and last;
410     } else {
411     #expected a char but got eof
412     $readset->remove($fh);
413     close($fh);
414     map {
415     $pids{$_} == $fh and
416     waitpid($_,WNOHANG) and
417     delete $pids{$_} and
418     $::log->verb(5,"Collected pid $_ (rc=$?),",
419     length($logoutput{$metafile}),"bytes log output");
420     } keys %pids;
421     last;
422     }
423     }
424     }
425     }
426    
427     # log out all collected log data from our children
428     foreach my $metafile ( sort keys %logoutput ) {
429     foreach my $line ( split(/\n/,$logoutput{$metafile}) ) {
430     $line =~ /^ERROR\s+(.*)$/ and $::log->err($1);
431     $line =~ /^WARN\s+(.*)$/ and $::log->warn($1);
432     $line =~ /^VERBOSE\((\d+)\)\s+(.*)$/ and $::log->verb($1,$2);
433     $line =~ /^DEBUG\((\d+)\)\s+(.*)$/ and $::log->debug($1,$2);
434     }
435     }
436    
437     return 1;
438     }

Properties

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

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