1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <linux/fs.h>
5 #include <sys/vfs.h>
6 
7 #include "sd-id128.h"
8 
9 #include "homework-password-cache.h"
10 #include "loop-util.h"
11 #include "missing_keyctl.h"
12 #include "missing_syscall.h"
13 #include "user-record.h"
14 #include "user-record-util.h"
15 
16 typedef struct HomeSetup {
17         char *dm_name;   /* "home-<username>" */
18         char *dm_node;   /* "/dev/mapper/home-<username>" */
19 
20         LoopDevice *loop;
21         struct crypt_device *crypt_device;
22         int root_fd;
23         int image_fd;
24         sd_id128_t found_partition_uuid;
25         sd_id128_t found_luks_uuid;
26         sd_id128_t found_fs_uuid;
27 
28         uint8_t fscrypt_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
29 
30         void *volume_key;
31         size_t volume_key_size;
32 
33         key_serial_t key_serial;
34 
35         bool undo_dm:1;
36         bool undo_mount:1;            /* Whether to unmount /run/systemd/user-home-mount */
37         bool do_offline_fitrim:1;
38         bool do_offline_fallocate:1;
39         bool do_mark_clean:1;
40         bool do_drop_caches:1;
41 
42         uint64_t partition_offset;
43         uint64_t partition_size;
44 
45         char *mount_suffix;           /* The directory to use as home dir is this path below /run/systemd/user-home-mount */
46 
47         char *temporary_image_path;
48 } HomeSetup;
49 
50 #define HOME_SETUP_INIT                                 \
51         {                                               \
52                 .root_fd = -1,                          \
53                 .image_fd = -1,                         \
54                 .partition_offset = UINT64_MAX,         \
55                 .partition_size = UINT64_MAX,           \
56                 .key_serial = -1,                       \
57         }
58 
59 /* Various flags for the operation of setting up a home directory */
60 typedef enum HomeSetupFlags {
61         HOME_SETUP_ALREADY_ACTIVATED           = 1 << 0, /* Open an already activated home, rather than activate it afresh */
62 
63         /* CIFS backend: */
64         HOME_SETUP_CIFS_MKDIR                  = 1 << 1, /* Create CIFS subdir when missing */
65 
66         /* Applies only for resize operations */
67         HOME_SETUP_RESIZE_DONT_SYNC_IDENTITIES = 1 << 2, /* Don't sync identity records into home and LUKS header */
68         HOME_SETUP_RESIZE_MINIMIZE             = 1 << 3, /* Shrink to minimal size */
69         HOME_SETUP_RESIZE_DONT_GROW            = 1 << 4, /* If the resize would grow, gracefully terminate operation */
70         HOME_SETUP_RESIZE_DONT_SHRINK          = 1 << 5, /* If the resize would shrink, gracefully terminate operation */
71         HOME_SETUP_RESIZE_DONT_UNDO            = 1 << 6, /* Leave loopback/DM device context open after successful operation */
72 } HomeSetupFlags;
73 
74 int home_setup_done(HomeSetup *setup);
75 
76 int home_setup_undo_mount(HomeSetup *setup, int level);
77 int home_setup_undo_dm(HomeSetup *setup, int level);
78 
79 int keyring_unlink(key_serial_t k);
80 
81 int home_setup(UserRecord *h, HomeSetupFlags flags, HomeSetup *setup, PasswordCache *cache, UserRecord **ret_header_home);
82 
83 int home_refresh(UserRecord *h, HomeSetupFlags flags, HomeSetup *setup, UserRecord *header_home, PasswordCache *cache, struct statfs *ret_statfs, UserRecord **ret_new_home);
84 
85 int home_maybe_shift_uid(UserRecord *h, HomeSetupFlags flags, HomeSetup *setup);
86 int home_populate(UserRecord *h, int dir_fd);
87 
88 int home_load_embedded_identity(UserRecord *h, int root_fd, UserRecord *header_home, UserReconcileMode mode, PasswordCache *cache, UserRecord **ret_embedded_home, UserRecord **ret_new_home);
89 int home_store_embedded_identity(UserRecord *h, int root_fd, uid_t uid, UserRecord *old_home);
90 int home_extend_embedded_identity(UserRecord *h, UserRecord *used, HomeSetup *setup);
91 
92 int user_record_authenticate(UserRecord *h, UserRecord *secret, PasswordCache *cache, bool strict_verify);
93 
94 int home_sync_and_statfs(int root_fd, struct statfs *ret);
95 
96 #define HOME_RUNTIME_WORK_DIR "/run/systemd/user-home-mount"
97