1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "btrfs-util.h"
7 #include "homework-blob.h"
8 #include "homework-directory.h"
9 #include "homework-mount.h"
10 #include "homework-quota.h"
12 #include "mount-util.h"
13 #include "path-util.h"
15 #include "tmpfile-util.h"
16 #include "umask-util.h"
17 #include "user-util.h"
19 int home_setup_directory(UserRecord
*h
, HomeSetup
*setup
) {
24 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
));
26 assert(!setup
->undo_mount
);
27 assert(setup
->root_fd
< 0);
29 /* We'll bind mount the image directory to a new mount point where we'll start adjusting it. Only
30 * once that's complete we'll move the thing to its final place eventually. */
31 r
= home_unshare_and_mkdir();
35 assert_se(ip
= user_record_image_path(h
));
37 r
= mount_follow_verbose(LOG_ERR
, ip
, HOME_RUNTIME_WORK_DIR
, NULL
, MS_BIND
, NULL
);
41 setup
->undo_mount
= true;
43 /* Turn off any form of propagation for this */
44 r
= mount_nofollow_verbose(LOG_ERR
, NULL
, HOME_RUNTIME_WORK_DIR
, NULL
, MS_PRIVATE
, NULL
);
48 /* Adjust MS_SUID and similar flags */
49 r
= mount_nofollow_verbose(LOG_ERR
, NULL
, HOME_RUNTIME_WORK_DIR
, NULL
, MS_BIND
|MS_REMOUNT
|user_record_mount_flags(h
), NULL
);
53 setup
->root_fd
= open(HOME_RUNTIME_WORK_DIR
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
54 if (setup
->root_fd
< 0)
55 return log_error_errno(errno
, "Failed to open home directory: %m");
60 int home_activate_directory(
65 UserRecord
**ret_home
) {
67 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
, *header_home
= NULL
;
72 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
));
76 assert_se(hdo
= user_record_home_directory(h
));
77 hd
= strdupa_safe(hdo
);
79 r
= home_setup(h
, flags
, setup
, cache
, &header_home
);
83 r
= home_refresh(h
, flags
, setup
, header_home
, cache
, NULL
, &new_home
);
87 r
= home_extend_embedded_identity(new_home
, h
, setup
);
91 /* Close fd to private mount before moving mount */
92 setup
->root_fd
= safe_close(setup
->root_fd
);
94 /* We are now done with everything, move the mount into place */
95 r
= home_move_mount(NULL
, hd
);
99 setup
->undo_mount
= false;
101 setup
->do_drop_caches
= false;
103 log_info("Everything completed.");
105 *ret_home
= TAKE_PTR(new_home
);
109 int home_create_directory_or_subvolume(UserRecord
*h
, HomeSetup
*setup
, UserRecord
**ret_home
) {
110 _cleanup_(rm_rf_subvolume_and_freep
) char *temporary
= NULL
;
111 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
;
112 _cleanup_close_
int mount_fd
= -EBADF
;
113 _cleanup_free_
char *d
= NULL
;
114 bool is_subvolume
= false;
119 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
));
123 assert_se(ip
= user_record_image_path(h
));
125 r
= tempfn_random(ip
, "homework", &d
);
127 return log_error_errno(r
, "Failed to allocate temporary directory: %m");
129 (void) mkdir_parents(d
, 0755);
131 switch (user_record_storage(h
)) {
135 r
= btrfs_subvol_make(AT_FDCWD
, d
);
138 log_info("Subvolume created.");
141 if (h
->disk_size
!= UINT64_MAX
) {
143 /* Enable quota for the subvolume we just created. Note we don't check for
144 * errors here and only log about debug level about this. */
145 r
= btrfs_quota_enable(d
, true);
147 log_debug_errno(r
, "Failed to enable quota on %s, ignoring: %m", d
);
149 r
= btrfs_subvol_auto_qgroup(d
, 0, false);
151 log_debug_errno(r
, "Failed to set up automatic quota group on %s, ignoring: %m", d
);
153 /* Actually configure the quota. We also ignore errors here, but we do log
154 * about them loudly, to keep things discoverable even though we don't
155 * consider lacking quota support in kernel fatal. */
156 (void) home_update_quota_btrfs(h
, d
);
162 return log_error_errno(r
, "Failed to create temporary home directory subvolume %s: %m", d
);
164 log_info("Creating subvolume %s is not supported, as file system does not support subvolumes. Falling back to regular directory.", d
);
169 if (mkdir(d
, 0700) < 0)
170 return log_error_errno(errno
, "Failed to create temporary home directory %s: %m", d
);
172 (void) home_update_quota_classic(h
, d
);
176 assert_not_reached();
179 temporary
= TAKE_PTR(d
); /* Needs to be destroyed now */
181 /* Let's decouple namespaces now, so that we can possibly mount a UID map mount into
182 * /run/systemd/user-home-mount/ that no one will see but us. */
183 r
= home_unshare_and_mkdir();
187 setup
->root_fd
= open(temporary
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
188 if (setup
->root_fd
< 0)
189 return log_error_errno(errno
, "Failed to open temporary home directory: %m");
191 /* Try to apply a UID shift, so that the directory is actually owned by "nobody", and is only mapped
192 * to the proper UID while active. — Well, that's at least the theory. Unfortunately, only btrfs does
193 * per-subvolume quota. The others do per-uid quota. Which means mapping all home directories to the
194 * same UID of "nobody" makes quota impossible. Hence unless we actually managed to create a btrfs
195 * subvolume for this user we'll map the user's UID to itself. Now you might ask: why bother mapping
196 * at all? It's because we want to restrict the UIDs used on the home directory: we leave all other
197 * UIDs of the homed UID range unmapped, thus making them unavailable to programs accessing the
199 r
= home_shift_uid(setup
->root_fd
, HOME_RUNTIME_WORK_DIR
, is_subvolume
? UID_NOBODY
: h
->uid
, h
->uid
, &mount_fd
);
201 setup
->undo_mount
= true; /* If uidmaps worked we have a mount to undo again */
204 /* If we have established a new mount, then we can use that as new root fd to our home directory. */
205 safe_close(setup
->root_fd
);
207 setup
->root_fd
= fd_reopen(mount_fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
208 if (setup
->root_fd
< 0)
209 return log_error_errno(setup
->root_fd
, "Unable to convert mount fd into proper directory fd: %m");
211 mount_fd
= safe_close(mount_fd
);
214 r
= home_populate(h
, setup
->root_fd
);
218 r
= home_sync_and_statfs(setup
->root_fd
, NULL
);
222 r
= user_record_clone(h
, USER_RECORD_LOAD_MASK_SECRET
|USER_RECORD_PERMISSIVE
, &new_home
);
224 return log_error_errno(r
, "Failed to clone record: %m");
226 r
= user_record_add_binding(
228 user_record_storage(h
),
241 return log_error_errno(r
, "Failed to add binding to record: %m");
243 setup
->root_fd
= safe_close(setup
->root_fd
);
245 /* Unmount mapped mount before we move the dir into place */
246 r
= home_setup_undo_mount(setup
, LOG_ERR
);
250 if (rename(temporary
, ip
) < 0)
251 return log_error_errno(errno
, "Failed to rename %s to %s: %m", temporary
, ip
);
253 temporary
= mfree(temporary
);
255 log_info("Everything completed.");
257 *ret_home
= TAKE_PTR(new_home
);
261 int home_resize_directory(
263 HomeSetupFlags flags
,
265 PasswordCache
*cache
,
266 UserRecord
**ret_home
) {
268 _cleanup_(user_record_unrefp
) UserRecord
*embedded_home
= NULL
, *new_home
= NULL
;
274 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
));
276 r
= home_setup(h
, flags
, setup
, cache
, NULL
);
280 reconciled
= home_load_embedded_identity(h
, setup
->root_fd
, NULL
, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL
, cache
, &embedded_home
, &new_home
);
284 r
= home_maybe_shift_uid(h
, flags
, setup
);
288 r
= home_update_quota_auto(h
, NULL
);
289 if (ERRNO_IS_NEG_NOT_SUPPORTED(r
))
290 return -ESOCKTNOSUPPORT
; /* make recognizable */
294 r
= home_store_embedded_identity(new_home
, setup
->root_fd
, embedded_home
);
298 r
= home_reconcile_blob_dirs(new_home
, setup
->root_fd
, reconciled
);
302 r
= home_extend_embedded_identity(new_home
, h
, setup
);
306 r
= home_sync_and_statfs(setup
->root_fd
, NULL
);
310 r
= home_setup_done(setup
);
314 log_info("Everything completed.");
316 *ret_home
= TAKE_PTR(new_home
);