| OLD | NEW | 
|---|
| 1 #!/usr/bin/perl | 1 #!/usr/bin/perl | 
| 2 | 2 | 
| 3 | 3 | 
| 4 # Perl c_rehash script, scan all files in a directory | 4 # Perl c_rehash script, scan all files in a directory | 
| 5 # and add symbolic links to their hash values. | 5 # and add symbolic links to their hash values. | 
| 6 | 6 | 
| 7 my $openssl; | 7 my $openssl; | 
| 8 | 8 | 
| 9 my $dir = "/usr/local/ssl"; | 9 my $dir = "/usr/local/ssl"; | 
| 10 my $prefix = "/usr/local/ssl"; | 10 my $prefix = "/usr/local/ssl"; | 
| 11 | 11 | 
| 12 if(defined $ENV{OPENSSL}) { | 12 if(defined $ENV{OPENSSL}) { | 
| 13         $openssl = $ENV{OPENSSL}; | 13         $openssl = $ENV{OPENSSL}; | 
| 14 } else { | 14 } else { | 
| 15         $openssl = "openssl"; | 15         $openssl = "openssl"; | 
| 16         $ENV{OPENSSL} = $openssl; | 16         $ENV{OPENSSL} = $openssl; | 
| 17 } | 17 } | 
| 18 | 18 | 
| 19 $ENV{PATH} .= ":$dir/bin"; | 19 my $pwd; | 
|  | 20 eval "require Cwd"; | 
|  | 21 if (defined(&Cwd::getcwd)) { | 
|  | 22 »       $pwd=Cwd::getcwd(); | 
|  | 23 } else { | 
|  | 24 »       $pwd=`pwd`; chomp($pwd); | 
|  | 25 } | 
|  | 26 my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':'; # DOS/Win32 or Unix delimite
     r? | 
|  | 27 | 
|  | 28 $ENV{PATH} = "$prefix/bin" . ($ENV{PATH} ? $path_delim . $ENV{PATH} : ""); # pre
     fix our path | 
