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

« back to all changes in this revision

Viewing changes to src/model/Rule.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.2 - http://gufw.tuxfamily.org
 
2
# Copyright (C) 2008-2010 Marcos Alvarez Costales <marcos@softastur.org>
 
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
import commands
 
19
 
 
20
 
 
21
class Rule:
 
22
    """Get, insert and remove Rules"""
 
23
    def __init__(self):
 
24
        self.rules = self.get_ufw_rules()
 
25
    
 
26
    def get_ufw_rules(self):
 
27
        return_rules = []
 
28
        ufw_rules = commands.getstatusoutput("LANGUAGE=C ufw status verbose")
 
29
        rule_lines = ufw_rules[1].split("\n")
 
30
        
 
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)
 
37
        
 
38
        return return_rules
 
39
    
 
40
    def get_rules(self):
 
41
        """Get actual rules"""
 
42
        return self.rules
 
43
    
 
44
    def add_rule(self, is_program, insert_number, action, direction, log, protocol, fromip, fromport, toip, toport):
 
45
        """Add a rule"""
 
46
        # Component rule
 
47
        if is_program:
 
48
            rule = "ufw insert &insert &action &direction &log proto &protocol from &fromIP port &fromPort to &toIP port &toPort"
 
49
        else:
 
50
            rule = "ufw insert &insert &action &direction &log &toPort" 
 
51
        
 
52
        # Insert Number
 
53
        if insert_number != "0":
 
54
            rule = rule.replace("&insert", insert_number)
 
55
        else:
 
56
            rule = rule.replace("insert &insert ", "")
 
57
        
 
58
        # Action
 
59
        rule = rule.replace("&action", action)
 
60
        
 
61
        # Direction
 
62
        rule = rule.replace("&direction", direction)
 
63
        
 
64
        # Log
 
65
        if log != "log-default":
 
66
            rule = rule.replace("&log", log)
 
67
        else:
 
68
            rule = rule.replace("&log ", "")
 
69
        
 
70
        # Protocol
 
71
        if protocol != "both":
 
72
            rule = rule.replace("&protocol", protocol)
 
73
        else:
 
74
            rule = rule.replace(" proto &protocol ", " ")
 
75
            
 
76
        # FROM
 
77
        if fromip != "":
 
78
            rule = rule.replace("&fromIP", fromip)
 
79
        else:
 
80
            rule = rule.replace("&fromIP", "any")
 
81
            
 
82
        if fromport != "":
 
83
            rule = rule.replace("&fromPort", fromport)
 
84
        else:
 
85
            rule = rule.replace(" port &fromPort ", " ")
 
86
            
 
87
        # TO
 
88
        if toip != "":
 
89
            rule = rule.replace("&toIP", toip)
 
90
        else:
 
91
            rule = rule.replace("&toIP", "any")
 
92
            
 
93
        if toport != "":
 
94
            rule = rule.replace("&toPort", toport)
 
95
        else:
 
96
            rule = rule.replace(" port &toPort", "")
 
97
        
 
98
        # Run ufw command
 
99
        ufw_cmd = commands.getstatusoutput(rule)
 
100
        if ufw_cmd[0] == 0:
 
101
            self.rules = self.get_ufw_rules()
 
102
        return ufw_cmd[0], rule
 
103
    
 
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)
 
108
        if ufw_cmd[0] == 0:
 
109
            self.rules = self.get_ufw_rules()
 
110
        return ufw_cmd[0], command_rule
 
111
        
 
112
    def refresh_rules(self):
 
113
        """Refresh rules in ufw"""
 
114
        self.rules = self.get_ufw_rules()