fix[utils]: Fix name_is_resolvable()

- `gethostbyaddr()` works with IPv4 and IPv6, but requires a valid DNS-PTR-RR.
- `gethostbyname()` only works with IPv4

Use `getaddrinfo()` instead which works with both IPv4 and IPv6 and also resolved the name in the right direction only requiting a DNS-A- or AAAA-RR.

This should resolve several MRs proposing the wrong fix:
- https://github.com/univention/univention-domain-join/pull/4
- https://github.com/univention/univention-domain-join/pull/2
- https://forge.univention.org/bugzilla/show_bug.cgi?id=52726

Bug #52726
This commit is contained in:
Philipp Hahn 2022-09-19 15:05:57 +02:00
parent acac9bdd01
commit 229cb6fd95

View File

@ -47,7 +47,6 @@ def execute_as_root(func):
def name_is_resolvable(name):
try:
socket.gethostbyaddr(name)
return True
return bool(socket.getaddrinfo(name, 22, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP))
except Exception:
return False