From bc02d3098671a2284b5764205b893facdeacf80e Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy <abokovoy@redhat.com>
Date: Tue, 19 Jul 2011 15:33:53 +0300
Subject: [PATCH] Modify /etc/sysconfig/network on a client when IPA manages
 hostname

https://fedorahosted.org/freeipa/ticket/1368
---
 ipa-client/ipa-install/ipa-client-install |   35 ++++++++++++++++++++++++++--
 1 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/ipa-client/ipa-install/ipa-client-install b/ipa-client/ipa-install/ipa-client-install
index 2e1a28ca087dee9eea04ccc55557a9e6e4f8ce89..0199dc69049a484d92fcace3721de0cd04dbe1ad 100755
--- a/ipa-client/ipa-install/ipa-client-install
+++ b/ipa-client/ipa-install/ipa-client-install
@@ -28,10 +28,11 @@ try:
     import logging
     import tempfile
     import getpass
+    import re
     from ipaclient import ipadiscovery
     import ipaclient.ipachangeconf
     import ipaclient.ntpconf
-    from ipapython.ipautil import run, user_input, CalledProcessError, file_exists
+    from ipapython.ipautil import run, user_input, CalledProcessError, file_exists, install_file
     from ipapython import ipautil
     from ipapython import dnsclient
     from ipapython import sysrestore
@@ -520,6 +521,30 @@ def configure_certmonger(fstore, subject_base, cli_realm, hostname, options):
         except:
             print "certmonger request for host certificate failed"
 
+def backup_and_replace_hostname(fstore, hostname):
+    # TODO: this code is for Red Hat-based systems
+    #       it need to be rewritten for cross-paltform support
+    #       so that different configuration backends would be possible
+    #       (GNU/Debian stores this information in a different place)
+    network_filename = "/etc/sysconfig/network"
+    # Backup original /etc/sysconfig/network
+    fstore.backup_file(network_filename)
+    hostname_pattern = re.compile('^HOSTNAME=')
+    temp_filename = None
+    with tempfile.NamedTemporaryFile(delete=False) as new_config:
+        temp_filename = new_config.name
+        with open(network_filename, 'r') as f:
+            for line in f:
+                if hostname_pattern.match(line):
+                    new_config.write("HOSTNAME=%s\n" % (hostname))
+                else:
+                    new_config.write(line)
+        new_config.flush()
+    # At this point new_config is closed but not removed due to 'delete=False' above
+    # Now, install the temporary file as configuration and ensure old version is available as .orig
+    # While .orig file is not used during uninstall, it is left there for administrator.
+    install_file(temp_filename, network_filename)
+
 def configure_sssd_conf(fstore, cli_realm, cli_domain, cli_server, options):
     sssdconfig = SSSDConfig.SSSDConfig()
     sssdconfig.new_config()
@@ -566,6 +591,10 @@ def configure_sssd_conf(fstore, cli_realm, cli_domain, cli_server, options):
     sssdconfig.save_domain(domain)
     sssdconfig.write("/etc/sssd/sssd.conf")
 
+    # configure /etc/sysconfig/network to contain the hostname we set.
+    if options.hostname:
+        backup_and_replace_hostname(fstore, options.hostname)
+
     return 0
 
 def resolve_ipaddress(server):
@@ -940,8 +969,8 @@ def main():
             if not options.sssd:
                 print >>sys.stderr, "Failed to configure automatic startup of the NSCD daemon"
                 print >>sys.stderr, "Caching of users/groups will not be available after reboot"
-	    else:
-		print >>sys.stderr, "Failed to disable NSCD daemon. Please disable it manually."
+            else:
+                print >>sys.stderr, "Failed to disable NSCD daemon. Please disable it manually."
 
     else:
         # this is optional service, just log
-- 
1.7.6

