When --hostname is given, client installation tries to write to /etc/sysconfig/network, which is not guaranteed to exist. Create it if it doesn't exist and we need to write to it.

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

--
PetrĀ³


From 826700fe8a695f3dffdd83398cebd3526f0a1640 Mon Sep 17 00:00:00 2001
From: Petr Viktorin <pvikt...@redhat.com>
Date: Thu, 19 Jul 2012 09:07:23 -0400
Subject: [PATCH] Create /etc/sysconfig/network if it doesn't exist

When the --hostname option is given to ipa-client-install, we
write HOSTNAME to /etc/sysconfig/network. When that file didn't exist,
the installer crashed.

Create the file if it doesn't exist and we need to write to it.

https://fedorahosted.org/freeipa/ticket/2840
---
 ipapython/ipautil.py         |    4 +++-
 ipapython/platform/redhat.py |   16 +++++++++++++---
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index 22c8e2937a731252ffcac77c9bd7e0bf636e61ff..d4cbcb3b437e26521f5abd50c0ba3c23e0b5f24f 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -990,13 +990,15 @@ def add_options(config, replacevars, appendvars, oldvars):
 
     return old_values
 
-def backup_config_and_replace_variables(fstore, filepath, replacevars=dict(), appendvars=dict()):
+def backup_config_and_replace_variables(
+        fstore, filepath, replacevars=dict(), appendvars=dict()):
     """
     Take a key=value based configuration file, back up it, and
     write new version with certain values replaced or appended
 
     All (key,value) pairs from replacevars and appendvars that
     were not found in the configuration file, will be added there.
+    The file must exist before this function is called.
 
     It is responsibility of a caller to ensure that replacevars and
     appendvars do not overlap.
diff --git a/ipapython/platform/redhat.py b/ipapython/platform/redhat.py
index d3c23ab0debbcfa58eefa9607fe5cac8fcbf592b..3f35cfcc9607bd0c9a05659d936a9903ec198f1b 100644
--- a/ipapython/platform/redhat.py
+++ b/ipapython/platform/redhat.py
@@ -24,6 +24,8 @@
 import stat
 import sys
 import socket
+import stat
+
 from ipapython import ipautil
 from ipapython.platform import base
 from ipalib import api
@@ -187,10 +189,18 @@ def backup_and_replace_hostname(fstore, statestore, hostname):
     except ipautil.CalledProcessError, e:
         print >>sys.stderr, "Failed to set this machine hostname to %s (%s)." % (hostname, str(e))
     replacevars = {'HOSTNAME':hostname}
-    old_values = ipautil.backup_config_and_replace_variables(fstore,
-                                                          "/etc/sysconfig/network",
-                                                          replacevars=replacevars)
+
+    filepath = '/etc/sysconfig/network'
+    if not os.path.exists(filepath):
+        # file doesn't exist; create it with correct ownership & mode
+        open(filepath, 'a').close()
+        os.chmod(filepath,
+            stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
+        os.chown(filepath, 0, 0)
+    old_values = ipautil.backup_config_and_replace_variables(
+        fstore, filepath, replacevars=replacevars)
     restore_context("/etc/sysconfig/network")
+
     if 'HOSTNAME' in old_values:
         statestore.backup_state('network', 'hostname', old_values['HOSTNAME'])
     else:
-- 
1.7.10.4

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

Reply via email to