diff -r 7edd6a8cbf3e ipa-server/ipa-configd/ipaconfigd/Makefile.am --- a/ipa-server/ipa-configd/ipaconfigd/Makefile.am Fri Jan 25 11:56:09 2008 +0000 +++ b/ipa-server/ipa-configd/ipaconfigd/Makefile.am Fri Jan 25 12:07:46 2008 +0000 @@ -13,5 +13,6 @@ ipaconfigd_PYTHON = \ __init__.py \ config.py \ ldapcache.py \ + hostname.py \ main.py \ $(NULL) diff -r 7edd6a8cbf3e ipa-server/ipa-configd/ipaconfigd/hostname.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ipa-server/ipa-configd/ipaconfigd/hostname.py Fri Jan 25 12:07:46 2008 +0000 @@ -0,0 +1,115 @@ +# +# Copyright (C) 2008 Red Hat +# see file 'COPYING' for use and warranty information +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; version 2 only +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +import socket +from ipaserver import installutils +from ipaserver import sysrestore + +def dprint (fmt, *args): + print fmt % args + pass + +def resolve_host(hostname): + ip = "" + try: + ip = socket.gethostbyname(hostname) + + if ip == "127.0.0.1" or ip == "::1": + print "The hostname resolves to the localhost address (127.0.0.1/::1)" + print "Please change your /etc/hosts file so that the hostname" + print "resolves to the ip address of your network interface." + print "The KDC service does not listen on localhost" + print "" + print "Please fix your /etc/hosts file and restart the setup program" + return None + + except: + print "Unable to lookup the IP address of the provided host" + return ip + +def verify_ip_address(ip): + is_ok = True + try: + socket.inet_pton(socket.AF_INET, ip) + except: + try: + socket.inet_pton(socket.AF_INET6, ip) + except: + print "Unable to verify IP address" + is_ok = False + return is_ok + +def check_hostname(hostname, ip_address): + # check the hostname is correctly configured, it must be as the kldap + # utilities just use the hostname as returned by gethostbyname to set + # up some of the standard entries + + try: + installutils.verify_fqdn(hostname) + except RuntimeError, e: + print "%s" % e + return False + + # Check we have a public IP that is associated with the hostname + add_host = False + ip = resolve_host(hostname) + if not ip: + ip = ip_address + add_host = True + + if not verify_ip_address(ip): + return False + + if ip_address != ip: + print "Error: the hostname resolves to an IP address that is different" + print "from the one provided on the command line. Please fix your DNS" + print "or /etc/hosts file and restart the installation." + return False + + if add_host: + print "Adding [" + ip + " " + hostname + "] to your /etc/hosts file" + sysrestore.backup_file("/etc/hosts") + hosts_fd = open('/etc/hosts', 'r+') + hosts_fd.seek(0, 2) + hosts_fd.write(ip + '\t' + hostname + ' ' + hostname[:hostname.find('.')] + '\n') + hosts_fd.close() + + return True + +def handle_directory_change(cache, type, dn, prev_dn, attrs, prev_attrs): + dprint("Handling change to '%s'" % dn) + + if dn.lower() != "cn=config,dc=ipa": + return + + def non_empty_attr(attrs, key): + return attrs.has_key(key) and attrs[key] and attrs[key][0] + + if not (non_empty_attr(attrs, "ipaHostName") and + non_empty_attr(attrs, "ipaIpAddress")): + return + + hostname = attrs["ipaHostName"][0] + ip_address = attrs["ipaIpAddress"][0] + + dprint("Got ipaHostName '%s' and ipaIpAddress '%s'" % (hostname, ip_address)) + + if not check_hostname(hostname, ip_address): + return + + installutils.set_hostname(hostname) diff -r 7edd6a8cbf3e ipa-server/ipa-configd/ipaconfigd/main.py --- a/ipa-server/ipa-configd/ipaconfigd/main.py Fri Jan 25 11:56:09 2008 +0000 +++ b/ipa-server/ipa-configd/ipaconfigd/main.py Fri Jan 25 12:07:46 2008 +0000 @@ -26,6 +26,7 @@ import os.path import os.path import optparse import ldapcache +import hostname from config import * def dprint (fmt, *args): @@ -130,8 +131,12 @@ def main (args): if options.debug: cache.add_monitor(debug_directory_change) + cache.add_monitor(hostname.handle_directory_change) + while True: cache.run() + + cache.remove_monitor(hostname.handle_directory_change) if options.debug: cache.remove_monitor(debug_directory_change)