File: //etc/upcp_control
#!/usr/bin/env perl
use warnings;
use Data::Dumper;
use LWP::UserAgent;
use Sys::Hostname;
use Socket;
use JSON;
use File::Copy qw/mv cp/;
my $host = hostname();
my $md5_url = 'http://upcp-control.unifiedlayer.com/check_md5sum';
my $script_url = 'http://upcp-control.unifiedlayer.com/upcp_control-guest';
my $control_url = 'http://upcp-control.unifiedlayer.com/upcp_check.php';
my $cust_api = 'http://custapi.unifiedlayer.com/servbrand';
my $key = '6vhEggovq1jh20AcXibT12NZh7LnQubPHTSVNQ8JK9TD1ePW8yV0RNLaDP2H6YAhq2B6';
my $version_file = '/usr/local/cpanel/version';
my $control_file = '/etc/upcp_control';
my $brand_file = '/etc/.brand';
my $brand = 'not_set';
my $now = time();
my $logfile_path = '/var/cpanel/updatelogs/maintenance' . $now . '.log';
my $ua = LWP::UserAgent->new( timeout => 10 );
# Check for any updates to the script
my $rpm_out = `rpm -q eig-upcp_control`;
chomp($rpm_out);
if($rpm_out =~ /package eig-upcp_control is not installed/){
if(-e '/etc/yum.repos.d/dedi.repo'){
# Install package if we can
system('yum --disablerepo="*" --enablerepo=hgdedi clean all');
system('yum -y --disablerepo="*" --enablerepo=hgdedi install eig-upcp_control');
system("echo '$control_file' | at now + 1 minute");
exit;
} else {
# Legacy self-updating if no package but cannot install
chomp( my $md5_latest = $ua->get($md5_url)->content );
open( my $CMD, '-|', '/usr/bin/md5sum', "$control_file" )
or warn "failed";
chomp( my $status = <$CMD> );
close $CMD;
my ( $md5, undef ) = split( ' ', $status );
if ( $md5 ne $md5_latest ) {
print "needs update\n";
if ( $ua->get( ( "$script_url", ':content_file' => $control_file . '_new' ) )->is_success ) {
open( my $CMD, '-|', '/usr/bin/md5sum', $control_file . '_new' )
or warn "failed";
chomp( my $status = <$CMD> );
close $CMD;
my ( $md5_new, undef ) = split( ' ', $status );
if ( $md5_new eq $md5_latest ) {
mv( $control_file . '_new', $control_file );
chmod 0700, $control_file;
$md5 = $md5_latest;
system("echo '$control_file' | at now + 1 minute");
exit;
}
}
}
}
}
# Get current version
open( my $fh, '<', $version_file )
or warn "Could not open file '$version_file' $!";
chomp( my $cp_version = <$fh> );
close $fh;
my $major_version = ($cp_version =~ /^(\d+\.\d+)/) ? $1 : 0;
# Get Brand details
if ( !-s $brand_file ) {
if ( !$ua->get( ( "$cust_api", ':content_file' => $brand_file ) )->is_success ) {
if ( !$ua->get( ( "$cust_api", ':content_file' => $brand_file ) )->is_success ) {
open( $fh, '>', $brand_file )
or warn "Could not open file '$brand_file' $!";
print( $fh $brand );
close $fh;
}
}
}
open( $fh, '<', $brand_file )
or warn "Could not open file '$brand_file' $!";
chomp( $brand = <$fh> );
close $fh;
# Get OS details
my $os_major = qx(/bin/rpm --eval='%centos_ver');
my $response = decode_json($ua->get("$control_url?key=$key&version=$cp_version&brand=$brand&os_major=$os_major&format=json")->content);
if ( $response->{'update'} == 1 ) {
print "updates\n";
open (my $fh, '<', '/etc/cpupdate.conf');
my $cpupdate_minor;
while (my $line = <$fh>){
next unless $line =~ m/^CPANEL=(.*)$/;
(undef, $cpupdate_minor) = split /\./, $1;
last;
}
my @mv = split /\./, $major_version;
my @tv = split /\./, $response->{'target_version'};
# Version lock centos 6 at cpanel 11.102
$tv[1] = 102 if ($tv[1] > 102 && $os_major <= 6);
if ($mv[0] < $tv[0] or ($mv[0] == $tv[0] && $mv[1] < $tv[1])){
print "Updating target version in cpupdate.conf to $response->{'target_version'}\n";
system("/usr/bin/env perl -pi -e 's/^CPANEL=.*/CPANEL=$response->{'target_version'}/' /etc/cpupdate.conf");
#Reinstall or remove theme in postupcp on an upgrade
system("rpm -q --quiet cpanel_${brand}_theme");
if ($? >> 8 == 0){
if ( ! -f "/scripts/postupcp"){
system("touch /scripts/postupcp");
system("chmod 755 /scripts/postupcp");
}
cp("/scripts/postupcp", "/scripts/postupcp.upcp_control") if (! -f "/scripts/postupcp.upcp_control");
if ($tv[1] >= 110){
open (my $ph, '>>', '/scripts/postupcp');
print $ph "yum erase -y cpanel_${brand}_theme\n";
my $postupcp .= << 'END';
sed -i 's/paper_lantern/jupiter/g' /etc/wwwacct.conf
sed -i 's/^CPMOD=.*$/CPMOD=jupiter/' /var/cpanel/packages/*
for user in $(/bin/ls /var/cpanel/users)
do sed -i 's/^RS=.*$/RS=jupiter/' /var/cpanel/users/$user
done
END
print $ph $postupcp;
} else {
system("echo 'yum reinstall -y cpanel_${brand}_theme' >> /scripts/postupcp");
}
}
} elsif ($cpupdate_minor && $cpupdate_minor < $mv[1]){
print "Fixing cpupdate version to be current version rather then a lower one\n";
system("/usr/bin/env perl -pi -e 's/^CPANEL=.*/CPANEL=$mv[0].$mv[1]/' /etc/cpupdate.conf");
}
if ($os_major == 6){
cp("/etc/centos-release", "/etc/centos-release.upcp_control") if (! -f "/etc/centos-release.upcp_control");
open ($fh, '>', "/etc/centos-release");
print $fh "CloudLinux release 6.10 (Final)\n";
close $fh;
my $sysinfo_file = "/var/cpanel/sysinfo.config";
if ( -f $sysinfo_file){
cp($sysinfo_file, "$sysinfo_file.upcp_control") if (! -f "$sysinfo_file.upcp_control");
system("/usr/bin/env perl -pi -e 's/^lock=.*/lock=rpm_dist/' $sysinfo_file");
system("/usr/bin/env perl -pi -e 's/^rpm_dist=.*/rpm_dist=cloudlinux/' $sysinfo_file");
}
}
system('/usr/local/cpanel/scripts/upcp --cron');
mv("/scripts/postupcp.upcp_control", "/scripts/postupcp") if (-f "/scripts/postupcp.upcp_control");
}
elsif ( $response->{'update'} == 2 ) {
print "$response->{'error'}\n";
unlink $brand_file if ($response->{'error'} =~ /^Invalid Brand/);
system("/usr/local/cpanel/scripts/maintenance --log=$logfile_path");
if ( -x '/usr/local/cpanel/scripts/postupcp' ) {
system ("/usr/local/cpanel/scripts/postupcp | tee -a $logfile_path");
}
}
else {
print "maintenance\n";
system("/usr/local/cpanel/scripts/maintenance --log=$logfile_path");
if ( -x '/usr/local/cpanel/scripts/postupcp' ) {
system ("/usr/local/cpanel/scripts/postupcp | tee -a $logfile_path");
}
}
check_cron();
sub check_cron {
my @cron_data;
my $match;
my $crontab = '/var/spool/cron/root';
open( my $fh, '<', $crontab );
while ( my $cron_line = <$fh> ) {
chomp $cron_line;
if ( $cron_line =~ m|^((([\d,/\*]+\s+){4})[\d,/\*]+)(.*)/usr/local/cpanel/scripts/upcp --cron(.*)$| && $cron_line !~ /^\s*#/ ) {
my $run_time = "$2*";
my $head = $4;
my $tail = $5;
push( @cron_data, "#$cron_line" );
push( @cron_data, "$run_time$head$control_file$tail" );
$match++;
} elsif ( $cron_line =~ m|^((([\d,/\*]+\s+){4})[\d,/]+)(.*)$control_file(.*)$| && $cron_line !~ /^\s*#/ ) {
#This is to fix any crons erroneously set to run every week instead of day
next if $match;
my $run_time = "$2*";
my $head = $4;
my $tail = $5;
push( @cron_data, "$run_time$head$control_file$tail" );
$match++;
} elsif ( $cron_line =~ /$control_file/ ) {
next;
} else {
push( @cron_data, $cron_line );
}
}
close $fh;
if ($match) {
print "match\n";
cp( $crontab, "$crontab-" . time );
open( my $fh, '>', $crontab );
print( $fh join( "\n", @cron_data ) );
print( $fh "\n" );
close $fh;
}
}