HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/sysusers/sysusers.c

92,777 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 3771185f538a8e5a5122153e1bfaf83112f378fe

Lines 1001–2000 of 2,416

        _cleanup_(unlink_and_freep) char *passwd_tmp = NULL, *group_tmp = NULL, *shadow_tmp = NULL, *gshadow_tmp = NULL;
        int r;
 
        _cleanup_free_ char *passwd_path = path_join(arg_root, "/etc/passwd");
        if (!passwd_path)
                return log_oom();
 
        _cleanup_free_ char *shadow_path = path_join(arg_root, "/etc/shadow");
        if (!shadow_path)
                return log_oom();
 
        _cleanup_free_ char *group_path = path_join(arg_root, "/etc/group");
        if (!group_path)
                return log_oom();
 
        _cleanup_free_ char *gshadow_path = path_join(arg_root, "/etc/gshadow");
        if (!gshadow_path)
                return log_oom();
 
        assert(c);
 
        r = write_temporary_group(c, group_path, &group, &group_tmp);
        if (r < 0)
                return r;
 
        r = write_temporary_gshadow(c, gshadow_path, &gshadow, &gshadow_tmp);
        if (r < 0)
                return r;
 
        r = write_temporary_passwd(c, passwd_path, &passwd, &passwd_tmp);
        if (r < 0)
                return r;
 
        r = write_temporary_shadow(c, shadow_path, &shadow, &shadow_tmp);
        if (r < 0)
                return r;
 
        /* Make a backup of the old files */
        if (group) {
                r = make_backup(group_path, group_path, c->label_context);
                if (r < 0)
                        return log_error_errno(r, "Failed to backup %s: %m", group_path);
        }
        if (gshadow) {
                r = make_backup(gshadow_path, gshadow_path, c->label_context);
                if (r < 0)
                        return log_error_errno(r, "Failed to backup %s: %m", gshadow_path);
        }
 
        if (passwd) {
                r = make_backup(passwd_path, passwd_path, c->label_context);
                if (r < 0)
                        return log_error_errno(r, "Failed to backup %s: %m", passwd_path);
        }
        if (shadow) {
                r = make_backup(shadow_path, shadow_path, c->label_context);
                if (r < 0)
                        return log_error_errno(r, "Failed to backup %s: %m", shadow_path);
        }
 
        /* And make the new files count */
        if (group) {
                r = rename_and_apply_smack_floor_label(group_tmp, group_path);
                if (r < 0)
                        return log_error_errno(r, "Failed to rename %s to %s: %m",
                                               group_tmp, group_path);
                group_tmp = mfree(group_tmp);
        }
        /* OK, we have written the group entries successfully */
        log_audit_accounts(c, ADD_GROUP);
        if (gshadow) {
                r = rename_and_apply_smack_floor_label(gshadow_tmp, gshadow_path);
                if (r < 0)
                        return log_error_errno(r, "Failed to rename %s to %s: %m",
                                               gshadow_tmp, gshadow_path);
 
                gshadow_tmp = mfree(gshadow_tmp);
        }
 
        if (passwd) {
                r = rename_and_apply_smack_floor_label(passwd_tmp, passwd_path);
                if (r < 0)
                        return log_error_errno(r, "Failed to rename %s to %s: %m",
                                               passwd_tmp, passwd_path);
 
                passwd_tmp = mfree(passwd_tmp);
        }
        /* OK, we have written the user entries successfully */
        log_audit_accounts(c, ADD_USER);
        if (shadow) {
                r = rename_and_apply_smack_floor_label(shadow_tmp, shadow_path);
                if (r < 0)
                        return log_error_errno(r, "Failed to rename %s to %s: %m",
                                               shadow_tmp, shadow_path);
 
                shadow_tmp = mfree(shadow_tmp);
        }
 
        return 0;
}
 
static int uid_is_ok(
                Context *c,
                uid_t uid,
                const char *name,
                bool check_with_gid) {
 
        int r;
        assert(c);
 
        /* Let's see if we already have assigned the UID a second time */
        if (ordered_hashmap_contains(c->todo_uids, UID_TO_PTR(uid)))
                return 0;
 
        /* Try to avoid using uids that are already used by a group
         * that doesn't have the same name as our new user. */
        if (check_with_gid) {
                Item *i;
 
                i = ordered_hashmap_get(c->todo_gids, GID_TO_PTR(uid));
                if (i && !streq(i->name, name))
                        return 0;
        }
 
        /* Let's check the files directly */
        if (hashmap_contains(c->database_by_uid, UID_TO_PTR(uid)))
                return 0;
 
        if (check_with_gid) {
                const char *n;
 
                n = hashmap_get(c->database_by_gid, GID_TO_PTR(uid));
                if (n && !streq(n, name))
                        return 0;
        }
 
        /* Let's also check via NSS, to avoid UID clashes over LDAP and such, just in case */
        if (!arg_root) {
                _cleanup_free_ struct group *g = NULL;
 
                r = getpwuid_malloc(uid, /* ret= */ NULL);
                if (r >= 0)
                        return 0;
                if (r != -ESRCH)
                        log_warning_errno(r, "Unexpected failure while looking up UID '" UID_FMT "' via NSS, assuming it doesn't exist: %m", uid);
 
                if (check_with_gid) {
                        r = getgrgid_malloc((gid_t) uid, &g);
                        if (r >= 0) {
                                if (!streq(g->gr_name, name))
                                        return 0;
                        } else if (r != -ESRCH)
                                log_warning_errno(r, "Unexpected failure while looking up GID '" GID_FMT "' via NSS, assuming it doesn't exist: %m", uid);
                }
        }
 
        return 1;
}
 
static int root_stat(const char *p, struct stat *ret_st) {
        return chase_and_stat(p, arg_root, CHASE_PREFIX_ROOT, /* ret_path= */ NULL, ret_st);
}
 
static int read_id_from_file(Item *i, uid_t *ret_uid, gid_t *ret_gid) {
        struct stat st;
        bool found_uid = false, found_gid = false;
        uid_t uid = 0;
        gid_t gid = 0;
 
        assert(i);
 
        /* First, try to get the GID directly */
        if (ret_gid && i->gid_path && root_stat(i->gid_path, &st) >= 0) {
                gid = st.st_gid;
                found_gid = true;
        }
 
        /* Then, try to get the UID directly */
        if ((ret_uid || (ret_gid && !found_gid))
            && i->uid_path
            && root_stat(i->uid_path, &st) >= 0) {
 
                uid = st.st_uid;
                found_uid = true;
 
                /* If we need the gid, but had no success yet, also derive it from the UID path */
                if (ret_gid && !found_gid) {
                        gid = st.st_gid;
                        found_gid = true;
                }
        }
 
        /* If that didn't work yet, then let's reuse the GID as UID */
        if (ret_uid && !found_uid && i->gid_path) {
 
                if (found_gid) {
                        uid = (uid_t) gid;
                        found_uid = true;
                } else if (root_stat(i->gid_path, &st) >= 0) {
                        uid = (uid_t) st.st_gid;
                        found_uid = true;
                }
        }
 
        if (ret_uid) {
                if (!found_uid)
                        return 0;
 
                *ret_uid = uid;
        }
 
        if (ret_gid) {
                if (!found_gid)
                        return 0;
 
                *ret_gid = gid;
        }
 
        return 1;
}
 
