HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/home/homed-home.c
Git blob: 12d83b8f6fbc3ea7861f40de99c2ab85b02f6207
Lines 2001–3000 of 3,443
int r;
assert(h);
r = user_record_test_password_change_required(h->record);
if (IN_SET(r, -EKEYREVOKED, -EOWNERDEAD, -EKEYEXPIRED, -ESTALE))
return 0; /* expired in some form, but changing is allowed */
if (IN_SET(r, -EKEYREJECTED, -EROFS))
return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Expiration settings of account %s do not allow changing of password.", h->user_name);
if (r < 0)
return log_error_errno(r, "Failed to test password expiry: %m");
return 0; /* not expired */
}
int home_passwd(Home *h,
UserRecord *new_secret,
UserRecord *old_secret,
sd_bus_error *error) {
_cleanup_(user_record_unrefp) UserRecord *c = NULL, *merged_secret = NULL, *signed_c = NULL;
HomeState state;
int r;
assert(h);
assert(new_secret);
assert(old_secret);
if (h->signed_locally <= 0) /* Don't allow changing of records not signed only by us */
return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_SIGNED, "Home %s is signed and cannot be modified locally.", h->user_name);
state = home_get_state(h);
switch (state) {
case HOME_UNFIXATED:
return sd_bus_error_setf(error, BUS_ERROR_HOME_UNFIXATED, "Home %s has not been fixated yet.", h->user_name);
case HOME_ABSENT:
return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
case HOME_LOCKED:
return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
case HOME_INACTIVE:
case HOME_DIRTY:
case HOME_ACTIVE:
case HOME_LINGERING:
break;
default:
return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
}
r = home_ratelimit(h, error);
if (r < 0)
return r;
r = home_may_change_password(h, error);
if (r < 0)
return r;
r = user_record_clone(h->record, USER_RECORD_LOAD_REFUSE_SECRET|USER_RECORD_PERMISSIVE, &c);
if (r < 0)
return r;
merged_secret = user_record_new();
if (!merged_secret)
return -ENOMEM;
r = user_record_merge_secret(merged_secret, old_secret);
if (r < 0)
return r;
r = user_record_merge_secret(merged_secret, new_secret);
if (r < 0)
return r;
if (!strv_isempty(new_secret->password)) {
/* Update the password only if one is specified, otherwise let's just reuse the old password
* data. This is useful as a way to propagate updated user records into the LUKS backends
* properly. */
r = user_record_make_hashed_password(c, new_secret->password, /* extend= */ false);
if (r < 0)
return r;
r = user_record_set_password_change_now(c, -1 /* remove */);
if (r < 0)
return r;
}
r = user_record_update_last_changed(c, true);
if (r == -ECHRNG)
return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_MISMATCH, "Record last change time of %s is newer than current time, cannot update.", h->user_name);
if (r < 0)
return r;
r = manager_sign_user_record(h->manager, c, &signed_c, error);
if (r < 0)
return r;
if (c->enforce_password_policy == false)
log_debug("Password quality check turned off for account, skipping.");
else {
r = user_record_check_password_quality(c, merged_secret, error);
if (r < 0)
return r;
}
r = home_update_internal(h, "passwd", signed_c, merged_secret, NULL, 0, error);
if (r < 0)
return r;
home_set_state(h, HOME_STATE_IS_ACTIVE(state) ? HOME_PASSWD_WHILE_ACTIVE : HOME_PASSWD);
return 0;
}
int home_unregister(Home *h, sd_bus_error *error) {
int r;
assert(h);
switch (home_get_state(h)) {
case HOME_UNFIXATED:
return sd_bus_error_setf(error, BUS_ERROR_HOME_UNFIXATED, "Home %s is not registered.", h->user_name);
case HOME_LOCKED:
return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
case HOME_ABSENT:
case HOME_INACTIVE:
case HOME_DIRTY:
break;
case HOME_ACTIVE:
case HOME_LINGERING:
default:
return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "Home %s is currently being used, or an operation on home %s is currently being executed.", h->user_name, h->user_name);
}
Manager *m = ASSERT_PTR(h->manager);
r = home_unlink_record(h);
if (r < 0)
return r;
/* And destroy the whole entry. The caller needs to be prepared for that. */
h = home_free(h);
/* Let's rescan, who knows, maybe this revealed a directory in /home/ that we should pick up now */
manager_enqueue_rescan(m);
return 1;
}
int home_lock(Home *h, sd_bus_error *error) {
int r;
assert(h);
switch (home_get_state(h)) {
case HOME_UNFIXATED:
case HOME_ABSENT:
case HOME_INACTIVE:
case HOME_DIRTY:
return sd_bus_error_setf(error, BUS_ERROR_HOME_NOT_ACTIVE, "Home %s is not active.", h->user_name);
case HOME_LOCKED:
return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is already locked.", h->user_name);
case HOME_ACTIVE:
case HOME_LINGERING:
break;
default:
return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
}
r = home_start_work(h, "lock", h->record, NULL, NULL, 0);
if (r < 0)
return r;
home_set_state(h, HOME_LOCKING);
return 0;
}
static int home_unlock_internal(Home *h, UserRecord *secret, HomeState for_state, sd_bus_error *error) {
int r;
assert(h);
assert(secret);
assert(IN_SET(for_state, HOME_UNLOCKING, HOME_UNLOCKING_FOR_ACQUIRE));
r = home_start_work(h, "unlock", h->record, secret, NULL, 0);
if (r < 0)
return r;
home_set_state(h, for_state);
return 0;
}
int home_unlock(Home *h, UserRecord *secret, sd_bus_error *error) {
int r;
assert(h);
assert(secret);
r = home_ratelimit(h, error);
if (r < 0)
return r;
switch (home_get_state(h)) {
case HOME_UNFIXATED:
case HOME_ABSENT:
case HOME_INACTIVE:
case HOME_ACTIVE:
case HOME_LINGERING:
case HOME_DIRTY:
return sd_bus_error_setf(error, BUS_ERROR_HOME_NOT_LOCKED, "Home %s is not locked.", h->user_name);
case HOME_LOCKED:
break;
default:
return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
}
return home_unlock_internal(h, secret, HOME_UNLOCKING, error);
}
HomeState home_get_state(Home *h) {
int r;
assert(h);
/* When the state field is initialized, it counts. */
if (h->state >= 0)
return h->state;
/* Otherwise, let's see if the home directory is mounted. If so, we assume for sure the home
* directory is active */
if (user_record_test_home_directory(h->record) == USER_TEST_MOUNTED)
return h->retry_deactivate_event_source ? HOME_LINGERING : HOME_ACTIVE;
/* And if we see the image being gone, we report this as absent */
r = user_record_test_image_path(h->record);
if (r == USER_TEST_ABSENT)
return HOME_ABSENT;
if (r == USER_TEST_DIRTY)
return HOME_DIRTY;
/* And for all other cases we return "inactive". */
return HOME_INACTIVE;
}
void home_process_notify(Home *h, char **l, int fd) {
_cleanup_close_ int taken_fd = TAKE_FD(fd);
const char *e;
int error;
int r;
assert(h);
e = strv_env_get(l, "SYSTEMD_LUKS_LOCK_FD");
if (e) {
r = parse_boolean(e);
if (r < 0)
return (void) log_debug_errno(r, "Failed to parse SYSTEMD_LUKS_LOCK_FD value: %m");
if (r > 0) {
if (taken_fd < 0)
return (void) log_debug("Got notify message with SYSTEMD_LUKS_LOCK_FD=1 but no fd passed, ignoring.");
close_and_replace(h->luks_lock_fd, taken_fd);
log_debug("Successfully acquired LUKS lock fd from worker.");
/* Immediately check if we actually want to keep it */
home_maybe_close_luks_lock_fd(h, _HOME_STATE_INVALID);
} else {
if (taken_fd >= 0)
return (void) log_debug("Got notify message with SYSTEMD_LUKS_LOCK_FD=0 but fd passed, ignoring.");
h->luks_lock_fd = safe_close(h->luks_lock_fd);
}
return;
}
e = strv_env_get(l, "ERRNO");
if (!e)
return (void) log_debug("Got notify message lacking both ERRNO= and SYSTEMD_LUKS_LOCK_FD= field, ignoring.");
r = safe_atoi(e, &error);
if (r < 0)
return (void) log_debug_errno(r, "Failed to parse received error number, ignoring: %s", e);
if (error <= 0)
return (void) log_debug("Error number is out of range: %i", error);
h->worker_error_code = error;
}
int home_killall(Home *h) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_free_ char *unit = NULL;
int r;
assert(h);
if (!uid_is_valid(h->uid))
return 0;
assert(h->uid > 0); /* We never should be UID 0 */
/* Let's kill everything matching the specified UID */
r = pidref_safe_fork(
"(sd-killer)",
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL|FORK_WAIT|FORK_LOG|FORK_REOPEN_LOG,
/* ret= */ NULL);
if (r < 0)
return r;
if (r == 0) {
/* Child */
r = fully_set_uid_gid(h->uid, user_record_gid(h->record), /* supplementary_gids= */ NULL, /* n_supplementary_gids= */ 0);
if (r < 0) {
log_error_errno(r, "Failed to change UID/GID to " UID_FMT "/" GID_FMT ": %m", h->uid, user_record_gid(h->record));
_exit(EXIT_FAILURE);
}
if (kill(-1, SIGKILL) < 0) {
log_error_errno(errno, "Failed to kill all processes of UID " UID_FMT ": %m", h->uid);
_exit(EXIT_FAILURE);
}
_exit(EXIT_SUCCESS);
}
/* Let's also kill everything in the user's slice */
if (asprintf(&unit, "user-" UID_FMT ".slice", h->uid) < 0)
return log_oom();
r = bus_call_method(h->manager->bus, bus_systemd_mgr, "KillUnit", &error, NULL, "ssi", unit, "all", SIGKILL);
if (r < 0)
log_full_errno(sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ? LOG_DEBUG : LOG_WARNING,
r, "Failed to kill login processes of user, ignoring: %s", bus_error_message(&error, r));
return 1;
}
static int home_get_disk_status_luks(
Home *h,
HomeState state,
uint64_t *ret_disk_size,
uint64_t *ret_disk_usage,
uint64_t *ret_disk_free,
uint64_t *ret_disk_ceiling,
uint64_t *ret_disk_floor,
statfs_f_type_t *ret_fstype,
mode_t *ret_access_mode) {
uint64_t disk_size = UINT64_MAX, disk_usage = UINT64_MAX, disk_free = UINT64_MAX,
disk_ceiling = UINT64_MAX, disk_floor = UINT64_MAX,
stat_used = UINT64_MAX, fs_size = UINT64_MAX, header_size = 0;
mode_t access_mode = MODE_INVALID;
statfs_f_type_t fstype = 0;
struct statfs sfs;
struct stat st;
const char *hd;
int r;
assert(h);
if (state != HOME_ABSENT) {
const char *ip;
ip = user_record_image_path(h->record);
if (ip) {
if (stat(ip, &st) < 0)
log_debug_errno(errno, "Failed to stat() %s, ignoring: %m", ip);
else if (S_ISREG(st.st_mode)) {
_cleanup_free_ char *parent = NULL;
disk_size = st.st_size;
stat_used = st.st_blocks * 512;
r = path_extract_directory(ip, &parent);
if (r < 0)
return log_error_errno(r, "Failed to extract parent directory from image path '%s': %m", ip);
if (statfs(parent, &sfs) < 0)
log_debug_errno(errno, "Failed to statfs() %s, ignoring: %m", parent);
else
disk_ceiling = stat_used + sfs.f_bsize * sfs.f_bavail;
} else if (S_ISBLK(st.st_mode)) {
_cleanup_free_ char *szbuf = NULL;
char p[SYS_BLOCK_PATH_MAX("/size")];
/* Let's read the size off sysfs, so that we don't have to open the device */
xsprintf_sys_block_path(p, "/size", st.st_rdev);
r = read_one_line_file(p, &szbuf);
if (r < 0)
log_debug_errno(r, "Failed to read %s, ignoring: %m", p);
else {
uint64_t sz;
r = safe_atou64(szbuf, &sz);
if (r < 0)
log_debug_errno(r, "Failed to parse %s, ignoring: %s", p, szbuf);
else
disk_size = sz * 512;
}
} else
log_debug("Image path is not a block device or regular file, not able to acquire size.");
}
}
if (!HOME_STATE_IS_ACTIVE(state))
goto finish;
hd = user_record_home_directory(h->record);
if (!hd)
goto finish;
if (stat(hd, &st) < 0) {
log_debug_errno(errno, "Failed to stat() %s, ignoring: %m", hd);
goto finish;
}
r = stat_verify_directory(&st);
if (r < 0) {
log_debug_errno(r, "Home directory %s is not a directory, ignoring: %m", hd);
goto finish;
}
access_mode = st.st_mode & 07777;
if (statfs(hd, &sfs) < 0) {
log_debug_errno(errno, "Failed to statfs() %s, ignoring: %m", hd);
goto finish;
}
fstype = sfs.f_type;
disk_free = sfs.f_bsize * sfs.f_bavail;
fs_size = sfs.f_bsize * sfs.f_blocks;
if (disk_size != UINT64_MAX && disk_size > fs_size)
header_size = disk_size - fs_size;
/* We take a perspective from the user here (as opposed to from the host): the used disk space is the
* difference from the limit and what's free. This makes a difference if sparse mode is not used: in
* that case the image is pre-allocated and thus appears all used from the host PoV but is not used
* up at all yet from the user's PoV.
*
* That said, we use the stat() reported loopback file size as upper boundary: our footprint can
* never be larger than what we take up on the lowest layers. */
if (disk_size != UINT64_MAX && disk_size > disk_free) {
disk_usage = disk_size - disk_free;
if (stat_used != UINT64_MAX && disk_usage > stat_used)
disk_usage = stat_used;
} else
disk_usage = stat_used;
/* If we have the magic, determine floor preferably by magic */
disk_floor = minimal_size_by_fs_magic(sfs.f_type) + header_size;
finish:
/* If we don't know the magic, go by file system name */
if (disk_floor == UINT64_MAX)
disk_floor = minimal_size_by_fs_name(user_record_file_system_type(h->record));
if (ret_disk_size)
*ret_disk_size = disk_size;
if (ret_disk_usage)
*ret_disk_usage = disk_usage;
if (ret_disk_free)
*ret_disk_free = disk_free;
if (ret_disk_ceiling)
*ret_disk_ceiling = disk_ceiling;
if (ret_disk_floor)
*ret_disk_floor = disk_floor;
if (ret_fstype)
*ret_fstype = fstype;
if (ret_access_mode)
*ret_access_mode = access_mode;
return 0;
}
static int home_get_disk_status_directory(
Home *h,
HomeState state,
uint64_t *ret_disk_size,
uint64_t *ret_disk_usage,
uint64_t *ret_disk_free,
uint64_t *ret_disk_ceiling,
uint64_t *ret_disk_floor,
statfs_f_type_t *ret_fstype,
mode_t *ret_access_mode) {
uint64_t disk_size = UINT64_MAX, disk_usage = UINT64_MAX, disk_free = UINT64_MAX,
disk_ceiling = UINT64_MAX, disk_floor = UINT64_MAX;
mode_t access_mode = MODE_INVALID;
_cleanup_close_ int fd = -EBADF;
statfs_f_type_t fstype = 0;
struct statfs sfs;
struct dqblk req;
const char *path = NULL;
int r;
assert(h);
if (HOME_STATE_IS_ACTIVE(state))
path = user_record_home_directory(h->record);
if (!path) {
if (state == HOME_ABSENT)
goto finish;
path = user_record_image_path(h->record);
}
if (!path)
goto finish;
fd = open(path, O_CLOEXEC|O_RDONLY);
if (fd < 0) {
log_debug_errno(errno, "Failed to open '%s', ignoring: %m", path);
goto finish;
}
if (fstatfs(fd, &sfs) < 0)
log_debug_errno(errno, "Failed to statfs() %s, ignoring: %m", path);
else {
disk_free = sfs.f_bsize * sfs.f_bavail;
disk_size = sfs.f_bsize * sfs.f_blocks;
/* We don't initialize disk_usage from statfs() data here, since the device is likely not used
* by us alone, and disk_usage should only reflect our own use. */
fstype = sfs.f_type;
}
if (IN_SET(h->record->storage, USER_CLASSIC, USER_DIRECTORY, USER_SUBVOLUME)) {
r = btrfs_is_subvol_fd(fd);
if (r < 0)
log_debug_errno(r, "Failed to determine whether %s is a btrfs subvolume: %m", path);
else if (r > 0) {
BtrfsQuotaInfo qi;
r = btrfs_subvol_get_subtree_quota_fd(fd, /* subvol_id= */ 0, &qi);
if (r < 0)
log_debug_errno(r, "Failed to query btrfs subtree quota, ignoring: %m");
else {
disk_usage = qi.referenced;
if (disk_free != UINT64_MAX) {
disk_ceiling = qi.referenced + disk_free;
if (disk_size != UINT64_MAX && disk_ceiling > disk_size)
disk_ceiling = disk_size;
}
if (qi.referenced_max != UINT64_MAX) {
if (disk_size != UINT64_MAX)
disk_size = MIN(qi.referenced_max, disk_size);
else
disk_size = qi.referenced_max;
}
if (disk_size != UINT64_MAX) {
if (disk_size > disk_usage)
disk_free = disk_size - disk_usage;
else
disk_free = 0;
}
}
goto finish;
}
}
if (IN_SET(h->record->storage, USER_CLASSIC, USER_DIRECTORY, USER_FSCRYPT)) {
r = quotactl_fd_with_fallback(fd, QCMD_FIXED(Q_GETQUOTA, USRQUOTA), h->uid, &req);
if (r < 0) {
if (ERRNO_IS_NOT_SUPPORTED(r)) {
log_debug_errno(r, "No UID quota support on %s.", path);
goto finish;
}
if (r != -ESRCH) {
log_debug_errno(r, "Failed to query disk quota for UID " UID_FMT ": %m", h->uid);
goto finish;
}
disk_usage = 0; /* No record of this user? then nothing was used */
} else {
if (FLAGS_SET(req.dqb_valid, QIF_SPACE) && disk_free != UINT64_MAX) {
disk_ceiling = req.dqb_curspace + disk_free;
if (disk_size != UINT64_MAX && disk_ceiling > disk_size)
disk_ceiling = disk_size;
}
if (FLAGS_SET(req.dqb_valid, QIF_BLIMITS)) {
uint64_t q;
/* Take the minimum of the quota and the available disk space here */
q = req.dqb_bhardlimit * QIF_DQBLKSIZE;
if (disk_size != UINT64_MAX)
disk_size = MIN(disk_size, q);
else
disk_size = q;
}
if (FLAGS_SET(req.dqb_valid, QIF_SPACE)) {
disk_usage = req.dqb_curspace;
if (disk_size != UINT64_MAX) {
if (disk_size > disk_usage)
disk_free = disk_size - disk_usage;
else
disk_free = 0;
}
}
}
}
finish:
if (ret_disk_size)
*ret_disk_size = disk_size;
if (ret_disk_usage)
*ret_disk_usage = disk_usage;
if (ret_disk_free)
*ret_disk_free = disk_free;
if (ret_disk_ceiling)
*ret_disk_ceiling = disk_ceiling;
if (ret_disk_floor)
*ret_disk_floor = disk_floor;
if (ret_fstype)
*ret_fstype = fstype;
if (ret_access_mode)
*ret_access_mode = access_mode;
return 0;
}
static int home_get_disk_status_internal(
Home *h,
HomeState state,
uint64_t *ret_disk_size,
uint64_t *ret_disk_usage,
uint64_t *ret_disk_free,
uint64_t *ret_disk_ceiling,
uint64_t *ret_disk_floor,
statfs_f_type_t *ret_fstype,
mode_t *ret_access_mode) {
assert(h);
assert(h->record);
switch (h->record->storage) {
case USER_LUKS:
return home_get_disk_status_luks(h, state, ret_disk_size, ret_disk_usage, ret_disk_free, ret_disk_ceiling, ret_disk_floor, ret_fstype, ret_access_mode);
case USER_CLASSIC:
case USER_DIRECTORY:
case USER_SUBVOLUME:
case USER_FSCRYPT:
case USER_CIFS:
return home_get_disk_status_directory(h, state, ret_disk_size, ret_disk_usage, ret_disk_free, ret_disk_ceiling, ret_disk_floor, ret_fstype, ret_access_mode);
default:
/* don't know */
if (ret_disk_size)
*ret_disk_size = UINT64_MAX;
if (ret_disk_usage)
*ret_disk_usage = UINT64_MAX;
if (ret_disk_free)
*ret_disk_free = UINT64_MAX;
if (ret_disk_ceiling)
*ret_disk_ceiling = UINT64_MAX;
if (ret_disk_floor)
*ret_disk_floor = UINT64_MAX;
if (ret_fstype)
*ret_fstype = 0;
if (ret_access_mode)
*ret_access_mode = MODE_INVALID;
return 0;
}
}
int home_get_disk_status(
Home *h,
uint64_t *ret_disk_size,
uint64_t *ret_disk_usage,
uint64_t *ret_disk_free,
uint64_t *ret_disk_ceiling,
uint64_t *ret_disk_floor,
statfs_f_type_t *ret_fstype,
mode_t *ret_access_mode) {
assert(h);
return home_get_disk_status_internal(
h,
home_get_state(h),
ret_disk_size,
ret_disk_usage,
ret_disk_free,
ret_disk_ceiling,
ret_disk_floor,
ret_fstype,
ret_access_mode);
}
int home_augment_status(
Home *h,
UserRecordLoadFlags flags,
UserRecord **ret) {
uint64_t disk_size = UINT64_MAX, disk_usage = UINT64_MAX, disk_free = UINT64_MAX, disk_ceiling = UINT64_MAX, disk_floor = UINT64_MAX;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL, *v = NULL, *m = NULL, *status = NULL;
_cleanup_(user_record_unrefp) UserRecord *ur = NULL;
statfs_f_type_t magic;
const char *fstype;
mode_t access_mode;
HomeState state;
sd_id128_t id;
int r;
assert(h);
assert(ret);
/* We are supposed to add this, this can't be on hence. */
assert(!FLAGS_SET(flags, USER_RECORD_STRIP_STATUS));
r = sd_id128_get_machine(&id);
if (r < 0)
return r;
state = home_get_state(h);
r = home_get_disk_status_internal(
h, state,
&disk_size,
&disk_usage,
&disk_free,
&disk_ceiling,
&disk_floor,
&magic,
&access_mode);
if (r < 0)
return r;
fstype = fs_type_to_string(magic);
if (disk_floor == UINT64_MAX || (disk_usage != UINT64_MAX && disk_floor < disk_usage))
disk_floor = disk_usage;
if (disk_floor == UINT64_MAX || disk_floor < USER_DISK_SIZE_MIN)
disk_floor = USER_DISK_SIZE_MIN;
if (disk_ceiling == UINT64_MAX || disk_ceiling > USER_DISK_SIZE_MAX)
disk_ceiling = USER_DISK_SIZE_MAX;
r = sd_json_buildo(&status,
SD_JSON_BUILD_PAIR_STRING("state", home_state_to_string(state)),
SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.Home")),
SD_JSON_BUILD_PAIR_BOOLEAN("useFallback", !HOME_STATE_IS_ACTIVE(state)),
SD_JSON_BUILD_PAIR("fallbackShell", JSON_BUILD_CONST_STRING(BINDIR "/systemd-home-fallback-shell")),
SD_JSON_BUILD_PAIR("fallbackHomeDirectory", JSON_BUILD_CONST_STRING("/")),
SD_JSON_BUILD_PAIR_CONDITION(disk_size != UINT64_MAX, "diskSize", SD_JSON_BUILD_UNSIGNED(disk_size)),
SD_JSON_BUILD_PAIR_CONDITION(disk_usage != UINT64_MAX, "diskUsage", SD_JSON_BUILD_UNSIGNED(disk_usage)),
SD_JSON_BUILD_PAIR_CONDITION(disk_free != UINT64_MAX, "diskFree", SD_JSON_BUILD_UNSIGNED(disk_free)),
SD_JSON_BUILD_PAIR_CONDITION(disk_ceiling != UINT64_MAX, "diskCeiling", SD_JSON_BUILD_UNSIGNED(disk_ceiling)),
SD_JSON_BUILD_PAIR_CONDITION(disk_floor != UINT64_MAX, "diskFloor", SD_JSON_BUILD_UNSIGNED(disk_floor)),
SD_JSON_BUILD_PAIR_CONDITION(h->signed_locally >= 0, "signedLocally", SD_JSON_BUILD_BOOLEAN(h->signed_locally)),
SD_JSON_BUILD_PAIR_CONDITION(!!fstype, "fileSystemType", SD_JSON_BUILD_STRING(fstype)),
SD_JSON_BUILD_PAIR_CONDITION(access_mode != MODE_INVALID, "accessMode", SD_JSON_BUILD_UNSIGNED(access_mode)));
if (r < 0)
return r;
j = sd_json_variant_ref(h->record->json);
v = sd_json_variant_ref(sd_json_variant_by_key(j, "status"));
m = sd_json_variant_ref(sd_json_variant_by_key(v, SD_ID128_TO_STRING(id)));
r = sd_json_variant_filter(&m, STRV_MAKE("diskSize", "diskUsage", "diskFree", "diskCeiling", "diskFloor", "signedLocally"));
if (r < 0)
return r;
r = sd_json_variant_merge_object(&m, status);
if (r < 0)
return r;
r = sd_json_variant_set_field(&v, SD_ID128_TO_STRING(id), m);
if (r < 0)
return r;
r = sd_json_variant_set_field(&j, "status", v);
if (r < 0)
return r;
ur = user_record_new();
if (!ur)
return -ENOMEM;
r = user_record_load(ur, j, flags);
if (r < 0)
return r;
ur->incomplete =
FLAGS_SET(h->record->mask, USER_RECORD_PRIVILEGED) &&
!FLAGS_SET(ur->mask, USER_RECORD_PRIVILEGED);
*ret = TAKE_PTR(ur);
return 0;
}
static int on_home_ref_eof(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_(operation_unrefp) Operation *o = NULL;
Home *h = ASSERT_PTR(userdata);
assert(s);
if (h->ref_event_source_please_suspend == s)
h->ref_event_source_please_suspend = sd_event_source_disable_unref(h->ref_event_source_please_suspend);
if (h->ref_event_source_dont_suspend == s)
h->ref_event_source_dont_suspend = sd_event_source_disable_unref(h->ref_event_source_dont_suspend);
if (home_is_referenced(h))
return 0;
log_info("Got notification that all sessions of user %s ended, deactivating automatically.", h->user_name);
o = operation_new(OPERATION_PIPE_EOF, NULL);
if (!o) {
log_oom();
return 0;
}
home_schedule_operation(h, o, NULL);
return 0;
}
int home_create_fifo(Home *h, bool please_suspend) {
_cleanup_close_ int ret_fd = -EBADF;
sd_event_source **ss;
const char *fn, *suffix;
int r;
assert(h);
if (please_suspend) {
suffix = ".please-suspend";
ss = &h->ref_event_source_please_suspend;
} else {
suffix = ".dont-suspend";
ss = &h->ref_event_source_dont_suspend;
}
fn = strjoina("/run/systemd/home/", h->user_name, suffix);
if (!*ss) {
_cleanup_close_ int ref_fd = -EBADF;
(void) mkdir("/run/systemd/home/", 0755);
if (mkfifo(fn, 0600) < 0 && errno != EEXIST)
return log_error_errno(errno, "Failed to create FIFO %s: %m", fn);
ref_fd = open(fn, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
if (ref_fd < 0)
return log_error_errno(errno, "Failed to open FIFO %s for reading: %m", fn);
r = sd_event_add_io(h->manager->event, ss, ref_fd, 0, on_home_ref_eof, h);
if (r < 0)
return log_error_errno(r, "Failed to allocate reference FIFO event source: %m");
(void) sd_event_source_set_description(*ss, "acquire-ref");
/* We need to notice dropped refs before we process new bus requests (which
* might try to obtain new refs) */
r = sd_event_source_set_priority(*ss, SD_EVENT_PRIORITY_NORMAL-10);
if (r < 0)
return r;
r = sd_event_source_set_io_fd_own(*ss, true);
if (r < 0)
return log_error_errno(r, "Failed to pass ownership of FIFO event fd to event source: %m");
TAKE_FD(ref_fd);
}
ret_fd = open(fn, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
if (ret_fd < 0)
return log_error_errno(errno, "Failed to open FIFO %s for writing: %m", fn);
return TAKE_FD(ret_fd);
}
static int home_dispatch_acquire(Home *h, Operation *o) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int (*call)(Home *h, UserRecord *secret, HomeState for_state, sd_bus_error *error) = NULL;
HomeState for_state;
int r;
assert(h);
assert(o);
assert(o->type == OPERATION_ACQUIRE);
switch (home_get_state(h)) {
case HOME_UNFIXATED:
for_state = HOME_FIXATING_FOR_ACQUIRE;
call = home_fixate_internal;
break;
case HOME_ABSENT:
r = sd_bus_error_setf(&error, BUS_ERROR_HOME_ABSENT,
"Home %s is currently missing or not plugged in.", h->user_name);
operation_result(o, r, &error);
return 1;
case HOME_INACTIVE:
case HOME_DIRTY:
for_state = HOME_ACTIVATING_FOR_ACQUIRE;
call = home_activate_internal;
break;
case HOME_ACTIVE:
case HOME_LINGERING:
for_state = HOME_AUTHENTICATING_FOR_ACQUIRE;
call = home_authenticate_internal;
break;
case HOME_LOCKED:
for_state = HOME_UNLOCKING_FOR_ACQUIRE;
call = home_unlock_internal;
break;
default:
/* All other cases means we are currently executing an operation, which means the job remains
* pending. */
return 0;
}
assert(!h->current_operation);
r = home_ratelimit(h, &error);
if (r >= 0)
r = call(h, o->secret, for_state, &error);
if (r != 0) /* failure or completed */
operation_result(o, r, &error);
else /* ongoing */
h->current_operation = operation_ref(o);
return 1;
}
bool home_is_referenced(Home *h) {
assert(h);
return h->ref_event_source_dont_suspend || h->ref_event_source_please_suspend;
}
bool home_shall_suspend(Home *h) {
assert(h);
/* Suspend if there's at least one client referencing this home directory that wants a suspend and none who does not. */
return h->ref_event_source_please_suspend && !h->ref_event_source_dont_suspend;
}
static int home_dispatch_release(Home *h, Operation *o) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
assert(h);
assert(o);
assert(o->type == OPERATION_RELEASE);
if (home_is_referenced(h)) {
/* If there's now a reference again, then let's abort the release attempt */
r = sd_bus_error_setf(&error, BUS_ERROR_HOME_BUSY, "Home %s is currently referenced.", h->user_name);
operation_result(o, r, &error);
return 1;
}
switch (home_get_state(h)) {
case HOME_UNFIXATED:
case HOME_ABSENT:
case HOME_INACTIVE:
case HOME_DIRTY:
r = 1; /* done */
break;
case HOME_LOCKED:
r = sd_bus_error_setf(&error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
break;
case HOME_ACTIVE:
case HOME_LINGERING:
r = home_deactivate_internal(h, false, &error);
break;
default:
/* All other cases means we are currently executing an operation, which means the job remains
* pending. */
return 0;
}