attach: simplify lxc_attach_getpwshell()

parent ba7bd8c8
...@@ -448,12 +448,15 @@ static char *lxc_attach_getpwshell(uid_t uid) ...@@ -448,12 +448,15 @@ static char *lxc_attach_getpwshell(uid_t uid)
int fd, ret; int fd, ret;
pid_t pid; pid_t pid;
int pipes[2]; int pipes[2];
char *result = NULL; FILE *pipe_f;
bool found = false;
size_t line_bufsz = 0;
char *line = NULL, *result = NULL;
/* We need to fork off a process that runs the getent program, and we /* We need to fork off a process that runs the getent program, and we
* need to capture its output, so we use a pipe for that purpose. * need to capture its output, so we use a pipe for that purpose.
*/ */
ret = pipe(pipes); ret = pipe2(pipes, O_CLOEXEC);
if (ret < 0) if (ret < 0)
return NULL; return NULL;
...@@ -464,12 +467,45 @@ static char *lxc_attach_getpwshell(uid_t uid) ...@@ -464,12 +467,45 @@ static char *lxc_attach_getpwshell(uid_t uid)
return NULL; return NULL;
} }
if (pid) { if (!pid) {
int status; char uid_buf[32];
FILE *pipe_f; char *arguments[] = {
int found = 0; "getent",
size_t line_bufsz = 0; "passwd",
char *line = NULL; uid_buf,
NULL
};
close(pipes[0]);
/* We want to capture stdout. */
ret = dup2(pipes[1], STDOUT_FILENO);
close(pipes[1]);
if (ret < 0)
exit(EXIT_FAILURE);
/* Get rid of stdin/stderr, so we try to associate it with
* /dev/null.
*/
fd = open_devnull();
if (fd < 0) {
close(STDIN_FILENO);
close(STDERR_FILENO);
} else {
(void)dup3(fd, STDIN_FILENO, O_CLOEXEC);
(void)dup3(fd, STDOUT_FILENO, O_CLOEXEC);
close(fd);
}
/* Finish argument list. */
ret = snprintf(uid_buf, sizeof(uid_buf), "%ld", (long)uid);
if (ret <= 0 || ret >= sizeof(uid_buf))
exit(EXIT_FAILURE);
/* Try to run getent program. */
(void)execvp("getent", arguments);
exit(EXIT_FAILURE);
}
close(pipes[1]); close(pipes[1]);
...@@ -495,23 +531,28 @@ static char *lxc_attach_getpwshell(uid_t uid) ...@@ -495,23 +531,28 @@ static char *lxc_attach_getpwshell(uid_t uid)
token = strtok_r(line, ":", &saveptr); token = strtok_r(line, ":", &saveptr);
if (!token) if (!token)
continue; continue;
/* next: dummy password field */ /* next: dummy password field */
token = strtok_r(NULL, ":", &saveptr); token = strtok_r(NULL, ":", &saveptr);
if (!token) if (!token)
continue; continue;
/* next: user id */ /* next: user id */
token = strtok_r(NULL, ":", &saveptr); token = strtok_r(NULL, ":", &saveptr);
value = token ? strtol(token, &endptr, 10) : 0; value = token ? strtol(token, &endptr, 10) : 0;
if (!token || !endptr || *endptr || value == LONG_MIN || value == LONG_MAX) if (!token || !endptr || *endptr || value == LONG_MIN ||
value == LONG_MAX)
continue; continue;
/* dummy sanity check: user id matches */ /* dummy sanity check: user id matches */
if ((uid_t) value != uid) if ((uid_t)value != uid)
continue; continue;
/* skip fields: gid, gecos, dir, go to next field 'shell' */ /* skip fields: gid, gecos, dir, go to next field 'shell' */
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
token = strtok_r(NULL, ":", &saveptr); token = strtok_r(NULL, ":", &saveptr);
if (!token) if (!token)
break; continue;
} }
if (!token) if (!token)
continue; continue;
...@@ -523,30 +564,13 @@ static char *lxc_attach_getpwshell(uid_t uid) ...@@ -523,30 +564,13 @@ static char *lxc_attach_getpwshell(uid_t uid)
if (token) if (token)
continue; continue;
found = 1; found = true;
} }
free(line); free(line);
fclose(pipe_f); fclose(pipe_f);
again:
if (waitpid(pid, &status, 0) < 0) {
if (errno == EINTR)
goto again;
free(result);
return NULL;
}
/* Some sanity checks. If anything even hinted at going wrong,
* we can't be sure we have a valid result, so we assume we
* don't.
*/
if (!WIFEXITED(status)) {
free(result);
return NULL;
}
if (WEXITSTATUS(status) != 0) { ret = wait_for_pid(pid);
if (ret < 0) {
free(result); free(result);
return NULL; return NULL;
} }
...@@ -557,43 +581,6 @@ static char *lxc_attach_getpwshell(uid_t uid) ...@@ -557,43 +581,6 @@ static char *lxc_attach_getpwshell(uid_t uid)
} }
return result; return result;
} else {
char uid_buf[32];
char *arguments[] = {
"getent",
"passwd",
uid_buf,
NULL
};
close(pipes[0]);
/* We want to capture stdout. */
dup2(pipes[1], 1);
close(pipes[1]);
/* Get rid of stdin/stderr, so we try to associate it with
* /dev/null.
*/
fd = open("/dev/null", O_RDWR);
if (fd < 0) {
close(0);
close(2);
} else {
dup2(fd, 0);
dup2(fd, 2);
close(fd);
}
/* Finish argument list. */
ret = snprintf(uid_buf, sizeof(uid_buf), "%ld", (long) uid);
if (ret <= 0)
exit(-1);
/* Try to run getent program. */
(void) execvp("getent", arguments);
exit(-1);
}
} }
static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid) static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment