1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "cgroup-util.h"
6 #include "chase-symlinks.h"
7 #include "dns-domain.h"
12 #include "hexdecoct.h"
13 #include "hostname-util.h"
14 #include "memory-util.h"
15 #include "path-util.h"
16 #include "pkcs11-util.h"
17 #include "rlimit-util.h"
18 #include "stat-util.h"
19 #include "string-table.h"
21 #include "user-record.h"
22 #include "user-util.h"
24 #define DEFAULT_RATELIMIT_BURST 30
25 #define DEFAULT_RATELIMIT_INTERVAL_USEC (1*USEC_PER_MINUTE)
27 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
28 static int parse_alloc_uid(const char *path
, const char *name
, const char *t
, uid_t
*ret_uid
) {
32 r
= parse_uid(t
, &uid
);
34 return log_debug_errno(r
, "%s: failed to parse %s %s, ignoring: %m", path
, name
, t
);
43 int read_login_defs(UGIDAllocationRange
*ret_defs
, const char *path
, const char *root
) {
44 UGIDAllocationRange defs
= {
45 .system_alloc_uid_min
= SYSTEM_ALLOC_UID_MIN
,
46 .system_uid_max
= SYSTEM_UID_MAX
,
47 .system_alloc_gid_min
= SYSTEM_ALLOC_GID_MIN
,
48 .system_gid_max
= SYSTEM_GID_MAX
,
51 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
52 _cleanup_fclose_
FILE *f
= NULL
;
56 path
= "/etc/login.defs";
58 r
= chase_symlinks_and_fopen_unlocked(path
, root
, CHASE_PREFIX_ROOT
, "re", &f
, NULL
);
62 return log_debug_errno(r
, "Failed to open %s: %m", path
);
65 _cleanup_free_
char *line
= NULL
;
68 r
= read_line(f
, LINE_MAX
, &line
);
70 return log_debug_errno(r
, "Failed to read %s: %m", path
);
74 if ((t
= first_word(line
, "SYS_UID_MIN")))
75 (void) parse_alloc_uid(path
, "SYS_UID_MIN", t
, &defs
.system_alloc_uid_min
);
76 else if ((t
= first_word(line
, "SYS_UID_MAX")))
77 (void) parse_alloc_uid(path
, "SYS_UID_MAX", t
, &defs
.system_uid_max
);
78 else if ((t
= first_word(line
, "SYS_GID_MIN")))
79 (void) parse_alloc_uid(path
, "SYS_GID_MIN", t
, &defs
.system_alloc_gid_min
);
80 else if ((t
= first_word(line
, "SYS_GID_MAX")))
81 (void) parse_alloc_uid(path
, "SYS_GID_MAX", t
, &defs
.system_gid_max
);
85 if (defs
.system_alloc_uid_min
> defs
.system_uid_max
) {
86 log_debug("%s: SYS_UID_MIN > SYS_UID_MAX, resetting.", path
);
87 defs
.system_alloc_uid_min
= MIN(defs
.system_uid_max
- 1, (uid_t
) SYSTEM_ALLOC_UID_MIN
);
88 /* Look at sys_uid_max to make sure sys_uid_min..sys_uid_max remains a valid range. */
90 if (defs
.system_alloc_gid_min
> defs
.system_gid_max
) {
91 log_debug("%s: SYS_GID_MIN > SYS_GID_MAX, resetting.", path
);
92 defs
.system_alloc_gid_min
= MIN(defs
.system_gid_max
- 1, (gid_t
) SYSTEM_ALLOC_GID_MIN
);
93 /* Look at sys_gid_max to make sure sys_gid_min..sys_gid_max remains a valid range. */
101 const UGIDAllocationRange
*acquire_ugid_allocation_range(void) {
102 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
103 static thread_local UGIDAllocationRange defs
= {
105 static const UGIDAllocationRange defs
= {
107 .system_alloc_uid_min
= SYSTEM_ALLOC_UID_MIN
,
108 .system_uid_max
= SYSTEM_UID_MAX
,
109 .system_alloc_gid_min
= SYSTEM_ALLOC_GID_MIN
,
110 .system_gid_max
= SYSTEM_GID_MAX
,
113 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
114 /* This function will ignore failure to read the file, so it should only be called from places where
115 * we don't crucially depend on the answer. In other words, it's appropriate for journald, but
116 * probably not for sysusers. */
118 static thread_local
bool initialized
= false;
121 (void) read_login_defs(&defs
, NULL
, NULL
);
129 bool uid_is_system(uid_t uid
) {
130 const UGIDAllocationRange
*defs
;
131 assert_se(defs
= acquire_ugid_allocation_range());
133 return uid
<= defs
->system_uid_max
;
136 bool gid_is_system(gid_t gid
) {
137 const UGIDAllocationRange
*defs
;
138 assert_se(defs
= acquire_ugid_allocation_range());
140 return gid
<= defs
->system_gid_max
;
143 UserRecord
* user_record_new(void) {
146 h
= new(UserRecord
, 1);
152 .disposition
= _USER_DISPOSITION_INVALID
,
153 .last_change_usec
= UINT64_MAX
,
154 .last_password_change_usec
= UINT64_MAX
,
155 .umask
= MODE_INVALID
,
156 .nice_level
= INT_MAX
,
157 .not_before_usec
= UINT64_MAX
,
158 .not_after_usec
= UINT64_MAX
,
160 .storage
= _USER_STORAGE_INVALID
,
161 .access_mode
= MODE_INVALID
,
162 .disk_size
= UINT64_MAX
,
163 .disk_size_relative
= UINT64_MAX
,
164 .tasks_max
= UINT64_MAX
,
165 .memory_high
= UINT64_MAX
,
166 .memory_max
= UINT64_MAX
,
167 .cpu_weight
= UINT64_MAX
,
168 .io_weight
= UINT64_MAX
,
174 .luks_offline_discard
= -1,
175 .luks_volume_key_size
= UINT64_MAX
,
176 .luks_pbkdf_time_cost_usec
= UINT64_MAX
,
177 .luks_pbkdf_memory_cost
= UINT64_MAX
,
178 .luks_pbkdf_parallel_threads
= UINT64_MAX
,
179 .disk_usage
= UINT64_MAX
,
180 .disk_free
= UINT64_MAX
,
181 .disk_ceiling
= UINT64_MAX
,
182 .disk_floor
= UINT64_MAX
,
183 .signed_locally
= -1,
184 .good_authentication_counter
= UINT64_MAX
,
185 .bad_authentication_counter
= UINT64_MAX
,
186 .last_good_authentication_usec
= UINT64_MAX
,
187 .last_bad_authentication_usec
= UINT64_MAX
,
188 .ratelimit_begin_usec
= UINT64_MAX
,
189 .ratelimit_count
= UINT64_MAX
,
190 .ratelimit_interval_usec
= UINT64_MAX
,
191 .ratelimit_burst
= UINT64_MAX
,
193 .enforce_password_policy
= -1,
195 .stop_delay_usec
= UINT64_MAX
,
196 .kill_processes
= -1,
197 .password_change_min_usec
= UINT64_MAX
,
198 .password_change_max_usec
= UINT64_MAX
,
199 .password_change_warn_usec
= UINT64_MAX
,
200 .password_change_inactive_usec
= UINT64_MAX
,
201 .password_change_now
= -1,
202 .pkcs11_protected_authentication_path_permitted
= -1,
203 .fido2_user_presence_permitted
= -1,
204 .fido2_user_verification_permitted
= -1,
210 static void pkcs11_encrypted_key_done(Pkcs11EncryptedKey
*k
) {
215 erase_and_free(k
->data
);
216 erase_and_free(k
->hashed_password
);
219 static void fido2_hmac_credential_done(Fido2HmacCredential
*c
) {
226 static void fido2_hmac_salt_done(Fido2HmacSalt
*s
) {
230 fido2_hmac_credential_done(&s
->credential
);
231 erase_and_free(s
->salt
);
232 erase_and_free(s
->hashed_password
);
235 static void recovery_key_done(RecoveryKey
*k
) {
240 erase_and_free(k
->hashed_password
);
243 static UserRecord
* user_record_free(UserRecord
*h
) {
249 free(h
->user_name_and_realm_auto
);
251 free(h
->email_address
);
252 erase_and_free(h
->password_hint
);
258 strv_free(h
->environment
);
260 free(h
->preferred_language
);
261 rlimit_free_all(h
->rlimits
);
263 free(h
->skeleton_directory
);
265 strv_free_erase(h
->hashed_password
);
266 strv_free_erase(h
->ssh_authorized_keys
);
267 strv_free_erase(h
->password
);
268 strv_free_erase(h
->token_pin
);
270 free(h
->cifs_service
);
271 free(h
->cifs_user_name
);
272 free(h
->cifs_domain
);
275 free(h
->image_path_auto
);
276 free(h
->home_directory
);
277 free(h
->home_directory_auto
);
279 strv_free(h
->member_of
);
281 free(h
->file_system_type
);
282 free(h
->luks_cipher
);
283 free(h
->luks_cipher_mode
);
284 free(h
->luks_pbkdf_hash_algorithm
);
285 free(h
->luks_pbkdf_type
);
290 strv_free(h
->pkcs11_token_uri
);
291 for (size_t i
= 0; i
< h
->n_pkcs11_encrypted_key
; i
++)
292 pkcs11_encrypted_key_done(h
->pkcs11_encrypted_key
+ i
);
293 free(h
->pkcs11_encrypted_key
);
295 for (size_t i
= 0; i
< h
->n_fido2_hmac_credential
; i
++)
296 fido2_hmac_credential_done(h
->fido2_hmac_credential
+ i
);
297 for (size_t i
= 0; i
< h
->n_fido2_hmac_salt
; i
++)
298 fido2_hmac_salt_done(h
->fido2_hmac_salt
+ i
);
300 strv_free(h
->recovery_key_type
);
301 for (size_t i
= 0; i
< h
->n_recovery_key
; i
++)
302 recovery_key_done(h
->recovery_key
+ i
);
304 json_variant_unref(h
->json
);
309 DEFINE_TRIVIAL_REF_UNREF_FUNC(UserRecord
, user_record
, user_record_free
);
311 int json_dispatch_realm(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
316 if (json_variant_is_null(variant
)) {
321 if (!json_variant_is_string(variant
))
322 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
324 n
= json_variant_string(variant
);
325 r
= dns_name_is_valid(n
);
327 return json_log(variant
, flags
, r
, "Failed to check if JSON field '%s' is a valid DNS domain.", strna(name
));
329 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid DNS domain.", strna(name
));
331 r
= free_and_strdup(s
, n
);
333 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
338 int json_dispatch_gecos(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
342 if (json_variant_is_null(variant
)) {
347 if (!json_variant_is_string(variant
))
348 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
350 n
= json_variant_string(variant
);
351 if (valid_gecos(n
)) {
352 if (free_and_strdup(s
, n
) < 0)
353 return json_log_oom(variant
, flags
);
355 _cleanup_free_
char *m
= NULL
;
357 json_log(variant
, flags
|JSON_DEBUG
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid GECOS compatible string, mangling.", strna(name
));
361 return json_log_oom(variant
, flags
);
363 free_and_replace(*s
, m
);
369 static int json_dispatch_nice(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
373 if (json_variant_is_null(variant
)) {
378 if (!json_variant_is_integer(variant
))
379 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
381 m
= json_variant_integer(variant
);
382 if (m
< PRIO_MIN
|| m
>= PRIO_MAX
)
383 return json_log(variant
, flags
, SYNTHETIC_ERRNO(ERANGE
), "JSON field '%s' is not a valid nice level.", strna(name
));
389 static int json_dispatch_rlimit_value(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
390 rlim_t
*ret
= userdata
;
392 if (json_variant_is_null(variant
))
393 *ret
= RLIM_INFINITY
;
394 else if (json_variant_is_unsigned(variant
)) {
397 w
= json_variant_unsigned(variant
);
398 if (w
== RLIM_INFINITY
|| (uintmax_t) w
!= json_variant_unsigned(variant
))
399 return json_log(variant
, flags
, SYNTHETIC_ERRNO(ERANGE
), "Resource limit value '%s' is out of range.", name
);
403 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit value '%s' is not an unsigned integer.", name
);
408 static int json_dispatch_rlimits(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
409 struct rlimit
** limits
= userdata
;
416 if (json_variant_is_null(variant
)) {
417 rlimit_free_all(limits
);
421 if (!json_variant_is_object(variant
))
422 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
424 JSON_VARIANT_OBJECT_FOREACH(key
, value
, variant
) {
425 JsonVariant
*jcur
, *jmax
;
430 p
= startswith(key
, "RLIMIT_");
432 l
= -SYNTHETIC_ERRNO(EINVAL
);
434 l
= rlimit_from_string(p
);
436 return json_log(variant
, flags
, l
, "Resource limit '%s' not known.", key
);
438 if (!json_variant_is_object(value
))
439 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' has invalid value.", key
);
441 if (json_variant_elements(value
) != 4)
442 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' value is does not have two fields as expected.", key
);
444 jcur
= json_variant_by_key(value
, "cur");
446 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' lacks 'cur' field.", key
);
447 r
= json_dispatch_rlimit_value("cur", jcur
, flags
, &rl
.rlim_cur
);
451 jmax
= json_variant_by_key(value
, "max");
453 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' lacks 'max' field.", key
);
454 r
= json_dispatch_rlimit_value("max", jmax
, flags
, &rl
.rlim_max
);
461 limits
[l
] = newdup(struct rlimit
, &rl
, 1);
470 static int json_dispatch_filename_or_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
477 if (json_variant_is_null(variant
)) {
482 if (!json_variant_is_string(variant
))
483 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
485 n
= json_variant_string(variant
);
486 if (!filename_is_valid(n
) && !path_is_normalized(n
))
487 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid file name or normalized path.", strna(name
));
489 r
= free_and_strdup(s
, n
);
491 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
496 static int json_dispatch_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
501 if (json_variant_is_null(variant
)) {
506 if (!json_variant_is_string(variant
))
507 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
509 n
= json_variant_string(variant
);
510 if (!path_is_normalized(n
))
511 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a normalized file system path.", strna(name
));
512 if (!path_is_absolute(n
))
513 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an absolute file system path.", strna(name
));
515 r
= free_and_strdup(s
, n
);
517 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
522 static int json_dispatch_home_directory(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
527 if (json_variant_is_null(variant
)) {
532 if (!json_variant_is_string(variant
))
533 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
535 n
= json_variant_string(variant
);
537 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid home directory path.", strna(name
));
539 r
= free_and_strdup(s
, n
);
541 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
546 static int json_dispatch_image_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
551 if (json_variant_is_null(variant
)) {
556 if (!json_variant_is_string(variant
))
557 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
559 n
= json_variant_string(variant
);
560 if (empty_or_root(n
) || !path_is_valid(n
) || !path_is_absolute(n
))
561 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid image path.", strna(name
));
563 r
= free_and_strdup(s
, n
);
565 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
570 static int json_dispatch_umask(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
571 mode_t
*m
= userdata
;
574 if (json_variant_is_null(variant
)) {
579 if (!json_variant_is_unsigned(variant
))
580 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a number.", strna(name
));
582 k
= json_variant_unsigned(variant
);
584 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' outside of valid range 0…0777.", strna(name
));
590 static int json_dispatch_access_mode(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
591 mode_t
*m
= userdata
;
594 if (json_variant_is_null(variant
)) {
599 if (!json_variant_is_unsigned(variant
))
600 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a number.", strna(name
));
602 k
= json_variant_unsigned(variant
);
604 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' outside of valid range 0…07777.", strna(name
));
610 static int json_dispatch_environment(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
611 _cleanup_strv_free_
char **n
= NULL
;
612 char ***l
= userdata
;
615 if (json_variant_is_null(variant
)) {
620 if (!json_variant_is_array(variant
))
621 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array.", strna(name
));
623 for (size_t i
= 0; i
< json_variant_elements(variant
); i
++) {
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
));
636 r
= strv_env_replace_strdup(&n
, a
);
638 return json_log_oom(variant
, flags
);
641 return strv_free_and_replace(*l
, n
);
644 int json_dispatch_user_disposition(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
645 UserDisposition
*disposition
= userdata
, k
;
647 if (json_variant_is_null(variant
)) {
648 *disposition
= _USER_DISPOSITION_INVALID
;
652 if (!json_variant_is_string(variant
))
653 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
655 k
= user_disposition_from_string(json_variant_string(variant
));
657 return json_log(variant
, flags
, k
, "Disposition type '%s' not known.", json_variant_string(variant
));
663 static int json_dispatch_storage(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
664 UserStorage
*storage
= userdata
, k
;
666 if (json_variant_is_null(variant
)) {
667 *storage
= _USER_STORAGE_INVALID
;
671 if (!json_variant_is_string(variant
))
672 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
674 k
= user_storage_from_string(json_variant_string(variant
));
676 return json_log(variant
, flags
, k
, "Storage type '%s' not known.", json_variant_string(variant
));
682 static int json_dispatch_disk_size(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
683 uint64_t *size
= userdata
;
686 if (json_variant_is_null(variant
)) {
691 if (!json_variant_is_unsigned(variant
))
692 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an integer.", strna(name
));
694 k
= json_variant_unsigned(variant
);
695 if (k
< USER_DISK_SIZE_MIN
|| k
> USER_DISK_SIZE_MAX
)
696 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
);
702 static int json_dispatch_tasks_or_memory_max(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
703 uint64_t *limit
= userdata
;
706 if (json_variant_is_null(variant
)) {
711 if (!json_variant_is_unsigned(variant
))
712 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an integer.", strna(name
));
714 k
= json_variant_unsigned(variant
);
715 if (k
<= 0 || k
>= UINT64_MAX
)
716 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);
722 static int json_dispatch_weight(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
723 uint64_t *weight
= userdata
;
726 if (json_variant_is_null(variant
)) {
727 *weight
= UINT64_MAX
;
731 if (!json_variant_is_unsigned(variant
))
732 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an integer.", strna(name
));
734 k
= json_variant_unsigned(variant
);
735 if (k
<= CGROUP_WEIGHT_MIN
|| k
>= CGROUP_WEIGHT_MAX
)
736 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
);
742 int json_dispatch_user_group_list(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
743 _cleanup_strv_free_
char **l
= NULL
;
744 char ***list
= userdata
;
748 if (!json_variant_is_array(variant
))
749 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of strings.", strna(name
));
751 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
753 if (!json_variant_is_string(e
))
754 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
756 if (!valid_user_group_name(json_variant_string(e
), FLAGS_SET(flags
, JSON_RELAX
) ? VALID_USER_RELAX
: 0))
757 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a valid user/group name: %s", json_variant_string(e
));
759 r
= strv_extend(&l
, json_variant_string(e
));
761 return json_log(e
, flags
, r
, "Failed to append array element: %m");
764 r
= strv_extend_strv(list
, l
, true);
766 return json_log(variant
, flags
, r
, "Failed to merge user/group arrays: %m");
771 static int dispatch_secret(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
773 static const JsonDispatch secret_dispatch_table
[] = {
774 { "password", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, password
), 0 },
775 { "tokenPin", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, token_pin
), 0 },
776 { "pkcs11Pin", /* legacy alias */ _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, token_pin
), 0 },
777 { "pkcs11ProtectedAuthenticationPathPermitted", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, pkcs11_protected_authentication_path_permitted
), 0 },
778 { "fido2UserPresencePermitted", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, fido2_user_presence_permitted
), 0 },
779 { "fido2UserVerificationPermitted", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, fido2_user_verification_permitted
), 0 },
783 return json_dispatch(variant
, secret_dispatch_table
, NULL
, flags
, userdata
);
786 static int dispatch_pkcs11_uri(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
791 if (json_variant_is_null(variant
)) {
796 if (!json_variant_is_string(variant
))
797 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
799 n
= json_variant_string(variant
);
800 if (!pkcs11_uri_valid(n
))
801 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name
));
803 r
= free_and_strdup(s
, n
);
805 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
810 static int dispatch_pkcs11_uri_array(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
811 _cleanup_strv_free_
char **z
= NULL
;
812 char ***l
= userdata
;
816 if (json_variant_is_null(variant
)) {
821 if (json_variant_is_string(variant
)) {
824 n
= json_variant_string(variant
);
825 if (!pkcs11_uri_valid(n
))
826 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name
));
834 if (!json_variant_is_array(variant
))
835 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string or array of strings.", strna(name
));
837 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
840 if (!json_variant_is_string(e
))
841 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
843 n
= json_variant_string(e
);
844 if (!pkcs11_uri_valid(n
))
845 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
);
847 r
= strv_extend(&z
, n
);
853 strv_free_and_replace(*l
, z
);
857 static int dispatch_pkcs11_key_data(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
858 Pkcs11EncryptedKey
*k
= userdata
;
863 if (json_variant_is_null(variant
)) {
864 k
->data
= erase_and_free(k
->data
);
869 if (!json_variant_is_string(variant
))
870 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
872 r
= unbase64mem(json_variant_string(variant
), SIZE_MAX
, &b
, &l
);
874 return json_log(variant
, flags
, r
, "Failed to decode encrypted PKCS#11 key: %m");
876 erase_and_free(k
->data
);
883 static int dispatch_pkcs11_key(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
884 UserRecord
*h
= userdata
;
888 if (!json_variant_is_array(variant
))
889 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
891 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
892 Pkcs11EncryptedKey
*array
, *k
;
894 static const JsonDispatch pkcs11_key_dispatch_table
[] = {
895 { "uri", JSON_VARIANT_STRING
, dispatch_pkcs11_uri
, offsetof(Pkcs11EncryptedKey
, uri
), JSON_MANDATORY
},
896 { "data", JSON_VARIANT_STRING
, dispatch_pkcs11_key_data
, 0, JSON_MANDATORY
},
897 { "hashedPassword", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(Pkcs11EncryptedKey
, hashed_password
), JSON_MANDATORY
},
901 if (!json_variant_is_object(e
))
902 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
904 array
= reallocarray(h
->pkcs11_encrypted_key
, h
->n_pkcs11_encrypted_key
+ 1, sizeof(Pkcs11EncryptedKey
));
908 h
->pkcs11_encrypted_key
= array
;
909 k
= h
->pkcs11_encrypted_key
+ h
->n_pkcs11_encrypted_key
;
910 *k
= (Pkcs11EncryptedKey
) {};
912 r
= json_dispatch(e
, pkcs11_key_dispatch_table
, NULL
, flags
, k
);
914 pkcs11_encrypted_key_done(k
);
918 h
->n_pkcs11_encrypted_key
++;
924 static int dispatch_fido2_hmac_credential(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
925 Fido2HmacCredential
*k
= userdata
;
930 if (json_variant_is_null(variant
)) {
931 k
->id
= mfree(k
->id
);
936 if (!json_variant_is_string(variant
))
937 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
939 r
= unbase64mem(json_variant_string(variant
), SIZE_MAX
, &b
, &l
);
941 return json_log(variant
, flags
, r
, "Failed to decode FIDO2 credential ID: %m");
943 free_and_replace(k
->id
, b
);
949 static int dispatch_fido2_hmac_credential_array(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
950 UserRecord
*h
= userdata
;
954 if (!json_variant_is_array(variant
))
955 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of strings.", strna(name
));
957 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
958 Fido2HmacCredential
*array
;
962 if (!json_variant_is_string(e
))
963 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
965 array
= reallocarray(h
->fido2_hmac_credential
, h
->n_fido2_hmac_credential
+ 1, sizeof(Fido2HmacCredential
));
969 r
= unbase64mem(json_variant_string(e
), SIZE_MAX
, &b
, &l
);
971 return json_log(variant
, flags
, r
, "Failed to decode FIDO2 credential ID: %m");
973 h
->fido2_hmac_credential
= array
;
975 h
->fido2_hmac_credential
[h
->n_fido2_hmac_credential
++] = (Fido2HmacCredential
) {
984 static int dispatch_fido2_hmac_salt_value(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
985 Fido2HmacSalt
*k
= userdata
;
990 if (json_variant_is_null(variant
)) {
991 k
->salt
= erase_and_free(k
->salt
);
996 if (!json_variant_is_string(variant
))
997 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
999 r
= unbase64mem(json_variant_string(variant
), SIZE_MAX
, &b
, &l
);
1001 return json_log(variant
, flags
, r
, "Failed to decode FIDO2 salt: %m");
1003 erase_and_free(k
->salt
);
1010 static int dispatch_fido2_hmac_salt(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1011 UserRecord
*h
= userdata
;
1015 if (!json_variant_is_array(variant
))
1016 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
1018 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
1019 Fido2HmacSalt
*array
, *k
;
1021 static const JsonDispatch fido2_hmac_salt_dispatch_table
[] = {
1022 { "credential", JSON_VARIANT_STRING
, dispatch_fido2_hmac_credential
, offsetof(Fido2HmacSalt
, credential
), JSON_MANDATORY
},
1023 { "salt", JSON_VARIANT_STRING
, dispatch_fido2_hmac_salt_value
, 0, JSON_MANDATORY
},
1024 { "hashedPassword", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(Fido2HmacSalt
, hashed_password
), JSON_MANDATORY
},
1025 { "up", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(Fido2HmacSalt
, up
), 0 },
1026 { "uv", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(Fido2HmacSalt
, uv
), 0 },
1027 { "clientPin", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(Fido2HmacSalt
, client_pin
), 0 },
1031 if (!json_variant_is_object(e
))
1032 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
1034 array
= reallocarray(h
->fido2_hmac_salt
, h
->n_fido2_hmac_salt
+ 1, sizeof(Fido2HmacSalt
));
1038 h
->fido2_hmac_salt
= array
;
1039 k
= h
->fido2_hmac_salt
+ h
->n_fido2_hmac_salt
;
1040 *k
= (Fido2HmacSalt
) {
1046 r
= json_dispatch(e
, fido2_hmac_salt_dispatch_table
, NULL
, flags
, k
);
1048 fido2_hmac_salt_done(k
);
1052 h
->n_fido2_hmac_salt
++;
1058 static int dispatch_recovery_key(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1059 UserRecord
*h
= userdata
;
1063 if (!json_variant_is_array(variant
))
1064 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
1066 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
1067 RecoveryKey
*array
, *k
;
1069 static const JsonDispatch recovery_key_dispatch_table
[] = {
1070 { "type", JSON_VARIANT_STRING
, json_dispatch_string
, 0, JSON_MANDATORY
},
1071 { "hashedPassword", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(RecoveryKey
, hashed_password
), JSON_MANDATORY
},
1075 if (!json_variant_is_object(e
))
1076 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
1078 array
= reallocarray(h
->recovery_key
, h
->n_recovery_key
+ 1, sizeof(RecoveryKey
));
1082 h
->recovery_key
= array
;
1083 k
= h
->recovery_key
+ h
->n_recovery_key
;
1084 *k
= (RecoveryKey
) {};
1086 r
= json_dispatch(e
, recovery_key_dispatch_table
, NULL
, flags
, k
);
1088 recovery_key_done(k
);
1092 h
->n_recovery_key
++;
1098 static int dispatch_privileged(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1100 static const JsonDispatch privileged_dispatch_table
[] = {
1101 { "passwordHint", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, password_hint
), 0 },
1102 { "hashedPassword", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, hashed_password
), JSON_SAFE
},
1103 { "sshAuthorizedKeys", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, ssh_authorized_keys
), 0 },
1104 { "pkcs11EncryptedKey", JSON_VARIANT_ARRAY
, dispatch_pkcs11_key
, 0, 0 },
1105 { "fido2HmacSalt", JSON_VARIANT_ARRAY
, dispatch_fido2_hmac_salt
, 0, 0 },
1106 { "recoveryKey", JSON_VARIANT_ARRAY
, dispatch_recovery_key
, 0, 0 },
1110 return json_dispatch(variant
, privileged_dispatch_table
, NULL
, flags
, userdata
);
1113 static int dispatch_binding(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1115 static const JsonDispatch binding_dispatch_table
[] = {
1116 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_image_path
, offsetof(UserRecord
, image_path
), 0 },
1117 { "homeDirectory", JSON_VARIANT_STRING
, json_dispatch_home_directory
, offsetof(UserRecord
, home_directory
), 0 },
1118 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
1119 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
1120 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
1121 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
1122 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
1123 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
1124 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
1125 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
1126 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
1127 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
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
));
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 },
1378 if (!json_variant_is_object(variant
))
1379 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
1381 r
= sd_id128_get_machine(&mid
);
1383 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
1385 m
= json_variant_by_key(variant
, SD_ID128_TO_STRING(mid
));
1389 return json_dispatch(m
, status_dispatch_table
, NULL
, flags
, userdata
);
1392 int user_record_build_image_path(UserStorage storage
, const char *user_name_and_realm
, char **ret
) {
1396 assert(storage
>= 0);
1397 assert(user_name_and_realm
);
1400 if (storage
== USER_LUKS
)
1402 else if (IN_SET(storage
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
))
1403 suffix
= ".homedir";
1409 z
= strjoin("/home/", user_name_and_realm
, suffix
);
1417 static int user_record_augment(UserRecord
*h
, JsonDispatchFlags json_flags
) {
1422 if (!FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
))
1425 assert(h
->user_name
);
1427 if (!h
->user_name_and_realm_auto
&& h
->realm
) {
1428 h
->user_name_and_realm_auto
= strjoin(h
->user_name
, "@", h
->realm
);
1429 if (!h
->user_name_and_realm_auto
)
1430 return json_log_oom(h
->json
, json_flags
);
1433 /* Let's add in the following automatisms only for regular users, they don't make sense for any others */
1434 if (user_record_disposition(h
) != USER_REGULAR
)
1437 if (!h
->home_directory
&& !h
->home_directory_auto
) {
1438 h
->home_directory_auto
= path_join("/home/", h
->user_name
);
1439 if (!h
->home_directory_auto
)
1440 return json_log_oom(h
->json
, json_flags
);
1443 if (!h
->image_path
&& !h
->image_path_auto
) {
1444 r
= user_record_build_image_path(user_record_storage(h
), user_record_user_name_and_realm(h
), &h
->image_path_auto
);
1446 return json_log(h
->json
, json_flags
, r
, "Failed to determine default image path: %m");
1452 int user_group_record_mangle(
1454 UserRecordLoadFlags load_flags
,
1455 JsonVariant
**ret_variant
,
1456 UserRecordMask
*ret_mask
) {
1458 static const struct {
1459 UserRecordMask mask
;
1462 { USER_RECORD_PRIVILEGED
, "privileged" },
1463 { USER_RECORD_SECRET
, "secret" },
1464 { USER_RECORD_BINDING
, "binding" },
1465 { USER_RECORD_PER_MACHINE
, "perMachine" },
1466 { USER_RECORD_STATUS
, "status" },
1467 { USER_RECORD_SIGNATURE
, "signature" },
1470 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1471 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
;
1472 JsonVariant
*array
[ELEMENTSOF(mask_field
) * 2];
1473 size_t n_retain
= 0;
1474 UserRecordMask m
= 0;
1477 assert((load_flags
& _USER_RECORD_MASK_MAX
) == 0); /* detect mistakes when accidentally passing
1478 * UserRecordMask bit masks as UserRecordLoadFlags
1482 assert(ret_variant
);
1485 /* Note that this function is shared with the group record parser, hence we try to be generic in our
1486 * log message wording here, to cover both cases. */
1488 if (!json_variant_is_object(v
))
1489 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is not a JSON object, refusing.");
1491 if (USER_RECORD_ALLOW_MASK(load_flags
) == 0) /* allow nothing? */
1492 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "Nothing allowed in record, refusing.");
1494 if (USER_RECORD_STRIP_MASK(load_flags
) == _USER_RECORD_MASK_MAX
) /* strip everything? */
1495 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "Stripping everything from record, refusing.");
1497 /* Check if we have the special sections and if they match our flags set */
1498 for (size_t i
= 0; i
< ELEMENTSOF(mask_field
); i
++) {
1501 if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags
), mask_field
[i
].mask
)) {
1503 w
= json_variant_ref(v
);
1505 r
= json_variant_filter(&w
, STRV_MAKE(mask_field
[i
].name
));
1507 return json_log(w
, json_flags
, r
, "Failed to remove field from variant: %m");
1512 e
= json_variant_by_key_full(v
, mask_field
[i
].name
, &k
);
1514 if (!FLAGS_SET(USER_RECORD_ALLOW_MASK(load_flags
), mask_field
[i
].mask
))
1515 return json_log(e
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record contains '%s' field, which is not allowed.", mask_field
[i
].name
);
1517 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1518 array
[n_retain
++] = k
;
1519 array
[n_retain
++] = e
;
1522 m
|= mask_field
[i
].mask
;
1524 if (FLAGS_SET(USER_RECORD_REQUIRE_MASK(load_flags
), mask_field
[i
].mask
))
1525 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record lacks '%s' field, which is required.", mask_field
[i
].name
);
1529 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1530 /* If we are supposed to strip regular items, then let's instead just allocate a new object
1531 * with just the stuff we need. */
1533 w
= json_variant_unref(w
);
1534 r
= json_variant_new_object(&w
, array
, n_retain
);
1536 return json_log(v
, json_flags
, r
, "Failed to allocate new object: %m");
1538 /* And now check if there's anything else in the record */
1539 for (size_t i
= 0; i
< json_variant_elements(v
); i
+= 2) {
1541 bool special
= false;
1543 assert_se(f
= json_variant_string(json_variant_by_index(v
, i
)));
1545 for (size_t j
= 0; j
< ELEMENTSOF(mask_field
); j
++)
1546 if (streq(f
, mask_field
[j
].name
)) { /* already covered in the loop above */
1552 if ((load_flags
& (USER_RECORD_ALLOW_REGULAR
|USER_RECORD_REQUIRE_REGULAR
)) == 0)
1553 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record contains '%s' field, which is not allowed.", f
);
1555 m
|= USER_RECORD_REGULAR
;
1560 if (FLAGS_SET(load_flags
, USER_RECORD_REQUIRE_REGULAR
) && !FLAGS_SET(m
, USER_RECORD_REGULAR
))
1561 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record lacks basic identity fields, which are required.");
1563 if (!FLAGS_SET(load_flags
, USER_RECORD_EMPTY_OK
) && m
== 0)
1564 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is empty.");
1567 *ret_variant
= TAKE_PTR(w
);
1569 *ret_variant
= json_variant_ref(v
);
1575 int user_record_load(UserRecord
*h
, JsonVariant
*v
, UserRecordLoadFlags load_flags
) {
1577 static const JsonDispatch user_dispatch_table
[] = {
1578 { "userName", JSON_VARIANT_STRING
, json_dispatch_user_group_name
, offsetof(UserRecord
, user_name
), JSON_RELAX
},
1579 { "realm", JSON_VARIANT_STRING
, json_dispatch_realm
, offsetof(UserRecord
, realm
), 0 },
1580 { "realName", JSON_VARIANT_STRING
, json_dispatch_gecos
, offsetof(UserRecord
, real_name
), 0 },
1581 { "emailAddress", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, email_address
), JSON_SAFE
},
1582 { "iconName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, icon_name
), JSON_SAFE
},
1583 { "location", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, location
), 0 },
1584 { "disposition", JSON_VARIANT_STRING
, json_dispatch_user_disposition
, offsetof(UserRecord
, disposition
), 0 },
1585 { "lastChangeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_change_usec
), 0 },
1586 { "lastPasswordChangeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_password_change_usec
), 0 },
1587 { "shell", JSON_VARIANT_STRING
, json_dispatch_filename_or_path
, offsetof(UserRecord
, shell
), 0 },
1588 { "umask", JSON_VARIANT_UNSIGNED
, json_dispatch_umask
, offsetof(UserRecord
, umask
), 0 },
1589 { "environment", JSON_VARIANT_ARRAY
, json_dispatch_environment
, offsetof(UserRecord
, environment
), 0 },
1590 { "timeZone", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, time_zone
), JSON_SAFE
},
1591 { "preferredLanguage", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, preferred_language
), JSON_SAFE
},
1592 { "niceLevel", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_nice
, offsetof(UserRecord
, nice_level
), 0 },
1593 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_rlimits
, offsetof(UserRecord
, rlimits
), 0 },
1594 { "locked", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, locked
), 0 },
1595 { "notBeforeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_before_usec
), 0 },
1596 { "notAfterUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_after_usec
), 0 },
1597 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
1598 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_disk_size
, offsetof(UserRecord
, disk_size
), 0 },
1599 { "diskSizeRelative", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size_relative
), 0 },
1600 { "skeletonDirectory", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, skeleton_directory
), 0 },
1601 { "accessMode", JSON_VARIANT_UNSIGNED
, json_dispatch_access_mode
, offsetof(UserRecord
, access_mode
), 0 },
1602 { "tasksMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, tasks_max
), 0 },
1603 { "memoryHigh", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_high
), 0 },
1604 { "memoryMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_max
), 0 },
1605 { "cpuWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, cpu_weight
), 0 },
1606 { "ioWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, io_weight
), 0 },
1607 { "mountNoDevices", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nodev
), 0 },
1608 { "mountNoSuid", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nosuid
), 0 },
1609 { "mountNoExecute", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, noexec
), 0 },
1610 { "cifsDomain", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_domain
), JSON_SAFE
},
1611 { "cifsUserName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_user_name
), JSON_SAFE
},
1612 { "cifsService", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_service
), JSON_SAFE
},
1613 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, image_path
), 0 },
1614 { "homeDirectory", JSON_VARIANT_STRING
, json_dispatch_home_directory
, offsetof(UserRecord
, home_directory
), 0 },
1615 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
1616 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
1617 { "memberOf", JSON_VARIANT_ARRAY
, json_dispatch_user_group_list
, offsetof(UserRecord
, member_of
), JSON_RELAX
},
1618 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
1619 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
1620 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
1621 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
1622 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_discard
), 0 },
1623 { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_offline_discard
), 0 },
1624 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
1625 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
1626 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
1627 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_hash_algorithm
), JSON_SAFE
},
1628 { "luksPbkdfType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_type
), JSON_SAFE
},
1629 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_time_cost_usec
), 0 },
1630 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_memory_cost
), 0 },
1631 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_parallel_threads
), 0 },
1632 { "service", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, service
), JSON_SAFE
},
1633 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_interval_usec
), 0 },
1634 { "rateLimitBurst", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_burst
), 0 },
1635 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, enforce_password_policy
), 0 },
1636 { "autoLogin", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, auto_login
), 0 },
1637 { "stopDelayUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, stop_delay_usec
), 0 },
1638 { "killProcesses", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, kill_processes
), 0 },
1639 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_min_usec
), 0 },
1640 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_max_usec
), 0 },
1641 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_warn_usec
), 0 },
1642 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_inactive_usec
), 0 },
1643 { "passwordChangeNow", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, password_change_now
), 0 },
1644 { "pkcs11TokenUri", JSON_VARIANT_ARRAY
, dispatch_pkcs11_uri_array
, offsetof(UserRecord
, pkcs11_token_uri
), 0 },
1645 { "fido2HmacCredential", JSON_VARIANT_ARRAY
, dispatch_fido2_hmac_credential_array
, 0, 0 },
1646 { "recoveryKeyType", JSON_VARIANT_ARRAY
, json_dispatch_strv
, offsetof(UserRecord
, recovery_key_type
), 0 },
1648 { "secret", JSON_VARIANT_OBJECT
, dispatch_secret
, 0, 0 },
1649 { "privileged", JSON_VARIANT_OBJECT
, dispatch_privileged
, 0, 0 },
1651 /* Ignore the perMachine, binding, status stuff here, and process it later, so that it overrides whatever is set above */
1652 { "perMachine", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1653 { "binding", JSON_VARIANT_OBJECT
, NULL
, 0, 0 },
1654 { "status", JSON_VARIANT_OBJECT
, NULL
, 0, 0 },
1656 /* Ignore 'signature', we check it with explicit accessors instead */
1657 { "signature", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1661 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1667 /* Note that this call will leave a half-initialized record around on failure! */
1669 r
= user_group_record_mangle(v
, load_flags
, &h
->json
, &h
->mask
);
1673 r
= json_dispatch(h
->json
, user_dispatch_table
, NULL
, json_flags
, h
);
1677 /* During the parsing operation above we ignored the 'perMachine', 'binding' and 'status' fields,
1678 * since we want them to override the global options. Let's process them now. */
1680 r
= dispatch_per_machine("perMachine", json_variant_by_key(h
->json
, "perMachine"), json_flags
, h
);
1684 r
= dispatch_binding("binding", json_variant_by_key(h
->json
, "binding"), json_flags
, h
);
1688 r
= dispatch_status("status", json_variant_by_key(h
->json
, "status"), json_flags
, h
);
1692 if (FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
) && !h
->user_name
)
1693 return json_log(h
->json
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "User name field missing, refusing.");
1695 r
= user_record_augment(h
, json_flags
);
1702 int user_record_build(UserRecord
**ret
, ...) {
1703 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1704 _cleanup_(user_record_unrefp
) UserRecord
*u
= NULL
;
1711 r
= json_buildv(&v
, ap
);
1717 u
= user_record_new();
1721 r
= user_record_load(u
, v
, USER_RECORD_LOAD_FULL
);
1729 const char *user_record_user_name_and_realm(UserRecord
*h
) {
1732 /* Return the pre-initialized joined string if it is defined */
1733 if (h
->user_name_and_realm_auto
)
1734 return h
->user_name_and_realm_auto
;
1736 /* If it's not defined then we cannot have a realm */
1738 return h
->user_name
;
1741 UserStorage
user_record_storage(UserRecord
*h
) {
1744 if (h
->storage
>= 0)
1747 return USER_CLASSIC
;
1750 const char *user_record_file_system_type(UserRecord
*h
) {
1753 return h
->file_system_type
?: "btrfs";
1756 const char *user_record_skeleton_directory(UserRecord
*h
) {
1759 return h
->skeleton_directory
?: "/etc/skel";
1762 mode_t
user_record_access_mode(UserRecord
*h
) {
1765 return h
->access_mode
!= MODE_INVALID
? h
->access_mode
: 0700;
1768 const char* user_record_home_directory(UserRecord
*h
) {
1771 if (h
->home_directory
)
1772 return h
->home_directory
;
1773 if (h
->home_directory_auto
)
1774 return h
->home_directory_auto
;
1776 /* The root user is special, hence be special about it */
1777 if (streq_ptr(h
->user_name
, "root"))
1783 const char *user_record_image_path(UserRecord
*h
) {
1787 return h
->image_path
;
1788 if (h
->image_path_auto
)
1789 return h
->image_path_auto
;
1791 return IN_SET(user_record_storage(h
), USER_CLASSIC
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
) ? user_record_home_directory(h
) : NULL
;
1794 const char *user_record_cifs_user_name(UserRecord
*h
) {
1797 return h
->cifs_user_name
?: h
->user_name
;
1800 unsigned long user_record_mount_flags(UserRecord
*h
) {
1803 return (h
->nosuid
? MS_NOSUID
: 0) |
1804 (h
->noexec
? MS_NOEXEC
: 0) |
1805 (h
->nodev
? MS_NODEV
: 0);
1808 const char *user_record_shell(UserRecord
*h
) {
1814 if (streq_ptr(h
->user_name
, "root"))
1817 if (user_record_disposition(h
) == USER_REGULAR
)
1823 const char *user_record_real_name(UserRecord
*h
) {
1826 return h
->real_name
?: h
->user_name
;
1829 bool user_record_luks_discard(UserRecord
*h
) {
1834 if (h
->luks_discard
>= 0)
1835 return h
->luks_discard
;
1837 ip
= user_record_image_path(h
);
1841 /* Use discard by default if we are referring to a real block device, but not when operating on a
1842 * loopback device. We want to optimize for SSD and flash storage after all, but we should be careful
1843 * when storing stuff on top of regular file systems in loopback files as doing discard then would
1844 * mean thin provisioning and we should not do that willy-nilly since it means we'll risk EIO later
1845 * on should the disk space to back our file systems not be available. */
1847 return path_startswith(ip
, "/dev/");
1850 bool user_record_luks_offline_discard(UserRecord
*h
) {
1855 if (h
->luks_offline_discard
>= 0)
1856 return h
->luks_offline_discard
;
1858 /* Discard while we are logged out should generally be a good idea, except when operating directly on
1859 * physical media, where we should just bind it to the online discard mode. */
1861 ip
= user_record_image_path(h
);
1865 if (path_startswith(ip
, "/dev/"))
1866 return user_record_luks_discard(h
);
1871 const char *user_record_luks_cipher(UserRecord
*h
) {
1874 return h
->luks_cipher
?: "aes";
1877 const char *user_record_luks_cipher_mode(UserRecord
*h
) {
1880 return h
->luks_cipher_mode
?: "xts-plain64";
1883 uint64_t user_record_luks_volume_key_size(UserRecord
*h
) {
1886 /* We return a value here that can be cast without loss into size_t which is what libcrypsetup expects */
1888 if (h
->luks_volume_key_size
== UINT64_MAX
)
1891 return MIN(h
->luks_volume_key_size
, SIZE_MAX
);
1894 const char* user_record_luks_pbkdf_type(UserRecord
*h
) {
1897 return h
->luks_pbkdf_type
?: "argon2id";
1900 uint64_t user_record_luks_pbkdf_time_cost_usec(UserRecord
*h
) {
1903 /* Returns a value with ms granularity, since that's what libcryptsetup expects */
1905 if (h
->luks_pbkdf_time_cost_usec
== UINT64_MAX
)
1906 return 500 * USEC_PER_MSEC
; /* We default to 500ms, in contrast to libcryptsetup's 2s, which is just awfully slow on every login */
1908 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_time_cost_usec
, USEC_PER_MSEC
), UINT32_MAX
) * USEC_PER_MSEC
;
1911 uint64_t user_record_luks_pbkdf_memory_cost(UserRecord
*h
) {
1914 /* Returns a value with kb granularity, since that's what libcryptsetup expects */
1916 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1917 return 64*1024*1024; /* We default to 64M, since this should work on smaller systems too */
1919 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_memory_cost
, 1024), UINT32_MAX
) * 1024;
1922 uint64_t user_record_luks_pbkdf_parallel_threads(UserRecord
*h
) {
1925 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1926 return 1; /* We default to 1, since this should work on smaller systems too */
1928 return MIN(h
->luks_pbkdf_parallel_threads
, UINT32_MAX
);
1931 const char *user_record_luks_pbkdf_hash_algorithm(UserRecord
*h
) {
1934 return h
->luks_pbkdf_hash_algorithm
?: "sha512";
1937 gid_t
user_record_gid(UserRecord
*h
) {
1940 if (gid_is_valid(h
->gid
))
1943 return (gid_t
) h
->uid
;
1946 UserDisposition
user_record_disposition(UserRecord
*h
) {
1949 if (h
->disposition
>= 0)
1950 return h
->disposition
;
1952 /* If not declared, derive from UID */
1954 if (!uid_is_valid(h
->uid
))
1955 return _USER_DISPOSITION_INVALID
;
1957 if (h
->uid
== 0 || h
->uid
== UID_NOBODY
)
1958 return USER_INTRINSIC
;
1960 if (uid_is_system(h
->uid
))
1963 if (uid_is_dynamic(h
->uid
))
1964 return USER_DYNAMIC
;
1966 if (uid_is_container(h
->uid
))
1967 return USER_CONTAINER
;
1969 if (h
->uid
> INT32_MAX
)
1970 return USER_RESERVED
;
1972 return USER_REGULAR
;
1975 int user_record_removable(UserRecord
*h
) {
1976 UserStorage storage
;
1979 if (h
->removable
>= 0)
1980 return h
->removable
;
1982 /* Refuse to decide for classic records */
1983 storage
= user_record_storage(h
);
1984 if (h
->storage
< 0 || h
->storage
== USER_CLASSIC
)
1987 /* For now consider only LUKS home directories with a reference by path as removable */
1988 return storage
== USER_LUKS
&& path_startswith(user_record_image_path(h
), "/dev/");
1991 uint64_t user_record_ratelimit_interval_usec(UserRecord
*h
) {
1994 if (h
->ratelimit_interval_usec
== UINT64_MAX
)
1995 return DEFAULT_RATELIMIT_INTERVAL_USEC
;
1997 return h
->ratelimit_interval_usec
;
2000 uint64_t user_record_ratelimit_burst(UserRecord
*h
) {
2003 if (h
->ratelimit_burst
== UINT64_MAX
)
2004 return DEFAULT_RATELIMIT_BURST
;
2006 return h
->ratelimit_burst
;
2009 bool user_record_can_authenticate(UserRecord
*h
) {
2012 /* Returns true if there's some form of property configured that the user can authenticate against */
2014 if (h
->n_pkcs11_encrypted_key
> 0)
2017 if (h
->n_fido2_hmac_salt
> 0)
2020 return !strv_isempty(h
->hashed_password
);
2023 uint64_t user_record_ratelimit_next_try(UserRecord
*h
) {
2026 /* Calculates when the it's possible to login next. Returns:
2028 * UINT64_MAX → Nothing known
2030 * Any other → Next time in CLOCK_REALTIME in usec (which could be in the past)
2033 if (h
->ratelimit_begin_usec
== UINT64_MAX
||
2034 h
->ratelimit_count
== UINT64_MAX
)
2037 if (h
->ratelimit_begin_usec
> now(CLOCK_REALTIME
)) /* If the ratelimit time is in the future, then
2038 * the local clock is probably incorrect. Let's
2039 * not refuse login then. */
2042 if (h
->ratelimit_count
< user_record_ratelimit_burst(h
))
2045 return usec_add(h
->ratelimit_begin_usec
, user_record_ratelimit_interval_usec(h
));
2048 bool user_record_equal(UserRecord
*a
, UserRecord
*b
) {
2052 /* We assume that when a record is modified its JSON data is updated at the same time, hence it's
2053 * sufficient to compare the JSON data. */
2055 return json_variant_equal(a
->json
, b
->json
);
2058 bool user_record_compatible(UserRecord
*a
, UserRecord
*b
) {
2062 /* If either lacks the regular section, we can't really decide, let's hence say they are
2064 if (!(a
->mask
& b
->mask
& USER_RECORD_REGULAR
))
2067 return streq_ptr(a
->user_name
, b
->user_name
) &&
2068 streq_ptr(a
->realm
, b
->realm
);
2071 int user_record_compare_last_change(UserRecord
*a
, UserRecord
*b
) {
2075 if (a
->last_change_usec
== b
->last_change_usec
)
2078 /* Always consider a record with a timestamp newer than one without */
2079 if (a
->last_change_usec
== UINT64_MAX
)
2081 if (b
->last_change_usec
== UINT64_MAX
)
2084 return CMP(a
->last_change_usec
, b
->last_change_usec
);
2087 int user_record_clone(UserRecord
*h
, UserRecordLoadFlags flags
, UserRecord
**ret
) {
2088 _cleanup_(user_record_unrefp
) UserRecord
*c
= NULL
;
2094 c
= user_record_new();
2098 r
= user_record_load(c
, h
->json
, flags
);
2106 int user_record_masked_equal(UserRecord
*a
, UserRecord
*b
, UserRecordMask mask
) {
2107 _cleanup_(user_record_unrefp
) UserRecord
*x
= NULL
, *y
= NULL
;
2113 /* Compares the two records, but ignores anything not listed in the specified mask */
2115 if ((a
->mask
& ~mask
) != 0) {
2116 r
= user_record_clone(a
, USER_RECORD_ALLOW(mask
) | USER_RECORD_STRIP(~mask
& _USER_RECORD_MASK_MAX
) | USER_RECORD_PERMISSIVE
, &x
);
2123 if ((b
->mask
& ~mask
) != 0) {
2124 r
= user_record_clone(b
, USER_RECORD_ALLOW(mask
) | USER_RECORD_STRIP(~mask
& _USER_RECORD_MASK_MAX
) | USER_RECORD_PERMISSIVE
, &y
);
2131 return user_record_equal(a
, b
);
2134 int user_record_test_blocked(UserRecord
*h
) {
2137 /* Checks whether access to the specified user shall be allowed at the moment. Returns:
2139 * -ESTALE: Record is from the future
2140 * -ENOLCK: Record is blocked
2141 * -EL2HLT: Record is not valid yet
2142 * -EL3HLT: Record is not valid anymore
2151 n
= now(CLOCK_REALTIME
);
2153 if (h
->not_before_usec
!= UINT64_MAX
&& n
< h
->not_before_usec
)
2155 if (h
->not_after_usec
!= UINT64_MAX
&& n
> h
->not_after_usec
)
2158 if (h
->last_change_usec
!= UINT64_MAX
&&
2159 h
->last_change_usec
> n
) /* Complain during log-ins when the record is from the future */
2165 int user_record_test_password_change_required(UserRecord
*h
) {
2166 bool change_permitted
;
2171 /* Checks whether the user must change the password when logging in
2173 -EKEYREVOKED: Change password now because admin said so
2174 -EOWNERDEAD: Change password now because it expired
2175 -EKEYREJECTED: Password is expired, no changing is allowed
2176 -EKEYEXPIRED: Password is about to expire, warn user
2177 -ENETDOWN: Record has expiration info but no password change timestamp
2178 -EROFS: No password change required nor permitted
2179 -ESTALE: RTC likely incorrect, last password change is in the future
2180 0: No password change required, but permitted
2183 /* If a password change request has been set explicitly, it overrides everything */
2184 if (h
->password_change_now
> 0)
2185 return -EKEYREVOKED
;
2187 n
= now(CLOCK_REALTIME
);
2189 /* Password change in the future? Then our RTC is likely incorrect */
2190 if (h
->last_password_change_usec
!= UINT64_MAX
&&
2191 h
->last_password_change_usec
> n
&&
2192 (h
->password_change_min_usec
!= UINT64_MAX
||
2193 h
->password_change_max_usec
!= UINT64_MAX
||
2194 h
->password_change_inactive_usec
!= UINT64_MAX
))
2197 /* Then, let's check if password changing is currently allowed at all */
2198 if (h
->password_change_min_usec
!= UINT64_MAX
) {
2200 /* Expiry configured but no password change timestamp known? */
2201 if (h
->last_password_change_usec
== UINT64_MAX
)
2204 if (h
->password_change_min_usec
>= UINT64_MAX
- h
->last_password_change_usec
)
2205 change_permitted
= false;
2207 change_permitted
= n
>= h
->last_password_change_usec
+ h
->password_change_min_usec
;
2210 change_permitted
= true;
2212 /* Let's check whether the password has expired. */
2213 if (!(h
->password_change_max_usec
== UINT64_MAX
||
2214 h
->password_change_max_usec
>= UINT64_MAX
- h
->last_password_change_usec
)) {
2216 uint64_t change_before
;
2218 /* Expiry configured but no password change timestamp known? */
2219 if (h
->last_password_change_usec
== UINT64_MAX
)
2222 /* Password is in inactive phase? */
2223 if (h
->password_change_inactive_usec
!= UINT64_MAX
&&
2224 h
->password_change_inactive_usec
< UINT64_MAX
- h
->password_change_max_usec
) {
2227 added
= h
->password_change_inactive_usec
+ h
->password_change_max_usec
;
2228 if (added
< UINT64_MAX
- h
->last_password_change_usec
&&
2229 n
>= h
->last_password_change_usec
+ added
)
2230 return -EKEYREJECTED
;
2233 /* Password needs to be changed now? */
2234 change_before
= h
->last_password_change_usec
+ h
->password_change_max_usec
;
2235 if (n
>= change_before
)
2236 return change_permitted
? -EOWNERDEAD
: -EKEYREJECTED
;
2239 if (h
->password_change_warn_usec
!= UINT64_MAX
&&
2240 (change_before
< h
->password_change_warn_usec
||
2241 n
>= change_before
- h
->password_change_warn_usec
))
2242 return change_permitted
? -EKEYEXPIRED
: -EROFS
;
2245 /* No password changing necessary */
2246 return change_permitted
? 0 : -EROFS
;
2249 static const char* const user_storage_table
[_USER_STORAGE_MAX
] = {
2250 [USER_CLASSIC
] = "classic",
2251 [USER_LUKS
] = "luks",
2252 [USER_DIRECTORY
] = "directory",
2253 [USER_SUBVOLUME
] = "subvolume",
2254 [USER_FSCRYPT
] = "fscrypt",
2255 [USER_CIFS
] = "cifs",
2258 DEFINE_STRING_TABLE_LOOKUP(user_storage
, UserStorage
);
2260 static const char* const user_disposition_table
[_USER_DISPOSITION_MAX
] = {
2261 [USER_INTRINSIC
] = "intrinsic",
2262 [USER_SYSTEM
] = "system",
2263 [USER_DYNAMIC
] = "dynamic",
2264 [USER_REGULAR
] = "regular",
2265 [USER_CONTAINER
] = "container",
2266 [USER_RESERVED
] = "reserved",
2269 DEFINE_STRING_TABLE_LOOKUP(user_disposition
, UserDisposition
);