]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/user-record.c
7e7b28eb55023b1cb12e6ebe89577eb9232ba11e
[thirdparty/systemd.git] / src / shared / user-record.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sys/mount.h>
4
5 #include "cgroup-util.h"
6 #include "dns-domain.h"
7 #include "env-util.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "fs-util.h"
11 #include "hexdecoct.h"
12 #include "hostname-util.h"
13 #include "memory-util.h"
14 #include "path-util.h"
15 #include "pkcs11-util.h"
16 #include "rlimit-util.h"
17 #include "stat-util.h"
18 #include "string-table.h"
19 #include "strv.h"
20 #include "user-record.h"
21 #include "user-util.h"
22
23 #define DEFAULT_RATELIMIT_BURST 30
24 #define DEFAULT_RATELIMIT_INTERVAL_USEC (1*USEC_PER_MINUTE)
25
26 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
27 static int parse_alloc_uid(const char *path, const char *name, const char *t, uid_t *ret_uid) {
28 uid_t uid;
29 int r;
30
31 r = parse_uid(t, &uid);
32 if (r < 0)
33 return log_debug_errno(r, "%s: failed to parse %s %s, ignoring: %m", path, name, t);
34 if (uid == 0)
35 uid = 1;
36
37 *ret_uid = uid;
38 return 0;
39 }
40 #endif
41
42 int read_login_defs(UGIDAllocationRange *ret_defs, const char *path, const char *root) {
43 UGIDAllocationRange defs = {
44 .system_alloc_uid_min = SYSTEM_ALLOC_UID_MIN,
45 .system_uid_max = SYSTEM_UID_MAX,
46 .system_alloc_gid_min = SYSTEM_ALLOC_GID_MIN,
47 .system_gid_max = SYSTEM_GID_MAX,
48 };
49
50 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
51 _cleanup_fclose_ FILE *f = NULL;
52 int r;
53
54 if (!path)
55 path = "/etc/login.defs";
56
57 r = chase_symlinks_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT, "re", &f, NULL);
58 if (r == -ENOENT)
59 goto assign;
60 if (r < 0)
61 return log_debug_errno(r, "Failed to open %s: %m", path);
62
63 for (;;) {
64 _cleanup_free_ char *line = NULL;
65 char *t;
66
67 r = read_line(f, LINE_MAX, &line);
68 if (r < 0)
69 return log_debug_errno(r, "Failed to read %s: %m", path);
70 if (r == 0)
71 break;
72
73 if ((t = first_word(line, "SYS_UID_MIN")))
74 (void) parse_alloc_uid(path, "SYS_UID_MIN", t, &defs.system_alloc_uid_min);
75 else if ((t = first_word(line, "SYS_UID_MAX")))
76 (void) parse_alloc_uid(path, "SYS_UID_MAX", t, &defs.system_uid_max);
77 else if ((t = first_word(line, "SYS_GID_MIN")))
78 (void) parse_alloc_uid(path, "SYS_GID_MIN", t, &defs.system_alloc_gid_min);
79 else if ((t = first_word(line, "SYS_GID_MAX")))
80 (void) parse_alloc_uid(path, "SYS_GID_MAX", t, &defs.system_gid_max);
81 }
82
83 assign:
84 if (defs.system_alloc_uid_min > defs.system_uid_max) {
85 log_debug("%s: SYS_UID_MIN > SYS_UID_MAX, resetting.", path);
86 defs.system_alloc_uid_min = MIN(defs.system_uid_max - 1, (uid_t) SYSTEM_ALLOC_UID_MIN);
87 /* Look at sys_uid_max to make sure sys_uid_min..sys_uid_max remains a valid range. */
88 }
89 if (defs.system_alloc_gid_min > defs.system_gid_max) {
90 log_debug("%s: SYS_GID_MIN > SYS_GID_MAX, resetting.", path);
91 defs.system_alloc_gid_min = MIN(defs.system_gid_max - 1, (gid_t) SYSTEM_ALLOC_GID_MIN);
92 /* Look at sys_gid_max to make sure sys_gid_min..sys_gid_max remains a valid range. */
93 }
94 #endif
95
96 *ret_defs = defs;
97 return 0;
98 }
99
100 const UGIDAllocationRange *acquire_ugid_allocation_range(void) {
101 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
102 static thread_local UGIDAllocationRange defs = {
103 #else
104 static const UGIDAllocationRange defs = {
105 #endif
106 .system_alloc_uid_min = SYSTEM_ALLOC_UID_MIN,
107 .system_uid_max = SYSTEM_UID_MAX,
108 .system_alloc_gid_min = SYSTEM_ALLOC_GID_MIN,
109 .system_gid_max = SYSTEM_GID_MAX,
110 };
111
112 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
113 /* This function will ignore failure to read the file, so it should only be called from places where
114 * we don't crucially depend on the answer. In other words, it's appropriate for journald, but
115 * probably not for sysusers. */
116
117 static thread_local bool initialized = false;
118
119 if (!initialized) {
120 (void) read_login_defs(&defs, NULL, NULL);
121 initialized = true;
122 }
123 #endif
124
125 return &defs;
126 }
127
128 bool uid_is_system(uid_t uid) {
129 const UGIDAllocationRange *defs;
130 assert_se(defs = acquire_ugid_allocation_range());
131
132 return uid <= defs->system_uid_max;
133 }
134
135 bool gid_is_system(gid_t gid) {
136 const UGIDAllocationRange *defs;
137 assert_se(defs = acquire_ugid_allocation_range());
138
139 return gid <= defs->system_gid_max;
140 }
141
142 UserRecord* user_record_new(void) {
143 UserRecord *h;
144
145 h = new(UserRecord, 1);
146 if (!h)
147 return NULL;
148
149 *h = (UserRecord) {
150 .n_ref = 1,
151 .disposition = _USER_DISPOSITION_INVALID,
152 .last_change_usec = UINT64_MAX,
153 .last_password_change_usec = UINT64_MAX,
154 .umask = MODE_INVALID,
155 .nice_level = INT_MAX,
156 .not_before_usec = UINT64_MAX,
157 .not_after_usec = UINT64_MAX,
158 .locked = -1,
159 .storage = _USER_STORAGE_INVALID,
160 .access_mode = MODE_INVALID,
161 .disk_size = UINT64_MAX,
162 .disk_size_relative = UINT64_MAX,
163 .tasks_max = UINT64_MAX,
164 .memory_high = UINT64_MAX,
165 .memory_max = UINT64_MAX,
166 .cpu_weight = UINT64_MAX,
167 .io_weight = UINT64_MAX,
168 .uid = UID_INVALID,
169 .gid = GID_INVALID,
170 .nodev = true,
171 .nosuid = true,
172 .luks_discard = -1,
173 .luks_offline_discard = -1,
174 .luks_volume_key_size = UINT64_MAX,
175 .luks_pbkdf_time_cost_usec = UINT64_MAX,
176 .luks_pbkdf_memory_cost = UINT64_MAX,
177 .luks_pbkdf_parallel_threads = UINT64_MAX,
178 .disk_usage = UINT64_MAX,
179 .disk_free = UINT64_MAX,
180 .disk_ceiling = UINT64_MAX,
181 .disk_floor = UINT64_MAX,
182 .signed_locally = -1,
183 .good_authentication_counter = UINT64_MAX,
184 .bad_authentication_counter = UINT64_MAX,
185 .last_good_authentication_usec = UINT64_MAX,
186 .last_bad_authentication_usec = UINT64_MAX,
187 .ratelimit_begin_usec = UINT64_MAX,
188 .ratelimit_count = UINT64_MAX,
189 .ratelimit_interval_usec = UINT64_MAX,
190 .ratelimit_burst = UINT64_MAX,
191 .removable = -1,
192 .enforce_password_policy = -1,
193 .auto_login = -1,
194 .stop_delay_usec = UINT64_MAX,
195 .kill_processes = -1,
196 .password_change_min_usec = UINT64_MAX,
197 .password_change_max_usec = UINT64_MAX,
198 .password_change_warn_usec = UINT64_MAX,
199 .password_change_inactive_usec = UINT64_MAX,
200 .password_change_now = -1,
201 .pkcs11_protected_authentication_path_permitted = -1,
202 .fido2_user_presence_permitted = -1,
203 };
204
205 return h;
206 }
207
208 static void pkcs11_encrypted_key_done(Pkcs11EncryptedKey *k) {
209 if (!k)
210 return;
211
212 free(k->uri);
213 erase_and_free(k->data);
214 erase_and_free(k->hashed_password);
215 }
216
217 static void fido2_hmac_credential_done(Fido2HmacCredential *c) {
218 if (!c)
219 return;
220
221 free(c->id);
222 }
223
224 static void fido2_hmac_salt_done(Fido2HmacSalt *s) {
225 if (!s)
226 return;
227
228 fido2_hmac_credential_done(&s->credential);
229 erase_and_free(s->salt);
230 erase_and_free(s->hashed_password);
231 }
232
233 static void recovery_key_done(RecoveryKey *k) {
234 if (!k)
235 return;
236
237 free(k->type);
238 erase_and_free(k->hashed_password);
239 }
240
241 static UserRecord* user_record_free(UserRecord *h) {
242 if (!h)
243 return NULL;
244
245 free(h->user_name);
246 free(h->realm);
247 free(h->user_name_and_realm_auto);
248 free(h->real_name);
249 free(h->email_address);
250 erase_and_free(h->password_hint);
251 free(h->location);
252 free(h->icon_name);
253
254 free(h->shell);
255
256 strv_free(h->environment);
257 free(h->time_zone);
258 free(h->preferred_language);
259 rlimit_free_all(h->rlimits);
260
261 free(h->skeleton_directory);
262
263 strv_free_erase(h->hashed_password);
264 strv_free_erase(h->ssh_authorized_keys);
265 strv_free_erase(h->password);
266 strv_free_erase(h->token_pin);
267
268 free(h->cifs_service);
269 free(h->cifs_user_name);
270 free(h->cifs_domain);
271
272 free(h->image_path);
273 free(h->image_path_auto);
274 free(h->home_directory);
275 free(h->home_directory_auto);
276
277 strv_free(h->member_of);
278
279 free(h->file_system_type);
280 free(h->luks_cipher);
281 free(h->luks_cipher_mode);
282 free(h->luks_pbkdf_hash_algorithm);
283 free(h->luks_pbkdf_type);
284
285 free(h->state);
286 free(h->service);
287
288 strv_free(h->pkcs11_token_uri);
289 for (size_t i = 0; i < h->n_pkcs11_encrypted_key; i++)
290 pkcs11_encrypted_key_done(h->pkcs11_encrypted_key + i);
291 free(h->pkcs11_encrypted_key);
292
293 for (size_t i = 0; i < h->n_fido2_hmac_credential; i++)
294 fido2_hmac_credential_done(h->fido2_hmac_credential + i);
295 for (size_t i = 0; i < h->n_fido2_hmac_salt; i++)
296 fido2_hmac_salt_done(h->fido2_hmac_salt + i);
297
298 strv_free(h->recovery_key_type);
299 for (size_t i = 0; i < h->n_recovery_key; i++)
300 recovery_key_done(h->recovery_key + i);
301
302 json_variant_unref(h->json);
303
304 return mfree(h);
305 }
306
307 DEFINE_TRIVIAL_REF_UNREF_FUNC(UserRecord, user_record, user_record_free);
308
309 int json_dispatch_realm(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
310 char **s = userdata;
311 const char *n;
312 int r;
313
314 if (json_variant_is_null(variant)) {
315 *s = mfree(*s);
316 return 0;
317 }
318
319 if (!json_variant_is_string(variant))
320 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
321
322 n = json_variant_string(variant);
323 r = dns_name_is_valid(n);
324 if (r < 0)
325 return json_log(variant, flags, r, "Failed to check if JSON field '%s' is a valid DNS domain.", strna(name));
326 if (r == 0)
327 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid DNS domain.", strna(name));
328
329 r = free_and_strdup(s, n);
330 if (r < 0)
331 return json_log(variant, flags, r, "Failed to allocate string: %m");
332
333 return 0;
334 }
335
336 int json_dispatch_gecos(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
337 char **s = userdata;
338 const char *n;
339
340 if (json_variant_is_null(variant)) {
341 *s = mfree(*s);
342 return 0;
343 }
344
345 if (!json_variant_is_string(variant))
346 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
347
348 n = json_variant_string(variant);
349 if (valid_gecos(n)) {
350 if (free_and_strdup(s, n) < 0)
351 return json_log_oom(variant, flags);
352 } else {
353 _cleanup_free_ char *m = NULL;
354
355 json_log(variant, flags|JSON_DEBUG, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid GECOS compatible string, mangling.", strna(name));
356
357 m = mangle_gecos(n);
358 if (!m)
359 return json_log_oom(variant, flags);
360
361 free_and_replace(*s, m);
362 }
363
364 return 0;
365 }
366
367 static int json_dispatch_nice(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
368 int *nl = userdata;
369 intmax_t m;
370
371 if (json_variant_is_null(variant)) {
372 *nl = INT_MAX;
373 return 0;
374 }
375
376 if (!json_variant_is_integer(variant))
377 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
378
379 m = json_variant_integer(variant);
380 if (m < PRIO_MIN || m >= PRIO_MAX)
381 return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' is not a valid nice level.", strna(name));
382
383 *nl = m;
384 return 0;
385 }
386
387 static int json_dispatch_rlimit_value(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
388 rlim_t *ret = userdata;
389
390 if (json_variant_is_null(variant))
391 *ret = RLIM_INFINITY;
392 else if (json_variant_is_unsigned(variant)) {
393 uintmax_t w;
394
395 w = json_variant_unsigned(variant);
396 if (w == RLIM_INFINITY || (uintmax_t) w != json_variant_unsigned(variant))
397 return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "Resource limit value '%s' is out of range.", name);
398
399 *ret = (rlim_t) w;
400 } else
401 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit value '%s' is not an unsigned integer.", name);
402
403 return 0;
404 }
405
406 static int json_dispatch_rlimits(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
407 struct rlimit** limits = userdata;
408 JsonVariant *value;
409 const char *key;
410 int r;
411
412 assert_se(limits);
413
414 if (json_variant_is_null(variant)) {
415 rlimit_free_all(limits);
416 return 0;
417 }
418
419 if (!json_variant_is_object(variant))
420 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name));
421
422 JSON_VARIANT_OBJECT_FOREACH(key, value, variant) {
423 JsonVariant *jcur, *jmax;
424 struct rlimit rl;
425 const char *p;
426 int l;
427
428 p = startswith(key, "RLIMIT_");
429 if (!p)
430 l = -1;
431 else
432 l = rlimit_from_string(p);
433 if (l < 0)
434 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' not known.", key);
435
436 if (!json_variant_is_object(value))
437 return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' has invalid value.", key);
438
439 if (json_variant_elements(value) != 4)
440 return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' value is does not have two fields as expected.", key);
441
442 jcur = json_variant_by_key(value, "cur");
443 if (!jcur)
444 return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' lacks 'cur' field.", key);
445 r = json_dispatch_rlimit_value("cur", jcur, flags, &rl.rlim_cur);
446 if (r < 0)
447 return r;
448
449 jmax = json_variant_by_key(value, "max");
450 if (!jmax)
451 return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' lacks 'max' field.", key);
452 r = json_dispatch_rlimit_value("max", jmax, flags, &rl.rlim_max);
453 if (r < 0)
454 return r;
455
456 if (limits[l])
457 *(limits[l]) = rl;
458 else {
459 limits[l] = newdup(struct rlimit, &rl, 1);
460 if (!limits[l])
461 return log_oom();
462 }
463 }
464
465 return 0;
466 }
467
468 static int json_dispatch_filename_or_path(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
469 char **s = userdata;
470 const char *n;
471 int r;
472
473 assert(s);
474
475 if (json_variant_is_null(variant)) {
476 *s = mfree(*s);
477 return 0;
478 }
479
480 if (!json_variant_is_string(variant))
481 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
482
483 n = json_variant_string(variant);
484 if (!filename_is_valid(n) && !path_is_normalized(n))
485 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid file name or normalized path.", strna(name));
486
487 r = free_and_strdup(s, n);
488 if (r < 0)
489 return json_log(variant, flags, r, "Failed to allocate string: %m");
490
491 return 0;
492 }
493
494 static int json_dispatch_path(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
495 char **s = userdata;
496 const char *n;
497 int r;
498
499 if (json_variant_is_null(variant)) {
500 *s = mfree(*s);
501 return 0;
502 }
503
504 if (!json_variant_is_string(variant))
505 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
506
507 n = json_variant_string(variant);
508 if (!path_is_normalized(n))
509 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a normalized file system path.", strna(name));
510 if (!path_is_absolute(n))
511 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an absolute file system path.", strna(name));
512
513 r = free_and_strdup(s, n);
514 if (r < 0)
515 return json_log(variant, flags, r, "Failed to allocate string: %m");
516
517 return 0;
518 }
519
520 static int json_dispatch_home_directory(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
521 char **s = userdata;
522 const char *n;
523 int r;
524
525 if (json_variant_is_null(variant)) {
526 *s = mfree(*s);
527 return 0;
528 }
529
530 if (!json_variant_is_string(variant))
531 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
532
533 n = json_variant_string(variant);
534 if (!valid_home(n))
535 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid home directory path.", strna(name));
536
537 r = free_and_strdup(s, n);
538 if (r < 0)
539 return json_log(variant, flags, r, "Failed to allocate string: %m");
540
541 return 0;
542 }
543
544 static int json_dispatch_image_path(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
545 char **s = userdata;
546 const char *n;
547 int r;
548
549 if (json_variant_is_null(variant)) {
550 *s = mfree(*s);
551 return 0;
552 }
553
554 if (!json_variant_is_string(variant))
555 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
556
557 n = json_variant_string(variant);
558 if (empty_or_root(n) || !path_is_valid(n) || !path_is_absolute(n))
559 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid image path.", strna(name));
560
561 r = free_and_strdup(s, n);
562 if (r < 0)
563 return json_log(variant, flags, r, "Failed to allocate string: %m");
564
565 return 0;
566 }
567
568 static int json_dispatch_umask(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
569 mode_t *m = userdata;
570 uintmax_t k;
571
572 if (json_variant_is_null(variant)) {
573 *m = (mode_t) -1;
574 return 0;
575 }
576
577 if (!json_variant_is_unsigned(variant))
578 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a number.", strna(name));
579
580 k = json_variant_unsigned(variant);
581 if (k > 0777)
582 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' outside of valid range 0…0777.", strna(name));
583
584 *m = (mode_t) k;
585 return 0;
586 }
587
588 static int json_dispatch_access_mode(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
589 mode_t *m = userdata;
590 uintmax_t k;
591
592 if (json_variant_is_null(variant)) {
593 *m = (mode_t) -1;
594 return 0;
595 }
596
597 if (!json_variant_is_unsigned(variant))
598 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a number.", strna(name));
599
600 k = json_variant_unsigned(variant);
601 if (k > 07777)
602 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' outside of valid range 0…07777.", strna(name));
603
604 *m = (mode_t) k;
605 return 0;
606 }
607
608 static int json_dispatch_environment(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
609 _cleanup_strv_free_ char **n = NULL;
610 char ***l = userdata;
611 size_t i;
612 int r;
613
614 if (json_variant_is_null(variant)) {
615 *l = strv_free(*l);
616 return 0;
617 }
618
619 if (!json_variant_is_array(variant))
620 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name));
621
622 for (i = 0; i < json_variant_elements(variant); i++) {
623 _cleanup_free_ char *c = NULL;
624 JsonVariant *e;
625 const char *a;
626
627 e = json_variant_by_index(variant, i);
628 if (!json_variant_is_string(e))
629 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
630
631 assert_se(a = json_variant_string(e));
632
633 if (!env_assignment_is_valid(a))
634 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of environment variables.", strna(name));
635
636 c = strdup(a);
637 if (!c)
638 return json_log_oom(variant, flags);
639
640 r = strv_env_replace(&n, c);
641 if (r < 0)
642 return json_log_oom(variant, flags);
643
644 c = NULL;
645 }
646
647 strv_free_and_replace(*l, n);
648 return 0;
649 }
650
651 int json_dispatch_user_disposition(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
652 UserDisposition *disposition = userdata, k;
653
654 if (json_variant_is_null(variant)) {
655 *disposition = _USER_DISPOSITION_INVALID;
656 return 0;
657 }
658
659 if (!json_variant_is_string(variant))
660 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
661
662 k = user_disposition_from_string(json_variant_string(variant));
663 if (k < 0)
664 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Disposition type '%s' not known.", json_variant_string(variant));
665
666 *disposition = k;
667 return 0;
668 }
669
670 static int json_dispatch_storage(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
671 UserStorage *storage = userdata, k;
672
673 if (json_variant_is_null(variant)) {
674 *storage = _USER_STORAGE_INVALID;
675 return 0;
676 }
677
678 if (!json_variant_is_string(variant))
679 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
680
681 k = user_storage_from_string(json_variant_string(variant));
682 if (k < 0)
683 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Storage type '%s' not known.", json_variant_string(variant));
684
685 *storage = k;
686 return 0;
687 }
688
689 static int json_dispatch_disk_size(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
690 uint64_t *size = userdata;
691 uintmax_t k;
692
693 if (json_variant_is_null(variant)) {
694 *size = UINT64_MAX;
695 return 0;
696 }
697
698 if (!json_variant_is_unsigned(variant))
699 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name));
700
701 k = json_variant_unsigned(variant);
702 if (k < USER_DISK_SIZE_MIN || k > USER_DISK_SIZE_MAX)
703 return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' is not in valid range %" PRIu64 "…%" PRIu64 ".", strna(name), USER_DISK_SIZE_MIN, USER_DISK_SIZE_MAX);
704
705 *size = k;
706 return 0;
707 }
708
709 static int json_dispatch_tasks_or_memory_max(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
710 uint64_t *limit = userdata;
711 uintmax_t k;
712
713 if (json_variant_is_null(variant)) {
714 *limit = UINT64_MAX;
715 return 0;
716 }
717
718 if (!json_variant_is_unsigned(variant))
719 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a integer.", strna(name));
720
721 k = json_variant_unsigned(variant);
722 if (k <= 0 || k >= UINT64_MAX)
723 return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' is not in valid range %" PRIu64 "…%" PRIu64 ".", strna(name), (uint64_t) 1, UINT64_MAX-1);
724
725 *limit = k;
726 return 0;
727 }
728
729 static int json_dispatch_weight(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
730 uint64_t *weight = userdata;
731 uintmax_t k;
732
733 if (json_variant_is_null(variant)) {
734 *weight = UINT64_MAX;
735 return 0;
736 }
737
738 if (!json_variant_is_unsigned(variant))
739 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a integer.", strna(name));
740
741 k = json_variant_unsigned(variant);
742 if (k <= CGROUP_WEIGHT_MIN || k >= CGROUP_WEIGHT_MAX)
743 return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' is not in valid range %" PRIu64 "…%" PRIu64 ".", strna(name), (uint64_t) CGROUP_WEIGHT_MIN, (uint64_t) CGROUP_WEIGHT_MAX);
744
745 *weight = k;
746 return 0;
747 }
748
749 int json_dispatch_user_group_list(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
750 _cleanup_strv_free_ char **l = NULL;
751 char ***list = userdata;
752 JsonVariant *e;
753 int r;
754
755 if (!json_variant_is_array(variant))
756 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
757
758 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
759
760 if (!json_variant_is_string(e))
761 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a string.");
762
763 if (!valid_user_group_name(json_variant_string(e), FLAGS_SET(flags, JSON_RELAX) ? VALID_USER_RELAX : 0))
764 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a valid user/group name: %s", json_variant_string(e));
765
766 r = strv_extend(&l, json_variant_string(e));
767 if (r < 0)
768 return json_log(e, flags, r, "Failed to append array element: %m");
769 }
770
771 r = strv_extend_strv(list, l, true);
772 if (r < 0)
773 return json_log(variant, flags, r, "Failed to merge user/group arrays: %m");
774
775 return 0;
776 }
777
778 static int dispatch_secret(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
779
780 static const JsonDispatch secret_dispatch_table[] = {
781 { "password", _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(UserRecord, password), 0 },
782 { "tokenPin", _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(UserRecord, token_pin), 0 },
783 { "pkcs11Pin", /* legacy alias */ _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(UserRecord, token_pin), 0 },
784 { "pkcs11ProtectedAuthenticationPathPermitted", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, pkcs11_protected_authentication_path_permitted), 0 },
785 { "fido2UserPresencePermitted", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, fido2_user_presence_permitted), 0 },
786 {},
787 };
788
789 return json_dispatch(variant, secret_dispatch_table, NULL, flags, userdata);
790 }
791
792 static int dispatch_pkcs11_uri(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
793 char **s = userdata;
794 const char *n;
795 int r;
796
797 if (json_variant_is_null(variant)) {
798 *s = mfree(*s);
799 return 0;
800 }
801
802 if (!json_variant_is_string(variant))
803 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
804
805 n = json_variant_string(variant);
806 if (!pkcs11_uri_valid(n))
807 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name));
808
809 r = free_and_strdup(s, n);
810 if (r < 0)
811 return json_log(variant, flags, r, "Failed to allocate string: %m");
812
813 return 0;
814 }
815
816 static int dispatch_pkcs11_uri_array(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
817 _cleanup_strv_free_ char **z = NULL;
818 char ***l = userdata;
819 JsonVariant *e;
820 int r;
821
822 if (json_variant_is_null(variant)) {
823 *l = strv_free(*l);
824 return 0;
825 }
826
827 if (json_variant_is_string(variant)) {
828 const char *n;
829
830 n = json_variant_string(variant);
831 if (!pkcs11_uri_valid(n))
832 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name));
833
834 z = strv_new(n);
835 if (!z)
836 return log_oom();
837
838 } else {
839
840 if (!json_variant_is_array(variant))
841 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string or array of strings.", strna(name));
842
843 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
844 const char *n;
845
846 if (!json_variant_is_string(e))
847 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a string.");
848
849 n = json_variant_string(e);
850 if (!pkcs11_uri_valid(n))
851 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);
852
853 r = strv_extend(&z, n);
854 if (r < 0)
855 return log_oom();
856 }
857 }
858
859 strv_free_and_replace(*l, z);
860 return 0;
861 }
862
863 static int dispatch_pkcs11_key_data(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
864 Pkcs11EncryptedKey *k = userdata;
865 size_t l;
866 void *b;
867 int r;
868
869 if (json_variant_is_null(variant)) {
870 k->data = erase_and_free(k->data);
871 k->size = 0;
872 return 0;
873 }
874
875 if (!json_variant_is_string(variant))
876 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
877
878 r = unbase64mem(json_variant_string(variant), (size_t) -1, &b, &l);
879 if (r < 0)
880 return json_log(variant, flags, r, "Failed to decode encrypted PKCS#11 key: %m");
881
882 erase_and_free(k->data);
883 k->data = b;
884 k->size = l;
885
886 return 0;
887 }
888
889 static int dispatch_pkcs11_key(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
890 UserRecord *h = userdata;
891 JsonVariant *e;
892 int r;
893
894 if (!json_variant_is_array(variant))
895 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
896
897 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
898 Pkcs11EncryptedKey *array, *k;
899
900 static const JsonDispatch pkcs11_key_dispatch_table[] = {
901 { "uri", JSON_VARIANT_STRING, dispatch_pkcs11_uri, offsetof(Pkcs11EncryptedKey, uri), JSON_MANDATORY },
902 { "data", JSON_VARIANT_STRING, dispatch_pkcs11_key_data, 0, JSON_MANDATORY },
903 { "hashedPassword", JSON_VARIANT_STRING, json_dispatch_string, offsetof(Pkcs11EncryptedKey, hashed_password), JSON_MANDATORY },
904 {},
905 };
906
907 if (!json_variant_is_object(e))
908 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
909
910 array = reallocarray(h->pkcs11_encrypted_key, h->n_pkcs11_encrypted_key + 1, sizeof(Pkcs11EncryptedKey));
911 if (!array)
912 return log_oom();
913
914 h->pkcs11_encrypted_key = array;
915 k = h->pkcs11_encrypted_key + h->n_pkcs11_encrypted_key;
916 *k = (Pkcs11EncryptedKey) {};
917
918 r = json_dispatch(e, pkcs11_key_dispatch_table, NULL, flags, k);
919 if (r < 0) {
920 pkcs11_encrypted_key_done(k);
921 return r;
922 }
923
924 h->n_pkcs11_encrypted_key++;
925 }
926
927 return 0;
928 }
929
930 static int dispatch_fido2_hmac_credential(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
931 Fido2HmacCredential *k = userdata;
932 size_t l;
933 void *b;
934 int r;
935
936 if (json_variant_is_null(variant)) {
937 k->id = mfree(k->id);
938 k->size = 0;
939 return 0;
940 }
941
942 if (!json_variant_is_string(variant))
943 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
944
945 r = unbase64mem(json_variant_string(variant), (size_t) -1, &b, &l);
946 if (r < 0)
947 return json_log(variant, flags, r, "Failed to decode FIDO2 credential ID: %m");
948
949 free_and_replace(k->id, b);
950 k->size = l;
951
952 return 0;
953 }
954
955 static int dispatch_fido2_hmac_credential_array(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
956 UserRecord *h = userdata;
957 JsonVariant *e;
958 int r;
959
960 if (!json_variant_is_array(variant))
961 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
962
963 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
964 Fido2HmacCredential *array;
965 size_t l;
966 void *b;
967
968 if (!json_variant_is_string(e))
969 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a string.");
970
971 array = reallocarray(h->fido2_hmac_credential, h->n_fido2_hmac_credential + 1, sizeof(Fido2HmacCredential));
972 if (!array)
973 return log_oom();
974
975 r = unbase64mem(json_variant_string(e), (size_t) -1, &b, &l);
976 if (r < 0)
977 return json_log(variant, flags, r, "Failed to decode FIDO2 credential ID: %m");
978
979 h->fido2_hmac_credential = array;
980
981 h->fido2_hmac_credential[h->n_fido2_hmac_credential++] = (Fido2HmacCredential) {
982 .id = b,
983 .size = l,
984 };
985 }
986
987 return 0;
988 }
989
990 static int dispatch_fido2_hmac_salt_value(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
991 Fido2HmacSalt *k = userdata;
992 size_t l;
993 void *b;
994 int r;
995
996 if (json_variant_is_null(variant)) {
997 k->salt = erase_and_free(k->salt);
998 k->salt_size = 0;
999 return 0;
1000 }
1001
1002 if (!json_variant_is_string(variant))
1003 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
1004
1005 r = unbase64mem(json_variant_string(variant), (size_t) -1, &b, &l);
1006 if (r < 0)
1007 return json_log(variant, flags, r, "Failed to decode FIDO2 salt: %m");
1008
1009 erase_and_free(k->salt);
1010 k->salt = b;
1011 k->salt_size = l;
1012
1013 return 0;
1014 }
1015
1016 static int dispatch_fido2_hmac_salt(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
1017 UserRecord *h = userdata;
1018 JsonVariant *e;
1019 int r;
1020
1021 if (!json_variant_is_array(variant))
1022 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
1023
1024 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
1025 Fido2HmacSalt *array, *k;
1026
1027 static const JsonDispatch fido2_hmac_salt_dispatch_table[] = {
1028 { "credential", JSON_VARIANT_STRING, dispatch_fido2_hmac_credential, offsetof(Fido2HmacSalt, credential), JSON_MANDATORY },
1029 { "salt", JSON_VARIANT_STRING, dispatch_fido2_hmac_salt_value, 0, JSON_MANDATORY },
1030 { "hashedPassword", JSON_VARIANT_STRING, json_dispatch_string, offsetof(Fido2HmacSalt, hashed_password), JSON_MANDATORY },
1031 {},
1032 };
1033
1034 if (!json_variant_is_object(e))
1035 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
1036
1037 array = reallocarray(h->fido2_hmac_salt, h->n_fido2_hmac_salt + 1, sizeof(Fido2HmacSalt));
1038 if (!array)
1039 return log_oom();
1040
1041 h->fido2_hmac_salt = array;
1042 k = h->fido2_hmac_salt + h->n_fido2_hmac_salt;
1043 *k = (Fido2HmacSalt) {};
1044
1045 r = json_dispatch(e, fido2_hmac_salt_dispatch_table, NULL, flags, k);
1046 if (r < 0) {
1047 fido2_hmac_salt_done(k);
1048 return r;
1049 }
1050
1051 h->n_fido2_hmac_salt++;
1052 }
1053
1054 return 0;
1055 }
1056
1057 static int dispatch_recovery_key(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
1058 UserRecord *h = userdata;
1059 JsonVariant *e;
1060 int r;
1061
1062 if (!json_variant_is_array(variant))
1063 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
1064
1065 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
1066 RecoveryKey *array, *k;
1067
1068 static const JsonDispatch recovery_key_dispatch_table[] = {
1069 { "type", JSON_VARIANT_STRING, json_dispatch_string, 0, JSON_MANDATORY },
1070 { "hashedPassword", JSON_VARIANT_STRING, json_dispatch_string, offsetof(RecoveryKey, hashed_password), JSON_MANDATORY },
1071 {},
1072 };
1073
1074 if (!json_variant_is_object(e))
1075 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object.");
1076
1077 array = reallocarray(h->recovery_key, h->n_recovery_key + 1, sizeof(RecoveryKey));
1078 if (!array)
1079 return log_oom();
1080
1081 h->recovery_key = array;
1082 k = h->recovery_key + h->n_recovery_key;
1083 *k = (RecoveryKey) {};
1084
1085 r = json_dispatch(e, recovery_key_dispatch_table, NULL, flags, k);
1086 if (r < 0) {
1087 recovery_key_done(k);
1088 return r;
1089 }
1090
1091 h->n_recovery_key++;
1092 }
1093
1094 return 0;
1095 }
1096
1097 static int dispatch_privileged(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
1098
1099 static const JsonDispatch privileged_dispatch_table[] = {
1100 { "passwordHint", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, password_hint), 0 },
1101 { "hashedPassword", _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(UserRecord, hashed_password), JSON_SAFE },
1102 { "sshAuthorizedKeys", _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(UserRecord, ssh_authorized_keys), 0 },
1103 { "pkcs11EncryptedKey", JSON_VARIANT_ARRAY, dispatch_pkcs11_key, 0, 0 },
1104 { "fido2HmacSalt", JSON_VARIANT_ARRAY, dispatch_fido2_hmac_salt, 0, 0 },
1105 { "recoveryKey", JSON_VARIANT_ARRAY, dispatch_recovery_key, 0, 0 },
1106 {},
1107 };
1108
1109 return json_dispatch(variant, privileged_dispatch_table, NULL, flags, userdata);
1110 }
1111
1112 static int dispatch_binding(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
1113
1114 static const JsonDispatch binding_dispatch_table[] = {
1115 { "imagePath", JSON_VARIANT_STRING, json_dispatch_image_path, offsetof(UserRecord, image_path), 0 },
1116 { "homeDirectory", JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, home_directory), 0 },
1117 { "partitionUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 },
1118 { "luksUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 },
1119 { "fileSystemUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 },
1120 { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 },
1121 { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 },
1122 { "storage", JSON_VARIANT_STRING, json_dispatch_storage, offsetof(UserRecord, storage), 0 },
1123 { "fileSystemType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, file_system_type), JSON_SAFE },
1124 { "luksCipher", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher), JSON_SAFE },
1125 { "luksCipherMode", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), JSON_SAFE },
1126 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 },
1127 {},
1128 };
1129
1130 char smid[SD_ID128_STRING_MAX];
1131 JsonVariant *m;
1132 sd_id128_t mid;
1133 int r;
1134
1135 if (!variant)
1136 return 0;
1137
1138 if (!json_variant_is_object(variant))
1139 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name));
1140
1141 r = sd_id128_get_machine(&mid);
1142 if (r < 0)
1143 return json_log(variant, flags, r, "Failed to determine machine ID: %m");
1144
1145 m = json_variant_by_key(variant, sd_id128_to_string(mid, smid));
1146 if (!m)
1147 return 0;
1148
1149 return json_dispatch(m, binding_dispatch_table, NULL, flags, userdata);
1150 }
1151
1152 int per_machine_id_match(JsonVariant *ids, JsonDispatchFlags flags) {
1153 sd_id128_t mid;
1154 int r;
1155
1156 r = sd_id128_get_machine(&mid);
1157 if (r < 0)
1158 return json_log(ids, flags, r, "Failed to acquire machine ID: %m");
1159
1160 if (json_variant_is_string(ids)) {
1161 sd_id128_t k;
1162
1163 r = sd_id128_from_string(json_variant_string(ids), &k);
1164 if (r < 0) {
1165 json_log(ids, flags, r, "%s is not a valid machine ID, ignoring: %m", json_variant_string(ids));
1166 return 0;
1167 }
1168
1169 return sd_id128_equal(mid, k);
1170 }
1171
1172 if (json_variant_is_array(ids)) {
1173 JsonVariant *e;
1174
1175 JSON_VARIANT_ARRAY_FOREACH(e, ids) {
1176 sd_id128_t k;
1177
1178 if (!json_variant_is_string(e)) {
1179 json_log(e, flags, 0, "Machine ID is not a string, ignoring: %m");
1180 continue;
1181 }
1182
1183 r = sd_id128_from_string(json_variant_string(e), &k);
1184 if (r < 0) {
1185 json_log(e, flags, r, "%s is not a valid machine ID, ignoring: %m", json_variant_string(e));
1186 continue;
1187 }
1188
1189 if (sd_id128_equal(mid, k))
1190 return true;
1191 }
1192
1193 return false;
1194 }
1195
1196 json_log(ids, flags, 0, "Machine ID is not a string or array of strings, ignoring: %m");
1197 return false;
1198 }
1199
1200 int per_machine_hostname_match(JsonVariant *hns, JsonDispatchFlags flags) {
1201 _cleanup_free_ char *hn = NULL;
1202 int r;
1203
1204 r = gethostname_strict(&hn);
1205 if (r == -ENXIO) {
1206 json_log(hns, flags, r, "No hostname set, not matching perMachine hostname record: %m");
1207 return false;
1208 }
1209 if (r < 0)
1210 return json_log(hns, flags, r, "Failed to acquire hostname: %m");
1211
1212 if (json_variant_is_string(hns))
1213 return streq(json_variant_string(hns), hn);
1214
1215 if (json_variant_is_array(hns)) {
1216 JsonVariant *e;
1217
1218 JSON_VARIANT_ARRAY_FOREACH(e, hns) {
1219
1220 if (!json_variant_is_string(e)) {
1221 json_log(e, flags, 0, "Hostname is not a string, ignoring: %m");
1222 continue;
1223 }
1224
1225 if (streq(json_variant_string(hns), hn))
1226 return true;
1227 }
1228
1229 return false;
1230 }
1231
1232 json_log(hns, flags, 0, "Hostname is not a string or array of strings, ignoring: %m");
1233 return false;
1234 }
1235
1236 static int dispatch_per_machine(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
1237
1238 static const JsonDispatch per_machine_dispatch_table[] = {
1239 { "matchMachineId", _JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 },
1240 { "matchHostname", _JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 },
1241 { "iconName", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, icon_name), JSON_SAFE },
1242 { "location", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, location), 0 },
1243 { "shell", JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, shell), 0 },
1244 { "umask", JSON_VARIANT_UNSIGNED, json_dispatch_umask, offsetof(UserRecord, umask), 0 },
1245 { "environment", JSON_VARIANT_ARRAY, json_dispatch_environment, offsetof(UserRecord, environment), 0 },
1246 { "timeZone", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, time_zone), JSON_SAFE },
1247 { "preferredLanguage", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, preferred_language), JSON_SAFE },
1248 { "niceLevel", _JSON_VARIANT_TYPE_INVALID, json_dispatch_nice, offsetof(UserRecord, nice_level), 0 },
1249 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID, json_dispatch_rlimits, offsetof(UserRecord, rlimits), 0 },
1250 { "locked", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, locked), 0 },
1251 { "notBeforeUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, not_before_usec), 0 },
1252 { "notAfterUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, not_after_usec), 0 },
1253 { "storage", JSON_VARIANT_STRING, json_dispatch_storage, offsetof(UserRecord, storage), 0 },
1254 { "diskSize", JSON_VARIANT_UNSIGNED, json_dispatch_disk_size, offsetof(UserRecord, disk_size), 0 },
1255 { "diskSizeRelative", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, disk_size_relative), 0 },
1256 { "skeletonDirectory", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, skeleton_directory), 0 },
1257 { "accessMode", JSON_VARIANT_UNSIGNED, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 },
1258 { "tasksMax", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, tasks_max), 0 },
1259 { "memoryHigh", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_high), 0 },
1260 { "memoryMax", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_max), 0 },
1261 { "cpuWeight", JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, cpu_weight), 0 },
1262 { "ioWeight", JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, io_weight), 0 },
1263 { "mountNoDevices", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, nodev), 0 },
1264 { "mountNoSuid", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, nosuid), 0 },
1265 { "mountNoExecute", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, noexec), 0 },
1266 { "cifsDomain", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_domain), JSON_SAFE },
1267 { "cifsUserName", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_user_name), JSON_SAFE },
1268 { "cifsService", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_service), JSON_SAFE },
1269 { "imagePath", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, image_path), 0 },
1270 { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 },
1271 { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 },
1272 { "memberOf", JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(UserRecord, member_of), JSON_RELAX},
1273 { "fileSystemType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, file_system_type), JSON_SAFE },
1274 { "partitionUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 },
1275 { "luksUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 },
1276 { "fileSystemUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 },
1277 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID, json_dispatch_tristate, offsetof(UserRecord, luks_discard), 0, },
1278 { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID, json_dispatch_tristate, offsetof(UserRecord, luks_offline_discard), 0, },
1279 { "luksCipher", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher), JSON_SAFE },
1280 { "luksCipherMode", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), JSON_SAFE },
1281 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 },
1282 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_pbkdf_hash_algorithm), JSON_SAFE },
1283 { "luksPbkdfType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_pbkdf_type), JSON_SAFE },
1284 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_time_cost_usec), 0 },
1285 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_memory_cost), 0 },
1286 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_parallel_threads), 0 },
1287 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, ratelimit_interval_usec), 0 },
1288 { "rateLimitBurst", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, ratelimit_burst), 0 },
1289 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, enforce_password_policy), 0 },
1290 { "autoLogin", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, auto_login), 0 },
1291 { "stopDelayUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, stop_delay_usec), 0 },
1292 { "killProcesses", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, kill_processes), 0 },
1293 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, password_change_min_usec), 0 },
1294 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, password_change_max_usec), 0 },
1295 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, password_change_warn_usec), 0 },
1296 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, password_change_inactive_usec), 0 },
1297 { "passwordChangeNow", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, password_change_now), 0 },
1298 { "pkcs11TokenUri", JSON_VARIANT_ARRAY, dispatch_pkcs11_uri_array, offsetof(UserRecord, pkcs11_token_uri), 0 },
1299 { "fido2HmacCredential", JSON_VARIANT_ARRAY, dispatch_fido2_hmac_credential_array, 0, 0 },
1300 {},
1301 };
1302
1303 JsonVariant *e;
1304 int r;
1305
1306 if (!variant)
1307 return 0;
1308
1309 if (!json_variant_is_array(variant))
1310 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name));
1311
1312 JSON_VARIANT_ARRAY_FOREACH(e, variant) {
1313 bool matching = false;
1314 JsonVariant *m;
1315
1316 if (!json_variant_is_object(e))
1317 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name));
1318
1319 m = json_variant_by_key(e, "matchMachineId");
1320 if (m) {
1321 r = per_machine_id_match(m, flags);
1322 if (r < 0)
1323 return r;
1324
1325 matching = r > 0;
1326 }
1327
1328 if (!matching) {
1329 m = json_variant_by_key(e, "matchHostname");
1330 if (m) {
1331 r = per_machine_hostname_match(m, flags);
1332 if (r < 0)
1333 return r;
1334
1335 matching = r > 0;
1336 }
1337 }
1338
1339 if (!matching)
1340 continue;
1341
1342 r = json_dispatch(e, per_machine_dispatch_table, NULL, flags, userdata);
1343 if (r < 0)
1344 return r;
1345 }
1346
1347 return 0;
1348 }
1349
1350 static int dispatch_status(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
1351
1352 static const JsonDispatch status_dispatch_table[] = {
1353 { "diskUsage", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, disk_usage), 0 },
1354 { "diskFree", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, disk_free), 0 },
1355 { "diskSize", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, disk_size), 0 },
1356 { "diskCeiling", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, disk_ceiling), 0 },
1357 { "diskFloor", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, disk_floor), 0 },
1358 { "state", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, state), JSON_SAFE },
1359 { "service", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, service), JSON_SAFE },
1360 { "signedLocally", _JSON_VARIANT_TYPE_INVALID, json_dispatch_tristate, offsetof(UserRecord, signed_locally), 0 },
1361 { "goodAuthenticationCounter", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, good_authentication_counter), 0 },
1362 { "badAuthenticationCounter", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, bad_authentication_counter), 0 },
1363 { "lastGoodAuthenticationUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, last_good_authentication_usec), 0 },
1364 { "lastBadAuthenticationUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, last_bad_authentication_usec), 0 },
1365 { "rateLimitBeginUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, ratelimit_begin_usec), 0 },
1366 { "rateLimitCount", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, ratelimit_count), 0 },
1367 { "removable", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, removable), 0 },
1368 {},
1369 };
1370
1371 char smid[SD_ID128_STRING_MAX];
1372 JsonVariant *m;
1373 sd_id128_t mid;
1374 int r;
1375
1376 if (!variant)
1377 return 0;
1378
1379 if (!json_variant_is_object(variant))
1380 return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name));
1381
1382 r = sd_id128_get_machine(&mid);
1383 if (r < 0)
1384 return json_log(variant, flags, r, "Failed to determine machine ID: %m");
1385
1386 m = json_variant_by_key(variant, sd_id128_to_string(mid, smid));
1387 if (!m)
1388 return 0;
1389
1390 return json_dispatch(m, status_dispatch_table, NULL, flags, userdata);
1391 }
1392
1393 int user_record_build_image_path(UserStorage storage, const char *user_name_and_realm, char **ret) {
1394 const char *suffix;
1395 char *z;
1396
1397 assert(storage >= 0);
1398 assert(user_name_and_realm);
1399 assert(ret);
1400
1401 if (storage == USER_LUKS)
1402 suffix = ".home";
1403 else if (IN_SET(storage, USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT))
1404 suffix = ".homedir";
1405 else {
1406 *ret = NULL;
1407 return 0;
1408 }
1409
1410 z = strjoin("/home/", user_name_and_realm, suffix);
1411 if (!z)
1412 return -ENOMEM;
1413
1414 *ret = z;
1415 return 1;
1416 }
1417
1418 static int user_record_augment(UserRecord *h, JsonDispatchFlags json_flags) {
1419 int r;
1420
1421 assert(h);
1422
1423 if (!FLAGS_SET(h->mask, USER_RECORD_REGULAR))
1424 return 0;
1425
1426 assert(h->user_name);
1427
1428 if (!h->user_name_and_realm_auto && h->realm) {
1429 h->user_name_and_realm_auto = strjoin(h->user_name, "@", h->realm);
1430 if (!h->user_name_and_realm_auto)
1431 return json_log_oom(h->json, json_flags);
1432 }
1433
1434 /* Let's add in the following automatisms only for regular users, they don't make sense for any others */
1435 if (user_record_disposition(h) != USER_REGULAR)
1436 return 0;
1437
1438 if (!h->home_directory && !h->home_directory_auto) {
1439 h->home_directory_auto = path_join("/home/", h->user_name);
1440 if (!h->home_directory_auto)
1441 return json_log_oom(h->json, json_flags);
1442 }
1443
1444 if (!h->image_path && !h->image_path_auto) {
1445 r = user_record_build_image_path(user_record_storage(h), user_record_user_name_and_realm(h), &h->image_path_auto);
1446 if (r < 0)
1447 return json_log(h->json, json_flags, r, "Failed to determine default image path: %m");
1448 }
1449
1450 return 0;
1451 }
1452
1453 int user_group_record_mangle(
1454 JsonVariant *v,
1455 UserRecordLoadFlags load_flags,
1456 JsonVariant **ret_variant,
1457 UserRecordMask *ret_mask) {
1458
1459 static const struct {
1460 UserRecordMask mask;
1461 const char *name;
1462 } mask_field[] = {
1463 { USER_RECORD_PRIVILEGED, "privileged" },
1464 { USER_RECORD_SECRET, "secret" },
1465 { USER_RECORD_BINDING, "binding" },
1466 { USER_RECORD_PER_MACHINE, "perMachine" },
1467 { USER_RECORD_STATUS, "status" },
1468 { USER_RECORD_SIGNATURE, "signature" },
1469 };
1470
1471 JsonDispatchFlags json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags);
1472 _cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
1473 JsonVariant *array[ELEMENTSOF(mask_field) * 2];
1474 size_t n_retain = 0, i;
1475 UserRecordMask m = 0;
1476 int r;
1477
1478 assert((load_flags & _USER_RECORD_MASK_MAX) == 0); /* detect mistakes when accidentally passing
1479 * UserRecordMask bit masks as UserRecordLoadFlags
1480 * value */
1481
1482 assert(v);
1483 assert(ret_variant);
1484 assert(ret_mask);
1485
1486 /* Note that this function is shared with the group record parser, hence we try to be generic in our
1487 * log message wording here, to cover both cases. */
1488
1489 if (!json_variant_is_object(v))
1490 return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record is not a JSON object, refusing.");
1491
1492 if (USER_RECORD_ALLOW_MASK(load_flags) == 0) /* allow nothing? */
1493 return json_log(v, json_flags, SYNTHETIC_ERRNO(EINVAL), "Nothing allowed in record, refusing.");
1494
1495 if (USER_RECORD_STRIP_MASK(load_flags) == _USER_RECORD_MASK_MAX) /* strip everything? */
1496 return json_log(v, json_flags, SYNTHETIC_ERRNO(EINVAL), "Stripping everything from record, refusing.");
1497
1498 /* Check if we have the special sections and if they match our flags set */
1499 for (i = 0; i < ELEMENTSOF(mask_field); i++) {
1500 JsonVariant *e, *k;
1501
1502 if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags), mask_field[i].mask)) {
1503 if (!w)
1504 w = json_variant_ref(v);
1505
1506 r = json_variant_filter(&w, STRV_MAKE(mask_field[i].name));
1507 if (r < 0)
1508 return json_log(w, json_flags, r, "Failed to remove field from variant: %m");
1509
1510 continue;
1511 }
1512
1513 e = json_variant_by_key_full(v, mask_field[i].name, &k);
1514 if (e) {
1515 if (!FLAGS_SET(USER_RECORD_ALLOW_MASK(load_flags), mask_field[i].mask))
1516 return json_log(e, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record contains '%s' field, which is not allowed.", mask_field[i].name);
1517
1518 if (FLAGS_SET(load_flags, USER_RECORD_STRIP_REGULAR)) {
1519 array[n_retain++] = k;
1520 array[n_retain++] = e;
1521 }
1522
1523 m |= mask_field[i].mask;
1524 } else {
1525 if (FLAGS_SET(USER_RECORD_REQUIRE_MASK(load_flags), mask_field[i].mask))
1526 return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record lacks '%s' field, which is required.", mask_field[i].name);
1527 }
1528 }
1529
1530 if (FLAGS_SET(load_flags, USER_RECORD_STRIP_REGULAR)) {
1531 /* If we are supposed to strip regular items, then let's instead just allocate a new object
1532 * with just the stuff we need. */
1533
1534 w = json_variant_unref(w);
1535 r = json_variant_new_object(&w, array, n_retain);
1536 if (r < 0)
1537 return json_log(v, json_flags, r, "Failed to allocate new object: %m");
1538 } else {
1539 /* And now check if there's anything else in the record */
1540 for (i = 0; i < json_variant_elements(v); i += 2) {
1541 const char *f;
1542 bool special = false;
1543 size_t j;
1544
1545 assert_se(f = json_variant_string(json_variant_by_index(v, i)));
1546
1547 for (j = 0; j < ELEMENTSOF(mask_field); j++)
1548 if (streq(f, mask_field[j].name)) { /* already covered in the loop above */
1549 special = true;
1550 continue;
1551 }
1552
1553 if (!special) {
1554 if ((load_flags & (USER_RECORD_ALLOW_REGULAR|USER_RECORD_REQUIRE_REGULAR)) == 0)
1555 return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record contains '%s' field, which is not allowed.", f);
1556
1557 m |= USER_RECORD_REGULAR;
1558 break;
1559 }
1560 }
1561 }
1562
1563 if (FLAGS_SET(load_flags, USER_RECORD_REQUIRE_REGULAR) && !FLAGS_SET(m, USER_RECORD_REGULAR))
1564 return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record lacks basic identity fields, which are required.");
1565
1566 if (m == 0)
1567 return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record is empty.");
1568
1569 if (w)
1570 *ret_variant = TAKE_PTR(w);
1571 else
1572 *ret_variant = json_variant_ref(v);
1573
1574 *ret_mask = m;
1575 return 0;
1576 }
1577
1578 int user_record_load(UserRecord *h, JsonVariant *v, UserRecordLoadFlags load_flags) {
1579
1580 static const JsonDispatch user_dispatch_table[] = {
1581 { "userName", JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(UserRecord, user_name), JSON_RELAX},
1582 { "realm", JSON_VARIANT_STRING, json_dispatch_realm, offsetof(UserRecord, realm), 0 },
1583 { "realName", JSON_VARIANT_STRING, json_dispatch_gecos, offsetof(UserRecord, real_name), 0 },
1584 { "emailAddress", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, email_address), JSON_SAFE },
1585 { "iconName", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, icon_name), JSON_SAFE },
1586 { "location", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, location), 0 },
1587 { "disposition", JSON_VARIANT_STRING, json_dispatch_user_disposition, offsetof(UserRecord, disposition), 0 },
1588 { "lastChangeUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, last_change_usec), 0 },
1589 { "lastPasswordChangeUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, last_password_change_usec), 0 },
1590 { "shell", JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, shell), 0 },
1591 { "umask", JSON_VARIANT_UNSIGNED, json_dispatch_umask, offsetof(UserRecord, umask), 0 },
1592 { "environment", JSON_VARIANT_ARRAY, json_dispatch_environment, offsetof(UserRecord, environment), 0 },
1593 { "timeZone", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, time_zone), JSON_SAFE },
1594 { "preferredLanguage", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, preferred_language), JSON_SAFE },
1595 { "niceLevel", _JSON_VARIANT_TYPE_INVALID, json_dispatch_nice, offsetof(UserRecord, nice_level), 0 },
1596 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID, json_dispatch_rlimits, offsetof(UserRecord, rlimits), 0 },
1597 { "locked", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, locked), 0 },
1598 { "notBeforeUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, not_before_usec), 0 },
1599 { "notAfterUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, not_after_usec), 0 },
1600 { "storage", JSON_VARIANT_STRING, json_dispatch_storage, offsetof(UserRecord, storage), 0 },
1601 { "diskSize", JSON_VARIANT_UNSIGNED, json_dispatch_disk_size, offsetof(UserRecord, disk_size), 0 },
1602 { "diskSizeRelative", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, disk_size_relative), 0 },
1603 { "skeletonDirectory", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, skeleton_directory), 0 },
1604 { "accessMode", JSON_VARIANT_UNSIGNED, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 },
1605 { "tasksMax", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, tasks_max), 0 },
1606 { "memoryHigh", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_high), 0 },
1607 { "memoryMax", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_max), 0 },
1608 { "cpuWeight", JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, cpu_weight), 0 },
1609 { "ioWeight", JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, io_weight), 0 },
1610 { "mountNoDevices", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, nodev), 0 },
1611 { "mountNoSuid", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, nosuid), 0 },
1612 { "mountNoExecute", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, noexec), 0 },
1613 { "cifsDomain", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_domain), JSON_SAFE },
1614 { "cifsUserName", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_user_name), JSON_SAFE },
1615 { "cifsService", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_service), JSON_SAFE },
1616 { "imagePath", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, image_path), 0 },
1617 { "homeDirectory", JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, home_directory), 0 },
1618 { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 },
1619 { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 },
1620 { "memberOf", JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(UserRecord, member_of), JSON_RELAX},
1621 { "fileSystemType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, file_system_type), JSON_SAFE },
1622 { "partitionUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 },
1623 { "luksUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 },
1624 { "fileSystemUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 },
1625 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID, json_dispatch_tristate, offsetof(UserRecord, luks_discard), 0 },
1626 { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID, json_dispatch_tristate, offsetof(UserRecord, luks_offline_discard), 0 },
1627 { "luksCipher", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher), JSON_SAFE },
1628 { "luksCipherMode", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), JSON_SAFE },
1629 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 },
1630 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_pbkdf_hash_algorithm), JSON_SAFE },
1631 { "luksPbkdfType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_pbkdf_type), JSON_SAFE },
1632 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_time_cost_usec), 0 },
1633 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_memory_cost), 0 },
1634 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_parallel_threads), 0 },
1635 { "service", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, service), JSON_SAFE },
1636 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, ratelimit_interval_usec), 0 },
1637 { "rateLimitBurst", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, ratelimit_burst), 0 },
1638 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, enforce_password_policy), 0 },
1639 { "autoLogin", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, auto_login), 0 },
1640 { "stopDelayUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, stop_delay_usec), 0 },
1641 { "killProcesses", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, kill_processes), 0 },
1642 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, password_change_min_usec), 0 },
1643 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, password_change_max_usec), 0 },
1644 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, password_change_warn_usec), 0 },
1645 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserRecord, password_change_inactive_usec), 0 },
1646 { "passwordChangeNow", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, password_change_now), 0 },
1647 { "pkcs11TokenUri", JSON_VARIANT_ARRAY, dispatch_pkcs11_uri_array, offsetof(UserRecord, pkcs11_token_uri), 0 },
1648 { "fido2HmacCredential", JSON_VARIANT_ARRAY, dispatch_fido2_hmac_credential_array, 0, 0 },
1649 { "recoveryKeyType", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(UserRecord, recovery_key_type), 0 },
1650
1651 { "secret", JSON_VARIANT_OBJECT, dispatch_secret, 0, 0 },
1652 { "privileged", JSON_VARIANT_OBJECT, dispatch_privileged, 0, 0 },
1653
1654 /* Ignore the perMachine, binding, status stuff here, and process it later, so that it overrides whatever is set above */
1655 { "perMachine", JSON_VARIANT_ARRAY, NULL, 0, 0 },
1656 { "binding", JSON_VARIANT_OBJECT, NULL, 0, 0 },
1657 { "status", JSON_VARIANT_OBJECT, NULL, 0, 0 },
1658
1659 /* Ignore 'signature', we check it with explicit accessors instead */
1660 { "signature", JSON_VARIANT_ARRAY, NULL, 0, 0 },
1661 {},
1662 };
1663
1664 JsonDispatchFlags json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags);
1665 int r;
1666
1667 assert(h);
1668 assert(!h->json);
1669
1670 /* Note that this call will leave a half-initialized record around on failure! */
1671
1672 r = user_group_record_mangle(v, load_flags, &h->json, &h->mask);
1673 if (r < 0)
1674 return r;
1675
1676 r = json_dispatch(h->json, user_dispatch_table, NULL, json_flags, h);
1677 if (r < 0)
1678 return r;
1679
1680 /* During the parsing operation above we ignored the 'perMachine', 'binding' and 'status' fields,
1681 * since we want them to override the global options. Let's process them now. */
1682
1683 r = dispatch_per_machine("perMachine", json_variant_by_key(h->json, "perMachine"), json_flags, h);
1684 if (r < 0)
1685 return r;
1686
1687 r = dispatch_binding("binding", json_variant_by_key(h->json, "binding"), json_flags, h);
1688 if (r < 0)
1689 return r;
1690
1691 r = dispatch_status("status", json_variant_by_key(h->json, "status"), json_flags, h);
1692 if (r < 0)
1693 return r;
1694
1695 if (FLAGS_SET(h->mask, USER_RECORD_REGULAR) && !h->user_name)
1696 return json_log(h->json, json_flags, SYNTHETIC_ERRNO(EINVAL), "User name field missing, refusing.");
1697
1698 r = user_record_augment(h, json_flags);
1699 if (r < 0)
1700 return r;
1701
1702 return 0;
1703 }
1704
1705 int user_record_build(UserRecord **ret, ...) {
1706 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
1707 _cleanup_(user_record_unrefp) UserRecord *u = NULL;
1708 va_list ap;
1709 int r;
1710
1711 assert(ret);
1712
1713 va_start(ap, ret);
1714 r = json_buildv(&v, ap);
1715 va_end(ap);
1716
1717 if (r < 0)
1718 return r;
1719
1720 u = user_record_new();
1721 if (!u)
1722 return -ENOMEM;
1723
1724 r = user_record_load(u, v, USER_RECORD_LOAD_FULL);
1725 if (r < 0)
1726 return r;
1727
1728 *ret = TAKE_PTR(u);
1729 return 0;
1730 }
1731
1732 const char *user_record_user_name_and_realm(UserRecord *h) {
1733 assert(h);
1734
1735 /* Return the pre-initialized joined string if it is defined */
1736 if (h->user_name_and_realm_auto)
1737 return h->user_name_and_realm_auto;
1738
1739 /* If it's not defined then we cannot have a realm */
1740 assert(!h->realm);
1741 return h->user_name;
1742 }
1743
1744 UserStorage user_record_storage(UserRecord *h) {
1745 assert(h);
1746
1747 if (h->storage >= 0)
1748 return h->storage;
1749
1750 return USER_CLASSIC;
1751 }
1752
1753 const char *user_record_file_system_type(UserRecord *h) {
1754 assert(h);
1755
1756 return h->file_system_type ?: "btrfs";
1757 }
1758
1759 const char *user_record_skeleton_directory(UserRecord *h) {
1760 assert(h);
1761
1762 return h->skeleton_directory ?: "/etc/skel";
1763 }
1764
1765 mode_t user_record_access_mode(UserRecord *h) {
1766 assert(h);
1767
1768 return h->access_mode != (mode_t) -1 ? h->access_mode : 0700;
1769 }
1770
1771 const char* user_record_home_directory(UserRecord *h) {
1772 assert(h);
1773
1774 if (h->home_directory)
1775 return h->home_directory;
1776 if (h->home_directory_auto)
1777 return h->home_directory_auto;
1778
1779 /* The root user is special, hence be special about it */
1780 if (streq_ptr(h->user_name, "root"))
1781 return "/root";
1782
1783 return "/";
1784 }
1785
1786 const char *user_record_image_path(UserRecord *h) {
1787 assert(h);
1788
1789 if (h->image_path)
1790 return h->image_path;
1791 if (h->image_path_auto)
1792 return h->image_path_auto;
1793
1794 return IN_SET(user_record_storage(h), USER_CLASSIC, USER_DIRECTORY, USER_SUBVOLUME, USER_FSCRYPT) ? user_record_home_directory(h) : NULL;
1795 }
1796
1797 const char *user_record_cifs_user_name(UserRecord *h) {
1798 assert(h);
1799
1800 return h->cifs_user_name ?: h->user_name;
1801 }
1802
1803 unsigned long user_record_mount_flags(UserRecord *h) {
1804 assert(h);
1805
1806 return (h->nosuid ? MS_NOSUID : 0) |
1807 (h->noexec ? MS_NOEXEC : 0) |
1808 (h->nodev ? MS_NODEV : 0);
1809 }
1810
1811 const char *user_record_shell(UserRecord *h) {
1812 assert(h);
1813
1814 if (h->shell)
1815 return h->shell;
1816
1817 if (streq_ptr(h->user_name, "root"))
1818 return "/bin/sh";
1819
1820 if (user_record_disposition(h) == USER_REGULAR)
1821 return "/bin/bash";
1822
1823 return NOLOGIN;
1824 }
1825
1826 const char *user_record_real_name(UserRecord *h) {
1827 assert(h);
1828
1829 return h->real_name ?: h->user_name;
1830 }
1831
1832 bool user_record_luks_discard(UserRecord *h) {
1833 const char *ip;
1834
1835 assert(h);
1836
1837 if (h->luks_discard >= 0)
1838 return h->luks_discard;
1839
1840 ip = user_record_image_path(h);
1841 if (!ip)
1842 return false;
1843
1844 /* Use discard by default if we are referring to a real block device, but not when operating on a
1845 * loopback device. We want to optimize for SSD and flash storage after all, but we should be careful
1846 * when storing stuff on top of regular file systems in loopback files as doing discard then would
1847 * mean thin provisioning and we should not do that willy-nilly since it means we'll risk EIO later
1848 * on should the disk space to back our file systems not be available. */
1849
1850 return path_startswith(ip, "/dev/");
1851 }
1852
1853 bool user_record_luks_offline_discard(UserRecord *h) {
1854 const char *ip;
1855
1856 assert(h);
1857
1858 if (h->luks_offline_discard >= 0)
1859 return h->luks_offline_discard;
1860
1861 /* Discard while we are logged out should generally be a good idea, except when operating directly on
1862 * physical media, where we should just bind it to the online discard mode. */
1863
1864 ip = user_record_image_path(h);
1865 if (!ip)
1866 return false;
1867
1868 if (path_startswith(ip, "/dev/"))
1869 return user_record_luks_discard(h);
1870
1871 return true;
1872 }
1873
1874 const char *user_record_luks_cipher(UserRecord *h) {
1875 assert(h);
1876
1877 return h->luks_cipher ?: "aes";
1878 }
1879
1880 const char *user_record_luks_cipher_mode(UserRecord *h) {
1881 assert(h);
1882
1883 return h->luks_cipher_mode ?: "xts-plain64";
1884 }
1885
1886 uint64_t user_record_luks_volume_key_size(UserRecord *h) {
1887 assert(h);
1888
1889 /* We return a value here that can be cast without loss into size_t which is what libcrypsetup expects */
1890
1891 if (h->luks_volume_key_size == UINT64_MAX)
1892 return 256 / 8;
1893
1894 return MIN(h->luks_volume_key_size, SIZE_MAX);
1895 }
1896
1897 const char* user_record_luks_pbkdf_type(UserRecord *h) {
1898 assert(h);
1899
1900 return h->luks_pbkdf_type ?: "argon2i";
1901 }
1902
1903 uint64_t user_record_luks_pbkdf_time_cost_usec(UserRecord *h) {
1904 assert(h);
1905
1906 /* Returns a value with ms granularity, since that's what libcryptsetup expects */
1907
1908 if (h->luks_pbkdf_time_cost_usec == UINT64_MAX)
1909 return 500 * USEC_PER_MSEC; /* We default to 500ms, in contrast to libcryptsetup's 2s, which is just awfully slow on every login */
1910
1911 return MIN(DIV_ROUND_UP(h->luks_pbkdf_time_cost_usec, USEC_PER_MSEC), UINT32_MAX) * USEC_PER_MSEC;
1912 }
1913
1914 uint64_t user_record_luks_pbkdf_memory_cost(UserRecord *h) {
1915 assert(h);
1916
1917 /* Returns a value with kb granularity, since that's what libcryptsetup expects */
1918
1919 if (h->luks_pbkdf_memory_cost == UINT64_MAX)
1920 return 64*1024*1024; /* We default to 64M, since this should work on smaller systems too */
1921
1922 return MIN(DIV_ROUND_UP(h->luks_pbkdf_memory_cost, 1024), UINT32_MAX) * 1024;
1923 }
1924
1925 uint64_t user_record_luks_pbkdf_parallel_threads(UserRecord *h) {
1926 assert(h);
1927
1928 if (h->luks_pbkdf_memory_cost == UINT64_MAX)
1929 return 1; /* We default to 1, since this should work on smaller systems too */
1930
1931 return MIN(h->luks_pbkdf_parallel_threads, UINT32_MAX);
1932 }
1933
1934 const char *user_record_luks_pbkdf_hash_algorithm(UserRecord *h) {
1935 assert(h);
1936
1937 return h->luks_pbkdf_hash_algorithm ?: "sha512";
1938 }
1939
1940 gid_t user_record_gid(UserRecord *h) {
1941 assert(h);
1942
1943 if (gid_is_valid(h->gid))
1944 return h->gid;
1945
1946 return (gid_t) h->uid;
1947 }
1948
1949 UserDisposition user_record_disposition(UserRecord *h) {
1950 assert(h);
1951
1952 if (h->disposition >= 0)
1953 return h->disposition;
1954
1955 /* If not declared, derive from UID */
1956
1957 if (!uid_is_valid(h->uid))
1958 return _USER_DISPOSITION_INVALID;
1959
1960 if (h->uid == 0 || h->uid == UID_NOBODY)
1961 return USER_INTRINSIC;
1962
1963 if (uid_is_system(h->uid))
1964 return USER_SYSTEM;
1965
1966 if (uid_is_dynamic(h->uid))
1967 return USER_DYNAMIC;
1968
1969 if (uid_is_container(h->uid))
1970 return USER_CONTAINER;
1971
1972 if (h->uid > INT32_MAX)
1973 return USER_RESERVED;
1974
1975 return USER_REGULAR;
1976 }
1977
1978 int user_record_removable(UserRecord *h) {
1979 UserStorage storage;
1980 assert(h);
1981
1982 if (h->removable >= 0)
1983 return h->removable;
1984
1985 /* Refuse to decide for classic records */
1986 storage = user_record_storage(h);
1987 if (h->storage < 0 || h->storage == USER_CLASSIC)
1988 return -1;
1989
1990 /* For now consider only LUKS home directories with a reference by path as removable */
1991 return storage == USER_LUKS && path_startswith(user_record_image_path(h), "/dev/");
1992 }
1993
1994 uint64_t user_record_ratelimit_interval_usec(UserRecord *h) {
1995 assert(h);
1996
1997 if (h->ratelimit_interval_usec == UINT64_MAX)
1998 return DEFAULT_RATELIMIT_INTERVAL_USEC;
1999
2000 return h->ratelimit_interval_usec;
2001 }
2002
2003 uint64_t user_record_ratelimit_burst(UserRecord *h) {
2004 assert(h);
2005
2006 if (h->ratelimit_burst == UINT64_MAX)
2007 return DEFAULT_RATELIMIT_BURST;
2008
2009 return h->ratelimit_burst;
2010 }
2011
2012 bool user_record_can_authenticate(UserRecord *h) {
2013 assert(h);
2014
2015 /* Returns true if there's some form of property configured that the user can authenticate against */
2016
2017 if (h->n_pkcs11_encrypted_key > 0)
2018 return true;
2019
2020 if (h->n_fido2_hmac_salt > 0)
2021 return true;
2022
2023 return !strv_isempty(h->hashed_password);
2024 }
2025
2026 uint64_t user_record_ratelimit_next_try(UserRecord *h) {
2027 assert(h);
2028
2029 /* Calculates when the it's possible to login next. Returns:
2030 *
2031 * UINT64_MAX → Nothing known
2032 * 0 → Right away
2033 * Any other → Next time in CLOCK_REALTIME in usec (which could be in the past)
2034 */
2035
2036 if (h->ratelimit_begin_usec == UINT64_MAX ||
2037 h->ratelimit_count == UINT64_MAX)
2038 return UINT64_MAX;
2039
2040 if (h->ratelimit_begin_usec > now(CLOCK_REALTIME)) /* If the ratelimit time is in the future, then
2041 * the local clock is probably incorrect. Let's
2042 * not refuse login then. */
2043 return UINT64_MAX;
2044
2045 if (h->ratelimit_count < user_record_ratelimit_burst(h))
2046 return 0;
2047
2048 return usec_add(h->ratelimit_begin_usec, user_record_ratelimit_interval_usec(h));
2049 }
2050
2051 bool user_record_equal(UserRecord *a, UserRecord *b) {
2052 assert(a);
2053 assert(b);
2054
2055 /* We assume that when a record is modified its JSON data is updated at the same time, hence it's
2056 * sufficient to compare the JSON data. */
2057
2058 return json_variant_equal(a->json, b->json);
2059 }
2060
2061 bool user_record_compatible(UserRecord *a, UserRecord *b) {
2062 assert(a);
2063 assert(b);
2064
2065 /* If either lacks a the regular section, we can't really decide, let's hence say they are
2066 * incompatible. */
2067 if (!(a->mask & b->mask & USER_RECORD_REGULAR))
2068 return false;
2069
2070 return streq_ptr(a->user_name, b->user_name) &&
2071 streq_ptr(a->realm, b->realm);
2072 }
2073
2074 int user_record_compare_last_change(UserRecord *a, UserRecord *b) {
2075 assert(a);
2076 assert(b);
2077
2078 if (a->last_change_usec == b->last_change_usec)
2079 return 0;
2080
2081 /* Always consider a record with a timestamp newer than one without */
2082 if (a->last_change_usec == UINT64_MAX)
2083 return -1;
2084 if (b->last_change_usec == UINT64_MAX)
2085 return 1;
2086
2087 return CMP(a->last_change_usec, b->last_change_usec);
2088 }
2089
2090 int user_record_clone(UserRecord *h, UserRecordLoadFlags flags, UserRecord **ret) {
2091 _cleanup_(user_record_unrefp) UserRecord *c = NULL;
2092 int r;
2093
2094 assert(h);
2095 assert(ret);
2096
2097 c = user_record_new();
2098 if (!c)
2099 return -ENOMEM;
2100
2101 r = user_record_load(c, h->json, flags);
2102 if (r < 0)
2103 return r;
2104
2105 *ret = TAKE_PTR(c);
2106 return 0;
2107 }
2108
2109 int user_record_masked_equal(UserRecord *a, UserRecord *b, UserRecordMask mask) {
2110 _cleanup_(user_record_unrefp) UserRecord *x = NULL, *y = NULL;
2111 int r;
2112
2113 assert(a);
2114 assert(b);
2115
2116 /* Compares the two records, but ignores anything not listed in the specified mask */
2117
2118 if ((a->mask & ~mask) != 0) {
2119 r = user_record_clone(a, USER_RECORD_ALLOW(mask) | USER_RECORD_STRIP(~mask & _USER_RECORD_MASK_MAX), &x);
2120 if (r < 0)
2121 return r;
2122
2123 a = x;
2124 }
2125
2126 if ((b->mask & ~mask) != 0) {
2127 r = user_record_clone(b, USER_RECORD_ALLOW(mask) | USER_RECORD_STRIP(~mask & _USER_RECORD_MASK_MAX), &y);
2128 if (r < 0)
2129 return r;
2130
2131 b = y;
2132 }
2133
2134 return user_record_equal(a, b);
2135 }
2136
2137 int user_record_test_blocked(UserRecord *h) {
2138 usec_t n;
2139
2140 /* Checks whether access to the specified user shall be allowed at the moment. Returns:
2141 *
2142 * -ESTALE: Record is from the future
2143 * -ENOLCK: Record is blocked
2144 * -EL2HLT: Record is not valid yet
2145 * -EL3HLT: Record is not valid anymore
2146 *
2147 */
2148
2149 assert(h);
2150
2151 if (h->locked > 0)
2152 return -ENOLCK;
2153
2154 n = now(CLOCK_REALTIME);
2155
2156 if (h->not_before_usec != UINT64_MAX && n < h->not_before_usec)
2157 return -EL2HLT;
2158 if (h->not_after_usec != UINT64_MAX && n > h->not_after_usec)
2159 return -EL3HLT;
2160
2161 if (h->last_change_usec != UINT64_MAX &&
2162 h->last_change_usec > n) /* Complain during log-ins when the record is from the future */
2163 return -ESTALE;
2164
2165 return 0;
2166 }
2167
2168 int user_record_test_password_change_required(UserRecord *h) {
2169 bool change_permitted;
2170 usec_t n;
2171
2172 assert(h);
2173
2174 /* Checks whether the user must change the password when logging in
2175
2176 -EKEYREVOKED: Change password now because admin said so
2177 -EOWNERDEAD: Change password now because it expired
2178 -EKEYREJECTED: Password is expired, no changing is allowed
2179 -EKEYEXPIRED: Password is about to expire, warn user
2180 -ENETDOWN: Record has expiration info but no password change timestamp
2181 -EROFS: No password change required nor permitted
2182 -ESTALE: RTC likely incorrect, last password change is in the future
2183 0: No password change required, but permitted
2184 */
2185
2186 /* If a password change request has been set explicitly, it overrides everything */
2187 if (h->password_change_now > 0)
2188 return -EKEYREVOKED;
2189
2190 n = now(CLOCK_REALTIME);
2191
2192 /* Password change in the future? Then our RTC is likely incorrect */
2193 if (h->last_password_change_usec != UINT64_MAX &&
2194 h->last_password_change_usec > n &&
2195 (h->password_change_min_usec != UINT64_MAX ||
2196 h->password_change_max_usec != UINT64_MAX ||
2197 h->password_change_inactive_usec != UINT64_MAX))
2198 return -ESTALE;
2199
2200 /* Then, let's check if password changing is currently allowed at all */
2201 if (h->password_change_min_usec != UINT64_MAX) {
2202
2203 /* Expiry configured but no password change timestamp known? */
2204 if (h->last_password_change_usec == UINT64_MAX)
2205 return -ENETDOWN;
2206
2207 if (h->password_change_min_usec >= UINT64_MAX - h->last_password_change_usec)
2208 change_permitted = false;
2209 else
2210 change_permitted = n >= h->last_password_change_usec + h->password_change_min_usec;
2211
2212 } else
2213 change_permitted = true;
2214
2215 /* Let's check whether the password has expired. */
2216 if (!(h->password_change_max_usec == UINT64_MAX ||
2217 h->password_change_max_usec >= UINT64_MAX - h->last_password_change_usec)) {
2218
2219 uint64_t change_before;
2220
2221 /* Expiry configured but no password change timestamp known? */
2222 if (h->last_password_change_usec == UINT64_MAX)
2223 return -ENETDOWN;
2224
2225 /* Password is in inactive phase? */
2226 if (h->password_change_inactive_usec != UINT64_MAX &&
2227 h->password_change_inactive_usec < UINT64_MAX - h->password_change_max_usec) {
2228 usec_t added;
2229
2230 added = h->password_change_inactive_usec + h->password_change_max_usec;
2231 if (added < UINT64_MAX - h->last_password_change_usec &&
2232 n >= h->last_password_change_usec + added)
2233 return -EKEYREJECTED;
2234 }
2235
2236 /* Password needs to be changed now? */
2237 change_before = h->last_password_change_usec + h->password_change_max_usec;
2238 if (n >= change_before)
2239 return change_permitted ? -EOWNERDEAD : -EKEYREJECTED;
2240
2241 /* Warn user? */
2242 if (h->password_change_warn_usec != UINT64_MAX &&
2243 (change_before < h->password_change_warn_usec ||
2244 n >= change_before - h->password_change_warn_usec))
2245 return change_permitted ? -EKEYEXPIRED : -EROFS;
2246 }
2247
2248 /* No password changing necessary */
2249 return change_permitted ? 0 : -EROFS;
2250 }
2251
2252 static const char* const user_storage_table[_USER_STORAGE_MAX] = {
2253 [USER_CLASSIC] = "classic",
2254 [USER_LUKS] = "luks",
2255 [USER_DIRECTORY] = "directory",
2256 [USER_SUBVOLUME] = "subvolume",
2257 [USER_FSCRYPT] = "fscrypt",
2258 [USER_CIFS] = "cifs",
2259 };
2260
2261 DEFINE_STRING_TABLE_LOOKUP(user_storage, UserStorage);
2262
2263 static const char* const user_disposition_table[_USER_DISPOSITION_MAX] = {
2264 [USER_INTRINSIC] = "intrinsic",
2265 [USER_SYSTEM] = "system",
2266 [USER_DYNAMIC] = "dynamic",
2267 [USER_REGULAR] = "regular",
2268 [USER_CONTAINER] = "container",
2269 [USER_RESERVED] = "reserved",
2270 };
2271
2272 DEFINE_STRING_TABLE_LOOKUP(user_disposition, UserDisposition);