#!/usr/bin/perl
#--------------------
# PSMT 2.3.13
# 2013.03.01 update
# 2013.08.07 /tmpȤʤ
# 2023.05.22 wgetޥɥѥ򥳥եǽ
# 2024.12.16 Sys::Hostname⥸塼Ȥʤˡѹ
#--------------------
# poststatus
#
use Socket;

$manager = $ARGV[0];

# ѥ᡼
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 = "scanchk.cgi";

$temp_dir ="/var/opt/proscan/tmp";
$scanresult = "$temp_dir/scan.log";
$dummy_file = "/dev/null";
$agent_file = "/opt/proscan/agent/data/agent.csv";
$group_file = "/opt/proscan/agent/data/group";

#--------------------------------------------------------
# ʹߤϽʤǤ
#
# ʲϷ
#
# ProScan File scanner Ver.6.0.6.0 starting...
# All Rights Reserved,Copyright(C) 2003-2012 Promark Inc.
# ProScan version -----------------------------
#  version        : 6.0.6.0
#  engine version : 0.98
#  VDF version    : 17985
# File scan setting ---------------------------
#  log_level      : 7
#  directory scan : yes
#  symlink scan   : yes
#  scan level     : 7
#  action         : none
#  save directory : /var/opt/proscan/save
#  show level     : 5
#  report address :
#  output         :
#  exclude mask   : /dev/:/sys/:/lib/udev
#  include mask   :
# File scan start -----------------------------
# Current working directory : /root
# File scan done
# scan results -------------------
#  directories :       23
#        files :       76
#       alerts :        0
#     infected :        0
#    protected :        0
#       delete :        0
#         move :        0
#      exclude :        0
# --------------------------------
#   start time : 23:21:53
#     end time : 23:22:09
#    scan time : 00:00:16
# --------------------------------
# File scanning exit

$url = "http://$manager:$port/cgi-bin/$cgi";
$wget_opt = "-q -O $dummy_file --post-file=$scanresult";

open(LOG,"$scanresult");
while(<LOG>) {
    chomp;
    ($pro_version) = /^ version\s*: (\d+\.\d+\.\d+\.\d+)/ if /^ version/;
    ($eng_version1) = /engine version\s* : (\d+)/ if /engine version/;
    ($eng_version2) = /engine version\s* : \d+\.(\d+)/ if /engine version/;
    ($eng_version3) = /engine version\s* : \d+\.\d+\.(\d+)/ if /engine version/;
    ($vdf_version) = /VDF version\s*: (\d+)/ if /VDF version/;
    ($status) = /alerts :\s*(\d+)/ if /alerts :/;
    ($start) = /start time : (.*)/ if /start time :/;
    ($end) = /end time : (.*)/ if /end time :/;
    ($scantime) = /scan time : (.*)/ if /scan time :/;
    $result = "ok" if /scan result/;
}
close(LOG);

$eng_version = "$eng_version1.$eng_version2";
$eng_version .= ".$eng_version3" if $eng_version3;

if ( $result ne "ok" ) {
    $status = "ERR";
}

if ( $start > $end ) {
    @T = localtime(time - 86400);
} else {
    @T = localtime;
}
$date = sprintf("%04d/%02d/%02d",$T[5]+1900,$T[4]+1,$T[3]);
$time = "${date} ${start}\-\>${end}($scantime)";

open(LOG,">>$scanresult");
print LOG "STATUS=$status,$time\n";
close(LOG);

#print "$wget $wget_opt '$url'\n";
system("$wget $wget_opt '$url'");

# data file read
if ( -f $agent_file ) {
    open(DATA,"$agent_file");
    $data = <DATA>;
    close(DATA);
    chomp($data);
} else {
    $hostname = `hostname`;
    chomp($hostname);
    ($host,$alias,$atype,$len,@addrs) = gethostbyname($hostname);
    $ip = sprintf("%d.%d.%d.%d",unpack("CCCC",$addrs[0]));
    open(GRP,$group_file);
    $grp = <GRP>;
    chomp($grp);
    close(GRP);
    ($group,$type) = split(/:/,$grp);
    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 = "$hostname,$ip,$group,$type,,$pro_version,$eng_version,$vdf_version,,,$status,$time,$schedule";
}
@D = split(/,/,$data);

open(DATA,">$agent_file");
chmod(DATA,0600);

$D[10] = $status;
$D[11] = $time;

print DATA join(",",@D);

close(DATA);

exit(0);

