HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/home/homed-home.c

129,052 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 12d83b8f6fbc3ea7861f40de99c2ab85b02f6207

Lines 1001–2000 of 3,443

        }
 
        if (hr) {
                _cleanup_(user_record_unrefp) UserRecord *signed_hr = NULL;
                bool signed_locally = false;
                int allowed;
 
                allowed = user_record_self_changes_allowed(h->record, hr);
                if (allowed < 0) {
                        r = log_error_errno(allowed, "Failed to determine whether worker returned permitted changes: %m");
                        goto finish;
                }
 
                if (!allowed) {
                        r = home_verify_user_record(h, hr, &signed_locally, &error);
                        if (r < 0)
                                goto finish;
                } else {
                        int is_signed = manager_verify_user_record(h->manager, hr);
 
                        switch (is_signed) {
 
                        case USER_RECORD_SIGNED_EXCLUSIVE:
                                signed_locally = true;
                                break;
 
                        case USER_RECORD_SIGNED:
                        case USER_RECORD_FOREIGN:
                                break;
 
                        case USER_RECORD_UNSIGNED:
                                if (h->signed_locally <= 0) {
                                        r = sd_bus_error_setf(&error, BUS_ERROR_HOME_RECORD_SIGNED,
                                                              "Home %s is signed and cannot be modified locally.", h->user_name);
                                        goto finish;
                                }
 
                                r = manager_sign_user_record(h->manager, hr, &signed_hr, &error);
                                if (r < 0)
                                        goto finish;
 
                                hr = signed_hr;
                                signed_locally = true;
                                break;
 
                        case -ENOKEY:
                                r = home_verify_user_record(h, hr, &signed_locally, &error);
                                assert(r < 0);
                                goto finish;
 
                        default:
                                assert(is_signed < 0);
                                r = log_error_errno(is_signed, "Failed to verify worker returned record: %m");
                                goto finish;
                        }
                }
 
                if (!FLAGS_SET(flags, SD_HOMED_UPDATE_OFFLINE)) {
                        r = user_record_good_authentication(h->record);
                        if (r < 0)
                                log_warning_errno(r, "Failed to increase good authentication counter, ignoring: %m");
                }
 
                r = home_set_record(h, hr);
                if (r >= 0) {
                        h->signed_locally = signed_locally;
 
                        r = home_save_record(h);
                }
                if (r < 0) {
                        if (FLAGS_SET(flags, SD_HOMED_UPDATE_OFFLINE)) {
                                log_error_errno(r, "Failed to update home record and write it to disk: %m");
                                sd_bus_error_set(&error, SD_BUS_ERROR_FAILED, "Failed to cache changes to home record");
                                goto finish;
                        } else
                                log_warning_errno(r, "Failed to update home record, ignoring: %m");
                }
        }
 
        log_debug("Change operation of %s completed.", h->user_name);
        (void) manager_schedule_rebalance(h->manager, /* immediately= */ false);
        r = 0;
 
finish:
        h->current_operation = operation_result_unref(h->current_operation, r, &error);
        home_set_state(h, _HOME_STATE_INVALID);
}
 
static void home_locking_finish(Home *h, int ret, UserRecord *hr) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        int r;
 
        assert(h);
        assert(h->state == HOME_LOCKING);
 
        if (ret < 0) {
                (void) convert_worker_errno(h, ret, &error);
                r = log_error_errno(ret, "Locking operation failed: %m");
                goto finish;
        }
 
        log_debug("Locking operation of %s completed.", h->user_name);
        h->current_operation = operation_result_unref(h->current_operation, 0, NULL);
        home_set_state(h, HOME_LOCKED);
        return;
 
finish:
        /* If a specific home doesn't know the concept of locking, then that's totally OK, don't propagate
         * the error if we are executing a LockAllHomes() operation. */
 
        if (h->current_operation->type == OPERATION_LOCK_ALL && r == -ENOTTY)
                h->current_operation = operation_result_unref(h->current_operation, 0, NULL);
        else
                h->current_operation = operation_result_unref(h->current_operation, r, &error);
 
        home_set_state(h, _HOME_STATE_INVALID);
}
 
