HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/tmpfiles/tmpfiles.c

203,319 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: c4caa0b857918b2141bedc1762e40c4e54e030a4

Lines 4001–5000 of 5,453

}
 
static int process_item_array(
                Context *c,
                ItemArray *array,
                OperationMask operation) {
 
        int r = 0;
 
        assert(c);
        assert(array);
 
        /* Create any parent first. */
        if (FLAGS_SET(operation, OPERATION_CREATE) && array->parent)
                r = process_item_array(c, array->parent, operation & OPERATION_CREATE);
 
        /* Clean up all children first */
        if ((operation & (OPERATION_REMOVE|OPERATION_CLEAN|OPERATION_PURGE)) && !set_isempty(array->children)) {
                ItemArray *cc;
 
                SET_FOREACH(cc, array->children)
                        RET_GATHER(r, process_item_array(c, cc, operation & (OPERATION_REMOVE|OPERATION_CLEAN|OPERATION_PURGE)));
        }
 
        FOREACH_ARRAY(item, array->items, array->n_items)
                RET_GATHER(r, process_item(c, item, operation));
 
        return r;
}
 
static void item_free_contents(Item *i) {
        assert(i);
        free(i->path);
        free(i->argument);
        free(i->binary_argument);
        strv_free(i->xattrs);
 
#if HAVE_ACL
        if (i->acl_access)
                sym_acl_free(i->acl_access);
 
        if (i->acl_access_exec)
                sym_acl_free(i->acl_access_exec);
 
        if (i->acl_default)
                sym_acl_free(i->acl_default);
#endif
}
 
static ItemArray* item_array_free(ItemArray *a) {
        if (!a)
                return NULL;
 
        FOREACH_ARRAY(item, a->items, a->n_items)
                item_free_contents(item);
 
        set_free(a->children);
        free(a->items);
        return mfree(a);
}
 
static int item_compare(const Item *a, const Item *b) {
        /* Make sure that the ownership taking item is put first, so
         * that we first create the node, and then can adjust it */
 
        if (takes_ownership(a->type) && !takes_ownership(b->type))
                return -1;
        if (!takes_ownership(a->type) && takes_ownership(b->type))
                return 1;
 
        return CMP(a->type, b->type);
}
 
static bool item_compatible(const Item *a, const Item *b) {
        assert(a);
        assert(b);
        assert(streq(a->path, b->path));
 
        if (takes_ownership(a->type) && takes_ownership(b->type))
                /* check if the items are the same */
                return memcmp_nn(item_binary_argument(a), item_binary_argument_size(a),
                                 item_binary_argument(b), item_binary_argument_size(b)) == 0 &&
 
                        a->uid_set == b->uid_set &&
                        a->uid == b->uid &&
                        a->uid_only_create == b->uid_only_create &&
 
                        a->gid_set == b->gid_set &&
                        a->gid == b->gid &&
                        a->gid_only_create == b->gid_only_create &&
 
                        a->mode_set == b->mode_set &&
                        a->mode == b->mode &&
                        a->mode_only_create == b->mode_only_create &&
 
                        a->age_set == b->age_set &&
                        a->age == b->age &&
 
                        a->age_by_file == b->age_by_file &&
                        a->age_by_dir == b->age_by_dir &&
 
                        a->mask_perms == b->mask_perms &&
 
                        a->keep_first_level == b->keep_first_level &&
 
                        a->major_minor == b->major_minor;
 
        return true;
}
 
static bool should_include_path(const char *path) {
        STRV_FOREACH(prefix, arg_exclude_prefixes)
                if (path_startswith(path, *prefix)) {
                        log_debug("Entry \"%s\" matches exclude prefix \"%s\", skipping.",
                                  path, *prefix);
                        return false;
                }
 
        STRV_FOREACH(prefix, arg_include_prefixes)
                if (path_startswith(path, *prefix)) {
                        log_debug("Entry \"%s\" matches include prefix \"%s\".", path, *prefix);
                        return true;
                }
 
        /* no matches, so we should include this path only if we have no allow list at all */
        if (strv_isempty(arg_include_prefixes))
                return true;
 
        log_debug("Entry \"%s\" does not match any include prefix, skipping.", path);
        return false;
}
 
