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 my $pwd = `pwd`; chomp $pwd;
19 my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':'; # DOS/Win32 or Unix delimiter?
21 $ENV{PATH} .= "$path_delim$dir/bin";
25 foreach (split /$path_delim/, $ENV{PATH}) {
26 if(-x "$_/$openssl") {
32 print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
39 } elsif($ENV{SSL_CERT_DIR}) {
40 @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};
42 $dirlist[0] = "$dir/certs";
47 $openssl="$pwd/$openssl" if (!-x $openssl);
59 print "Doing $_[0]\n";
62 my @flist = readdir(DIR);
63 # Delete any existing symbolic links
64 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
70 FILE: foreach $fname (grep {/\.pem$/} @flist) {
71 # Check to see if certificates and/or CRLs present.
72 my ($cert, $crl) = check_file($fname);
74 print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
77 link_hash_cert($fname) if($cert);
78 link_hash_crl($fname) if($crl);
83 my ($is_cert, $is_crl) = (0,0);
87 if(/^-----BEGIN (.*)-----/) {
89 if($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
92 } elsif($hdr eq "X509 CRL") {
99 return ($is_cert, $is_crl);
103 # Link a certificate to its subject name hash value, each hash is of
104 # the form <hash>.<n> where n is an integer. If the hash value already exists
105 # then we need to up the value of n, unless its a duplicate in which
106 # case we skip the link. We check for duplicates by comparing the
107 # certificate fingerprints
111 $fname =~ s/'/'\\''/g;
112 my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout -in "$fname"`;
118 # Search for an unused hash filename
119 while(exists $hashlist{"$hash.$suffix"}) {
120 # Hash matches: if fingerprint matches its a duplicate cert
121 if($hashlist{"$hash.$suffix"} eq $fprint) {
122 print STDERR "WARNING: Skipping duplicate certificate $fname\n";
128 print "$fname => $hash\n";
129 $symlink_exists=eval {symlink("",""); 1};
130 if ($symlink_exists) {
131 symlink $fname, $hash;
133 open IN,"<$fname" or die "can't open $fname for read";
134 open OUT,">$hash" or die "can't open $hash for write";
135 print OUT <IN>; # does the job for small text files
139 $hashlist{$hash} = $fprint;
142 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
146 $fname =~ s/'/'\\''/g;
147 my ($hash, $fprint) = `"$openssl" crl -hash -fingerprint -noout -in '$fname'`;
153 # Search for an unused hash filename
154 while(exists $hashlist{"$hash.r$suffix"}) {
155 # Hash matches: if fingerprint matches its a duplicate cert
156 if($hashlist{"$hash.r$suffix"} eq $fprint) {
157 print STDERR "WARNING: Skipping duplicate CRL $fname\n";
162 $hash .= ".r$suffix";
163 print "$fname => $hash\n";
164 $symlink_exists=eval {symlink("",""); 1};
165 if ($symlink_exists) {
166 symlink $fname, $hash;
168 system ("cp", $fname, $hash);
170 $hashlist{$hash} = $fprint;