summaryrefslogtreecommitdiffstats
path: root/src/home/homed-manager-bus.c
blob: 7cf543922f9ae299c580628791de5833a7ba9c4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <linux/capability.h>

#include "alloc-util.h"
#include "bus-common-errors.h"
#include "bus-polkit.h"
#include "format-util.h"
#include "homed-bus.h"
#include "homed-home-bus.h"
#include "homed-manager-bus.h"
#include "homed-manager.h"
#include "strv.h"
#include "user-record-sign.h"
#include "user-record-util.h"
#include "user-util.h"

static int property_get_auto_login(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *error) {

        Manager *m = ASSERT_PTR(userdata);
        Home *h;
        int r;

        assert(bus);
        assert(reply);

        r = sd_bus_message_open_container(reply, 'a', "(sso)");
        if (r < 0)
                return r;

        HASHMAP_FOREACH(h, m->homes_by_name) {
                _cleanup_strv_free_ char **seats = NULL;
                _cleanup_free_ char *home_path = NULL;

                r = home_auto_login(h, &seats);
                if (r < 0) {
                        log_debug_errno(r, "Failed to determine whether home '%s' is candidate for auto-login, ignoring: %m", h->user_name);
                        continue;
                }
                if (!r)
                        continue;

                r = bus_home_path(h, &home_path);
                if (r < 0)
                        return log_error_errno(r, "Failed to generate home bus path: %m");

                STRV_FOREACH(s, seats) {
                        r = sd_bus_message_append(reply, "(sso)", h->user_name, *s, home_path);
                        if (r < 0)
                                return r;
                }
        }

        return sd_bus_message_close_container(reply);
}

static int method_get_home_by_name(
                sd_bus_message *message,
                void *userdata,
                sd_bus_error *error) {

        _cleanup_free_ char *path = NULL;
        const char *user_name;
        Manager *m = ASSERT_PTR(userdata);
        Home *h;
        int r;

        assert(message);

        r = sd_bus_message_read(message, "s", &user_name);
        if (r < 0)
                return r;
        if (!valid_user_group_name(user_name, 0))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "User name %s is not valid", user_name);

        h = hashmap_get(m->homes_by_name, user_name);
        if (!h)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "No home for user %s known", user_name);

        r = bus_home_path(h, &path);
        if (r < 0)
                return r;

        return sd_bus_reply_method_return(
                        message, "usussso",
                        (uint32_t) h->uid,
                        home_state_to_string(home_get_state(h)),
                        h->record ? (uint32_t) user_record_gid(h->record) : GID_INVALID,
                        h->record ? user_record_real_name(h->record) : NULL,
                        h->record ? user_record_home_directory(h->record) : NULL,
                        h->record ? user_record_shell(h->record) : NULL,
                        path);
}

static int method_get_home_by_uid(
                sd_bus_message *message,
                void *userdata,
                sd_bus_error *error) {

        _cleanup_free_ char *path = NULL;
        Manager *m = ASSERT_PTR(userdata);
        uint32_t uid;
        int r;
        Home *h;

        assert(message);

        r = sd_bus_message_read(message, "u", &uid);
        if (r < 0)
                return r;
        if (!uid_is_valid(uid))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "UID " UID_FMT " is not valid", uid);

        h = hashmap_get(m->homes_by_uid, UID_TO_PTR(uid));
        if (!h)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "No home for UID " UID_FMT " known", uid);

        /* Note that we don't use bus_home_path() here, but build the path manually, since if we are queried
         * for a UID we should also generate the bus path with a UID, and bus_home_path() uses our more
         * typical bus path by name. */
        if (asprintf(&path, "/org/freedesktop/home1/home/" UID_FMT, h->uid) < 0)
                return -ENOMEM;

        return sd_bus_reply_method_return(
                        message, "ssussso",
                        h->user_name,
                        home_state_to_string(home_get_state(h)),
                        h->record ? (uint32_t) user_record_gid(h->record) : GID_INVALID,
                        h->record ? user_record_real_name(h->record) : NULL,
                        h->record ? user_record_home_directory(h->record) : NULL,
                        h->record ? user_record_shell(h->record) : NULL,
                        path);
}

