]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-setuid.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
20 static int spawn_getent(const char *database
, const char *key
, pid_t
*rpid
) {
28 if (pipe2(pipe_fds
, O_CLOEXEC
) < 0)
29 return log_error_errno(errno
, "Failed to allocate pipe: %m");
31 r
= safe_fork_full("(getent)",
32 (int[]) { -EBADF
, pipe_fds
[1], -EBADF
}, NULL
, 0,
33 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG_SIGTERM
|FORK_REARRANGE_STDIO
|FORK_LOG
|FORK_RLIMIT_NOFILE_SAFE
,
36 safe_close_pair(pipe_fds
);
40 execle("/usr/bin/getent", "getent", database
, key
, NULL
, &(char*[1]){});
41 execle("/bin/getent", "getent", database
, key
, NULL
, &(char*[1]){});
45 pipe_fds
[1] = safe_close(pipe_fds
[1]);
52 int change_uid_gid_raw(
55 const gid_t
*supplementary_gids
,
56 size_t n_supplementary_gids
,
61 if (!uid_is_valid(uid
))
63 if (!gid_is_valid(gid
))
67 (void) fchown(STDIN_FILENO
, uid
, gid
);
68 (void) fchown(STDOUT_FILENO
, uid
, gid
);
69 (void) fchown(STDERR_FILENO
, uid
, gid
);
72 r
= fully_set_uid_gid(uid
, gid
, supplementary_gids
, n_supplementary_gids
);
74 return log_error_errno(r
, "Changing privileges failed: %m");
79 int change_uid_gid(const char *user
, bool chown_stdio
, char **ret_home
) {
81 _cleanup_free_ gid_t
*gids
= NULL
;
82 _cleanup_free_
char *home
= NULL
, *line
= NULL
;
83 _cleanup_fclose_
FILE *f
= NULL
;
84 _cleanup_close_
int fd
= -EBADF
;
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
= take_fdopen(&fd
, "r");
113 r
= read_line(f
, LONG_LINE_MAX
, &line
);
115 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
116 "Failed to resolve user %s.", user
);
118 return log_error_errno(r
, "Failed to read from getent: %m");
120 (void) wait_for_terminate_and_check("getent passwd", pid
, WAIT_LOG
);
122 x
= strchr(line
, ':');
124 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
125 "/etc/passwd entry has invalid user field.");
127 u
= strchr(x
+1, ':');
129 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
130 "/etc/passwd entry has invalid password field.");
135 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
136 "/etc/passwd entry has invalid UID field.");
142 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
143 "/etc/passwd entry has invalid GID field.");
146 h
= strchr(x
+1, ':');
148 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
149 "/etc/passwd entry has invalid GECOS field.");
154 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
155 "/etc/passwd entry has invalid home directory field.");
159 r
= parse_uid(u
, &uid
);
161 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
162 "Failed to parse UID of user.");
164 r
= parse_gid(g
, &gid
);
166 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
167 "Failed to parse GID of user.");
176 /* Second, get group memberships */
177 fd
= spawn_getent("initgroups", user
, &pid
);
181 f
= take_fdopen(&fd
, "r");
185 r
= read_line(f
, LONG_LINE_MAX
, &line
);
187 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
188 "Failed to resolve user %s.", user
);
190 return log_error_errno(r
, "Failed to read from getent: %m");
192 (void) wait_for_terminate_and_check("getent initgroups", pid
, WAIT_LOG
);
194 /* Skip over the username and subsequent separator whitespace */
196 x
+= strcspn(x
, WHITESPACE
);
197 x
+= strspn(x
, WHITESPACE
);
199 for (const char *p
= x
;;) {
200 _cleanup_free_
char *word
= NULL
;
202 r
= extract_first_word(&p
, &word
, NULL
, 0);
204 return log_error_errno(r
, "Failed to parse group data from getent: %m");
208 if (!GREEDY_REALLOC(gids
, n_gids
+1))
211 r
= parse_gid(word
, &gids
[n_gids
++]);
213 return log_error_errno(r
, "Failed to parse group data from getent: %m");
216 r
= mkdir_parents(home
, 0775);
218 return log_error_errno(r
, "Failed to make home root directory: %m");
220 r
= mkdir_safe(home
, 0755, uid
, gid
, 0);
221 if (r
< 0 && !IN_SET(r
, -EEXIST
, -ENOTDIR
))
222 return log_error_errno(r
, "Failed to make home directory: %m");
224 r
= change_uid_gid_raw(uid
, gid
, gids
, n_gids
, chown_stdio
);
229 *ret_home
= TAKE_PTR(home
);