static int add_user(Context *c, Item *i) {
        void *z;
        int r;
 
        assert(c);
        assert(i);
 
        /* Check the database directly */
        z = hashmap_get(c->database_by_username, i->name);
        if (z) {
                log_debug("User %s already exists.", i->name);
                i->uid = PTR_TO_UID(z);
                i->uid_set = true;
                return 0;
        }
 
        if (!arg_root) {
                _cleanup_free_ struct passwd *p = NULL;
 
                /* Also check NSS */
                r = getpwnam_malloc(i->name, &p);
                if (r >= 0) {
                        log_debug("User %s already exists.", i->name);
                        i->uid = p->pw_uid;
                        i->uid_set = true;
 
                        r = free_and_strdup(&i->description, p->pw_gecos);
                        if (r < 0)
                                return log_oom();
 
                        return 0;
                }
                if (r != -ESRCH)
                        log_warning_errno(r, "Unexpected failure while looking up user '%s' via NSS, assuming it doesn't exist: %m", i->name);
        }
 
        /* Try to use the suggested numeric UID */
        if (i->uid_set) {
                r = uid_is_ok(c, i->uid, i->name, !i->id_set_strict);
                if (r < 0)
                        return log_error_errno(r, "Failed to verify UID " UID_FMT ": %m", i->uid);
                if (r == 0) {
                        log_info("Suggested user ID " UID_FMT " for %s already used.", i->uid, i->name);
                        i->uid_set = false;
                }
        }
 
        /* If that didn't work, try to read it from the specified path */
        if (!i->uid_set) {
                uid_t candidate;
 
                if (read_id_from_file(i, &candidate, NULL) > 0) {
 
                        if (candidate <= 0 || !uid_range_contains(c->uid_range, candidate))
                                log_debug("User ID " UID_FMT " of file not suitable for %s.", candidate, i->name);
                        else {
                                r = uid_is_ok(c, candidate, i->name, true);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to verify UID " UID_FMT ": %m", i->uid);
                                else if (r > 0) {
                                        i->uid = candidate;
                                        i->uid_set = true;
                                } else
                                        log_debug("User ID " UID_FMT " of file for %s is already used.", candidate, i->name);
                        }
                }
        }
 
        /* Otherwise, try to reuse the group ID */
        if (!i->uid_set && i->gid_set) {
                r = uid_is_ok(c, (uid_t) i->gid, i->name, true);
                if (r < 0)
                        return log_error_errno(r, "Failed to verify UID " UID_FMT ": %m", i->uid);
                if (r > 0) {
                        i->uid = (uid_t) i->gid;
                        i->uid_set = true;
                }
        }
 
        /* And if that didn't work either, let's try to find a free one */
        if (!i->uid_set) {
                maybe_emit_login_defs_warning(c);
 
                for (;;) {
                        r = uid_range_next_lower(c->uid_range, &c->search_uid);
                        if (r < 0)
                                return log_error_errno(r, "No free user ID available for %s.", i->name);
 
                        r = uid_is_ok(c, c->search_uid, i->name, true);
                        if (r < 0)
                                return log_error_errno(r, "Failed to verify UID " UID_FMT ": %m", i->uid);
                        else if (r > 0)
                                break;
                }
 
                i->uid_set = true;
                i->uid = c->search_uid;
        }
 
        r = ordered_hashmap_ensure_put(&c->todo_uids, NULL, UID_TO_PTR(i->uid), i);
        if (r == -EEXIST)
                return log_error_errno(r, "Requested user %s with UID " UID_FMT " and gid" GID_FMT " to be created is duplicated "
                                       "or conflicts with another user.", i->name, i->uid, i->gid);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0)
                return log_error_errno(r, "Failed to store user %s with UID " UID_FMT " and GID " GID_FMT " to be created: %m",
                                       i->name, i->uid, i->gid);
 
        i->todo_user = true;
        log_info("Creating user '%s' (%s) with UID " UID_FMT " and GID " GID_FMT ".",
                 i->name, strna(i->description), i->uid, i->gid);
 
        return 0;
}
 
