]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-setuid.c
24f433fcbc0145b9dd58228dfbd4a9263f4bd382
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
25 #include "alloc-util.h"
31 #include "nspawn-setuid.h"
32 #include "process-util.h"
33 #include "signal-util.h"
34 #include "string-util.h"
36 #include "user-util.h"
39 static int spawn_getent(const char *database
, const char *key
, pid_t
*rpid
) {
47 if (pipe2(pipe_fds
, O_CLOEXEC
) < 0)
48 return log_error_errno(errno
, "Failed to allocate pipe: %m");
50 r
= safe_fork("(getent)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
52 safe_close_pair(pipe_fds
);
56 char *empty_env
= NULL
;
58 safe_close(pipe_fds
[0]);
60 if (rearrange_stdio(-1, pipe_fds
[1], -1) < 0)
63 close_all_fds(NULL
, 0);
65 execle("/usr/bin/getent", "getent", database
, key
, NULL
, &empty_env
);
66 execle("/bin/getent", "getent", database
, key
, NULL
, &empty_env
);
70 pipe_fds
[1] = safe_close(pipe_fds
[1]);
77 int change_uid_gid(const char *user
, char **_home
) {
79 const char *word
, *state
;
80 _cleanup_free_ uid_t
*uids
= NULL
;
81 _cleanup_free_
char *home
= NULL
, *line
= NULL
;
82 _cleanup_fclose_
FILE *f
= NULL
;
83 _cleanup_close_
int fd
= -1;
93 if (!user
|| STR_IN_SET(user
, "root", "0")) {
94 /* Reset everything fully to 0, just in case */
98 return log_error_errno(r
, "Failed to become root: %m");
104 /* First, get user credentials */
105 fd
= spawn_getent("passwd", user
, &pid
);
109 f
= fdopen(fd
, "re");
114 r
= read_line(f
, LONG_LINE_MAX
, &line
);
116 log_error("Failed to resolve user %s.", user
);
120 return log_error_errno(r
, "Failed to read from getent: %m");
122 (void) wait_for_terminate_and_check("getent passwd", pid
, WAIT_LOG
);
124 x
= strchr(line
, ':');
126 log_error("/etc/passwd entry has invalid user field.");
130 u
= strchr(x
+1, ':');
132 log_error("/etc/passwd entry has invalid password field.");
139 log_error("/etc/passwd entry has invalid UID field.");
147 log_error("/etc/passwd entry has invalid GID field.");
152 h
= strchr(x
+1, ':');
154 log_error("/etc/passwd entry has invalid GECOS field.");
161 log_error("/etc/passwd entry has invalid home directory field.");
167 r
= parse_uid(u
, &uid
);
169 log_error("Failed to parse UID of user.");
173 r
= parse_gid(g
, &gid
);
175 log_error("Failed to parse GID of user.");
186 /* Second, get group memberships */
187 fd
= spawn_getent("initgroups", user
, &pid
);
191 f
= fdopen(fd
, "re");
196 r
= read_line(f
, LONG_LINE_MAX
, &line
);
198 log_error("Failed to resolve user %s.", user
);
202 return log_error_errno(r
, "Failed to read from getent: %m");
204 (void) wait_for_terminate_and_check("getent initgroups", pid
, WAIT_LOG
);
206 /* Skip over the username and subsequent separator whitespace */
208 x
+= strcspn(x
, WHITESPACE
);
209 x
+= strspn(x
, WHITESPACE
);
211 FOREACH_WORD(word
, l
, x
, state
) {
217 if (!GREEDY_REALLOC(uids
, sz
, n_uids
+1))
220 r
= parse_uid(c
, &uids
[n_uids
++]);
222 return log_error_errno(r
, "Failed to parse group data from getent: %m");
225 r
= mkdir_parents(home
, 0775);
227 return log_error_errno(r
, "Failed to make home root directory: %m");
229 r
= mkdir_safe(home
, 0755, uid
, gid
, 0);
230 if (r
< 0 && r
!= -EEXIST
)
231 return log_error_errno(r
, "Failed to make home directory: %m");
233 (void) fchown(STDIN_FILENO
, uid
, gid
);
234 (void) fchown(STDOUT_FILENO
, uid
, gid
);
235 (void) fchown(STDERR_FILENO
, uid
, gid
);
237 if (setgroups(n_uids
, uids
) < 0)
238 return log_error_errno(errno
, "Failed to set auxiliary groups: %m");
240 if (setresgid(gid
, gid
, gid
) < 0)
241 return log_error_errno(errno
, "setresgid() failed: %m");
243 if (setresuid(uid
, uid
, uid
) < 0)
244 return log_error_errno(errno
, "setresuid() failed: %m");