xref: /freebsd-src/crypto/openssl/util/wrap.pl.in (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! {- $config{HASHBANGPERL} -}
2*e0c4386eSCy Schubert
3*e0c4386eSCy Schubertuse strict;
4*e0c4386eSCy Schubertuse warnings;
5*e0c4386eSCy Schubert
6*e0c4386eSCy Schubertuse File::Basename;
7*e0c4386eSCy Schubertuse File::Spec::Functions;
8*e0c4386eSCy Schubert
9*e0c4386eSCy SchubertBEGIN {
10*e0c4386eSCy Schubert    # This method corresponds exactly to 'use OpenSSL::Util',
11*e0c4386eSCy Schubert    # but allows us to use a platform specific file spec.
12*e0c4386eSCy Schubert    require {-
13*e0c4386eSCy Schubert         use Cwd qw(abs_path);
14*e0c4386eSCy Schubert
15*e0c4386eSCy Schubert         "'" . abs_path(catfile($config{sourcedir},
16*e0c4386eSCy Schubert                                'util', 'perl', 'OpenSSL', 'Util.pm')) . "'";
17*e0c4386eSCy Schubert         -};
18*e0c4386eSCy Schubert    OpenSSL::Util->import();
19*e0c4386eSCy Schubert}
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubertmy $there = canonpath(catdir(dirname($0), updir()));
22*e0c4386eSCy Schubertmy $std_engines = catdir($there, 'engines');
23*e0c4386eSCy Schubertmy $std_providers = catdir($there, 'providers');
24*e0c4386eSCy Schubertmy $std_openssl_conf = catdir($there, 'apps/openssl.cnf');
25*e0c4386eSCy Schubertmy $unix_shlib_wrap = catfile($there, 'util/shlib_wrap.sh');
26*e0c4386eSCy Schubert
27*e0c4386eSCy Schubertif ($ARGV[0] eq '-fips') {
28*e0c4386eSCy Schubert    $std_openssl_conf = {-
29*e0c4386eSCy Schubert         use Cwd qw(abs_path);
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubert         "'" . abs_path(catfile($config{sourcedir}, 'test/fips-and-base.cnf')) . "'";
32*e0c4386eSCy Schubert         -};
33*e0c4386eSCy Schubert    shift;
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert    my $std_openssl_conf_include = catdir($there, 'providers');
36*e0c4386eSCy Schubert    $ENV{OPENSSL_CONF_INCLUDE} = $std_openssl_conf_include
37*e0c4386eSCy Schubert        if ($ENV{OPENSSL_CONF_INCLUDE} // '') eq ''
38*e0c4386eSCy Schubert            && -d $std_openssl_conf_include;
39*e0c4386eSCy Schubert}
40*e0c4386eSCy Schubert
41*e0c4386eSCy Schubert$ENV{OPENSSL_ENGINES} = $std_engines
42*e0c4386eSCy Schubert    if ($ENV{OPENSSL_ENGINES} // '') eq '' && -d $std_engines;
43*e0c4386eSCy Schubert$ENV{OPENSSL_MODULES} = $std_providers
44*e0c4386eSCy Schubert    if ($ENV{OPENSSL_MODULES} // '') eq '' && -d $std_providers;
45*e0c4386eSCy Schubert$ENV{OPENSSL_CONF} = $std_openssl_conf
46*e0c4386eSCy Schubert    if ($ENV{OPENSSL_CONF} // '') eq '' && -f $std_openssl_conf;
47*e0c4386eSCy Schubert
48*e0c4386eSCy Schubertmy $use_system = 0;
49*e0c4386eSCy Schubertmy @cmd;
50*e0c4386eSCy Schubert
51*e0c4386eSCy Schubertif ($^O eq 'VMS') {
52*e0c4386eSCy Schubert    # VMS needs the command to be appropriately quotified
53*e0c4386eSCy Schubert    @cmd = fixup_cmd(@ARGV);
54*e0c4386eSCy Schubert} elsif (-x $unix_shlib_wrap) {
55*e0c4386eSCy Schubert    @cmd = ( $unix_shlib_wrap, @ARGV );
56*e0c4386eSCy Schubert} else {
57*e0c4386eSCy Schubert    # Hope for the best
58*e0c4386eSCy Schubert    @cmd = ( @ARGV );
59*e0c4386eSCy Schubert}
60*e0c4386eSCy Schubert
61*e0c4386eSCy Schubert# The exec() statement on MSWin32 doesn't seem to give back the exit code
62*e0c4386eSCy Schubert# from the call, so we resort to using system() instead.
63*e0c4386eSCy Schubertmy $waitcode = system @cmd;
64*e0c4386eSCy Schubert
65*e0c4386eSCy Schubert# According to documentation, -1 means that system() couldn't run the command,
66*e0c4386eSCy Schubert# otherwise, the value is similar to the Unix wait() status value
67*e0c4386eSCy Schubert# (exitcode << 8 | signalcode)
68*e0c4386eSCy Schubertdie "wrap.pl: Failed to execute '", join(' ', @cmd), "': $!\n"
69*e0c4386eSCy Schubert    if $waitcode == -1;
70*e0c4386eSCy Schubert
71*e0c4386eSCy Schubert# When the subprocess aborted on a signal, we simply raise the same signal.
72*e0c4386eSCy Schubertkill(($? & 255) => $$) if ($? & 255) != 0;
73*e0c4386eSCy Schubert
74*e0c4386eSCy Schubert# If that didn't stop this script, mimic what Unix shells do, by
75*e0c4386eSCy Schubert# converting the signal code to an exit code by setting the high bit.
76*e0c4386eSCy Schubert# This only happens on Unix flavored operating systems, the others don't
77*e0c4386eSCy Schubert# have this sort of signaling to date, and simply leave the low byte zero.
78*e0c4386eSCy Schubertexit(($? & 255) | 128) if ($? & 255) != 0;
79*e0c4386eSCy Schubert
80*e0c4386eSCy Schubert# When not a signal, just shift down the subprocess exit code and use that.
81*e0c4386eSCy Schubertmy $exitcode = $? >> 8;
82*e0c4386eSCy Schubert
83*e0c4386eSCy Schubert# For VMS, perl recommendations is to emulate what the C library exit() does
84*e0c4386eSCy Schubert# for all non-zero exit codes, except we set the error severity rather than
85*e0c4386eSCy Schubert# success.
86*e0c4386eSCy Schubert# Ref: https://perldoc.perl.org/perlport#exit
87*e0c4386eSCy Schubert#      https://perldoc.perl.org/perlvms#$?
88*e0c4386eSCy Schubertif ($^O eq 'VMS' && $exitcode != 0) {
89*e0c4386eSCy Schubert    $exitcode =
90*e0c4386eSCy Schubert        0x35a000                # C facility code
91*e0c4386eSCy Schubert        + ($exitcode * 8)       # shift up to make space for the 3 severity bits
92*e0c4386eSCy Schubert        + 2                     # Severity: E(rror)
93*e0c4386eSCy Schubert        + 0x10000000;           # bit 28 set => the shell stays silent
94*e0c4386eSCy Schubert}
95*e0c4386eSCy Schubertexit($exitcode);
96