]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/home/homed-home.c
homed: move supported_fstype() to home-util.c
[thirdparty/systemd.git] / src / home / homed-home.c
CommitLineData
70a5db58
LP
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#if HAVE_LINUX_MEMFD_H
4#include <linux/memfd.h>
5#endif
6
7#include <sys/mman.h>
8#include <sys/quota.h>
9#include <sys/vfs.h>
10
11#include "blockdev-util.h"
12#include "btrfs-util.h"
13#include "bus-common-errors.h"
14#include "env-util.h"
15#include "errno-list.h"
16#include "errno-util.h"
17#include "fd-util.h"
18#include "fileio.h"
19#include "home-util.h"
20#include "homed-home-bus.h"
21#include "homed-home.h"
22#include "missing_syscall.h"
23#include "mkdir.h"
24#include "path-util.h"
25#include "process-util.h"
26#include "pwquality-util.h"
27#include "quota-util.h"
28#include "resize-fs.h"
29#include "set.h"
30#include "signal-util.h"
31#include "stat-util.h"
32#include "string-table.h"
33#include "strv.h"
34#include "user-record-sign.h"
35#include "user-record-util.h"
36#include "user-record.h"
37#include "user-util.h"
38
39#define HOME_USERS_MAX 500
40#define PENDING_OPERATIONS_MAX 100
41
42assert_cc(HOME_UID_MIN <= HOME_UID_MAX);
43assert_cc(HOME_USERS_MAX <= (HOME_UID_MAX - HOME_UID_MIN + 1));
44
45static int home_start_work(Home *h, const char *verb, UserRecord *hr, UserRecord *secret);
46
47DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(operation_hash_ops, void, trivial_hash_func, trivial_compare_func, Operation, operation_unref);
48
49static int suitable_home_record(UserRecord *hr) {
50 int r;
51
52 assert(hr);
53
54 if (!hr->user_name)
55 return -EUNATCH;
56
57 /* We are a bit more restrictive with what we accept as homed-managed user than what we accept in
58 * home records in general. Let's enforce the stricter rule here. */
59 if (!suitable_user_name(hr->user_name))
60 return -EINVAL;
61 if (!uid_is_valid(hr->uid))
62 return -EINVAL;
63
64 /* Insist we are outside of the dynamic and system range */
65 if (uid_is_system(hr->uid) || gid_is_system(user_record_gid(hr)) ||
66 uid_is_dynamic(hr->uid) || gid_is_dynamic(user_record_gid(hr)))
67 return -EADDRNOTAVAIL;
68
69 /* Insist that GID and UID match */
70 if (user_record_gid(hr) != (gid_t) hr->uid)
71 return -EBADSLT;
72
73 /* Similar for the realm */
74 if (hr->realm) {
75 r = suitable_realm(hr->realm);
76 if (r < 0)
77 return r;
78 if (r == 0)
79 return -EINVAL;
80 }
81
82 return 0;
83}
84
85int home_new(Manager *m, UserRecord *hr, const char *sysfs, Home **ret) {
86 _cleanup_(home_freep) Home *home = NULL;
87 _cleanup_free_ char *nm = NULL, *ns = NULL;
88 int r;
89
90 assert(m);
91 assert(hr);
92
93 r = suitable_home_record(hr);
94 if (r < 0)
95 return r;
96
97 if (hashmap_contains(m->homes_by_name, hr->user_name))
98 return -EBUSY;
99
100 if (hashmap_contains(m->homes_by_uid, UID_TO_PTR(hr->uid)))
101 return -EBUSY;
102
103 if (sysfs && hashmap_contains(m->homes_by_sysfs, sysfs))
104 return -EBUSY;
105
106 if (hashmap_size(m->homes_by_name) >= HOME_USERS_MAX)
107 return -EUSERS;
108
109 nm = strdup(hr->user_name);
110 if (!nm)
111 return -ENOMEM;
112
113 if (sysfs) {
114 ns = strdup(sysfs);
115 if (!ns)
116 return -ENOMEM;
117 }
118
119 home = new(Home, 1);
120 if (!home)
121 return -ENOMEM;
122
123 *home = (Home) {
124 .manager = m,
125 .user_name = TAKE_PTR(nm),
126 .uid = hr->uid,
127 .state = _HOME_STATE_INVALID,
128 .worker_stdout_fd = -1,
129 .sysfs = TAKE_PTR(ns),
130 .signed_locally = -1,
131 };
132
133 r = hashmap_put(m->homes_by_name, home->user_name, home);
134 if (r < 0)
135 return r;
136
137 r = hashmap_put(m->homes_by_uid, UID_TO_PTR(home->uid), home);
138 if (r < 0)
139 return r;
140
141 if (home->sysfs) {
142 r = hashmap_put(m->homes_by_sysfs, home->sysfs, home);
143 if (r < 0)
144 return r;
145 }
146
147 r = user_record_clone(hr, USER_RECORD_LOAD_MASK_SECRET, &home->record);
148 if (r < 0)
149 return r;
150
151 (void) bus_manager_emit_auto_login_changed(m);
152 (void) bus_home_emit_change(home);
153
154 if (ret)
155 *ret = TAKE_PTR(home);
156 else
157 TAKE_PTR(home);
158
159 return 0;
160}
161
162Home *home_free(Home *h) {
163
164 if (!h)
165 return NULL;
166
167 if (h->manager) {
168 (void) bus_home_emit_remove(h);
169 (void) bus_manager_emit_auto_login_changed(h->manager);
170
171 if (h->user_name)
172 (void) hashmap_remove_value(h->manager->homes_by_name, h->user_name, h);
173
174 if (uid_is_valid(h->uid))
175 (void) hashmap_remove_value(h->manager->homes_by_uid, UID_TO_PTR(h->uid), h);
176
177 if (h->sysfs)
178 (void) hashmap_remove_value(h->manager->homes_by_sysfs, h->sysfs, h);
179
180 if (h->worker_pid > 0)
181 (void) hashmap_remove_value(h->manager->homes_by_worker_pid, PID_TO_PTR(h->worker_pid), h);
182
183 if (h->manager->gc_focus == h)
184 h->manager->gc_focus = NULL;
185 }
186
187 user_record_unref(h->record);
188 user_record_unref(h->secret);
189
190 h->worker_event_source = sd_event_source_unref(h->worker_event_source);
191 safe_close(h->worker_stdout_fd);
192 free(h->user_name);
193 free(h->sysfs);
194
195 h->ref_event_source_please_suspend = sd_event_source_unref(h->ref_event_source_please_suspend);
196 h->ref_event_source_dont_suspend = sd_event_source_unref(h->ref_event_source_dont_suspend);
197
198 h->pending_operations = ordered_set_free(h->pending_operations);
199 h->pending_event_source = sd_event_source_unref(h->pending_event_source);
200 h->deferred_change_event_source = sd_event_source_unref(h->deferred_change_event_source);
201
202 h->current_operation = operation_unref(h->current_operation);
203
204 return mfree(h);
205}
206
207int home_set_record(Home *h, UserRecord *hr) {
208 _cleanup_(user_record_unrefp) UserRecord *new_hr = NULL;
209 Home *other;
210 int r;
211
212 assert(h);
213 assert(h->user_name);
214 assert(h->record);
215 assert(hr);
216
217 if (user_record_equal(h->record, hr))
218 return 0;
219
220 r = suitable_home_record(hr);
221 if (r < 0)
222 return r;
223
224 if (!user_record_compatible(h->record, hr))
225 return -EREMCHG;
226
227 if (!FLAGS_SET(hr->mask, USER_RECORD_REGULAR) ||
228 FLAGS_SET(hr->mask, USER_RECORD_SECRET))
229 return -EINVAL;
230
231 if (FLAGS_SET(h->record->mask, USER_RECORD_STATUS)) {
232 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
233
234 /* Hmm, the existing record has status fields? If so, copy them over */
235
236 v = json_variant_ref(hr->json);
237 r = json_variant_set_field(&v, "status", json_variant_by_key(h->record->json, "status"));
238 if (r < 0)
239 return r;
240
241 new_hr = user_record_new();
242 if (!new_hr)
243 return -ENOMEM;
244
245 r = user_record_load(new_hr, v, USER_RECORD_LOAD_REFUSE_SECRET);
246 if (r < 0)
247 return r;
248
249 hr = new_hr;
250 }
251
252 other = hashmap_get(h->manager->homes_by_uid, UID_TO_PTR(hr->uid));
253 if (other && other != h)
254 return -EBUSY;
255
256 if (h->uid != hr->uid) {
257 r = hashmap_remove_and_replace(h->manager->homes_by_uid, UID_TO_PTR(h->uid), UID_TO_PTR(hr->uid), h);
258 if (r < 0)
259 return r;
260 }
261
262 user_record_unref(h->record);
263 h->record = user_record_ref(hr);
264 h->uid = h->record->uid;
265
266 /* The updated record might have a different autologin setting, trigger a PropertiesChanged event for it */
267 (void) bus_manager_emit_auto_login_changed(h->manager);
268 (void) bus_home_emit_change(h);
269
270 return 0;
271}
272
273int home_save_record(Home *h) {
274 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
275 _cleanup_free_ char *text = NULL;
276 const char *fn;
277 int r;
278
279 assert(h);
280
281 v = json_variant_ref(h->record->json);
282 r = json_variant_normalize(&v);
283 if (r < 0)
284 log_warning_errno(r, "User record could not be normalized.");
285
286 r = json_variant_format(v, JSON_FORMAT_PRETTY|JSON_FORMAT_NEWLINE, &text);
287 if (r < 0)
288 return r;
289
290 (void) mkdir("/var/lib/systemd/", 0755);
291 (void) mkdir("/var/lib/systemd/home/", 0700);
292
293 fn = strjoina("/var/lib/systemd/home/", h->user_name, ".identity");
294
295 r = write_string_file(fn, text, WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MODE_0600);
296 if (r < 0)
297 return r;
298
299 return 0;
300}
301
302int home_unlink_record(Home *h) {
303 const char *fn;
304
305 assert(h);
306
307 fn = strjoina("/var/lib/systemd/home/", h->user_name, ".identity");
308 if (unlink(fn) < 0 && errno != ENOENT)
309 return -errno;
310
311 fn = strjoina("/run/systemd/home/", h->user_name, ".ref");
312 if (unlink(fn) < 0 && errno != ENOENT)
313 return -errno;
314
315 return 0;
316}
317
318static void home_set_state(Home *h, HomeState state) {
319 HomeState old_state, new_state;
320
321 assert(h);
322
323 old_state = home_get_state(h);
324 h->state = state;
325 new_state = home_get_state(h); /* Query the new state, since the 'state' variable might be set to -1,
326 * in which case we synthesize an high-level state on demand */
327
328 log_info("%s: changing state %s → %s", h->user_name,
329 home_state_to_string(old_state),
330 home_state_to_string(new_state));
331
332 if (HOME_STATE_IS_EXECUTING_OPERATION(old_state) && !HOME_STATE_IS_EXECUTING_OPERATION(new_state)) {
333 /* If we just finished executing some operation, process the queue of pending operations. And
334 * enqueue it for GC too. */
335
336 home_schedule_operation(h, NULL, NULL);
337 manager_enqueue_gc(h->manager, h);
338 }
339}
340
341static int home_parse_worker_stdout(int _fd, UserRecord **ret) {
342 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
343 _cleanup_close_ int fd = _fd; /* take possession, even on failure */
344 _cleanup_(user_record_unrefp) UserRecord *hr = NULL;
345 _cleanup_fclose_ FILE *f = NULL;
346 unsigned line, column;
347 struct stat st;
348 int r;
349
350 if (fstat(fd, &st) < 0)
351 return log_error_errno(errno, "Failed to stat stdout fd: %m");
352
353 assert(S_ISREG(st.st_mode));
354
355 if (st.st_size == 0) { /* empty record */
356 *ret = NULL;
357 return 0;
358 }
359
360 if (lseek(fd, SEEK_SET, 0) == (off_t) -1)
361 return log_error_errno(errno, "Failed to seek to beginning of memfd: %m");
362
4fa744a3 363 f = take_fdopen(&fd, "r");
70a5db58
LP
364 if (!f)
365 return log_error_errno(errno, "Failed to reopen memfd: %m");
366
70a5db58
LP
367 if (DEBUG_LOGGING) {
368 _cleanup_free_ char *text = NULL;
369
370 r = read_full_stream(f, &text, NULL);
371 if (r < 0)
372 return log_error_errno(r, "Failed to read from client: %m");
373
374 log_debug("Got from worker: %s", text);
375 rewind(f);
376 }
377
378 r = json_parse_file(f, "stdout", JSON_PARSE_SENSITIVE, &v, &line, &column);
379 if (r < 0)
380 return log_error_errno(r, "Failed to parse identity at %u:%u: %m", line, column);
381
382 hr = user_record_new();
383 if (!hr)
384 return log_oom();
385
386 r = user_record_load(hr, v, USER_RECORD_LOAD_REFUSE_SECRET);
387 if (r < 0)
388 return log_error_errno(r, "Failed to load home record identity: %m");
389
390 *ret = TAKE_PTR(hr);
391 return 1;
392}
393
394static int home_verify_user_record(Home *h, UserRecord *hr, bool *ret_signed_locally, sd_bus_error *ret_error) {
395 int is_signed;
396
397 assert(h);
398 assert(hr);
399 assert(ret_signed_locally);
400
401 is_signed = manager_verify_user_record(h->manager, hr);
402 switch (is_signed) {
403
404 case USER_RECORD_SIGNED_EXCLUSIVE:
405 log_info("Home %s is signed exclusively by our key, accepting.", hr->user_name);
406 *ret_signed_locally = true;
407 return 0;
408
409 case USER_RECORD_SIGNED:
410 log_info("Home %s is signed by our key (and others), accepting.", hr->user_name);
411 *ret_signed_locally = false;
412 return 0;
413
414 case USER_RECORD_FOREIGN:
415 log_info("Home %s is signed by foreign key we like, accepting.", hr->user_name);
416 *ret_signed_locally = false;
417 return 0;
418
419 case USER_RECORD_UNSIGNED:
420 sd_bus_error_setf(ret_error, BUS_ERROR_BAD_SIGNATURE, "User record %s is not signed at all, refusing.", hr->user_name);
421 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Home %s contains user record that is not signed at all, refusing.", hr->user_name);
422
423 case -ENOKEY:
424 sd_bus_error_setf(ret_error, BUS_ERROR_BAD_SIGNATURE, "User record %s is not signed by any known key, refusing.", hr->user_name);
80ace4f2 425 return log_error_errno(is_signed, "Home %s contains user record that is not signed by any known key, refusing.", hr->user_name);
70a5db58
LP
426
427 default:
428 assert(is_signed < 0);
429 return log_error_errno(is_signed, "Failed to verify signature on user record for %s, refusing fixation: %m", hr->user_name);
430 }
431}
432
433static int convert_worker_errno(Home *h, int e, sd_bus_error *error) {
434 /* Converts the error numbers the worker process returned into somewhat sensible dbus errors */
435
436 switch (e) {
437
438 case -EMSGSIZE:
162392b7 439 return sd_bus_error_setf(error, BUS_ERROR_BAD_HOME_SIZE, "File systems of this type cannot be shrunk");
70a5db58 440 case -ETXTBSY:
162392b7 441 return sd_bus_error_setf(error, BUS_ERROR_BAD_HOME_SIZE, "File systems of this type can only be shrunk offline");
70a5db58
LP
442 case -ERANGE:
443 return sd_bus_error_setf(error, BUS_ERROR_BAD_HOME_SIZE, "File system size too small");
444 case -ENOLINK:
445 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "System does not support selected storage backend");
446 case -EPROTONOSUPPORT:
447 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "System does not support selected file system");
448 case -ENOTTY:
449 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Operation not supported on storage backend");
450 case -ESOCKTNOSUPPORT:
451 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Operation not supported on file system");
452 case -ENOKEY:
453 return sd_bus_error_setf(error, BUS_ERROR_BAD_PASSWORD, "Password for home %s is incorrect or not sufficient for authentication.", h->user_name);
454 case -EBADSLT:
455 return sd_bus_error_setf(error, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN, "Password for home %s is incorrect or not sufficient, and configured security token not found either.", h->user_name);
456 case -ENOANO:
457 return sd_bus_error_setf(error, BUS_ERROR_TOKEN_PIN_NEEDED, "PIN for security token required.");
458 case -ERFKILL:
459 return sd_bus_error_setf(error, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED, "Security token requires protected authentication path.");
460 case -EOWNERDEAD:
461 return sd_bus_error_setf(error, BUS_ERROR_TOKEN_PIN_LOCKED, "PIN of security token locked.");
462 case -ENOLCK:
463 return sd_bus_error_setf(error, BUS_ERROR_TOKEN_BAD_PIN, "Bad PIN of security token.");
464 case -ETOOMANYREFS:
465 return sd_bus_error_setf(error, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT, "Bad PIN of security token, and only a few tries left.");
466 case -EUCLEAN:
467 return sd_bus_error_setf(error, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT, "Bad PIN of security token, and only one try left.");
468 case -EBUSY:
469 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);
470 case -ENOEXEC:
471 return sd_bus_error_setf(error, BUS_ERROR_HOME_NOT_ACTIVE, "Home %s is currently not active", h->user_name);
472 case -ENOSPC:
473 return sd_bus_error_setf(error, BUS_ERROR_NO_DISK_SPACE, "Not enough disk space for home %s", h->user_name);
474 }
475
476 return 0;
477}
478
479static void home_count_bad_authentication(Home *h, bool save) {
480 int r;
481
482 assert(h);
483
484 r = user_record_bad_authentication(h->record);
485 if (r < 0) {
486 log_warning_errno(r, "Failed to increase bad authentication counter, ignoring: %m");
487 return;
488 }
489
490 if (save) {
491 r = home_save_record(h);
492 if (r < 0)
493 log_warning_errno(r, "Failed to write home record to disk, ignoring: %m");
494 }
495}
496
497static void home_fixate_finish(Home *h, int ret, UserRecord *hr) {
498 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
499 _cleanup_(user_record_unrefp) UserRecord *secret = NULL;
500 bool signed_locally;
501 int r;
502
503 assert(h);
504 assert(IN_SET(h->state, HOME_FIXATING, HOME_FIXATING_FOR_ACTIVATION, HOME_FIXATING_FOR_ACQUIRE));
505
506 secret = TAKE_PTR(h->secret); /* Take possession */
507
508 if (ret < 0) {
509 if (ret == -ENOKEY)
510 (void) home_count_bad_authentication(h, false);
511
512 (void) convert_worker_errno(h, ret, &error);
513 r = log_error_errno(ret, "Fixation failed: %m");
514 goto fail;
515 }
516 if (!hr) {
517 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Did not receive user record from worker process, fixation failed.");
518 goto fail;
519 }
520
521 r = home_verify_user_record(h, hr, &signed_locally, &error);
522 if (r < 0)
523 goto fail;
524
525 r = home_set_record(h, hr);
526 if (r < 0) {
527 log_error_errno(r, "Failed to update home record: %m");
528 goto fail;
529 }
530
531 h->signed_locally = signed_locally;
532
533 /* When we finished fixating (and don't follow-up with activation), let's count this as good authentication */
534 if (h->state == HOME_FIXATING) {
535 r = user_record_good_authentication(h->record);
536 if (r < 0)
537 log_warning_errno(r, "Failed to increase good authentication counter, ignoring: %m");
538 }
539
540 r = home_save_record(h);
541 if (r < 0)
542 log_warning_errno(r, "Failed to write home record to disk, ignoring: %m");
543
544 if (IN_SET(h->state, HOME_FIXATING_FOR_ACTIVATION, HOME_FIXATING_FOR_ACQUIRE)) {
545
546 r = home_start_work(h, "activate", h->record, secret);
547 if (r < 0) {
548 h->current_operation = operation_result_unref(h->current_operation, r, NULL);
549 home_set_state(h, _HOME_STATE_INVALID);
550 } else
551 home_set_state(h, h->state == HOME_FIXATING_FOR_ACTIVATION ? HOME_ACTIVATING : HOME_ACTIVATING_FOR_ACQUIRE);
552
553 return;
554 }
555
556 log_debug("Fixation of %s completed.", h->user_name);
557
558 h->current_operation = operation_result_unref(h->current_operation, 0, NULL);
559
560 /* Reset the state to "invalid", which makes home_get_state() test if the image exists and returns
561 * HOME_ABSENT vs. HOME_INACTIVE as necessary. */
562 home_set_state(h, _HOME_STATE_INVALID);
563 return;
564
565fail:
566 /* If fixation fails, we stay in unfixated state! */
567 h->current_operation = operation_result_unref(h->current_operation, r, &error);
568 home_set_state(h, HOME_UNFIXATED);
569}
570
571static void home_activate_finish(Home *h, int ret, UserRecord *hr) {
572 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
573 int r;
574
575 assert(h);
576 assert(IN_SET(h->state, HOME_ACTIVATING, HOME_ACTIVATING_FOR_ACQUIRE));
577
578 if (ret < 0) {
579 if (ret == -ENOKEY)
580 home_count_bad_authentication(h, true);
581
582 (void) convert_worker_errno(h, ret, &error);
583 r = log_error_errno(ret, "Activation failed: %m");
584 goto finish;
585 }
586
587 if (hr) {
588 bool signed_locally;
589
590 r = home_verify_user_record(h, hr, &signed_locally, &error);
591 if (r < 0)
592 goto finish;
593
594 r = home_set_record(h, hr);
595 if (r < 0) {
596 log_error_errno(r, "Failed to update home record, ignoring: %m");
597 goto finish;
598 }
599
600 h->signed_locally = signed_locally;
601
602 r = user_record_good_authentication(h->record);
603 if (r < 0)
604 log_warning_errno(r, "Failed to increase good authentication counter, ignoring: %m");
605
606 r = home_save_record(h);
607 if (r < 0)
608 log_warning_errno(r, "Failed to write home record to disk, ignoring: %m");
609 }
610
611 log_debug("Activation of %s completed.", h->user_name);
612 r = 0;
613
614finish:
615 h->current_operation = operation_result_unref(h->current_operation, r, &error);
616 home_set_state(h, _HOME_STATE_INVALID);
617}
618
619static void home_deactivate_finish(Home *h, int ret, UserRecord *hr) {
620 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
621 int r;
622
623 assert(h);
624 assert(h->state == HOME_DEACTIVATING);
625 assert(!hr); /* We don't expect a record on this operation */
626
627 if (ret < 0) {
628 (void) convert_worker_errno(h, ret, &error);
629 r = log_error_errno(ret, "Deactivation of %s failed: %m", h->user_name);
630 goto finish;
631 }
632
633 log_debug("Deactivation of %s completed.", h->user_name);
634 r = 0;
635
636finish:
637 h->current_operation = operation_result_unref(h->current_operation, r, &error);
638 home_set_state(h, _HOME_STATE_INVALID);
639}
640
641static void home_remove_finish(Home *h, int ret, UserRecord *hr) {
642 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
643 Manager *m;
644 int r;
645
646 assert(h);
647 assert(h->state == HOME_REMOVING);
648 assert(!hr); /* We don't expect a record on this operation */
649
650 m = h->manager;
651
652 if (ret < 0 && ret != -EALREADY) {
653 (void) convert_worker_errno(h, ret, &error);
654 r = log_error_errno(ret, "Removing %s failed: %m", h->user_name);
655 goto fail;
656 }
657
658 /* For a couple of storage types we can't delete the actual data storage when called (such as LUKS on
659 * partitions like USB sticks, or so). Sometimes these storage locations are among those we normally
660 * automatically discover in /home or in udev. When such a home is deleted let's hence issue a rescan
661 * after completion, so that "unfixated" entries are rediscovered. */
662 if (!IN_SET(user_record_test_image_path(h->record), USER_TEST_UNDEFINED, USER_TEST_ABSENT))
663 manager_enqueue_rescan(m);
664
665 /* The image is now removed from disk. Now also remove our stored record */
666 r = home_unlink_record(h);
667 if (r < 0) {
668 log_error_errno(r, "Removing record file failed: %m");
669 goto fail;
670 }
671
672 log_debug("Removal of %s completed.", h->user_name);
673 h->current_operation = operation_result_unref(h->current_operation, 0, NULL);
674
675 /* Unload this record from memory too now. */
676 h = home_free(h);
677 return;
678
679fail:
680 h->current_operation = operation_result_unref(h->current_operation, r, &error);
681 home_set_state(h, _HOME_STATE_INVALID);
682}
683
684static void home_create_finish(Home *h, int ret, UserRecord *hr) {
685 int r;
686
687 assert(h);
688 assert(h->state == HOME_CREATING);
689
690 if (ret < 0) {
691 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
692
693 (void) convert_worker_errno(h, ret, &error);
694 log_error_errno(ret, "Operation on %s failed: %m", h->user_name);
695 h->current_operation = operation_result_unref(h->current_operation, ret, &error);
696
697 if (h->unregister_on_failure) {
698 (void) home_unlink_record(h);
699 h = home_free(h);
700 return;
701 }
702
703 home_set_state(h, _HOME_STATE_INVALID);
704 return;
705 }
706
707 if (hr) {
708 r = home_set_record(h, hr);
709 if (r < 0)
710 log_warning_errno(r, "Failed to update home record, ignoring: %m");
711 }
712
713 r = home_save_record(h);
714 if (r < 0)
715 log_warning_errno(r, "Failed to save record to disk, ignoring: %m");
716
717 log_debug("Creation of %s completed.", h->user_name);
718
719 h->current_operation = operation_result_unref(h->current_operation, 0, NULL);
720 home_set_state(h, _HOME_STATE_INVALID);
721}
722
723static void home_change_finish(Home *h, int ret, UserRecord *hr) {
724 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
725 int r;
726
727 assert(h);
728
729 if (ret < 0) {
730 if (ret == -ENOKEY)
731 (void) home_count_bad_authentication(h, true);
732
733 (void) convert_worker_errno(h, ret, &error);
734 r = log_error_errno(ret, "Change operation failed: %m");
735 goto finish;
736 }
737
738 if (hr) {
739 r = home_set_record(h, hr);
740 if (r < 0)
741 log_warning_errno(r, "Failed to update home record, ignoring: %m");
742 else {
743 r = user_record_good_authentication(h->record);
744 if (r < 0)
745 log_warning_errno(r, "Failed to increase good authentication counter, ignoring: %m");
746
747 r = home_save_record(h);
748 if (r < 0)
749 log_warning_errno(r, "Failed to write home record to disk, ignoring: %m");
750 }
751 }
752
753 log_debug("Change operation of %s completed.", h->user_name);
754 r = 0;
755
756finish:
757 h->current_operation = operation_result_unref(h->current_operation, r, &error);
758 home_set_state(h, _HOME_STATE_INVALID);
759}
760
761static void home_locking_finish(Home *h, int ret, UserRecord *hr) {
762 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
763 int r;
764
765 assert(h);
766 assert(h->state == HOME_LOCKING);
767
768 if (ret < 0) {
769 (void) convert_worker_errno(h, ret, &error);
770 r = log_error_errno(ret, "Locking operation failed: %m");
771 goto finish;
772 }
773
774 log_debug("Locking operation of %s completed.", h->user_name);
775 h->current_operation = operation_result_unref(h->current_operation, 0, NULL);
776 home_set_state(h, HOME_LOCKED);
777 return;
778
779finish:
780 /* If a specific home doesn't know the concept of locking, then that's totally OK, don't propagate
781 * the error if we are executing a LockAllHomes() operation. */
782
783 if (h->current_operation->type == OPERATION_LOCK_ALL && r == -ENOTTY)
784 h->current_operation = operation_result_unref(h->current_operation, 0, NULL);
785 else
786 h->current_operation = operation_result_unref(h->current_operation, r, &error);
787
788 home_set_state(h, _HOME_STATE_INVALID);
789}
790
791static void home_unlocking_finish(Home *h, int ret, UserRecord *hr) {
792 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
793 int r;
794
795 assert(h);
796 assert(IN_SET(h->state, HOME_UNLOCKING, HOME_UNLOCKING_FOR_ACQUIRE));
797
798 if (ret < 0) {
799 if (ret == -ENOKEY)
800 (void) home_count_bad_authentication(h, true);
801
802 (void) convert_worker_errno(h, ret, &error);
803 r = log_error_errno(ret, "Unlocking operation failed: %m");
804
805 /* Revert to locked state */
806 home_set_state(h, HOME_LOCKED);
807 h->current_operation = operation_result_unref(h->current_operation, r, &error);
808 return;
809 }
810
811 r = user_record_good_authentication(h->record);
812 if (r < 0)
813 log_warning_errno(r, "Failed to increase good authentication counter, ignoring: %m");
814 else {
815 r = home_save_record(h);
816 if (r < 0)
817 log_warning_errno(r, "Failed to write home record to disk, ignoring: %m");
818 }
819
820 log_debug("Unlocking operation of %s completed.", h->user_name);
821
822 h->current_operation = operation_result_unref(h->current_operation, r, &error);
823 home_set_state(h, _HOME_STATE_INVALID);
824 return;
825}
826
827static void home_authenticating_finish(Home *h, int ret, UserRecord *hr) {
828 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
829 int r;
830
831 assert(h);
832 assert(IN_SET(h->state, HOME_AUTHENTICATING, HOME_AUTHENTICATING_WHILE_ACTIVE, HOME_AUTHENTICATING_FOR_ACQUIRE));
833
834 if (ret < 0) {
835 if (ret == -ENOKEY)
836 (void) home_count_bad_authentication(h, true);
837
838 (void) convert_worker_errno(h, ret, &error);
839 r = log_error_errno(ret, "Authentication failed: %m");
840 goto finish;
841 }
842
843 if (hr) {
844 r = home_set_record(h, hr);
845 if (r < 0)
846 log_warning_errno(r, "Failed to update home record, ignoring: %m");
847 else {
848 r = user_record_good_authentication(h->record);
849 if (r < 0)
850 log_warning_errno(r, "Failed to increase good authentication counter, ignoring: %m");
851
852 r = home_save_record(h);
853 if (r < 0)
854 log_warning_errno(r, "Failed to write home record to disk, ignoring: %m");
855 }
856 }
857
858 log_debug("Authentication of %s completed.", h->user_name);
859 r = 0;
860
861finish:
862 h->current_operation = operation_result_unref(h->current_operation, r, &error);
863 home_set_state(h, _HOME_STATE_INVALID);
864}
865
866static int home_on_worker_process(sd_event_source *s, const siginfo_t *si, void *userdata) {
867 _cleanup_(user_record_unrefp) UserRecord *hr = NULL;
868 Home *h = userdata;
869 int ret;
870
871 assert(s);
872 assert(si);
873 assert(h);
874
875 assert(h->worker_pid == si->si_pid);
876 assert(h->worker_event_source);
877 assert(h->worker_stdout_fd >= 0);
878
879 (void) hashmap_remove_value(h->manager->homes_by_worker_pid, PID_TO_PTR(h->worker_pid), h);
880
881 h->worker_pid = 0;
882 h->worker_event_source = sd_event_source_unref(h->worker_event_source);
883
884 if (si->si_code != CLD_EXITED) {
885 assert(IN_SET(si->si_code, CLD_KILLED, CLD_DUMPED));
886 ret = log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Worker process died abnormally with signal %s.", signal_to_string(si->si_status));
887 } else if (si->si_status != EXIT_SUCCESS) {
888 /* If we received an error code via sd_notify(), use it */
889 if (h->worker_error_code != 0)
890 ret = log_debug_errno(h->worker_error_code, "Worker reported error code %s.", errno_to_name(h->worker_error_code));
891 else
892 ret = log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Worker exited with exit code %i.", si->si_status);
893 } else
894 ret = home_parse_worker_stdout(TAKE_FD(h->worker_stdout_fd), &hr);
895
896 h->worker_stdout_fd = safe_close(h->worker_stdout_fd);
897
898 switch (h->state) {
899
900 case HOME_FIXATING:
901 case HOME_FIXATING_FOR_ACTIVATION:
902 case HOME_FIXATING_FOR_ACQUIRE:
903 home_fixate_finish(h, ret, hr);
904 break;
905
906 case HOME_ACTIVATING:
907 case HOME_ACTIVATING_FOR_ACQUIRE:
908 home_activate_finish(h, ret, hr);
909 break;
910
911 case HOME_DEACTIVATING:
912 home_deactivate_finish(h, ret, hr);
913 break;
914
915 case HOME_LOCKING:
916 home_locking_finish(h, ret, hr);
917 break;
918
919 case HOME_UNLOCKING:
920 case HOME_UNLOCKING_FOR_ACQUIRE:
921 home_unlocking_finish(h, ret, hr);
922 break;
923
924 case HOME_CREATING:
925 home_create_finish(h, ret, hr);
926 break;
927
928 case HOME_REMOVING:
929 home_remove_finish(h, ret, hr);
930 break;
931
932 case HOME_UPDATING:
933 case HOME_UPDATING_WHILE_ACTIVE:
934 case HOME_RESIZING:
935 case HOME_RESIZING_WHILE_ACTIVE:
936 case HOME_PASSWD:
937 case HOME_PASSWD_WHILE_ACTIVE:
938 home_change_finish(h, ret, hr);
939 break;
940
941 case HOME_AUTHENTICATING:
942 case HOME_AUTHENTICATING_WHILE_ACTIVE:
943 case HOME_AUTHENTICATING_FOR_ACQUIRE:
944 home_authenticating_finish(h, ret, hr);
945 break;
946
947 default:
948 assert_not_reached("Unexpected state after worker exited");
949 }
950
951 return 0;
952}
953
954static int home_start_work(Home *h, const char *verb, UserRecord *hr, UserRecord *secret) {
955 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
956 _cleanup_(erase_and_freep) char *formatted = NULL;
957 _cleanup_close_ int stdin_fd = -1, stdout_fd = -1;
958 pid_t pid = 0;
959 int r;
960
961 assert(h);
962 assert(verb);
963 assert(hr);
964
965 if (h->worker_pid != 0)
966 return -EBUSY;
967
968 assert(h->worker_stdout_fd < 0);
969 assert(!h->worker_event_source);
970
971 v = json_variant_ref(hr->json);
972
973 if (secret) {
974 JsonVariant *sub = NULL;
975
976 sub = json_variant_by_key(secret->json, "secret");
977 if (!sub)
978 return -ENOKEY;
979
980 r = json_variant_set_field(&v, "secret", sub);
981 if (r < 0)
982 return r;
983 }
984
985 r = json_variant_format(v, 0, &formatted);
986 if (r < 0)
987 return r;
988
989 stdin_fd = acquire_data_fd(formatted, strlen(formatted), 0);
990 if (stdin_fd < 0)
991 return stdin_fd;
992
993 log_debug("Sending to worker: %s", formatted);
994
995 stdout_fd = memfd_create("homework-stdout", MFD_CLOEXEC);
996 if (stdout_fd < 0)
997 return -errno;
998
999 r = safe_fork_full("(sd-homework)",
1000 (int[]) { stdin_fd, stdout_fd }, 2,
1001 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_LOG, &pid);
1002 if (r < 0)
1003 return r;
1004 if (r == 0) {
c06bcd4d
LP
1005 const char *homework;
1006
70a5db58
LP
1007 /* Child */
1008
1009 if (setenv("NOTIFY_SOCKET", "/run/systemd/home/notify", 1) < 0) {
1010 log_error_errno(errno, "Failed to set $NOTIFY_SOCKET: %m");
1011 _exit(EXIT_FAILURE);
1012 }
1013
1014 r = rearrange_stdio(stdin_fd, stdout_fd, STDERR_FILENO);
1015 if (r < 0) {
1016 log_error_errno(r, "Failed to rearrange stdin/stdout/stderr: %m");
1017 _exit(EXIT_FAILURE);
1018 }
1019
1020 stdin_fd = stdout_fd = -1; /* have been invalidated by rearrange_stdio() */
1021
c06bcd4d
LP
1022 /* Allow overriding the homework path via an environment variable, to make debugging
1023 * easier. */
1024 homework = getenv("SYSTEMD_HOMEWORK_PATH") ?: SYSTEMD_HOMEWORK_PATH;
1025
1026 execl(homework, homework, verb, NULL);
70a5db58
LP
1027 log_error_errno(errno, "Failed to invoke " SYSTEMD_HOMEWORK_PATH ": %m");
1028 _exit(EXIT_FAILURE);
1029 }
1030
1031 r = sd_event_add_child(h->manager->event, &h->worker_event_source, pid, WEXITED, home_on_worker_process, h);
1032 if (r < 0)
1033 return r;
1034
1035 (void) sd_event_source_set_description(h->worker_event_source, "worker");
1036
1037 r = hashmap_put(h->manager->homes_by_worker_pid, PID_TO_PTR(pid), h);
1038 if (r < 0) {
1039 h->worker_event_source = sd_event_source_unref(h->worker_event_source);
1040 return r;
1041 }
1042
1043 h->worker_stdout_fd = TAKE_FD(stdout_fd);
1044 h->worker_pid = pid;
1045 h->worker_error_code = 0;
1046
1047 return 0;
1048}
1049
1050static int home_ratelimit(Home *h, sd_bus_error *error) {
1051 int r, ret;
1052
1053 assert(h);
1054
1055 ret = user_record_ratelimit(h->record);
1056 if (ret < 0)
1057 return ret;
1058
1059 if (h->state != HOME_UNFIXATED) {
1060 r = home_save_record(h);
1061 if (r < 0)
1062 log_warning_errno(r, "Failed to save updated record, ignoring: %m");
1063 }
1064
1065 if (ret == 0) {
1066 char buf[FORMAT_TIMESPAN_MAX];
1067 usec_t t, n;
1068
1069 n = now(CLOCK_REALTIME);
1070 t = user_record_ratelimit_next_try(h->record);
1071
1072 if (t != USEC_INFINITY && t > n)
1073 return sd_bus_error_setf(error, BUS_ERROR_AUTHENTICATION_LIMIT_HIT, "Too many login attempts, please try again in %s!",
1074 format_timespan(buf, sizeof(buf), t - n, USEC_PER_SEC));
1075
1076 return sd_bus_error_setf(error, BUS_ERROR_AUTHENTICATION_LIMIT_HIT, "Too many login attempts, please try again later.");
1077 }
1078
1079 return 0;
1080}
1081
1082static int home_fixate_internal(
1083 Home *h,
1084 UserRecord *secret,
1085 HomeState for_state,
1086 sd_bus_error *error) {
1087
1088 int r;
1089
1090 assert(h);
1091 assert(IN_SET(for_state, HOME_FIXATING, HOME_FIXATING_FOR_ACTIVATION, HOME_FIXATING_FOR_ACQUIRE));
1092
1093 r = home_start_work(h, "inspect", h->record, secret);
1094 if (r < 0)
1095 return r;
1096
1097 if (for_state == HOME_FIXATING_FOR_ACTIVATION) {
1098 /* Remember the secret data, since we need it for the activation again, later on. */
1099 user_record_unref(h->secret);
1100 h->secret = user_record_ref(secret);
1101 }
1102
1103 home_set_state(h, for_state);
1104 return 0;
1105}
1106
1107int home_fixate(Home *h, UserRecord *secret, sd_bus_error *error) {
1108 int r;
1109
1110 assert(h);
1111
1112 switch (home_get_state(h)) {
1113 case HOME_ABSENT:
1114 return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
1115 case HOME_INACTIVE:
1116 case HOME_ACTIVE:
1117 case HOME_LOCKED:
1118 return sd_bus_error_setf(error, BUS_ERROR_HOME_ALREADY_FIXATED, "Home %s is already fixated.", h->user_name);
1119 case HOME_UNFIXATED:
1120 break;
1121 default:
1122 return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
1123 }
1124
1125 r = home_ratelimit(h, error);
1126 if (r < 0)
1127 return r;
1128
1129 return home_fixate_internal(h, secret, HOME_FIXATING, error);
1130}
1131
1132static int home_activate_internal(Home *h, UserRecord *secret, HomeState for_state, sd_bus_error *error) {
1133 int r;
1134
1135 assert(h);
1136 assert(IN_SET(for_state, HOME_ACTIVATING, HOME_ACTIVATING_FOR_ACQUIRE));
1137
1138 r = home_start_work(h, "activate", h->record, secret);
1139 if (r < 0)
1140 return r;
1141
1142 home_set_state(h, for_state);
1143 return 0;
1144}
1145
1146int home_activate(Home *h, UserRecord *secret, sd_bus_error *error) {
1147 int r;
1148
1149 assert(h);
1150
1151 switch (home_get_state(h)) {
1152 case HOME_UNFIXATED:
1153 return home_fixate_internal(h, secret, HOME_FIXATING_FOR_ACTIVATION, error);
1154 case HOME_ABSENT:
1155 return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
1156 case HOME_ACTIVE:
1157 return sd_bus_error_setf(error, BUS_ERROR_HOME_ALREADY_ACTIVE, "Home %s is already active.", h->user_name);
1158 case HOME_LOCKED:
1159 return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
1160 case HOME_INACTIVE:
1161 break;
1162 default:
1163 return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
1164 }
1165
1166 r = home_ratelimit(h, error);
1167 if (r < 0)
1168 return r;
1169
1170 return home_activate_internal(h, secret, HOME_ACTIVATING, error);
1171}
1172
1173static int home_authenticate_internal(Home *h, UserRecord *secret, HomeState for_state, sd_bus_error *error) {
1174 int r;
1175
1176 assert(h);
1177 assert(IN_SET(for_state, HOME_AUTHENTICATING, HOME_AUTHENTICATING_WHILE_ACTIVE, HOME_AUTHENTICATING_FOR_ACQUIRE));
1178
1179 r = home_start_work(h, "inspect", h->record, secret);
1180 if (r < 0)
1181 return r;
1182
1183 home_set_state(h, for_state);
1184 return 0;
1185}
1186
1187int home_authenticate(Home *h, UserRecord *secret, sd_bus_error *error) {
1188 HomeState state;
1189 int r;
1190
1191 assert(h);
1192
1193 state = home_get_state(h);
1194 switch (state) {
1195 case HOME_ABSENT:
1196 return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
1197 case HOME_LOCKED:
1198 return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
1199 case HOME_UNFIXATED:
1200 case HOME_INACTIVE:
1201 case HOME_ACTIVE:
1202 break;
1203 default:
1204 return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
1205 }
1206
1207 r = home_ratelimit(h, error);
1208 if (r < 0)
1209 return r;
1210
1211 return home_authenticate_internal(h, secret, state == HOME_ACTIVE ? HOME_AUTHENTICATING_WHILE_ACTIVE : HOME_AUTHENTICATING, error);
1212}
1213
1214static int home_deactivate_internal(Home *h, bool force, sd_bus_error *error) {
1215 int r;
1216
1217 assert(h);
1218
1219 r = home_start_work(h, force ? "deactivate-force" : "deactivate", h->record, NULL);
1220 if (r < 0)
1221 return r;
1222
1223 home_set_state(h, HOME_DEACTIVATING);
1224 return 0;
1225}
1226
1227int home_deactivate(Home *h, bool force, sd_bus_error *error) {
1228 assert(h);
1229
1230 switch (home_get_state(h)) {
1231 case HOME_UNFIXATED:
1232 case HOME_ABSENT:
1233 case HOME_INACTIVE:
1234 return sd_bus_error_setf(error, BUS_ERROR_HOME_NOT_ACTIVE, "Home %s not active.", h->user_name);
1235 case HOME_LOCKED:
1236 return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
1237 case HOME_ACTIVE:
1238 break;
1239 default:
1240 return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
1241 }
1242
1243 return home_deactivate_internal(h, force, error);
1244}
1245
1246int home_create(Home *h, UserRecord *secret, sd_bus_error *error) {
1247 int r;
1248
1249 assert(h);
1250
1251 switch (home_get_state(h)) {
1252 case HOME_INACTIVE:
1253 if (h->record->storage < 0)
1254 break; /* if no storage is defined we don't know what precisely to look for, hence
1255 * HOME_INACTIVE is OK in that case too. */
1256
1257 if (IN_SET(user_record_test_image_path(h->record), USER_TEST_MAYBE, USER_TEST_UNDEFINED))
1258 break; /* And if the image path test isn't conclusive, let's also go on */
1259
1260 _fallthrough_;
1261 case HOME_UNFIXATED:
1262 return sd_bus_error_setf(error, BUS_ERROR_HOME_EXISTS, "Home of user %s already exists.", h->user_name);
1263 case HOME_ABSENT:
1264 break;
1265 case HOME_ACTIVE:
1266 case HOME_LOCKED:
1267 default:
1268 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);
1269 }
1270
1271 if (h->record->enforce_password_policy == false)
1272 log_debug("Password quality check turned off for account, skipping.");
1273 else {
1274 r = quality_check_password(h->record, secret, error);
1275 if (r < 0)
1276 return r;
1277 }
1278
1279 r = home_start_work(h, "create", h->record, secret);
1280 if (r < 0)
1281 return r;
1282
1283 home_set_state(h, HOME_CREATING);
1284 return 0;
1285}
1286
1287int home_remove(Home *h, sd_bus_error *error) {
1288 HomeState state;
1289 int r;
1290
1291 assert(h);
1292
1293 state = home_get_state(h);
1294 switch (state) {
1295 case HOME_ABSENT: /* If the home directory is absent, then this is just like unregistering */
1296 return home_unregister(h, error);
1297 case HOME_LOCKED:
1298 return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
1299 case HOME_UNFIXATED:
1300 case HOME_INACTIVE:
1301 break;
1302 case HOME_ACTIVE:
1303 default:
1304 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);
1305 }
1306
1307 r = home_start_work(h, "remove", h->record, NULL);
1308 if (r < 0)
1309 return r;
1310
1311 home_set_state(h, HOME_REMOVING);
1312 return 0;
1313}
1314
1315static int user_record_extend_with_binding(UserRecord *hr, UserRecord *with_binding, UserRecordLoadFlags flags, UserRecord **ret) {
1316 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
1317 _cleanup_(user_record_unrefp) UserRecord *nr = NULL;
1318 JsonVariant *binding;
1319 int r;
1320
1321 assert(hr);
1322 assert(with_binding);
1323 assert(ret);
1324
1325 assert_se(v = json_variant_ref(hr->json));
1326
1327 binding = json_variant_by_key(with_binding->json, "binding");
1328 if (binding) {
1329 r = json_variant_set_field(&v, "binding", binding);
1330 if (r < 0)
1331 return r;
1332 }
1333
1334 nr = user_record_new();
1335 if (!nr)
1336 return -ENOMEM;
1337
1338 r = user_record_load(nr, v, flags);
1339 if (r < 0)
1340 return r;
1341
1342 *ret = TAKE_PTR(nr);
1343 return 0;
1344}
1345
1346static int home_update_internal(Home *h, const char *verb, UserRecord *hr, UserRecord *secret, sd_bus_error *error) {
1347 _cleanup_(user_record_unrefp) UserRecord *new_hr = NULL, *saved_secret = NULL, *signed_hr = NULL;
1348 int r, c;
1349
1350 assert(h);
1351 assert(verb);
1352 assert(hr);
1353
1354 if (!user_record_compatible(hr, h->record))
1355 return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_MISMATCH, "Updated user record is not compatible with existing one.");
1356 c = user_record_compare_last_change(hr, h->record); /* refuse downgrades */
1357 if (c < 0)
1358 return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_DOWNGRADE, "Refusing to update to older home record.");
1359
1360 if (!secret && FLAGS_SET(hr->mask, USER_RECORD_SECRET)) {
1361 r = user_record_clone(hr, USER_RECORD_EXTRACT_SECRET, &saved_secret);
1362 if (r < 0)
1363 return r;
1364
1365 secret = saved_secret;
1366 }
1367
1368 r = manager_verify_user_record(h->manager, hr);
1369 switch (r) {
1370
1371 case USER_RECORD_UNSIGNED:
1372 if (h->signed_locally <= 0) /* If the existing record is not owned by us, don't accept an
1373 * unsigned new record. i.e. only implicitly sign new records
1374 * that where previously signed by us too. */
1375 return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_SIGNED, "Home %s is signed and cannot be modified locally.", h->user_name);
1376
1377 /* The updated record is not signed, then do so now */
1378 r = manager_sign_user_record(h->manager, hr, &signed_hr, error);
1379 if (r < 0)
1380 return r;
1381
1382 hr = signed_hr;
1383 break;
1384
1385 case USER_RECORD_SIGNED_EXCLUSIVE:
1386 case USER_RECORD_SIGNED:
1387 case USER_RECORD_FOREIGN:
1388 /* Has already been signed. Great! */
1389 break;
1390
1391 case -ENOKEY:
1392 default:
1393 return r;
1394 }
1395
1396 r = user_record_extend_with_binding(hr, h->record, USER_RECORD_LOAD_MASK_SECRET, &new_hr);
1397 if (r < 0)
1398 return r;
1399
1400 if (c == 0) {
1401 /* different payload but same lastChangeUSec field? That's not cool! */
1402
1403 r = user_record_masked_equal(new_hr, h->record, USER_RECORD_REGULAR|USER_RECORD_PRIVILEGED|USER_RECORD_PER_MACHINE);
1404 if (r < 0)
1405 return r;
1406 if (r == 0)
1407 return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_MISMATCH, "Home record different but timestamp remained the same, refusing.");
1408 }
1409
1410 r = home_start_work(h, verb, new_hr, secret);
1411 if (r < 0)
1412 return r;
1413
1414 return 0;
1415}
1416
1417int home_update(Home *h, UserRecord *hr, sd_bus_error *error) {
1418 HomeState state;
1419 int r;
1420
1421 assert(h);
1422 assert(hr);
1423
1424 state = home_get_state(h);
1425 switch (state) {
1426 case HOME_UNFIXATED:
1427 return sd_bus_error_setf(error, BUS_ERROR_HOME_UNFIXATED, "Home %s has not been fixated yet.", h->user_name);
1428 case HOME_ABSENT:
1429 return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
1430 case HOME_LOCKED:
1431 return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
1432 case HOME_INACTIVE:
1433 case HOME_ACTIVE:
1434 break;
1435 default:
1436 return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
1437 }
1438
1439 r = home_ratelimit(h, error);
1440 if (r < 0)
1441 return r;
1442
1443 r = home_update_internal(h, "update", hr, NULL, error);
1444 if (r < 0)
1445 return r;
1446
1447 home_set_state(h, state == HOME_ACTIVE ? HOME_UPDATING_WHILE_ACTIVE : HOME_UPDATING);
1448 return 0;
1449}
1450
1451int home_resize(Home *h, uint64_t disk_size, UserRecord *secret, sd_bus_error *error) {
1452 _cleanup_(user_record_unrefp) UserRecord *c = NULL;
1453 HomeState state;
1454 int r;
1455
1456 assert(h);
1457
1458 state = home_get_state(h);
1459 switch (state) {
1460 case HOME_UNFIXATED:
1461 return sd_bus_error_setf(error, BUS_ERROR_HOME_UNFIXATED, "Home %s has not been fixated yet.", h->user_name);
1462 case HOME_ABSENT:
1463 return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
1464 case HOME_LOCKED:
1465 return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
1466 case HOME_INACTIVE:
1467 case HOME_ACTIVE:
1468 break;
1469 default:
1470 return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
1471 }
1472
1473 r = home_ratelimit(h, error);
1474 if (r < 0)
1475 return r;
1476
1477 if (disk_size == UINT64_MAX || disk_size == h->record->disk_size) {
1478 if (h->record->disk_size == UINT64_MAX)
80ace4f2 1479 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No disk size to resize to specified.");
70a5db58
LP
1480
1481 c = user_record_ref(h->record); /* Shortcut if size is unspecified or matches the record */
1482 } else {
1483 _cleanup_(user_record_unrefp) UserRecord *signed_c = NULL;
1484
1485 if (h->signed_locally <= 0) /* Don't allow changing of records not signed only by us */
1486 return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_SIGNED, "Home %s is signed and cannot be modified locally.", h->user_name);
1487
1488 r = user_record_clone(h->record, USER_RECORD_LOAD_REFUSE_SECRET, &c);
1489 if (r < 0)
1490 return r;
1491
1492 r = user_record_set_disk_size(c, disk_size);
1493 if (r == -ERANGE)
1494 return sd_bus_error_setf(error, BUS_ERROR_BAD_HOME_SIZE, "Requested size for home %s out of acceptable range.", h->user_name);
1495 if (r < 0)
1496 return r;
1497
1498 r = user_record_update_last_changed(c, false);
1499 if (r == -ECHRNG)
1500 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);
1501 if (r < 0)
1502 return r;
1503
1504 r = manager_sign_user_record(h->manager, c, &signed_c, error);
1505 if (r < 0)
1506 return r;
1507
1508 user_record_unref(c);
1509 c = TAKE_PTR(signed_c);
1510 }
1511
1512 r = home_update_internal(h, "resize", c, secret, error);
1513 if (r < 0)
1514 return r;
1515
1516 home_set_state(h, state == HOME_ACTIVE ? HOME_RESIZING_WHILE_ACTIVE : HOME_RESIZING);
1517 return 0;
1518}
1519
1520static int home_may_change_password(
1521 Home *h,
1522 sd_bus_error *error) {
1523
1524 int r;
1525
1526 assert(h);
1527
1528 r = user_record_test_password_change_required(h->record);
1529 if (IN_SET(r, -EKEYREVOKED, -EOWNERDEAD, -EKEYEXPIRED))
86b52a39 1530 return 0; /* expired in some form, but changing is allowed */
70a5db58
LP
1531 if (IN_SET(r, -EKEYREJECTED, -EROFS))
1532 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);
1533 if (r < 0)
1534 return log_error_errno(r, "Failed to test password expiry: %m");
1535
1536 return 0; /* not expired */
1537}
1538
1539int home_passwd(Home *h,
1540 UserRecord *new_secret,
1541 UserRecord *old_secret,
1542 sd_bus_error *error) {
1543
1544 _cleanup_(user_record_unrefp) UserRecord *c = NULL, *merged_secret = NULL, *signed_c = NULL;
1545 HomeState state;
1546 int r;
1547
1548 assert(h);
1549
1550 if (h->signed_locally <= 0) /* Don't allow changing of records not signed only by us */
1551 return sd_bus_error_setf(error, BUS_ERROR_HOME_RECORD_SIGNED, "Home %s is signed and cannot be modified locally.", h->user_name);
1552
1553 state = home_get_state(h);
1554 switch (state) {
1555 case HOME_UNFIXATED:
1556 return sd_bus_error_setf(error, BUS_ERROR_HOME_UNFIXATED, "Home %s has not been fixated yet.", h->user_name);
1557 case HOME_ABSENT:
1558 return sd_bus_error_setf(error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
1559 case HOME_LOCKED:
1560 return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
1561 case HOME_INACTIVE:
1562 case HOME_ACTIVE:
1563 break;
1564 default:
1565 return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
1566 }
1567
1568 r = home_ratelimit(h, error);
1569 if (r < 0)
1570 return r;
1571
1572 r = home_may_change_password(h, error);
1573 if (r < 0)
1574 return r;
1575
1576 r = user_record_clone(h->record, USER_RECORD_LOAD_REFUSE_SECRET, &c);
1577 if (r < 0)
1578 return r;
1579
1580 merged_secret = user_record_new();
1581 if (!merged_secret)
1582 return -ENOMEM;
1583
1584 r = user_record_merge_secret(merged_secret, old_secret);
1585 if (r < 0)
1586 return r;
1587
1588 r = user_record_merge_secret(merged_secret, new_secret);
1589 if (r < 0)
1590 return r;
1591
1592 if (!strv_isempty(new_secret->password)) {
1593 /* Update the password only if one is specified, otherwise let's just reuse the old password
1594 * data. This is useful as a way to propagate updated user records into the LUKS backends
1595 * properly. */
1596
1597 r = user_record_make_hashed_password(c, new_secret->password, /* extend = */ false);
1598 if (r < 0)
1599 return r;
1600
1601 r = user_record_set_password_change_now(c, -1 /* remove */);
1602 if (r < 0)
1603 return r;
1604 }
1605
1606 r = user_record_update_last_changed(c, true);
1607 if (r == -ECHRNG)
1608 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);
1609 if (r < 0)
1610 return r;
1611
1612 r = manager_sign_user_record(h->manager, c, &signed_c, error);
1613 if (r < 0)
1614 return r;
1615
1616 if (c->enforce_password_policy == false)
1617 log_debug("Password quality check turned off for account, skipping.");
1618 else {
1619 r = quality_check_password(c, merged_secret, error);
1620 if (r < 0)
1621 return r;
1622 }
1623
1624 r = home_update_internal(h, "passwd", signed_c, merged_secret, error);
1625 if (r < 0)
1626 return r;
1627
1628 home_set_state(h, state == HOME_ACTIVE ? HOME_PASSWD_WHILE_ACTIVE : HOME_PASSWD);
1629 return 0;
1630}
1631
1632int home_unregister(Home *h, sd_bus_error *error) {
1633 int r;
1634
1635 assert(h);
1636
1637 switch (home_get_state(h)) {
1638 case HOME_UNFIXATED:
1639 return sd_bus_error_setf(error, BUS_ERROR_HOME_UNFIXATED, "Home %s is not registered.", h->user_name);
1640 case HOME_LOCKED:
1641 return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
1642 case HOME_ABSENT:
1643 case HOME_INACTIVE:
1644 break;
1645 case HOME_ACTIVE:
1646 default:
1647 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);
1648 }
1649
1650 r = home_unlink_record(h);
1651 if (r < 0)
1652 return r;
1653
1654 /* And destroy the whole entry. The caller needs to be prepared for that. */
1655 h = home_free(h);
1656 return 1;
1657}
1658
1659int home_lock(Home *h, sd_bus_error *error) {
1660 int r;
1661
1662 assert(h);
1663
1664 switch (home_get_state(h)) {
1665 case HOME_UNFIXATED:
1666 case HOME_ABSENT:
1667 case HOME_INACTIVE:
1668 return sd_bus_error_setf(error, BUS_ERROR_HOME_NOT_ACTIVE, "Home %s is not active.", h->user_name);
1669 case HOME_LOCKED:
1670 return sd_bus_error_setf(error, BUS_ERROR_HOME_LOCKED, "Home %s is already locked.", h->user_name);
1671 case HOME_ACTIVE:
1672 break;
1673 default:
1674 return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
1675 }
1676
1677 r = home_start_work(h, "lock", h->record, NULL);
1678 if (r < 0)
1679 return r;
1680
1681 home_set_state(h, HOME_LOCKING);
1682 return 0;
1683}
1684
1685static int home_unlock_internal(Home *h, UserRecord *secret, HomeState for_state, sd_bus_error *error) {
1686 int r;
1687
1688 assert(h);
1689 assert(IN_SET(for_state, HOME_UNLOCKING, HOME_UNLOCKING_FOR_ACQUIRE));
1690
1691 r = home_start_work(h, "unlock", h->record, secret);
1692 if (r < 0)
1693 return r;
1694
1695 home_set_state(h, for_state);
1696 return 0;
1697}
1698
1699int home_unlock(Home *h, UserRecord *secret, sd_bus_error *error) {
1700 int r;
1701 assert(h);
1702
1703 r = home_ratelimit(h, error);
1704 if (r < 0)
1705 return r;
1706
1707 switch (home_get_state(h)) {
1708 case HOME_UNFIXATED:
1709 case HOME_ABSENT:
1710 case HOME_INACTIVE:
1711 case HOME_ACTIVE:
1712 return sd_bus_error_setf(error, BUS_ERROR_HOME_NOT_LOCKED, "Home %s is not locked.", h->user_name);
1713 case HOME_LOCKED:
1714 break;
1715 default:
1716 return sd_bus_error_setf(error, BUS_ERROR_HOME_BUSY, "An operation on home %s is currently being executed.", h->user_name);
1717 }
1718
1719 return home_unlock_internal(h, secret, HOME_UNLOCKING, error);
1720}
1721
1722HomeState home_get_state(Home *h) {
1723 assert(h);
1724
1725 /* When the state field is initialized, it counts. */
1726 if (h->state >= 0)
1727 return h->state;
1728
1729 /* Otherwise, let's see if the home directory is mounted. If so, we assume for sure the home
1730 * directory is active */
1731 if (user_record_test_home_directory(h->record) == USER_TEST_MOUNTED)
1732 return HOME_ACTIVE;
1733
1734 /* And if we see the image being gone, we report this as absent */
1735 if (user_record_test_image_path(h->record) == USER_TEST_ABSENT)
1736 return HOME_ABSENT;
1737
1738 /* And for all other cases we return "inactive". */
1739 return HOME_INACTIVE;
1740}
1741
1742void home_process_notify(Home *h, char **l) {
1743 const char *e;
1744 int error;
1745 int r;
1746
1747 assert(h);
1748
1749 e = strv_env_get(l, "ERRNO");
1750 if (!e) {
1751 log_debug("Got notify message lacking ERRNO= field, ignoring.");
1752 return;
1753 }
1754
1755 r = safe_atoi(e, &error);
1756 if (r < 0) {
162392b7 1757 log_debug_errno(r, "Failed to parse received error number, ignoring: %s", e);
70a5db58
LP
1758 return;
1759 }
1760 if (error <= 0) {
1761 log_debug("Error number is out of range: %i", error);
1762 return;
1763 }
1764
1765 h->worker_error_code = error;
1766}
1767
1768int home_killall(Home *h) {
1769 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1770 _cleanup_free_ char *unit = NULL;
1771 int r;
1772
1773 assert(h);
1774
1775 if (!uid_is_valid(h->uid))
1776 return 0;
1777
1778 assert(h->uid > 0); /* We never should be UID 0 */
1779
1780 /* Let's kill everything matching the specified UID */
1781 r = safe_fork("(sd-killer)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL);
1782 if (r < 0)
1783 return r;
1784 if (r == 0) {
1785 gid_t gid;
1786
1787 /* Child */
1788
1789 gid = user_record_gid(h->record);
1790 if (setresgid(gid, gid, gid) < 0) {
1791 log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
1792 _exit(EXIT_FAILURE);
1793 }
1794
1795 if (setgroups(0, NULL) < 0) {
1796 log_error_errno(errno, "Failed to reset auxiliary groups list: %m");
1797 _exit(EXIT_FAILURE);
1798 }
1799
1800 if (setresuid(h->uid, h->uid, h->uid) < 0) {
1801 log_error_errno(errno, "Failed to change UID to " UID_FMT ": %m", h->uid);
1802 _exit(EXIT_FAILURE);
1803 }
1804
1805 if (kill(-1, SIGKILL) < 0) {
1806 log_error_errno(errno, "Failed to kill all processes of UID " UID_FMT ": %m", h->uid);
1807 _exit(EXIT_FAILURE);
1808 }
1809
1810 _exit(EXIT_SUCCESS);
1811 }
1812
1813 /* Let's also kill everything in the user's slice */
1814 if (asprintf(&unit, "user-" UID_FMT ".slice", h->uid) < 0)
1815 return log_oom();
1816
1817 r = sd_bus_call_method(
1818 h->manager->bus,
1819 "org.freedesktop.systemd1",
1820 "/org/freedesktop/systemd1",
1821 "org.freedesktop.systemd1.Manager",
1822 "KillUnit",
1823 &error,
1824 NULL,
1825 "ssi", unit, "all", SIGKILL);
1826 if (r < 0)
1827 log_full_errno(sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ? LOG_DEBUG : LOG_WARNING,
1828 r, "Failed to kill login processes of user, ignoring: %s", bus_error_message(&error, r));
1829
1830 return 1;
1831}
1832
1833static int home_get_disk_status_luks(
1834 Home *h,
1835 HomeState state,
1836 uint64_t *ret_disk_size,
1837 uint64_t *ret_disk_usage,
1838 uint64_t *ret_disk_free,
1839 uint64_t *ret_disk_ceiling,
1840 uint64_t *ret_disk_floor) {
1841
1842 uint64_t disk_size = UINT64_MAX, disk_usage = UINT64_MAX, disk_free = UINT64_MAX,
1843 disk_ceiling = UINT64_MAX, disk_floor = UINT64_MAX,
1844 stat_used = UINT64_MAX, fs_size = UINT64_MAX, header_size = 0;
1845
1846 struct statfs sfs;
1847 const char *hd;
1848 int r;
1849
1850 assert(h);
1851 assert(ret_disk_size);
1852 assert(ret_disk_usage);
1853 assert(ret_disk_free);
1854 assert(ret_disk_ceiling);
1855
1856 if (state != HOME_ABSENT) {
1857 const char *ip;
1858
1859 ip = user_record_image_path(h->record);
1860 if (ip) {
1861 struct stat st;
1862
1863 if (stat(ip, &st) < 0)
1864 log_debug_errno(errno, "Failed to stat() %s, ignoring: %m", ip);
1865 else if (S_ISREG(st.st_mode)) {
1866 _cleanup_free_ char *parent = NULL;
1867
1868 disk_size = st.st_size;
1869 stat_used = st.st_blocks * 512;
1870
1871 parent = dirname_malloc(ip);
1872 if (!parent)
1873 return log_oom();
1874
1875 if (statfs(parent, &sfs) < 0)
1876 log_debug_errno(errno, "Failed to statfs() %s, ignoring: %m", parent);
1877 else
1878 disk_ceiling = stat_used + sfs.f_bsize * sfs.f_bavail;
1879
1880 } else if (S_ISBLK(st.st_mode)) {
1881 _cleanup_free_ char *szbuf = NULL;
1882 char p[SYS_BLOCK_PATH_MAX("/size")];
1883
1884 /* Let's read the size off sysfs, so that we don't have to open the device */
1885 xsprintf_sys_block_path(p, "/size", st.st_rdev);
1886 r = read_one_line_file(p, &szbuf);
1887 if (r < 0)
1888 log_debug_errno(r, "Failed to read %s, ignoring: %m", p);
1889 else {
1890 uint64_t sz;
1891
1892 r = safe_atou64(szbuf, &sz);
1893 if (r < 0)
1894 log_debug_errno(r, "Failed to parse %s, ignoring: %s", p, szbuf);
1895 else
1896 disk_size = sz * 512;
1897 }
1898 } else
1899 log_debug("Image path is not a block device or regular file, not able to acquire size.");
1900 }
1901 }
1902
1903 if (!HOME_STATE_IS_ACTIVE(state))
1904 goto finish;
1905
1906 hd = user_record_home_directory(h->record);
1907 if (!hd)
1908 goto finish;
1909
1910 if (statfs(hd, &sfs) < 0) {
80ace4f2 1911 log_debug_errno(errno, "Failed to statfs() %s, ignoring: %m", hd);
70a5db58
LP
1912 goto finish;
1913 }
1914
1915 disk_free = sfs.f_bsize * sfs.f_bavail;
1916 fs_size = sfs.f_bsize * sfs.f_blocks;
1917 if (disk_size != UINT64_MAX && disk_size > fs_size)
1918 header_size = disk_size - fs_size;
1919
1920 /* We take a perspective from the user here (as opposed to from the host): the used disk space is the
1921 * difference from the limit and what's free. This makes a difference if sparse mode is not used: in
1922 * that case the image is pre-allocated and thus appears all used from the host PoV but is not used
1923 * up at all yet from the user's PoV.
1924 *
1925 * That said, we use use the stat() reported loopback file size as upper boundary: our footprint can
1926 * never be larger than what we take up on the lowest layers. */
1927
1928 if (disk_size != UINT64_MAX && disk_size > disk_free) {
1929 disk_usage = disk_size - disk_free;
1930
1931 if (stat_used != UINT64_MAX && disk_usage > stat_used)
1932 disk_usage = stat_used;
1933 } else
1934 disk_usage = stat_used;
1935
1936 /* If we have the magic, determine floor preferably by magic */
1937 disk_floor = minimal_size_by_fs_magic(sfs.f_type) + header_size;
1938
1939finish:
1940 /* If we don't know the magic, go by file system name */
1941 if (disk_floor == UINT64_MAX)
1942 disk_floor = minimal_size_by_fs_name(user_record_file_system_type(h->record));
1943
1944 *ret_disk_size = disk_size;
1945 *ret_disk_usage = disk_usage;
1946 *ret_disk_free = disk_free;
1947 *ret_disk_ceiling = disk_ceiling;
1948 *ret_disk_floor = disk_floor;
1949
1950 return 0;
1951}
1952
1953static int home_get_disk_status_directory(
1954 Home *h,
1955 HomeState state,
1956 uint64_t *ret_disk_size,
1957 uint64_t *ret_disk_usage,
1958 uint64_t *ret_disk_free,
1959 uint64_t *ret_disk_ceiling,
1960 uint64_t *ret_disk_floor) {
1961
1962 uint64_t disk_size = UINT64_MAX, disk_usage = UINT64_MAX, disk_free = UINT64_MAX,
1963 disk_ceiling = UINT64_MAX, disk_floor = UINT64_MAX;
1964 struct statfs sfs;
1965 struct dqblk req;
1966 const char *path = NULL;
1967 int r;
1968
1969 assert(ret_disk_size);
1970 assert(ret_disk_usage);
1971 assert(ret_disk_free);
1972 assert(ret_disk_ceiling);
1973 assert(ret_disk_floor);
1974
1975 if (HOME_STATE_IS_ACTIVE(state))
1976 path = user_record_home_directory(h->record);
1977
1978 if (!path) {
1979 if (state == HOME_ABSENT)
1980 goto finish;
1981
1982 path = user_record_image_path(h->record);
1983 }
1984
1985 if (!path)
1986 goto finish;
1987
1988 if (statfs(path, &sfs) < 0)
1989 log_debug_errno(errno, "Failed to statfs() %s, ignoring: %m", path);
1990 else {
1991 disk_free = sfs.f_bsize * sfs.f_bavail;
1992 disk_size = sfs.f_bsize * sfs.f_blocks;
1993
1994 /* We don't initialize disk_usage from statfs() data here, since the device is likely not used
1995 * by us alone, and disk_usage should only reflect our own use. */
1996 }
1997
1998 if (IN_SET(h->record->storage, USER_CLASSIC, USER_DIRECTORY, USER_SUBVOLUME)) {
1999
2000 r = btrfs_is_subvol(path);
2001 if (r < 0)
2002 log_debug_errno(r, "Failed to determine whether %s is a btrfs subvolume: %m", path);
2003 else if (r > 0) {
2004 BtrfsQuotaInfo qi;
2005
2006 r = btrfs_subvol_get_subtree_quota(path, 0, &qi);
2007 if (r < 0)
2008 log_debug_errno(r, "Failed to query btrfs subtree quota, ignoring: %m");
2009 else {
2010 disk_usage = qi.referenced;
2011
2012 if (disk_free != UINT64_MAX) {
2013 disk_ceiling = qi.referenced + disk_free;
2014
2015 if (disk_size != UINT64_MAX && disk_ceiling > disk_size)
2016 disk_ceiling = disk_size;
2017 }
2018
2019 if (qi.referenced_max != UINT64_MAX) {
2020 if (disk_size != UINT64_MAX)
2021 disk_size = MIN(qi.referenced_max, disk_size);
2022 else
2023 disk_size = qi.referenced_max;
2024 }
2025
2026 if (disk_size != UINT64_MAX) {
2027 if (disk_size > disk_usage)
2028 disk_free = disk_size - disk_usage;
2029 else
2030 disk_free = 0;
2031 }
2032 }
2033
2034 goto finish;
2035 }
2036 }
2037
2038 if (IN_SET(h->record->storage, USER_CLASSIC, USER_DIRECTORY, USER_FSCRYPT)) {
2039 r = quotactl_path(QCMD_FIXED(Q_GETQUOTA, USRQUOTA), path, h->uid, &req);
2040 if (r < 0) {
2041 if (ERRNO_IS_NOT_SUPPORTED(r)) {
2042 log_debug_errno(r, "No UID quota support on %s.", path);
2043 goto finish;
2044 }
2045
2046 if (r != -ESRCH) {
2047 log_debug_errno(r, "Failed to query disk quota for UID " UID_FMT ": %m", h->uid);
2048 goto finish;
2049 }
2050
2051 disk_usage = 0; /* No record of this user? then nothing was used */
2052 } else {
2053 if (FLAGS_SET(req.dqb_valid, QIF_SPACE) && disk_free != UINT64_MAX) {
2054 disk_ceiling = req.dqb_curspace + disk_free;
2055
2056 if (disk_size != UINT64_MAX && disk_ceiling > disk_size)
2057 disk_ceiling = disk_size;
2058 }
2059
2060 if (FLAGS_SET(req.dqb_valid, QIF_BLIMITS)) {
2061 uint64_t q;
2062
2063 /* Take the minimum of the quota and the available disk space here */
2064 q = req.dqb_bhardlimit * QIF_DQBLKSIZE;
2065 if (disk_size != UINT64_MAX)
2066 disk_size = MIN(disk_size, q);
2067 else
2068 disk_size = q;
2069 }
2070 if (FLAGS_SET(req.dqb_valid, QIF_SPACE)) {
2071 disk_usage = req.dqb_curspace;
2072
2073 if (disk_size != UINT64_MAX) {
2074 if (disk_size > disk_usage)
2075 disk_free = disk_size - disk_usage;
2076 else
2077 disk_free = 0;
2078 }
2079 }
2080 }
2081 }
2082
2083finish:
2084 *ret_disk_size = disk_size;
2085 *ret_disk_usage = disk_usage;
2086 *ret_disk_free = disk_free;
2087 *ret_disk_ceiling = disk_ceiling;
2088 *ret_disk_floor = disk_floor;
2089
2090 return 0;
2091}
2092
2093int home_augment_status(
2094 Home *h,
2095 UserRecordLoadFlags flags,
2096 UserRecord **ret) {
2097
2098 uint64_t disk_size = UINT64_MAX, disk_usage = UINT64_MAX, disk_free = UINT64_MAX, disk_ceiling = UINT64_MAX, disk_floor = UINT64_MAX;
2099 _cleanup_(json_variant_unrefp) JsonVariant *j = NULL, *v = NULL, *m = NULL, *status = NULL;
2100 _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
2101 char ids[SD_ID128_STRING_MAX];
2102 HomeState state;
2103 sd_id128_t id;
2104 int r;
2105
2106 assert(h);
2107 assert(ret);
2108
2109 /* We are supposed to add this, this can't be on hence. */
2110 assert(!FLAGS_SET(flags, USER_RECORD_STRIP_STATUS));
2111
2112 r = sd_id128_get_machine(&id);
2113 if (r < 0)
2114 return r;
2115
2116 state = home_get_state(h);
2117
2118 switch (h->record->storage) {
2119
2120 case USER_LUKS:
2121 r = home_get_disk_status_luks(h, state, &disk_size, &disk_usage, &disk_free, &disk_ceiling, &disk_floor);
2122 if (r < 0)
2123 return r;
2124
2125 break;
2126
2127 case USER_CLASSIC:
2128 case USER_DIRECTORY:
2129 case USER_SUBVOLUME:
2130 case USER_FSCRYPT:
2131 case USER_CIFS:
2132 r = home_get_disk_status_directory(h, state, &disk_size, &disk_usage, &disk_free, &disk_ceiling, &disk_floor);
2133 if (r < 0)
2134 return r;
2135
2136 break;
2137
2138 default:
2139 ; /* unset */
2140 }
2141
2142 if (disk_floor == UINT64_MAX || (disk_usage != UINT64_MAX && disk_floor < disk_usage))
2143 disk_floor = disk_usage;
2144 if (disk_floor == UINT64_MAX || disk_floor < USER_DISK_SIZE_MIN)
2145 disk_floor = USER_DISK_SIZE_MIN;
2146 if (disk_ceiling == UINT64_MAX || disk_ceiling > USER_DISK_SIZE_MAX)
2147 disk_ceiling = USER_DISK_SIZE_MAX;
2148
2149 r = json_build(&status,
2150 JSON_BUILD_OBJECT(
2151 JSON_BUILD_PAIR("state", JSON_BUILD_STRING(home_state_to_string(state))),
2152 JSON_BUILD_PAIR("service", JSON_BUILD_STRING("io.systemd.Home")),
2153 JSON_BUILD_PAIR_CONDITION(disk_size != UINT64_MAX, "diskSize", JSON_BUILD_UNSIGNED(disk_size)),
2154 JSON_BUILD_PAIR_CONDITION(disk_usage != UINT64_MAX, "diskUsage", JSON_BUILD_UNSIGNED(disk_usage)),
2155 JSON_BUILD_PAIR_CONDITION(disk_free != UINT64_MAX, "diskFree", JSON_BUILD_UNSIGNED(disk_free)),
2156 JSON_BUILD_PAIR_CONDITION(disk_ceiling != UINT64_MAX, "diskCeiling", JSON_BUILD_UNSIGNED(disk_ceiling)),
2157 JSON_BUILD_PAIR_CONDITION(disk_floor != UINT64_MAX, "diskFloor", JSON_BUILD_UNSIGNED(disk_floor)),
2158 JSON_BUILD_PAIR_CONDITION(h->signed_locally >= 0, "signedLocally", JSON_BUILD_BOOLEAN(h->signed_locally))
2159 ));
2160 if (r < 0)
2161 return r;
2162
2163 j = json_variant_ref(h->record->json);
2164 v = json_variant_ref(json_variant_by_key(j, "status"));
2165 m = json_variant_ref(json_variant_by_key(v, sd_id128_to_string(id, ids)));
2166
2167 r = json_variant_filter(&m, STRV_MAKE("diskSize", "diskUsage", "diskFree", "diskCeiling", "diskFloor", "signedLocally"));
2168 if (r < 0)
2169 return r;
2170
2171 r = json_variant_merge(&m, status);
2172 if (r < 0)
2173 return r;
2174
2175 r = json_variant_set_field(&v, ids, m);
2176 if (r < 0)
2177 return r;
2178
2179 r = json_variant_set_field(&j, "status", v);
2180 if (r < 0)
2181 return r;
2182
2183 ur = user_record_new();
2184 if (!ur)
2185 return -ENOMEM;
2186
2187 r = user_record_load(ur, j, flags);
2188 if (r < 0)
2189 return r;
2190
2191 ur->incomplete =
2192 FLAGS_SET(h->record->mask, USER_RECORD_PRIVILEGED) &&
2193 !FLAGS_SET(ur->mask, USER_RECORD_PRIVILEGED);
2194
2195 *ret = TAKE_PTR(ur);
2196 return 0;
2197}
2198
2199static int on_home_ref_eof(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2200 _cleanup_(operation_unrefp) Operation *o = NULL;
2201 Home *h = userdata;
2202
2203 assert(s);
2204 assert(h);
2205
2206 if (h->ref_event_source_please_suspend == s)
2207 h->ref_event_source_please_suspend = sd_event_source_disable_unref(h->ref_event_source_please_suspend);
2208
2209 if (h->ref_event_source_dont_suspend == s)
2210 h->ref_event_source_dont_suspend = sd_event_source_disable_unref(h->ref_event_source_dont_suspend);
2211
2212 if (h->ref_event_source_dont_suspend || h->ref_event_source_please_suspend)
2213 return 0;
2214
2215 log_info("Got notification that all sessions of user %s ended, deactivating automatically.", h->user_name);
2216
2217 o = operation_new(OPERATION_PIPE_EOF, NULL);
2218 if (!o) {
2219 log_oom();
2220 return 0;
2221 }
2222
2223 home_schedule_operation(h, o, NULL);
2224 return 0;
2225}
2226
2227int home_create_fifo(Home *h, bool please_suspend) {
2228 _cleanup_close_ int ret_fd = -1;
2229 sd_event_source **ss;
2230 const char *fn, *suffix;
2231 int r;
2232
2233 assert(h);
2234
2235 if (please_suspend) {
2236 suffix = ".please-suspend";
2237 ss = &h->ref_event_source_please_suspend;
2238 } else {
2239 suffix = ".dont-suspend";
2240 ss = &h->ref_event_source_dont_suspend;
2241 }
2242
2243 fn = strjoina("/run/systemd/home/", h->user_name, suffix);
2244
2245 if (!*ss) {
2246 _cleanup_close_ int ref_fd = -1;
2247
2248 (void) mkdir("/run/systemd/home/", 0755);
2249 if (mkfifo(fn, 0600) < 0 && errno != EEXIST)
2250 return log_error_errno(errno, "Failed to create FIFO %s: %m", fn);
2251
2252 ref_fd = open(fn, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
2253 if (ref_fd < 0)
2254 return log_error_errno(errno, "Failed to open FIFO %s for reading: %m", fn);
2255
2256 r = sd_event_add_io(h->manager->event, ss, ref_fd, 0, on_home_ref_eof, h);
2257 if (r < 0)
2258 return log_error_errno(r, "Failed to allocate reference FIFO event source: %m");
2259
2260 (void) sd_event_source_set_description(*ss, "acquire-ref");
2261
2262 r = sd_event_source_set_priority(*ss, SD_EVENT_PRIORITY_IDLE-1);
2263 if (r < 0)
2264 return r;
2265
2266 r = sd_event_source_set_io_fd_own(*ss, true);
2267 if (r < 0)
2268 return log_error_errno(r, "Failed to pass ownership of FIFO event fd to event source: %m");
2269
2270 TAKE_FD(ref_fd);
2271 }
2272
2273 ret_fd = open(fn, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
2274 if (ret_fd < 0)
2275 return log_error_errno(errno, "Failed to open FIFO %s for writing: %m", fn);
2276
2277 return TAKE_FD(ret_fd);
2278}
2279
2280static int home_dispatch_acquire(Home *h, Operation *o) {
2281 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2282 int (*call)(Home *h, UserRecord *secret, HomeState for_state, sd_bus_error *error) = NULL;
2283 HomeState for_state;
2284 int r;
2285
2286 assert(h);
2287 assert(o);
2288 assert(o->type == OPERATION_ACQUIRE);
2289
2290 switch (home_get_state(h)) {
2291
2292 case HOME_UNFIXATED:
2293 for_state = HOME_FIXATING_FOR_ACQUIRE;
2294 call = home_fixate_internal;
2295 break;
2296
2297 case HOME_ABSENT:
2298 r = sd_bus_error_setf(&error, BUS_ERROR_HOME_ABSENT, "Home %s is currently missing or not plugged in.", h->user_name);
2299 break;
2300
2301 case HOME_INACTIVE:
2302 for_state = HOME_ACTIVATING_FOR_ACQUIRE;
2303 call = home_activate_internal;
2304 break;
2305
2306 case HOME_ACTIVE:
2307 for_state = HOME_AUTHENTICATING_FOR_ACQUIRE;
2308 call = home_authenticate_internal;
2309 break;
2310
2311 case HOME_LOCKED:
2312 for_state = HOME_UNLOCKING_FOR_ACQUIRE;
2313 call = home_unlock_internal;
2314 break;
2315
2316 default:
2317 /* All other cases means we are currently executing an operation, which means the job remains
2318 * pending. */
2319 return 0;
2320 }
2321
2322 assert(!h->current_operation);
2323
2324 if (call) {
2325 r = home_ratelimit(h, &error);
2326 if (r >= 0)
2327 r = call(h, o->secret, for_state, &error);
2328 }
2329
2330 if (r != 0) /* failure or completed */
2331 operation_result(o, r, &error);
2332 else /* ongoing */
2333 h->current_operation = operation_ref(o);
2334
2335 return 1;
2336}
2337
2338static int home_dispatch_release(Home *h, Operation *o) {
2339 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2340 int r;
2341
2342 assert(h);
2343 assert(o);
2344 assert(o->type == OPERATION_RELEASE);
2345
2346 if (h->ref_event_source_dont_suspend || h->ref_event_source_please_suspend)
2347 /* If there's now a reference again, then let's abort the release attempt */
2348 r = sd_bus_error_setf(&error, BUS_ERROR_HOME_BUSY, "Home %s is currently referenced.", h->user_name);
2349 else {
2350 switch (home_get_state(h)) {
2351
2352 case HOME_UNFIXATED:
2353 case HOME_ABSENT:
2354 case HOME_INACTIVE:
2355 r = 0; /* done */
2356 break;
2357
2358 case HOME_LOCKED:
2359 r = sd_bus_error_setf(&error, BUS_ERROR_HOME_LOCKED, "Home %s is currently locked.", h->user_name);
2360 break;
2361
2362 case HOME_ACTIVE:
2363 r = home_deactivate_internal(h, false, &error);
2364 break;
2365
2366 default:
2367 /* All other cases means we are currently executing an operation, which means the job remains
2368 * pending. */
2369 return 0;
2370 }
2371 }
2372
2373 assert(!h->current_operation);
2374
2375 if (r <= 0) /* failure or completed */
2376 operation_result(o, r, &error);
2377 else /* ongoing */
2378 h->current_operation = operation_ref(o);
2379
2380 return 1;
2381}
2382
2383static int home_dispatch_lock_all(Home *h, Operation *o) {
2384 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2385 int r;
2386
2387 assert(h);
2388 assert(o);
2389 assert(o->type == OPERATION_LOCK_ALL);
2390
2391 switch (home_get_state(h)) {
2392
2393 case HOME_UNFIXATED:
2394 case HOME_ABSENT:
2395 case HOME_INACTIVE:
2396 log_info("Home %s is not active, no locking necessary.", h->user_name);
2397 r = 0; /* done */
2398 break;
2399
2400 case HOME_LOCKED:
2401 log_info("Home %s is already locked.", h->user_name);
2402 r = 0; /* done */
2403 break;
2404
2405 case HOME_ACTIVE:
2406 log_info("Locking home %s.", h->user_name);
2407 r = home_lock(h, &error);
2408 break;
2409
2410 default:
2411 /* All other cases means we are currently executing an operation, which means the job remains
2412 * pending. */
2413 return 0;
2414 }
2415
2416 assert(!h->current_operation);
2417
2418 if (r != 0) /* failure or completed */
2419 operation_result(o, r, &error);
2420 else /* ongoing */
2421 h->current_operation = operation_ref(o);
2422
2423 return 1;
2424}
2425
2426static int home_dispatch_pipe_eof(Home *h, Operation *o) {
2427 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2428 int r;
2429
2430 assert(h);
2431 assert(o);
2432 assert(o->type == OPERATION_PIPE_EOF);
2433
2434 if (h->ref_event_source_please_suspend || h->ref_event_source_dont_suspend)
2435 return 1; /* Hmm, there's a reference again, let's cancel this */
2436
2437 switch (home_get_state(h)) {
2438
2439 case HOME_UNFIXATED:
2440 case HOME_ABSENT:
2441 case HOME_INACTIVE:
2442 log_info("Home %s already deactivated, no automatic deactivation needed.", h->user_name);
2443 break;
2444
2445 case HOME_DEACTIVATING:
2446 log_info("Home %s is already being deactivated, automatic deactivated unnecessary.", h->user_name);
2447 break;
2448
2449 case HOME_ACTIVE:
2450 r = home_deactivate_internal(h, false, &error);
2451 if (r < 0)
2452 log_warning_errno(r, "Failed to deactivate %s, ignoring: %s", h->user_name, bus_error_message(&error, r));
2453 break;
2454
2455 case HOME_LOCKED:
2456 default:
2457 /* If the device is locked or any operation is being executed, let's leave this pending */
2458 return 0;
2459 }
2460
2461 /* Note that we don't call operation_fail() or operation_success() here, because this kind of
2462 * operation has no message associated with it, and thus there's no need to propagate success. */
2463
2464 assert(!o->message);
2465 return 1;
2466}
2467
2468static int home_dispatch_deactivate_force(Home *h, Operation *o) {
2469 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2470 int r;
2471
2472 assert(h);
2473 assert(o);
2474 assert(o->type == OPERATION_DEACTIVATE_FORCE);
2475
2476 switch (home_get_state(h)) {
2477
2478 case HOME_UNFIXATED:
2479 case HOME_ABSENT:
2480 case HOME_INACTIVE:
2481 log_debug("Home %s already deactivated, no forced deactivation due to unplug needed.", h->user_name);
2482 break;
2483
2484 case HOME_DEACTIVATING:
2485 log_debug("Home %s is already being deactivated, forced deactivation due to unplug unnecessary.", h->user_name);
2486 break;
2487
2488 case HOME_ACTIVE:
2489 case HOME_LOCKED:
2490 r = home_deactivate_internal(h, true, &error);
2491 if (r < 0)
2492 log_warning_errno(r, "Failed to forcibly deactivate %s, ignoring: %s", h->user_name, bus_error_message(&error, r));
2493 break;
2494
2495 default:
2496 /* If any operation is being executed, let's leave this pending */
2497 return 0;
2498 }
2499
2500 /* Note that we don't call operation_fail() or operation_success() here, because this kind of
2501 * operation has no message associated with it, and thus there's no need to propagate success. */
2502
2503 assert(!o->message);
2504 return 1;
2505}
2506
2507static int on_pending(sd_event_source *s, void *userdata) {
2508 Home *h = userdata;
2509 Operation *o;
2510 int r;
2511
2512 assert(s);
2513 assert(h);
2514
2515 o = ordered_set_first(h->pending_operations);
2516 if (o) {
2517 static int (* const operation_table[_OPERATION_MAX])(Home *h, Operation *o) = {
2518 [OPERATION_ACQUIRE] = home_dispatch_acquire,
2519 [OPERATION_RELEASE] = home_dispatch_release,
2520 [OPERATION_LOCK_ALL] = home_dispatch_lock_all,
2521 [OPERATION_PIPE_EOF] = home_dispatch_pipe_eof,
2522 [OPERATION_DEACTIVATE_FORCE] = home_dispatch_deactivate_force,
2523 };
2524
2525 assert(operation_table[o->type]);
2526 r = operation_table[o->type](h, o);
2527 if (r != 0) {
2528 /* The operation completed, let's remove it from the pending list, and exit while
2529 * leaving the event source enabled as it is. */
2530 assert_se(ordered_set_remove(h->pending_operations, o) == o);
2531 operation_unref(o);
2532 return 0;
2533 }
2534 }
2535
2536 /* Nothing to do anymore, let's turn off this event source */
2537 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2538 if (r < 0)
2539 return log_error_errno(r, "Failed to disable event source: %m");
2540
2541 return 0;
2542}
2543
2544int home_schedule_operation(Home *h, Operation *o, sd_bus_error *error) {
2545 int r;
2546
2547 assert(h);
2548
2549 if (o) {
2550 if (ordered_set_size(h->pending_operations) >= PENDING_OPERATIONS_MAX)
2551 return sd_bus_error_setf(error, BUS_ERROR_TOO_MANY_OPERATIONS, "Too many client operations requested");
2552
2553 r = ordered_set_ensure_allocated(&h->pending_operations, &operation_hash_ops);
2554 if (r < 0)
2555 return r;
2556
2557 r = ordered_set_put(h->pending_operations, o);
2558 if (r < 0)
2559 return r;
2560
2561 operation_ref(o);
2562 }
2563
2564 if (!h->pending_event_source) {
2565 r = sd_event_add_defer(h->manager->event, &h->pending_event_source, on_pending, h);
2566 if (r < 0)
2567 return log_error_errno(r, "Failed to allocate pending defer event source: %m");
2568
2569 (void) sd_event_source_set_description(h->pending_event_source, "pending");
2570
2571 r = sd_event_source_set_priority(h->pending_event_source, SD_EVENT_PRIORITY_IDLE);
2572 if (r < 0)
2573 return r;
2574 }
2575
2576 r = sd_event_source_set_enabled(h->pending_event_source, SD_EVENT_ON);
2577 if (r < 0)
2578 return log_error_errno(r, "Failed to trigger pending event source: %m");
2579
2580 return 0;
2581}
2582
2583static int home_get_image_path_seat(Home *h, char **ret) {
2584 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
2585 _cleanup_free_ char *c = NULL;
2586 const char *ip, *seat;
2587 struct stat st;
2588 int r;
2589
2590 assert(h);
2591
2592 if (user_record_storage(h->record) != USER_LUKS)
2593 return -ENXIO;
2594
2595 ip = user_record_image_path(h->record);
2596 if (!ip)
2597 return -ENXIO;
2598
2599 if (!path_startswith(ip, "/dev/"))
2600 return -ENXIO;
2601
2602 if (stat(ip, &st) < 0)
2603 return -errno;
2604
2605 if (!S_ISBLK(st.st_mode))
2606 return -ENOTBLK;
2607
2608 r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
2609 if (r < 0)
2610 return r;
2611
2612 r = sd_device_get_property_value(d, "ID_SEAT", &seat);
2613 if (r == -ENOENT) /* no property means seat0 */
2614 seat = "seat0";
2615 else if (r < 0)
2616 return r;
2617
2618 c = strdup(seat);
2619 if (!c)
2620 return -ENOMEM;
2621
2622 *ret = TAKE_PTR(c);
2623 return 0;
2624}
2625
2626int home_auto_login(Home *h, char ***ret_seats) {
2627 _cleanup_free_ char *seat = NULL, *seat2 = NULL;
2628
2629 assert(h);
2630 assert(ret_seats);
2631
2632 (void) home_get_image_path_seat(h, &seat);
2633
2634 if (h->record->auto_login > 0 && !streq_ptr(seat, "seat0")) {
2635 /* For now, when the auto-login boolean is set for a user, let's make it mean
2636 * "seat0". Eventually we can extend the concept and allow configuration of any kind of seat,
2637 * but let's keep simple initially, most likely the feature is interesting on single-user
2638 * systems anyway, only.
2639 *
2640 * We filter out users marked for auto-login in we know for sure their home directory is
2641 * absent. */
2642
2643 if (user_record_test_image_path(h->record) != USER_TEST_ABSENT) {
2644 seat2 = strdup("seat0");
2645 if (!seat2)
2646 return -ENOMEM;
2647 }
2648 }
2649
2650 if (seat || seat2) {
2651 _cleanup_strv_free_ char **list = NULL;
2652 size_t i = 0;
2653
2654 list = new(char*, 3);
2655 if (!list)
2656 return -ENOMEM;
2657
2658 if (seat)
2659 list[i++] = TAKE_PTR(seat);
2660 if (seat2)
2661 list[i++] = TAKE_PTR(seat2);
2662
2663 list[i] = NULL;
2664 *ret_seats = TAKE_PTR(list);
2665 return 1;
2666 }
2667
2668 *ret_seats = NULL;
2669 return 0;
2670}
2671
2672int home_set_current_message(Home *h, sd_bus_message *m) {
2673 assert(h);
2674
2675 if (!m)
2676 return 0;
2677
2678 if (h->current_operation)
2679 return -EBUSY;
2680
2681 h->current_operation = operation_new(OPERATION_IMMEDIATE, m);
2682 if (!h->current_operation)
2683 return -ENOMEM;
2684
2685 return 1;
2686}
2687
2688static const char* const home_state_table[_HOME_STATE_MAX] = {
2689 [HOME_UNFIXATED] = "unfixated",
2690 [HOME_ABSENT] = "absent",
2691 [HOME_INACTIVE] = "inactive",
2692 [HOME_FIXATING] = "fixating",
2693 [HOME_FIXATING_FOR_ACTIVATION] = "fixating-for-activation",
2694 [HOME_FIXATING_FOR_ACQUIRE] = "fixating-for-acquire",
2695 [HOME_ACTIVATING] = "activating",
2696 [HOME_ACTIVATING_FOR_ACQUIRE] = "activating-for-acquire",
2697 [HOME_DEACTIVATING] = "deactivating",
2698 [HOME_ACTIVE] = "active",
2699 [HOME_LOCKING] = "locking",
2700 [HOME_LOCKED] = "locked",
2701 [HOME_UNLOCKING] = "unlocking",
2702 [HOME_UNLOCKING_FOR_ACQUIRE] = "unlocking-for-acquire",
2703 [HOME_CREATING] = "creating",
2704 [HOME_REMOVING] = "removing",
2705 [HOME_UPDATING] = "updating",
2706 [HOME_UPDATING_WHILE_ACTIVE] = "updating-while-active",
2707 [HOME_RESIZING] = "resizing",
2708 [HOME_RESIZING_WHILE_ACTIVE] = "resizing-while-active",
2709 [HOME_PASSWD] = "passwd",
2710 [HOME_PASSWD_WHILE_ACTIVE] = "passwd-while-active",
2711 [HOME_AUTHENTICATING] = "authenticating",
2712 [HOME_AUTHENTICATING_WHILE_ACTIVE] = "authenticating-while-active",
2713 [HOME_AUTHENTICATING_FOR_ACQUIRE] = "authenticating-for-acquire",
2714};
2715
2716DEFINE_STRING_TABLE_LOOKUP(home_state, HomeState);