[PATCH 2/2 rhel7-branch] Text network spoke: more strict ipv6 address input checking (#909299)

Radek Vykydal rvykydal at redhat.com
Thu Aug 22 13:20:11 UTC 2013


I think most important is checking of typos in "auto", "dhcp",
"ignore", but let's check ipv6 properly too.
---
 pyanaconda/network.py               |  8 ++++++++
 pyanaconda/ui/tui/spokes/network.py | 22 ++++++++++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/pyanaconda/network.py b/pyanaconda/network.py
index 182ed07..ca92151 100644
--- a/pyanaconda/network.py
+++ b/pyanaconda/network.py
@@ -33,6 +33,7 @@ import time
 import tempfile
 import simpleconfig
 import re
+import IPy
 from flags import flags
 from simpleconfig import IfcfgFile
 import urlgrabber.grabber
@@ -75,6 +76,13 @@ def setup_ifcfg_log():
 
     ifcfglog = logging.getLogger("ifcfg")
 
+def check_ip_address(address, version=None):
+    try:
+        _ip, ver = IPy.parseAddress(address)
+    except ValueError:
+        return False
+    if version and version == ver:
+        return True
 
 def sanityCheckHostname(hostname):
     """
diff --git a/pyanaconda/ui/tui/spokes/network.py b/pyanaconda/ui/tui/spokes/network.py
index a479cc7..1b1f8aa 100644
--- a/pyanaconda/ui/tui/spokes/network.py
+++ b/pyanaconda/ui/tui/spokes/network.py
@@ -236,6 +236,24 @@ class NetworkSpoke(EditTUISpoke):
 
 RE_IPV4="^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"
 
+class Fake_RE_IPV6(object):
+    def __init__(self, allow_prefix=False, whitelist=None):
+        self.whitelist = whitelist or []
+        self.allow_prefix = allow_prefix
+    def match(self, value):
+        if value in self.whitelist:
+            return True
+        addr, _slash, prefix = value.partition("/")
+        if prefix:
+            if not self.allow_prefix:
+                return False
+            try:
+                if not 1 <= int(prefix) <= 128:
+                    return False
+            except ValueError:
+                return False
+        return network.check_ip_address(addr, version=6)
+
 class ConfigureNetworkSpoke(EditTUISpoke):
     """ Spoke to set various configuration options for net devices. """
     title = _("Device configuration")
@@ -245,8 +263,8 @@ class ConfigureNetworkSpoke(EditTUISpoke):
         Entry(_('IPv4 address or %s for DHCP' % '"dhcp"'), "ip", re.compile(RE_IPV4+"|dhcp$"), True),
         Entry(_("IPv4 netmask"), "netmask", re.compile(RE_IPV4), True),
         Entry(_("IPv4 gateway"), "gateway", re.compile(RE_IPV4), True),
-        Entry(_('IPv6 address or %s for automatic, %s for DHCP, %s to turn off') % ('"auto"', '"dhcp"', '"ignore"'), "ipv6", re.compile(".*:|^auto$|^ignore$|^dhcp$"), True),
-        Entry(_("IPv6 default gateway"), "ipv6gateway", re.compile(".*$"), True),
+        Entry(_('IPv6 address or %s for automatic, %s for DHCP, %s to turn off') % ('"auto"', '"dhcp"', '"ignore"'), "ipv6", Fake_RE_IPV6(allow_prefix=True, whitelist=["auto", "dhcp", "ignore"]), True),
+        Entry(_("IPv6 default gateway"), "ipv6gateway", Fake_RE_IPV6(), True),
         Entry(_("Nameservers (comma separated)"), "nameserver", re.compile(".*$"), True),
         Entry(_("Connect automatically after reboot"), "onboot", EditTUISpoke.CHECK, True),
         Entry(_("Apply configuration in installer"), "_apply", EditTUISpoke.CHECK, True),
-- 
1.7.11.7



More information about the anaconda-patches mailing list