perl paranoia about @INC
[exim.git] / src / util / ocsp_fetch.pl
1 #!/usr/bin/perl
2 # Copyright (C) 2012 Wizards Internet Ltd
3 # License GPLv2: GNU GPL version 2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
4 use strict;
5 BEGIN { pop @INC if $INC[-1] eq '.' };
6 use Getopt::Std;
7 $Getopt::Std::STANDARD_HELP_VERSION=1;
8 use IO::Handle;
9 use Date::Parse;
10 my ($o,$i,$s,$f,$t,$u,$VERSION);
11 $VERSION='1.0';
12 $o={'m'=>10};
13 getopts("c:i:u:a:o:m:fv",$o);
14 usage('No issuer specified') if ! $o->{'i'} && ! -f $o->{'i'};
15 usage('No certificate specified') if ! $o->{'c'} && ! -f $o->{'c'};
16 usage('No CA chain specified') if ! $o->{'a'} && ! -f $o->{'a'};
17 usage('No OCSP file specified') if ! $o->{'o'};
18 usage('No URL specified') if ! $o->{'u'};
19 $o->{'t'}=$o->{'o'}.'.tmp';
20
21 # check if we need to
22 if ( $o->{'f'}
23 || ! -f $o->{'o'}
24 || ( -M $o->{'o'} > 0 )
25 )
26 {
27 $i = new IO::Handle;
28 open( $i, "openssl ocsp -issuer $o->{'i'} -cert $o->{'c'} -url $o->{'u'} -CAfile $o->{'a'} -respout $o->{'t'} 2>/dev/null |" ) || die 'Unable to execute ocsp command';
29 $s = <$i> || die 'Unable to read status';
30 $f = <$i> || die 'Unable to read update time';
31 $t = <$i> || die 'Unable to read next update time';
32 close $i;
33 # Status ok ?
34 chomp($s);
35 chomp($f);
36 chomp($t);
37 $s =~ s/[^:]*: //;
38 $f =~ s/[^:]*: //;
39 $t =~ s/[^:]*: //;
40 $t = str2time($t);
41 die "OCSP status is $s" if $s ne 'good';
42 warn "Next Update $t" if $o->{'v'};
43 # response is good, adjust mod time and move into place.
44 $u = $t - $o->{'m'} * (($t - time)/100);
45 utime $u,$u,$o->{'t'};
46 rename $o->{'t'},$o->{'o'};
47 }
48 exit;
49
50 sub
51 usage
52 {
53 my $m = shift;
54 print STDERR "$m\n" if $m;
55 HELP_MESSAGE(\*STDERR);
56 die;
57 }
58 sub
59 HELP_MESSAGE
60 {
61 my $h = shift;
62 print $h <<EOF
63 Usage: $0 -i issuer -c certificate -u ocsp_url -a ca_certs -o response [-v] [-f]
64
65 For a certificate "www.example.com.pem"
66 signed by "signing.example.net.pem"
67 signed by root CA "ca.example.net.pem"
68 with OCSP server http://ocsp.example.net/
69
70 Ensure there is a file with the signing chain
71
72 cat ca.example.net.pem signing.example.net.pem >chain.pem
73
74 The update procedure would be
75
76 ocsp_fetch -i signing.example.net.pem \
77 -c www.example.com.pem \
78 -u http://ocsp.example.net/ \
79 -a chain.pem \
80 -o www.example.com.ocsp.der
81 EOF
82 }
83 # vi: aw ai sw=4
84 # End of File