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