/[pdpsoft]/nl.nikhef.pdp.fetchcrl/trunk/CRLWriter.pm
ViewVC logotype

Annotation of /nl.nikhef.pdp.fetchcrl/trunk/CRLWriter.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2672 - (hide annotations) (download) (as text)
Tue Sep 17 09:44:40 2013 UTC (8 years, 2 months ago) by davidg
File MIME type: application/x-perl
File size: 10504 byte(s)
Write all CRLs with trailing newline

1 davidg 1758 #
2     # @(#)$Id$
3     #
4     # ###########################################################################
5     #
6     #
7     # Syntax:
8     # CRLWriter->new( [name [,index]] );
9     # CRLWriter->setTA( trustanchor );
10     # CRLWriter->setIndex( index );
11     #
12     package CRLWriter;
13     use strict;
14     use File::Basename;
15     use File::Temp qw/ tempfile /;
16     require OSSL and import OSSL unless defined &OSSL::new;
17     require base64 and import base64 unless defined &base64::b64encode;
18     use vars qw/ $log $cnf /;
19    
20     sub new {
21     my $obref = {}; bless $obref;
22     my $self = shift;
23     $self = $obref;
24     my $name = shift;
25     my $index = shift;
26    
27     $self->setTA($name) if defined $name;
28     $self->setIndex($name) if defined $index;
29    
30     return $self;
31     }
32    
33    
34     sub getName($) {
35     my $self = shift;
36     return 0 unless defined $self;
37     return $self->{"ta"}->getAnchorName;
38     }
39    
40     sub setTA($$) {
41     my $self = shift;
42     my ($ta) = shift;
43     return 0 unless defined $ta and defined $self;
44     $ta->{"anchorname"} or
45     $::log->err("CRLWriter::setTA called without uninitialised trust anchor")
46     and return 0;
47     $self->{"ta"} = $ta;
48     return 1;
49     }
50    
51     sub setIndex($$) {
52     my $self = shift;
53     my ($index) = shift;
54     return 0 unless defined $self;
55     $self->{"ta"} or
56     $::log->err("CRLWriter::setIndex called without a loaded TA") and
57     return 0;
58     my $ta = $self->{"ta"};
59    
60     $ta->{"crlurls"} or
61     $::log->err("CRLWriter::setIndex called with uninitialised TA") and
62     return 0;
63    
64     ! defined $index and delete $self->{"index"} and return 1;
65    
66     $index < 0 and
67     $::log->err("CRLWriter::setIndex called with invalid index $index") and
68     return 0;
69     $index > $#{$ta->{"crlurls"}} and
70     $::log->err("CRLWriter::setIndex index $index too large") and
71     return 0;
72    
73     $self->{"index"} = $index;
74    
75     return 1;
76     }
77    
78     sub updatefile($$%) {
79     my $file = shift;
80     my $content = shift;
81     my %flags = @_;
82     $content or return undef;
83     $file or
84     $::log->err("Cannot write content to undefined path") and return undef;
85    
86     my ( $basename, $path, $suffix ) = fileparse($file);
87    
88     # get content and do a comparison. If data identical, touch only
89     # to update mtime (other tools like NGC Nagios use this mtime semantics)
90     #
91     my $olddata;
92     my $mytime;
93     -f $file and do {
94     $mytime = (stat(_))[9];
95     {
96     open OLDFILE,'<',$file or
97     $::log->err("Cannot make backup of $file: $!") and return undef;
98     binmode OLDFILE; local $/;
99     $olddata = <OLDFILE>; close OLDFILE;
100     }
101     };
102     if ( $flags{"BACKUP"} and $olddata ) {
103     if ( -w $path ) {
104     -e "$file~" and ( unlink "$file~" or
105     $::log->warn("Cannot remove old backup $file~: $!") and return undef);
106     if (open BCKFILE,'>',"$file~" ) {
107     print BCKFILE $olddata;
108     close BCKFILE;
109     utime $mytime,$mytime, "$file~";
110     } else {
111     $::log->warn("Cannot reate backup $file~: $!");
112     }
113     } else {
114     $::log->warn("Cannot make backup, $path not writable");
115     }
116     }
117    
118     defined $olddata and $olddata eq $content and do {
119     $::log->verb(4,"$file unchanged - touch only");
120     utime time,time,$file and return 1;
121     $::log->warn("Touch of $file failed, CRL unmodified");
122     return 0;
123     };
124    
125     if ( open FH,'>',$file ) {
126 davidg 2672 if ($content !~ /\n$/sm) {
127     $content="$content\n";
128     }
129 davidg 1758 print FH $content or
130     $::log->err("Write to $file: $!") and return undef;
131     close FH or
132     $::log->err("Close on write of $file: $!") and return undef;
133     } else { # something went wrong in opening the file for write,
134     # so try and restore backup if that was selected
135     $::log->err("Open for write of $file: $!");
136     $flags{"BACKUP"} and ! -s "$file" and -s "$file~" and do {
137     #file has been clobbed, but backup OK
138     unlink "$file" and link "$file~","$file" and unlink "$file~" or
139     $::log->err("Restore of backup $file failed: $!");
140     };
141     return undef;
142     }
143     return 1;
144     }
145    
146     sub writePEM($$$$) {
147     my $self = shift;
148     my $idx = shift;
149     my $data = shift;
150     my $ta = shift;
151     defined $idx and $data and $ta or
152     $::log->err("CRLWriter::writePEM: missing index or data") and return 0;
153    
154     my $output = $::cnf->{_}->{"output"};
155     $output = $::cnf->{_}->{"output_pem"} if defined $::cnf->{_}->{"output_pem"};
156     $output and -d $output or
157     $::log->err("PEM target directory $output invalid") and return 0;
158    
159     my $filename = "$output/".$ta->{"nametemplate_pem"};
160     $filename =~ s/\@R\@/$idx/g;
161    
162     my %flags = ();
163     $::cnf->{_}->{"backups"} and $flags{"BACKUP"} = 1;
164    
165     $::log->verb(3,"Writing PEM file",$filename);
166     &updatefile($filename,$data,%flags) or return 0;
167     return 1;
168     }
169    
170     sub writeDER($$$$) {
171     my $self = shift;
172     my $idx = shift;
173     my $data = shift;
174     my $ta = shift;
175     defined $idx and $data and $ta or
176     $::log->err("CRLWriter::writeDER: missing index or data") and return 0;
177    
178     my $output = $::cnf->{_}->{"output"};
179     $output = $::cnf->{_}->{"output_der"} if defined $::cnf->{_}->{"output_der"};
180     $output and -d $output or
181     $::log->err("DER target directory $output invalid") and return 0;
182    
183     my $filename = "$output/".$ta->{"nametemplate_der"};
184     $filename =~ s/\@R\@/$idx/g;
185    
186     my %flags = ();
187     $::cnf->{_}->{"backups"} and $flags{"BACKUP"} = 1;
188    
189     my $openssl=OSSL->new();
190     my ($der,$errors) = $openssl->Exec3($data,qw/crl -inform PEM -outform DER/);
191     $errors or not $der and
192     $::log->err("Data count not be converted to DER: $errors") and return 0;
193    
194     $::log->verb(3,"Writing DER file",$filename);
195     &updatefile($filename,$der,%flags) or return 0;
196     return 1;
197     }
198    
199     sub writeOpenSSL($$$$) {
200     my $self = shift;
201     my $idx = shift;
202     my $data = shift;
203     my $ta = shift;
204     defined $idx and $data and $ta or
205     $::log->err("CRLWriter::writeOpenSSL: missing index, data or ta") and
206     return 0;
207    
208     my $output = $::cnf->{_}->{"output"};
209     $output = $::cnf->{_}->{"output_openssl"} if
210     defined $::cnf->{_}->{"output_openssl"};
211     $output and -d $output or
212     $::log->err("OpenSSL target directory $output invalid") and return 0;
213    
214     my $openssl=OSSL->new();
215    
216     # guess the hash name or names from OpenSSL
217     # if mode is dual (and OpenSSL1 installed) write two files
218     my $opensslversion = $openssl->getVersion() or return 0;
219    
220     my ($cmddata,$errors);
221     my @hashes = ();
222     if ( $opensslversion ge "1" and $::cnf->{_}->{"opensslmode"} eq "dual" ) {
223     $::log->verb(5,"OpenSSL version 1 dual-mode enabled");
224     # this mode needs the ta cafile to get both hashes, since these
225     # can only be extracted by the x509 subcommand from a CA ...
226     ($cmddata,$errors) = $openssl->Exec3(undef,
227     qw/x509 -noout -subject_hash -subject_hash_old -in/,
228     $ta->{"cafile"}[0]);
229     $cmddata or
230     $::log->err("OpenSSL cannot extract hashes from",$ta->{"cafile"}[0]) and
231     return 0;
232     @hashes = split(/[\s\n]+/,$cmddata);
233     } else {
234     $::log->verb(5,"OpenSSL version 1 single-mode or pre-1.0 style");
235     ($cmddata,$errors) = $openssl->Exec3($data,qw/crl -noout -hash/);
236     $cmddata or
237     $::log->err("OpenSSL cannot extract hashes from CRL for",
238     $ta->{"alias"}.'/'.$idx
239     ) and
240     return 0;
241     @hashes = split(/[\s\n]+/,$cmddata);
242     }
243    
244     my %flags = ();
245     $::cnf->{_}->{"backups"} and $flags{"BACKUP"} = 1;
246    
247     foreach my $hash ( @hashes ) {
248     my $filename = "$output/$hash.r$idx";
249     $::log->verb(3,"Writing OpenSSL file",$filename);
250     &updatefile($filename,$data,%flags) or return 0;
251     }
252     return 1;
253     }
254    
255     sub writeNSS($$$$) {
256     my $self = shift;
257     my $idx = shift;
258     my $data = shift;
259     my $ta = shift;
260     defined $idx and $data and $ta or
261     $::log->err("CRLWriter::writeNSS: missing index, data or ta") and return 0;
262    
263     my $output = $::cnf->{_}->{"output"};
264     $output = $::cnf->{_}->{"output_nss"} if defined $::cnf->{_}->{"output_nss"};
265     $output and -d $output or
266     $::log->err("NSS target directory $output invalid") and return 0;
267    
268     my $dbprefix="";
269     $dbprefix = $::cnf->{_}->{"nssdbprefix"}
270     if defined $::cnf->{_}->{"nssdbprefix"};
271    
272     my $filename = "$output/$dbprefix";
273    
274     # the crlutil tool requires the DER formatted cert in a file
275     my $tmpdir = $::cnf->{_}->{exec3tmpdir} || $ENV{"TMPDIR"} || '/tmp';
276     my ($derfh,$dername) = tempfile("fetchcrl3der.XXXXXX",
277     DIR=>$tmpdir, UNLINK=>1);
278     (my $b64data = $data) =~ s/-[^\n]+//gm;
279     $b64data =~ s/\s+//gm;
280     print $derfh base64::b64decode($b64data); # der is decoded PEM :-)
281    
282     my $cmd = "crlutil -I -d \"$output\" -P \"$dbprefix\" ";
283     $::cnf->{_}->{nonssverify} and $cmd .= "-B ";
284     $cmd .= "-n ".$ta->{"alias"}.'.'.$idx." ";
285     $cmd .= "-i \"$dername\"";
286     my $result = `$cmd 2>&1`;
287     unlink $dername;
288     if ( $? != 0 ) {
289     $::log->err("Cannot update NSSDB filename: $result");
290     } else {
291     $::log->verb(3,"WriteNSS: ".$ta->{"alias"}.'.'.$idx." added to $filename");
292     }
293    
294     return 1;
295     }
296    
297    
298     sub writeall($) {
299     my $self = shift;
300     return 0 unless defined $self;
301     $self->{"ta"} or
302     $::log->err("CRLWriter::setIndex called without a loaded TA") and
303     return 0;
304     my $ta = $self->{"ta"};
305     $ta->{"crlurls"} or
306     $::log->err("CRLWriter::setIndex called with uninitialised TA") and
307     return 0;
308    
309     $::log->verb(2,"Writing CRLs for",$ta->{"anchorname"});
310    
311     my $completesuccess = 1;
312     for ( my $idx = 0 ; $idx <= $#{$ta->{"crl"}} ; $idx++ ) {
313     $ta->{"crl"}[$idx]{"pemdata"} or
314     $::log->verb(3,"Ignored CRL $idx skipped") and
315     next; # ignore empty crls, leave these in place
316    
317     my $writeAttempt = 0;
318     my $writeSuccess = 0;
319    
320     ( grep /^pem$/, @{$::cnf->{_}->{formats_}} ) and ++$writeAttempt and
321     $writeSuccess += $self->writePEM($idx,$ta->{"crl"}[$idx]{"pemdata"},$ta);
322    
323     ( grep /^der$/, @{$::cnf->{_}->{formats_}} ) and ++$writeAttempt and
324     $writeSuccess += $self->writeDER($idx,$ta->{"crl"}[$idx]{"pemdata"},$ta);
325    
326     ( grep /^openssl$/, @{$::cnf->{_}->{formats_}} ) and ++$writeAttempt and
327     $writeSuccess += $self->writeOpenSSL($idx,
328     $ta->{"crl"}[$idx]{"pemdata"},$ta);
329    
330     ( grep /^nss$/, @{$::cnf->{_}->{formats_}} ) and ++$writeAttempt and
331     $writeSuccess += $self->writeNSS($idx,$ta->{"crl"}[$idx]{"pemdata"},$ta);
332    
333     if ( $writeSuccess == $writeAttempt ) {
334     $::log->verb(4,"LastWrite time (mtime) set to current time");
335     $ta->{"crl"}[$idx]{"state"}{"mtime"} = time;
336     } else {
337     $::log->warn("Partial updating ($writeSuccess of $writeAttempt) for",
338     $ta->{"anchorname"},
339     "CRL $idx: mtime not updated");
340     }
341     $completesuccess &&= ($writeSuccess == $writeAttempt);
342     }
343    
344     return $completesuccess;
345     }
346    
347     1;

Properties

Name Value
svn:keywords Id

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