static int method_list_homes(
                sd_bus_message *message,
                void *userdata,
                sd_bus_error *error) {

        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        Manager *m = ASSERT_PTR(userdata);
        Home *h;
        int r;

        assert(message);

        r = sd_bus_message_new_method_return(message, &reply);
        if (r < 0)
                return r;

        r = sd_bus_message_open_container(reply, 'a', "(susussso)");
        if (r < 0)
                return r;

        HASHMAP_FOREACH(h, m->homes_by_uid) {
                _cleanup_free_ char *path = NULL;

                r = bus_home_path(h, &path);
                if (r < 0)
                        return r;

                r = sd_bus_message_append(
                                reply, "(susussso)",
                                h->user_name,
                                (uint32_t) h->uid,
                                home_state_to_string(home_get_state(h)),
                                h->record ? (uint32_t) user_record_gid(h->record) : GID_INVALID,
                                h->record ? user_record_real_name(h->record) : NULL,
                                h->record ? user_record_home_directory(h->record) : NULL,
                                h->record ? user_record_shell(h->record) : NULL,
                                path);
                if (r < 0)
                        return r;
        }

        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;

        return sd_bus_send(NULL, reply, NULL);
}

static int method_get_user_record_by_name(
                sd_bus_message *message,
                void *userdata,
                sd_bus_error *error) {

        _cleanup_free_ char *json = NULL, *path = NULL;
        Manager *m = ASSERT_PTR(userdata);
        const char *user_name;
        bool incomplete;
        Home *h;
        int r;

        assert(message);

        r = sd_bus_message_read(message, "s", &user_name);
        if (r < 0)
                return r;
        if (!valid_user_group_name(user_name, 0))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "User name %s is not valid", user_name);

        h = hashmap_get(m->homes_by_name, user_name);
        if (!h)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "No home for user %s known", user_name);

        r = bus_home_get_record_json(h, message, &json, &incomplete);
        if (r < 0)
                return r;

        r = bus_home_path(h, &path);
        if (r < 0)
                return r;

        return sd_bus_reply_method_return(
                        message, "sbo",
                        json,
                        incomplete,
                        path);
}

static int method_get_user_record_by_uid(
                sd_bus_message *message,
                void *userdata,
                sd_bus_error *error) {

        _cleanup_free_ char *json = NULL, *path = NULL;
        Manager *m = ASSERT_PTR(userdata);
        bool incomplete;
        uint32_t uid;
        Home *h;
        int r;

        assert(message);

        r = sd_bus_message_read(message, "u", &uid);
        if (r < 0)
                return r;
        if (!uid_is_valid(uid))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "UID " UID_FMT " is not valid", uid);

        h = hashmap_get(m->homes_by_uid, UID_TO_PTR(uid));
        if (!h)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "No home for UID " UID_FMT " known", uid);

        r = bus_home_get_record_json(h, message, &json, &incomplete);
        if (r < 0)
                return r;

        if (asprintf(&path, "/org/freedesktop/home1/home/" UID_FMT, h->uid) < 0)
                return -ENOMEM;

        return sd_bus_reply_method_return(
                        message, "sbo",
                        json,
                        incomplete,
                        path);
}

static int generic_home_method(
                Manager *m,
                sd_bus_message *message,
                sd_bus_message_handler_t handler,
                sd_bus_error *error) {

        const char *user_name;
        Home *h;
        int r;

        r = sd_bus_message_read(message, "s", &user_name);
        if (r < 0)
                return r;

        if (!valid_user_group_name(user_name, 0))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "User name %s is not valid", user_name);

        h = hashmap_get(m->homes_by_name, user_name);
        if (!h)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "No home for user %s known", user_name);

        return handler(message, h, error);
}

static int method_activate_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_activate, error);
}

