Verify that the hostname is correct in /etc/hosts

Don't ignore exceptions when getting the hostname from the user

433515
This commit is contained in:
Rob Crittenden 2008-03-06 13:17:28 -05:00
parent 546155c3af
commit 03d7125eac
2 changed files with 32 additions and 3 deletions

View File

@ -123,9 +123,8 @@ def read_host_name(host_default):
host_name = host_input
try:
verify_fqdn(host_name)
except:
host_name = ""
continue
except Exception, e:
raise e
else:
host_ok = True
return host_name

View File

@ -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