static void home_unlocking_finish(Home *h, int ret, UserRecord *hr) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        int r;
 
        assert(h);
        assert(IN_SET(h->state, HOME_UNLOCKING, HOME_UNLOCKING_FOR_ACQUIRE));
 
        if (ret < 0) {
                home_count_bad_authentication(h, ret, /* save= */ true);
 
                (void) convert_worker_errno(h, ret, &error);
                r = log_full_errno(error_is_bad_password(ret) ? LOG_NOTICE : LOG_ERR,
                                   ret, "Unlocking operation failed: %s", bus_error_message(&error, ret));
 
                /* Revert to locked state */
                home_set_state(h, HOME_LOCKED);
                h->current_operation = operation_result_unref(h->current_operation, r, &error);
                return;
        }
 
        r = user_record_good_authentication(h->record);
        if (r < 0)
                log_warning_errno(r, "Failed to increase good authentication counter, ignoring: %m");
        else {
                r = home_save_record(h);
                if (r < 0)
                        log_warning_errno(r, "Failed to write home record to disk, ignoring: %m");
        }
 
        log_debug("Unlocking operation of %s completed.", h->user_name);
 
        h->current_operation = operation_result_unref(h->current_operation, 0, NULL);
        home_set_state(h, _HOME_STATE_INVALID);
}
 
static void home_authenticating_finish(Home *h, int ret, UserRecord *hr) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        int r;
 
        assert(h);
        assert(IN_SET(h->state, HOME_AUTHENTICATING, HOME_AUTHENTICATING_WHILE_ACTIVE, HOME_AUTHENTICATING_FOR_ACQUIRE));
 
        if (ret < 0) {
                home_count_bad_authentication(h, ret, /* save= */ true);
 
                (void) convert_worker_errno(h, ret, &error);
                r = log_full_errno(error_is_bad_password(ret) ? LOG_NOTICE : LOG_ERR,
                                   ret, "Authentication failed: %s", bus_error_message(&error, ret));
                goto finish;
        }
 
        if (hr) {
                bool signed_locally = false, signed_locally_needs_update = false;
 
                r = home_verify_user_record(h, hr, &signed_locally, /* ret_error= */ NULL);
                if (r < 0)
                        hr = h->record;
                else
                        signed_locally_needs_update = true;
 
                r = home_set_record(h, hr);
                if (r < 0)
                        log_warning_errno(r, "Failed to update home record, ignoring: %m");
                else {
                        if (signed_locally_needs_update)
                                h->signed_locally = signed_locally;
 
                        r = user_record_good_authentication(h->record);
                        if (r < 0)
                                log_warning_errno(r, "Failed to increase good authentication counter, ignoring: %m");
 
                        r = home_save_record(h);
                        if (r < 0)
                                log_warning_errno(r, "Failed to write home record to disk, ignoring: %m");
                }
        }
 
        log_debug("Authentication of %s completed.", h->user_name);
        r = 0;
 
finish:
        h->current_operation = operation_result_unref(h->current_operation, r, &error);
        home_set_state(h, _HOME_STATE_INVALID);
}
 
