/[pdpsoft]/nl.nikhef.pdp.fetchcrl/tags/fetch-crl-3.0.2-1/fetch-crl3.pl
ViewVC logotype

Annotation of /nl.nikhef.pdp.fetchcrl/tags/fetch-crl-3.0.2-1/fetch-crl3.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1923 - (hide annotations) (download) (as text)
Mon Sep 20 13:08:23 2010 UTC (11 years, 2 months ago) by davidg
Original Path: tags/fetchcrl/fetch-crl-3.0.2-1/fetch-crl3.pl
File MIME type: text/x-prolog
File size: 13679 byte(s)
Fixed lintian manpage problem

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

Properties

Name Value
svn:executable *

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