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;
20 if (defined(&Cwd
::getcwd
)) {
23 $pwd=`pwd`; chomp($pwd);
25 my $path_delim = ($pwd =~ /^[a-z]\:/i) ?
';' : ':'; # DOS/Win32 or Unix delimiter?
27 $ENV{PATH
} .= "$path_delim$dir/bin";
31 foreach (split /$path_delim/, $ENV{PATH
}) {
32 if(-x
"$_/$openssl") {
34 $openssl = "$_/$openssl";
39 print STDERR
"c_rehash: rehashing skipped ('openssl' program not available)\n";
46 } elsif($ENV{SSL_CERT_DIR
}) {
47 @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR
};
49 $dirlist[0] = "$dir/certs";
54 $openssl="$pwd/$openssl" if (!-x
$openssl);
66 print "Doing $_[0]\n";
69 my @flist = readdir(DIR
);
70 # Delete any existing symbolic links
71 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
77 FILE
: foreach $fname (grep {/\.pem$/} @flist) {
78 # Check to see if certificates and/or CRLs present.
79 my ($cert, $crl) = check_file
($fname);
81 print STDERR
"WARNING: $fname does not contain a certificate or CRL: skipping\n";
84 link_hash_cert
($fname) if($cert);
85 link_hash_crl
($fname) if($crl);
90 my ($is_cert, $is_crl) = (0,0);
94 if(/^-----BEGIN (.*)-----/) {
96 if($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
99 } elsif($hdr eq "X509 CRL") {
106 return ($is_cert, $is_crl);
110 # Link a certificate to its subject name hash value, each hash is of
111 # the form <hash>.<n> where n is an integer. If the hash value already exists
112 # then we need to up the value of n, unless its a duplicate in which
113 # case we skip the link. We check for duplicates by comparing the
114 # certificate fingerprints
118 $fname =~ s/'/'\\''/g;
119 my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout -in "$fname"`;
125 # Search for an unused hash filename
126 while(exists $hashlist{"$hash.$suffix"}) {
127 # Hash matches: if fingerprint matches its a duplicate cert
128 if($hashlist{"$hash.$suffix"} eq $fprint) {
129 print STDERR
"WARNING: Skipping duplicate certificate $fname\n";
135 print "$fname => $hash\n";
136 $symlink_exists=eval {symlink("",""); 1};
137 if ($symlink_exists) {
138 symlink $fname, $hash;
140 open IN
,"<$fname" or die "can't open $fname for read";
141 open OUT
,">$hash" or die "can't open $hash for write";
142 print OUT
<IN
>; # does the job for small text files
146 $hashlist{$hash} = $fprint;
149 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
153 $fname =~ s/'/'\\''/g;
154 my ($hash, $fprint) = `"$openssl" crl -hash -fingerprint -noout -in '$fname'`;
160 # Search for an unused hash filename
161 while(exists $hashlist{"$hash.r$suffix"}) {
162 # Hash matches: if fingerprint matches its a duplicate cert
163 if($hashlist{"$hash.r$suffix"} eq $fprint) {
164 print STDERR
"WARNING: Skipping duplicate CRL $fname\n";
169 $hash .= ".r$suffix";
170 print "$fname => $hash\n";
171 $symlink_exists=eval {symlink("",""); 1};
172 if ($symlink_exists) {
173 symlink $fname, $hash;
175 system ("cp", $fname, $hash);
177 $hashlist{$hash} = $fprint;