4 # Perl c_rehash script, scan all files in a directory
5 # and add symbolic links to their hash values.
12 if(defined $ENV{OPENSSL}) {
13 $openssl = $ENV{OPENSSL};
16 $ENV{OPENSSL} = $openssl;
19 $ENV{PATH} .= ":$dir/bin";
23 foreach (split /:/, $ENV{PATH}) {
24 if(-x "$_/$openssl") {
30 print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
37 } elsif($ENV{SSL_CERT_DIR}) {
38 @dirlist = split /:/, $ENV{SSL_CERT_DIR};
40 $dirlist[0] = "$dir/certs";
52 print "Doing $_[0]\n";
55 my @flist = readdir(DIR);
56 # Delete any existing symbolic links
57 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
63 FILE: foreach $fname (grep {/\.pem$/} @flist) {
64 # Check to see if certificates and/or CRLs present.
65 my ($cert, $crl) = check_file($fname);
67 print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
70 link_hash_cert($fname) if($cert);
71 link_hash_crl($fname) if($crl);
76 my ($is_cert, $is_crl) = (0,0);
80 if(/^-----BEGIN (.*)-----/) {
82 if($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
85 } elsif($hdr eq "X509 CRL") {
92 return ($is_cert, $is_crl);
96 # Link a certificate to its subject name hash value, each hash is of
97 # the form <hash>.<n> where n is an integer. If the hash value already exists
98 # then we need to up the value of n, unless its a duplicate in which
99 # case we skip the link. We check for duplicates by comparing the
100 # certificate fingerprints
104 $fname =~ s/'/'\\''/g;
105 my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout -in '$fname'`;
111 # Search for an unused hash filename
112 while(exists $hashlist{"$hash.$suffix"}) {
113 # Hash matches: if fingerprint matches its a duplicate cert
114 if($hashlist{"$hash.$suffix"} eq $fprint) {
115 print STDERR "WARNING: Skipping duplicate certificate $fname\n";
121 print "$fname => $hash\n";
122 $symlink_exists=eval {symlink("",""); 1};
123 if ($symlink_exists) {
124 symlink $fname, $hash;
126 system ("cp", $fname, $hash);
128 $hashlist{$hash} = $fprint;
131 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
135 $fname =~ s/'/'\\''/g;
136 my ($hash, $fprint) = `"$openssl" crl -hash -fingerprint -noout -in '$fname'`;
142 # Search for an unused hash filename
143 while(exists $hashlist{"$hash.r$suffix"}) {
144 # Hash matches: if fingerprint matches its a duplicate cert
145 if($hashlist{"$hash.r$suffix"} eq $fprint) {
146 print STDERR "WARNING: Skipping duplicate CRL $fname\n";
151 $hash .= ".r$suffix";
152 print "$fname => $hash\n";
153 $symlink_exists=eval {symlink("",""); 1};
154 if ($symlink_exists) {
155 symlink $fname, $hash;
157 system ("cp", $fname, $hash);
159 $hashlist{$hash} = $fprint;