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