1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <openssl/evp.h>
5 
6 #include "sd-bus.h"
7 #include "sd-device.h"
8 #include "sd-event.h"
9 
10 typedef struct Manager Manager;
11 
12 #include "hashmap.h"
13 #include "homed-home.h"
14 #include "varlink.h"
15 
16 /* The LUKS free disk space rebalancing logic goes through this state machine */
17 typedef enum RebalanceState {
18         REBALANCE_OFF,       /* No rebalancing enabled */
19         REBALANCE_IDLE,      /* Rebalancing enabled, but currently nothing scheduled */
20         REBALANCE_WAITING,   /* Rebalancing has been requested for a later point in time */
21         REBALANCE_PENDING,   /* Rebalancing has been requested and will be executed ASAP */
22         REBALANCE_SHRINKING, /* Rebalancing ongoing, and we are running all shrinking operations */
23         REBALANCE_GROWING,   /* Rebalancing ongoign, and we are running all growing operations */
24         _REBALANCE_STATE_MAX,
25         _REBALANCE_STATE_INVALID = -1,
26 } RebalanceState;
27 
28 struct Manager {
29         sd_event *event;
30         sd_bus *bus;
31 
32         Hashmap *polkit_registry;
33 
34         Hashmap *homes_by_uid;
35         Hashmap *homes_by_name;
36         Hashmap *homes_by_worker_pid;
37         Hashmap *homes_by_sysfs;
38 
39         bool scan_slash_home;
40         UserStorage default_storage;
41         char *default_file_system_type;
42 
43         sd_event_source *inotify_event_source;
44 
45         /* An event source we receive sd_notify() messages from our worker from */
46         sd_event_source *notify_socket_event_source;
47 
48         sd_device_monitor *device_monitor;
49 
50         sd_event_source *deferred_rescan_event_source;
51         sd_event_source *deferred_gc_event_source;
52         sd_event_source *deferred_auto_login_event_source;
53 
54         sd_event_source *rebalance_event_source;
55 
56         Home *gc_focus;
57 
58         VarlinkServer *varlink_server;
59         char *userdb_service;
60 
61         EVP_PKEY *private_key; /* actually a pair of private and public key */
62         Hashmap *public_keys; /* key name [char*] → publick key [EVP_PKEY*] */
63 
64         RebalanceState rebalance_state;
65         usec_t rebalance_interval_usec;
66 
67         /* In order to allow synchronous rebalance requests via bus calls we maintain two pools of bus
68          * messages: 'rebalance_pending_methods' are the method calls we are currently operating on and
69          * running a rebalancing operation for. 'rebalance_queued_method_calls' are the method calls that
70          * have been queued since then and that we'll operate on once we complete the current run. */
71         Set *rebalance_pending_method_calls, *rebalance_queued_method_calls;
72 };
73 
74 int manager_new(Manager **ret);
75 Manager* manager_free(Manager *m);
76 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
77 
78 int manager_startup(Manager *m);
79 
80 int manager_augment_record_with_uid(Manager *m, UserRecord *hr);
81 
82 int manager_enqueue_rescan(Manager *m);
83 int manager_enqueue_gc(Manager *m, Home *focus);
84 
85 int manager_schedule_rebalance(Manager *m, bool immediately);
86 int manager_reschedule_rebalance(Manager *m);
87 
88 int manager_verify_user_record(Manager *m, UserRecord *hr);
89 
90 int manager_acquire_key_pair(Manager *m);
91 int manager_sign_user_record(Manager *m, UserRecord *u, UserRecord **ret, sd_bus_error *error);
92 
93 int bus_manager_emit_auto_login_changed(Manager *m);
94