| Linux sh105.webhostingservices.com 4.19.286-203.ELK.el7.x86_64 #1 SMP Wed Jun 14 04:33:55 CDT 2023 x86_64 Path : /cpanel_installer/ |
| Current File : //cpanel_installer/Common.pm |
package Common;
# cpanel - Common.pm Copyright 2021 cPanel, L.L.C.
# All rights reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
use strict;
use warnings;
use IO::Handle ();
use IO::Select ();
use IPC::Open3 ();
use CpanelLogger; # Must import!
use CpanelGPG ();
use CpanelConfig ();
################################################################################
# Set up output handling code
################################################################################
sub ssystem {
my @cmd = @_;
my $conf_hr = ref( $cmd[-1] ) eq 'HASH' ? pop(@cmd) : {};
die "no command line passed to ssystem!" unless @cmd;
local $CpanelLogger::message_caller_depth = $CpanelLogger::message_caller_depth + 1; # Set caller depth deeper during this sub so debugging it clearer.
DEBUG( '- ssystem [BEGIN]: ' . join( ' ', @cmd ) );
open( my $rnull, '<', '/dev/null' ) or die "Can't open /dev/null: $!";
my $io = IO::Handle->new;
my $pid = IPC::Open3::open3( $rnull, $io, $io, @cmd );
$io->blocking(0);
my $select = IO::Select->new($io);
my $exit_status;
my $buffer = '';
my $buffered_waiting_count = 0;
while ( !defined $exit_status ) {
while ( my $line = readline($io) ) {
# Push the buffer lacking a newline onto the front of this.
if ($buffer) {
$line = $buffer . $line;
$buffer = '';
}
$line =~ s/\r//msg; # Strip ^M from output for better log output.
# Internally buffer on newlines.
if ( $line =~ m/\n$/ms ) {
DEBUG( " " . $line );
$buffered_waiting_count = 0;
}
else {
print "." if ( $buffered_waiting_count++ > 1 );
$buffer = $line;
}
}
# Parse exit status or yield time to the CPU.
if ( waitpid( $pid, 1 ) == $pid ) {
DEBUG( " " . $buffer ) if $buffer;
$exit_status = $? >> 8;
}
else {
# Watch the file handle for output.
$select->can_read(0.01);
}
}
if ($exit_status) {
if ( !$conf_hr->{'ignore_errors'} ) {
ERROR(" - ssystem [EXIT_CODE] '$cmd[0]' exited with $exit_status");
}
else {
DEBUG(" - ssystem [EXIT_CODE] '$cmd[0]' exited with $exit_status (ignored)");
}
}
close($rnull);
$io->close();
DEBUG('- ssystem [END]');
return $exit_status;
}
sub cpfetch {
my ( $url, %opts ) = @_;
if ( !$url ) {
FATAL("The system called the cpfetch process without a URL.");
}
my $file = _get_file( $url, %opts );
return unless defined $file;
if ( $file =~ /\.bz2$/ ) {
ssystem( "/usr/bin/bunzip2", $file );
}
if ( CpanelConfig::signatures_enabled() ) {
$url =~ s/\.bz2$//g;
$file =~ s/\.bz2$//g;
my $sig = _get_file("$url.asc");
CpanelGPG::verify_file( $file, $sig, $url );
}
# the xz file itself is signed only extract it after checking the signature
if ( $file =~ /\.xz$/ ) {
ssystem( "/usr/bin/unxz", $file );
}
return;
}
sub _get_file {
my ( $url, %opts ) = @_;
$url = 'http://' . CpanelConfig::get_update_source() . $url;
# NOTE: assumes no query or fragment in URL
my @FILE = split( /\//, $url );
my $file = pop(@FILE);
if ( -e $file ) {
WARN("Warning: Overwriting the $file file...");
unlink $file;
FATAL("The system could not remove the $file file.") if ( -e $file );
}
DEBUG("Retrieving $url to the $file file...");
my ( $rc, $out ) = wget_url( $url, $file );
if ( !-e $file || -z $file ) {
unlink $file;
if ( $opts{is_optional} ) {
WARN("The system could not fetch the optional $file file: $out");
return;
}
FATAL("The system could not fetch the $file file: $out");
}
return $file;
}
# $file can be '-' to return the output as a scalar, or the path to a file to download the given $url
sub wget_url {
my ( $url, $file ) = @_;
my $max = 3;
foreach my $iter ( 1 .. $max ) {
my ( $wget_bin, $wget_args ) = _get_download_tool_binary();
FATAL("No wget binary defined at this stage.") unless $wget_bin;
unlink $file;
my $output = `$wget_bin $wget_args '$file' $url 2>&1`; ## no critic(ProhibitQxAndBackticks)
return ( 1, $output ) if $? == 0;
WARN("Call to URL '$url' failed, attempt [$iter/$max]");
if ( $iter == $max ) {
my $type = substr( $url, -4 ) eq '.asc' ? 'signature' : 'file';
FATAL("Failed to download $type at URL $url: $output");
}
sleep 3;
}
return;
}
sub _get_download_tool_binary {
for my $bin (qw(/bin/wget /usr/bin/wget /usr/local/bin/wget)) {
next if ( !-e $bin );
next if ( !-x _ );
next if ( -z _ );
return ( $bin, '-q --no-dns-cache --tries=20 --timeout=60 --dns-timeout=60 --read-timeout=30 --waitretry=1 --retry-connrefused -O' ) if ( `$bin --version` =~ m/GNU\s+Wget\s+[0-9]+\.[0-9]+/ims ); ## no critic(ProhibitQxAndBackticks)
}
FATAL("The installation process could not find the wget binary. Install it to a standard location.");
return;
}
1;