static int gid_is_ok(
                Context *c,
                gid_t gid,
                const char *groupname,
                bool check_with_uid) {
 
        Item *user;
        char *username;
        int r;
 
        assert(c);
        assert(groupname);
 
        if (ordered_hashmap_contains(c->todo_gids, GID_TO_PTR(gid)))
                return 0;
 
        /* Avoid reusing gids that are already used by a different user */
        if (check_with_uid) {
                user = ordered_hashmap_get(c->todo_uids, UID_TO_PTR(gid));
                if (user && !streq(user->name, groupname))
                        return 0;
        }
 
        if (hashmap_contains(c->database_by_gid, GID_TO_PTR(gid)))
                return 0;
 
        if (check_with_uid) {
                username = hashmap_get(c->database_by_uid, UID_TO_PTR(gid));
                if (username && !streq(username, groupname))
                        return 0;
        }
 
        if (!arg_root) {
                r = getgrgid_malloc(gid, /* ret= */ NULL);
                if (r >= 0)
                        return 0;
                if (r != -ESRCH)
                        log_warning_errno(r, "Unexpected failure while looking up GID '" GID_FMT "' via NSS, assuming it doesn't exist: %m", gid);
 
                if (check_with_uid) {
                        r = getpwuid_malloc(gid, /* ret= */ NULL);
                        if (r >= 0)
                                return 0;
                        if (r != -ESRCH)
                                log_warning_errno(r, "Unexpected failure while looking up GID '" GID_FMT "' via NSS, assuming it doesn't exist: %m", gid);
                }
        }
 
        return 1;
}
 
static int get_gid_by_name(
                Context *c,
                const char *name,
                gid_t *ret_gid) {
 
        void *z;
        int r;
 
        assert(c);
        assert(ret_gid);
 
        /* Check the database directly */
        z = hashmap_get(c->database_by_groupname, name);
        if (z) {
                *ret_gid = PTR_TO_GID(z);
                return 0;
        }
 
        /* Also check NSS */
        if (!arg_root) {
                _cleanup_free_ struct group *g = NULL;
 
                r = getgrnam_malloc(name, &g);
                if (r >= 0) {
                        *ret_gid = g->gr_gid;
                        return 0;
                }
                if (r != -ESRCH)
                        log_warning_errno(r, "Unexpected failure while looking up group '%s' via NSS, assuming it doesn't exist: %m", name);
        }
 
        return -ENOENT;
}
 
static int add_group(Context *c, Item *i) {
        int r;
 
        assert(c);
        assert(i);
 
        r = get_gid_by_name(c, i->name, &i->gid);
        if (r != -ENOENT) {
                if (r < 0)
                        return r;
                log_debug("Group %s already exists.", i->name);
                i->gid_set = true;
                return 0;
        }
 
        /* Try to use the suggested numeric GID */
        if (i->gid_set) {
                r = gid_is_ok(c, i->gid, i->name, false);
                if (r < 0)
                        return log_error_errno(r, "Failed to verify GID " GID_FMT ": %m", i->gid);
                if (i->id_set_strict) {
                        /* If we require the GID to already exist we can return here:
                         * r > 0: means the GID does not exist -> fail
                         * r == 0: means the GID exists -> nothing more to do.
                         */
                        if (r > 0)
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "Failed to create %s: please create GID " GID_FMT,
                                                       i->name, i->gid);
                        if (r == 0)
                                return 0;
                }
                if (r == 0) {
                        log_info("Suggested group ID " GID_FMT " for %s already used.", i->gid, i->name);
                        i->gid_set = false;
                }
        }
 
        /* Try to reuse the numeric uid, if there's one */
        if (!i->gid_set && i->uid_set) {
                r = gid_is_ok(c, (gid_t) i->uid, i->name, true);
                if (r < 0)
                        return log_error_errno(r, "Failed to verify GID " GID_FMT ": %m", i->gid);
                if (r > 0) {
                        i->gid = (gid_t) i->uid;
                        i->gid_set = true;
                }
        }
 
        /* If that didn't work, try to read it from the specified path */
        if (!i->gid_set) {
                gid_t candidate;
 
                if (read_id_from_file(i, NULL, &candidate) > 0) {
 
                        if (candidate <= 0 || !uid_range_contains(c->uid_range, candidate))
                                log_debug("Group ID " GID_FMT " of file not suitable for %s.", candidate, i->name);
                        else {
                                r = gid_is_ok(c, candidate, i->name, true);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to verify GID " GID_FMT ": %m", i->gid);
                                else if (r > 0) {
                                        i->gid = candidate;
                                        i->gid_set = true;
                                } else
                                        log_debug("Group ID " GID_FMT " of file for %s already used.", candidate, i->name);
                        }
                }
        }
 
        /* And if that didn't work either, let's try to find a free one */
        if (!i->gid_set) {
                maybe_emit_login_defs_warning(c);
 
                for (;;) {
                        /* We look for new GIDs in the UID pool! */
                        r = uid_range_next_lower(c->uid_range, &c->search_uid);
                        if (r < 0)
                                return log_error_errno(r, "No free group ID available for %s.", i->name);
 
                        r = gid_is_ok(c, c->search_uid, i->name, true);
                        if (r < 0)
                                return log_error_errno(r, "Failed to verify GID " GID_FMT ": %m", i->gid);
                        else if (r > 0)
                                break;
                }
 
                i->gid_set = true;
                i->gid = c->search_uid;
        }
 
        r = ordered_hashmap_ensure_put(&c->todo_gids, NULL, GID_TO_PTR(i->gid), i);
        if (r == -EEXIST)
                return log_error_errno(r, "Requested group %s with GID "GID_FMT " to be created is duplicated or conflicts with another user.", i->name, i->gid);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0)
                return log_error_errno(r, "Failed to store group %s with GID " GID_FMT " to be created: %m", i->name, i->gid);
 
        i->todo_group = true;
        log_info("Creating group '%s' with GID " GID_FMT ".", i->name, i->gid);
 
        return 0;
}
 