static int method_deactivate_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_deactivate, error);
}

static int validate_and_allocate_home(Manager *m, UserRecord *hr, Home **ret, sd_bus_error *error) {
        _cleanup_(user_record_unrefp) UserRecord *signed_hr = NULL;
        struct passwd *pw;
        struct group *gr;
        bool signed_locally;
        Home *other;
        int r;

        assert(m);
        assert(hr);
        assert(ret);

        r = user_record_is_supported(hr, error);
        if (r < 0)
                return r;

        other = hashmap_get(m->homes_by_name, hr->user_name);
        if (other)
                return sd_bus_error_setf(error, BUS_ERROR_USER_NAME_EXISTS, "Specified user name %s exists already, refusing.", hr->user_name);

        pw = getpwnam(hr->user_name);
        if (pw)
                return sd_bus_error_setf(error, BUS_ERROR_USER_NAME_EXISTS, "Specified user name %s exists in the NSS user database, refusing.", hr->user_name);

        gr = getgrnam(hr->user_name);
        if (gr)
                return sd_bus_error_setf(error, BUS_ERROR_USER_NAME_EXISTS, "Specified user name %s conflicts with an NSS group by the same name, refusing.", hr->user_name);

        r = manager_verify_user_record(m, hr);
        switch (r) {

        case USER_RECORD_UNSIGNED:
                /* If the record is unsigned, then let's sign it with our own key */
                r = manager_sign_user_record(m, hr, &signed_hr, error);
                if (r < 0)
                        return r;

                hr = signed_hr;
                _fallthrough_;

        case USER_RECORD_SIGNED_EXCLUSIVE:
                signed_locally = true;
                break;

        case USER_RECORD_SIGNED:
        case USER_RECORD_FOREIGN:
                signed_locally = false;
                break;

        case -ENOKEY:
                return sd_bus_error_setf(error, BUS_ERROR_BAD_SIGNATURE, "Specified user record for %s is signed by a key we don't recognize, refusing.", hr->user_name);

        default:
                return sd_bus_error_set_errnof(error, r, "Failed to validate signature for '%s': %m", hr->user_name);
        }

        if (uid_is_valid(hr->uid)) {
                other = hashmap_get(m->homes_by_uid, UID_TO_PTR(hr->uid));
                if (other)
                        return sd_bus_error_setf(error, BUS_ERROR_UID_IN_USE, "Specified UID " UID_FMT " already in use by home %s, refusing.", hr->uid, other->user_name);

                pw = getpwuid(hr->uid);
                if (pw)
                        return sd_bus_error_setf(error, BUS_ERROR_UID_IN_USE, "Specified UID " UID_FMT " already in use by NSS user %s, refusing.", hr->uid, pw->pw_name);

                gr = getgrgid(hr->uid);
                if (gr)
                        return sd_bus_error_setf(error, BUS_ERROR_UID_IN_USE, "Specified UID " UID_FMT " already in use as GID by NSS group %s, refusing.", hr->uid, gr->gr_name);
        } else {
                r = manager_augment_record_with_uid(m, hr);
                if (r < 0)
                        return sd_bus_error_set_errnof(error, r, "Failed to acquire UID for '%s': %m", hr->user_name);
        }

        r = home_new(m, hr, NULL, ret);
        if (r < 0)
                return r;

        (*ret)->signed_locally = signed_locally;
        return r;
}

static int method_register_home(
                sd_bus_message *message,
                void *userdata,
                sd_bus_error *error) {

        _cleanup_(user_record_unrefp) UserRecord *hr = NULL;
        Manager *m = ASSERT_PTR(userdata);
        _cleanup_(home_freep) Home *h = NULL;
        int r;

        assert(message);

        r = bus_message_read_home_record(message, USER_RECORD_LOAD_EMBEDDED|USER_RECORD_PERMISSIVE, &hr, error);
        if (r < 0)
                return r;

        r = bus_verify_polkit_async(
                        message,
                        CAP_SYS_ADMIN,
                        "org.freedesktop.home1.create-home",
                        NULL,
                        true,
                        UID_INVALID,
                        &m->polkit_registry,
                        error);
        if (r < 0)
                return r;
        if (r == 0)
                return 1; /* Will call us back */

        r = validate_and_allocate_home(m, hr, &h, error);
        if (r < 0)
                return r;

        r = home_save_record(h);
        if (r < 0)
                return r;

        TAKE_PTR(h);

        return sd_bus_reply_method_return(message, NULL);
}

