c_rehash.in 5.7 KB
Newer Older
1
#!{- $config{perl} -}
2

R
Richard Levitte 已提交
3 4
# {- join("\n# ", @autowarntext) -}

5 6 7
# Perl c_rehash script, scan all files in a directory
# and add symbolic links to their hash values.

8 9
my $dir = {- quotify1($config{openssldir}) -};
my $prefix = {- quotify1($config{prefix}) -};
10

R
Rich Salz 已提交
11
my $errorcount = 0;
12 13 14 15 16 17 18 19 20
my $openssl = $ENV{OPENSSL} || "openssl";
my $pwd;
my $x509hash = "-subject_hash";
my $crlhash = "-hash";
my $verbose = 0;
my $symlink_exists=eval {symlink("",""); 1};
my $removelinks = 1;

##  Parse flags.
21
while ( $ARGV[0] =~ /^-/ ) {
22 23
    my $flag = shift @ARGV;
    last if ( $flag eq '--');
24
    if ( $flag eq '-old') {
25 26
	    $x509hash = "-subject_hash_old";
	    $crlhash = "-hash_old";
27
    } elsif ( $flag eq '-h') {
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	    help();
    } elsif ( $flag eq '-n' ) {
	    $removelinks = 0;
    } elsif ( $flag eq '-v' ) {
	    $verbose++;
    }
    else {
	    print STDERR "Usage error; try -help.\n";
	    exit 1;
    }
}

sub help {
	print "Usage: c_rehash [-old] [-h] [-v] [dirs...]\n";
	print "   -old use old-style digest\n";
	print "   -h print this help text\n";
	print "   -v print files removed and linked\n";
	exit 0;
46 47
}

48 49 50 51
eval "require Cwd";
if (defined(&Cwd::getcwd)) {
	$pwd=Cwd::getcwd();
} else {
52 53
	$pwd=`pwd`;
	chomp($pwd);
54
}
55

56 57 58
# DOS/Win32 or Unix delimiter?  Prefix our installdir, then search.
my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':';
$ENV{PATH} = "$prefix/bin" . ($ENV{PATH} ? $path_delim . $ENV{PATH} : "");
59

60
if (! -x $openssl) {
61
	my $found = 0;
62
	foreach (split /$path_delim/, $ENV{PATH}) {
63
		if (-x "$_/$openssl") {
64
			$found = 1;
65
			$openssl = "$_/$openssl";
66 67 68
			last;
		}	
	}
69
	if ($found == 0) {
70 71 72 73 74
		print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";
		exit 0;
	}
}

75
if (@ARGV) {
76
	@dirlist = @ARGV;
77
} elsif ($ENV{SSL_CERT_DIR}) {
78
	@dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};
79 80 81 82
} else {
	$dirlist[0] = "$dir/certs";
}

83 84 85 86 87
if (-d $dirlist[0]) {
	chdir $dirlist[0];
	$openssl="$pwd/$openssl" if (!-x $openssl);
	chdir $pwd;
}
88 89

foreach (@dirlist) {
90 91
	if (-d $_ ) {
            if ( -w $_) {
92
		hash_dir($_);
93 94
            } else {
                print "Skipping $_, can't write\n";
R
Rich Salz 已提交
95
                $errorcount++;
96
            }
97 98
	}
}
R
Rich Salz 已提交
99
exit($errorcount);
100 101 102 103 104

sub hash_dir {
	my %hashlist;
	print "Doing $_[0]\n";
	chdir $_[0];
R
Rich Salz 已提交
105 106 107
	opendir(DIR, ".");
	my @flist = readdir(DIR);
	closedir DIR;
108 109 110
	if ( $removelinks ) {
		# Delete any existing symbolic links
		foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {
111
			if (-l $_) {
112
				print "unlink $_" if $verbose;
113
				unlink $_ || warn "Can't unlink $_, $!\n";
114
			}
115 116
		}
	}
117
	FILE: foreach $fname (grep {/\.(pem)|(crt)|(cer)|(crl)$/} @flist) {
118 119
		# Check to see if certificates and/or CRLs present.
		my ($cert, $crl) = check_file($fname);
120
		if (!$cert && !$crl) {
121 122 123
			print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";
			next;
		}
124 125
		link_hash_cert($fname) if ($cert);
		link_hash_crl($fname) if ($crl);
126 127 128 129 130 131 132 133
	}
}

