1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "cgroup-util.h"
6 #include "dns-domain.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"
20 #include "user-record.h"
21 #include "user-util.h"
23 #define DEFAULT_RATELIMIT_BURST 30
24 #define DEFAULT_RATELIMIT_INTERVAL_USEC (1*USEC_PER_MINUTE)
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
) {
31 r
= parse_uid(t
, &uid
);
33 return log_debug_errno(r
, "%s: failed to parse %s %s, ignoring: %m", path
, name
, t
);
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
,
50 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
51 _cleanup_fclose_
FILE *f
= NULL
;
55 path
= "/etc/login.defs";
57 r
= chase_symlinks_and_fopen_unlocked(path
, root
, CHASE_PREFIX_ROOT
, "re", &f
, NULL
);
61 return log_debug_errno(r
, "Failed to open %s: %m", path
);
64 _cleanup_free_
char *line
= NULL
;
67 r
= read_line(f
, LINE_MAX
, &line
);
69 return log_debug_errno(r
, "Failed to read %s: %m", path
);
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
);
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. */
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. */
100 const UGIDAllocationRange
*acquire_ugid_allocation_range(void) {
101 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
102 static thread_local UGIDAllocationRange defs
= {
104 static const UGIDAllocationRange defs
= {
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
,
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. */
117 static thread_local
bool initialized
= false;
120 (void) read_login_defs(&defs
, NULL
, NULL
);
128 bool uid_is_system(uid_t uid
) {
129 const UGIDAllocationRange
*defs
;
130 assert_se(defs
= acquire_ugid_allocation_range());
132 return uid
<= defs
->system_uid_max
;
135 bool gid_is_system(gid_t gid
) {
136 const UGIDAllocationRange
*defs
;
137 assert_se(defs
= acquire_ugid_allocation_range());
139 return gid
<= defs
->system_gid_max
;
142 UserRecord
* user_record_new(void) {
145 h
= new(UserRecord
, 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
,
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
,
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
,
192 .enforce_password_policy
= -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,
208 static void pkcs11_encrypted_key_done(Pkcs11EncryptedKey
*k
) {
213 erase_and_free(k
->data
);
214 erase_and_free(k
->hashed_password
);
217 static void fido2_hmac_credential_done(Fido2HmacCredential
*c
) {
224 static void fido2_hmac_salt_done(Fido2HmacSalt
*s
) {
228 fido2_hmac_credential_done(&s
->credential
);
229 erase_and_free(s
->salt
);
230 erase_and_free(s
->hashed_password
);
233 static void recovery_key_done(RecoveryKey
*k
) {
238 erase_and_free(k
->hashed_password
);
241 static UserRecord
* user_record_free(UserRecord
*h
) {
247 free(h
->user_name_and_realm_auto
);
249 free(h
->email_address
);
250 erase_and_free(h
->password_hint
);
256 strv_free(h
->environment
);
258 free(h
->preferred_language
);
259 rlimit_free_all(h
->rlimits
);
261 free(h
->skeleton_directory
);
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
);
268 free(h
->cifs_service
);
269 free(h
->cifs_user_name
);
270 free(h
->cifs_domain
);
273 free(h
->image_path_auto
);
274 free(h
->home_directory
);
275 free(h
->home_directory_auto
);
277 strv_free(h
->member_of
);
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
);
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
);
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
);
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
);
302 json_variant_unref(h
->json
);
307 DEFINE_TRIVIAL_REF_UNREF_FUNC(UserRecord
, user_record
, user_record_free
);
309 int json_dispatch_realm(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
314 if (json_variant_is_null(variant
)) {
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
));
322 n
= json_variant_string(variant
);
323 r
= dns_name_is_valid(n
);
325 return json_log(variant
, flags
, r
, "Failed to check if JSON field '%s' is a valid DNS domain.", strna(name
));
327 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid DNS domain.", strna(name
));
329 r
= free_and_strdup(s
, n
);
331 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
336 int json_dispatch_gecos(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
340 if (json_variant_is_null(variant
)) {
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
));
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
);
353 _cleanup_free_
char *m
= NULL
;
355 json_log(variant
, flags
|JSON_DEBUG
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid GECOS compatible string, mangling.", strna(name
));
359 return json_log_oom(variant
, flags
);
361 free_and_replace(*s
, m
);
367 static int json_dispatch_nice(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
371 if (json_variant_is_null(variant
)) {
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
));
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
));
387 static int json_dispatch_rlimit_value(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
388 rlim_t
*ret
= userdata
;
390 if (json_variant_is_null(variant
))
391 *ret
= RLIM_INFINITY
;
392 else if (json_variant_is_unsigned(variant
)) {
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
);
401 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit value '%s' is not an unsigned integer.", name
);
406 static int json_dispatch_rlimits(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
407 struct rlimit
** limits
= userdata
;
414 if (json_variant_is_null(variant
)) {
415 rlimit_free_all(limits
);
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
));
422 JSON_VARIANT_OBJECT_FOREACH(key
, value
, variant
) {
423 JsonVariant
*jcur
, *jmax
;
428 p
= startswith(key
, "RLIMIT_");
432 l
= rlimit_from_string(p
);
434 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' not known.", key
);
436 if (!json_variant_is_object(value
))
437 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' has invalid value.", key
);
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
);
442 jcur
= json_variant_by_key(value
, "cur");
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
);
449 jmax
= json_variant_by_key(value
, "max");
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
);
459 limits
[l
] = newdup(struct rlimit
, &rl
, 1);
468 static int json_dispatch_filename_or_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
475 if (json_variant_is_null(variant
)) {
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
));
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
));
487 r
= free_and_strdup(s
, n
);
489 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
494 static int json_dispatch_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
499 if (json_variant_is_null(variant
)) {
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
));
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
));
513 r
= free_and_strdup(s
, n
);
515 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
520 static int json_dispatch_home_directory(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
525 if (json_variant_is_null(variant
)) {
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
));
533 n
= json_variant_string(variant
);
535 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid home directory path.", strna(name
));
537 r
= free_and_strdup(s
, n
);
539 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
544 static int json_dispatch_image_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
549 if (json_variant_is_null(variant
)) {
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
));
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
));
561 r
= free_and_strdup(s
, n
);
563 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
568 static int json_dispatch_umask(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
569 mode_t
*m
= userdata
;
572 if (json_variant_is_null(variant
)) {
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
));
580 k
= json_variant_unsigned(variant
);
582 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' outside of valid range 0…0777.", strna(name
));
588 static int json_dispatch_access_mode(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
589 mode_t
*m
= userdata
;
592 if (json_variant_is_null(variant
)) {
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
));
600 k
= json_variant_unsigned(variant
);
602 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' outside of valid range 0…07777.", strna(name
));
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
;
614 if (json_variant_is_null(variant
)) {
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
));
622 for (i
= 0; i
< json_variant_elements(variant
); i
++) {
623 _cleanup_free_
char *c
= NULL
;
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
));
631 assert_se(a
= json_variant_string(e
));
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
));
638 return json_log_oom(variant
, flags
);
640 r
= strv_env_replace(&n
, c
);
642 return json_log_oom(variant
, flags
);
647 strv_free_and_replace(*l
, n
);
651 int json_dispatch_user_disposition(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
652 UserDisposition
*disposition
= userdata
, k
;
654 if (json_variant_is_null(variant
)) {
655 *disposition
= _USER_DISPOSITION_INVALID
;
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
));
662 k
= user_disposition_from_string(json_variant_string(variant
));
664 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Disposition type '%s' not known.", json_variant_string(variant
));
670 static int json_dispatch_storage(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
671 UserStorage
*storage
= userdata
, k
;
673 if (json_variant_is_null(variant
)) {
674 *storage
= _USER_STORAGE_INVALID
;
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
));
681 k
= user_storage_from_string(json_variant_string(variant
));
683 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Storage type '%s' not known.", json_variant_string(variant
));
689 static int json_dispatch_disk_size(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
690 uint64_t *size
= userdata
;
693 if (json_variant_is_null(variant
)) {
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
));
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
);
709 static int json_dispatch_tasks_or_memory_max(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
710 uint64_t *limit
= userdata
;
713 if (json_variant_is_null(variant
)) {
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
));
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);
729 static int json_dispatch_weight(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
730 uint64_t *weight
= userdata
;
733 if (json_variant_is_null(variant
)) {
734 *weight
= UINT64_MAX
;
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
));
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
);
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
;
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
));
758 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
760 if (!json_variant_is_string(e
))
761 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
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
));
766 r
= strv_extend(&l
, json_variant_string(e
));
768 return json_log(e
, flags
, r
, "Failed to append array element: %m");
771 r
= strv_extend_strv(list
, l
, true);
773 return json_log(variant
, flags
, r
, "Failed to merge user/group arrays: %m");
778 static int dispatch_secret(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
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 },
789 return json_dispatch(variant
, secret_dispatch_table
, NULL
, flags
, userdata
);
792 static int dispatch_pkcs11_uri(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
797 if (json_variant_is_null(variant
)) {
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
));
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
));
809 r
= free_and_strdup(s
, n
);
811 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
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
;
822 if (json_variant_is_null(variant
)) {
827 if (json_variant_is_string(variant
)) {
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
));
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
));
843 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
846 if (!json_variant_is_string(e
))
847 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
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
);
853 r
= strv_extend(&z
, n
);
859 strv_free_and_replace(*l
, z
);
863 static int dispatch_pkcs11_key_data(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
864 Pkcs11EncryptedKey
*k
= userdata
;
869 if (json_variant_is_null(variant
)) {
870 k
->data
= erase_and_free(k
->data
);
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
));
878 r
= unbase64mem(json_variant_string(variant
), (size_t) -1, &b
, &l
);
880 return json_log(variant
, flags
, r
, "Failed to decode encrypted PKCS#11 key: %m");
882 erase_and_free(k
->data
);
889 static int dispatch_pkcs11_key(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
890 UserRecord
*h
= userdata
;
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
));
897 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
898 Pkcs11EncryptedKey
*array
, *k
;
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
},
907 if (!json_variant_is_object(e
))
908 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
910 array
= reallocarray(h
->pkcs11_encrypted_key
, h
->n_pkcs11_encrypted_key
+ 1, sizeof(Pkcs11EncryptedKey
));
914 h
->pkcs11_encrypted_key
= array
;
915 k
= h
->pkcs11_encrypted_key
+ h
->n_pkcs11_encrypted_key
;
916 *k
= (Pkcs11EncryptedKey
) {};
918 r
= json_dispatch(e
, pkcs11_key_dispatch_table
, NULL
, flags
, k
);
920 pkcs11_encrypted_key_done(k
);
924 h
->n_pkcs11_encrypted_key
++;
930 static int dispatch_fido2_hmac_credential(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
931 Fido2HmacCredential
*k
= userdata
;
936 if (json_variant_is_null(variant
)) {
937 k
->id
= mfree(k
->id
);
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
));
945 r
= unbase64mem(json_variant_string(variant
), (size_t) -1, &b
, &l
);
947 return json_log(variant
, flags
, r
, "Failed to decode FIDO2 credential ID: %m");
949 free_and_replace(k
->id
, b
);
955 static int dispatch_fido2_hmac_credential_array(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
956 UserRecord
*h
= userdata
;
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
));
963 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
964 Fido2HmacCredential
*array
;
968 if (!json_variant_is_string(e
))
969 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
971 array
= reallocarray(h
->fido2_hmac_credential
, h
->n_fido2_hmac_credential
+ 1, sizeof(Fido2HmacCredential
));
975 r
= unbase64mem(json_variant_string(e
), (size_t) -1, &b
, &l
);
977 return json_log(variant
, flags
, r
, "Failed to decode FIDO2 credential ID: %m");
979 h
->fido2_hmac_credential
= array
;
981 h
->fido2_hmac_credential
[h
->n_fido2_hmac_credential
++] = (Fido2HmacCredential
) {
990 static int dispatch_fido2_hmac_salt_value(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
991 Fido2HmacSalt
*k
= userdata
;
996 if (json_variant_is_null(variant
)) {
997 k
->salt
= erase_and_free(k
->salt
);
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
));
1005 r
= unbase64mem(json_variant_string(variant
), (size_t) -1, &b
, &l
);
1007 return json_log(variant
, flags
, r
, "Failed to decode FIDO2 salt: %m");
1009 erase_and_free(k
->salt
);
1016 static int dispatch_fido2_hmac_salt(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1017 UserRecord
*h
= userdata
;
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
));
1024 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
1025 Fido2HmacSalt
*array
, *k
;
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
},
1034 if (!json_variant_is_object(e
))
1035 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
1037 array
= reallocarray(h
->fido2_hmac_salt
, h
->n_fido2_hmac_salt
+ 1, sizeof(Fido2HmacSalt
));
1041 h
->fido2_hmac_salt
= array
;
1042 k
= h
->fido2_hmac_salt
+ h
->n_fido2_hmac_salt
;
1043 *k
= (Fido2HmacSalt
) {};
1045 r
= json_dispatch(e
, fido2_hmac_salt_dispatch_table
, NULL
, flags
, k
);
1047 fido2_hmac_salt_done(k
);
1051 h
->n_fido2_hmac_salt
++;
1057 static int dispatch_recovery_key(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1058 UserRecord
*h
= userdata
;
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
));
1065 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
1066 RecoveryKey
*array
, *k
;
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
},
1074 if (!json_variant_is_object(e
))
1075 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
1077 array
= reallocarray(h
->recovery_key
, h
->n_recovery_key
+ 1, sizeof(RecoveryKey
));
1081 h
->recovery_key
= array
;
1082 k
= h
->recovery_key
+ h
->n_recovery_key
;
1083 *k
= (RecoveryKey
) {};
1085 r
= json_dispatch(e
, recovery_key_dispatch_table
, NULL
, flags
, k
);
1087 recovery_key_done(k
);
1091 h
->n_recovery_key
++;
1097 static int dispatch_privileged(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
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 },
1109 return json_dispatch(variant
, privileged_dispatch_table
, NULL
, flags
, userdata
);
1112 static int dispatch_binding(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
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 },
1130 char smid
[SD_ID128_STRING_MAX
];
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
));
1141 r
= sd_id128_get_machine(&mid
);
1143 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
1145 m
= json_variant_by_key(variant
, sd_id128_to_string(mid
, smid
));
1149 return json_dispatch(m
, binding_dispatch_table
, NULL
, flags
, userdata
);
1152 int per_machine_id_match(JsonVariant
*ids
, JsonDispatchFlags flags
) {
1156 r
= sd_id128_get_machine(&mid
);
1158 return json_log(ids
, flags
, r
, "Failed to acquire machine ID: %m");
1160 if (json_variant_is_string(ids
)) {
1163 r
= sd_id128_from_string(json_variant_string(ids
), &k
);
1165 json_log(ids
, flags
, r
, "%s is not a valid machine ID, ignoring: %m", json_variant_string(ids
));
1169 return sd_id128_equal(mid
, k
);
1172 if (json_variant_is_array(ids
)) {
1175 JSON_VARIANT_ARRAY_FOREACH(e
, ids
) {
1178 if (!json_variant_is_string(e
)) {
1179 json_log(e
, flags
, 0, "Machine ID is not a string, ignoring: %m");
1183 r
= sd_id128_from_string(json_variant_string(e
), &k
);
1185 json_log(e
, flags
, r
, "%s is not a valid machine ID, ignoring: %m", json_variant_string(e
));
1189 if (sd_id128_equal(mid
, k
))
1196 json_log(ids
, flags
, 0, "Machine ID is not a string or array of strings, ignoring: %m");
1200 int per_machine_hostname_match(JsonVariant
*hns
, JsonDispatchFlags flags
) {
1201 _cleanup_free_
char *hn
= NULL
;
1204 r
= gethostname_strict(&hn
);
1206 json_log(hns
, flags
, r
, "No hostname set, not matching perMachine hostname record: %m");
1210 return json_log(hns
, flags
, r
, "Failed to acquire hostname: %m");
1212 if (json_variant_is_string(hns
))
1213 return streq(json_variant_string(hns
), hn
);
1215 if (json_variant_is_array(hns
)) {
1218 JSON_VARIANT_ARRAY_FOREACH(e
, hns
) {
1220 if (!json_variant_is_string(e
)) {
1221 json_log(e
, flags
, 0, "Hostname is not a string, ignoring: %m");
1225 if (streq(json_variant_string(hns
), hn
))
1232 json_log(hns
, flags
, 0, "Hostname is not a string or array of strings, ignoring: %m");
1236 static int dispatch_per_machine(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
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 },
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
));
1312 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
1313 bool matching
= false;
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
));
1319 m
= json_variant_by_key(e
, "matchMachineId");
1321 r
= per_machine_id_match(m
, flags
);
1329 m
= json_variant_by_key(e
, "matchHostname");
1331 r
= per_machine_hostname_match(m
, flags
);
1342 r
= json_dispatch(e
, per_machine_dispatch_table
, NULL
, flags
, userdata
);
1350 static int dispatch_status(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
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 },
1371 char smid
[SD_ID128_STRING_MAX
];
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
));
1382 r
= sd_id128_get_machine(&mid
);
1384 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
1386 m
= json_variant_by_key(variant
, sd_id128_to_string(mid
, smid
));
1390 return json_dispatch(m
, status_dispatch_table
, NULL
, flags
, userdata
);
1393 int user_record_build_image_path(UserStorage storage
, const char *user_name_and_realm
, char **ret
) {
1397 assert(storage
>= 0);
1398 assert(user_name_and_realm
);
1401 if (storage
== USER_LUKS
)
1403 else if (IN_SET(storage
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
))
1404 suffix
= ".homedir";
1410 z
= strjoin("/home/", user_name_and_realm
, suffix
);
1418 static int user_record_augment(UserRecord
*h
, JsonDispatchFlags json_flags
) {
1423 if (!FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
))
1426 assert(h
->user_name
);
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
);
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
)
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
);
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
);
1447 return json_log(h
->json
, json_flags
, r
, "Failed to determine default image path: %m");
1453 int user_group_record_mangle(
1455 UserRecordLoadFlags load_flags
,
1456 JsonVariant
**ret_variant
,
1457 UserRecordMask
*ret_mask
) {
1459 static const struct {
1460 UserRecordMask mask
;
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" },
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;
1478 assert((load_flags
& _USER_RECORD_MASK_MAX
) == 0); /* detect mistakes when accidentally passing
1479 * UserRecordMask bit masks as UserRecordLoadFlags
1483 assert(ret_variant
);
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. */
1489 if (!json_variant_is_object(v
))
1490 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is not a JSON object, refusing.");
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.");
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.");
1498 /* Check if we have the special sections and if they match our flags set */
1499 for (i
= 0; i
< ELEMENTSOF(mask_field
); i
++) {
1502 if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags
), mask_field
[i
].mask
)) {
1504 w
= json_variant_ref(v
);
1506 r
= json_variant_filter(&w
, STRV_MAKE(mask_field
[i
].name
));
1508 return json_log(w
, json_flags
, r
, "Failed to remove field from variant: %m");
1513 e
= json_variant_by_key_full(v
, mask_field
[i
].name
, &k
);
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
);
1518 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1519 array
[n_retain
++] = k
;
1520 array
[n_retain
++] = e
;
1523 m
|= mask_field
[i
].mask
;
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
);
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. */
1534 w
= json_variant_unref(w
);
1535 r
= json_variant_new_object(&w
, array
, n_retain
);
1537 return json_log(v
, json_flags
, r
, "Failed to allocate new object: %m");
1539 /* And now check if there's anything else in the record */
1540 for (i
= 0; i
< json_variant_elements(v
); i
+= 2) {
1542 bool special
= false;
1545 assert_se(f
= json_variant_string(json_variant_by_index(v
, i
)));
1547 for (j
= 0; j
< ELEMENTSOF(mask_field
); j
++)
1548 if (streq(f
, mask_field
[j
].name
)) { /* already covered in the loop above */
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
);
1557 m
|= USER_RECORD_REGULAR
;
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.");
1567 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is empty.");
1570 *ret_variant
= TAKE_PTR(w
);
1572 *ret_variant
= json_variant_ref(v
);
1578 int user_record_load(UserRecord
*h
, JsonVariant
*v
, UserRecordLoadFlags load_flags
) {
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 },
1651 { "secret", JSON_VARIANT_OBJECT
, dispatch_secret
, 0, 0 },
1652 { "privileged", JSON_VARIANT_OBJECT
, dispatch_privileged
, 0, 0 },
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 },
1659 /* Ignore 'signature', we check it with explicit accessors instead */
1660 { "signature", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1664 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1670 /* Note that this call will leave a half-initialized record around on failure! */
1672 r
= user_group_record_mangle(v
, load_flags
, &h
->json
, &h
->mask
);
1676 r
= json_dispatch(h
->json
, user_dispatch_table
, NULL
, json_flags
, h
);
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. */
1683 r
= dispatch_per_machine("perMachine", json_variant_by_key(h
->json
, "perMachine"), json_flags
, h
);
1687 r
= dispatch_binding("binding", json_variant_by_key(h
->json
, "binding"), json_flags
, h
);
1691 r
= dispatch_status("status", json_variant_by_key(h
->json
, "status"), json_flags
, h
);
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.");
1698 r
= user_record_augment(h
, json_flags
);
1705 int user_record_build(UserRecord
**ret
, ...) {
1706 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1707 _cleanup_(user_record_unrefp
) UserRecord
*u
= NULL
;
1714 r
= json_buildv(&v
, ap
);
1720 u
= user_record_new();
1724 r
= user_record_load(u
, v
, USER_RECORD_LOAD_FULL
);
1732 const char *user_record_user_name_and_realm(UserRecord
*h
) {
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
;
1739 /* If it's not defined then we cannot have a realm */
1741 return h
->user_name
;
1744 UserStorage
user_record_storage(UserRecord
*h
) {
1747 if (h
->storage
>= 0)
1750 return USER_CLASSIC
;
1753 const char *user_record_file_system_type(UserRecord
*h
) {
1756 return h
->file_system_type
?: "btrfs";
1759 const char *user_record_skeleton_directory(UserRecord
*h
) {
1762 return h
->skeleton_directory
?: "/etc/skel";
1765 mode_t
user_record_access_mode(UserRecord
*h
) {
1768 return h
->access_mode
!= (mode_t
) -1 ? h
->access_mode
: 0700;
1771 const char* user_record_home_directory(UserRecord
*h
) {
1774 if (h
->home_directory
)
1775 return h
->home_directory
;
1776 if (h
->home_directory_auto
)
1777 return h
->home_directory_auto
;
1779 /* The root user is special, hence be special about it */
1780 if (streq_ptr(h
->user_name
, "root"))
1786 const char *user_record_image_path(UserRecord
*h
) {
1790 return h
->image_path
;
1791 if (h
->image_path_auto
)
1792 return h
->image_path_auto
;
1794 return IN_SET(user_record_storage(h
), USER_CLASSIC
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
) ? user_record_home_directory(h
) : NULL
;
1797 const char *user_record_cifs_user_name(UserRecord
*h
) {
1800 return h
->cifs_user_name
?: h
->user_name
;
1803 unsigned long user_record_mount_flags(UserRecord
*h
) {
1806 return (h
->nosuid
? MS_NOSUID
: 0) |
1807 (h
->noexec
? MS_NOEXEC
: 0) |
1808 (h
->nodev
? MS_NODEV
: 0);
1811 const char *user_record_shell(UserRecord
*h
) {
1817 if (streq_ptr(h
->user_name
, "root"))
1820 if (user_record_disposition(h
) == USER_REGULAR
)
1826 const char *user_record_real_name(UserRecord
*h
) {
1829 return h
->real_name
?: h
->user_name
;
1832 bool user_record_luks_discard(UserRecord
*h
) {
1837 if (h
->luks_discard
>= 0)
1838 return h
->luks_discard
;
1840 ip
= user_record_image_path(h
);
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. */
1850 return path_startswith(ip
, "/dev/");
1853 bool user_record_luks_offline_discard(UserRecord
*h
) {
1858 if (h
->luks_offline_discard
>= 0)
1859 return h
->luks_offline_discard
;
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. */
1864 ip
= user_record_image_path(h
);
1868 if (path_startswith(ip
, "/dev/"))
1869 return user_record_luks_discard(h
);
1874 const char *user_record_luks_cipher(UserRecord
*h
) {
1877 return h
->luks_cipher
?: "aes";
1880 const char *user_record_luks_cipher_mode(UserRecord
*h
) {
1883 return h
->luks_cipher_mode
?: "xts-plain64";
1886 uint64_t user_record_luks_volume_key_size(UserRecord
*h
) {
1889 /* We return a value here that can be cast without loss into size_t which is what libcrypsetup expects */
1891 if (h
->luks_volume_key_size
== UINT64_MAX
)
1894 return MIN(h
->luks_volume_key_size
, SIZE_MAX
);
1897 const char* user_record_luks_pbkdf_type(UserRecord
*h
) {
1900 return h
->luks_pbkdf_type
?: "argon2i";
1903 uint64_t user_record_luks_pbkdf_time_cost_usec(UserRecord
*h
) {
1906 /* Returns a value with ms granularity, since that's what libcryptsetup expects */
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 */
1911 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_time_cost_usec
, USEC_PER_MSEC
), UINT32_MAX
) * USEC_PER_MSEC
;
1914 uint64_t user_record_luks_pbkdf_memory_cost(UserRecord
*h
) {
1917 /* Returns a value with kb granularity, since that's what libcryptsetup expects */
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 */
1922 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_memory_cost
, 1024), UINT32_MAX
) * 1024;
1925 uint64_t user_record_luks_pbkdf_parallel_threads(UserRecord
*h
) {
1928 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1929 return 1; /* We default to 1, since this should work on smaller systems too */
1931 return MIN(h
->luks_pbkdf_parallel_threads
, UINT32_MAX
);
1934 const char *user_record_luks_pbkdf_hash_algorithm(UserRecord
*h
) {
1937 return h
->luks_pbkdf_hash_algorithm
?: "sha512";
1940 gid_t
user_record_gid(UserRecord
*h
) {
1943 if (gid_is_valid(h
->gid
))
1946 return (gid_t
) h
->uid
;
1949 UserDisposition
user_record_disposition(UserRecord
*h
) {
1952 if (h
->disposition
>= 0)
1953 return h
->disposition
;
1955 /* If not declared, derive from UID */
1957 if (!uid_is_valid(h
->uid
))
1958 return _USER_DISPOSITION_INVALID
;
1960 if (h
->uid
== 0 || h
->uid
== UID_NOBODY
)
1961 return USER_INTRINSIC
;
1963 if (uid_is_system(h
->uid
))
1966 if (uid_is_dynamic(h
->uid
))
1967 return USER_DYNAMIC
;
1969 if (uid_is_container(h
->uid
))
1970 return USER_CONTAINER
;
1972 if (h
->uid
> INT32_MAX
)
1973 return USER_RESERVED
;
1975 return USER_REGULAR
;
1978 int user_record_removable(UserRecord
*h
) {
1979 UserStorage storage
;
1982 if (h
->removable
>= 0)
1983 return h
->removable
;
1985 /* Refuse to decide for classic records */
1986 storage
= user_record_storage(h
);
1987 if (h
->storage
< 0 || h
->storage
== USER_CLASSIC
)
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/");
1994 uint64_t user_record_ratelimit_interval_usec(UserRecord
*h
) {
1997 if (h
->ratelimit_interval_usec
== UINT64_MAX
)
1998 return DEFAULT_RATELIMIT_INTERVAL_USEC
;
2000 return h
->ratelimit_interval_usec
;
2003 uint64_t user_record_ratelimit_burst(UserRecord
*h
) {
2006 if (h
->ratelimit_burst
== UINT64_MAX
)
2007 return DEFAULT_RATELIMIT_BURST
;
2009 return h
->ratelimit_burst
;
2012 bool user_record_can_authenticate(UserRecord
*h
) {
2015 /* Returns true if there's some form of property configured that the user can authenticate against */
2017 if (h
->n_pkcs11_encrypted_key
> 0)
2020 if (h
->n_fido2_hmac_salt
> 0)
2023 return !strv_isempty(h
->hashed_password
);
2026 uint64_t user_record_ratelimit_next_try(UserRecord
*h
) {
2029 /* Calculates when the it's possible to login next. Returns:
2031 * UINT64_MAX → Nothing known
2033 * Any other → Next time in CLOCK_REALTIME in usec (which could be in the past)
2036 if (h
->ratelimit_begin_usec
== UINT64_MAX
||
2037 h
->ratelimit_count
== UINT64_MAX
)
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. */
2045 if (h
->ratelimit_count
< user_record_ratelimit_burst(h
))
2048 return usec_add(h
->ratelimit_begin_usec
, user_record_ratelimit_interval_usec(h
));
2051 bool user_record_equal(UserRecord
*a
, UserRecord
*b
) {
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. */
2058 return json_variant_equal(a
->json
, b
->json
);
2061 bool user_record_compatible(UserRecord
*a
, UserRecord
*b
) {
2065 /* If either lacks a the regular section, we can't really decide, let's hence say they are
2067 if (!(a
->mask
& b
->mask
& USER_RECORD_REGULAR
))
2070 return streq_ptr(a
->user_name
, b
->user_name
) &&
2071 streq_ptr(a
->realm
, b
->realm
);
2074 int user_record_compare_last_change(UserRecord
*a
, UserRecord
*b
) {
2078 if (a
->last_change_usec
== b
->last_change_usec
)
2081 /* Always consider a record with a timestamp newer than one without */
2082 if (a
->last_change_usec
== UINT64_MAX
)
2084 if (b
->last_change_usec
== UINT64_MAX
)
2087 return CMP(a
->last_change_usec
, b
->last_change_usec
);
2090 int user_record_clone(UserRecord
*h
, UserRecordLoadFlags flags
, UserRecord
**ret
) {
2091 _cleanup_(user_record_unrefp
) UserRecord
*c
= NULL
;
2097 c
= user_record_new();
2101 r
= user_record_load(c
, h
->json
, flags
);
2109 int user_record_masked_equal(UserRecord
*a
, UserRecord
*b
, UserRecordMask mask
) {
2110 _cleanup_(user_record_unrefp
) UserRecord
*x
= NULL
, *y
= NULL
;
2116 /* Compares the two records, but ignores anything not listed in the specified mask */
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
);
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
);
2134 return user_record_equal(a
, b
);
2137 int user_record_test_blocked(UserRecord
*h
) {
2140 /* Checks whether access to the specified user shall be allowed at the moment. Returns:
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
2154 n
= now(CLOCK_REALTIME
);
2156 if (h
->not_before_usec
!= UINT64_MAX
&& n
< h
->not_before_usec
)
2158 if (h
->not_after_usec
!= UINT64_MAX
&& n
> h
->not_after_usec
)
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 */
2168 int user_record_test_password_change_required(UserRecord
*h
) {
2169 bool change_permitted
;
2174 /* Checks whether the user must change the password when logging in
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
2186 /* If a password change request has been set explicitly, it overrides everything */
2187 if (h
->password_change_now
> 0)
2188 return -EKEYREVOKED
;
2190 n
= now(CLOCK_REALTIME
);
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
))
2200 /* Then, let's check if password changing is currently allowed at all */
2201 if (h
->password_change_min_usec
!= UINT64_MAX
) {
2203 /* Expiry configured but no password change timestamp known? */
2204 if (h
->last_password_change_usec
== UINT64_MAX
)
2207 if (h
->password_change_min_usec
>= UINT64_MAX
- h
->last_password_change_usec
)
2208 change_permitted
= false;
2210 change_permitted
= n
>= h
->last_password_change_usec
+ h
->password_change_min_usec
;
2213 change_permitted
= true;
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
)) {
2219 uint64_t change_before
;
2221 /* Expiry configured but no password change timestamp known? */
2222 if (h
->last_password_change_usec
== UINT64_MAX
)
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
) {
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
;
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
;
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
;
2248 /* No password changing necessary */
2249 return change_permitted
? 0 : -EROFS
;
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",
2261 DEFINE_STRING_TABLE_LOOKUP(user_storage
, UserStorage
);
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",
2272 DEFINE_STRING_TABLE_LOOKUP(user_disposition
, UserDisposition
);