Debian Codegraph @

firewallsettings.py

raw
1 #!/usr/bin/python3
2
3 ## system-config-printer
4
5 ## Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2015 Red Hat, Inc.
6 ## Authors:
7 ## Tim Waugh <[email protected]>
8
9 ## This program is free software; you can redistribute it and/or modify
10 ## it under the terms of the GNU General Public License as published by
11 ## the Free Software Foundation; either version 2 of the License, or
12 ## (at your option) any later version.
13
14 ## This program is distributed in the hope that it will be useful,
15 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ## GNU General Public License for more details.
18
19 ## You should have received a copy of the GNU General Public License
20 ## along with this program; if not, write to the Free Software
21 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
23 # config is generated from config.py.in by configure
24 import config
25
26 import dbus
27 import json
28 from debug import *
29
30 IPP_CLIENT_SERVICE = "ipp-client"
31 IPP_CLIENT_PORT = "631"
32 IPP_CLIENT_PROTOCOL = "udp"
33 IPP_SERVER_SERVICE = "ipp"
34 IPP_SERVER_PORT = "631"
35 IPP_SERVER_PROTOCOL = "tcp"
36 MDNS_SERVICE = "mdns"
37 MDNS_PORT = "5353"
38 MDNS_PROTOCOL = "udp"
39 SAMBA_CLIENT_SERVICE = "samba-client"
40
41 class FirewallD:
42 def __init__ (self):
43 try:
44 from firewall.client import FirewallClient
45 self._fw = FirewallClient ()
46 if not self._fw.connected:
47 debugprint ("FirewallD seems to be installed but not running")
48 self._fw = None
49 self._zone = None
50 self.running = False
51 return
52 zone_name = self._get_active_zone ()
53 if zone_name:
54 self._zone = self._fw.config().getZoneByName (zone_name)
55 else:
56 self._zone = None
57 self.running = True
58 debugprint ("Using /org/fedoraproject/FirewallD1")
59 except (ImportError, dbus.exceptions.DBusException):
60 self._fw = None
61 self._zone = None
62 self.running = False
63
64 def _get_active_zone (self):
65 zones = list(self._fw.getActiveZones().keys())
66 if not zones:
67 debugprint ("FirewallD: no changeable zone")
68 return None
69 elif len (zones) == 1:
70 # most probable case
71 return zones[0]
72 else:
73 # Do we need to handle the 'more active zones' case ?
74 # It's quite unlikely case because that would mean that more
75 # network connections are up and running and they are
76 # in different network zones at the same time.
77 debugprint ("FirewallD returned more zones, taking first one")
78 return zones[0]
79
80 def _get_fw_data (self, reply_handler=None, error_handler=None):
81 try:
82 debugprint ("%s in _get_fw_data: _fw_data is %s" %
83 (self, repr(self._fw_data.getServices())))
84 if self._fw_data:
85 debugprint ("Using cached firewall data")
86 if reply_handler:
87 reply_handler (self._fw_data)
88 except AttributeError:
89 try:
90 self._fw_data = self._zone.getSettings ()
91 debugprint ("Firewall data obtained")
92 if reply_handler:
93 reply_handler (self._fw_data)
94 except (dbus.exceptions.DBusException, AttributeError, ValueError) as e:
95 self._fw_data = None
96 debugprint ("Exception examining firewall")
97 if error_handler:
98 error_handler (e)
99
100 return self._fw_data
101
102 def read (self, reply_handler=None, error_handler=None):
103 if reply_handler:
104 self._get_fw_data (reply_handler,
105 error_handler)
106 else:
107 self._get_fw_data ()
108
109 def write (self):
110 try:
111 if self._zone:
112 self._zone.update (self._fw_data)
113 self._fw.reload ()
114 except dbus.exceptions.DBusException:
115 nonfatalException ()
116
117 def add_service (self, service):
118 if not self._get_fw_data ():
119 return
120
121 from firewall.errors import FirewallError
122 import firewall.errors
123 try:
124 self._fw_data.addService (service)
125 except FirewallError as e:
126 if e.code is firewall.errors.ALREADY_ENABLED:
127 pass
128 else:
129 raise FirewallError (e.code, e.msg)
130
131 def check_ipp_client_allowed (self):
132 if not self._get_fw_data ():
133 return True
134
135 return (IPP_CLIENT_SERVICE in self._fw_data.getServices () or
136 [IPP_CLIENT_PORT, IPP_CLIENT_PROTOCOL] in self._fw_data.getPorts ())
137
138 def check_ipp_server_allowed (self):
139 if not self._get_fw_data ():
140 return True
141
142 return (IPP_SERVER_SERVICE in self._fw_data.getServices () or
143 [IPP_SERVER_PORT, IPP_SERVER_PROTOCOL] in self._fw_data.getPorts ())
144
145 def check_samba_client_allowed (self):
146 if not self._get_fw_data ():
147 return True
148
149 return (SAMBA_CLIENT_SERVICE in self._fw_data.getServices ())
150
151 def check_mdns_allowed (self):
152 if not self._get_fw_data ():
153 return True
154
155 return (MDNS_SERVICE in self._fw_data.getServices () or
156 [MDNS_PORT, MDNS_PROTOCOL] in self._fw_data.getPorts ())
157
158
159
160
161 class SystemConfigFirewall:
162 DBUS_INTERFACE = "org.fedoraproject.Config.Firewall"
163 DBUS_PATH = "/org/fedoraproject/Config/Firewall"
164
165 def __init__(self):
166 try:
167 bus = dbus.SystemBus ()
168 obj = bus.get_object (self.DBUS_INTERFACE, self.DBUS_PATH)
169 self._fw = dbus.Interface (obj, self.DBUS_INTERFACE)
170 debugprint ("Using system-config-firewall")
171 except dbus.exceptions.DBusException:
172 debugprint ("No firewall ")
173 self._fw = None
174 self._fw_data = (None, None)
175
176 def _get_fw_data (self, reply_handler=None, error_handler=None):
177 try:
178 debugprint ("%s in _get_fw_data: _fw_data is %s" %
179 (self, repr(self._fw_data)))
180 if self._fw_data:
181 debugprint ("Using cached firewall data")
182 if reply_handler is None:
183 return self._fw_data
184
185 self._client_reply_handler (self._fw_data)
186 except AttributeError:
187 try:
188 if reply_handler:
189 self._fw.read (reply_handler=reply_handler,
190 error_handler=error_handler)
191 return
192
193 p = self._fw.read ()
194 self._fw_data = json.loads (p)
195 except (dbus.exceptions.DBusException, AttributeError, ValueError) as e:
196 self._fw_data = (None, None)
197 if error_handler:
198 debugprint ("Exception examining firewall")
199 self._client_error_handler (e)
200
201 return self._fw_data
202
203 def read (self, reply_handler=None, error_handler=None):
204 if reply_handler:
205 self._client_reply_handler = reply_handler
206 self._client_error_handler = error_handler
207 self._get_fw_data (reply_handler=self.reply_handler,
208 error_handler=self.error_handler)
209 else:
210 self._get_fw_data ()
211
212 def reply_handler (self, result):
213 try:
214 self._fw_data = json.loads (result)
215 except ValueError as e:
216 self.error_handler (e)
217 return
218
219 debugprint ("Firewall data obtained")
220 self._client_reply_handler (self._fw_data)
221
222 def error_handler (self, exc):
223 debugprint ("Exception fetching firewall data")
224 if self._client_error_handler:
225 self._client_error_handler (exc)
226 else:
227 debugprint ("Exception: %r" % exc)
228
229 def write (self):
230 try:
231 self._fw.write (json.dumps (self._fw_data[0]))
232 except:
233 pass
234
235 def _check_any_allowed (self, search):
236 (args, filename) = self._get_fw_data ()
237 if filename is None: return True
238 isect = set (search).intersection (set (args))
239 return len (isect) != 0
240
241
242 def add_service (self, service):
243 try:
244 (args, filename) = self._fw_data
245 except AttributeError:
246 (args, filename) = self._get_fw_data ()
247 if filename is None: return
248
249 args.append ("--service=" + service)
250 self._fw_data = (args, filename)
251
252 def check_ipp_client_allowed (self):
253 return self._check_any_allowed (set(["--port=%s:%s" %
254 (IPP_CLIENT_PORT, IPP_CLIENT_PROTOCOL),
255 "--service=" + IPP_CLIENT_SERVICE]))
256
257 def check_ipp_server_allowed (self):
258 return self._check_any_allowed (set(["--port=%s:%s" %
259 (IPP_SERVER_PORT, IPP_SERVER_PROTOCOL),
260 "--service=" + IPP_SERVER_SERVICE]))
261
262 def check_samba_client_allowed (self):
263 return self._check_any_allowed (set(["--service=" + SAMBA_CLIENT_SERVICE]))
264
265 def check_mdns_allowed (self):
266 return self._check_any_allowed (set(["--port=%s:%s" %
267 (MDNS_PORT, MDNS_PROTOCOL),
268 "--service=" + MDNS_SERVICE]))