static int home_on_worker_process(sd_event_source *s, const siginfo_t *si, void *userdata) {
        _cleanup_(user_record_unrefp) UserRecord *hr = NULL;
        Home *h = ASSERT_PTR(userdata);
        int ret;
 
        assert(s);
        assert(si);
 
        assert(h->worker_pid.pid == si->si_pid);
        assert(h->worker_event_source);
        assert(h->worker_stdout_fd >= 0);
 
        (void) hashmap_remove_value(h->manager->homes_by_worker_pid, &h->worker_pid, h);
 
        pidref_done(&h->worker_pid);
        h->worker_event_source = sd_event_source_disable_unref(h->worker_event_source);
 
        if (si->si_code != CLD_EXITED) {
                assert(IN_SET(si->si_code, CLD_KILLED, CLD_DUMPED));
                ret = log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Worker process died abnormally with signal %s.", signal_to_string(si->si_status));
        } else if (si->si_status != EXIT_SUCCESS) {
                /* If we received an error code via sd_notify(), use it */
                if (h->worker_error_code != 0)
                        ret = log_debug_errno(h->worker_error_code, "Worker reported error code %s.", ERRNO_NAME(h->worker_error_code));
                else
                        ret = log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Worker exited with exit code %i.", si->si_status);
        } else
                ret = home_parse_worker_stdout(TAKE_FD(h->worker_stdout_fd), &hr);
 
        h->worker_stdout_fd = safe_close(h->worker_stdout_fd);
 
        switch (h->state) {
 
        case HOME_FIXATING:
        case HOME_FIXATING_FOR_ACTIVATION:
        case HOME_FIXATING_FOR_ACQUIRE:
                home_fixate_finish(h, ret, hr);
                break;
 
        case HOME_ACTIVATING:
        case HOME_ACTIVATING_FOR_ACQUIRE:
                home_activate_finish(h, ret, hr);
                break;
 
        case HOME_DEACTIVATING:
                home_deactivate_finish(h, ret, hr);
                break;
 
        case HOME_LOCKING:
                home_locking_finish(h, ret, hr);
                break;
 
        case HOME_UNLOCKING:
        case HOME_UNLOCKING_FOR_ACQUIRE:
                home_unlocking_finish(h, ret, hr);
                break;
 
        case HOME_CREATING:
                home_create_finish(h, ret, hr);
                break;
 
        case HOME_REMOVING:
                home_remove_finish(h, ret, hr);
                break;
 
        case HOME_UPDATING:
        case HOME_UPDATING_WHILE_ACTIVE:
        case HOME_RESIZING:
        case HOME_RESIZING_WHILE_ACTIVE:
        case HOME_PASSWD:
        case HOME_PASSWD_WHILE_ACTIVE:
                home_change_finish(h, ret, hr);
                break;
 
        case HOME_AUTHENTICATING:
        case HOME_AUTHENTICATING_WHILE_ACTIVE:
        case HOME_AUTHENTICATING_FOR_ACQUIRE:
                home_authenticating_finish(h, ret, hr);
                break;
 
        default:
                assert_not_reached();
        }
 
        return 0;
}
 
