Skip to content

Commit

Permalink
Allow a user's primary group to be created in --groups (#1279041)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dashea committed Nov 10, 2015
1 parent fdb809e commit cb17936
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pyanaconda/users.py
Expand Up @@ -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'])])
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/pyanaconda_tests/user_create_test.py
Expand Up @@ -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")

0 comments on commit cb17936

Please sign in to comment.