cURL / Mailing Lists / curl-library / Single Mail

curl-library

[PATCH} Patch for mk-ca-bundle.pl

From: tlhackque <tlhackque_at_yahoo.com>
Date: Thu, 03 Jan 2013 07:08:18 -0500

I originally sent this on 25-Dec - now that folks seem to be back from
their holiday, thought I should re-send.

I hope someone can apply it to your source repository - I don't have (or
want) access - hopefully this is a one-time contribution.

Happy New Year!

(The 25-Dec version is identical except that I just added the new [-f]
on the usage summary line.)

-------- Original Message --------
Subject: Patch for mk-ca-bundle.pl
Date: Tue, 25 Dec 2012 06:33:15 -0500
From: tlhackque <tlhackque_at_yahoo.com>
To: curl-library_at_cool.haxx.se

I have updated mk-ca-bundle.pl to implement the following :

1. When the downloaded data file from Mozilla is current, but the output
bundle does not exist: continue processing to create the bundle. The
goal is to have the output file - not just download the latest input.
2. added -f option to force re-processing the file. Useful for
debugging/testing the process.
3. added support for output to '-' (stdout), allowing the output to be
piped.
4. All progress and error messages go to STDERR rather than STDOUT (3)
5. The script opened and closed the output file many times
unnecessarily. It now opens it once, does the output and closes it.
6. Backup of the input files happens after successful processing, not
before.
7. The output is written to a temporary file, and renamed to the
requested name after backup - this greatly reduces the window where the
file can be seen partially written.
8. all die calls have a \n at the end to suppress perl's traceback - the
traceback isn't useful to end users.

Patch follows.

--- mk-ca-bundle.pl.orig 2012-12-24 21:30:27.000000000 -0500
+++ mk-ca-bundle.pl 2013-01-03 06:54:23.330827450 -0500
@@ -32,19 +32,19 @@
  #
  use Getopt::Std;
  use MIME::Base64;
  use LWP::UserAgent;
  use strict;
-use vars qw($opt_b $opt_h $opt_i $opt_l $opt_n $opt_q $opt_t $opt_u
$opt_v);
+use vars qw($opt_b $opt_f $opt_h $opt_i $opt_l $opt_n $opt_q $opt_t
$opt_u $opt_v);

  my $url =
'http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1';
  # If the OpenSSL commandline is not in search path you can configure
it here!
  my $openssl = 'openssl';

-my $version = '1.16';
+my $version = '1.17';

