From 6bb04f7c38aaee66a4bfd974c4d7bb9348345e67 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Wed, 23 May 2018 18:20:04 -0700 Subject: [PATCH] Workaround iptables save() quoting issue This fixes #240. --- iptc/ip4tc.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 29d77ea..545d9fb 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -387,11 +387,18 @@ def _save(self, name, ip): else: return self._get_all_values(buf) + def _unquote_value(self, value): + value = value.replace("\\\\", "\\") + value = value.replace("\\\"", "\"") + value = value.replace("\\'", "'") + return value + def _get_all_values(self, buf): table = {} # variable -> (value, inverted) res = re.findall(IPTCModule.pattern, buf) for x in res: value, invert = (x[3], x[0] or x[2]) + value = self._unquote_value(value) table[x[1].replace("-", "_")] = "%s%s" % (invert and "!" or "", value) return table @@ -403,6 +410,7 @@ def _get_value(self, buf, name): table[x[1]] = (x[3], x[0] or x[2]) try: value, invert = table[name] + value = self._unquote_value(value) return "%s%s" % (invert and "!" or "", value) except KeyError: return None @@ -420,7 +428,7 @@ def get_all_parameters(self): res.reverse() inv = False while len(res) > 0: - x = res.pop() + x = self._unquote_value(res.pop()) if x == '!': # Next parameter is negated. inv = True