1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/memfd.h>
11 #include "blockdev-util.h"
12 #include "btrfs-util.h"
13 #include "bus-common-errors.h"
15 #include "errno-list.h"
16 #include "errno-util.h"
19 #include "home-util.h"
20 #include "homed-home-bus.h"
21 #include "homed-home.h"
22 #include "missing_syscall.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "pwquality-util.h"
27 #include "quota-util.h"
28 #include "resize-fs.h"
30 #include "signal-util.h"
31 #include "stat-util.h"
32 #include "string-table.h"
34 #include "user-record-sign.h"
35 #include "user-record-util.h"
36 #include "user-record.h"
37 #include "user-util.h"
39 #define HOME_USERS_MAX 500
40 #define PENDING_OPERATIONS_MAX 100
42 assert_cc(HOME_UID_MIN
<= HOME_UID_MAX
);
43 assert_cc(HOME_USERS_MAX
<= (HOME_UID_MAX
- HOME_UID_MIN
+ 1));
45 static int home_start_work(Home
*h
, const char *verb
, UserRecord
*hr
, UserRecord
*secret
);
47 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(operation_hash_ops
, void, trivial_hash_func
, trivial_compare_func
, Operation
, operation_unref
);
49 static int suitable_home_record(UserRecord
*hr
) {
57 /* We are a bit more restrictive with what we accept as homed-managed user than what we accept in
58 * home records in general. Let's enforce the stricter rule here. */
59 if (!suitable_user_name(hr
->user_name
))
61 if (!uid_is_valid(hr
->uid
))
64 /* Insist we are outside of the dynamic and system range */
65 if (uid_is_system(hr
->uid
) || gid_is_system(user_record_gid(hr
)) ||
66 uid_is_dynamic(hr
->uid
) || gid_is_dynamic(user_record_gid(hr
)))
67 return -EADDRNOTAVAIL
;
69 /* Insist that GID and UID match */
70 if (user_record_gid(hr
) != (gid_t
) hr
->uid
)
73 /* Similar for the realm */
75 r
= suitable_realm(hr
->realm
);
85 int home_new(Manager
*m
, UserRecord
*hr
, const char *sysfs
, Home
**ret
) {
86 _cleanup_(home_freep
) Home
*home
= NULL
;
87 _cleanup_free_
char *nm
= NULL
, *ns
= NULL
;
93 r
= suitable_home_record(hr
);
97 if (hashmap_contains(m
->homes_by_name
, hr
->user_name
))
100 if (hashmap_contains(m
->homes_by_uid
, UID_TO_PTR(hr
->uid
)))
103 if (sysfs
&& hashmap_contains(m
->homes_by_sysfs
, sysfs
))
106 if (hashmap_size(m
->homes_by_name
) >= HOME_USERS_MAX
)
109 nm
= strdup(hr
->user_name
);
125 .user_name
= TAKE_PTR(nm
),
127 .state
= _HOME_STATE_INVALID
,
128 .worker_stdout_fd
= -1,
129 .sysfs
= TAKE_PTR(ns
),
130 .signed_locally
= -1,
133 r
= hashmap_put(m
->homes_by_name
, home
->user_name
, home
);
137 r
= hashmap_put(m
->homes_by_uid
, UID_TO_PTR(home
->uid
), home
);
142 r
= hashmap_put(m
->homes_by_sysfs
, home
->sysfs
, home
);
147 r
= user_record_clone(hr
, USER_RECORD_LOAD_MASK_SECRET
, &home
->record
);
151 (void) bus_manager_emit_auto_login_changed(m
);
152 (void) bus_home_emit_change(home
);
155 *ret
= TAKE_PTR(home
);
162 Home
*home_free(Home
*h
) {
168 (void) bus_home_emit_remove(h
);
169 (void) bus_manager_emit_auto_login_changed(h
->manager
);
172 (void) hashmap_remove_value(h
->manager
->homes_by_name
, h
->user_name
, h
);
174 if (uid_is_valid(h
->uid
))
175 (void) hashmap_remove_value(h
->manager
->homes_by_uid
, UID_TO_PTR(h
->uid
), h
);
178 (void) hashmap_remove_value(h
->manager
->homes_by_sysfs
, h
->sysfs
, h
);
180 if (h
->worker_pid
> 0)
181 (void) hashmap_remove_value(h
->manager
->homes_by_worker_pid
, PID_TO_PTR(h
->worker_pid
), h
);
183 if (h
->manager
->gc_focus
== h
)
184 h
->manager
->gc_focus
= NULL
;
187 user_record_unref(h
->record
);
188 user_record_unref(h
->secret
);
190 h
->worker_event_source
= sd_event_source_unref(h
->worker_event_source
);
191 safe_close(h
->worker_stdout_fd
);
195 h
->ref_event_source_please_suspend
= sd_event_source_unref(h
->ref_event_source_please_suspend
);
196 h
->ref_event_source_dont_suspend
= sd_event_source_unref(h
->ref_event_source_dont_suspend
);
198 h
->pending_operations
= ordered_set_free(h
->pending_operations
);
199 h
->pending_event_source
= sd_event_source_unref(h
->pending_event_source
);
200 h
->deferred_change_event_source
= sd_event_source_unref(h
->deferred_change_event_source
);
202 h
->current_operation
= operation_unref(h
->current_operation
);
207 int home_set_record(Home
*h
, UserRecord
*hr
) {
208 _cleanup_(user_record_unrefp
) UserRecord
*new_hr
= NULL
;
213 assert(h
->user_name
);
217 if (user_record_equal(h
->record
, hr
))
220 r
= suitable_home_record(hr
);
224 if (!user_record_compatible(h
->record
, hr
))
227 if (!FLAGS_SET(hr
->mask
, USER_RECORD_REGULAR
) ||
228 FLAGS_SET(hr
->mask
, USER_RECORD_SECRET
))
231 if (FLAGS_SET(h
->record
->mask
, USER_RECORD_STATUS
)) {
232 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
234 /* Hmm, the existing record has status fields? If so, copy them over */
236 v
= json_variant_ref(hr
->json
);
237 r
= json_variant_set_field(&v
, "status", json_variant_by_key(h
->record
->json
, "status"));
241 new_hr
= user_record_new();
245 r
= user_record_load(new_hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
);
252 other
= hashmap_get(h
->manager
->homes_by_uid
, UID_TO_PTR(hr
->uid
));
253 if (other
&& other
!= h
)
256 if (h
->uid
!= hr
->uid
) {
257 r
= hashmap_remove_and_replace(h
->manager
->homes_by_uid
, UID_TO_PTR(h
->uid
), UID_TO_PTR(hr
->uid
), h
);
262 user_record_unref(h
->record
);
263 h
->record
= user_record_ref(hr
);
264 h
->uid
= h
->record
->uid
;
266 /* The updated record might have a different autologin setting, trigger a PropertiesChanged event for it */
267 (void) bus_manager_emit_auto_login_changed(h
->manager
);
268 (void) bus_home_emit_change(h
);
273 int home_save_record(Home
*h
) {
274 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
275 _cleanup_free_
char *text
= NULL
;
281 v
= json_variant_ref(h
->record
->json
);
282 r
= json_variant_normalize(&v
);
284 log_warning_errno(r
, "User record could not be normalized.");
286 r
= json_variant_format(v
, JSON_FORMAT_PRETTY
|JSON_FORMAT_NEWLINE
, &text
);
290 (void) mkdir("/var/lib/systemd/", 0755);
291 (void) mkdir("/var/lib/systemd/home/", 0700);
293 fn
= strjoina("/var/lib/systemd/home/", h
->user_name
, ".identity");
295 r
= write_string_file(fn
, text
, WRITE_STRING_FILE_ATOMIC
|WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_MODE_0600
);
302 int home_unlink_record(Home
*h
) {
307 fn
= strjoina("/var/lib/systemd/home/", h
->user_name
, ".identity");
308 if (unlink(fn
) < 0 && errno
!= ENOENT
)
311 fn
= strjoina("/run/systemd/home/", h
->user_name
, ".ref");
312 if (unlink(fn
) < 0 && errno
!= ENOENT
)
318 static void home_set_state(Home
*h
, HomeState state
) {
319 HomeState old_state
, new_state
;
323 old_state
= home_get_state(h
);
325 new_state
= home_get_state(h
); /* Query the new state, since the 'state' variable might be set to -1,
326 * in which case we synthesize an high-level state on demand */
328 log_info("%s: changing state %s → %s", h
->user_name
,
329 home_state_to_string(old_state
),
330 home_state_to_string(new_state
));
332 if (HOME_STATE_IS_EXECUTING_OPERATION(old_state
) && !HOME_STATE_IS_EXECUTING_OPERATION(new_state
)) {
333 /* If we just finished executing some operation, process the queue of pending operations. And
334 * enqueue it for GC too. */
336 home_schedule_operation(h
, NULL
, NULL
);
337 manager_enqueue_gc(h
->manager
, h
);
341 static int home_parse_worker_stdout(int _fd
, UserRecord
**ret
) {
342 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
343 _cleanup_close_
int fd
= _fd
; /* take possession, even on failure */
344 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
345 _cleanup_fclose_
FILE *f
= NULL
;
346 unsigned line
, column
;
350 if (fstat(fd
, &st
) < 0)
351 return log_error_errno(errno
, "Failed to stat stdout fd: %m");
353 assert(S_ISREG(st
.st_mode
));
355 if (st
.st_size
== 0) { /* empty record */
360 if (lseek(fd
, SEEK_SET
, 0) == (off_t
) -1)
361 return log_error_errno(errno
, "Failed to seek to beginning of memfd: %m");
363 f
= take_fdopen(&fd
, "r");
365 return log_error_errno(errno
, "Failed to reopen memfd: %m");
368 _cleanup_free_
char *text
= NULL
;
370 r
= read_full_stream(f
, &text
, NULL
);
372 return log_error_errno(r
, "Failed to read from client: %m");
374 log_debug("Got from worker: %s", text
);
378 r
= json_parse_file(f
, "stdout", JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
380 return log_error_errno(r
, "Failed to parse identity at %u:%u: %m", line
, column
);
382 hr
= user_record_new();
386 r
= user_record_load(hr
, v
, USER_RECORD_LOAD_REFUSE_SECRET
);
388 return log_error_errno(r
, "Failed to load home record identity: %m");
394 static int home_verify_user_record(Home
*h
, UserRecord
*hr
, bool *ret_signed_locally
, sd_bus_error
*ret_error
) {
399 assert(ret_signed_locally
);
401 is_signed
= manager_verify_user_record(h
->manager
, hr
);
404 case USER_RECORD_SIGNED_EXCLUSIVE
:
405 log_info("Home %s is signed exclusively by our key, accepting.", hr
->user_name
);
406 *ret_signed_locally
= true;
409 case USER_RECORD_SIGNED
:
410 log_info("Home %s is signed by our key (and others), accepting.", hr
->user_name
);
411 *ret_signed_locally
= false;
414 case USER_RECORD_FOREIGN
:
415 log_info("Home %s is signed by foreign key we like, accepting.", hr
->user_name
);
416 *ret_signed_locally
= false;
419 case USER_RECORD_UNSIGNED
:
420 sd_bus_error_setf(ret_error
, BUS_ERROR_BAD_SIGNATURE
, "User record %s is not signed at all, refusing.", hr
->user_name
);
421 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Home %s contains user record that is not signed at all, refusing.", hr
->user_name
);
424 sd_bus_error_setf(ret_error
, BUS_ERROR_BAD_SIGNATURE
, "User record %s is not signed by any known key, refusing.", hr
->user_name
);
425 return log_error_errno(is_signed
, "Home %s contains user record that is not signed by any known key, refusing.", hr
->user_name
);
428 assert(is_signed
< 0);
429 return log_error_errno(is_signed
, "Failed to verify signature on user record for %s, refusing fixation: %m", hr
->user_name
);
433 static int convert_worker_errno(Home
*h
, int e
, sd_bus_error
*error
) {
434 /* Converts the error numbers the worker process returned into somewhat sensible dbus errors */
439 return sd_bus_error_setf(error
, BUS_ERROR_BAD_HOME_SIZE
, "File systems of this type cannot be shrunk");
441 return sd_bus_error_setf(error
, BUS_ERROR_BAD_HOME_SIZE
, "File systems of this type can only be shrunk offline");
443 return sd_bus_error_setf(error
, BUS_ERROR_BAD_HOME_SIZE
, "File system size too small");
445 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "System does not support selected storage backend");
446 case -EPROTONOSUPPORT
:
447 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "System does not support selected file system");
449 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Operation not supported on storage backend");
450 case -ESOCKTNOSUPPORT
:
451 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Operation not supported on file system");
453 return sd_bus_error_setf(error
, BUS_ERROR_BAD_PASSWORD
, "Password for home %s is incorrect or not sufficient for authentication.", h
->user_name
);
455 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
);
457 return sd_bus_error_setf(error
, BUS_ERROR_TOKEN_PIN_NEEDED
, "PIN for security token required.");
459 return sd_bus_error_setf(error
, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED
, "Security token requires protected authentication path.");
461 return sd_bus_error_setf(error
, BUS_ERROR_TOKEN_PIN_LOCKED
, "PIN of security token locked.");
463 return sd_bus_error_setf(error
, BUS_ERROR_TOKEN_BAD_PIN
, "Bad PIN of security token.");
465 return sd_bus_error_setf(error
, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT
, "Bad PIN of security token, and only a few tries left.");
467 return sd_bus_error_setf(error
, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT
, "Bad PIN of security token, and only one try left.");
469 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
);
471 return sd_bus_error_setf(error
, BUS_ERROR_HOME_NOT_ACTIVE
, "Home %s is currently not active", h
->user_name
);
473 return sd_bus_error_setf(error
, BUS_ERROR_NO_DISK_SPACE
, "Not enough disk space for home %s", h
->user_name
);
479 static void home_count_bad_authentication(Home
*h
, bool save
) {
484 r
= user_record_bad_authentication(h
->record
);
486 log_warning_errno(r
, "Failed to increase bad authentication counter, ignoring: %m");
491 r
= home_save_record(h
);
493 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
497 static void home_fixate_finish(Home
*h
, int ret
, UserRecord
*hr
) {
498 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
499 _cleanup_(user_record_unrefp
) UserRecord
*secret
= NULL
;
504 assert(IN_SET(h
->state
, HOME_FIXATING
, HOME_FIXATING_FOR_ACTIVATION
, HOME_FIXATING_FOR_ACQUIRE
));
506 secret
= TAKE_PTR(h
->secret
); /* Take possession */
510 (void) home_count_bad_authentication(h
, false);
512 (void) convert_worker_errno(h
, ret
, &error
);
513 r
= log_error_errno(ret
, "Fixation failed: %m");
517 r
= log_error_errno(SYNTHETIC_ERRNO(EIO
), "Did not receive user record from worker process, fixation failed.");
521 r
= home_verify_user_record(h
, hr
, &signed_locally
, &error
);
525 r
= home_set_record(h
, hr
);
527 log_error_errno(r
, "Failed to update home record: %m");
531 h
->signed_locally
= signed_locally
;
533 /* When we finished fixating (and don't follow-up with activation), let's count this as good authentication */
534 if (h
->state
== HOME_FIXATING
) {
535 r
= user_record_good_authentication(h
->record
);
537 log_warning_errno(r
, "Failed to increase good authentication counter, ignoring: %m");
540 r
= home_save_record(h
);
542 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
544 if (IN_SET(h
->state
, HOME_FIXATING_FOR_ACTIVATION
, HOME_FIXATING_FOR_ACQUIRE
)) {
546 r
= home_start_work(h
, "activate", h
->record
, secret
);
548 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, NULL
);
549 home_set_state(h
, _HOME_STATE_INVALID
);
551 home_set_state(h
, h
->state
== HOME_FIXATING_FOR_ACTIVATION
? HOME_ACTIVATING
: HOME_ACTIVATING_FOR_ACQUIRE
);
556 log_debug("Fixation of %s completed.", h
->user_name
);
558 h
->current_operation
= operation_result_unref(h
->current_operation
, 0, NULL
);
560 /* Reset the state to "invalid", which makes home_get_state() test if the image exists and returns
561 * HOME_ABSENT vs. HOME_INACTIVE as necessary. */
562 home_set_state(h
, _HOME_STATE_INVALID
);
566 /* If fixation fails, we stay in unfixated state! */
567 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
568 home_set_state(h
, HOME_UNFIXATED
);
571 static void home_activate_finish(Home
*h
, int ret
, UserRecord
*hr
) {
572 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
576 assert(IN_SET(h
->state
, HOME_ACTIVATING
, HOME_ACTIVATING_FOR_ACQUIRE
));
580 home_count_bad_authentication(h
, true);
582 (void) convert_worker_errno(h
, ret
, &error
);
583 r
= log_error_errno(ret
, "Activation failed: %m");
590 r
= home_verify_user_record(h
, hr
, &signed_locally
, &error
);
594 r
= home_set_record(h
, hr
);
596 log_error_errno(r
, "Failed to update home record, ignoring: %m");
600 h
->signed_locally
= signed_locally
;
602 r
= user_record_good_authentication(h
->record
);
604 log_warning_errno(r
, "Failed to increase good authentication counter, ignoring: %m");
606 r
= home_save_record(h
);
608 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
611 log_debug("Activation of %s completed.", h
->user_name
);
615 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
616 home_set_state(h
, _HOME_STATE_INVALID
);
619 static void home_deactivate_finish(Home
*h
, int ret
, UserRecord
*hr
) {
620 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
624 assert(h
->state
== HOME_DEACTIVATING
);
625 assert(!hr
); /* We don't expect a record on this operation */
628 (void) convert_worker_errno(h
, ret
, &error
);
629 r
= log_error_errno(ret
, "Deactivation of %s failed: %m", h
->user_name
);
633 log_debug("Deactivation of %s completed.", h
->user_name
);
637 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
638 home_set_state(h
, _HOME_STATE_INVALID
);
641 static void home_remove_finish(Home
*h
, int ret
, UserRecord
*hr
) {
642 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
647 assert(h
->state
== HOME_REMOVING
);
648 assert(!hr
); /* We don't expect a record on this operation */
652 if (ret
< 0 && ret
!= -EALREADY
) {
653 (void) convert_worker_errno(h
, ret
, &error
);
654 r
= log_error_errno(ret
, "Removing %s failed: %m", h
->user_name
);
658 /* For a couple of storage types we can't delete the actual data storage when called (such as LUKS on
659 * partitions like USB sticks, or so). Sometimes these storage locations are among those we normally
660 * automatically discover in /home or in udev. When such a home is deleted let's hence issue a rescan
661 * after completion, so that "unfixated" entries are rediscovered. */
662 if (!IN_SET(user_record_test_image_path(h
->record
), USER_TEST_UNDEFINED
, USER_TEST_ABSENT
))
663 manager_enqueue_rescan(m
);
665 /* The image is now removed from disk. Now also remove our stored record */
666 r
= home_unlink_record(h
);
668 log_error_errno(r
, "Removing record file failed: %m");
672 log_debug("Removal of %s completed.", h
->user_name
);
673 h
->current_operation
= operation_result_unref(h
->current_operation
, 0, NULL
);
675 /* Unload this record from memory too now. */
680 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
681 home_set_state(h
, _HOME_STATE_INVALID
);
684 static void home_create_finish(Home
*h
, int ret
, UserRecord
*hr
) {
688 assert(h
->state
== HOME_CREATING
);
691 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
693 (void) convert_worker_errno(h
, ret
, &error
);
694 log_error_errno(ret
, "Operation on %s failed: %m", h
->user_name
);
695 h
->current_operation
= operation_result_unref(h
->current_operation
, ret
, &error
);
697 if (h
->unregister_on_failure
) {
698 (void) home_unlink_record(h
);
703 home_set_state(h
, _HOME_STATE_INVALID
);
708 r
= home_set_record(h
, hr
);
710 log_warning_errno(r
, "Failed to update home record, ignoring: %m");
713 r
= home_save_record(h
);
715 log_warning_errno(r
, "Failed to save record to disk, ignoring: %m");
717 log_debug("Creation of %s completed.", h
->user_name
);
719 h
->current_operation
= operation_result_unref(h
->current_operation
, 0, NULL
);
720 home_set_state(h
, _HOME_STATE_INVALID
);
723 static void home_change_finish(Home
*h
, int ret
, UserRecord
*hr
) {
724 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
731 (void) home_count_bad_authentication(h
, true);
733 (void) convert_worker_errno(h
, ret
, &error
);
734 r
= log_error_errno(ret
, "Change operation failed: %m");
739 r
= home_set_record(h
, hr
);
741 log_warning_errno(r
, "Failed to update home record, ignoring: %m");
743 r
= user_record_good_authentication(h
->record
);
745 log_warning_errno(r
, "Failed to increase good authentication counter, ignoring: %m");
747 r
= home_save_record(h
);
749 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
753 log_debug("Change operation of %s completed.", h
->user_name
);
757 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
758 home_set_state(h
, _HOME_STATE_INVALID
);
761 static void home_locking_finish(Home
*h
, int ret
, UserRecord
*hr
) {
762 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
766 assert(h
->state
== HOME_LOCKING
);
769 (void) convert_worker_errno(h
, ret
, &error
);
770 r
= log_error_errno(ret
, "Locking operation failed: %m");
774 log_debug("Locking operation of %s completed.", h
->user_name
);
775 h
->current_operation
= operation_result_unref(h
->current_operation
, 0, NULL
);
776 home_set_state(h
, HOME_LOCKED
);
780 /* If a specific home doesn't know the concept of locking, then that's totally OK, don't propagate
781 * the error if we are executing a LockAllHomes() operation. */
783 if (h
->current_operation
->type
== OPERATION_LOCK_ALL
&& r
== -ENOTTY
)
784 h
->current_operation
= operation_result_unref(h
->current_operation
, 0, NULL
);
786 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
788 home_set_state(h
, _HOME_STATE_INVALID
);
791 static void home_unlocking_finish(Home
*h
, int ret
, UserRecord
*hr
) {
792 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
796 assert(IN_SET(h
->state
, HOME_UNLOCKING
, HOME_UNLOCKING_FOR_ACQUIRE
));
800 (void) home_count_bad_authentication(h
, true);
802 (void) convert_worker_errno(h
, ret
, &error
);
803 r
= log_error_errno(ret
, "Unlocking operation failed: %m");
805 /* Revert to locked state */
806 home_set_state(h
, HOME_LOCKED
);
807 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
811 r
= user_record_good_authentication(h
->record
);
813 log_warning_errno(r
, "Failed to increase good authentication counter, ignoring: %m");
815 r
= home_save_record(h
);
817 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
820 log_debug("Unlocking operation of %s completed.", h
->user_name
);
822 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
823 home_set_state(h
, _HOME_STATE_INVALID
);
827 static void home_authenticating_finish(Home
*h
, int ret
, UserRecord
*hr
) {
828 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
832 assert(IN_SET(h
->state
, HOME_AUTHENTICATING
, HOME_AUTHENTICATING_WHILE_ACTIVE
, HOME_AUTHENTICATING_FOR_ACQUIRE
));
836 (void) home_count_bad_authentication(h
, true);
838 (void) convert_worker_errno(h
, ret
, &error
);
839 r
= log_error_errno(ret
, "Authentication failed: %m");
844 r
= home_set_record(h
, hr
);
846 log_warning_errno(r
, "Failed to update home record, ignoring: %m");
848 r
= user_record_good_authentication(h
->record
);
850 log_warning_errno(r
, "Failed to increase good authentication counter, ignoring: %m");
852 r
= home_save_record(h
);
854 log_warning_errno(r
, "Failed to write home record to disk, ignoring: %m");
858 log_debug("Authentication of %s completed.", h
->user_name
);
862 h
->current_operation
= operation_result_unref(h
->current_operation
, r
, &error
);
863 home_set_state(h
, _HOME_STATE_INVALID
);
866 static int home_on_worker_process(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
867 _cleanup_(user_record_unrefp
) UserRecord
*hr
= NULL
;
875 assert(h
->worker_pid
== si
->si_pid
);
876 assert(h
->worker_event_source
);
877 assert(h
->worker_stdout_fd
>= 0);
879 (void) hashmap_remove_value(h
->manager
->homes_by_worker_pid
, PID_TO_PTR(h
->worker_pid
), h
);
882 h
->worker_event_source
= sd_event_source_unref(h
->worker_event_source
);
884 if (si
->si_code
!= CLD_EXITED
) {
885 assert(IN_SET(si
->si_code
, CLD_KILLED
, CLD_DUMPED
));
886 ret
= log_debug_errno(SYNTHETIC_ERRNO(EPROTO
), "Worker process died abnormally with signal %s.", signal_to_string(si
->si_status
));
887 } else if (si
->si_status
!= EXIT_SUCCESS
) {
888 /* If we received an error code via sd_notify(), use it */
889 if (h
->worker_error_code
!= 0)
890 ret
= log_debug_errno(h
->worker_error_code
, "Worker reported error code %s.", errno_to_name(h
->worker_error_code
));
892 ret
= log_debug_errno(SYNTHETIC_ERRNO(EPROTO
), "Worker exited with exit code %i.", si
->si_status
);
894 ret
= home_parse_worker_stdout(TAKE_FD(h
->worker_stdout_fd
), &hr
);
896 h
->worker_stdout_fd
= safe_close(h
->worker_stdout_fd
);
901 case HOME_FIXATING_FOR_ACTIVATION
:
902 case HOME_FIXATING_FOR_ACQUIRE
:
903 home_fixate_finish(h
, ret
, hr
);
906 case HOME_ACTIVATING
:
907 case HOME_ACTIVATING_FOR_ACQUIRE
:
908 home_activate_finish(h
, ret
, hr
);
911 case HOME_DEACTIVATING
:
912 home_deactivate_finish(h
, ret
, hr
);
916 home_locking_finish(h
, ret
, hr
);
920 case HOME_UNLOCKING_FOR_ACQUIRE
:
921 home_unlocking_finish(h
, ret
, hr
);
925 home_create_finish(h
, ret
, hr
);
929 home_remove_finish(h
, ret
, hr
);
933 case HOME_UPDATING_WHILE_ACTIVE
:
935 case HOME_RESIZING_WHILE_ACTIVE
:
937 case HOME_PASSWD_WHILE_ACTIVE
:
938 home_change_finish(h
, ret
, hr
);
941 case HOME_AUTHENTICATING
:
942 case HOME_AUTHENTICATING_WHILE_ACTIVE
:
943 case HOME_AUTHENTICATING_FOR_ACQUIRE
:
944 home_authenticating_finish(h
, ret
, hr
);
948 assert_not_reached("Unexpected state after worker exited");
954 static int home_start_work(Home
*h
, const char *verb
, UserRecord
*hr
, UserRecord
*secret
) {
955 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
956 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
957 _cleanup_close_
int stdin_fd
= -1, stdout_fd
= -1;
965 if (h
->worker_pid
!= 0)
968 assert(h
->worker_stdout_fd
< 0);
969 assert(!h
->worker_event_source
);
971 v
= json_variant_ref(hr
->json
);
974 JsonVariant
*sub
= NULL
;
976 sub
= json_variant_by_key(secret
->json
, "secret");
980 r
= json_variant_set_field(&v
, "secret", sub
);
985 r
= json_variant_format(v
, 0, &formatted
);
989 stdin_fd
= acquire_data_fd(formatted
, strlen(formatted
), 0);
993 log_debug("Sending to worker: %s", formatted
);
995 stdout_fd
= memfd_create("homework-stdout", MFD_CLOEXEC
);
999 r
= safe_fork_full("(sd-homework)",
1000 (int[]) { stdin_fd
, stdout_fd
}, 2,
1001 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
1007 if (setenv("NOTIFY_SOCKET", "/run/systemd/home/notify", 1) < 0) {
1008 log_error_errno(errno
, "Failed to set $NOTIFY_SOCKET: %m");
1009 _exit(EXIT_FAILURE
);
1012 r
= rearrange_stdio(stdin_fd
, stdout_fd
, STDERR_FILENO
);
1014 log_error_errno(r
, "Failed to rearrange stdin/stdout/stderr: %m");
1015 _exit(EXIT_FAILURE
);
1018 stdin_fd
= stdout_fd
= -1; /* have been invalidated by rearrange_stdio() */
1020 execl(SYSTEMD_HOMEWORK_PATH
, SYSTEMD_HOMEWORK_PATH
, verb
, NULL
);
1021 log_error_errno(errno
, "Failed to invoke " SYSTEMD_HOMEWORK_PATH
": %m");
1022 _exit(EXIT_FAILURE
);
1025 r
= sd_event_add_child(h
->manager
->event
, &h
->worker_event_source
, pid
, WEXITED
, home_on_worker_process
, h
);
1029 (void) sd_event_source_set_description(h
->worker_event_source
, "worker");
1031 r
= hashmap_put(h
->manager
->homes_by_worker_pid
, PID_TO_PTR(pid
), h
);
1033 h
->worker_event_source
= sd_event_source_unref(h
->worker_event_source
);
1037 h
->worker_stdout_fd
= TAKE_FD(stdout_fd
);
1038 h
->worker_pid
= pid
;
1039 h
->worker_error_code
= 0;
1044 static int home_ratelimit(Home
*h
, sd_bus_error
*error
) {
1049 ret
= user_record_ratelimit(h
->record
);
1053 if (h
->state
!= HOME_UNFIXATED
) {
1054 r
= home_save_record(h
);
1056 log_warning_errno(r
, "Failed to save updated record, ignoring: %m");
1060 char buf
[FORMAT_TIMESPAN_MAX
];
1063 n
= now(CLOCK_REALTIME
);
1064 t
= user_record_ratelimit_next_try(h
->record
);
1066 if (t
!= USEC_INFINITY
&& t
> n
)
1067 return sd_bus_error_setf(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
, "Too many login attempts, please try again in %s!",
1068 format_timespan(buf
, sizeof(buf
), t
- n
, USEC_PER_SEC
));
1070 return sd_bus_error_setf(error
, BUS_ERROR_AUTHENTICATION_LIMIT_HIT
, "Too many login attempts, please try again later.");
1076 static int home_fixate_internal(
1079 HomeState for_state
,
1080 sd_bus_error
*error
) {
1085 assert(IN_SET(for_state
, HOME_FIXATING
, HOME_FIXATING_FOR_ACTIVATION
, HOME_FIXATING_FOR_ACQUIRE
));
1087 r
= home_start_work(h
, "inspect", h
->record
, secret
);
1091 if (for_state
== HOME_FIXATING_FOR_ACTIVATION
) {
1092 /* Remember the secret data, since we need it for the activation again, later on. */
1093 user_record_unref(h
->secret
);
1094 h
->secret
= user_record_ref(secret
);
1097 home_set_state(h
, for_state
);
1101 int home_fixate(Home
*h
, UserRecord
*secret
, sd_bus_error
*error
) {
1106 switch (home_get_state(h
)) {
1108 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1112 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ALREADY_FIXATED
, "Home %s is already fixated.", h
->user_name
);
1113 case HOME_UNFIXATED
:
1116 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1119 r
= home_ratelimit(h
, error
);
1123 return home_fixate_internal(h
, secret
, HOME_FIXATING
, error
);
1126 static int home_activate_internal(Home
*h
, UserRecord
*secret
, HomeState for_state
, sd_bus_error
*error
) {
1130 assert(IN_SET(for_state
, HOME_ACTIVATING
, HOME_ACTIVATING_FOR_ACQUIRE
));
1132 r
= home_start_work(h
, "activate", h
->record
, secret
);
1136 home_set_state(h
, for_state
);
1140 int home_activate(Home
*h
, UserRecord
*secret
, sd_bus_error
*error
) {
1145 switch (home_get_state(h
)) {
1146 case HOME_UNFIXATED
:
1147 return home_fixate_internal(h
, secret
, HOME_FIXATING_FOR_ACTIVATION
, error
);
1149 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1151 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ALREADY_ACTIVE
, "Home %s is already active.", h
->user_name
);
1153 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1157 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1160 r
= home_ratelimit(h
, error
);
1164 return home_activate_internal(h
, secret
, HOME_ACTIVATING
, error
);
1167 static int home_authenticate_internal(Home
*h
, UserRecord
*secret
, HomeState for_state
, sd_bus_error
*error
) {
1171 assert(IN_SET(for_state
, HOME_AUTHENTICATING
, HOME_AUTHENTICATING_WHILE_ACTIVE
, HOME_AUTHENTICATING_FOR_ACQUIRE
));
1173 r
= home_start_work(h
, "inspect", h
->record
, secret
);
1177 home_set_state(h
, for_state
);
1181 int home_authenticate(Home
*h
, UserRecord
*secret
, sd_bus_error
*error
) {
1187 state
= home_get_state(h
);
1190 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1192 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1193 case HOME_UNFIXATED
:
1198 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1201 r
= home_ratelimit(h
, error
);
1205 return home_authenticate_internal(h
, secret
, state
== HOME_ACTIVE
? HOME_AUTHENTICATING_WHILE_ACTIVE
: HOME_AUTHENTICATING
, error
);
1208 static int home_deactivate_internal(Home
*h
, bool force
, sd_bus_error
*error
) {
1213 r
= home_start_work(h
, force
? "deactivate-force" : "deactivate", h
->record
, NULL
);
1217 home_set_state(h
, HOME_DEACTIVATING
);
1221 int home_deactivate(Home
*h
, bool force
, sd_bus_error
*error
) {
1224 switch (home_get_state(h
)) {
1225 case HOME_UNFIXATED
:
1228 return sd_bus_error_setf(error
, BUS_ERROR_HOME_NOT_ACTIVE
, "Home %s not active.", h
->user_name
);
1230 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1234 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1237 return home_deactivate_internal(h
, force
, error
);
1240 int home_create(Home
*h
, UserRecord
*secret
, sd_bus_error
*error
) {
1245 switch (home_get_state(h
)) {
1247 if (h
->record
->storage
< 0)
1248 break; /* if no storage is defined we don't know what precisely to look for, hence
1249 * HOME_INACTIVE is OK in that case too. */
1251 if (IN_SET(user_record_test_image_path(h
->record
), USER_TEST_MAYBE
, USER_TEST_UNDEFINED
))
1252 break; /* And if the image path test isn't conclusive, let's also go on */
1255 case HOME_UNFIXATED
:
1256 return sd_bus_error_setf(error
, BUS_ERROR_HOME_EXISTS
, "Home of user %s already exists.", h
->user_name
);
1262 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
);
1265 if (h
->record
->enforce_password_policy
== false)
1266 log_debug("Password quality check turned off for account, skipping.");
1268 r
= quality_check_password(h
->record
, secret
, error
);
1273 r
= home_start_work(h
, "create", h
->record
, secret
);
1277 home_set_state(h
, HOME_CREATING
);
1281 int home_remove(Home
*h
, sd_bus_error
*error
) {
1287 state
= home_get_state(h
);
1289 case HOME_ABSENT
: /* If the home directory is absent, then this is just like unregistering */
1290 return home_unregister(h
, error
);
1292 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1293 case HOME_UNFIXATED
:
1298 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
);
1301 r
= home_start_work(h
, "remove", h
->record
, NULL
);
1305 home_set_state(h
, HOME_REMOVING
);
1309 static int user_record_extend_with_binding(UserRecord
*hr
, UserRecord
*with_binding
, UserRecordLoadFlags flags
, UserRecord
**ret
) {
1310 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1311 _cleanup_(user_record_unrefp
) UserRecord
*nr
= NULL
;
1312 JsonVariant
*binding
;
1316 assert(with_binding
);
1319 assert_se(v
= json_variant_ref(hr
->json
));
1321 binding
= json_variant_by_key(with_binding
->json
, "binding");
1323 r
= json_variant_set_field(&v
, "binding", binding
);
1328 nr
= user_record_new();
1332 r
= user_record_load(nr
, v
, flags
);
1336 *ret
= TAKE_PTR(nr
);
1340 static int home_update_internal(Home
*h
, const char *verb
, UserRecord
*hr
, UserRecord
*secret
, sd_bus_error
*error
) {
1341 _cleanup_(user_record_unrefp
) UserRecord
*new_hr
= NULL
, *saved_secret
= NULL
, *signed_hr
= NULL
;
1348 if (!user_record_compatible(hr
, h
->record
))
1349 return sd_bus_error_setf(error
, BUS_ERROR_HOME_RECORD_MISMATCH
, "Updated user record is not compatible with existing one.");
1350 c
= user_record_compare_last_change(hr
, h
->record
); /* refuse downgrades */
1352 return sd_bus_error_setf(error
, BUS_ERROR_HOME_RECORD_DOWNGRADE
, "Refusing to update to older home record.");
1354 if (!secret
&& FLAGS_SET(hr
->mask
, USER_RECORD_SECRET
)) {
1355 r
= user_record_clone(hr
, USER_RECORD_EXTRACT_SECRET
, &saved_secret
);
1359 secret
= saved_secret
;
1362 r
= manager_verify_user_record(h
->manager
, hr
);
1365 case USER_RECORD_UNSIGNED
:
1366 if (h
->signed_locally
<= 0) /* If the existing record is not owned by us, don't accept an
1367 * unsigned new record. i.e. only implicitly sign new records
1368 * that where previously signed by us too. */
1369 return sd_bus_error_setf(error
, BUS_ERROR_HOME_RECORD_SIGNED
, "Home %s is signed and cannot be modified locally.", h
->user_name
);
1371 /* The updated record is not signed, then do so now */
1372 r
= manager_sign_user_record(h
->manager
, hr
, &signed_hr
, error
);
1379 case USER_RECORD_SIGNED_EXCLUSIVE
:
1380 case USER_RECORD_SIGNED
:
1381 case USER_RECORD_FOREIGN
:
1382 /* Has already been signed. Great! */
1390 r
= user_record_extend_with_binding(hr
, h
->record
, USER_RECORD_LOAD_MASK_SECRET
, &new_hr
);
1395 /* different payload but same lastChangeUSec field? That's not cool! */
1397 r
= user_record_masked_equal(new_hr
, h
->record
, USER_RECORD_REGULAR
|USER_RECORD_PRIVILEGED
|USER_RECORD_PER_MACHINE
);
1401 return sd_bus_error_setf(error
, BUS_ERROR_HOME_RECORD_MISMATCH
, "Home record different but timestamp remained the same, refusing.");
1404 r
= home_start_work(h
, verb
, new_hr
, secret
);
1411 int home_update(Home
*h
, UserRecord
*hr
, sd_bus_error
*error
) {
1418 state
= home_get_state(h
);
1420 case HOME_UNFIXATED
:
1421 return sd_bus_error_setf(error
, BUS_ERROR_HOME_UNFIXATED
, "Home %s has not been fixated yet.", h
->user_name
);
1423 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1425 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1430 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1433 r
= home_ratelimit(h
, error
);
1437 r
= home_update_internal(h
, "update", hr
, NULL
, error
);
1441 home_set_state(h
, state
== HOME_ACTIVE
? HOME_UPDATING_WHILE_ACTIVE
: HOME_UPDATING
);
1445 int home_resize(Home
*h
, uint64_t disk_size
, UserRecord
*secret
, sd_bus_error
*error
) {
1446 _cleanup_(user_record_unrefp
) UserRecord
*c
= NULL
;
1452 state
= home_get_state(h
);
1454 case HOME_UNFIXATED
:
1455 return sd_bus_error_setf(error
, BUS_ERROR_HOME_UNFIXATED
, "Home %s has not been fixated yet.", h
->user_name
);
1457 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1459 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1464 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1467 r
= home_ratelimit(h
, error
);
1471 if (disk_size
== UINT64_MAX
|| disk_size
== h
->record
->disk_size
) {
1472 if (h
->record
->disk_size
== UINT64_MAX
)
1473 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "No disk size to resize to specified.");
1475 c
= user_record_ref(h
->record
); /* Shortcut if size is unspecified or matches the record */
1477 _cleanup_(user_record_unrefp
) UserRecord
*signed_c
= NULL
;
1479 if (h
->signed_locally
<= 0) /* Don't allow changing of records not signed only by us */
1480 return sd_bus_error_setf(error
, BUS_ERROR_HOME_RECORD_SIGNED
, "Home %s is signed and cannot be modified locally.", h
->user_name
);
1482 r
= user_record_clone(h
->record
, USER_RECORD_LOAD_REFUSE_SECRET
, &c
);
1486 r
= user_record_set_disk_size(c
, disk_size
);
1488 return sd_bus_error_setf(error
, BUS_ERROR_BAD_HOME_SIZE
, "Requested size for home %s out of acceptable range.", h
->user_name
);
1492 r
= user_record_update_last_changed(c
, false);
1494 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
);
1498 r
= manager_sign_user_record(h
->manager
, c
, &signed_c
, error
);
1502 user_record_unref(c
);
1503 c
= TAKE_PTR(signed_c
);
1506 r
= home_update_internal(h
, "resize", c
, secret
, error
);
1510 home_set_state(h
, state
== HOME_ACTIVE
? HOME_RESIZING_WHILE_ACTIVE
: HOME_RESIZING
);
1514 static int home_may_change_password(
1516 sd_bus_error
*error
) {
1522 r
= user_record_test_password_change_required(h
->record
);
1523 if (IN_SET(r
, -EKEYREVOKED
, -EOWNERDEAD
, -EKEYEXPIRED
))
1524 return 0; /* expired in some form, but changing is allowed */
1525 if (IN_SET(r
, -EKEYREJECTED
, -EROFS
))
1526 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
);
1528 return log_error_errno(r
, "Failed to test password expiry: %m");
1530 return 0; /* not expired */
1533 int home_passwd(Home
*h
,
1534 UserRecord
*new_secret
,
1535 UserRecord
*old_secret
,
1536 sd_bus_error
*error
) {
1538 _cleanup_(user_record_unrefp
) UserRecord
*c
= NULL
, *merged_secret
= NULL
, *signed_c
= NULL
;
1544 if (h
->signed_locally
<= 0) /* Don't allow changing of records not signed only by us */
1545 return sd_bus_error_setf(error
, BUS_ERROR_HOME_RECORD_SIGNED
, "Home %s is signed and cannot be modified locally.", h
->user_name
);
1547 state
= home_get_state(h
);
1549 case HOME_UNFIXATED
:
1550 return sd_bus_error_setf(error
, BUS_ERROR_HOME_UNFIXATED
, "Home %s has not been fixated yet.", h
->user_name
);
1552 return sd_bus_error_setf(error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
1554 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1559 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1562 r
= home_ratelimit(h
, error
);
1566 r
= home_may_change_password(h
, error
);
1570 r
= user_record_clone(h
->record
, USER_RECORD_LOAD_REFUSE_SECRET
, &c
);
1574 merged_secret
= user_record_new();
1578 r
= user_record_merge_secret(merged_secret
, old_secret
);
1582 r
= user_record_merge_secret(merged_secret
, new_secret
);
1586 if (!strv_isempty(new_secret
->password
)) {
1587 /* Update the password only if one is specified, otherwise let's just reuse the old password
1588 * data. This is useful as a way to propagate updated user records into the LUKS backends
1591 r
= user_record_make_hashed_password(c
, new_secret
->password
, /* extend = */ false);
1595 r
= user_record_set_password_change_now(c
, -1 /* remove */);
1600 r
= user_record_update_last_changed(c
, true);
1602 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
);
1606 r
= manager_sign_user_record(h
->manager
, c
, &signed_c
, error
);
1610 if (c
->enforce_password_policy
== false)
1611 log_debug("Password quality check turned off for account, skipping.");
1613 r
= quality_check_password(c
, merged_secret
, error
);
1618 r
= home_update_internal(h
, "passwd", signed_c
, merged_secret
, error
);
1622 home_set_state(h
, state
== HOME_ACTIVE
? HOME_PASSWD_WHILE_ACTIVE
: HOME_PASSWD
);
1626 int home_unregister(Home
*h
, sd_bus_error
*error
) {
1631 switch (home_get_state(h
)) {
1632 case HOME_UNFIXATED
:
1633 return sd_bus_error_setf(error
, BUS_ERROR_HOME_UNFIXATED
, "Home %s is not registered.", h
->user_name
);
1635 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
1641 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
);
1644 r
= home_unlink_record(h
);
1648 /* And destroy the whole entry. The caller needs to be prepared for that. */
1653 int home_lock(Home
*h
, sd_bus_error
*error
) {
1658 switch (home_get_state(h
)) {
1659 case HOME_UNFIXATED
:
1662 return sd_bus_error_setf(error
, BUS_ERROR_HOME_NOT_ACTIVE
, "Home %s is not active.", h
->user_name
);
1664 return sd_bus_error_setf(error
, BUS_ERROR_HOME_LOCKED
, "Home %s is already locked.", h
->user_name
);
1668 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1671 r
= home_start_work(h
, "lock", h
->record
, NULL
);
1675 home_set_state(h
, HOME_LOCKING
);
1679 static int home_unlock_internal(Home
*h
, UserRecord
*secret
, HomeState for_state
, sd_bus_error
*error
) {
1683 assert(IN_SET(for_state
, HOME_UNLOCKING
, HOME_UNLOCKING_FOR_ACQUIRE
));
1685 r
= home_start_work(h
, "unlock", h
->record
, secret
);
1689 home_set_state(h
, for_state
);
1693 int home_unlock(Home
*h
, UserRecord
*secret
, sd_bus_error
*error
) {
1697 r
= home_ratelimit(h
, error
);
1701 switch (home_get_state(h
)) {
1702 case HOME_UNFIXATED
:
1706 return sd_bus_error_setf(error
, BUS_ERROR_HOME_NOT_LOCKED
, "Home %s is not locked.", h
->user_name
);
1710 return sd_bus_error_setf(error
, BUS_ERROR_HOME_BUSY
, "An operation on home %s is currently being executed.", h
->user_name
);
1713 return home_unlock_internal(h
, secret
, HOME_UNLOCKING
, error
);
1716 HomeState
home_get_state(Home
*h
) {
1719 /* When the state field is initialized, it counts. */
1723 /* Otherwise, let's see if the home directory is mounted. If so, we assume for sure the home
1724 * directory is active */
1725 if (user_record_test_home_directory(h
->record
) == USER_TEST_MOUNTED
)
1728 /* And if we see the image being gone, we report this as absent */
1729 if (user_record_test_image_path(h
->record
) == USER_TEST_ABSENT
)
1732 /* And for all other cases we return "inactive". */
1733 return HOME_INACTIVE
;
1736 void home_process_notify(Home
*h
, char **l
) {
1743 e
= strv_env_get(l
, "ERRNO");
1745 log_debug("Got notify message lacking ERRNO= field, ignoring.");
1749 r
= safe_atoi(e
, &error
);
1751 log_debug_errno(r
, "Failed to parse received error number, ignoring: %s", e
);
1755 log_debug("Error number is out of range: %i", error
);
1759 h
->worker_error_code
= error
;
1762 int home_killall(Home
*h
) {
1763 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1764 _cleanup_free_
char *unit
= NULL
;
1769 if (!uid_is_valid(h
->uid
))
1772 assert(h
->uid
> 0); /* We never should be UID 0 */
1774 /* Let's kill everything matching the specified UID */
1775 r
= safe_fork("(sd-killer)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_WAIT
|FORK_LOG
, NULL
);
1783 gid
= user_record_gid(h
->record
);
1784 if (setresgid(gid
, gid
, gid
) < 0) {
1785 log_error_errno(errno
, "Failed to change GID to " GID_FMT
": %m", gid
);
1786 _exit(EXIT_FAILURE
);
1789 if (setgroups(0, NULL
) < 0) {
1790 log_error_errno(errno
, "Failed to reset auxiliary groups list: %m");
1791 _exit(EXIT_FAILURE
);
1794 if (setresuid(h
->uid
, h
->uid
, h
->uid
) < 0) {
1795 log_error_errno(errno
, "Failed to change UID to " UID_FMT
": %m", h
->uid
);
1796 _exit(EXIT_FAILURE
);
1799 if (kill(-1, SIGKILL
) < 0) {
1800 log_error_errno(errno
, "Failed to kill all processes of UID " UID_FMT
": %m", h
->uid
);
1801 _exit(EXIT_FAILURE
);
1804 _exit(EXIT_SUCCESS
);
1807 /* Let's also kill everything in the user's slice */
1808 if (asprintf(&unit
, "user-" UID_FMT
".slice", h
->uid
) < 0)
1811 r
= sd_bus_call_method(
1813 "org.freedesktop.systemd1",
1814 "/org/freedesktop/systemd1",
1815 "org.freedesktop.systemd1.Manager",
1819 "ssi", unit
, "all", SIGKILL
);
1821 log_full_errno(sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ? LOG_DEBUG
: LOG_WARNING
,
1822 r
, "Failed to kill login processes of user, ignoring: %s", bus_error_message(&error
, r
));
1827 static int home_get_disk_status_luks(
1830 uint64_t *ret_disk_size
,
1831 uint64_t *ret_disk_usage
,
1832 uint64_t *ret_disk_free
,
1833 uint64_t *ret_disk_ceiling
,
1834 uint64_t *ret_disk_floor
) {
1836 uint64_t disk_size
= UINT64_MAX
, disk_usage
= UINT64_MAX
, disk_free
= UINT64_MAX
,
1837 disk_ceiling
= UINT64_MAX
, disk_floor
= UINT64_MAX
,
1838 stat_used
= UINT64_MAX
, fs_size
= UINT64_MAX
, header_size
= 0;
1845 assert(ret_disk_size
);
1846 assert(ret_disk_usage
);
1847 assert(ret_disk_free
);
1848 assert(ret_disk_ceiling
);
1850 if (state
!= HOME_ABSENT
) {
1853 ip
= user_record_image_path(h
->record
);
1857 if (stat(ip
, &st
) < 0)
1858 log_debug_errno(errno
, "Failed to stat() %s, ignoring: %m", ip
);
1859 else if (S_ISREG(st
.st_mode
)) {
1860 _cleanup_free_
char *parent
= NULL
;
1862 disk_size
= st
.st_size
;
1863 stat_used
= st
.st_blocks
* 512;
1865 parent
= dirname_malloc(ip
);
1869 if (statfs(parent
, &sfs
) < 0)
1870 log_debug_errno(errno
, "Failed to statfs() %s, ignoring: %m", parent
);
1872 disk_ceiling
= stat_used
+ sfs
.f_bsize
* sfs
.f_bavail
;
1874 } else if (S_ISBLK(st
.st_mode
)) {
1875 _cleanup_free_
char *szbuf
= NULL
;
1876 char p
[SYS_BLOCK_PATH_MAX("/size")];
1878 /* Let's read the size off sysfs, so that we don't have to open the device */
1879 xsprintf_sys_block_path(p
, "/size", st
.st_rdev
);
1880 r
= read_one_line_file(p
, &szbuf
);
1882 log_debug_errno(r
, "Failed to read %s, ignoring: %m", p
);
1886 r
= safe_atou64(szbuf
, &sz
);
1888 log_debug_errno(r
, "Failed to parse %s, ignoring: %s", p
, szbuf
);
1890 disk_size
= sz
* 512;
1893 log_debug("Image path is not a block device or regular file, not able to acquire size.");
1897 if (!HOME_STATE_IS_ACTIVE(state
))
1900 hd
= user_record_home_directory(h
->record
);
1904 if (statfs(hd
, &sfs
) < 0) {
1905 log_debug_errno(errno
, "Failed to statfs() %s, ignoring: %m", hd
);
1909 disk_free
= sfs
.f_bsize
* sfs
.f_bavail
;
1910 fs_size
= sfs
.f_bsize
* sfs
.f_blocks
;
1911 if (disk_size
!= UINT64_MAX
&& disk_size
> fs_size
)
1912 header_size
= disk_size
- fs_size
;
1914 /* We take a perspective from the user here (as opposed to from the host): the used disk space is the
1915 * difference from the limit and what's free. This makes a difference if sparse mode is not used: in
1916 * that case the image is pre-allocated and thus appears all used from the host PoV but is not used
1917 * up at all yet from the user's PoV.
1919 * That said, we use use the stat() reported loopback file size as upper boundary: our footprint can
1920 * never be larger than what we take up on the lowest layers. */
1922 if (disk_size
!= UINT64_MAX
&& disk_size
> disk_free
) {
1923 disk_usage
= disk_size
- disk_free
;
1925 if (stat_used
!= UINT64_MAX
&& disk_usage
> stat_used
)
1926 disk_usage
= stat_used
;
1928 disk_usage
= stat_used
;
1930 /* If we have the magic, determine floor preferably by magic */
1931 disk_floor
= minimal_size_by_fs_magic(sfs
.f_type
) + header_size
;
1934 /* If we don't know the magic, go by file system name */
1935 if (disk_floor
== UINT64_MAX
)
1936 disk_floor
= minimal_size_by_fs_name(user_record_file_system_type(h
->record
));
1938 *ret_disk_size
= disk_size
;
1939 *ret_disk_usage
= disk_usage
;
1940 *ret_disk_free
= disk_free
;
1941 *ret_disk_ceiling
= disk_ceiling
;
1942 *ret_disk_floor
= disk_floor
;
1947 static int home_get_disk_status_directory(
1950 uint64_t *ret_disk_size
,
1951 uint64_t *ret_disk_usage
,
1952 uint64_t *ret_disk_free
,
1953 uint64_t *ret_disk_ceiling
,
1954 uint64_t *ret_disk_floor
) {
1956 uint64_t disk_size
= UINT64_MAX
, disk_usage
= UINT64_MAX
, disk_free
= UINT64_MAX
,
1957 disk_ceiling
= UINT64_MAX
, disk_floor
= UINT64_MAX
;
1960 const char *path
= NULL
;
1963 assert(ret_disk_size
);
1964 assert(ret_disk_usage
);
1965 assert(ret_disk_free
);
1966 assert(ret_disk_ceiling
);
1967 assert(ret_disk_floor
);
1969 if (HOME_STATE_IS_ACTIVE(state
))
1970 path
= user_record_home_directory(h
->record
);
1973 if (state
== HOME_ABSENT
)
1976 path
= user_record_image_path(h
->record
);
1982 if (statfs(path
, &sfs
) < 0)
1983 log_debug_errno(errno
, "Failed to statfs() %s, ignoring: %m", path
);
1985 disk_free
= sfs
.f_bsize
* sfs
.f_bavail
;
1986 disk_size
= sfs
.f_bsize
* sfs
.f_blocks
;
1988 /* We don't initialize disk_usage from statfs() data here, since the device is likely not used
1989 * by us alone, and disk_usage should only reflect our own use. */
1992 if (IN_SET(h
->record
->storage
, USER_CLASSIC
, USER_DIRECTORY
, USER_SUBVOLUME
)) {
1994 r
= btrfs_is_subvol(path
);
1996 log_debug_errno(r
, "Failed to determine whether %s is a btrfs subvolume: %m", path
);
2000 r
= btrfs_subvol_get_subtree_quota(path
, 0, &qi
);
2002 log_debug_errno(r
, "Failed to query btrfs subtree quota, ignoring: %m");
2004 disk_usage
= qi
.referenced
;
2006 if (disk_free
!= UINT64_MAX
) {
2007 disk_ceiling
= qi
.referenced
+ disk_free
;
2009 if (disk_size
!= UINT64_MAX
&& disk_ceiling
> disk_size
)
2010 disk_ceiling
= disk_size
;
2013 if (qi
.referenced_max
!= UINT64_MAX
) {
2014 if (disk_size
!= UINT64_MAX
)
2015 disk_size
= MIN(qi
.referenced_max
, disk_size
);
2017 disk_size
= qi
.referenced_max
;
2020 if (disk_size
!= UINT64_MAX
) {
2021 if (disk_size
> disk_usage
)
2022 disk_free
= disk_size
- disk_usage
;
2032 if (IN_SET(h
->record
->storage
, USER_CLASSIC
, USER_DIRECTORY
, USER_FSCRYPT
)) {
2033 r
= quotactl_path(QCMD_FIXED(Q_GETQUOTA
, USRQUOTA
), path
, h
->uid
, &req
);
2035 if (ERRNO_IS_NOT_SUPPORTED(r
)) {
2036 log_debug_errno(r
, "No UID quota support on %s.", path
);
2041 log_debug_errno(r
, "Failed to query disk quota for UID " UID_FMT
": %m", h
->uid
);
2045 disk_usage
= 0; /* No record of this user? then nothing was used */
2047 if (FLAGS_SET(req
.dqb_valid
, QIF_SPACE
) && disk_free
!= UINT64_MAX
) {
2048 disk_ceiling
= req
.dqb_curspace
+ disk_free
;
2050 if (disk_size
!= UINT64_MAX
&& disk_ceiling
> disk_size
)
2051 disk_ceiling
= disk_size
;
2054 if (FLAGS_SET(req
.dqb_valid
, QIF_BLIMITS
)) {
2057 /* Take the minimum of the quota and the available disk space here */
2058 q
= req
.dqb_bhardlimit
* QIF_DQBLKSIZE
;
2059 if (disk_size
!= UINT64_MAX
)
2060 disk_size
= MIN(disk_size
, q
);
2064 if (FLAGS_SET(req
.dqb_valid
, QIF_SPACE
)) {
2065 disk_usage
= req
.dqb_curspace
;
2067 if (disk_size
!= UINT64_MAX
) {
2068 if (disk_size
> disk_usage
)
2069 disk_free
= disk_size
- disk_usage
;
2078 *ret_disk_size
= disk_size
;
2079 *ret_disk_usage
= disk_usage
;
2080 *ret_disk_free
= disk_free
;
2081 *ret_disk_ceiling
= disk_ceiling
;
2082 *ret_disk_floor
= disk_floor
;
2087 int home_augment_status(
2089 UserRecordLoadFlags flags
,
2092 uint64_t disk_size
= UINT64_MAX
, disk_usage
= UINT64_MAX
, disk_free
= UINT64_MAX
, disk_ceiling
= UINT64_MAX
, disk_floor
= UINT64_MAX
;
2093 _cleanup_(json_variant_unrefp
) JsonVariant
*j
= NULL
, *v
= NULL
, *m
= NULL
, *status
= NULL
;
2094 _cleanup_(user_record_unrefp
) UserRecord
*ur
= NULL
;
2095 char ids
[SD_ID128_STRING_MAX
];
2103 /* We are supposed to add this, this can't be on hence. */
2104 assert(!FLAGS_SET(flags
, USER_RECORD_STRIP_STATUS
));
2106 r
= sd_id128_get_machine(&id
);
2110 state
= home_get_state(h
);
2112 switch (h
->record
->storage
) {
2115 r
= home_get_disk_status_luks(h
, state
, &disk_size
, &disk_usage
, &disk_free
, &disk_ceiling
, &disk_floor
);
2122 case USER_DIRECTORY
:
2123 case USER_SUBVOLUME
:
2126 r
= home_get_disk_status_directory(h
, state
, &disk_size
, &disk_usage
, &disk_free
, &disk_ceiling
, &disk_floor
);
2136 if (disk_floor
== UINT64_MAX
|| (disk_usage
!= UINT64_MAX
&& disk_floor
< disk_usage
))
2137 disk_floor
= disk_usage
;
2138 if (disk_floor
== UINT64_MAX
|| disk_floor
< USER_DISK_SIZE_MIN
)
2139 disk_floor
= USER_DISK_SIZE_MIN
;
2140 if (disk_ceiling
== UINT64_MAX
|| disk_ceiling
> USER_DISK_SIZE_MAX
)
2141 disk_ceiling
= USER_DISK_SIZE_MAX
;
2143 r
= json_build(&status
,
2145 JSON_BUILD_PAIR("state", JSON_BUILD_STRING(home_state_to_string(state
))),
2146 JSON_BUILD_PAIR("service", JSON_BUILD_STRING("io.systemd.Home")),
2147 JSON_BUILD_PAIR_CONDITION(disk_size
!= UINT64_MAX
, "diskSize", JSON_BUILD_UNSIGNED(disk_size
)),
2148 JSON_BUILD_PAIR_CONDITION(disk_usage
!= UINT64_MAX
, "diskUsage", JSON_BUILD_UNSIGNED(disk_usage
)),
2149 JSON_BUILD_PAIR_CONDITION(disk_free
!= UINT64_MAX
, "diskFree", JSON_BUILD_UNSIGNED(disk_free
)),
2150 JSON_BUILD_PAIR_CONDITION(disk_ceiling
!= UINT64_MAX
, "diskCeiling", JSON_BUILD_UNSIGNED(disk_ceiling
)),
2151 JSON_BUILD_PAIR_CONDITION(disk_floor
!= UINT64_MAX
, "diskFloor", JSON_BUILD_UNSIGNED(disk_floor
)),
2152 JSON_BUILD_PAIR_CONDITION(h
->signed_locally
>= 0, "signedLocally", JSON_BUILD_BOOLEAN(h
->signed_locally
))
2157 j
= json_variant_ref(h
->record
->json
);
2158 v
= json_variant_ref(json_variant_by_key(j
, "status"));
2159 m
= json_variant_ref(json_variant_by_key(v
, sd_id128_to_string(id
, ids
)));
2161 r
= json_variant_filter(&m
, STRV_MAKE("diskSize", "diskUsage", "diskFree", "diskCeiling", "diskFloor", "signedLocally"));
2165 r
= json_variant_merge(&m
, status
);
2169 r
= json_variant_set_field(&v
, ids
, m
);
2173 r
= json_variant_set_field(&j
, "status", v
);
2177 ur
= user_record_new();
2181 r
= user_record_load(ur
, j
, flags
);
2186 FLAGS_SET(h
->record
->mask
, USER_RECORD_PRIVILEGED
) &&
2187 !FLAGS_SET(ur
->mask
, USER_RECORD_PRIVILEGED
);
2189 *ret
= TAKE_PTR(ur
);
2193 static int on_home_ref_eof(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
2194 _cleanup_(operation_unrefp
) Operation
*o
= NULL
;
2200 if (h
->ref_event_source_please_suspend
== s
)
2201 h
->ref_event_source_please_suspend
= sd_event_source_disable_unref(h
->ref_event_source_please_suspend
);
2203 if (h
->ref_event_source_dont_suspend
== s
)
2204 h
->ref_event_source_dont_suspend
= sd_event_source_disable_unref(h
->ref_event_source_dont_suspend
);
2206 if (h
->ref_event_source_dont_suspend
|| h
->ref_event_source_please_suspend
)
2209 log_info("Got notification that all sessions of user %s ended, deactivating automatically.", h
->user_name
);
2211 o
= operation_new(OPERATION_PIPE_EOF
, NULL
);
2217 home_schedule_operation(h
, o
, NULL
);
2221 int home_create_fifo(Home
*h
, bool please_suspend
) {
2222 _cleanup_close_
int ret_fd
= -1;
2223 sd_event_source
**ss
;
2224 const char *fn
, *suffix
;
2229 if (please_suspend
) {
2230 suffix
= ".please-suspend";
2231 ss
= &h
->ref_event_source_please_suspend
;
2233 suffix
= ".dont-suspend";
2234 ss
= &h
->ref_event_source_dont_suspend
;
2237 fn
= strjoina("/run/systemd/home/", h
->user_name
, suffix
);
2240 _cleanup_close_
int ref_fd
= -1;
2242 (void) mkdir("/run/systemd/home/", 0755);
2243 if (mkfifo(fn
, 0600) < 0 && errno
!= EEXIST
)
2244 return log_error_errno(errno
, "Failed to create FIFO %s: %m", fn
);
2246 ref_fd
= open(fn
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
2248 return log_error_errno(errno
, "Failed to open FIFO %s for reading: %m", fn
);
2250 r
= sd_event_add_io(h
->manager
->event
, ss
, ref_fd
, 0, on_home_ref_eof
, h
);
2252 return log_error_errno(r
, "Failed to allocate reference FIFO event source: %m");
2254 (void) sd_event_source_set_description(*ss
, "acquire-ref");
2256 r
= sd_event_source_set_priority(*ss
, SD_EVENT_PRIORITY_IDLE
-1);
2260 r
= sd_event_source_set_io_fd_own(*ss
, true);
2262 return log_error_errno(r
, "Failed to pass ownership of FIFO event fd to event source: %m");
2267 ret_fd
= open(fn
, O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
);
2269 return log_error_errno(errno
, "Failed to open FIFO %s for writing: %m", fn
);
2271 return TAKE_FD(ret_fd
);
2274 static int home_dispatch_acquire(Home
*h
, Operation
*o
) {
2275 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2276 int (*call
)(Home
*h
, UserRecord
*secret
, HomeState for_state
, sd_bus_error
*error
) = NULL
;
2277 HomeState for_state
;
2282 assert(o
->type
== OPERATION_ACQUIRE
);
2284 switch (home_get_state(h
)) {
2286 case HOME_UNFIXATED
:
2287 for_state
= HOME_FIXATING_FOR_ACQUIRE
;
2288 call
= home_fixate_internal
;
2292 r
= sd_bus_error_setf(&error
, BUS_ERROR_HOME_ABSENT
, "Home %s is currently missing or not plugged in.", h
->user_name
);
2296 for_state
= HOME_ACTIVATING_FOR_ACQUIRE
;
2297 call
= home_activate_internal
;
2301 for_state
= HOME_AUTHENTICATING_FOR_ACQUIRE
;
2302 call
= home_authenticate_internal
;
2306 for_state
= HOME_UNLOCKING_FOR_ACQUIRE
;
2307 call
= home_unlock_internal
;
2311 /* All other cases means we are currently executing an operation, which means the job remains
2316 assert(!h
->current_operation
);
2319 r
= home_ratelimit(h
, &error
);
2321 r
= call(h
, o
->secret
, for_state
, &error
);
2324 if (r
!= 0) /* failure or completed */
2325 operation_result(o
, r
, &error
);
2327 h
->current_operation
= operation_ref(o
);
2332 static int home_dispatch_release(Home
*h
, Operation
*o
) {
2333 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2338 assert(o
->type
== OPERATION_RELEASE
);
2340 if (h
->ref_event_source_dont_suspend
|| h
->ref_event_source_please_suspend
)
2341 /* If there's now a reference again, then let's abort the release attempt */
2342 r
= sd_bus_error_setf(&error
, BUS_ERROR_HOME_BUSY
, "Home %s is currently referenced.", h
->user_name
);
2344 switch (home_get_state(h
)) {
2346 case HOME_UNFIXATED
:
2353 r
= sd_bus_error_setf(&error
, BUS_ERROR_HOME_LOCKED
, "Home %s is currently locked.", h
->user_name
);
2357 r
= home_deactivate_internal(h
, false, &error
);
2361 /* All other cases means we are currently executing an operation, which means the job remains
2367 assert(!h
->current_operation
);
2369 if (r
<= 0) /* failure or completed */
2370 operation_result(o
, r
, &error
);
2372 h
->current_operation
= operation_ref(o
);
2377 static int home_dispatch_lock_all(Home
*h
, Operation
*o
) {
2378 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2383 assert(o
->type
== OPERATION_LOCK_ALL
);
2385 switch (home_get_state(h
)) {
2387 case HOME_UNFIXATED
:
2390 log_info("Home %s is not active, no locking necessary.", h
->user_name
);
2395 log_info("Home %s is already locked.", h
->user_name
);
2400 log_info("Locking home %s.", h
->user_name
);
2401 r
= home_lock(h
, &error
);
2405 /* All other cases means we are currently executing an operation, which means the job remains
2410 assert(!h
->current_operation
);
2412 if (r
!= 0) /* failure or completed */
2413 operation_result(o
, r
, &error
);
2415 h
->current_operation
= operation_ref(o
);
2420 static int home_dispatch_pipe_eof(Home
*h
, Operation
*o
) {
2421 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2426 assert(o
->type
== OPERATION_PIPE_EOF
);
2428 if (h
->ref_event_source_please_suspend
|| h
->ref_event_source_dont_suspend
)
2429 return 1; /* Hmm, there's a reference again, let's cancel this */
2431 switch (home_get_state(h
)) {
2433 case HOME_UNFIXATED
:
2436 log_info("Home %s already deactivated, no automatic deactivation needed.", h
->user_name
);
2439 case HOME_DEACTIVATING
:
2440 log_info("Home %s is already being deactivated, automatic deactivated unnecessary.", h
->user_name
);
2444 r
= home_deactivate_internal(h
, false, &error
);
2446 log_warning_errno(r
, "Failed to deactivate %s, ignoring: %s", h
->user_name
, bus_error_message(&error
, r
));
2451 /* If the device is locked or any operation is being executed, let's leave this pending */
2455 /* Note that we don't call operation_fail() or operation_success() here, because this kind of
2456 * operation has no message associated with it, and thus there's no need to propagate success. */
2458 assert(!o
->message
);
2462 static int home_dispatch_deactivate_force(Home
*h
, Operation
*o
) {
2463 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2468 assert(o
->type
== OPERATION_DEACTIVATE_FORCE
);
2470 switch (home_get_state(h
)) {
2472 case HOME_UNFIXATED
:
2475 log_debug("Home %s already deactivated, no forced deactivation due to unplug needed.", h
->user_name
);
2478 case HOME_DEACTIVATING
:
2479 log_debug("Home %s is already being deactivated, forced deactivation due to unplug unnecessary.", h
->user_name
);
2484 r
= home_deactivate_internal(h
, true, &error
);
2486 log_warning_errno(r
, "Failed to forcibly deactivate %s, ignoring: %s", h
->user_name
, bus_error_message(&error
, r
));
2490 /* If any operation is being executed, let's leave this pending */
2494 /* Note that we don't call operation_fail() or operation_success() here, because this kind of
2495 * operation has no message associated with it, and thus there's no need to propagate success. */
2497 assert(!o
->message
);
2501 static int on_pending(sd_event_source
*s
, void *userdata
) {
2509 o
= ordered_set_first(h
->pending_operations
);
2511 static int (* const operation_table
[_OPERATION_MAX
])(Home
*h
, Operation
*o
) = {
2512 [OPERATION_ACQUIRE
] = home_dispatch_acquire
,
2513 [OPERATION_RELEASE
] = home_dispatch_release
,
2514 [OPERATION_LOCK_ALL
] = home_dispatch_lock_all
,
2515 [OPERATION_PIPE_EOF
] = home_dispatch_pipe_eof
,
2516 [OPERATION_DEACTIVATE_FORCE
] = home_dispatch_deactivate_force
,
2519 assert(operation_table
[o
->type
]);
2520 r
= operation_table
[o
->type
](h
, o
);
2522 /* The operation completed, let's remove it from the pending list, and exit while
2523 * leaving the event source enabled as it is. */
2524 assert_se(ordered_set_remove(h
->pending_operations
, o
) == o
);
2530 /* Nothing to do anymore, let's turn off this event source */
2531 r
= sd_event_source_set_enabled(s
, SD_EVENT_OFF
);
2533 return log_error_errno(r
, "Failed to disable event source: %m");
2538 int home_schedule_operation(Home
*h
, Operation
*o
, sd_bus_error
*error
) {
2544 if (ordered_set_size(h
->pending_operations
) >= PENDING_OPERATIONS_MAX
)
2545 return sd_bus_error_setf(error
, BUS_ERROR_TOO_MANY_OPERATIONS
, "Too many client operations requested");
2547 r
= ordered_set_ensure_allocated(&h
->pending_operations
, &operation_hash_ops
);
2551 r
= ordered_set_put(h
->pending_operations
, o
);
2558 if (!h
->pending_event_source
) {
2559 r
= sd_event_add_defer(h
->manager
->event
, &h
->pending_event_source
, on_pending
, h
);
2561 return log_error_errno(r
, "Failed to allocate pending defer event source: %m");
2563 (void) sd_event_source_set_description(h
->pending_event_source
, "pending");
2565 r
= sd_event_source_set_priority(h
->pending_event_source
, SD_EVENT_PRIORITY_IDLE
);
2570 r
= sd_event_source_set_enabled(h
->pending_event_source
, SD_EVENT_ON
);
2572 return log_error_errno(r
, "Failed to trigger pending event source: %m");
2577 static int home_get_image_path_seat(Home
*h
, char **ret
) {
2578 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
2579 _cleanup_free_
char *c
= NULL
;
2580 const char *ip
, *seat
;
2586 if (user_record_storage(h
->record
) != USER_LUKS
)
2589 ip
= user_record_image_path(h
->record
);
2593 if (!path_startswith(ip
, "/dev/"))
2596 if (stat(ip
, &st
) < 0)
2599 if (!S_ISBLK(st
.st_mode
))
2602 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
2606 r
= sd_device_get_property_value(d
, "ID_SEAT", &seat
);
2607 if (r
== -ENOENT
) /* no property means seat0 */
2620 int home_auto_login(Home
*h
, char ***ret_seats
) {
2621 _cleanup_free_
char *seat
= NULL
, *seat2
= NULL
;
2626 (void) home_get_image_path_seat(h
, &seat
);
2628 if (h
->record
->auto_login
> 0 && !streq_ptr(seat
, "seat0")) {
2629 /* For now, when the auto-login boolean is set for a user, let's make it mean
2630 * "seat0". Eventually we can extend the concept and allow configuration of any kind of seat,
2631 * but let's keep simple initially, most likely the feature is interesting on single-user
2632 * systems anyway, only.
2634 * We filter out users marked for auto-login in we know for sure their home directory is
2637 if (user_record_test_image_path(h
->record
) != USER_TEST_ABSENT
) {
2638 seat2
= strdup("seat0");
2644 if (seat
|| seat2
) {
2645 _cleanup_strv_free_
char **list
= NULL
;
2648 list
= new(char*, 3);
2653 list
[i
++] = TAKE_PTR(seat
);
2655 list
[i
++] = TAKE_PTR(seat2
);
2658 *ret_seats
= TAKE_PTR(list
);
2666 int home_set_current_message(Home
*h
, sd_bus_message
*m
) {
2672 if (h
->current_operation
)
2675 h
->current_operation
= operation_new(OPERATION_IMMEDIATE
, m
);
2676 if (!h
->current_operation
)
2682 static const char* const home_state_table
[_HOME_STATE_MAX
] = {
2683 [HOME_UNFIXATED
] = "unfixated",
2684 [HOME_ABSENT
] = "absent",
2685 [HOME_INACTIVE
] = "inactive",
2686 [HOME_FIXATING
] = "fixating",
2687 [HOME_FIXATING_FOR_ACTIVATION
] = "fixating-for-activation",
2688 [HOME_FIXATING_FOR_ACQUIRE
] = "fixating-for-acquire",
2689 [HOME_ACTIVATING
] = "activating",
2690 [HOME_ACTIVATING_FOR_ACQUIRE
] = "activating-for-acquire",
2691 [HOME_DEACTIVATING
] = "deactivating",
2692 [HOME_ACTIVE
] = "active",
2693 [HOME_LOCKING
] = "locking",
2694 [HOME_LOCKED
] = "locked",
2695 [HOME_UNLOCKING
] = "unlocking",
2696 [HOME_UNLOCKING_FOR_ACQUIRE
] = "unlocking-for-acquire",
2697 [HOME_CREATING
] = "creating",
2698 [HOME_REMOVING
] = "removing",
2699 [HOME_UPDATING
] = "updating",
2700 [HOME_UPDATING_WHILE_ACTIVE
] = "updating-while-active",
2701 [HOME_RESIZING
] = "resizing",
2702 [HOME_RESIZING_WHILE_ACTIVE
] = "resizing-while-active",
2703 [HOME_PASSWD
] = "passwd",
2704 [HOME_PASSWD_WHILE_ACTIVE
] = "passwd-while-active",
2705 [HOME_AUTHENTICATING
] = "authenticating",
2706 [HOME_AUTHENTICATING_WHILE_ACTIVE
] = "authenticating-while-active",
2707 [HOME_AUTHENTICATING_FOR_ACQUIRE
] = "authenticating-for-acquire",
2710 DEFINE_STRING_TABLE_LOOKUP(home_state
, HomeState
);