]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/home/homework-directory.c
homework: Reconcile blob directories
[thirdparty/systemd.git] / src / home / homework-directory.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/mount.h>
4
5 #include "btrfs-util.h"
6 #include "fd-util.h"
7 #include "homework-blob.h"
8 #include "homework-directory.h"
9 #include "homework-mount.h"
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"
17 #include "user-util.h"
18
19 int home_setup_directory(UserRecord *h, HomeSetup *setup) {
20 const char *ip;
21 int r;
22
23 assert(h);
24 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME));
25 assert(setup);
26 assert(!setup->undo_mount);
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;
40
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);
54 if (setup->root_fd < 0)
55 return log_error_errno(errno, "Failed to open home directory: %m");
56
57 return 0;
58 }
59
60 int home_activate_directory(
61 UserRecord *h,
62 HomeSetupFlags flags,
63 HomeSetup *setup,
64 PasswordCache *cache,
65 UserRecord **ret_home) {
66
67 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL, *header_home = NULL;
68 const char *hd, *hdo;
69 int r;
70
71 assert(h);
72 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT));
73 assert(setup);
74 assert(ret_home);
75
76 assert_se(hdo = user_record_home_directory(h));
77 hd = strdupa_safe(hdo);
78
79 r = home_setup(h, flags, setup, cache, &header_home);
80 if (r < 0)
81 return r;
82
83 r = home_refresh(h, flags, setup, header_home, cache, NULL, &new_home);
84 if (r < 0)
85 return r;
86
87 r = home_extend_embedded_identity(new_home, h, setup);
88 if (r < 0)
89 return r;
90
91 /* Close fd to private mount before moving mount */
92 setup->root_fd = safe_close(setup->root_fd);
93
94 /* We are now done with everything, move the mount into place */
95 r = home_move_mount(NULL, hd);
96 if (r < 0)
97 return r;
98
99 setup->undo_mount = false;
100
101 setup->do_drop_caches = false;
102
103 log_info("Everything completed.");
104
105 *ret_home = TAKE_PTR(new_home);
106 return 0;
107 }
108
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;
115 const char *ip;
116 int r;
117
118 assert(h);
119 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME));
120 assert(setup);
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:
134 WITH_UMASK(0077)
135 r = btrfs_subvol_make(AT_FDCWD, d);
136
137 if (r >= 0) {
138 log_info("Subvolume created.");
139 is_subvolume = true;
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:
176 assert_not_reached();
177 }
178
179 temporary = TAKE_PTR(d); /* Needs to be destroyed now */
180
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();
184 if (r < 0)
185 return r;
186
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");
190
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
214 r = home_populate(h, setup->root_fd);
215 if (r < 0)
216 return r;
217
218 r = home_sync_and_statfs(setup->root_fd, NULL);
219 if (r < 0)
220 return r;
221
222 r = user_record_clone(h, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_PERMISSIVE, &new_home);
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
243 setup->root_fd = safe_close(setup->root_fd);
244
245 /* Unmount mapped mount before we move the dir into place */
246 r = home_setup_undo_mount(setup, LOG_ERR);
247 if (r < 0)
248 return r;
249
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
261 int home_resize_directory(
262 UserRecord *h,
263 HomeSetupFlags flags,
264 HomeSetup *setup,
265 PasswordCache *cache,
266 UserRecord **ret_home) {
267
268 _cleanup_(user_record_unrefp) UserRecord *embedded_home = NULL, *new_home = NULL;
269 int r, reconciled;
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
276 r = home_setup(h, flags, setup, cache, NULL);
277 if (r < 0)
278 return r;
279
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;
283
284 r = home_maybe_shift_uid(h, flags, setup);
285 if (r < 0)
286 return r;
287
288 r = home_update_quota_auto(h, NULL);
289 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
290 return -ESOCKTNOSUPPORT; /* make recognizable */
291 if (r < 0)
292 return r;
293
294 r = home_store_embedded_identity(new_home, setup->root_fd, embedded_home);
295 if (r < 0)
296 return r;
297
298 r = home_reconcile_blob_dirs(new_home, setup->root_fd, reconciled);
299 if (r < 0)
300 return r;
301
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
310 r = home_setup_done(setup);
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 }