HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/home/homectl.c
Git blob: 0fcf210f9340db07221cb542aaf5fc1759a817bb
Lines 1001–2000 of 5,181
}
return json_variant_unref_and_replace(*_v, v);
}
static int add_disposition(sd_json_variant **v) {
int r;
assert(v);
if (sd_json_variant_by_key(*v, "disposition"))
return 0;
/* Set the disposition to regular, if not configured explicitly */
r = sd_json_variant_set_field_string(v, "disposition", "regular");
if (r < 0)
return log_error_errno(r, "Failed to set disposition field: %m");
return 1;
}
static int acquire_new_home_record(sd_json_variant *input, UserRecord **ret, char **ret_recovery_key) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
_cleanup_(erase_and_freep) char *recovery_key = NULL;
_cleanup_(user_record_unrefp) UserRecord *hr = NULL;
int r;
assert(ret);
assert(ret_recovery_key);
if (arg_identity) {
unsigned line = 0, column = 0;
if (input)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Two identity records specified, refusing.");
r = sd_json_parse_file(
streq(arg_identity, "-") ? stdin : NULL,
streq(arg_identity, "-") ? "<stdin>" : arg_identity,
SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_SENSITIVE,
&v,
&line,
&column);
if (r < 0)
return log_error_errno(r, "Failed to parse identity at %u:%u: %m", line, column);
} else
v = sd_json_variant_ref(input);
r = apply_identity_changes(&v);
if (r < 0)
return r;
r = add_disposition(&v);
if (r < 0)
return r;
STRV_FOREACH(i, arg_pkcs11_token_uri) {
r = identity_add_pkcs11_key_data(&v, *i);
if (r < 0)
return r;
}
STRV_FOREACH(i, arg_fido2_device) {
r = identity_add_fido2_parameters(&v, *i, arg_fido2_lock_with, arg_fido2_cred_alg);
if (r < 0)
return r;
}
if (arg_recovery_key) {
r = identity_add_recovery_key(&v, &recovery_key);
if (r < 0)
return r;
}
r = update_last_change(&v, true, false);
if (r < 0)
return r;
if (DEBUG_LOGGING)
sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY, NULL, NULL);
hr = user_record_new();
if (!hr)
return log_oom();
r = user_record_load(
hr,
v,
USER_RECORD_REQUIRE_REGULAR|
USER_RECORD_ALLOW_SECRET|
USER_RECORD_ALLOW_PRIVILEGED|
USER_RECORD_ALLOW_PER_MACHINE|
USER_RECORD_STRIP_BINDING|
USER_RECORD_STRIP_STATUS|
(arg_seize ? USER_RECORD_STRIP_SIGNATURE : USER_RECORD_ALLOW_SIGNATURE) |
USER_RECORD_LOG|
USER_RECORD_PERMISSIVE);
if (r < 0)
return r;
*ret = TAKE_PTR(hr);
*ret_recovery_key = TAKE_PTR(recovery_key);
return 0;
}
static int acquire_new_password(
const char *user_name,
UserRecord *hr,
bool suggest,
char **ret) {
_cleanup_(erase_and_freep) char *envpw = NULL;
unsigned i = 5;
int r;
assert(user_name);
assert(hr);
r = getenv_steal_erase("NEWPASSWORD", &envpw);
if (r < 0)
return log_error_errno(r, "Failed to acquire password from environment: %m");
if (r > 0) {
/* As above, this is not for use, just for testing */
r = user_record_set_password(hr, STRV_MAKE(envpw), /* prepend= */ true);
if (r < 0)
return log_error_errno(r, "Failed to store password: %m");
if (ret)
*ret = TAKE_PTR(envpw);
return 0;
}
if (suggest)
(void) suggest_passwords();
for (;;) {
_cleanup_strv_free_erase_ char **first = NULL, **second = NULL;
_cleanup_free_ char *question = NULL;
if (--i == 0)
return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), "Too many attempts, giving up.");
if (asprintf(&question, "Please enter new password for user %s:", user_name) < 0)
return log_oom();
AskPasswordRequest req = {
.tty_fd = -EBADF,
.message = question,
.icon = "user-home",
.keyring = "home-password",
.credential = "home.new-password",
.until = USEC_INFINITY,
.hup_fd = -EBADF,
};
r = ask_password_auto(
&req,
/* flags= */ 0, /* no caching, we want to collect a new password here after all */
&first);
if (r < 0)
return log_error_errno(r, "Failed to acquire password: %m");
assert(!strv_isempty(first));
question = mfree(question);
if (asprintf(&question, "Please enter new password for user %s (repeat):", user_name) < 0)
return log_oom();
req.message = question;
r = ask_password_auto(
&req,
/* flags= */ 0, /* no caching */
&second);
if (r < 0)
return log_error_errno(r, "Failed to acquire password: %m");
if (strv_equal(first, second)) {
_cleanup_(erase_and_freep) char *copy = NULL;
if (ret) {
copy = strdup(first[0]);
if (!copy)
return log_oom();
}
r = user_record_set_password(hr, first, /* prepend= */ true);
if (r < 0)
return log_error_errno(r, "Failed to store password: %m");
if (ret)
*ret = TAKE_PTR(copy);
return 0;
}
log_error("Password didn't match, try again.");
}
}
static int acquire_merged_blob_dir(UserRecord *hr, bool existing, Hashmap **ret) {
_cleanup_free_ char *sys_blob_path = NULL;
_cleanup_hashmap_free_ Hashmap *blobs = NULL;
_cleanup_closedir_ DIR *d = NULL;
const char *src_blob_path, *filename;
void *fd_ptr;
int r;
assert(ret);
HASHMAP_FOREACH_KEY(fd_ptr, filename, arg_blob_files) {
_cleanup_free_ char *filename_dup = NULL;
_cleanup_close_ int fd_dup = -EBADF;
filename_dup = strdup(filename);
if (!filename_dup)
return log_oom();
if (PTR_TO_FD(fd_ptr) != -EBADF) {
fd_dup = fcntl(PTR_TO_FD(fd_ptr), F_DUPFD_CLOEXEC, 3);
if (fd_dup < 0)
return log_error_errno(errno, "Failed to duplicate fd of %s: %m", filename);
}
r = hashmap_ensure_put(&blobs, &blob_fd_hash_ops, filename_dup, FD_TO_PTR(fd_dup));
if (r < 0)
return r;
TAKE_PTR(filename_dup); /* Ownership transferred to hashmap */
TAKE_FD(fd_dup);
}
if (arg_blob_dir)
src_blob_path = arg_blob_dir;
else if (existing && !arg_blob_clear) {
if (hr->blob_directory)
src_blob_path = hr->blob_directory;
else {
/* This isn't technically a correct thing to do for generic user records,
* so anyone looking at this code for reference shouldn't replicate it.
* However, since homectl is tied to homed, this is OK. This adds robustness
* for situations where the user record is coming directly from the CLI and
* thus doesn't have a blobDirectory set */
sys_blob_path = path_join(home_system_blob_dir(), hr->user_name);
if (!sys_blob_path)
return log_oom();
src_blob_path = sys_blob_path;
}
} else
goto nodir; /* Shortcut: no dir to merge with, so just return copy of arg_blob_files */
d = opendir(src_blob_path);
if (!d)
return log_error_errno(errno, "Failed to open %s: %m", src_blob_path);
FOREACH_DIRENT_ALL(de, d, return log_error_errno(errno, "Failed to read %s: %m", src_blob_path)) {
_cleanup_free_ char *name = NULL;
_cleanup_close_ int fd = -EBADF;
if (dot_or_dot_dot(de->d_name))
continue;
if (hashmap_contains(blobs, de->d_name))
continue; /* arg_blob_files should override the base dir */
if (!suitable_blob_filename(de->d_name)) {
log_warning("File %s in blob directory %s has an invalid filename. Skipping.", de->d_name, src_blob_path);
continue;
}
name = strdup(de->d_name);
if (!name)
return log_oom();
fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return log_error_errno(errno, "Failed to open %s in %s: %m", de->d_name, src_blob_path);
r = fd_verify_regular(fd);
if (r < 0) {
log_warning_errno(r, "Entry %s in blob directory %s is not a regular file. Skipping.", de->d_name, src_blob_path);
continue;
}
r = hashmap_ensure_put(&blobs, &blob_fd_hash_ops, name, FD_TO_PTR(fd));
if (r < 0)
return r;
TAKE_PTR(name); /* Ownership transferred to hashmap */
TAKE_FD(fd);
}
nodir:
*ret = TAKE_PTR(blobs);
return 0;
}
static int bus_message_append_blobs(sd_bus_message *m, Hashmap *blobs) {
const char *filename;
void *fd_ptr;
int r;
assert(m);
r = sd_bus_message_open_container(m, 'a', "{sh}");
if (r < 0)
return r;
HASHMAP_FOREACH_KEY(fd_ptr, filename, blobs) {
int fd = PTR_TO_FD(fd_ptr);
if (fd == -EBADF) /* File marked for deletion */
continue;
r = sd_bus_message_append(m, "{sh}", filename, fd);
if (r < 0)
return r;
}
return sd_bus_message_close_container(m);
}
static void finalize_recovery_key(const char *recovery_key, const char *recovery_key_file) {
int r;
if (!recovery_key)
return;
if (recovery_key_file) {
r = recovery_key_file_write(recovery_key_file, recovery_key);
if (r < 0)
log_warning_errno(r,
"Failed to write recovery key file '%s', ignoring: %m",
recovery_key_file);
}
show_recovery_key(recovery_key);
}
static int create_home_common(sd_json_variant *input, bool show_enforce_password_policy_hint) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(user_record_unrefp) UserRecord *hr = NULL;
_cleanup_hashmap_free_ Hashmap *blobs = NULL;
_cleanup_(erase_and_freep) char *recovery_key = NULL;
int r;
r = acquire_new_home_record(input, &hr, &recovery_key);
if (r < 0)
return r;
r = acquire_merged_blob_dir(hr, false, &blobs);
if (r < 0)
return r;
/* If the JSON record carries no plain text password (besides the recovery key), then let's query it
* manually. */
if (strv_length(hr->password) <= arg_recovery_key) {
if (strv_isempty(hr->hashed_password)) {
_cleanup_(erase_and_freep) char *new_password = NULL;
/* No regular (i.e. non-PKCS#11) hashed passwords set in the record, let's fix that. */
r = acquire_new_password(hr->user_name, hr, /* suggest= */ true, &new_password);
if (r < 0)
return r;
r = user_record_make_hashed_password(hr, STRV_MAKE(new_password), /* extend= */ false);
if (r < 0)
return log_error_errno(r, "Failed to hash password: %m");
} else {
/* There's a hash password set in the record, acquire the unhashed version of it. */
r = acquire_existing_password(
hr->user_name,
hr,
/* emphasize_current_password= */ false,
ASK_PASSWORD_ACCEPT_CACHED | ASK_PASSWORD_PUSH_CACHE);
if (r < 0)
return r;
}
}
if (hr->enforce_password_policy == 0) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
/* If password quality enforcement is disabled, let's at least warn client side */
r = user_record_check_password_quality(hr, hr, &error);
if (r < 0)
log_warning_errno(r, "Specified password does not pass quality checks (%s), proceeding anyway.", bus_error_message(&error, r));
}
if (arg_dry_run) {
sd_json_variant_dump(hr->json, SD_JSON_FORMAT_COLOR_AUTO|SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_NEWLINE, stderr, /* prefix= */ NULL);
finalize_recovery_key(recovery_key, /* recovery_key_file= */ NULL);
return 0;
}
r = acquire_bus(&bus);
if (r < 0)
return r;
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
for (;;) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
_cleanup_(erase_and_freep) char *formatted = NULL;
r = sd_json_variant_format(hr->json, /* flags= */ 0, &formatted);
if (r < 0)
return log_error_errno(r, "Failed to format user record: %m");
r = bus_message_new_method_call(bus, &m, bus_mgr, "CreateHomeEx");
if (r < 0)
return bus_log_create_error(r);
(void) sd_bus_message_sensitive(m);
r = sd_bus_message_append(m, "s", formatted);
if (r < 0)
return bus_log_create_error(r);
r = bus_message_append_blobs(m, blobs);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(m, "t", UINT64_C(0));
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL);
if (r < 0) {
if (sd_bus_error_has_name(&error, BUS_ERROR_LOW_PASSWORD_QUALITY)) {
_cleanup_(erase_and_freep) char *new_password = NULL;
log_error_errno(r, "%s", bus_error_message(&error, r));
if (show_enforce_password_policy_hint)
log_info("(Use --enforce-password-policy=no to turn off password quality checks for this account.)");
r = acquire_new_password(hr->user_name, hr, /* suggest= */ false, &new_password);
if (r < 0)
return r;
r = user_record_make_hashed_password(hr, STRV_MAKE(new_password), /* extend= */ false);
if (r < 0)
return log_error_errno(r, "Failed to hash passwords: %m");
} else {
r = handle_generic_user_record_error(hr->user_name, hr, &error, r, false);
if (r < 0)
return r;
}
} else
break; /* done */
}
finalize_recovery_key(recovery_key, arg_recovery_key_file);
return 0;
}
VERB(verb_create_home, "create", "USER\0", VERB_ANY, 2, 0,
"Create a home area");
static int verb_create_home(int argc, char *argv[], uintptr_t _data, void *userdata) {
int r;
if (argc >= 2) {
/* If a username was specified, use it */
if (valid_user_group_name(argv[1], /* flags= */ 0))
r = sd_json_variant_set_field_string(&arg_identity_extra, "userName", argv[1]);
else {
_cleanup_free_ char *un = NULL, *rr = NULL;
/* Before we consider the user name invalid, let's check if we can split it? */
r = split_user_name_realm(argv[1], &un, &rr);
if (r < 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "User name '%s' is not valid.", argv[1]);
if (rr) {
r = sd_json_variant_set_field_string(&arg_identity_extra, "realm", rr);
if (r < 0)
return log_error_errno(r, "Failed to set realm field: %m");
}
r = sd_json_variant_set_field_string(&arg_identity_extra, "userName", un);
}
if (r < 0)
return log_error_errno(r, "Failed to set userName field: %m");
} else {
/* If neither a username nor an identity have been specified we cannot operate. */
if (!arg_identity)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "User name required.");
}
return create_home_common(/* input= */ NULL, /* show_enforce_password_policy_hint= */ true);
}
static int acquire_updated_home_record(
sd_bus *bus,
const char *username,
UserRecord **ret,
char **ret_recovery_key) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
_cleanup_(erase_and_freep) char *recovery_key = NULL;
_cleanup_(user_record_unrefp) UserRecord *hr = NULL;
int r;
assert(ret);
assert(ret_recovery_key);
if (arg_identity) {
unsigned line = 0, column = 0;
sd_json_variant *un;
r = sd_json_parse_file(
streq(arg_identity, "-") ? stdin : NULL,
streq(arg_identity, "-") ? "<stdin>" : arg_identity,
SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_SENSITIVE,
&json,
&line,
&column);
if (r < 0)
return log_error_errno(r, "Failed to parse identity at %u:%u: %m", line, column);
un = sd_json_variant_by_key(json, "userName");
if (un) {
if (!sd_json_variant_is_string(un) || (username && !streq(sd_json_variant_string(un), username)))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "User name specified on command line and in JSON record do not match.");
} else {
if (!username)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No username specified.");
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");
}
} else {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
int incomplete;
const char *text;
if (!identity_properties_specified())
return log_error_errno(SYNTHETIC_ERRNO(EALREADY), "No field to change specified.");
r = bus_call_method(bus, bus_mgr, "GetUserRecordByName", &error, &reply, "s", username);
if (r < 0)
return log_error_errno(r, "Failed to acquire user home record: %s", bus_error_message(&error, r));
r = sd_bus_message_read(reply, "sbo", &text, &incomplete, NULL);
if (r < 0)
return bus_log_parse_error(r);
if (incomplete)
return log_error_errno(SYNTHETIC_ERRNO(EACCES), "Lacking rights to acquire user record including privileged metadata, can't update record.");
r = sd_json_parse(
text,
SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_SENSITIVE,
&json,
/* reterr_line= */ NULL,
/* reterr_column= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse JSON identity: %m");
reply = sd_bus_message_unref(reply);
r = sd_json_variant_filter(&json, STRV_MAKE("binding", "status", "signature", "blobManifest"));
if (r < 0)
return log_error_errno(r, "Failed to strip binding and status from record to update: %m");
}
r = apply_identity_changes(&json);
if (r < 0)
return r;
STRV_FOREACH(i, arg_pkcs11_token_uri) {
r = identity_add_pkcs11_key_data(&json, *i);
if (r < 0)
return r;
}
STRV_FOREACH(i, arg_fido2_device) {
r = identity_add_fido2_parameters(&json, *i, arg_fido2_lock_with, arg_fido2_cred_alg);
if (r < 0)
return r;
}
if (arg_recovery_key) {
r = identity_add_recovery_key(&json, &recovery_key);
if (r < 0)
return r;
}
/* If the user supplied a full record, then add in lastChange, but do not override. Otherwise always
* override. */
r = update_last_change(&json, arg_pkcs11_token_uri || arg_fido2_device || arg_recovery_key, !arg_identity);
if (r < 0)
return r;
if (DEBUG_LOGGING)
sd_json_variant_dump(json, SD_JSON_FORMAT_PRETTY, NULL, NULL);
hr = user_record_new();
if (!hr)
return log_oom();
r = user_record_load(hr, json, USER_RECORD_REQUIRE_REGULAR|USER_RECORD_ALLOW_PRIVILEGED|USER_RECORD_ALLOW_PER_MACHINE|USER_RECORD_ALLOW_SECRET|USER_RECORD_ALLOW_SIGNATURE|USER_RECORD_LOG|USER_RECORD_PERMISSIVE);
if (r < 0)
return r;
*ret = TAKE_PTR(hr);
*ret_recovery_key = TAKE_PTR(recovery_key);
return 0;
}
static int home_record_reset_human_interaction_permission(UserRecord *hr) {
int r;
assert(hr);
/* When we execute multiple operations one after the other, let's reset the permission to ask the
* user each time, so that if interaction is necessary we will be told so again and thus can print a
* nice message to the user, telling the user so. */
r = user_record_set_pkcs11_protected_authentication_path_permitted(hr, -1);
if (r < 0)
return log_error_errno(r, "Failed to reset PKCS#11 protected authentication path permission flag: %m");
r = user_record_set_fido2_user_presence_permitted(hr, -1);
if (r < 0)
return log_error_errno(r, "Failed to reset FIDO2 user presence permission flag: %m");
r = user_record_set_fido2_user_verification_permitted(hr, -1);
if (r < 0)
return log_error_errno(r, "Failed to reset FIDO2 user verification permission flag: %m");
return 0;
}
VERB(verb_update_home, "update", "USER\0", VERB_ANY, 2, 0,
"Update a home area");
static int verb_update_home(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(user_record_unrefp) UserRecord *hr = NULL, *secret = NULL;
_cleanup_free_ char *buffer = NULL;
_cleanup_(erase_and_freep) char *recovery_key = NULL;
_cleanup_hashmap_free_ Hashmap *blobs = NULL;
const char *username;
uint64_t flags = 0;
int r;
if (argc >= 2)
username = argv[1];
else if (!arg_identity) {
buffer = getusername_malloc();
if (!buffer)
return log_oom();
username = buffer;
} else
username = NULL;
bool and_change_password = !strv_isempty(arg_pkcs11_token_uri) || !strv_isempty(arg_fido2_device);
bool and_resize = arg_disk_size != UINT64_MAX || arg_disk_size_relative != UINT64_MAX;
r = acquire_bus(&bus);
if (r < 0)
return r;
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
r = acquire_updated_home_record(bus, username, &hr, &recovery_key);
if (r < 0)
return r;
/* Add in all secrets we can acquire cheaply */
r = acquire_passed_secrets(username, &secret);
if (r < 0)
return r;
r = user_record_merge_secret(hr, secret);
if (r < 0)
return r;
r = acquire_merged_blob_dir(hr, true, &blobs);
if (r < 0)
return r;
if (arg_dry_run) {
sd_json_variant_dump(hr->json, SD_JSON_FORMAT_COLOR_AUTO|SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_NEWLINE, stderr, /* prefix= */ NULL);
finalize_recovery_key(recovery_key, /* recovery_key_file= */ NULL);
return 0;
}
/* If we do multiple operations, let's output things more verbosely, since otherwise the repeated
* authentication might be confusing. */
if (and_resize || and_change_password)
log_info("Updating home directory.");
if (arg_offline)
flags |= SD_HOMED_UPDATE_OFFLINE;
for (;;) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
_cleanup_free_ char *formatted = NULL;
r = bus_message_new_method_call(bus, &m, bus_mgr, "UpdateHomeEx");
if (r < 0)
return bus_log_create_error(r);
r = sd_json_variant_format(hr->json, /* flags= */ 0, &formatted);
if (r < 0)
return log_error_errno(r, "Failed to format user record: %m");
(void) sd_bus_message_sensitive(m);
r = sd_bus_message_append(m, "s", formatted);
if (r < 0)
return bus_log_create_error(r);
r = bus_message_append_blobs(m, blobs);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(m, "t", flags);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL);
if (r < 0) {
if (and_change_password &&
sd_bus_error_has_name(&error, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN))
/* In the generic handler we'd ask for a password in this case, but when
* changing passwords that's not sufficient, as we need to acquire all keys
* first. */
return log_error_errno(r, "Security token not inserted, refusing.");
r = handle_generic_user_record_error(hr->user_name, hr, &error, r, false);
if (r < 0)
return r;
} else
break;
}
finalize_recovery_key(recovery_key, arg_recovery_key_file);
if (and_resize)
log_info("Resizing home.");
(void) home_record_reset_human_interaction_permission(hr);
/* Also sync down disk size to underlying LUKS/fscrypt/quota */
while (and_resize) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
r = bus_message_new_method_call(bus, &m, bus_mgr, "ResizeHome");
if (r < 0)
return bus_log_create_error(r);
/* Specify UINT64_MAX as size, in which case the underlying disk size will just be synced */
r = sd_bus_message_append(m, "st", hr->user_name, UINT64_MAX);
if (r < 0)
return bus_log_create_error(r);
r = bus_message_append_secret(m, hr);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL);
if (r < 0) {
if (and_change_password &&
sd_bus_error_has_name(&error, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN))
return log_error_errno(r, "Security token not inserted, refusing.");
r = handle_generic_user_record_error(hr->user_name, hr, &error, r, false);
if (r < 0)
return r;
} else
break;
}
if (and_change_password)
log_info("Synchronizing passwords and encryption keys.");
(void) home_record_reset_human_interaction_permission(hr);
/* Also sync down passwords to underlying LUKS/fscrypt */
while (and_change_password) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
r = bus_message_new_method_call(bus, &m, bus_mgr, "ChangePasswordHome");
if (r < 0)
return bus_log_create_error(r);
/* Specify an empty new secret, in which case the underlying LUKS/fscrypt password will just be synced */
r = sd_bus_message_append(m, "ss", hr->user_name, "{}");
if (r < 0)
return bus_log_create_error(r);
r = bus_message_append_secret(m, hr);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL);
if (r < 0) {
if (sd_bus_error_has_name(&error, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN))
return log_error_errno(r, "Security token not inserted, refusing.");
r = handle_generic_user_record_error(hr->user_name, hr, &error, r, false);
if (r < 0)
return r;
} else
break;
}
return 0;
}
VERB(verb_passwd_home, "passwd", "USER\0", VERB_ANY, 2, 0,
"Change password of a home area");
static int verb_passwd_home(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(user_record_unrefp) UserRecord *old_secret = NULL, *new_secret = NULL;
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_free_ char *buffer = NULL;
const char *username;
int r;
if (arg_pkcs11_token_uri)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"To change the PKCS#11 security token use 'homectl update --pkcs11-token-uri=%s'.",
glyph(GLYPH_ELLIPSIS));
if (arg_fido2_device)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"To change the FIDO2 security token use 'homectl update --fido2-device=%s'.",
glyph(GLYPH_ELLIPSIS));
if (identity_properties_specified())
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The 'passwd' verb does not permit changing other record properties at the same time.");
if (argc >= 2)
username = argv[1];
else {
buffer = getusername_malloc();
if (!buffer)
return log_oom();
username = buffer;
}
r = acquire_bus(&bus);
if (r < 0)
return r;
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
r = acquire_passed_secrets(username, &old_secret);
if (r < 0)
return r;
new_secret = user_record_new();
if (!new_secret)
return log_oom();
r = acquire_new_password(username, new_secret, /* suggest= */ true, NULL);
if (r < 0)
return r;
for (;;) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
r = bus_message_new_method_call(bus, &m, bus_mgr, "ChangePasswordHome");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(m, "s", username);
if (r < 0)
return bus_log_create_error(r);
r = bus_message_append_secret(m, new_secret);
if (r < 0)
return bus_log_create_error(r);
r = bus_message_append_secret(m, old_secret);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL);
if (r < 0) {
if (sd_bus_error_has_name(&error, BUS_ERROR_LOW_PASSWORD_QUALITY)) {
log_error_errno(r, "%s", bus_error_message(&error, r));
r = acquire_new_password(username, new_secret, /* suggest= */ false, NULL);
} else if (sd_bus_error_has_name(&error, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN))
/* In the generic handler we'd ask for a password in this case, but when
* changing passwords that's not sufficeint, as we need to acquire all keys
* first. */
return log_error_errno(r, "Security token not inserted, refusing.");
else
r = handle_generic_user_record_error(username, old_secret, &error, r, true);
if (r < 0)
return r;
} else
break;
}
return 0;
}
static int parse_disk_size(const char *t, uint64_t *ret) {
int r;
assert(t);
assert(ret);
if (streq(t, "min"))
*ret = 0;
else if (streq(t, "max"))
*ret = UINT64_MAX-1; /* Largest size that isn't UINT64_MAX special marker */
else {
uint64_t ds;
r = parse_size(t, 1024, &ds);
if (r < 0)
return log_error_errno(r, "Failed to parse disk size parameter: %s", t);
if (ds >= UINT64_MAX) /* UINT64_MAX has special meaning for us ("dont change"), refuse */
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Disk size out of range: %s", t);
*ret = ds;
}
return 0;
}
VERB(verb_resize_home, "resize", "USER SIZE\0", 2, 3, 0,
"Resize a home area");
static int verb_resize_home(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(user_record_unrefp) UserRecord *secret = NULL;
uint64_t ds = UINT64_MAX;
int r;
r = acquire_bus(&bus);
if (r < 0)
return r;
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
if (arg_disk_size_relative != UINT64_MAX ||
(argc > 2 && parse_permyriad(argv[2]) >= 0))
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"Relative disk size specification currently not supported when resizing.");
if (argc > 2) {
r = parse_disk_size(argv[2], &ds);
if (r < 0)
return r;
}
if (arg_disk_size != UINT64_MAX) {
if (ds != UINT64_MAX && ds != arg_disk_size)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Disk size specified twice and doesn't match, refusing.");
ds = arg_disk_size;
}
r = acquire_passed_secrets(argv[1], &secret);
if (r < 0)
return r;
for (;;) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
r = bus_message_new_method_call(bus, &m, bus_mgr, "ResizeHome");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(m, "st", argv[1], ds);
if (r < 0)
return bus_log_create_error(r);
r = bus_message_append_secret(m, secret);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL);
if (r < 0) {
r = handle_generic_user_record_error(argv[1], secret, &error, r, false);