]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/home/homework-directory.c
mkosi: Fix particle profile
[thirdparty/systemd.git] / src / home / homework-directory.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
70a5db58
LP
2
3#include <sys/mount.h>
4
5#include "btrfs-util.h"
6#include "fd-util.h"
17ac40e4 7#include "homework-blob.h"
70a5db58 8#include "homework-directory.h"
2ba38f78 9#include "homework-mount.h"
70a5db58
LP
10#include "homework-quota.h"
11#include "mkdir.h"
12#include "mount-util.h"
13#include "path-util.h"
14#include "rm-rf.h"
15#include "tmpfile-util.h"
16#include "umask-util.h"
cf5115f6 17#include "user-util.h"
70a5db58 18
e1df968b 19int home_setup_directory(UserRecord *h, HomeSetup *setup) {
2ba38f78
LP
20 const char *ip;
21 int r;
22
70a5db58 23 assert(h);
cf5115f6 24 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME));
70a5db58 25 assert(setup);
cf5115f6 26 assert(!setup->undo_mount);
2ba38f78
LP
27 assert(setup->root_fd < 0);
28
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();
32 if (r < 0)
33 return r;
34
35 assert_se(ip = user_record_image_path(h));
36
37 r = mount_follow_verbose(LOG_ERR, ip, HOME_RUNTIME_WORK_DIR, NULL, MS_BIND, NULL);
38 if (r < 0)
39 return r;
70a5db58 40
2ba38f78
LP
41 setup->undo_mount = true;
42
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);
45 if (r < 0)
46 return r;
47
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);
50 if (r < 0)
51 return r;
52
53 setup->root_fd = open(HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
70a5db58
LP
54 if (setup->root_fd < 0)
55 return log_error_errno(errno, "Failed to open home directory: %m");
56
57 return 0;
58}
59
60int home_activate_directory(
61 UserRecord *h,
6f2c8136 62 HomeSetupFlags flags,
a74e2e44 63 HomeSetup *setup,
7b78db28 64 PasswordCache *cache,
70a5db58
LP
65 UserRecord **ret_home) {
66
67 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL, *header_home = NULL;
2ba38f78 68 const char *hd, *hdo;
70a5db58
LP
69 int r;
70
71 assert(h);
72 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT));
a74e2e44 73 assert(setup);
70a5db58
LP
74 assert(ret_home);
75
70a5db58 76 assert_se(hdo = user_record_home_directory(h));
2f82562b 77 hd = strdupa_safe(hdo);
70a5db58 78
6f2c8136 79 r = home_setup(h, flags, setup, cache, &header_home);
70a5db58
LP
80 if (r < 0)
81 return r;
82
6f2c8136 83 r = home_refresh(h, flags, setup, header_home, cache, NULL, &new_home);
70a5db58
LP
84 if (r < 0)
85 return r;
86
cfef46f5
LP
87 r = home_extend_embedded_identity(new_home, h, setup);
88 if (r < 0)
89 return r;
90
2ba38f78 91 /* Close fd to private mount before moving mount */
a74e2e44 92 setup->root_fd = safe_close(setup->root_fd);
70a5db58 93
2ba38f78
LP
94 /* We are now done with everything, move the mount into place */
95 r = home_move_mount(NULL, hd);
70a5db58
LP
96 if (r < 0)
97 return r;
98
2ba38f78 99 setup->undo_mount = false;
70a5db58 100
aa0379f1
LP
101 setup->do_drop_caches = false;
102
70a5db58
LP
103 log_info("Everything completed.");
104
105 *ret_home = TAKE_PTR(new_home);
106 return 0;
107}
108
e1aeaf27 109int home_create_directory_or_subvolume(UserRecord *h, HomeSetup *setup, UserRecord **ret_home) {
70a5db58
LP
110 _cleanup_(rm_rf_subvolume_and_freep) char *temporary = NULL;
111 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
254d1313 112 _cleanup_close_ int mount_fd = -EBADF;
70a5db58 113 _cleanup_free_ char *d = NULL;
cf5115f6 114 bool is_subvolume = false;
70a5db58
LP
115 const char *ip;
116 int r;
117
118 assert(h);
119 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME));
e1aeaf27 120 assert(setup);
70a5db58
LP
121 assert(ret_home);
122
123 assert_se(ip = user_record_image_path(h));
124
125 r = tempfn_random(ip, "homework", &d);
126 if (r < 0)
127 return log_error_errno(r, "Failed to allocate temporary directory: %m");
128
129 (void) mkdir_parents(d, 0755);
130
131 switch (user_record_storage(h)) {
132
133 case USER_SUBVOLUME:
2053593f 134 WITH_UMASK(0077)
e54c79cc 135 r = btrfs_subvol_make(AT_FDCWD, d);
70a5db58
LP
136
137 if (r >= 0) {
138 log_info("Subvolume created.");
cf5115f6 139 is_subvolume = true;
70a5db58
LP
140
141 if (h->disk_size != UINT64_MAX) {
142
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);
146 if (r < 0)
147 log_debug_errno(r, "Failed to enable quota on %s, ignoring: %m", d);
148
149 r = btrfs_subvol_auto_qgroup(d, 0, false);
150 if (r < 0)
151 log_debug_errno(r, "Failed to set up automatic quota group on %s, ignoring: %m", d);
152
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);
157 }
158
159 break;
160 }
161 if (r != -ENOTTY)
162 return log_error_errno(r, "Failed to create temporary home directory subvolume %s: %m", d);
163
164 log_info("Creating subvolume %s is not supported, as file system does not support subvolumes. Falling back to regular directory.", d);
165 _fallthrough_;
166
167 case USER_DIRECTORY:
168
169 if (mkdir(d, 0700) < 0)
170 return log_error_errno(errno, "Failed to create temporary home directory %s: %m", d);
171
172 (void) home_update_quota_classic(h, d);
173 break;
174
175 default:
04499a70 176 assert_not_reached();
70a5db58
LP
177 }
178
179 temporary = TAKE_PTR(d); /* Needs to be destroyed now */
180
cf5115f6 181 /* Let's decouple namespaces now, so that we can possibly mount a UID map mount into
a6f44d61 182 * /run/systemd/user-home-mount/ that no one will see but us. */
cf5115f6
LP
183 r = home_unshare_and_mkdir();
184 if (r < 0)
185 return r;
186
e1aeaf27
LP
187 setup->root_fd = open(temporary, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
188 if (setup->root_fd < 0)
70a5db58
LP
189 return log_error_errno(errno, "Failed to open temporary home directory: %m");
190
cf5115f6
LP
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
198 * mount. */
199 r = home_shift_uid(setup->root_fd, HOME_RUNTIME_WORK_DIR, is_subvolume ? UID_NOBODY : h->uid, h->uid, &mount_fd);
200 if (r > 0)
201 setup->undo_mount = true; /* If uidmaps worked we have a mount to undo again */
202
203 if (mount_fd >= 0) {
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);
206
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");
210
211 mount_fd = safe_close(mount_fd);
212 }
213
e1aeaf27 214 r = home_populate(h, setup->root_fd);
70a5db58
LP
215 if (r < 0)
216 return r;
217
e1aeaf27 218 r = home_sync_and_statfs(setup->root_fd, NULL);
70a5db58
LP
219 if (r < 0)
220 return r;
221
bfc0cc1a 222 r = user_record_clone(h, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_PERMISSIVE, &new_home);
70a5db58
LP
223 if (r < 0)
224 return log_error_errno(r, "Failed to clone record: %m");
225
226 r = user_record_add_binding(
227 new_home,
228 user_record_storage(h),
229 ip,
230 SD_ID128_NULL,
231 SD_ID128_NULL,
232 SD_ID128_NULL,
233 NULL,
234 NULL,
235 UINT64_MAX,
236 NULL,
237 NULL,
238 h->uid,
239 (gid_t) h->uid);
240 if (r < 0)
241 return log_error_errno(r, "Failed to add binding to record: %m");
242
cf5115f6
LP
243 setup->root_fd = safe_close(setup->root_fd);
244
245 /* Unmount mapped mount before we move the dir into place */
55166094
LP
246 r = home_setup_undo_mount(setup, LOG_ERR);
247 if (r < 0)
248 return r;
cf5115f6 249
70a5db58
LP
250 if (rename(temporary, ip) < 0)
251 return log_error_errno(errno, "Failed to rename %s to %s: %m", temporary, ip);
252
253 temporary = mfree(temporary);
254
255 log_info("Everything completed.");
256
257 *ret_home = TAKE_PTR(new_home);
258 return 0;
259}
260
261int home_resize_directory(
262 UserRecord *h,
e1df968b 263 HomeSetupFlags flags,
70a5db58 264 HomeSetup *setup,
c00b2ddc 265 PasswordCache *cache,
70a5db58
LP
266 UserRecord **ret_home) {
267
268 _cleanup_(user_record_unrefp) UserRecord *embedded_home = NULL, *new_home = NULL;
17ac40e4 269 int r, reconciled;
70a5db58
LP
270
271 assert(h);
272 assert(setup);
273 assert(ret_home);
274 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT));
275
c00b2ddc 276 r = home_setup(h, flags, setup, cache, NULL);
70a5db58
LP
277 if (r < 0)
278 return r;
279
17ac40e4
AV
280 reconciled = home_load_embedded_identity(h, setup->root_fd, NULL, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL, cache, &embedded_home, &new_home);
281 if (reconciled < 0)
282 return reconciled;
70a5db58 283
6f2c8136 284 r = home_maybe_shift_uid(h, flags, setup);
cf5115f6
LP
285 if (r < 0)
286 return r;
287
70a5db58 288 r = home_update_quota_auto(h, NULL);
bb44fd07
ZJS
289 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
290 return -ESOCKTNOSUPPORT; /* make recognizable */
291 if (r < 0)
70a5db58
LP
292 return r;
293
285ad523 294 r = home_store_embedded_identity(new_home, setup->root_fd, embedded_home);
70a5db58
LP
295 if (r < 0)
296 return r;
297
17ac40e4
AV
298 r = home_reconcile_blob_dirs(new_home, setup->root_fd, reconciled);
299 if (r < 0)
300 return r;
301
70a5db58
LP
302 r = home_extend_embedded_identity(new_home, h, setup);
303 if (r < 0)
304 return r;
305
306 r = home_sync_and_statfs(setup->root_fd, NULL);
307 if (r < 0)
308 return r;
309
66aa51f8 310 r = home_setup_done(setup);
70a5db58
LP
311 if (r < 0)
312 return r;
313
314 log_info("Everything completed.");
315
316 *ret_home = TAKE_PTR(new_home);
317 return 0;
318}