]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/home/homework-directory.c
homework: fix message typo
[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,
a74e2e44 61 HomeSetup *setup,
7b78db28 62 PasswordCache *cache,
70a5db58
LP
63 UserRecord **ret_home) {
64
65 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL, *header_home = NULL;
2ba38f78 66 const char *hd, *hdo;
70a5db58
LP
67 int r;
68
69 assert(h);
70 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT));
a74e2e44 71 assert(setup);
70a5db58
LP
72 assert(ret_home);
73
70a5db58 74 assert_se(hdo = user_record_home_directory(h));
2f82562b 75 hd = strdupa_safe(hdo);
70a5db58 76
c00b2ddc 77 r = home_setup(h, 0, setup, cache, &header_home);
70a5db58
LP
78 if (r < 0)
79 return r;
80
a74e2e44 81 r = home_refresh(h, setup, header_home, cache, NULL, &new_home);
70a5db58
LP
82 if (r < 0)
83 return r;
84
cfef46f5
LP
85 r = home_extend_embedded_identity(new_home, h, setup);
86 if (r < 0)
87 return r;
88
2ba38f78 89 /* Close fd to private mount before moving mount */
a74e2e44 90 setup->root_fd = safe_close(setup->root_fd);
70a5db58 91
2ba38f78
LP
92 /* We are now done with everything, move the mount into place */
93 r = home_move_mount(NULL, hd);
70a5db58
LP
94 if (r < 0)
95 return r;
96
2ba38f78 97 setup->undo_mount = false;
70a5db58 98
aa0379f1
LP
99 setup->do_drop_caches = false;
100
70a5db58
LP
101 log_info("Everything completed.");
102
103 *ret_home = TAKE_PTR(new_home);
104 return 0;
105}
106
e1aeaf27 107int home_create_directory_or_subvolume(UserRecord *h, HomeSetup *setup, UserRecord **ret_home) {
70a5db58
LP
108 _cleanup_(rm_rf_subvolume_and_freep) char *temporary = NULL;
109 _cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
cf5115f6 110 _cleanup_close_ int mount_fd = -1;
70a5db58 111 _cleanup_free_ char *d = NULL;
cf5115f6 112 bool is_subvolume = false;
70a5db58
LP
113 const char *ip;
114 int r;
115
116 assert(h);
117 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME));
e1aeaf27 118 assert(setup);
70a5db58
LP
119 assert(ret_home);
120
121 assert_se(ip = user_record_image_path(h));
122
123 r = tempfn_random(ip, "homework", &d);
124 if (r < 0)
125 return log_error_errno(r, "Failed to allocate temporary directory: %m");
126
127 (void) mkdir_parents(d, 0755);
128
129 switch (user_record_storage(h)) {
130
131 case USER_SUBVOLUME:
132 RUN_WITH_UMASK(0077)
133 r = btrfs_subvol_make(d);
134
135 if (r >= 0) {
136 log_info("Subvolume created.");
cf5115f6 137 is_subvolume = true;
70a5db58
LP
138
139 if (h->disk_size != UINT64_MAX) {
140
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);
144 if (r < 0)
145 log_debug_errno(r, "Failed to enable quota on %s, ignoring: %m", d);
146
147 r = btrfs_subvol_auto_qgroup(d, 0, false);
148 if (r < 0)
149 log_debug_errno(r, "Failed to set up automatic quota group on %s, ignoring: %m", d);
150
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);
155 }
156
157 break;
158 }
159 if (r != -ENOTTY)
160 return log_error_errno(r, "Failed to create temporary home directory subvolume %s: %m", d);
161
162 log_info("Creating subvolume %s is not supported, as file system does not support subvolumes. Falling back to regular directory.", d);
163 _fallthrough_;
164
165 case USER_DIRECTORY:
166
167 if (mkdir(d, 0700) < 0)
168 return log_error_errno(errno, "Failed to create temporary home directory %s: %m", d);
169
170 (void) home_update_quota_classic(h, d);
171 break;
172
173 default:
04499a70 174 assert_not_reached();
70a5db58
LP
175 }
176
177 temporary = TAKE_PTR(d); /* Needs to be destroyed now */
178
cf5115f6
LP
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();
182 if (r < 0)
183 return r;
184
e1aeaf27
LP
185 setup->root_fd = open(temporary, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
186 if (setup->root_fd < 0)
70a5db58
LP
187 return log_error_errno(errno, "Failed to open temporary home directory: %m");
188
cf5115f6
LP
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
196 * mount. */
197 r = home_shift_uid(setup->root_fd, HOME_RUNTIME_WORK_DIR, is_subvolume ? UID_NOBODY : h->uid, h->uid, &mount_fd);
198 if (r > 0)
199 setup->undo_mount = true; /* If uidmaps worked we have a mount to undo again */
200
201 if (mount_fd >= 0) {
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);
204
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");
208
209 mount_fd = safe_close(mount_fd);
210 }
211
e1aeaf27 212 r = home_populate(h, setup->root_fd);
70a5db58
LP
213 if (r < 0)
214 return r;
215
e1aeaf27 216 r = home_sync_and_statfs(setup->root_fd, NULL);
70a5db58
LP
217 if (r < 0)
218 return r;
219
bfc0cc1a 220 r = user_record_clone(h, USER_RECORD_LOAD_MASK_SECRET|USER_RECORD_PERMISSIVE, &new_home);
70a5db58
LP
221 if (r < 0)
222 return log_error_errno(r, "Failed to clone record: %m");
223
224 r = user_record_add_binding(
225 new_home,
226 user_record_storage(h),
227 ip,
228 SD_ID128_NULL,
229 SD_ID128_NULL,
230 SD_ID128_NULL,
231 NULL,
232 NULL,
233 UINT64_MAX,
234 NULL,
235 NULL,
236 h->uid,
237 (gid_t) h->uid);
238 if (r < 0)
239 return log_error_errno(r, "Failed to add binding to record: %m");
240
cf5115f6
LP
241 setup->root_fd = safe_close(setup->root_fd);
242
243 /* Unmount mapped mount before we move the dir into place */
55166094
LP
244 r = home_setup_undo_mount(setup, LOG_ERR);
245 if (r < 0)
246 return r;
cf5115f6 247
70a5db58
LP
248 if (rename(temporary, ip) < 0)
249 return log_error_errno(errno, "Failed to rename %s to %s: %m", temporary, ip);
250
251 temporary = mfree(temporary);
252
253 log_info("Everything completed.");
254
255 *ret_home = TAKE_PTR(new_home);
256 return 0;
257}
258
259int home_resize_directory(
260 UserRecord *h,
e1df968b 261 HomeSetupFlags flags,
70a5db58 262 HomeSetup *setup,
c00b2ddc 263 PasswordCache *cache,
70a5db58
LP
264 UserRecord **ret_home) {
265
266 _cleanup_(user_record_unrefp) UserRecord *embedded_home = NULL, *new_home = NULL;
267 int r;
268
269 assert(h);
270 assert(setup);
271 assert(ret_home);
272 assert(IN_SET(user_record_storage(h), USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT));
273
c00b2ddc 274 r = home_setup(h, flags, setup, cache, NULL);
70a5db58
LP
275 if (r < 0)
276 return r;
277
7b78db28 278 r = home_load_embedded_identity(h, setup->root_fd, NULL, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL, cache, &embedded_home, &new_home);
70a5db58
LP
279 if (r < 0)
280 return r;
281
cf5115f6
LP
282 r = home_maybe_shift_uid(h, setup);
283 if (r < 0)
284 return r;
285
70a5db58
LP
286 r = home_update_quota_auto(h, NULL);
287 if (ERRNO_IS_NOT_SUPPORTED(r))
288 return -ESOCKTNOSUPPORT; /* make recognizable */
289 if (r < 0)
290 return r;
291
292 r = home_store_embedded_identity(new_home, setup->root_fd, h->uid, embedded_home);
293 if (r < 0)
294 return r;
295
296 r = home_extend_embedded_identity(new_home, h, setup);
297 if (r < 0)
298 return r;
299
300 r = home_sync_and_statfs(setup->root_fd, NULL);
301 if (r < 0)
302 return r;
303
66aa51f8 304 r = home_setup_done(setup);
70a5db58
LP
305 if (r < 0)
306 return r;
307
308 log_info("Everything completed.");
309
310 *ret_home = TAKE_PTR(new_home);
311 return 0;
312}