]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machined-core.c
machined: refactor UID/GID machine translation
[thirdparty/systemd.git] / src / machine / machined-core.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 Iterator i;
42 int r;
43
44 assert(m);
45 assert(uid_is_valid(uid));
46
47 /* Finds the machine for the specified host UID and returns it along with the UID translated into the
48 * internal UID inside the machine */
49
50 HASHMAP_FOREACH(machine, m->machines, i) {
51 uid_t converted;
52
53 r = machine_owns_uid(machine, uid, &converted);
54 if (r < 0)
55 return r;
56 if (r) {
57 if (ret_machine)
58 *ret_machine = machine;
59
60 if (ret_internal_uid)
61 *ret_internal_uid = converted;
62
63 return true;
64 }
65 }
66
67 if (ret_machine)
68 *ret_machine = NULL;
69 if (ret_internal_uid)
70 *ret_internal_uid = UID_INVALID;
71
72 return false;
73 }
74
75 int manager_find_machine_for_gid(Manager *m, gid_t gid, Machine **ret_machine, gid_t *ret_internal_gid) {
76 Machine *machine;
77 Iterator i;
78 int r;
79
80 assert(m);
81 assert(gid_is_valid(gid));
82
83 HASHMAP_FOREACH(machine, m->machines, i) {
84 gid_t converted;
85
86 r = machine_owns_gid(machine, gid, &converted);
87 if (r < 0)
88 return r;
89 if (r) {
90 if (ret_machine)
91 *ret_machine = machine;
92
93 if (ret_internal_gid)
94 *ret_internal_gid = converted;
95
96 return true;
97 }
98 }
99
100 if (ret_machine)
101 *ret_machine = NULL;
102 if (ret_internal_gid)
103 *ret_internal_gid = GID_INVALID;
104
105 return false;
106 }