1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "cgroup-util.h"
6 #include "dns-domain.h"
10 #include "hostname-util.h"
11 #include "memory-util.h"
12 #include "path-util.h"
13 #include "pkcs11-util.h"
14 #include "rlimit-util.h"
15 #include "stat-util.h"
16 #include "string-table.h"
18 #include "user-record.h"
19 #include "user-util.h"
21 #define DEFAULT_RATELIMIT_BURST 30
22 #define DEFAULT_RATELIMIT_INTERVAL_USEC (1*USEC_PER_MINUTE)
24 UserRecord
* user_record_new(void) {
27 h
= new(UserRecord
, 1);
33 .disposition
= _USER_DISPOSITION_INVALID
,
34 .last_change_usec
= UINT64_MAX
,
35 .last_password_change_usec
= UINT64_MAX
,
36 .umask
= MODE_INVALID
,
37 .nice_level
= INT_MAX
,
38 .not_before_usec
= UINT64_MAX
,
39 .not_after_usec
= UINT64_MAX
,
41 .storage
= _USER_STORAGE_INVALID
,
42 .access_mode
= MODE_INVALID
,
43 .disk_size
= UINT64_MAX
,
44 .disk_size_relative
= UINT64_MAX
,
45 .tasks_max
= UINT64_MAX
,
46 .memory_high
= UINT64_MAX
,
47 .memory_max
= UINT64_MAX
,
48 .cpu_weight
= UINT64_MAX
,
49 .io_weight
= UINT64_MAX
,
55 .luks_offline_discard
= -1,
56 .luks_volume_key_size
= UINT64_MAX
,
57 .luks_pbkdf_time_cost_usec
= UINT64_MAX
,
58 .luks_pbkdf_memory_cost
= UINT64_MAX
,
59 .luks_pbkdf_parallel_threads
= UINT64_MAX
,
60 .disk_usage
= UINT64_MAX
,
61 .disk_free
= UINT64_MAX
,
62 .disk_ceiling
= UINT64_MAX
,
63 .disk_floor
= UINT64_MAX
,
65 .good_authentication_counter
= UINT64_MAX
,
66 .bad_authentication_counter
= UINT64_MAX
,
67 .last_good_authentication_usec
= UINT64_MAX
,
68 .last_bad_authentication_usec
= UINT64_MAX
,
69 .ratelimit_begin_usec
= UINT64_MAX
,
70 .ratelimit_count
= UINT64_MAX
,
71 .ratelimit_interval_usec
= UINT64_MAX
,
72 .ratelimit_burst
= UINT64_MAX
,
74 .enforce_password_policy
= -1,
76 .stop_delay_usec
= UINT64_MAX
,
78 .password_change_min_usec
= UINT64_MAX
,
79 .password_change_max_usec
= UINT64_MAX
,
80 .password_change_warn_usec
= UINT64_MAX
,
81 .password_change_inactive_usec
= UINT64_MAX
,
82 .password_change_now
= -1,
83 .pkcs11_protected_authentication_path_permitted
= -1,
84 .fido2_user_presence_permitted
= -1,
90 static void pkcs11_encrypted_key_done(Pkcs11EncryptedKey
*k
) {
95 erase_and_free(k
->data
);
96 erase_and_free(k
->hashed_password
);
99 static void fido2_hmac_credential_done(Fido2HmacCredential
*c
) {
106 static void fido2_hmac_salt_done(Fido2HmacSalt
*s
) {
110 fido2_hmac_credential_done(&s
->credential
);
111 erase_and_free(s
->salt
);
112 erase_and_free(s
->hashed_password
);
115 static UserRecord
* user_record_free(UserRecord
*h
) {
121 free(h
->user_name_and_realm_auto
);
123 free(h
->email_address
);
124 erase_and_free(h
->password_hint
);
130 strv_free(h
->environment
);
132 free(h
->preferred_language
);
133 rlimit_free_all(h
->rlimits
);
135 free(h
->skeleton_directory
);
137 strv_free_erase(h
->hashed_password
);
138 strv_free_erase(h
->ssh_authorized_keys
);
139 strv_free_erase(h
->password
);
140 strv_free_erase(h
->token_pin
);
142 free(h
->cifs_service
);
143 free(h
->cifs_user_name
);
144 free(h
->cifs_domain
);
147 free(h
->image_path_auto
);
148 free(h
->home_directory
);
149 free(h
->home_directory_auto
);
151 strv_free(h
->member_of
);
153 free(h
->file_system_type
);
154 free(h
->luks_cipher
);
155 free(h
->luks_cipher_mode
);
156 free(h
->luks_pbkdf_hash_algorithm
);
157 free(h
->luks_pbkdf_type
);
162 strv_free(h
->pkcs11_token_uri
);
163 for (size_t i
= 0; i
< h
->n_pkcs11_encrypted_key
; i
++)
164 pkcs11_encrypted_key_done(h
->pkcs11_encrypted_key
+ i
);
165 free(h
->pkcs11_encrypted_key
);
167 for (size_t i
= 0; i
< h
->n_fido2_hmac_credential
; i
++)
168 fido2_hmac_credential_done(h
->fido2_hmac_credential
+ i
);
169 for (size_t i
= 0; i
< h
->n_fido2_hmac_salt
; i
++)
170 fido2_hmac_salt_done(h
->fido2_hmac_salt
+ i
);
172 json_variant_unref(h
->json
);
177 DEFINE_TRIVIAL_REF_UNREF_FUNC(UserRecord
, user_record
, user_record_free
);
179 int json_dispatch_realm(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
184 if (json_variant_is_null(variant
)) {
189 if (!json_variant_is_string(variant
))
190 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
192 n
= json_variant_string(variant
);
193 r
= dns_name_is_valid(n
);
195 return json_log(variant
, flags
, r
, "Failed to check if JSON field '%s' is a valid DNS domain.", strna(name
));
197 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid DNS domain.", strna(name
));
199 r
= free_and_strdup(s
, n
);
201 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
206 int json_dispatch_gecos(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
210 if (json_variant_is_null(variant
)) {
215 if (!json_variant_is_string(variant
))
216 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
218 n
= json_variant_string(variant
);
219 if (valid_gecos(n
)) {
220 if (free_and_strdup(s
, n
) < 0)
221 return json_log_oom(variant
, flags
);
223 _cleanup_free_
char *m
= NULL
;
225 json_log(variant
, flags
|JSON_DEBUG
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid GECOS compatible string, mangling.", strna(name
));
229 return json_log_oom(variant
, flags
);
231 free_and_replace(*s
, m
);
237 static int json_dispatch_nice(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
241 if (json_variant_is_null(variant
)) {
246 if (!json_variant_is_integer(variant
))
247 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
249 m
= json_variant_integer(variant
);
250 if (m
< PRIO_MIN
|| m
>= PRIO_MAX
)
251 return json_log(variant
, flags
, SYNTHETIC_ERRNO(ERANGE
), "JSON field '%s' is not a valid nice level.", strna(name
));
257 static int json_dispatch_rlimit_value(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
258 rlim_t
*ret
= userdata
;
260 if (json_variant_is_null(variant
))
261 *ret
= RLIM_INFINITY
;
262 else if (json_variant_is_unsigned(variant
)) {
265 w
= json_variant_unsigned(variant
);
266 if (w
== RLIM_INFINITY
|| (uintmax_t) w
!= json_variant_unsigned(variant
))
267 return json_log(variant
, flags
, SYNTHETIC_ERRNO(ERANGE
), "Resource limit value '%s' is out of range.", name
);
271 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit value '%s' is not an unsigned integer.", name
);
276 static int json_dispatch_rlimits(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
277 struct rlimit
** limits
= userdata
;
284 if (json_variant_is_null(variant
)) {
285 rlimit_free_all(limits
);
289 if (!json_variant_is_object(variant
))
290 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
292 JSON_VARIANT_OBJECT_FOREACH(key
, value
, variant
) {
293 JsonVariant
*jcur
, *jmax
;
298 p
= startswith(key
, "RLIMIT_");
302 l
= rlimit_from_string(p
);
304 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' not known.", key
);
306 if (!json_variant_is_object(value
))
307 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' has invalid value.", key
);
309 if (json_variant_elements(value
) != 4)
310 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' value is does not have two fields as expected.", key
);
312 jcur
= json_variant_by_key(value
, "cur");
314 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' lacks 'cur' field.", key
);
315 r
= json_dispatch_rlimit_value("cur", jcur
, flags
, &rl
.rlim_cur
);
319 jmax
= json_variant_by_key(value
, "max");
321 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' lacks 'max' field.", key
);
322 r
= json_dispatch_rlimit_value("max", jmax
, flags
, &rl
.rlim_max
);
329 limits
[l
] = newdup(struct rlimit
, &rl
, 1);
338 static int json_dispatch_filename_or_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
345 if (json_variant_is_null(variant
)) {
350 if (!json_variant_is_string(variant
))
351 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
353 n
= json_variant_string(variant
);
354 if (!filename_is_valid(n
) && !path_is_normalized(n
))
355 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid file name or normalized path.", strna(name
));
357 r
= free_and_strdup(s
, n
);
359 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
364 static int json_dispatch_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
369 if (json_variant_is_null(variant
)) {
374 if (!json_variant_is_string(variant
))
375 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
377 n
= json_variant_string(variant
);
378 if (!path_is_normalized(n
))
379 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a normalized file system path.", strna(name
));
380 if (!path_is_absolute(n
))
381 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an absolute file system path.", strna(name
));
383 r
= free_and_strdup(s
, n
);
385 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
390 static int json_dispatch_home_directory(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
395 if (json_variant_is_null(variant
)) {
400 if (!json_variant_is_string(variant
))
401 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
403 n
= json_variant_string(variant
);
405 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid home directory path.", strna(name
));
407 r
= free_and_strdup(s
, n
);
409 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
414 static int json_dispatch_image_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
419 if (json_variant_is_null(variant
)) {
424 if (!json_variant_is_string(variant
))
425 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
427 n
= json_variant_string(variant
);
428 if (empty_or_root(n
) || !path_is_valid(n
) || !path_is_absolute(n
))
429 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid image path.", strna(name
));
431 r
= free_and_strdup(s
, n
);
433 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
438 static int json_dispatch_umask(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
439 mode_t
*m
= userdata
;
442 if (json_variant_is_null(variant
)) {
447 if (!json_variant_is_unsigned(variant
))
448 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a number.", strna(name
));
450 k
= json_variant_unsigned(variant
);
452 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' outside of valid range 0…0777.", strna(name
));
458 static int json_dispatch_access_mode(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
459 mode_t
*m
= userdata
;
462 if (json_variant_is_null(variant
)) {
467 if (!json_variant_is_unsigned(variant
))
468 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a number.", strna(name
));
470 k
= json_variant_unsigned(variant
);
472 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' outside of valid range 0…07777.", strna(name
));
478 static int json_dispatch_environment(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
479 _cleanup_strv_free_
char **n
= NULL
;
480 char ***l
= userdata
;
484 if (json_variant_is_null(variant
)) {
489 if (!json_variant_is_array(variant
))
490 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array.", strna(name
));
492 for (i
= 0; i
< json_variant_elements(variant
); i
++) {
493 _cleanup_free_
char *c
= NULL
;
497 e
= json_variant_by_index(variant
, i
);
498 if (!json_variant_is_string(e
))
499 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of strings.", strna(name
));
501 assert_se(a
= json_variant_string(e
));
503 if (!env_assignment_is_valid(a
))
504 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of environment variables.", strna(name
));
508 return json_log_oom(variant
, flags
);
510 r
= strv_env_replace(&n
, c
);
512 return json_log_oom(variant
, flags
);
517 strv_free_and_replace(*l
, n
);
521 int json_dispatch_user_disposition(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
522 UserDisposition
*disposition
= userdata
, k
;
524 if (json_variant_is_null(variant
)) {
525 *disposition
= _USER_DISPOSITION_INVALID
;
529 if (!json_variant_is_string(variant
))
530 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
532 k
= user_disposition_from_string(json_variant_string(variant
));
534 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Disposition type '%s' not known.", json_variant_string(variant
));
540 static int json_dispatch_storage(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
541 UserStorage
*storage
= userdata
, k
;
543 if (json_variant_is_null(variant
)) {
544 *storage
= _USER_STORAGE_INVALID
;
548 if (!json_variant_is_string(variant
))
549 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
551 k
= user_storage_from_string(json_variant_string(variant
));
553 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Storage type '%s' not known.", json_variant_string(variant
));
559 static int json_dispatch_disk_size(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
560 uint64_t *size
= userdata
;
563 if (json_variant_is_null(variant
)) {
568 if (!json_variant_is_unsigned(variant
))
569 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an integer.", strna(name
));
571 k
= json_variant_unsigned(variant
);
572 if (k
< USER_DISK_SIZE_MIN
|| k
> USER_DISK_SIZE_MAX
)
573 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
);
579 static int json_dispatch_tasks_or_memory_max(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
580 uint64_t *limit
= userdata
;
583 if (json_variant_is_null(variant
)) {
588 if (!json_variant_is_unsigned(variant
))
589 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a integer.", strna(name
));
591 k
= json_variant_unsigned(variant
);
592 if (k
<= 0 || k
>= UINT64_MAX
)
593 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);
599 static int json_dispatch_weight(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
600 uint64_t *weight
= userdata
;
603 if (json_variant_is_null(variant
)) {
604 *weight
= UINT64_MAX
;
608 if (!json_variant_is_unsigned(variant
))
609 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a integer.", strna(name
));
611 k
= json_variant_unsigned(variant
);
612 if (k
<= CGROUP_WEIGHT_MIN
|| k
>= CGROUP_WEIGHT_MAX
)
613 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
);
619 int json_dispatch_user_group_list(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
620 _cleanup_strv_free_
char **l
= NULL
;
621 char ***list
= userdata
;
625 if (!json_variant_is_array(variant
))
626 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of strings.", strna(name
));
628 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
630 if (!json_variant_is_string(e
))
631 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
633 if (!valid_user_group_name(json_variant_string(e
), FLAGS_SET(flags
, JSON_RELAX
) ? VALID_USER_RELAX
: 0))
634 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a valid user/group name: %s", json_variant_string(e
));
636 r
= strv_extend(&l
, json_variant_string(e
));
638 return json_log(e
, flags
, r
, "Failed to append array element: %m");
641 r
= strv_extend_strv(list
, l
, true);
643 return json_log(variant
, flags
, r
, "Failed to merge user/group arrays: %m");
648 static int dispatch_secret(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
650 static const JsonDispatch secret_dispatch_table
[] = {
651 { "password", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, password
), 0 },
652 { "tokenPin", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, token_pin
), 0 },
653 { "pkcs11Pin", /* legacy alias */ _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, token_pin
), 0 },
654 { "pkcs11ProtectedAuthenticationPathPermitted", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, pkcs11_protected_authentication_path_permitted
), 0 },
655 { "fido2UserPresencePermitted", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, fido2_user_presence_permitted
), 0 },
659 return json_dispatch(variant
, secret_dispatch_table
, NULL
, flags
, userdata
);
662 static int dispatch_pkcs11_uri(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
667 if (json_variant_is_null(variant
)) {
672 if (!json_variant_is_string(variant
))
673 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
675 n
= json_variant_string(variant
);
676 if (!pkcs11_uri_valid(n
))
677 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name
));
679 r
= free_and_strdup(s
, n
);
681 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
686 static int dispatch_pkcs11_uri_array(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
687 _cleanup_strv_free_
char **z
= NULL
;
688 char ***l
= userdata
;
692 if (json_variant_is_null(variant
)) {
697 if (json_variant_is_string(variant
)) {
700 n
= json_variant_string(variant
);
701 if (!pkcs11_uri_valid(n
))
702 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name
));
710 if (!json_variant_is_array(variant
))
711 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string or array of strings.", strna(name
));
713 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
716 if (!json_variant_is_string(e
))
717 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
719 n
= json_variant_string(e
);
720 if (!pkcs11_uri_valid(n
))
721 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
);
723 r
= strv_extend(&z
, n
);
729 strv_free_and_replace(*l
, z
);
733 static int dispatch_pkcs11_key_data(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
734 Pkcs11EncryptedKey
*k
= userdata
;
739 if (json_variant_is_null(variant
)) {
740 k
->data
= erase_and_free(k
->data
);
745 if (!json_variant_is_string(variant
))
746 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
748 r
= unbase64mem(json_variant_string(variant
), (size_t) -1, &b
, &l
);
750 return json_log(variant
, flags
, r
, "Failed to decode encrypted PKCS#11 key: %m");
752 erase_and_free(k
->data
);
759 static int dispatch_pkcs11_key(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
760 UserRecord
*h
= userdata
;
764 if (!json_variant_is_array(variant
))
765 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
767 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
768 Pkcs11EncryptedKey
*array
, *k
;
770 static const JsonDispatch pkcs11_key_dispatch_table
[] = {
771 { "uri", JSON_VARIANT_STRING
, dispatch_pkcs11_uri
, offsetof(Pkcs11EncryptedKey
, uri
), JSON_MANDATORY
},
772 { "data", JSON_VARIANT_STRING
, dispatch_pkcs11_key_data
, 0, JSON_MANDATORY
},
773 { "hashedPassword", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(Pkcs11EncryptedKey
, hashed_password
), JSON_MANDATORY
},
777 if (!json_variant_is_object(e
))
778 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
780 array
= reallocarray(h
->pkcs11_encrypted_key
, h
->n_pkcs11_encrypted_key
+ 1, sizeof(Pkcs11EncryptedKey
));
784 h
->pkcs11_encrypted_key
= array
;
785 k
= h
->pkcs11_encrypted_key
+ h
->n_pkcs11_encrypted_key
;
786 *k
= (Pkcs11EncryptedKey
) {};
788 r
= json_dispatch(e
, pkcs11_key_dispatch_table
, NULL
, flags
, k
);
790 pkcs11_encrypted_key_done(k
);
794 h
->n_pkcs11_encrypted_key
++;
800 static int dispatch_fido2_hmac_credential(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
801 Fido2HmacCredential
*k
= userdata
;
806 if (json_variant_is_null(variant
)) {
807 k
->id
= mfree(k
->id
);
812 if (!json_variant_is_string(variant
))
813 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
815 r
= unbase64mem(json_variant_string(variant
), (size_t) -1, &b
, &l
);
817 return json_log(variant
, flags
, r
, "Failed to decode FIDO2 credential ID: %m");
819 free_and_replace(k
->id
, b
);
825 static int dispatch_fido2_hmac_credential_array(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
826 UserRecord
*h
= userdata
;
830 if (!json_variant_is_array(variant
))
831 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of strings.", strna(name
));
833 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
834 Fido2HmacCredential
*array
;
838 if (!json_variant_is_string(e
))
839 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
841 array
= reallocarray(h
->fido2_hmac_credential
, h
->n_fido2_hmac_credential
+ 1, sizeof(Fido2HmacCredential
));
845 r
= unbase64mem(json_variant_string(e
), (size_t) -1, &b
, &l
);
847 return json_log(variant
, flags
, r
, "Failed to decode FIDO2 credential ID: %m");
849 h
->fido2_hmac_credential
= array
;
851 h
->fido2_hmac_credential
[h
->n_fido2_hmac_credential
++] = (Fido2HmacCredential
) {
860 static int dispatch_fido2_hmac_salt_value(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
861 Fido2HmacSalt
*k
= userdata
;
866 if (json_variant_is_null(variant
)) {
867 k
->salt
= erase_and_free(k
->salt
);
872 if (!json_variant_is_string(variant
))
873 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
875 r
= unbase64mem(json_variant_string(variant
), (size_t) -1, &b
, &l
);
877 return json_log(variant
, flags
, r
, "Failed to decode FIDO2 salt: %m");
879 erase_and_free(k
->salt
);
886 static int dispatch_fido2_hmac_salt(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
887 UserRecord
*h
= userdata
;
891 if (!json_variant_is_array(variant
))
892 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
894 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
895 Fido2HmacSalt
*array
, *k
;
897 static const JsonDispatch fido2_hmac_salt_dispatch_table
[] = {
898 { "credential", JSON_VARIANT_STRING
, dispatch_fido2_hmac_credential
, offsetof(Fido2HmacSalt
, credential
), JSON_MANDATORY
},
899 { "salt", JSON_VARIANT_STRING
, dispatch_fido2_hmac_salt_value
, 0, JSON_MANDATORY
},
900 { "hashedPassword", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(Fido2HmacSalt
, hashed_password
), JSON_MANDATORY
},
904 if (!json_variant_is_object(e
))
905 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
907 array
= reallocarray(h
->fido2_hmac_salt
, h
->n_fido2_hmac_salt
+ 1, sizeof(Fido2HmacSalt
));
911 h
->fido2_hmac_salt
= array
;
912 k
= h
->fido2_hmac_salt
+ h
->n_fido2_hmac_salt
;
913 *k
= (Fido2HmacSalt
) {};
915 r
= json_dispatch(e
, fido2_hmac_salt_dispatch_table
, NULL
, flags
, k
);
917 fido2_hmac_salt_done(k
);
921 h
->n_fido2_hmac_salt
++;
927 static int dispatch_privileged(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
929 static const JsonDispatch privileged_dispatch_table
[] = {
930 { "passwordHint", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, password_hint
), 0 },
931 { "hashedPassword", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, hashed_password
), JSON_SAFE
},
932 { "sshAuthorizedKeys", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, ssh_authorized_keys
), 0 },
933 { "pkcs11EncryptedKey", JSON_VARIANT_ARRAY
, dispatch_pkcs11_key
, 0, 0 },
934 { "fido2HmacSalt", JSON_VARIANT_ARRAY
, dispatch_fido2_hmac_salt
, 0, 0 },
938 return json_dispatch(variant
, privileged_dispatch_table
, NULL
, flags
, userdata
);
941 static int dispatch_binding(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
943 static const JsonDispatch binding_dispatch_table
[] = {
944 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_image_path
, offsetof(UserRecord
, image_path
), 0 },
945 { "homeDirectory", JSON_VARIANT_STRING
, json_dispatch_home_directory
, offsetof(UserRecord
, home_directory
), 0 },
946 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
947 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
948 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
949 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
950 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
951 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
952 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
953 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
954 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
955 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
959 char smid
[SD_ID128_STRING_MAX
];
967 if (!json_variant_is_object(variant
))
968 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
970 r
= sd_id128_get_machine(&mid
);
972 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
974 m
= json_variant_by_key(variant
, sd_id128_to_string(mid
, smid
));
978 return json_dispatch(m
, binding_dispatch_table
, NULL
, flags
, userdata
);
981 int per_machine_id_match(JsonVariant
*ids
, JsonDispatchFlags flags
) {
985 r
= sd_id128_get_machine(&mid
);
987 return json_log(ids
, flags
, r
, "Failed to acquire machine ID: %m");
989 if (json_variant_is_string(ids
)) {
992 r
= sd_id128_from_string(json_variant_string(ids
), &k
);
994 json_log(ids
, flags
, r
, "%s is not a valid machine ID, ignoring: %m", json_variant_string(ids
));
998 return sd_id128_equal(mid
, k
);
1001 if (json_variant_is_array(ids
)) {
1004 JSON_VARIANT_ARRAY_FOREACH(e
, ids
) {
1007 if (!json_variant_is_string(e
)) {
1008 json_log(e
, flags
, 0, "Machine ID is not a string, ignoring: %m");
1012 r
= sd_id128_from_string(json_variant_string(e
), &k
);
1014 json_log(e
, flags
, r
, "%s is not a valid machine ID, ignoring: %m", json_variant_string(e
));
1018 if (sd_id128_equal(mid
, k
))
1025 json_log(ids
, flags
, 0, "Machine ID is not a string or array of strings, ignoring: %m");
1029 int per_machine_hostname_match(JsonVariant
*hns
, JsonDispatchFlags flags
) {
1030 _cleanup_free_
char *hn
= NULL
;
1033 r
= gethostname_strict(&hn
);
1035 json_log(hns
, flags
, r
, "No hostname set, not matching perMachine hostname record: %m");
1039 return json_log(hns
, flags
, r
, "Failed to acquire hostname: %m");
1041 if (json_variant_is_string(hns
))
1042 return streq(json_variant_string(hns
), hn
);
1044 if (json_variant_is_array(hns
)) {
1047 JSON_VARIANT_ARRAY_FOREACH(e
, hns
) {
1049 if (!json_variant_is_string(e
)) {
1050 json_log(e
, flags
, 0, "Hostname is not a string, ignoring: %m");
1054 if (streq(json_variant_string(hns
), hn
))
1061 json_log(hns
, flags
, 0, "Hostname is not a string or array of strings, ignoring: %m");
1065 static int dispatch_per_machine(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1067 static const JsonDispatch per_machine_dispatch_table
[] = {
1068 { "matchMachineId", _JSON_VARIANT_TYPE_INVALID
, NULL
, 0, 0 },
1069 { "matchHostname", _JSON_VARIANT_TYPE_INVALID
, NULL
, 0, 0 },
1070 { "iconName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, icon_name
), JSON_SAFE
},
1071 { "location", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, location
), 0 },
1072 { "shell", JSON_VARIANT_STRING
, json_dispatch_filename_or_path
, offsetof(UserRecord
, shell
), 0 },
1073 { "umask", JSON_VARIANT_UNSIGNED
, json_dispatch_umask
, offsetof(UserRecord
, umask
), 0 },
1074 { "environment", JSON_VARIANT_ARRAY
, json_dispatch_environment
, offsetof(UserRecord
, environment
), 0 },
1075 { "timeZone", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, time_zone
), JSON_SAFE
},
1076 { "preferredLanguage", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, preferred_language
), JSON_SAFE
},
1077 { "niceLevel", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_nice
, offsetof(UserRecord
, nice_level
), 0 },
1078 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_rlimits
, offsetof(UserRecord
, rlimits
), 0 },
1079 { "locked", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, locked
), 0 },
1080 { "notBeforeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_before_usec
), 0 },
1081 { "notAfterUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_after_usec
), 0 },
1082 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
1083 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_disk_size
, offsetof(UserRecord
, disk_size
), 0 },
1084 { "diskSizeRelative", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size_relative
), 0 },
1085 { "skeletonDirectory", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, skeleton_directory
), 0 },
1086 { "accessMode", JSON_VARIANT_UNSIGNED
, json_dispatch_access_mode
, offsetof(UserRecord
, access_mode
), 0 },
1087 { "tasksMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, tasks_max
), 0 },
1088 { "memoryHigh", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_high
), 0 },
1089 { "memoryMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_max
), 0 },
1090 { "cpuWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, cpu_weight
), 0 },
1091 { "ioWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, io_weight
), 0 },
1092 { "mountNoDevices", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nodev
), 0 },
1093 { "mountNoSuid", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nosuid
), 0 },
1094 { "mountNoExecute", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, noexec
), 0 },
1095 { "cifsDomain", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_domain
), JSON_SAFE
},
1096 { "cifsUserName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_user_name
), JSON_SAFE
},
1097 { "cifsService", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_service
), JSON_SAFE
},
1098 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, image_path
), 0 },
1099 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
1100 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
1101 { "memberOf", JSON_VARIANT_ARRAY
, json_dispatch_user_group_list
, offsetof(UserRecord
, member_of
), JSON_RELAX
},
1102 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
1103 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
1104 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
1105 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
1106 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_discard
), 0, },
1107 { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_offline_discard
), 0, },
1108 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
1109 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
1110 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
1111 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_hash_algorithm
), JSON_SAFE
},
1112 { "luksPbkdfType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_type
), JSON_SAFE
},
1113 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_time_cost_usec
), 0 },
1114 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_memory_cost
), 0 },
1115 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_parallel_threads
), 0 },
1116 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_interval_usec
), 0 },
1117 { "rateLimitBurst", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_burst
), 0 },
1118 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, enforce_password_policy
), 0 },
1119 { "autoLogin", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, auto_login
), 0 },
1120 { "stopDelayUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, stop_delay_usec
), 0 },
1121 { "killProcesses", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, kill_processes
), 0 },
1122 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_min_usec
), 0 },
1123 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_max_usec
), 0 },
1124 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_warn_usec
), 0 },
1125 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_inactive_usec
), 0 },
1126 { "passwordChangeNow", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, password_change_now
), 0 },
1127 { "pkcs11TokenUri", JSON_VARIANT_ARRAY
, dispatch_pkcs11_uri_array
, offsetof(UserRecord
, pkcs11_token_uri
), 0 },
1128 { "fido2HmacCredential", JSON_VARIANT_ARRAY
, dispatch_fido2_hmac_credential_array
, 0, 0 },
1138 if (!json_variant_is_array(variant
))
1139 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array.", strna(name
));
1141 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
1142 bool matching
= false;
1145 if (!json_variant_is_object(e
))
1146 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
1148 m
= json_variant_by_key(e
, "matchMachineId");
1150 r
= per_machine_id_match(m
, flags
);
1158 m
= json_variant_by_key(e
, "matchHostname");
1160 r
= per_machine_hostname_match(m
, flags
);
1171 r
= json_dispatch(e
, per_machine_dispatch_table
, NULL
, flags
, userdata
);
1179 static int dispatch_status(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1181 static const JsonDispatch status_dispatch_table
[] = {
1182 { "diskUsage", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_usage
), 0 },
1183 { "diskFree", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_free
), 0 },
1184 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size
), 0 },
1185 { "diskCeiling", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_ceiling
), 0 },
1186 { "diskFloor", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_floor
), 0 },
1187 { "state", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, state
), JSON_SAFE
},
1188 { "service", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, service
), JSON_SAFE
},
1189 { "signedLocally", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, signed_locally
), 0 },
1190 { "goodAuthenticationCounter", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, good_authentication_counter
), 0 },
1191 { "badAuthenticationCounter", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, bad_authentication_counter
), 0 },
1192 { "lastGoodAuthenticationUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_good_authentication_usec
), 0 },
1193 { "lastBadAuthenticationUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_bad_authentication_usec
), 0 },
1194 { "rateLimitBeginUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_begin_usec
), 0 },
1195 { "rateLimitCount", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_count
), 0 },
1196 { "removable", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, removable
), 0 },
1200 char smid
[SD_ID128_STRING_MAX
];
1208 if (!json_variant_is_object(variant
))
1209 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
1211 r
= sd_id128_get_machine(&mid
);
1213 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
1215 m
= json_variant_by_key(variant
, sd_id128_to_string(mid
, smid
));
1219 return json_dispatch(m
, status_dispatch_table
, NULL
, flags
, userdata
);
1222 int user_record_build_image_path(UserStorage storage
, const char *user_name_and_realm
, char **ret
) {
1226 assert(storage
>= 0);
1227 assert(user_name_and_realm
);
1230 if (storage
== USER_LUKS
)
1232 else if (IN_SET(storage
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
))
1233 suffix
= ".homedir";
1239 z
= strjoin("/home/", user_name_and_realm
, suffix
);
1247 static int user_record_augment(UserRecord
*h
, JsonDispatchFlags json_flags
) {
1252 if (!FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
))
1255 assert(h
->user_name
);
1257 if (!h
->user_name_and_realm_auto
&& h
->realm
) {
1258 h
->user_name_and_realm_auto
= strjoin(h
->user_name
, "@", h
->realm
);
1259 if (!h
->user_name_and_realm_auto
)
1260 return json_log_oom(h
->json
, json_flags
);
1263 /* Let's add in the following automatisms only for regular users, they don't make sense for any others */
1264 if (user_record_disposition(h
) != USER_REGULAR
)
1267 if (!h
->home_directory
&& !h
->home_directory_auto
) {
1268 h
->home_directory_auto
= path_join("/home/", h
->user_name
);
1269 if (!h
->home_directory_auto
)
1270 return json_log_oom(h
->json
, json_flags
);
1273 if (!h
->image_path
&& !h
->image_path_auto
) {
1274 r
= user_record_build_image_path(user_record_storage(h
), user_record_user_name_and_realm(h
), &h
->image_path_auto
);
1276 return json_log(h
->json
, json_flags
, r
, "Failed to determine default image path: %m");
1282 int user_group_record_mangle(
1284 UserRecordLoadFlags load_flags
,
1285 JsonVariant
**ret_variant
,
1286 UserRecordMask
*ret_mask
) {
1288 static const struct {
1289 UserRecordMask mask
;
1292 { USER_RECORD_PRIVILEGED
, "privileged" },
1293 { USER_RECORD_SECRET
, "secret" },
1294 { USER_RECORD_BINDING
, "binding" },
1295 { USER_RECORD_PER_MACHINE
, "perMachine" },
1296 { USER_RECORD_STATUS
, "status" },
1297 { USER_RECORD_SIGNATURE
, "signature" },
1300 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1301 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
;
1302 JsonVariant
*array
[ELEMENTSOF(mask_field
) * 2];
1303 size_t n_retain
= 0, i
;
1304 UserRecordMask m
= 0;
1307 assert((load_flags
& _USER_RECORD_MASK_MAX
) == 0); /* detect mistakes when accidentally passing
1308 * UserRecordMask bit masks as UserRecordLoadFlags
1312 assert(ret_variant
);
1315 /* Note that this function is shared with the group record parser, hence we try to be generic in our
1316 * log message wording here, to cover both cases. */
1318 if (!json_variant_is_object(v
))
1319 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is not a JSON object, refusing.");
1321 if (USER_RECORD_ALLOW_MASK(load_flags
) == 0) /* allow nothing? */
1322 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "Nothing allowed in record, refusing.");
1324 if (USER_RECORD_STRIP_MASK(load_flags
) == _USER_RECORD_MASK_MAX
) /* strip everything? */
1325 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "Stripping everything from record, refusing.");
1327 /* Check if we have the special sections and if they match our flags set */
1328 for (i
= 0; i
< ELEMENTSOF(mask_field
); i
++) {
1331 if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags
), mask_field
[i
].mask
)) {
1333 w
= json_variant_ref(v
);
1335 r
= json_variant_filter(&w
, STRV_MAKE(mask_field
[i
].name
));
1337 return json_log(w
, json_flags
, r
, "Failed to remove field from variant: %m");
1342 e
= json_variant_by_key_full(v
, mask_field
[i
].name
, &k
);
1344 if (!FLAGS_SET(USER_RECORD_ALLOW_MASK(load_flags
), mask_field
[i
].mask
))
1345 return json_log(e
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record contains '%s' field, which is not allowed.", mask_field
[i
].name
);
1347 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1348 array
[n_retain
++] = k
;
1349 array
[n_retain
++] = e
;
1352 m
|= mask_field
[i
].mask
;
1354 if (FLAGS_SET(USER_RECORD_REQUIRE_MASK(load_flags
), mask_field
[i
].mask
))
1355 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record lacks '%s' field, which is required.", mask_field
[i
].name
);
1359 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1360 /* If we are supposed to strip regular items, then let's instead just allocate a new object
1361 * with just the stuff we need. */
1363 w
= json_variant_unref(w
);
1364 r
= json_variant_new_object(&w
, array
, n_retain
);
1366 return json_log(v
, json_flags
, r
, "Failed to allocate new object: %m");
1368 /* And now check if there's anything else in the record */
1369 for (i
= 0; i
< json_variant_elements(v
); i
+= 2) {
1371 bool special
= false;
1374 assert_se(f
= json_variant_string(json_variant_by_index(v
, i
)));
1376 for (j
= 0; j
< ELEMENTSOF(mask_field
); j
++)
1377 if (streq(f
, mask_field
[j
].name
)) { /* already covered in the loop above */
1383 if ((load_flags
& (USER_RECORD_ALLOW_REGULAR
|USER_RECORD_REQUIRE_REGULAR
)) == 0)
1384 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record contains '%s' field, which is not allowed.", f
);
1386 m
|= USER_RECORD_REGULAR
;
1392 if (FLAGS_SET(load_flags
, USER_RECORD_REQUIRE_REGULAR
) && !FLAGS_SET(m
, USER_RECORD_REGULAR
))
1393 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record lacks basic identity fields, which are required.");
1396 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is empty.");
1399 *ret_variant
= TAKE_PTR(w
);
1401 *ret_variant
= json_variant_ref(v
);
1407 int user_record_load(UserRecord
*h
, JsonVariant
*v
, UserRecordLoadFlags load_flags
) {
1409 static const JsonDispatch user_dispatch_table
[] = {
1410 { "userName", JSON_VARIANT_STRING
, json_dispatch_user_group_name
, offsetof(UserRecord
, user_name
), JSON_RELAX
},
1411 { "realm", JSON_VARIANT_STRING
, json_dispatch_realm
, offsetof(UserRecord
, realm
), 0 },
1412 { "realName", JSON_VARIANT_STRING
, json_dispatch_gecos
, offsetof(UserRecord
, real_name
), 0 },
1413 { "emailAddress", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, email_address
), JSON_SAFE
},
1414 { "iconName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, icon_name
), JSON_SAFE
},
1415 { "location", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, location
), 0 },
1416 { "disposition", JSON_VARIANT_STRING
, json_dispatch_user_disposition
, offsetof(UserRecord
, disposition
), 0 },
1417 { "lastChangeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_change_usec
), 0 },
1418 { "lastPasswordChangeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_password_change_usec
), 0 },
1419 { "shell", JSON_VARIANT_STRING
, json_dispatch_filename_or_path
, offsetof(UserRecord
, shell
), 0 },
1420 { "umask", JSON_VARIANT_UNSIGNED
, json_dispatch_umask
, offsetof(UserRecord
, umask
), 0 },
1421 { "environment", JSON_VARIANT_ARRAY
, json_dispatch_environment
, offsetof(UserRecord
, environment
), 0 },
1422 { "timeZone", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, time_zone
), JSON_SAFE
},
1423 { "preferredLanguage", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, preferred_language
), JSON_SAFE
},
1424 { "niceLevel", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_nice
, offsetof(UserRecord
, nice_level
), 0 },
1425 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_rlimits
, offsetof(UserRecord
, rlimits
), 0 },
1426 { "locked", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, locked
), 0 },
1427 { "notBeforeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_before_usec
), 0 },
1428 { "notAfterUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_after_usec
), 0 },
1429 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
1430 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_disk_size
, offsetof(UserRecord
, disk_size
), 0 },
1431 { "diskSizeRelative", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size_relative
), 0 },
1432 { "skeletonDirectory", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, skeleton_directory
), 0 },
1433 { "accessMode", JSON_VARIANT_UNSIGNED
, json_dispatch_access_mode
, offsetof(UserRecord
, access_mode
), 0 },
1434 { "tasksMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, tasks_max
), 0 },
1435 { "memoryHigh", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_high
), 0 },
1436 { "memoryMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_max
), 0 },
1437 { "cpuWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, cpu_weight
), 0 },
1438 { "ioWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, io_weight
), 0 },
1439 { "mountNoDevices", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nodev
), 0 },
1440 { "mountNoSuid", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nosuid
), 0 },
1441 { "mountNoExecute", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, noexec
), 0 },
1442 { "cifsDomain", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_domain
), JSON_SAFE
},
1443 { "cifsUserName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_user_name
), JSON_SAFE
},
1444 { "cifsService", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_service
), JSON_SAFE
},
1445 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, image_path
), 0 },
1446 { "homeDirectory", JSON_VARIANT_STRING
, json_dispatch_home_directory
, offsetof(UserRecord
, home_directory
), 0 },
1447 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
1448 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
1449 { "memberOf", JSON_VARIANT_ARRAY
, json_dispatch_user_group_list
, offsetof(UserRecord
, member_of
), JSON_RELAX
},
1450 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
1451 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
1452 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
1453 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
1454 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_discard
), 0 },
1455 { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_offline_discard
), 0 },
1456 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
1457 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
1458 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
1459 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_hash_algorithm
), JSON_SAFE
},
1460 { "luksPbkdfType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_type
), JSON_SAFE
},
1461 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_time_cost_usec
), 0 },
1462 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_memory_cost
), 0 },
1463 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_parallel_threads
), 0 },
1464 { "service", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, service
), JSON_SAFE
},
1465 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_interval_usec
), 0 },
1466 { "rateLimitBurst", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_burst
), 0 },
1467 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, enforce_password_policy
), 0 },
1468 { "autoLogin", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, auto_login
), 0 },
1469 { "stopDelayUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, stop_delay_usec
), 0 },
1470 { "killProcesses", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, kill_processes
), 0 },
1471 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_min_usec
), 0 },
1472 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_max_usec
), 0 },
1473 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_warn_usec
), 0 },
1474 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_inactive_usec
), 0 },
1475 { "passwordChangeNow", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, password_change_now
), 0 },
1476 { "pkcs11TokenUri", JSON_VARIANT_ARRAY
, dispatch_pkcs11_uri_array
, offsetof(UserRecord
, pkcs11_token_uri
), 0 },
1477 { "fido2HmacCredential", JSON_VARIANT_ARRAY
, dispatch_fido2_hmac_credential_array
, 0, 0 },
1479 { "secret", JSON_VARIANT_OBJECT
, dispatch_secret
, 0, 0 },
1480 { "privileged", JSON_VARIANT_OBJECT
, dispatch_privileged
, 0, 0 },
1482 /* Ignore the perMachine, binding, status stuff here, and process it later, so that it overrides whatever is set above */
1483 { "perMachine", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1484 { "binding", JSON_VARIANT_OBJECT
, NULL
, 0, 0 },
1485 { "status", JSON_VARIANT_OBJECT
, NULL
, 0, 0 },
1487 /* Ignore 'signature', we check it with explicit accessors instead */
1488 { "signature", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1492 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1498 /* Note that this call will leave a half-initialized record around on failure! */
1500 r
= user_group_record_mangle(v
, load_flags
, &h
->json
, &h
->mask
);
1504 r
= json_dispatch(h
->json
, user_dispatch_table
, NULL
, json_flags
, h
);
1508 /* During the parsing operation above we ignored the 'perMachine', 'binding' and 'status' fields,
1509 * since we want them to override the global options. Let's process them now. */
1511 r
= dispatch_per_machine("perMachine", json_variant_by_key(h
->json
, "perMachine"), json_flags
, h
);
1515 r
= dispatch_binding("binding", json_variant_by_key(h
->json
, "binding"), json_flags
, h
);
1519 r
= dispatch_status("status", json_variant_by_key(h
->json
, "status"), json_flags
, h
);
1523 if (FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
) && !h
->user_name
)
1524 return json_log(h
->json
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "User name field missing, refusing.");
1526 r
= user_record_augment(h
, json_flags
);
1533 int user_record_build(UserRecord
**ret
, ...) {
1534 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1535 _cleanup_(user_record_unrefp
) UserRecord
*u
= NULL
;
1542 r
= json_buildv(&v
, ap
);
1548 u
= user_record_new();
1552 r
= user_record_load(u
, v
, USER_RECORD_LOAD_FULL
);
1560 const char *user_record_user_name_and_realm(UserRecord
*h
) {
1563 /* Return the pre-initialized joined string if it is defined */
1564 if (h
->user_name_and_realm_auto
)
1565 return h
->user_name_and_realm_auto
;
1567 /* If it's not defined then we cannot have a realm */
1569 return h
->user_name
;
1572 UserStorage
user_record_storage(UserRecord
*h
) {
1575 if (h
->storage
>= 0)
1578 return USER_CLASSIC
;
1581 const char *user_record_file_system_type(UserRecord
*h
) {
1584 return h
->file_system_type
?: "ext4";
1587 const char *user_record_skeleton_directory(UserRecord
*h
) {
1590 return h
->skeleton_directory
?: "/etc/skel";
1593 mode_t
user_record_access_mode(UserRecord
*h
) {
1596 return h
->access_mode
!= (mode_t
) -1 ? h
->access_mode
: 0700;
1599 const char* user_record_home_directory(UserRecord
*h
) {
1602 if (h
->home_directory
)
1603 return h
->home_directory
;
1604 if (h
->home_directory_auto
)
1605 return h
->home_directory_auto
;
1607 /* The root user is special, hence be special about it */
1608 if (streq_ptr(h
->user_name
, "root"))
1614 const char *user_record_image_path(UserRecord
*h
) {
1618 return h
->image_path
;
1619 if (h
->image_path_auto
)
1620 return h
->image_path_auto
;
1622 return IN_SET(user_record_storage(h
), USER_CLASSIC
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
) ? user_record_home_directory(h
) : NULL
;
1625 const char *user_record_cifs_user_name(UserRecord
*h
) {
1628 return h
->cifs_user_name
?: h
->user_name
;
1631 unsigned long user_record_mount_flags(UserRecord
*h
) {
1634 return (h
->nosuid
? MS_NOSUID
: 0) |
1635 (h
->noexec
? MS_NOEXEC
: 0) |
1636 (h
->nodev
? MS_NODEV
: 0);
1639 const char *user_record_shell(UserRecord
*h
) {
1645 if (streq_ptr(h
->user_name
, "root"))
1648 if (user_record_disposition(h
) == USER_REGULAR
)
1654 const char *user_record_real_name(UserRecord
*h
) {
1657 return h
->real_name
?: h
->user_name
;
1660 bool user_record_luks_discard(UserRecord
*h
) {
1665 if (h
->luks_discard
>= 0)
1666 return h
->luks_discard
;
1668 ip
= user_record_image_path(h
);
1672 /* Use discard by default if we are referring to a real block device, but not when operating on a
1673 * loopback device. We want to optimize for SSD and flash storage after all, but we should be careful
1674 * when storing stuff on top of regular file systems in loopback files as doing discard then would
1675 * mean thin provisioning and we should not do that willy-nilly since it means we'll risk EIO later
1676 * on should the disk space to back our file systems not be available. */
1678 return path_startswith(ip
, "/dev/");
1681 bool user_record_luks_offline_discard(UserRecord
*h
) {
1686 if (h
->luks_offline_discard
>= 0)
1687 return h
->luks_offline_discard
;
1689 /* Discard while we are logged out should generally be a good idea, except when operating directly on
1690 * physical media, where we should just bind it to the online discard mode. */
1692 ip
= user_record_image_path(h
);
1696 if (path_startswith(ip
, "/dev/"))
1697 return user_record_luks_discard(h
);
1702 const char *user_record_luks_cipher(UserRecord
*h
) {
1705 return h
->luks_cipher
?: "aes";
1708 const char *user_record_luks_cipher_mode(UserRecord
*h
) {
1711 return h
->luks_cipher_mode
?: "xts-plain64";
1714 uint64_t user_record_luks_volume_key_size(UserRecord
*h
) {
1717 /* We return a value here that can be cast without loss into size_t which is what libcrypsetup expects */
1719 if (h
->luks_volume_key_size
== UINT64_MAX
)
1722 return MIN(h
->luks_volume_key_size
, SIZE_MAX
);
1725 const char* user_record_luks_pbkdf_type(UserRecord
*h
) {
1728 return h
->luks_pbkdf_type
?: "argon2i";
1731 uint64_t user_record_luks_pbkdf_time_cost_usec(UserRecord
*h
) {
1734 /* Returns a value with ms granularity, since that's what libcryptsetup expects */
1736 if (h
->luks_pbkdf_time_cost_usec
== UINT64_MAX
)
1737 return 500 * USEC_PER_MSEC
; /* We default to 500ms, in contrast to libcryptsetup's 2s, which is just awfully slow on every login */
1739 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_time_cost_usec
, USEC_PER_MSEC
), UINT32_MAX
) * USEC_PER_MSEC
;
1742 uint64_t user_record_luks_pbkdf_memory_cost(UserRecord
*h
) {
1745 /* Returns a value with kb granularity, since that's what libcryptsetup expects */
1747 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1748 return 64*1024*1024; /* We default to 64M, since this should work on smaller systems too */
1750 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_memory_cost
, 1024), UINT32_MAX
) * 1024;
1753 uint64_t user_record_luks_pbkdf_parallel_threads(UserRecord
*h
) {
1756 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1757 return 1; /* We default to 1, since this should work on smaller systems too */
1759 return MIN(h
->luks_pbkdf_parallel_threads
, UINT32_MAX
);
1762 const char *user_record_luks_pbkdf_hash_algorithm(UserRecord
*h
) {
1765 return h
->luks_pbkdf_hash_algorithm
?: "sha512";
1768 gid_t
user_record_gid(UserRecord
*h
) {
1771 if (gid_is_valid(h
->gid
))
1774 return (gid_t
) h
->uid
;
1777 UserDisposition
user_record_disposition(UserRecord
*h
) {
1780 if (h
->disposition
>= 0)
1781 return h
->disposition
;
1783 /* If not declared, derive from UID */
1785 if (!uid_is_valid(h
->uid
))
1786 return _USER_DISPOSITION_INVALID
;
1788 if (h
->uid
== 0 || h
->uid
== UID_NOBODY
)
1789 return USER_INTRINSIC
;
1791 if (uid_is_system(h
->uid
))
1794 if (uid_is_dynamic(h
->uid
))
1795 return USER_DYNAMIC
;
1797 if (uid_is_container(h
->uid
))
1798 return USER_CONTAINER
;
1800 if (h
->uid
> INT32_MAX
)
1801 return USER_RESERVED
;
1803 return USER_REGULAR
;
1806 int user_record_removable(UserRecord
*h
) {
1807 UserStorage storage
;
1810 if (h
->removable
>= 0)
1811 return h
->removable
;
1813 /* Refuse to decide for classic records */
1814 storage
= user_record_storage(h
);
1815 if (h
->storage
< 0 || h
->storage
== USER_CLASSIC
)
1818 /* For now consider only LUKS home directories with a reference by path as removable */
1819 return storage
== USER_LUKS
&& path_startswith(user_record_image_path(h
), "/dev/");
1822 uint64_t user_record_ratelimit_interval_usec(UserRecord
*h
) {
1825 if (h
->ratelimit_interval_usec
== UINT64_MAX
)
1826 return DEFAULT_RATELIMIT_INTERVAL_USEC
;
1828 return h
->ratelimit_interval_usec
;
1831 uint64_t user_record_ratelimit_burst(UserRecord
*h
) {
1834 if (h
->ratelimit_burst
== UINT64_MAX
)
1835 return DEFAULT_RATELIMIT_BURST
;
1837 return h
->ratelimit_burst
;
1840 bool user_record_can_authenticate(UserRecord
*h
) {
1843 /* Returns true if there's some form of property configured that the user can authenticate against */
1845 if (h
->n_pkcs11_encrypted_key
> 0)
1848 if (h
->n_fido2_hmac_salt
> 0)
1851 return !strv_isempty(h
->hashed_password
);
1854 uint64_t user_record_ratelimit_next_try(UserRecord
*h
) {
1857 /* Calculates when the it's possible to login next. Returns:
1859 * UINT64_MAX → Nothing known
1861 * Any other → Next time in CLOCK_REALTIME in usec (which could be in the past)
1864 if (h
->ratelimit_begin_usec
== UINT64_MAX
||
1865 h
->ratelimit_count
== UINT64_MAX
)
1868 if (h
->ratelimit_count
< user_record_ratelimit_burst(h
))
1871 return usec_add(h
->ratelimit_begin_usec
, user_record_ratelimit_interval_usec(h
));
1874 bool user_record_equal(UserRecord
*a
, UserRecord
*b
) {
1878 /* We assume that when a record is modified its JSON data is updated at the same time, hence it's
1879 * sufficient to compare the JSON data. */
1881 return json_variant_equal(a
->json
, b
->json
);
1884 bool user_record_compatible(UserRecord
*a
, UserRecord
*b
) {
1888 /* If either lacks a the regular section, we can't really decide, let's hence say they are
1890 if (!(a
->mask
& b
->mask
& USER_RECORD_REGULAR
))
1893 return streq_ptr(a
->user_name
, b
->user_name
) &&
1894 streq_ptr(a
->realm
, b
->realm
);
1897 int user_record_compare_last_change(UserRecord
*a
, UserRecord
*b
) {
1901 if (a
->last_change_usec
== b
->last_change_usec
)
1904 /* Always consider a record with a timestamp newer than one without */
1905 if (a
->last_change_usec
== UINT64_MAX
)
1907 if (b
->last_change_usec
== UINT64_MAX
)
1910 return CMP(a
->last_change_usec
, b
->last_change_usec
);
1913 int user_record_clone(UserRecord
*h
, UserRecordLoadFlags flags
, UserRecord
**ret
) {
1914 _cleanup_(user_record_unrefp
) UserRecord
*c
= NULL
;
1920 c
= user_record_new();
1924 r
= user_record_load(c
, h
->json
, flags
);
1932 int user_record_masked_equal(UserRecord
*a
, UserRecord
*b
, UserRecordMask mask
) {
1933 _cleanup_(user_record_unrefp
) UserRecord
*x
= NULL
, *y
= NULL
;
1939 /* Compares the two records, but ignores anything not listed in the specified mask */
1941 if ((a
->mask
& ~mask
) != 0) {
1942 r
= user_record_clone(a
, USER_RECORD_ALLOW(mask
) | USER_RECORD_STRIP(~mask
& _USER_RECORD_MASK_MAX
), &x
);
1949 if ((b
->mask
& ~mask
) != 0) {
1950 r
= user_record_clone(b
, USER_RECORD_ALLOW(mask
) | USER_RECORD_STRIP(~mask
& _USER_RECORD_MASK_MAX
), &y
);
1957 return user_record_equal(a
, b
);
1960 int user_record_test_blocked(UserRecord
*h
) {
1963 /* Checks whether access to the specified user shall be allowed at the moment. Returns:
1965 * -ESTALE: Record is from the future
1966 * -ENOLCK: Record is blocked
1967 * -EL2HLT: Record is not valid yet
1968 * -EL3HLT: Record is not valid anymore
1974 n
= now(CLOCK_REALTIME
);
1975 if (h
->last_change_usec
!= UINT64_MAX
&&
1976 h
->last_change_usec
> n
) /* Don't allow log ins when the record is from the future */
1982 if (h
->not_before_usec
!= UINT64_MAX
&& n
< h
->not_before_usec
)
1984 if (h
->not_after_usec
!= UINT64_MAX
&& n
> h
->not_after_usec
)
1990 int user_record_test_password_change_required(UserRecord
*h
) {
1991 bool change_permitted
;
1996 /* Checks whether the user must change the password when logging in
1998 -EKEYREVOKED: Change password now because admin said so
1999 -EOWNERDEAD: Change password now because it expired
2000 -EKEYREJECTED: Password is expired, no changing is allowed
2001 -EKEYEXPIRED: Password is about to expire, warn user
2002 -ENETDOWN: Record has expiration info but no password change timestamp
2003 -EROFS: No password change required nor permitted
2004 0: No password change required, but permitted
2007 /* If a password change request has been set explicitly, it overrides everything */
2008 if (h
->password_change_now
> 0)
2009 return -EKEYREVOKED
;
2011 n
= now(CLOCK_REALTIME
);
2013 /* Then, let's check if password changing is currently allowed at all */
2014 if (h
->password_change_min_usec
!= UINT64_MAX
) {
2016 /* Expiry configured but no password change timestamp known? */
2017 if (h
->last_password_change_usec
== UINT64_MAX
)
2020 if (h
->password_change_min_usec
>= UINT64_MAX
- h
->last_password_change_usec
)
2021 change_permitted
= false;
2023 change_permitted
= n
>= h
->last_password_change_usec
+ h
->password_change_min_usec
;
2026 change_permitted
= true;
2028 /* Let's check whether the password has expired. */
2029 if (!(h
->password_change_max_usec
== UINT64_MAX
||
2030 h
->password_change_max_usec
>= UINT64_MAX
- h
->last_password_change_usec
)) {
2032 uint64_t change_before
;
2034 /* Expiry configured but no password change timestamp known? */
2035 if (h
->last_password_change_usec
== UINT64_MAX
)
2038 /* Password is in inactive phase? */
2039 if (h
->password_change_inactive_usec
!= UINT64_MAX
&&
2040 h
->password_change_inactive_usec
< UINT64_MAX
- h
->password_change_max_usec
) {
2043 added
= h
->password_change_inactive_usec
+ h
->password_change_max_usec
;
2044 if (added
< UINT64_MAX
- h
->last_password_change_usec
&&
2045 n
>= h
->last_password_change_usec
+ added
)
2046 return -EKEYREJECTED
;
2049 /* Password needs to be changed now? */
2050 change_before
= h
->last_password_change_usec
+ h
->password_change_max_usec
;
2051 if (n
>= change_before
)
2052 return change_permitted
? -EOWNERDEAD
: -EKEYREJECTED
;
2055 if (h
->password_change_warn_usec
!= UINT64_MAX
&&
2056 (change_before
< h
->password_change_warn_usec
||
2057 n
>= change_before
- h
->password_change_warn_usec
))
2058 return change_permitted
? -EKEYEXPIRED
: -EROFS
;
2061 /* No password changing necessary */
2062 return change_permitted
? 0 : -EROFS
;
2065 static const char* const user_storage_table
[_USER_STORAGE_MAX
] = {
2066 [USER_CLASSIC
] = "classic",
2067 [USER_LUKS
] = "luks",
2068 [USER_DIRECTORY
] = "directory",
2069 [USER_SUBVOLUME
] = "subvolume",
2070 [USER_FSCRYPT
] = "fscrypt",
2071 [USER_CIFS
] = "cifs",
2074 DEFINE_STRING_TABLE_LOOKUP(user_storage
, UserStorage
);
2076 static const char* const user_disposition_table
[_USER_DISPOSITION_MAX
] = {
2077 [USER_INTRINSIC
] = "intrinsic",
2078 [USER_SYSTEM
] = "system",
2079 [USER_DYNAMIC
] = "dynamic",
2080 [USER_REGULAR
] = "regular",
2081 [USER_CONTAINER
] = "container",
2082 [USER_RESERVED
] = "reserved",
2085 DEFINE_STRING_TABLE_LOOKUP(user_disposition
, UserDisposition
);