static int home_start_work(
                Home *h,
                const char *verb,
                UserRecord *hr,
                UserRecord *secret,
                Hashmap *blobs,
                uint64_t flags) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *fdmap = NULL;
        _cleanup_(erase_and_freep) char *formatted = NULL;
        _cleanup_close_ int stdin_fd = -EBADF, stdout_fd = -EBADF;
        _cleanup_free_ int *blob_fds = NULL;
        int r;
 
        assert(h);
        assert(verb);
        assert(hr);
 
        if (pidref_is_set(&h->worker_pid))
                return -EBUSY;
 
        assert(h->worker_stdout_fd < 0);
        assert(!h->worker_event_source);
 
        v = sd_json_variant_ref(hr->json);
 
        if (secret) {
                sd_json_variant *sub = NULL;
 
                sub = sd_json_variant_by_key(secret->json, "secret");
                if (!sub)
                        return -ENOKEY;
 
                sd_json_variant_sensitive(sub);
 
                r = sd_json_variant_set_field(&v, "secret", sub);
                if (r < 0)
                        return r;
        }
 
        if (blobs) {
                const char *blob_filename = NULL;
                void *fd_ptr;
                size_t i = 0;
 
                blob_fds = new(int, hashmap_size(blobs));
                if (!blob_fds)
                        return -ENOMEM;
 
                /* homework needs to be able to tell the difference between blobs being null
                 * (the fdmap field is completely missing) and it being empty (the field is an
                 * empty object) */
                r = sd_json_variant_new_object(&fdmap, NULL, 0);
                if (r < 0)
                        return r;
 
                HASHMAP_FOREACH_KEY(fd_ptr, blob_filename, blobs) {
                        blob_fds[i] = PTR_TO_FD(fd_ptr);
 
                        r = sd_json_variant_set_field_integer(&fdmap, blob_filename, i);
                        if (r < 0)
                                return r;
 
                        i++;
                }
 
                r = sd_json_variant_set_field(&v, HOMEWORK_BLOB_FDMAP_FIELD, fdmap);
                if (r < 0)
                        return r;
        }
 
        r = sd_json_variant_format(v, 0, &formatted);
        if (r < 0)
                return r;
 
        stdin_fd = memfd_new_and_seal_string("request", formatted);
        if (stdin_fd < 0)
                return stdin_fd;
 
        if (DEBUG_LOGGING) {
                _cleanup_(erase_and_freep) char *censored_text = NULL;
 
                /* Suppress sensitive fields in the debug output */
                r = sd_json_variant_format(v, /* flags= */ SD_JSON_FORMAT_CENSOR_SENSITIVE, &censored_text);
                if (r < 0)
                        return r;
 
                log_debug("Sending to worker: %s", censored_text);
        }
 
        stdout_fd = memfd_new("homework-stdout");
        if (stdout_fd < 0)
                return stdout_fd;
 
        _cleanup_(pidref_done_sigkill_wait) PidRef pid = PIDREF_NULL;
        r = pidref_safe_fork_full("(sd-homework)",
                           (int[]) { stdin_fd, stdout_fd, STDERR_FILENO },
                           blob_fds, hashmap_size(blobs),
                           FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_CLOEXEC_OFF|FORK_PACK_FDS|FORK_DEATHSIG_SIGTERM|
                           FORK_REARRANGE_STDIO|FORK_LOG|FORK_REOPEN_LOG, &pid);
        if (r < 0)
                return r;
        if (r == 0) {
                /* Child */
 
                if (setenv("NOTIFY_SOCKET", h->manager->notify_socket_path, /* overwrite= */ true) < 0) {
                        log_error_errno(errno, "Failed to set $NOTIFY_SOCKET: %m");
                        _exit(EXIT_FAILURE);
                }
 
                /* If we haven't locked the device yet, ask for a lock to be taken and be passed back to us via sd_notify(). */
                if (setenv("SYSTEMD_LUKS_LOCK", one_zero(h->luks_lock_fd < 0), 1) < 0) {
                        log_error_errno(errno, "Failed to set $SYSTEMD_LUKS_LOCK: %m");
                        _exit(EXIT_FAILURE);
                }
 
                if (h->manager->default_storage >= 0)
                        if (setenv("SYSTEMD_HOME_DEFAULT_STORAGE", user_storage_to_string(h->manager->default_storage), 1) < 0) {
                                log_error_errno(errno, "Failed to set $SYSTEMD_HOME_DEFAULT_STORAGE: %m");
                                _exit(EXIT_FAILURE);
                        }
 
                if (h->manager->default_file_system_type)
                        if (setenv("SYSTEMD_HOME_DEFAULT_FILE_SYSTEM_TYPE", h->manager->default_file_system_type, 1) < 0) {
                                log_error_errno(errno, "Failed to set $SYSTEMD_HOME_DEFAULT_FILE_SYSTEM_TYPE: %m");
                                _exit(EXIT_FAILURE);
                        }
 
                if (setenv("SYSTEMD_HOMEWORK_UPDATE_OFFLINE", one_zero(FLAGS_SET(flags, SD_HOMED_UPDATE_OFFLINE)), 1) < 0) {
                        log_error_errno(errno, "Failed to set $SYSTEMD_HOMEWORK_UPDATE_OFFLINE: %m");
                        _exit(EXIT_FAILURE);
                }
 
                r = setenv_systemd_exec_pid(true);
                if (r < 0)
                        log_warning_errno(r, "Failed to update $SYSTEMD_EXEC_PID, ignoring: %m");
 
                r = setenv_systemd_log_level();
                if (r < 0)
                        log_warning_errno(r, "Failed to update $SYSTEMD_LOG_LEVEL, ignoring: %m");
 
                r = invoke_callout_binary(SYSTEMD_HOMEWORK_PATH, STRV_MAKE(SYSTEMD_HOMEWORK_PATH, verb));
                log_error_errno(r, "Failed to invoke %s: %m", SYSTEMD_HOMEWORK_PATH);
                _exit(EXIT_FAILURE);
        }
 
        r = event_add_child_pidref(h->manager->event, &h->worker_event_source, &pid, WEXITED, home_on_worker_process, h);
        if (r < 0)
                return r;
 
        (void) sd_event_source_set_description(h->worker_event_source, "worker");
 
        h->worker_pid = TAKE_PIDREF(pid);
        r = hashmap_put(h->manager->homes_by_worker_pid, &h->worker_pid, h);
        if (r < 0) {
                pidref_done_sigkill_wait(&h->worker_pid);
                h->worker_event_source = sd_event_source_disable_unref(h->worker_event_source);
                return r;
        }
 
        h->worker_stdout_fd = TAKE_FD(stdout_fd);
        h->worker_error_code = 0;
 
        return 0;
}
 