static int specifier_expansion_from_arg(const Specifier *specifier_table, Item *i) {
        int r;
 
        assert(i);
 
        if (!i->argument)
                return 0;
 
        switch (i->type) {
        case COPY_FILES:
        case CREATE_SYMLINK:
        case CREATE_FILE:
        case TRUNCATE_FILE:
        case WRITE_FILE: {
                _cleanup_free_ char *unescaped = NULL, *resolved = NULL;
                ssize_t l;
 
                l = cunescape(i->argument, 0, &unescaped);
                if (l < 0)
                        return log_error_errno(l, "Failed to unescape parameter to write: %s", i->argument);
 
                r = specifier_printf(unescaped, PATH_MAX-1, specifier_table, arg_root, NULL, &resolved);
                if (r < 0)
                        return r;
 
                return free_and_replace(i->argument, resolved);
        }
        case SET_XATTR:
        case RECURSIVE_SET_XATTR:
                STRV_FOREACH(xattr, i->xattrs) {
                        _cleanup_free_ char *resolved = NULL;
 
                        r = specifier_printf(*xattr, SIZE_MAX, specifier_table, arg_root, NULL, &resolved);
                        if (r < 0)
                                return r;
 
                        free_and_replace(*xattr, resolved);
                }
                return 0;
 
        default:
                return 0;
        }
}
 
static int patch_var_run(const char *fname, unsigned line, char **path) {
        const char *k;
        char *n;
 
        assert(path);
        assert(*path);
 
        /* Optionally rewrites lines referencing /var/run/, to use /run/ instead. Why bother? tmpfiles merges
         * lines in some cases and detects conflicts in others. If files/directories are specified through
         * two equivalent lines this is problematic as neither case will be detected. Ideally we'd detect
         * these cases by resolving symlinks early, but that's precisely not what we can do here as this code
         * very likely is running very early on, at a time where the paths in question are not available yet,
         * or even more importantly, our own tmpfiles rules might create the paths that are intermediary to
         * the listed paths. We can't really cover the generic case, but the least we can do is cover the
         * specific case of /var/run vs. /run, as /var/run is a legacy name for /run only, and we explicitly
         * document that and require that on systemd systems the former is a symlink to the latter. Moreover
         * files below this path are by far the primary use case for tmpfiles.d/. */
 
        k = path_startswith(*path, "/var/run/");
        if (isempty(k)) /* Don't complain about paths other than under /var/run,
                         * and not about /var/run itself either. */
                return 0;
 
        n = path_join("/run", k);
        if (!n)
                return log_oom();
 
        /* Also log about this briefly. We do so at LOG_NOTICE level, as we fixed up the situation
         * automatically, hence there's no immediate need for action by the user. However, in the interest of
         * making things less confusing to the user, let's still inform the user that these snippets should
         * really be updated. */
        log_syntax(NULL, LOG_NOTICE, fname, line, 0,
                   "Line references path below legacy directory /var/run/, updating %s → %s; please update the tmpfiles.d/ drop-in file accordingly.",
                   *path, n);
 
        free_and_replace(*path, n);
 
        return 0;
}
 
static int find_uid(const char *user, uid_t *ret_uid, Hashmap **cache) {
        int r;
 
        assert(user);
        assert(ret_uid);
 
        /* First: parse as numeric UID string */
        r = parse_uid(user, ret_uid);
        if (r >= 0)
                return r;
 
        /* Second: pass to NSS if we are running "online" */
        if (!arg_root)
                return get_user_creds(user, /* flags= */ 0, NULL, ret_uid, NULL, NULL, NULL);
 
        /* Third, synthesize "root" unconditionally */
        if (streq(user, "root")) {
                *ret_uid = 0;
                return 0;
        }
 
        /* Fourth: use fgetpwent() to read /etc/passwd directly, if we are "offline" */
        return name_to_uid_offline(arg_root, user, ret_uid, cache);
}
 
static int find_gid(const char *group, gid_t *ret_gid, Hashmap **cache) {
        int r;
 
        assert(group);
        assert(ret_gid);
 
        /* First: parse as numeric GID string */
        r = parse_gid(group, ret_gid);
        if (r >= 0)
                return r;
 
        /* Second: pass to NSS if we are running "online" */
        if (!arg_root)
                return get_group_creds(group, /* flags= */ 0, /* ret_name= */ NULL, ret_gid);
 
        /* Third, synthesize "root" unconditionally */
        if (streq(group, "root")) {
                *ret_gid = 0;
                return 0;
        }
 
        /* Fourth: use fgetgrent() to read /etc/group directly, if we are "offline" */
        return name_to_gid_offline(arg_root, group, ret_gid, cache);
}
 
