2017-10-24 19:16:36 +05:00
|
|
|
import subprocess
|
|
|
|
|
import CyberCPLogFileWriter as logging
|
|
|
|
|
import shlex
|
2018-11-16 14:41:40 +05:00
|
|
|
from processUtilities import ProcessUtilities
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FirewallUtilities:
|
|
|
|
|
|
2018-11-16 14:41:40 +05:00
|
|
|
@staticmethod
|
|
|
|
|
def resFailed(res):
|
|
|
|
|
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu and res != 0:
|
|
|
|
|
return True
|
|
|
|
|
elif ProcessUtilities.decideDistro() == ProcessUtilities.centos and res == 1:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
@staticmethod
|
2018-11-14 15:15:20 -05:00
|
|
|
def doCommand(command):
|
2017-10-24 19:16:36 +05:00
|
|
|
try:
|
2018-11-14 15:15:20 -05:00
|
|
|
cmd = shlex.split(command)
|
|
|
|
|
res = subprocess.call(cmd)
|
2018-11-16 14:41:40 +05:00
|
|
|
if FirewallUtilities.resFailed(res):
|
|
|
|
|
logging.CyberCPLogFileWriter.writeToFile("Failed to apply rule: " + command + " Error #" + str(res))
|
2018-11-14 15:15:20 -05:00
|
|
|
return 0
|
2017-11-02 02:09:47 +05:00
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
except OSError, msg:
|
2018-11-16 14:41:40 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile("Failed to apply rule: " + command + " Error: " + str(msg))
|
2018-11-14 15:15:20 -05:00
|
|
|
return 0
|
|
|
|
|
except ValueError, msg:
|
2018-11-16 14:41:40 +05:00
|
|
|
logging.CyberCPLogFileWriter.writeToFile("Failed to apply rule: " + command + " Error: " + str(msg), 1)
|
2018-11-14 15:15:20 -05:00
|
|
|
return 0
|
|
|
|
|
return 1
|
2017-10-24 19:16:36 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
@staticmethod
|
|
|
|
|
def addRule(proto,port,ipAddress):
|
|
|
|
|
ruleFamily = 'rule family="ipv4"'
|
|
|
|
|
sourceAddress = 'source address="' + ipAddress + '"'
|
|
|
|
|
ruleProtocol = 'port protocol="' + proto + '"'
|
|
|
|
|
rulePort = 'port="' + port + '"'
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
command = "sudo firewall-cmd --permanent --zone=public --add-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-11-16 14:41:40 +05:00
|
|
|
if not FirewallUtilities.doCommand(command):
|
2017-10-24 19:16:36 +05:00
|
|
|
return 0
|
2018-11-14 15:15:20 -05:00
|
|
|
|
|
|
|
|
ruleFamily = 'rule family="ipv6"'
|
|
|
|
|
sourceAddress = ''
|
|
|
|
|
|
|
|
|
|
command = "sudo firewall-cmd --permanent --zone=public --add-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
|
|
|
|
|
|
2018-11-16 14:41:40 +05:00
|
|
|
if not FirewallUtilities.doCommand(command):
|
2018-11-14 15:15:20 -05:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
command = 'sudo firewall-cmd --reload'
|
|
|
|
|
|
2018-11-16 14:41:40 +05:00
|
|
|
if not FirewallUtilities.doCommand(command):
|
2017-10-24 19:16:36 +05:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2018-11-14 12:17:24 -05:00
|
|
|
def deleteRule(proto, port, ipAddress):
|
2018-11-14 15:15:20 -05:00
|
|
|
ruleFamily = 'rule family="ipv4"'
|
|
|
|
|
sourceAddress = 'source address="' + ipAddress + '"'
|
|
|
|
|
ruleProtocol = 'port protocol="' + proto + '"'
|
|
|
|
|
rulePort = 'port="' + port + '"'
|
2018-11-14 13:45:03 -05:00
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
command = "sudo firewall-cmd --permanent --zone=public --remove-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-11-16 14:41:40 +05:00
|
|
|
if not FirewallUtilities.doCommand(command):
|
2018-11-14 15:15:20 -05:00
|
|
|
return 0
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
ruleFamily = 'rule family="ipv6"'
|
|
|
|
|
sourceAddress = ''
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
command = "sudo firewall-cmd --permanent --zone=public --remove-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-11-16 14:41:40 +05:00
|
|
|
if not FirewallUtilities.doCommand(command):
|
2018-11-14 15:15:20 -05:00
|
|
|
return 0
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
command = 'sudo firewall-cmd --reload'
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-11-16 14:41:40 +05:00
|
|
|
if not FirewallUtilities.doCommand(command):
|
2017-10-24 19:16:36 +05:00
|
|
|
return 0
|
|
|
|
|
|
2018-11-16 14:41:40 +05:00
|
|
|
return 1
|