]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/home/homed-home.h
Merge pull request #31480 from rpigott/dnssec-maxwork
[thirdparty/systemd.git] / src / home / homed-home.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 typedef struct Home Home;
5
6 #include "hashmap.h"
7 #include "homed-manager.h"
8 #include "homed-operation.h"
9 #include "list.h"
10 #include "ordered-set.h"
11 #include "stat-util.h"
12 #include "user-record.h"
13
14 typedef enum HomeState {
15 HOME_UNFIXATED, /* home exists, but local record does not */
16 HOME_ABSENT, /* local record exists, but home does not */
17 HOME_INACTIVE, /* record and home exist, but is not logged in */
18 HOME_DIRTY, /* like HOME_INACTIVE, but the home directory wasn't cleanly deactivated */
19 HOME_FIXATING, /* generating local record from home */
20 HOME_FIXATING_FOR_ACTIVATION, /* fixating in order to activate soon */
21 HOME_FIXATING_FOR_ACQUIRE, /* fixating because Acquire() was called */
22 HOME_ACTIVATING,
23 HOME_ACTIVATING_FOR_ACQUIRE, /* activating because Acquire() was called */
24 HOME_DEACTIVATING,
25 HOME_ACTIVE, /* logged in right now */
26 HOME_LINGERING, /* not logged in anymore, but we didn't manage to deactivate (because some process keeps it busy?) but we'll keep trying */
27 HOME_LOCKING,
28 HOME_LOCKED,
29 HOME_UNLOCKING,
30 HOME_UNLOCKING_FOR_ACQUIRE, /* unlocking because Acquire() was called */
31 HOME_CREATING,
32 HOME_REMOVING,
33 HOME_UPDATING,
34 HOME_UPDATING_WHILE_ACTIVE,
35 HOME_RESIZING,
36 HOME_RESIZING_WHILE_ACTIVE,
37 HOME_PASSWD,
38 HOME_PASSWD_WHILE_ACTIVE,
39 HOME_AUTHENTICATING,
40 HOME_AUTHENTICATING_WHILE_ACTIVE,
41 HOME_AUTHENTICATING_FOR_ACQUIRE, /* authenticating because Acquire() was called */
42 _HOME_STATE_MAX,
43 _HOME_STATE_INVALID = -EINVAL,
44 } HomeState;
45
46 static inline bool HOME_STATE_IS_ACTIVE(HomeState state) {
47 return IN_SET(state,
48 HOME_ACTIVE,
49 HOME_LINGERING,
50 HOME_UPDATING_WHILE_ACTIVE,
51 HOME_RESIZING_WHILE_ACTIVE,
52 HOME_PASSWD_WHILE_ACTIVE,
53 HOME_AUTHENTICATING_WHILE_ACTIVE,
54 HOME_AUTHENTICATING_FOR_ACQUIRE);
55 }
56
57 static inline bool HOME_STATE_IS_EXECUTING_OPERATION(HomeState state) {
58 return IN_SET(state,
59 HOME_FIXATING,
60 HOME_FIXATING_FOR_ACTIVATION,
61 HOME_FIXATING_FOR_ACQUIRE,
62 HOME_ACTIVATING,
63 HOME_ACTIVATING_FOR_ACQUIRE,
64 HOME_DEACTIVATING,
65 HOME_LOCKING,
66 HOME_UNLOCKING,
67 HOME_UNLOCKING_FOR_ACQUIRE,
68 HOME_CREATING,
69 HOME_REMOVING,
70 HOME_UPDATING,
71 HOME_UPDATING_WHILE_ACTIVE,
72 HOME_RESIZING,
73 HOME_RESIZING_WHILE_ACTIVE,
74 HOME_PASSWD,
75 HOME_PASSWD_WHILE_ACTIVE,
76 HOME_AUTHENTICATING,
77 HOME_AUTHENTICATING_WHILE_ACTIVE,
78 HOME_AUTHENTICATING_FOR_ACQUIRE);
79 }
80
81 static inline bool HOME_STATE_SHALL_PIN(HomeState state) {
82 /* Like HOME_STATE_IS_ACTIVE() – but HOME_LINGERING is missing! */
83 return IN_SET(state,
84 HOME_ACTIVE,
85 HOME_UPDATING_WHILE_ACTIVE,
86 HOME_RESIZING_WHILE_ACTIVE,
87 HOME_PASSWD_WHILE_ACTIVE,
88 HOME_AUTHENTICATING_WHILE_ACTIVE,
89 HOME_AUTHENTICATING_FOR_ACQUIRE);
90 }
91
92 #define HOME_STATE_SHALL_REBALANCE(state) HOME_STATE_SHALL_PIN(state)
93
94 static inline bool HOME_STATE_MAY_RETRY_DEACTIVATE(HomeState state) {
95 /* Indicates when to leave the deactivate retry timer active */
96 return IN_SET(state,
97 HOME_ACTIVE,
98 HOME_LINGERING,
99 HOME_DEACTIVATING,
100 HOME_LOCKING,
101 HOME_UNLOCKING,
102 HOME_UNLOCKING_FOR_ACQUIRE,
103 HOME_UPDATING_WHILE_ACTIVE,
104 HOME_RESIZING_WHILE_ACTIVE,
105 HOME_PASSWD_WHILE_ACTIVE,
106 HOME_AUTHENTICATING_WHILE_ACTIVE,
107 HOME_AUTHENTICATING_FOR_ACQUIRE);
108 }
109
110 struct Home {
111 Manager *manager;
112 char *user_name;
113 uid_t uid;
114
115 char *sysfs; /* When found via plugged in device, the sysfs path to it */
116
117 /* Note that the 'state' field is only set to a state while we are doing something (i.e. activating,
118 * deactivating, creating, removing, and such), or when the home is an "unfixated" one. When we are
119 * done with an operation we invalidate the state. This is hint for home_get_state() to check the
120 * state on request as needed from the mount table and similar. */
121 HomeState state;
122 int signed_locally; /* signed only by us */
123
124 UserRecord *record;
125
126 pid_t worker_pid;
127 int worker_stdout_fd;
128 sd_event_source *worker_event_source;
129 int worker_error_code;
130
131 /* The message we are currently processing, and thus need to reply to on completion */
132 Operation *current_operation;
133
134 /* Stores the raw, plaintext passwords, but only for short periods of time */
135 UserRecord *secret;
136
137 /* When we create a home area and that fails, we should possibly unregister the record altogether
138 * again, which is remembered in this boolean. */
139 bool unregister_on_failure;
140
141 /* The reading side of a FIFO stored in /run/systemd/home/, the writing side being used for reference
142 * counting. The references dropped to zero as soon as we see EOF. This concept exists thrice: once
143 * for clients that are fine if we lock the home directory on system suspend, once for clients
144 * that are not ok with that, and once for clients that are usually ok with it but temporarily
145 * want to opt-out so that they can implement more advanced behavior on their own. This allows
146 * us to determine for each home whether there are any clients that don't support suspend at this
147 * moment. */
148 sd_event_source *ref_event_source_please_suspend;
149 sd_event_source *ref_event_source_dont_suspend;
150 /* This is distinct from ref_event_source_dont_suspend because it can be obtained from unprivileged
151 * code, and thus we don't count it as a reference on the home area. */
152 sd_event_source *inhibit_suspend_event_source;
153
154 /* Any pending operations we still need to execute. These are for operations we want to queue if we
155 * can't execute them right-away. */
156 OrderedSet *pending_operations;
157
158 /* A defer event source that processes pending acquire/release/eof events. We have a common
159 * dispatcher that processes all three kinds of events. */
160 sd_event_source *pending_event_source;
161
162 /* Did we send out a D-Bus notification about this entry? */
163 bool announced;
164
165 /* Used to coalesce bus PropertiesChanged events */
166 sd_event_source *deferred_change_event_source;
167
168 /* An fd to the top-level home directory we keep while logged in, to keep the dir busy */
169 int pin_fd;
170
171 /* A time event used to repeatedly try to unmount home dir after use if it didn't work on first try */
172 sd_event_source *retry_deactivate_event_source;
173
174 /* An fd that locks the backing file of LUKS home dirs with a BSD lock. */
175 int luks_lock_fd;
176
177 /* Space metrics during rebalancing */
178 uint64_t rebalance_size, rebalance_usage, rebalance_free, rebalance_min, rebalance_weight, rebalance_goal;
179
180 /* Whether a rebalance operation is pending */
181 bool rebalance_pending;
182 };
183
184 int home_new(Manager *m, UserRecord *hr, const char *sysfs, Home **ret);
185 Home *home_free(Home *h);
186
187 DEFINE_TRIVIAL_CLEANUP_FUNC(Home*, home_free);
188
189 int home_set_record(Home *h, UserRecord *hr);
190 int home_save_record(Home *h);
191 int home_unlink_record(Home *h);
192
193 int home_fixate(Home *h, UserRecord *secret, sd_bus_error *error);
194 int home_activate(Home *h, bool if_referenced, UserRecord *secret, sd_bus_error *error);
195 int home_authenticate(Home *h, UserRecord *secret, sd_bus_error *error);
196 int home_deactivate(Home *h, bool force, sd_bus_error *error);
197 int home_create(Home *h, UserRecord *secret, Hashmap *blobs, uint64_t flags, sd_bus_error *error);
198 int home_remove(Home *h, sd_bus_error *error);
199 int home_update(Home *h, UserRecord *new_record, Hashmap *blobs, uint64_t flags, sd_bus_error *error);
200 int home_resize(Home *h, uint64_t disk_size, UserRecord *secret, bool automatic, sd_bus_error *error);
201 int home_passwd(Home *h, UserRecord *new_secret, UserRecord *old_secret, sd_bus_error *error);
202 int home_unregister(Home *h, sd_bus_error *error);
203 int home_lock(Home *h, sd_bus_error *error);
204 int home_unlock(Home *h, UserRecord *secret, sd_bus_error *error);
205
206 bool home_is_referenced(Home *h);
207 bool home_shall_suspend(Home *h);
208 HomeState home_get_state(Home *h);
209
210 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);
211
212 void home_process_notify(Home *h, char **l, int fd);
213
214 int home_killall(Home *h);
215
216 int home_augment_status(Home *h, UserRecordLoadFlags flags, UserRecord **ret);
217
218 typedef enum {
219 HOME_FIFO_PLEASE_SUSPEND,
220 HOME_FIFO_DONT_SUSPEND,
221 HOME_FIFO_INHIBIT_SUSPEND,
222 _HOME_FIFO_TYPE_MAX,
223 _HOME_FIFO_TYPE_INVALID = -EINVAL,
224 } HomeFifoType;
225 int home_create_fifo(Home *h, HomeFifoType mode);
226
227 int home_schedule_operation(Home *h, Operation *o, sd_bus_error *error);
228
229 int home_auto_login(Home *h, char ***ret_seats);
230
231 int home_set_current_message(Home *h, sd_bus_message *m);
232
233 int home_wait_for_worker(Home *h);
234
235 bool home_shall_rebalance(Home *h);
236
237 bool home_is_busy(Home *h);
238
239 const char *home_state_to_string(HomeState state);
240 HomeState home_state_from_string(const char *s);