#!/bin/sh
#
#    Copyright (c) 2018 Brocade LLC.
#    All rights reserved.
#
#    Description:
#
#		firmwarecheck CLI implementation.	
#

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

PLATFORM=`sin | \
		  /bin/grep Platform | \
	      /usr/bin/cut -d" " -f2 | \
		  /usr/bin/cut -d"," -f1`

MACHINE=`uname -m`			
echo -e "Validating integrity of firmware on root filesystem"
echo -e "Please wait..."

	VIOLATIONS=`rpm -Va --nodeps | grep "^.\{2\}5.\{6\} .\+$" | grep -v "^.\{13\}\(\/var\).\+$" | grep -v "ld-2.3.6.so\|ldconfig\|/fabos/share/release\|/usr/share/curl"`
    #
    # 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 "^.\{13\}\(\/etc\|\/fabos\/man\/cat7\).\+$" | grep -v "\/lib\/libfipscrypto.so.1.1" | grep -v "\/lib\/modules\/5.4.66_rt38\/modules.dep"`
	EXCEPTIONS=`echo -e "$VIOLATIONS" | grep "^.\{13\}\(\/etc\/profile\|\/fabos\/man\/cat7\/|\/etc\/fstab\|\/etc\/rc.d\/init.d\|\/etc\/fabos\/profile\|\/etc\/rc.d\/rc\)"`



    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
