Debian Codegraph @

src/shared/user-record.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/mount.h>
4
5 #include "alloc-util.h"
6 #include "bitfield.h"
7 #include "capability-list.h"
8 #include "cgroup-util.h"
9 #include "dns-domain.h"
10 #include "glyph-util.h"
11 #include "hashmap.h"
12 #include "hostname-setup.h"
13 #include "json-util.h"
14 #include "locale-util.h"
15 #include "log.h"
16 #include "memory-util.h"
17 #include "path-util.h"
18 #include "percent-util.h"
19 #include "pkcs11-util.h"
20 #include "rlimit-util.h"
21 #include "sha256.h"
22 #include "string-table.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "time-util.h"
26 #include "uid-classification.h"
27 #include "user-record.h"
28 #include "user-util.h"
29
30 #define DEFAULT_RATELIMIT_BURST 30
31 #define DEFAULT_RATELIMIT_INTERVAL_USEC (1*USEC_PER_MINUTE)
32
33 UserRecord* user_record_new(void) {
34 UserRecord *h;
35
36 h = new(UserRecord, 1);
37 if (!h)
38 return NULL;
39
40 *h = (UserRecord) {
41 .n_ref = 1,
42 .disposition = _USER_DISPOSITION_INVALID,
43 .last_change_usec = UINT64_MAX,
44 .last_password_change_usec = UINT64_MAX,
45 .umask = MODE_INVALID,
46 .nice_level = INT_MAX,
47 .not_before_usec = UINT64_MAX,
48 .not_after_usec = UINT64_MAX,
49 .birth_date = BIRTH_DATE_UNSET,
50 .locked = -1,
51 .storage = _USER_STORAGE_INVALID,
52 .access_mode = MODE_INVALID,
53 .disk_size = UINT64_MAX,
54 .disk_size_relative = UINT64_MAX,
55 .tasks_max = UINT64_MAX,
56 .memory_high = UINT64_MAX,
57 .memory_max = UINT64_MAX,
58 .cpu_weight = UINT64_MAX,
59 .io_weight = UINT64_MAX,
60 .uid = UID_INVALID,
61 .gid = GID_INVALID,
62 .nodev = true,
63 .nosuid = true,
64 .luks_discard = -1,
65 .luks_offline_discard = -1,
66 .luks_volume_key_size = UINT64_MAX,
67 .luks_pbkdf_force_iterations = UINT64_MAX,
68 .luks_pbkdf_time_cost_usec = UINT64_MAX,
69 .luks_pbkdf_memory_cost = UINT64_MAX,
70 .luks_pbkdf_parallel_threads = UINT64_MAX,
71 .luks_sector_size = UINT64_MAX,
72 .disk_usage = UINT64_MAX,
73 .disk_free = UINT64_MAX,
74 .disk_ceiling = UINT64_MAX,
75 .disk_floor = UINT64_MAX,
76 .signed_locally = -1,
77 .good_authentication_counter = UINT64_MAX,
78 .bad_authentication_counter = UINT64_MAX,
79 .last_good_authentication_usec = UINT64_MAX,
80 .last_bad_authentication_usec = UINT64_MAX,
81 .ratelimit_begin_usec = UINT64_MAX,
82 .ratelimit_count = UINT64_MAX,
83 .ratelimit_interval_usec = UINT64_MAX,
84 .ratelimit_burst = UINT64_MAX,
85 .removable = -1,
86 .enforce_password_policy = -1,
87 .auto_login = -1,
88 .stop_delay_usec = UINT64_MAX,
89 .kill_processes = -1,
90 .password_change_min_usec = UINT64_MAX,
91 .password_change_max_usec = UINT64_MAX,
92 .password_change_warn_usec = UINT64_MAX,
93 .password_change_inactive_usec = UINT64_MAX,
94 .password_change_now = -1,
95 .pkcs11_protected_authentication_path_permitted = -1,
96 .fido2_user_presence_permitted = -1,
97 .fido2_user_verification_permitted = -1,
98 .drop_caches = -1,
99 .auto_resize_mode = _AUTO_RESIZE_MODE_INVALID,
100 .rebalance_weight = REBALANCE_WEIGHT_UNSET,
101 .tmp_limit = TMPFS_LIMIT_NULL,
102 .dev_shm_limit = TMPFS_LIMIT_NULL,
103 };
104
105 return h;
106 }
107
108 sd_json_dispatch_flags_t USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(UserRecordLoadFlags flags) {
109 return (FLAGS_SET(flags, USER_RECORD_LOG) ? SD_JSON_LOG : 0) |
110 (FLAGS_SET(flags, USER_RECORD_PERMISSIVE) ? SD_JSON_PERMISSIVE : 0);
111 }
112
113 static void pkcs11_encrypted_key_done(Pkcs11EncryptedKey *k) {
114 if (!k)
115 return;
116
117 free(k->uri);
118 erase_and_free(k->data);
119 erase_and_free(k->hashed_password);
120 }
121
122 static void fido2_hmac_credential_done(Fido2HmacCredential *c) {
123 if (!c)
124 return;
125
126 free(c->id);
127 }
128
129 static void fido2_hmac_salt_done(Fido2HmacSalt *s) {
130 if (!s)
131 return;
132
133 fido2_hmac_credential_done(&s->credential);
134 erase_and_free(s->salt);
135 erase_and_free(s->hashed_password);
136 }
137
138 static void recovery_key_done(RecoveryKey *k) {
139 if (!k)
140 return;
141
142 free(k->type);
143 erase_and_free(k->hashed_password);
144 }
145
146 static UserRecord* user_record_free(UserRecord *h) {
147 if (!h)
148 return NULL;
149
150 free(h->user_name);
151 free(h->realm);
152 free(h->user_name_and_realm_auto);
153 strv_free(h->aliases);
154 free(h->real_name);
155 free(h->email_address);
156 erase_and_free(h->password_hint);
157 free(h->location);
158 free(h->icon_name);
159
160 free(h->blob_directory);
161 hashmap_free(h->blob_manifest);
162
163 free(h->shell);
164
165 strv_free(h->environment);
166 free(h->time_zone);
167 free(h->preferred_language);
168 strv_free(h->additional_languages);
169 rlimit_free_all(h->rlimits);
170
171 free(h->skeleton_directory);
172
173 strv_free_erase(h->hashed_password);
174 strv_free_erase(h->ssh_authorized_keys);
175 strv_free_erase(h->password);
176 strv_free_erase(h->token_pin);
177
178 free(h->cifs_service);
179 free(h->cifs_user_name);
180 free(h->cifs_domain);
181 free(h->cifs_extra_mount_options);
182
183 free(h->image_path);
184 free(h->image_path_auto);
185 free(h->home_directory);
186 free(h->home_directory_auto);
187
188 free(h->fallback_shell);
189 free(h->fallback_home_directory);
190
191 strv_free(h->member_of);
192 strv_free(h->capability_bounding_set);
193 strv_free(h->capability_ambient_set);
194
195 free(h->file_system_type);
196 free(h->luks_cipher);
197 free(h->luks_cipher_mode);
198 free(h->luks_pbkdf_hash_algorithm);
199 free(h->luks_pbkdf_type);
200 free(h->luks_extra_mount_options);
201
202 free(h->state);
203 free(h->service);
204
205 free(h->preferred_session_type);
206 free(h->preferred_session_launcher);
207
208 strv_free(h->pkcs11_token_uri);
209 for (size_t i = 0; i < h->n_pkcs11_encrypted_key; i++)
210 pkcs11_encrypted_key_done(h->pkcs11_encrypted_key + i);
211 free(h->pkcs11_encrypted_key);
212
213 for (size_t i = 0; i < h->n_fido2_hmac_credential; i++)
214 fido2_hmac_credential_done(h->fido2_hmac_credential + i);
215 free(h->fido2_hmac_credential);
216 for (size_t i = 0; i < h->n_fido2_hmac_salt; i++)
217 fido2_hmac_salt_done(h->fido2_hmac_salt + i);
218 free(h->fido2_hmac_salt);
219
220 strv_free(h->recovery_key_type);
221 for (size_t i = 0; i < h->n_recovery_key; i++)
222 recovery_key_done(h->recovery_key + i);
223 free(h->recovery_key);
224
225 strv_free(h->self_modifiable_fields);
226 strv_free(h->self_modifiable_blobs);
227 strv_free(h->self_modifiable_privileged);
228
229 free(h->default_area);
230
231 sd_json_variant_unref(h->json);
232
233 return mfree(h);
234 }
235
236 DEFINE_TRIVIAL_REF_UNREF_FUNC(UserRecord, user_record, user_record_free);
237
238 int json_dispatch_realm(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
239 char **s = userdata;
240 const char *n;
241 int r;
242
243 if (sd_json_variant_is_null(variant)) {
244 *s = mfree(*s);
245 return 0;
246 }
247
248 if (!sd_json_variant_is_string(variant))
249 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
250
251 n = sd_json_variant_string(variant);
252 r = dns_name_is_valid(n);
253 if (r < 0)
254 return json_log(variant, flags, r, "Failed to check if JSON field '%s' is a valid DNS domain.", strna(name));
255 if (r == 0)
256 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid DNS domain.", strna(name));
257
258 r = free_and_strdup(s, n);
259 if (r < 0)
260 return json_log(variant, flags, r, "Failed to allocate string: %m");
261
262 return 0;
263 }
264
265 int json_dispatch_gecos(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
266 char **s = userdata;
267 const char *n;
268
269 if (sd_json_variant_is_null(variant)) {
270 *s = mfree(*s);
271 return 0;
272 }
273
274 if (!sd_json_variant_is_string(variant))
275 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
276
277 n = sd_json_variant_string(variant);
278 if (valid_gecos(n)) {
279 if (free_and_strdup(s, n) < 0)
280 return json_log_oom(variant, flags);
281 } else {
282 _cleanup_free_ char *m = NULL;
283
284 json_log(variant, flags|SD_JSON_DEBUG, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid GECOS compatible string, mangling.", strna(name));
285
286 m = mangle_gecos(n);
287 if (!m)
288 return json_log_oom(variant, flags);
289
290 free_and_replace(*s, m);
291 }
292
293 return 0;
294 }
295
296 static int json_dispatch_nice(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
297 int *nl = userdata;
298 int64_t m;
299
300 if (sd_json_variant_is_null(variant)) {
301 *nl = INT_MAX;
302 return 0;
303 }
304
305 if (!sd_json_variant_is_integer(variant))
306 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
307
308 m = sd_json_variant_integer(variant);
309 if (m < PRIO_MIN || m >= PRIO_MAX)
310 return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' is not a valid nice level.", strna(name));
311
312 *nl = m;
313 return 0;
314 }
315
316 static int json_dispatch_rlimit_value(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
317 rlim_t *ret = userdata;
318
319 if (sd_json_variant_is_null(variant))
320 *ret = RLIM_INFINITY;
321 else if (sd_json_variant_is_unsigned(variant)) {
322 uint64_t w;
323
324 w = sd_json_variant_unsigned(variant);
325 if (w == RLIM_INFINITY || w != sd_json_variant_unsigned(variant))
326 return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "Resource limit value '%s' is out of range.", name);
327
328 *ret = (rlim_t) w;
329 } else
330 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit value '%s' is not an unsigned integer.", name);
331
332 return 0;
333 }
334
335 static int json_dispatch_rlimits(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
336 struct rlimit** limits = userdata;
337 sd_json_variant *value;
338 const char *key;
339 int r;
340
341 assert_se(limits);
342
343 if (sd_json_variant_is_null(variant)) {
344 rlimit_free_all(limits);
345 return 0;
346 }
347
348 if (!sd_json_variant_is_object(variant))
349 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name));
350
351 JSON_VARIANT_OBJECT_FOREACH(key, value, variant) {
352 sd_json_variant *jcur, *jmax;
353 struct rlimit rl;
354 const char *p;
355 int l;
356
357 p = startswith(key, "RLIMIT_");
358 if (!p)
359 l = -SYNTHETIC_ERRNO(EINVAL);
360 else
361 l = rlimit_from_string(p);
362 if (l < 0)
363 return json_log(variant, flags, l, "Resource limit '%s' not known.", key);
364
365 if (!sd_json_variant_is_object(value))
366 return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' has invalid value.", key);
367
368 if (sd_json_variant_elements(value) != 4)
369 return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' value is does not have two fields as expected.", key);
370
371 jcur = sd_json_variant_by_key(value, "cur");
372 if (!jcur)
373 return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' lacks 'cur' field.", key);
374 r = json_dispatch_rlimit_value("cur", jcur, flags, &rl.rlim_cur);
375 if (r < 0)
376 return r;
377
378 jmax = sd_json_variant_by_key(value, "max");
379 if (!jmax)
380 return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' lacks 'max' field.", key);
381 r = json_dispatch_rlimit_value("max", jmax, flags, &rl.rlim_max);
382 if (r < 0)
383 return r;
384
385 if (limits[l])
386 *(limits[l]) = rl;
387 else {
388 limits[l] = newdup(struct rlimit, &rl, 1);
389 if (!limits[l])
390 return log_oom();
391 }
392 }
393
394 return 0;
395 }
396
397 static int json_dispatch_filename_or_path(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
398 char **s = ASSERT_PTR(userdata);
399 const char *n;
400 int r;
401
402 if (sd_json_variant_is_null(variant)) {
403 *s = mfree(*s);
404 return 0;
405 }
406
407 if (!sd_json_variant_is_string(variant))
408 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
409
410 n = sd_json_variant_string(variant);
411 if (!filename_is_valid(n) && !path_is_normalized(n))
412 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid file name or normalized path.", strna(name));
413
414 r = free_and_strdup(s, n);
415 if (r < 0)
416 return json_log(variant, flags, r, "Failed to allocate string: %m");
417
418 return 0;
419 }
420
421 static int json_dispatch_birth_date(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
422 struct tm *ret = ASSERT_PTR(userdata);
423 const char *s;
424 int r;
425
426 if (sd_json_variant_is_null(variant)) {
427 *ret = BIRTH_DATE_UNSET;
428 return 0;
429 }
430
431 if (!sd_json_variant_is_string(variant))
432 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
433
434 s = sd_json_variant_string(variant);
435
436 r = parse_birth_date(s, ret);
437 if (r < 0)
438 return json_log(variant, flags, r, "JSON field '%s' is not a valid ISO 8601 date (expected YYYY-MM-DD).", strna(name));
439
440 return 0;
441 }
442
443 static int json_dispatch_home_directory(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
444 char **s = userdata;
445 const char *n;
446 int r;
447
448 if (sd_json_variant_is_null(variant)) {
449 *s = mfree(*s);
450 return 0;
451 }
452
453 if (!sd_json_variant_is_string(variant))
454 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
455
456 n = sd_json_variant_string(variant);
457 if (!valid_home(n))
458 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid home directory path.", strna(name));
459
460 r = free_and_strdup(s, n);
461 if (r < 0)
462 return json_log(variant, flags, r, "Failed to allocate string: %m");
463
464 return 0;
465 }
466
467 static int json_dispatch_image_path(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
468 char **s = userdata;
469 const char *n;
470 int r;
471
472 if (sd_json_variant_is_null(variant)) {
473 *s = mfree(*s);
474 return 0;
475 }
476
477 if (!sd_json_variant_is_string(variant))
478 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
479
480 n = sd_json_variant_string(variant);
481 if (empty_or_root(n) || !path_is_valid(n) || !path_is_absolute(n))
482 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid image path.", strna(name));
483
484 r = free_and_strdup(s, n);
485 if (r < 0)
486 return json_log(variant, flags, r, "Failed to allocate string: %m");
487
488 return 0;
489 }
490
491 static int json_dispatch_locale(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
492 char **s = userdata;
493 const char *n;
494 int r;
495
496 if (sd_json_variant_is_null(variant)) {
497 *s = mfree(*s);
498 return 0;
499 }
500
501 if (!sd_json_variant_is_string(variant))
502 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
503
504 n = sd_json_variant_string(variant);
505
506 if (!locale_is_valid(n))
507 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid locale.", strna(name));
508
509 r = free_and_strdup(s, n);
510 if (r < 0)
511 return json_log(variant, flags, r, "Failed to allocate string: %m");
512
513 return 0;
514 }
515
516 static int json_dispatch_locales(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
517 _cleanup_strv_free_ char **n = NULL;
518 char ***l = userdata;
519 const char *locale;
520 sd_json_variant *e;
521 size_t s = 0;
522 int r;
523
524 if (sd_json_variant_is_null(variant)) {
525 *l = strv_free(*l);
526 return 0;
527 }
528
529 if (!sd_json_variant_is_array(variant))
530 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
531
532 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
533 if (!sd_json_variant_is_string(e))
534 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
535
536 locale = sd_json_variant_string(e);
537 if (!locale_is_valid(locale))
538 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of valid locales.", strna(name));
539
540 r = strv_extend_with_size(&n, &s, locale);
541 if (r < 0)
542 return json_log_oom(variant, flags);
543 }
544
545 return strv_free_and_replace(*l, n);
546 }
547
548 JSON_DISPATCH_ENUM_DEFINE(json_dispatch_user_disposition, UserDisposition, user_disposition_from_string);
549 static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_user_storage, UserStorage, user_storage_from_string);
550
551 static int json_dispatch_tasks_or_memory_max(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
552 uint64_t *limit = userdata, k;
553
554 if (sd_json_variant_is_null(variant)) {
555 *limit = UINT64_MAX;
556 return 0;
557 }
558
559 if (!sd_json_variant_is_unsigned(variant))
560 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name));
561
562 k = sd_json_variant_unsigned(variant);
563 if (k <= 0 || k >= UINT64_MAX)
564 return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE),
565 "JSON field '%s' is not in valid range %" PRIu64 "%s%" PRIu64 ".",
566 strna(name), (uint64_t) 1, glyph(GLYPH_ELLIPSIS), UINT64_MAX-1);
567
568 *limit = k;
569 return 0;
570 }
571
572 static int json_dispatch_weight(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
573 uint64_t *weight = userdata, k;
574
575 if (sd_json_variant_is_null(variant)) {
576 *weight = UINT64_MAX;
577 return 0;
578 }
579
580 if (!sd_json_variant_is_unsigned(variant))
581 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name));
582
583 k = sd_json_variant_unsigned(variant);
584 if (k < CGROUP_WEIGHT_MIN || k > CGROUP_WEIGHT_MAX)
585 return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE),
586 "JSON field '%s' is not in valid range %" PRIu64 "%s%" PRIu64 ".",
587 strna(name), CGROUP_WEIGHT_MIN,
588 glyph(GLYPH_ELLIPSIS), CGROUP_WEIGHT_MAX);
589
590 *weight = k;
591 return 0;
592 }
593
594 int json_dispatch_user_group_list(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
595 char ***list = ASSERT_PTR(userdata);
596 _cleanup_strv_free_ char **l = NULL;
597 size_t s = 0;
598 int r;
599
600 if (!sd_json_variant_is_array(variant))
601 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
602
603 sd_json_variant *e;
604 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
605 if (!sd_json_variant_is_string(e))
606 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a string.");
607
608 if (!valid_user_group_name(sd_json_variant_string(e), FLAGS_SET(flags, SD_JSON_RELAX) ? VALID_USER_RELAX : 0))
609 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a valid user/group name: %s", sd_json_variant_string(e));
610
611 r = strv_extend_with_size(&l, &s, sd_json_variant_string(e));
612 if (r < 0)
613 return json_log(e, flags, r, "Failed to append array element: %m");
614 }
615
616 r = strv_extend_strv_consume(list, TAKE_PTR(l), /* filter_duplicates= */ true);
617 if (r < 0)
618 return json_log(variant, flags, r, "Failed to merge user/group arrays: %m");
619
620 return 0;
621 }
622
623 static int dispatch_secret(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
624
625 static const sd_json_dispatch_field secret_dispatch_table[] = {
626 { "password", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(UserRecord, password), 0 },
627 { "tokenPin", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(UserRecord, token_pin), 0 },
628 { "pkcs11Pin", /* legacy alias */ _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(UserRecord, token_pin), 0 },
629 { "pkcs11ProtectedAuthenticationPathPermitted", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, pkcs11_protected_authentication_path_permitted), 0 },
630 { "fido2UserPresencePermitted", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, fido2_user_presence_permitted), 0 },
631 { "fido2UserVerificationPermitted", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, fido2_user_verification_permitted), 0 },
632 {},
633 };
634
635 return sd_json_dispatch(variant, secret_dispatch_table, flags, userdata);
636 }
637
638 static int dispatch_pkcs11_uri(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
639 char **s = userdata;
640 const char *n;
641 int r;
642
643 if (sd_json_variant_is_null(variant)) {
644 *s = mfree(*s);
645 return 0;
646 }
647
648 if (!sd_json_variant_is_string(variant))
649 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
650
651 n = sd_json_variant_string(variant);
652 if (!pkcs11_uri_valid(n))
653 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name));
654
655 r = free_and_strdup(s, n);
656 if (r < 0)
657 return json_log(variant, flags, r, "Failed to allocate string: %m");
658
659 return 0;
660 }
661
662 static int dispatch_pkcs11_uri_array(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
663 _cleanup_strv_free_ char **z = NULL;
664 char ***l = userdata;
665 sd_json_variant *e;
666 int r;
667
668 if (sd_json_variant_is_null(variant)) {
669 *l = strv_free(*l);
670 return 0;
671 }
672
673 if (sd_json_variant_is_string(variant)) {
674 const char *n;
675
676 n = sd_json_variant_string(variant);
677 if (!pkcs11_uri_valid(n))
678 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name));
679
680 z = strv_new(n);
681 if (!z)
682 return log_oom();
683
684 } else {
685
686 if (!sd_json_variant_is_array(variant))
687 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string or array of strings.", strna(name));
688
689 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
690 const char *n;
691
692 if (!sd_json_variant_is_string(e))
693 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a string.");
694
695 n = sd_json_variant_string(e);
696 if (!pkcs11_uri_valid(n))
697 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element in '%s' is not a valid RFC7512 PKCS#11 URI: %s", strna(name), n);
698
699 r = strv_extend(&z, n);
700 if (r < 0)
701 return log_oom();
702 }
703 }
704
705 strv_free_and_replace(*l, z);
706 return 0;
707 }
708
709 static int dispatch_pkcs11_key_data(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
710 Pkcs11EncryptedKey *k = userdata;
711 size_t l;
712 void *b;
713 int r;
714
715 if (sd_json_variant_is_null(variant)) {
716 k->data = erase_and_free(k->data);
717 k->size = 0;
718 return 0;
719 }
720
721 r = sd_json_variant_unbase64(variant, &b, &l);
722 if (r < 0)
723 return json_log(variant, flags, r, "Failed to decode encrypted PKCS#11 key: %m");
724
725 erase_and_free(k->data);
726 k->data = b;
727 k->size = l;
728
729 return 0;
730 }
731
732 static int dispatch_pkcs11_padding(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
733 Pkcs11RsaPadding *p = ASSERT_PTR(userdata);
734 Pkcs11RsaPadding v;
735
736 if (sd_json_variant_is_null(variant)) {
737 *p = PKCS11_RSA_PADDING_PKCS1V15;
738 return 0;
739 }
740
741 if (!sd_json_variant_is_string(variant))
742 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL),
743 "JSON field '%s' is not a string.", strna(name));
744
745 v = pkcs11_rsa_padding_from_string(sd_json_variant_string(variant));
746 if (v < 0)
747 return json_log(variant, flags, v,
748 "JSON field '%s' has unsupported value '%s'.",
749 strna(name), sd_json_variant_string(variant));
750
751 *p = v;
752 return 0;
753 }
754
755 static int dispatch_pkcs11_key(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
756 UserRecord *h = userdata;
757 sd_json_variant *e;
758 int r;
759
760 if (!sd_json_variant_is_array(variant))
761 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
762
763 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
764 static const sd_json_dispatch_field pkcs11_key_dispatch_table[] = {
765 { "uri", SD_JSON_VARIANT_STRING, dispatch_pkcs11_uri, offsetof(Pkcs11EncryptedKey, uri), SD_JSON_MANDATORY },
766 { "data", SD_JSON_VARIANT_STRING, dispatch_pkcs11_key_data, 0, SD_JSON_MANDATORY },
767 { "hashedPassword", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Pkcs11EncryptedKey, hashed_password), SD_JSON_MANDATORY },
768 { "padding", SD_JSON_VARIANT_STRING, dispatch_pkcs11_padding, offsetof(Pkcs11EncryptedKey, padding), 0 },
769 {},
770 };
771
772 if (!sd_json_variant_is_object(e))
773 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
774
775 if (!GREEDY_REALLOC(h->pkcs11_encrypted_key, h->n_pkcs11_encrypted_key + 1))
776 return log_oom();
777
778 Pkcs11EncryptedKey *k = h->pkcs11_encrypted_key + h->n_pkcs11_encrypted_key;
779 *k = (Pkcs11EncryptedKey) {
780 .padding = PKCS11_RSA_PADDING_PKCS1V15, /* legacy default if field is absent */
781 };
782
783 r = sd_json_dispatch(e, pkcs11_key_dispatch_table, flags, k);
784 if (r < 0) {
785 pkcs11_encrypted_key_done(k);
786 return r;
787 }
788
789 h->n_pkcs11_encrypted_key++;
790 }
791
792 return 0;
793 }
794
795 static int dispatch_fido2_hmac_credential(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
796 Fido2HmacCredential *k = userdata;
797 size_t l;
798 void *b;
799 int r;
800
801 if (sd_json_variant_is_null(variant)) {
802 k->id = mfree(k->id);
803 k->size = 0;
804 return 0;
805 }
806
807 r = sd_json_variant_unbase64(variant, &b, &l);
808 if (r < 0)
809 return json_log(variant, flags, r, "Failed to decode FIDO2 credential ID: %m");
810
811 free_and_replace(k->id, b);
812 k->size = l;
813
814 return 0;
815 }
816
817 static int dispatch_fido2_hmac_credential_array(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
818 UserRecord *h = userdata;
819 sd_json_variant *e;
820 int r;
821
822 if (!sd_json_variant_is_array(variant))
823 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
824
825 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
826 size_t l;
827 void *b;
828
829 if (!GREEDY_REALLOC(h->fido2_hmac_credential, h->n_fido2_hmac_credential + 1))
830 return log_oom();
831
832 r = sd_json_variant_unbase64(e, &b, &l);
833 if (r < 0)
834 return json_log(variant, flags, r, "Failed to decode FIDO2 credential ID: %m");
835
836 h->fido2_hmac_credential[h->n_fido2_hmac_credential++] = (Fido2HmacCredential) {
837 .id = b,
838 .size = l,
839 };
840 }
841
842 return 0;
843 }
844
845 static int dispatch_fido2_hmac_salt_value(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
846 Fido2HmacSalt *k = userdata;
847 size_t l;
848 void *b;
849 int r;
850
851 if (sd_json_variant_is_null(variant)) {
852 k->salt = erase_and_free(k->salt);
853 k->salt_size = 0;
854 return 0;
855 }
856
857 r = sd_json_variant_unbase64(variant, &b, &l);
858 if (r < 0)
859 return json_log(variant, flags, r, "Failed to decode FIDO2 salt: %m");
860
861 erase_and_free(k->salt);
862 k->salt = b;
863 k->salt_size = l;
864
865 return 0;
866 }
867
868 static int dispatch_fido2_hmac_salt(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
869 UserRecord *h = userdata;
870 sd_json_variant *e;
871 int r;
872
873 if (!sd_json_variant_is_array(variant))
874 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
875
876 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
877 static const sd_json_dispatch_field fido2_hmac_salt_dispatch_table[] = {
878 { "credential", SD_JSON_VARIANT_STRING, dispatch_fido2_hmac_credential, offsetof(Fido2HmacSalt, credential), SD_JSON_MANDATORY },
879 { "salt", SD_JSON_VARIANT_STRING, dispatch_fido2_hmac_salt_value, 0, SD_JSON_MANDATORY },
880 { "hashedPassword", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Fido2HmacSalt, hashed_password), SD_JSON_MANDATORY },
881 { "up", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Fido2HmacSalt, up), 0 },
882 { "uv", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Fido2HmacSalt, uv), 0 },
883 { "clientPin", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Fido2HmacSalt, client_pin), 0 },
884 {},
885 };
886
887 if (!sd_json_variant_is_object(e))
888 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
889
890 if (!GREEDY_REALLOC(h->fido2_hmac_salt, h->n_fido2_hmac_salt + 1))
891 return log_oom();
892
893 Fido2HmacSalt *k = h->fido2_hmac_salt + h->n_fido2_hmac_salt;
894 *k = (Fido2HmacSalt) {
895 .uv = -1,
896 .up = -1,
897 .client_pin = -1,
898 };
899
900 r = sd_json_dispatch(e, fido2_hmac_salt_dispatch_table, flags, k);
901 if (r < 0) {
902 fido2_hmac_salt_done(k);
903 return r;
904 }
905
906 h->n_fido2_hmac_salt++;
907 }
908
909 return 0;
910 }
911
912 static int dispatch_recovery_key(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
913 UserRecord *h = userdata;
914 sd_json_variant *e;
915 int r;
916
917 if (!sd_json_variant_is_array(variant))
918 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
919
920 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
921 static const sd_json_dispatch_field recovery_key_dispatch_table[] = {
922 { "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, 0, SD_JSON_MANDATORY },
923 { "hashedPassword", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(RecoveryKey, hashed_password), SD_JSON_MANDATORY },
924 {},
925 };
926
927 if (!sd_json_variant_is_object(e))
928 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
929
930 if (!GREEDY_REALLOC(h->recovery_key, h->n_recovery_key + 1))
931 return log_oom();
932
933 RecoveryKey *k = h->recovery_key + h->n_recovery_key;
934 *k = (RecoveryKey) {};
935
936 r = sd_json_dispatch(e, recovery_key_dispatch_table, flags, k);
937 if (r < 0) {
938 recovery_key_done(k);
939 return r;
940 }
941
942 h->n_recovery_key++;
943 }
944
945 return 0;
946 }
947
948 static int dispatch_auto_resize_mode(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
949 AutoResizeMode *mode = userdata, m;
950
951 assert_se(mode);
952
953 if (sd_json_variant_is_null(variant)) {
954 *mode = _AUTO_RESIZE_MODE_INVALID;
955 return 0;
956 }
957
958 if (sd_json_variant_is_boolean(variant)) {
959 *mode = sd_json_variant_boolean(variant) ? AUTO_RESIZE_SHRINK_AND_GROW : AUTO_RESIZE_OFF;
960 return 0;
961 }
962
963 if (!sd_json_variant_is_string(variant))
964 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string, boolean or null.", strna(name));
965
966 m = auto_resize_mode_from_string(sd_json_variant_string(variant));
967 if (m < 0)
968 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid automatic resize mode.", strna(name));
969
970 *mode = m;
971 return 0;
972 }
973
974 static int dispatch_rebalance_weight(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
975 uint64_t *rebalance_weight = userdata;
976 uintmax_t u;
977
978 assert_se(rebalance_weight);
979
980 if (sd_json_variant_is_null(variant)) {
981 *rebalance_weight = REBALANCE_WEIGHT_UNSET;
982 return 0;
983 }
984
985 if (sd_json_variant_is_boolean(variant)) {
986 *rebalance_weight = sd_json_variant_boolean(variant) ? REBALANCE_WEIGHT_DEFAULT : REBALANCE_WEIGHT_OFF;
987 return 0;
988 }
989
990 if (!sd_json_variant_is_unsigned(variant))
991 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an unsigned integer, boolean or null.", strna(name));
992
993 u = sd_json_variant_unsigned(variant);
994 if (u >= REBALANCE_WEIGHT_MIN && u <= REBALANCE_WEIGHT_MAX)
995 *rebalance_weight = (uint64_t) u;
996 else if (u == 0)
997 *rebalance_weight = REBALANCE_WEIGHT_OFF;
998 else
999 return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE),
1000 "Rebalance weight is out of valid range %" PRIu64 "%s%" PRIu64 ".",
1001 REBALANCE_WEIGHT_MIN, glyph(GLYPH_ELLIPSIS), REBALANCE_WEIGHT_MAX);
1002
1003 return 0;
1004 }
1005
1006 static int dispatch_tmpfs_limit(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
1007 TmpfsLimit *limit = ASSERT_PTR(userdata);
1008 int r;
1009
1010 if (sd_json_variant_is_null(variant)) {
1011 *limit = TMPFS_LIMIT_NULL;
1012 return 0;
1013 }
1014
1015 r = sd_json_dispatch_uint64(name, variant, flags, &limit->limit);
1016 if (r < 0)
1017 return r;
1018
1019 limit->is_set = true;
1020 return 0;
1021 }
1022
1023 static int dispatch_tmpfs_limit_scale(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
1024 TmpfsLimit *limit = ASSERT_PTR(userdata);
1025 int r;
1026
1027 if (sd_json_variant_is_null(variant)) {
1028 *limit = TMPFS_LIMIT_NULL;
1029 return 0;
1030 }
1031
1032 r = sd_json_dispatch_uint32(name, variant, flags, &limit->limit_scale);
1033 if (r < 0)
1034 return r;
1035
1036 limit->is_set = true;
1037 return 0;
1038 }
1039
1040 static int dispatch_privileged(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
1041
1042 static const sd_json_dispatch_field privileged_dispatch_table[] = {
1043 { "passwordHint", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, password_hint), 0 },
1044 { "hashedPassword", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(UserRecord, hashed_password), SD_JSON_STRICT },
1045 { "sshAuthorizedKeys", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(UserRecord, ssh_authorized_keys), 0 },
1046 { "pkcs11EncryptedKey", SD_JSON_VARIANT_ARRAY, dispatch_pkcs11_key, 0, 0 },
1047 { "fido2HmacSalt", SD_JSON_VARIANT_ARRAY, dispatch_fido2_hmac_salt, 0, 0 },
1048 { "recoveryKey", SD_JSON_VARIANT_ARRAY, dispatch_recovery_key, 0, 0 },
1049 {},
1050 };
1051
1052 return sd_json_dispatch(variant, privileged_dispatch_table, flags, userdata);
1053 }
1054
1055 static int dispatch_binding(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
1056
1057 static const sd_json_dispatch_field binding_dispatch_table[] = {
1058 { "blobDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, blob_directory), SD_JSON_STRICT },
1059 { "imagePath", SD_JSON_VARIANT_STRING, json_dispatch_image_path, offsetof(UserRecord, image_path), 0 },
1060 { "homeDirectory", SD_JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, home_directory), 0 },
1061 { "partitionUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 },
1062 { "luksUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 },
1063 { "fileSystemUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 },
1064 { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 },
1065 { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 },
1066 { "storage", SD_JSON_VARIANT_STRING, json_dispatch_user_storage, offsetof(UserRecord, storage), 0 },
1067 { "fileSystemType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, file_system_type), SD_JSON_STRICT },
1068 { "luksCipher", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher), SD_JSON_STRICT },
1069 { "luksCipherMode", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), SD_JSON_STRICT },
1070 { "luksVolumeKeySize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 },
1071 {},
1072 };
1073
1074 sd_json_variant *m;
1075 sd_id128_t mid;
1076 int r;
1077
1078 if (!variant)
1079 return 0;
1080
1081 if (!sd_json_variant_is_object(variant))
1082 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name));
1083
1084 r = sd_id128_get_machine(&mid);
1085 if (r < 0)
1086 return json_log(variant, flags, r, "Failed to determine machine ID: %m");
1087
1088 m = sd_json_variant_by_key(variant, SD_ID128_TO_STRING(mid));
1089 if (!m)
1090 return 0;
1091
1092 return sd_json_dispatch(m, binding_dispatch_table, flags, userdata);
1093 }
1094
1095 static int dispatch_blob_manifest(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
1096 _cleanup_hashmap_free_ Hashmap *manifest = NULL;
1097 Hashmap **ret = ASSERT_PTR(userdata);
1098 sd_json_variant *value;
1099 const char *key;
1100 int r;
1101
1102 if (!variant)
1103 return 0;
1104
1105 if (!sd_json_variant_is_object(variant))
1106 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name));
1107
1108 JSON_VARIANT_OBJECT_FOREACH(key, value, variant) {
1109 _cleanup_free_ char *filename = NULL;
1110 _cleanup_free_ uint8_t *hash = NULL;
1111
1112 if (!sd_json_variant_is_string(value))
1113 return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Blob entry '%s' has invalid hash.", key);
1114
1115 if (!suitable_blob_filename(key))
1116 return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Blob entry '%s' has invalid filename.", key);
1117
1118 filename = strdup(key);
1119 if (!filename)
1120 return json_log_oom(value, flags);
1121
1122 hash = malloc(SHA256_DIGEST_SIZE);
1123 if (!hash)
1124 return json_log_oom(value, flags);
1125
1126 r = parse_sha256(sd_json_variant_string(value), hash);
1127 if (r < 0)
1128 return json_log(value, flags, r, "Blob entry '%s' has invalid hash: %s", filename, sd_json_variant_string(value));
1129
1130 r = hashmap_ensure_put(&manifest, &path_hash_ops_free_free, filename, hash);
1131 if (r < 0)
1132 return json_log(value, flags, r, "Failed to insert blob manifest entry '%s': %m", filename);
1133 TAKE_PTR(filename); /* Ownership transfers to hashmap */
1134 TAKE_PTR(hash);
1135 }
1136
1137 hashmap_free_and_replace(*ret, manifest);
1138 return 0;
1139 }
1140
1141 int per_machine_id_match(sd_json_variant *ids, sd_json_dispatch_flags_t flags) {
1142 sd_id128_t mid;
1143 int r;
1144
1145 assert(ids);
1146
1147 r = sd_id128_get_machine(&mid);
1148 if (r < 0)
1149 return json_log(ids, flags, r, "Failed to acquire machine ID: %m");
1150
1151 if (sd_json_variant_is_string(ids)) {
1152 sd_id128_t k;
1153
1154 r = sd_id128_from_string(sd_json_variant_string(ids), &k);
1155 if (r < 0) {
1156 json_log(ids, flags, r, "%s is not a valid machine ID, ignoring: %m", sd_json_variant_string(ids));
1157 return 0;
1158 }
1159
1160 return sd_id128_equal(mid, k);
1161 }
1162
1163 if (sd_json_variant_is_array(ids)) {
1164 sd_json_variant *e;
1165
1166 JSON_VARIANT_ARRAY_FOREACH(e, ids) {
1167 sd_id128_t k;
1168
1169 if (!sd_json_variant_is_string(e)) {
1170 json_log(e, flags, 0, "Machine ID is not a string, ignoring: %m");
1171 continue;
1172 }
1173
1174 r = sd_id128_from_string(sd_json_variant_string(e), &k);
1175 if (r < 0) {
1176 json_log(e, flags, r, "%s is not a valid machine ID, ignoring: %m", sd_json_variant_string(e));
1177 continue;
1178 }
1179
1180 if (sd_id128_equal(mid, k))
1181 return true;
1182 }
1183
1184 return false;
1185 }
1186
1187 json_log(ids, flags, 0, "Machine ID is not a string or array of strings, ignoring: %m");
1188 return false;
1189 }
1190
1191 int per_machine_hostname_match(sd_json_variant *hns, sd_json_dispatch_flags_t flags) {
1192 _cleanup_free_ char *hn = NULL;
1193 int r;
1194
1195 assert(hns);
1196
1197 r = gethostname_strict(&hn);
1198 if (r == -ENXIO) {
1199 json_log(hns, flags, r, "No hostname set, not matching perMachine hostname record: %m");
1200 return false;
1201 }
1202 if (r < 0)
1203 return json_log(hns, flags, r, "Failed to acquire hostname: %m");
1204
1205 if (sd_json_variant_is_string(hns))
1206 return streq(sd_json_variant_string(hns), hn);
1207
1208 if (sd_json_variant_is_array(hns)) {
1209 sd_json_variant *e;
1210
1211 JSON_VARIANT_ARRAY_FOREACH(e, hns) {
1212
1213 if (!sd_json_variant_is_string(e)) {
1214 json_log(e, flags, 0, "Hostname is not a string, ignoring: %m");
1215 continue;
1216 }
1217
1218 if (streq(sd_json_variant_string(e), hn))
1219 return true;
1220 }
1221
1222 return false;
1223 }
1224
1225 json_log(hns, flags, 0, "Hostname is not a string or array of strings, ignoring: %m");
1226 return false;
1227 }
1228
1229 int per_machine_match(sd_json_variant *entry, sd_json_dispatch_flags_t flags) {
1230 sd_json_variant *m;
1231 int r;
1232
1233 assert(sd_json_variant_is_object(entry));
1234
1235 m = sd_json_variant_by_key(entry, "matchMachineId");
1236 if (m) {
1237 r = per_machine_id_match(m, flags);
1238 if (r < 0)
1239 return r;
1240 if (r > 0)
1241 return true;
1242 }
1243
1244 m = sd_json_variant_by_key(entry, "matchNotMachineId");
1245 if (m) {
1246 r = per_machine_id_match(m, flags);
1247 if (r < 0)
1248 return r;
1249 if (r == 0)
1250 return true;
1251 }
1252
1253 m = sd_json_variant_by_key(entry, "matchHostname");
1254 if (m) {
1255 r = per_machine_hostname_match(m, flags);
1256 if (r < 0)
1257 return r;
1258 if (r > 0)
1259 return true;
1260 }
1261
1262 m = sd_json_variant_by_key(entry, "matchNotHostname");
1263 if (m) {
1264 r = per_machine_hostname_match(m, flags);
1265 if (r < 0)
1266 return r;
1267 if (r == 0)
1268 return true;
1269 }
1270
1271 return false;
1272 }
1273
1274 static int dispatch_per_machine(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
1275
1276 static const sd_json_dispatch_field per_machine_dispatch_table[] = {
1277 { "matchMachineId", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 },
1278 { "matchNotMachineId", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 },
1279 { "matchHostname", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 },
1280 { "matchNotHostname", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 },
1281 { "blobDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, blob_directory), SD_JSON_STRICT },
1282 { "blobManifest", SD_JSON_VARIANT_OBJECT, dispatch_blob_manifest, offsetof(UserRecord, blob_manifest), 0 },
1283 { "iconName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, icon_name), SD_JSON_STRICT },
1284 { "location", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, location), 0 },
1285 { "shell", SD_JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, shell), 0 },
1286 { "umask", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_access_mode, offsetof(UserRecord, umask), SD_JSON_STRICT },
1287 { "environment", SD_JSON_VARIANT_ARRAY, json_dispatch_strv_environment, offsetof(UserRecord, environment), 0 },
1288 { "timeZone", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, time_zone), SD_JSON_STRICT },
1289 { "preferredLanguage", SD_JSON_VARIANT_STRING, json_dispatch_locale, offsetof(UserRecord, preferred_language), 0 },
1290 { "additionalLanguages", SD_JSON_VARIANT_ARRAY, json_dispatch_locales, offsetof(UserRecord, additional_languages), 0 },
1291 { "niceLevel", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_nice, offsetof(UserRecord, nice_level), 0 },
1292 { "resourceLimits", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_rlimits, offsetof(UserRecord, rlimits), 0 },
1293 { "locked", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, locked), 0 },
1294 { "notBeforeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, not_before_usec), 0 },
1295 { "notAfterUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, not_after_usec), 0 },
1296 { "storage", SD_JSON_VARIANT_STRING, json_dispatch_user_storage, offsetof(UserRecord, storage), 0 },
1297 { "diskSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_size), 0 },
1298 { "diskSizeRelative", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_size_relative), 0 },
1299 { "skeletonDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, skeleton_directory), SD_JSON_STRICT },
1300 { "accessMode", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 },
1301 { "tasksMax", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, tasks_max), 0 },
1302 { "memoryHigh", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_high), 0 },
1303 { "memoryMax", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_max), 0 },
1304 { "cpuWeight", SD_JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, cpu_weight), 0 },
1305 { "ioWeight", SD_JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, io_weight), 0 },
1306 { "mountNoDevices", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, nodev), 0 },
1307 { "mountNoSuid", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, nosuid), 0 },
1308 { "mountNoExecute", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, noexec), 0 },
1309 { "cifsDomain", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_domain), SD_JSON_STRICT },
1310 { "cifsUserName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_user_name), SD_JSON_STRICT },
1311 { "cifsService", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_service), SD_JSON_STRICT },
1312 { "cifsExtraMountOptions", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_extra_mount_options), 0 },
1313 { "imagePath", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, image_path), SD_JSON_STRICT },
1314 { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 },
1315 { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 },
1316 { "memberOf", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(UserRecord, member_of), SD_JSON_RELAX },
1317 { "capabilityBoundingSet", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, capability_bounding_set), SD_JSON_STRICT },
1318 { "capabilityAmbientSet", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, capability_ambient_set), SD_JSON_STRICT },
1319 { "fileSystemType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, file_system_type), SD_JSON_STRICT },
1320 { "partitionUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 },
1321 { "luksUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 },
1322 { "fileSystemUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 },
1323 { "luksDiscard", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_tristate, offsetof(UserRecord, luks_discard), 0, },
1324 { "luksOfflineDiscard", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_tristate, offsetof(UserRecord, luks_offline_discard), 0, },
1325 { "luksCipher", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher), SD_JSON_STRICT },
1326 { "luksCipherMode", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), SD_JSON_STRICT },
1327 { "luksVolumeKeySize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 },
1328 { "luksPbkdfHashAlgorithm", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_pbkdf_hash_algorithm), SD_JSON_STRICT },
1329 { "luksPbkdfType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_pbkdf_type), SD_JSON_STRICT },
1330 { "luksPbkdfForceIterations", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_force_iterations), 0 },
1331 { "luksPbkdfTimeCostUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_time_cost_usec), 0 },
1332 { "luksPbkdfMemoryCost", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_memory_cost), 0 },
1333 { "luksPbkdfParallelThreads", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_parallel_threads), 0 },
1334 { "luksSectorSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_sector_size), 0 },
1335 { "luksExtraMountOptions", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_extra_mount_options), 0 },
1336 { "dropCaches", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, drop_caches), 0 },
1337 { "autoResizeMode", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_auto_resize_mode, offsetof(UserRecord, auto_resize_mode), 0 },
1338 { "rebalanceWeight", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_rebalance_weight, offsetof(UserRecord, rebalance_weight), 0 },
1339 { "rateLimitIntervalUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_interval_usec), 0 },
1340 { "rateLimitBurst", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_burst), 0 },
1341 { "enforcePasswordPolicy", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, enforce_password_policy), 0 },
1342 { "autoLogin", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, auto_login), 0 },
1343 { "preferredSessionType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, preferred_session_type), SD_JSON_STRICT },
1344 { "preferredSessionLauncher", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, preferred_session_launcher), SD_JSON_STRICT },
1345 { "stopDelayUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, stop_delay_usec), 0 },
1346 { "killProcesses", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, kill_processes), 0 },
1347 { "passwordChangeMinUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_min_usec), 0 },
1348 { "passwordChangeMaxUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_max_usec), 0 },
1349 { "passwordChangeWarnUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_warn_usec), 0 },
1350 { "passwordChangeInactiveUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_inactive_usec), 0 },
1351 { "passwordChangeNow", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, password_change_now), 0 },
1352 { "pkcs11TokenUri", SD_JSON_VARIANT_ARRAY, dispatch_pkcs11_uri_array, offsetof(UserRecord, pkcs11_token_uri), 0 },
1353 { "fido2HmacCredential", SD_JSON_VARIANT_ARRAY, dispatch_fido2_hmac_credential_array, 0, 0 },
1354 { "selfModifiableFields", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, self_modifiable_fields), SD_JSON_STRICT },
1355 { "selfModifiableBlobs", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, self_modifiable_blobs), SD_JSON_STRICT },
1356 { "selfModifiablePrivileged", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, self_modifiable_privileged), SD_JSON_STRICT },
1357 { "tmpLimit", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_tmpfs_limit, offsetof(UserRecord, tmp_limit), 0, },
1358 { "tmpLimitScale", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_tmpfs_limit_scale, offsetof(UserRecord, tmp_limit), 0, },
1359 { "devShmLimit", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_tmpfs_limit, offsetof(UserRecord, dev_shm_limit), 0, },
1360 { "devShmLimitScale", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_tmpfs_limit_scale, offsetof(UserRecord, dev_shm_limit), 0, },
1361 { "defaultArea", SD_JSON_VARIANT_STRING, json_dispatch_filename, offsetof(UserRecord, default_area), 0 },
1362 {},
1363 };
1364
1365 sd_json_variant *e;
1366 int r;
1367
1368 if (!variant)
1369 return 0;
1370
1371 if (!sd_json_variant_is_array(variant))
1372 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name));
1373
1374 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
1375 if (!sd_json_variant_is_object(e))
1376 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
1377
1378 r = per_machine_match(e, flags);
1379 if (r < 0)
1380 return r;
1381 if (r == 0)
1382 continue;
1383
1384 r = sd_json_dispatch(e, per_machine_dispatch_table, flags, userdata);
1385 if (r < 0)
1386 return r;
1387 }
1388
1389 return 0;
1390 }
1391
1392 static int dispatch_status(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
1393
1394 static const sd_json_dispatch_field status_dispatch_table[] = {
1395 { "diskUsage", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_usage), 0 },
1396 { "diskFree", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_free), 0 },
1397 { "diskSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_size), 0 },
1398 { "diskCeiling", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_ceiling), 0 },
1399 { "diskFloor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_floor), 0 },
1400 { "state", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, state), SD_JSON_STRICT },
1401 { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, service), SD_JSON_STRICT },
1402 { "signedLocally", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_tristate, offsetof(UserRecord, signed_locally), 0 },
1403 { "goodAuthenticationCounter", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, good_authentication_counter), 0 },
1404 { "badAuthenticationCounter", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, bad_authentication_counter), 0 },
1405 { "lastGoodAuthenticationUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, last_good_authentication_usec), 0 },
1406 { "lastBadAuthenticationUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, last_bad_authentication_usec), 0 },
1407 { "rateLimitBeginUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_begin_usec), 0 },
1408 { "rateLimitCount", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_count), 0 },
1409 { "removable", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, removable), 0 },
1410 { "accessMode", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 },
1411 { "fileSystemType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, file_system_type), SD_JSON_STRICT },
1412 { "fallbackShell", SD_JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, fallback_shell), 0 },
1413 { "fallbackHomeDirectory", SD_JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, fallback_home_directory), 0 },
1414 { "useFallback", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, use_fallback), 0 },
1415 { "defaultArea", SD_JSON_VARIANT_STRING, json_dispatch_filename, offsetof(UserRecord, default_area), 0 },
1416 { "aliases", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(UserRecord, aliases), SD_JSON_RELAX },
1417 {},
1418 };
1419
1420 sd_json_variant *m;
1421 sd_id128_t mid;
1422 int r;
1423
1424 if (!variant)
1425 return 0;
1426
1427 if (!sd_json_variant_is_object(variant))
1428 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name));
1429
1430 r = sd_id128_get_machine(&mid);
1431 if (r < 0)
1432 return json_log(variant, flags, r, "Failed to determine machine ID: %m");
1433
1434 m = sd_json_variant_by_key(variant, SD_ID128_TO_STRING(mid));
1435 if (!m)
1436 return 0;
1437
1438 return sd_json_dispatch(m, status_dispatch_table, flags, userdata);
1439 }
1440
1441 int user_record_build_image_path(UserStorage storage, const char *user_name_and_realm, char **ret) {
1442 const char *suffix;
1443 char *z;
1444
1445 assert(storage >= 0);
1446 assert(user_name_and_realm);
1447 assert(ret);
1448
1449 if (storage == USER_LUKS)
1450 suffix = ".home";
1451 else if (IN_SET(storage, USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT))
1452 suffix = ".homedir";
1453 else {
1454 *ret = NULL;
1455 return 0;
1456 }
1457
1458 z = strjoin(get_home_root(), "/", user_name_and_realm, suffix);
1459 if (!z)
1460 return -ENOMEM;
1461
1462 *ret = path_simplify(z);
1463 return 1;
1464 }
1465
1466 static int user_record_augment(UserRecord *h, sd_json_dispatch_flags_t json_flags) {
1467 int r;
1468
1469 assert(h);
1470
1471 if (!FLAGS_SET(h->mask, USER_RECORD_REGULAR))
1472 return 0;
1473
1474 assert(h->user_name);
1475
1476 if (!h->user_name_and_realm_auto && h->realm) {
1477 h->user_name_and_realm_auto = strjoin(h->user_name, "@", h->realm);
1478 if (!h->user_name_and_realm_auto)
1479 return json_log_oom(h->json, json_flags);
1480 }
1481
1482 /* Let's add in the following automatisms only for regular users, they don't make sense for any others */
1483 if (user_record_disposition(h) != USER_REGULAR)
1484 return 0;
1485
1486 if (!h->home_directory && !h->home_directory_auto) {
1487 h->home_directory_auto = path_join(get_home_root(), h->user_name);
1488 if (!h->home_directory_auto)
1489 return json_log_oom(h->json, json_flags);
1490 }
1491
1492 if (!h->image_path && !h->image_path_auto) {
1493 r = user_record_build_image_path(user_record_storage(h), user_record_user_name_and_realm(h), &h->image_path_auto);
1494 if (r < 0)
1495 return json_log(h->json, json_flags, r, "Failed to determine default image path: %m");
1496 }
1497
1498 return 0;
1499 }
1500
1501 int user_group_record_mangle(
1502 sd_json_variant *v,
1503 UserRecordLoadFlags load_flags,
1504 sd_json_variant **ret_variant,
1505 UserRecordMask *ret_mask) {
1506
1507 static const struct {
1508 UserRecordMask mask;
1509 const char *name;
1510 } mask_field[] = {
1511 { USER_RECORD_PRIVILEGED, "privileged" },
1512 { USER_RECORD_SECRET, "secret" },
1513 { USER_RECORD_BINDING, "binding" },
1514 { USER_RECORD_PER_MACHINE, "perMachine" },
1515 { USER_RECORD_STATUS, "status" },
1516 { USER_RECORD_SIGNATURE, "signature" },
1517 };
1518
1519 sd_json_dispatch_flags_t json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags);
1520 _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL;
1521 sd_json_variant *array[ELEMENTSOF(mask_field) * 2];
1522 size_t n_retain = 0;
1523 UserRecordMask m = 0;
1524 int r;
1525
1526 assert((load_flags & _USER_RECORD_MASK_MAX) == 0); /* detect mistakes when accidentally passing
1527 * UserRecordMask bit masks as UserRecordLoadFlags
1528 * value */
1529
1530 assert(v);
1531 assert(ret_variant);
1532
1533 /* Note that this function is shared with the group record parser, hence we try to be generic in our
1534 * log message wording here, to cover both cases. */
1535
1536 if (!sd_json_variant_is_object(v))
1537 return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record is not a JSON object, refusing.");
1538
1539 if (USER_RECORD_ALLOW_MASK(load_flags) == 0) /* allow nothing? */
1540 return json_log(v, json_flags, SYNTHETIC_ERRNO(EINVAL), "Nothing allowed in record, refusing.");
1541
1542 if (USER_RECORD_STRIP_MASK(load_flags) == _USER_RECORD_MASK_MAX) /* strip everything? */
1543 return json_log(v, json_flags, SYNTHETIC_ERRNO(EINVAL), "Stripping everything from record, refusing.");
1544
1545 /* Extra safety: mark sensitive parts of the JSON as such, so that they are not included in debug
1546 * output and erased from memory when we are done. We do this for any record that passes through here. */
1547 FOREACH_STRING(key,
1548 /* This section contains literal passwords and such in plain text */
1549 "secret",
1550
1551 /* Personally Identifiable Information (PII) — avoid leaking in logs */
1552 "realName",
1553 "location",
1554 "emailAddress",
1555 "birthDate")
1556 sd_json_variant_sensitive(sd_json_variant_by_key(v, key));
1557
1558 /* Check if we have the special sections and if they match our flags set */
1559 FOREACH_ELEMENT(i, mask_field) {
1560 sd_json_variant *e, *k;
1561
1562 if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags), i->mask)) {
1563 if (!w)
1564 w = sd_json_variant_ref(v);
1565
1566 r = sd_json_variant_filter(&w, STRV_MAKE(i->name));
1567 if (r < 0)
1568 return json_log(w, json_flags, r, "Failed to remove field from variant: %m");
1569
1570 continue;
1571 }
1572
1573 e = sd_json_variant_by_key_full(v, i->name, &k);
1574 if (e) {
1575 if (!FLAGS_SET(USER_RECORD_ALLOW_MASK(load_flags), i->mask))
1576 return json_log(e, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record contains '%s' field, which is not allowed.", i->name);
1577
1578 if (FLAGS_SET(load_flags, USER_RECORD_STRIP_REGULAR)) {
1579 array[n_retain++] = k;
1580 array[n_retain++] = e;
1581 }
1582
1583 m |= i->mask;
1584 } else {
1585 if (FLAGS_SET(USER_RECORD_REQUIRE_MASK(load_flags), i->mask))
1586 return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record lacks '%s' field, which is required.", i->name);
1587 }
1588 }
1589
1590 if (FLAGS_SET(load_flags, USER_RECORD_STRIP_REGULAR)) {
1591 /* If we are supposed to strip regular items, then let's instead just allocate a new object
1592 * with just the stuff we need. */
1593
1594 w = sd_json_variant_unref(w);
1595 r = sd_json_variant_new_object(&w, array, n_retain);
1596 if (r < 0)
1597 return json_log(v, json_flags, r, "Failed to allocate new object: %m");
1598 } else
1599 /* And now check if there's anything else in the record */
1600 for (size_t i = 0; i < sd_json_variant_elements(v); i += 2) {
1601 const char *f;
1602 bool special = false;
1603
1604 assert_se(f = sd_json_variant_string(sd_json_variant_by_index(v, i)));
1605
1606 FOREACH_ELEMENT(j, mask_field)
1607 if (streq(f, j->name)) { /* already covered in the loop above */
1608 special = true;
1609 continue;
1610 }
1611
1612 if (!special) {
1613 if ((load_flags & (USER_RECORD_ALLOW_REGULAR|USER_RECORD_REQUIRE_REGULAR)) == 0)
1614 return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record contains '%s' field, which is not allowed.", f);
1615
1616 m |= USER_RECORD_REGULAR;
1617 break;
1618 }
1619 }
1620
1621 if (FLAGS_SET(load_flags, USER_RECORD_REQUIRE_REGULAR) && !FLAGS_SET(m, USER_RECORD_REGULAR))
1622 return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record lacks basic identity fields, which are required.");
1623
1624 if (!FLAGS_SET(load_flags, USER_RECORD_EMPTY_OK) && m == 0)
1625 return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record is empty.");
1626
1627 if (w)
1628 *ret_variant = TAKE_PTR(w);
1629 else
1630 *ret_variant = sd_json_variant_ref(v);
1631
1632 if (ret_mask)
1633 *ret_mask = m;
1634 return 0;
1635 }
1636
1637 int user_record_load(UserRecord *h, sd_json_variant *v, UserRecordLoadFlags load_flags) {
1638
1639 static const sd_json_dispatch_field user_dispatch_table[] = {
1640 { "userName", SD_JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(UserRecord, user_name), SD_JSON_RELAX },
1641 { "aliases", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(UserRecord, aliases), SD_JSON_RELAX },
1642 { "realm", SD_JSON_VARIANT_STRING, json_dispatch_realm, offsetof(UserRecord, realm), 0 },
1643 { "uuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, uuid), 0 },
1644 { "blobDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, blob_directory), SD_JSON_STRICT },
1645 { "blobManifest", SD_JSON_VARIANT_OBJECT, dispatch_blob_manifest, offsetof(UserRecord, blob_manifest), 0 },
1646 { "realName", SD_JSON_VARIANT_STRING, json_dispatch_gecos, offsetof(UserRecord, real_name), 0 },
1647 { "emailAddress", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, email_address), SD_JSON_STRICT },
1648 { "iconName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, icon_name), SD_JSON_STRICT },
1649 { "location", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, location), 0 },
1650 { "birthDate", SD_JSON_VARIANT_STRING, json_dispatch_birth_date, offsetof(UserRecord, birth_date), 0 },
1651 { "disposition", SD_JSON_VARIANT_STRING, json_dispatch_user_disposition, offsetof(UserRecord, disposition), 0 },
1652 { "lastChangeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, last_change_usec), 0 },
1653 { "lastPasswordChangeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, last_password_change_usec), 0 },
1654 { "shell", SD_JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, shell), 0 },
1655 { "umask", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_access_mode, offsetof(UserRecord, umask), SD_JSON_STRICT },
1656 { "environment", SD_JSON_VARIANT_ARRAY, json_dispatch_strv_environment, offsetof(UserRecord, environment), 0 },
1657 { "timeZone", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, time_zone), SD_JSON_STRICT },
1658 { "preferredLanguage", SD_JSON_VARIANT_STRING, json_dispatch_locale, offsetof(UserRecord, preferred_language), 0 },
1659 { "additionalLanguages", SD_JSON_VARIANT_ARRAY, json_dispatch_locales, offsetof(UserRecord, additional_languages), 0 },
1660 { "niceLevel", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_nice, offsetof(UserRecord, nice_level), 0 },
1661 { "resourceLimits", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_rlimits, offsetof(UserRecord, rlimits), 0 },
1662 { "locked", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, locked), 0 },
1663 { "notBeforeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, not_before_usec), 0 },
1664 { "notAfterUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, not_after_usec), 0 },
1665 { "storage", SD_JSON_VARIANT_STRING, json_dispatch_user_storage, offsetof(UserRecord, storage), 0 },
1666 { "diskSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_size), 0 },
1667 { "diskSizeRelative", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_size_relative), 0 },
1668 { "skeletonDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, skeleton_directory), SD_JSON_STRICT },
1669 { "accessMode", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 },
1670 { "tasksMax", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, tasks_max), 0 },
1671 { "memoryHigh", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_high), 0 },
1672 { "memoryMax", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_max), 0 },
1673 { "cpuWeight", SD_JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, cpu_weight), 0 },
1674 { "ioWeight", SD_JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, io_weight), 0 },
1675 { "mountNoDevices", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, nodev), 0 },
1676 { "mountNoSuid", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, nosuid), 0 },
1677 { "mountNoExecute", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, noexec), 0 },
1678 { "cifsDomain", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_domain), SD_JSON_STRICT },
1679 { "cifsUserName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_user_name), SD_JSON_STRICT },
1680 { "cifsService", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_service), SD_JSON_STRICT },
1681 { "cifsExtraMountOptions", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_extra_mount_options), 0 },
1682 { "imagePath", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, image_path), SD_JSON_STRICT },
1683 { "homeDirectory", SD_JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, home_directory), 0 },
1684 { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 },
1685 { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 },
1686 { "memberOf", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(UserRecord, member_of), SD_JSON_RELAX },
1687 { "capabilityBoundingSet", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, capability_bounding_set), SD_JSON_STRICT },
1688 { "capabilityAmbientSet", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, capability_ambient_set), SD_JSON_STRICT },
1689 { "fileSystemType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, file_system_type), SD_JSON_STRICT },
1690 { "partitionUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 },
1691 { "luksUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 },
1692 { "fileSystemUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 },
1693 { "luksDiscard", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_tristate, offsetof(UserRecord, luks_discard), 0 },
1694 { "luksOfflineDiscard", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_tristate, offsetof(UserRecord, luks_offline_discard), 0 },
1695 { "luksCipher", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher), SD_JSON_STRICT },
1696 { "luksCipherMode", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), SD_JSON_STRICT },
1697 { "luksVolumeKeySize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 },
1698 { "luksPbkdfHashAlgorithm", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_pbkdf_hash_algorithm), SD_JSON_STRICT },
1699 { "luksPbkdfType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_pbkdf_type), SD_JSON_STRICT },
1700 { "luksPbkdfForceIterations", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_force_iterations), 0 },
1701 { "luksPbkdfTimeCostUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_time_cost_usec), 0 },
1702 { "luksPbkdfMemoryCost", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_memory_cost), 0 },
1703 { "luksPbkdfParallelThreads", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_parallel_threads), 0 },
1704 { "luksSectorSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_sector_size), 0 },
1705 { "luksExtraMountOptions", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_extra_mount_options), 0 },
1706 { "dropCaches", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, drop_caches), 0 },
1707 { "autoResizeMode", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_auto_resize_mode, offsetof(UserRecord, auto_resize_mode), 0 },
1708 { "rebalanceWeight", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_rebalance_weight, offsetof(UserRecord, rebalance_weight), 0 },
1709 { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, service), SD_JSON_STRICT },
1710 { "rateLimitIntervalUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_interval_usec), 0 },
1711 { "rateLimitBurst", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_burst), 0 },
1712 { "enforcePasswordPolicy", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, enforce_password_policy), 0 },
1713 { "autoLogin", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, auto_login), 0 },
1714 { "preferredSessionType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, preferred_session_type), SD_JSON_STRICT },
1715 { "preferredSessionLauncher", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, preferred_session_launcher), SD_JSON_STRICT },
1716 { "stopDelayUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, stop_delay_usec), 0 },
1717 { "killProcesses", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, kill_processes), 0 },
1718 { "passwordChangeMinUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_min_usec), 0 },
1719 { "passwordChangeMaxUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_max_usec), 0 },
1720 { "passwordChangeWarnUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_warn_usec), 0 },
1721 { "passwordChangeInactiveUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_inactive_usec), 0 },
1722 { "passwordChangeNow", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, password_change_now), 0 },
1723 { "pkcs11TokenUri", SD_JSON_VARIANT_ARRAY, dispatch_pkcs11_uri_array, offsetof(UserRecord, pkcs11_token_uri), 0 },
1724 { "fido2HmacCredential", SD_JSON_VARIANT_ARRAY, dispatch_fido2_hmac_credential_array, 0, 0 },
1725 { "recoveryKeyType", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, recovery_key_type), 0 },
1726 { "selfModifiableFields", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, self_modifiable_fields), SD_JSON_STRICT },
1727 { "selfModifiableBlobs", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, self_modifiable_blobs), SD_JSON_STRICT },
1728 { "selfModifiablePrivileged", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, self_modifiable_privileged), SD_JSON_STRICT },
1729 { "tmpLimit", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_tmpfs_limit, offsetof(UserRecord, tmp_limit), 0, },
1730 { "tmpLimitScale", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_tmpfs_limit_scale, offsetof(UserRecord, tmp_limit), 0, },
1731 { "devShmLimit", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_tmpfs_limit, offsetof(UserRecord, dev_shm_limit), 0, },
1732 { "devShmLimitScale", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_tmpfs_limit_scale, offsetof(UserRecord, dev_shm_limit), 0, },
1733 { "defaultArea", SD_JSON_VARIANT_STRING, json_dispatch_filename, offsetof(UserRecord, default_area), 0 },
1734
1735 { "secret", SD_JSON_VARIANT_OBJECT, dispatch_secret, 0, 0 },
1736 { "privileged", SD_JSON_VARIANT_OBJECT, dispatch_privileged, 0, 0 },
1737
1738 /* Ignore the perMachine, binding, status stuff here, and process it later, so that it overrides whatever is set above */
1739 { "perMachine", SD_JSON_VARIANT_ARRAY, NULL, 0, 0 },
1740 { "binding", SD_JSON_VARIANT_OBJECT, NULL, 0, 0 },
1741 { "status", SD_JSON_VARIANT_OBJECT, NULL, 0, 0 },
1742
1743 /* Ignore 'signature', we check it with explicit accessors instead */
1744 { "signature", SD_JSON_VARIANT_ARRAY, NULL, 0, 0 },
1745 {},
1746 };
1747
1748 sd_json_dispatch_flags_t json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags);
1749 int r;
1750
1751 assert(h);
1752 assert(!h->json);
1753
1754 /* Note that this call will leave a half-initialized record around on failure! */
1755
1756 r = user_group_record_mangle(v, load_flags, &h->json, &h->mask);
1757 if (r < 0)
1758 return r;
1759
1760 r = sd_json_dispatch(h->json, user_dispatch_table, json_flags | SD_JSON_ALLOW_EXTENSIONS, h);
1761 if (r < 0)
1762 return r;
1763
1764 /* During the parsing operation above we ignored the 'perMachine', 'binding' and 'status' fields,
1765 * since we want them to override the global options. Let's process them now. */
1766
1767 r = dispatch_per_machine("perMachine", sd_json_variant_by_key(h->json, "perMachine"), json_flags, h);
1768 if (r < 0)
1769 return r;
1770
1771 r = dispatch_binding("binding", sd_json_variant_by_key(h->json, "binding"), json_flags, h);
1772 if (r < 0)
1773 return r;
1774
1775 r = dispatch_status("status", sd_json_variant_by_key(h->json, "status"), json_flags, h);
1776 if (r < 0)
1777 return r;
1778
1779 if (FLAGS_SET(h->mask, USER_RECORD_REGULAR) && !h->user_name)
1780 return json_log(h->json, json_flags, SYNTHETIC_ERRNO(EINVAL), "User name field missing, refusing.");
1781
1782 r = user_record_augment(h, json_flags);
1783 if (r < 0)
1784 return r;
1785
1786 return 0;
1787 }
1788
1789 int user_record_build(UserRecord **ret, ...) {
1790 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
1791 _cleanup_(user_record_unrefp) UserRecord *u = NULL;
1792 va_list ap;
1793 int r;
1794
1795 assert(ret);
1796
1797 va_start(ap, ret);
1798 r = sd_json_buildv(&v, ap);
1799 va_end(ap);
1800
1801 if (r < 0)
1802 return r;
1803
1804 u = user_record_new();
1805 if (!u)
1806 return -ENOMEM;
1807
1808 r = user_record_load(u, v, USER_RECORD_LOAD_FULL);
1809 if (r < 0)
1810 return r;
1811
1812 *ret = TAKE_PTR(u);
1813 return 0;
1814 }
1815
1816 const char* user_record_user_name_and_realm(UserRecord *h) {
1817 assert(h);
1818
1819 /* Return the pre-initialized joined string if it is defined */
1820 if (h->user_name_and_realm_auto)
1821 return h->user_name_and_realm_auto;
1822
1823 /* If it's not defined then we cannot have a realm */
1824 assert(!h->realm);
1825 return h->user_name;
1826 }
1827
1828 UserStorage user_record_storage(UserRecord *h) {
1829 assert(h);
1830
1831 if (h->storage >= 0)
1832 return h->storage;
1833
1834 return USER_CLASSIC;
1835 }
1836
1837 const char* user_record_file_system_type(UserRecord *h) {
1838 assert(h);
1839
1840 return h->file_system_type ?: "btrfs";
1841 }
1842
1843 const char* user_record_skeleton_directory(UserRecord *h) {
1844 assert(h);
1845
1846 return h->skeleton_directory ?: "/etc/skel";
1847 }
1848
1849 mode_t user_record_access_mode(UserRecord *h) {
1850 assert(h);
1851
1852 return h->access_mode != MODE_INVALID ? h->access_mode : 0700;
1853 }
1854
1855 static const char *user_record_home_directory_real(UserRecord *h) {
1856 assert(h);
1857
1858 if (h->home_directory)
1859 return h->home_directory;
1860 if (h->home_directory_auto)
1861 return h->home_directory_auto;
1862
1863 /* The root user is special, hence be special about it */
1864 if (user_record_is_root(h))
1865 return "/root";
1866
1867 return "/";
1868 }
1869
1870 const char* user_record_home_directory(UserRecord *h) {
1871 assert(h);
1872
1873 if (h->use_fallback && h->fallback_home_directory)
1874 return h->fallback_home_directory;
1875
1876 return user_record_home_directory_real(h);
1877 }
1878
1879 const char* user_record_image_path(UserRecord *h) {
1880 assert(h);
1881
1882 if (h->image_path)
1883 return h->image_path;
1884 if (h->image_path_auto)
1885 return h->image_path_auto;
1886
1887 /* For some storage types the image is the home directory itself. (But let's ignore the fallback logic for it) */
1888 return IN_SET(user_record_storage(h), USER_CLASSIC, USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT) ?
1889 user_record_home_directory_real(h) : NULL;
1890 }
1891
1892 static bool user_record_image_is_blockdev(UserRecord *h) {
1893 assert(h);
1894
1895 const char *p = user_record_image_path(h);
1896 if (!p)
1897 return false;
1898
1899 return path_startswith(p, "/dev/");
1900 }
1901
1902 const char* user_record_cifs_user_name(UserRecord *h) {
1903 assert(h);
1904
1905 return h->cifs_user_name ?: h->user_name;
1906 }
1907
1908 unsigned long user_record_mount_flags(UserRecord *h) {
1909 assert(h);
1910
1911 return (h->nosuid ? MS_NOSUID : 0) |
1912 (h->noexec ? MS_NOEXEC : 0) |
1913 (h->nodev ? MS_NODEV : 0);
1914 }
1915
1916 static const char *user_record_shell_real(UserRecord *h) {
1917 assert(h);
1918
1919 if (h->shell)
1920 return h->shell;
1921
1922 if (user_record_is_root(h))
1923 return "/bin/sh";
1924
1925 if (user_record_disposition(h) == USER_REGULAR)
1926 return DEFAULT_USER_SHELL;
1927
1928 return NOLOGIN;
1929 }
1930
1931 const char* user_record_shell(UserRecord *h) {
1932 const char *shell;
1933
1934 assert(h);
1935
1936 shell = user_record_shell_real(h);
1937
1938 /* Return fallback shall if we are told so — except if the primary shell is already a nologin shell,
1939 * then let's not risk anything. */
1940 if (h->use_fallback && h->fallback_shell)
1941 return is_nologin_shell(shell) ? NOLOGIN : h->fallback_shell;
1942
1943 return shell;
1944 }
1945
1946 const char* user_record_real_name(UserRecord *h) {
1947 assert(h);
1948
1949 return h->real_name ?: h->user_name;
1950 }
1951
1952 bool user_record_luks_discard(UserRecord *h) {
1953 assert(h);
1954
1955 if (h->luks_discard >= 0)
1956 return h->luks_discard;
1957
1958 /* Use discard by default if we are referring to a real block device, but not when operating on a
1959 * loopback device. We want to optimize for SSD and flash storage after all, but we should be careful
1960 * when storing stuff on top of regular file systems in loopback files as doing discard then would
1961 * mean thin provisioning and we should not do that willy-nilly since it means we'll risk EIO later
1962 * on should the disk space to back our file systems not be available. */
1963
1964 return user_record_image_is_blockdev(h);
1965 }
1966
1967 bool user_record_luks_offline_discard(UserRecord *h) {
1968 const char *ip;
1969
1970 assert(h);
1971
1972 if (h->luks_offline_discard >= 0)
1973 return h->luks_offline_discard;
1974
1975 /* Discard while we are logged out should generally be a good idea, except when operating directly on
1976 * physical media, where we should just bind it to the online discard mode. */
1977
1978 ip = user_record_image_path(h);
1979 if (!ip)
1980 return false;
1981
1982 if (path_startswith(ip, "/dev/"))
1983 return user_record_luks_discard(h);
1984
1985 return true;
1986 }
1987
1988 const char* user_record_luks_cipher(UserRecord *h) {
1989 assert(h);
1990
1991 return h->luks_cipher ?: "aes";
1992 }
1993
1994 const char* user_record_luks_cipher_mode(UserRecord *h) {
1995 assert(h);
1996
1997 return h->luks_cipher_mode ?: "xts-plain64";
1998 }
1999
2000 uint64_t user_record_luks_volume_key_size(UserRecord *h) {
2001 assert(h);
2002
2003 /* We return a value here that can be cast without loss into size_t which is what libcrypsetup expects */
2004
2005 if (h->luks_volume_key_size == UINT64_MAX)
2006 return 256 / 8;
2007
2008 return MIN(h->luks_volume_key_size, SIZE_MAX);
2009 }
2010
2011 const char* user_record_luks_pbkdf_type(UserRecord *h) {
2012 assert(h);
2013
2014 return h->luks_pbkdf_type ?: "argon2id";
2015 }
2016
2017 uint64_t user_record_luks_pbkdf_force_iterations(UserRecord *h) {
2018 assert(h);
2019
2020 /* propagate default "benchmark" mode as itself */
2021 if (h->luks_pbkdf_force_iterations == UINT64_MAX)
2022 return UINT64_MAX;
2023
2024 /* clamp everything else to actually accepted number of iterations of libcryptsetup */
2025 return CLAMP(h->luks_pbkdf_force_iterations, 1U, UINT32_MAX);
2026 }
2027
2028 uint64_t user_record_luks_pbkdf_time_cost_usec(UserRecord *h) {
2029 assert(h);
2030
2031 /* Returns a value with ms granularity, since that's what libcryptsetup expects */
2032
2033 if (h->luks_pbkdf_time_cost_usec == UINT64_MAX)
2034 return 500 * USEC_PER_MSEC; /* We default to 500ms, in contrast to libcryptsetup's 2s, which is just awfully slow on every login */
2035
2036 return MIN(DIV_ROUND_UP(h->luks_pbkdf_time_cost_usec, USEC_PER_MSEC), UINT32_MAX) * USEC_PER_MSEC;
2037 }
2038
2039 uint64_t user_record_luks_pbkdf_memory_cost(UserRecord *h) {
2040 assert(h);
2041
2042 /* Returns a value with kb granularity, since that's what libcryptsetup expects */
2043 if (h->luks_pbkdf_memory_cost == UINT64_MAX)
2044 return streq(user_record_luks_pbkdf_type(h), "pbkdf2") ? 0 : /* doesn't apply for simple pbkdf2 */
2045 64*1024*1024; /* We default to 64M, since this should work on smaller systems too */
2046
2047 return MIN(DIV_ROUND_UP(h->luks_pbkdf_memory_cost, 1024), UINT32_MAX) * 1024;
2048 }
2049
2050 uint64_t user_record_luks_pbkdf_parallel_threads(UserRecord *h) {
2051 assert(h);
2052
2053 if (h->luks_pbkdf_parallel_threads == UINT64_MAX)
2054 return streq(user_record_luks_pbkdf_type(h), "pbkdf2") ? 0 : /* doesn't apply for simple pbkdf2 */
2055 1; /* We default to 1, since this should work on smaller systems too */
2056
2057 return MIN(h->luks_pbkdf_parallel_threads, UINT32_MAX);
2058 }
2059
2060 uint64_t user_record_luks_sector_size(UserRecord *h) {
2061 assert(h);
2062
2063 if (h->luks_sector_size == UINT64_MAX)
2064 return 512;
2065
2066 /* Allow up to 4K due to dm-crypt support and 4K alignment by the homed LUKS backend */
2067 return CLAMP(UINT64_C(1) << (63 - __builtin_clzll(h->luks_sector_size)), 512U, 4096U);
2068 }
2069
2070 const char* user_record_luks_pbkdf_hash_algorithm(UserRecord *h) {
2071 assert(h);
2072
2073 return h->luks_pbkdf_hash_algorithm ?: "sha512";
2074 }
2075
2076 gid_t user_record_gid(UserRecord *h) {
2077 assert(h);
2078
2079 if (gid_is_valid(h->gid))
2080 return h->gid;
2081
2082 return (gid_t) h->uid;
2083 }
2084
2085 UserDisposition user_record_disposition(UserRecord *h) {
2086 assert(h);
2087
2088 if (h->disposition >= 0)
2089 return h->disposition;
2090
2091 /* If not declared, derive from UID */
2092
2093 if (!uid_is_valid(h->uid))
2094 return _USER_DISPOSITION_INVALID;
2095
2096 if (user_record_is_root(h) || user_record_is_nobody(h))
2097 return USER_INTRINSIC;
2098
2099 if (uid_is_system(h->uid))
2100 return USER_SYSTEM;
2101
2102 if (uid_is_dynamic(h->uid) || uid_is_greeter(h->uid))
2103 return USER_DYNAMIC;
2104
2105 if (uid_is_container(h->uid))
2106 return USER_CONTAINER;
2107
2108 if (uid_is_foreign(h->uid))
2109 return USER_FOREIGN;
2110
2111 if (h->uid > INT32_MAX)
2112 return USER_RESERVED;
2113
2114 return USER_REGULAR;
2115 }
2116
2117 int user_record_removable(UserRecord *h) {
2118 UserStorage storage;
2119 assert(h);
2120
2121 if (h->removable >= 0)
2122 return h->removable;
2123
2124 /* Refuse to decide for classic records */
2125 storage = user_record_storage(h);
2126 if (h->storage < 0 || h->storage == USER_CLASSIC)
2127 return -1;
2128
2129 /* For now consider only LUKS home directories with a reference by path as removable */
2130 return storage == USER_LUKS && user_record_image_is_blockdev(h);
2131 }
2132
2133 uint64_t user_record_ratelimit_interval_usec(UserRecord *h) {
2134 assert(h);
2135
2136 if (h->ratelimit_interval_usec == UINT64_MAX)
2137 return DEFAULT_RATELIMIT_INTERVAL_USEC;
2138
2139 return h->ratelimit_interval_usec;
2140 }
2141
2142 uint64_t user_record_ratelimit_burst(UserRecord *h) {
2143 assert(h);
2144
2145 if (h->ratelimit_burst == UINT64_MAX)
2146 return DEFAULT_RATELIMIT_BURST;
2147
2148 return h->ratelimit_burst;
2149 }
2150
2151 bool user_record_can_authenticate(UserRecord *h) {
2152 assert(h);
2153
2154 /* Returns true if there's some form of property configured that the user can authenticate against */
2155
2156 if (h->n_pkcs11_encrypted_key > 0)
2157 return true;
2158
2159 if (h->n_fido2_hmac_salt > 0)
2160 return true;
2161
2162 return !strv_isempty(h->hashed_password);
2163 }
2164
2165 bool user_record_drop_caches(UserRecord *h) {
2166 assert(h);
2167
2168 if (h->drop_caches >= 0)
2169 return h->drop_caches;
2170
2171 /* By default drop caches on fscrypt, not otherwise. */
2172 return user_record_storage(h) == USER_FSCRYPT;
2173 }
2174
2175 AutoResizeMode user_record_auto_resize_mode(UserRecord *h) {
2176 assert(h);
2177
2178 if (h->auto_resize_mode >= 0)
2179 return h->auto_resize_mode;
2180
2181 return user_record_storage(h) == USER_LUKS ? AUTO_RESIZE_SHRINK_AND_GROW : AUTO_RESIZE_OFF;
2182 }
2183
2184 uint64_t user_record_rebalance_weight(UserRecord *h) {
2185 assert(h);
2186
2187 if (h->rebalance_weight == REBALANCE_WEIGHT_UNSET)
2188 return REBALANCE_WEIGHT_DEFAULT;
2189
2190 return h->rebalance_weight;
2191 }
2192
2193 static uint64_t parse_caps_strv(char **l) {
2194 uint64_t c = 0;
2195 int r;
2196
2197 STRV_FOREACH(i, l) {
2198 r = capability_from_name(*i);
2199 if (r < 0)
2200 log_debug_errno(r, "Don't know capability '%s', ignoring: %m", *i);
2201 else
2202 c |= UINT64_C(1) << r;
2203 }
2204
2205 return c;
2206 }
2207
2208 uint64_t user_record_capability_bounding_set(UserRecord *h) {
2209 assert(h);
2210
2211 /* Returns UINT64_MAX if no bounding set is configured (!) */
2212
2213 if (!h->capability_bounding_set)
2214 return UINT64_MAX;
2215
2216 return parse_caps_strv(h->capability_bounding_set);
2217 }
2218
2219 uint64_t user_record_capability_ambient_set(UserRecord *h) {
2220 assert(h);
2221
2222 /* Returns UINT64_MAX if no ambient set is configured (!) */
2223
2224 if (!h->capability_ambient_set)
2225 return UINT64_MAX;
2226
2227 return parse_caps_strv(h->capability_ambient_set) & user_record_capability_bounding_set(h);
2228 }
2229
2230 int user_record_languages(UserRecord *h, char ***ret) {
2231 _cleanup_strv_free_ char **l = NULL;
2232 int r;
2233
2234 assert(h);
2235 assert(ret);
2236
2237 if (h->preferred_language) {
2238 l = strv_new(h->preferred_language);
2239 if (!l)
2240 return -ENOMEM;
2241 }
2242
2243 r = strv_extend_strv(&l, h->additional_languages, /* filter_duplicates= */ true);
2244 if (r < 0)
2245 return r;
2246
2247 *ret = TAKE_PTR(l);
2248 return 0;
2249 }
2250
2251 uint32_t user_record_tmp_limit_scale(UserRecord *h) {
2252 assert(h);
2253
2254 if (h->tmp_limit.is_set)
2255 return h->tmp_limit.limit_scale;
2256
2257 /* By default grant regular users only 80% quota */
2258 if (user_record_disposition(h) == USER_REGULAR)
2259 return UINT32_SCALE_FROM_PERCENT(80);
2260
2261 return UINT32_MAX;
2262 }
2263
2264 uint32_t user_record_dev_shm_limit_scale(UserRecord *h) {
2265 assert(h);
2266
2267 if (h->dev_shm_limit.is_set)
2268 return h->dev_shm_limit.limit_scale;
2269
2270 /* By default grant regular users only 80% quota */
2271 if (user_record_disposition(h) == USER_REGULAR)
2272 return UINT32_SCALE_FROM_PERCENT(80);
2273
2274 return UINT32_MAX;
2275 }
2276
2277 const char** user_record_self_modifiable_fields(UserRecord *h) {
2278 /* As a rule of thumb: a setting is safe if it cannot be used by a
2279 * user to give themselves some unfair advantage over other users on
2280 * a given system. */
2281 static const char *const default_fields[] = {
2282 /* For display purposes */
2283 "realName",
2284 "emailAddress", /* Just the $EMAIL env var */
2285 "iconName",
2286 "location",
2287
2288 /* Basic account settings */
2289 "shell",
2290 "umask",
2291 "environment",
2292 "timeZone",
2293 "preferredLanguage",
2294 "additionalLanguages",
2295 "preferredSessionLauncher",
2296 "preferredSessionType",
2297 "defaultArea",
2298
2299 /* Authentication methods */
2300 "pkcs11TokenUri",
2301 "fido2HmacCredential",
2302 "recoveryKeyType",
2303
2304 "lastChangeUSec", /* Necessary to be able to change record at all */
2305 "lastPasswordChangeUSec", /* Ditto, but for authentication methods */
2306 NULL
2307 };
2308
2309 assert(h);
2310
2311 /* Note: if the self_modifiable_fields field in UserRecord is NULL we'll apply a default, if we have
2312 * one. If it is a non-NULL empty strv, we'll report it as explicit empty list. When the field is
2313 * NULL and we have no default list we'll return NULL. */
2314
2315 /* Note that we intentionally distinguish between NULL and an empty array here */
2316 if (h->self_modifiable_fields)
2317 return (const char**) h->self_modifiable_fields;
2318
2319 return user_record_disposition(h) == USER_REGULAR ? (const char**) default_fields : NULL;
2320 }
2321
2322 const char** user_record_self_modifiable_blobs(UserRecord *h) {
2323 static const char *const default_blobs[] = {
2324 /* For display purposes */
2325 "avatar",
2326 "login-background",
2327 NULL
2328 };
2329
2330 assert(h);
2331
2332 /* Note that we intentionally distinguish between NULL and an empty array here */
2333 if (h->self_modifiable_blobs)
2334 return (const char**) h->self_modifiable_blobs;
2335
2336 return user_record_disposition(h) == USER_REGULAR ? (const char**) default_blobs : NULL;
2337 }
2338
2339 const char** user_record_self_modifiable_privileged(UserRecord *h) {
2340 static const char *const default_fields[] = {
2341 /* For display purposes */
2342 "passwordHint",
2343
2344 /* Authentication methods */
2345 "hashedPassword",
2346 "pkcs11EncryptedKey",
2347 "fido2HmacSalt",
2348 "recoveryKey",
2349
2350 "sshAuthorizedKeys", /* Basically just ~/.ssh/authorized_keys */
2351 NULL
2352 };
2353
2354 assert(h);
2355
2356 /* Note that we intentionally distinguish between NULL and an empty array here */
2357 if (h->self_modifiable_privileged)
2358 return (const char**) h->self_modifiable_privileged;
2359
2360 return user_record_disposition(h) == USER_REGULAR ? (const char**) default_fields : NULL;
2361 }
2362
2363 static int remove_self_modifiable_json_fields_common(UserRecord *current, sd_json_variant **target) {
2364 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *blobs = NULL;
2365 char **allowed;
2366 int r;
2367
2368 assert(current);
2369 assert(target);
2370
2371 if (!sd_json_variant_is_object(*target))
2372 return -EINVAL;
2373
2374 v = sd_json_variant_ref(*target);
2375
2376 /* Handle basic fields */
2377 allowed = (char**) user_record_self_modifiable_fields(current);
2378 r = sd_json_variant_filter(&v, allowed);
2379 if (r < 0)
2380 return r;
2381
2382 /* Handle blobs */
2383 blobs = sd_json_variant_ref(sd_json_variant_by_key(v, "blobManifest"));
2384 if (blobs) {
2385 /* The blobManifest contains the sha256 hashes of the blobs,
2386 * which are enforced by the service managing the user. So, by
2387 * comparing the blob manifests like this, we're actually comparing
2388 * the contents of the blob directories & files */
2389
2390 allowed = (char**) user_record_self_modifiable_blobs(current);
2391 r = sd_json_variant_filter(&blobs, allowed);
2392 if (r < 0)
2393 return r;
2394
2395 if (sd_json_variant_is_blank_object(blobs))
2396 r = sd_json_variant_filter(&v, STRV_MAKE("blobManifest"));
2397 else
2398 r = sd_json_variant_set_field(&v, "blobManifest", blobs);
2399 if (r < 0)
2400 return r;
2401 }
2402
2403 return json_variant_unref_and_replace(*target, v);
2404 }
2405
2406 static int remove_self_modifiable_json_fields(UserRecord *current, UserRecord *h, sd_json_variant **ret) {
2407 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *privileged = NULL;
2408 sd_json_variant *per_machine;
2409 char **allowed;
2410 int r;
2411
2412 assert(current);
2413 assert(h);
2414 assert(ret);
2415
2416 r = user_group_record_mangle(h->json, USER_RECORD_EXTRACT_SIGNABLE|USER_RECORD_PERMISSIVE, &v, NULL);
2417 if (r < 0)
2418 return r;
2419
2420 /* Handle the regular section */
2421 r = remove_self_modifiable_json_fields_common(current, &v);
2422 if (r < 0)
2423 return r;
2424
2425 /* Handle the perMachine section */
2426 per_machine = sd_json_variant_by_key(v, "perMachine");
2427 if (per_machine) {
2428 _cleanup_(sd_json_variant_unrefp) sd_json_variant *new_per_machine = NULL;
2429 sd_json_variant *e;
2430
2431 if (!sd_json_variant_is_array(per_machine))
2432 return -EINVAL;
2433
2434 JSON_VARIANT_ARRAY_FOREACH(e, per_machine) {
2435 _cleanup_(sd_json_variant_unrefp) sd_json_variant *z = NULL;
2436
2437 if (!sd_json_variant_is_object(e))
2438 return -EINVAL;
2439
2440 r = per_machine_match(e, 0);
2441 if (r < 0)
2442 return r;
2443 if (r == 0) {
2444 /* It's only permissible to change anything inside of matching perMachine sections */
2445 r = sd_json_variant_append_array(&new_per_machine, e);
2446 if (r < 0)
2447 return r;
2448 continue;
2449 }
2450
2451 z = sd_json_variant_ref(e);
2452
2453 r = remove_self_modifiable_json_fields_common(current, &z);
2454 if (r < 0)
2455 return r;
2456
2457 if (!sd_json_variant_is_blank_object(z)) {
2458 r = sd_json_variant_append_array(&new_per_machine, z);
2459 if (r < 0)
2460 return r;
2461 }
2462 }
2463
2464 if (sd_json_variant_is_blank_array(new_per_machine))
2465 r = sd_json_variant_filter(&v, STRV_MAKE("perMachine"));
2466 else
2467 r = sd_json_variant_set_field(&v, "perMachine", new_per_machine);
2468 if (r < 0)
2469 return r;
2470 }
2471
2472 /* Handle the privileged section */
2473 privileged = sd_json_variant_ref(sd_json_variant_by_key(v, "privileged"));
2474 if (privileged) {
2475 allowed = (char**) user_record_self_modifiable_privileged(current);
2476 r = sd_json_variant_filter(&privileged, allowed);
2477 if (r < 0)
2478 return r;
2479
2480 if (sd_json_variant_is_blank_object(privileged))
2481 r = sd_json_variant_filter(&v, STRV_MAKE("privileged"));
2482 else
2483 r = sd_json_variant_set_field(&v, "privileged", privileged);
2484 if (r < 0)
2485 return r;
2486 }
2487
2488 return json_variant_unref_and_replace(*ret, v);
2489 }
2490
2491 int user_record_self_changes_allowed(UserRecord *current, UserRecord *incoming) {
2492 _cleanup_(sd_json_variant_unrefp) sd_json_variant *vc = NULL, *vi = NULL;
2493 int r;
2494
2495 assert(current);
2496 assert(incoming);
2497
2498 /* We remove the fields that the user is allowed to change and then
2499 * compare the resulting JSON records. If they are not equal, that
2500 * means a disallowed field has been changed and thus we should
2501 * require administrator permission to apply the changes. */
2502
2503 r = remove_self_modifiable_json_fields(current, current, &vc);
2504 if (r < 0)
2505 return r;
2506
2507 /* Note that we use `current` as the source of the allowlist, and not
2508 * `incoming`. This prevents the user from adding fields. Consider a
2509 * scenario that would've been possible if we had messed up this check:
2510 *
2511 * 1) A user starts out with no group memberships and no custom allowlist.
2512 * Thus, this user is not an administrator, and the `memberOf` and
2513 * `selfModifiableFields` fields are unset in their record.
2514 * 2) This user crafts a request to add the following to their record:
2515 * { "memberOf": ["wheel"], "selfModifiableFields": ["memberOf", "selfModifiableFields"] }
2516 * 3) We remove the `memberOf` and `selfModifiabileFields` fields from `incoming`
2517 * 4) `current` and `incoming` compare as equal, so we let the change happen
2518 * 5) the user has granted themselves administrator privileges
2519 */
2520 r = remove_self_modifiable_json_fields(current, incoming, &vi);
2521 if (r < 0)
2522 return r;
2523
2524 return sd_json_variant_equal(vc, vi);
2525 }
2526
2527 uint64_t user_record_ratelimit_next_try(UserRecord *h) {
2528 assert(h);
2529
2530 /* Calculates when the it's possible to login next. Returns:
2531 *
2532 * UINT64_MAX → Nothing known
2533 * 0 → Right away
2534 * Any other → Next time in CLOCK_REALTIME in usec (which could be in the past)
2535 */
2536
2537 if (h->ratelimit_begin_usec == UINT64_MAX ||
2538 h->ratelimit_count == UINT64_MAX)
2539 return UINT64_MAX;
2540
2541 if (h->ratelimit_begin_usec > now(CLOCK_REALTIME)) /* If the ratelimit time is in the future, then
2542 * the local clock is probably incorrect. Let's
2543 * not refuse login then. */
2544 return UINT64_MAX;
2545
2546 if (h->ratelimit_count < user_record_ratelimit_burst(h))
2547 return 0;
2548
2549 return usec_add(h->ratelimit_begin_usec, user_record_ratelimit_interval_usec(h));
2550 }
2551
2552 bool user_record_equal(UserRecord *a, UserRecord *b) {
2553 assert(a);
2554 assert(b);
2555
2556 /* We assume that when a record is modified its JSON data is updated at the same time, hence it's
2557 * sufficient to compare the JSON data. */
2558
2559 return sd_json_variant_equal(a->json, b->json);
2560 }
2561
2562 bool user_record_compatible(UserRecord *a, UserRecord *b) {
2563 assert(a);
2564 assert(b);
2565
2566 /* If either lacks the regular section, we can't really decide, let's hence say they are
2567 * incompatible. */
2568 if (!(a->mask & b->mask & USER_RECORD_REGULAR))
2569 return false;
2570
2571 return streq_ptr(a->user_name, b->user_name) &&
2572 streq_ptr(a->realm, b->realm);
2573 }
2574
2575 int user_record_compare_last_change(UserRecord *a, UserRecord *b) {
2576 assert(a);
2577 assert(b);
2578
2579 if (a->last_change_usec == b->last_change_usec)
2580 return 0;
2581
2582 /* Always consider a record with a timestamp newer than one without */
2583 if (a->last_change_usec == UINT64_MAX)
2584 return -1;
2585 if (b->last_change_usec == UINT64_MAX)
2586 return 1;
2587
2588 return CMP(a->last_change_usec, b->last_change_usec);
2589 }
2590
2591 int user_record_clone(UserRecord *h, UserRecordLoadFlags flags, UserRecord **ret) {
2592 _cleanup_(user_record_unrefp) UserRecord *c = NULL;
2593 int r;
2594
2595 assert(h);
2596 assert(ret);
2597
2598 c = user_record_new();
2599 if (!c)
2600 return -ENOMEM;
2601
2602 r = user_record_load(c, h->json, flags);
2603 if (r < 0)
2604 return r;
2605
2606 *ret = TAKE_PTR(c);
2607 return 0;
2608 }
2609
2610 int user_record_masked_equal(UserRecord *a, UserRecord *b, UserRecordMask mask) {
2611 _cleanup_(user_record_unrefp) UserRecord *x = NULL, *y = NULL;
2612 int r;
2613
2614 assert(a);
2615 assert(b);
2616
2617 /* Compares the two records, but ignores anything not listed in the specified mask */
2618
2619 if ((a->mask & ~mask) != 0) {
2620 r = user_record_clone(a, USER_RECORD_ALLOW(mask) | USER_RECORD_STRIP(~mask & _USER_RECORD_MASK_MAX) | USER_RECORD_PERMISSIVE, &x);
2621 if (r < 0)
2622 return r;
2623
2624 a = x;
2625 }
2626
2627 if ((b->mask & ~mask) != 0) {
2628 r = user_record_clone(b, USER_RECORD_ALLOW(mask) | USER_RECORD_STRIP(~mask & _USER_RECORD_MASK_MAX) | USER_RECORD_PERMISSIVE, &y);
2629 if (r < 0)
2630 return r;
2631
2632 b = y;
2633 }
2634
2635 return user_record_equal(a, b);
2636 }
2637
2638 int user_record_test_blocked(UserRecord *h) {
2639 usec_t n;
2640
2641 /* Checks whether access to the specified user shall be allowed at the moment. Returns:
2642 *
2643 * -ESTALE: Record is from the future
2644 * -ENOLCK: Record is blocked
2645 * -EL2HLT: Record is not valid yet
2646 * -EL3HLT: Record is not valid anymore
2647 *
2648 */
2649
2650 assert(h);
2651
2652 if (h->locked > 0)
2653 return -ENOLCK;
2654
2655 n = now(CLOCK_REALTIME);
2656
2657 if (h->not_before_usec != UINT64_MAX && n < h->not_before_usec)
2658 return -EL2HLT;
2659 if (h->not_after_usec != UINT64_MAX && n > h->not_after_usec)
2660 return -EL3HLT;
2661
2662 if (h->last_change_usec != UINT64_MAX &&
2663 h->last_change_usec > n) /* Complain during log-ins when the record is from the future */
2664 return -ESTALE;
2665
2666 return 0;
2667 }
2668
2669 int user_record_test_password_change_required(UserRecord *h) {
2670 bool change_permitted;
2671 usec_t n;
2672
2673 assert(h);
2674
2675 /* Checks whether the user must change the password when logging in
2676
2677 -EKEYREVOKED: Change password now because admin said so
2678 -EOWNERDEAD: Change password now because it expired
2679 -EKEYREJECTED: Password is expired, no changing is allowed
2680 -EKEYEXPIRED: Password is about to expire, warn user
2681 -ENETDOWN: Record has expiration info but no password change timestamp
2682 -EROFS: No password change required nor permitted
2683 -ESTALE: RTC likely incorrect, last password change is in the future
2684 0: No password change required, but permitted
2685 */
2686
2687 /* If a password change request has been set explicitly, it overrides everything */
2688 if (h->password_change_now > 0)
2689 return -EKEYREVOKED;
2690
2691 n = now(CLOCK_REALTIME);
2692
2693 /* Password change in the future? Then our RTC is likely incorrect */
2694 if (h->last_password_change_usec != UINT64_MAX &&
2695 h->last_password_change_usec > n &&
2696 (h->password_change_min_usec != UINT64_MAX ||
2697 h->password_change_max_usec != UINT64_MAX ||
2698 h->password_change_inactive_usec != UINT64_MAX))
2699 return -ESTALE;
2700
2701 /* Then, let's check if password changing is currently allowed at all */
2702 if (h->password_change_min_usec != UINT64_MAX) {
2703
2704 /* Expiry configured but no password change timestamp known? */
2705 if (h->last_password_change_usec == UINT64_MAX)
2706 return -ENETDOWN;
2707
2708 if (h->password_change_min_usec >= UINT64_MAX - h->last_password_change_usec)
2709 change_permitted = false;
2710 else
2711 change_permitted = n >= h->last_password_change_usec + h->password_change_min_usec;
2712
2713 } else
2714 change_permitted = true;
2715
2716 /* Let's check whether the password has expired. */
2717 if (!(h->password_change_max_usec == UINT64_MAX ||
2718 h->password_change_max_usec >= UINT64_MAX - h->last_password_change_usec)) {
2719
2720 uint64_t change_before;
2721
2722 /* Expiry configured but no password change timestamp known? */
2723 if (h->last_password_change_usec == UINT64_MAX)
2724 return -ENETDOWN;
2725
2726 /* Password is in inactive phase? */
2727 if (h->password_change_inactive_usec != UINT64_MAX &&
2728 h->password_change_inactive_usec < UINT64_MAX - h->password_change_max_usec) {
2729 usec_t added;
2730
2731 added = h->password_change_inactive_usec + h->password_change_max_usec;
2732 if (added < UINT64_MAX - h->last_password_change_usec &&
2733 n >= h->last_password_change_usec + added)
2734 return -EKEYREJECTED;
2735 }
2736
2737 /* Password needs to be changed now? */
2738 change_before = h->last_password_change_usec + h->password_change_max_usec;
2739 if (n >= change_before)
2740 return change_permitted ? -EOWNERDEAD : -EKEYREJECTED;
2741
2742 /* Warn user? */
2743 if (h->password_change_warn_usec != UINT64_MAX &&
2744 (change_before < h->password_change_warn_usec ||
2745 n >= change_before - h->password_change_warn_usec))
2746 return change_permitted ? -EKEYEXPIRED : -EROFS;
2747 }
2748
2749 /* No password changing necessary */
2750 return change_permitted ? 0 : -EROFS;
2751 }
2752
2753 bool user_record_is_root(const UserRecord *u) {
2754 assert(u);
2755
2756 return u->uid == 0 || streq_ptr(u->user_name, "root");
2757 }
2758
2759 bool user_record_is_nobody(const UserRecord *u) {
2760 assert(u);
2761
2762 return u->uid == UID_NOBODY || STRPTR_IN_SET(u->user_name, NOBODY_USER_NAME, "nobody");
2763 }
2764
2765 bool user_record_matches_user_name(const UserRecord *u, const char *user_name) {
2766 assert(u);
2767 assert(user_name);
2768
2769 if (streq_ptr(u->user_name, user_name))
2770 return true;
2771
2772 if (streq_ptr(u->user_name_and_realm_auto, user_name))
2773 return true;
2774
2775 if (strv_contains(u->aliases, user_name))
2776 return true;
2777
2778 const char *realm = strrchr(user_name, '@');
2779 if (realm && streq_ptr(realm+1, u->realm))
2780 STRV_FOREACH(a, u->aliases)
2781 if (startswith(user_name, *a) == realm)
2782 return true;
2783
2784 return false;
2785 }
2786
2787 int suitable_blob_filename(const char *name) {
2788 /* Enforces filename requirements as described in docs/USER_RECORD_BULK_DIRS.md */
2789 return filename_is_valid(name) &&
2790 in_charset(name, URI_UNRESERVED) &&
2791 name[0] != '.';
2792 }
2793
2794 bool userdb_match_is_set(const UserDBMatch *match) {
2795 if (!match)
2796 return false;
2797
2798 return !strv_isempty(match->fuzzy_names) ||
2799 !FLAGS_SET(match->disposition_mask, USER_DISPOSITION_MASK_ALL) ||
2800 match->uid_min > 0 ||
2801 match->uid_max < UID_INVALID-1 ||
2802 !sd_id128_is_null(match->uuid);
2803 }
2804
2805 void userdb_match_done(UserDBMatch *match) {
2806 assert(match);
2807 strv_free(match->fuzzy_names);
2808 }
2809
2810 bool user_name_fuzzy_match(const char *names[], size_t n_names, char **matches) {
2811 assert(names || n_names == 0);
2812
2813 /* Checks if any of the user record strings in the names[] array matches any of the search strings in
2814 * the matches** strv fuzzily. */
2815
2816 FOREACH_ARRAY(n, names, n_names) {
2817 if (!*n)
2818 continue;
2819
2820 _cleanup_free_ char *lcn = strdup(*n);
2821 if (!lcn)
2822 return -ENOMEM;
2823
2824 ascii_strlower(lcn);
2825
2826 STRV_FOREACH(i, matches) {
2827 _cleanup_free_ char *lc = strdup(*i);
2828 if (!lc)
2829 return -ENOMEM;
2830
2831 ascii_strlower(lc);
2832
2833 /* First do substring check */
2834 if (strstr(lcn, lc))
2835 return true;
2836
2837 /* Then do some fuzzy string comparison (but only if the needle is non-trivially long) */
2838 if (strlen(lc) >= 5 && strlevenshtein(lcn, lc) < 3)
2839 return true;
2840 }
2841 }
2842
2843 return false;
2844 }
2845
2846 bool user_record_match(UserRecord *u, const UserDBMatch *match) {
2847 assert(u);
2848
2849 if (!match)
2850 return true;
2851
2852 if (!uid_is_valid(u->uid))
2853 return false;
2854
2855 if (u->uid < match->uid_min || u->uid > match->uid_max)
2856 return false;
2857
2858 if (!BIT_SET(match->disposition_mask, user_record_disposition(u)))
2859 return false;
2860
2861 if (!sd_id128_is_null(match->uuid) && !sd_id128_equal(match->uuid, u->uuid))
2862 return false;
2863
2864 if (!strv_isempty(match->fuzzy_names)) {
2865
2866 /* Note this array of names is sparse, i.e. various entries listed in it will be
2867 * NULL. Because of that we are not using a NULL terminated strv here, but a regular
2868 * array. */
2869 const char* names[] = {
2870 u->user_name,
2871 user_record_user_name_and_realm(u),
2872 u->real_name,
2873 u->email_address,
2874 u->cifs_user_name,
2875 };
2876
2877 if (!user_name_fuzzy_match(names, ELEMENTSOF(names), match->fuzzy_names) &&
2878 !user_name_fuzzy_match((const char**) u->aliases, strv_length(u->aliases), match->fuzzy_names))
2879 return false;
2880 }
2881
2882 return true;
2883 }
2884
2885 int json_dispatch_dispositions_mask(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
2886 uint64_t *mask = ASSERT_PTR(userdata);
2887
2888 if (sd_json_variant_is_null(variant)) {
2889 *mask = UINT64_MAX;
2890 return 0;
2891 }
2892
2893 if (!sd_json_variant_is_array(variant))
2894 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name));
2895
2896 uint64_t m = 0;
2897 for (size_t i = 0; i < sd_json_variant_elements(variant); i++) {
2898 sd_json_variant *e;
2899 const char *a;
2900
2901 e = sd_json_variant_by_index(variant, i);
2902 if (!sd_json_variant_is_string(e))
2903 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
2904
2905 assert_se(a = sd_json_variant_string(e));
2906
2907 UserDisposition d = user_disposition_from_string(a);
2908 if (d < 0)
2909 return json_log(e, flags, d, "JSON field '%s' contains an invalid user disposition type: %s", strna(name), a);
2910
2911 m |= INDEX_TO_MASK(uint64_t, d);
2912 }
2913
2914 *mask = m;
2915 return 0;
2916 }
2917
2918 static const char* const user_storage_table[_USER_STORAGE_MAX] = {
2919 [USER_CLASSIC] = "classic",
2920 [USER_LUKS] = "luks",
2921 [USER_DIRECTORY] = "directory",
2922 [USER_SUBVOLUME] = "subvolume",
2923 [USER_FSCRYPT] = "fscrypt",
2924 [USER_CIFS] = "cifs",
2925 };
2926
2927 DEFINE_STRING_TABLE_LOOKUP(user_storage, UserStorage);
2928
2929 static const char* const user_disposition_table[_USER_DISPOSITION_MAX] = {
2930 [USER_INTRINSIC] = "intrinsic",
2931 [USER_SYSTEM] = "system",
2932 [USER_DYNAMIC] = "dynamic",
2933 [USER_REGULAR] = "regular",
2934 [USER_CONTAINER] = "container",
2935 [USER_FOREIGN] = "foreign",
2936 [USER_RESERVED] = "reserved",
2937 };
2938
2939 DEFINE_STRING_TABLE_LOOKUP(user_disposition, UserDisposition);
2940
2941 static const char* const auto_resize_mode_table[_AUTO_RESIZE_MODE_MAX] = {
2942 [AUTO_RESIZE_OFF] = "off",
2943 [AUTO_RESIZE_GROW] = "grow",
2944 [AUTO_RESIZE_SHRINK_AND_GROW] = "shrink-and-grow",
2945 };
2946
2947 DEFINE_STRING_TABLE_LOOKUP(auto_resize_mode, AutoResizeMode);