static int parse_age_by_from_arg(const char *age_by_str, Item *item) {
        AgeBy ab_f = 0, ab_d = 0;
 
        static const struct {
                char age_by_chr;
                AgeBy age_by_flag;
        } age_by_types[] = {
                { 'a', AGE_BY_ATIME },
                { 'b', AGE_BY_BTIME },
                { 'c', AGE_BY_CTIME },
                { 'm', AGE_BY_MTIME },
        };
 
        assert(age_by_str);
        assert(item);
 
        if (isempty(age_by_str))
                return -EINVAL;
 
        for (const char *s = age_by_str; *s != 0; s++) {
                size_t i;
 
                /* Ignore whitespace. */
                if (strchr(WHITESPACE, *s))
                        continue;
 
                for (i = 0; i < ELEMENTSOF(age_by_types); i++) {
                        /* Check lower-case for files, upper-case for directories. */
                        if (*s == age_by_types[i].age_by_chr) {
                                ab_f |= age_by_types[i].age_by_flag;
                                break;
                        } else if (*s == ascii_toupper(age_by_types[i].age_by_chr)) {
                                ab_d |= age_by_types[i].age_by_flag;
                                break;
                        }
                }
 
                /* Invalid character. */
                if (i >= ELEMENTSOF(age_by_types))
                        return -EINVAL;
        }
 
        /* No match. */
        if (ab_f == 0 && ab_d == 0)
                return -EINVAL;
 
        item->age_by_file = ab_f > 0 ? ab_f : AGE_BY_DEFAULT_FILE;
        item->age_by_dir = ab_d > 0 ? ab_d : AGE_BY_DEFAULT_DIR;
 
        return 0;
}
 
