[Ovirt-devel] [PATCH server] verify hostname is not on the loopback entry in /etc/hosts
David Lutterkort
lutter at redhat.com
Wed Apr 1 23:46:07 UTC 2009
On Wed, 2009-03-25 at 15:21 -0400, Joey Boggs wrote:
> +# verify hostname is not on the loopback line in /etc/hosts to prevent kerberos problems
> +hostname = `hostname`
> +hostsfile = File.new("/etc/hosts", "r")
> +while (line = hostsfile.gets)
> + if line =~ /127.0.0.1/ && line.include?(hostname.chomp)
> + if hostname !~ /localhost.localdomain/
> + @cli.say("\nHostname must not be on the loopback 127.0.0.1 line in /etc/hosts")
> + @cli.say("#{line}")
> + exit(0)
> + end
> + end
> +end
> +hostsfile.close
This can produce false positives in a number of ways: (1) it will get
confused by comments:
# We used to have, but that's wrong
# 127.0.0.1 host.exmaple.com
127.0.0.1 localhost.localdomain
It also gets confused if hostname is 'host.example.com' and the
127.0.0.1 line has an alias 'myhost.example.com'.
Finally, (but that should happen throughout the installer), the
installer shouldn't exit with status 0 when there was an error. It would
be good to have a convention like status == 0 when installer finished
successfully, status == 1 when an error happened, status == 2 when user
exited installer (e.g., by answering 'no' to some question)
A more robust implementation of the above would be:
aug = Augeas::init(nil, nil, 0)
hosts = "/files/etc/hosts/*"
paths = aug.match("#{hosts}/canonical[../ipaddr = '127.0.0.1']") +
aug.match("#{hosts}/alias[../ipaddr = '127.0.0.1']")
paths.each do |p|
name = aug.get(p)
if hostname == name
@cli.say("\nHostname must not be on the loopback 127.0.0.1 line in /etc/hosts")
if (prompt_yes_no("Should I fix that ?", :default => "n") == "y")
aug.rm(p)
aug.save
end
end
end
David
More information about the ovirt-devel
mailing list