-getopts('bhilnqtuv');
+getopts('bfhilnqtuv');

  if ($opt_i) {
    print ("=" x 78 . "\n");
    print "Script Version : $version\n";
    print "Perl Version : $]\n";
@@ -56,12 +56,13 @@
    print ("=" x 78 . "\n");
  }

  $0 =~ s@.*(/|\\)@@;
  if ($opt_h) {
- printf("Usage:\t%s [-b] [-i] [-l] [-n] [-q] [-t] [-u] [-v]
[<outputfile>]\n", $0);
+ printf("Usage:\t%s [-b] [-f] [-i] [-l] [-n] [-q] [-t] [-u] [-v]
[<outputfile>]\n", $0);
    print "\t-b\tbackup an existing version of ca-bundle.crt\n";
+ print "\t-f\tforce rebuild even if certdata.txt is current\n";
    print "\t-i\tprint version info about used modules\n";
    print "\t-l\tprint license info about certdata.txt\n";
    print "\t-n\tno download of certdata.txt (to use existing)\n";
    print "\t-q\tbe really quiet (no progress output at all)\n";
    print "\t-t\tinclude plain text listing of certificates\n";
@@ -71,35 +72,41 @@
  }

  my $crt = $ARGV[0] || 'ca-bundle.crt';
  (my $txt = $url) =~ s@(.*/|\?.*)@@g;

+my $stdout = $crt eq '-';
  my $resp;
+my $fetched;

  unless ($opt_n and -e $txt) {
- print "Downloading '$txt' ...\n" if (!$opt_q);
+ print STDERR "Downloading '$txt' ...\n" if (!$opt_q);
    my $ua = new LWP::UserAgent(agent => "$0/$version");
    $ua->env_proxy();
    $resp = $ua->mirror($url, $txt);
    if ($resp && $resp->code eq '304') {
- print "Not modified\n" unless $opt_q;
- exit 0;
+ print STDERR "Not modified\n" unless $opt_q;
+ exit 0 if -e $crt && !$opt_f;
+ } else {
+ $fetched = 1;
+ }
+ if( !$resp || $resp->code !~ /^(?:200|304)$/ ) {
+ print STDERR "Unable to download latest data: "
+ . ($resp? $resp->code . ' - ' . $resp->message : "LWP failed")
. "\n"
+ unless $opt_q;
+ exit 1 if -e $crt || ! -r $txt;
    }
  }

-my $currentdate = scalar gmtime($resp ? $resp->last_modified :
(stat($txt))[9]);
-
-if ($opt_b && -e $crt) {
- my $bk = 1;
- while (-e "$crt.~${bk}~") {
- $bk++;
- }
- rename $crt, "$crt.~${bk}~";
-}
+my $currentdate = scalar gmtime($fetched ? $resp->last_modified :
(stat($txt))[9]);

  my $format = $opt_t ? "plain text and " : "";
-open(CRT,">$crt") or die "Couldn't open $crt: $!";
+if( $stdout ) {
+ open(CRT, '> -') or die "Couldn't open STDOUT: $!\n";
+} else {
+ open(CRT,">$crt.~") or die "Couldn't open $crt.~: $!\n";
+}
  print CRT <<EOT;
  ##
  ## $crt -- Bundle of CA Root Certificates
  ##
  ## Certificate data from Mozilla as of: ${currentdate}
@@ -115,37 +122,31 @@
  ## Just configure this file as the SSLCACertificateFile.
  ##

  EOT

-close(CRT) or die "Couldn't close $crt: $!";
-
-print "Processing '$txt' ...\n" if (!$opt_q);
+print STDERR "Processing '$txt' ...\n" if (!$opt_q);
  my $caname;
  my $certnum = 0;
  my $skipnum = 0;
  my $start_of_cert = 0;

-open(TXT,"$txt") or die "Couldn't open $txt: $!";
+open(TXT,"$txt") or die "Couldn't open $txt: $!\n";
  while (<TXT>) {
    if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
- open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
      print CRT;
      print if ($opt_l);
      while (<TXT>) {
        print CRT;
        print if ($opt_l);
        last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
      }
- close(CRT) or die "Couldn't close $crt: $!";
    }
    next if /^#|^\s*$/;
    chomp;
    if (/^CVS_ID\s+\"(.*)\"/) {
- open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
      print CRT "# $1\n";
- close(CRT) or die "Couldn't close $crt: $!";
    }

    # this is a match for the start of a certificate
    if (/^CKA_CLASS CK_OBJECT_CLASS CKO_CERTIFICATE/) {
      $start_of_cert = 1
@@ -182,30 +183,41 @@
        $skipnum ++;
      } else {
        my $pem = "-----BEGIN CERTIFICATE-----\n"
                . MIME::Base64::encode($data)
                . "-----END CERTIFICATE-----\n";
- open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
        print CRT "\n$caname\n";
        print CRT ("=" x length($caname) . "\n");
        if (!$opt_t) {
          print CRT $pem;
        }
- close(CRT) or die "Couldn't close $crt: $!";
        if ($opt_t) {
- open(TMP, "|$openssl x509 -md5 -fingerprint -text -inform PEM
>> $crt") or die "Couldn't open openssl pipe: $!";
+ open(TMP, "|$openssl x509 -md5 -fingerprint -text -inform PEM
>> $crt") or die "Couldn't open openssl pipe: $!\n";
          print TMP $pem;
- close(TMP) or die "Couldn't close openssl pipe: $!";
+ close(TMP) or die "Couldn't close openssl pipe: $!\n";
        }
- print "Parsing: $caname\n" if ($opt_v);
+ print STDERR "Parsing: $caname\n" if ($opt_v);
        $certnum ++;
        $start_of_cert = 0;
      }
    }
  }
-close(TXT) or die "Couldn't close $txt: $!";
+close(TXT) or die "Couldn't close $txt: $!\n";
+close(CRT) or die "Couldn't close $crt.~: $!\n";
+unless( $stdout ) {
+ if ($opt_b && -e $crt) {
+ my $bk = 1;
+ while (-e "$crt.~${bk}~") {
+ $bk++;
+ }
+ rename $crt, "$crt.~${bk}~" or die "Failed to create backup
$crt.~$bk}~: $!\n";
+ } elsif( -e $crt ) {
+ unlink( $crt ) or die "Failed to remove $crt: $!\n";
+ }
+ rename "$crt.~", $crt or die "Failed to rename $crt.~ to $crt: $!\n";
+}
  unlink $txt if ($opt_u);
-print "Done ($certnum CA certs processed, $skipnum untrusted
skipped).\n" if (!$opt_q);
+print STDERR "Done ($certnum CA certs processed, $skipnum untrusted
skipped).\n" if (!$opt_q);

  exit;

-- 
This communication may not represent my employer's views,
if any, on the matters discussed.
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Received on 2013-01-03