#!/bin/bash2


RUNDIR="/var/phion/run/boxadm/"
CONFDIR="/var/phion/preserve/boxadm/"
[ ! -d ${RUNDIR} ] && mkdir -p ${RUNDIR}
[ ! -d ${CONFDIR} ] && mkdir -p ${CONFDIR}




if [ -f /etc/phion/phion.sh ]; then
        . /etc/phion/phion.sh
fi

if [ -f /etc/phion/options ]; then
        . /etc/phion/options
fi

if [ -f /etc/phion/functions ]; then
        . /etc/phion/functions
fi



[ _$H = _ ] && eval H=0

let H++


PREFIX=""

for ((k=1 ; H-k ; k++)); do
    PREFIX="${PREFIX} $(SUB)"
done

let H++
export H=$H


ARG0="${PREFIX} $(basename $0)"
ARGS=""



ACLLEN=32
ACLMAX=$((ACLLEN*2))

PMODPATH="/opt/phion/modules/box/boxadm"
[ -f ${PMODPATH}/moddefs.conf ] && source ${PMODPATH}/moddefs.conf  

MYLOGFLE="${logfile_activate:-"box_System_messages"}"

#MYLOGFILE=box_Settings

logerror="LogError $MYLOGFLE"
EVENTAPI="/etc/phion/bin/events  --layer_id=1 --layer_name=\"network layer\""

if [ -x /etc/phion/bin/iplist ]; then
	#ACLS=$(/etc/phion/bin/iplist -b -acl)
	ACLS=$(/etc/phion/bin/iplist -acl)
fi

ACPFCTRL="/opt/phion/bin/acpfctrl"



$logerror "Info" "[0051076] ${ARG0}(${ARGS}): ----------------- START -----------------------"
$logerror "Info" "[0050100] ${ARG0}(${ARGS}): analyzing ACL protection settings"


if [ ! -x ${ACPFCTRL} ]; then
	$logerror "Error" "[0051078] ${ARG0}(${ARGS})->1: missing utility $(MARK ${ACPFCTRL}), terminating"  
	eval $EVENTAPI --class_id=3 --class_name=writeACL --type_id=110 --type_name="${ACPFCTRL}" --data=\"Missing utility ${ACPFCTRL}, ACL protection not activated.\"
	$logerror "Warning" "[0051083] ${ARG0}(${ARGS}): ACL protection NOT activated"
	$logerror "Info" "[0051084] ${ARG0}(${ARGS}): ------------------ END ------------------------"
	exit 1
fi


BAKPOL=0
if [ "_$ACLS" = "_" ]; then
	$logerror "Security" "[0051080] ${ARG0}(${ARGS}): empty ACL encountered"
	eval $EVENTAPI --class_id=3 --class_name=writeACL --type_id=4300 --type_name=\"Empty ACL Encountered\" --data=\"An empty ACL represents an unnecessary security risk.\"
	#ACLLIST="0\t0"
	ACLS="0.0.0.0/32"
	BAKPOL=1
	$logerror "Warning" "[0051083] ${ARG0}(${ARGS}): ACL protection is ineffective (ACL=0.0.0.0/32)"
fi

trap '' INT HUP TERM PIPE

TMPFLE=${RUNDIR}/dynACL.conf.$$.tmp


{
echo "ACLLIST[]=${ACLS}" 
echo "BAKPOL=${BAKPOL}" 
} > ${TMPFLE}	

if diff --brief ${TMPFLE} /opt/phion/config/active/dynACL.conf &>/dev/null; then
	rm -f ${TMPFLE}
else
	mv -f ${TMPFLE} /opt/phion/config/active/dynACL.conf
	$logerror "Notice" "[0051084] ${ARG0}(${ARGS}): ACL scope has changed"
fi


trap - INT HUP TERM PIPE


[ $BAKPOL != 1 ] && $logerror "Info" "[0051083] ${ARG0}(${ARGS}): ACL protection activated"

 

$logerror "Info" "[0051084] ${ARG0}(${ARGS}): ------------------ END ------------------------"

exit 0


