HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/sysusers/sysusers.c
Git blob: 3771185f538a8e5a5122153e1bfaf83112f378fe
Lines 2001–2416 of 2,416
i->shell = TAKE_PTR(resolved_shell);
i->locked = locked;
h = c->users;
break;
case ADD_GROUP:
if (!name)
return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EINVAL),
"Lines of type 'g' 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 'g'.");
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 = ordered_hashmap_ensure_allocated(&c->groups, &item_hash_ops);
if (r < 0)
return log_oom();
i = item_new(ADD_GROUP, resolved_name, fname, line);
if (!i)
return log_oom();
if (resolved_id) {
if (path_is_absolute(resolved_id))
i->gid_path = path_simplify(TAKE_PTR(resolved_id));
else {
r = parse_gid(resolved_id, &i->gid);
if (r < 0)
return log_syntax(NULL, LOG_ERR, fname, line, r,
"Failed to parse GID: '%s': %m", id);
i->gid_set = true;
}
}
h = c->groups;
break;
default:
assert_not_reached();
}
existing = ordered_hashmap_get(h, i->name);
if (existing) {
/* Two functionally-equivalent items are fine */
r = item_equivalent(i, existing);
if (r < 0)
return r;
if (r == 0) {
if (existing->filename)
log_syntax(NULL, LOG_WARNING, fname, line, 0,
"Conflict with earlier configuration for %s '%s' in %s:%u, ignoring line.",
item_type_to_string(i->type),
i->name,
existing->filename, existing->line);
else
log_syntax(NULL, LOG_WARNING, fname, line, 0,
"Conflict with earlier configuration for %s '%s', ignoring line.",
item_type_to_string(i->type),
i->name);
}
return 0;
}
r = ordered_hashmap_put(h, i->name, i);
if (r < 0)
return log_oom();
i = NULL;
return 0;
}
static int read_config_file(Context *c, const char *fn, bool ignore_enoent) {
return conf_file_read(
arg_root,
(const char**) CONF_PATHS_STRV("sysusers.d"),
ASSERT_PTR(fn),
parse_line,
ASSERT_PTR(c),
ignore_enoent,
/* invalid_config= */ NULL);
}
static int cat_config(void) {
_cleanup_strv_free_ char **files = NULL;
int r;
r = conf_files_list_with_replacement(arg_root, CONF_PATHS_STRV("sysusers.d"), arg_replace, &files, NULL);
if (r < 0)
return r;
pager_open(arg_pager_flags);
return cat_files(NULL, files, arg_cat_flags);
}
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_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("root", "PATH", "Operate on an alternate filesystem root"):
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_root);
if (r < 0)
return r;
break;
OPTION_LONG("image", "PATH", "Operate on disk image as filesystem root"):
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image);
if (r < 0)
return r;
break;
OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"):
r = parse_image_policy_argument(opts.arg, &arg_image_policy);
if (r < 0)
return r;
break;
OPTION_LONG("replace", "PATH", "Treat arguments as replacement for PATH"):
if (!path_is_absolute(opts.arg))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"The argument to --replace= must be an absolute path.");
if (!endswith(opts.arg, ".conf"))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"The argument to --replace= must have the extension '.conf'.");
arg_replace = opts.arg;
break;
OPTION('n', "dry-run", NULL, "Just print what would be done"):
arg_dry_run = true;
break;
OPTION_LONG("inline", NULL, "Treat arguments as configuration lines"):
arg_inline = true;
break;
OPTION_COMMON_NO_PAGER:
arg_pager_flags |= PAGER_DISABLE;
break;
OPTION_COMMON_INTROSPECT_CLI:
return introspect_cli(SD_JSON_FORMAT_OFF);
}
char **args = option_parser_get_args(&opts);
size_t n_args = option_parser_get_n_args(&opts);
if (arg_replace && arg_cat_flags != CAT_CONFIG_OFF)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Option --replace= is not supported with --cat-config/--tldr.");
if (arg_inline && arg_cat_flags != CAT_CONFIG_OFF)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Option --inline is not supported with --cat-config/--tldr.");
if (arg_replace && n_args == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"When --replace= is given, some configuration items must be specified.");
if (arg_image && arg_root)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Use either --root= or --image=, the combination of both is not supported.");
*ret_args = args;
return 1;
}
static int parse_arguments(Context *c, char **args) {
unsigned pos = 1;
int r;
assert(c);
STRV_FOREACH(arg, args) {
if (arg_inline)
/* Use (argument):n, where n==1 for the first positional arg */
r = parse_line("(argument)", pos, *arg, /* invalid_config= */ NULL, c);
else
r = read_config_file(c, *arg, /* ignore_enoent= */ false);
if (r < 0)
return r;
pos++;
}
return 0;
}
static int read_config_files(Context *c, char **args) {
_cleanup_strv_free_ char **files = NULL;
_cleanup_free_ char *p = NULL;
int r;
assert(c);
r = conf_files_list_with_replacement(arg_root, CONF_PATHS_STRV("sysusers.d"), arg_replace, &files, &p);
if (r < 0)
return r;
STRV_FOREACH(f, files)
if (p && path_equal(*f, p)) {
log_debug("Parsing arguments at position \"%s\"%s", *f, glyph(GLYPH_ELLIPSIS));
r = parse_arguments(c, args);
if (r < 0)
return r;
} else {
log_debug("Reading config file \"%s\"%s", *f, glyph(GLYPH_ELLIPSIS));
/* Just warn, ignore result otherwise */
(void) read_config_file(c, *f, /* ignore_enoent= */ true);
}
return 0;
}
static int read_credential_lines(Context *c) {
_cleanup_free_ char *j = NULL;
const char *d;
int r;
assert(c);
r = get_credentials_dir(&d);
if (r == -ENXIO)
return 0;
if (r < 0)
return log_error_errno(r, "Failed to get credentials directory: %m");
j = path_join(d, "sysusers.extra");
if (!j)
return log_oom();
(void) read_config_file(c, j, /* ignore_enoent= */ true);
return 0;
}
static int run(int argc, char *argv[]) {
_cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
_cleanup_(umount_and_freep) char *mounted_dir = NULL;
_cleanup_close_ int lock = -EBADF;
_cleanup_(context_done) Context c = {
.audit_fd = -EBADF,
.search_uid = UID_INVALID,
};
Item *i;
int r;
LIBAUDIT_NOTE(recommended);
LIBBLKID_NOTE(recommended);
LIBCRYPT_NOTE(recommended);
LIBCRYPTSETUP_NOTE(suggested);
LIBCRYPTO_NOTE(suggested);
LIBMOUNT_NOTE(recommended);
LIBSELINUX_NOTE(recommended);
char **args = NULL;
r = parse_argv(argc, argv, &args);
if (r <= 0)
return r;
log_setup();
if (arg_cat_flags != CAT_CONFIG_OFF)
return cat_config();
if (should_bypass("SYSTEMD_SYSUSERS"))
return 0;
umask(0022);
r = mac_init();
if (r < 0)
return r;
if (arg_image) {
assert(!arg_root);
r = mount_image_privately_interactively(
arg_image,
arg_image_policy,
DISSECT_IMAGE_GENERIC_ROOT |
DISSECT_IMAGE_REQUIRE_ROOT |
DISSECT_IMAGE_VALIDATE_OS |
DISSECT_IMAGE_RELAX_VAR_CHECK |
DISSECT_IMAGE_FSCK |
DISSECT_IMAGE_GROWFS |
DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
&mounted_dir,
/* ret_dir_fd= */ NULL,
&loop_device);
if (r < 0)
return r;
arg_root = strdup(mounted_dir);
if (!arg_root)
return log_oom();
}
r = mac_label_context_new(arg_root, &c.label_context);
if (r < 0)
return log_error_errno(r, "Failed to initialize label context for root '%s': %m", arg_root);
/* Prepare to emit audit events, but only if we're operating on the host system. */
if (!arg_root)
c.audit_fd = open_audit_fd_or_warn();
/* If command line arguments are specified along with --replace, read all configuration files and
* insert the positional arguments at the specified place. Otherwise, if command line arguments are
* specified, execute just them, and finally, without --replace= or any positional arguments, just
* read configuration and execute it. */
if (arg_replace || strv_isempty(args))
r = read_config_files(&c, args);
else
r = parse_arguments(&c, args);
if (r < 0)
return r;
r = read_credential_lines(&c);
if (r < 0)
return r;
/* Let's tell nss-systemd not to synthesize the "root" and "nobody" entries for it, so that our
* detection whether the names or UID/GID area already used otherwise doesn't get confused. After
* all, even though nss-systemd synthesizes these users/groups, they should still appear in
* /etc/passwd and /etc/group, as the synthesizing logic is merely supposed to be fallback for cases
* where we run with a completely unpopulated /etc. */
if (setenv("SYSTEMD_NSS_BYPASS_SYNTHETIC", "1", 1) < 0)
return log_error_errno(errno, "Failed to set SYSTEMD_NSS_BYPASS_SYNTHETIC environment variable: %m");
if (!c.uid_range) {
/* Default to default range of SYSTEMD_UID_MIN..SYSTEM_UID_MAX. */
r = read_login_defs(&c.login_defs, NULL, arg_root);
if (r < 0)
return log_error_errno(r, "Failed to read %s%s: %m",
strempty(arg_root), "/etc/login.defs");
c.login_defs_need_warning = true;
/* We pick a range that very conservative: we look at compiled-in maximum and the value in
* /etc/login.defs. That way the UIDs/GIDs which we allocate will be interpreted correctly,
* even if /etc/login.defs is removed later. (The bottom bound doesn't matter much, since
* it's only used during allocation, so we use the configured value directly). */
uid_t begin = c.login_defs.system_alloc_uid_min,
end = MIN3((uid_t) SYSTEM_UID_MAX, c.login_defs.system_uid_max, c.login_defs.system_gid_max);
if (begin < end) {
r = uid_range_add(&c.uid_range, begin, end - begin + 1);
if (r < 0)
return log_oom();
}
}
r = add_implicit(&c);
if (r < 0)
return r;
if (!arg_dry_run) {
lock = take_etc_passwd_lock(arg_root);
if (lock < 0)
return log_error_errno(lock, "Failed to take /etc/passwd lock: %m");
}
r = load_user_database(&c);
if (r < 0)
return log_error_errno(r, "Failed to load user database: %m");
r = load_group_database(&c);
if (r < 0)
return log_error_errno(r, "Failed to read group database: %m");
ORDERED_HASHMAP_FOREACH(i, c.groups)
(void) process_item(&c, i);
ORDERED_HASHMAP_FOREACH(i, c.users)
(void) process_item(&c, i);
return write_files(&c);
}
DEFINE_MAIN_FUNCTION(run);