1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <linux/memfd.h>
11 #include "blockdev-util.h"
12 #include "btrfs-util.h"
13 #include "bus-common-errors.h"
14 #include "data-fd-util.h"
16 #include "errno-list.h"
17 #include "errno-util.h"
20 #include "filesystems.h"
22 #include "glyph-util.h"
23 #include "home-util.h"
24 #include "homed-home-bus.h"
25 #include "homed-home.h"
26 #include "missing_magic.h"
27 #include "missing_syscall.h"
29 #include "path-util.h"
30 #include "process-util.h"
31 #include "pwquality-util.h"
32 #include "quota-util.h"
33 #include "resize-fs.h"
35 #include "signal-util.h"
36 #include "stat-util.h"
37 #include "string-table.h"
39 #include "uid-alloc-range.h"
40 #include "user-record-pwquality.h"
41 #include "user-record-sign.h"
42 #include "user-record-util.h"
43 #include "user-record.h"
44 #include "user-util.h"
46 /* Retry to deactivate home directories again and again every 15s until it works */
47 #define RETRY_DEACTIVATE_USEC (15U * USEC_PER_SEC)
49 #define HOME_USERS_MAX 500
50 #define PENDING_OPERATIONS_MAX 100
52 assert_cc(HOME_UID_MIN
<= HOME_UID_MAX
);
53 assert_cc(HOME_USERS_MAX
<= (HOME_UID_MAX
- HOME_UID_MIN
+ 1));
55 static int home_start_work(Home
*h
, const char *verb
, UserRecord
*hr
, UserRecord
*secret
);
57 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(operation_hash_ops
, void, trivial_hash_func
, trivial_compare_func
, Operation
, operation_unref
);
59 static int suitable_home_record(UserRecord
*hr
) {
67 /* We are a bit more restrictive with what we accept as homed-managed user than what we accept in
68 * home records in general. Let's enforce the stricter rule here. */
69 if (!suitable_user_name(hr
->user_name
))
71 if (!uid_is_valid(hr
->uid
))
74 /* Insist we are outside of the dynamic and system range */
75 if (uid_is_system(hr
->uid
) || gid_is_system(user_record_gid(hr
)) ||
76 uid_is_dynamic(hr
->uid
) || gid_is_dynamic(user_record_gid(hr
)))
77 return -EADDRNOTAVAIL
;
79 /* Insist that GID and UID match */
80 if (user_record_gid(hr
) != (gid_t
) hr
->uid
)
83 /* Similar for the realm */
85 r
= suitable_realm(hr
->realm
);
95 int home_new(Manager
*m
, UserRecord
*hr
, const char *sysfs
, Home
**ret
) {
96 _cleanup_(home_freep
) Home
*home
= NULL
;
97 _cleanup_free_
char *nm
= NULL
, *ns
= NULL
;
103 r
= suitable_home_record(hr
);
107 if (hashmap_contains(m
->homes_by_name
, hr
->user_name
))
110 if (hashmap_contains(m
->homes_by_uid
, UID_TO_PTR(hr
->uid
)))
113 if (sysfs
&& hashmap_contains(m
->homes_by_sysfs
, sysfs
))
116 if (hashmap_size(m
->homes_by_name
) >= HOME_USERS_MAX
)
119 nm
= strdup(hr
->user_name
);
135 .user_name
= TAKE_PTR(nm
),
137 .state
= _HOME_STATE_INVALID
,
138 .worker_stdout_fd
= -EBADF
,
139 .sysfs
= TAKE_PTR(ns
),
140 .signed_locally
= -1,
142 .luks_lock_fd
= -EBADF
,
145 r
= hashmap_put(m
->homes_by_name
, home
->user_name
, home
);
149 r
= hashmap_put(m
->homes_by_uid
, UID_TO_PTR(home
->uid
), home
);
154 r
= hashmap_put(m
->homes_by_sysfs
, home
->sysfs
, home
);
159 r
= user_record_clone(hr
, USER_RECORD_LOAD_MASK_SECRET
|USER_RECORD_PERMISSIVE
, &home
->record
);
163 (void) bus_manager_emit_auto_login_changed(m
);
164 (void) bus_home_emit_change(home
);
165 (void) manager_schedule_rebalance(m
, /* immediately= */ false);
168 *ret
= TAKE_PTR(home
);
175 Home
*home_free(Home
*h
) {
181 (void) bus_home_emit_remove(h
);
182 (void) bus_manager_emit_auto_login_changed(h
->manager
);
185 (void) hashmap_remove_value(h
->manager
->homes_by_name
, h
->user_name
, h
);
187 if (uid_is_valid(h
->uid
))
188 (void) hashmap_remove_value(h
->manager
->homes_by_uid
, UID_TO_PTR(h
->uid
), h
);
191 (void) hashmap_remove_value(h
->manager
->homes_by_sysfs
, h
->sysfs
, h
);
193 if (h
->worker_pid
> 0)
194 (void) hashmap_remove_value(h
->manager
->homes_by_worker_pid
, PID_TO_PTR(h
->worker_pid
), h
);
196 if (h
->manager
->gc_focus
== h
)
197 h
->manager
->gc_focus
= NULL
;
199 (void) manager_schedule_rebalance(h
->manager
, /* immediately= */ false);
202 user_record_unref(h
->record
);
203 user_record_unref(h
->secret
);
205 h
->worker_event_source
= sd_event_source_disable_unref(h
->worker_event_source
);
206 safe_close(h
->worker_stdout_fd
);
210 h
->ref_event_source_please_suspend
= sd_event_source_disable_unref(h
->ref_event_source_please_suspend
);
211 h
->ref_event_source_dont_suspend
= sd_event_source_disable_unref(h
->ref_event_source_dont_suspend
);
213 h
->pending_operations
= ordered_set_free(h
->pending_operations
);
214 h
->pending_event_source
= sd_event_source_disable_unref(h
->pending_event_source
);
215 h
->deferred_change_event_source
= sd_event_source_disable_unref(h
->deferred_change_event_source
);
217 h
->current_operation
= operation_unref(h
->current_operation
);
219 safe_close(h
->pin_fd
);
220 safe_close(h
->luks_lock_fd
);
222 h
->retry_deactivate_event_source
= sd_event_source_disable_unref(h
->retry_deactivate_event_source
);
227 int home_set_record(Home
*h
, UserRecord
*hr
) {
228 _cleanup_(user_record_unrefp
) UserRecord
*new_hr
= NULL
;
233 assert(h
->user_name
);
237 if (user_record_equal(h
->record
, hr
))
240 r
= suitable_home_record(hr
);
244 if (!user_record_compatible(h
->record
, hr
))
247 if (!FLAGS_SET(hr
->mask
, USER_RECORD_REGULAR
) ||
248 FLAGS_SET(hr
->mask
, USER_RECORD_SECRET
))
251 if (FLAGS_SET(h
->record
->mask
, USER_RECORD_STATUS
)) {
252 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
254 /* Hmm, the existing record has status fields? If so, copy them over */
256 v
= json_variant_ref(hr
->json
);
257 r
= json_variant_set_field(&v
, "status", json_variant_by_key(h
->record
->json
, "status"));
261 new_hr
= user_record_new();
265 r
= user_record_load(new_hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_PERMISSIVE
);
272 other
= hashmap_get(h
->manager
->homes_by_uid
, UID_TO_PTR(hr
->uid
));
273 if (other
&& other
!= h
)
276 if (h
->uid
!= hr
->uid
) {
277 r
= hashmap_remove_and_replace(h
->manager
->homes_by_uid
, UID_TO_PTR(h
->uid
), UID_TO_PTR(hr
->uid
), h
);
282 user_record_unref(h
->record
);
283 h
->record
= user_record_ref(hr
);
284 h
->uid
= h
->record
->uid
;
286 /* The updated record might have a different autologin setting, trigger a PropertiesChanged event for it */
287 (void) bus_manager_emit_auto_login_changed(h
->manager
);
288 (void) bus_home_emit_change(h
);
293 int home_save_record(Home
*h
) {
294 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
295 _cleanup_free_
char *text
= NULL
;
301 v
= json_variant_ref(h
->record
->json
);
302 r
= json_variant_normalize(&v
);
304 log_warning_errno(r
, "User record could not be normalized.");
306 r
= json_variant_format(v
, JSON_FORMAT_PRETTY
|JSON_FORMAT_NEWLINE
, &text
);
310 (void) mkdir("/var/lib/systemd/", 0755);
311 (void) mkdir(home_record_dir(), 0700);
313 fn
= strjoina(home_record_dir(), "/", h
->user_name
, ".identity");
315 r
= write_string_file(fn
, text
, WRITE_STRING_FILE_ATOMIC
|WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_MODE_0600
|WRITE_STRING_FILE_SYNC
);
322 int home_unlink_record(Home
*h
) {
327 fn
= strjoina(home_record_dir(), "/", h
->user_name
, ".identity");
328 if (unlink(fn
) < 0 && errno
!= ENOENT
)
331 fn
= strjoina("/run/systemd/home/", h
->user_name
, ".ref");
332 if (unlink(fn
) < 0 && errno
!= ENOENT
)
338 static void home_unpin(Home
*h
) {
344 h
->pin_fd
= safe_close(h
->pin_fd
);
345 log_debug("Successfully closed pin fd on home for %s.", h
->user_name
);
348 static void home_pin(Home
*h
) {
353 if (h
->pin_fd
>= 0) /* Already pinned? */
356 path
= user_record_home_directory(h
->record
);
358 log_warning("No home directory path to pin for %s, ignoring.", h
->user_name
);
362 h
->pin_fd
= open(path
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
);
364 log_warning_errno(errno
, "Couldn't open home directory '%s' for pinning, ignoring: %m", path
);
368 log_debug("Successfully pinned home directory '%s'.", path
);
371 static void home_update_pin_fd(Home
*h
, HomeState state
) {
375 state
= home_get_state(h
);
377 return HOME_STATE_SHALL_PIN(state
) ? home_pin(h
) : home_unpin(h
);
380 static void home_maybe_close_luks_lock_fd(Home
*h
, HomeState state
) {
383 if (h
->luks_lock_fd
< 0)
387 state
= home_get_state(h
);
389 /* Keep the lock as long as the home dir is active or has some operation going */
390 if (HOME_STATE_IS_EXECUTING_OPERATION(state
) || HOME_STATE_IS_ACTIVE(state
) || state
== HOME_LOCKED
)
393 h
->luks_lock_fd
= safe_close(h
->luks_lock_fd
);
394 log_debug("Successfully closed LUKS backing file lock for %s.", h
->user_name
);
397 static void home_maybe_stop_retry_deactivate(Home
*h
, HomeState state
) {
400 /* Free the deactivation retry event source if we won't need it anymore. Specifically, we'll free the
401 * event source whenever the home directory is already deactivated (and we thus where successful) or
402 * if we start executing an operation that indicates that the home directory is going to be used or
403 * operated on again. Also, if the home is referenced again stop the timer */
405 if (HOME_STATE_MAY_RETRY_DEACTIVATE(state
) &&
406 !h
->ref_event_source_dont_suspend
&&
407 !h
->ref_event_source_please_suspend
)
410 h
->retry_deactivate_event_source
= sd_event_source_disable_unref(h
->retry_deactivate_event_source
);
413 static int home_deactivate_internal(Home
*h
, bool force
, sd_bus_error
*error
);
414 static void home_start_retry_deactivate(Home
*h
);
416 static int home_on_retry_deactivate(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
417 Home
*h
= ASSERT_PTR(userdata
);
422 /* 15s after the last attempt to deactivate the home directory passed. Let's try it one more time. */
424 h
->retry_deactivate_event_source
= sd_event_source_disable_unref(h
->retry_deactivate_event_source
);
426 state
= home_get_state(h
);
427 if (!HOME_STATE_MAY_RETRY_DEACTIVATE(state
))
430 if (IN_SET(state
, HOME_ACTIVE
, HOME_LINGERING
)) {
431 log_info("Again trying to deactivate home directory.");
433 /* If we are not executing any operation, let's start deactivating now. Note that this will
434 * restart our timer again, we are gonna be called again if this doesn't work. */
435 (void) home_deactivate_internal(h
, /* force= */ false, NULL
);
437 /* if we are executing an operation (specifically, area already running a deactivation
438 * operation), then simply reque the timer, so that we retry again. */
439 home_start_retry_deactivate(h
);
444 static void home_start_retry_deactivate(Home
*h
) {
450 /* Already allocated? */
451 if (h
->retry_deactivate_event_source
)
454 /* If the home directory is being used now don't start the timer */
455 if (h
->ref_event_source_dont_suspend
|| h
->ref_event_source_please_suspend
)
458 r
= sd_event_add_time_relative(
460 &h
->retry_deactivate_event_source
,
462 RETRY_DEACTIVATE_USEC
,
464 home_on_retry_deactivate
,
467 return (void) log_warning_errno(r
, "Failed to install retry-deactivate event source, ignoring: %m");
469 (void) sd_event_source_set_description(h
->retry_deactivate_event_source
, "retry-deactivate");
472 static void home_set_state(Home
*h
, HomeState state
) {
473 HomeState old_state
, new_state
;
477 old_state
= home_get_state(h
);
479 new_state
= home_get_state(h
); /* Query the new state, since the 'state' variable might be set to -1,
480 * in which case we synthesize an high-level state on demand */
482 log_info("%s: changing state %s %s %s", h
->user_name
,
483 home_state_to_string(old_state
),
484 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
),
485 home_state_to_string(new_state
));
487 home_update_pin_fd(h
, new_state
);
488 home_maybe_close_luks_lock_fd(h
, new_state
);
489 home_maybe_stop_retry_deactivate(h
, new_state
);
491 if (HOME_STATE_IS_EXECUTING_OPERATION(old_state
) && !HOME_STATE_IS_EXECUTING_OPERATION(new_state
)) {
492 /* If we just finished executing some operation, process the queue of pending operations. And
493 * enqueue it for GC too. */
495 home_schedule_operation(h
, NULL
, NULL
);
496 manager_reschedule_rebalance(h
->manager
);
497 manager_enqueue_gc(h
->manager
, h
);
501 static int home_parse_worker_stdout(int _fd
, UserRecord
**ret
) {
502 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
503 _cleanup_close_
int fd
= _fd
; /* take possession, even on failure */
504 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
505 _cleanup_fclose_
FILE *f
= NULL
;
506 unsigned line
, column
;
510 if (fstat(fd
, &st
) < 0)
511 return log_error_errno(errno
, "Failed to stat stdout fd: %m");
513 assert(S_ISREG(st
.st_mode
));
515 if (st
.st_size
== 0) { /* empty record */
520 if (lseek(fd
, SEEK_SET
, 0) == (off_t
) -1)
521 return log_error_errno(errno
, "Failed to seek to beginning of memfd: %m");
523 f
= take_fdopen(&fd
, "r");
525 return log_error_errno(errno
, "Failed to reopen memfd: %m");
528 _cleanup_free_
char *text
= NULL
;
530 r
= read_full_stream(f
, &text
, NULL
);
532 return log_error_errno(r
, "Failed to read from client: %m");
534 log_debug("Got from worker: %s", text
);
538 r
= json_parse_file(f
, "stdout", JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
540 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
542 hr
= user_record_new();
546 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_PERMISSIVE
);
548 return log_error_errno(r
, "Failed to load home record identity: %m");
554 static int home_verify_user_record(Home
*h
, UserRecord
*hr
, bool *ret_signed_locally
, sd_bus_error
*ret_error
) {
559 assert(ret_signed_locally
);
561 is_signed
= manager_verify_user_record(h
->manager
, hr
);
564 case USER_RECORD_SIGNED_EXCLUSIVE
:
565 log_info("Home %s is signed exclusively by our key, accepting.", hr
->user_name
);
566 *ret_signed_locally
= true;
569 case USER_RECORD_SIGNED
:
570 log_info("Home %s is signed by our key (and others), accepting.", hr
->user_name
);
571 *ret_signed_locally
= false;
574 case USER_RECORD_FOREIGN
:
575 log_info("Home %s is signed by foreign key we like, accepting.", hr
->user_name
);
576 *ret_signed_locally
= false;
579 case USER_RECORD_UNSIGNED
:
580 sd_bus_error_setf(ret_error
, BUS_ERROR_BAD_SIGNATURE
, "User record %s is not signed at all, refusing.", hr
->user_name
);
581 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Home %s contains user record that is not signed at all, refusing.", hr
->user_name
);
584 sd_bus_error_setf(ret_error
, BUS_ERROR_BAD_SIGNATURE
, "User record %s is not signed by any known key, refusing.", hr
->user_name
);
585 return log_error_errno(is_signed
, "Home %s contains user record that is not signed by any known key, refusing.", hr
->user_name
);
588 assert(is_signed
< 0);
589 return log_error_errno(is_signed
, "Failed to verify signature on user record for %s, refusing fixation: %m", hr
->user_name
);
593 static int convert_worker_errno(Home
*h
, int e
, sd_bus_error
*error
) {
594 /* Converts the error numbers the worker process returned into somewhat sensible dbus errors */
599 return sd_bus_error_set(error
, BUS_ERROR_BAD_HOME_SIZE
, "File systems of this type cannot be shrunk");
601 return sd_bus_error_set(error
, BUS_ERROR_BAD_HOME_SIZE
, "File systems of this type can only be shrunk offline");
603 return sd_bus_error_set(error
, BUS_ERROR_BAD_HOME_SIZE
, "File system size too small");
605 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "System does not support selected storage backend");
606 case -EPROTONOSUPPORT
:
607 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "System does not support selected file system");
609 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Operation not supported on storage backend");
610 case -ESOCKTNOSUPPORT
:
611 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Operation not supported on file system");
613 return sd_bus_error_setf(error
, BUS_ERROR_BAD_PASSWORD
, "Password for home %s is incorrect or not sufficient for authentication.", h
->user_name
);
615 return sd_bus_error_setf(error
, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN
, "Password for home %s is incorrect or not sufficient, and configured security token not found either.", h
->user_name
);
617 return sd_bus_error_setf(error
, BUS_ERROR_BAD_RECOVERY_KEY
, "Recovery key for home %s is incorrect or not sufficient for authentication.", h
->user_name
);
619 return sd_bus_error_set(error
, BUS_ERROR_TOKEN_PIN_NEEDED
, "PIN for security token required.");
621 return sd_bus_error_set(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
, "Security token requires protected authentication path.");
623 return sd_bus_error_set(error
, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED
, "Security token requires presence confirmation.");
625 return sd_bus_error_set(error
, BUS_ERROR_TOKEN_USER_VERIFICATION_NEEDED
, "Security token requires user verification.");
627 return sd_bus_error_set(error
, BUS_ERROR_TOKEN_ACTION_TIMEOUT
, "Token action timeout. (User was supposed to verify presence or similar, by interacting with the token, and didn't do that in time.)");
629 return sd_bus_error_set(error
, BUS_ERROR_TOKEN_PIN_LOCKED
, "PIN of security token locked.");
631 return sd_bus_error_set(error
, BUS_ERROR_TOKEN_BAD_PIN
, "Bad PIN of security token.");
633 return sd_bus_error_set(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
, "Bad PIN of security token, and only a few tries left.");
635 return sd_bus_error_set(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
, "Bad PIN of security token, and only one try left.");
637 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
);
639 return sd_bus_error_setf(error
, BUS_ERROR_HOME_NOT_ACTIVE
, "Home %s is currently not active", h
->user_name
);
641 return sd_bus_error_setf(error
, BUS_ERROR_NO_DISK_SPACE
, "Not enough disk space for home %s", h
->user_name
);
643 return sd_bus_error_setf(error
, BUS_ERROR_HOME_CANT_AUTHENTICATE
, "Home %s has no password or other authentication mechanism defined.", h
->user_name
);
645 return sd_bus_error_setf(error
, BUS_ERROR_HOME_IN_USE
, "Home %s is currently being used elsewhere.", h
->user_name
);
651 static void home_count_bad_authentication(Home
*h
, bool save
) {
656 r
= user_record_bad_authentication(h
->record
);
658 log_warning_errno(r
, "Failed to increase bad authentication counter, ignoring: %m");
663 r
= home_save_record(h
);
665 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
669 static void home_fixate_finish(Home
*h
, int ret
, UserRecord
*hr
) {
670 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
671 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
676 assert(IN_SET(h
->state
, HOME_FIXATING
, HOME_FIXATING_FOR_ACTIVATION
, HOME_FIXATING_FOR_ACQUIRE
));
678 secret
= TAKE_PTR(h
->secret
); /* Take possession */
682 (void) home_count_bad_authentication(h
, false);
684 (void) convert_worker_errno(h
, ret
, &error
);
685 r
= log_error_errno(ret
, "Fixation failed: %m");
689 r
= log_error_errno(SYNTHETIC_ERRNO(EIO
), "Did not receive user record from worker process, fixation failed.");
693 r
= home_verify_user_record(h
, hr
, &signed_locally
, &error
);
697 r
= home_set_record(h
, hr
);
699 log_error_errno(r
, "Failed to update home record: %m");
703 h
->signed_locally
= signed_locally
;
705 /* When we finished fixating (and don't follow-up with activation), let's count this as good authentication */
706 if (h
->state
== HOME_FIXATING
) {
707 r
= user_record_good_authentication(h
->record
);
709 log_warning_errno(r
, "Failed to increase good authentication counter, ignoring: %m");
712 r
= home_save_record(h
);
714 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
716 if (IN_SET(h
->state
, HOME_FIXATING_FOR_ACTIVATION
, HOME_FIXATING_FOR_ACQUIRE
)) {
718 r
= home_start_work(h
, "activate", h
->record
, secret
);
720 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, NULL
);
721 home_set_state(h
, _HOME_STATE_INVALID
);
723 home_set_state(h
, h
->state
== HOME_FIXATING_FOR_ACTIVATION
? HOME_ACTIVATING
: HOME_ACTIVATING_FOR_ACQUIRE
);
728 log_debug("Fixation of %s completed.", h
->user_name
);
730 h
->current_operation
= operation_result_unref(h
->current_operation
, 0, NULL
);
732 /* Reset the state to "invalid", which makes home_get_state() test if the image exists and returns
733 * HOME_ABSENT vs. HOME_INACTIVE as necessary. */
734 home_set_state(h
, _HOME_STATE_INVALID
);
735 (void) manager_schedule_rebalance(h
->manager
, /* immediately= */ false);
739 /* If fixation fails, we stay in unfixated state! */
740 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
741 home_set_state(h
, HOME_UNFIXATED
);
744 static void home_activate_finish(Home
*h
, int ret
, UserRecord
*hr
) {
745 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
749 assert(IN_SET(h
->state
, HOME_ACTIVATING
, HOME_ACTIVATING_FOR_ACQUIRE
));
753 home_count_bad_authentication(h
, true);
755 (void) convert_worker_errno(h
, ret
, &error
);
756 r
= log_error_errno(ret
, "Activation failed: %m");
763 r
= home_verify_user_record(h
, hr
, &signed_locally
, &error
);
767 r
= home_set_record(h
, hr
);
769 log_error_errno(r
, "Failed to update home record, ignoring: %m");
773 h
->signed_locally
= signed_locally
;
775 r
= user_record_good_authentication(h
->record
);
777 log_warning_errno(r
, "Failed to increase good authentication counter, ignoring: %m");
779 r
= home_save_record(h
);
781 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
784 log_debug("Activation of %s completed.", h
->user_name
);
788 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
789 home_set_state(h
, _HOME_STATE_INVALID
);
792 (void) manager_schedule_rebalance(h
->manager
, /* immediately= */ true);
795 static void home_deactivate_finish(Home
*h
, int ret
, UserRecord
*hr
) {
796 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
800 assert(h
->state
== HOME_DEACTIVATING
);
801 assert(!hr
); /* We don't expect a record on this operation */
804 (void) convert_worker_errno(h
, ret
, &error
);
805 r
= log_error_errno(ret
, "Deactivation of %s failed: %m", h
->user_name
);
809 log_debug("Deactivation of %s completed.", h
->user_name
);
813 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
814 home_set_state(h
, _HOME_STATE_INVALID
);
817 (void) manager_schedule_rebalance(h
->manager
, /* immediately= */ true);
820 static void home_remove_finish(Home
*h
, int ret
, UserRecord
*hr
) {
821 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
826 assert(h
->state
== HOME_REMOVING
);
827 assert(!hr
); /* We don't expect a record on this operation */
831 if (ret
< 0 && ret
!= -EALREADY
) {
832 (void) convert_worker_errno(h
, ret
, &error
);
833 r
= log_error_errno(ret
, "Removing %s failed: %m", h
->user_name
);
837 /* For a couple of storage types we can't delete the actual data storage when called (such as LUKS on
838 * partitions like USB sticks, or so). Sometimes these storage locations are among those we normally
839 * automatically discover in /home or in udev. When such a home is deleted let's hence issue a rescan
840 * after completion, so that "unfixated" entries are rediscovered. */
841 if (!IN_SET(user_record_test_image_path(h
->record
), USER_TEST_UNDEFINED
, USER_TEST_ABSENT
))
842 manager_enqueue_rescan(m
);
844 /* The image is now removed from disk. Now also remove our stored record */
845 r
= home_unlink_record(h
);
847 log_error_errno(r
, "Removing record file failed: %m");
851 log_debug("Removal of %s completed.", h
->user_name
);
852 h
->current_operation
= operation_result_unref(h
->current_operation
, 0, NULL
);
854 /* Unload this record from memory too now. */
857 (void) manager_schedule_rebalance(m
, /* immediately= */ true);
861 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
862 home_set_state(h
, _HOME_STATE_INVALID
);
865 static void home_create_finish(Home
*h
, int ret
, UserRecord
*hr
) {
869 assert(h
->state
== HOME_CREATING
);
872 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
874 (void) convert_worker_errno(h
, ret
, &error
);
875 log_error_errno(ret
, "Operation on %s failed: %m", h
->user_name
);
876 h
->current_operation
= operation_result_unref(h
->current_operation
, ret
, &error
);
878 if (h
->unregister_on_failure
) {
879 (void) home_unlink_record(h
);
884 home_set_state(h
, _HOME_STATE_INVALID
);
889 r
= home_set_record(h
, hr
);
891 log_warning_errno(r
, "Failed to update home record, ignoring: %m");
894 r
= home_save_record(h
);
896 log_warning_errno(r
, "Failed to save record to disk, ignoring: %m");
898 log_debug("Creation of %s completed.", h
->user_name
);
900 h
->current_operation
= operation_result_unref(h
->current_operation
, 0, NULL
);
901 home_set_state(h
, _HOME_STATE_INVALID
);
903 (void) manager_schedule_rebalance(h
->manager
, /* immediately= */ true);
906 static void home_change_finish(Home
*h
, int ret
, UserRecord
*hr
) {
907 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
914 (void) home_count_bad_authentication(h
, true);
916 (void) convert_worker_errno(h
, ret
, &error
);
917 r
= log_error_errno(ret
, "Change operation failed: %m");
922 r
= home_set_record(h
, hr
);
924 log_warning_errno(r
, "Failed to update home record, ignoring: %m");
926 r
= user_record_good_authentication(h
->record
);
928 log_warning_errno(r
, "Failed to increase good authentication counter, ignoring: %m");
930 r
= home_save_record(h
);
932 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
936 log_debug("Change operation of %s completed.", h
->user_name
);
937 (void) manager_schedule_rebalance(h
->manager
, /* immediately= */ false);
941 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
942 home_set_state(h
, _HOME_STATE_INVALID
);
945 static void home_locking_finish(Home
*h
, int ret
, UserRecord
*hr
) {
946 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
950 assert(h
->state
== HOME_LOCKING
);
953 (void) convert_worker_errno(h
, ret
, &error
);
954 r
= log_error_errno(ret
, "Locking operation failed: %m");
958 log_debug("Locking operation of %s completed.", h
->user_name
);
959 h
->current_operation
= operation_result_unref(h
->current_operation
, 0, NULL
);
960 home_set_state(h
, HOME_LOCKED
);
964 /* If a specific home doesn't know the concept of locking, then that's totally OK, don't propagate
965 * the error if we are executing a LockAllHomes() operation. */
967 if (h
->current_operation
->type
== OPERATION_LOCK_ALL
&& r
== -ENOTTY
)
968 h
->current_operation
= operation_result_unref(h
->current_operation
, 0, NULL
);
970 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
972 home_set_state(h
, _HOME_STATE_INVALID
);
975 static void home_unlocking_finish(Home
*h
, int ret
, UserRecord
*hr
) {
976 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
980 assert(IN_SET(h
->state
, HOME_UNLOCKING
, HOME_UNLOCKING_FOR_ACQUIRE
));
984 (void) home_count_bad_authentication(h
, true);
986 (void) convert_worker_errno(h
, ret
, &error
);
987 r
= log_error_errno(ret
, "Unlocking operation failed: %m");
989 /* Revert to locked state */
990 home_set_state(h
, HOME_LOCKED
);
991 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
995 r
= user_record_good_authentication(h
->record
);
997 log_warning_errno(r
, "Failed to increase good authentication counter, ignoring: %m");
999 r
= home_save_record(h
);
1001 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
1004 log_debug("Unlocking operation of %s completed.", h
->user_name
);
1006 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
1007 home_set_state(h
, _HOME_STATE_INVALID
);
1011 static void home_authenticating_finish(Home
*h
, int ret
, UserRecord
*hr
) {
1012 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1016 assert(IN_SET(h
->state
, HOME_AUTHENTICATING
, HOME_AUTHENTICATING_WHILE_ACTIVE
, HOME_AUTHENTICATING_FOR_ACQUIRE
));
1020 (void) home_count_bad_authentication(h
, true);
1022 (void) convert_worker_errno(h
, ret
, &error
);
1023 r
= log_error_errno(ret
, "Authentication failed: %m");
1028 r
= home_set_record(h
, hr
);
1030 log_warning_errno(r
, "Failed to update home record, ignoring: %m");
1032 r
= user_record_good_authentication(h
->record
);
1034 log_warning_errno(r
, "Failed to increase good authentication counter, ignoring: %m");
1036 r
= home_save_record(h
);
1038 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
1042 log_debug("Authentication of %s completed.", h
->user_name
);
1046 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
1047 home_set_state(h
, _HOME_STATE_INVALID
);
1050 static int home_on_worker_process(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
1051 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
1052 Home
*h
= ASSERT_PTR(userdata
);
1058 assert(h
->worker_pid
== si
->si_pid
);
1059 assert(h
->worker_event_source
);
1060 assert(h
->worker_stdout_fd
>= 0);
1062 (void) hashmap_remove_value(h
->manager
->homes_by_worker_pid
, PID_TO_PTR(h
->worker_pid
), h
);
1065 h
->worker_event_source
= sd_event_source_disable_unref(h
->worker_event_source
);
1067 if (si
->si_code
!= CLD_EXITED
) {
1068 assert(IN_SET(si
->si_code
, CLD_KILLED
, CLD_DUMPED
));
1069 ret
= log_debug_errno(SYNTHETIC_ERRNO(EPROTO
), "Worker process died abnormally with signal %s.", signal_to_string(si
->si_status
));
1070 } else if (si
->si_status
!= EXIT_SUCCESS
) {
1071 /* If we received an error code via sd_notify(), use it */
1072 if (h
->worker_error_code
!= 0)
1073 ret
= log_debug_errno(h
->worker_error_code
, "Worker reported error code %s.", errno_to_name(h
->worker_error_code
));
1075 ret
= log_debug_errno(SYNTHETIC_ERRNO(EPROTO
), "Worker exited with exit code %i.", si
->si_status
);
1077 ret
= home_parse_worker_stdout(TAKE_FD(h
->worker_stdout_fd
), &hr
);
1079 h
->worker_stdout_fd
= safe_close(h
->worker_stdout_fd
);
1084 case HOME_FIXATING_FOR_ACTIVATION
:
1085 case HOME_FIXATING_FOR_ACQUIRE
:
1086 home_fixate_finish(h
, ret
, hr
);
1089 case HOME_ACTIVATING
:
1090 case HOME_ACTIVATING_FOR_ACQUIRE
:
1091 home_activate_finish(h
, ret
, hr
);
1094 case HOME_DEACTIVATING
:
1095 home_deactivate_finish(h
, ret
, hr
);
1099 home_locking_finish(h
, ret
, hr
);
1102 case HOME_UNLOCKING
:
1103 case HOME_UNLOCKING_FOR_ACQUIRE
:
1104 home_unlocking_finish(h
, ret
, hr
);
1108 home_create_finish(h
, ret
, hr
);
1112 home_remove_finish(h
, ret
, hr
);
1116 case HOME_UPDATING_WHILE_ACTIVE
:
1118 case HOME_RESIZING_WHILE_ACTIVE
:
1120 case HOME_PASSWD_WHILE_ACTIVE
:
1121 home_change_finish(h
, ret
, hr
);
1124 case HOME_AUTHENTICATING
:
1125 case HOME_AUTHENTICATING_WHILE_ACTIVE
:
1126 case HOME_AUTHENTICATING_FOR_ACQUIRE
:
1127 home_authenticating_finish(h
, ret
, hr
);
1131 assert_not_reached();
1137 static int home_start_work(Home
*h
, const char *verb
, UserRecord
*hr
, UserRecord
*secret
) {
1138 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1139 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
1140 _cleanup_close_
int stdin_fd
= -EBADF
, stdout_fd
= -EBADF
;
1148 if (h
->worker_pid
!= 0)
1151 assert(h
->worker_stdout_fd
< 0);
1152 assert(!h
->worker_event_source
);
1154 v
= json_variant_ref(hr
->json
);
1157 JsonVariant
*sub
= NULL
;
1159 sub
= json_variant_by_key(secret
->json
, "secret");
1163 r
= json_variant_set_field(&v
, "secret", sub
);
1168 r
= json_variant_format(v
, 0, &formatted
);
1172 stdin_fd
= acquire_data_fd(formatted
, strlen(formatted
), 0);
1176 log_debug("Sending to worker: %s", formatted
);
1178 stdout_fd
= memfd_create("homework-stdout", MFD_CLOEXEC
);
1182 r
= safe_fork_full("(sd-homework)",
1183 (int[]) { stdin_fd
, stdout_fd
}, 2,
1184 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
|FORK_REOPEN_LOG
, &pid
);
1188 _cleanup_free_
char *joined
= NULL
;
1189 const char *homework
, *suffix
, *unix_path
;
1193 suffix
= getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
1195 joined
= strjoin("/run/systemd/home/notify.", suffix
);
1200 unix_path
= "/run/systemd/home/notify";
1202 if (setenv("NOTIFY_SOCKET", unix_path
, 1) < 0) {
1203 log_error_errno(errno
, "Failed to set $NOTIFY_SOCKET: %m");
1204 _exit(EXIT_FAILURE
);
1207 /* If we haven't locked the device yet, ask for a lock to be taken and be passed back to us via sd_notify(). */
1208 if (setenv("SYSTEMD_LUKS_LOCK", one_zero(h
->luks_lock_fd
< 0), 1) < 0) {
1209 log_error_errno(errno
, "Failed to set $SYSTEMD_LUKS_LOCK: %m");
1210 _exit(EXIT_FAILURE
);
1213 if (h
->manager
->default_storage
>= 0)
1214 if (setenv("SYSTEMD_HOME_DEFAULT_STORAGE", user_storage_to_string(h
->manager
->default_storage
), 1) < 0) {
1215 log_error_errno(errno
, "Failed to set $SYSTEMD_HOME_DEFAULT_STORAGE: %m");
1216 _exit(EXIT_FAILURE
);
1219 if (h
->manager
->default_file_system_type
)
1220 if (setenv("SYSTEMD_HOME_DEFAULT_FILE_SYSTEM_TYPE", h
->manager
->default_file_system_type
, 1) < 0) {
1221 log_error_errno(errno
, "Failed to set $SYSTEMD_HOME_DEFAULT_FILE_SYSTEM_TYPE: %m");
1222 _exit(EXIT_FAILURE
);
1225 r
= setenv_systemd_exec_pid(true);
1227 log_warning_errno(r
, "Failed to update $SYSTEMD_EXEC_PID, ignoring: %m");
1229 r
= rearrange_stdio(TAKE_FD(stdin_fd
), TAKE_FD(stdout_fd
), STDERR_FILENO
); /* fds are invalidated by rearrange_stdio() even on failure */
1231 log_error_errno(r
, "Failed to rearrange stdin/stdout/stderr: %m");
1232 _exit(EXIT_FAILURE
);
1236 /* Allow overriding the homework path via an environment variable, to make debugging
1238 homework
= getenv("SYSTEMD_HOMEWORK_PATH") ?: SYSTEMD_HOMEWORK_PATH
;
1240 execl(homework
, homework
, verb
, NULL
);
1241 log_error_errno(errno
, "Failed to invoke %s: %m", homework
);
1242 _exit(EXIT_FAILURE
);
1245 r
= sd_event_add_child(h
->manager
->event
, &h
->worker_event_source
, pid
, WEXITED
, home_on_worker_process
, h
);
1249 (void) sd_event_source_set_description(h
->worker_event_source
, "worker");
1251 r
= hashmap_put(h
->manager
->homes_by_worker_pid
, PID_TO_PTR(pid
), h
);
1253 h
->worker_event_source
= sd_event_source_disable_unref(h
->worker_event_source
);
1257 h
->worker_stdout_fd
= TAKE_FD(stdout_fd
);
1258 h
->worker_pid
= pid
;
1259 h
->worker_error_code
= 0;
1264 static int home_ratelimit(Home
*h
, sd_bus_error
*error
) {
1269 ret
= user_record_ratelimit(h
->record
);
1273 if (h
->state
!= HOME_UNFIXATED
) {
1274 r
= home_save_record(h
);
1276 log_warning_errno(r
, "Failed to save updated record, ignoring: %m");
1282 n
= now(CLOCK_REALTIME
);
1283 t
= user_record_ratelimit_next_try(h
->record
);
1285 if (t
!= USEC_INFINITY
&& t
> n
)
1286 return sd_bus_error_setf(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
,
1287 "Too many login attempts, please try again in %s!",
1288 FORMAT_TIMESPAN(t
- n
, USEC_PER_SEC
));
1290 return sd_bus_error_set(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
, "Too many login attempts, please try again later.");
1296 static int home_fixate_internal(
1299 HomeState for_state
,
1300 sd_bus_error
*error
) {
1305 assert(IN_SET(for_state
, HOME_FIXATING
, HOME_FIXATING_FOR_ACTIVATION
, HOME_FIXATING_FOR_ACQUIRE
));
1307 r
= home_start_work(h
, "inspect", h
->record
, secret
);
1311 if (IN_SET(for_state
, HOME_FIXATING_FOR_ACTIVATION
, HOME_FIXATING_FOR_ACQUIRE
)) {
1312 /* Remember the secret data, since we need it for the activation again, later on. */
1313 user_record_unref(h
->secret
);
1314 h
->secret
= user_record_ref(secret
);
1317 home_set_state(h
, for_state
);
1321 int home_fixate(Home
*h
, UserRecord
*secret
, sd_bus_error
*error
) {
1326 switch (home_get_state(h
)) {
1328 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1332 case HOME_LINGERING
:
1334 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ALREADY_FIXATED
, "Home %s is already fixated.", h
->user_name
);
1335 case HOME_UNFIXATED
:
1338 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1341 r
= home_ratelimit(h
, error
);
1345 return home_fixate_internal(h
, secret
, HOME_FIXATING
, error
);
1348 static int home_activate_internal(Home
*h
, UserRecord
*secret
, HomeState for_state
, sd_bus_error
*error
) {
1352 assert(IN_SET(for_state
, HOME_ACTIVATING
, HOME_ACTIVATING_FOR_ACQUIRE
));
1354 r
= home_start_work(h
, "activate", h
->record
, secret
);
1358 home_set_state(h
, for_state
);
1362 int home_activate(Home
*h
, UserRecord
*secret
, sd_bus_error
*error
) {
1367 switch (home_get_state(h
)) {
1368 case HOME_UNFIXATED
:
1369 return home_fixate_internal(h
, secret
, HOME_FIXATING_FOR_ACTIVATION
, error
);
1371 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1373 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ALREADY_ACTIVE
, "Home %s is already active.", h
->user_name
);
1374 case HOME_LINGERING
:
1375 /* If we are lingering, i.e. active but are supposed to be deactivated, then cancel this
1376 * timer if the user explicitly asks us to be active */
1377 h
->retry_deactivate_event_source
= sd_event_source_disable_unref(h
->retry_deactivate_event_source
);
1380 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1385 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1388 r
= home_ratelimit(h
, error
);
1392 return home_activate_internal(h
, secret
, HOME_ACTIVATING
, error
);
1395 static int home_authenticate_internal(Home
*h
, UserRecord
*secret
, HomeState for_state
, sd_bus_error
*error
) {
1399 assert(IN_SET(for_state
, HOME_AUTHENTICATING
, HOME_AUTHENTICATING_WHILE_ACTIVE
, HOME_AUTHENTICATING_FOR_ACQUIRE
));
1401 r
= home_start_work(h
, "inspect", h
->record
, secret
);
1405 home_set_state(h
, for_state
);
1409 int home_authenticate(Home
*h
, UserRecord
*secret
, sd_bus_error
*error
) {
1415 state
= home_get_state(h
);
1418 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1420 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1421 case HOME_UNFIXATED
:
1425 case HOME_LINGERING
:
1428 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1431 r
= home_ratelimit(h
, error
);
1435 return home_authenticate_internal(h
, secret
, HOME_STATE_IS_ACTIVE(state
) ? HOME_AUTHENTICATING_WHILE_ACTIVE
: HOME_AUTHENTICATING
, error
);
1438 static int home_deactivate_internal(Home
*h
, bool force
, sd_bus_error
*error
) {
1443 home_unpin(h
); /* unpin so that we can deactivate */
1445 r
= home_start_work(h
, force
? "deactivate-force" : "deactivate", h
->record
, NULL
);
1447 /* Operation failed before it even started, reacquire pin fd, if state still dictates so */
1448 home_update_pin_fd(h
, _HOME_STATE_INVALID
);
1450 home_set_state(h
, HOME_DEACTIVATING
);
1454 /* Let's start a timer to retry deactivation in 15. We'll stop the timer once we manage to deactivate
1455 * the home directory again, or we start any other operation. */
1456 home_start_retry_deactivate(h
);
1461 int home_deactivate(Home
*h
, bool force
, sd_bus_error
*error
) {
1464 switch (home_get_state(h
)) {
1465 case HOME_UNFIXATED
:
1469 return sd_bus_error_setf(error
, BUS_ERROR_HOME_NOT_ACTIVE
, "Home %s not active.", h
->user_name
);
1471 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1473 case HOME_LINGERING
:
1476 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1479 return home_deactivate_internal(h
, force
, error
);
1482 int home_create(Home
*h
, UserRecord
*secret
, sd_bus_error
*error
) {
1487 switch (home_get_state(h
)) {
1488 case HOME_INACTIVE
: {
1491 if (h
->record
->storage
< 0)
1492 break; /* if no storage is defined we don't know what precisely to look for, hence
1493 * HOME_INACTIVE is OK in that case too. */
1495 t
= user_record_test_image_path(h
->record
);
1496 if (IN_SET(t
, USER_TEST_MAYBE
, USER_TEST_UNDEFINED
))
1497 break; /* And if the image path test isn't conclusive, let's also go on */
1499 if (IN_SET(t
, -EBADF
, -ENOTDIR
))
1500 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
);
1502 return sd_bus_error_setf(error
, BUS_ERROR_HOME_EXISTS
, "Selected home image of user %s already exists.", h
->user_name
);
1504 case HOME_UNFIXATED
:
1506 return sd_bus_error_setf(error
, BUS_ERROR_HOME_EXISTS
, "Home of user %s already exists.", h
->user_name
);
1510 case HOME_LINGERING
:
1513 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
);
1516 if (h
->record
->enforce_password_policy
== false)
1517 log_debug("Password quality check turned off for account, skipping.");
1519 r
= user_record_quality_check_password(h
->record
, secret
, error
);
1524 r
= home_start_work(h
, "create", h
->record
, secret
);
1528 home_set_state(h
, HOME_CREATING
);
1532 int home_remove(Home
*h
, sd_bus_error
*error
) {
1538 state
= home_get_state(h
);
1540 case HOME_ABSENT
: /* If the home directory is absent, then this is just like unregistering */
1541 return home_unregister(h
, error
);
1543 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1544 case HOME_UNFIXATED
:
1549 case HOME_LINGERING
:
1551 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
);
1554 r
= home_start_work(h
, "remove", h
->record
, NULL
);
1558 home_set_state(h
, HOME_REMOVING
);
1562 static int user_record_extend_with_binding(UserRecord
*hr
, UserRecord
*with_binding
, UserRecordLoadFlags flags
, UserRecord
**ret
) {
1563 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1564 _cleanup_(user_record_unrefp
) UserRecord
*nr
= NULL
;
1565 JsonVariant
*binding
;
1569 assert(with_binding
);
1572 assert_se(v
= json_variant_ref(hr
->json
));
1574 binding
= json_variant_by_key(with_binding
->json
, "binding");
1576 r
= json_variant_set_field(&v
, "binding", binding
);
1581 nr
= user_record_new();
1585 r
= user_record_load(nr
, v
, flags
);
1589 *ret
= TAKE_PTR(nr
);
1593 static int home_update_internal(
1598 sd_bus_error
*error
) {
1600 _cleanup_(user_record_unrefp
) UserRecord
*new_hr
= NULL
, *saved_secret
= NULL
, *signed_hr
= NULL
;
1607 if (!user_record_compatible(hr
, h
->record
))
1608 return sd_bus_error_set(error
, BUS_ERROR_HOME_RECORD_MISMATCH
, "Updated user record is not compatible with existing one.");
1609 c
= user_record_compare_last_change(hr
, h
->record
); /* refuse downgrades */
1611 return sd_bus_error_set(error
, BUS_ERROR_HOME_RECORD_DOWNGRADE
, "Refusing to update to older home record.");
1613 if (!secret
&& FLAGS_SET(hr
->mask
, USER_RECORD_SECRET
)) {
1614 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SECRET
|USER_RECORD_PERMISSIVE
, &saved_secret
);
1618 secret
= saved_secret
;
1621 r
= manager_verify_user_record(h
->manager
, hr
);
1624 case USER_RECORD_UNSIGNED
:
1625 if (h
->signed_locally
<= 0) /* If the existing record is not owned by us, don't accept an
1626 * unsigned new record. i.e. only implicitly sign new records
1627 * that where previously signed by us too. */
1628 return sd_bus_error_setf(error
, BUS_ERROR_HOME_RECORD_SIGNED
, "Home %s is signed and cannot be modified locally.", h
->user_name
);
1630 /* The updated record is not signed, then do so now */
1631 r
= manager_sign_user_record(h
->manager
, hr
, &signed_hr
, error
);
1638 case USER_RECORD_SIGNED_EXCLUSIVE
:
1639 case USER_RECORD_SIGNED
:
1640 case USER_RECORD_FOREIGN
:
1641 /* Has already been signed. Great! */
1649 r
= user_record_extend_with_binding(hr
, h
->record
, USER_RECORD_LOAD_MASK_SECRET
|USER_RECORD_PERMISSIVE
, &new_hr
);
1654 /* different payload but same lastChangeUSec field? That's not cool! */
1656 r
= user_record_masked_equal(new_hr
, h
->record
, USER_RECORD_REGULAR
|USER_RECORD_PRIVILEGED
|USER_RECORD_PER_MACHINE
);
1660 return sd_bus_error_set(error
, BUS_ERROR_HOME_RECORD_MISMATCH
, "Home record different but timestamp remained the same, refusing.");
1663 r
= home_start_work(h
, verb
, new_hr
, secret
);
1670 int home_update(Home
*h
, UserRecord
*hr
, sd_bus_error
*error
) {
1677 state
= home_get_state(h
);
1679 case HOME_UNFIXATED
:
1680 return sd_bus_error_setf(error
, BUS_ERROR_HOME_UNFIXATED
, "Home %s has not been fixated yet.", h
->user_name
);
1682 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1684 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1688 case HOME_LINGERING
:
1691 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1694 r
= home_ratelimit(h
, error
);
1698 r
= home_update_internal(h
, "update", hr
, NULL
, error
);
1702 home_set_state(h
, HOME_STATE_IS_ACTIVE(state
) ? HOME_UPDATING_WHILE_ACTIVE
: HOME_UPDATING
);
1706 int home_resize(Home
*h
,
1710 sd_bus_error
*error
) {
1712 _cleanup_(user_record_unrefp
) UserRecord
*c
= NULL
;
1718 state
= home_get_state(h
);
1720 case HOME_UNFIXATED
:
1721 return sd_bus_error_setf(error
, BUS_ERROR_HOME_UNFIXATED
, "Home %s has not been fixated yet.", h
->user_name
);
1723 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1725 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1729 case HOME_LINGERING
:
1732 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1735 r
= home_ratelimit(h
, error
);
1739 /* If the user didn't specify any size explicitly and rebalancing is on, then the disk size is
1740 * determined by automatic rebalancing and hence not user configured but determined by us and thus
1741 * applied anyway. */
1742 if (disk_size
== UINT64_MAX
&& h
->record
->rebalance_weight
!= REBALANCE_WEIGHT_OFF
)
1743 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Disk size is being determined by automatic disk space rebalancing.");
1745 if (disk_size
== UINT64_MAX
|| disk_size
== h
->record
->disk_size
) {
1746 if (h
->record
->disk_size
== UINT64_MAX
)
1747 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "No disk size to resize to specified.");
1749 c
= user_record_ref(h
->record
); /* Shortcut if size is unspecified or matches the record */
1751 _cleanup_(user_record_unrefp
) UserRecord
*signed_c
= NULL
;
1753 if (h
->signed_locally
<= 0) /* Don't allow changing of records not signed only by us */
1754 return sd_bus_error_setf(error
, BUS_ERROR_HOME_RECORD_SIGNED
, "Home %s is signed and cannot be modified locally.", h
->user_name
);
1756 r
= user_record_clone(h
->record
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_PERMISSIVE
, &c
);
1760 r
= user_record_set_disk_size(c
, disk_size
);
1762 return sd_bus_error_setf(error
, BUS_ERROR_BAD_HOME_SIZE
, "Requested size for home %s out of acceptable range.", h
->user_name
);
1766 /* If user picked an explicit size, then turn off rebalancing, so that we don't undo what user chose */
1767 r
= user_record_set_rebalance_weight(c
, REBALANCE_WEIGHT_OFF
);
1771 r
= user_record_update_last_changed(c
, false);
1773 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
);
1777 r
= manager_sign_user_record(h
->manager
, c
, &signed_c
, error
);
1781 user_record_unref(c
);
1782 c
= TAKE_PTR(signed_c
);
1785 r
= home_update_internal(h
, automatic
? "resize-auto" : "resize", c
, secret
, error
);
1789 home_set_state(h
, HOME_STATE_IS_ACTIVE(state
) ? HOME_RESIZING_WHILE_ACTIVE
: HOME_RESIZING
);
1793 static int home_may_change_password(
1795 sd_bus_error
*error
) {
1801 r
= user_record_test_password_change_required(h
->record
);
1802 if (IN_SET(r
, -EKEYREVOKED
, -EOWNERDEAD
, -EKEYEXPIRED
, -ESTALE
))
1803 return 0; /* expired in some form, but changing is allowed */
1804 if (IN_SET(r
, -EKEYREJECTED
, -EROFS
))
1805 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Expiration settings of account %s do not allow changing of password.", h
->user_name
);
1807 return log_error_errno(r
, "Failed to test password expiry: %m");
1809 return 0; /* not expired */
1812 int home_passwd(Home
*h
,
1813 UserRecord
*new_secret
,
1814 UserRecord
*old_secret
,
1815 sd_bus_error
*error
) {
1817 _cleanup_(user_record_unrefp
) UserRecord
*c
= NULL
, *merged_secret
= NULL
, *signed_c
= NULL
;
1823 if (h
->signed_locally
<= 0) /* Don't allow changing of records not signed only by us */
1824 return sd_bus_error_setf(error
, BUS_ERROR_HOME_RECORD_SIGNED
, "Home %s is signed and cannot be modified locally.", h
->user_name
);
1826 state
= home_get_state(h
);
1828 case HOME_UNFIXATED
:
1829 return sd_bus_error_setf(error
, BUS_ERROR_HOME_UNFIXATED
, "Home %s has not been fixated yet.", h
->user_name
);
1831 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1833 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1837 case HOME_LINGERING
:
1840 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1843 r
= home_ratelimit(h
, error
);
1847 r
= home_may_change_password(h
, error
);
1851 r
= user_record_clone(h
->record
, USER_RECORD_LOAD_REFUSE_SECRET
|USER_RECORD_PERMISSIVE
, &c
);
1855 merged_secret
= user_record_new();
1859 r
= user_record_merge_secret(merged_secret
, old_secret
);
1863 r
= user_record_merge_secret(merged_secret
, new_secret
);
1867 if (!strv_isempty(new_secret
->password
)) {
1868 /* Update the password only if one is specified, otherwise let's just reuse the old password
1869 * data. This is useful as a way to propagate updated user records into the LUKS backends
1872 r
= user_record_make_hashed_password(c
, new_secret
->password
, /* extend = */ false);
1876 r
= user_record_set_password_change_now(c
, -1 /* remove */);
1881 r
= user_record_update_last_changed(c
, true);
1883 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
);
1887 r
= manager_sign_user_record(h
->manager
, c
, &signed_c
, error
);
1891 if (c
->enforce_password_policy
== false)
1892 log_debug("Password quality check turned off for account, skipping.");
1894 r
= user_record_quality_check_password(c
, merged_secret
, error
);
1899 r
= home_update_internal(h
, "passwd", signed_c
, merged_secret
, error
);
1903 home_set_state(h
, HOME_STATE_IS_ACTIVE(state
) ? HOME_PASSWD_WHILE_ACTIVE
: HOME_PASSWD
);
1907 int home_unregister(Home
*h
, sd_bus_error
*error
) {
1912 switch (home_get_state(h
)) {
1913 case HOME_UNFIXATED
:
1914 return sd_bus_error_setf(error
, BUS_ERROR_HOME_UNFIXATED
, "Home %s is not registered.", h
->user_name
);
1916 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1922 case HOME_LINGERING
:
1924 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
);
1927 r
= home_unlink_record(h
);
1931 /* And destroy the whole entry. The caller needs to be prepared for that. */
1936 int home_lock(Home
*h
, sd_bus_error
*error
) {
1941 switch (home_get_state(h
)) {
1942 case HOME_UNFIXATED
:
1946 return sd_bus_error_setf(error
, BUS_ERROR_HOME_NOT_ACTIVE
, "Home %s is not active.", h
->user_name
);
1948 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is already locked.", h
->user_name
);
1950 case HOME_LINGERING
:
1953 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1956 r
= home_start_work(h
, "lock", h
->record
, NULL
);
1960 home_set_state(h
, HOME_LOCKING
);
1964 static int home_unlock_internal(Home
*h
, UserRecord
*secret
, HomeState for_state
, sd_bus_error
*error
) {
1968 assert(IN_SET(for_state
, HOME_UNLOCKING
, HOME_UNLOCKING_FOR_ACQUIRE
));
1970 r
= home_start_work(h
, "unlock", h
->record
, secret
);
1974 home_set_state(h
, for_state
);
1978 int home_unlock(Home
*h
, UserRecord
*secret
, sd_bus_error
*error
) {
1982 r
= home_ratelimit(h
, error
);
1986 switch (home_get_state(h
)) {
1987 case HOME_UNFIXATED
:
1991 case HOME_LINGERING
:
1993 return sd_bus_error_setf(error
, BUS_ERROR_HOME_NOT_LOCKED
, "Home %s is not locked.", h
->user_name
);
1997 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
2000 return home_unlock_internal(h
, secret
, HOME_UNLOCKING
, error
);
2003 HomeState
home_get_state(Home
*h
) {
2007 /* When the state field is initialized, it counts. */
2011 /* Otherwise, let's see if the home directory is mounted. If so, we assume for sure the home
2012 * directory is active */
2013 if (user_record_test_home_directory(h
->record
) == USER_TEST_MOUNTED
)
2014 return h
->retry_deactivate_event_source
? HOME_LINGERING
: HOME_ACTIVE
;
2016 /* And if we see the image being gone, we report this as absent */
2017 r
= user_record_test_image_path(h
->record
);
2018 if (r
== USER_TEST_ABSENT
)
2020 if (r
== USER_TEST_DIRTY
)
2023 /* And for all other cases we return "inactive". */
2024 return HOME_INACTIVE
;
2027 void home_process_notify(Home
*h
, char **l
, int fd
) {
2028 _cleanup_close_
int taken_fd
= TAKE_FD(fd
);
2035 e
= strv_env_get(l
, "SYSTEMD_LUKS_LOCK_FD");
2037 r
= parse_boolean(e
);
2039 return (void) log_debug_errno(r
, "Failed to parse SYSTEMD_LUKS_LOCK_FD value: %m");
2042 return (void) log_debug("Got notify message with SYSTEMD_LUKS_LOCK_FD=1 but no fd passed, ignoring: %m");
2044 close_and_replace(h
->luks_lock_fd
, taken_fd
);
2046 log_debug("Successfully acquired LUKS lock fd from worker.");
2048 /* Immediately check if we actually want to keep it */
2049 home_maybe_close_luks_lock_fd(h
, _HOME_STATE_INVALID
);
2052 return (void) log_debug("Got notify message with SYSTEMD_LUKS_LOCK_FD=0 but fd passed, ignoring: %m");
2054 h
->luks_lock_fd
= safe_close(h
->luks_lock_fd
);
2060 e
= strv_env_get(l
, "ERRNO");
2062 return (void) log_debug("Got notify message lacking both ERRNO= and SYSTEMD_LUKS_LOCK_FD= field, ignoring.");
2064 r
= safe_atoi(e
, &error
);
2066 return (void) log_debug_errno(r
, "Failed to parse received error number, ignoring: %s", e
);
2068 return (void) log_debug("Error number is out of range: %i", error
);
2070 h
->worker_error_code
= error
;
2073 int home_killall(Home
*h
) {
2074 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2075 _cleanup_free_
char *unit
= NULL
;
2080 if (!uid_is_valid(h
->uid
))
2083 assert(h
->uid
> 0); /* We never should be UID 0 */
2085 /* Let's kill everything matching the specified UID */
2086 r
= safe_fork("(sd-killer)",
2087 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_WAIT
|FORK_LOG
|FORK_REOPEN_LOG
,
2096 gid
= user_record_gid(h
->record
);
2097 if (setresgid(gid
, gid
, gid
) < 0) {
2098 log_error_errno(errno
, "Failed to change GID to " GID_FMT
": %m", gid
);
2099 _exit(EXIT_FAILURE
);
2102 if (setgroups(0, NULL
) < 0) {
2103 log_error_errno(errno
, "Failed to reset auxiliary groups list: %m");
2104 _exit(EXIT_FAILURE
);
2107 if (setresuid(h
->uid
, h
->uid
, h
->uid
) < 0) {
2108 log_error_errno(errno
, "Failed to change UID to " UID_FMT
": %m", h
->uid
);
2109 _exit(EXIT_FAILURE
);
2112 if (kill(-1, SIGKILL
) < 0) {
2113 log_error_errno(errno
, "Failed to kill all processes of UID " UID_FMT
": %m", h
->uid
);
2114 _exit(EXIT_FAILURE
);
2117 _exit(EXIT_SUCCESS
);
2120 /* Let's also kill everything in the user's slice */
2121 if (asprintf(&unit
, "user-" UID_FMT
".slice", h
->uid
) < 0)
2124 r
= sd_bus_call_method(
2126 "org.freedesktop.systemd1",
2127 "/org/freedesktop/systemd1",
2128 "org.freedesktop.systemd1.Manager",
2132 "ssi", unit
, "all", SIGKILL
);
2134 log_full_errno(sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ? LOG_DEBUG
: LOG_WARNING
,
2135 r
, "Failed to kill login processes of user, ignoring: %s", bus_error_message(&error
, r
));
2140 static int home_get_disk_status_luks(
2143 uint64_t *ret_disk_size
,
2144 uint64_t *ret_disk_usage
,
2145 uint64_t *ret_disk_free
,
2146 uint64_t *ret_disk_ceiling
,
2147 uint64_t *ret_disk_floor
,
2148 statfs_f_type_t
*ret_fstype
,
2149 mode_t
*ret_access_mode
) {
2151 uint64_t disk_size
= UINT64_MAX
, disk_usage
= UINT64_MAX
, disk_free
= UINT64_MAX
,
2152 disk_ceiling
= UINT64_MAX
, disk_floor
= UINT64_MAX
,
2153 stat_used
= UINT64_MAX
, fs_size
= UINT64_MAX
, header_size
= 0;
2154 mode_t access_mode
= MODE_INVALID
;
2155 statfs_f_type_t fstype
= 0;
2163 if (state
!= HOME_ABSENT
) {
2166 ip
= user_record_image_path(h
->record
);
2168 if (stat(ip
, &st
) < 0)
2169 log_debug_errno(errno
, "Failed to stat() %s, ignoring: %m", ip
);
2170 else if (S_ISREG(st
.st_mode
)) {
2171 _cleanup_free_
char *parent
= NULL
;
2173 disk_size
= st
.st_size
;
2174 stat_used
= st
.st_blocks
* 512;
2176 r
= path_extract_directory(ip
, &parent
);
2178 return log_error_errno(r
, "Failed to extract parent directory from image path '%s': %m", ip
);
2180 if (statfs(parent
, &sfs
) < 0)
2181 log_debug_errno(errno
, "Failed to statfs() %s, ignoring: %m", parent
);
2183 disk_ceiling
= stat_used
+ sfs
.f_bsize
* sfs
.f_bavail
;
2185 } else if (S_ISBLK(st
.st_mode
)) {
2186 _cleanup_free_
char *szbuf
= NULL
;
2187 char p
[SYS_BLOCK_PATH_MAX("/size")];
2189 /* Let's read the size off sysfs, so that we don't have to open the device */
2190 xsprintf_sys_block_path(p
, "/size", st
.st_rdev
);
2191 r
= read_one_line_file(p
, &szbuf
);
2193 log_debug_errno(r
, "Failed to read %s, ignoring: %m", p
);
2197 r
= safe_atou64(szbuf
, &sz
);
2199 log_debug_errno(r
, "Failed to parse %s, ignoring: %s", p
, szbuf
);
2201 disk_size
= sz
* 512;
2204 log_debug("Image path is not a block device or regular file, not able to acquire size.");
2208 if (!HOME_STATE_IS_ACTIVE(state
))
2211 hd
= user_record_home_directory(h
->record
);
2215 if (stat(hd
, &st
) < 0) {
2216 log_debug_errno(errno
, "Failed to stat() %s, ignoring: %m", hd
);
2220 r
= stat_verify_directory(&st
);
2222 log_debug_errno(r
, "Home directory %s is not a directory, ignoring: %m", hd
);
2226 access_mode
= st
.st_mode
& 07777;
2228 if (statfs(hd
, &sfs
) < 0) {
2229 log_debug_errno(errno
, "Failed to statfs() %s, ignoring: %m", hd
);
2233 fstype
= sfs
.f_type
;
2235 disk_free
= sfs
.f_bsize
* sfs
.f_bavail
;
2236 fs_size
= sfs
.f_bsize
* sfs
.f_blocks
;
2237 if (disk_size
!= UINT64_MAX
&& disk_size
> fs_size
)
2238 header_size
= disk_size
- fs_size
;
2240 /* We take a perspective from the user here (as opposed to from the host): the used disk space is the
2241 * difference from the limit and what's free. This makes a difference if sparse mode is not used: in
2242 * that case the image is pre-allocated and thus appears all used from the host PoV but is not used
2243 * up at all yet from the user's PoV.
2245 * That said, we use use the stat() reported loopback file size as upper boundary: our footprint can
2246 * never be larger than what we take up on the lowest layers. */
2248 if (disk_size
!= UINT64_MAX
&& disk_size
> disk_free
) {
2249 disk_usage
= disk_size
- disk_free
;
2251 if (stat_used
!= UINT64_MAX
&& disk_usage
> stat_used
)
2252 disk_usage
= stat_used
;
2254 disk_usage
= stat_used
;
2256 /* If we have the magic, determine floor preferably by magic */
2257 disk_floor
= minimal_size_by_fs_magic(sfs
.f_type
) + header_size
;
2260 /* If we don't know the magic, go by file system name */
2261 if (disk_floor
== UINT64_MAX
)
2262 disk_floor
= minimal_size_by_fs_name(user_record_file_system_type(h
->record
));
2265 *ret_disk_size
= disk_size
;
2267 *ret_disk_usage
= disk_usage
;
2269 *ret_disk_free
= disk_free
;
2270 if (ret_disk_ceiling
)
2271 *ret_disk_ceiling
= disk_ceiling
;
2273 *ret_disk_floor
= disk_floor
;
2275 *ret_fstype
= fstype
;
2276 if (ret_access_mode
)
2277 *ret_access_mode
= access_mode
;
2282 static int home_get_disk_status_directory(
2285 uint64_t *ret_disk_size
,
2286 uint64_t *ret_disk_usage
,
2287 uint64_t *ret_disk_free
,
2288 uint64_t *ret_disk_ceiling
,
2289 uint64_t *ret_disk_floor
,
2290 statfs_f_type_t
*ret_fstype
,
2291 mode_t
*ret_access_mode
) {
2293 uint64_t disk_size
= UINT64_MAX
, disk_usage
= UINT64_MAX
, disk_free
= UINT64_MAX
,
2294 disk_ceiling
= UINT64_MAX
, disk_floor
= UINT64_MAX
;
2295 mode_t access_mode
= MODE_INVALID
;
2296 statfs_f_type_t fstype
= 0;
2299 const char *path
= NULL
;
2304 if (HOME_STATE_IS_ACTIVE(state
))
2305 path
= user_record_home_directory(h
->record
);
2308 if (state
== HOME_ABSENT
)
2311 path
= user_record_image_path(h
->record
);
2317 if (statfs(path
, &sfs
) < 0)
2318 log_debug_errno(errno
, "Failed to statfs() %s, ignoring: %m", path
);
2320 disk_free
= sfs
.f_bsize
* sfs
.f_bavail
;
2321 disk_size
= sfs
.f_bsize
* sfs
.f_blocks
;
2323 /* We don't initialize disk_usage from statfs() data here, since the device is likely not used
2324 * by us alone, and disk_usage should only reflect our own use. */
2326 fstype
= sfs
.f_type
;
2329 if (IN_SET(h
->record
->storage
, USER_CLASSIC
, USER_DIRECTORY
, USER_SUBVOLUME
)) {
2331 r
= btrfs_is_subvol(path
);
2333 log_debug_errno(r
, "Failed to determine whether %s is a btrfs subvolume: %m", path
);
2337 r
= btrfs_subvol_get_subtree_quota(path
, 0, &qi
);
2339 log_debug_errno(r
, "Failed to query btrfs subtree quota, ignoring: %m");
2341 disk_usage
= qi
.referenced
;
2343 if (disk_free
!= UINT64_MAX
) {
2344 disk_ceiling
= qi
.referenced
+ disk_free
;
2346 if (disk_size
!= UINT64_MAX
&& disk_ceiling
> disk_size
)
2347 disk_ceiling
= disk_size
;
2350 if (qi
.referenced_max
!= UINT64_MAX
) {
2351 if (disk_size
!= UINT64_MAX
)
2352 disk_size
= MIN(qi
.referenced_max
, disk_size
);
2354 disk_size
= qi
.referenced_max
;
2357 if (disk_size
!= UINT64_MAX
) {
2358 if (disk_size
> disk_usage
)
2359 disk_free
= disk_size
- disk_usage
;
2369 if (IN_SET(h
->record
->storage
, USER_CLASSIC
, USER_DIRECTORY
, USER_FSCRYPT
)) {
2370 r
= quotactl_path(QCMD_FIXED(Q_GETQUOTA
, USRQUOTA
), path
, h
->uid
, &req
);
2372 if (ERRNO_IS_NOT_SUPPORTED(r
)) {
2373 log_debug_errno(r
, "No UID quota support on %s.", path
);
2378 log_debug_errno(r
, "Failed to query disk quota for UID " UID_FMT
": %m", h
->uid
);
2382 disk_usage
= 0; /* No record of this user? then nothing was used */
2384 if (FLAGS_SET(req
.dqb_valid
, QIF_SPACE
) && disk_free
!= UINT64_MAX
) {
2385 disk_ceiling
= req
.dqb_curspace
+ disk_free
;
2387 if (disk_size
!= UINT64_MAX
&& disk_ceiling
> disk_size
)
2388 disk_ceiling
= disk_size
;
2391 if (FLAGS_SET(req
.dqb_valid
, QIF_BLIMITS
)) {
2394 /* Take the minimum of the quota and the available disk space here */
2395 q
= req
.dqb_bhardlimit
* QIF_DQBLKSIZE
;
2396 if (disk_size
!= UINT64_MAX
)
2397 disk_size
= MIN(disk_size
, q
);
2401 if (FLAGS_SET(req
.dqb_valid
, QIF_SPACE
)) {
2402 disk_usage
= req
.dqb_curspace
;
2404 if (disk_size
!= UINT64_MAX
) {
2405 if (disk_size
> disk_usage
)
2406 disk_free
= disk_size
- disk_usage
;
2416 *ret_disk_size
= disk_size
;
2418 *ret_disk_usage
= disk_usage
;
2420 *ret_disk_free
= disk_free
;
2421 if (ret_disk_ceiling
)
2422 *ret_disk_ceiling
= disk_ceiling
;
2424 *ret_disk_floor
= disk_floor
;
2426 *ret_fstype
= fstype
;
2427 if (ret_access_mode
)
2428 *ret_access_mode
= access_mode
;
2433 static int home_get_disk_status_internal(
2436 uint64_t *ret_disk_size
,
2437 uint64_t *ret_disk_usage
,
2438 uint64_t *ret_disk_free
,
2439 uint64_t *ret_disk_ceiling
,
2440 uint64_t *ret_disk_floor
,
2441 statfs_f_type_t
*ret_fstype
,
2442 mode_t
*ret_access_mode
) {
2447 switch (h
->record
->storage
) {
2450 return home_get_disk_status_luks(h
, state
, ret_disk_size
, ret_disk_usage
, ret_disk_free
, ret_disk_ceiling
, ret_disk_floor
, ret_fstype
, ret_access_mode
);
2453 case USER_DIRECTORY
:
2454 case USER_SUBVOLUME
:
2457 return home_get_disk_status_directory(h
, state
, ret_disk_size
, ret_disk_usage
, ret_disk_free
, ret_disk_ceiling
, ret_disk_floor
, ret_fstype
, ret_access_mode
);
2463 *ret_disk_size
= UINT64_MAX
;
2465 *ret_disk_usage
= UINT64_MAX
;
2467 *ret_disk_free
= UINT64_MAX
;
2468 if (ret_disk_ceiling
)
2469 *ret_disk_ceiling
= UINT64_MAX
;
2471 *ret_disk_floor
= UINT64_MAX
;
2474 if (ret_access_mode
)
2475 *ret_access_mode
= MODE_INVALID
;
2481 int home_get_disk_status(
2483 uint64_t *ret_disk_size
,
2484 uint64_t *ret_disk_usage
,
2485 uint64_t *ret_disk_free
,
2486 uint64_t *ret_disk_ceiling
,
2487 uint64_t *ret_disk_floor
,
2488 statfs_f_type_t
*ret_fstype
,
2489 mode_t
*ret_access_mode
) {
2493 return home_get_disk_status_internal(
2505 int home_augment_status(
2507 UserRecordLoadFlags flags
,
2510 uint64_t disk_size
= UINT64_MAX
, disk_usage
= UINT64_MAX
, disk_free
= UINT64_MAX
, disk_ceiling
= UINT64_MAX
, disk_floor
= UINT64_MAX
;
2511 _cleanup_(json_variant_unrefp
) JsonVariant
*j
= NULL
, *v
= NULL
, *m
= NULL
, *status
= NULL
;
2512 _cleanup_(user_record_unrefp
) UserRecord
*ur
= NULL
;
2513 statfs_f_type_t magic
;
2523 /* We are supposed to add this, this can't be on hence. */
2524 assert(!FLAGS_SET(flags
, USER_RECORD_STRIP_STATUS
));
2526 r
= sd_id128_get_machine(&id
);
2530 state
= home_get_state(h
);
2532 r
= home_get_disk_status_internal(
2544 fstype
= fs_type_to_string(magic
);
2546 if (disk_floor
== UINT64_MAX
|| (disk_usage
!= UINT64_MAX
&& disk_floor
< disk_usage
))
2547 disk_floor
= disk_usage
;
2548 if (disk_floor
== UINT64_MAX
|| disk_floor
< USER_DISK_SIZE_MIN
)
2549 disk_floor
= USER_DISK_SIZE_MIN
;
2550 if (disk_ceiling
== UINT64_MAX
|| disk_ceiling
> USER_DISK_SIZE_MAX
)
2551 disk_ceiling
= USER_DISK_SIZE_MAX
;
2553 r
= json_build(&status
,
2555 JSON_BUILD_PAIR("state", JSON_BUILD_STRING(home_state_to_string(state
))),
2556 JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.Home")),
2557 JSON_BUILD_PAIR_CONDITION(disk_size
!= UINT64_MAX
, "diskSize", JSON_BUILD_UNSIGNED(disk_size
)),
2558 JSON_BUILD_PAIR_CONDITION(disk_usage
!= UINT64_MAX
, "diskUsage", JSON_BUILD_UNSIGNED(disk_usage
)),
2559 JSON_BUILD_PAIR_CONDITION(disk_free
!= UINT64_MAX
, "diskFree", JSON_BUILD_UNSIGNED(disk_free
)),
2560 JSON_BUILD_PAIR_CONDITION(disk_ceiling
!= UINT64_MAX
, "diskCeiling", JSON_BUILD_UNSIGNED(disk_ceiling
)),
2561 JSON_BUILD_PAIR_CONDITION(disk_floor
!= UINT64_MAX
, "diskFloor", JSON_BUILD_UNSIGNED(disk_floor
)),
2562 JSON_BUILD_PAIR_CONDITION(h
->signed_locally
>= 0, "signedLocally", JSON_BUILD_BOOLEAN(h
->signed_locally
)),
2563 JSON_BUILD_PAIR_CONDITION(fstype
, "fileSystemType", JSON_BUILD_STRING(fstype
)),
2564 JSON_BUILD_PAIR_CONDITION(access_mode
!= MODE_INVALID
, "accessMode", JSON_BUILD_UNSIGNED(access_mode
))
2569 j
= json_variant_ref(h
->record
->json
);
2570 v
= json_variant_ref(json_variant_by_key(j
, "status"));
2571 m
= json_variant_ref(json_variant_by_key(v
, SD_ID128_TO_STRING(id
)));
2573 r
= json_variant_filter(&m
, STRV_MAKE("diskSize", "diskUsage", "diskFree", "diskCeiling", "diskFloor", "signedLocally"));
2577 r
= json_variant_merge(&m
, status
);
2581 r
= json_variant_set_field(&v
, SD_ID128_TO_STRING(id
), m
);
2585 r
= json_variant_set_field(&j
, "status", v
);
2589 ur
= user_record_new();
2593 r
= user_record_load(ur
, j
, flags
);
2598 FLAGS_SET(h
->record
->mask
, USER_RECORD_PRIVILEGED
) &&
2599 !FLAGS_SET(ur
->mask
, USER_RECORD_PRIVILEGED
);
2601 *ret
= TAKE_PTR(ur
);
2605 static int on_home_ref_eof(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
2606 _cleanup_(operation_unrefp
) Operation
*o
= NULL
;
2607 Home
*h
= ASSERT_PTR(userdata
);
2611 if (h
->ref_event_source_please_suspend
== s
)
2612 h
->ref_event_source_please_suspend
= sd_event_source_disable_unref(h
->ref_event_source_please_suspend
);
2614 if (h
->ref_event_source_dont_suspend
== s
)
2615 h
->ref_event_source_dont_suspend
= sd_event_source_disable_unref(h
->ref_event_source_dont_suspend
);
2617 if (h
->ref_event_source_dont_suspend
|| h
->ref_event_source_please_suspend
)
2620 log_info("Got notification that all sessions of user %s ended, deactivating automatically.", h
->user_name
);
2622 o
= operation_new(OPERATION_PIPE_EOF
, NULL
);
2628 home_schedule_operation(h
, o
, NULL
);
2632 int home_create_fifo(Home
*h
, bool please_suspend
) {
2633 _cleanup_close_
int ret_fd
= -EBADF
;
2634 sd_event_source
**ss
;
2635 const char *fn
, *suffix
;
2640 if (please_suspend
) {
2641 suffix
= ".please-suspend";
2642 ss
= &h
->ref_event_source_please_suspend
;
2644 suffix
= ".dont-suspend";
2645 ss
= &h
->ref_event_source_dont_suspend
;
2648 fn
= strjoina("/run/systemd/home/", h
->user_name
, suffix
);
2651 _cleanup_close_
int ref_fd
= -EBADF
;
2653 (void) mkdir("/run/systemd/home/", 0755);
2654 if (mkfifo(fn
, 0600) < 0 && errno
!= EEXIST
)
2655 return log_error_errno(errno
, "Failed to create FIFO %s: %m", fn
);
2657 ref_fd
= open(fn
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
2659 return log_error_errno(errno
, "Failed to open FIFO %s for reading: %m", fn
);
2661 r
= sd_event_add_io(h
->manager
->event
, ss
, ref_fd
, 0, on_home_ref_eof
, h
);
2663 return log_error_errno(r
, "Failed to allocate reference FIFO event source: %m");
2665 (void) sd_event_source_set_description(*ss
, "acquire-ref");
2667 r
= sd_event_source_set_priority(*ss
, SD_EVENT_PRIORITY_IDLE
-1);
2671 r
= sd_event_source_set_io_fd_own(*ss
, true);
2673 return log_error_errno(r
, "Failed to pass ownership of FIFO event fd to event source: %m");
2678 ret_fd
= open(fn
, O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
);
2680 return log_error_errno(errno
, "Failed to open FIFO %s for writing: %m", fn
);
2682 return TAKE_FD(ret_fd
);
2685 static int home_dispatch_acquire(Home
*h
, Operation
*o
) {
2686 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2687 int (*call
)(Home
*h
, UserRecord
*secret
, HomeState for_state
, sd_bus_error
*error
) = NULL
;
2688 HomeState for_state
;
2693 assert(o
->type
== OPERATION_ACQUIRE
);
2695 assert(!h
->current_operation
);
2697 switch (home_get_state(h
)) {
2699 case HOME_UNFIXATED
:
2700 for_state
= HOME_FIXATING_FOR_ACQUIRE
;
2701 call
= home_fixate_internal
;
2705 r
= sd_bus_error_setf(&error
, BUS_ERROR_HOME_ABSENT
,
2706 "Home %s is currently missing or not plugged in.", h
->user_name
);
2711 for_state
= HOME_ACTIVATING_FOR_ACQUIRE
;
2712 call
= home_activate_internal
;
2716 case HOME_LINGERING
:
2717 for_state
= HOME_AUTHENTICATING_FOR_ACQUIRE
;
2718 call
= home_authenticate_internal
;
2722 for_state
= HOME_UNLOCKING_FOR_ACQUIRE
;
2723 call
= home_unlock_internal
;
2727 /* All other cases means we are currently executing an operation, which means the job remains
2732 r
= home_ratelimit(h
, &error
);
2734 r
= call(h
, o
->secret
, for_state
, &error
);
2737 if (r
!= 0) /* failure or completed */
2738 operation_result(o
, r
, &error
);
2740 h
->current_operation
= operation_ref(o
);
2745 static int home_dispatch_release(Home
*h
, Operation
*o
) {
2746 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2751 assert(o
->type
== OPERATION_RELEASE
);
2753 if (h
->ref_event_source_dont_suspend
|| h
->ref_event_source_please_suspend
)
2754 /* If there's now a reference again, then let's abort the release attempt */
2755 r
= sd_bus_error_setf(&error
, BUS_ERROR_HOME_BUSY
, "Home %s is currently referenced.", h
->user_name
);
2757 switch (home_get_state(h
)) {
2759 case HOME_UNFIXATED
:
2767 r
= sd_bus_error_setf(&error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
2771 case HOME_LINGERING
:
2772 r
= home_deactivate_internal(h
, false, &error
);
2776 /* All other cases means we are currently executing an operation, which means the job remains
2782 assert(!h
->current_operation
);
2784 if (r
!= 0) /* failure or completed */
2785 operation_result(o
, r
, &error
);
2787 h
->current_operation
= operation_ref(o
);
2792 static int home_dispatch_lock_all(Home
*h
, Operation
*o
) {
2793 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2798 assert(o
->type
== OPERATION_LOCK_ALL
);
2800 switch (home_get_state(h
)) {
2802 case HOME_UNFIXATED
:
2806 log_info("Home %s is not active, no locking necessary.", h
->user_name
);
2811 log_info("Home %s is already locked.", h
->user_name
);
2816 case HOME_LINGERING
:
2817 log_info("Locking home %s.", h
->user_name
);
2818 r
= home_lock(h
, &error
);
2822 /* All other cases means we are currently executing an operation, which means the job remains
2827 assert(!h
->current_operation
);
2829 if (r
!= 0) /* failure or completed */
2830 operation_result(o
, r
, &error
);
2832 h
->current_operation
= operation_ref(o
);
2837 static int home_dispatch_deactivate_all(Home
*h
, Operation
*o
) {
2838 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2843 assert(o
->type
== OPERATION_DEACTIVATE_ALL
);
2845 switch (home_get_state(h
)) {
2847 case HOME_UNFIXATED
:
2851 log_info("Home %s is already deactivated.", h
->user_name
);
2856 log_info("Home %s is currently locked, not deactivating.", h
->user_name
);
2861 case HOME_LINGERING
:
2862 log_info("Deactivating home %s.", h
->user_name
);
2863 r
= home_deactivate_internal(h
, false, &error
);
2867 /* All other cases means we are currently executing an operation, which means the job remains
2872 assert(!h
->current_operation
);
2874 if (r
!= 0) /* failure or completed */
2875 operation_result(o
, r
, &error
);
2877 h
->current_operation
= operation_ref(o
);
2882 static int home_dispatch_pipe_eof(Home
*h
, Operation
*o
) {
2883 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2888 assert(o
->type
== OPERATION_PIPE_EOF
);
2890 if (h
->ref_event_source_please_suspend
|| h
->ref_event_source_dont_suspend
)
2891 return 1; /* Hmm, there's a reference again, let's cancel this */
2893 switch (home_get_state(h
)) {
2895 case HOME_UNFIXATED
:
2899 log_info("Home %s already deactivated, no automatic deactivation needed.", h
->user_name
);
2902 case HOME_DEACTIVATING
:
2903 log_info("Home %s is already being deactivated, automatic deactivated unnecessary.", h
->user_name
);
2907 case HOME_LINGERING
:
2908 r
= home_deactivate_internal(h
, false, &error
);
2910 log_warning_errno(r
, "Failed to deactivate %s, ignoring: %s", h
->user_name
, bus_error_message(&error
, r
));
2915 /* If the device is locked or any operation is being executed, let's leave this pending */
2919 /* Note that we don't call operation_fail() or operation_success() here, because this kind of
2920 * operation has no message associated with it, and thus there's no need to propagate success. */
2922 assert(!o
->message
);
2926 static int home_dispatch_deactivate_force(Home
*h
, Operation
*o
) {
2927 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2932 assert(o
->type
== OPERATION_DEACTIVATE_FORCE
);
2934 switch (home_get_state(h
)) {
2936 case HOME_UNFIXATED
:
2940 log_debug("Home %s already deactivated, no forced deactivation due to unplug needed.", h
->user_name
);
2943 case HOME_DEACTIVATING
:
2944 log_debug("Home %s is already being deactivated, forced deactivation due to unplug unnecessary.", h
->user_name
);
2949 case HOME_LINGERING
:
2950 r
= home_deactivate_internal(h
, true, &error
);
2952 log_warning_errno(r
, "Failed to forcibly deactivate %s, ignoring: %s", h
->user_name
, bus_error_message(&error
, r
));
2956 /* If any operation is being executed, let's leave this pending */
2960 /* Note that we don't call operation_fail() or operation_success() here, because this kind of
2961 * operation has no message associated with it, and thus there's no need to propagate success. */
2963 assert(!o
->message
);
2967 static int on_pending(sd_event_source
*s
, void *userdata
) {
2968 Home
*h
= ASSERT_PTR(userdata
);
2974 o
= ordered_set_first(h
->pending_operations
);
2976 static int (* const operation_table
[_OPERATION_MAX
])(Home
*h
, Operation
*o
) = {
2977 [OPERATION_ACQUIRE
] = home_dispatch_acquire
,
2978 [OPERATION_RELEASE
] = home_dispatch_release
,
2979 [OPERATION_LOCK_ALL
] = home_dispatch_lock_all
,
2980 [OPERATION_DEACTIVATE_ALL
] = home_dispatch_deactivate_all
,
2981 [OPERATION_PIPE_EOF
] = home_dispatch_pipe_eof
,
2982 [OPERATION_DEACTIVATE_FORCE
] = home_dispatch_deactivate_force
,
2985 assert(operation_table
[o
->type
]);
2986 r
= operation_table
[o
->type
](h
, o
);
2988 /* The operation completed, let's remove it from the pending list, and exit while
2989 * leaving the event source enabled as it is. */
2990 assert_se(ordered_set_remove(h
->pending_operations
, o
) == o
);
2996 /* Nothing to do anymore, let's turn off this event source */
2997 r
= sd_event_source_set_enabled(s
, SD_EVENT_OFF
);
2999 return log_error_errno(r
, "Failed to disable event source: %m");
3001 /* No operations pending anymore, maybe this is a good time to trigger a rebalancing */
3002 manager_reschedule_rebalance(h
->manager
);
3006 int home_schedule_operation(Home
*h
, Operation
*o
, sd_bus_error
*error
) {
3012 if (ordered_set_size(h
->pending_operations
) >= PENDING_OPERATIONS_MAX
)
3013 return sd_bus_error_set(error
, BUS_ERROR_TOO_MANY_OPERATIONS
, "Too many client operations requested");
3015 r
= ordered_set_ensure_put(&h
->pending_operations
, &operation_hash_ops
, o
);
3022 if (!h
->pending_event_source
) {
3023 r
= sd_event_add_defer(h
->manager
->event
, &h
->pending_event_source
, on_pending
, h
);
3025 return log_error_errno(r
, "Failed to allocate pending defer event source: %m");
3027 (void) sd_event_source_set_description(h
->pending_event_source
, "pending");
3029 r
= sd_event_source_set_priority(h
->pending_event_source
, SD_EVENT_PRIORITY_IDLE
);
3034 r
= sd_event_source_set_enabled(h
->pending_event_source
, SD_EVENT_ON
);
3036 return log_error_errno(r
, "Failed to trigger pending event source: %m");
3041 static int home_get_image_path_seat(Home
*h
, char **ret
) {
3042 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
3043 _cleanup_free_
char *c
= NULL
;
3044 const char *ip
, *seat
;
3050 if (user_record_storage(h
->record
) != USER_LUKS
)
3053 ip
= user_record_image_path(h
->record
);
3057 if (!path_startswith(ip
, "/dev/"))
3060 if (stat(ip
, &st
) < 0)
3063 if (!S_ISBLK(st
.st_mode
))
3066 r
= sd_device_new_from_stat_rdev(&d
, &st
);
3070 r
= sd_device_get_property_value(d
, "ID_SEAT", &seat
);
3071 if (r
== -ENOENT
) /* no property means seat0 */
3084 int home_auto_login(Home
*h
, char ***ret_seats
) {
3085 _cleanup_free_
char *seat
= NULL
, *seat2
= NULL
;
3090 (void) home_get_image_path_seat(h
, &seat
);
3092 if (h
->record
->auto_login
> 0 && !streq_ptr(seat
, "seat0")) {
3093 /* For now, when the auto-login boolean is set for a user, let's make it mean
3094 * "seat0". Eventually we can extend the concept and allow configuration of any kind of seat,
3095 * but let's keep simple initially, most likely the feature is interesting on single-user
3096 * systems anyway, only.
3098 * We filter out users marked for auto-login in we know for sure their home directory is
3101 if (user_record_test_image_path(h
->record
) != USER_TEST_ABSENT
) {
3102 seat2
= strdup("seat0");
3108 if (seat
|| seat2
) {
3109 _cleanup_strv_free_
char **list
= NULL
;
3112 list
= new(char*, 3);
3117 list
[i
++] = TAKE_PTR(seat
);
3119 list
[i
++] = TAKE_PTR(seat2
);
3122 *ret_seats
= TAKE_PTR(list
);
3130 int home_set_current_message(Home
*h
, sd_bus_message
*m
) {
3136 if (h
->current_operation
)
3139 h
->current_operation
= operation_new(OPERATION_IMMEDIATE
, m
);
3140 if (!h
->current_operation
)
3146 int home_wait_for_worker(Home
*h
) {
3151 if (h
->worker_pid
<= 0)
3154 log_info("Worker process for home %s is still running while exiting. Waiting for it to finish.", h
->user_name
);
3156 r
= wait_for_terminate_with_timeout(h
->worker_pid
, 30 * USEC_PER_SEC
);
3157 if (r
== -ETIMEDOUT
)
3158 log_warning_errno(r
, "Waiting for worker process for home %s timed out. Ignoring.", h
->user_name
);
3160 log_warning_errno(r
, "Failed to wait for worker process for home %s. Ignoring.", h
->user_name
);
3162 (void) hashmap_remove_value(h
->manager
->homes_by_worker_pid
, PID_TO_PTR(h
->worker_pid
), h
);
3167 bool home_shall_rebalance(Home
*h
) {
3172 /* Determines if the home directory is a candidate for rebalancing */
3174 if (!user_record_shall_rebalance(h
->record
))
3177 state
= home_get_state(h
);
3178 if (!HOME_STATE_SHALL_REBALANCE(state
))
3184 bool home_is_busy(Home
*h
) {
3187 if (h
->current_operation
)
3190 if (!ordered_set_isempty(h
->pending_operations
))
3193 return HOME_STATE_IS_EXECUTING_OPERATION(home_get_state(h
));
3196 static const char* const home_state_table
[_HOME_STATE_MAX
] = {
3197 [HOME_UNFIXATED
] = "unfixated",
3198 [HOME_ABSENT
] = "absent",
3199 [HOME_INACTIVE
] = "inactive",
3200 [HOME_DIRTY
] = "dirty",
3201 [HOME_FIXATING
] = "fixating",
3202 [HOME_FIXATING_FOR_ACTIVATION
] = "fixating-for-activation",
3203 [HOME_FIXATING_FOR_ACQUIRE
] = "fixating-for-acquire",
3204 [HOME_ACTIVATING
] = "activating",
3205 [HOME_ACTIVATING_FOR_ACQUIRE
] = "activating-for-acquire",
3206 [HOME_DEACTIVATING
] = "deactivating",
3207 [HOME_ACTIVE
] = "active",
3208 [HOME_LINGERING
] = "lingering",
3209 [HOME_LOCKING
] = "locking",
3210 [HOME_LOCKED
] = "locked",
3211 [HOME_UNLOCKING
] = "unlocking",
3212 [HOME_UNLOCKING_FOR_ACQUIRE
] = "unlocking-for-acquire",
3213 [HOME_CREATING
] = "creating",
3214 [HOME_REMOVING
] = "removing",
3215 [HOME_UPDATING
] = "updating",
3216 [HOME_UPDATING_WHILE_ACTIVE
] = "updating-while-active",
3217 [HOME_RESIZING
] = "resizing",
3218 [HOME_RESIZING_WHILE_ACTIVE
] = "resizing-while-active",
3219 [HOME_PASSWD
] = "passwd",
3220 [HOME_PASSWD_WHILE_ACTIVE
] = "passwd-while-active",
3221 [HOME_AUTHENTICATING
] = "authenticating",
3222 [HOME_AUTHENTICATING_WHILE_ACTIVE
] = "authenticating-while-active",
3223 [HOME_AUTHENTICATING_FOR_ACQUIRE
] = "authenticating-for-acquire",
3226 DEFINE_STRING_TABLE_LOOKUP(home_state
, HomeState
);