static bool is_duplicated_item(ItemArray *existing, const Item *i) {
        assert(existing);
        assert(i);
 
        FOREACH_ARRAY(e, existing->items, existing->n_items) {
                if (item_compatible(e, i))
                        continue;
 
                /* Only multiple 'w+' lines for the same path are allowed. */
                if (e->type != WRITE_FILE || !e->append_or_force ||
                    i->type != WRITE_FILE || !i->append_or_force)
                        return true;
        }
 
        return false;
}
 
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, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
        _cleanup_(item_free_contents) Item i = {
                /* The "age-by" argument considers all file timestamp types by default. */
                .age_by_file = AGE_BY_DEFAULT_FILE,
                .age_by_dir = AGE_BY_DEFAULT_DIR,
        };
        ItemArray *existing;
        OrderedHashmap *h;
        bool append_or_force = false, boot = false, allow_failure = false, try_replace = false,
                unbase64 = false, from_cred = false, missing_user_or_group = false, purge = false,
                ignore_if_target_missing = false;
        int r;
 
        assert(fname);
        assert(line >= 1);
        assert(buffer);
        assert(invalid_config);
 
        const Specifier specifier_table[] = {
                { 'h', specifier_user_home,       NULL },
 
                { 'C', specifier_directory,       UINT_TO_PTR(DIRECTORY_CACHE)   },
                { 'D', specifier_directory,       UINT_TO_PTR(DIRECTORY_SHARED)  },
                { 'L', specifier_directory,       UINT_TO_PTR(DIRECTORY_LOGS)    },
                { 'S', specifier_directory,       UINT_TO_PTR(DIRECTORY_STATE)   },
                { 't', specifier_directory,       UINT_TO_PTR(DIRECTORY_RUNTIME) },
 
                COMMON_SYSTEM_SPECIFIERS,
                COMMON_CREDS_SPECIFIERS(arg_runtime_scope),
                COMMON_TMP_SPECIFIERS,
                {}
        };
 
        r = extract_many_words(
                        &buffer,
                        NULL,
                        EXTRACT_UNQUOTE | EXTRACT_CUNESCAPE,
                        &action,
                        &path,
                        &mode,
                        &user,
                        &group,
                        &age);
        if (r < 0) {
                if (IN_SET(r, -EINVAL, -EBADSLT))
                        /* invalid quoting and such or an unknown specifier */
                        *invalid_config = true;
                return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to parse line: %m");
        } else if (r < 2) {
                *invalid_config = true;
                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), "Syntax error.");
        }
 
        if (!empty_or_dash(buffer)) {
                i.argument = strdup(buffer);
                if (!i.argument)
                        return log_oom();
        }
 
        if (isempty(action)) {
                *invalid_config = true;
                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                  "Command too short '%s'.", action);
        }
 
        for (int pos = 1; action[pos]; pos++)
                if (action[pos] == '!' && !boot)
                        boot = true;
                else if (action[pos] == '+' && !append_or_force)
                        append_or_force = true;
                else if (action[pos] == '-' && !allow_failure)
                        allow_failure = true;
                else if (action[pos] == '=' && !try_replace)
                        try_replace = true;
                else if (action[pos] == '~' && !unbase64)
                        unbase64 = true;
                else if (action[pos] == '^' && !from_cred)
                        from_cred = true;
                else if (action[pos] == '$' && !purge)
                        purge = true;
                else if (action[pos] == '?' && !ignore_if_target_missing)
                        ignore_if_target_missing = true;
                else {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "Unknown modifiers in command: %s", action);
                }
 
        if (boot && !arg_boot) {
                log_syntax(NULL, LOG_DEBUG, fname, line, 0,
                           "Ignoring entry %s \"%s\" because --boot is not specified.", action, path);
                return 0;
        }
 
        i.type = action[0];
        i.append_or_force = append_or_force;
        i.allow_failure = allow_failure;
        i.try_replace = try_replace;
        i.purge = purge;
        i.ignore_if_target_missing = ignore_if_target_missing;
 
        r = specifier_printf(path, PATH_MAX-1, specifier_table, arg_root, NULL, &i.path);
        if (ERRNO_IS_NEG_NOINFO(r))
                return log_unresolvable_specifier(fname, line);
        if (r < 0) {
                if (IN_SET(r, -EINVAL, -EBADSLT))
                        *invalid_config = true;
                return log_syntax(NULL, LOG_ERR, fname, line, r,
                                  "Failed to replace specifiers in '%s': %m", path);
        }
 
        r = patch_var_run(fname, line, &i.path);
        if (r < 0)
                return r;
 
        if (!path_is_absolute(i.path)) {
                *invalid_config = true;
                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                  "Path '%s' not absolute.", i.path);
        }
 
        path_simplify(i.path);
 
        switch (i.type) {
 
        case CREATE_DIRECTORY:
        case CREATE_SUBVOLUME:
        case CREATE_SUBVOLUME_INHERIT_QUOTA:
        case CREATE_SUBVOLUME_NEW_QUOTA:
        case EMPTY_DIRECTORY:
        case TRUNCATE_DIRECTORY:
        case CREATE_FIFO:
        case IGNORE_PATH:
        case IGNORE_DIRECTORY_PATH:
        case REMOVE_PATH:
        case RECURSIVE_REMOVE_PATH:
        case ADJUST_MODE:
        case RELABEL_PATH:
        case RECURSIVE_RELABEL_PATH:
                if (i.argument) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "%c lines don't take argument fields.", (char) i.type);
                }
                break;
 
        case CREATE_FILE:
        case TRUNCATE_FILE:
                break;
 
        case CREATE_SYMLINK:
                if (unbase64) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "base64 decoding not supported for symlink targets.");
                }
                break;
 
        case WRITE_FILE:
                if (!i.argument) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "Write file requires argument.");
                }
                break;
 
        case COPY_FILES:
                if (unbase64) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "base64 decoding not supported for copy sources.");
                }
                break;
 
        case CREATE_CHAR_DEVICE:
        case CREATE_BLOCK_DEVICE:
                if (unbase64) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "base64 decoding not supported for device node creation.");
                }
 
                if (!i.argument) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "Device file requires argument.");
                }
 
                r = parse_devnum(i.argument, &i.major_minor);
                if (r < 0) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, r,
                                          "Can't parse device file major/minor '%s'.", i.argument);
                }
 
                break;
 
        case SET_XATTR:
        case RECURSIVE_SET_XATTR:
                if (unbase64) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "base64 decoding not supported for extended attributes.");
                }
                if (!i.argument) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "Set extended attribute requires argument.");
                }
                r = parse_xattrs_from_arg(&i);
                if (r < 0)
                        return r;
                break;
 
        case SET_ACL:
        case RECURSIVE_SET_ACL:
                if (unbase64) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "base64 decoding not supported for ACLs.");
                }
                if (!i.argument) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "Set ACLs requires argument.");
                }
                r = parse_acls_from_arg(&i);
                if (r < 0)
                        return r;
                break;
 
        case SET_FCAPS:
        case RECURSIVE_SET_FCAPS:
                if (unbase64) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "base64 decoding not supported for capabilities.");
                }
                if (!i.argument) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "Set capabilities requires argument.");
                }
                r = parse_caps_from_arg(&i);
                if (r < 0)
                        return r;
                break;
 
        case SET_ATTRIBUTE:
        case RECURSIVE_SET_ATTRIBUTE:
                if (unbase64) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "base64 decoding not supported for file attributes.");
                }
                if (!i.argument) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "Set file attribute requires argument.");
                }
                r = parse_attribute_from_arg(&i);
                if (IN_SET(r, -EINVAL, -EBADSLT))
                        *invalid_config = true;
                if (r < 0)
                        return r;
                break;
 
        default:
                *invalid_config = true;
                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                  "Unknown command type '%c'.", (char) i.type);
        }
 
        if (i.purge && !needs_purge(i.type)) {
                *invalid_config = true;
                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                  "Purge flag '$' combined with line type '%c' which does not support purging.", (char) i.type);
        }
 
        if (i.ignore_if_target_missing && !supports_ignore_if_target_missing(i.type)) {
                *invalid_config = true;
                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                  "Modifier '?' combined with line type '%c' which does not support this modifier.", (char) i.type);
        }
 
        if (!should_include_path(i.path))
                return 0;
 
        if (!unbase64) {
                /* Do specifier expansion except if base64 mode is enabled */
                r = specifier_expansion_from_arg(specifier_table, &i);
                if (ERRNO_IS_NEG_NOINFO(r))
                        return log_unresolvable_specifier(fname, line);
                if (r < 0) {
                        if (IN_SET(r, -EINVAL, -EBADSLT))
                                *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, r,
                                          "Failed to substitute specifiers in argument: %m");
                }
        }
 
        switch (i.type) {
        case CREATE_SYMLINK:
                if (!i.argument) {
                        i.argument = path_join("/usr/share/factory", i.path);
                        if (!i.argument)
                                return log_oom();
                }
 
                break;
 
        case COPY_FILES:
                if (!i.argument) {
                        i.argument = path_join("/usr/share/factory", i.path);
                        if (!i.argument)
                                return log_oom();
                } else if (!path_is_absolute(i.argument)) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                          "Source path '%s' is not absolute.", i.argument);
 
                }
 
                if (!empty_or_root(arg_root)) {
                        char *p;
 
                        p = path_join(arg_root, i.argument);
                        if (!p)
                                return log_oom();
                        free_and_replace(i.argument, p);
                }
 
                path_simplify(i.argument);
 
                if (access_nofollow(i.argument, F_OK) == -ENOENT) {
                        /* Silently skip over lines where the source file is missing. */
                        log_syntax(NULL, LOG_DEBUG, fname, line, 0,
                                   "Copy source path '%s' does not exist, skipping line.", i.argument);
                        return 0;
                }
 
                break;
 
        default:
                ;
        }
 
        if (from_cred) {
                if (!i.argument)
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Reading from credential requested, but no credential name specified.");
                if (!credential_name_valid(i.argument))
                        return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
                                          "Credential name not valid: %s", i.argument);
 
                r = read_credential(i.argument, &i.binary_argument, &i.binary_argument_size);
                if (IN_SET(r, -ENXIO, -ENOENT)) {
                        /* Silently skip over lines that have no credentials passed */
                        log_syntax(NULL, LOG_DEBUG, fname, line, 0,
                                   "Credential '%s' not specified, skipping line.", i.argument);
                        return 0;
                }
                if (r < 0)
                        return log_error_errno(r, "Failed to read credential '%s': %m", i.argument);
        }
 
        /* If base64 decoding is requested, do so now */
        if (unbase64 && item_binary_argument(&i)) {
                _cleanup_free_ void *data = NULL;
                size_t data_size = 0;
 
                r = unbase64mem_full(item_binary_argument(&i), item_binary_argument_size(&i), /* secure= */ false,
                                     &data, &data_size);
                if (r < 0)
                        return log_syntax(NULL, LOG_ERR, fname, line, r, "Failed to base64 decode specified argument '%s': %m", i.argument);
 
                free_and_replace(i.binary_argument, data);
                i.binary_argument_size = data_size;
        }
 
        if (!empty_or_root(arg_root)) {
                char *p;
 
                p = path_join(arg_root, i.path);
                if (!p)
                        return log_oom();
                free_and_replace(i.path, p);
        }
 
        if (!empty_or_dash(user)) {
                const char *u;
 
                u = startswith(user, ":");
                if (u)
                        i.uid_only_create = true;
                else
                        u = user;
 
                r = find_uid(u, &i.uid, &c->uid_cache);
                if (r == -ESRCH && arg_graceful) {
                        log_syntax(NULL, LOG_DEBUG, fname, line, r,
                                   "%s: user '%s' not found, not adjusting ownership.", i.path, u);
                        missing_user_or_group = true;
                } else if (r < 0) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, r,
                                          "Failed to resolve user '%s': %s", u, STRERROR_USER(r));
                } else
                        i.uid_set = true;
        }
 
        if (!empty_or_dash(group)) {
                const char *g;
 
                g = startswith(group, ":");
                if (g)
                        i.gid_only_create = true;
                else
                        g = group;
 
                r = find_gid(g, &i.gid, &c->gid_cache);
                if (r == -ESRCH && arg_graceful) {
                        log_syntax(NULL, LOG_DEBUG, fname, line, r,
                                   "%s: group '%s' not found, not adjusting ownership.", i.path, g);
                        missing_user_or_group = true;
                } else if (r < 0) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, r,
                                          "Failed to resolve group '%s': %s", g, STRERROR_GROUP(r));
                } else
                        i.gid_set = true;
        }
 
        if (!empty_or_dash(mode)) {
                const char *mm;
                unsigned m;
 
                for (mm = mode;; mm++)
                        if (*mm == '~')
                                i.mask_perms = true;
                        else if (*mm == ':')
                                i.mode_only_create = true;
                        else
                                break;
 
                r = parse_mode(mm, &m);
                if (r < 0) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid mode '%s'.", mode);
                }
 
                i.mode = m;
                i.mode_set = true;
        } else
                i.mode = IN_SET(i.type,
                                CREATE_DIRECTORY,
                                TRUNCATE_DIRECTORY,
                                CREATE_SUBVOLUME,
                                CREATE_SUBVOLUME_INHERIT_QUOTA,
                                CREATE_SUBVOLUME_NEW_QUOTA) ? 0755 : 0644;
 
        if (missing_user_or_group && (i.mode & ~0777) != 0) {
                /* Refuse any special bits for nodes where we couldn't resolve the ownership properly. */
                mode_t adjusted = i.mode & 0777;
                log_syntax(NULL, LOG_INFO, fname, line, 0,
                           "Changing mode 0%o to 0%o because of changed ownership.", i.mode, adjusted);
                i.mode = adjusted;
        }
 
        if (!empty_or_dash(age)) {
                const char *a = age;
                _cleanup_free_ char *seconds = NULL, *age_by = NULL;
 
                if (*a == '~') {
                        if (i.type == REMOVE_PATH) {
                                *invalid_config = true;
                                return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG),
                                                  "Age modifier '~' is not supported for %c lines.", (char) i.type);
                        }
 
                        i.keep_first_level = true;
                        a++;
                }
 
                /* Format: "age-by:age"; where age-by is "[abcmABCM]+". */
                r = split_pair(a, ":", &age_by, &seconds);
                if (r == -ENOMEM)
                        return log_oom();
                if (r < 0 && r != -EINVAL)
                        return log_error_errno(r, "Failed to parse age-by for '%s': %m", age);
                if (r >= 0) {
                        /* We found a ":", parse the "age-by" part. */
                        r = parse_age_by_from_arg(age_by, &i);
                        if (r == -ENOMEM)
                                return log_oom();
                        if (r < 0) {
                                *invalid_config = true;
                                return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age-by '%s'.", age_by);
                        }
 
                        /* For parsing the "age" part, after the ":". */
                        a = seconds;
                }
 
                r = parse_sec(a, &i.age);
                if (r < 0) {
                        *invalid_config = true;
                        return log_syntax(NULL, LOG_ERR, fname, line, r, "Invalid age '%s'.", a);
                }
 
                i.age_set = true;
        }
 
        h = needs_glob(i.type) ? c->globs : c->items;
 
        existing = ordered_hashmap_get(h, i.path);
        if (existing) {
                if (is_duplicated_item(existing, &i)) {
                        log_syntax(NULL, LOG_NOTICE, fname, line, 0,
                                   "Duplicate line for path \"%s\", ignoring.", i.path);
                        return 0;
                }
        } else {
                existing = new0(ItemArray, 1);
                if (!existing)
                        return log_oom();
 
                r = ordered_hashmap_put(h, i.path, existing);
                if (r < 0) {
                        free(existing);
                        return log_oom();
                }
        }
 
        if (!GREEDY_REALLOC(existing->items, existing->n_items + 1))
                return log_oom();
 
        existing->items[existing->n_items++] = TAKE_STRUCT(i);
 
        /* Sort item array, to enforce stable ordering of application */
        typesafe_qsort(existing->items, existing->n_items, item_compare);
 
        return 0;
}
 
