1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "blockdev-util.h"
7 #include "chown-recursive.h"
12 #include "home-util.h"
13 #include "homework-cifs.h"
14 #include "homework-directory.h"
15 #include "homework-fido2.h"
16 #include "homework-fscrypt.h"
17 #include "homework-luks.h"
18 #include "homework-mount.h"
19 #include "homework-pkcs11.h"
21 #include "libcrypt-util.h"
22 #include "main-func.h"
23 #include "memory-util.h"
24 #include "missing_magic.h"
25 #include "mount-util.h"
26 #include "path-util.h"
27 #include "recovery-key.h"
29 #include "stat-util.h"
31 #include "sync-util.h"
32 #include "tmpfile-util.h"
33 #include "user-util.h"
36 /* Make sure a bad password always results in a 3s delay, no matter what */
37 #define BAD_PASSWORD_DELAY_USEC (3 * USEC_PER_SEC)
39 void password_cache_free(PasswordCache
*cache
) {
43 cache
->pkcs11_passwords
= strv_free_erase(cache
->pkcs11_passwords
);
44 cache
->fido2_passwords
= strv_free_erase(cache
->fido2_passwords
);
47 int user_record_authenticate(
53 bool need_password
= false, need_recovery_key
= false, need_token
= false, need_pin
= false,
54 need_protected_authentication_path_permitted
= false, need_user_presence_permitted
= false,
55 need_user_verification_permitted
= false, pin_locked
= false, pin_incorrect
= false,
56 pin_incorrect_few_tries_left
= false, pin_incorrect_one_try_left
= false, token_action_timeout
= false;
62 /* Tries to authenticate a user record with the supplied secrets. i.e. checks whether at least one
63 * supplied plaintext passwords matches a hashed password field of the user record. Or if a
64 * configured PKCS#11 or FIDO2 token is around and can unlock the record.
66 * Note that the 'cache' parameter is both an input and output parameter: it contains lists of
67 * configured, decrypted PKCS#11/FIDO2 passwords. We typically have to call this function multiple
68 * times over the course of an operation (think: on login we authenticate the host user record, the
69 * record embedded in the LUKS record and the one embedded in $HOME). Hence we keep a list of
70 * passwords we already decrypted, so that we don't have to do the (slow and potentially interactive)
71 * PKCS#11/FIDO2 dance for the relevant token again and again. */
73 /* First, let's see if the supplied plain-text passwords work? */
74 r
= user_record_test_password(h
, secret
);
78 log_debug_errno(r
, "User record has no hashed passwords, plaintext passwords not tested.");
80 return log_error_errno(r
, "Failed to validate password of record: %m");
82 log_info("Provided password unlocks user record.");
86 /* Similar, but test against the recovery keys */
87 r
= user_record_test_recovery_key(h
, secret
);
89 need_recovery_key
= true;
91 log_debug_errno(r
, "User record has no recovery keys, plaintext passwords not tested against it.");
93 return log_error_errno(r
, "Failed to validate the recovery key of the record: %m");
95 log_info("Provided password is a recovery key that unlocks the user record.");
99 if (need_password
&& need_recovery_key
)
100 log_info("None of the supplied plaintext passwords unlock the user record's hashed passwords or recovery keys.");
101 else if (need_password
)
102 log_info("None of the supplied plaintext passwords unlock the user record's hashed passwords.");
104 log_info("None of the supplied plaintext passwords unlock the user record's hashed recovery keys.");
106 /* Second, test cached PKCS#11 passwords */
107 for (size_t n
= 0; n
< h
->n_pkcs11_encrypted_key
; n
++) {
110 STRV_FOREACH(pp
, cache
->pkcs11_passwords
) {
111 r
= test_password_one(h
->pkcs11_encrypted_key
[n
].hashed_password
, *pp
);
113 return log_error_errno(r
, "Failed to check supplied PKCS#11 password: %m");
115 log_info("Previously acquired PKCS#11 password unlocks user record.");
121 /* Third, test cached FIDO2 passwords */
122 for (size_t n
= 0; n
< h
->n_fido2_hmac_salt
; n
++) {
125 /* See if any of the previously calculated passwords work */
126 STRV_FOREACH(pp
, cache
->fido2_passwords
) {
127 r
= test_password_one(h
->fido2_hmac_salt
[n
].hashed_password
, *pp
);
129 return log_error_errno(r
, "Failed to check supplied FIDO2 password: %m");
131 log_info("Previously acquired FIDO2 password unlocks user record.");
137 /* Fourth, let's see if any of the PKCS#11 security tokens are plugged in and help us */
138 for (size_t n
= 0; n
< h
->n_pkcs11_encrypted_key
; n
++) {
140 _cleanup_(pkcs11_callback_data_release
) struct pkcs11_callback_data data
= {
143 .encrypted_key
= h
->pkcs11_encrypted_key
+ n
,
146 r
= pkcs11_find_token(data
.encrypted_key
->uri
, pkcs11_callback
, &data
);
155 need_protected_authentication_path_permitted
= true;
161 pin_incorrect
= true;
164 pin_incorrect
= pin_incorrect_few_tries_left
= true;
167 pin_incorrect
= pin_incorrect_few_tries_left
= pin_incorrect_one_try_left
= true;
173 r
= test_password_one(data
.encrypted_key
->hashed_password
, data
.decrypted_password
);
175 return log_error_errno(r
, "Failed to test PKCS#11 password: %m");
177 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Configured PKCS#11 security token %s does not decrypt encrypted key correctly.", data
.encrypted_key
->uri
);
179 log_info("Decrypted password from PKCS#11 security token %s unlocks user record.", data
.encrypted_key
->uri
);
181 r
= strv_extend(&cache
->pkcs11_passwords
, data
.decrypted_password
);
193 /* Fifth, let's see if any of the FIDO2 security tokens are plugged in and help us */
194 for (size_t n
= 0; n
< h
->n_fido2_hmac_salt
; n
++) {
196 _cleanup_(erase_and_freep
) char *decrypted_password
= NULL
;
198 r
= fido2_use_token(h
, secret
, h
->fido2_hmac_salt
+ n
, &decrypted_password
);
210 pin_incorrect
= true;
213 need_user_presence_permitted
= true;
216 need_user_verification_permitted
= true;
219 token_action_timeout
= true;
225 r
= test_password_one(h
->fido2_hmac_salt
[n
].hashed_password
, decrypted_password
);
227 return log_error_errno(r
, "Failed to test FIDO2 password: %m");
229 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Configured FIDO2 security token does not decrypt encrypted key correctly.");
231 log_info("Decrypted password from FIDO2 security token unlocks user record.");
233 r
= strv_extend(&cache
->fido2_passwords
, decrypted_password
);
245 /* Ordered by "relevance", i.e. the most "important" or "interesting" error condition is returned. */
246 if (pin_incorrect_one_try_left
)
248 if (pin_incorrect_few_tries_left
)
249 return -ETOOMANYREFS
;
254 if (token_action_timeout
)
256 if (need_protected_authentication_path_permitted
)
258 if (need_user_presence_permitted
)
260 if (need_user_verification_permitted
)
268 if (need_recovery_key
)
271 /* Hmm, this means neither PCKS#11/FIDO2 nor classic hashed passwords or recovery keys were supplied,
272 * we cannot authenticate this reasonably */
274 return log_debug_errno(SYNTHETIC_ERRNO(EKEYREVOKED
),
275 "No hashed passwords, no recovery keys and no PKCS#11/FIDO2 tokens defined, cannot authenticate user record, refusing.");
277 /* If strict verification is off this means we are possibly in the case where we encountered an
278 * unfixated record, i.e. a synthetic one that accordingly lacks any authentication data. In this
279 * case, allow the authentication to pass for now, so that the second (or third) authentication level
280 * (the ones of the user record in the LUKS header or inside the home directory) will then catch
281 * invalid passwords. The second/third authentication always runs in strict verification mode. */
282 log_debug("No hashed passwords, not recovery keys and no PKCS#11 tokens defined in record, cannot authenticate user record. "
283 "Deferring to embedded user record.");
287 static void drop_caches_now(void) {
290 /* Drop file system caches now. See https://www.kernel.org/doc/Documentation/sysctl/vm.txt for
291 * details. We write "2" into /proc/sys/vm/drop_caches to ensure dentries/inodes are flushed, but not
294 r
= write_string_file("/proc/sys/vm/drop_caches", "2\n", WRITE_STRING_FILE_DISABLE_BUFFER
);
296 log_warning_errno(r
, "Failed to drop caches, ignoring: %m");
298 log_debug("Dropped caches.");
301 int home_setup_done(HomeSetup
*setup
) {
306 if (setup
->root_fd
>= 0) {
307 if (setup
->do_offline_fitrim
) {
308 q
= run_fitrim(setup
->root_fd
);
313 if (syncfs(setup
->root_fd
) < 0)
314 log_debug_errno(errno
, "Failed to synchronize home directory, ignoring: %m");
316 setup
->root_fd
= safe_close(setup
->root_fd
);
319 if (setup
->undo_mount
) {
320 q
= umount_verbose(LOG_DEBUG
, HOME_RUNTIME_WORK_DIR
, UMOUNT_NOFOLLOW
);
325 if (setup
->undo_dm
&& setup
->crypt_device
&& setup
->dm_name
) {
326 q
= sym_crypt_deactivate_by_name(setup
->crypt_device
, setup
->dm_name
, 0);
331 if (setup
->image_fd
>= 0) {
332 if (setup
->do_offline_fallocate
) {
333 q
= run_fallocate(setup
->image_fd
, NULL
);
338 if (setup
->do_mark_clean
) {
339 q
= run_mark_dirty(setup
->image_fd
, false);
344 setup
->image_fd
= safe_close(setup
->image_fd
);
347 setup
->undo_mount
= false;
348 setup
->undo_dm
= false;
349 setup
->do_offline_fitrim
= false;
350 setup
->do_offline_fallocate
= false;
351 setup
->do_mark_clean
= false;
353 setup
->dm_name
= mfree(setup
->dm_name
);
354 setup
->dm_node
= mfree(setup
->dm_node
);
356 setup
->loop
= loop_device_unref(setup
->loop
);
357 if (setup
->crypt_device
) {
358 sym_crypt_free(setup
->crypt_device
);
359 setup
->crypt_device
= NULL
;
362 setup
->volume_key
= erase_and_free(setup
->volume_key
);
363 setup
->volume_key_size
= 0;
365 if (setup
->do_drop_caches
)
373 HomeSetupFlags flags
,
374 PasswordCache
*cache
,
376 UserRecord
**ret_header_home
) {
382 assert(!setup
->loop
);
383 assert(!setup
->crypt_device
);
384 assert(setup
->root_fd
< 0);
385 assert(!setup
->undo_dm
);
386 assert(!setup
->undo_mount
);
388 /* Makes a home directory accessible (through the root_fd file descriptor, not by path!). */
390 if (!FLAGS_SET(flags
, HOME_SETUP_ALREADY_ACTIVATED
)) /* If we set up the directory, we should also drop caches once we are done */
391 setup
->do_drop_caches
= setup
->do_drop_caches
|| user_record_drop_caches(h
);
393 switch (user_record_storage(h
)) {
396 return home_setup_luks(h
, flags
, NULL
, cache
, setup
, ret_header_home
);
400 r
= home_setup_directory(h
, setup
);
404 r
= home_setup_fscrypt(h
, cache
, setup
);
408 r
= home_setup_cifs(h
, flags
, setup
);
412 return log_error_errno(SYNTHETIC_ERRNO(ENOLINK
), "Processing home directories of type '%s' currently not supported.", user_storage_to_string(user_record_storage(h
)));
419 *ret_header_home
= NULL
;
424 int home_sync_and_statfs(int root_fd
, struct statfs
*ret
) {
425 assert(root_fd
>= 0);
427 /* Let's sync this to disk, so that the disk space reported by fstatfs() below is accurate (for file
428 * systems such as btrfs where this is determined lazily). */
430 if (syncfs(root_fd
) < 0)
431 return log_error_errno(errno
, "Failed to synchronize file system: %m");
434 if (fstatfs(root_fd
, ret
) < 0)
435 return log_error_errno(errno
, "Failed to statfs() file system: %m");
437 log_info("Synchronized disk.");
442 static int read_identity_file(int root_fd
, JsonVariant
**ret
) {
443 _cleanup_(fclosep
) FILE *identity_file
= NULL
;
444 _cleanup_close_
int identity_fd
= -1;
445 unsigned line
, column
;
448 assert(root_fd
>= 0);
451 identity_fd
= openat(root_fd
, ".identity", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
|O_NONBLOCK
);
453 return log_error_errno(errno
, "Failed to open .identity file in home directory: %m");
455 r
= fd_verify_regular(identity_fd
);
457 return log_error_errno(r
, "Embedded identity file is not a regular file, refusing: %m");
459 identity_file
= take_fdopen(&identity_fd
, "r");
463 r
= json_parse_file(identity_file
, ".identity", JSON_PARSE_SENSITIVE
, ret
, &line
, &column
);
465 return log_error_errno(r
, "[.identity:%u:%u] Failed to parse JSON data: %m", line
, column
);
467 log_info("Read embedded .identity file.");
472 static int write_identity_file(int root_fd
, JsonVariant
*v
, uid_t uid
) {
473 _cleanup_(json_variant_unrefp
) JsonVariant
*normalized
= NULL
;
474 _cleanup_(fclosep
) FILE *identity_file
= NULL
;
475 _cleanup_close_
int identity_fd
= -1;
476 _cleanup_free_
char *fn
= NULL
;
479 assert(root_fd
>= 0);
482 normalized
= json_variant_ref(v
);
484 r
= json_variant_normalize(&normalized
);
486 log_warning_errno(r
, "Failed to normalize user record, ignoring: %m");
488 r
= tempfn_random(".identity", NULL
, &fn
);
492 identity_fd
= openat(root_fd
, fn
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0600);
494 return log_error_errno(errno
, "Failed to create .identity file in home directory: %m");
496 identity_file
= take_fdopen(&identity_fd
, "w");
497 if (!identity_file
) {
502 json_variant_dump(normalized
, JSON_FORMAT_PRETTY
, identity_file
, NULL
);
504 r
= fflush_and_check(identity_file
);
506 log_error_errno(r
, "Failed to write .identity file: %m");
510 if (fchown(fileno(identity_file
), uid
, uid
) < 0) {
511 log_error_errno(r
, "Failed to change ownership of identity file: %m");
515 if (renameat(root_fd
, fn
, root_fd
, ".identity") < 0) {
516 r
= log_error_errno(errno
, "Failed to move identity file into place: %m");
520 log_info("Wrote embedded .identity file.");
525 (void) unlinkat(root_fd
, fn
, 0);
529 int home_load_embedded_identity(
532 UserRecord
*header_home
,
533 UserReconcileMode mode
,
534 PasswordCache
*cache
,
535 UserRecord
**ret_embedded_home
,
536 UserRecord
**ret_new_home
) {
538 _cleanup_(user_record_unrefp
) UserRecord
*embedded_home
= NULL
, *intermediate_home
= NULL
, *new_home
= NULL
;
539 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
543 assert(root_fd
>= 0);
545 r
= read_identity_file(root_fd
, &v
);
549 embedded_home
= user_record_new();
553 r
= user_record_load(embedded_home
, v
, USER_RECORD_LOAD_EMBEDDED
|USER_RECORD_PERMISSIVE
);
557 if (!user_record_compatible(h
, embedded_home
))
558 return log_error_errno(SYNTHETIC_ERRNO(EREMCHG
), "Embedded home record not compatible with host record, refusing.");
560 /* Insist that credentials the user supplies also unlocks any embedded records. */
561 r
= user_record_authenticate(embedded_home
, h
, cache
, /* strict_verify= */ true);
564 assert(r
> 0); /* Insist that a password was verified */
566 /* At this point we have three records to deal with:
568 * · The record we got passed from the host
569 * · The record included in the LUKS header (only if LUKS is used)
570 * · The record in the home directory itself (~.identity)
572 * Now we have to reconcile all three, and let the newest one win. */
575 /* Note we relax the requirements here. Instead of insisting that the host record is strictly
576 * newer, let's also be OK if its equally new. If it is, we'll however insist that the
577 * embedded record must be newer, so that we update at least one of the two. */
579 r
= user_record_reconcile(h
, header_home
, mode
== USER_RECONCILE_REQUIRE_NEWER
? USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL
: mode
, &intermediate_home
);
580 if (r
== -EREMCHG
) /* this was supposed to be checked earlier already, but let's check this again */
581 return log_error_errno(r
, "Identity stored on host and in header don't match, refusing.");
583 return log_error_errno(r
, "Embedded identity record is newer than supplied record, refusing.");
585 return log_error_errno(r
, "Failed to reconcile host and header identities: %m");
586 if (r
== USER_RECONCILE_EMBEDDED_WON
)
587 log_info("Reconciling header user identity completed (header version was newer).");
588 else if (r
== USER_RECONCILE_HOST_WON
) {
589 log_info("Reconciling header user identity completed (host version was newer).");
591 if (mode
== USER_RECONCILE_REQUIRE_NEWER
) /* Host version is newer than the header
592 * version, hence we'll update
593 * something. This means we can relax the
594 * requirements on the embedded
596 mode
= USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL
;
598 assert(r
== USER_RECONCILE_IDENTICAL
);
599 log_info("Reconciling user identities completed (host and header version were identical).");
602 h
= intermediate_home
;
605 r
= user_record_reconcile(h
, embedded_home
, mode
, &new_home
);
607 return log_error_errno(r
, "Identity stored on host and in home don't match, refusing.");
609 return log_error_errno(r
, "Embedded identity record is equally new or newer than supplied record, refusing.");
611 return log_error_errno(r
, "Failed to reconcile host and embedded identities: %m");
612 if (r
== USER_RECONCILE_EMBEDDED_WON
)
613 log_info("Reconciling embedded user identity completed (embedded version was newer).");
614 else if (r
== USER_RECONCILE_HOST_WON
)
615 log_info("Reconciling embedded user identity completed (host version was newer).");
617 assert(r
== USER_RECONCILE_IDENTICAL
);
618 log_info("Reconciling embedded user identity completed (host and embedded version were identical).");
621 if (ret_embedded_home
)
622 *ret_embedded_home
= TAKE_PTR(embedded_home
);
625 *ret_new_home
= TAKE_PTR(new_home
);
630 int home_store_embedded_identity(UserRecord
*h
, int root_fd
, uid_t uid
, UserRecord
*old_home
) {
631 _cleanup_(user_record_unrefp
) UserRecord
*embedded
= NULL
;
635 assert(root_fd
>= 0);
636 assert(uid_is_valid(uid
));
638 r
= user_record_clone(h
, USER_RECORD_EXTRACT_EMBEDDED
|USER_RECORD_PERMISSIVE
, &embedded
);
640 return log_error_errno(r
, "Failed to determine new embedded record: %m");
642 if (old_home
&& user_record_equal(old_home
, embedded
)) {
643 log_debug("Not updating embedded home record.");
647 /* The identity has changed, let's update it in the image */
648 r
= write_identity_file(root_fd
, embedded
->json
, h
->uid
);
655 static const char *file_system_type_fd(int fd
) {
660 if (fstatfs(fd
, &sfs
) < 0) {
661 log_debug_errno(errno
, "Failed to statfs(): %m");
665 if (is_fs_type(&sfs
, XFS_SB_MAGIC
))
667 if (is_fs_type(&sfs
, EXT4_SUPER_MAGIC
))
669 if (is_fs_type(&sfs
, BTRFS_SUPER_MAGIC
))
675 int home_extend_embedded_identity(UserRecord
*h
, UserRecord
*used
, HomeSetup
*setup
) {
682 r
= user_record_add_binding(
684 user_record_storage(used
),
685 user_record_image_path(used
),
686 setup
->found_partition_uuid
,
687 setup
->found_luks_uuid
,
688 setup
->found_fs_uuid
,
689 setup
->crypt_device
? sym_crypt_get_cipher(setup
->crypt_device
) : NULL
,
690 setup
->crypt_device
? sym_crypt_get_cipher_mode(setup
->crypt_device
) : NULL
,
691 setup
->crypt_device
? luks_volume_key_size_convert(setup
->crypt_device
) : UINT64_MAX
,
692 file_system_type_fd(setup
->root_fd
),
693 user_record_home_directory(used
),
697 return log_error_errno(r
, "Failed to update binding in record: %m");
702 static int chown_recursive_directory(int root_fd
, uid_t uid
) {
705 assert(root_fd
>= 0);
706 assert(uid_is_valid(uid
));
708 r
= fd_chown_recursive(root_fd
, uid
, (gid_t
) uid
, 0777);
710 return log_error_errno(r
, "Failed to change ownership of files and directories: %m");
712 log_info("Recursive changing of ownership not necessary, skipped.");
714 log_info("Recursive changing of ownership completed.");
719 int home_maybe_shift_uid(
723 _cleanup_close_
int mount_fd
= -1;
728 assert(setup
->root_fd
>= 0);
730 if (fstat(setup
->root_fd
, &st
) < 0)
731 return log_error_errno(errno
, "Failed to stat() home directory: %m");
733 /* Let's shift UIDs of this mount. Hopefully this makes the later chowning unnecessary. (Note that we
734 * also prefer to do UID mapping even if the UID already matches our goal UID. That's because we want
735 * to leave UIDs in the homed managed range unmapped.) */
736 (void) home_shift_uid(setup
->root_fd
, NULL
, st
.st_uid
, h
->uid
, &mount_fd
);
738 /* If this worked, then we'll have a reference to the mount now, which we can also use like an O_PATH
739 * fd to the new dir. Let's convert it into a proper O_DIRECTORY fd. */
741 safe_close(setup
->root_fd
);
743 setup
->root_fd
= fd_reopen(mount_fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
744 if (setup
->root_fd
< 0)
745 return log_error_errno(setup
->root_fd
, "Failed to convert mount fd into regular directory fd: %m");
754 UserRecord
*header_home
,
755 PasswordCache
*cache
,
756 struct statfs
*ret_statfs
,
757 UserRecord
**ret_new_home
) {
759 _cleanup_(user_record_unrefp
) UserRecord
*embedded_home
= NULL
, *new_home
= NULL
;
764 assert(ret_new_home
);
766 /* When activating a home directory, does the identity work: loads the identity from the $HOME
767 * directory, reconciles it with our idea, chown()s everything. */
769 r
= home_load_embedded_identity(h
, setup
->root_fd
, header_home
, USER_RECONCILE_ANY
, cache
, &embedded_home
, &new_home
);
773 r
= home_maybe_shift_uid(h
, setup
);
777 r
= home_store_header_identity_luks(new_home
, setup
, header_home
);
781 r
= home_store_embedded_identity(new_home
, setup
->root_fd
, h
->uid
, embedded_home
);
785 r
= chown_recursive_directory(setup
->root_fd
, h
->uid
);
789 r
= home_sync_and_statfs(setup
->root_fd
, ret_statfs
);
793 *ret_new_home
= TAKE_PTR(new_home
);
797 static int home_activate(UserRecord
*h
, UserRecord
**ret_home
) {
798 _cleanup_(home_setup_done
) HomeSetup setup
= HOME_SETUP_INIT
;
799 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
;
800 _cleanup_(password_cache_free
) PasswordCache cache
= {};
806 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User record lacks user name, refusing.");
807 if (!uid_is_valid(h
->uid
))
808 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User record lacks UID, refusing.");
809 if (!IN_SET(user_record_storage(h
), USER_LUKS
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
, USER_CIFS
))
810 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
), "Activating home directories of type '%s' currently not supported.", user_storage_to_string(user_record_storage(h
)));
812 r
= user_record_authenticate(h
, h
, &cache
, /* strict_verify= */ false);
816 r
= user_record_test_home_directory_and_warn(h
);
819 if (r
== USER_TEST_MOUNTED
)
820 return log_error_errno(SYNTHETIC_ERRNO(EALREADY
), "Home directory %s is already mounted, refusing.", user_record_home_directory(h
));
822 r
= user_record_test_image_path_and_warn(h
);
825 if (r
== USER_TEST_ABSENT
)
826 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
), "Image path %s is missing, refusing.", user_record_image_path(h
));
828 switch (user_record_storage(h
)) {
831 r
= home_activate_luks(h
, &setup
, &cache
, &new_home
);
840 r
= home_activate_directory(h
, &setup
, &cache
, &new_home
);
847 r
= home_activate_cifs(h
, &setup
, &cache
, &new_home
);
854 assert_not_reached();
857 /* Note that the returned object might either be a reference to an updated version of the existing
858 * home object, or a reference to a newly allocated home object. The caller has to be able to deal
859 * with both, and consider the old object out-of-date. */
860 if (user_record_equal(h
, new_home
)) {
862 return 0; /* no identity change */
865 *ret_home
= TAKE_PTR(new_home
);
866 return 1; /* identity updated */
869 static int home_deactivate(UserRecord
*h
, bool force
) {
876 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User record incomplete, refusing.");
877 if (!IN_SET(user_record_storage(h
), USER_LUKS
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
, USER_CIFS
))
878 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
), "Deactivating home directories of type '%s' currently not supported.", user_storage_to_string(user_record_storage(h
)));
880 r
= user_record_test_home_directory_and_warn(h
);
883 if (r
== USER_TEST_MOUNTED
) {
884 if (user_record_storage(h
) == USER_LUKS
) {
885 r
= home_trim_luks(h
);
890 /* Sync explicitly, so that the drop caches logic below can work as documented */
891 r
= syncfs_path(AT_FDCWD
, user_record_home_directory(h
));
893 log_debug_errno(r
, "Failed to synchronize home directory, ignoring: %m");
895 log_info("Syncing completed.");
897 if (umount2(user_record_home_directory(h
), UMOUNT_NOFOLLOW
| (force
? MNT_FORCE
|MNT_DETACH
: 0)) < 0)
898 return log_error_errno(errno
, "Failed to unmount %s: %m", user_record_home_directory(h
));
900 log_info("Unmounting completed.");
903 log_info("Directory %s is already unmounted.", user_record_home_directory(h
));
905 if (user_record_storage(h
) == USER_LUKS
) {
906 r
= home_deactivate_luks(h
);
914 return log_error_errno(SYNTHETIC_ERRNO(ENOEXEC
), "Home is not active.");
916 if (user_record_drop_caches(h
))
919 log_info("Everything completed.");
923 static int copy_skel(int root_fd
, const char *skel
) {
926 assert(root_fd
>= 0);
928 r
= copy_tree_at(AT_FDCWD
, skel
, root_fd
, ".", UID_INVALID
, GID_INVALID
, COPY_MERGE
|COPY_REPLACE
);
930 log_info("Skeleton directory %s missing, ignoring.", skel
);
934 return log_error_errno(r
, "Failed to copy in %s: %m", skel
);
936 log_info("Copying in %s completed.", skel
);
940 static int change_access_mode(int root_fd
, mode_t m
) {
941 assert(root_fd
>= 0);
943 if (fchmod(root_fd
, m
) < 0)
944 return log_error_errno(errno
, "Failed to change access mode of top-level directory: %m");
946 log_info("Changed top-level directory access mode to 0%o.", m
);
950 int home_populate(UserRecord
*h
, int dir_fd
) {
956 r
= copy_skel(dir_fd
, user_record_skeleton_directory(h
));
960 r
= home_store_embedded_identity(h
, dir_fd
, h
->uid
, NULL
);
964 r
= chown_recursive_directory(dir_fd
, h
->uid
);
968 r
= change_access_mode(dir_fd
, user_record_access_mode(h
));
975 static int user_record_compile_effective_passwords(
977 PasswordCache
*cache
,
978 char ***ret_effective_passwords
) {
980 _cleanup_(strv_free_erasep
) char **effective
= NULL
;
988 /* We insist on at least one classic hashed password to be defined in addition to any PKCS#11 one, as
989 * a safe fallback, but also to simplify the password changing algorithm: there we require providing
990 * the old literal password only (and do not care for the old PKCS#11 token) */
992 if (strv_isempty(h
->hashed_password
))
993 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
994 "User record has no hashed passwords, refusing.");
996 /* Generates the list of plaintext passwords to propagate to LUKS/fscrypt devices, and checks whether
997 * we have a plaintext password for each hashed one. If we are missing one we'll fail, since we
998 * couldn't sync fscrypt/LUKS to the login account properly. */
1000 STRV_FOREACH(i
, h
->hashed_password
) {
1004 log_debug("Looking for plaintext password for: %s", *i
);
1006 /* Let's scan all provided plaintext passwords */
1007 STRV_FOREACH(j
, h
->password
) {
1008 r
= test_password_one(*i
, *j
);
1010 return log_error_errno(r
, "Failed to test plaintext password: %m");
1012 if (ret_effective_passwords
) {
1013 r
= strv_extend(&effective
, *j
);
1018 log_debug("Found literal plaintext password.");
1025 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY
), "Missing plaintext password for defined hashed password");
1028 for (n
= 0; n
< h
->n_recovery_key
; n
++) {
1032 log_debug("Looking for plaintext recovery key for: %s", h
->recovery_key
[n
].hashed_password
);
1034 STRV_FOREACH(j
, h
->password
) {
1035 _cleanup_(erase_and_freep
) char *mangled
= NULL
;
1038 if (streq(h
->recovery_key
[n
].type
, "modhex64")) {
1040 r
= normalize_recovery_key(*j
, &mangled
);
1041 if (r
== -EINVAL
) /* Not properly formatted, probably a regular password. */
1044 return log_error_errno(r
, "Failed to normalize recovery key: %m");
1050 r
= test_password_one(h
->recovery_key
[n
].hashed_password
, p
);
1052 return log_error_errno(r
, "Failed to test plaintext recovery key: %m");
1054 if (ret_effective_passwords
) {
1055 r
= strv_extend(&effective
, p
);
1060 log_debug("Found plaintext recovery key.");
1067 return log_error_errno(SYNTHETIC_ERRNO(EREMOTEIO
), "Missing plaintext recovery key for defined recovery key");
1070 for (n
= 0; n
< h
->n_pkcs11_encrypted_key
; n
++) {
1072 _cleanup_(pkcs11_callback_data_release
) struct pkcs11_callback_data data
= {
1075 .encrypted_key
= h
->pkcs11_encrypted_key
+ n
,
1078 r
= pkcs11_find_token(data
.encrypted_key
->uri
, pkcs11_callback
, &data
);
1084 r
= test_password_one(data
.encrypted_key
->hashed_password
, data
.decrypted_password
);
1086 return log_error_errno(r
, "Failed to test PKCS#11 password: %m");
1088 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Decrypted password from token is not correct, refusing.");
1090 if (ret_effective_passwords
) {
1091 r
= strv_extend(&effective
, data
.decrypted_password
);
1096 r
= strv_extend(&cache
->pkcs11_passwords
, data
.decrypted_password
);
1104 for (n
= 0; n
< h
->n_fido2_hmac_salt
; n
++) {
1106 _cleanup_(erase_and_freep
) char *decrypted_password
= NULL
;
1108 r
= fido2_use_token(h
, h
, h
->fido2_hmac_salt
+ n
, &decrypted_password
);
1112 r
= test_password_one(h
->fido2_hmac_salt
[n
].hashed_password
, decrypted_password
);
1114 return log_error_errno(r
, "Failed to test FIDO2 password: %m");
1116 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Decrypted password from token is not correct, refusing.");
1118 if (ret_effective_passwords
) {
1119 r
= strv_extend(&effective
, decrypted_password
);
1124 r
= strv_extend(&cache
->fido2_passwords
, decrypted_password
);
1132 if (ret_effective_passwords
)
1133 *ret_effective_passwords
= TAKE_PTR(effective
);
1138 static int determine_default_storage(UserStorage
*ret
) {
1139 UserStorage storage
= _USER_STORAGE_INVALID
;
1145 /* homed tells us via an environment variable which default storage to use */
1146 e
= getenv("SYSTEMD_HOME_DEFAULT_STORAGE");
1148 storage
= user_storage_from_string(e
);
1150 log_warning("$SYSTEMD_HOME_DEFAULT_STORAGE set to invalid storage type, ignoring: %s", e
);
1152 log_info("Using configured default storage '%s'.", user_storage_to_string(storage
));
1158 /* When neither user nor admin specified the storage type to use, fix it to be LUKS — unless we run
1159 * in a container where loopback devices and LUKS/DM are not available. Also, if /home is encrypted
1160 * anyway, let's avoid duplicate encryption. Note that we typically default to the assumption of
1161 * "classic" storage for most operations. However, if we create a new home, then let's user LUKS if
1162 * nothing is specified. */
1164 r
= detect_container();
1166 return log_error_errno(r
, "Failed to determine whether we are in a container: %m");
1168 r
= path_is_encrypted(get_home_root());
1170 log_info("%s is encrypted, not using '%s' storage, in order to avoid double encryption.", get_home_root(), user_storage_to_string(USER_LUKS
));
1173 log_warning_errno(r
, "Failed to determine if %s is encrypted, ignoring: %m", get_home_root());
1175 r
= dlopen_cryptsetup();
1177 log_info("Not using '%s' storage, since libcryptsetup could not be loaded.", user_storage_to_string(USER_LUKS
));
1179 log_info("Using automatic default storage of '%s'.", user_storage_to_string(USER_LUKS
));
1185 log_info("Running in container, not using '%s' storage.", user_storage_to_string(USER_LUKS
));
1187 r
= path_is_fs_type(get_home_root(), BTRFS_SUPER_MAGIC
);
1189 log_warning_errno(r
, "Failed to determine file system of %s, ignoring: %m", get_home_root());
1191 log_info("%s is on btrfs, using '%s' as storage.", get_home_root(), user_storage_to_string(USER_SUBVOLUME
));
1192 *ret
= USER_SUBVOLUME
;
1194 log_info("%s is on simple file system, using '%s' as storage.", get_home_root(), user_storage_to_string(USER_DIRECTORY
));
1195 *ret
= USER_DIRECTORY
;
1201 static int home_create(UserRecord
*h
, UserRecord
**ret_home
) {
1202 _cleanup_(strv_free_erasep
) char **effective_passwords
= NULL
;
1203 _cleanup_(home_setup_done
) HomeSetup setup
= HOME_SETUP_INIT
;
1204 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
;
1205 _cleanup_(password_cache_free
) PasswordCache cache
= {};
1206 UserStorage new_storage
= _USER_STORAGE_INVALID
;
1207 const char *new_fs
= NULL
;
1213 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User record lacks name, refusing.");
1214 if (!uid_is_valid(h
->uid
))
1215 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User record lacks UID, refusing.");
1217 r
= user_record_compile_effective_passwords(h
, &cache
, &effective_passwords
);
1221 r
= user_record_test_home_directory_and_warn(h
);
1224 if (r
!= USER_TEST_ABSENT
)
1225 return log_error_errno(SYNTHETIC_ERRNO(EEXIST
), "Home directory %s already exists, refusing.", user_record_home_directory(h
));
1227 if (h
->storage
< 0) {
1228 r
= determine_default_storage(&new_storage
);
1233 if ((h
->storage
== USER_LUKS
||
1234 (h
->storage
< 0 && new_storage
== USER_LUKS
)) &&
1235 !h
->file_system_type
)
1236 new_fs
= getenv("SYSTEMD_HOME_DEFAULT_FILE_SYSTEM_TYPE");
1238 if (new_storage
>= 0 || new_fs
) {
1239 r
= user_record_add_binding(
1254 return log_error_errno(r
, "Failed to change storage type to LUKS: %m");
1257 r
= user_record_test_image_path_and_warn(h
);
1260 if (!IN_SET(r
, USER_TEST_ABSENT
, USER_TEST_UNDEFINED
, USER_TEST_MAYBE
))
1261 return log_error_errno(SYNTHETIC_ERRNO(EEXIST
), "Image path %s already exists, refusing.", user_record_image_path(h
));
1263 switch (user_record_storage(h
)) {
1266 r
= home_create_luks(h
, &cache
, effective_passwords
, &new_home
);
1269 case USER_DIRECTORY
:
1270 case USER_SUBVOLUME
:
1271 r
= home_create_directory_or_subvolume(h
, &setup
, &new_home
);
1275 r
= home_create_fscrypt(h
, effective_passwords
, &new_home
);
1279 r
= home_create_cifs(h
, &setup
, &new_home
);
1283 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
),
1284 "Creating home directories of type '%s' currently not supported.", user_storage_to_string(user_record_storage(h
)));
1289 if (user_record_equal(h
, new_home
)) {
1294 *ret_home
= TAKE_PTR(new_home
);
1298 static int home_remove(UserRecord
*h
) {
1299 bool deleted
= false;
1300 const char *ip
, *hd
;
1306 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User record lacks user name, refusing.");
1307 if (!IN_SET(user_record_storage(h
), USER_LUKS
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
, USER_CIFS
))
1308 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
), "Removing home directories of type '%s' currently not supported.", user_storage_to_string(user_record_storage(h
)));
1310 hd
= user_record_home_directory(h
);
1312 r
= user_record_test_home_directory_and_warn(h
);
1315 if (r
== USER_TEST_MOUNTED
)
1316 return log_error_errno(SYNTHETIC_ERRNO(EBUSY
), "Directory %s is still mounted, refusing.", hd
);
1320 r
= user_record_test_image_path_and_warn(h
);
1324 ip
= user_record_image_path(h
);
1326 switch (user_record_storage(h
)) {
1333 if (stat(ip
, &st
) < 0) {
1334 if (errno
!= ENOENT
)
1335 return log_error_errno(errno
, "Failed to stat() %s: %m", ip
);
1338 if (S_ISREG(st
.st_mode
)) {
1339 if (unlink(ip
) < 0) {
1340 if (errno
!= ENOENT
)
1341 return log_error_errno(errno
, "Failed to remove %s: %m", ip
);
1343 _cleanup_free_
char *parent
= NULL
;
1347 r
= path_extract_directory(ip
, &parent
);
1349 log_debug_errno(r
, "Failed to determine parent directory of '%s': %m", ip
);
1351 r
= fsync_path_at(AT_FDCWD
, parent
);
1353 log_debug_errno(r
, "Failed to synchronize disk after deleting '%s', ignoring: %m", ip
);
1357 } else if (S_ISBLK(st
.st_mode
))
1358 log_info("Not removing file system on block device %s.", ip
);
1360 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK
), "Image file %s is neither block device, nor regular, refusing removal.", ip
);
1366 case USER_SUBVOLUME
:
1367 case USER_DIRECTORY
:
1371 r
= rm_rf(ip
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
|REMOVE_SYNCFS
);
1374 return log_warning_errno(r
, "Failed to remove %s: %m", ip
);
1378 /* If the image path and the home directory are the same invalidate the home directory, so
1379 * that we don't remove it anymore */
1380 if (path_equal(ip
, hd
))
1386 /* Nothing else to do here: we won't remove remote stuff. */
1387 log_info("Not removing home directory on remote server.");
1391 assert_not_reached();
1395 if (rmdir(hd
) < 0) {
1396 if (errno
!= ENOENT
)
1397 return log_error_errno(errno
, "Failed to remove %s, ignoring: %m", hd
);
1403 if (user_record_drop_caches(h
))
1406 log_info("Everything completed.");
1408 return log_notice_errno(SYNTHETIC_ERRNO(EALREADY
),
1409 "Nothing to remove.");
1414 static int home_validate_update(UserRecord
*h
, HomeSetup
*setup
, HomeSetupFlags
*flags
) {
1415 bool has_mount
= false;
1422 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User record lacks user name, refusing.");
1423 if (!uid_is_valid(h
->uid
))
1424 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User record lacks UID, refusing.");
1425 if (!IN_SET(user_record_storage(h
), USER_LUKS
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
, USER_CIFS
))
1426 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
), "Processing home directories of type '%s' currently not supported.", user_storage_to_string(user_record_storage(h
)));
1428 r
= user_record_test_home_directory_and_warn(h
);
1432 has_mount
= r
== USER_TEST_MOUNTED
;
1434 r
= user_record_test_image_path_and_warn(h
);
1437 if (r
== USER_TEST_ABSENT
)
1438 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
), "Image path %s does not exist", user_record_image_path(h
));
1440 switch (user_record_storage(h
)) {
1442 case USER_DIRECTORY
:
1443 case USER_SUBVOLUME
:
1449 r
= home_get_state_luks(h
, setup
);
1452 if ((r
> 0) != has_mount
)
1453 return log_error_errno(SYNTHETIC_ERRNO(EBUSY
), "Home mount incompletely set up.");
1459 assert_not_reached();
1463 SET_FLAG(*flags
, HOME_SETUP_ALREADY_ACTIVATED
, has_mount
);
1465 return has_mount
; /* return true if the home record is already active */
1468 static int home_update(UserRecord
*h
, UserRecord
**ret
) {
1469 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
, *header_home
= NULL
, *embedded_home
= NULL
;
1470 _cleanup_(home_setup_done
) HomeSetup setup
= HOME_SETUP_INIT
;
1471 _cleanup_(password_cache_free
) PasswordCache cache
= {};
1472 HomeSetupFlags flags
= 0;
1478 r
= user_record_authenticate(h
, h
, &cache
, /* strict_verify= */ true);
1481 assert(r
> 0); /* Insist that a password was verified */
1483 r
= home_validate_update(h
, &setup
, &flags
);
1487 r
= home_setup(h
, flags
, &cache
, &setup
, &header_home
);
1491 r
= home_load_embedded_identity(h
, setup
.root_fd
, header_home
, USER_RECONCILE_REQUIRE_NEWER
, &cache
, &embedded_home
, &new_home
);
1495 r
= home_store_header_identity_luks(new_home
, &setup
, header_home
);
1499 r
= home_store_embedded_identity(new_home
, setup
.root_fd
, h
->uid
, embedded_home
);
1503 r
= home_extend_embedded_identity(new_home
, h
, &setup
);
1507 r
= home_sync_and_statfs(setup
.root_fd
, NULL
);
1511 r
= home_setup_done(&setup
);
1515 log_info("Everything completed.");
1517 *ret
= TAKE_PTR(new_home
);
1521 static int home_resize(UserRecord
*h
, UserRecord
**ret
) {
1522 _cleanup_(home_setup_done
) HomeSetup setup
= HOME_SETUP_INIT
;
1523 _cleanup_(password_cache_free
) PasswordCache cache
= {};
1524 HomeSetupFlags flags
= 0;
1530 if (h
->disk_size
== UINT64_MAX
)
1531 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No target size specified, refusing.");
1533 r
= user_record_authenticate(h
, h
, &cache
, /* strict_verify= */ true);
1536 assert(r
> 0); /* Insist that a password was verified */
1538 r
= home_validate_update(h
, &setup
, &flags
);
1542 switch (user_record_storage(h
)) {
1545 return home_resize_luks(h
, flags
, &cache
, &setup
, ret
);
1547 case USER_DIRECTORY
:
1548 case USER_SUBVOLUME
:
1550 return home_resize_directory(h
, flags
, &cache
, &setup
, ret
);
1553 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
), "Resizing home directories of type '%s' currently not supported.", user_storage_to_string(user_record_storage(h
)));
1557 static int home_passwd(UserRecord
*h
, UserRecord
**ret_home
) {
1558 _cleanup_(user_record_unrefp
) UserRecord
*header_home
= NULL
, *embedded_home
= NULL
, *new_home
= NULL
;
1559 _cleanup_(strv_free_erasep
) char **effective_passwords
= NULL
;
1560 _cleanup_(home_setup_done
) HomeSetup setup
= HOME_SETUP_INIT
;
1561 _cleanup_(password_cache_free
) PasswordCache cache
= {};
1562 HomeSetupFlags flags
= 0;
1568 if (!IN_SET(user_record_storage(h
), USER_LUKS
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
))
1569 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
), "Changing password of home directories of type '%s' currently not supported.", user_storage_to_string(user_record_storage(h
)));
1571 r
= user_record_compile_effective_passwords(h
, &cache
, &effective_passwords
);
1575 r
= home_validate_update(h
, &setup
, &flags
);
1579 r
= home_setup(h
, flags
, &cache
, &setup
, &header_home
);
1583 r
= home_load_embedded_identity(h
, setup
.root_fd
, header_home
, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL
, &cache
, &embedded_home
, &new_home
);
1587 switch (user_record_storage(h
)) {
1590 r
= home_passwd_luks(h
, &setup
, &cache
, effective_passwords
);
1596 r
= home_passwd_fscrypt(h
, &setup
, &cache
, effective_passwords
);
1605 r
= home_store_header_identity_luks(new_home
, &setup
, header_home
);
1609 r
= home_store_embedded_identity(new_home
, setup
.root_fd
, h
->uid
, embedded_home
);
1613 r
= home_extend_embedded_identity(new_home
, h
, &setup
);
1617 r
= home_sync_and_statfs(setup
.root_fd
, NULL
);
1621 r
= home_setup_done(&setup
);
1625 log_info("Everything completed.");
1627 *ret_home
= TAKE_PTR(new_home
);
1631 static int home_inspect(UserRecord
*h
, UserRecord
**ret_home
) {
1632 _cleanup_(user_record_unrefp
) UserRecord
*header_home
= NULL
, *new_home
= NULL
;
1633 _cleanup_(home_setup_done
) HomeSetup setup
= HOME_SETUP_INIT
;
1634 _cleanup_(password_cache_free
) PasswordCache cache
= {};
1635 HomeSetupFlags flags
= 0;
1641 r
= user_record_authenticate(h
, h
, &cache
, /* strict_verify= */ false);
1645 r
= home_validate_update(h
, &setup
, &flags
);
1649 r
= home_setup(h
, flags
, &cache
, &setup
, &header_home
);
1653 r
= home_load_embedded_identity(h
, setup
.root_fd
, header_home
, USER_RECONCILE_ANY
, &cache
, NULL
, &new_home
);
1657 r
= home_extend_embedded_identity(new_home
, h
, &setup
);
1661 r
= home_setup_done(&setup
);
1665 log_info("Everything completed.");
1667 *ret_home
= TAKE_PTR(new_home
);
1671 static int home_lock(UserRecord
*h
) {
1677 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User record incomplete, refusing.");
1678 if (user_record_storage(h
) != USER_LUKS
)
1679 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
), "Locking home directories of type '%s' currently not supported.", user_storage_to_string(user_record_storage(h
)));
1681 r
= user_record_test_home_directory_and_warn(h
);
1684 if (r
!= USER_TEST_MOUNTED
)
1685 return log_error_errno(SYNTHETIC_ERRNO(ENOEXEC
), "Home directory of %s is not mounted, can't lock.", h
->user_name
);
1687 r
= home_lock_luks(h
);
1691 log_info("Everything completed.");
1695 static int home_unlock(UserRecord
*h
) {
1696 _cleanup_(password_cache_free
) PasswordCache cache
= {};
1702 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "User record incomplete, refusing.");
1703 if (user_record_storage(h
) != USER_LUKS
)
1704 return log_error_errno(SYNTHETIC_ERRNO(ENOTTY
), "Unlocking home directories of type '%s' currently not supported.", user_storage_to_string(user_record_storage(h
)));
1706 /* Note that we don't check if $HOME is actually mounted, since we want to avoid disk accesses on
1707 * that mount until we have resumed the device. */
1709 r
= user_record_authenticate(h
, h
, &cache
, /* strict_verify= */ false);
1713 r
= home_unlock_luks(h
, &cache
);
1717 log_info("Everything completed.");
1721 static int run(int argc
, char *argv
[]) {
1722 _cleanup_(user_record_unrefp
) UserRecord
*home
= NULL
, *new_home
= NULL
;
1723 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1724 _cleanup_(fclosep
) FILE *opened_file
= NULL
;
1725 unsigned line
= 0, column
= 0;
1726 const char *json_path
= NULL
;
1731 start
= now(CLOCK_MONOTONIC
);
1737 if (argc
< 2 || argc
> 3)
1738 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program takes one or two arguments.");
1741 json_path
= argv
[2];
1743 opened_file
= fopen(json_path
, "re");
1745 return log_error_errno(errno
, "Failed to open %s: %m", json_path
);
1747 json_file
= opened_file
;
1749 json_path
= "<stdin>";
1753 r
= json_parse_file(json_file
, json_path
, JSON_PARSE_SENSITIVE
, &v
, &line
, &column
);
1755 return log_error_errno(r
, "[%s:%u:%u] Failed to parse JSON data: %m", json_path
, line
, column
);
1757 home
= user_record_new();
1761 r
= user_record_load(home
, v
, USER_RECORD_LOAD_FULL
|USER_RECORD_LOG
|USER_RECORD_PERMISSIVE
);
1765 /* Well known return values of these operations, that systemd-homed knows and converts to proper D-Bus errors:
1767 * EMSGSIZE → file systems of this type cannot be shrunk
1768 * ETXTBSY → file systems of this type can only be shrunk offline
1769 * ERANGE → file system size too small
1770 * ENOLINK → system does not support selected storage backend
1771 * EPROTONOSUPPORT → system does not support selected file system
1772 * ENOTTY → operation not support on this storage
1773 * ESOCKTNOSUPPORT → operation not support on this file system
1774 * ENOKEY → password incorrect (or not sufficient, or not supplied)
1775 * EREMOTEIO → recovery key incorrect (or not sufficeint, or not supplied — only if no passwords defined)
1776 * EBADSLT → similar, but PKCS#11 device is defined and might be able to provide password, if it was plugged in which it is not
1777 * ENOANO → suitable PKCS#11/FIDO2 device found, but PIN is missing to unlock it
1778 * ERFKILL → suitable PKCS#11 device found, but OK to ask for on-device interactive authentication not given
1779 * EMEDIUMTYPE → suitable FIDO2 device found, but OK to ask for user presence not given
1780 * ENOCSI → suitable FIDO2 device found, but OK to ask for user verification not given
1781 * ENOSTR → suitable FIDO2 device found, but user didn't react to action request on token quickly enough
1782 * EOWNERDEAD → suitable PKCS#11/FIDO2 device found, but its PIN is locked
1783 * ENOLCK → suitable PKCS#11/FIDO2 device found, but PIN incorrect
1784 * ETOOMANYREFS → suitable PKCS#11 device found, but PIN incorrect, and only few tries left
1785 * EUCLEAN → suitable PKCS#11 device found, but PIN incorrect, and only one try left
1786 * EBUSY → file system is currently active
1787 * ENOEXEC → file system is currently not active
1788 * ENOSPC → not enough disk space for operation
1789 * EKEYREVOKED → user record has not suitable hashed password or pkcs#11 entry, we cannot authenticate
1790 * EADDRINUSE → home image is already used elsewhere (lock taken)
1793 if (streq(argv
[1], "activate"))
1794 r
= home_activate(home
, &new_home
);
1795 else if (streq(argv
[1], "deactivate"))
1796 r
= home_deactivate(home
, false);
1797 else if (streq(argv
[1], "deactivate-force"))
1798 r
= home_deactivate(home
, true);
1799 else if (streq(argv
[1], "create"))
1800 r
= home_create(home
, &new_home
);
1801 else if (streq(argv
[1], "remove"))
1802 r
= home_remove(home
);
1803 else if (streq(argv
[1], "update"))
1804 r
= home_update(home
, &new_home
);
1805 else if (streq(argv
[1], "resize"))
1806 r
= home_resize(home
, &new_home
);
1807 else if (streq(argv
[1], "passwd"))
1808 r
= home_passwd(home
, &new_home
);
1809 else if (streq(argv
[1], "inspect"))
1810 r
= home_inspect(home
, &new_home
);
1811 else if (streq(argv
[1], "lock"))
1812 r
= home_lock(home
);
1813 else if (streq(argv
[1], "unlock"))
1814 r
= home_unlock(home
);
1816 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown verb '%s'.", argv
[1]);
1817 if (IN_SET(r
, -ENOKEY
, -EREMOTEIO
) && !strv_isempty(home
->password
) ) { /* There were passwords specified but they were incorrect */
1820 /* Make sure bad password replies always take at least 3s, and if longer multiples of 3s, so
1821 * that it's not clear how long we actually needed for our calculations. */
1822 n
= now(CLOCK_MONOTONIC
);
1825 d
= usec_sub_unsigned(n
, start
);
1826 if (d
> BAD_PASSWORD_DELAY_USEC
)
1827 end
= start
+ DIV_ROUND_UP(d
, BAD_PASSWORD_DELAY_USEC
) * BAD_PASSWORD_DELAY_USEC
;
1829 end
= start
+ BAD_PASSWORD_DELAY_USEC
;
1832 (void) usleep(usec_sub_unsigned(end
, n
));
1837 /* We always pass the new record back, regardless if it changed or not. This allows our caller to
1838 * prepare a fresh record, send to us, and only if it works use it without having to keep a local
1841 json_variant_dump(new_home
->json
, JSON_FORMAT_NEWLINE
, stdout
, NULL
);
1846 DEFINE_MAIN_FUNCTION(run
);