ac7d03
From 68d97e2beca1ee3b398fc5f0d3ed70aa8b69e732 Mon Sep 17 00:00:00 2001
ac7d03
From: David Kupka <dkupka@redhat.com>
ac7d03
Date: Tue, 11 Apr 2017 17:35:30 +0200
ac7d03
Subject: [PATCH] ipapython.ipautil.run: Add option to set umask before
ac7d03
 executing command
ac7d03
ac7d03
https://pagure.io/freeipa/issue/6831
ac7d03
ac7d03
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
ac7d03
Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
ac7d03
---
ac7d03
 ipapython/ipautil.py | 43 +++++++++++++++++++++++--------------------
ac7d03
 1 file changed, 23 insertions(+), 20 deletions(-)
ac7d03
ac7d03
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
ac7d03
index cd66328e6c9a0f69e6f83582a9d288ac239c5be3..317fc225b722ad3ce2f4b9d92822b4f19d49adb9 100644
ac7d03
--- a/ipapython/ipautil.py
ac7d03
+++ b/ipapython/ipautil.py
ac7d03
@@ -309,7 +309,7 @@ class _RunResult(collections.namedtuple('_RunResult',
ac7d03
 def run(args, stdin=None, raiseonerr=True, nolog=(), env=None,
ac7d03
         capture_output=False, skip_output=False, cwd=None,
ac7d03
         runas=None, suplementary_groups=[],
ac7d03
-        capture_error=False, encoding=None, redirect_output=False):
ac7d03
+        capture_error=False, encoding=None, redirect_output=False, umask=None):
ac7d03
     """
ac7d03
     Execute an external command.
ac7d03
 
ac7d03
@@ -345,6 +345,7 @@ def run(args, stdin=None, raiseonerr=True, nolog=(), env=None,
ac7d03
         error_output, and (if it's not bytes) stdin.
ac7d03
         If None, the current encoding according to locale is used.
ac7d03
     :param redirect_output: Redirect (error) output to standard (error) output.
ac7d03
+    :param umask: Set file-creation mask before running the command.
ac7d03
 
ac7d03
     :return: An object with these attributes:
ac7d03
 
ac7d03
@@ -416,25 +417,27 @@ def run(args, stdin=None, raiseonerr=True, nolog=(), env=None,
ac7d03
     root_logger.debug('Starting external process')
ac7d03
     root_logger.debug('args=%s' % arg_string)
ac7d03
 
ac7d03
-    preexec_fn = None
ac7d03
-    if runas is not None:
ac7d03
-        pent = pwd.getpwnam(runas)
ac7d03
-
ac7d03
-        suplementary_gids = [
ac7d03
-            grp.getgrnam(group).gr_gid for group in suplementary_groups
ac7d03
-        ]
ac7d03
-
ac7d03
-        root_logger.debug('runas=%s (UID %d, GID %s)', runas,
ac7d03
-            pent.pw_uid, pent.pw_gid)
ac7d03
-        if suplementary_groups:
ac7d03
-            for group, gid in zip(suplementary_groups, suplementary_gids):
ac7d03
-                root_logger.debug('suplementary_group=%s (GID %d)', group, gid)
ac7d03
-
ac7d03
-        preexec_fn = lambda: (
ac7d03
-            os.setgroups(suplementary_gids),
ac7d03
-            os.setregid(pent.pw_gid, pent.pw_gid),
ac7d03
-            os.setreuid(pent.pw_uid, pent.pw_uid),
ac7d03
-        )
ac7d03
+    def preexec_fn():
ac7d03
+        if runas is not None:
ac7d03
+            pent = pwd.getpwnam(runas)
ac7d03
+
ac7d03
+            suplementary_gids = [
ac7d03
+                grp.getgrnam(group).gr_gid for group in suplementary_groups
ac7d03
+            ]
ac7d03
+
ac7d03
+            root_logger.debug('runas=%s (UID %d, GID %s)', runas,
ac7d03
+                              pent.pw_uid, pent.pw_gid)
ac7d03
+            if suplementary_groups:
ac7d03
+                for group, gid in zip(suplementary_groups, suplementary_gids):
ac7d03
+                    root_logger.debug('suplementary_group=%s (GID %d)',
ac7d03
+                                      group, gid)
ac7d03
+
ac7d03
+            os.setgroups(suplementary_gids)
ac7d03
+            os.setregid(pent.pw_gid, pent.pw_gid)
ac7d03
+            os.setreuid(pent.pw_uid, pent.pw_uid)
ac7d03
+
ac7d03
+        if umask:
ac7d03
+            os.umask(umask)
ac7d03
 
ac7d03
     try:
ac7d03
         p = subprocess.Popen(args, stdin=p_in, stdout=p_out, stderr=p_err,
ac7d03
-- 
ac7d03
2.9.4
ac7d03