c_rehash.in 4.0 KB

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