Fix: support dns_search and dns_options for all address family

`dns_search` and `dns_options` should not be specific to the address
family. Previously, `dns_search` and `dns_options` were only supported
for IPv4 nameservers, so we also need to support `dns_search` and
`dns_options` for IPv6 nameservers.

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
This commit is contained in:
Wen Liang 2021-03-28 17:36:45 -04:00 committed by Gris Ge
parent 2444e27cce
commit 93e509b533
5 changed files with 165 additions and 17 deletions

View file

@ -534,8 +534,12 @@ class ArgValidator_DictIP(ArgValidatorDict):
r"^attempts:([1-9]\d*|0)$",
r"^debug$",
r"^edns0$",
r"^inet6$",
r"^ip6-bytestring$",
r"^ip6-dotint$",
r"^ndots:([1-9]\d*|0)$",
r"^no-check-names$",
r"^no-ip6-dotint$",
r"^no-reload$",
r"^no-tld-query$",
r"^rotate$",
@ -1759,6 +1763,13 @@ class ArgValidator_ListConnections(ArgValidatorList):
VALIDATE_ONE_MODE_INITSCRIPTS = "initscripts"
def validate_connection_one(self, mode, connections, idx):
def _ipv4_enabled(connection):
has_addrs4 = any(
address["family"] == socket.AF_INET
for address in connection["ip"]["address"]
)
return connection["ip"]["dhcp4"] or has_addrs4
connection = connections[idx]
if "type" not in connection:
return
@ -1832,3 +1843,45 @@ class ArgValidator_ListConnections(ArgValidatorList):
idx,
"ip.ipv6_disabled is not supported by initscripts.",
)
# Setting ip.dns is not allowed when corresponding IP method for that
# nameserver is disabled
for nameserver in connection["ip"]["dns"]:
if nameserver["family"] == socket.AF_INET and not _ipv4_enabled(connection):
raise ValidationError.from_connection(
idx,
"IPv4 needs to be enabled to support IPv4 nameservers.",
)
if (
nameserver["family"] == socket.AF_INET6
and connection["ip"]["ipv6_disabled"]
):
raise ValidationError.from_connection(
idx,
"IPv6 needs to be enabled to support IPv6 nameservers.",
)
# when IPv4 and IPv6 are disabled, setting ip.dns_options or
# ip.dns_search is not allowed
if connection["ip"]["dns_search"] or connection["ip"]["dns_options"]:
if not _ipv4_enabled(connection) and connection["ip"]["ipv6_disabled"]:
raise ValidationError.from_connection(
idx,
"Setting 'dns_search' or 'dns_options' is not allowed when "
"both IPv4 and IPv6 are disabled.",
)
# DNS options 'inet6', 'ip6-bytestring', 'ip6-dotint', 'no-ip6-dotint' are only
# supported for IPv6 configuration, so raise errors when IPv6 is disabled
if any(
option in connection["ip"]["dns_options"]
for option in [
"inet6",
"ip6-bytestring",
"ip6-dotint",
"no-ip6-dotint",
]
):
if connection["ip"]["ipv6_disabled"]:
raise ValidationError.from_connection(
idx,
"Setting DNS options 'inet6', 'ip6-bytestring', 'ip6-dotint', "
"'no-ip6-dotint' is not allowed when IPv6 is disabled.",
)