Back to index

system-config-printer  1.3.9+20120706
firewall.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## system-config-printer
00004 
00005 ## Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
00006 ## Authors:
00007 ##  Tim Waugh <twaugh@redhat.com>
00008 
00009 ## This program is free software; you can redistribute it and/or modify
00010 ## it under the terms of the GNU General Public License as published by
00011 ## the Free Software Foundation; either version 2 of the License, or
00012 ## (at your option) any later version.
00013 
00014 ## This program is distributed in the hope that it will be useful,
00015 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 ## GNU General Public License for more details.
00018 
00019 ## You should have received a copy of the GNU General Public License
00020 ## along with this program; if not, write to the Free Software
00021 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022 
00023 # config is generated from config.py.in by configure
00024 import config
00025 
00026 import dbus
00027 import json
00028 from debug import *
00029 
00030 class Firewall:
00031     ALLOW_IPP_CLIENT = "--service=ipp-client"
00032     ALLOW_IPP_SERVER = "--service=ipp"
00033     ALLOW_SAMBA_CLIENT = "--service=samba-client"
00034     ALLOW_MDNS = "--service=mdns"
00035 
00036     def _get_fw_data (self, reply_handler=None, error_handler=None):
00037         try:
00038             debugprint ("%s in _get_fw_data: _fw_data is %s" %
00039                         (self, repr(self._fw_data)))
00040             if self._fw_data:
00041                 debugprint ("Using cached firewall data")
00042                 if reply_handler == None:
00043                     return self._fw_data
00044 
00045                 self._client_reply_handler (self._fw_data)
00046         except AttributeError:
00047             try:
00048                 bus = dbus.SystemBus ()
00049                 obj = bus.get_object ("org.fedoraproject.Config.Firewall",
00050                                       "/org/fedoraproject/Config/Firewall")
00051                 iface = dbus.Interface (obj,
00052                                         "org.fedoraproject.Config.Firewall")
00053                 self._firewall = iface
00054                 if reply_handler:
00055                     self._firewall.read (reply_handler=reply_handler,
00056                                          error_handler=error_handler)
00057                     return
00058 
00059                 p = self._firewall.read ()
00060                 self._fw_data = json.loads (p.encode ('utf-8'))
00061             except (dbus.DBusException, ValueError), e:
00062                 self._fw_data = (None, None)
00063                 if error_handler:
00064                     debugprint ("D-Bus exception examining firewall")
00065                     self._client_error_handler (e)
00066 
00067         return self._fw_data
00068 
00069     def read (self, reply_handler=None, error_handler=None):
00070         if reply_handler:
00071             self._client_reply_handler = reply_handler
00072             self._client_error_handler = error_handler
00073             self._get_fw_data (reply_handler=self.reply_handler,
00074                                error_handler=self.error_handler)
00075         else:
00076             self._get_fw_data ()
00077 
00078     def reply_handler (self, result):
00079         try:
00080             self._fw_data = json.loads (result.encode ('utf-8'))
00081         except ValueError, e:
00082             self.error_handler (e)
00083             return
00084 
00085         debugprint ("Firewall data obtained")
00086         self._client_reply_handler (self._fw_data)
00087 
00088     def error_handler (self, exc):
00089         debugprint ("Exception fetching firewall data")
00090         self._client_error_handler (exc)
00091 
00092     def write (self):
00093         try:
00094             self._firewall.write (json.dumps (self._fw_data[0]))
00095         except:
00096             pass
00097 
00098     def _check_any_allowed (self, search):
00099         (args, filename) = self._get_fw_data ()
00100         if filename == None: return True
00101         isect = set (search).intersection (set (args))
00102         return len (isect) != 0
00103 
00104     def add_rule (self, rule):
00105         try:
00106             (args, filename) = self._fw_data
00107         except AttributeError:
00108             (args, filename) = self._get_fw_data ()
00109         if filename == None: return
00110 
00111         args.append (rule)
00112         self._fw_data = (args, filename)
00113 
00114     def check_ipp_client_allowed (self):
00115         return self._check_any_allowed (set(["--port=631:udp",
00116                                              self.ALLOW_IPP_CLIENT]))
00117 
00118     def check_ipp_server_allowed (self):
00119         return self._check_any_allowed (set(["--port=631:tcp",
00120                                              self.ALLOW_IPP_SERVER]))
00121 
00122     def check_samba_client_allowed (self):
00123         return self._check_any_allowed (set([self.ALLOW_SAMBA_CLIENT]))
00124 
00125     def check_mdns_allowed (self):
00126         return self._check_any_allowed (set(["--port=5353:udp",
00127                                              self.ALLOW_MDNS]))