1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "btrfs-util.h"
7 #include "homework-directory.h"
8 #include "homework-mount.h"
9 #include "homework-quota.h"
11 #include "mount-util.h"
12 #include "path-util.h"
14 #include "tmpfile-util.h"
15 #include "umask-util.h"
16 #include "user-util.h"
18 int home_setup_directory(UserRecord
*h
, HomeSetup
*setup
) {
23 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
));
25 assert(!setup
->undo_mount
);
26 assert(setup
->root_fd
< 0);
28 /* We'll bind mount the image directory to a new mount point where we'll start adjusting it. Only
29 * once that's complete we'll move the thing to its final place eventually. */
30 r
= home_unshare_and_mkdir();
34 assert_se(ip
= user_record_image_path(h
));
36 r
= mount_follow_verbose(LOG_ERR
, ip
, HOME_RUNTIME_WORK_DIR
, NULL
, MS_BIND
, NULL
);
40 setup
->undo_mount
= true;
42 /* Turn off any form of propagation for this */
43 r
= mount_nofollow_verbose(LOG_ERR
, NULL
, HOME_RUNTIME_WORK_DIR
, NULL
, MS_PRIVATE
, NULL
);
47 /* Adjust MS_SUID and similar flags */
48 r
= mount_nofollow_verbose(LOG_ERR
, NULL
, HOME_RUNTIME_WORK_DIR
, NULL
, MS_BIND
|MS_REMOUNT
|user_record_mount_flags(h
), NULL
);
52 setup
->root_fd
= open(HOME_RUNTIME_WORK_DIR
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
53 if (setup
->root_fd
< 0)
54 return log_error_errno(errno
, "Failed to open home directory: %m");
59 int home_activate_directory(
63 UserRecord
**ret_home
) {
65 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
, *header_home
= NULL
;
70 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
));
74 assert_se(hdo
= user_record_home_directory(h
));
75 hd
= strdupa_safe(hdo
);
77 r
= home_setup(h
, 0, cache
, setup
, &header_home
);
81 r
= home_refresh(h
, setup
, header_home
, cache
, NULL
, &new_home
);
85 r
= home_extend_embedded_identity(new_home
, h
, setup
);
89 /* Close fd to private mount before moving mount */
90 setup
->root_fd
= safe_close(setup
->root_fd
);
92 /* We are now done with everything, move the mount into place */
93 r
= home_move_mount(NULL
, hd
);
97 setup
->undo_mount
= false;
99 setup
->do_drop_caches
= false;
101 log_info("Everything completed.");
103 *ret_home
= TAKE_PTR(new_home
);
107 int home_create_directory_or_subvolume(UserRecord
*h
, HomeSetup
*setup
, UserRecord
**ret_home
) {
108 _cleanup_(rm_rf_subvolume_and_freep
) char *temporary
= NULL
;
109 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
;
110 _cleanup_close_
int mount_fd
= -1;
111 _cleanup_free_
char *d
= NULL
;
112 bool is_subvolume
= false;
117 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
));
121 assert_se(ip
= user_record_image_path(h
));
123 r
= tempfn_random(ip
, "homework", &d
);
125 return log_error_errno(r
, "Failed to allocate temporary directory: %m");
127 (void) mkdir_parents(d
, 0755);
129 switch (user_record_storage(h
)) {
133 r
= btrfs_subvol_make(d
);
136 log_info("Subvolume created.");
139 if (h
->disk_size
!= UINT64_MAX
) {
141 /* Enable quota for the subvolume we just created. Note we don't check for
142 * errors here and only log about debug level about this. */
143 r
= btrfs_quota_enable(d
, true);
145 log_debug_errno(r
, "Failed to enable quota on %s, ignoring: %m", d
);
147 r
= btrfs_subvol_auto_qgroup(d
, 0, false);
149 log_debug_errno(r
, "Failed to set up automatic quota group on %s, ignoring: %m", d
);
151 /* Actually configure the quota. We also ignore errors here, but we do log
152 * about them loudly, to keep things discoverable even though we don't
153 * consider lacking quota support in kernel fatal. */
154 (void) home_update_quota_btrfs(h
, d
);
160 return log_error_errno(r
, "Failed to create temporary home directory subvolume %s: %m", d
);
162 log_info("Creating subvolume %s is not supported, as file system does not support subvolumes. Falling back to regular directory.", d
);
167 if (mkdir(d
, 0700) < 0)
168 return log_error_errno(errno
, "Failed to create temporary home directory %s: %m", d
);
170 (void) home_update_quota_classic(h
, d
);
174 assert_not_reached();
177 temporary
= TAKE_PTR(d
); /* Needs to be destroyed now */
179 /* Let's decouple namespaces now, so that we can possibly mount a UID map mount into
180 * /run/systemd/user-home-mount/ that noone will see but us. */
181 r
= home_unshare_and_mkdir();
185 setup
->root_fd
= open(temporary
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
186 if (setup
->root_fd
< 0)
187 return log_error_errno(errno
, "Failed to open temporary home directory: %m");
189 /* Try to apply a UID shift, so that the directory is actually owned by "nobody", and is only mapped
190 * to the proper UID while active. — Well, that's at least the theory. Unfortunately, only btrfs does
191 * per-subvolume quota. The others do per-uid quota. Which means mapping all home directories to the
192 * same UID of "nobody" makes quota impossible. Hence unless we actually managed to create a btrfs
193 * subvolume for this user we'll map the user's UID to itself. Now you might ask: why bother mapping
194 * at all? It's because we want to restrict the UIDs used on the home directory: we leave all other
195 * UIDs of the homed UID range unmapped, thus making them unavailable to programs accessing the
197 r
= home_shift_uid(setup
->root_fd
, HOME_RUNTIME_WORK_DIR
, is_subvolume
? UID_NOBODY
: h
->uid
, h
->uid
, &mount_fd
);
199 setup
->undo_mount
= true; /* If uidmaps worked we have a mount to undo again */
202 /* If we have established a new mount, then we can use that as new root fd to our home directory. */
203 safe_close(setup
->root_fd
);
205 setup
->root_fd
= fd_reopen(mount_fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
206 if (setup
->root_fd
< 0)
207 return log_error_errno(setup
->root_fd
, "Unable to convert mount fd into proper directory fd: %m");
209 mount_fd
= safe_close(mount_fd
);
212 r
= home_populate(h
, setup
->root_fd
);
216 r
= home_sync_and_statfs(setup
->root_fd
, NULL
);
220 r
= user_record_clone(h
, USER_RECORD_LOAD_MASK_SECRET
|USER_RECORD_PERMISSIVE
, &new_home
);
222 return log_error_errno(r
, "Failed to clone record: %m");
224 r
= user_record_add_binding(
226 user_record_storage(h
),
239 return log_error_errno(r
, "Failed to add binding to record: %m");
241 setup
->root_fd
= safe_close(setup
->root_fd
);
243 /* Unmount mapped mount before we move the dir into place */
244 if (setup
->undo_mount
) {
245 r
= umount_verbose(LOG_ERR
, HOME_RUNTIME_WORK_DIR
, UMOUNT_NOFOLLOW
);
249 setup
->undo_mount
= false;
252 if (rename(temporary
, ip
) < 0)
253 return log_error_errno(errno
, "Failed to rename %s to %s: %m", temporary
, ip
);
255 temporary
= mfree(temporary
);
257 log_info("Everything completed.");
259 *ret_home
= TAKE_PTR(new_home
);
263 int home_resize_directory(
265 HomeSetupFlags flags
,
266 PasswordCache
*cache
,
268 UserRecord
**ret_home
) {
270 _cleanup_(user_record_unrefp
) UserRecord
*embedded_home
= NULL
, *new_home
= NULL
;
276 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
));
278 r
= home_setup(h
, flags
, cache
, setup
, NULL
);
282 r
= home_load_embedded_identity(h
, setup
->root_fd
, NULL
, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL
, cache
, &embedded_home
, &new_home
);
286 r
= home_maybe_shift_uid(h
, setup
);
290 r
= home_update_quota_auto(h
, NULL
);
291 if (ERRNO_IS_NOT_SUPPORTED(r
))
292 return -ESOCKTNOSUPPORT
; /* make recognizable */
296 r
= home_store_embedded_identity(new_home
, setup
->root_fd
, h
->uid
, embedded_home
);
300 r
= home_extend_embedded_identity(new_home
, h
, setup
);
304 r
= home_sync_and_statfs(setup
->root_fd
, NULL
);
308 r
= home_setup_done(setup
);
312 log_info("Everything completed.");
314 *ret_home
= TAKE_PTR(new_home
);