summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Kosek <mkosek@redhat.com>2011-10-13 12:15:41 +0200
committerRob Crittenden <rcritten@redhat.com>2011-10-13 00:54:45 -0400
commit363c23a37c3631a0fb16df6b88bd5138677b818a (patch)
treeba48083177509119af8af8a74043c4cbfb923519
parent77cc5e024601ae7be6ce706499d5f403c8b71408 (diff)
downloadfreeipa-363c23a37c3631a0fb16df6b88bd5138677b818a.zip
freeipa-363c23a37c3631a0fb16df6b88bd5138677b818a.tar.gz
freeipa-363c23a37c3631a0fb16df6b88bd5138677b818a.tar.xz
Check /etc/hosts file in ipa-server-install
There may already be a record in /etc/hosts for chosen IP address which may not be detected under some circumstances. Make sure that /etc/hosts is checked properly. https://fedorahosted.org/freeipa/ticket/1923
-rwxr-xr-xinstall/tools/ipa-server-install22
-rw-r--r--ipaserver/install/bindinstance.py2
-rw-r--r--ipaserver/install/installutils.py28
3 files changed, 42 insertions, 10 deletions
diff --git a/install/tools/ipa-server-install b/install/tools/ipa-server-install
index 6d64388..76d5f2f 100755
--- a/install/tools/ipa-server-install
+++ b/install/tools/ipa-server-install
@@ -796,11 +796,33 @@ def main():
ip = options.ip_address
+ ip_add_to_hosts = False
if ip is None:
ip = read_ip_address(host_name, fstore)
logging.debug("read ip_address: %s\n" % str(ip))
+ ip_add_to_hosts = True
+
ip_address = str(ip)
+ # check /etc/hosts sanity, add a record when needed
+ hosts_record = record_in_hosts(ip_address)
+
+ if hosts_record is None:
+ if ip_add_to_hosts:
+ print "Adding ["+ip_address+" "+host_name+"] to your /etc/hosts file"
+ fstore.backup_file("/etc/hosts")
+ add_record_to_hosts(ip_address, host_name)
+ else:
+ primary_host = hosts_record[1][0]
+ if primary_host != host_name:
+ print >>sys.stderr, "Error: there is already a record in /etc/hosts for IP address %s:" \
+ % ip_address
+ print >>sys.stderr, hosts_record[0], " ".join(hosts_record[1])
+ print >>sys.stderr, "Chosen hostname %s does not match configured canonical hostname %s" \
+ % (host_name, primary_host)
+ print >>sys.stderr, "Please fix your /etc/hosts file and restart the installation."
+ return 1
+
if options.reverse_zone and not bindinstance.verify_reverse_zone(options.reverse_zone, ip):
sys.exit(1)
diff --git a/ipaserver/install/bindinstance.py b/ipaserver/install/bindinstance.py
index 8dbcdbd..ddf5497 100644
--- a/ipaserver/install/bindinstance.py
+++ b/ipaserver/install/bindinstance.py
@@ -394,7 +394,7 @@ class BindInstance(service.Service):
# get a connection to the DS
self.ldap_connect()
- if not installutils.record_in_hosts(self.ip_address, self.fqdn):
+ if installutils.record_in_hosts(self.ip_address, self.fqdn) is None:
installutils.add_record_to_hosts(self.ip_address, self.fqdn)
if not dns_container_exists(self.fqdn, self.suffix, realm=self.realm,
diff --git a/ipaserver/install/installutils.py b/ipaserver/install/installutils.py
index ea629e5..ca9a826 100644
--- a/ipaserver/install/installutils.py
+++ b/ipaserver/install/installutils.py
@@ -203,7 +203,18 @@ def verify_fqdn(host_name, no_host_dns=False, local_hostname=True):
else:
print "Warning: Hostname (%s) not found in DNS" % host_name
-def record_in_hosts(ip, host_name, file="/etc/hosts"):
+def record_in_hosts(ip, host_name=None, file="/etc/hosts"):
+ """
+ Search record in /etc/hosts - static table lookup for hostnames
+
+ In case of match, returns a tuple of ip address and a list of
+ hostname aliases
+ When no record is matched, None is returned
+
+ :param ip: IP address
+ :param host_name: Optional hostname to search
+ :param file: Optional path to the lookup table
+ """
hosts = open(file, 'r').readlines()
for line in hosts:
line = line.rstrip('\n')
@@ -217,13 +228,17 @@ def record_in_hosts(ip, host_name, file="/etc/hosts"):
if hosts_ip != ip:
continue
- if host_name in names:
- return True
+ if host_name is not None:
+ if host_name in names:
+ return (hosts_ip, names)
+ else:
+ return None
+ return (hosts_ip, names)
except IndexError:
print "Warning: Erroneous line '%s' in %s" % (line, file)
continue
- return False
+ return None
def add_record_to_hosts(ip, host_name, file="/etc/hosts"):
hosts_fd = open(file, 'r+')
@@ -242,11 +257,6 @@ def read_ip_address(host_name, fstore):
else:
break
- ip = str(ip_parsed)
- print "Adding ["+ip+" "+host_name+"] to your /etc/hosts file"
- fstore.backup_file("/etc/hosts")
- add_record_to_hosts(ip, host_name)
-
return ip_parsed
def read_dns_forwarders():