4 # Perl c_rehash script, scan all files in a directory
5 # and add symbolic links to their hash values.
11 if(defined $ENV{OPENSSL
}) {
12 $openssl = $ENV{OPENSSL
};
15 $ENV{OPENSSL
} = $openssl;
18 $ENV{PATH
} .= ":$dir/bin";
22 foreach (split /:/, $ENV{PATH
}) {
23 if(-x
"$_/$openssl") {
29 print STDERR
"c_rehash: rehashing skipped ('openssl' program not available)\n";
36 } elsif($ENV{SSL_CERT_DIR
}) {
37 @dirlist = split /:/, $ENV{SSL_CERT_DIR
};
39 $dirlist[0] = "$dir/certs";
51 print "Doing $_[0]\n";
54 my @flist = readdir(DIR
);
55 # Delete any existing symbolic links
56 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
62 FILE
: foreach $fname (grep {/\.pem$/} @flist) {
63 # Check to see if certificates and/or CRLs present.
64 my ($cert, $crl) = check_file
($fname);
66 print STDERR
"WARNING: $fname does not contain a certificate or CRL: skipping\n";
69 link_hash_cert
($fname) if($cert);
70 link_hash_crl
($fname) if($crl);
75 my ($is_cert, $is_crl) = (0,0);
79 if(/^-----BEGIN (.*)-----/) {
81 if($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
84 } elsif($hdr eq "X509 CRL") {
91 return ($is_cert, $is_crl);
95 # Link a certificate to its subject name hash value, each hash is of
96 # the form <hash>.<n> where n is an integer. If the hash value already exists
97 # then we need to up the value of n, unless its a duplicate in which
98 # case we skip the link. We check for duplicates by comparing the
99 # certificate fingerprints
103 $fname =~ s/'/'\\''/g;
104 my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout -in '$fname'`;
110 # Search for an unused hash filename
111 while(exists $hashlist{"$hash.$suffix"}) {
112 # Hash matches: if fingerprint matches its a duplicate cert
113 if($hashlist{"$hash.$suffix"} eq $fprint) {
114 print STDERR
"WARNING: Skipping duplicate certificate $fname\n";
120 print "$fname => $hash\n";
121 $symlink_exists=eval {symlink("",""); 1};
122 if ($symlink_exists) {
123 symlink $fname, $hash;
125 system ("cp", $fname, $hash);
127 $hashlist{$hash} = $fprint;
130 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
134 $fname =~ s/'/'\\''/g;
135 my ($hash, $fprint) = `"$openssl" crl -hash -fingerprint -noout -in '$fname'`;
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";
150 $hash .= ".r$suffix";
151 print "$fname => $hash\n";
152 $symlink_exists=eval {symlink("",""); 1};
153 if ($symlink_exists) {
154 symlink $fname, $hash;
156 system ("cp", $fname, $hash);
158 $hashlist{$hash} = $fprint;