#########################Configuration#Section##########################################################################
# If you want to enforce the CCS version choose enforceCCS = "yes"
# This option will uninstall any other CCS version and install the specified version
enforceCCS = "yes"
CCSVersion = "11.6.1.7849"
########################################################################################################################
import os
import ctypes
import re
import time
import socket
import _winreg
import shutil
import ssl
import urllib2
import getpass
import sys
import datetime

i=0
j=0
min = '5' 
datenow = datetime.datetime.now()
datestamp = datenow + datetime.timedelta(minutes = int(min) -1)

# Create 'TimeStampStatus' file and write current time
def WriteTimeStampToTheTimeStampFile(datetowrite):
	Path_for_time_stamp_file = r""
	Os_Path = r"{0}\Program Files (x86)".format(os.environ['systemdrive'])
	if os.path.exists(Os_Path):
		Path_for_time_stamp_file = r"{0}\Program Files (x86)\COMODO\Endpoint Manager\rmmlogs\TimeStampStatus.txt".format(os.environ['systemdrive'])
	else:
		Path_for_time_stamp_file = r"{0}\Program Files\COMODO\Endpoint Manager\rmmlogs\TimeStampStatus.txt".format(os.environ['systemdrive'])
	open(Path_for_time_stamp_file, "w+").write(str(datetowrite.strftime("%y-%m-%d %H:%M:%S")))

# read 'TimeStampStatus' file
def readTimeStampStatus():
	Path_for_time_stamp_file = r""
	Os_Path = r"{0}\Program Files (x86)".format(os.environ['systemdrive'])
	if os.path.exists(Os_Path):
		Path_for_time_stamp_file = r"{0}\Program Files (x86)\COMODO\Endpoint Manager\rmmlogs\TimeStampStatus.txt".format(os.environ['systemdrive'])
	else:
		Path_for_time_stamp_file = r"{0}\Program Files\COMODO\Endpoint Manager\rmmlogs\TimeStampStatus.txt".format(os.environ['systemdrive'])
	vartime = open(Path_for_time_stamp_file, "r")
	time = vartime.read()
	return time
def verifyfileexist():
	Path_for_time_stamp_file = ""
	Os_Path = r"{0}\Program Files (x86)".format(os.environ['systemdrive'])
	if os.path.exists(Os_Path):
		Path_for_time_stamp_file = r"{0}\Program Files (x86)\COMODO\Endpoint Manager\rmmlogs\TimeStampStatus.txt".format(os.environ['systemdrive'])
	else:
		Path_for_time_stamp_file = r"{0}\Program Files\COMODO\Endpoint Manager\rmmlogs\TimeStampStatus.txt".format(os.environ['systemdrive'])
	return Path_for_time_stamp_file

# read CCS version
def readCCSVersion():
	Path_for_CCS_Version = r""
	getccsvar = ""
	Os_Path = r"{0}\Program Files\COMODO\COMODO Internet Security".format(os.environ['systemdrive'])
	if os.path.exists(Os_Path):
		Path_for_CCS_Version = r"{0}\Program Files\COMODO\COMODO Internet Security\cfpver.dat".format(os.environ['systemdrive'])
		ccsvar = open(Path_for_CCS_Version, "r")
		getccsvar = ccsvar.read()
		getccsvar = getccsvar.strip('\r\n')
	return getccsvar

def alert(arg):
    sys.stderr.write("%d%d%d" % (arg, arg, arg))
    
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

def AVDB():
	if os.path.exists(os.environ['programdata'] + '\COMODO\Cis\signatures'):
		return True
	return False

def checkCCS():
	if os.path.exists(os.environ['systemdrive'] + '\Program Files\COMODO\COMODO Internet Security\cmdagent.exe'):
		return True
	return False

if not os.path.exists(verifyfileexist()):
	inst1=checkCCS()
	if inst1:
		print "Comodo Client - Security is installed on the Endpoint\n"
		if enforceCCS is "yes":
			testccsversion = readCCSVersion()
			if testccsversion == CCSVersion:
				print "\tComodo Client - Security Enforced Version = " + testccsversion + "\n"
				inst2=AVDB()
				if inst2:
					print "\tAntivirus signature database up to date\n"
				else:
					print "\tAntivirus signature database is NOT up to date\n"
					i=1
			else:
				print "\tComodo Client - Security Enforced Version is NOT " + CCSVersion + "\n"
				print "\tComodo Client - Security Version " + testccsversion + " will be uninstalled\n"
				i=1
		else:
			inst2=AVDB()
			if inst2:
				print "\tAntivirus signature database up to date\n"
			else:
				print "\tAntivirus signature database is NOT up to date\n"
				i=1
	else:
		print "Comodo Client - Security is not installed on the Endpoint\n"
		i=1
	if i==1:
		WriteTimeStampToTheTimeStampFile(datestamp)
		alert(1)
	else:
		alert(0)
else:
	statusdate = datetime.datetime.strptime(readTimeStampStatus(), '%y-%m-%d %H:%M:%S')
	if datenow < statusdate:
		alert(0)
	else:
		inst1=checkCCS()
		if inst1:
			print "Comodo Client - Security is installed on the Endpoint\n"
			if enforceCCS is "yes":
				testccsversion = readCCSVersion()
				if testccsversion == CCSVersion:
					print "\tComodo Client - Security Enforced Version = " + testccsversion + "\n"
					inst2=AVDB()
					if inst2:
						print "\tAntivirus signature database up to date\n"
					else:
						print "\tAntivirus signature database is NOT up to date\n"
						i=1
				else:
					print "\tComodo Client - Security Enforced Version is NOT " + CCSVersion + "\n"
					print "\tComodo Client - Security Version " + testccsversion + " will be uninstalled\n"
					i=1
			else:
				inst2=AVDB()
				if inst2:
					print "\tAntivirus signature database up to date\n"
				else:
					print "\tAntivirus signature database is NOT up to date\n"
					i=1
		else:
			print "Comodo Client - Security is not installed on the Endpoint\n"
			i=1
		if i==1:
			WriteTimeStampToTheTimeStampFile(datestamp)
			alert(1)
		else:
			alert(0)