]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machined-core.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / machine / machined-core.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "machined.h"
4 #include "nscd-flush.h"
5 #include "strv.h"
6 #include "user-util.h"
7
8 static int on_nscd_cache_flush_event(sd_event_source *s, void *userdata) {
9 /* Let's ask glibc's nscd daemon to flush its caches. We request this for the three database machines may show
10 * up in: the hosts database (for resolvable machine names) and the user and group databases (for the user ns
11 * ranges). */
12
13 (void) nscd_flush_cache(STRV_MAKE("passwd", "group", "hosts"));
14 return 0;
15 }
16
17 int manager_enqueue_nscd_cache_flush(Manager *m) {
18 int r;
19
20 assert(m);
21
22 if (!m->nscd_cache_flush_event) {
23 r = sd_event_add_defer(m->event, &m->nscd_cache_flush_event, on_nscd_cache_flush_event, m);
24 if (r < 0)
25 return log_error_errno(r, "Failed to allocate NSCD cache flush event: %m");
26
27 sd_event_source_set_description(m->nscd_cache_flush_event, "nscd-cache-flush");
28 }
29
30 r = sd_event_source_set_enabled(m->nscd_cache_flush_event, SD_EVENT_ONESHOT);
31 if (r < 0) {
32 m->nscd_cache_flush_event = sd_event_source_unref(m->nscd_cache_flush_event);
33 return log_error_errno(r, "Failed to enable NSCD cache flush event: %m");
34 }
35
36 return 0;
37 }
38
39 int manager_find_machine_for_uid(Manager *m, uid_t uid, Machine **ret_machine, uid_t *ret_internal_uid) {
40 Machine *machine;
41 int r;
42
43 assert(m);
44 assert(uid_is_valid(uid));
45
46 /* Finds the machine for the specified host UID and returns it along with the UID translated into the
47 * internal UID inside the machine */
48
49 HASHMAP_FOREACH(machine, m->machines) {
50 uid_t converted;
51
52 r = machine_owns_uid(machine, uid, &converted);
53 if (r < 0)
54 return r;
55 if (r) {
56 if (ret_machine)
57 *ret_machine = machine;
58
59 if (ret_internal_uid)
60 *ret_internal_uid = converted;
61
62 return true;
63 }
64 }
65
66 if (ret_machine)
67 *ret_machine = NULL;
68 if (ret_internal_uid)
69 *ret_internal_uid = UID_INVALID;
70
71 return false;
72 }
73
74 int manager_find_machine_for_gid(Manager *m, gid_t gid, Machine **ret_machine, gid_t *ret_internal_gid) {
75 Machine *machine;
76 int r;
77
78 assert(m);
79 assert(gid_is_valid(gid));
80
81 HASHMAP_FOREACH(machine, m->machines) {
82 gid_t converted;
83
84 r = machine_owns_gid(machine, gid, &converted);
85 if (r < 0)
86 return r;
87 if (r) {
88 if (ret_machine)
89 *ret_machine = machine;
90
91 if (ret_internal_gid)
92 *ret_internal_gid = converted;
93
94 return true;
95 }
96 }
97
98 if (ret_machine)
99 *ret_machine = NULL;
100 if (ret_internal_gid)
101 *ret_internal_gid = GID_INVALID;
102
103 return false;
104 }