sub check_file {
	my ($is_cert, $is_crl) = (0,0);
	my $fname = $_[0];
	open IN, $fname;
	while(<IN>) {
134
		if (/^-----BEGIN (.*)-----/) {
135
			my $hdr = $1;
136
			if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {
137
				$is_cert = 1;
138 139
				last if ($is_crl);
			} elsif ($hdr eq "X509 CRL") {
140
				$is_crl = 1;
141
				last if ($is_cert);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
			}
		}
	}
	close IN;
	return ($is_cert, $is_crl);
}


# Link a certificate to its subject name hash value, each hash is of
# the form <hash>.<n> where n is an integer. If the hash value already exists
# then we need to up the value of n, unless its a duplicate in which
# case we skip the link. We check for duplicates by comparing the
# certificate fingerprints

sub link_hash_cert {
		my $fname = $_[0];
158
		$fname =~ s/'/'\\''/g;
159
		my ($hash, $fprint) = `"$openssl" x509 $x509hash -fingerprint -noout -in "$fname"`;
160 161 162 163 164 165 166 167
		chomp $hash;
		chomp $fprint;
		$fprint =~ s/^.*=//;
		$fprint =~ tr/://d;
		my $suffix = 0;
		# Search for an unused hash filename
		while(exists $hashlist{"$hash.$suffix"}) {
			# Hash matches: if fingerprint matches its a duplicate cert
168
			if ($hashlist{"$hash.$suffix"} eq $fprint) {
169 170 171 172 173 174
				print STDERR "WARNING: Skipping duplicate certificate $fname\n";
				return;
			}
			$suffix++;
		}
		$hash .= ".$suffix";
175
		if ($symlink_exists) {
176
			print "link $fname -> $hash\n" if $verbose;
177
			symlink $fname, $hash || warn "Can't symlink, $!";
178
		} else {
179
			print "copy $fname -> $hash\n" if $verbose;
180 181 182 183 184 185 186 187 188 189 190
                        if (open($in, "<", $fname)) {
                            if (open($out,">", $hash)) {
                                print $out $_ while (<$in>);
                                close $out;
                            } else {
                                warn "can't open $hash for write, $!";
                            }
                            close $in;
                        } else {
                            warn "can't open $fname for read, $!";
                        }
191
		}
192 193 194 195 196 197 198
		$hashlist{$hash} = $fprint;
}

# Same as above except for a CRL. CRL links are of the form <hash>.r<n>

sub link_hash_crl {
		my $fname = $_[0];
199
		$fname =~ s/'/'\\''/g;
200
		my ($hash, $fprint) = `"$openssl" crl $crlhash -fingerprint -noout -in '$fname'`;
201 202 203 204 205 206 207 208
		chomp $hash;
		chomp $fprint;
		$fprint =~ s/^.*=//;
		$fprint =~ tr/://d;
		my $suffix = 0;
		# Search for an unused hash filename
		while(exists $hashlist{"$hash.r$suffix"}) {
			# Hash matches: if fingerprint matches its a duplicate cert
209
			if ($hashlist{"$hash.r$suffix"} eq $fprint) {
210 211 212 213 214 215
				print STDERR "WARNING: Skipping duplicate CRL $fname\n";
				return;
			}
			$suffix++;
		}
		$hash .= ".r$suffix";
216
		if ($symlink_exists) {
217
			print "link $fname -> $hash\n" if $verbose;
218
			symlink $fname, $hash || warn "Can't symlink, $!";
219
		} else {
220
			print "cp $fname -> $hash\n" if $verbose;
221 222
			system ("cp", $fname, $hash);
                        warn "Can't copy, $!" if ($? >> 8) != 0;
223
		}
224 225
		$hashlist{$hash} = $fprint;
}