static int home_ratelimit(Home *h, sd_bus_error *error) {
        int r, ret;
 
        assert(h);
 
        ret = user_record_ratelimit(h->record);
        if (ret < 0)
                return ret;
 
        if (h->state != HOME_UNFIXATED) {
                r = home_save_record(h);
                if (r < 0)
                        log_warning_errno(r, "Failed to save updated record, ignoring: %m");
        }
 
        if (ret == 0) {
                usec_t t, n;
 
                n = now(CLOCK_REALTIME);
                t = user_record_ratelimit_next_try(h->record);
 
                if (t != USEC_INFINITY && t > n)
                        return sd_bus_error_setf(error, BUS_ERROR_AUTHENTICATION_LIMIT_HIT,
                                                 "Too many login attempts, please try again in %s!",
                                                 FORMAT_TIMESPAN(t - n, USEC_PER_SEC));
 
                return sd_bus_error_set(error, BUS_ERROR_AUTHENTICATION_LIMIT_HIT, "Too many login attempts, please try again later.");
        }
 
        return 0;
}
 
static int home_fixate_internal(
                Home *h,
                UserRecord *secret,
                HomeState for_state,
                sd_bus_error *error) {
 
        int r;
 
        assert(h);
        assert(secret);
        assert(IN_SET(for_state, HOME_FIXATING, HOME_FIXATING_FOR_ACTIVATION, HOME_FIXATING_FOR_ACQUIRE));
 
        r = home_start_work(h, "inspect", h->record, secret, NULL, 0);
        if (r < 0)
                return r;
 
        if (IN_SET(for_state, HOME_FIXATING_FOR_ACTIVATION, HOME_FIXATING_FOR_ACQUIRE)) {
                /* Remember the secret data, since we need it for the activation again, later on. */
                user_record_unref(h->secret);
                h->secret = user_record_ref(secret);
        }
 
        home_set_state(h, for_state);
        return 0;
}
 
int home_fixate(Home *h, UserRecord *secret, sd_bus_error *error) {
        int r;
 
        assert(h);
        assert(secret);
 
        switch (home_get_state(h)) {
        case HOME_ABSENT:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
        case HOME_INACTIVE:
        case HOME_DIRTY:
        case HOME_ACTIVE:
        case HOME_LINGERING:
        case HOME_LOCKED:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_ALREADY_FIXATED, "Home %s is already fixated.", h->user_name);
        case HOME_UNFIXATED:
                break;
        default:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
        }
 
        r = home_ratelimit(h, error);
        if (r < 0)
                return r;
 
        return home_fixate_internal(h, secret, HOME_FIXATING, error);
}
 
static int home_activate_internal(Home *h, UserRecord *secret, HomeState for_state, sd_bus_error *error) {
        int r;
 
        assert(h);
        assert(secret);
        assert(IN_SET(for_state, HOME_ACTIVATING, HOME_ACTIVATING_FOR_ACQUIRE));
 
        r = home_start_work(h, "activate", h->record, secret, NULL, 0);
        if (r < 0)
                return r;
 
        home_set_state(h, for_state);
        return 0;
}
 
int home_activate(Home *h, bool if_referenced, UserRecord *secret, sd_bus_error *error) {
        int r;
 
        assert(h);
        assert(secret);
 
        if (if_referenced && !home_is_referenced(h))
                return sd_bus_error_setf(error, BUS_ERROR_HOME_NOT_REFERENCED, "Home %s is currently not referenced.", h->user_name);
 
        switch (home_get_state(h)) {
        case HOME_UNFIXATED:
                return home_fixate_internal(h, secret, HOME_FIXATING_FOR_ACTIVATION, error);
        case HOME_ABSENT:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
        case HOME_ACTIVE:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_ALREADY_ACTIVE, "Home %s is already active.", h->user_name);
        case HOME_LINGERING:
                /* If we are lingering, i.e. active but are supposed to be deactivated, then cancel this
                 * timer if the user explicitly asks us to be active */
                h->retry_deactivate_event_source = sd_event_source_disable_unref(h->retry_deactivate_event_source);
                return 0;
        case HOME_LOCKED:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
        case HOME_INACTIVE:
        case HOME_DIRTY:
                break;
        default:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
        }
 
        r = home_ratelimit(h, error);
        if (r < 0)
                return r;
 
        return home_activate_internal(h, secret, HOME_ACTIVATING, error);
}
 
static int home_authenticate_internal(Home *h, UserRecord *secret, HomeState for_state, sd_bus_error *error) {
        int r;
 
        assert(h);
        assert(secret);
        assert(IN_SET(for_state, HOME_AUTHENTICATING, HOME_AUTHENTICATING_WHILE_ACTIVE, HOME_AUTHENTICATING_FOR_ACQUIRE));
 
        r = home_start_work(h, "inspect", h->record, secret, NULL, 0);
        if (r < 0)
                return r;
 
        home_set_state(h, for_state);
        return 0;
}
 