static int cat_config(char **config_dirs, char **args) {
        _cleanup_strv_free_ char **files = NULL;
        int r;
 
        r = conf_files_list_with_replacement(arg_root, config_dirs, arg_replace, &files, NULL);
        if (r < 0)
                return r;
 
        pager_open(arg_pager_flags);
 
        return cat_files(NULL, files, arg_cat_flags);
}
 
static int exclude_default_prefixes(void) {
        int r;
 
        /* Provide an easy way to exclude virtual/memory file systems from what we do here. Useful in
         * combination with --root= where we probably don't want to apply stuff to these dirs as they are
         * likely over-mounted if the root directory is actually used, and it wouldbe less than ideal to have
         * all kinds of files created/adjusted underneath these mount points. */
 
        r = strv_extend_many(
                        &arg_exclude_prefixes,
                        "/dev",
                        "/proc",
                        "/run",
                        "/sys");
        if (r < 0)
                return log_oom();
 
        strv_uniq(arg_exclude_prefixes);
        return 0;
}
 
static int parse_argv(int argc, char *argv[], char ***ret_args) {
        int r;
 
        assert(argc >= 0);
        assert(argv);
 
        OptionParser opts = { argc, argv };
 
        FOREACH_OPTION_OR_RETURN(c, &opts)
                switch (c) {
 
                OPTION_GROUP("Commands"): {}
 
                OPTION_LONG("create", NULL, "Create and adjust files and directories"):
                        arg_operation |= OPERATION_CREATE;
                        break;
 
                OPTION_LONG("clean", NULL, "Clean up files and directories"):
                        arg_operation |= OPERATION_CLEAN;
                        break;
 
                OPTION_LONG("remove", NULL, "Remove files and directories marked for removal"):
                        arg_operation |= OPERATION_REMOVE;
                        break;
 
                OPTION_LONG("purge", NULL,
                            "Delete files and directories marked for creation in"
                            " specified configuration files (careful!)"):
                        arg_operation |= OPERATION_PURGE;
                        break;
 
                OPTION_COMMON_CAT_CONFIG:
                        arg_cat_flags = CAT_CONFIG_ON;
                        break;
 
                OPTION_COMMON_TLDR:
                        arg_cat_flags = CAT_TLDR;
                        break;
 
                OPTION_COMMON_HELP:
                        return command_print_help();
 
                OPTION_COMMON_VERSION:
                        return version();
 
                OPTION_GROUP("Options"): {}
 
                OPTION_LONG("user", NULL, "Execute user configuration"):
                        arg_runtime_scope = RUNTIME_SCOPE_USER;
                        break;
 
                OPTION_LONG("boot", NULL, "Execute actions only safe at boot"):
                        arg_boot = true;
                        break;
 
                OPTION_LONG("graceful", NULL, "Quietly ignore unknown users or groups"):
                        arg_graceful = true;
                        break;
 
                OPTION_LONG("prefix", "PATH", "Only apply rules with the specified prefix"):
                        if (strv_extend(&arg_include_prefixes, opts.arg) < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("exclude-prefix", "PATH", "Ignore rules with the specified prefix"):
                        if (strv_extend(&arg_exclude_prefixes, opts.arg) < 0)
                                return log_oom();
                        break;
 
                OPTION_SHORT('E', NULL, "Ignore rules prefixed with /dev, /proc, /run, /sys"):
                        r = exclude_default_prefixes();
                        if (r < 0)
                                return r;
                        break;
 
                OPTION_LONG("root", "PATH", "Operate on an alternate filesystem root"):
                        r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_root);