static int process_item(Context *c, Item *i) {
        int r;
 
        assert(c);
        assert(i);
 
        switch (i->type) {
 
        case ADD_USER: {
                Item *j = NULL;
 
                if (!i->gid_set) {
                        j = ordered_hashmap_get(c->groups, i->group_name ?: i->name);
 
                        /* If that's not a match, also check if the group name
                         * matches a user name in the queue. */
                        if (!j && i->group_name)
                                j = ordered_hashmap_get(c->users, i->group_name);
                }
 
                if (j && j->todo_group) {
                        /* When a group with the target name is already in queue,
                         * use the information about the group and do not create
                         * duplicated group entry. */
                        i->gid_set = j->gid_set;
                        i->gid = j->gid;
                        i->id_set_strict = true;
                } else if (i->group_name) {
                        /* When a group name was given instead of a GID and it's
                         * not in queue, then it must already exist. */
                        r = get_gid_by_name(c, i->group_name, &i->gid);
                        if (r < 0)
                                return log_error_errno(r, "Group %s not found.", i->group_name);
                        i->gid_set = true;
                        i->id_set_strict = true;
                } else {
                        r = add_group(c, i);
                        if (r < 0)
                                return r;
                }
 
                return add_user(c, i);
        }
 
        case ADD_GROUP:
                return add_group(c, i);
 
        default:
                assert_not_reached();
        }
}
 
static Item* item_free(Item *i) {
        if (!i)
                return NULL;
 
        free(i->name);
        free(i->group_name);
        free(i->uid_path);
        free(i->gid_path);
        free(i->description);
        free(i->home);
        free(i->shell);
        free(i->filename);
        return mfree(i);
}
 
DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_hash_ops, char, string_hash_func, string_compare_func, Item, item_free);
 
static Item* item_new(ItemType type, const char *name, const char *filename, unsigned line) {
        assert(name);
        assert(!!filename == (line > 0));
 
        _cleanup_(item_freep) Item *new = new(Item, 1);
        if (!new)
                return NULL;
 
        *new = (Item) {
                .type = type,
                .line = line,
        };
 
        if (free_and_strdup(&new->name, name) < 0 ||
            free_and_strdup(&new->filename, filename) < 0)
                return NULL;
 
        return TAKE_PTR(new);
}
 