int home_authenticate(Home *h, UserRecord *secret, sd_bus_error *error) {
        HomeState state;
        int r;
 
        assert(h);
        assert(secret);
 
        state = home_get_state(h);
        switch (state) {
        case HOME_ABSENT:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
        case HOME_LOCKED:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
        case HOME_UNFIXATED:
        case HOME_INACTIVE:
        case HOME_DIRTY:
        case HOME_ACTIVE:
        case HOME_LINGERING:
                break;
        default:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
        }
 
        r = home_ratelimit(h, error);
        if (r < 0)
                return r;
 
        return home_authenticate_internal(h, secret, HOME_STATE_IS_ACTIVE(state) ? HOME_AUTHENTICATING_WHILE_ACTIVE : HOME_AUTHENTICATING, error);
}
 
static int home_deactivate_internal(Home *h, bool force, sd_bus_error *error) {
        int r;
 
        assert(h);
 
        home_unpin(h); /* unpin so that we can deactivate */
 
        r = home_start_work(h, force ? "deactivate-force" : "deactivate", h->record, NULL, NULL, 0);
        if (r < 0)
                /* Operation failed before it even started, reacquire pin fd, if state still dictates so */
                home_update_pin_fd(h, _HOME_STATE_INVALID);
        else {
                home_set_state(h, HOME_DEACTIVATING);
                r = 0;
        }
 
        /* Let's start a timer to retry deactivation in 15. We'll stop the timer once we manage to deactivate
         * the home directory again, or we start any other operation. */
        home_start_retry_deactivate(h);
 
        return r;
}
 
int home_deactivate(Home *h, bool force, sd_bus_error *error) {
        assert(h);
 
        switch (home_get_state(h)) {
        case HOME_UNFIXATED:
        case HOME_ABSENT:
        case HOME_INACTIVE:
        case HOME_DIRTY:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_NOT_ACTIVE, "Home %s not active.", h->user_name);
        case HOME_LOCKED:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
        case HOME_ACTIVE:
        case HOME_LINGERING:
                break;
        default:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
        }
 
        return home_deactivate_internal(h, force, error);
}
 
int home_create(Home *h, UserRecord *secret, Hashmap *blobs, uint64_t flags, sd_bus_error *error) {
        int r;
 
        assert(h);
        assert(secret);
 
        switch (home_get_state(h)) {
        case HOME_INACTIVE: {
                int t;
 
                if (h->record->storage < 0)
                        break; /* if no storage is defined we don't know what precisely to look for, hence
                                * HOME_INACTIVE is OK in that case too. */
 
                t = user_record_test_image_path(h->record);
                if (IN_SET(t, USER_TEST_MAYBE, USER_TEST_UNDEFINED))
                        break; /* And if the image path test isn't conclusive, let's also go on */
 
                if (IN_SET(t, -EBADF, -ENOTDIR))
                        return sd_bus_error_setf(error, BUS_ERROR_HOME_EXISTS, "Selected home image of user %s already exists or has wrong inode type.", h->user_name);
 
                return sd_bus_error_setf(error, BUS_ERROR_HOME_EXISTS, "Selected home image of user %s already exists.", h->user_name);
        }
        case HOME_UNFIXATED:
        case HOME_DIRTY:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_EXISTS, "Home of user %s already exists.", h->user_name);
        case HOME_ABSENT:
                break;
        case HOME_ACTIVE:
        case HOME_LINGERING:
        case HOME_LOCKED:
        default:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "Home %s is currently being used, or an operation on home %s is currently being executed.", h->user_name, h->user_name);
        }
 
        if (h->record->enforce_password_policy == false)
                log_debug("Password quality check turned off for account, skipping.");
        else {
                r = user_record_check_password_quality(h->record, secret, error);
                if (r < 0)
                        return r;
        }
 
        r = home_start_work(h, "create", h->record, secret, blobs, flags);
        if (r < 0)
                return r;
 
        home_set_state(h, HOME_CREATING);
        return 0;
}
 