| 20 | 29 | 
| 21 if(! -x $openssl) { | 30 if(! -x $openssl) { | 
| 22         my $found = 0; | 31         my $found = 0; | 
| 23 »       foreach (split /:/, $ENV{PATH}) { | 32 »       foreach (split /$path_delim/, $ENV{PATH}) { | 
| 24                 if(-x "$_/$openssl") { | 33                 if(-x "$_/$openssl") { | 
| 25                         $found = 1; | 34                         $found = 1; | 
|  | 35                         $openssl = "$_/$openssl"; | 
| 26                         last; | 36                         last; | 
| 27                 } | 37                 } | 
| 28         } | 38         } | 
| 29         if($found == 0) { | 39         if($found == 0) { | 
| 30                 print STDERR "c_rehash: rehashing skipped ('openssl' program not
      available)\n"; | 40                 print STDERR "c_rehash: rehashing skipped ('openssl' program not
      available)\n"; | 
| 31                 exit 0; | 41                 exit 0; | 
| 32         } | 42         } | 
| 33 } | 43 } | 
| 34 | 44 | 
| 35 if(@ARGV) { | 45 if(@ARGV) { | 
| 36         @dirlist = @ARGV; | 46         @dirlist = @ARGV; | 
| 37 } elsif($ENV{SSL_CERT_DIR}) { | 47 } elsif($ENV{SSL_CERT_DIR}) { | 
| 38 »       @dirlist = split /:/, $ENV{SSL_CERT_DIR}; | 48 »       @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR}; | 
| 39 } else { | 49 } else { | 
| 40         $dirlist[0] = "$dir/certs"; | 50         $dirlist[0] = "$dir/certs"; | 
| 41 } | 51 } | 
| 42 | 52 | 
|  | 53 if (-d $dirlist[0]) { | 
|  | 54         chdir $dirlist[0]; | 
|  | 55         $openssl="$pwd/$openssl" if (!-x $openssl); | 
|  | 56         chdir $pwd; | 
|  | 57 } | 
| 43 | 58 | 
| 44 foreach (@dirlist) { | 59 foreach (@dirlist) { | 
| 45         if(-d $_ and -w $_) { | 60         if(-d $_ and -w $_) { | 
| 46                 hash_dir($_); | 61                 hash_dir($_); | 
| 47         } | 62         } | 
| 48 } | 63 } | 
| 49 | 64 | 
| 50 sub hash_dir { | 65 sub hash_dir { | 
| 51         my %hashlist; | 66         my %hashlist; | 
| 52         print "Doing $_[0]\n"; | 67         print "Doing $_[0]\n"; | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 95 | 110 | 
| 96 # Link a certificate to its subject name hash value, each hash is of | 111 # Link a certificate to its subject name hash value, each hash is of | 
| 97 # the form <hash>.<n> where n is an integer. If the hash value already exists | 112 # the form <hash>.<n> where n is an integer. If the hash value already exists | 
| 98 # then we need to up the value of n, unless its a duplicate in which | 113 # then we need to up the value of n, unless its a duplicate in which | 
| 99 # case we skip the link. We check for duplicates by comparing the | 114 # case we skip the link. We check for duplicates by comparing the | 
| 100 # certificate fingerprints | 115 # certificate fingerprints | 
| 101 | 116 | 
| 102 sub link_hash_cert { | 117 sub link_hash_cert { | 
| 103                 my $fname = $_[0]; | 118                 my $fname = $_[0]; | 
| 104                 $fname =~ s/'/'\\''/g; | 119                 $fname =~ s/'/'\\''/g; | 
| 105 »       »       my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout
      -in '$fname'`; | 120 »       »       my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout
      -in "$fname"`; | 
| 106                 chomp $hash; | 121                 chomp $hash; | 
| 107                 chomp $fprint; | 122                 chomp $fprint; | 
| 108                 $fprint =~ s/^.*=//; | 123                 $fprint =~ s/^.*=//; | 
| 109                 $fprint =~ tr/://d; | 124                 $fprint =~ tr/://d; | 
| 110                 my $suffix = 0; | 125                 my $suffix = 0; | 
| 111                 # Search for an unused hash filename | 126                 # Search for an unused hash filename | 
| 112                 while(exists $hashlist{"$hash.$suffix"}) { | 127                 while(exists $hashlist{"$hash.$suffix"}) { | 
| 113                         # Hash matches: if fingerprint matches its a duplicate c
     ert | 128                         # Hash matches: if fingerprint matches its a duplicate c
     ert | 
| 114                         if($hashlist{"$hash.$suffix"} eq $fprint) { | 129                         if($hashlist{"$hash.$suffix"} eq $fprint) { | 
| 115                                 print STDERR "WARNING: Skipping duplicate certif
     icate $fname\n"; | 130                                 print STDERR "WARNING: Skipping duplicate certif
     icate $fname\n"; | 
| 116                                 return; | 131                                 return; | 
| 117                         } | 132                         } | 
| 118                         $suffix++; | 133                         $suffix++; | 
| 119                 } | 134                 } | 
| 120                 $hash .= ".$suffix"; | 135                 $hash .= ".$suffix"; | 
| 121                 print "$fname => $hash\n"; | 136                 print "$fname => $hash\n"; | 
| 122                 $symlink_exists=eval {symlink("",""); 1}; | 137                 $symlink_exists=eval {symlink("",""); 1}; | 
| 123                 if ($symlink_exists) { | 138                 if ($symlink_exists) { | 
| 124                         symlink $fname, $hash; | 139                         symlink $fname, $hash; | 
| 125                 } else { | 140                 } else { | 
| 126 »       »       »       system ("cp", $fname, $hash); | 141 »       »       »       open IN,"<$fname" or die "can't open $fname for read"; | 
|  | 142 »       »       »       open OUT,">$hash" or die "can't open $hash for write"; | 
|  | 143 »       »       »       print OUT <IN>;»# does the job for small text files | 
|  | 144 »       »       »       close OUT; | 
|  | 145 »       »       »       close IN; | 
| 127                 } | 146                 } | 
| 128                 $hashlist{$hash} = $fprint; | 147                 $hashlist{$hash} = $fprint; | 
| 129 } | 148 } | 
| 130 | 149 | 
| 131 # Same as above except for a CRL. CRL links are of the form <hash>.r<n> | 150 # Same as above except for a CRL. CRL links are of the form <hash>.r<n> | 
| 132 | 151 | 
| 133 sub link_hash_crl { | 152 sub link_hash_crl { | 
| 134                 my $fname = $_[0]; | 153                 my $fname = $_[0]; | 
| 135                 $fname =~ s/'/'\\''/g; | 154                 $fname =~ s/'/'\\''/g; | 
| 136                 my ($hash, $fprint) = `"$openssl" crl -hash -fingerprint -noout 
     -in '$fname'`; | 155                 my ($hash, $fprint) = `"$openssl" crl -hash -fingerprint -noout 
     -in '$fname'`; | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
| 152                 print "$fname => $hash\n"; | 171                 print "$fname => $hash\n"; | 
| 153                 $symlink_exists=eval {symlink("",""); 1}; | 172                 $symlink_exists=eval {symlink("",""); 1}; | 
| 154                 if ($symlink_exists) { | 173                 if ($symlink_exists) { | 
| 155                         symlink $fname, $hash; | 174                         symlink $fname, $hash; | 
| 156                 } else { | 175                 } else { | 
| 157                         system ("cp", $fname, $hash); | 176                         system ("cp", $fname, $hash); | 
| 158                 } | 177                 } | 
| 159                 $hashlist{$hash} = $fprint; | 178                 $hashlist{$hash} = $fprint; | 
| 160 } | 179 } | 
| 161 | 180 | 
| OLD | NEW | 
|---|