]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-setuid.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
13 #include "nspawn-setuid.h"
14 #include "process-util.h"
15 #include "signal-util.h"
16 #include "string-util.h"
18 #include "user-util.h"
21 static int spawn_getent(const char *database
, const char *key
, pid_t
*rpid
) {
29 if (pipe2(pipe_fds
, O_CLOEXEC
) < 0)
30 return log_error_errno(errno
, "Failed to allocate pipe: %m");
32 r
= safe_fork("(getent)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
34 safe_close_pair(pipe_fds
);
38 char *empty_env
= NULL
;
40 safe_close(pipe_fds
[0]);
42 if (rearrange_stdio(-1, pipe_fds
[1], -1) < 0)
45 close_all_fds(NULL
, 0);
47 execle("/usr/bin/getent", "getent", database
, key
, NULL
, &empty_env
);
48 execle("/bin/getent", "getent", database
, key
, NULL
, &empty_env
);
52 pipe_fds
[1] = safe_close(pipe_fds
[1]);
59 int change_uid_gid(const char *user
, char **_home
) {
61 const char *word
, *state
;
62 _cleanup_free_ uid_t
*uids
= NULL
;
63 _cleanup_free_
char *home
= NULL
, *line
= NULL
;
64 _cleanup_fclose_
FILE *f
= NULL
;
65 _cleanup_close_
int fd
= -1;
75 if (!user
|| STR_IN_SET(user
, "root", "0")) {
76 /* Reset everything fully to 0, just in case */
80 return log_error_errno(r
, "Failed to become root: %m");
86 /* First, get user credentials */
87 fd
= spawn_getent("passwd", user
, &pid
);
96 r
= read_line(f
, LONG_LINE_MAX
, &line
);
98 log_error("Failed to resolve user %s.", user
);
102 return log_error_errno(r
, "Failed to read from getent: %m");
104 (void) wait_for_terminate_and_check("getent passwd", pid
, WAIT_LOG
);
106 x
= strchr(line
, ':');
108 log_error("/etc/passwd entry has invalid user field.");
112 u
= strchr(x
+1, ':');
114 log_error("/etc/passwd entry has invalid password field.");
121 log_error("/etc/passwd entry has invalid UID field.");
129 log_error("/etc/passwd entry has invalid GID field.");
134 h
= strchr(x
+1, ':');
136 log_error("/etc/passwd entry has invalid GECOS field.");
143 log_error("/etc/passwd entry has invalid home directory field.");
149 r
= parse_uid(u
, &uid
);
151 log_error("Failed to parse UID of user.");
155 r
= parse_gid(g
, &gid
);
157 log_error("Failed to parse GID of user.");
168 /* Second, get group memberships */
169 fd
= spawn_getent("initgroups", user
, &pid
);
173 f
= fdopen(fd
, "re");
178 r
= read_line(f
, LONG_LINE_MAX
, &line
);
180 log_error("Failed to resolve user %s.", user
);
184 return log_error_errno(r
, "Failed to read from getent: %m");
186 (void) wait_for_terminate_and_check("getent initgroups", pid
, WAIT_LOG
);
188 /* Skip over the username and subsequent separator whitespace */
190 x
+= strcspn(x
, WHITESPACE
);
191 x
+= strspn(x
, WHITESPACE
);
193 FOREACH_WORD(word
, l
, x
, state
) {
199 if (!GREEDY_REALLOC(uids
, sz
, n_uids
+1))
202 r
= parse_uid(c
, &uids
[n_uids
++]);
204 return log_error_errno(r
, "Failed to parse group data from getent: %m");
207 r
= mkdir_parents(home
, 0775);
209 return log_error_errno(r
, "Failed to make home root directory: %m");
211 r
= mkdir_safe(home
, 0755, uid
, gid
, 0);
212 if (r
< 0 && !IN_SET(r
, -EEXIST
, -ENOTDIR
))
213 return log_error_errno(r
, "Failed to make home directory: %m");
215 (void) fchown(STDIN_FILENO
, uid
, gid
);
216 (void) fchown(STDOUT_FILENO
, uid
, gid
);
217 (void) fchown(STDERR_FILENO
, uid
, gid
);
219 if (setgroups(n_uids
, uids
) < 0)
220 return log_error_errno(errno
, "Failed to set auxiliary groups: %m");
222 if (setresgid(gid
, gid
, gid
) < 0)
223 return log_error_errno(errno
, "setresgid() failed: %m");
225 if (setresuid(uid
, uid
, uid
) < 0)
226 return log_error_errno(errno
, "setresuid() failed: %m");
229 *_home
= TAKE_PTR(home
);