~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: 2009-05-17 17:57:33 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20090517175733-g5b7k9wzsvmgv433
Tags: upstream-9.04.0
ImportĀ upstreamĀ versionĀ 9.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Gufw 9.04.0 - 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
from Variable import Variable
 
21
 
 
22
 
 
23
class Frontend:
 
24
 
 
25
    def __init__(self):
 
26
        self.variable = Variable()
 
27
 
 
28
       
 
29
    # Return status ufw
 
30
    def set_status(self, status):
 
31
        commands.getstatusoutput(self.variable.get_command(status))
 
32
        return status
 
33
 
 
34
 
 
35
    # Get status ufw
 
36
    def get_status(self):
 
37
        l_status = commands.getstatusoutput(self.variable.get_command("status"))
 
38
        if l_status[1].find("Status: active") != -1:
 
39
            return self.variable.get_constant("enabled")
 
40
        else:
 
41
            return self.variable.get_constant("disabled")
 
42
 
 
43
 
 
44
    # Return status ufw
 
45
    def set_default(self, default):
 
46
        commands.getstatusoutput(self.variable.get_command(default))
 
47
        return default
 
48
 
 
49
 
 
50
    # Get status ufw
 
51
    def get_default(self):
 
52
        l_default = commands.getstatusoutput(self.variable.get_command("search_policy"))
 
53
        if l_default[1].find("ACCEPT") != -1:
 
54
            return self.variable.get_constant("allow")
 
55
        elif l_default[1].find("DROP") != -1:
 
56
            return self.variable.get_constant("deny")
 
57
        elif l_default[1].find("REJECT") != -1:
 
58
            return self.variable.get_constant("reject")
 
59
 
 
60
 
 
61
    # Get actual rules (NOT SPLIT BY ACTION)
 
62
    def get_rules(self):
 
63
        l_rules        = commands.getstatusoutput(self.variable.get_command("status"))
 
64
        l_rule_lines   = l_rules[1].split("\n")
 
65
        l_return_rules = []
 
66
        
 
67
        for l_descomponent_rules in l_rule_lines:
 
68
            
 
69
            if l_descomponent_rules.find(self.variable.get_constant("allow_upper"))  != -1 or \
 
70
               l_descomponent_rules.find(self.variable.get_constant("deny_upper"))   != -1 or \
 
71
               l_descomponent_rules.find(self.variable.get_constant("limit_upper"))  != -1 or \
 
72
               l_descomponent_rules.find(self.variable.get_constant("reject_upper")) != -1:
 
73
                
 
74
                l_return_rules.append(l_descomponent_rules)
 
75
                
 
76
        return l_return_rules
 
77
 
 
78
 
 
79
    # Return status ufw
 
80
    def set_ufw_log(self, status_ufw_log):
 
81
        commands.getstatusoutput(self.variable.get_command(status_ufw_log))
 
82
        return status_ufw_log
 
83
 
 
84
 
 
85
    # Get status ufw
 
86
    def get_ufw_log(self):
 
87
        l_status = commands.getstatusoutput(self.variable.get_command("status_verbose"))
 
88
        if l_status[1].find("Logging: on (high)") != -1:
 
89
            return self.variable.get_constant("ufw_log_high")
 
90
        elif l_status[1].find("Logging: on (medium)") != -1:
 
91
            return self.variable.get_constant("ufw_log_medium")
 
92
        elif l_status[1].find("Logging: on (low)") != -1:
 
93
            return self.variable.get_constant("ufw_log_low")
 
94
        else:
 
95
            return self.variable.get_constant("ufw_log_off")
 
96
 
 
97
 
 
98
    # Return status ufw
 
99
    def set_gufw_log(self, status_gufw_log):
 
100
        if not self.variable.dev:
 
101
            l_status_gufw_log = status_gufw_log
 
102
        else:
 
103
            l_status_gufw_log = status_gufw_log + "_dev"
 
104
            
 
105
        commands.getstatusoutput(self.variable.get_command(l_status_gufw_log))
 
106
        return status_gufw_log
 
107
 
 
108
 
 
109
    # Get status ufw
 
110
    def get_gufw_log(self):
 
111
        if not self.variable.dev:
 
112
            l_command = commands.getstatusoutput(self.variable.get_command("cfg_gufw_log"))
 
113
        else:
 
114
            l_command = commands.getstatusoutput(self.variable.get_command("cfg_gufw_log_dev"))
 
115
            
 
116
        if l_command[0] == 0:
 
117
            return self.variable.get_constant("gufw_log_on")
 
118
        else:
 
119
            return self.variable.get_constant("gufw_log_off")
 
120
 
 
121
 
 
122
    # Add rule 
 
123
    def add_rule_component(self, service, action, protocol, fromip, fromport, toip, toport):
 
124
        # Component rule
 
125
        if service == self.variable.get_constant("service_no"):
 
126
            l_rule = "LANG=en ufw &action proto &protocol from &fromIP port &fromPort to &toIP port &toPort"
 
127
        else:
 
128
            l_rule = "LANG=en ufw &action &toPort"
 
129
        
 
130
        # Action
 
131
        l_rule = l_rule.replace("&action", action)
 
132
        
 
133
        # Protocol
 
134
        if protocol != self.variable.get_constant("both"):
 
135
            l_rule = l_rule.replace("&protocol", protocol)
 
136
        else:
 
137
            l_rule = l_rule.replace(" proto &protocol ", " ")
 
138
            
 
139
        # FROM
 
140
        if fromip != "":
 
141
            l_rule = l_rule.replace("&fromIP", fromip)
 
