1
# Gufw 10.04.1 - http://gufw.tuxfamily.org
2
# Copyright (C) 2009 Marcos Alvarez Costales
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
24
def __init__(self, variable):
25
self.variable = variable
29
def set_status(self, status):
30
commands.getstatusoutput(self.variable.get_command(status))
35
status = commands.getstatusoutput(self.variable.get_command("status"))
36
if status[1].find("Status: active") != -1:
37
return self.variable.get_constant("enabled")
39
return self.variable.get_constant("disabled")
43
def set_default(self, default):
44
commands.getstatusoutput(self.variable.get_command(default))
48
def get_default(self):
53
default = commands.getstatusoutput(self.variable.get_command("search_policy_incoming"))
54
if default[1].find("ACCEPT") != -1:
55
incoming = self.variable.get_constant("allow")
56
elif default[1].find("DROP") != -1:
57
incoming = self.variable.get_constant("deny")
58
elif default[1].find("REJECT") != -1:
59
incoming = self.variable.get_constant("reject")
62
default = commands.getstatusoutput(self.variable.get_command("search_policy_outgoing"))
63
if default[1].find("ACCEPT") != -1:
64
outgoing = self.variable.get_constant("allow")
65
elif default[1].find("DROP") != -1:
66
outgoing = self.variable.get_constant("deny")
67
elif default[1].find("REJECT") != -1:
68
outgoing = self.variable.get_constant("reject")
70
return incoming, outgoing
73
# Get actual rules (NOT SPLIT BY ACTION)
75
rules = commands.getstatusoutput(self.variable.get_command("status"))
76
rule_lines = rules[1].split("\n")
79
for descomponent_rules in rule_lines:
81
if descomponent_rules.find(self.variable.get_constant("allow_upper")) != -1 or \
82
descomponent_rules.find(self.variable.get_constant("deny_upper")) != -1 or \
83
descomponent_rules.find(self.variable.get_constant("limit_upper")) != -1 or \
84
descomponent_rules.find(self.variable.get_constant("reject_upper")) != -1:
86
return_rules.append(descomponent_rules)
92
def set_ufw_log(self, status_ufw_log):
93
commands.getstatusoutput(self.variable.get_command(status_ufw_log))
97
def get_ufw_log(self):
98
status = commands.getstatusoutput(self.variable.get_command("status_verbose"))
99
if status[1].find("Logging: on (high)") != -1:
100
return self.variable.get_constant("ufw_log_high")
101
elif status[1].find("Logging: on (medium)") != -1:
102
return self.variable.get_constant("ufw_log_medium")
103
elif status[1].find("Logging: on (low)") != -1:
104
return self.variable.get_constant("ufw_log_low")
105
elif status[1].find("Logging: on (full)") != -1:
106
return self.variable.get_constant("ufw_log_full")
108
return self.variable.get_constant("ufw_log_off")
112
def set_gufw_log(self, status_gufw_log):
113
if not self.variable.dev:
114
status_gufw_log_aux = status_gufw_log
116
status_gufw_log_aux = status_gufw_log + "_dev"
118
commands.getstatusoutput(self.variable.get_command(status_gufw_log_aux))
122
def get_gufw_log(self):
123
if not self.variable.dev:
124
command = commands.getstatusoutput(self.variable.get_command("cfg_gufw_log"))
126
command = commands.getstatusoutput(self.variable.get_command("cfg_gufw_log_dev"))
129
return self.variable.get_constant("gufw_log_on")
131
return self.variable.get_constant("gufw_log_off")
135
def add_rule_component(self, service, insert_number, action, direction, log, protocol, fromip, fromport, toip, toport):
137
if service == self.variable.get_constant("service_no"):
138
rule = self.variable.get_command("add_complete_rule")
140
rule = self.variable.get_command("add_short_rule")
143
if insert_number != "0":
144
rule = rule.replace("&insert", insert_number)
146
rule = rule.replace("insert &insert ", "")
149
rule = rule.replace("&action", action)
152
rule = rule.replace("&direction", direction)
155
if log != self.variable.get_constant("log-default"):
156
rule = rule.replace("&log", log)
158
rule = rule.replace("&log ", "")
161
if protocol != self.variable.get_constant("both"):
162
rule = rule.replace("&protocol", protocol)
164
rule = rule.replace(" proto &protocol ", " ")
168
rule = rule.replace("&fromIP", fromip)
170
rule = rule.replace("&fromIP", self.variable.get_constant("any"))
173
rule = rule.replace("&fromPort", fromport)
175
rule = rule.replace(" port &fromPort ", " ")
179
rule = rule.replace("&toIP", toip)
181
rule = rule.replace("&toIP", self.variable.get_constant("any"))
184
rule = rule.replace("&toPort", toport)
186
rule = rule.replace(" port &toPort", "")
188
# Return rule command
193
def add_rule(self, rule):
194
result = commands.getstatusoutput(rule)
199
def remove_rule_component_command(self, number_rule_row):
200
command_rule = self.variable.get_command("remove_rule")
201
command_rule = command_rule.replace("&number", number_rule_row)
205
# Command Remove rule
206
def remove_rule(self, command_rule):
207
result = commands.getstatusoutput(command_rule)
211
# Return size Gufw window
212
def get_old_size_window(self):
213
if not self.variable.dev:
214
command = self.variable.get_command("read_size_win")
216
command = self.variable.get_command("read_size_win_dev")
218
width_height = commands.getstatusoutput(command)
219
if width_height[0] == 0:
220
width_height_split = width_height[1].split(";")
221
width = width_height_split[0].replace("width=","")
222
height = width_height_split[1].replace("height=","")
224
width = self.variable.get_constant("window_width")
225
height = self.variable.get_constant("window_height")
227
return int(width),int(height)
230
# Save actual size window
231
def save_size_window(self, win_width, win_height):
232
if not self.variable.dev:
233
command = self.variable.get_command("save_size_win")
235
command = self.variable.get_command("save_size_win_dev")
237
command = command.replace("&1", str(win_width))
238
command = command.replace("&2", str(win_height))
240
commands.getstatusoutput(command)
244
def reset_all_rules(self):
245
command = self.variable.get_command("reset_all_rules")
246
commands.getstatusoutput(command)