Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
c_rehash255 linesDownload Raw Back to bin
1#!/usr/bin/env perl2 3# WARNING: do not edit!4# Generated by Makefile from tools/c_rehash.in5# Copyright 1999-2025 The OpenSSL Project Authors. All Rights Reserved.6#7# Licensed under the Apache License 2.0 (the "License").  You may not use8# this file except in compliance with the License.  You can obtain a copy9# in the file LICENSE in the source distribution or at10# https://www.openssl.org/source/license.html11 12# Perl c_rehash script, scan all files in a directory13# and add symbolic links to their hash values.14 15my $dir = "/usr/ssl";16my $prefix = "/usr";17 18my $errorcount = 0;19my $openssl = $ENV{OPENSSL} || "openssl";20my $pwd;21my $x509hash = "-subject_hash";22my $crlhash = "-hash";23my $verbose = 0;24my $symlink_exists=eval {symlink("",""); 1};25if ($^O eq "msys") { $symlink_exists=0 };26my $removelinks = 1;27 28##  Parse flags.29while ( $ARGV[0] =~ /^-/ ) {30    my $flag = shift @ARGV;31    last if ( $flag eq '--');32    if ( $flag eq '-old') {33        $x509hash = "-subject_hash_old";34        $crlhash = "-hash_old";35    } elsif ( $flag eq '-h' || $flag eq '-help' ) {36        help();37    } elsif ( $flag eq '-n' ) {38        $removelinks = 0;39    } elsif ( $flag eq '-v' ) {40        $verbose++;41    }42    else {43        print STDERR "Usage error; try -h.\n";44        exit 1;45    }46}47 48sub help {49    print "Usage: c_rehash [-old] [-h] [-help] [-v] [dirs...]\n";50    print "   -old use old-style digest\n";51    print "   -h or -help print this help text\n";52    print "   -v print files removed and linked\n";53    exit 0;54}55 56eval "require Cwd";57if (defined(&Cwd::getcwd)) {58    $pwd=Cwd::getcwd();59} else {60    $pwd=`pwd`;61    chomp($pwd);62}63 64# DOS/Win32 or Unix delimiter?  Prefix our installdir, then search.65my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':';66$ENV{PATH} = "$prefix/bin" . ($ENV{PATH} ? $path_delim . $ENV{PATH} : "");67 68if (!(-f $openssl && -x $openssl)) {69    my $found = 0;70    foreach (split /$path_delim/, $ENV{PATH}) {71        if (-f "$_/$openssl" && -x "$_/$openssl") {72            $found = 1;73            $openssl = "$_/$openssl";74            last;75        }76    }77    if ($found == 0) {78        print STDERR "c_rehash: rehashing skipped ('openssl' program not available)\n";79        exit 0;80    }81}82 83if (@ARGV) {84    @dirlist = @ARGV;85} elsif ($ENV{SSL_CERT_DIR}) {86    @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};87} else {88    $dirlist[0] = "$dir/certs";89}90 91if (-d $dirlist[0]) {92    chdir $dirlist[0];93    $openssl="$pwd/$openssl" if (!(-f $openssl && -x $openssl));94    chdir $pwd;95}96 97foreach (@dirlist) {98    if (-d $_ ) {99        if ( -w $_) {100            hash_dir($_);101        } else {102            print "Skipping $_, can't write\n";103            $errorcount++;104        }105    }106}107exit($errorcount);108 109sub copy_file {110    my ($src_fname, $dst_fname) = @_;111 112    if (open(my $in, "<", $src_fname)) {113        if (open(my $out, ">", $dst_fname)) {114            print $out $_ while (<$in>);115            close $out;116        } else {117            warn "Cannot open $dst_fname for write, $!";118        }119        close $in;120    } else {121        warn "Cannot open $src_fname for read, $!";122    }123}124 125sub hash_dir {126    my $dir = shift;127    my %hashlist;128 129    print "Doing $dir\n";130 131    if (!chdir $dir) {132        print STDERR "WARNING: Cannot chdir to '$dir', $!\n";133        return;134    }135 136    opendir(DIR, ".") || print STDERR "WARNING: Cannot opendir '.', $!\n";137    my @flist = sort readdir(DIR);138    closedir DIR;139    if ( $removelinks ) {140        # Delete any existing symbolic links141        foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {142            if (-l $_) {143                print "unlink $_\n" if $verbose;144                unlink $_ || warn "Can't unlink $_, $!\n";145            }146        }147    }148    FILE: foreach $fname (grep {/\.(pem|crt|cer|crl)$/} @flist) {149        # Check to see if certificates and/or CRLs present.150        my ($cert, $crl) = check_file($fname);151        if (!$cert && !$crl) {152            print STDERR "WARNING: $fname does not contain a certificate or CRL: skipping\n";153            next;154        }155        link_hash_cert($fname) if ($cert);156        link_hash_crl($fname) if ($crl);157    }158 159    chdir $pwd;160}161 162sub check_file {163    my ($is_cert, $is_crl) = (0,0);164    my $fname = $_[0];165 166    open(my $in, "<", $fname);167    while(<$in>) {168        if (/^-----BEGIN (.*)-----/) {169            my $hdr = $1;170            if ($hdr =~ /^(X509 |TRUSTED |)CERTIFICATE$/) {171                $is_cert = 1;172                last if ($is_crl);173            } elsif ($hdr eq "X509 CRL") {174                $is_crl = 1;175                last if ($is_cert);176            }177        }178    }179    close $in;180    return ($is_cert, $is_crl);181}182 183sub compute_hash {184    my $fh;185    if ( $^O eq "VMS" ) {186        # VMS uses the open through shell187        # The file names are safe there and list form is unsupported188        if (!open($fh, "-|", join(' ', @_))) {189            print STDERR "Cannot compute hash on '$fname'\n";190            return;191        }192    } else {193        if (!open($fh, "-|", @_)) {194            print STDERR "Cannot compute hash on '$fname'\n";195            return;196        }197        binmode($fh, ":crlf");198    }199    return (<$fh>, <$fh>);200}201 202# Link a certificate to its subject name hash value, each hash is of203# the form <hash>.<n> where n is an integer. If the hash value already exists204# then we need to up the value of n, unless its a duplicate in which205# case we skip the link. We check for duplicates by comparing the206# certificate fingerprints207 208sub link_hash_cert {209    link_hash($_[0], 'cert');210}211 212# Same as above except for a CRL. CRL links are of the form <hash>.r<n>213 214sub link_hash_crl {215    link_hash($_[0], 'crl');216}217 218sub link_hash {219    my ($fname, $type) = @_;220    my $is_cert = $type eq 'cert';221 222    my ($hash, $fprint) = compute_hash($openssl,223                                       $is_cert ? "x509" : "crl",224                                       $is_cert ? $x509hash : $crlhash,225                                       "-fingerprint", "-noout",226                                       "-in", $fname);227    chomp $hash;228    $hash =~ s/^.*=// if !$is_cert;229    chomp $fprint;230    return if !$hash;231    $fprint =~ s/^.*=//;232    $fprint =~ tr/://d;233    my $suffix = 0;234    # Search for an unused hash filename235    my $crlmark = $is_cert ? "" : "r";236    while(exists $hashlist{"$hash.$crlmark$suffix"}) {237        # Hash matches: if fingerprint matches its a duplicate cert238        if ($hashlist{"$hash.$crlmark$suffix"} eq $fprint) {239            my $what = $is_cert ? 'certificate' : 'CRL';240            print STDERR "WARNING: Skipping duplicate $what $fname\n";241            return;242        }243        $suffix++;244    }245    $hash .= ".$crlmark$suffix";246    if ($symlink_exists) {247        print "link $fname -> $hash\n" if $verbose;248        symlink $fname, $hash || warn "Can't symlink, $!";249    } else {250        print "copy $fname -> $hash\n" if $verbose;251        copy_file($fname, $hash);252    }253    $hashlist{$hash} = $fprint;254}255 
codekingpro/portable-devtools · Team Ai