HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/home/homectl.c
Git blob: 0fcf210f9340db07221cb542aaf5fc1759a817bb
Lines 3001–4000 of 5,181
return ret < 0 ? ret : n_processed;
}
static int has_regular_user(void) {
_cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
UserDBMatch match = USERDB_MATCH_NULL;
int r;
match.disposition_mask = INDEX_TO_MASK(uint64_t, USER_REGULAR);
r = userdb_all(&match, USERDB_SUPPRESS_SHADOW, &iterator);
if (r < 0)
return log_error_errno(r, "Failed to create user enumerator: %m");
r = userdb_iterator_get(iterator, &match, /* ret= */ NULL);
if (r == -ESRCH)
return false;
if (r < 0)
return log_error_errno(r, "Failed to enumerate users: %m");
return true;
}
static int username_is_ok(const char *name, void *userdata) {
int r;
assert(name);
if (!valid_user_group_name(name, /* flags= */ 0)) {
log_notice("Specified user name is not a valid UNIX user name, try again: %s", name);
return false;
}
r = userdb_by_name(name, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, /* ret= */ NULL);
if (r == -ESRCH)
return true;
if (r < 0)
return log_error_errno(r, "Failed to check if specified user '%s' already exists: %m", name);
log_notice("Specified user '%s' exists already, try again.", name);
return false;
}
static void end_marker(void) {
printf("\n%sExiting user account creation tool.%s\n\n", ansi_grey(), ansi_normal());
fflush(stdout);
}
static int create_interactively(void) {
_cleanup_free_ char *username = NULL;
int r;
if (!arg_prompt_new_user) {
log_debug("Prompting for user creation was not requested.");
return 0;
}
/* Needs to be called before mute_console or it will garble the screen */
(void) plymouth_hide_splash();
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *mute_console_link = NULL;
if (arg_mute_console)
(void) mute_console(&mute_console_link);
(void) terminal_reset_defensive_locked(STDOUT_FILENO, /* flags= */ 0);
if (arg_chrome)
chrome_show("Create a User Account", /* bottom= */ NULL);
DEFER_VOID_CALL(end_marker);
DEFER_VOID_CALL(chrome_hide);
if (emoji_enabled()) {
fputs(glyph(GLYPH_HOME), stdout);
putchar(' ');
}
printf("Please create your user account!\n\n");
r = prompt_loop("Please enter user name to create",
GLYPH_IDCARD,
/* prefill= */ NULL,
/* menu= */ NULL,
/* accepted= */ NULL,
/* ellipsize_percentage= */ 60,
/* n_columns= */ 3,
/* column_width= */ 20,
username_is_ok,
/* refresh= */ NULL,
/* userdata= */ NULL,
PROMPT_MAY_SKIP|PROMPT_SILENT_VALIDATE,
&username);
if (r < 0)
return r;
if (isempty(username))
return 0;
r = sd_json_variant_set_field_string(&arg_identity_extra, "userName", username);
if (r < 0)
return log_error_errno(r, "Failed to set userName field: %m");
/* Let's not insist on a strong password in the firstboot interactive interface. Insisting on this is
* really annoying, as the user cannot just invoke the tool again with "--enforce-password-policy=no"
* because after all the tool is called from the boot process, and not from an interactive
* shell. Moreover, when setting up an initial system we can assume the user owns it, and hence we
* don't need to hard enforce some policy on password strength some organization or OS vendor
* requires. Note that this just disables the *strict* enforcement of the password policy. Even with
* this disabled we'll still tell the user in the UI that the password is too weak and suggest better
* ones, even if we then accept the weak ones if the user insists, by repeating it. */
r = sd_json_variant_set_field_boolean(&arg_identity_extra, "enforcePasswordPolicy", false);
if (r < 0)
return log_error_errno(r, "Failed to set enforcePasswordPolicy field: %m");
if (arg_prompt_groups) {
_cleanup_strv_free_ char **groups = NULL;
putchar('\n');
r = prompt_groups(username, &groups);
if (r < 0)
return r;
if (!strv_isempty(groups)) {
r = sd_json_variant_set_field_strv(&arg_identity_extra, "memberOf", groups);
if (r < 0)
return log_error_errno(r, "Failed to set memberOf field: %m");
}
}
if (arg_prompt_shell) {
_cleanup_free_ char *shell = NULL;
putchar('\n');
r = prompt_shell(username, &shell);
if (r < 0)
return r;
if (!isempty(shell)) {
log_info("Selected %s as the shell for user %s", shell, username);
r = sd_json_variant_set_field_string(&arg_identity_extra, "shell", shell);
if (r < 0)
return log_error_errno(r, "Failed to set shell field: %m");
}
}
putchar('\n');
r = create_home_common(/* input= */ NULL, /* show_enforce_password_policy_hint= */ false);
if (r < 0)
return r;
log_info("Successfully created account '%s'.", username);
return 0;
}
VERB_NOARG(verb_firstboot, "firstboot",
"Run first-boot home area creation wizard");
static int verb_firstboot(int argc, char *argv[], uintptr_t _data, void *userdata) {
int r;
/* Let's honour the systemd.firstboot kernel command line option, just like the systemd-firstboot
* tool. */
FirstBootMode mode;
_cleanup_free_ char *bad = NULL;
r = firstboot_mode_from_cmdline(&mode, &bad);
if (r < 0)
return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument%s: %m",
bad ? strjoina(" (invalid value '", bad, "')") : "");
if (IN_SET(mode, FIRSTBOOT_NO, FIRSTBOOT_HEADLESS)) {
log_debug("Found systemd.firstboot=%s kernel command line argument, turning off all prompts.",
firstboot_mode_to_string(mode));
arg_prompt_new_user = false;
}
int ret = 0;
RET_GATHER(ret, add_signing_keys_from_credentials());
r = create_or_register_from_credentials();
RET_GATHER(ret, r);
bool existing_users = r > 0;
r = getenv_bool("SYSTEMD_HOME_FIRSTBOOT_OVERRIDE");
if (r == 0)
return 0;
if (r < 0) {
if (r != -ENXIO)
log_warning_errno(r, "Failed to parse $SYSTEMD_HOME_FIRSTBOOT_OVERRIDE, ignoring: %m");
if (!existing_users) {
r = has_regular_user();
if (r < 0)
return r;
existing_users = r > 0;
}
if (existing_users) {
log_info("Regular user already present in user database, skipping interactive user creation.");
return 0;
}
}
RET_GATHER(ret, create_interactively());
return ret;
}
#define drop_from_identity(...) _drop_from_identity(STRV_MAKE(__VA_ARGS__))
static int _drop_from_identity(char **fields) {
int r;
/* If we are called to update an identity record and drop some field, let's keep track of what to
* remove from the old record */
r = strv_extend_strv(&arg_identity_filter, fields, /* filter_duplicates= */ true);
if (r < 0)
return log_oom();
/* Let's also drop the field if it was previously set to a new value on the same command line */
r = sd_json_variant_filter(&arg_identity_extra, fields);
if (r < 0)
return log_error_errno(r, "Failed to filter JSON identity data: %m");
r = sd_json_variant_filter(&arg_identity_extra_this_machine, fields);
if (r < 0)
return log_error_errno(r, "Failed to filter JSON identity data: %m");
r = sd_json_variant_filter(&arg_identity_extra_privileged, fields);
if (r < 0)
return log_error_errno(r, "Failed to filter JSON identity data: %m");
return 0;
}
static int parse_ssh_authorized_keys(sd_json_variant **identity, const char *field, const char *arg) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
_cleanup_strv_free_ char **l = NULL, **add = NULL;
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
if (arg[0] == '@') {
/* If prefixed with '@', read from a file */
_cleanup_fclose_ FILE *f = fopen(arg + 1, "re");
if (!f)
return log_error_errno(errno, "Failed to open '%s': %m", arg + 1);
for (;;) {
_cleanup_free_ char *line = NULL;
r = read_line(f, LONG_LINE_MAX, &line);
if (r < 0)
return log_error_errno(r, "Failed to read from '%s': %m", arg + 1);
if (r == 0)
break;
if (isempty(line) || line[0] == '#')
continue;
r = strv_consume(&add, TAKE_PTR(line));
if (r < 0)
return log_oom();
}
} else {
/* Otherwise, assume it's a literal key. Let's do some superficial checks
* before accepting it though. */
if (string_has_cc(arg, NULL))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Authorized key contains control characters, refusing.");
if (arg[0] == '#')
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified key is a comment?");
add = strv_new(arg);
if (!add)
return log_oom();
}
v = sd_json_variant_ref(sd_json_variant_by_key(*identity, field));
if (v) {
r = sd_json_variant_strv(v, &l);
if (r < 0)
return log_error_errno(r, "Failed to parse %s list: %m", field);
}
r = strv_extend_strv_consume(&l, TAKE_PTR(add), /* filter_duplicates= */ true);
if (r < 0)
return log_oom();
v = sd_json_variant_unref(v);
r = sd_json_variant_new_array_strv(&v, l);
if (r < 0)
return log_oom();
r = sd_json_variant_set_field(identity, field, v);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_string_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
r = sd_json_variant_set_field_string(identity, field, arg);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_home_directory_field(sd_json_variant **identity, const char *field, const char *arg) {
_cleanup_free_ char *hd = NULL;
int r;
assert(identity);
assert(field);
if (!isempty(arg)) {
r = parse_path_argument(arg, /* suppress_root= */ false, &hd);
if (r < 0)
return r;
if (!valid_home(hd))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Home directory '%s' not valid.", hd);
}
return parse_string_field(identity, field, hd);
}
static int parse_realm_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (!isempty(arg)) {
r = dns_name_is_valid(arg);
if (r < 0)
return log_error_errno(r, "Failed to determine whether realm '%s' is a valid DNS domain: %m", arg);
if (r == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Realm '%s' is not a valid DNS domain.", arg);
}
return parse_string_field(identity, field, arg);
}
static int parse_path_field(sd_json_variant **identity, const char *field, const char *arg) {
_cleanup_free_ char *v = NULL;
int r;
assert(identity);
assert(field);
if (!isempty(arg)) {
r = parse_path_argument(arg, /* suppress_root= */ false, &v);
if (r < 0)
return r;
}
return parse_string_field(identity, field, v);
}
static int parse_filename_field(sd_json_variant **identity, const char *field, const char *arg) {
assert(identity);
assert(field);
if (!isempty(arg) && !filename_is_valid(arg))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Parameter for %s field not a valid filename: %s", field, arg);
return parse_string_field(identity, field, arg);
}
static int parse_unsigned_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
unsigned n;
r = safe_atou(arg, &n);
if (r < 0)
return log_error_errno(r, "Failed to parse %s parameter: %s", field, arg);
r = sd_json_variant_set_field_unsigned(identity, field, n);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_u64_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
uint64_t n;
r = safe_atou64(arg, &n);
if (r < 0)
return log_error_errno(r, "Failed to parse %s parameter: %s", field, arg);
r = sd_json_variant_set_field_unsigned(identity, field, n);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_size_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
uint64_t n;
r = parse_size(arg, 1024, &n);
if (r < 0)
return log_error_errno(r, "Failed to parse %s parameter: %s", field, arg);
r = sd_json_variant_set_field_unsigned(identity, field, n);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_boolean_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
r = parse_boolean(arg);
if (r < 0)
return log_error_errno(r, "Failed to parse boolean parameter %s: %s", field, arg);
r = sd_json_variant_set_field_boolean(identity, field, r > 0);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_mode_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
mode_t mode;
r = parse_mode(arg, &mode);
if (r < 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Access mode '%s' not valid.", arg);
r = sd_json_variant_set_field_unsigned(identity, field, mode);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_timestamp_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
usec_t n;
r = parse_timestamp(arg, &n);
if (r < 0)
return log_error_errno(r, "Failed to parse %s parameter: %s", field, arg);
r = sd_json_variant_set_field_unsigned(identity, field, n);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_time_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
usec_t n;
r = parse_sec(arg, &n);
if (r < 0)
return log_error_errno(r, "Failed to parse %s parameter: %s", field, arg);
r = sd_json_variant_set_field_unsigned(identity, field, n);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_uid_field(sd_json_variant **identity, const char *field, const char *arg) {
uid_t uid;
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
r = parse_uid(arg, &uid);
if (r < 0)
return log_error_errno(r, "Failed to parse UID '%s'.", arg);
const char *bad_range =
uid_is_system(uid) ? "in system range" :
uid_is_greeter(uid) ? "in greeter range" :
uid_is_dynamic(uid) ? "in dynamic ragne" :
uid == UID_NOBODY ? "nobody UID" : NULL;
if (bad_range)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "UID "UID_FMT" is %s, refusing.", uid, bad_range);
r = sd_json_variant_set_field_unsigned(identity, field, uid);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_nice_field(sd_json_variant **identity, const char *field, const char *arg) {
int nc, r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
r = parse_nice(arg, &nc);
if (r < 0)
return log_error_errno(r, "Failed to parse nice level '%s': %m", arg);
r = sd_json_variant_set_field_integer(identity, field, nc);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_auto_resize_mode_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (!isempty(arg)) {
r = auto_resize_mode_from_string(arg);
if (r < 0)
return log_error_errno(r, "Failed to parse %s parameter: %s", field, arg);
arg = auto_resize_mode_to_string(r);
}
return parse_string_field(identity, field, arg);
}
static int parse_rebalance_weight(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
uint64_t u;
if (streq(arg, "off"))
u = REBALANCE_WEIGHT_OFF;
else {
r = safe_atou64(arg, &u);
if (r < 0)
return log_error_errno(r, "Failed to parse rebalance weight parameter: %s", arg);
if (u < REBALANCE_WEIGHT_MIN || u > REBALANCE_WEIGHT_MAX)
return log_error_errno(SYNTHETIC_ERRNO(ERANGE),
"Rebalancing weight out of valid range %" PRIu64 "%s%" PRIu64 ": %s",
REBALANCE_WEIGHT_MIN, glyph(GLYPH_ELLIPSIS), REBALANCE_WEIGHT_MAX,
arg);
}
/* Drop from per machine stuff and everywhere */
r = drop_from_identity(field);
if (r < 0)
return r;
r = sd_json_variant_set_field_unsigned(identity, field, u);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_rlimit_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg)) {
/* Remove all resource limits */
r = drop_from_identity(field);
if (r < 0)
return r;
arg_identity_filter_rlimits = strv_free(arg_identity_filter_rlimits);
*identity = sd_json_variant_unref(*identity);
return 0;
}
const char *eq = strchr(arg, '=');
if (!eq)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Can't parse resource limit assignment: %s", arg);
_cleanup_free_ char *s = strndup(arg, eq - arg);
if (!s)
return log_oom();
int limit = rlimit_from_string_harder(s);
if (limit < 0)
return log_error_errno(limit, "Unknown resource limit type: %s", s);
const char *rlimit_field = strjoina("RLIMIT_", rlimit_to_string(limit));
if (isempty(eq + 1)) {
/* Remove only the specific rlimit */
r = strv_extend(&arg_identity_filter_rlimits, rlimit_field);
if (r < 0)
return r;
r = sd_json_variant_filter(identity, STRV_MAKE(rlimit_field));
if (r < 0)
return log_error_errno(r, "Failed to filter JSON identity data: %m");
return 0;
}
_cleanup_(sd_json_variant_unrefp) sd_json_variant *jcur = NULL, *jmax = NULL;
struct rlimit rl;
r = rlimit_parse(limit, eq + 1, &rl);
if (r < 0)
return log_error_errno(r, "Failed to parse resource limit value: %s", eq + 1);
r = rl.rlim_cur == RLIM_INFINITY ? sd_json_variant_new_null(&jcur) : sd_json_variant_new_unsigned(&jcur, rl.rlim_cur);
if (r < 0)
return log_error_errno(r, "Failed to allocate json variant: %m");
r = rl.rlim_max == RLIM_INFINITY ? sd_json_variant_new_null(&jmax) : sd_json_variant_new_unsigned(&jmax, rl.rlim_max);
if (r < 0)
return log_error_errno(r, "Failed to allocate json variant: %m");
r = sd_json_variant_set_fieldbo(identity, rlimit_field,
SD_JSON_BUILD_PAIR_VARIANT("cur", jcur),
SD_JSON_BUILD_PAIR_VARIANT("max", jmax));
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", rlimit_field);
return 0;
}
static int parse_disk_size_field(sd_json_variant **identity, const char *arg) {
int r;
assert(identity);
if (isempty(arg)) {
r = drop_from_identity("diskSize", "diskSizeRelative", "rebalanceWeight");
if (r < 0)
return r;
arg_disk_size = arg_disk_size_relative = UINT64_MAX;
return 0;
}
r = parse_permyriad(arg);
if (r < 0) {
r = parse_disk_size(arg, &arg_disk_size);
if (r < 0)
return r;
r = drop_from_identity("diskSizeRelative");
if (r < 0)
return r;
r = sd_json_variant_set_field_unsigned(identity, "diskSize", arg_disk_size);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", "diskSize");
arg_disk_size_relative = UINT64_MAX;
} else {
/* Normalize to UINT32_MAX == 100% */
arg_disk_size_relative = UINT32_SCALE_FROM_PERMYRIAD(r);
r = drop_from_identity("diskSize");
if (r < 0)
return r;
r = sd_json_variant_set_field_unsigned(identity, "diskSizeRelative", arg_disk_size_relative);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", "diskSizeRelative");
arg_disk_size = UINT64_MAX;
}
/* Automatically turn off the rebalance logic if user configured a size explicitly */
r = sd_json_variant_set_field_unsigned(identity, "rebalanceWeight", REBALANCE_WEIGHT_OFF);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", "rebalanceWeight");
return 0;
}
static int parse_sector_size_field(sd_json_variant **identity, const char *field, const char *arg) {
uint64_t ss;
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
r = parse_sector_size(arg, &ss);
if (r < 0)
return r;
r = sd_json_variant_set_field_unsigned(identity, field, ss);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_weight_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
uint64_t u;
r = safe_atou64(arg, &u);
if (r < 0)
return log_error_errno(r, "Failed to parse %s parameter: %s", field, arg);
if (!CGROUP_WEIGHT_IS_OK(u))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Weight %" PRIu64 " is out of valid range for field %s.", u, field);
r = sd_json_variant_set_field_unsigned(identity, field, u);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_environment_field(sd_json_variant **identity, const char *field, const char *arg) {
_cleanup_strv_free_ char **l = NULL;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *ne = NULL;
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
sd_json_variant *e = sd_json_variant_by_key(*identity, field);
if (e) {
r = sd_json_variant_strv(e, &l);
if (r < 0)
return log_error_errno(r, "Failed to parse JSON environment field: %m");
}
r = strv_env_replace_strdup_passthrough(&l, arg);
if (r < 0)
return log_error_errno(r, "Cannot assign environment variable %s: %m", arg);
strv_sort(l);
r = sd_json_variant_new_array_strv(&ne, l);
if (r < 0)
return log_error_errno(r, "Failed to allocate json list: %m");
r = sd_json_variant_set_field(identity, field, ne);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_language_field(char ***languages, const char *arg) {
int r;
assert(languages);
if (isempty(arg)) {
r = drop_from_identity("preferredLanguage", "additionalLanguages");
if (r < 0)
return r;
strv_freep(languages);
return 0;
}
for (const char *p = arg;;) {
_cleanup_free_ char *word = NULL;
r = extract_first_word(&p, &word, ",:", /* flags= */ 0);
if (r < 0)
return log_error_errno(r, "Failed to parse locale list: %m");
if (r == 0)
return 0;
if (!locale_is_valid(word))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale '%s' is not valid.", word);
if (locale_is_installed(word) <= 0)
log_warning("Locale '%s' is not installed, accepting anyway.", word);
r = strv_consume(languages, TAKE_PTR(word));
if (r < 0)
return log_oom();
strv_uniq(*languages);
}
}
static int parse_group_field(
sd_json_variant **identity,
const char *field,
const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
for (const char *p = arg;;) {
_cleanup_free_ char *word = NULL;
_cleanup_strv_free_ char **list = NULL;
r = extract_first_word(&p, &word, ",", /* flags= */ 0);
if (r < 0)
return log_error_errno(r, "Failed to parse group list: %m");
if (r == 0)
return 0;
if (!valid_user_group_name(word, /* flags= */ 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid group name %s.", word);
_cleanup_(sd_json_variant_unrefp) sd_json_variant *mo =
sd_json_variant_ref(sd_json_variant_by_key(*identity, field));
r = sd_json_variant_strv(mo, &list);
if (r < 0)
return log_error_errno(r, "Failed to parse group list: %m");
r = strv_extend(&list, word);
if (r < 0)
return log_oom();
strv_sort_uniq(list);
mo = sd_json_variant_unref(mo);
r = sd_json_variant_new_array_strv(&mo, list);
if (r < 0)
return log_error_errno(r, "Failed to allocate json list: %m");
r = sd_json_variant_set_field(identity, field, mo);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
}
}
static int parse_capability_set_field(
sd_json_variant **identity,
uint64_t *capability_set,
const char *field,
const char *arg) {
_cleanup_strv_free_ char **l = NULL;
int r;
assert(identity);
assert(capability_set);
assert(field);
assert(arg);
r = parse_capability_set(arg, CAP_MASK_UNSET, capability_set);
if (r == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid capabilities in capability string '%s'.", arg);
if (r < 0)
return log_error_errno(r, "Failed to parse capability string '%s': %m", arg);
if (*capability_set == CAP_MASK_UNSET)
return drop_from_identity(field);
if (capability_set_to_strv(*capability_set, &l) < 0)
return log_oom();
r = sd_json_variant_set_field_strv(identity, field, l);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_tmpfs_limit_field(
sd_json_variant **identity,
const char *field,
const char *field_scale,
const char *arg) {
int r;
assert(identity);
assert(field);
assert(field_scale);
if (isempty(arg))
return drop_from_identity(field, field_scale);
r = parse_permyriad(arg);
if (r < 0) {
uint64_t u;
r = parse_size(arg, 1024, &u);
if (r < 0)
return log_error_errno(r, "Failed to parse %s/%s parameter: %s", field, field_scale, arg);
r = sd_json_variant_set_field_unsigned(identity, field, u);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return drop_from_identity(field_scale);
}
r = sd_json_variant_set_field_unsigned(identity, field_scale, UINT32_SCALE_FROM_PERMYRIAD(r));
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field_scale);
return drop_from_identity(field);
}
static int parse_pkcs11_token_uri_field(const char *arg) {
int r;
assert(arg);
if (streq(arg, "list"))
return pkcs11_list_tokens();
/* If --pkcs11-token-uri= is specified we always drop everything old */
r = drop_from_identity("pkcs11TokenUri", "pkcs11EncryptedKey");
if (r < 0)
return r;
if (isempty(arg)) {
arg_pkcs11_token_uri = strv_free(arg_pkcs11_token_uri);
return 1;
}
if (streq(arg, "auto")) {
char *found;
r = pkcs11_find_token_auto(&found);
if (r < 0)
return r;
r = strv_consume(&arg_pkcs11_token_uri, found);
} else {