3 # Perl c_rehash script, scan all files in a directory
4 # and add symbolic links to their hash values.
10 my $openssl = $ENV{OPENSSL} || "openssl";
12 my $x509hash = "-subject_hash";
13 my $crlhash = "-hash";
15 my $symlink_exists=eval {symlink("",""); 1};
19 while ( $ARGV[0] =~ /^-/ ) {
20 my $flag = shift @ARGV;
21 last if ( $flag eq '--');
22 if ( $flag eq '-old') {
23 $x509hash = "-subject_hash_old";
24 $crlhash = "-hash_old";
25 } elsif ( $flag eq '-h') {
27 } elsif ( $flag eq '-n' ) {
29 } elsif ( $flag eq '-v' ) {
33 print STDERR "Usage error; try -help.\n";
39 print "Usage: c_rehash [-old] [-h] [-v] [dirs...]\n";
40 print " -old use old-style digest\n";
41 print " -h print this help text\n";
42 print " -v print files removed and linked\n";
47 if (defined(&Cwd::getcwd)) {
54 # DOS/Win32 or Unix delimiter? Prefix our installdir, then search.
55 my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':';
56 $ENV{PATH} = "$prefix/bin" . ($ENV{PATH} ? $path_delim . $ENV{PATH} : "");
60 foreach (split /$path_delim/, $ENV{PATH}) {
61 if (-x "$_/$openssl") {
63 $openssl = "$_/$openssl";
68 print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
75 } elsif ($ENV{SSL_CERT_DIR}) {
76 @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};
78 $dirlist[0] = "$dir/certs";
83 $openssl="$pwd/$openssl" if (!-x $openssl);
92 print "Skipping $_, can't write\n";
101 print "Doing $_[0]\n";
104 my @flist = readdir(DIR);
106 if ( $removelinks ) {
107 # Delete any existing symbolic links
108 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
110 print "unlink $_" if $verbose;
111 unlink $_ || warn "Can't unlink $_, $!\n";
115 FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {
116 # Check to see if certificates and/or CRLs present.
117 my ($cert, $crl) = check_file($fname);
118 if (!$cert && !$crl) {
119 print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
122 link_hash_cert($fname) if ($cert);
123 link_hash_crl($fname) if ($crl);
128 my ($is_cert, $is_crl) = (0,0);
132 if (/^-----BEGIN (.*)-----/) {
134 if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
137 } elsif ($hdr eq "X509 CRL") {
144 return ($is_cert, $is_crl);
148 # Link a certificate to its subject name hash value, each hash is of
149 # the form <hash>.<n> where n is an integer. If the hash value already exists
150 # then we need to up the value of n, unless its a duplicate in which
151 # case we skip the link. We check for duplicates by comparing the
152 # certificate fingerprints
156 $fname =~ s/'/'\\''/g;
157 my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`;
163 # Search for an unused hash filename
164 while(exists $hashlist{"$hash.$suffix"}) {
165 # Hash matches: if fingerprint matches its a duplicate cert
166 if ($hashlist{"$hash.$suffix"} eq $fprint) {
167 print STDERR "WARNING: Skipping duplicate certificate $fname\n";
173 if ($symlink_exists) {
174 print "link $fname -> $hash\n" if $verbose;
175 symlink $fname, $hash || warn "Can't symlink, $!";
177 print "copy $fname -> $hash\n" if $verbose;
178 if (open($in, "<", $fname)) {
179 if (open($out,">", $hash)) {
180 print $out $_ while (<$in>);
183 warn "can't open $hash for write, $!";
187 warn "can't open $fname for read, $!";
190 $hashlist{$hash} = $fprint;
193 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>
197 $fname =~ s/'/'\\''/g;
198 my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`;
204 # Search for an unused hash filename
205 while(exists $hashlist{"$hash.r$suffix"}) {
206 # Hash matches: if fingerprint matches its a duplicate cert
207 if ($hashlist{"$hash.r$suffix"} eq $fprint) {
208 print STDERR "WARNING: Skipping duplicate CRL $fname\n";
213 $hash .= ".r$suffix";
214 if ($symlink_exists) {
215 print "link $fname -> $hash\n" if $verbose;
216 symlink $fname, $hash || warn "Can't symlink, $!";
218 print "cp $fname -> $hash\n" if $verbose;
219 system ("cp", $fname, $hash);
220 warn "Can't copy, $!" if ($? >> 8) != 0;
222 $hashlist{$hash} = $fprint;