From 03d7125eacb5c0fc15d416349f6ad48d22ef5acb Mon Sep 17 00:00:00 2001 From: Rob Crittenden Date: Thu, 6 Mar 2008 13:17:28 -0500 Subject: Verify that the hostname is correct in /etc/hosts Don't ignore exceptions when getting the hostname from the user 433515 --- ipa-server/ipaserver/installutils.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'ipa-server/ipaserver/installutils.py') diff --git a/ipa-server/ipaserver/installutils.py b/ipa-server/ipaserver/installutils.py index dd410404..3d09e85c 100644 --- a/ipa-server/ipaserver/installutils.py +++ b/ipa-server/ipaserver/installutils.py @@ -41,6 +41,11 @@ def get_fqdn(): except: fqdn = "" return fqdn + +def reverse_ip(ipaddr): + i = ipaddr.split('.') + i.reverse() + return '.'.join(i) def verify_fqdn(host_name): if len(host_name.split(".")) < 2 or host_name == "localhost.localdomain": @@ -65,6 +70,31 @@ def verify_fqdn(host_name): if forward != reverse: raise RuntimeError("The DNS forward record %s does not match the reverse lookup %s" % (forward, reverse)) + # Look in /etc/hosts for this IP + try: + fd = open("/etc/hosts", "r") + except: + raise RuntimeError("Unable to open /etc/hosts for reading. Check file permissions.") + + p = re.compile('([a-zA-Z0-9\.:]+)\s+([a-zA-Z0-9\.\-]+)') + while True: + line = fd.readline() + if not line: break + if len(line) > 0 and line[0] == "#": + continue + m = p.match(line) + hname = None + try: + if m.group(1) == ipaddr: + hname = m.group(2) + "." + except: + pass + if hname and hname != forward: + fd.close() + raise RuntimeError("The IP address in /etc/hosts defines the hostname as '%s' but DNS says it is '%s'. The fully-qualified hostname needs to appear on the list first in /etc/hosts" % (hname, forward)) + + fd.close() + def port_available(port): """Try to bind to a port on the wildcard host Return 1 if the port is available -- cgit