lost track of attempts to dump cruft
[feisty_meow.git] / production / 3rdparty / deprecated / openssl / lib / c_rehash.pl
1 #!/usr/bin/env perl\r
2 \r
3 # WARNING: do not edit!\r
4 # Generated by makefile from tools\c_rehash.in\r
5 # Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.\r
6 #\r
7 # Licensed under the OpenSSL license (the "License").  You may not use\r
8 # this file except in compliance with the License.  You can obtain a copy\r
9 # in the file LICENSE in the source distribution or at\r
10 # https://www.openssl.org/source/license.html\r
11 \r
12 # Perl c_rehash script, scan all files in a directory\r
13 # and add symbolic links to their hash values.\r
14 \r
15 my $dir = "";\r
16 my $prefix = "";\r
17 \r
18 my $errorcount = 0;\r
19 my $openssl = $ENV{OPENSSL} || "openssl";\r
20 my $pwd;\r
21 my $x509hash = "-subject_hash";\r
22 my $crlhash = "-hash";\r
23 my $verbose = 0;\r
24 my $symlink_exists=eval {symlink("",""); 1};\r
25 my $removelinks = 1;\r
26 \r
27 ##  Parse flags.\r
28 while ( $ARGV[0] =~ /^-/ ) {\r
29     my $flag = shift @ARGV;\r
30     last if ( $flag eq '--');\r
31     if ( $flag eq '-old') {\r
32             $x509hash = "-subject_hash_old";\r
33             $crlhash = "-hash_old";\r
34     } elsif ( $flag eq '-h' || $flag eq '-help' ) {\r
35             help();\r
36     } elsif ( $flag eq '-n' ) {\r
37             $removelinks = 0;\r
38     } elsif ( $flag eq '-v' ) {\r
39             $verbose++;\r
40     }\r
41     else {\r
42             print STDERR "Usage error; try -h.\n";\r
43             exit 1;\r
44     }\r
45 }\r
46 \r
47 sub help {\r
48         print "Usage: c_rehash [-old] [-h] [-help] [-v] [dirs...]\n";\r
49         print "   -old use old-style digest\n";\r
50         print "   -h or -help print this help text\n";\r
51         print "   -v print files removed and linked\n";\r
52         exit 0;\r
53 }\r
54 \r
55 eval "require Cwd";\r
56 if (defined(&Cwd::getcwd)) {\r
57         $pwd=Cwd::getcwd();\r
58 } else {\r
59         $pwd=`pwd`;\r
60         chomp($pwd);\r
61 }\r
62 \r
63 # DOS/Win32 or Unix delimiter?  Prefix our installdir, then search.\r
64 my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':';\r
65 $ENV{PATH} = "$prefix/bin" . ($ENV{PATH} ? $path_delim . $ENV{PATH} : "");\r
66 \r
67 if (! -x $openssl) {\r
68         my $found = 0;\r
69         foreach (split /$path_delim/, $ENV{PATH}) {\r
70                 if (-x "$_/$openssl") {\r
71                         $found = 1;\r
72                         $openssl = "$_/$openssl";\r
73                         last;\r
74                 }       \r
75         }\r
76         if ($found == 0) {\r
77                 print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";\r
78                 exit 0;\r
79         }\r
80 }\r
81 \r
82 if (@ARGV) {\r
83         @dirlist = @ARGV;\r
84 } elsif ($ENV{SSL_CERT_DIR}) {\r
85         @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};\r
86 } else {\r
87         $dirlist[0] = "$dir/certs";\r
88 }\r
89 \r
90 if (-d $dirlist[0]) {\r
91         chdir $dirlist[0];\r
92         $openssl="$pwd/$openssl" if (!-x $openssl);\r
93         chdir $pwd;\r
94 }\r
95 \r
96 foreach (@dirlist) {\r
97         if (-d $_ ) {\r
98             if ( -w $_) {\r
99                 hash_dir($_);\r
100             } else {\r
101                 print "Skipping $_, can't write\n";\r
102                 $errorcount++;\r
103             }\r
104         }\r
105 }\r
106 exit($errorcount);\r
107 \r
108 sub hash_dir {\r
109         my %hashlist;\r
110         print "Doing $_[0]\n";\r
111         chdir $_[0];\r
112         opendir(DIR, ".");\r
113         my @flist = sort readdir(DIR);\r
114         closedir DIR;\r
115         if ( $removelinks ) {\r
116                 # Delete any existing symbolic links\r
117                 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {\r
118                         if (-l $_) {\r
119                                 print "unlink $_" if $verbose;\r
120                                 unlink $_ || warn "Can't unlink $_, $!\n";\r
121                         }\r
122                 }\r
123         }\r
124         FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {\r
125                 # Check to see if certificates and/or CRLs present.\r
126                 my ($cert, $crl) = check_file($fname);\r
127                 if (!$cert && !$crl) {\r
128                         print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";\r
129                         next;\r
130                 }\r
131                 link_hash_cert($fname) if ($cert);\r
132                 link_hash_crl($fname) if ($crl);\r
133         }\r
134 }\r
135 \r
136 sub check_file {\r
137         my ($is_cert, $is_crl) = (0,0);\r
138         my $fname = $_[0];\r
139         open IN, $fname;\r
140         while(<IN>) {\r
141                 if (/^-----BEGIN (.*)-----/) {\r
142                         my $hdr = $1;\r
143                         if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {\r
144                                 $is_cert = 1;\r
145                                 last if ($is_crl);\r
146                         } elsif ($hdr eq "X509 CRL") {\r
147                                 $is_crl = 1;\r
148                                 last if ($is_cert);\r
149                         }\r
150                 }\r
151         }\r
152         close IN;\r
153         return ($is_cert, $is_crl);\r
154 }\r
155 \r
156 \r
157 # Link a certificate to its subject name hash value, each hash is of\r
158 # the form <hash>.<n> where n is an integer. If the hash value already exists\r
159 # then we need to up the value of n, unless its a duplicate in which\r
160 # case we skip the link. We check for duplicates by comparing the\r
161 # certificate fingerprints\r
162 \r
163 sub link_hash_cert {\r
164                 my $fname = $_[0];\r
165                 $fname =~ s/'/'\\''/g;\r
166                 my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`;\r
167                 chomp $hash;\r
168                 chomp $fprint;\r
169                 $fprint =~ s/^.*=//;\r
170                 $fprint =~ tr/://d;\r
171                 my $suffix = 0;\r
172                 # Search for an unused hash filename\r
173                 while(exists $hashlist{"$hash.$suffix"}) {\r
174                         # Hash matches: if fingerprint matches its a duplicate cert\r
175                         if ($hashlist{"$hash.$suffix"} eq $fprint) {\r
176                                 print STDERR "WARNING: Skipping duplicate certificate $fname\n";\r
177                                 return;\r
178                         }\r
179                         $suffix++;\r
180                 }\r
181                 $hash .= ".$suffix";\r
182                 if ($symlink_exists) {\r
183                         print "link $fname -> $hash\n" if $verbose;\r
184                         symlink $fname, $hash || warn "Can't symlink, $!";\r
185                 } else {\r
186                         print "copy $fname -> $hash\n" if $verbose;\r
187                         if (open($in, "<", $fname)) {\r
188                             if (open($out,">", $hash)) {\r
189                                 print $out $_ while (<$in>);\r
190                                 close $out;\r
191                             } else {\r
192                                 warn "can't open $hash for write, $!";\r
193                             }\r
194                             close $in;\r
195                         } else {\r
196                             warn "can't open $fname for read, $!";\r
197                         }\r
198                 }\r
199                 $hashlist{$hash} = $fprint;\r
200 }\r
201 \r
202 # Same as above except for a CRL. CRL links are of the form <hash>.r<n>\r
203 \r
204 sub link_hash_crl {\r
205                 my $fname = $_[0];\r
206                 $fname =~ s/'/'\\''/g;\r
207                 my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`;\r
208                 chomp $hash;\r
209                 chomp $fprint;\r
210                 $fprint =~ s/^.*=//;\r
211                 $fprint =~ tr/://d;\r
212                 my $suffix = 0;\r
213                 # Search for an unused hash filename\r
214                 while(exists $hashlist{"$hash.r$suffix"}) {\r
215                         # Hash matches: if fingerprint matches its a duplicate cert\r
216                         if ($hashlist{"$hash.r$suffix"} eq $fprint) {\r
217                                 print STDERR "WARNING: Skipping duplicate CRL $fname\n";\r
218                                 return;\r
219                         }\r
220                         $suffix++;\r
221                 }\r
222                 $hash .= ".r$suffix";\r
223                 if ($symlink_exists) {\r
224                         print "link $fname -> $hash\n" if $verbose;\r
225                         symlink $fname, $hash || warn "Can't symlink, $!";\r
226                 } else {\r
227                         print "cp $fname -> $hash\n" if $verbose;\r
228                         system ("cp", $fname, $hash);\r
229                         warn "Can't copy, $!" if ($? >> 8) != 0;\r
230                 }\r
231                 $hashlist{$hash} = $fprint;\r
232 }\r