#!/usr/bin/perl
#--------------------
# PSMT 2.0
# 2013.03.01 update
# 2016.12.03 AIX
# 2018.09.03 LIBPATH
# 2023.05.22 wgetޥɥѥ򥳥եǽ
#
use Socket;

# for AIX
#$libpath = $ENV{'LIBPATH'};
#$ENV{'LIBPATH'} = "/usr/lib/clamav:$libpath";

$manager = $ARGV[0];
$update = $ARGV[1];

# ѥ᡼
open(CONFIG,"/opt/proscan/agent/conf/psmt_agent.conf");
while(<CONFIG>) {
    chomp;
    next if /^#/ || /^$/;
    ($name,$value) = split(/=/,$_);
    eval("\$$name = $value");
}
close(CONFIG);

# wgetޥɤΥѥ
$wget = "/usr/bin/wget";
$wget = $WGET if $WGET;

# ФΥɥ쥹
$manager = "$MANAGER" unless $manager;
$port = "$PORT";

# ФΥСCGI̾
$cgi = "verchk.cgi";

# ProScan⥸塼ΥСǧޥ
$clamav = "LIBPATH=/usr/lib/clamav /usr/lib/clamav/clamd --version";
$licinfo = "/opt/proscan/bin/licenseviewer -s";
$proscan = "/opt/proscan/bin/proscan -v";

# logfile
$log_file = "/var/opt/proscan/log/psmt_agent_script.log";
$log_file = $LOGFILE if $LOGFILE;


#--------------------------------------------------------
# ʹߤϽʤǤ
#
open(LOG,">>$log_file");
print LOG `date`;

$url = "http://$manager:$port/cgi-bin/$cgi";
$wget_opt = "-q -O /tmp/dummy";
$host = `hostname`;
chomp($host);
print LOG "HOST=$host,";

$addr = inet_aton($host);
$ip = inet_ntoa($addr);
print LOG "IP=$ip\n";

open(VER,"$clamav|");
$clamd_version = <VER>;
($eng_version,$vdf_version) = $clamd_version =~ /ClamAV ([\d\.]+)\/(\d+)\//;
close(VER);
print LOG "$clamd_version";

open(LIC,"$licinfo|");
while(<LIC>) {
    if ( /Expire date/ ) {
        ($expire_y,$expire_m,$expire_d) = /Expire date       = (\d+)\/(\d+)\/(\d+) /;
        last;
    }
}
close(LIC);

$expire = sprintf("%04d/%02d/%02d",$expire_y,$expire_m,$expire_d);

open(VER,"$proscan|");
while(<VER>) {
    chomp;
    ($pro_version) = /Ver.([\d\.]*)\s*Build/ if /Ver/;
}
close(VER);

open(GID,"/opt/proscan/agent/data/group");
$_ = <GID>;
chomp;
($group_id,$type) = split(/:/,$_);
close(GID);

$ENV{'LIBPATH'} = "$libpath";

print LOG "$wget $wget_opt --post-data='p=$pro_version&e=$eng_version&v=$vdf_version&d=$expire&u=$update' $url\n";
`$wget $wget_opt --post-data='p=$pro_version&e=$eng_version&v=$vdf_version&d=$expire&u=$update' $url`;

open(RES,"/opt/proscan/agent/data/scanresult");
$_ = <RES>;
chomp;
($result,$scantime) = split(/,/,$_);
close(RES);

if ( -f "/etc/cron.d/psmt_agent" ) {
    open(CRON,"/etc/cron.d/psmt_agent");
    $_ = <CRON>;
    chomp;
    if ( $_ ) {
	@UC = split(/ /,$_);
    	$upd_schedule = "$UC[0] $UC[1] $UC[2] $UC[3] $UC[4]";
    }
    $_ = <CRON>;
    chomp;
    if ( $_ ) {
	@SC = split(/ /,$_);
    	$scan_schedule = "$SC[0] $SC[1] $SC[2] $SC[3] $SC[4]";
	$scan_path = $SC[7];
	$scan_path =~ s/"//g;
    }
    close(CRON);
    $schedule = "$upd_schedule:$scan_schedule:$scan_path";
} elsif ( -f "/var/spool/cron/crontabs/root" ) {
    open(CRON,"/var/spool/cron/crontabs/root");
    while(<CRON>) {
	chomp;
	if ( /update/ ) {
	    @UC = split(/ /,$_);
	} elsif ( /scan/ ) {
	    @SC = split(/ /,$_);
	}
    }
    close(CRON);
    $upd_schedule = "$UC[0] $UC[1] $UC[2] $UC[3] $UC[4]";
    $scan_schedule = "$SC[0] $SC[1] $SC[2] $SC[3] $SC[4]";
    $scan_path = $SC[7];
    $scan_path =~ s/"//g;
    $schedule = "$upd_schedule:$scan_schedule:$scan_path";
}

# data file output
open(DATA,">/opt/proscan/agent/data/agent.csv");
chmod(DATA,0600);
print DATA qq($host,$ip,$group_id,$type,$status,$pro_version,$eng_version,$vdf_version,$update,$expire,$result,$scantime,$schedule);
close(DATA);

