Debian Codegraph @

src/shared/machine-bind-user.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <grp.h>
4 #include <pwd.h>
5
6 #include "alloc-util.h"
7 #include "chase.h"
8 #include "fd-util.h"
9 #include "format-util.h"
10 #include "json-util.h"
11 #include "log.h"
12 #include "machine-bind-user.h"
13 #include "path-util.h"
14 #include "string-util.h"
15 #include "strv.h"
16 #include "user-util.h"
17 #include "userdb.h"
18
19 static int check_etc_passwd_collisions(
20 const char *directory,
21 const char *name,
22 uid_t uid) {
23
24 _cleanup_fclose_ FILE *f = NULL;
25 int r;
26
27 assert(name || uid_is_valid(uid));
28
29 if (!directory)
30 return 0;
31
32 r = chase_and_fopen_unlocked("/etc/passwd", directory, CHASE_PREFIX_ROOT, "re", NULL, &f);
33 if (r == -ENOENT)
34 return 0; /* no user database? then no user, hence no collision */
35 if (r < 0)
36 return log_error_errno(r, "Failed to open /etc/passwd of machine: %m");
37
38 for (;;) {
39 struct passwd *pw;
40
41 r = fgetpwent_sane(f, &pw);
42 if (r < 0)
43 return log_error_errno(r, "Failed to iterate through /etc/passwd of machine: %m");
44 if (r == 0) /* EOF */
45 return 0; /* no collision */
46
47 if (name && streq_ptr(pw->pw_name, name))
48 return 1; /* name collision */
49 if (uid_is_valid(uid) && pw->pw_uid == uid)
50 return 1; /* UID collision */
51 }
52 }
53
54 static int check_etc_group_collisions(
55 const char *directory,
56 const char *name,
57 gid_t gid) {
58
59 _cleanup_fclose_ FILE *f = NULL;
60 int r;
61
62 assert(name || gid_is_valid(gid));
63
64 if (!directory)
65 return 0;
66
67 r = chase_and_fopen_unlocked("/etc/group", directory, CHASE_PREFIX_ROOT, "re", NULL, &f);
68 if (r == -ENOENT)
69 return 0; /* no group database? then no group, hence no collision */
70 if (r < 0)
71 return log_error_errno(r, "Failed to open /etc/group of machine: %m");
72
73 for (;;) {
74 struct group *gr;
75
76 r = fgetgrent_sane(f, &gr);
77 if (r < 0)
78 return log_error_errno(r, "Failed to iterate through /etc/group of machine: %m");
79 if (r == 0)
80 return 0; /* no collision */
81
82 if (name && streq_ptr(gr->gr_name, name))
83 return 1; /* name collision */
84 if (gid_is_valid(gid) && gr->gr_gid == gid)
85 return 1; /* gid collision */
86 }
87 }
88
89 static int convert_user(
90 const char *directory,
91 UserRecord *u,
92 GroupRecord *g,
93 uid_t allocate_uid,
94 const char *shell,
95 bool shell_copy,
96 const char *home_mount_directory,
97 char **groups,
98 UserRecord **ret_converted_user,
99 GroupRecord **ret_converted_group) {
100
101 _cleanup_(group_record_unrefp) GroupRecord *converted_group = NULL;
102 _cleanup_(user_record_unrefp) UserRecord *converted_user = NULL;
103 _cleanup_free_ char *h = NULL;
104 sd_json_variant *p, *hp = NULL, *ssh = NULL;
105 int r;
106
107 assert(u);
108 assert(g);
109 assert(user_record_gid(u) == g->gid);
110 assert(ret_converted_user);
111 assert(ret_converted_group);
112
113 if (shell_copy)
114 shell = u->shell;
115
116 r = check_etc_passwd_collisions(directory, u->user_name, UID_INVALID);
117 if (r < 0)
118 return r;
119 if (r > 0)
120 return log_error_errno(SYNTHETIC_ERRNO(EBUSY),
121 "Sorry, the user '%s' already exists in the machine.", u->user_name);
122
123 r = check_etc_group_collisions(directory, g->group_name, GID_INVALID);
124 if (r < 0)
125 return r;
126 if (r > 0)
127 return log_error_errno(SYNTHETIC_ERRNO(EBUSY),
128 "Sorry, the group '%s' already exists in the machine.", g->group_name);
129
130 h = path_join(home_mount_directory, u->user_name);
131 if (!h)
132 return log_oom();
133
134 /* Acquire the source hashed password array as-is, so that it retains the JSON_VARIANT_SENSITIVE flag */
135 p = sd_json_variant_by_key(u->json, "privileged");
136 if (p) {
137 hp = sd_json_variant_by_key(p, "hashedPassword");
138 ssh = sd_json_variant_by_key(p, "sshAuthorizedKeys");
139 }
140
141 r = user_record_build(
142 &converted_user,
143 SD_JSON_BUILD_OBJECT(
144 SD_JSON_BUILD_PAIR_STRING("userName", u->user_name),
145 SD_JSON_BUILD_PAIR_UNSIGNED("uid", allocate_uid),
146 SD_JSON_BUILD_PAIR_UNSIGNED("gid", allocate_uid),
147 SD_JSON_BUILD_PAIR_CONDITION(u->disposition >= 0, "disposition", SD_JSON_BUILD_STRING(user_disposition_to_string(u->disposition))),
148 SD_JSON_BUILD_PAIR_STRING("homeDirectory", h),
149 SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NSpawn")),
150 JSON_BUILD_PAIR_STRING_NON_EMPTY("shell", shell),
151 SD_JSON_BUILD_PAIR_STRV("memberOf", groups),
152 SD_JSON_BUILD_PAIR("privileged", SD_JSON_BUILD_OBJECT(
153 SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(u->hashed_password), "hashedPassword", SD_JSON_BUILD_VARIANT(hp)),
154 SD_JSON_BUILD_PAIR_CONDITION(!!ssh, "sshAuthorizedKeys", SD_JSON_BUILD_VARIANT(ssh))))));
155 if (r < 0)
156 return log_error_errno(r, "Failed to build machine user record: %m");
157
158 r = group_record_build(
159 &converted_group,
160 SD_JSON_BUILD_OBJECT(
161 SD_JSON_BUILD_PAIR_STRING("groupName", g->group_name),
162 SD_JSON_BUILD_PAIR_UNSIGNED("gid", allocate_uid),
163 SD_JSON_BUILD_PAIR_CONDITION(g->disposition >= 0, "disposition", SD_JSON_BUILD_STRING(user_disposition_to_string(g->disposition))),
164 SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NSpawn"))));
165 if (r < 0)
166 return log_error_errno(r, "Failed to build machine group record: %m");
167
168 *ret_converted_user = TAKE_PTR(converted_user);
169 *ret_converted_group = TAKE_PTR(converted_group);
170
171 return 0;
172 }
173
174 static int find_free_uid(const char *directory, uid_t *current_uid) {
175 int r;
176
177 assert(current_uid);
178
179 for (;; (*current_uid)++) {
180 if (*current_uid > MAP_UID_MAX)
181 return log_error_errno(
182 SYNTHETIC_ERRNO(EBUSY),
183 "No suitable available UID in range " UID_FMT "…" UID_FMT " in machine detected, can't map user.",
184 MAP_UID_MIN, MAP_UID_MAX);
185
186 r = check_etc_passwd_collisions(directory, NULL, *current_uid);
187 if (r < 0)
188 return r;
189 if (r > 0) /* already used */
190 continue;
191
192 /* We want to use the UID also as GID, hence check for it in /etc/group too */
193 r = check_etc_group_collisions(directory, NULL, (gid_t) *current_uid);
194 if (r <= 0)
195 return r;
196 }
197 }
198
199 MachineBindUserContext* machine_bind_user_context_free(MachineBindUserContext *c) {
200 if (!c)
201 return NULL;
202
203 FOREACH_ARRAY(d, c->data, c->n_data) {
204 user_record_unref(d->host_user);
205 group_record_unref(d->host_group);
206 user_record_unref(d->payload_user);
207 group_record_unref(d->payload_group);
208 }
209
210 return mfree(c);
211 }
212
213 int machine_bind_user_prepare(
214 const char *directory,
215 char **bind_user,
216 const char *bind_user_shell,
217 bool bind_user_shell_copy,
218 const char *bind_user_home_mount_directory,
219 char **bind_user_groups,
220 MachineBindUserContext **ret) {
221
222 _cleanup_(machine_bind_user_context_freep) MachineBindUserContext *c = NULL;
223 uid_t current_uid = MAP_UID_MIN;
224 int r;
225
226 assert(ret);
227
228 /* This resolves the users specified in 'bind_user', generates a minimalized JSON user + group record
229 * for it to stick in the machine, allocates a UID/GID for it, and updates the custom mount table,
230 * to include an appropriate bind mount mapping.
231 *
232 * This extends the passed custom_mounts/n_custom_mounts with the home directories, and allocates a
233 * new BindUserContext for the user records */
234
235 if (strv_isempty(bind_user)) {
236 *ret = NULL;
237 return 0;
238 }
239
240 c = new0(MachineBindUserContext, 1);
241 if (!c)
242 return log_oom();
243
244 STRV_FOREACH(n, bind_user) {
245 _cleanup_(user_record_unrefp) UserRecord *u = NULL, *cu = NULL;
246 _cleanup_(group_record_unrefp) GroupRecord *g = NULL, *cg = NULL;
247
248 r = userdb_by_name(*n, /* match= */ NULL, USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN, &u);
249 if (r == -ENOEXEC)
250 return log_error_errno(r, "User '%s' did not pass filter.", *n);
251 if (r < 0)
252 return log_error_errno(r, "Failed to resolve user '%s': %s", *n, STRERROR_USER(r));
253
254 /* For now, let's refuse mapping the root/nobody users explicitly. The records we generate
255 * are strictly additive, nss-systemd is typically placed last in /etc/nsswitch.conf. Thus
256 * even if we wanted, we couldn't override the root or nobody user records. Note we also
257 * check for name conflicts in /etc/passwd + /etc/group later on, which would usually filter
258 * out root/nobody too, hence these checks might appear redundant — but they actually are
259 * not, as we want to support environments where /etc/passwd and /etc/group are non-existent,
260 * and the user/group databases fully synthesized at runtime. Moreover, the name of the
261 * user/group name of the "nobody" account differs between distros, hence a check by numeric
262 * UID is safer. */
263 if (user_record_is_root(u))
264 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Mapping 'root' user not supported, sorry.");
265
266 if (user_record_is_nobody(u))
267 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Mapping 'nobody' user not supported, sorry.");
268
269 if (!uid_is_valid(u->uid))
270 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot bind user with no UID, refusing.");
271
272 r = groupdb_by_gid(user_record_gid(u), /* match= */ NULL, USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN, &g);
273 if (r == -ENOEXEC)
274 return log_error_errno(r, "Group of user '%s' did not pass filter.", u->user_name);
275 if (r < 0)
276 return log_error_errno(r, "Failed to resolve group of user '%s': %s",
277 u->user_name, STRERROR_GROUP(r));
278
279 /* We want to synthesize exactly one user + group from the host into the machine. This only
280 * makes sense if the user on the host has its own private group. We can't reasonably check
281 * this, so we just check of the name of user and group match.
282 *
283 * One of these days we might want to support users in a shared/common group too, but it's
284 * not clear to me how this would have to be mapped, precisely given that the common group
285 * probably already exists in the machine. */
286 if (!streq(u->user_name, g->group_name))
287 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
288 "Sorry, mapping users without private groups is currently not supported.");
289
290 r = find_free_uid(directory, &current_uid);
291 if (r < 0)
292 return r;
293
294 r = convert_user(
295 directory,
296 u, g,
297 current_uid,
298 bind_user_shell,
299 bind_user_shell_copy,
300 bind_user_home_mount_directory,
301 bind_user_groups,
302 &cu, &cg);
303 if (r < 0)
304 return r;
305
306 if (!GREEDY_REALLOC(c->data, c->n_data + 1))
307 return log_oom();
308
309 c->data[c->n_data++] = (MachineBindUserData) {
310 .host_user = TAKE_PTR(u),
311 .host_group = TAKE_PTR(g),
312 .payload_user = TAKE_PTR(cu),
313 .payload_group = TAKE_PTR(cg),
314 };
315
316 current_uid++;
317 }
318
319 *ret = TAKE_PTR(c);
320 return 1;
321 }