Blame SOURCES/firewalld-0.3.9-RHBZ#1058853_85f0beed.patch

36ae71
commit 85f0beed719cf123eebcf705ffa24ef7f13a4442
36ae71
Author: Jiri Popelka <jpopelka@redhat.com>
36ae71
Date:   Thu Feb 6 19:52:20 2014 +0100
36ae71
36ae71
    if Python2 then encode strings from sax parser (RHBZ#1059104,RHBZ#1058853)
36ae71
    
36ae71
    this kinda reverts 0a6900516f, but should be Python 2 & 3 compatible
36ae71
36ae71
diff --git a/src/firewall/core/io/direct.py b/src/firewall/core/io/direct.py
36ae71
index 0f786a7..7d15987 100644
36ae71
--- a/src/firewall/core/io/direct.py
36ae71
+++ b/src/firewall/core/io/direct.py
36ae71
@@ -106,7 +106,7 @@ class Direct(IO_Object):
36ae71
 
36ae71
     IMPORT_EXPORT_STRUCTURE = (
36ae71
         # chain: [ ipv, table, [ chain ] ]
36ae71
-        ( "chains", [ ( "", "", "" ), ], ),                   # a(ssas)
36ae71
+        ( "chains", [ ( "", "", "" ), ], ),                   # a(sss)
36ae71
         # rule: [ ipv, table, chain, [ priority, [ arg ] ] ]
36ae71
         ( "rules", [ ( "", "", "", 0, [ "" ] ), ], ),         # a(sssias)
36ae71
         # passthrough: [ ipv, [ [ arg ] ] ]
36ae71
diff --git a/src/firewall/core/io/icmptype.py b/src/firewall/core/io/icmptype.py
36ae71
index ab42313..a3ef5e4 100644
36ae71
--- a/src/firewall/core/io/icmptype.py
36ae71
+++ b/src/firewall/core/io/icmptype.py
36ae71
@@ -26,7 +26,7 @@ import shutil
36ae71
 
36ae71
 from firewall.config import ETC_FIREWALLD
36ae71
 from firewall.errors import *
36ae71
-from firewall import functions
36ae71
+from firewall.functions import u2b_if_py2
36ae71
 from firewall.core.io.io_object import *
36ae71
 from firewall.core.logger import log
36ae71
 
36ae71
@@ -59,9 +59,18 @@ class IcmpType(IO_Object):
36ae71
     def cleanup(self):
36ae71
         self.version = ""
36ae71
         self.short = ""
36ae71
-        self.description = ""        
36ae71
+        self.description = ""
36ae71
         del self.destination[:]
36ae71
 
36ae71
+    def encode_strings(self):
36ae71
+        """ HACK. I haven't been able to make sax parser return
36ae71
+            strings encoded (because of python 2) instead of in unicode.
36ae71
+            Get rid of it once we throw out python 2 support."""
36ae71
+        self.version = u2b_if_py2(self.version)
36ae71
+        self.short = u2b_if_py2(self.short)
36ae71
+        self.description = u2b_if_py2(self.description)
36ae71
+        self.destination = [u2b_if_py2(m) for m in self.destination]
36ae71
+
36ae71
     def _check_config(self, config, item):
36ae71
         if item == "destination":
36ae71
             for destination in config:
36ae71
@@ -107,6 +116,8 @@ def icmptype_reader(filename, path):
36ae71
         parser.parse(f)
36ae71
     del handler
36ae71
     del parser
36ae71
+    if PY2:
36ae71
+        icmptype.encode_strings()
36ae71
     return icmptype
36ae71
 
36ae71
 def icmptype_writer(icmptype, path=None):
36ae71
diff --git a/src/firewall/core/io/lockdown_whitelist.py b/src/firewall/core/io/lockdown_whitelist.py
36ae71
index 7cc416c..6221eeb 100644
36ae71
--- a/src/firewall/core/io/lockdown_whitelist.py
36ae71
+++ b/src/firewall/core/io/lockdown_whitelist.py
36ae71
@@ -28,7 +28,7 @@ from firewall.errors import *
36ae71
 from firewall.core.io.io_object import *
36ae71
 from firewall.core.logger import log
36ae71
 from firewall.functions import uniqify, checkUser, checkUid, checkCommand, \
36ae71
-    checkContext
36ae71
+                               checkContext, u2b_if_py2
36ae71
 
36ae71
 class lockdown_whitelist_ContentHandler(IO_Object_ContentHandler):
36ae71
     def __init__(self, item):
36ae71
@@ -138,6 +138,14 @@ class LockdownWhitelist(IO_Object):
36ae71
 #        del self.gids[:]
36ae71
 #        del self.groups[:]
36ae71
 
36ae71
+    def encode_strings(self):
36ae71
+        """ HACK. I haven't been able to make sax parser return
36ae71
+            strings encoded (because of python 2) instead of in unicode.
36ae71
+            Get rid of it once we throw out python 2 support."""
36ae71
+        self.commands = u2b_if_py2(self.commands)
36ae71
+        self.contexts = u2b_if_py2(self.contexts)
36ae71
+        self.users = u2b_if_py2(self.users)
36ae71
+
36ae71
     # commands
36ae71
 
36ae71
     def add_command(self, command):
36ae71
@@ -297,6 +305,8 @@ class LockdownWhitelist(IO_Object):
36ae71
         parser.parse(self.filename)
36ae71
         del handler
36ae71
         del parser
36ae71
+        if PY2:
36ae71
+            self.encode_strings()
36ae71
 
36ae71
     def write(self):
36ae71
         if os.path.exists(self.filename):
36ae71
diff --git a/src/firewall/core/io/service.py b/src/firewall/core/io/service.py
36ae71
index 1ff161c..b56550a 100644
36ae71
--- a/src/firewall/core/io/service.py
36ae71
+++ b/src/firewall/core/io/service.py
36ae71
@@ -26,7 +26,8 @@ import shutil
36ae71
 
36ae71
 from firewall.config import ETC_FIREWALLD
36ae71
 from firewall.errors import *
36ae71
-from firewall import functions
36ae71
+from firewall.functions import checkProtocol, check_address, \
36ae71
+                               checkIPnMask, checkIP6nMask, u2b_if_py2
36ae71
 from firewall.core.io.io_object import *
36ae71
 from firewall.core.logger import log
36ae71
 
36ae71
@@ -70,6 +71,17 @@ class Service(IO_Object):
36ae71
         del self.modules[:]
36ae71
         self.destination.clear()
36ae71
 
36ae71
+    def encode_strings(self):
36ae71
+        """ HACK. I haven't been able to make sax parser return
36ae71
+            strings encoded (because of python 2) instead of in unicode.
36ae71
+            Get rid of it once we throw out python 2 support."""
36ae71
+        self.version = u2b_if_py2(self.version)
36ae71
+        self.short = u2b_if_py2(self.short)
36ae71
+        self.description = u2b_if_py2(self.description)
36ae71
+        self.ports = [(u2b_if_py2(po),u2b_if_py2(pr)) for (po,pr) in self.ports]
36ae71
+        self.modules = [u2b_if_py2(m) for m in self.modules]
36ae71
+        self.destination = {u2b_if_py2(k):u2b_if_py2(v) for k,v in self.destination.items()}
36ae71
+
36ae71
     def _check_config(self, config, item):
36ae71
         if item == "ports":
36ae71
             for port in config:
36ae71
@@ -78,14 +90,14 @@ class Service(IO_Object):
36ae71
                     check_protocol(port[1])
36ae71
                 else:
36ae71
                     # only protocol
36ae71
-                    if not functions.checkProtocol(port[1]):
36ae71
+                    if not checkProtocol(port[1]):
36ae71
                         raise FirewallError(INVALID_PROTOCOL, port[1])
36ae71
 
36ae71
         elif item == "destination":
36ae71
             for destination in config:
36ae71
                 if destination not in [ "ipv4", "ipv6" ]:
36ae71
                     raise FirewallError(INVALID_DESTINATION, destination)
36ae71
-                if not functions.check_address(destination, config[destination]):
36ae71
+                if not check_address(destination, config[destination]):
36ae71
                     raise FirewallError(INVALID_ADDR, config[destination])
36ae71
 
36ae71
 # PARSER
36ae71
@@ -109,9 +121,9 @@ class service_ContentHandler(IO_Object_ContentHandler):
36ae71
             for x in [ "ipv4", "ipv6" ]:
36ae71
                 if x in attrs:
36ae71
                     s = attrs[x]
36ae71
-                    if x == "ipv4" and not functions.checkIPnMask(s):
36ae71
+                    if x == "ipv4" and not checkIPnMask(s):
36ae71
                         raise FirewallError(INVALID_DESTINATION, s)
36ae71
-                    if x == "ipv6" and not functions.checkIP6nMask(s):
36ae71
+                    if x == "ipv6" and not checkIP6nMask(s):
36ae71
                         raise FirewallError(INVALID_DESTINATION, s)
36ae71
                     self.item.destination[x] = attrs[x]
36ae71
         elif name == "module":
36ae71
@@ -134,6 +146,8 @@ def service_reader(filename, path):
36ae71
         parser.parse(f)
36ae71
     del handler
36ae71
     del parser
36ae71
+    if PY2:
36ae71
+        service.encode_strings()
36ae71
     return service
36ae71
 
36ae71
 def service_writer(service, path=None):
36ae71
diff --git a/src/firewall/core/io/zone.py b/src/firewall/core/io/zone.py
36ae71
index 8783dc6..b0d5ca2 100644
36ae71
--- a/src/firewall/core/io/zone.py
36ae71
+++ b/src/firewall/core/io/zone.py
36ae71
@@ -25,7 +25,7 @@ import shutil
36ae71
 
36ae71
 from firewall.config import ETC_FIREWALLD
36ae71
 from firewall.errors import *
36ae71
-from firewall.functions import checkIP, uniqify, max_zone_name_len
36ae71
+from firewall.functions import checkIP, uniqify, max_zone_name_len, u2b_if_py2
36ae71
 from firewall.core.base import DEFAULT_ZONE_TARGET, ZONE_TARGETS
36ae71
 from firewall.core.io.io_object import *
36ae71
 from firewall.core.rich import *
36ae71
@@ -123,9 +123,25 @@ class Zone(IO_Object):
36ae71
         del self.sources[:]
36ae71
         self.fw_config = None # to be able to check services and a icmp_blocks
36ae71
         del self.rules[:]
36ae71
-        self.combined = False        
36ae71
+        self.combined = False
36ae71
         self.applied = False
36ae71
 
36ae71
+    def encode_strings(self):
36ae71
+        """ HACK. I haven't been able to make sax parser return
36ae71
+            strings encoded (because of python 2) instead of in unicode.
36ae71
+            Get rid of it once we throw out python 2 support."""
36ae71
+        self.version = u2b_if_py2(self.version)
36ae71
+        self.short = u2b_if_py2(self.short)
36ae71
+        self.description = u2b_if_py2(self.description)
36ae71
+        self.target = u2b_if_py2(self.target)
36ae71
+        self.services = [u2b_if_py2(s) for s in self.services]
36ae71
+        self.ports = [(u2b_if_py2(po),u2b_if_py2(pr)) for (po,pr) in self.ports]
36ae71
+        self.icmp_blocks = [u2b_if_py2(i) for i in self.icmp_blocks]
36ae71
+        self.forward_ports = [(u2b_if_py2(p1),u2b_if_py2(p2),u2b_if_py2(p3),u2b_if_py2(p4)) for (p1,p2,p3,p4) in self.forward_ports]
36ae71
+        self.interfaces = [u2b_if_py2(i) for i in self.interfaces]
36ae71
+        self.sources = [u2b_if_py2(s) for s in self.sources]
36ae71
+        self.rules = [u2b_if_py2(s) for s in self.rules]
36ae71
+
36ae71
     def __getattr__(self, name):
36ae71
         if name == "rules_str":
36ae71
             rules_str = [str(rule) for rule in self.rules]
36ae71
@@ -482,6 +497,8 @@ def zone_reader(filename, path):
36ae71
         parser.parse(f)
36ae71
     del handler
36ae71
     del parser
36ae71
+    if PY2:
36ae71
+        zone.encode_strings()
36ae71
     return zone
36ae71
 
36ae71
 def zone_writer(zone, path=None):
36ae71
diff --git a/src/firewall/functions.py b/src/firewall/functions.py
36ae71
index fc70179..6dd4240 100644
36ae71
--- a/src/firewall/functions.py
36ae71
+++ b/src/firewall/functions.py
36ae71
@@ -401,11 +401,17 @@ def splitArgs(string):
36ae71
 def b2u(string):
36ae71
     """ bytes to unicode """
36ae71
     if isinstance(string, bytes):
36ae71
-        return string.decode('utf-8', 'replace')
36ae71
+        return string.decode('UTF-8', 'replace')
36ae71
     return string
36ae71
 
36ae71
 def u2b(string):
36ae71
     """ unicode to bytes """
36ae71
     if not isinstance(string, bytes):
36ae71
-        return string.encode('utf-8', 'replace')
36ae71
+        return string.encode('UTF-8', 'replace')
36ae71
+    return string
36ae71
+
36ae71
+def u2b_if_py2(string):
36ae71
+    """ unicode to bytes only if Python 2"""
36ae71
+    if PY2 and isinstance(string, unicode):
36ae71
+            return string.encode('UTF-8', 'replace')
36ae71
     return string