1
# Gufw 10.04.2 - http://gufw.tuxfamily.org
2
# Copyright (C) 2008-2010 Marcos Alvarez Costales <marcos@softastur.org>
4
# Gufw is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 3 of the License, or
7
# (at your option) any later version.
9
# Gufw is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with Gufw; if not, see http://www.gnu.org/licenses for more
22
"""Get, insert and remove Rules"""
24
self.rules = self.get_ufw_rules()
26
def get_ufw_rules(self):
28
ufw_rules = commands.getstatusoutput("LANGUAGE=C ufw status verbose")
29
rule_lines = ufw_rules[1].split("\n")
31
for descomponent_rules in rule_lines:
32
if descomponent_rules.find("ALLOW") != -1 or \
33
descomponent_rules.find("DENY") != -1 or \
34
descomponent_rules.find("LIMIT") != -1 or \
35
descomponent_rules.find("REJECT") != -1:
36
return_rules.append(descomponent_rules)
41
"""Get actual rules"""
44
def add_rule(self, is_program, insert_number, action, direction, log, protocol, fromip, fromport, toip, toport):
48
rule = "ufw insert &insert &action &direction &log proto &protocol from &fromIP port &fromPort to &toIP port &toPort"
50
rule = "ufw insert &insert &action &direction &log &toPort"
53
if insert_number != "0":
54
rule = rule.replace("&insert", insert_number)
56
rule = rule.replace("insert &insert ", "")
59
rule = rule.replace("&action", action)
62
rule = rule.replace("&direction", direction)
65
if log != "log-default":
66
rule = rule.replace("&log", log)
68
rule = rule.replace("&log ", "")
71
if protocol != "both":
72
rule = rule.replace("&protocol", protocol)
74
rule = rule.replace(" proto &protocol ", " ")
78
rule = rule.replace("&fromIP", fromip)
80
rule = rule.replace("&fromIP", "any")
83
rule = rule.replace("&fromPort", fromport)
85
rule = rule.replace(" port &fromPort ", " ")
89
rule = rule.replace("&toIP", toip)
91
rule = rule.replace("&toIP", "any")
94
rule = rule.replace("&toPort", toport)
96
rule = rule.replace(" port &toPort", "")
99
ufw_cmd = commands.getstatusoutput(rule)
101
self.rules = self.get_ufw_rules()
102
return ufw_cmd[0], rule
104
def remove_rule(self, number_rule):
105
"""Remove number rule"""
106
command_rule = "ufw --force delete &number".replace("&number", number_rule)
107
ufw_cmd = commands.getstatusoutput(command_rule)
109
self.rules = self.get_ufw_rules()
110
return ufw_cmd[0], command_rule
112
def refresh_rules(self):
113
"""Refresh rules in ufw"""
114
self.rules = self.get_ufw_rules()