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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2690 - (show annotations) (download)
Tue Mar 4 16:26:06 2014 UTC (7 years, 9 months ago) by davidg
Original Path: nl.nikhef.pdp.fetchcrl/trunk/fetch-crl3.pl.cin
File size: 15463 byte(s)
Add rcmode option and no re-run of boot script (first version)

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

Properties

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

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