static int method_unregister_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_unregister, error);
}

static int method_create_home(
                sd_bus_message *message,
                void *userdata,
                sd_bus_error *error) {

        _cleanup_(user_record_unrefp) UserRecord *hr = NULL;
        Manager *m = ASSERT_PTR(userdata);
        Home *h;
        int r;

        assert(message);

        r = bus_message_read_home_record(message, USER_RECORD_REQUIRE_REGULAR|USER_RECORD_ALLOW_SECRET|USER_RECORD_ALLOW_PRIVILEGED|USER_RECORD_ALLOW_PER_MACHINE|USER_RECORD_ALLOW_SIGNATURE, &hr, error);
        if (r < 0)
                return r;

        r = bus_verify_polkit_async(
                        message,
                        CAP_SYS_ADMIN,
                        "org.freedesktop.home1.create-home",
                        NULL,
                        true,
                        UID_INVALID,
                        &m->polkit_registry,
                        error);
        if (r < 0)
                return r;
        if (r == 0)
                return 1; /* Will call us back */

        r = validate_and_allocate_home(m, hr, &h, error);
        if (r < 0)
                return r;

        r = home_create(h, hr, error);
        if (r < 0)
                goto fail;

        assert(r == 0);
        h->unregister_on_failure = true;
        assert(!h->current_operation);

        r = home_set_current_message(h, message);
        if (r < 0)
                return r;

        return 1;

fail:
        (void) home_unlink_record(h);
        h = home_free(h);
        return r;
}

static int method_realize_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_realize, error);
}

static int method_remove_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_remove, error);
}

static int method_fixate_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_fixate, error);
}

static int method_authenticate_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_authenticate, error);
}

static int method_update_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_(user_record_unrefp) UserRecord *hr = NULL;
        Manager *m = ASSERT_PTR(userdata);
        Home *h;
        int r;

        assert(message);

        r = bus_message_read_home_record(message, USER_RECORD_REQUIRE_REGULAR|USER_RECORD_ALLOW_SECRET|USER_RECORD_ALLOW_PRIVILEGED|USER_RECORD_ALLOW_PER_MACHINE|USER_RECORD_ALLOW_SIGNATURE|USER_RECORD_PERMISSIVE, &hr, error);
        if (r < 0)
                return r;

        assert(hr->user_name);

        h = hashmap_get(m->homes_by_name, hr->user_name);
        if (!h)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "No home for user %s known", hr->user_name);

        return bus_home_method_update_record(h, message, hr, error);
}

static int method_resize_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_resize, error);
}

static int method_change_password_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_change_password, error);
}

static int method_lock_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_lock, error);
}

static int method_unlock_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_unlock, error);
}

static int method_acquire_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_acquire, error);
}

static int method_ref_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_ref, error);
}

static int method_release_home(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return generic_home_method(userdata, message, bus_home_method_release, error);
}

