#!/usr/bin/python

#:'#######:::'######::'##::::'##:::'#####:::
#'##.... ##:'##... ##: ##:::: ##::'##.. ##::
#..::::: ##: ##:::..:: ##:::: ##:'## #:: ##:
#:'#######:: ##::::::: #########: ##: #: ##:
#:...... ##: ##::::::: ##.... ##: ##:: # ##:
#'##:::: ##: ##::: ##: ##:::: ##:. ##:: ##::
#. #######::. ######:: ##:::: ##::. #####:::
#:.......::::......:::..:::::..::::.....::::
#:'#######:::::'###:::::'######::'########::
#'##.... ##:::'## ##:::'##... ##: ##.....:::
# ##:::: ##::'##:. ##:: ##:::..:: ##::::::::
#: #######::'##:::. ##:. ######:: ######::::
#'##.... ##: #########::..... ##: ##...:::::
# ##:::: ##: ##.... ##:'##::: ##: ##::::::::
#. #######:: ##:::: ##:. ######:: ########::
#:.......:::..:::::..:::......:::........:::


import os
import sys
import getopt
import platform
import subprocess
import mounting_tests
import partition_tests
import fs_tests
import report

#List of tests to perform. These are defined by the CIS hardening guidelines for CentOS Linux 07
FILESYSTEMS = True


def main():                     #Arguments for main set the tests which are to be disbaled
	global FILESYSTEMS

	if len(sys.argv) == 1:
		print("Please enter a command. For a list of available commands and flags, add -help")

	if "-help" in str(sys.argv):
		print("       runtest       initiate a test for the system")
		print("       -dfs          disable filesystem tests")
		print("       -ignm         ignore mitigation advisories")
		print("       -igne         ignore error logging")

	if "-dfs" in str(sys.argv):
		FILESYSTEMS = False

	if "-ignm" in str(sys.argv):
		report.set_mitigation(False)

	if "-igne" in str(sys.argv):
		report.set_error(False)

	if "runtest" in str(sys.argv):
		init_test()


def init_test():                 #The main testing phase

	subprocess.check_output(('clear'))
	
	global FILESYSTEMS           #Relocalization of Global Variables

	FSTESTS = 0.0                #Total number of filesystem tests
	FSPASS = 0.0                 #Total number of tests passed
	
	if "redhat" not in platform.version():
		report.report("\nWARNING:")
		report.report("-----------------------------------------------------------------------------------------")
		report.report("These tests and mitigation strategies are designed to be run and implemented on CentOS 7.")
		report.report("Using this diagnostic on another system may give some information about general security")
		report.report("status, but this information will by no means be sufficient for any form of information")
		report.report("gathering if used by itself.")
		report.report("-----------------------------------------------------------------------------------------\n")

	if FILESYSTEMS == True:

		#Tests for mountable filesystems
		#-------------------------------

		cramfsResult = mounting_tests.run("cramfs")
		if (cramfsResult.error == None):
			FSTESTS += cramfsResult.totalpoints
			FSPASS += cramfsResult.points
		else:
			report.error(cramfsResult.errorstatus)
		report.report ("\n")

		freevxfsResult = mounting_tests.run("freevxfs")
		if (freevxfsResult.error == None):
			FSTESTS += freevxfsResult.totalpoints
			FSPASS += freevxfsResult.points
		else:
			report.error(freevxfsResult.errorstatus)
		report.report ("\n")
		
		hfsResult = mounting_tests.run("hfs")
		if (hfsResult.error == None):
			FSTESTS += hfsResult.totalpoints
			FSPASS += hfsResult.points
		else:
			report.error(hfsResult.errorstatus)
		report.report ("\n")

		hfsplusResult = mounting_tests.run("hfsplus")
		if (hfsplusResult.error == None):
			FSTESTS += hfsplusResult.totalpoints
			FSPASS += hfsplusResult.points
		else:
			report.error(hfsplusResult.errorstatus)
		report.report ("\n")

		squashfsResult = mounting_tests.run("squashfs")
		if (squashfsResult.error == None):
			FSTESTS += squashfsResult.totalpoints
			FSPASS += squashfsResult.points
		else:
			report.error(squashfsResult.errorstatus)
		report.report ("\n")

		udfResult = mounting_tests.run("udf")
		if (udfResult.error == None):
			FSTESTS += udfResult.totalpoints
			FSPASS += udfResult.points
		else:
			report.error(udfResult.errorstatus)
		report.report ("\n")
			
		vfatResult = mounting_tests.run("vfat")
		if (vfatResult.error == None):
			FSTESTS += vfatResult.totalpoints
			FSPASS += vfatResult.points
		else:
			report.error(vfatResult.errorstatus)
		report.report ("\n")
		
		
		#Tests for world-writable directories
		#------------------------------------
		tmpResults = partition_tests.run("/tmp", "tmp", True)
		if (tmpResults.error == None):
			FSTESTS += tmpResults.totalpoints
			FSPASS += tmpResults.points
		else:
			report.error(tmpResults.errorstatus)
		report.report ("\n")

		varResults = partition_tests.run("/var", "var", False)
		if (varResults.error == None):
			FSTESTS += varResults.totalpoints
			FSPASS += varResults.points
		else:
			report.error(varResults.errorstatus)
		report.report ("\n")

		varTmpResults = partition_tests.run("/var/tmp", "var/tmp", True)
		if (varTmpResults.error == None):
			FSTESTS += varTmpResults.totalpoints
			FSPASS += varTmpResults.points
		else:
			report.error(varTmpResults.errorstatus)
		report.report ("\n")

		varLogResults = partition_tests.run("/var/log", "var/log", False)
		if (varLogResults.error == None):
			FSTESTS += varLogResults.totalpoints
			FSPASS += varLogResults.points
		else:
			report.error(varLogResults.errorstatus)
		report.report ("\n")

		varLogAuditResults = partition_tests.run("/var/log/audit", "var/log/audit", False)
		if (varLogAuditResults.error == None):
			FSTESTS += varLogAuditResults.totalpoints
			FSPASS += varLogAuditResults.points
		else:
			report.error(varLogAuditResults.errorstatus)
		report.report ("\n")

		homeResults = partition_tests.run("/home", "home", True)
		if (homeResults.error == None):
			FSTESTS += homeResults.totalpoints
			FSPASS += homeResults.points
		else:
			report.error(homeResults.errorstatus)
		report.report ("\n")

	if FSTESTS != 0:
		FSGRADE = FSPASS/FSTESTS * 100
		print ("Filesystem overall score = {}".format(FSGRADE))
		print ("Out of {} tests run, {} were passed.".format(FSTESTS,FSPASS))
	else:
		report.report("No tests were run successfully. Are you running on the correct operating system?")
	



if __name__ == "__main__":
	main()
