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