Debian Codegraph @

src/shared/group-record.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "bitfield.h"
5 #include "group-record.h"
6 #include "json-util.h"
7 #include "log.h"
8 #include "string-util.h"
9 #include "strv.h"
10 #include "uid-classification.h"
11 #include "user-util.h"
12
13 GroupRecord* group_record_new(void) {
14 GroupRecord *h;
15
16 h = new(GroupRecord, 1);
17 if (!h)
18 return NULL;
19
20 *h = (GroupRecord) {
21 .n_ref = 1,
22 .disposition = _USER_DISPOSITION_INVALID,
23 .last_change_usec = UINT64_MAX,
24 .gid = GID_INVALID,
25 };
26
27 return h;
28 }
29
30 static GroupRecord *group_record_free(GroupRecord *g) {
31 if (!g)
32 return NULL;
33
34 free(g->group_name);
35 free(g->realm);
36 free(g->group_name_and_realm_auto);
37 free(g->description);
38
39 strv_free(g->members);
40 free(g->service);
41 strv_free(g->administrators);
42 strv_free_erase(g->hashed_password);
43
44 sd_json_variant_unref(g->json);
45
46 return mfree(g);
47 }
48
49 DEFINE_TRIVIAL_REF_UNREF_FUNC(GroupRecord, group_record, group_record_free);
50
51 static int dispatch_privileged(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
52
53 static const sd_json_dispatch_field privileged_dispatch_table[] = {
54 { "hashedPassword", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(GroupRecord, hashed_password), SD_JSON_STRICT },
55 {},
56 };
57
58 return sd_json_dispatch(variant, privileged_dispatch_table, flags, userdata);
59 }
60
61 static int dispatch_binding(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
62
63 static const sd_json_dispatch_field binding_dispatch_table[] = {
64 { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(GroupRecord, gid), 0 },
65 {},
66 };
67
68 sd_json_variant *m;
69 sd_id128_t mid;
70 int r;
71
72 if (!variant)
73 return 0;
74
75 if (!sd_json_variant_is_object(variant))
76 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name));
77
78 r = sd_id128_get_machine(&mid);
79 if (r < 0)
80 return json_log(variant, flags, r, "Failed to determine machine ID: %m");
81
82 m = sd_json_variant_by_key(variant, SD_ID128_TO_STRING(mid));
83 if (!m)
84 return 0;
85
86 return sd_json_dispatch(m, binding_dispatch_table, flags, userdata);
87 }
88
89 static int dispatch_per_machine(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
90
91 static const sd_json_dispatch_field per_machine_dispatch_table[] = {
92 { "matchMachineId", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 },
93 { "matchHostname", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 },
94 { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(GroupRecord, gid), 0 },
95 { "members", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, members), SD_JSON_RELAX },
96 { "administrators", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, administrators), SD_JSON_RELAX },
97 {},
98 };
99
100 sd_json_variant *e;
101 int r;
102
103 if (!variant)
104 return 0;
105
106 if (!sd_json_variant_is_array(variant))
107 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name));
108
109 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
110 if (!sd_json_variant_is_object(e))
111 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
112
113 r = per_machine_match(e, flags);
114 if (r < 0)
115 return r;
116 if (r == 0)
117 continue;
118
119 r = sd_json_dispatch(e, per_machine_dispatch_table, flags, userdata);
120 if (r < 0)
121 return r;
122 }
123
124 return 0;
125 }
126
127 static int dispatch_status(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
128
129 static const sd_json_dispatch_field status_dispatch_table[] = {
130 { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(GroupRecord, service), SD_JSON_STRICT },
131 {},
132 };
133
134 sd_json_variant *m;
135 sd_id128_t mid;
136 int r;
137
138 if (!variant)
139 return 0;
140
141 if (!sd_json_variant_is_object(variant))
142 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name));
143
144 r = sd_id128_get_machine(&mid);
145 if (r < 0)
146 return json_log(variant, flags, r, "Failed to determine machine ID: %m");
147
148 m = sd_json_variant_by_key(variant, SD_ID128_TO_STRING(mid));
149 if (!m)
150 return 0;
151
152 return sd_json_dispatch(m, status_dispatch_table, flags, userdata);
153 }
154
155 static int group_record_augment(GroupRecord *h, sd_json_dispatch_flags_t json_flags) {
156 assert(h);
157
158 if (!FLAGS_SET(h->mask, USER_RECORD_REGULAR))
159 return 0;
160
161 assert(h->group_name);
162
163 if (!h->group_name_and_realm_auto && h->realm) {
164 h->group_name_and_realm_auto = strjoin(h->group_name, "@", h->realm);
165 if (!h->group_name_and_realm_auto)
166 return json_log_oom(h->json, json_flags);
167 }
168
169 return 0;
170 }
171
172 int group_record_load(
173 GroupRecord *h,
174 sd_json_variant *v,
175 UserRecordLoadFlags load_flags) {
176
177 static const sd_json_dispatch_field group_dispatch_table[] = {
178 { "groupName", SD_JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(GroupRecord, group_name), SD_JSON_RELAX },
179 { "realm", SD_JSON_VARIANT_STRING, json_dispatch_realm, offsetof(GroupRecord, realm), 0 },
180 { "uuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(GroupRecord, uuid), 0 },
181 { "description", SD_JSON_VARIANT_STRING, json_dispatch_gecos, offsetof(GroupRecord, description), 0 },
182 { "disposition", SD_JSON_VARIANT_STRING, json_dispatch_user_disposition, offsetof(GroupRecord, disposition), 0 },
183 { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(GroupRecord, service), SD_JSON_STRICT },
184 { "lastChangeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(GroupRecord, last_change_usec), 0 },
185 { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(GroupRecord, gid), 0 },
186 { "members", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, members), SD_JSON_RELAX },
187 { "administrators", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, administrators), SD_JSON_RELAX },
188
189 { "privileged", SD_JSON_VARIANT_OBJECT, dispatch_privileged, 0, 0 },
190
191 /* Not defined for now, for groups, but let's at least generate sensible errors about it */
192 { "secret", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_unsupported, 0, 0 },
193
194 /* Ignore the perMachine, binding and status stuff here, and process it later, so that it overrides whatever is set above */
195 { "perMachine", SD_JSON_VARIANT_ARRAY, NULL, 0, 0 },
196 { "binding", SD_JSON_VARIANT_OBJECT, NULL, 0, 0 },
197 { "status", SD_JSON_VARIANT_OBJECT, NULL, 0, 0 },
198
199 /* Ignore 'signature', we check it with explicit accessors instead */
200 { "signature", SD_JSON_VARIANT_ARRAY, NULL, 0, 0 },
201 {},
202 };
203
204 sd_json_dispatch_flags_t json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags);
205 int r;
206
207 assert(h);
208 assert(!h->json);
209
210 /* Note that this call will leave a half-initialized record around on failure! */
211
212 if ((USER_RECORD_REQUIRE_MASK(load_flags) & (USER_RECORD_SECRET|USER_RECORD_PRIVILEGED)))
213 return json_log(v, json_flags, SYNTHETIC_ERRNO(EINVAL), "Secret and privileged section currently not available for groups, refusing.");
214
215 r = user_group_record_mangle(v, load_flags, &h->json, &h->mask);
216 if (r < 0)
217 return r;
218
219 r = sd_json_dispatch(h->json, group_dispatch_table, json_flags | SD_JSON_ALLOW_EXTENSIONS, h);
220 if (r < 0)
221 return r;
222
223 /* During the parsing operation above we ignored the 'perMachine', 'binding' and 'status' fields, since we want
224 * them to override the global options. Let's process them now. */
225
226 r = dispatch_per_machine("perMachine", sd_json_variant_by_key(h->json, "perMachine"), json_flags, h);
227 if (r < 0)
228 return r;
229
230 r = dispatch_binding("binding", sd_json_variant_by_key(h->json, "binding"), json_flags, h);
231 if (r < 0)
232 return r;
233
234 r = dispatch_status("status", sd_json_variant_by_key(h->json, "status"), json_flags, h);
235 if (r < 0)
236 return r;
237
238 if (FLAGS_SET(h->mask, USER_RECORD_REGULAR) && !h->group_name)
239 return json_log(h->json, json_flags, SYNTHETIC_ERRNO(EINVAL), "Group name field missing, refusing.");
240
241 r = group_record_augment(h, json_flags);
242 if (r < 0)
243 return r;
244
245 return 0;
246 }
247
248 int group_record_build(GroupRecord **ret, ...) {
249 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
250 _cleanup_(group_record_unrefp) GroupRecord *g = NULL;
251 va_list ap;
252 int r;
253
254 assert(ret);
255
256 va_start(ap, ret);
257 r = sd_json_buildv(&v, ap);
258 va_end(ap);
259
260 if (r < 0)
261 return r;
262
263 g = group_record_new();
264 if (!g)
265 return -ENOMEM;
266
267 r = group_record_load(g, v, USER_RECORD_LOAD_FULL);
268 if (r < 0)
269 return r;
270
271 *ret = TAKE_PTR(g);
272 return 0;
273 }
274
275 const char* group_record_group_name_and_realm(GroupRecord *h) {
276 assert(h);
277
278 /* Return the pre-initialized joined string if it is defined */
279 if (h->group_name_and_realm_auto)
280 return h->group_name_and_realm_auto;
281
282 /* If it's not defined then we cannot have a realm */
283 assert(!h->realm);
284 return h->group_name;
285 }
286
287 UserDisposition group_record_disposition(GroupRecord *h) {
288 assert(h);
289
290 if (h->disposition >= 0)
291 return h->disposition;
292
293 /* If not declared, derive from GID */
294
295 if (!gid_is_valid(h->gid))
296 return _USER_DISPOSITION_INVALID;
297
298 if (h->gid == 0 || h->gid == GID_NOBODY)
299 return USER_INTRINSIC;
300
301 if (gid_is_system(h->gid))
302 return USER_SYSTEM;
303
304 if (gid_is_dynamic(h->gid))
305 return USER_DYNAMIC;
306
307 if (gid_is_container(h->gid))
308 return USER_CONTAINER;
309
310 if (gid_is_foreign(h->gid))
311 return USER_FOREIGN;
312
313 if (h->gid > INT32_MAX)
314 return USER_RESERVED;
315
316 return USER_REGULAR;
317 }
318
319 int group_record_clone(GroupRecord *h, UserRecordLoadFlags flags, GroupRecord **ret) {
320 _cleanup_(group_record_unrefp) GroupRecord *c = NULL;
321 int r;
322
323 assert(h);
324 assert(ret);
325
326 c = group_record_new();
327 if (!c)
328 return -ENOMEM;
329
330 r = group_record_load(c, h->json, flags);
331 if (r < 0)
332 return r;
333
334 *ret = TAKE_PTR(c);
335 return 0;
336 }
337
338 bool group_record_matches_group_name(const GroupRecord *g, const char *group_name) {
339 assert(g);
340 assert(group_name);
341
342 if (streq_ptr(g->group_name, group_name))
343 return true;
344
345 if (streq_ptr(g->group_name_and_realm_auto, group_name))
346 return true;
347
348 return false;
349 }
350
351 bool group_record_match(GroupRecord *h, const UserDBMatch *match) {
352 assert(h);
353
354 if (!match)
355 return true;
356
357 if (!gid_is_valid(h->gid))
358 return false;
359
360 if (h->gid < match->gid_min || h->gid > match->gid_max)
361 return false;
362
363 if (!BIT_SET(match->disposition_mask, group_record_disposition(h)))
364 return false;
365
366 if (!strv_isempty(match->fuzzy_names)) {
367 const char* names[] = {
368 h->group_name,
369 group_record_group_name_and_realm(h),
370 h->description,
371 };
372
373 if (!user_name_fuzzy_match(names, ELEMENTSOF(names), match->fuzzy_names))
374 return false;
375 }
376
377 return true;
378 }
379
380 bool group_record_is_root(const GroupRecord *g) {
381 assert(g);
382
383 return g->gid == 0 || streq_ptr(g->group_name, "root");
384 }
385
386 bool group_record_is_nobody(const GroupRecord *g) {
387 assert(g);
388
389 return g->gid == GID_NOBODY || STRPTR_IN_SET(g->group_name, NOBODY_GROUP_NAME, "nobody");
390 }