static int add_implicit(Context *c) {
        char *g, **l;
        int r;
 
        assert(c);
 
        /* Implicitly create additional users and groups, if they were listed in "m" lines */
        ORDERED_HASHMAP_FOREACH_KEY(l, g, c->members) {
                STRV_FOREACH(m, l)
                        if (!ordered_hashmap_contains(c->users, *m)) {
                                _cleanup_(item_freep) Item *j =
                                        item_new(ADD_USER, *m, /* filename= */ NULL, /* line= */ 0);
                                if (!j)
                                        return log_oom();
 
                                r = ordered_hashmap_ensure_put(&c->users, &item_hash_ops, j->name, j);
                                if (r == -ENOMEM)
                                        return log_oom();
                                if (r < 0)
                                        return log_error_errno(r, "Failed to add implicit user '%s': %m", j->name);
 
                                log_debug("Adding implicit user '%s' due to m line", j->name);
                                TAKE_PTR(j);
                        }
 
                if (!(ordered_hashmap_contains(c->users, g) ||
                      ordered_hashmap_contains(c->groups, g))) {
                        _cleanup_(item_freep) Item *j =
                                item_new(ADD_GROUP, g, /* filename= */ NULL, /* line= */ 0);
                        if (!j)
                                return log_oom();
 
                        r = ordered_hashmap_ensure_put(&c->groups, &item_hash_ops, j->name, j);
                        if (r == -ENOMEM)
                                return log_oom();
                        if (r < 0)
                                return log_error_errno(r, "Failed to add implicit group '%s': %m", j->name);
 
                        log_debug("Adding implicit group '%s' due to m line", j->name);
                        TAKE_PTR(j);
                }
        }
 
        return 0;
}
 
static int item_equivalent(Item *a, Item *b) {
        int r;
 
        assert(a);
        assert(b);
 
        if (a->type != b->type) {
                log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
                           "Item not equivalent because types differ");
                return false;
        }
 
        if (!streq_ptr(a->name, b->name)) {
                log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
                           "Item not equivalent because names differ ('%s' vs. '%s')",
                           a->name, b->name);
                return false;
        }
 
        /* Paths were simplified previously, so we can use streq. */
        if (!streq_ptr(a->uid_path, b->uid_path)) {
                log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
                           "Item not equivalent because UID paths differ (%s vs. %s)",
                           a->uid_path ?: "(unset)", b->uid_path ?: "(unset)");
                return false;
        }
 
        if (!streq_ptr(a->gid_path, b->gid_path)) {
                log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
                           "Item not equivalent because GID paths differ (%s vs. %s)",
                           a->gid_path ?: "(unset)", b->gid_path ?: "(unset)");
                return false;
        }
 
        if (!streq_ptr(a->description, b->description))  {
                log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
                           "Item not equivalent because descriptions differ ('%s' vs. '%s')",
                           strempty(a->description), strempty(b->description));
                return false;
        }
 
        if ((a->uid_set != b->uid_set) ||
            (a->uid_set && a->uid != b->uid)) {
                log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
                           "Item not equivalent because UIDs differ (%s vs. %s)",
                           a->uid_set ? FORMAT_UID(a->uid) : "(unset)",
                           b->uid_set ? FORMAT_UID(b->uid) : "(unset)");
                return false;
        }
 
        if ((a->gid_set != b->gid_set) ||
            (a->gid_set && a->gid != b->gid)) {
                log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
                           "Item not equivalent because GIDs differ (%s vs. %s)",
                           a->gid_set ? FORMAT_GID(a->gid) : "(unset)",
                           b->gid_set ? FORMAT_GID(b->gid) : "(unset)");
                return false;
        }
 
        if (!streq_ptr(a->home, b->home)) {
                log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
                           "Item not equivalent because home directories differ ('%s' vs. '%s')",
                           strempty(a->description), strempty(b->description));
                return false;
        }
 
        /* Check if the two paths refer to the same file.
         * If the paths are equal (after normalization), it's obviously the same file.
         * If both paths specify a nologin shell, treat them as the same (e.g. /bin/true and /bin/false).
         * Otherwise, try to resolve the paths, and see if we get the same result, (e.g. /sbin/nologin and
         * /usr/sbin/nologin).
         * If we can't resolve something, treat different paths as different. */
 
        const char *a_shell = pick_shell(a),
                   *b_shell = pick_shell(b);
        if (!path_equal(a_shell, b_shell) &&
            !(is_nologin_shell(a_shell) && is_nologin_shell(b_shell))) {
                _cleanup_free_ char *pa = NULL, *pb = NULL;
 
                r = chase(a_shell, arg_root, CHASE_PREFIX_ROOT | CHASE_NONEXISTENT, &pa, NULL);
                if (r < 0) {
                        log_full_errno(ERRNO_IS_RESOURCE(r) ? LOG_ERR : LOG_DEBUG,
                                       r, "Failed to look up path '%s%s%s': %m",
                                       strempty(arg_root), arg_root ? "/" : "", a_shell);
                        return ERRNO_IS_RESOURCE(r) ? r : false;
                }
 
                r = chase(b_shell, arg_root, CHASE_PREFIX_ROOT | CHASE_NONEXISTENT, &pb, NULL);
                if (r < 0) {
                        log_full_errno(ERRNO_IS_RESOURCE(r) ? LOG_ERR : LOG_DEBUG,
                                       r, "Failed to look up path '%s%s%s': %m",
                                       strempty(arg_root), arg_root ? "/" : "", b_shell);
                        return ERRNO_IS_RESOURCE(r) ? r : false;
                }
 
                if (!path_equal(pa, pb)) {
                        log_syntax(NULL, LOG_DEBUG, a->filename, a->line, 0,
                                   "Item not equivalent because shells differ ('%s' vs. '%s')",
                                   pa, pb);
                        return false;
                }
        }
 
        return true;
}
 
