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") {
38 print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
45 } elsif($ENV{SSL_CERT_DIR}) {
46 @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};
48 $dirlist[0] = "$dir/certs";
53 $openssl="$pwd/$openssl" if (!-x $openssl);
65 print "Doing $_[0]\n";
68 my @flist = readdir(DIR);
69 # Delete any existing symbolic links
70 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
76 FILE: foreach $fname (grep {/\.pem$/} @flist) {
77 # Check to see if certificates and/or CRLs present.
78 my ($cert, $crl) = check_file($fname);
80 print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
83 link_hash_cert($fname) if($cert);
84 link_hash_crl($fname) if($crl);
89 my ($is_cert, $is_crl) = (0,0);
93 if(/^-----BEGIN (.*)-----/) {
95 if($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
98 } elsif($hdr eq "X509 CRL") {
105 return ($is_cert, $is_crl);
109 # Link a certificate to its subject name hash value, each hash is of
110 # the form <hash>.<n> where n is an integer. If the hash value already exists
111 # then we need to up the value of n, unless its a duplicate in which
112 # case we skip the link. We check for duplicates by comparing the
113 # certificate fingerprints
117 $fname =~ s/'/'\\''/g;
118 my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout -in "$fname"`;
124 # Search for an unused hash filename
125 while(exists $hashlist{"$hash.$suffix"}) {
126 # Hash matches: if fingerprint matches its a duplicate cert
127 if($hashlist{"$hash.$suffix"} eq $fprint) {
128 print STDERR "WARNING: Skipping duplicate certificate $fname\n";
134 print "$fname => $hash\n";
135 $symlink_exists=eval {symlink("",""); 1};
136 if ($symlink_exists) {
137 symlink $fname, $hash;
139 open IN,"<$fname" or die "can't open $fname for read";
140 open OUT,">$hash" or die "can't open $hash for write";
141 print OUT <IN>; # does the job for small text files
145 $hashlist{$hash} = $fprint;
148 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
152 $fname =~ s/'/'\\''/g;
153 my ($hash, $fprint) = `"$openssl" crl -hash -fingerprint -noout -in '$fname'`;
159 # Search for an unused hash filename
160 while(exists $hashlist{"$hash.r$suffix"}) {
161 # Hash matches: if fingerprint matches its a duplicate cert
162 if($hashlist{"$hash.r$suffix"} eq $fprint) {
163 print STDERR "WARNING: Skipping duplicate CRL $fname\n";
168 $hash .= ".r$suffix";
169 print "$fname => $hash\n";
170 $symlink_exists=eval {symlink("",""); 1};
171 if ($symlink_exists) {
172 symlink $fname, $hash;
174 system ("cp", $fname, $hash);
176 $hashlist{$hash} = $fprint;