static int method_lock_all_homes(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_(operation_unrefp) Operation *o = NULL;
        bool waiting = false;
        Manager *m = ASSERT_PTR(userdata);
        Home *h;
        int r;

        /* This is called from logind when we are preparing for system suspend. We enqueue a lock operation
         * for every suitable home we have and only when all of them completed we send a reply indicating
         * completion. */

        HASHMAP_FOREACH(h, m->homes_by_name) {

                /* Automatically suspend all homes that have at least one client referencing it that asked
                 * for "please suspend", and no client that asked for "please do not suspend". */
                if (h->ref_event_source_dont_suspend ||
                    !h->ref_event_source_please_suspend)
                        continue;

                if (!o) {
                        o = operation_new(OPERATION_LOCK_ALL, message);
                        if (!o)
                                return -ENOMEM;
                }

                log_info("Automatically locking home of user %s.", h->user_name);

                r = home_schedule_operation(h, o, error);
                if (r < 0)
                        return r;

                waiting = true;
        }

        if (waiting) /* At least one lock operation was enqeued, let's leave here without a reply: it will
                      * be sent as soon as the last of the lock operations completed. */
                return 1;

        return sd_bus_reply_method_return(message, NULL);
}

static int method_deactivate_all_homes(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_(operation_unrefp) Operation *o = NULL;
        bool waiting = false;
        Manager *m = ASSERT_PTR(userdata);
        Home *h;
        int r;

        /* This is called from systemd-homed-activate.service's ExecStop= command to ensure that all home
         * directories are shutdown before the system goes down. Note that we don't do this from
         * systemd-homed.service itself since we want to allow restarting of it without tearing down all home
         * directories. */

        HASHMAP_FOREACH(h, m->homes_by_name) {

                if (!o) {
                        o = operation_new(OPERATION_DEACTIVATE_ALL, message);
                        if (!o)
                                return -ENOMEM;
                }

                log_info("Automatically deactivating home of user %s.", h->user_name);

                r = home_schedule_operation(h, o, error);
                if (r < 0)
                        return r;

                waiting = true;
        }

        if (waiting) /* At least one lock operation was enqeued, let's leave here without a reply: it will be
                      * sent as soon as the last of the deactivation operations completed. */
                return 1;

        return sd_bus_reply_method_return(message, NULL);
}

static int method_rebalance(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        Manager *m = ASSERT_PTR(userdata);
        int r;

        r = manager_schedule_rebalance(m, /* immediately= */ true);
        if (r == 0)
                return sd_bus_reply_method_errorf(message, BUS_ERROR_REBALANCE_NOT_NEEDED, "No home directories need rebalancing.");
        if (r < 0)
                return r;

        /* Keep a reference to this message, so that we can reply to it once we are done */
        r = set_ensure_put(&m->rebalance_queued_method_calls, &bus_message_hash_ops, message);
        if (r < 0)
                return log_error_errno(r, "Failed to track rebalance bus message: %m");

        sd_bus_message_ref(message);
        return 1;
}

