From f255d068c5d4251762b83467d1927ab72da57482 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 28 Sep 2012 18:39:46 -0700 Subject: Ensure that the directory where the sudoers file is being added actually exists before it is written into and ensure that the directory is included in the main sudoers file. --- cloudinit/distros/__init__.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 86ab557c..11422644 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -296,6 +296,38 @@ class Distro(object): return True + def ensure_sudo_dir(self, path, sudo_base='/etc/sudoers'): + # Ensure the dir is included and that + # it actually exists as a directory + sudoers_contents = '' + if os.path.exists(sudo_base): + sudoers_contents = util.load_file(sudo_base) + found_include = False + for line in sudoers_contents.splitlines(): + line = line.strip() + mtch = re.search(r"#includedir\s+(.*)$", line) + if not mtch: + continue + included_dir = mtch.group(1).strip() + if not included_dir: + continue + included_dir = os.path.abspath(included_dir) + if included_dir == path: + found_include = True + break + if not found_include: + sudoers_contents += "\n#includedir %s\n" % (path) + try: + if not os.path.exists(sudo_base): + util.write_file(sudo_base, sudoers_contents, 0440) + else: + with open(sudo_base, 'a') as f: + f.write(sudoers_contents) + except IOError as e: + util.logexc(LOG, "Failed to write %s" % sudo_base, e) + raise e + util.ensure_dir(path, 0440) + def write_sudo_rules(self, user, rules, @@ -311,9 +343,10 @@ class Distro(object): content += "%s %s\n" % (user, rule) content += "\n" + self.ensure_sudo_dir(os.path.dirname(sudo_file)) + if not os.path.exists(sudo_file): util.write_file(sudo_file, content, 0440) - else: try: with open(sudo_file, 'a') as f: -- cgit v1.2.3 From 9e7320df7a6fb6f1e9a5401735c471467b1fe365 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 28 Sep 2012 20:32:00 -0700 Subject: Dir should be 0755, not 0440 --- cloudinit/distros/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 11422644..301eeed2 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -326,7 +326,7 @@ class Distro(object): except IOError as e: util.logexc(LOG, "Failed to write %s" % sudo_base, e) raise e - util.ensure_dir(path, 0440) + util.ensure_dir(path, 0755) def write_sudo_rules(self, user, -- cgit v1.2.3 From 9a252b3a41ad757587cba729b31079c04b253faa Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 28 Sep 2012 21:04:00 -0700 Subject: Update the log statement used here to be a little more relevant. --- cloudinit/distros/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 301eeed2..500147c6 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -248,7 +248,7 @@ class Distro(object): if util.is_user(name): LOG.warn("User %s already exists, skipping." % name) else: - LOG.debug("Creating name %s" % name) + LOG.debug("Adding user named %s", name) try: util.subp(adduser_cmd, logstring=x_adduser_cmd) except Exception as e: -- cgit v1.2.3 From e2b85e2087b796c7a130ee20f688dbd5d161739f Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Sun, 30 Sep 2012 13:47:00 -0700 Subject: Ensure that the include dir starts the line and is not a part of a comment or other part of the line. --- cloudinit/distros/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 500147c6..cdae1add 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -305,10 +305,10 @@ class Distro(object): found_include = False for line in sudoers_contents.splitlines(): line = line.strip() - mtch = re.search(r"#includedir\s+(.*)$", line) - if not mtch: + include_match = re.search(r"^#includedir\s+(.*)$", line) + if not include_match: continue - included_dir = mtch.group(1).strip() + included_dir = include_match.group(1).strip() if not included_dir: continue included_dir = os.path.abspath(included_dir) -- cgit v1.2.3