xref: /src/crypto/openssl/util/wrap.pl.in (revision 10a428653ee7216475f1ddce3fb4cbf1200319f8)
1#! {- $config{HASHBANGPERL} -}
2
3use strict;
4use warnings;
5
6use File::Basename;
7use File::Spec::Functions;
8
9BEGIN {
10    # This method corresponds exactly to 'use OpenSSL::Util',
11    # but allows us to use a platform specific file spec.
12    require {-
13         use Cwd qw(abs_path);
14
15         "'" . abs_path(catfile($config{sourcedir},
16                                'util', 'perl', 'OpenSSL', 'Util.pm')) . "'";
17         -};
18    OpenSSL::Util->import();
19}
20
21sub quote_cmd_win32 {
22    my $cmd = "";
23
24    foreach my $arg (@_) {
25        if ($arg =~ m{\A[\w,-./@]+\z}) {
26            $cmd .= $arg . q{ };;
27        } else {
28            $cmd .= q{"} . quote_arg_win32($arg) . q{" };
29        }
30    }
31    return substr($cmd, 0, -1);
32}
33
34sub quote_arg_win32 {
35    my ($arg) = @_;
36    my $val = "";
37
38    pos($arg) = 0;
39    while (1) {
40        return $val if (pos($arg) == length($arg));
41        if ($arg =~ m{\G((?:(?>[\\]*)[^"\\]+)+)}ogc) {
42            $val .= $1;
43        } elsif ($arg =~ m{\G"}ogc) {
44            $val .= qq{\\"};
45        } elsif ($arg =~ m{\G((?>[\\]+)(?="|\z))}ogc) {
46            $val .= qq{\\} x (2 * length($1));
47        } else {
48            die sprintf("Internal error quoting: '%s'\n", $arg);
49        }
50    }
51}
52
53my $there = canonpath(catdir(dirname($0), updir()));
54my $std_engines = catdir($there, 'engines');
55my $std_providers = catdir($there, 'providers');
56my $std_openssl_conf = catdir($there, 'apps/openssl.cnf');
57my $unix_shlib_wrap = catfile($there, 'util/shlib_wrap.sh');
58my $std_openssl_conf_include;
59
60if ($ARGV[0] eq '-fips') {
61    $std_openssl_conf = {-
62         use Cwd qw(abs_path);
63
64         "'" . abs_path(catfile($config{sourcedir}, 'test/fips-and-base.cnf')) . "'";
65         -};
66    shift;
67
68    $std_openssl_conf_include = catdir($there, 'providers');
69}
70
71if ($ARGV[0] eq '-jitter') {
72    $std_openssl_conf = {-
73         use Cwd qw(abs_path);
74
75         "'" . abs_path(catfile($config{sourcedir}, 'test/default-and-jitter.cnf')) . "'";
76         -};
77    shift;
78
79    $std_openssl_conf_include = catdir($there, 'providers');
80}
81
82local $ENV{OPENSSL_RUNNING_UNIT_TESTS} = "yes";
83
84local $ENV{OPENSSL_CONF_INCLUDE} = $std_openssl_conf_include
85    if defined $std_openssl_conf_include
86       &&($ENV{OPENSSL_CONF_INCLUDE} // '') eq ''
87       && -d $std_openssl_conf_include;
88local $ENV{OPENSSL_ENGINES} = $std_engines
89    if ($ENV{OPENSSL_ENGINES} // '') eq '' && -d $std_engines;
90local $ENV{OPENSSL_MODULES} = $std_providers
91    if ($ENV{OPENSSL_MODULES} // '') eq '' && -d $std_providers;
92local $ENV{OPENSSL_CONF} = $std_openssl_conf
93    if ($ENV{OPENSSL_CONF} // '') eq '' && -f $std_openssl_conf;
94{-
95     # For VMS, we define logical names to get the libraries properly
96     # defined.
97     use File::Spec::Functions qw(rel2abs);
98
99     if ($^O eq "VMS") {
100         my $bldtop = rel2abs($config{builddir});
101         my %names =
102             map { platform->sharedname($_) => $bldtop.platform->sharedlib($_) }
103             grep { !$unified_info{attributes}->{libraries}->{$_}->{noinst} }
104             @{$unified_info{libraries}};
105
106         foreach (sort keys %names) {
107             $OUT .= "local \$ENV\{'$_'\} = '$names{$_}';\n";
108         }
109     }
110-}
111my $use_system = 0;
112my @cmd;
113
114if ($^O eq 'VMS') {
115    # VMS needs the command to be appropriately quotified
116    @cmd = fixup_cmd(@ARGV);
117} elsif (-x $unix_shlib_wrap) {
118    @cmd = ( $unix_shlib_wrap, @ARGV );
119} else {
120    # Hope for the best
121    @cmd = ( @ARGV );
122}
123
124# The exec() statement on MSWin32 doesn't seem to give back the exit code
125# from the call, so we resort to using system() instead.
126my $waitcode;
127if ($^O eq 'MSWin32') {
128    $waitcode = system(quote_cmd_win32(@cmd));
129} else {
130    $waitcode = system @cmd;
131}
132
133# According to documentation, -1 means that system() couldn't run the command,
134# otherwise, the value is similar to the Unix wait() status value
135# (exitcode << 8 | signalcode)
136die "wrap.pl: Failed to execute '", join(' ', @cmd), "': $!\n"
137    if $waitcode == -1;
138
139# When the subprocess aborted on a signal, we simply raise the same signal.
140kill(($? & 255) => $$) if ($? & 255) != 0;
141
142# If that didn't stop this script, mimic what Unix shells do, by
143# converting the signal code to an exit code by setting the high bit.
144# This only happens on Unix flavored operating systems, the others don't
145# have this sort of signaling to date, and simply leave the low byte zero.
146exit(($? & 255) | 128) if ($? & 255) != 0;
147
148# When not a signal, just shift down the subprocess exit code and use that.
149my $exitcode = $? >> 8;
150
151# For VMS, perl recommendations is to emulate what the C library exit() does
152# for all non-zero exit codes, except we set the error severity rather than
153# success.
154# Ref: https://perldoc.perl.org/perlport#exit
155#      https://perldoc.perl.org/perlvms#$?
156if ($^O eq 'VMS' && $exitcode != 0) {
157    $exitcode =
158        0x35a000                # C facility code
159        + ($exitcode * 8)       # shift up to make space for the 3 severity bits
160        + 2                     # Severity: E(rror)
161        + 0x10000000;           # bit 28 set => the shell stays silent
162}
163exit($exitcode);
164