Fixes handling of empty lines, erroneous lines and comments in /etc/hosts.

https://fedorahosted.org/freeipa/ticket/971

Honza
>From 1313015b03fb9174a0d911cf81bf4968cb3f693f Mon Sep 17 00:00:00 2001
From: Jan Cholasta <jchol...@redhat.com>
Date: Tue, 15 Feb 2011 17:51:18 +0100
Subject: [PATCH] Fix handling of /etc/hosts

ticket 971
---
 ipaserver/install/installutils.py |   21 ++++++++++++++++-----
 1 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/ipaserver/install/installutils.py b/ipaserver/install/installutils.py
index 563333b..63e6019 100644
--- a/ipaserver/install/installutils.py
+++ b/ipaserver/install/installutils.py
@@ -159,13 +159,24 @@ def verify_ip_address(ip):
 def record_in_hosts(ip, host_name, file="/etc/hosts"):
     hosts = open(file, 'r').readlines()
     for line in hosts:
-        hosts_ip = line.split()[0]
-        if hosts_ip != ip:
+        if line[-1] == '\n':
+            line = line[:-1]
+
+        fields = line.partition('#')[0].split()
+        if len(fields) == 0:
             continue
 
-        names = line.split()[1:]
-        if host_name in names:
-            return True
+        try:
+            hosts_ip = fields[0]
+            names = fields[1:]
+
+            if hosts_ip != ip:
+                continue
+            if host_name in names:
+                return True
+        except IndexError:
+            print "Warning: Erroneous line '{}' in {}".format(line, file)
+            continue            
 
     return False
 
-- 
1.7.4

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to