Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: return error from set_home_env() if the user was not found #1633

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions src/libcrun/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,7 @@ set_home_env (uid_t id)
cleanup_free char *buf = NULL;
long buf_size;
cleanup_file FILE *stream = NULL;
int ret = -1;

buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
if (buf_size < 0)
Expand All @@ -1457,26 +1458,17 @@ set_home_env (uid_t id)

stream = fopen ("/etc/passwd", "re");
if (stream == NULL)
{
if (errno == ENOENT)
goto exit;

return -1;
}
goto error;

for (;;)
{
int ret;
struct passwd *ret_pw = NULL;

ret = fgetpwent_r (stream, &pwd, buf, buf_size, &ret_pw);
if (UNLIKELY (ret != 0))
{
if (errno == ENOENT)
return 0;

if (errno != ERANGE)
return ret;
goto error;

buf_size *= 2;
buf = xrealloc (buf, buf_size);
Expand All @@ -1490,10 +1482,9 @@ set_home_env (uid_t id)
}
}

exit:
/* If the user was not found, set it to something reasonable. */
setenv ("HOME", "/", 1);
return 0;
error:
/* Let callers handle the error if the user was not found. */
return ret;
}

/*if subuid or subgid exist, take the first range for the user */
Expand Down
Loading