3 # Perl c_rehash script, scan all files in a directory
4 # and add symbolic links to their hash values.
9 my $openssl = $ENV{OPENSSL} || "openssl";
11 my $x509hash = "-subject_hash";
12 my $crlhash = "-hash";
14 my $symlink_exists=eval {symlink("",""); 1};
18 while ( $ARGV[0] =~ /^-/ ) {
19 my $flag = shift @ARGV;
20 last if ( $flag eq '--');
21 if ( $flag eq '-old') {
22 $x509hash = "-subject_hash_old";
23 $crlhash = "-hash_old";
24 } elsif ( $flag eq '-h') {
26 } elsif ( $flag eq '-n' ) {
28 } elsif ( $flag eq '-v' ) {
32 print STDERR "Usage error; try -help.\n";
38 print "Usage: c_rehash [-old] [-h] [-v] [dirs...]\n";
39 print " -old use old-style digest\n";
40 print " -h print this help text\n";
41 print " -v print files removed and linked\n";
46 if (defined(&Cwd::getcwd)) {
53 # DOS/Win32 or Unix delimiter? Prefix our installdir, then search.
54 my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':';
55 $ENV{PATH} = "$prefix/bin" . ($ENV{PATH} ? $path_delim . $ENV{PATH} : "");
59 foreach (split /$path_delim/, $ENV{PATH}) {
60 if (-x "$_/$openssl") {
62 $openssl = "$_/$openssl";
67 print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
74 } elsif ($ENV{SSL_CERT_DIR}) {
75 @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};
77 $dirlist[0] = "$dir/certs";
82 $openssl="$pwd/$openssl" if (!-x $openssl);
91 print "Skipping $_, can't write\n";
98 print "Doing $_[0]\n";
101 my @flist = readdir(DIR);
103 if ( $removelinks ) {
104 # Delete any existing symbolic links
105 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
107 print "unlink $_" if $verbose;
108 unlink $_ || warn "Can't unlink $_, $!\n";
112 FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {
113 # Check to see if certificates and/or CRLs present.
114 my ($cert, $crl) = check_file($fname);
115 if (!$cert && !$crl) {
116 print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
119 link_hash_cert($fname) if ($cert);
120 link_hash_crl($fname) if ($crl);
125 my ($is_cert, $is_crl) = (0,0);
129 if (/^-----BEGIN (.*)-----/) {
131 if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
134 } elsif ($hdr eq "X509 CRL") {
141 return ($is_cert, $is_crl);
145 # Link a certificate to its subject name hash value, each hash is of
146 # the form <hash>.<n> where n is an integer. If the hash value already exists
147 # then we need to up the value of n, unless its a duplicate in which
148 # case we skip the link. We check for duplicates by comparing the
149 # certificate fingerprints
153 $fname =~ s/'/'\\''/g;
154 my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`;
160 # Search for an unused hash filename
161 while(exists $hashlist{"$hash.$suffix"}) {
162 # Hash matches: if fingerprint matches its a duplicate cert
163 if ($hashlist{"$hash.$suffix"} eq $fprint) {
164 print STDERR "WARNING: Skipping duplicate certificate $fname\n";
170 if ($symlink_exists) {
171 print "link $fname -> $hash\n" if $verbose;
172 symlink $fname, $hash || warn "Can't symlink, $!";
174 print "copy $fname -> $hash\n" if $verbose;
175 if (open($in, "<", $fname)) {
176 if (open($out,">", $hash)) {
177 print $out $_ while (<$in>);
180 warn "can't open $hash for write, $!";
184 warn "can't open $fname for read, $!";
187 $hashlist{$hash} = $fprint;
190 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
194 $fname =~ s/'/'\\''/g;
195 my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`;
201 # Search for an unused hash filename
202 while(exists $hashlist{"$hash.r$suffix"}) {
203 # Hash matches: if fingerprint matches its a duplicate cert
204 if ($hashlist{"$hash.r$suffix"} eq $fprint) {
205 print STDERR "WARNING: Skipping duplicate CRL $fname\n";
210 $hash .= ".r$suffix";
211 if ($symlink_exists) {
212 print "link $fname -> $hash\n" if $verbose;
213 symlink $fname, $hash || warn "Can't symlink, $!";
215 print "cp $fname -> $hash\n" if $verbose;
216 system ("cp", $fname, $hash);
217 warn "Can't copy, $!" if ($? >> 8) != 0;
219 $hashlist{$hash} = $fprint;