int home_remove(Home *h, sd_bus_error *error) {
        HomeState state;
        int r;
 
        assert(h);
 
        state = home_get_state(h);
        switch (state) {
        case HOME_ABSENT: /* If the home directory is absent, then this is just like unregistering */
                return home_unregister(h, error);
        case HOME_LOCKED:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
        case HOME_UNFIXATED:
        case HOME_INACTIVE:
        case HOME_DIRTY:
                break;
        case HOME_ACTIVE:
        case HOME_LINGERING:
        default:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "Home %s is currently being used, or an operation on home %s is currently being executed.", h->user_name, h->user_name);
        }
 
        r = home_start_work(h, "remove", h->record, NULL, NULL, 0);
        if (r < 0)
                return r;
 
        home_set_state(h, HOME_REMOVING);
        return 0;
}
 
static int user_record_extend_with_binding(UserRecord *hr, UserRecord *with_binding, UserRecordLoadFlags flags, UserRecord **ret) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
        _cleanup_(user_record_unrefp) UserRecord *nr = NULL;
        sd_json_variant *binding;
        int r;
 
        assert(hr);
        assert(with_binding);
        assert(ret);
 
        assert_se(v = sd_json_variant_ref(hr->json));
 
        binding = sd_json_variant_by_key(with_binding->json, "binding");
        if (binding) {
                r = sd_json_variant_set_field(&v, "binding", binding);
                if (r < 0)
                        return r;
        }
 
        nr = user_record_new();
        if (!nr)
                return -ENOMEM;
 
        r = user_record_load(nr, v, flags);
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(nr);
        return 0;
}
 
static int home_update_internal(
                Home *h,
                const char *verb,
                UserRecord *hr,
                UserRecord *secret,
                Hashmap *blobs,
                uint64_t flags,
                sd_bus_error *error) {
 
        _cleanup_(user_record_unrefp) UserRecord *new_hr = NULL, *saved_secret = NULL, *signed_hr = NULL;
        int r, c;
 
        assert(h);
        assert(verb);
        assert(hr);
 
        if (!user_record_compatible(hr, h->record))
                return sd_bus_error_set(error, BUS_ERROR_HOME_RECORD_MISMATCH, "Updated user record is not compatible with existing one.");
        c = user_record_compare_last_change(hr, h->record); /* refuse downgrades */
        if (c < 0)
                return sd_bus_error_set(error, BUS_ERROR_HOME_RECORD_DOWNGRADE, "Refusing to update to older home record.");
 
        if (!secret && FLAGS_SET(hr->mask, USER_RECORD_SECRET)) {
                r = user_record_clone(hr, USER_RECORD_EXTRACT_SECRET|USER_RECORD_PERMISSIVE, &saved_secret);
                if (r < 0)
                        return r;
 
                secret = saved_secret;
        }
 
        r = manager_verify_user_record(h->manager, hr);
        switch (r) {
 
        case USER_RECORD_UNSIGNED:
                if (h->signed_locally <= 0) /* If the existing record is not owned by us, don't accept an
                                             * unsigned new record. i.e. only implicitly sign new records
                                             * that where previously signed by us too. */
                        return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_SIGNED, "Home %s is signed and cannot be modified locally.", h->user_name);
 
                /* The updated record is not signed, then do so now */
                r = manager_sign_user_record(h->manager, hr, &signed_hr, error);
                if (r < 0)
                        return r;
 
                hr = signed_hr;
                break;
 
        case USER_RECORD_SIGNED_EXCLUSIVE:
        case USER_RECORD_SIGNED:
        case USER_RECORD_FOREIGN:
                /* Has already been signed. Great! */
                break;
 
        case -ENOKEY:
        default:
                return r;
        }
 
        r = user_record_extend_with_binding(hr, h->record, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_PERMISSIVE, &new_hr);
        if (r < 0)
                return r;
 
        if (c == 0) {
                /* different payload but same lastChangeUSec field? That's not cool! */
 
                r = user_record_masked_equal(new_hr, h->record, USER_RECORD_REGULAR|USER_RECORD_PRIVILEGED|USER_RECORD_PER_MACHINE);
                if (r < 0)
                        return r;
                if (r == 0)
                        return sd_bus_error_set(error, BUS_ERROR_HOME_RECORD_MISMATCH, "Home record different but timestamp remained the same, refusing.");
        }
 
        r = home_start_work(h, verb, new_hr, secret, blobs, flags);
        if (r < 0)
                return r;
 
        return 0;
}
 
