LCOV - code coverage report
Current view: top level - sysusers - sysusers.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 34 1061 3.2 %
Date: 2019-08-22 15:41:25 Functions: 17 47 36.2 %

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

Generated by: LCOV version 1.14