static const sd_bus_vtable manager_vtable[] = {
        SD_BUS_VTABLE_START(0),

        SD_BUS_PROPERTY("AutoLogin", "a(sso)", property_get_auto_login, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),

        SD_BUS_METHOD_WITH_ARGS("GetHomeByName",
                                SD_BUS_ARGS("s", user_name),
                                SD_BUS_RESULT("u", uid,
                                              "s", home_state,
                                              "u", gid,
                                              "s", real_name,
                                              "s", home_directory,
                                              "s", shell,
                                              "o", bus_path),
                                method_get_home_by_name,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetHomeByUID",
                                SD_BUS_ARGS("u", uid),
                                SD_BUS_RESULT("s", user_name,
                                              "s", home_state,
                                              "u", gid,
                                              "s", real_name,
                                              "s", home_directory,
                                              "s", shell,
                                              "o", bus_path),
                                method_get_home_by_uid,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetUserRecordByName",
                                SD_BUS_ARGS("s", user_name),
                                SD_BUS_RESULT("s", user_record, "b", incomplete, "o", bus_path),
                                method_get_user_record_by_name,
                                SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_SENSITIVE),
        SD_BUS_METHOD_WITH_ARGS("GetUserRecordByUID",
                                SD_BUS_ARGS("u", uid),
                                SD_BUS_RESULT("s", user_record, "b", incomplete, "o", bus_path),
                                method_get_user_record_by_uid,
                                SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_SENSITIVE),
        SD_BUS_METHOD_WITH_ARGS("ListHomes",
                                SD_BUS_NO_ARGS,
                                SD_BUS_RESULT("a(susussso)", home_areas),
                                method_list_homes,
                                SD_BUS_VTABLE_UNPRIVILEGED),

        /* The following methods directly execute an operation on a home area, without ref-counting, queueing
         * or anything, and are accessible through homectl. */
        SD_BUS_METHOD_WITH_ARGS("ActivateHome",
                                SD_BUS_ARGS("s", user_name, "s", secret),
                                SD_BUS_NO_RESULT,
                                method_activate_home,
                                SD_BUS_VTABLE_SENSITIVE),
        SD_BUS_METHOD_WITH_ARGS("DeactivateHome",
                                SD_BUS_ARGS("s", user_name),
                                SD_BUS_NO_RESULT,
                                method_deactivate_home,
                                0),

        /* Add the JSON record to homed, but don't create actual $HOME */
        SD_BUS_METHOD_WITH_ARGS("RegisterHome",
                                SD_BUS_ARGS("s", user_record),
                                SD_BUS_NO_RESULT,
                                method_register_home,
                                SD_BUS_VTABLE_UNPRIVILEGED),

        /* Remove the JSON record from homed, but don't remove actual $HOME  */
        SD_BUS_METHOD_WITH_ARGS("UnregisterHome",
                                SD_BUS_ARGS("s", user_name),
                                SD_BUS_NO_RESULT,
                                method_unregister_home,
                                SD_BUS_VTABLE_UNPRIVILEGED),

        /* Add JSON record, and create $HOME for it */
        SD_BUS_METHOD_WITH_ARGS("CreateHome",
                                SD_BUS_ARGS("s", user_record),
                                SD_BUS_NO_RESULT,
                                method_create_home,
                                SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_SENSITIVE),

        /* Create $HOME for already registered JSON entry */
        SD_BUS_METHOD_WITH_ARGS("RealizeHome",
                                SD_BUS_ARGS("s", user_name, "s", secret),
                                SD_BUS_NO_RESULT,
                                method_realize_home,
                                SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_SENSITIVE),

        /* Remove the JSON record and remove $HOME */
        SD_BUS_METHOD_WITH_ARGS("RemoveHome",
                                SD_BUS_ARGS("s", user_name),
                                SD_BUS_NO_RESULT,
                                method_remove_home,
                                SD_BUS_VTABLE_UNPRIVILEGED),

        /* Investigate $HOME and propagate contained JSON record into our database */
        SD_BUS_METHOD_WITH_ARGS("FixateHome",
                                SD_BUS_ARGS("s", user_name, "s", secret),
                                SD_BUS_NO_RESULT,
                                method_fixate_home,
                                SD_BUS_VTABLE_SENSITIVE),

        /* Just check credentials */
        SD_BUS_METHOD_WITH_ARGS("AuthenticateHome",
                                SD_BUS_ARGS("s", user_name, "s", secret),
                                SD_BUS_NO_RESULT,
                                method_authenticate_home,
                                SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_SENSITIVE),

        /* Update the JSON record of existing user */
        SD_BUS_METHOD_WITH_ARGS("UpdateHome",
                                SD_BUS_ARGS("s", user_record),
                                SD_BUS_NO_RESULT,
                                method_update_home,
                                SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_SENSITIVE),

        SD_BUS_METHOD_WITH_ARGS("ResizeHome",
                                SD_BUS_ARGS("s", user_name, "t", size, "s", secret),
                                SD_BUS_NO_RESULT,
                                method_resize_home,
                                SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_SENSITIVE),

        SD_BUS_METHOD_WITH_ARGS("ChangePasswordHome",
                                SD_BUS_ARGS("s", user_name, "s", new_secret, "s", old_secret),
                                SD_BUS_NO_RESULT,
                                method_change_password_home,
                                SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_SENSITIVE),

        /* Prepare active home for system suspend: flush out passwords, suspend access */
        SD_BUS_METHOD_WITH_ARGS("LockHome",
                                SD_BUS_ARGS("s", user_name),
                                SD_BUS_NO_RESULT,
                                method_lock_home,
                                0),

        /* Make $HOME usable after system resume again */
        SD_BUS_METHOD_WITH_ARGS("UnlockHome",
                                SD_BUS_ARGS("s", user_name, "s", secret),
                                SD_BUS_NO_RESULT,
                                method_unlock_home,
                                SD_BUS_VTABLE_SENSITIVE),

        /* The following methods implement ref-counted activation, and are what the PAM module and "homectl
         * with" use. In contrast to the methods above which fail if an operation is already being executed
         * on a home directory, these ones will queue the request, and are thus more reliable. Moreover,
         * they are a bit smarter: AcquireHome() will fixate, activate, unlock, or authenticate depending on
         * the state of the home area, so that the end result is always the same (i.e. the home directory is
         * accessible), and we always validate the specified passwords. RefHome() will not authenticate, and
         * thus only works if the home area is already active. */
        SD_BUS_METHOD_WITH_ARGS("AcquireHome",
                                SD_BUS_ARGS("s", user_name, "s", secret, "b", please_suspend),
                                SD_BUS_RESULT("h", send_fd),
                                method_acquire_home,
                                SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_SENSITIVE),
        SD_BUS_METHOD_WITH_ARGS("RefHome",
                                SD_BUS_ARGS("s", user_name, "b", please_suspend),
                                SD_BUS_RESULT("h", send_fd),
                                method_ref_home,
                                0),
        SD_BUS_METHOD_WITH_ARGS("ReleaseHome",
                                SD_BUS_ARGS("s", user_name),
                                SD_BUS_NO_RESULT,
                                method_release_home,
                                0),

        /* An operation that acts on all homes that allow it */
        SD_BUS_METHOD("LockAllHomes", NULL, NULL, method_lock_all_homes, 0),
        SD_BUS_METHOD("DeactivateAllHomes", NULL, NULL, method_deactivate_all_homes, 0),
        SD_BUS_METHOD("Rebalance", NULL, NULL, method_rebalance, 0),

        SD_BUS_VTABLE_END
};

