]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
1b992147 | 2 | |
3f6fd1ba | 3 | #include <getopt.h> |
932ad62b | 4 | #include <utmp.h> |
1b992147 | 5 | |
b5efdb8a | 6 | #include "alloc-util.h" |
1b992147 LP |
7 | #include "conf-files.h" |
8 | #include "copy.h" | |
a0f29c76 | 9 | #include "def.h" |
0c3ee127 | 10 | #include "dissect-image.h" |
a0f29c76 | 11 | #include "fd-util.h" |
ee228be1 | 12 | #include "fileio.h" |
f97b34a6 | 13 | #include "format-util.h" |
dcd5c891 | 14 | #include "fs-util.h" |
3f6fd1ba | 15 | #include "hashmap.h" |
71da1673 | 16 | #include "main-func.h" |
0c3ee127 | 17 | #include "mount-util.h" |
dcd5c891 | 18 | #include "pager.h" |
3f6fd1ba | 19 | #include "path-util.h" |
294bf0c3 | 20 | #include "pretty-print.h" |
3f6fd1ba | 21 | #include "selinux-util.h" |
0c3ee127 | 22 | #include "set.h" |
07630cea | 23 | #include "smack-util.h" |
3f6fd1ba | 24 | #include "specifier.h" |
07630cea | 25 | #include "string-util.h" |
3f6fd1ba | 26 | #include "strv.h" |
e4de7287 | 27 | #include "tmpfile-util-label.h" |
3f6fd1ba | 28 | #include "uid-range.h" |
a0f29c76 | 29 | #include "user-util.h" |
3f6fd1ba LP |
30 | #include "utf8.h" |
31 | #include "util.h" | |
1b992147 LP |
32 | |
33 | typedef enum ItemType { | |
34 | ADD_USER = 'u', | |
35 | ADD_GROUP = 'g', | |
a12b0cc3 | 36 | ADD_MEMBER = 'm', |
8530dc44 | 37 | ADD_RANGE = 'r', |
1b992147 | 38 | } ItemType; |
dcd5c891 | 39 | |
1b992147 LP |
40 | typedef struct Item { |
41 | ItemType type; | |
42 | ||
43 | char *name; | |
649916d3 | 44 | char *group_name; |
1b992147 LP |
45 | char *uid_path; |
46 | char *gid_path; | |
47 | char *description; | |
7629889c | 48 | char *home; |
7b1aaf66 | 49 | char *shell; |
1b992147 LP |
50 | |
51 | gid_t gid; | |
52 | uid_t uid; | |
53 | ||
54 | bool gid_set:1; | |
fb959f14 ZJS |
55 | |
56 | /* When set the group with the specified gid must exist | |
57 | * and the check if a uid clashes with the gid is skipped. | |
58 | */ | |
b9ee05c2 | 59 | bool id_set_strict:1; |
fb959f14 | 60 | |
1b992147 LP |
61 | bool uid_set:1; |
62 | ||
c1b6b04f KS |
63 | bool todo_user:1; |
64 | bool todo_group:1; | |
1b992147 LP |
65 | } Item; |
66 | ||
67 | static char *arg_root = NULL; | |
0c3ee127 | 68 | static char *arg_image = NULL; |
ec0327d6 | 69 | static bool arg_cat_config = false; |
d16a1c1b | 70 | static const char *arg_replace = NULL; |
1b600bd5 | 71 | static bool arg_inline = false; |
0221d68a | 72 | static PagerFlags arg_pager_flags = 0; |
1b992147 | 73 | |
5bc9c980 MV |
74 | static OrderedHashmap *users = NULL, *groups = NULL; |
75 | static OrderedHashmap *todo_uids = NULL, *todo_gids = NULL; | |
76 | static OrderedHashmap *members = NULL; | |
1b992147 | 77 | |
71da1673 YW |
78 | static Hashmap *database_by_uid = NULL, *database_by_username = NULL; |
79 | static Hashmap *database_by_gid = NULL, *database_by_groupname = NULL; | |
80 | static Set *database_users = NULL, *database_groups = NULL; | |
1b992147 | 81 | |
fed1e721 | 82 | static uid_t search_uid = UID_INVALID; |
8530dc44 LP |
83 | static UidRange *uid_range = NULL; |
84 | static unsigned n_uid_range = 0; | |
1b992147 | 85 | |
71da1673 YW |
86 | STATIC_DESTRUCTOR_REGISTER(groups, ordered_hashmap_freep); |
87 | STATIC_DESTRUCTOR_REGISTER(users, ordered_hashmap_freep); | |
88 | STATIC_DESTRUCTOR_REGISTER(members, ordered_hashmap_freep); | |
89 | STATIC_DESTRUCTOR_REGISTER(todo_uids, ordered_hashmap_freep); | |
90 | STATIC_DESTRUCTOR_REGISTER(todo_gids, ordered_hashmap_freep); | |
91 | STATIC_DESTRUCTOR_REGISTER(database_by_uid, hashmap_freep); | |
92 | STATIC_DESTRUCTOR_REGISTER(database_by_username, hashmap_freep); | |
93 | STATIC_DESTRUCTOR_REGISTER(database_users, set_free_freep); | |
94 | STATIC_DESTRUCTOR_REGISTER(database_by_gid, hashmap_freep); | |
95 | STATIC_DESTRUCTOR_REGISTER(database_by_groupname, hashmap_freep); | |
96 | STATIC_DESTRUCTOR_REGISTER(database_groups, set_free_freep); | |
97 | STATIC_DESTRUCTOR_REGISTER(uid_range, freep); | |
98 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
0c3ee127 | 99 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
71da1673 | 100 | |
08c7c321 ZJS |
101 | static int errno_is_not_exists(int code) { |
102 | /* See getpwnam(3) and getgrnam(3): those codes and others can be returned if the user or group are | |
103 | * not found. */ | |
104 | return IN_SET(code, 0, ENOENT, ESRCH, EBADF, EPERM); | |
105 | } | |
106 | ||
1b992147 LP |
107 | static int load_user_database(void) { |
108 | _cleanup_fclose_ FILE *f = NULL; | |
109 | const char *passwd_path; | |
110 | struct passwd *pw; | |
111 | int r; | |
112 | ||
1d13f648 | 113 | passwd_path = prefix_roota(arg_root, "/etc/passwd"); |
1b992147 LP |
114 | f = fopen(passwd_path, "re"); |
115 | if (!f) | |
116 | return errno == ENOENT ? 0 : -errno; | |
117 | ||
71da1673 | 118 | r = hashmap_ensure_allocated(&database_by_username, &string_hash_ops); |
1b992147 LP |
119 | if (r < 0) |
120 | return r; | |
121 | ||
71da1673 | 122 | r = hashmap_ensure_allocated(&database_by_uid, NULL); |
1b992147 LP |
123 | if (r < 0) |
124 | return r; | |
125 | ||
71da1673 | 126 | r = set_ensure_allocated(&database_users, NULL); |
1b992147 LP |
127 | if (r < 0) |
128 | return r; | |
129 | ||
100d5f6e | 130 | while ((r = fgetpwent_sane(f, &pw)) > 0) { |
43e948ee | 131 | char *n; |
340ac019 | 132 | int k, q; |
1b992147 LP |
133 | |
134 | n = strdup(pw->pw_name); | |
135 | if (!n) | |
136 | return -ENOMEM; | |
137 | ||
71da1673 YW |
138 | k = set_put(database_users, n); |
139 | if (k < 0) { | |
1b992147 LP |
140 | free(n); |
141 | return k; | |
142 | } | |
143 | ||
71da1673 YW |
144 | k = hashmap_put(database_by_username, n, UID_TO_PTR(pw->pw_uid)); |
145 | if (k < 0 && k != -EEXIST) | |
146 | return k; | |
1b992147 | 147 | |
71da1673 YW |
148 | q = hashmap_put(database_by_uid, UID_TO_PTR(pw->pw_uid), n); |
149 | if (q < 0 && q != -EEXIST) | |
150 | return q; | |
1b992147 | 151 | } |
100d5f6e | 152 | return r; |
1b992147 LP |
153 | } |
154 | ||
155 | static int load_group_database(void) { | |
156 | _cleanup_fclose_ FILE *f = NULL; | |
157 | const char *group_path; | |
158 | struct group *gr; | |
159 | int r; | |
160 | ||
1d13f648 | 161 | group_path = prefix_roota(arg_root, "/etc/group"); |
1b992147 LP |
162 | f = fopen(group_path, "re"); |
163 | if (!f) | |
164 | return errno == ENOENT ? 0 : -errno; | |
165 | ||
71da1673 | 166 | r = hashmap_ensure_allocated(&database_by_groupname, &string_hash_ops); |
1b992147 LP |
167 | if (r < 0) |
168 | return r; | |
169 | ||
71da1673 | 170 | r = hashmap_ensure_allocated(&database_by_gid, NULL); |
1b992147 LP |
171 | if (r < 0) |
172 | return r; | |
173 | ||
71da1673 | 174 | r = set_ensure_allocated(&database_groups, NULL); |
1b992147 LP |
175 | if (r < 0) |
176 | return r; | |
177 | ||
f030d36c | 178 | while ((r = fgetgrent_sane(f, &gr)) > 0) { |
1b992147 LP |
179 | char *n; |
180 | int k, q; | |
181 | ||
182 | n = strdup(gr->gr_name); | |
183 | if (!n) | |
184 | return -ENOMEM; | |
185 | ||
71da1673 YW |
186 | k = set_put(database_groups, n); |
187 | if (k < 0) { | |
1b992147 LP |
188 | free(n); |
189 | return k; | |
190 | } | |
191 | ||
71da1673 YW |
192 | k = hashmap_put(database_by_groupname, n, GID_TO_PTR(gr->gr_gid)); |
193 | if (k < 0 && k != -EEXIST) | |
194 | return k; | |
1b992147 | 195 | |
71da1673 YW |
196 | q = hashmap_put(database_by_gid, GID_TO_PTR(gr->gr_gid), n); |
197 | if (q < 0 && q != -EEXIST) | |
198 | return q; | |
1b992147 | 199 | } |
f030d36c | 200 | return r; |
1b992147 LP |
201 | } |
202 | ||
9f1c1940 ZJS |
203 | static int make_backup(const char *target, const char *x) { |
204 | _cleanup_close_ int src = -1; | |
205 | _cleanup_fclose_ FILE *dst = NULL; | |
1fb5a5ed | 206 | _cleanup_free_ char *dst_tmp = NULL; |
0a12bb1e | 207 | char *backup; |
1b992147 LP |
208 | struct timespec ts[2]; |
209 | struct stat st; | |
210 | int r; | |
211 | ||
212 | src = open(x, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
213 | if (src < 0) { | |
214 | if (errno == ENOENT) /* No backup necessary... */ | |
215 | return 0; | |
216 | ||
217 | return -errno; | |
218 | } | |
219 | ||
220 | if (fstat(src, &st) < 0) | |
221 | return -errno; | |
222 | ||
1fb5a5ed | 223 | r = fopen_temporary_label(target, x, &dst, &dst_tmp); |
9f1c1940 ZJS |
224 | if (r < 0) |
225 | return r; | |
1b992147 | 226 | |
1c876927 | 227 | r = copy_bytes(src, fileno(dst), (uint64_t) -1, COPY_REFLINK); |
1b992147 LP |
228 | if (r < 0) |
229 | goto fail; | |
230 | ||
9f1c1940 ZJS |
231 | /* Don't fail on chmod() or chown(). If it stays owned by us |
232 | * and/or unreadable by others, then it isn't too bad... */ | |
233 | ||
63c372cb | 234 | backup = strjoina(x, "-"); |
9f1c1940 | 235 | |
1b992147 | 236 | /* Copy over the access mask */ |
1fb5a5ed | 237 | r = chmod_and_chown_unsafe(dst_tmp, st.st_mode & 07777, st.st_uid, st.st_gid); |
c039af23 YW |
238 | if (r < 0) |
239 | log_warning_errno(r, "Failed to change access mode or ownership of %s: %m", backup); | |
1b992147 LP |
240 | |
241 | ts[0] = st.st_atim; | |
242 | ts[1] = st.st_mtim; | |
f06863bd | 243 | if (futimens(fileno(dst), ts) < 0) |
56f64d95 | 244 | log_warning_errno(errno, "Failed to fix access and modification time of %s: %m", backup); |
1b992147 | 245 | |
0675e94a AJ |
246 | r = fflush_sync_and_check(dst); |
247 | if (r < 0) | |
248 | goto fail; | |
249 | ||
1fb5a5ed | 250 | if (rename(dst_tmp, backup) < 0) { |
0675e94a | 251 | r = -errno; |
1b992147 | 252 | goto fail; |
0675e94a | 253 | } |
1b992147 LP |
254 | |
255 | return 0; | |
256 | ||
257 | fail: | |
1fb5a5ed | 258 | (void) unlink(dst_tmp); |
1b992147 LP |
259 | return r; |
260 | } | |
261 | ||
a12b0cc3 LP |
262 | static int putgrent_with_members(const struct group *gr, FILE *group) { |
263 | char **a; | |
264 | ||
265 | assert(gr); | |
266 | assert(group); | |
267 | ||
5bc9c980 | 268 | a = ordered_hashmap_get(members, gr->gr_name); |
a12b0cc3 LP |
269 | if (a) { |
270 | _cleanup_strv_free_ char **l = NULL; | |
271 | bool added = false; | |
272 | char **i; | |
273 | ||
274 | l = strv_copy(gr->gr_mem); | |
275 | if (!l) | |
276 | return -ENOMEM; | |
277 | ||
278 | STRV_FOREACH(i, a) { | |
279 | if (strv_find(l, *i)) | |
280 | continue; | |
281 | ||
282 | if (strv_extend(&l, *i) < 0) | |
283 | return -ENOMEM; | |
284 | ||
285 | added = true; | |
286 | } | |
287 | ||
288 | if (added) { | |
289 | struct group t; | |
100d5f6e | 290 | int r; |
a12b0cc3 LP |
291 | |
292 | strv_uniq(l); | |
293 | strv_sort(l); | |
294 | ||
295 | t = *gr; | |
296 | t.gr_mem = l; | |
297 | ||
100d5f6e FB |
298 | r = putgrent_sane(&t, group); |
299 | return r < 0 ? r : 1; | |
a12b0cc3 LP |
300 | } |
301 | } | |
302 | ||
100d5f6e | 303 | return putgrent_sane(gr, group); |
a12b0cc3 LP |
304 | } |
305 | ||
349cc4a5 | 306 | #if ENABLE_GSHADOW |
9ab315cc LP |
307 | static int putsgent_with_members(const struct sgrp *sg, FILE *gshadow) { |
308 | char **a; | |
309 | ||
310 | assert(sg); | |
311 | assert(gshadow); | |
312 | ||
5bc9c980 | 313 | a = ordered_hashmap_get(members, sg->sg_namp); |
9ab315cc LP |
314 | if (a) { |
315 | _cleanup_strv_free_ char **l = NULL; | |
316 | bool added = false; | |
317 | char **i; | |
318 | ||
319 | l = strv_copy(sg->sg_mem); | |
320 | if (!l) | |
321 | return -ENOMEM; | |
322 | ||
323 | STRV_FOREACH(i, a) { | |
324 | if (strv_find(l, *i)) | |
325 | continue; | |
326 | ||
327 | if (strv_extend(&l, *i) < 0) | |
328 | return -ENOMEM; | |
329 | ||
330 | added = true; | |
331 | } | |
332 | ||
333 | if (added) { | |
334 | struct sgrp t; | |
100d5f6e | 335 | int r; |
9ab315cc LP |
336 | |
337 | strv_uniq(l); | |
338 | strv_sort(l); | |
339 | ||
340 | t = *sg; | |
341 | t.sg_mem = l; | |
342 | ||
100d5f6e FB |
343 | r = putsgent_sane(&t, gshadow); |
344 | return r < 0 ? r : 1; | |
9ab315cc LP |
345 | } |
346 | } | |
347 | ||
100d5f6e | 348 | return putsgent_sane(sg, gshadow); |
9ab315cc | 349 | } |
b14e1b43 | 350 | #endif |
9ab315cc | 351 | |
7b1aaf66 | 352 | static const char* default_shell(uid_t uid) { |
6db90462 | 353 | return uid == 0 ? "/bin/sh" : NOLOGIN; |
7b1aaf66 ZJS |
354 | } |
355 | ||
b20b0b66 FB |
356 | static int write_temporary_passwd(const char *passwd_path, FILE **tmpfile, char **tmpfile_path) { |
357 | _cleanup_fclose_ FILE *original = NULL, *passwd = NULL; | |
1dd98a71 | 358 | _cleanup_(unlink_and_freep) char *passwd_tmp = NULL; |
563dc6f8 | 359 | struct passwd *pw = NULL; |
1b992147 LP |
360 | Iterator iterator; |
361 | Item *i; | |
362 | int r; | |
363 | ||
5bc9c980 | 364 | if (ordered_hashmap_size(todo_uids) == 0) |
b20b0b66 | 365 | return 0; |
1b992147 | 366 | |
b20b0b66 FB |
367 | r = fopen_temporary_label("/etc/passwd", passwd_path, &passwd, &passwd_tmp); |
368 | if (r < 0) | |
369 | return r; | |
370 | ||
371 | original = fopen(passwd_path, "re"); | |
372 | if (original) { | |
b20b0b66 | 373 | |
90c81688 | 374 | r = sync_rights(fileno(original), fileno(passwd)); |
1b992147 | 375 | if (r < 0) |
1dd98a71 | 376 | return r; |
1b992147 | 377 | |
100d5f6e | 378 | while ((r = fgetpwent_sane(original, &pw)) > 0) { |
1b992147 | 379 | |
5bc9c980 | 380 | i = ordered_hashmap_get(users, pw->pw_name); |
330d1def ZJS |
381 | if (i && i->todo_user) |
382 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
383 | "%s: User \"%s\" already exists.", | |
384 | passwd_path, pw->pw_name); | |
e3c72c21 | 385 | |
330d1def ZJS |
386 | if (ordered_hashmap_contains(todo_uids, UID_TO_PTR(pw->pw_uid))) |
387 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
388 | "%s: Detected collision for UID " UID_FMT ".", | |
389 | passwd_path, pw->pw_uid); | |
b20b0b66 | 390 | |
563dc6f8 FB |
391 | /* Make sure we keep the NIS entries (if any) at the end. */ |
392 | if (IN_SET(pw->pw_name[0], '+', '-')) | |
393 | break; | |
394 | ||
100d5f6e FB |
395 | r = putpwent_sane(pw, passwd); |
396 | if (r < 0) | |
397 | return r; | |
b20b0b66 | 398 | } |
100d5f6e FB |
399 | if (r < 0) |
400 | return r; | |
1dd98a71 FB |
401 | |
402 | } else { | |
403 | if (errno != ENOENT) | |
404 | return -errno; | |
405 | if (fchmod(fileno(passwd), 0644) < 0) | |
406 | return -errno; | |
b20b0b66 FB |
407 | } |
408 | ||
5bc9c980 | 409 | ORDERED_HASHMAP_FOREACH(i, todo_uids, iterator) { |
b20b0b66 FB |
410 | struct passwd n = { |
411 | .pw_name = i->name, | |
412 | .pw_uid = i->uid, | |
413 | .pw_gid = i->gid, | |
414 | .pw_gecos = i->description, | |
1b992147 | 415 | |
b20b0b66 FB |
416 | /* "x" means the password is stored in the shadow file */ |
417 | .pw_passwd = (char*) "x", | |
a12b0cc3 | 418 | |
b20b0b66 | 419 | /* We default to the root directory as home */ |
636e72bc | 420 | .pw_dir = i->home ?: (char*) "/", |
1b992147 | 421 | |
b20b0b66 FB |
422 | /* Initialize the shell to nologin, with one exception: |
423 | * for root we patch in something special */ | |
7b1aaf66 | 424 | .pw_shell = i->shell ?: (char*) default_shell(i->uid), |
b20b0b66 | 425 | }; |
9ab315cc | 426 | |
100d5f6e FB |
427 | r = putpwent_sane(&n, passwd); |
428 | if (r < 0) | |
429 | return r; | |
b20b0b66 FB |
430 | } |
431 | ||
563dc6f8 FB |
432 | /* Append the remaining NIS entries if any */ |
433 | while (pw) { | |
100d5f6e FB |
434 | r = putpwent_sane(pw, passwd); |
435 | if (r < 0) | |
436 | return r; | |
563dc6f8 | 437 | |
100d5f6e FB |
438 | r = fgetpwent_sane(original, &pw); |
439 | if (r < 0) | |
440 | return r; | |
441 | if (r == 0) | |
442 | break; | |
563dc6f8 | 443 | } |
563dc6f8 | 444 | |
b20b0b66 FB |
445 | r = fflush_and_check(passwd); |
446 | if (r < 0) | |
1dd98a71 | 447 | return r; |
b20b0b66 | 448 | |
1cc6c93a YW |
449 | *tmpfile = TAKE_PTR(passwd); |
450 | *tmpfile_path = TAKE_PTR(passwd_tmp); | |
451 | ||
b20b0b66 | 452 | return 0; |
b20b0b66 FB |
453 | } |
454 | ||
455 | static int write_temporary_shadow(const char *shadow_path, FILE **tmpfile, char **tmpfile_path) { | |
456 | _cleanup_fclose_ FILE *original = NULL, *shadow = NULL; | |
1dd98a71 | 457 | _cleanup_(unlink_and_freep) char *shadow_tmp = NULL; |
19ec7de2 | 458 | struct spwd *sp = NULL; |
b20b0b66 FB |
459 | Iterator iterator; |
460 | long lstchg; | |
461 | Item *i; | |
462 | int r; | |
463 | ||
5bc9c980 | 464 | if (ordered_hashmap_size(todo_uids) == 0) |
b20b0b66 FB |
465 | return 0; |
466 | ||
467 | r = fopen_temporary_label("/etc/shadow", shadow_path, &shadow, &shadow_tmp); | |
468 | if (r < 0) | |
469 | return r; | |
9ab315cc | 470 | |
b20b0b66 FB |
471 | lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY); |
472 | ||
473 | original = fopen(shadow_path, "re"); | |
474 | if (original) { | |
b20b0b66 | 475 | |
90c81688 | 476 | r = sync_rights(fileno(original), fileno(shadow)); |
9ab315cc | 477 | if (r < 0) |
1dd98a71 | 478 | return r; |
9ab315cc | 479 | |
100d5f6e | 480 | while ((r = fgetspent_sane(original, &sp)) > 0) { |
9ab315cc | 481 | |
5bc9c980 | 482 | i = ordered_hashmap_get(users, sp->sp_namp); |
b20b0b66 FB |
483 | if (i && i->todo_user) { |
484 | /* we will update the existing entry */ | |
485 | sp->sp_lstchg = lstchg; | |
e3c72c21 | 486 | |
b20b0b66 FB |
487 | /* only the /etc/shadow stage is left, so we can |
488 | * safely remove the item from the todo set */ | |
489 | i->todo_user = false; | |
5bc9c980 | 490 | ordered_hashmap_remove(todo_uids, UID_TO_PTR(i->uid)); |
9ab315cc | 491 | } |
b20b0b66 | 492 | |
19ec7de2 FB |
493 | /* Make sure we keep the NIS entries (if any) at the end. */ |
494 | if (IN_SET(sp->sp_namp[0], '+', '-')) | |
495 | break; | |
496 | ||
100d5f6e FB |
497 | r = putspent_sane(sp, shadow); |
498 | if (r < 0) | |
499 | return r; | |
b20b0b66 | 500 | } |
100d5f6e FB |
501 | if (r < 0) |
502 | return r; | |
1dd98a71 FB |
503 | |
504 | } else { | |
505 | if (errno != ENOENT) | |
506 | return -errno; | |
507 | if (fchmod(fileno(shadow), 0000) < 0) | |
508 | return -errno; | |
b20b0b66 | 509 | } |
9ab315cc | 510 | |
5bc9c980 | 511 | ORDERED_HASHMAP_FOREACH(i, todo_uids, iterator) { |
b20b0b66 FB |
512 | struct spwd n = { |
513 | .sp_namp = i->name, | |
fc58c0c7 | 514 | .sp_pwdp = (char*) "!*", /* lock this password, and make it invalid */ |
b20b0b66 FB |
515 | .sp_lstchg = lstchg, |
516 | .sp_min = -1, | |
517 | .sp_max = -1, | |
518 | .sp_warn = -1, | |
519 | .sp_inact = -1, | |
12c82937 | 520 | .sp_expire = -1, |
b20b0b66 FB |
521 | .sp_flag = (unsigned long) -1, /* this appears to be what everybody does ... */ |
522 | }; | |
9ab315cc | 523 | |
100d5f6e FB |
524 | r = putspent_sane(&n, shadow); |
525 | if (r < 0) | |
526 | return r; | |
1b992147 | 527 | } |
19ec7de2 FB |
528 | |
529 | /* Append the remaining NIS entries if any */ | |
530 | while (sp) { | |
100d5f6e FB |
531 | r = putspent_sane(sp, shadow); |
532 | if (r < 0) | |
533 | return r; | |
19ec7de2 | 534 | |
100d5f6e FB |
535 | r = fgetspent_sane(original, &sp); |
536 | if (r < 0) | |
537 | return r; | |
538 | if (r == 0) | |
539 | break; | |
19ec7de2 FB |
540 | } |
541 | if (!IN_SET(errno, 0, ENOENT)) | |
542 | return -errno; | |
1b992147 | 543 | |
0675e94a | 544 | r = fflush_sync_and_check(shadow); |
b20b0b66 | 545 | if (r < 0) |
1dd98a71 | 546 | return r; |
b20b0b66 | 547 | |
1cc6c93a YW |
548 | *tmpfile = TAKE_PTR(shadow); |
549 | *tmpfile_path = TAKE_PTR(shadow_tmp); | |
550 | ||
b20b0b66 | 551 | return 0; |
b20b0b66 | 552 | } |
1b992147 | 553 | |
b20b0b66 FB |
554 | static int write_temporary_group(const char *group_path, FILE **tmpfile, char **tmpfile_path) { |
555 | _cleanup_fclose_ FILE *original = NULL, *group = NULL; | |
1dd98a71 | 556 | _cleanup_(unlink_and_freep) char *group_tmp = NULL; |
b20b0b66 | 557 | bool group_changed = false; |
563dc6f8 | 558 | struct group *gr = NULL; |
b20b0b66 FB |
559 | Iterator iterator; |
560 | Item *i; | |
561 | int r; | |
1b992147 | 562 | |
5bc9c980 | 563 | if (ordered_hashmap_size(todo_gids) == 0 && ordered_hashmap_size(members) == 0) |
b20b0b66 | 564 | return 0; |
e3c72c21 | 565 | |
b20b0b66 FB |
566 | r = fopen_temporary_label("/etc/group", group_path, &group, &group_tmp); |
567 | if (r < 0) | |
568 | return r; | |
569 | ||
570 | original = fopen(group_path, "re"); | |
571 | if (original) { | |
b20b0b66 | 572 | |
90c81688 | 573 | r = sync_rights(fileno(original), fileno(group)); |
b20b0b66 | 574 | if (r < 0) |
1dd98a71 | 575 | return r; |
b20b0b66 | 576 | |
100d5f6e | 577 | while ((r = fgetgrent_sane(original, &gr)) > 0) { |
b20b0b66 FB |
578 | /* Safety checks against name and GID collisions. Normally, |
579 | * this should be unnecessary, but given that we look at the | |
580 | * entries anyway here, let's make an extra verification | |
581 | * step that we don't generate duplicate entries. */ | |
582 | ||
5bc9c980 | 583 | i = ordered_hashmap_get(groups, gr->gr_name); |
330d1def ZJS |
584 | if (i && i->todo_group) |
585 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
586 | "%s: Group \"%s\" already exists.", | |
587 | group_path, gr->gr_name); | |
b20b0b66 | 588 | |
330d1def ZJS |
589 | if (ordered_hashmap_contains(todo_gids, GID_TO_PTR(gr->gr_gid))) |
590 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
591 | "%s: Detected collision for GID " GID_FMT ".", | |
592 | group_path, gr->gr_gid); | |
1b992147 | 593 | |
563dc6f8 FB |
594 | /* Make sure we keep the NIS entries (if any) at the end. */ |
595 | if (IN_SET(gr->gr_name[0], '+', '-')) | |
596 | break; | |
597 | ||
b20b0b66 FB |
598 | r = putgrent_with_members(gr, group); |
599 | if (r < 0) | |
1dd98a71 | 600 | return r; |
b20b0b66 FB |
601 | if (r > 0) |
602 | group_changed = true; | |
b20b0b66 | 603 | } |
100d5f6e FB |
604 | if (r < 0) |
605 | return r; | |
1dd98a71 FB |
606 | |
607 | } else { | |
608 | if (errno != ENOENT) | |
609 | return -errno; | |
610 | if (fchmod(fileno(group), 0644) < 0) | |
611 | return -errno; | |
b20b0b66 | 612 | } |
7629889c | 613 | |
5bc9c980 | 614 | ORDERED_HASHMAP_FOREACH(i, todo_gids, iterator) { |
b20b0b66 FB |
615 | struct group n = { |
616 | .gr_name = i->name, | |
617 | .gr_gid = i->gid, | |
618 | .gr_passwd = (char*) "x", | |
619 | }; | |
1b992147 | 620 | |
b20b0b66 FB |
621 | r = putgrent_with_members(&n, group); |
622 | if (r < 0) | |
1dd98a71 | 623 | return r; |
7629889c | 624 | |
b20b0b66 FB |
625 | group_changed = true; |
626 | } | |
1b992147 | 627 | |
563dc6f8 FB |
628 | /* Append the remaining NIS entries if any */ |
629 | while (gr) { | |
100d5f6e FB |
630 | r = putgrent_sane(gr, group); |
631 | if (r < 0) | |
632 | return r; | |
563dc6f8 | 633 | |
100d5f6e FB |
634 | r = fgetgrent_sane(original, &gr); |
635 | if (r < 0) | |
636 | return r; | |
637 | if (r == 0) | |
638 | break; | |
563dc6f8 | 639 | } |
563dc6f8 | 640 | |
0675e94a | 641 | r = fflush_sync_and_check(group); |
b20b0b66 | 642 | if (r < 0) |
1dd98a71 | 643 | return r; |
1b992147 | 644 | |
b20b0b66 | 645 | if (group_changed) { |
1cc6c93a YW |
646 | *tmpfile = TAKE_PTR(group); |
647 | *tmpfile_path = TAKE_PTR(group_tmp); | |
b20b0b66 FB |
648 | } |
649 | return 0; | |
b20b0b66 FB |
650 | } |
651 | ||
652 | static int write_temporary_gshadow(const char * gshadow_path, FILE **tmpfile, char **tmpfile_path) { | |
349cc4a5 | 653 | #if ENABLE_GSHADOW |
b20b0b66 | 654 | _cleanup_fclose_ FILE *original = NULL, *gshadow = NULL; |
1dd98a71 | 655 | _cleanup_(unlink_and_freep) char *gshadow_tmp = NULL; |
b20b0b66 FB |
656 | bool group_changed = false; |
657 | Iterator iterator; | |
658 | Item *i; | |
659 | int r; | |
660 | ||
5bc9c980 | 661 | if (ordered_hashmap_size(todo_gids) == 0 && ordered_hashmap_size(members) == 0) |
b20b0b66 FB |
662 | return 0; |
663 | ||
664 | r = fopen_temporary_label("/etc/gshadow", gshadow_path, &gshadow, &gshadow_tmp); | |
665 | if (r < 0) | |
666 | return r; | |
9ab315cc | 667 | |
b20b0b66 FB |
668 | original = fopen(gshadow_path, "re"); |
669 | if (original) { | |
670 | struct sgrp *sg; | |
671 | ||
90c81688 | 672 | r = sync_rights(fileno(original), fileno(gshadow)); |
1b992147 | 673 | if (r < 0) |
1dd98a71 | 674 | return r; |
9ab315cc | 675 | |
100d5f6e | 676 | while ((r = fgetsgent_sane(original, &sg)) > 0) { |
c5abf225 | 677 | |
5bc9c980 | 678 | i = ordered_hashmap_get(groups, sg->sg_namp); |
330d1def ZJS |
679 | if (i && i->todo_group) |
680 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
681 | "%s: Group \"%s\" already exists.", | |
682 | gshadow_path, sg->sg_namp); | |
9ab315cc | 683 | |
b20b0b66 | 684 | r = putsgent_with_members(sg, gshadow); |
fff19499 | 685 | if (r < 0) |
1dd98a71 | 686 | return r; |
b20b0b66 FB |
687 | if (r > 0) |
688 | group_changed = true; | |
b20b0b66 | 689 | } |
100d5f6e FB |
690 | if (r < 0) |
691 | return r; | |
1dd98a71 FB |
692 | |
693 | } else { | |
694 | if (errno != ENOENT) | |
695 | return -errno; | |
696 | if (fchmod(fileno(gshadow), 0000) < 0) | |
697 | return -errno; | |
b20b0b66 | 698 | } |
9ab315cc | 699 | |
5bc9c980 | 700 | ORDERED_HASHMAP_FOREACH(i, todo_gids, iterator) { |
b20b0b66 FB |
701 | struct sgrp n = { |
702 | .sg_namp = i->name, | |
703 | .sg_passwd = (char*) "!!", | |
704 | }; | |
9ab315cc | 705 | |
b20b0b66 | 706 | r = putsgent_with_members(&n, gshadow); |
9ab315cc | 707 | if (r < 0) |
1dd98a71 | 708 | return r; |
b20b0b66 FB |
709 | |
710 | group_changed = true; | |
9ab315cc LP |
711 | } |
712 | ||
0675e94a | 713 | r = fflush_sync_and_check(gshadow); |
b20b0b66 | 714 | if (r < 0) |
1dd98a71 | 715 | return r; |
b20b0b66 | 716 | |
9ab315cc | 717 | if (group_changed) { |
1cc6c93a YW |
718 | *tmpfile = TAKE_PTR(gshadow); |
719 | *tmpfile_path = TAKE_PTR(gshadow_tmp); | |
b20b0b66 FB |
720 | } |
721 | return 0; | |
b14e1b43 FB |
722 | #else |
723 | return 0; | |
724 | #endif | |
b20b0b66 FB |
725 | } |
726 | ||
727 | static int write_files(void) { | |
b20b0b66 | 728 | _cleanup_fclose_ FILE *passwd = NULL, *group = NULL, *shadow = NULL, *gshadow = NULL; |
1dd98a71 | 729 | _cleanup_(unlink_and_freep) char *passwd_tmp = NULL, *group_tmp = NULL, *shadow_tmp = NULL, *gshadow_tmp = NULL; |
b20b0b66 FB |
730 | const char *passwd_path = NULL, *group_path = NULL, *shadow_path = NULL, *gshadow_path = NULL; |
731 | int r; | |
732 | ||
733 | passwd_path = prefix_roota(arg_root, "/etc/passwd"); | |
734 | shadow_path = prefix_roota(arg_root, "/etc/shadow"); | |
735 | group_path = prefix_roota(arg_root, "/etc/group"); | |
736 | gshadow_path = prefix_roota(arg_root, "/etc/gshadow"); | |
737 | ||
738 | r = write_temporary_group(group_path, &group, &group_tmp); | |
739 | if (r < 0) | |
1dd98a71 | 740 | return r; |
b20b0b66 FB |
741 | |
742 | r = write_temporary_gshadow(gshadow_path, &gshadow, &gshadow_tmp); | |
743 | if (r < 0) | |
1dd98a71 | 744 | return r; |
b20b0b66 FB |
745 | |
746 | r = write_temporary_passwd(passwd_path, &passwd, &passwd_tmp); | |
747 | if (r < 0) | |
1dd98a71 | 748 | return r; |
b20b0b66 FB |
749 | |
750 | r = write_temporary_shadow(shadow_path, &shadow, &shadow_tmp); | |
751 | if (r < 0) | |
1dd98a71 | 752 | return r; |
b20b0b66 FB |
753 | |
754 | /* Make a backup of the old files */ | |
755 | if (group) { | |
756 | r = make_backup("/etc/group", group_path); | |
757 | if (r < 0) | |
1dd98a71 | 758 | return r; |
b20b0b66 FB |
759 | } |
760 | if (gshadow) { | |
761 | r = make_backup("/etc/gshadow", gshadow_path); | |
762 | if (r < 0) | |
1dd98a71 | 763 | return r; |
1b992147 LP |
764 | } |
765 | ||
766 | if (passwd) { | |
9f1c1940 | 767 | r = make_backup("/etc/passwd", passwd_path); |
1b992147 | 768 | if (r < 0) |
1dd98a71 | 769 | return r; |
1b992147 | 770 | } |
9ab315cc LP |
771 | if (shadow) { |
772 | r = make_backup("/etc/shadow", shadow_path); | |
773 | if (r < 0) | |
1dd98a71 | 774 | return r; |
9ab315cc | 775 | } |
1b992147 LP |
776 | |
777 | /* And make the new files count */ | |
b20b0b66 | 778 | if (group) { |
2da3dc69 | 779 | r = rename_and_apply_smack_floor_label(group_tmp, group_path); |
b20b0b66 | 780 | if (r < 0) |
1dd98a71 | 781 | return r; |
9ab315cc | 782 | |
b20b0b66 FB |
783 | group_tmp = mfree(group_tmp); |
784 | } | |
785 | if (gshadow) { | |
2da3dc69 | 786 | r = rename_and_apply_smack_floor_label(gshadow_tmp, gshadow_path); |
b20b0b66 | 787 | if (r < 0) |
1dd98a71 | 788 | return r; |
1b992147 | 789 | |
b20b0b66 | 790 | gshadow_tmp = mfree(gshadow_tmp); |
1b992147 LP |
791 | } |
792 | ||
793 | if (passwd) { | |
2da3dc69 | 794 | r = rename_and_apply_smack_floor_label(passwd_tmp, passwd_path); |
c02e7b1e | 795 | if (r < 0) |
1dd98a71 | 796 | return r; |
1b992147 | 797 | |
97b11eed | 798 | passwd_tmp = mfree(passwd_tmp); |
1b992147 | 799 | } |
9ab315cc | 800 | if (shadow) { |
2da3dc69 | 801 | r = rename_and_apply_smack_floor_label(shadow_tmp, shadow_path); |
c02e7b1e | 802 | if (r < 0) |
1dd98a71 | 803 | return r; |
9ab315cc | 804 | |
97b11eed | 805 | shadow_tmp = mfree(shadow_tmp); |
9ab315cc | 806 | } |
1b992147 | 807 | |
1dd98a71 | 808 | return 0; |
1b992147 LP |
809 | } |
810 | ||
b9ee05c2 | 811 | static int uid_is_ok(uid_t uid, const char *name, bool check_with_gid) { |
1b992147 LP |
812 | struct passwd *p; |
813 | struct group *g; | |
814 | const char *n; | |
815 | Item *i; | |
816 | ||
817 | /* Let's see if we already have assigned the UID a second time */ | |
5bc9c980 | 818 | if (ordered_hashmap_get(todo_uids, UID_TO_PTR(uid))) |
1b992147 LP |
819 | return 0; |
820 | ||
821 | /* Try to avoid using uids that are already used by a group | |
822 | * that doesn't have the same name as our new user. */ | |
b9ee05c2 MV |
823 | if (check_with_gid) { |
824 | i = ordered_hashmap_get(todo_gids, GID_TO_PTR(uid)); | |
825 | if (i && !streq(i->name, name)) | |
826 | return 0; | |
827 | } | |
1b992147 LP |
828 | |
829 | /* Let's check the files directly */ | |
71da1673 | 830 | if (hashmap_contains(database_by_uid, UID_TO_PTR(uid))) |
1b992147 LP |
831 | return 0; |
832 | ||
b9ee05c2 | 833 | if (check_with_gid) { |
71da1673 | 834 | n = hashmap_get(database_by_gid, GID_TO_PTR(uid)); |
b9ee05c2 MV |
835 | if (n && !streq(n, name)) |
836 | return 0; | |
837 | } | |
1b992147 LP |
838 | |
839 | /* Let's also check via NSS, to avoid UID clashes over LDAP and such, just in case */ | |
840 | if (!arg_root) { | |
841 | errno = 0; | |
842 | p = getpwuid(uid); | |
843 | if (p) | |
844 | return 0; | |
b0284aba | 845 | if (!IN_SET(errno, 0, ENOENT)) |
1b992147 LP |
846 | return -errno; |
847 | ||
b9ee05c2 MV |
848 | if (check_with_gid) { |
849 | errno = 0; | |
850 | g = getgrgid((gid_t) uid); | |
851 | if (g) { | |
852 | if (!streq(g->gr_name, name)) | |
853 | return 0; | |
854 | } else if (!IN_SET(errno, 0, ENOENT)) | |
855 | return -errno; | |
856 | } | |
1b992147 LP |
857 | } |
858 | ||
859 | return 1; | |
860 | } | |
861 | ||
862 | static int root_stat(const char *p, struct stat *st) { | |
863 | const char *fix; | |
864 | ||
1d13f648 | 865 | fix = prefix_roota(arg_root, p); |
1b992147 LP |
866 | if (stat(fix, st) < 0) |
867 | return -errno; | |
868 | ||
869 | return 0; | |
870 | } | |
871 | ||
872 | static int read_id_from_file(Item *i, uid_t *_uid, gid_t *_gid) { | |
873 | struct stat st; | |
874 | bool found_uid = false, found_gid = false; | |
56d21cde PDS |
875 | uid_t uid = 0; |
876 | gid_t gid = 0; | |
1b992147 LP |
877 | |
878 | assert(i); | |
879 | ||
880 | /* First, try to get the gid directly */ | |
881 | if (_gid && i->gid_path && root_stat(i->gid_path, &st) >= 0) { | |
882 | gid = st.st_gid; | |
883 | found_gid = true; | |
884 | } | |
885 | ||
886 | /* Then, try to get the uid directly */ | |
887 | if ((_uid || (_gid && !found_gid)) | |
888 | && i->uid_path | |
889 | && root_stat(i->uid_path, &st) >= 0) { | |
890 | ||
891 | uid = st.st_uid; | |
892 | found_uid = true; | |
893 | ||
894 | /* If we need the gid, but had no success yet, also derive it from the uid path */ | |
895 | if (_gid && !found_gid) { | |
896 | gid = st.st_gid; | |
897 | found_gid = true; | |
898 | } | |
899 | } | |
900 | ||
901 | /* If that didn't work yet, then let's reuse the gid as uid */ | |
902 | if (_uid && !found_uid && i->gid_path) { | |
903 | ||
904 | if (found_gid) { | |
905 | uid = (uid_t) gid; | |
906 | found_uid = true; | |
907 | } else if (root_stat(i->gid_path, &st) >= 0) { | |
908 | uid = (uid_t) st.st_gid; | |
909 | found_uid = true; | |
910 | } | |
911 | } | |
912 | ||
913 | if (_uid) { | |
914 | if (!found_uid) | |
915 | return 0; | |
916 | ||
917 | *_uid = uid; | |
918 | } | |
919 | ||
920 | if (_gid) { | |
921 | if (!found_gid) | |
922 | return 0; | |
923 | ||
924 | *_gid = gid; | |
925 | } | |
926 | ||
927 | return 1; | |
928 | } | |
929 | ||
930 | static int add_user(Item *i) { | |
931 | void *z; | |
932 | int r; | |
933 | ||
934 | assert(i); | |
935 | ||
936 | /* Check the database directly */ | |
71da1673 | 937 | z = hashmap_get(database_by_username, i->name); |
1b992147 LP |
938 | if (z) { |
939 | log_debug("User %s already exists.", i->name); | |
c1b6b04f | 940 | i->uid = PTR_TO_UID(z); |
1b992147 LP |
941 | i->uid_set = true; |
942 | return 0; | |
943 | } | |
944 | ||
945 | if (!arg_root) { | |
946 | struct passwd *p; | |
1b992147 LP |
947 | |
948 | /* Also check NSS */ | |
949 | errno = 0; | |
950 | p = getpwnam(i->name); | |
951 | if (p) { | |
952 | log_debug("User %s already exists.", i->name); | |
953 | i->uid = p->pw_uid; | |
954 | i->uid_set = true; | |
955 | ||
2fc09a9c DM |
956 | r = free_and_strdup(&i->description, p->pw_gecos); |
957 | if (r < 0) | |
958 | return log_oom(); | |
959 | ||
1b992147 LP |
960 | return 0; |
961 | } | |
08c7c321 | 962 | if (!errno_is_not_exists(errno)) |
4a62c710 | 963 | return log_error_errno(errno, "Failed to check if user %s already exists: %m", i->name); |
1b992147 LP |
964 | } |
965 | ||
966 | /* Try to use the suggested numeric uid */ | |
967 | if (i->uid_set) { | |
b9ee05c2 | 968 | r = uid_is_ok(i->uid, i->name, !i->id_set_strict); |
f647962d MS |
969 | if (r < 0) |
970 | return log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid); | |
1b992147 LP |
971 | if (r == 0) { |
972 | log_debug("Suggested user ID " UID_FMT " for %s already used.", i->uid, i->name); | |
973 | i->uid_set = false; | |
974 | } | |
975 | } | |
976 | ||
977 | /* If that didn't work, try to read it from the specified path */ | |
978 | if (!i->uid_set) { | |
979 | uid_t c; | |
980 | ||
981 | if (read_id_from_file(i, &c, NULL) > 0) { | |
982 | ||
8530dc44 | 983 | if (c <= 0 || !uid_range_contains(uid_range, n_uid_range, c)) |
1b992147 LP |
984 | log_debug("User ID " UID_FMT " of file not suitable for %s.", c, i->name); |
985 | else { | |
b9ee05c2 | 986 | r = uid_is_ok(c, i->name, true); |
f647962d MS |
987 | if (r < 0) |
988 | return log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid); | |
989 | else if (r > 0) { | |
1b992147 LP |
990 | i->uid = c; |
991 | i->uid_set = true; | |
992 | } else | |
993 | log_debug("User ID " UID_FMT " of file for %s is already used.", c, i->name); | |
994 | } | |
995 | } | |
996 | } | |
997 | ||
b938cb90 | 998 | /* Otherwise, try to reuse the group ID */ |
1b992147 | 999 | if (!i->uid_set && i->gid_set) { |
b9ee05c2 | 1000 | r = uid_is_ok((uid_t) i->gid, i->name, true); |
f647962d MS |
1001 | if (r < 0) |
1002 | return log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid); | |
1b992147 LP |
1003 | if (r > 0) { |
1004 | i->uid = (uid_t) i->gid; | |
1005 | i->uid_set = true; | |
1006 | } | |
1007 | } | |
1008 | ||
1009 | /* And if that didn't work either, let's try to find a free one */ | |
1010 | if (!i->uid_set) { | |
8530dc44 LP |
1011 | for (;;) { |
1012 | r = uid_range_next_lower(uid_range, n_uid_range, &search_uid); | |
330d1def ZJS |
1013 | if (r < 0) |
1014 | return log_error_errno(r, "No free user ID available for %s.", i->name); | |
1b992147 | 1015 | |
b9ee05c2 | 1016 | r = uid_is_ok(search_uid, i->name, true); |
f647962d MS |
1017 | if (r < 0) |
1018 | return log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid); | |
1019 | else if (r > 0) | |
1b992147 LP |
1020 | break; |
1021 | } | |
1022 | ||
1b992147 LP |
1023 | i->uid_set = true; |
1024 | i->uid = search_uid; | |
1b992147 LP |
1025 | } |
1026 | ||
5bc9c980 | 1027 | r = ordered_hashmap_ensure_allocated(&todo_uids, NULL); |
1b992147 LP |
1028 | if (r < 0) |
1029 | return log_oom(); | |
1030 | ||
5bc9c980 | 1031 | r = ordered_hashmap_put(todo_uids, UID_TO_PTR(i->uid), i); |
1b992147 LP |
1032 | if (r < 0) |
1033 | return log_oom(); | |
1034 | ||
c1b6b04f | 1035 | i->todo_user = true; |
1b992147 LP |
1036 | log_info("Creating user %s (%s) with uid " UID_FMT " and gid " GID_FMT ".", i->name, strna(i->description), i->uid, i->gid); |
1037 | ||
1038 | return 0; | |
1039 | } | |
1040 | ||
1041 | static int gid_is_ok(gid_t gid) { | |
1042 | struct group *g; | |
1043 | struct passwd *p; | |
1044 | ||
5bc9c980 | 1045 | if (ordered_hashmap_get(todo_gids, GID_TO_PTR(gid))) |
1b992147 LP |
1046 | return 0; |
1047 | ||
1048 | /* Avoid reusing gids that are already used by a different user */ | |
5bc9c980 | 1049 | if (ordered_hashmap_get(todo_uids, UID_TO_PTR(gid))) |
1b992147 LP |
1050 | return 0; |
1051 | ||
71da1673 | 1052 | if (hashmap_contains(database_by_gid, GID_TO_PTR(gid))) |
1b992147 LP |
1053 | return 0; |
1054 | ||
71da1673 | 1055 | if (hashmap_contains(database_by_uid, UID_TO_PTR(gid))) |
1b992147 LP |
1056 | return 0; |
1057 | ||
1058 | if (!arg_root) { | |
1059 | errno = 0; | |
1060 | g = getgrgid(gid); | |
1061 | if (g) | |
1062 | return 0; | |
b0284aba | 1063 | if (!IN_SET(errno, 0, ENOENT)) |
1b992147 LP |
1064 | return -errno; |
1065 | ||
1066 | errno = 0; | |
1067 | p = getpwuid((uid_t) gid); | |
1068 | if (p) | |
1069 | return 0; | |
b0284aba | 1070 | if (!IN_SET(errno, 0, ENOENT)) |
1b992147 LP |
1071 | return -errno; |
1072 | } | |
1073 | ||
1074 | return 1; | |
1075 | } | |
1076 | ||
649916d3 | 1077 | static int get_gid_by_name(const char *name, gid_t *gid) { |
1b992147 | 1078 | void *z; |
1b992147 | 1079 | |
649916d3 | 1080 | assert(gid); |
1b992147 LP |
1081 | |
1082 | /* Check the database directly */ | |
649916d3 | 1083 | z = hashmap_get(database_by_groupname, name); |
1b992147 | 1084 | if (z) { |
649916d3 | 1085 | *gid = PTR_TO_GID(z); |
1b992147 LP |
1086 | return 0; |
1087 | } | |
1088 | ||
1089 | /* Also check NSS */ | |
1090 | if (!arg_root) { | |
1091 | struct group *g; | |
1092 | ||
1093 | errno = 0; | |
649916d3 | 1094 | g = getgrnam(name); |
1b992147 | 1095 | if (g) { |
649916d3 | 1096 | *gid = g->gr_gid; |
1b992147 LP |
1097 | return 0; |
1098 | } | |
08c7c321 | 1099 | if (!errno_is_not_exists(errno)) |
649916d3 DM |
1100 | return log_error_errno(errno, "Failed to check if group %s already exists: %m", name); |
1101 | } | |
1102 | ||
1103 | return -ENOENT; | |
1104 | } | |
1105 | ||
1106 | static int add_group(Item *i) { | |
1107 | int r; | |
1108 | ||
1109 | assert(i); | |
1110 | ||
1111 | r = get_gid_by_name(i->name, &i->gid); | |
1112 | if (r != -ENOENT) { | |
1113 | if (r < 0) | |
1114 | return r; | |
1115 | log_debug("Group %s already exists.", i->name); | |
1116 | i->gid_set = true; | |
1117 | return 0; | |
1b992147 LP |
1118 | } |
1119 | ||
1120 | /* Try to use the suggested numeric gid */ | |
1121 | if (i->gid_set) { | |
1122 | r = gid_is_ok(i->gid); | |
f647962d MS |
1123 | if (r < 0) |
1124 | return log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid); | |
b9ee05c2 | 1125 | if (i->id_set_strict) { |
28e7fad7 MV |
1126 | /* If we require the gid to already exist we can return here: |
1127 | * r > 0: means the gid does not exist -> fail | |
1128 | * r == 0: means the gid exists -> nothing more to do. | |
1129 | */ | |
baaa35ad ZJS |
1130 | if (r > 0) |
1131 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1132 | "Failed to create %s: please create GID %d", | |
1133 | i->name, i->gid); | |
28e7fad7 MV |
1134 | if (r == 0) |
1135 | return 0; | |
1136 | } | |
1b992147 LP |
1137 | if (r == 0) { |
1138 | log_debug("Suggested group ID " GID_FMT " for %s already used.", i->gid, i->name); | |
1139 | i->gid_set = false; | |
1140 | } | |
1141 | } | |
1142 | ||
1143 | /* Try to reuse the numeric uid, if there's one */ | |
1144 | if (!i->gid_set && i->uid_set) { | |
1145 | r = gid_is_ok((gid_t) i->uid); | |
f647962d MS |
1146 | if (r < 0) |
1147 | return log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid); | |
1b992147 LP |
1148 | if (r > 0) { |
1149 | i->gid = (gid_t) i->uid; | |
1150 | i->gid_set = true; | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | /* If that didn't work, try to read it from the specified path */ | |
1155 | if (!i->gid_set) { | |
1156 | gid_t c; | |
1157 | ||
1158 | if (read_id_from_file(i, NULL, &c) > 0) { | |
1159 | ||
8530dc44 | 1160 | if (c <= 0 || !uid_range_contains(uid_range, n_uid_range, c)) |
1b992147 LP |
1161 | log_debug("Group ID " GID_FMT " of file not suitable for %s.", c, i->name); |
1162 | else { | |
1163 | r = gid_is_ok(c); | |
f647962d MS |
1164 | if (r < 0) |
1165 | return log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid); | |
1166 | else if (r > 0) { | |
1b992147 LP |
1167 | i->gid = c; |
1168 | i->gid_set = true; | |
1169 | } else | |
1170 | log_debug("Group ID " GID_FMT " of file for %s already used.", c, i->name); | |
1171 | } | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | /* And if that didn't work either, let's try to find a free one */ | |
1176 | if (!i->gid_set) { | |
8530dc44 LP |
1177 | for (;;) { |
1178 | /* We look for new GIDs in the UID pool! */ | |
1179 | r = uid_range_next_lower(uid_range, n_uid_range, &search_uid); | |
330d1def ZJS |
1180 | if (r < 0) |
1181 | return log_error_errno(r, "No free group ID available for %s.", i->name); | |
8530dc44 LP |
1182 | |
1183 | r = gid_is_ok(search_uid); | |
f647962d MS |
1184 | if (r < 0) |
1185 | return log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid); | |
1186 | else if (r > 0) | |
1b992147 LP |
1187 | break; |
1188 | } | |
1189 | ||
1b992147 | 1190 | i->gid_set = true; |
8530dc44 | 1191 | i->gid = search_uid; |
1b992147 LP |
1192 | } |
1193 | ||
5bc9c980 | 1194 | r = ordered_hashmap_ensure_allocated(&todo_gids, NULL); |
1b992147 LP |
1195 | if (r < 0) |
1196 | return log_oom(); | |
1197 | ||
5bc9c980 | 1198 | r = ordered_hashmap_put(todo_gids, GID_TO_PTR(i->gid), i); |
1b992147 LP |
1199 | if (r < 0) |
1200 | return log_oom(); | |
1201 | ||
c1b6b04f | 1202 | i->todo_group = true; |
1b992147 LP |
1203 | log_info("Creating group %s with gid " GID_FMT ".", i->name, i->gid); |
1204 | ||
1205 | return 0; | |
1206 | } | |
1207 | ||
1208 | static int process_item(Item *i) { | |
1209 | int r; | |
1210 | ||
1211 | assert(i); | |
1212 | ||
1213 | switch (i->type) { | |
1214 | ||
b5327d0a YW |
1215 | case ADD_USER: { |
1216 | Item *j; | |
1217 | ||
649916d3 | 1218 | j = ordered_hashmap_get(groups, i->group_name ?: i->name); |
b5327d0a | 1219 | if (j && j->todo_group) { |
649916d3 | 1220 | /* When a group with the target name is already in queue, |
b5327d0a YW |
1221 | * use the information about the group and do not create |
1222 | * duplicated group entry. */ | |
1223 | i->gid_set = j->gid_set; | |
1224 | i->gid = j->gid; | |
1225 | i->id_set_strict = true; | |
649916d3 DM |
1226 | } else if (i->group_name) { |
1227 | /* When a group name was given instead of a GID and it's | |
1228 | * not in queue, then it must already exist. */ | |
1229 | r = get_gid_by_name(i->group_name, &i->gid); | |
1230 | if (r < 0) | |
1231 | return log_error_errno(r, "Group %s not found.", i->group_name); | |
1232 | i->gid_set = true; | |
1233 | i->id_set_strict = true; | |
b5327d0a YW |
1234 | } else { |
1235 | r = add_group(i); | |
1236 | if (r < 0) | |
1237 | return r; | |
1238 | } | |
1b992147 LP |
1239 | |
1240 | return add_user(i); | |
b5327d0a | 1241 | } |
1b992147 | 1242 | |
e2c2060f | 1243 | case ADD_GROUP: |
1b992147 | 1244 | return add_group(i); |
1b992147 | 1245 | |
a12b0cc3 LP |
1246 | default: |
1247 | assert_not_reached("Unknown item type"); | |
1248 | } | |
1b992147 LP |
1249 | } |
1250 | ||
71da1673 | 1251 | static Item* item_free(Item *i) { |
1b992147 | 1252 | if (!i) |
71da1673 | 1253 | return NULL; |
1b992147 LP |
1254 | |
1255 | free(i->name); | |
649916d3 | 1256 | free(i->group_name); |
1b992147 LP |
1257 | free(i->uid_path); |
1258 | free(i->gid_path); | |
1259 | free(i->description); | |
d9b8ea54 | 1260 | free(i->home); |
7b1aaf66 | 1261 | free(i->shell); |
71da1673 | 1262 | return mfree(i); |
1b992147 LP |
1263 | } |
1264 | ||
1265 | DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free); | |
71da1673 | 1266 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_hash_ops, char, string_hash_func, string_compare_func, Item, item_free); |
1b992147 | 1267 | |
a12b0cc3 LP |
1268 | static int add_implicit(void) { |
1269 | char *g, **l; | |
1270 | Iterator iterator; | |
1271 | int r; | |
1272 | ||
1273 | /* Implicitly create additional users and groups, if they were listed in "m" lines */ | |
5bc9c980 | 1274 | ORDERED_HASHMAP_FOREACH_KEY(l, g, members, iterator) { |
a12b0cc3 LP |
1275 | char **m; |
1276 | ||
d4f0412d YW |
1277 | STRV_FOREACH(m, l) |
1278 | if (!ordered_hashmap_get(users, *m)) { | |
a12b0cc3 LP |
1279 | _cleanup_(item_freep) Item *j = NULL; |
1280 | ||
71da1673 | 1281 | r = ordered_hashmap_ensure_allocated(&users, &item_hash_ops); |
a12b0cc3 LP |
1282 | if (r < 0) |
1283 | return log_oom(); | |
1284 | ||
1285 | j = new0(Item, 1); | |
1286 | if (!j) | |
1287 | return log_oom(); | |
1288 | ||
1289 | j->type = ADD_USER; | |
1290 | j->name = strdup(*m); | |
1291 | if (!j->name) | |
1292 | return log_oom(); | |
1293 | ||
5bc9c980 | 1294 | r = ordered_hashmap_put(users, j->name, j); |
a12b0cc3 LP |
1295 | if (r < 0) |
1296 | return log_oom(); | |
1297 | ||
1298 | log_debug("Adding implicit user '%s' due to m line", j->name); | |
1299 | j = NULL; | |
1300 | } | |
d4f0412d YW |
1301 | |
1302 | if (!(ordered_hashmap_get(users, g) || | |
1303 | ordered_hashmap_get(groups, g))) { | |
1304 | _cleanup_(item_freep) Item *j = NULL; | |
1305 | ||
71da1673 | 1306 | r = ordered_hashmap_ensure_allocated(&groups, &item_hash_ops); |
d4f0412d YW |
1307 | if (r < 0) |
1308 | return log_oom(); | |
1309 | ||
1310 | j = new0(Item, 1); | |
1311 | if (!j) | |
1312 | return log_oom(); | |
1313 | ||
1314 | j->type = ADD_GROUP; | |
1315 | j->name = strdup(g); | |
1316 | if (!j->name) | |
1317 | return log_oom(); | |
1318 | ||
1319 | r = ordered_hashmap_put(groups, j->name, j); | |
1320 | if (r < 0) | |
1321 | return log_oom(); | |
1322 | ||
1323 | log_debug("Adding implicit group '%s' due to m line", j->name); | |
1324 | j = NULL; | |
a12b0cc3 LP |
1325 | } |
1326 | } | |
1327 | ||
1328 | return 0; | |
1329 | } | |
1330 | ||
1b992147 LP |
1331 | static bool item_equal(Item *a, Item *b) { |
1332 | assert(a); | |
1333 | assert(b); | |
1334 | ||
1335 | if (a->type != b->type) | |
1336 | return false; | |
1337 | ||
1338 | if (!streq_ptr(a->name, b->name)) | |
1339 | return false; | |
1340 | ||
1341 | if (!streq_ptr(a->uid_path, b->uid_path)) | |
1342 | return false; | |
1343 | ||
1344 | if (!streq_ptr(a->gid_path, b->gid_path)) | |
1345 | return false; | |
1346 | ||
1347 | if (!streq_ptr(a->description, b->description)) | |
1348 | return false; | |
1349 | ||
1350 | if (a->uid_set != b->uid_set) | |
1351 | return false; | |
1352 | ||
1353 | if (a->uid_set && a->uid != b->uid) | |
1354 | return false; | |
1355 | ||
1356 | if (a->gid_set != b->gid_set) | |
1357 | return false; | |
1358 | ||
1359 | if (a->gid_set && a->gid != b->gid) | |
1360 | return false; | |
1361 | ||
7629889c LP |
1362 | if (!streq_ptr(a->home, b->home)) |
1363 | return false; | |
1364 | ||
7b1aaf66 ZJS |
1365 | if (!streq_ptr(a->shell, b->shell)) |
1366 | return false; | |
1367 | ||
1b992147 LP |
1368 | return true; |
1369 | } | |
1370 | ||
1b992147 LP |
1371 | static int parse_line(const char *fname, unsigned line, const char *buffer) { |
1372 | ||
1373 | static const Specifier specifier_table[] = { | |
e97708fa ZJS |
1374 | { 'm', specifier_machine_id, NULL }, |
1375 | { 'b', specifier_boot_id, NULL }, | |
1376 | { 'H', specifier_host_name, NULL }, | |
1377 | { 'l', specifier_short_host_name, NULL }, | |
1378 | { 'v', specifier_kernel_release, NULL }, | |
1379 | { 'a', specifier_architecture, NULL }, | |
1380 | { 'o', specifier_os_id, NULL }, | |
1381 | { 'w', specifier_os_version_id, NULL }, | |
1382 | { 'B', specifier_os_build_id, NULL }, | |
1383 | { 'W', specifier_os_variant_id, NULL }, | |
1384 | { 'T', specifier_tmp_dir, NULL }, | |
1385 | { 'V', specifier_var_tmp_dir, NULL }, | |
1b992147 LP |
1386 | {} |
1387 | }; | |
1388 | ||
7b1aaf66 ZJS |
1389 | _cleanup_free_ char *action = NULL, |
1390 | *name = NULL, *resolved_name = NULL, | |
1391 | *id = NULL, *resolved_id = NULL, | |
b8bed700 YW |
1392 | *description = NULL, *resolved_description = NULL, |
1393 | *home = NULL, *resolved_home = NULL, | |
71fb1588 | 1394 | *shell = NULL, *resolved_shell = NULL; |
1b992147 LP |
1395 | _cleanup_(item_freep) Item *i = NULL; |
1396 | Item *existing; | |
5bc9c980 | 1397 | OrderedHashmap *h; |
7629889c LP |
1398 | int r; |
1399 | const char *p; | |
1b992147 LP |
1400 | |
1401 | assert(fname); | |
1402 | assert(line >= 1); | |
1403 | assert(buffer); | |
1404 | ||
7629889c LP |
1405 | /* Parse columns */ |
1406 | p = buffer; | |
4ec85141 | 1407 | r = extract_many_words(&p, NULL, EXTRACT_UNQUOTE, |
7b1aaf66 | 1408 | &action, &name, &id, &description, &home, &shell, NULL); |
330d1def ZJS |
1409 | if (r < 0) |
1410 | return log_error_errno(r, "[%s:%u] Syntax error.", fname, line); | |
1411 | if (r < 2) | |
1412 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1413 | "[%s:%u] Missing action and name columns.", fname, line); | |
1414 | if (!isempty(p)) | |
1415 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1416 | "[%s:%u] Trailing garbage.", fname, line); | |
1b992147 | 1417 | |
7629889c | 1418 | /* Verify action */ |
330d1def ZJS |
1419 | if (strlen(action) != 1) |
1420 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1421 | "[%s:%u] Unknown modifier '%s'", fname, line, action); | |
1b992147 | 1422 | |
330d1def ZJS |
1423 | if (!IN_SET(action[0], ADD_USER, ADD_GROUP, ADD_MEMBER, ADD_RANGE)) |
1424 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), | |
1425 | "[%s:%u] Unknown command type '%c'.", fname, line, action[0]); | |
1b992147 | 1426 | |
7629889c | 1427 | /* Verify name */ |
e7b88b7b | 1428 | if (empty_or_dash(name)) |
97b11eed | 1429 | name = mfree(name); |
1b992147 | 1430 | |
8530dc44 LP |
1431 | if (name) { |
1432 | r = specifier_printf(name, specifier_table, NULL, &resolved_name); | |
330d1def | 1433 | if (r < 0) |
0f7e4b28 | 1434 | return log_error_errno(r, "[%s:%u] Failed to replace specifiers in '%s': %m", fname, line, name); |
8530dc44 | 1435 | |
7a8867ab | 1436 | if (!valid_user_group_name(resolved_name, 0)) |
330d1def ZJS |
1437 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1438 | "[%s:%u] '%s' is not a valid user or group name.", | |
1439 | fname, line, resolved_name); | |
1b992147 LP |
1440 | } |
1441 | ||
8530dc44 | 1442 | /* Verify id */ |
e7b88b7b | 1443 | if (empty_or_dash(id)) |
97b11eed | 1444 | id = mfree(id); |
7629889c | 1445 | |
8530dc44 LP |
1446 | if (id) { |
1447 | r = specifier_printf(id, specifier_table, NULL, &resolved_id); | |
330d1def | 1448 | if (r < 0) |
0f7e4b28 | 1449 | return log_error_errno(r, "[%s:%u] Failed to replace specifiers in '%s': %m", |
330d1def | 1450 | fname, line, name); |
8530dc44 LP |
1451 | } |
1452 | ||
1453 | /* Verify description */ | |
e7b88b7b | 1454 | if (empty_or_dash(description)) |
97b11eed | 1455 | description = mfree(description); |
1b992147 | 1456 | |
8530dc44 | 1457 | if (description) { |
b8bed700 | 1458 | r = specifier_printf(description, specifier_table, NULL, &resolved_description); |
330d1def | 1459 | if (r < 0) |
0f7e4b28 | 1460 | return log_error_errno(r, "[%s:%u] Failed to replace specifiers in '%s': %m", |
330d1def | 1461 | fname, line, description); |
b8bed700 | 1462 | |
330d1def ZJS |
1463 | if (!valid_gecos(resolved_description)) |
1464 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1465 | "[%s:%u] '%s' is not a valid GECOS field.", | |
1466 | fname, line, resolved_description); | |
8530dc44 LP |
1467 | } |
1468 | ||
1469 | /* Verify home */ | |
e7b88b7b | 1470 | if (empty_or_dash(home)) |
97b11eed | 1471 | home = mfree(home); |
1b992147 | 1472 | |
8530dc44 | 1473 | if (home) { |
b8bed700 | 1474 | r = specifier_printf(home, specifier_table, NULL, &resolved_home); |
330d1def | 1475 | if (r < 0) |
0f7e4b28 | 1476 | return log_error_errno(r, "[%s:%u] Failed to replace specifiers in '%s': %m", |
330d1def | 1477 | fname, line, home); |
b8bed700 | 1478 | |
330d1def ZJS |
1479 | if (!valid_home(resolved_home)) |
1480 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1481 | "[%s:%u] '%s' is not a valid home directory field.", | |
1482 | fname, line, resolved_home); | |
8530dc44 LP |
1483 | } |
1484 | ||
7b1aaf66 | 1485 | /* Verify shell */ |
e7b88b7b | 1486 | if (empty_or_dash(shell)) |
7b1aaf66 ZJS |
1487 | shell = mfree(shell); |
1488 | ||
1489 | if (shell) { | |
1490 | r = specifier_printf(shell, specifier_table, NULL, &resolved_shell); | |
330d1def | 1491 | if (r < 0) |
0f7e4b28 | 1492 | return log_error_errno(r, "[%s:%u] Failed to replace specifiers in '%s': %m", |
330d1def | 1493 | fname, line, shell); |
7b1aaf66 | 1494 | |
330d1def ZJS |
1495 | if (!valid_shell(resolved_shell)) |
1496 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1497 | "[%s:%u] '%s' is not a valid login shell field.", | |
1498 | fname, line, resolved_shell); | |
7b1aaf66 ZJS |
1499 | } |
1500 | ||
a12b0cc3 | 1501 | switch (action[0]) { |
1b992147 | 1502 | |
8530dc44 | 1503 | case ADD_RANGE: |
330d1def ZJS |
1504 | if (resolved_name) |
1505 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1506 | "[%s:%u] Lines of type 'r' don't take a name field.", | |
1507 | fname, line); | |
1508 | ||
1509 | if (!resolved_id) | |
1510 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1511 | "[%s:%u] Lines of type 'r' require a ID range in the third field.", | |
1512 | fname, line); | |
1513 | ||
1514 | if (description || home || shell) | |
1515 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1516 | "[%s:%u] Lines of type '%c' don't take a %s field.", | |
1517 | fname, line, action[0], | |
1518 | description ? "GECOS" : home ? "home directory" : "login shell"); | |
7629889c | 1519 | |
8530dc44 | 1520 | r = uid_range_add_str(&uid_range, &n_uid_range, resolved_id); |
330d1def ZJS |
1521 | if (r < 0) |
1522 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1523 | "[%s:%u] Invalid UID range %s.", fname, line, resolved_id); | |
1b992147 | 1524 | |
8530dc44 LP |
1525 | return 0; |
1526 | ||
1527 | case ADD_MEMBER: { | |
8530dc44 | 1528 | /* Try to extend an existing member or group item */ |
330d1def ZJS |
1529 | if (!name) |
1530 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1531 | "[%s:%u] Lines of type 'm' require a user name in the second field.", | |
1532 | fname, line); | |
1533 | ||
1534 | if (!resolved_id) | |
1535 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1536 | "[%s:%u] Lines of type 'm' require a group name in the third field.", | |
1537 | fname, line); | |
1538 | ||
7a8867ab | 1539 | if (!valid_user_group_name(resolved_id, 0)) |
330d1def ZJS |
1540 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1541 | "[%s:%u] '%s' is not a valid user or group name.", | |
1542 | fname, line, resolved_id); | |
1543 | ||
1544 | if (description || home || shell) | |
1545 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1546 | "[%s:%u] Lines of type '%c' don't take a %s field.", | |
1547 | fname, line, action[0], | |
1548 | description ? "GECOS" : home ? "home directory" : "login shell"); | |
8530dc44 | 1549 | |
cde79109 | 1550 | r = string_strv_ordered_hashmap_put(&members, resolved_id, resolved_name); |
7629889c | 1551 | if (r < 0) |
cde79109 | 1552 | return log_error_errno(r, "Failed to store mapping for %s: %m", resolved_id); |
a12b0cc3 LP |
1553 | |
1554 | return 0; | |
1b992147 LP |
1555 | } |
1556 | ||
a12b0cc3 | 1557 | case ADD_USER: |
330d1def ZJS |
1558 | if (!name) |
1559 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1560 | "[%s:%u] Lines of type 'u' require a user name in the second field.", | |
1561 | fname, line); | |
8530dc44 | 1562 | |
71da1673 | 1563 | r = ordered_hashmap_ensure_allocated(&users, &item_hash_ops); |
a12b0cc3 LP |
1564 | if (r < 0) |
1565 | return log_oom(); | |
1566 | ||
1567 | i = new0(Item, 1); | |
1568 | if (!i) | |
1569 | return log_oom(); | |
1570 | ||
8530dc44 LP |
1571 | if (resolved_id) { |
1572 | if (path_is_absolute(resolved_id)) { | |
1cc6c93a | 1573 | i->uid_path = TAKE_PTR(resolved_id); |
858d36c1 | 1574 | path_simplify(i->uid_path, false); |
a12b0cc3 | 1575 | } else { |
4cb41413 MV |
1576 | _cleanup_free_ char *uid = NULL, *gid = NULL; |
1577 | if (split_pair(resolved_id, ":", &uid, &gid) == 0) { | |
1578 | r = parse_gid(gid, &i->gid); | |
649916d3 | 1579 | if (r < 0) { |
7a8867ab | 1580 | if (valid_user_group_name(gid, 0)) |
649916d3 DM |
1581 | i->group_name = TAKE_PTR(gid); |
1582 | else | |
1583 | return log_error_errno(r, "Failed to parse GID: '%s': %m", id); | |
1584 | } else { | |
1585 | i->gid_set = true; | |
1586 | i->id_set_strict = true; | |
1587 | } | |
4cb41413 | 1588 | free_and_replace(resolved_id, uid); |
a12b0cc3 | 1589 | } |
1825c909 MV |
1590 | if (!streq(resolved_id, "-")) { |
1591 | r = parse_uid(resolved_id, &i->uid); | |
1592 | if (r < 0) | |
1593 | return log_error_errno(r, "Failed to parse UID: '%s': %m", id); | |
1594 | i->uid_set = true; | |
1595 | } | |
a12b0cc3 LP |
1596 | } |
1597 | } | |
1598 | ||
b8bed700 YW |
1599 | i->description = TAKE_PTR(resolved_description); |
1600 | i->home = TAKE_PTR(resolved_home); | |
1cc6c93a | 1601 | i->shell = TAKE_PTR(resolved_shell); |
7b1aaf66 | 1602 | |
1b992147 | 1603 | h = users; |
a12b0cc3 LP |
1604 | break; |
1605 | ||
1606 | case ADD_GROUP: | |
330d1def ZJS |
1607 | if (!name) |
1608 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1609 | "[%s:%u] Lines of type 'g' require a user name in the second field.", | |
1610 | fname, line); | |
a12b0cc3 | 1611 | |
330d1def ZJS |
1612 | if (description || home || shell) |
1613 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1614 | "[%s:%u] Lines of type '%c' don't take a %s field.", | |
1615 | fname, line, action[0], | |
1616 | description ? "GECOS" : home ? "home directory" : "login shell"); | |
7629889c | 1617 | |
71da1673 | 1618 | r = ordered_hashmap_ensure_allocated(&groups, &item_hash_ops); |
7629889c LP |
1619 | if (r < 0) |
1620 | return log_oom(); | |
1621 | ||
a12b0cc3 LP |
1622 | i = new0(Item, 1); |
1623 | if (!i) | |
1624 | return log_oom(); | |
1625 | ||
8530dc44 LP |
1626 | if (resolved_id) { |
1627 | if (path_is_absolute(resolved_id)) { | |
1cc6c93a | 1628 | i->gid_path = TAKE_PTR(resolved_id); |
858d36c1 | 1629 | path_simplify(i->gid_path, false); |
a12b0cc3 | 1630 | } else { |
8530dc44 | 1631 | r = parse_gid(resolved_id, &i->gid); |
4cb41413 MV |
1632 | if (r < 0) |
1633 | return log_error_errno(r, "Failed to parse GID: '%s': %m", id); | |
a12b0cc3 LP |
1634 | |
1635 | i->gid_set = true; | |
1636 | } | |
1637 | } | |
1638 | ||
1b992147 | 1639 | h = groups; |
a12b0cc3 | 1640 | break; |
8530dc44 | 1641 | |
bce415ed RC |
1642 | default: |
1643 | return -EBADMSG; | |
1b992147 | 1644 | } |
a12b0cc3 LP |
1645 | |
1646 | i->type = action[0]; | |
1cc6c93a | 1647 | i->name = TAKE_PTR(resolved_name); |
1b992147 | 1648 | |
5bc9c980 | 1649 | existing = ordered_hashmap_get(h, i->name); |
1b992147 | 1650 | if (existing) { |
1b992147 LP |
1651 | /* Two identical items are fine */ |
1652 | if (!item_equal(existing, i)) | |
1653 | log_warning("Two or more conflicting lines for %s configured, ignoring.", i->name); | |
1654 | ||
1655 | return 0; | |
1656 | } | |
1657 | ||
5bc9c980 | 1658 | r = ordered_hashmap_put(h, i->name, i); |
a12b0cc3 LP |
1659 | if (r < 0) |
1660 | return log_oom(); | |
1b992147 LP |
1661 | |
1662 | i = NULL; | |
1663 | return 0; | |
1664 | } | |
1665 | ||
1666 | static int read_config_file(const char *fn, bool ignore_enoent) { | |
dfc87cbf LP |
1667 | _cleanup_fclose_ FILE *rf = NULL; |
1668 | FILE *f = NULL; | |
1b992147 | 1669 | unsigned v = 0; |
c4640902 | 1670 | int r = 0; |
1b992147 LP |
1671 | |
1672 | assert(fn); | |
1673 | ||
dfc87cbf LP |
1674 | if (streq(fn, "-")) |
1675 | f = stdin; | |
1676 | else { | |
a826d4f7 | 1677 | r = search_and_fopen(fn, "re", arg_root, (const char**) CONF_PATHS_STRV("sysusers.d"), &rf); |
dfc87cbf LP |
1678 | if (r < 0) { |
1679 | if (ignore_enoent && r == -ENOENT) | |
1680 | return 0; | |
1b992147 | 1681 | |
8d3d7072 | 1682 | return log_error_errno(r, "Failed to open '%s', ignoring: %m", fn); |
dfc87cbf LP |
1683 | } |
1684 | ||
1685 | f = rf; | |
1b992147 LP |
1686 | } |
1687 | ||
050ca299 LP |
1688 | for (;;) { |
1689 | _cleanup_free_ char *line = NULL; | |
1b992147 LP |
1690 | char *l; |
1691 | int k; | |
1692 | ||
050ca299 LP |
1693 | k = read_line(f, LONG_LINE_MAX, &line); |
1694 | if (k < 0) | |
1695 | return log_error_errno(k, "Failed to read '%s': %m", fn); | |
1696 | if (k == 0) | |
1697 | break; | |
1698 | ||
1b992147 LP |
1699 | v++; |
1700 | ||
1701 | l = strstrip(line); | |
4c701096 | 1702 | if (IN_SET(*l, 0, '#')) |
1b992147 LP |
1703 | continue; |
1704 | ||
1705 | k = parse_line(fn, v, l); | |
1706 | if (k < 0 && r == 0) | |
1707 | r = k; | |
1708 | } | |
1709 | ||
1710 | if (ferror(f)) { | |
56f64d95 | 1711 | log_error_errno(errno, "Failed to read from file %s: %m", fn); |
1b992147 LP |
1712 | if (r == 0) |
1713 | r = -EIO; | |
1714 | } | |
1715 | ||
1716 | return r; | |
1717 | } | |
1718 | ||
ec0327d6 ZJS |
1719 | static int cat_config(void) { |
1720 | _cleanup_strv_free_ char **files = NULL; | |
ec0327d6 ZJS |
1721 | int r; |
1722 | ||
a826d4f7 | 1723 | r = conf_files_list_with_replacement(arg_root, CONF_PATHS_STRV("sysusers.d"), arg_replace, &files, NULL); |
ec0327d6 ZJS |
1724 | if (r < 0) |
1725 | return r; | |
1726 | ||
0221d68a | 1727 | (void) pager_open(arg_pager_flags); |
dcd5c891 | 1728 | |
ec0327d6 ZJS |
1729 | return cat_files(NULL, files, 0); |
1730 | } | |
1731 | ||
37ec0fdd LP |
1732 | static int help(void) { |
1733 | _cleanup_free_ char *link = NULL; | |
1734 | int r; | |
1735 | ||
1736 | r = terminal_urlify_man("systemd-sysusers.service", "8", &link); | |
1737 | if (r < 0) | |
1738 | return log_oom(); | |
1739 | ||
1b992147 LP |
1740 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n" |
1741 | "Creates system user accounts.\n\n" | |
1742 | " -h --help Show this help\n" | |
1743 | " --version Show package version\n" | |
ec0327d6 | 1744 | " --cat-config Show configuration files\n" |
601185b4 | 1745 | " --root=PATH Operate on an alternate filesystem root\n" |
0c3ee127 | 1746 | " --image=PATH Operate on disk image as filesystem root\n" |
d16a1c1b | 1747 | " --replace=PATH Treat arguments as replacement for PATH\n" |
1b600bd5 | 1748 | " --inline Treat arguments as configuration lines\n" |
dcd5c891 | 1749 | " --no-pager Do not pipe output into a pager\n" |
37ec0fdd LP |
1750 | "\nSee the %s for details.\n" |
1751 | , program_invocation_short_name | |
1752 | , link | |
1753 | ); | |
1754 | ||
1755 | return 0; | |
1b992147 LP |
1756 | } |
1757 | ||
1758 | static int parse_argv(int argc, char *argv[]) { | |
1759 | ||
1760 | enum { | |
1761 | ARG_VERSION = 0x100, | |
ec0327d6 | 1762 | ARG_CAT_CONFIG, |
1b992147 | 1763 | ARG_ROOT, |
0c3ee127 | 1764 | ARG_IMAGE, |
d16a1c1b | 1765 | ARG_REPLACE, |
1b600bd5 | 1766 | ARG_INLINE, |
dcd5c891 | 1767 | ARG_NO_PAGER, |
1b992147 LP |
1768 | }; |
1769 | ||
1770 | static const struct option options[] = { | |
ec0327d6 ZJS |
1771 | { "help", no_argument, NULL, 'h' }, |
1772 | { "version", no_argument, NULL, ARG_VERSION }, | |
1773 | { "cat-config", no_argument, NULL, ARG_CAT_CONFIG }, | |
1774 | { "root", required_argument, NULL, ARG_ROOT }, | |
0c3ee127 | 1775 | { "image", required_argument, NULL, ARG_IMAGE }, |
ec0327d6 ZJS |
1776 | { "replace", required_argument, NULL, ARG_REPLACE }, |
1777 | { "inline", no_argument, NULL, ARG_INLINE }, | |
dcd5c891 | 1778 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
1b992147 LP |
1779 | {} |
1780 | }; | |
1781 | ||
0f474365 | 1782 | int c, r; |
1b992147 LP |
1783 | |
1784 | assert(argc >= 0); | |
1785 | assert(argv); | |
1786 | ||
601185b4 | 1787 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
1b992147 LP |
1788 | |
1789 | switch (c) { | |
1790 | ||
1791 | case 'h': | |
37ec0fdd | 1792 | return help(); |
1b992147 LP |
1793 | |
1794 | case ARG_VERSION: | |
3f6fd1ba | 1795 | return version(); |
1b992147 | 1796 | |
ec0327d6 ZJS |
1797 | case ARG_CAT_CONFIG: |
1798 | arg_cat_config = true; | |
1799 | break; | |
1800 | ||
1b992147 | 1801 | case ARG_ROOT: |
7dc6477d | 1802 | r = parse_path_argument_and_warn(optarg, /* suppress_root= */ false, &arg_root); |
0f474365 | 1803 | if (r < 0) |
0f03c2a4 | 1804 | return r; |
1b992147 LP |
1805 | break; |
1806 | ||
0c3ee127 LP |
1807 | case ARG_IMAGE: |
1808 | r = parse_path_argument_and_warn(optarg, /* suppress_root= */ false, &arg_image); | |
1809 | if (r < 0) | |
1810 | return r; | |
1811 | break; | |
1812 | ||
d16a1c1b ZJS |
1813 | case ARG_REPLACE: |
1814 | if (!path_is_absolute(optarg) || | |
baaa35ad ZJS |
1815 | !endswith(optarg, ".conf")) |
1816 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1817 | "The argument to --replace= must an absolute path to a config file"); | |
d16a1c1b ZJS |
1818 | |
1819 | arg_replace = optarg; | |
1820 | break; | |
1821 | ||
1b600bd5 ZJS |
1822 | case ARG_INLINE: |
1823 | arg_inline = true; | |
1824 | break; | |
1825 | ||
dcd5c891 | 1826 | case ARG_NO_PAGER: |
0221d68a | 1827 | arg_pager_flags |= PAGER_DISABLE; |
dcd5c891 LP |
1828 | break; |
1829 | ||
1b992147 LP |
1830 | case '?': |
1831 | return -EINVAL; | |
1832 | ||
1833 | default: | |
1834 | assert_not_reached("Unhandled option"); | |
1835 | } | |
1b992147 | 1836 | |
baaa35ad ZJS |
1837 | if (arg_replace && arg_cat_config) |
1838 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1839 | "Option --replace= is not supported with --cat-config"); | |
ec0327d6 | 1840 | |
baaa35ad ZJS |
1841 | if (arg_replace && optind >= argc) |
1842 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1843 | "When --replace= is given, some configuration items must be specified"); | |
d16a1c1b | 1844 | |
0c3ee127 LP |
1845 | if (arg_image && arg_root) |
1846 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); | |
1847 | ||
1b992147 LP |
1848 | return 1; |
1849 | } | |
1850 | ||
d16a1c1b ZJS |
1851 | static int parse_arguments(char **args) { |
1852 | char **arg; | |
1853 | unsigned pos = 1; | |
1854 | int r; | |
1855 | ||
1856 | STRV_FOREACH(arg, args) { | |
1857 | if (arg_inline) | |
1858 | /* Use (argument):n, where n==1 for the first positional arg */ | |
1859 | r = parse_line("(argument)", pos, *arg); | |
1860 | else | |
1861 | r = read_config_file(*arg, false); | |
1862 | if (r < 0) | |
1863 | return r; | |
1864 | ||
1865 | pos++; | |
1866 | } | |
1867 | ||
1868 | return 0; | |
1869 | } | |
1870 | ||
ec0327d6 | 1871 | static int read_config_files(char **args) { |
d16a1c1b ZJS |
1872 | _cleanup_strv_free_ char **files = NULL; |
1873 | _cleanup_free_ char *p = NULL; | |
1874 | char **f; | |
1875 | int r; | |
1876 | ||
a826d4f7 | 1877 | r = conf_files_list_with_replacement(arg_root, CONF_PATHS_STRV("sysusers.d"), arg_replace, &files, &p); |
d16a1c1b | 1878 | if (r < 0) |
ec0327d6 | 1879 | return r; |
d16a1c1b ZJS |
1880 | |
1881 | STRV_FOREACH(f, files) | |
1882 | if (p && path_equal(*f, p)) { | |
1883 | log_debug("Parsing arguments at position \"%s\"…", *f); | |
1884 | ||
1885 | r = parse_arguments(args); | |
1886 | if (r < 0) | |
1887 | return r; | |
1888 | } else { | |
1889 | log_debug("Reading config file \"%s\"…", *f); | |
1890 | ||
1891 | /* Just warn, ignore result otherwise */ | |
1892 | (void) read_config_file(*f, true); | |
1893 | } | |
1894 | ||
1895 | return 0; | |
1896 | } | |
1897 | ||
71da1673 | 1898 | static int run(int argc, char *argv[]) { |
0c3ee127 LP |
1899 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
1900 | _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL; | |
1901 | _cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL; | |
1b992147 LP |
1902 | _cleanup_close_ int lock = -1; |
1903 | Iterator iterator; | |
1b992147 | 1904 | Item *i; |
dd2fd972 | 1905 | int r; |
1b992147 LP |
1906 | |
1907 | r = parse_argv(argc, argv); | |
1908 | if (r <= 0) | |
71da1673 | 1909 | return r; |
1b992147 | 1910 | |
6bf3c61c | 1911 | log_setup_service(); |
1b992147 | 1912 | |
71da1673 YW |
1913 | if (arg_cat_config) |
1914 | return cat_config(); | |
ec0327d6 | 1915 | |
1b992147 LP |
1916 | umask(0022); |
1917 | ||
c3dacc8b | 1918 | r = mac_selinux_init(); |
71da1673 | 1919 | if (r < 0) |
a9ba0e32 | 1920 | return r; |
1b992147 | 1921 | |
0c3ee127 LP |
1922 | if (arg_image) { |
1923 | assert(!arg_root); | |
1924 | ||
1925 | r = mount_image_privately_interactively( | |
1926 | arg_image, | |
1927 | DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_VALIDATE_OS|DISSECT_IMAGE_RELAX_VAR_CHECK|DISSECT_IMAGE_FSCK, | |
1928 | &unlink_dir, | |
1929 | &loop_device, | |
1930 | &decrypted_image); | |
1931 | if (r < 0) | |
1932 | return r; | |
1933 | ||
1934 | arg_root = strdup(unlink_dir); | |
1935 | if (!arg_root) | |
1936 | return log_oom(); | |
1937 | } | |
1938 | ||
d16a1c1b ZJS |
1939 | /* If command line arguments are specified along with --replace, read all |
1940 | * configuration files and insert the positional arguments at the specified | |
1941 | * place. Otherwise, if command line arguments are specified, execute just | |
1942 | * them, and finally, without --replace= or any positional arguments, just | |
1943 | * read configuration and execute it. | |
1944 | */ | |
1945 | if (arg_replace || optind >= argc) | |
ec0327d6 | 1946 | r = read_config_files(argv + optind); |
d16a1c1b ZJS |
1947 | else |
1948 | r = parse_arguments(argv + optind); | |
1949 | if (r < 0) | |
71da1673 | 1950 | return r; |
1b992147 | 1951 | |
fe102d6a LP |
1952 | /* Let's tell nss-systemd not to synthesize the "root" and "nobody" entries for it, so that our detection |
1953 | * whether the names or UID/GID area already used otherwise doesn't get confused. After all, even though | |
1954 | * nss-systemd synthesizes these users/groups, they should still appear in /etc/passwd and /etc/group, as the | |
1955 | * synthesizing logic is merely supposed to be fallback for cases where we run with a completely unpopulated | |
1956 | * /etc. */ | |
71da1673 YW |
1957 | if (setenv("SYSTEMD_NSS_BYPASS_SYNTHETIC", "1", 1) < 0) |
1958 | return log_error_errno(errno, "Failed to set SYSTEMD_NSS_BYPASS_SYNTHETIC environment variable: %m"); | |
fe102d6a | 1959 | |
8530dc44 | 1960 | if (!uid_range) { |
b9ee05c2 | 1961 | /* Default to default range of 1..SYSTEM_UID_MAX */ |
8530dc44 | 1962 | r = uid_range_add(&uid_range, &n_uid_range, 1, SYSTEM_UID_MAX); |
71da1673 YW |
1963 | if (r < 0) |
1964 | return log_oom(); | |
8530dc44 LP |
1965 | } |
1966 | ||
a12b0cc3 LP |
1967 | r = add_implicit(); |
1968 | if (r < 0) | |
71da1673 | 1969 | return r; |
a12b0cc3 | 1970 | |
e929bee0 | 1971 | lock = take_etc_passwd_lock(arg_root); |
71da1673 YW |
1972 | if (lock < 0) |
1973 | return log_error_errno(lock, "Failed to take /etc/passwd lock: %m"); | |
1b992147 LP |
1974 | |
1975 | r = load_user_database(); | |
71da1673 YW |
1976 | if (r < 0) |
1977 | return log_error_errno(r, "Failed to load user database: %m"); | |
1b992147 LP |
1978 | |
1979 | r = load_group_database(); | |
71da1673 YW |
1980 | if (r < 0) |
1981 | return log_error_errno(r, "Failed to read group database: %m"); | |
1b992147 | 1982 | |
5bc9c980 | 1983 | ORDERED_HASHMAP_FOREACH(i, groups, iterator) |
c1a32819 | 1984 | (void) process_item(i); |
1b992147 | 1985 | |
5bc9c980 | 1986 | ORDERED_HASHMAP_FOREACH(i, users, iterator) |
c1a32819 | 1987 | (void) process_item(i); |
1b992147 LP |
1988 | |
1989 | r = write_files(); | |
1990 | if (r < 0) | |
71da1673 | 1991 | return log_error_errno(r, "Failed to write files: %m"); |
1b992147 | 1992 | |
71da1673 | 1993 | return 0; |
1b992147 | 1994 | } |
71da1673 YW |
1995 | |
1996 | DEFINE_MAIN_FUNCTION(run); |