c_rehash.in 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/local/bin/perl
  2. # Perl c_rehash script, scan all files in a directory
  3. # and add symbolic links to their hash values.
  4. my $openssl;
  5. my $dir;
  6. my $prefix;
  7. if(defined $ENV{OPENSSL}) {
  8. $openssl = $ENV{OPENSSL};
  9. } else {
  10. $openssl = "openssl";
  11. $ENV{OPENSSL} = $openssl;
  12. }
  13. $ENV{PATH} .= ":$dir/bin";
  14. if(! -x $openssl) {
  15. my $found = 0;
  16. foreach (split /:/, $ENV{PATH}) {
  17. if(-x "$_/$openssl") {
  18. $found = 1;
  19. last;
  20. }
  21. }
  22. if($found == 0) {
  23. print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
  24. exit 0;
  25. }
  26. }
  27. if(@ARGV) {
  28. @dirlist = @ARGV;
  29. } elsif($ENV{SSL_CERT_DIR}) {
  30. @dirlist = split /:/, $ENV{SSL_CERT_DIR};
  31. } else {
  32. $dirlist[0] = "$dir/certs";
  33. }
  34. foreach (@dirlist) {
  35. if(-d $_ and -w $_) {
  36. hash_dir($_);
  37. }
  38. }
  39. sub hash_dir {
  40. my %hashlist;
  41. print "Doing $_[0]\n";
  42. chdir $_[0];
  43. opendir(DIR, ".");
  44. my @flist = readdir(DIR);
  45. # Delete any existing symbolic links
  46. foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
  47. if(-l $_) {
  48. unlink $_;
  49. }
  50. }
  51. closedir DIR;
  52. FILE: foreach $fname (grep {/\.pem$/} @flist) {
  53. # Check to see if certificates and/or CRLs present.
  54. my ($cert, $crl) = check_file($fname);
  55. if(!$cert && !$crl) {
  56. print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
  57. next;
  58. }
  59. link_hash_cert($fname) if($cert);
  60. link_hash_crl($fname) if($crl);
  61. }
  62. }
  63. sub check_file {
  64. my ($is_cert, $is_crl) = (0,0);
  65. my $fname = $_[0];
  66. open IN, $fname;
  67. while(<IN>) {
  68. if(/^-----BEGIN (.*)-----/) {
  69. my $hdr = $1;
  70. if($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
  71. $is_cert = 1;
  72. last if($is_crl);
  73. } elsif($hdr eq "X509 CRL") {
  74. $is_crl = 1;
  75. last if($is_cert);
  76. }
  77. }
  78. }
  79. close IN;
  80. return ($is_cert, $is_crl);
  81. }
  82. # Link a certificate to its subject name hash value, each hash is of
  83. # the form <hash>.<n> where n is an integer. If the hash value already exists
  84. # then we need to up the value of n, unless its a duplicate in which
  85. # case we skip the link. We check for duplicates by comparing the
  86. # certificate fingerprints
  87. sub link_hash_cert {
  88. my $fname = $_[0];
  89. $fname =~ s/'/'\\''/g;
  90. my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout -in '$fname'`;
  91. chomp $hash;
  92. chomp $fprint;
  93. $fprint =~ s/^.*=//;
  94. $fprint =~ tr/://d;
  95. my $suffix = 0;
  96. # Search for an unused hash filename
  97. while(exists $hashlist{"$hash.$suffix"}) {
  98. # Hash matches: if fingerprint matches its a duplicate cert
  99. if($hashlist{"$hash.$suffix"} eq $fprint) {
  100. print STDERR "WARNING: Skipping duplicate certificate $fname\n";
  101. return;
  102. }
  103. $suffix++;
  104. }
  105. $hash .= ".$suffix";
  106. print "$fname => $hash\n";
  107. $symlink_exists=eval {symlink("",""); 1};
  108. if ($symlink_exists) {
  109. symlink $fname, $hash;
  110. } else {
  111. system ("cp", $fname, $hash);
  112. }
  113. $hashlist{$hash} = $fprint;
  114. }
  115. # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
  116. sub link_hash_crl {
  117. my $fname = $_[0];
  118. $fname =~ s/'/'\\''/g;
  119. my ($hash, $fprint) = `"$openssl" crl -hash -fingerprint -noout -in '$fname'`;
  120. chomp $hash;
  121. chomp $fprint;
  122. $fprint =~ s/^.*=//;
  123. $fprint =~ tr/://d;
  124. my $suffix = 0;
  125. # Search for an unused hash filename
  126. while(exists $hashlist{"$hash.r$suffix"}) {
  127. # Hash matches: if fingerprint matches its a duplicate cert
  128. if($hashlist{"$hash.r$suffix"} eq $fprint) {
  129. print STDERR "WARNING: Skipping duplicate CRL $fname\n";
  130. return;
  131. }
  132. $suffix++;
  133. }
  134. $hash .= ".r$suffix";
  135. print "$fname => $hash\n";
  136. $symlink_exists=eval {symlink("",""); 1};
  137. if ($symlink_exists) {
  138. symlink $fname, $hash;
  139. } else {
  140. system ("cp", $fname, $hash);
  141. }
  142. $hashlist{$hash} = $fprint;
  143. }