const BusObjectImplementation manager_object = {
        "/org/freedesktop/home1",
        "org.freedesktop.home1.Manager",
        .vtables = BUS_VTABLES(manager_vtable),
        .children = BUS_IMPLEMENTATIONS(&home_object),
};

static int on_deferred_auto_login(sd_event_source *s, void *userdata) {
        Manager *m = ASSERT_PTR(userdata);
        int r;

        m->deferred_auto_login_event_source = sd_event_source_disable_unref(m->deferred_auto_login_event_source);

        r = sd_bus_emit_properties_changed(
                        m->bus,
                        "/org/freedesktop/home1",
                        "org.freedesktop.home1.Manager",
                        "AutoLogin", NULL);
        if (r < 0)
                log_warning_errno(r, "Failed to send AutoLogin property change event, ignoring: %m");

        return 0;
}

int bus_manager_emit_auto_login_changed(Manager *m) {
        int r;
        assert(m);

        if (m->deferred_auto_login_event_source)
                return 0;

        if (!m->event)
                return 0;

        if (IN_SET(sd_event_get_state(m->event), SD_EVENT_FINISHED, SD_EVENT_EXITING))
                return 0;

        r = sd_event_add_defer(m->event, &m->deferred_auto_login_event_source, on_deferred_auto_login, m);
        if (r < 0)
                return log_error_errno(r, "Failed to allocate auto login event source: %m");

        r = sd_event_source_set_priority(m->deferred_auto_login_event_source, SD_EVENT_PRIORITY_IDLE+10);
        if (r < 0)
                log_warning_errno(r, "Failed to tweak priority of event source, ignoring: %m");

        (void) sd_event_source_set_description(m->deferred_auto_login_event_source, "deferred-auto-login");
        return 1;
}