1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "btrfs-util.h"
7 #include "homework-directory.h"
8 #include "homework-quota.h"
10 #include "mount-util.h"
11 #include "path-util.h"
13 #include "tmpfile-util.h"
14 #include "umask-util.h"
16 int home_setup_directory(UserRecord
*h
, HomeSetup
*setup
) {
20 setup
->root_fd
= open(user_record_image_path(h
), O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
21 if (setup
->root_fd
< 0)
22 return log_error_errno(errno
, "Failed to open home directory: %m");
27 int home_activate_directory(
31 UserRecord
**ret_home
) {
33 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
, *header_home
= NULL
;
34 const char *hdo
, *hd
, *ipo
, *ip
;
38 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
));
42 assert_se(ipo
= user_record_image_path(h
));
43 ip
= strdupa_safe(ipo
); /* copy out, since reconciliation might cause changing of the field */
45 assert_se(hdo
= user_record_home_directory(h
));
46 hd
= strdupa_safe(hdo
);
48 r
= home_setup(h
, 0, cache
, setup
, &header_home
);
52 r
= home_refresh(h
, setup
, header_home
, cache
, NULL
, &new_home
);
56 setup
->root_fd
= safe_close(setup
->root_fd
);
58 /* Create mount point to mount over if necessary */
59 if (!path_equal(ip
, hd
))
60 (void) mkdir_p(hd
, 0700);
62 /* Create a mount point (even if the directory is already placed correctly), as a way to indicate
63 * this mount point is now "activated". Moreover, we want to set per-user
64 * MS_NOSUID/MS_NOEXEC/MS_NODEV. */
65 r
= mount_nofollow_verbose(LOG_ERR
, ip
, hd
, NULL
, MS_BIND
, NULL
);
69 r
= mount_nofollow_verbose(LOG_ERR
, NULL
, hd
, NULL
, MS_BIND
|MS_REMOUNT
|user_record_mount_flags(h
), NULL
);
71 (void) umount_verbose(LOG_ERR
, hd
, UMOUNT_NOFOLLOW
);
75 log_info("Everything completed.");
77 *ret_home
= TAKE_PTR(new_home
);
81 int home_create_directory_or_subvolume(UserRecord
*h
, UserRecord
**ret_home
) {
82 _cleanup_(rm_rf_subvolume_and_freep
) char *temporary
= NULL
;
83 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
;
84 _cleanup_close_
int root_fd
= -1;
85 _cleanup_free_
char *d
= NULL
;
90 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
));
93 assert_se(ip
= user_record_image_path(h
));
95 r
= tempfn_random(ip
, "homework", &d
);
97 return log_error_errno(r
, "Failed to allocate temporary directory: %m");
99 (void) mkdir_parents(d
, 0755);
101 switch (user_record_storage(h
)) {
105 r
= btrfs_subvol_make(d
);
108 log_info("Subvolume created.");
110 if (h
->disk_size
!= UINT64_MAX
) {
112 /* Enable quota for the subvolume we just created. Note we don't check for
113 * errors here and only log about debug level about this. */
114 r
= btrfs_quota_enable(d
, true);
116 log_debug_errno(r
, "Failed to enable quota on %s, ignoring: %m", d
);
118 r
= btrfs_subvol_auto_qgroup(d
, 0, false);
120 log_debug_errno(r
, "Failed to set up automatic quota group on %s, ignoring: %m", d
);
122 /* Actually configure the quota. We also ignore errors here, but we do log
123 * about them loudly, to keep things discoverable even though we don't
124 * consider lacking quota support in kernel fatal. */
125 (void) home_update_quota_btrfs(h
, d
);
131 return log_error_errno(r
, "Failed to create temporary home directory subvolume %s: %m", d
);
133 log_info("Creating subvolume %s is not supported, as file system does not support subvolumes. Falling back to regular directory.", d
);
138 if (mkdir(d
, 0700) < 0)
139 return log_error_errno(errno
, "Failed to create temporary home directory %s: %m", d
);
141 (void) home_update_quota_classic(h
, d
);
145 assert_not_reached();
148 temporary
= TAKE_PTR(d
); /* Needs to be destroyed now */
150 root_fd
= open(temporary
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
152 return log_error_errno(errno
, "Failed to open temporary home directory: %m");
154 r
= home_populate(h
, root_fd
);
158 r
= home_sync_and_statfs(root_fd
, NULL
);
162 r
= user_record_clone(h
, USER_RECORD_LOAD_MASK_SECRET
|USER_RECORD_PERMISSIVE
, &new_home
);
164 return log_error_errno(r
, "Failed to clone record: %m");
166 r
= user_record_add_binding(
168 user_record_storage(h
),
181 return log_error_errno(r
, "Failed to add binding to record: %m");
183 if (rename(temporary
, ip
) < 0)
184 return log_error_errno(errno
, "Failed to rename %s to %s: %m", temporary
, ip
);
186 temporary
= mfree(temporary
);
188 log_info("Everything completed.");
190 *ret_home
= TAKE_PTR(new_home
);
194 int home_resize_directory(
196 HomeSetupFlags flags
,
197 PasswordCache
*cache
,
199 UserRecord
**ret_home
) {
201 _cleanup_(user_record_unrefp
) UserRecord
*embedded_home
= NULL
, *new_home
= NULL
;
207 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
));
209 r
= home_setup(h
, flags
, cache
, setup
, NULL
);
213 r
= home_load_embedded_identity(h
, setup
->root_fd
, NULL
, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL
, cache
, &embedded_home
, &new_home
);
217 r
= home_update_quota_auto(h
, NULL
);
218 if (ERRNO_IS_NOT_SUPPORTED(r
))
219 return -ESOCKTNOSUPPORT
; /* make recognizable */
223 r
= home_store_embedded_identity(new_home
, setup
->root_fd
, h
->uid
, embedded_home
);
227 r
= home_extend_embedded_identity(new_home
, h
, setup
);
231 r
= home_sync_and_statfs(setup
->root_fd
, NULL
);
235 r
= home_setup_done(setup
);
239 log_info("Everything completed.");
241 *ret_home
= TAKE_PTR(new_home
);