[master 1/1] Allow a user's primary group to be created in --groups (#1279041)
dashea
installerbot-noreply at redhat.com
Tue Nov 10 19:54:26 UTC 2015
From: David Shea <dshea at redhat.com>
If, for example, a user is created with
--gid=1000 --groups='newgroup(1000)'
recognize that the user's GID should be that of the newly created group
instead of trying to create a separate user group and crashing.
---
pyanaconda/users.py | 22 +++++++++++++++++-----
tests/pyanaconda_tests/user_create_test.py | 15 +++++++++++++++
2 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index 8fa997f..35c6d40 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -270,10 +270,23 @@ def createUser(self, user_name, *args, **kwargs):
args = ["-R", root]
- # If a specific gid is requested, and that gid does not exist,
- # create the user group with that GID.
+ # Split the groups argument into a list of (username, gid or None) tuples
+ # the gid, if any, is a string since that makes things simpler
+ group_gids = [GROUPLIST_FANCY_PARSE.match(group).groups()
+ for group in kwargs.get("groups", [])]
+
+ # If a specific gid is requested:
+ # - check if a group already exists with that GID. i.e., the user's
+ # GID should refer to a system group, such as users. If so, just set
+ # the GID.
+ # - check if a new group is requested with that GID. If so, set the GID
+ # and let the block below create the actual group.
+ # - if neither of those are true, create a new user group with the requested
+ # GID
+ # otherwise use -U to create a new user group with the next available GID.
if kwargs.get("gid", None):
- if not self._getgrgid(kwargs['gid'], root):
+ if not self._getgrgid(kwargs['gid'], root) and \
+ not any(filter(lambda x: x[1] == str(kwargs['gid']), group_gids)):
self.createGroup(user_name, gid=kwargs['gid'], root=root)
args.extend(['-g', str(kwargs['gid'])])
@@ -282,8 +295,7 @@ def createUser(self, user_name, *args, **kwargs):
# If any requested groups do not exist, create them.
group_list = []
- for group in kwargs.get("groups", []):
- group_name, gid = GROUPLIST_FANCY_PARSE.match(group).groups()
+ for group_name, gid in group_gids:
existing_group = self._getgrnam(group_name, root)
# Check for a bad GID request
diff --git a/tests/pyanaconda_tests/user_create_test.py b/tests/pyanaconda_tests/user_create_test.py
index 2a648c3..a93ab8b 100644
--- a/tests/pyanaconda_tests/user_create_test.py
+++ b/tests/pyanaconda_tests/user_create_test.py
@@ -343,3 +343,18 @@ def create_user_reuse_home_test(self):
stat_fields = os.stat(self.tmpdir + "/home/test_user")
self.assertEqual(stat_fields.st_uid, 1000)
self.assertEqual(stat_fields.st_gid, 1000)
+
+ def create_user_gid_in_group_list_test(self):
+ """Create a user with a GID equal to that of one of the requested groups"""
+
+ self.users.createUser("test_user", gid=1047, groups=["test_group(1047)"], root=self.tmpdir)
+
+ # Ensure that the user's GID is equal to the GID requested
+ pwd_fields = self._readFields("/etc/passwd", "test_user")
+ self.assertIsNotNone(pwd_fields)
+ self.assertEqual(pwd_fields[3], "1047")
+
+ # and that the requested group has the right GID
+ grp_fields = self._readFields("/etc/group", "test_group")
+ self.assertIsNotNone(grp_fields)
+ self.assertEqual(grp_fields[2], "1047")
--
To view this commit on github, visit https://github.com/rhinstaller/anaconda/commit/cb1793659c642bb2fe4bff373a598b019cbd3bb8
More information about the anaconda-patches
mailing list