3 # {- join("\n# ", @autowarntext) -}
5 # Perl c_rehash script, scan all files in a directory
6 # and add symbolic links to their hash values.
8 my $dir = {- quotify1($config{openssldir}) -};
9 my $prefix = {- quotify1($config{prefix}) -};
12 my $openssl = $ENV{OPENSSL} || "openssl";
14 my $x509hash = "-subject_hash";
15 my $crlhash = "-hash";
17 my $symlink_exists=eval {symlink("",""); 1};
21 while ( $ARGV[0] =~ /^-/ ) {
22 my $flag = shift @ARGV;
23 last if ( $flag eq '--');
24 if ( $flag eq '-old') {
25 $x509hash = "-subject_hash_old";
26 $crlhash = "-hash_old";
27 } elsif ( $flag eq '-h') {
29 } elsif ( $flag eq '-n' ) {
31 } elsif ( $flag eq '-v' ) {
35 print STDERR "Usage error; try -help.\n";
41 print "Usage: c_rehash [-old] [-h] [-v] [dirs...]\n";
42 print " -old use old-style digest\n";
43 print " -h print this help text\n";
44 print " -v print files removed and linked\n";
49 if (defined(&Cwd::getcwd)) {
56 # DOS/Win32 or Unix delimiter? Prefix our installdir, then search.
57 my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':';
58 $ENV{PATH} = "$prefix/bin" . ($ENV{PATH} ? $path_delim . $ENV{PATH} : "");
62 foreach (split /$path_delim/, $ENV{PATH}) {
63 if (-x "$_/$openssl") {
65 $openssl = "$_/$openssl";
70 print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
77 } elsif ($ENV{SSL_CERT_DIR}) {
78 @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};
80 $dirlist[0] = "$dir/certs";
85 $openssl="$pwd/$openssl" if (!-x $openssl);
94 print "Skipping $_, can't write\n";
103 print "Doing $_[0]\n";
106 my @flist = readdir(DIR);
108 if ( $removelinks ) {
109 # Delete any existing symbolic links
110 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
112 print "unlink $_" if $verbose;
113 unlink $_ || warn "Can't unlink $_, $!\n";
117 FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {
118 # Check to see if certificates and/or CRLs present.
119 my ($cert, $crl) = check_file($fname);
120 if (!$cert && !$crl) {
121 print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
124 link_hash_cert($fname) if ($cert);
125 link_hash_crl($fname) if ($crl);
130 my ($is_cert, $is_crl) = (0,0);
134 if (/^-----BEGIN (.*)-----/) {
136 if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
139 } elsif ($hdr eq "X509 CRL") {
146 return ($is_cert, $is_crl);
150 # Link a certificate to its subject name hash value, each hash is of
151 # the form <hash>.<n> where n is an integer. If the hash value already exists
152 # then we need to up the value of n, unless its a duplicate in which
153 # case we skip the link. We check for duplicates by comparing the
154 # certificate fingerprints
158 $fname =~ s/'/'\\''/g;
159 my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`;
165 # Search for an unused hash filename
166 while(exists $hashlist{"$hash.$suffix"}) {
167 # Hash matches: if fingerprint matches its a duplicate cert
168 if ($hashlist{"$hash.$suffix"} eq $fprint) {
169 print STDERR "WARNING: Skipping duplicate certificate $fname\n";
175 if ($symlink_exists) {
176 print "link $fname -> $hash\n" if $verbose;
177 symlink $fname, $hash || warn "Can't symlink, $!";
179 print "copy $fname -> $hash\n" if $verbose;
180 if (open($in, "<", $fname)) {
181 if (open($out,">", $hash)) {
182 print $out $_ while (<$in>);
185 warn "can't open $hash for write, $!";
189 warn "can't open $fname for read, $!";
192 $hashlist{$hash} = $fprint;
195 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
199 $fname =~ s/'/'\\''/g;
200 my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`;
206 # Search for an unused hash filename
207 while(exists $hashlist{"$hash.r$suffix"}) {
208 # Hash matches: if fingerprint matches its a duplicate cert
209 if ($hashlist{"$hash.r$suffix"} eq $fprint) {
210 print STDERR "WARNING: Skipping duplicate CRL $fname\n";
215 $hash .= ".r$suffix";
216 if ($symlink_exists) {
217 print "link $fname -> $hash\n" if $verbose;
218 symlink $fname, $hash || warn "Can't symlink, $!";
220 print "cp $fname -> $hash\n" if $verbose;
221 system ("cp", $fname, $hash);
222 warn "Can't copy, $!" if ($? >> 8) != 0;
224 $hashlist{$hash} = $fprint;