2017-10-24 19:16:36 +05:00
|
|
|
import subprocess
|
|
|
|
|
import shlex
|
|
|
|
|
|
|
|
|
|
class FirewallUtilities:
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2018-11-14 15:15:20 -05:00
|
|
|
def doCommand(command):
|
2019-12-12 11:04:05 +05:00
|
|
|
from . import install as inst
|
2017-10-24 19:16:36 +05:00
|
|
|
try:
|
|
|
|
|
cmd = shlex.split(command)
|
2019-03-26 16:19:03 +05:00
|
|
|
res = subprocess.call(cmd)
|
2018-11-14 13:48:37 -05:00
|
|
|
if inst.preFlightsChecks.resFailed(inst.get_distro(), res):
|
2018-11-14 15:15:20 -05:00
|
|
|
inst.preFlightsChecks.stdOut("Failed to apply rule: " + command + " Error #" + str(res), 1)
|
2018-11-14 13:39:53 -05:00
|
|
|
return 0
|
|
|
|
|
|
2019-12-10 15:09:10 +05:00
|
|
|
except OSError as msg:
|
2018-11-14 15:15:20 -05:00
|
|
|
inst.preFlightsChecks.stdOut("Failed to apply rule: " + command + " Error: " + str(msg), 1)
|
2017-10-24 19:16:36 +05:00
|
|
|
return 0
|
2019-12-10 15:09:10 +05:00
|
|
|
except ValueError as msg:
|
2018-11-14 15:15:20 -05:00
|
|
|
inst.preFlightsChecks.stdOut("Failed to apply rule: " + command + " Error: " + str(msg), 1)
|
2017-10-24 19:16:36 +05:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
return 1
|
|
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
|
2017-10-24 19:16:36 +05:00
|
|
|
@staticmethod
|
2018-11-14 15:15:20 -05:00
|
|
|
def addRule(proto,port):
|
|
|
|
|
if port == "21":
|
|
|
|
|
command = "sudo firewall-cmd --add-service=ftp --permanent"
|
|
|
|
|
else:
|
|
|
|
|
ipAddress = "0.0.0.0/0"
|
|
|
|
|
ruleFamily = 'rule family="ipv4"'
|
|
|
|
|
sourceAddress = 'source address="' + ipAddress + '"'
|
|
|
|
|
ruleProtocol = 'port protocol="' + proto + '"'
|
|
|
|
|
rulePort = 'port="' + port + '"'
|
|
|
|
|
|
|
|
|
|
command = "sudo firewall-cmd --permanent --zone=public --add-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
|
|
|
|
|
|
|
|
|
|
if not FirewallUtilities.doCommand(command):
|
|
|
|
|
return 0
|
2018-11-14 12:39:42 -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 --add-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
|
2017-11-02 02:09:47 +05:00
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
if not FirewallUtilities.doCommand(command):
|
|
|
|
|
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-14 15:15:20 -05:00
|
|
|
if not FirewallUtilities.doCommand(command):
|
|
|
|
|
return 0
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
return 1
|
2017-10-24 19:16:36 +05:00
|
|
|
|
2018-11-14 15:15:20 -05:00
|
|
|
@staticmethod
|
|
|
|
|
def deleteRule(proto, port):
|
|
|
|
|
if port=="21":
|
|
|
|
|
command = "sudo firewall-cmd --remove-service=ftp --permanent"
|
|
|
|
|
else:
|
|
|
|
|
ipAddress = "0.0.0.0/0"
|
|
|
|
|
ruleFamily = 'rule family="ipv4"'
|
|
|
|
|
sourceAddress = 'source address="' + ipAddress + '"'
|
|
|
|
|
ruleProtocol = 'port protocol="' + proto + '"'
|
|
|
|
|
rulePort = 'port="' + port + '"'
|
|
|
|
|
|
|
|
|
|
command = "sudo firewall-cmd --permanent --zone=public --remove-rich-rule='" + ruleFamily + " " + sourceAddress + " " + ruleProtocol + " " + rulePort + " " + "accept'"
|
|
|
|
|
|
|
|
|
|
if not FirewallUtilities.doCommand(command):
|
|
|
|
|
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-14 15:15:20 -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
|
|
|
|
|
|
|
|
command = 'sudo firewall-cmd --reload'
|
|
|
|
|
|
|
|
|
|
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
|