if (username)
{
#if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
- state->pw = getpwnam(username);
- if (!state->pw)
+ state->uid = -1;
+ const struct passwd *pw = getpwnam(username);
+ if (!pw)
{
msg(M_ERR, "failed to find UID for user %s", username);
}
+ else
+ {
+ state->uid = pw->pw_uid;
+ }
state->username = username;
ret = true;
#else /* if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID) */
platform_user_set(const struct platform_state_user *state)
{
#if defined(HAVE_GETPWNAM) && defined(HAVE_SETUID)
- if (state->username && state->pw)
+ if (state->username && state->uid >= 0)
{
- if (setuid(state->pw->pw_uid))
+ if (setuid(state->uid))
{
msg(M_ERR, "setuid('%s') failed", state->username);
}
if (groupname)
{
#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
- state->gr = getgrnam(groupname);
- if (!state->gr)
+ state->gid = -1;
+ const struct group *gr = getgrnam(groupname);
+ if (!gr)
{
msg(M_ERR, "failed to find GID for group %s", groupname);
}
+ else
+ {
+ state->gid = gr->gr_gid;
+ }
state->groupname = groupname;
ret = true;
#else /* if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID) */
platform_group_set(const struct platform_state_group *state)
{
#if defined(HAVE_GETGRNAM) && defined(HAVE_SETGID)
- if (state->groupname && state->gr)
+ if (state->groupname && state->gid >= 0)
{
- if (setgid(state->gr->gr_gid))
+ if (setgid(state->gid))
{
msg(M_ERR, "setgid('%s') failed", state->groupname);
}
#ifdef HAVE_SETGROUPS
{
gid_t gr_list[1];
- gr_list[0] = state->gr->gr_gid;
+ gr_list[0] = state->gid;
if (setgroups(1, gr_list))
{
msg(M_ERR, "setgroups('%s') failed", state->groupname);
* new_uid/new_gid defaults to -1, which will not make
* libcap-ng change the UID/GID unless configured
*/
- if (group_state->groupname && group_state->gr)
+ if (group_state->groupname && group_state->gid >= 0)
{
- new_gid = group_state->gr->gr_gid;
+ new_gid = group_state->gid;
}
- if (user_state->username && user_state->pw)
+ if (user_state->username && user_state->uid >= 0)
{
- new_uid = user_state->pw->pw_uid;
+ new_uid = user_state->uid;
}
/* Prepare capabilities before dropping UID/GID */
{
msg(M_ERR, "Cannot get user entry for %s", username);
}
- else if (ioctl(tt->fd, TUNSETOWNER, platform_state_user.pw->pw_uid) < 0)
+ else if (ioctl(tt->fd, TUNSETOWNER, platform_state_user.uid) < 0)
{
msg(M_ERR, "Cannot ioctl TUNSETOWNER(%s) %s", username, dev);
}
{
msg(M_ERR, "Cannot get group entry for %s", groupname);
}
- else if (ioctl(tt->fd, TUNSETGROUP, platform_state_group.gr->gr_gid) < 0)
+ else if (ioctl(tt->fd, TUNSETGROUP, platform_state_group.gid) < 0)
{
msg(M_ERR, "Cannot ioctl TUNSETGROUP(%s) %s", groupname, dev);
}