static int parse_line(
                const char *fname,
                unsigned line,
                const char *buffer,
                bool *invalid_config,
                void *context) {
 
        Context *c = ASSERT_PTR(context);
        _cleanup_free_ char *action = NULL,
                *name = NULL, *resolved_name = NULL,
                *id = NULL, *resolved_id = NULL,
                *description = NULL, *resolved_description = NULL,
                *home = NULL, *resolved_home = NULL,
                *shell = NULL, *resolved_shell = NULL;
        _cleanup_(item_freep) Item *i = NULL;
        Item *existing;
        OrderedHashmap *h;
        int r;
        const char *p;
 
        assert(fname);
        assert(line >= 1);
        assert(buffer);
        assert(!invalid_config); /* We don't support invalid_config yet. */
 
        /* Parse columns */
        p = buffer;
        r = extract_many_words(&p, NULL, EXTRACT_UNQUOTE,
                               &action, &name, &id, &description, &home, &shell);
        if (r < 0)
                return log_syntax(NULL, LOG_ERR, fname, line, r, "Syntax error.");
        if (r < 2)
                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                  "Missing action and name columns.");
        if (!isempty(p))
                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                  "Trailing garbage.");
 
        if (isempty(action))
                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                  "Empty command specification.");
 
        bool locked = false;
        for (int pos = 1; action[pos]; pos++)
                if (action[pos] == '!' && !locked)
                        locked = true;
                else
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "Unknown modifiers in command '%s'.", action);
 
        if (!IN_SET(action[0], ADD_USER, ADD_GROUP, ADD_MEMBER, ADD_RANGE))
                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                  "Unknown command type '%c'.", action[0]);
 
        /* Verify name */
        if (empty_or_dash(name))
                name = mfree(name);
 
        if (name) {
                r = specifier_printf(name, NAME_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved_name);
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to replace specifiers in '%s': %m", name);
 
                if (!valid_user_group_name(resolved_name, 0))
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "'%s' is not a valid user or group name.", resolved_name);
        }
 
        /* Verify id */
        if (empty_or_dash(id))
                id = mfree(id);
 
        if (id) {
                r = specifier_printf(id, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_id);
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, fname, line, r,
                                          "Failed to replace specifiers in '%s': %m", name);
        }
 
        /* Verify description */
        if (empty_or_dash(description))
                description = mfree(description);
 
        if (description) {
                r = specifier_printf(description, LONG_LINE_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved_description);
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, fname, line, r,
                                          "Failed to replace specifiers in '%s': %m", description);
 
                if (!valid_gecos(resolved_description))
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "'%s' is not a valid GECOS field.", resolved_description);
        }
 
        /* Verify home */
        if (empty_or_dash(home))
                home = mfree(home);
 
        if (home) {
                r = specifier_printf(home, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_home);
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, fname, line, r,
                                          "Failed to replace specifiers in '%s': %m", home);
 
                path_simplify(resolved_home);
 
                if (!valid_home(resolved_home))
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "'%s' is not a valid home directory field.", resolved_home);
        }
 
        /* Verify shell */
        if (empty_or_dash(shell))
                shell = mfree(shell);
 
        if (shell) {
                r = specifier_printf(shell, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_shell);
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, fname, line, r,
                                          "Failed to replace specifiers in '%s': %m", shell);
 
                path_simplify(resolved_shell);
 
                if (!valid_shell(resolved_shell))
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "'%s' is not a valid login shell field.", resolved_shell);
        }
 
        switch (action[0]) {
 
        case ADD_RANGE:
                if (locked)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Flag '!' not permitted on lines of type 'r'.");
 
                if (resolved_name)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Lines of type 'r' don't take a name field.");
 
                if (!resolved_id)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Lines of type 'r' require an ID range in the third field.");
 
                if (description || home || shell)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Lines of type '%c' don't take a %s field.",
                                          action[0],
                                          description ? "GECOS" : home ? "home directory" : "login shell");
 
                r = uid_range_add_str(&c->uid_range, resolved_id);
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Invalid UID range %s.", resolved_id);
 
                return 0;
 
        case ADD_MEMBER: {
                /* Try to extend an existing member or group item */
                if (!name)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Lines of type 'm' require a user name in the second field.");
 
                if (locked)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Flag '!' not permitted on lines of type 'm'.");
 
                if (!resolved_id)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Lines of type 'm' require a group name in the third field.");
 
                if (!valid_user_group_name(resolved_id, 0))
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                               "'%s' is not a valid user or group name.", resolved_id);
 
                if (description || home || shell)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Lines of type '%c' don't take a %s field.",
                                          action[0],
                                          description ? "GECOS" : home ? "home directory" : "login shell");
 
                r = string_strv_ordered_hashmap_put(&c->members, resolved_id, resolved_name);
                if (r < 0)
                        return log_error_errno(r, "Failed to store mapping for %s: %m", resolved_id);
 
                return 0;
        }
 
        case ADD_USER:
                if (!name)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Lines of type 'u' require a user name in the second field.");
 
                r = ordered_hashmap_ensure_allocated(&c->users, &item_hash_ops);
                if (r < 0)
                        return log_oom();
 
                i = item_new(ADD_USER, resolved_name, fname, line);
                if (!i)
                        return log_oom();
 
                if (resolved_id) {
                        if (path_is_absolute(resolved_id))
                                i->uid_path = path_simplify(TAKE_PTR(resolved_id));
                        else {
                                _cleanup_free_ char *uid = NULL, *gid = NULL;
                                if (split_pair(resolved_id, ":", &uid, &gid) == 0) {
                                        r = parse_gid(gid, &i->gid);
                                        if (r < 0) {
                                                if (valid_user_group_name(gid, 0))
                                                        i->group_name = TAKE_PTR(gid);
                                                else
                                                        return log_syntax(NULL, LOG_ERR, fname, line, r,
                                                                          "Failed to parse GID: '%s': %m", id);
                                        } else {
                                                i->gid_set = true;
                                                i->id_set_strict = true;
                                        }
                                        free_and_replace(resolved_id, uid);
                                }
                                if (!streq(resolved_id, "-")) {
                                        r = parse_uid(resolved_id, &i->uid);
                                        if (r < 0)
                                                return log_syntax(NULL, LOG_ERR, fname, line, r,
                                                                  "Failed to parse UID: '%s': %m", id);
                                        i->uid_set = true;
                                }
                        }
                }
 
                i->description = TAKE_PTR(resolved_description);
                i->home = TAKE_PTR(resolved_home);