Skip to content

Commit

Permalink
Add new APIs to interact with group IDs
Browse files Browse the repository at this point in the history
This will help us flesh out the next migrations, which is effectively
to move the `users` group from whatever the current gid is that systemd
dynamically generated, to the static gid of '100'.

Signed-off-by: Ikey Doherty <[email protected]>
  • Loading branch information
ikeydoherty committed Dec 3, 2017
1 parent 564face commit e710e33
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/user-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,53 @@ bool qol_user_add_to_group(QolUser *self, const char *group)
return true;
}

int qol_user_manager_get_group_id(__qol_unused__ QolUserManager *self, const char *group)
{
struct group *grp = NULL;

grp = getgrnam(group);
if (!grp) {
return -1;
}
return (int)grp->gr_gid;
}

bool qol_user_manager_change_group_id(QolUserManager *self, const char *group, int gid)
{
char *gid_str = NULL;
bool ret = false;

char *command[] = {
"/usr/sbin/groupmod",
"-g",
NULL, /* gid */
NULL, /* group */
NULL,
};

if (asprintf(&gid_str, "%u", gid) < 0) {
return false;
}

command[2] = gid_str;
command[3] = (char *)group;

if (!qol_exec_command(command)) {
goto failed;
}

/* Check the updated gid is now valid */
if (qol_user_manager_get_group_id(self, group) != gid) {
goto failed;
}

ret = true;

failed:
free(gid_str);
return ret;
}

/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
Expand Down
21 changes: 21 additions & 0 deletions src/user-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ bool qol_user_manager_refresh(QolUserManager *manager);
*/
void qol_user_manager_free(QolUserManager *manager);

/**
* Attempt to change the group ID for the given group
*
* @param manager Pointer to an allocated QolUserManager
* @param group Group name to update
* @param gid New gid for the group to take
*
* @returns True if the update worked, otherwise false.
*/
bool qol_user_manager_change_group_id(QolUserManager *manager, const char *group, int gid);

/**
* Get the group ID for the named group
*
* @param self Pointer to an allocated QolUserManager
* @param group Group name to check
*
* @returns The new gid, or -1 for an unknown group ID
*/
int qol_user_manager_get_group_id(QolUserManager *manager, const char *group);

/**
* Determine if this is an "active" user, i.e. not a system user
*
Expand Down

0 comments on commit e710e33

Please sign in to comment.