summaryrefslogtreecommitdiffstats
path: root/firewall.py
blob: 10ddf7e77747016579f0e2a3760de0f976773b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#
# firewall.py - firewall install data and installation
#
# Bill Nottingham <notting@redhat.com>
# Jeremy Katz <katzj@redhat.com>
#
# Copyright 2004 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
import iutil
import string
from flags import flags

from rhpl.log import log
from rhpl.translate import _, N_

class Service:
    def __init__ (self, key, name, ports):
        self.key = key
        self.name = name
        self.allowed = 0

        if type(ports) == type(""):
            self.ports = [ ports ]
        else:
            self.ports = ports


    def set_enabled(self, val):
        self.allowed = val

    def get_enabled(self):
        return self.allowed

    def get_name(self):
        return self.name

    def get_ports(self):
        return self.ports

class Firewall:
    def __init__ (self):
	self.enabled = 1
        self.trustdevs = []
	self.portlist = []
        self.services = [ Service("ssh", N_("Remote Login (SSH)"), "22:tcp"),
                          Service("http", N_("Web Server (HTTP, HTTPS)"), "80:tcp"),
                          Service("ftp", N_("File Transfer (FTP)"), "21:tcp"),

                          Service("smtp", N_("Mail Server (SMTP)"), "25:tcp") ]

    def writeKS(self, f):
	f.write("firewall")

        if self.enabled:
	    for arg in self.getArgList():
		f.write(" " + arg)
	else:
	    f.write(" --disabled")

	f.write("\n")

    def getArgList(self):
	args = []

        if self.enabled:
            args.append ("--enabled")
        else:
            args.append("--disabled")
            return args
        
        for service in self.services:
            if service.get_enabled():
                for p in service.get_ports():
                    args = args + [ "--port=%s" %(p,) ]

        for dev in self.trustdevs:
            args = args + [ "--trust=%s" %(dev,) ]

	for port in self.portlist:
	    args = args + [ "--port=%s" %(port,) ]
                
	return args

    def write (self, instPath):
	args = [ "/usr/sbin/lokkit", "--quiet", "--nostart", "-f" ]


        args = args + self.getArgList()

        try:
            if flags.setupFilesystems:
                iutil.execWithRedirect(args[0], args, root = instPath,
                                       stdout = None, stderr = None)
            else:
                log("would have run %s", args)
        except RuntimeError, msg:
            log ("lokkit run failed: %s", msg)
        except OSError, (errno, msg):
            log ("lokkit run failed: %s", msg)
        else:
            f = open(instPath +
                     '/etc/sysconfig/system-config-securitylevel', 'w')
            f.write("# system-config-securitylevel config written out by anaconda\n\n")
            for arg in args[3:]:
                f.write("%s\n" %(arg,))
            f.close()