blob: dc66a9d9625872a2eeb28ad364f2af5546721b84 [file] [log] [blame]
Richard Levitte291e94d2015-05-18 22:35:23 +02001#!{- $config{perl} -}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002
Richard Levitte9ab6fc52016-01-25 21:19:59 +01003# {- join("\n# ", @autowarntext) -}
4
Dr. Stephen Henson439df502000-05-18 00:33:00 +00005# Perl c_rehash script, scan all files in a directory
6# and add symbolic links to their hash values.
Ralf S. Engelschall13e91dd1998-12-22 15:59:57 +00007
Richard Levitte291e94d2015-05-18 22:35:23 +02008my $dir = {- quotify1($config{openssldir}) -};
9my $prefix = {- quotify1($config{prefix}) -};
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000010
Rich Salz4c7103a2015-09-10 11:46:13 -040011my $errorcount = 0;
Matthias Andreea787c252014-09-07 18:45:02 -040012my $openssl = $ENV{OPENSSL} || "openssl";
13my $pwd;
14my $x509hash = "-subject_hash";
15my $crlhash = "-hash";
16my $verbose = 0;
17my $symlink_exists=eval {symlink("",""); 1};
18my $removelinks = 1;
19
20## Parse flags.
Olaf Johansson8846adb2015-06-02 07:41:35 -040021while ( $ARGV[0] =~ /^-/ ) {
Matthias Andreea787c252014-09-07 18:45:02 -040022 my $flag = shift @ARGV;
23 last if ( $flag eq '--');
Olaf Johansson8846adb2015-06-02 07:41:35 -040024 if ( $flag eq '-old') {
Matthias Andreea787c252014-09-07 18:45:02 -040025 $x509hash = "-subject_hash_old";
26 $crlhash = "-hash_old";
Olaf Johansson8846adb2015-06-02 07:41:35 -040027 } elsif ( $flag eq '-h') {
Matthias Andreea787c252014-09-07 18:45:02 -040028 help();
29 } elsif ( $flag eq '-n' ) {
30 $removelinks = 0;
31 } elsif ( $flag eq '-v' ) {
32 $verbose++;
33 }
34 else {
35 print STDERR "Usage error; try -help.\n";
36 exit 1;
37 }
Dr. Stephen Henson439df502000-05-18 00:33:00 +000038}
39
Matthias Andreea787c252014-09-07 18:45:02 -040040sub help {
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";
45 exit 0;
46}
47
Andy Polyakova2688c82006-10-26 10:52:12 +000048eval "require Cwd";
49if (defined(&Cwd::getcwd)) {
50 $pwd=Cwd::getcwd();
51} else {
Matthias Andreea787c252014-09-07 18:45:02 -040052 $pwd=`pwd`;
53 chomp($pwd);
Andy Polyakova2688c82006-10-26 10:52:12 +000054}
Andy Polyakovd8cdd152006-10-21 16:28:03 +000055
Matthias Andreea787c252014-09-07 18:45:02 -040056# DOS/Win32 or Unix delimiter? Prefix our installdir, then search.
57my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':';
58$ENV{PATH} = "$prefix/bin" . ($ENV{PATH} ? $path_delim . $ENV{PATH} : "");
Dr. Stephen Henson439df502000-05-18 00:33:00 +000059
Rich Salzff2f6bb2015-09-07 22:21:38 -040060if (! -x $openssl) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +000061 my $found = 0;
Andy Polyakovd8cdd152006-10-21 16:28:03 +000062 foreach (split /$path_delim/, $ENV{PATH}) {
Rich Salzff2f6bb2015-09-07 22:21:38 -040063 if (-x "$_/$openssl") {
Dr. Stephen Henson439df502000-05-18 00:33:00 +000064 $found = 1;
Dr. Stephen Hensonef236ec2009-04-23 16:32:42 +000065 $openssl = "$_/$openssl";
Dr. Stephen Henson439df502000-05-18 00:33:00 +000066 last;
67 }
68 }
Rich Salzff2f6bb2015-09-07 22:21:38 -040069 if ($found == 0) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +000070 print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
71 exit 0;
72 }
73}
74
Rich Salzff2f6bb2015-09-07 22:21:38 -040075if (@ARGV) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +000076 @dirlist = @ARGV;
Rich Salzff2f6bb2015-09-07 22:21:38 -040077} elsif ($ENV{SSL_CERT_DIR}) {
Andy Polyakovd8cdd152006-10-21 16:28:03 +000078 @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};
Dr. Stephen Henson439df502000-05-18 00:33:00 +000079} else {
80 $dirlist[0] = "$dir/certs";
81}
82
Andy Polyakovd8cdd152006-10-21 16:28:03 +000083if (-d $dirlist[0]) {
84 chdir $dirlist[0];
85 $openssl="$pwd/$openssl" if (!-x $openssl);
86 chdir $pwd;
87}
Dr. Stephen Henson439df502000-05-18 00:33:00 +000088
89foreach (@dirlist) {
Rich Salzff2f6bb2015-09-07 22:21:38 -040090 if (-d $_ ) {
91 if ( -w $_) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +000092 hash_dir($_);
Rich Salzff2f6bb2015-09-07 22:21:38 -040093 } else {
94 print "Skipping $_, can't write\n";
Rich Salz4c7103a2015-09-10 11:46:13 -040095 $errorcount++;
Rich Salzff2f6bb2015-09-07 22:21:38 -040096 }
Dr. Stephen Henson439df502000-05-18 00:33:00 +000097 }
98}
Rich Salz4c7103a2015-09-10 11:46:13 -040099exit($errorcount);
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000100
101sub hash_dir {
102 my %hashlist;
103 print "Doing $_[0]\n";
104 chdir $_[0];
Rich Salz6f46c3c2014-09-11 13:08:30 -0400105 opendir(DIR, ".");
106 my @flist = readdir(DIR);
107 closedir DIR;
Matthias Andreea787c252014-09-07 18:45:02 -0400108 if ( $removelinks ) {
Matthias Andreea787c252014-09-07 18:45:02 -0400109 # Delete any existing symbolic links
110 foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
Rich Salzff2f6bb2015-09-07 22:21:38 -0400111 if (-l $_) {
Matthias Andreea787c252014-09-07 18:45:02 -0400112 print "unlink $_" if $verbose;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400113 unlink $_ || warn "Can't unlink $_, $!\n";
Matthias Andreea787c252014-09-07 18:45:02 -0400114 }
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000115 }
116 }
Matthias Andreea787c252014-09-07 18:45:02 -0400117 FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000118 # Check to see if certificates and/or CRLs present.
119 my ($cert, $crl) = check_file($fname);
Rich Salzff2f6bb2015-09-07 22:21:38 -0400120 if (!$cert && !$crl) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000121 print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
122 next;
123 }
Rich Salzff2f6bb2015-09-07 22:21:38 -0400124 link_hash_cert($fname) if ($cert);
125 link_hash_crl($fname) if ($crl);
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000126 }
127}
128
129sub check_file {
130 my ($is_cert, $is_crl) = (0,0);
131 my $fname = $_[0];
132 open IN, $fname;
133 while(<IN>) {
Rich Salzff2f6bb2015-09-07 22:21:38 -0400134 if (/^-----BEGIN (.*)-----/) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000135 my $hdr = $1;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400136 if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000137 $is_cert = 1;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400138 last if ($is_crl);
139 } elsif ($hdr eq "X509 CRL") {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000140 $is_crl = 1;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400141 last if ($is_cert);
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000142 }
143 }
144 }
145 close IN;
146 return ($is_cert, $is_crl);
147}
148
149
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
155
156sub link_hash_cert {
157 my $fname = $_[0];
Richard Levitteb7910992002-10-11 11:34:20 +0000158 $fname =~ s/'/'\\''/g;
Matthias Andreea787c252014-09-07 18:45:02 -0400159 my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`;
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000160 chomp $hash;
161 chomp $fprint;
162 $fprint =~ s/^.*=//;
163 $fprint =~ tr/://d;
164 my $suffix = 0;
165 # Search for an unused hash filename
166 while(exists $hashlist{"$hash.$suffix"}) {
167 # Hash matches: if fingerprint matches its a duplicate cert
Rich Salzff2f6bb2015-09-07 22:21:38 -0400168 if ($hashlist{"$hash.$suffix"} eq $fprint) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000169 print STDERR "WARNING: Skipping duplicate certificate $fname\n";
170 return;
171 }
172 $suffix++;
173 }
174 $hash .= ".$suffix";
Richard Levitte967d95f2001-04-04 15:50:30 +0000175 if ($symlink_exists) {
Matthias Andreea787c252014-09-07 18:45:02 -0400176 print "link $fname -> $hash\n" if $verbose;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400177 symlink $fname, $hash || warn "Can't symlink, $!";
Richard Levitte967d95f2001-04-04 15:50:30 +0000178 } else {
Matthias Andreea787c252014-09-07 18:45:02 -0400179 print "copy $fname -> $hash\n" if $verbose;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400180 if (open($in, "<", $fname)) {
181 if (open($out,">", $hash)) {
182 print $out $_ while (<$in>);
183 close $out;
184 } else {
185 warn "can't open $hash for write, $!";
186 }
187 close $in;
188 } else {
189 warn "can't open $fname for read, $!";
190 }
Richard Levitte967d95f2001-04-04 15:50:30 +0000191 }
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000192 $hashlist{$hash} = $fprint;
193}
194
195# Same as above except for a CRL. CRL links are of the form <hash>.r<n>
196
197sub link_hash_crl {
198 my $fname = $_[0];
Richard Levittecaa4f472002-10-11 20:28:23 +0000199 $fname =~ s/'/'\\''/g;
Matthias Andreea787c252014-09-07 18:45:02 -0400200 my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`;
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000201 chomp $hash;
202 chomp $fprint;
203 $fprint =~ s/^.*=//;
204 $fprint =~ tr/://d;
205 my $suffix = 0;
206 # Search for an unused hash filename
207 while(exists $hashlist{"$hash.r$suffix"}) {
208 # Hash matches: if fingerprint matches its a duplicate cert
Rich Salzff2f6bb2015-09-07 22:21:38 -0400209 if ($hashlist{"$hash.r$suffix"} eq $fprint) {
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000210 print STDERR "WARNING: Skipping duplicate CRL $fname\n";
211 return;
212 }
213 $suffix++;
214 }
215 $hash .= ".r$suffix";
Richard Levitte967d95f2001-04-04 15:50:30 +0000216 if ($symlink_exists) {
Matthias Andreea787c252014-09-07 18:45:02 -0400217 print "link $fname -> $hash\n" if $verbose;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400218 symlink $fname, $hash || warn "Can't symlink, $!";
Richard Levitte967d95f2001-04-04 15:50:30 +0000219 } else {
Matthias Andreea787c252014-09-07 18:45:02 -0400220 print "cp $fname -> $hash\n" if $verbose;
Rich Salzff2f6bb2015-09-07 22:21:38 -0400221 system ("cp", $fname, $hash);
222 warn "Can't copy, $!" if ($? >> 8) != 0;
Richard Levitte967d95f2001-04-04 15:50:30 +0000223 }
Dr. Stephen Henson439df502000-05-18 00:33:00 +0000224 $hashlist{$hash} = $fprint;
225}