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