File: //usr/local/cwaf/scripts/update-client.pl
#!/bin/sh
eval 'if [ -x /usr/local/cpanel/3rdparty/bin/perl ]; then exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}; else exec /usr/bin/perl -x $0 ${1+"$@"}; fi;'
if 0;
#!/usr/bin/perl
#SVN
use strict qw(refs subs);
use warnings;
use IO::Handle;
use File::Path qw(remove_tree);
use File::Find;
use Getopt::Long;
use POSIX;
use LWP::Simple;
BEGIN { require '/etc/cwaf/use_lib.pl' if -f '/etc/cwaf/use_lib.pl'; }
use Comodo::CWAF::Main;
use Comodo::CWAF::ClientAPI;
no warnings 'redefine';
#Init vars
our (%conf,$pr_name);
my ($var,$log_file,$tmp_log,$wpanel_flag,%opts);
my $force = 0;
# LOG file
# undef - STDOUT
# or some path to log file
$log_file = $conf{'log_dir'} . '/' . $conf{'utils_log'};
$tmp_log = $conf{'log_dir'} . '/' . $conf{'updater_log'};
$conf{'pid_dir'} = $conf{'cwaf_path'} . '/run';
######################################## BEGIN ####################################
$| = 1;
# script name for logging
$pr_name = get_name();
# updater not ran by web panel, set flag (default value)
$wpanel_flag = 0;
# set avail arguments
$var = GetOptions(\%opts,'help|h','force|f','version|v','wpanel|w' );
# argument --help, -h
if( $opts{'help'} ) { &do_print_help_message; exit(0); }
# argument --force, -f
if( $opts{'force'} ) { $force = 1; }
# argument --version, -v
elsif( $opts{'version'} ) {
print "Plugin version=".get_client_version()." \n";
print "Last available version=".get_available_version()." \n";
print "Installed for web platform=".get_web_platform()."\n";
exit(0);
}
# argument --cpanel, -c
elsif( $opts{'wpanel'} ) {
eval "use Comodo::CWAF::Platform";
#if can't load Cpanel module, write logs to STDERR
if($@) { open(LOGFILE, ">&STDERR"); }
# if no errors, set wpanel_flag to true.
else {
$wpanel_flag = 1;
# set max debug level & print log to $tmp_log
unlink($tmp_log) if( -e $tmp_log );
open(LOGFILE, ">>$tmp_log");
$conf{'debug'} = 10;
}
}
# if not set any argument
elsif( $var ) {
# open log handler
if(defined($log_file)) {
unless(open(LOGFILE, ">>$log_file")) {
print STDERR "ERROR: can't open file $log_file\n";
}
# redirect errors to /dev/null if debug < 6
# or redirect errors to logfile
if(int($conf{'debug'}) < 6) {
open(DEVNULL, ">/dev/null");
STDERR->fdopen(\*DEVNULL, 'w');
}
else {
STDERR->fdopen(\*LOGFILE, 'w');
}
}
else {open(LOGFILE, ">&STDERR");}
}
LOGFILE->autoflush(1);
do_log("debug is ON, level = $conf{'debug'}", 9);
# check pid file
do_log("create pid file", 10);
$var = create_pid_file();
if($var == 0 || $var > 1) {
do_log("ERROR: can't create pid file", 0) if($var == 0);
do_log("WARN: another process is started ($var)", 0) if($var > 1);
&do_exit(0);
}
&do_update();
##################################### FUNCTIONS ###################################
sub do_update() {
my ($run_str, $bckfile);
my $version = &do_check_version();
unless($force) {
if($version) {
do_log("Available new client version $version",10);
do_console_log("Available new client version $version");
} else {
do_log("current version is up to date",10);
do_console_log("current version is up to date");
&do_exit(0);
}
} else {
do_log("force update",10);
do_console_log("force update");
}
# create install dir
my $install_dir = $conf{'cwaf_path'}."/tmp/install";
mkdir $install_dir unless -d $install_dir;
my $file_path = $install_dir."/cwaf_client_install.sh";
# perform update
my $code = get_http_file($conf{'script_url'}, $file_path);
if($code != 200) {
do_log('ERROR: download failed, return code (' . $code . '), exit', 0);
&remove_directory($install_dir);
&do_exit(1);
}
do_log("script file downloaded OK",10);
do_console_log("script file downloaded OK");
do_log("running install script",10);
if($wpanel_flag) {
# web panel modes
if(is_cpanel()) {
$run_str = "bash ".$file_path." -- --update --mode cpanel"
} elsif(is_plesk()) {
$run_str = "bash ".$file_path." -- --update --mode auto --path=".$conf{'cwaf_path'} ;
} else {
$run_str = "bash ".$file_path." -- --update --mode auto";
}
} else {
$run_str = "bash ".$file_path." -- --update --mode auto --path=".$conf{'cwaf_path'};
}
do_log("Running update command: ".$run_str,10);
do_console_log("Running update command: ".$run_str);
$var = system($run_str);
# get system() return code
if($var) {
do_log("ERROR: can't run install script(error $@)",10);
&remove_directory($install_dir);
&do_exit(1);
}
$bckfile = "/usr/local/apache/conf/modsec2.conf.cwaf_backup";
if(-e $bckfile) {
do_log("Your mod_security config saved in $bckfile",10);
}
&remove_directory($install_dir);
do_log("Update succesful!",10);
do_console_log("Update succesful!");
do_exit(0);
}
# &do_check_version()
# check client version
#
# RETURN: 0 - same version, if all ok - available client version
sub do_check_version() {
my $lver = &get_client_version();
my $rver = &get_available_version();
if($lver eq 0 || $rver eq 0) {
do_log("ERROR: can't get local client version", 0) if($lver eq 0);
do_log("ERROR: can't get available client version", 0) if($rver eq 0);
&do_exit(0);
}
do_log("local client version = $lver", 8);
do_log("remote client version = $rver", 8);
unless(test_version($lver, $rver)) {
return 0;
}
return $rver;
}
# &remove_directory($dirname)
# remove directory tree at dirname
# RETURN:
# 0 - if got error or timeout
# 1 - if all ok
sub remove_directory($) {
my ($dirname) = @_;
if(-d $dirname) {
do_log("remove directory $dirname",10);
remove_tree($dirname,{ keep_root => 0, error => \my $err });
if(@$err) {
do_log("can't delete $dirname directory",10);
return 0;
}
}
return 1;
}
# &help_message()
# print help message
#
# RETURN: none
sub do_print_help_message {
print <<END;
Usage: $0 [arguments]
Arguments:
-h, --help - this help message
-f, --force - force update
-w, --wpanel - web panel update mode
-v, --version - show client version
END
}
# &do_exit($return_code)
# log "exit"-message and exit
#
# RETURN: none
sub do_exit($) {
my ($rcode) = @_;
do_log("update process finished!",1);
do_console_log("update process finished!");
exit($rcode);
}
# &do_console_log($msg)
# log to console if $wpanel_flag not set
#
# RETURN: none
sub do_console_log($) {
my ($msg) = @_;
print("$msg\n") unless($wpanel_flag);
}