]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/home/homework-directory.c
homework: Cleanup home_store_embedded_identity
[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"
7#include "homework-directory.h"
2ba38f78 8#include "homework-mount.h"
70a5db58
LP
9#include "homework-quota.h"
10#include "mkdir.h"
11#include "mount-util.h"
12#include "path-util.h"
13#include "rm-rf.h"
14#include "tmpfile-util.h"
15#include "umask-util.h"
cf5115f6 16#include "user-util.h"
70a5db58 17
e1df968b 18int home_setup_directory(UserRecord *h, HomeSetup *setup) {
2ba38f78
LP
19 const char *ip;
20 int r;
21
70a5db58 22 assert(h);
cf5115f6 23 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME));
70a5db58 24 assert(setup);
cf5115f6 25 assert(!setup->undo_mount);
2ba38f78
LP
26 assert(setup->root_fd < 0);
27
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();
31 if (r < 0)
32 return r;
33
34 assert_se(ip = user_record_image_path(h));
35
36 r = mount_follow_verbose(LOG_ERR, ip, HOME_RUNTIME_WORK_DIR, NULL, MS_BIND, NULL);
37 if (r < 0)
38 return r;
70a5db58 39
2ba38f78
LP
40 setup->undo_mount = true;
41
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);
44 if (r < 0)
45 return r;
46
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);
49 if (r < 0)
50 return r;
51
52 setup->root_fd = open(HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
70a5db58
LP
53 if (setup->root_fd < 0)
54 return log_error_errno(errno, "Failed to open home directory: %m");
55
56 return 0;
57}
58
59int home_activate_directory(
60 UserRecord *h,
6f2c8136 61 HomeSetupFlags flags,
a74e2e44 62 HomeSetup *setup,
7b78db28 63 PasswordCache *cache,
70a5db58
LP
64 UserRecord **ret_home) {
65
66 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL, *header_home = NULL;
2ba38f78 67 const char *hd, *hdo;
70a5db58
LP
68 int r;
69
70 assert(h);
71 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT));
a74e2e44 72 assert(setup);
70a5db58
LP
73 assert(ret_home);
74
70a5db58 75 assert_se(hdo = user_record_home_directory(h));
2f82562b 76 hd = strdupa_safe(hdo);
70a5db58 77
6f2c8136 78 r = home_setup(h, flags, setup, cache, &header_home);
70a5db58
LP
79 if (r < 0)
80 return r;
81
6f2c8136 82 r = home_refresh(h, flags, setup, header_home, cache, NULL, &new_home);
70a5db58
LP
83 if (r < 0)
84 return r;
85
cfef46f5
LP
86 r = home_extend_embedded_identity(new_home, h, setup);
87 if (r < 0)
88 return r;
89
2ba38f78 90 /* Close fd to private mount before moving mount */
a74e2e44 91 setup->root_fd = safe_close(setup->root_fd);
70a5db58 92
2ba38f78
LP
93 /* We are now done with everything, move the mount into place */
94 r = home_move_mount(NULL, hd);
70a5db58
LP
95 if (r < 0)
96 return r;
97
2ba38f78 98 setup->undo_mount = false;
70a5db58 99
aa0379f1
LP
100 setup->do_drop_caches = false;
101
70a5db58
LP
102 log_info("Everything completed.");
103
104 *ret_home = TAKE_PTR(new_home);
105 return 0;
106}
107
e1aeaf27 108int home_create_directory_or_subvolume(UserRecord *h, HomeSetup *setup, UserRecord **ret_home) {
70a5db58
LP
109 _cleanup_(rm_rf_subvolume_and_freep) char *temporary = NULL;
110 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
254d1313 111 _cleanup_close_ int mount_fd = -EBADF;
70a5db58 112 _cleanup_free_ char *d = NULL;
cf5115f6 113 bool is_subvolume = false;
70a5db58
LP
114 const char *ip;
115 int r;
116
117 assert(h);
118 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME));
e1aeaf27 119 assert(setup);
70a5db58
LP
120 assert(ret_home);
121
122 assert_se(ip = user_record_image_path(h));
123
124 r = tempfn_random(ip, "homework", &d);
125 if (r < 0)
126 return log_error_errno(r, "Failed to allocate temporary directory: %m");
127
128 (void) mkdir_parents(d, 0755);
129
130 switch (user_record_storage(h)) {
131
132 case USER_SUBVOLUME:
2053593f 133 WITH_UMASK(0077)
e54c79cc 134 r = btrfs_subvol_make(AT_FDCWD, d);
70a5db58
LP
135
136 if (r >= 0) {
137 log_info("Subvolume created.");
cf5115f6 138 is_subvolume = true;
70a5db58
LP
139
140 if (h->disk_size != UINT64_MAX) {
141
142 /* Enable quota for the subvolume we just created. Note we don't check for
143 * errors here and only log about debug level about this. */
144 r = btrfs_quota_enable(d, true);
145 if (r < 0)
146 log_debug_errno(r, "Failed to enable quota on %s, ignoring: %m", d);
147
148 r = btrfs_subvol_auto_qgroup(d, 0, false);
149 if (r < 0)
150 log_debug_errno(r, "Failed to set up automatic quota group on %s, ignoring: %m", d);
151
152 /* Actually configure the quota. We also ignore errors here, but we do log
153 * about them loudly, to keep things discoverable even though we don't
154 * consider lacking quota support in kernel fatal. */
155 (void) home_update_quota_btrfs(h, d);
156 }
157
158 break;
159 }
160 if (r != -ENOTTY)
161 return log_error_errno(r, "Failed to create temporary home directory subvolume %s: %m", d);
162
163 log_info("Creating subvolume %s is not supported, as file system does not support subvolumes. Falling back to regular directory.", d);
164 _fallthrough_;
165
166 case USER_DIRECTORY:
167
168 if (mkdir(d, 0700) < 0)
169 return log_error_errno(errno, "Failed to create temporary home directory %s: %m", d);
170
171 (void) home_update_quota_classic(h, d);
172 break;
173
174 default:
04499a70 175 assert_not_reached();
70a5db58
LP
176 }
177
178 temporary = TAKE_PTR(d); /* Needs to be destroyed now */
179
cf5115f6 180 /* Let's decouple namespaces now, so that we can possibly mount a UID map mount into
a6f44d61 181 * /run/systemd/user-home-mount/ that no one will see but us. */
cf5115f6
LP
182 r = home_unshare_and_mkdir();
183 if (r < 0)
184 return r;
185
e1aeaf27
LP
186 setup->root_fd = open(temporary, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
187 if (setup->root_fd < 0)
70a5db58
LP
188 return log_error_errno(errno, "Failed to open temporary home directory: %m");
189
cf5115f6
LP
190 /* Try to apply a UID shift, so that the directory is actually owned by "nobody", and is only mapped
191 * to the proper UID while active. — Well, that's at least the theory. Unfortunately, only btrfs does
192 * per-subvolume quota. The others do per-uid quota. Which means mapping all home directories to the
193 * same UID of "nobody" makes quota impossible. Hence unless we actually managed to create a btrfs
194 * subvolume for this user we'll map the user's UID to itself. Now you might ask: why bother mapping
195 * at all? It's because we want to restrict the UIDs used on the home directory: we leave all other
196 * UIDs of the homed UID range unmapped, thus making them unavailable to programs accessing the
197 * mount. */
198 r = home_shift_uid(setup->root_fd, HOME_RUNTIME_WORK_DIR, is_subvolume ? UID_NOBODY : h->uid, h->uid, &mount_fd);
199 if (r > 0)
200 setup->undo_mount = true; /* If uidmaps worked we have a mount to undo again */
201
202 if (mount_fd >= 0) {
203 /* If we have established a new mount, then we can use that as new root fd to our home directory. */
204 safe_close(setup->root_fd);
205
206 setup->root_fd = fd_reopen(mount_fd, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
207 if (setup->root_fd < 0)
208 return log_error_errno(setup->root_fd, "Unable to convert mount fd into proper directory fd: %m");
209
210 mount_fd = safe_close(mount_fd);
211 }
212
e1aeaf27 213 r = home_populate(h, setup->root_fd);
70a5db58
LP
214 if (r < 0)
215 return r;
216
e1aeaf27 217 r = home_sync_and_statfs(setup->root_fd, NULL);
70a5db58
LP
218 if (r < 0)
219 return r;
220
bfc0cc1a 221 r = user_record_clone(h, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_PERMISSIVE, &new_home);
70a5db58
LP
222 if (r < 0)
223 return log_error_errno(r, "Failed to clone record: %m");
224
225 r = user_record_add_binding(
226 new_home,
227 user_record_storage(h),
228 ip,
229 SD_ID128_NULL,
230 SD_ID128_NULL,
231 SD_ID128_NULL,
232 NULL,
233 NULL,
234 UINT64_MAX,
235 NULL,
236 NULL,
237 h->uid,
238 (gid_t) h->uid);
239 if (r < 0)
240 return log_error_errno(r, "Failed to add binding to record: %m");
241
cf5115f6
LP
242 setup->root_fd = safe_close(setup->root_fd);
243
244 /* Unmount mapped mount before we move the dir into place */
55166094
LP
245 r = home_setup_undo_mount(setup, LOG_ERR);
246 if (r < 0)
247 return r;
cf5115f6 248
70a5db58
LP
249 if (rename(temporary, ip) < 0)
250 return log_error_errno(errno, "Failed to rename %s to %s: %m", temporary, ip);
251
252 temporary = mfree(temporary);
253
254 log_info("Everything completed.");
255
256 *ret_home = TAKE_PTR(new_home);
257 return 0;
258}
259
260int home_resize_directory(
261 UserRecord *h,
e1df968b 262 HomeSetupFlags flags,
70a5db58 263 HomeSetup *setup,
c00b2ddc 264 PasswordCache *cache,
70a5db58
LP
265 UserRecord **ret_home) {
266
267 _cleanup_(user_record_unrefp) UserRecord *embedded_home = NULL, *new_home = NULL;
268 int r;
269
270 assert(h);
271 assert(setup);
272 assert(ret_home);
273 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT));
274
c00b2ddc 275 r = home_setup(h, flags, setup, cache, NULL);
70a5db58
LP
276 if (r < 0)
277 return r;
278
7b78db28 279 r = home_load_embedded_identity(h, setup->root_fd, NULL, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL, cache, &embedded_home, &new_home);
70a5db58
LP
280 if (r < 0)
281 return r;
282
6f2c8136 283 r = home_maybe_shift_uid(h, flags, setup);
cf5115f6
LP
284 if (r < 0)
285 return r;
286
70a5db58 287 r = home_update_quota_auto(h, NULL);
bb44fd07
ZJS
288 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
289 return -ESOCKTNOSUPPORT; /* make recognizable */
290 if (r < 0)
70a5db58
LP
291 return r;
292
285ad523 293 r = home_store_embedded_identity(new_home, setup->root_fd, embedded_home);
70a5db58
LP
294 if (r < 0)
295 return r;
296
297 r = home_extend_embedded_identity(new_home, h, setup);
298 if (r < 0)
299 return r;
300
301 r = home_sync_and_statfs(setup->root_fd, NULL);
302 if (r < 0)
303 return r;
304
66aa51f8 305 r = home_setup_done(setup);
70a5db58
LP
306 if (r < 0)
307 return r;
308
309 log_info("Everything completed.");
310
311 *ret_home = TAKE_PTR(new_home);
312 return 0;
313}