142
        else:
 
143
            l_rule = l_rule.replace("&fromIP", self.variable.get_constant("any"))
 
144
            
 
145
        if fromport != "":
 
146
            l_rule = l_rule.replace("&fromPort", fromport)
 
147
        else:
 
148
            l_rule = l_rule.replace(" port &fromPort ", " ")
 
149
            
 
150
        # TO
 
151
        if toip != "":
 
152
            l_rule = l_rule.replace("&toIP", toip)
 
153
        else:
 
154
            l_rule = l_rule.replace("&toIP", self.variable.get_constant("any"))
 
155
            
 
156
        if toport != "":
 
157
            l_rule = l_rule.replace("&toPort", toport)
 
158
        else:
 
159
            l_rule = l_rule.replace(" port &toPort", "")
 
160
 
 
161
        # Return rule command
 
162
        return l_rule
 
163
 
 
164
 
 
165
    # Command Add rule
 
166
    def add_rule(self, rule):
 
167
        l_result = commands.getstatusoutput(rule)
 
168
        return l_result[1]
 
169
 
 
170
 
 
171
    # Add rule 
 
172
    def remove_rule_component(self, rule_to, action, rule_from):
 
173
        # Component rule
 
174
        protocol = ""
 
175
        toip     = ""
 
176
        toport   = ""
 
177
        fromip   = "" 
 
178
        fromport = ""
 
179
        l_rule   = "LANG=en ufw delete &action proto &protocol from &fromIP port &fromPort to &toIP port &toPort"
 
180
        
 
181
        # Set protocol
 
182
        if rule_to.find("/tcp")   != -1 or rule_from.find("/tcp") != -1:
 
183
            protocol = self.variable.get_constant("tcp")
 
184
            rule_to   = rule_to.replace("/tcp","")
 
185
            rule_from = rule_from.replace("/tcp","")
 
186
        elif rule_to.find("/udp")   != -1 or rule_from.find("/udp") != -1:
 
187
            protocol = self.variable.get_constant("udp")
 
188
            rule_to   = rule_to.replace("/udp","")
 
189
            rule_from = rule_from.replace("/udp","")
 
190
        else:
 
191
            protocol = self.variable.get_constant("both")
 
192
 
 
193
        
 
194
        # Set IP/Port TO
 
195
        l_to_aux = rule_to.split(" ")
 
196
        if len(l_to_aux) == 1:
 
197
            # IP or Port?
 
198
            if l_to_aux[0] == self.variable.get_constant("anywhere"):
 
199
                toip = ""
 
200
            elif ( l_to_aux[0].find(".") != -1 ) or ( len(l_to_aux[0]) > 11 ):
 
201
                toip = l_to_aux[0]
 
202
            else:
 
203
                toport = l_to_aux[0]
 
204
        elif len(l_to_aux) == 2:
 
205
            toip   = l_to_aux[0]
 
206
            toport = l_to_aux[1]
 
207
        elif len(l_to_aux) == 3:
 
208
            # It's a app command
 
209
            l_rule = "LANG=en ufw delete " + l_to_aux[2]
 
210
            return l_rule
 
211
            
 
212
        # Set IP/Port FROM
 
213
        l_from_aux = rule_from.split(" ")
 
214
        if len(l_from_aux) == 1:
 
215
            # IP or Port?
 
216
            if l_from_aux[0] == self.variable.get_constant("anywhere"):
 
217
                fromip = ""
 
218
            elif ( l_from_aux[0].find(".") != -1 ) or ( len(l_from_aux[0]) > 11 ):
 
219
                fromip = l_from_aux[0]
 
220
            else:
 
221
                fromport = l_from_aux[0]
 
222
        elif len(l_from_aux) == 2:
 
223
            fromip   = l_from_aux[0]
 
224
            fromport = l_from_aux[1]
 
225
        elif len(l_from_aux) == 3:
 
226
            # It's a app command
 
227
            l_rule = "LANG=en ufw delete " + l_from_aux[2]
 
228
            return l_rule
 
229
            
 
230
        # Set rule    
 
231
        # Action
 
232
        l_rule = l_rule.replace("&action", action)
 
233
        
 
234
        # Protocol
 
235
        if protocol != self.variable.get_constant("both"):
 
236
            l_rule = l_rule.replace("&protocol", protocol)
 
237
        else:
 
238
            l_rule = l_rule.replace(" proto &protocol ", " ")
 
239
            
 
240
        # FROM
 
241
        if fromip != "":
 
242
            l_rule = l_rule.replace("&fromIP", fromip)
 
243
        else:
 
244
            l_rule = l_rule.replace("&fromIP", self.variable.get_constant("any"))
 
245
            
 
246
        if fromport != "":
 
247
            l_rule = l_rule.replace("&fromPort", fromport)
 
248
        else:
 
249
            l_rule = l_rule.replace(" port &fromPort ", " ")
 
250
            
 
251
        # TO
 
252
        if toip != "":
 
253
            l_rule = l_rule.replace("&toIP", toip)
 
254
        else:
 
255
            l_rule = l_rule.replace("&toIP", self.variable.get_constant("any"))
 
256
            
 
257
        if toport != "":
 
258
            l_rule = l_rule.replace("&toPort", toport)
 
259
        else:
 
260
            l_rule = l_rule.replace(" port &toPort", "")
 
261
 
 
262
        # Return rule command
 
263
        return l_rule
 
264
 
 
265
 
 
266
    # Command Add rule
 
267
    def remove_rule(self, rule):
 
268
        l_result = commands.getstatusoutput(rule)
 
269
        return l_result[1]