int home_update(Home *h, UserRecord *hr, Hashmap *blobs, uint64_t flags, sd_bus_error *error) {
        HomeState state;
        int r;
 
        assert(h);
        assert(hr);
 
        state = home_get_state(h);
        switch (state) {
        case HOME_UNFIXATED:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_UNFIXATED, "Home %s has not been fixated yet.", h->user_name);
        case HOME_ABSENT:
                if (!FLAGS_SET(flags, SD_HOMED_UPDATE_OFFLINE))
                        return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
                break; /* offline updates are compatible w/ an absent home area */
        case HOME_LOCKED:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
        case HOME_INACTIVE:
        case HOME_DIRTY:
        case HOME_ACTIVE:
        case HOME_LINGERING:
                break;
        default:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
        }
 
        r = home_ratelimit(h, error);
        if (r < 0)
                return r;
 
        r = home_update_internal(h, "update", hr, NULL, blobs, flags, error);
        if (r < 0)
                return r;
 
        home_set_state(h, HOME_STATE_IS_ACTIVE(state) ? HOME_UPDATING_WHILE_ACTIVE : HOME_UPDATING);
        return 0;
}
 
int home_resize(Home *h,
                uint64_t disk_size,
                UserRecord *secret,
                sd_bus_error *error) {
 
        _cleanup_(user_record_unrefp) UserRecord *c = NULL;
        HomeState state;
        int r;
 
        assert(h);
 
        state = home_get_state(h);
        switch (state) {
        case HOME_UNFIXATED:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_UNFIXATED, "Home %s has not been fixated yet.", h->user_name);
        case HOME_ABSENT:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
        case HOME_LOCKED:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
        case HOME_INACTIVE:
        case HOME_DIRTY:
        case HOME_ACTIVE:
        case HOME_LINGERING:
                break;
        default:
                return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
        }
 
        r = home_ratelimit(h, error);
        if (r < 0)
                return r;
 
        /* If the user didn't specify any size explicitly and rebalancing is on, then the disk size is
         * determined by automatic rebalancing and hence not user configured but determined by us and thus
         * applied anyway. */
        if (disk_size == UINT64_MAX && h->record->rebalance_weight != REBALANCE_WEIGHT_OFF)
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Disk size is being determined by automatic disk space rebalancing.");
 
        if (disk_size == UINT64_MAX || disk_size == h->record->disk_size) {
                if (h->record->disk_size == UINT64_MAX)
                        return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "No disk size to resize to specified.");
 
                c = user_record_ref(h->record); /* Shortcut if size is unspecified or matches the record */
        } else {
                _cleanup_(user_record_unrefp) UserRecord *signed_c = NULL;
 
                if (h->signed_locally <= 0) /* Don't allow changing of records not signed only by us */
                        return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_SIGNED, "Home %s is signed and cannot be modified locally.", h->user_name);
 
                r = user_record_clone(h->record, USER_RECORD_LOAD_REFUSE_SECRET|USER_RECORD_PERMISSIVE, &c);
                if (r < 0)
                        return r;
 
                r = user_record_set_disk_size(c, disk_size);
                if (r == -ERANGE)
                        return sd_bus_error_setf(error, BUS_ERROR_BAD_HOME_SIZE, "Requested size for home %s out of acceptable range.", h->user_name);
                if (r < 0)
                        return r;
 
                /* If user picked an explicit size, then turn off rebalancing, so that we don't undo what user chose */
                r = user_record_set_rebalance_weight(c, REBALANCE_WEIGHT_OFF);
                if (r < 0)
                        return r;
 
                r = user_record_update_last_changed(c, false);
                if (r == -ECHRNG)
                        return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_MISMATCH, "Record last change time of %s is newer than current time, cannot update.", h->user_name);
                if (r < 0)
                        return r;
 
                r = manager_sign_user_record(h->manager, c, &signed_c, error);
                if (r < 0)
                        return r;
 
                user_record_unref(c);
                c = TAKE_PTR(signed_c);
        }
 
        r = home_update_internal(h, "resize", c, secret, NULL, 0, error);
        if (r < 0)
                return r;
 
        home_set_state(h, HOME_STATE_IS_ACTIVE(state) ? HOME_RESIZING_WHILE_ACTIVE : HOME_RESIZING);
        return 0;
}
 
static int home_may_change_password(
                Home *h,
                sd_bus_error *error) {