~ubuntu-branches/ubuntu/maverick/gui-ufw/maverick

« back to all changes in this revision

Viewing changes to model/FrontEnd.py

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2010-03-15 22:19:04 UTC
  • mfrom: (1.1.15 upstream)
  • Revision ID: james.westby@ubuntu.com-20100315221904-bsiaax6ayc9j124o
Tags: 10.04.2-0ubuntu1
* New upstream release (LP: #538649).
* Do not install /etc/gufw/*.cfg anymore, only remove /etc/gufw/gufw.cfg if
  needed (remove debian/postinst).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Gufw 10.04.1 - http://gufw.tuxfamily.org
2
 
# Copyright (C) 2009 Marcos Alvarez Costales
3
 
#
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.
8
 
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.
13
 
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
16
 
# information.
17
 
 
18
 
 
19
 
import commands
20
 
 
21
 
 
22
 
class Frontend:
23
 
 
24
 
    def __init__(self, variable):
25
 
        self.variable = variable
26
 
 
27
 
       
28
 
    # Return status ufw
29
 
    def set_status(self, status):
30
 
        commands.getstatusoutput(self.variable.get_command(status))
31
 
 
32
 
 
33
 
    # Get status ufw
34
 
    def get_status(self):
35
 
        status = commands.getstatusoutput(self.variable.get_command("status"))
36
 
        if status[1].find("Status: active") != -1:
37
 
            return self.variable.get_constant("enabled")
38
 
        else:
39
 
            return self.variable.get_constant("disabled")
40
 
 
41
 
 
42
 
    # Return status ufw
43
 
    def set_default(self, default):
44
 
        commands.getstatusoutput(self.variable.get_command(default))
45
 
 
46
 
 
47
 
    # Get status ufw
48
 
    def get_default(self):
49
 
        incoming = ""
50
 
        outgoing = ""
51
 
 
52
 
        # Incoming
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")
60
 
         
61
 
        # Outgoing   
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")
69
 
            
70
 
        return incoming, outgoing
71
 
 
72
 
 
73
 
    # Get actual rules (NOT SPLIT BY ACTION)
74
 
    def get_rules(self):
75
 
        rules = commands.getstatusoutput(self.variable.get_command("status"))
76
 
        rule_lines = rules[1].split("\n")
77
 
        return_rules = []
78
 
        
79
 
        for descomponent_rules in rule_lines:
80
 
            
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:
85
 
                
86
 
                return_rules.append(descomponent_rules)
87
 
                
88
 
        return return_rules
89
 
 
90
 
 
91
 
    # Return status ufw
92
 
    def set_ufw_log(self, status_ufw_log):
93
 
        commands.getstatusoutput(self.variable.get_command(status_ufw_log))
94
 
 
95
 
 
96
 
    # Get status ufw
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")
107
 
        else:
108
 
            return self.variable.get_constant("ufw_log_off")
109
 
 
110
 
 
111
 
    # Return status ufw
112
 
    def set_gufw_log(self, status_gufw_log):
113
 
        if not self.variable.dev:
114
 
            status_gufw_log_aux = status_gufw_log
115
 
        else:
116
 
            status_gufw_log_aux = status_gufw_log + "_dev"
117
 
            
118
 
        commands.getstatusoutput(self.variable.get_command(status_gufw_log_aux))
119
 
 
120
 
 
121
 
    # Get status ufw
122
 
    def get_gufw_log(self):
123
 
        if not self.variable.dev:
124
 
            command = commands.getstatusoutput(self.variable.get_command("cfg_gufw_log"))
125
 
        else:
126
 
            command = commands.getstatusoutput(self.variable.get_command("cfg_gufw_log_dev"))
127
 
            
128
 
        if command[0] == 0:
129
 
            return self.variable.get_constant("gufw_log_on")
130
 
        else:
131
 
            return self.variable.get_constant("gufw_log_off")
132
 
 
133
 
 
134
 
    # Add rule 
135
 
    def add_rule_component(self, service, insert_number, action, direction, log, protocol, fromip, fromport, toip, toport):
136
 
        # Component rule
137
 
        if service == self.variable.get_constant("service_no"):
138
 
            rule = self.variable.get_command("add_complete_rule")
139
 
        else:
140
 
            rule = self.variable.get_command("add_short_rule") 
141
 
        
142
 
        # Insert Number
143
 
        if insert_number != "0":
144
 
            rule = rule.replace("&insert", insert_number)
145
 
        else:
146
 
            rule = rule.replace("insert &insert ", "")
147
 
        
148
 
        # Action
149
 
        rule = rule.replace("&action", action)
150
 
        
151
 
        # Direction
152
 
        rule = rule.replace("&direction", direction)
153
 
        
154
 
        # Log
155
 
        if log != self.variable.get_constant("log-default"):
156
 
            rule = rule.replace("&log", log)
157
 
        else:
158
 
            rule = rule.replace("&log ", "")
159
 
        
160
 
        # Protocol
161
 
        if protocol != self.variable.get_constant("both"):
162
 
            rule = rule.replace("&protocol", protocol)
163
 
        else:
164
 
            rule = rule.replace(" proto &protocol ", " ")
165
 
            
166
 
        # FROM
167
 
        if fromip != "":
168
 
            rule = rule.replace("&fromIP", fromip)
169
 
        else:
170
 
            rule = rule.replace("&fromIP", self.variable.get_constant("any"))
171
 
            
172
 
        if fromport != "":
173
 
            rule = rule.replace("&fromPort", fromport)
174
 
        else:
175
 
            rule = rule.replace(" port &fromPort ", " ")
176
 
            
177
 
        # TO
178
 
        if toip != "":
179
 
            rule = rule.replace("&toIP", toip)
180
 
        else:
181
 
            rule = rule.replace("&toIP", self.variable.get_constant("any"))
182
 
            
183
 
        if toport != "":
184
 
            rule = rule.replace("&toPort", toport)
185
 
        else:
186
 
            rule = rule.replace(" port &toPort", "")
187
 
 
188
 
        # Return rule command
189
 
        return rule
190
 
 
191
 
 
192
 
    # Command Add rule
193
 
    def add_rule(self, rule):
194
 
        result = commands.getstatusoutput(rule)
195
 
        return result[1]
196
 
 
197
 
 
198
 
    # Add 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)
202
 
        return command_rule
203
 
 
204
 
 
205
 
    # Command Remove rule
206
 
    def remove_rule(self, command_rule):
207
 
        result = commands.getstatusoutput(command_rule)
208
 
        return result[1]
209
 
 
210
 
 
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")
215
 
        else:
216
 
            command = self.variable.get_command("read_size_win_dev")
217
 
            
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=","")
223
 
        else:
224
 
            width  = self.variable.get_constant("window_width")
225
 
            height = self.variable.get_constant("window_height")
226
 
        
227
 
        return int(width),int(height)
228
 
        
229
 
 
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")
234
 
        else:
235
 
            command = self.variable.get_command("save_size_win_dev")
236
 
        
237
 
        command = command.replace("&1", str(win_width))
238
 
        command = command.replace("&2", str(win_height))
239
 
        
240
 
        commands.getstatusoutput(command)
241
 
 
242
 
 
243
 
    # Reset all rules
244
 
    def reset_all_rules(self):
245
 
        command = self.variable.get_command("reset_all_rules")
246
 
        commands.getstatusoutput(command)
247