]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/core-varlink.c
4bb262bd93d24cfcd426ff7cebb9f405a80049d6
[thirdparty/systemd.git] / src / core / core-varlink.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "core-varlink.h"
4 #include "mkdir.h"
5 #include "user-util.h"
6 #include "varlink.h"
7
8 typedef struct LookupParameters {
9 const char *user_name;
10 const char *group_name;
11 union {
12 uid_t uid;
13 gid_t gid;
14 };
15 const char *service;
16 } LookupParameters;
17
18 static int build_user_json(const char *user_name, uid_t uid, JsonVariant **ret) {
19 assert(user_name);
20 assert(uid_is_valid(uid));
21 assert(ret);
22
23 return json_build(ret, JSON_BUILD_OBJECT(
24 JSON_BUILD_PAIR("record", JSON_BUILD_OBJECT(
25 JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(user_name)),
26 JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(uid)),
27 JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(uid)),
28 JSON_BUILD_PAIR("realName", JSON_BUILD_STRING("Dynamic User")),
29 JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_STRING("/")),
30 JSON_BUILD_PAIR("shell", JSON_BUILD_STRING(NOLOGIN)),
31 JSON_BUILD_PAIR("locked", JSON_BUILD_BOOLEAN(true)),
32 JSON_BUILD_PAIR("service", JSON_BUILD_STRING("io.systemd.DynamicUser")),
33 JSON_BUILD_PAIR("disposition", JSON_BUILD_STRING("dynamic"))))));
34 }
35
36 static bool user_match_lookup_parameters(LookupParameters *p, const char *name, uid_t uid) {
37 assert(p);
38
39 if (p->user_name && !streq(name, p->user_name))
40 return false;
41
42 if (uid_is_valid(p->uid) && uid != p->uid)
43 return false;
44
45 return true;
46 }
47
48 static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
49
50 static const JsonDispatch dispatch_table[] = {
51 { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 },
52 { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), JSON_SAFE },
53 { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 },
54 {}
55 };
56
57 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
58 LookupParameters p = {
59 .uid = UID_INVALID,
60 };
61 _cleanup_free_ char *found_name = NULL;
62 uid_t found_uid = UID_INVALID, uid;
63 Manager *m = userdata;
64 const char *un;
65 int r;
66
67 assert(parameters);
68 assert(m);
69
70 r = json_dispatch(parameters, dispatch_table, NULL, 0, &p);
71 if (r < 0)
72 return r;
73
74 if (!streq_ptr(p.service, "io.systemd.DynamicUser"))
75 return varlink_error(link, "io.systemd.UserDatabase.BadService", NULL);
76
77 if (uid_is_valid(p.uid))
78 r = dynamic_user_lookup_uid(m, p.uid, &found_name);
79 else if (p.user_name)
80 r = dynamic_user_lookup_name(m, p.user_name, &found_uid);
81 else {
82 DynamicUser *d;
83
84 HASHMAP_FOREACH(d, m->dynamic_users) {
85 r = dynamic_user_current(d, &uid);
86 if (r == -EAGAIN) /* not realized yet? */
87 continue;
88 if (r < 0)
89 return r;
90
91 if (!user_match_lookup_parameters(&p, d->name, uid))
92 continue;
93
94 if (v) {
95 r = varlink_notify(link, v);
96 if (r < 0)
97 return r;
98
99 v = json_variant_unref(v);
100 }
101
102 r = build_user_json(d->name, uid, &v);
103 if (r < 0)
104 return r;
105 }
106
107 if (!v)
108 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
109
110 return varlink_reply(link, v);
111 }
112 if (r == -ESRCH)
113 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
114 if (r < 0)
115 return r;
116
117 uid = uid_is_valid(found_uid) ? found_uid : p.uid;
118 un = found_name ?: p.user_name;
119
120 if (!user_match_lookup_parameters(&p, un, uid))
121 return varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL);
122
123 r = build_user_json(un, uid, &v);
124 if (r < 0)
125 return r;
126
127 return varlink_reply(link, v);
128 }
129
130 static int build_group_json(const char *group_name, gid_t gid, JsonVariant **ret) {
131 assert(group_name);
132 assert(gid_is_valid(gid));
133 assert(ret);
134
135 return json_build(ret, JSON_BUILD_OBJECT(
136 JSON_BUILD_PAIR("record", JSON_BUILD_OBJECT(
137 JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(group_name)),
138 JSON_BUILD_PAIR("description", JSON_BUILD_STRING("Dynamic Group")),
139 JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(gid)),
140 JSON_BUILD_PAIR("service", JSON_BUILD_STRING("io.systemd.DynamicUser")),
141 JSON_BUILD_PAIR("disposition", JSON_BUILD_STRING("dynamic"))))));
142 }
143
144 static bool group_match_lookup_parameters(LookupParameters *p, const char *name, gid_t gid) {
145 assert(p);
146
147 if (p->group_name && !streq(name, p->group_name))
148 return false;
149
150 if (gid_is_valid(p->gid) && gid != p->gid)
151 return false;
152
153 return true;
154 }
155
156 static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
157
158 static const JsonDispatch dispatch_table[] = {
159 { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 },
160 { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), JSON_SAFE },
161 { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 },
162 {}
163 };
164
165 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
166 LookupParameters p = {
167 .gid = GID_INVALID,
168 };
169 _cleanup_free_ char *found_name = NULL;
170 uid_t found_gid = GID_INVALID, gid;
171 Manager *m = userdata;
172 const char *gn;
173 int r;
174
175 assert(parameters);
176 assert(m);
177
178 r = json_dispatch(parameters, dispatch_table, NULL, 0, &p);
179 if (r < 0)
180 return r;
181
182 if (!streq_ptr(p.service, "io.systemd.DynamicUser"))
183 return varlink_error(link, "io.systemd.UserDatabase.BadService", NULL);
184
185 if (gid_is_valid(p.gid))
186 r = dynamic_user_lookup_uid(m, (uid_t) p.gid, &found_name);
187 else if (p.group_name)
188 r = dynamic_user_lookup_name(m, p.group_name, (uid_t*) &found_gid);
189 else {
190 DynamicUser *d;
191
192 HASHMAP_FOREACH(d, m->dynamic_users) {
193 uid_t uid;
194
195 r = dynamic_user_current(d, &uid);
196 if (r == -EAGAIN)
197 continue;
198 if (r < 0)
199 return r;
200
201 if (!group_match_lookup_parameters(&p, d->name, (gid_t) uid))
202 continue;
203
204 if (v) {
205 r = varlink_notify(link, v);
206 if (r < 0)
207 return r;
208
209 v = json_variant_unref(v);
210 }
211
212 r = build_group_json(d->name, (gid_t) uid, &v);
213 if (r < 0)
214 return r;
215 }
216
217 if (!v)
218 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
219
220 return varlink_reply(link, v);
221 }
222 if (r == -ESRCH)
223 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
224 if (r < 0)
225 return r;
226
227 gid = gid_is_valid(found_gid) ? found_gid : p.gid;
228 gn = found_name ?: p.group_name;
229
230 if (!group_match_lookup_parameters(&p, gn, gid))
231 return varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL);
232
233 r = build_group_json(gn, gid, &v);
234 if (r < 0)
235 return r;
236
237 return varlink_reply(link, v);
238 }
239
240 static int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
241
242 static const JsonDispatch dispatch_table[] = {
243 { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), JSON_SAFE },
244 { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), JSON_SAFE },
245 { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 },
246 {}
247 };
248
249 LookupParameters p = {};
250 int r;
251
252 assert(parameters);
253
254 r = json_dispatch(parameters, dispatch_table, NULL, 0, &p);
255 if (r < 0)
256 return r;
257
258 if (!streq_ptr(p.service, "io.systemd.DynamicUser"))
259 return varlink_error(link, "io.systemd.UserDatabase.BadService", NULL);
260
261 /* We don't support auxiliary groups with dynamic users. */
262 return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL);
263 }
264
265 int manager_varlink_init(Manager *m) {
266 _cleanup_(varlink_server_unrefp) VarlinkServer *s = NULL;
267 int r;
268
269 assert(m);
270
271 if (m->varlink_server)
272 return 0;
273
274 if (!MANAGER_IS_SYSTEM(m))
275 return 0;
276
277 r = varlink_server_new(&s, VARLINK_SERVER_ACCOUNT_UID);
278 if (r < 0)
279 return log_error_errno(r, "Failed to allocate varlink server object: %m");
280
281 varlink_server_set_userdata(s, m);
282
283 r = varlink_server_bind_method_many(
284 s,
285 "io.systemd.UserDatabase.GetUserRecord", vl_method_get_user_record,
286 "io.systemd.UserDatabase.GetGroupRecord", vl_method_get_group_record,
287 "io.systemd.UserDatabase.GetMemberships", vl_method_get_memberships);
288 if (r < 0)
289 return log_error_errno(r, "Failed to register varlink methods: %m");
290
291 if (!MANAGER_IS_TEST_RUN(m)) {
292 (void) mkdir_p_label("/run/systemd/userdb", 0755);
293
294 r = varlink_server_listen_address(s, "/run/systemd/userdb/io.systemd.DynamicUser", 0666);
295 if (r < 0)
296 return log_error_errno(r, "Failed to bind to varlink socket: %m");
297 }
298
299 r = varlink_server_attach_event(s, m->event, SD_EVENT_PRIORITY_NORMAL);
300 if (r < 0)
301 return log_error_errno(r, "Failed to attach varlink connection to event loop: %m");
302
303 m->varlink_server = TAKE_PTR(s);
304 return 0;
305 }
306
307 void manager_varlink_done(Manager *m) {
308 assert(m);
309
310 m->varlink_server = varlink_server_unref(m->varlink_server);
311 }