#!/bin/sh
#
#    Copyright (c) 2007 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#
#		firmwarecheck CLI implementation.	
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin
STS_OK=0
STS_ERR=255

echo -e "Validating integrity of firmware on root filesystem"
echo -e "Please wait..."

VIOLATIONS=`rpm -Va --nodeps | grep "^.\{2\}5.\{6\} .\+$\|missing" | grep -v "^.\{11\}\(\/var\).\+$" | grep -v "/fabos/share/release\|__init__.py\|config.php\|/usr/confd/lib/confd/priv/yaws/lib/.gitignore\|/boot/silkworm_hct_2part.dtb\|/fabos/man/cat7/rasman.tar\|/usr/confd/erlang/econfd/vsn.mk"`
    #
    # exclude /etc files from validations with the exception for the files /etc/profile,
    # /etc/fstab and files under /etc/rc.d/init.d/
    #
    EXCLUSIONS=`echo -e "$VIOLATIONS" | grep -v "^.\{11\}\(\/etc\|\/dev\).\+$"`
    EXCEPTIONS=`echo -e "$VIOLATIONS" | grep "^.\{11\}\(\/etc\/fstab\|\/etc\/rc.d\/init.d\|\/etc\/fabos\/profile\|\/etc\/profile\|\/etc\/rc.d\/rc\|\/etc\/fabos\/Dcmd\/vcs_set_mode.sh\|\/etc\/confd\/confd_types.so\)"`
    if [ "$EXCLUSIONS" != "" -o "$EXCEPTIONS" != "" ]; then
        if [ "$EXCLUSIONS" != "" ]; then
            echo -e "$EXCLUSIONS"
        fi
        if [ "$EXCEPTIONS" != "" ]; then
            echo -e "$EXCEPTIONS"
        fi
		echo -e "Firmware integrity check failed."
		exit $STS_ERR
    fi

echo -e "Firmware integrity test passed."	
exit $STS_OK
