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,
89 static void pkcs11_encrypted_key_done(Pkcs11EncryptedKey
*k
) {
94 erase_and_free(k
->data
);
95 erase_and_free(k
->hashed_password
);
98 static UserRecord
* user_record_free(UserRecord
*h
) {
104 free(h
->user_name_and_realm_auto
);
106 free(h
->email_address
);
107 erase_and_free(h
->password_hint
);
113 strv_free(h
->environment
);
115 free(h
->preferred_language
);
116 rlimit_free_all(h
->rlimits
);
118 free(h
->skeleton_directory
);
120 strv_free_erase(h
->hashed_password
);
121 strv_free_erase(h
->ssh_authorized_keys
);
122 strv_free_erase(h
->password
);
123 strv_free_erase(h
->pkcs11_pin
);
125 free(h
->cifs_service
);
126 free(h
->cifs_user_name
);
127 free(h
->cifs_domain
);
130 free(h
->image_path_auto
);
131 free(h
->home_directory
);
132 free(h
->home_directory_auto
);
134 strv_free(h
->member_of
);
136 free(h
->file_system_type
);
137 free(h
->luks_cipher
);
138 free(h
->luks_cipher_mode
);
139 free(h
->luks_pbkdf_hash_algorithm
);
140 free(h
->luks_pbkdf_type
);
145 strv_free(h
->pkcs11_token_uri
);
146 for (size_t i
= 0; i
< h
->n_pkcs11_encrypted_key
; i
++)
147 pkcs11_encrypted_key_done(h
->pkcs11_encrypted_key
+ i
);
148 free(h
->pkcs11_encrypted_key
);
150 json_variant_unref(h
->json
);
155 DEFINE_TRIVIAL_REF_UNREF_FUNC(UserRecord
, user_record
, user_record_free
);
157 int json_dispatch_realm(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
162 if (json_variant_is_null(variant
)) {
167 if (!json_variant_is_string(variant
))
168 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
170 n
= json_variant_string(variant
);
171 r
= dns_name_is_valid(n
);
173 return json_log(variant
, flags
, r
, "Failed to check if JSON field '%s' is a valid DNS domain.", strna(name
));
175 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid DNS domain.", strna(name
));
177 r
= free_and_strdup(s
, n
);
179 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
184 static int json_dispatch_gecos(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
189 if (json_variant_is_null(variant
)) {
194 if (!json_variant_is_string(variant
))
195 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
197 n
= json_variant_string(variant
);
199 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid GECOS compatible real name.", strna(name
));
201 r
= free_and_strdup(s
, n
);
203 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
208 static int json_dispatch_nice(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
212 if (json_variant_is_null(variant
)) {
217 if (!json_variant_is_integer(variant
))
218 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
220 m
= json_variant_integer(variant
);
221 if (m
< PRIO_MIN
|| m
>= PRIO_MAX
)
222 return json_log(variant
, flags
, SYNTHETIC_ERRNO(ERANGE
), "JSON field '%s' is not a valid nice level.", strna(name
));
228 static int json_dispatch_rlimit_value(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
229 rlim_t
*ret
= userdata
;
231 if (json_variant_is_null(variant
))
232 *ret
= RLIM_INFINITY
;
233 else if (json_variant_is_unsigned(variant
)) {
236 w
= json_variant_unsigned(variant
);
237 if (w
== RLIM_INFINITY
|| (uintmax_t) w
!= json_variant_unsigned(variant
))
238 return json_log(variant
, flags
, SYNTHETIC_ERRNO(ERANGE
), "Resource limit value '%s' is out of range.", name
);
242 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit value '%s' is not an unsigned integer.", name
);
247 static int json_dispatch_rlimits(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
248 struct rlimit
** limits
= userdata
;
255 if (json_variant_is_null(variant
)) {
256 rlimit_free_all(limits
);
260 if (!json_variant_is_object(variant
))
261 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
263 JSON_VARIANT_OBJECT_FOREACH(key
, value
, variant
) {
264 JsonVariant
*jcur
, *jmax
;
269 p
= startswith(key
, "RLIMIT_");
273 l
= rlimit_from_string(p
);
275 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' not known.", key
);
277 if (!json_variant_is_object(value
))
278 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' has invalid value.", key
);
280 if (json_variant_elements(value
) != 4)
281 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' value is does not have two fields as expected.", key
);
283 jcur
= json_variant_by_key(value
, "cur");
285 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' lacks 'cur' field.", key
);
286 r
= json_dispatch_rlimit_value("cur", jcur
, flags
, &rl
.rlim_cur
);
290 jmax
= json_variant_by_key(value
, "max");
292 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' lacks 'max' field.", key
);
293 r
= json_dispatch_rlimit_value("max", jmax
, flags
, &rl
.rlim_max
);
300 limits
[l
] = newdup(struct rlimit
, &rl
, 1);
309 static int json_dispatch_filename_or_path(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 if (!filename_is_valid(n
) && !path_is_normalized(n
))
326 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid file name or normalized path.", strna(name
));
328 r
= free_and_strdup(s
, n
);
330 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
335 static int json_dispatch_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
340 if (json_variant_is_null(variant
)) {
345 if (!json_variant_is_string(variant
))
346 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
348 n
= json_variant_string(variant
);
349 if (!path_is_normalized(n
))
350 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a normalized file system path.", strna(name
));
351 if (!path_is_absolute(n
))
352 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an absolute file system path.", strna(name
));
354 r
= free_and_strdup(s
, n
);
356 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
361 static int json_dispatch_home_directory(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
366 if (json_variant_is_null(variant
)) {
371 if (!json_variant_is_string(variant
))
372 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
374 n
= json_variant_string(variant
);
376 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid home directory path.", strna(name
));
378 r
= free_and_strdup(s
, n
);
380 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
385 static int json_dispatch_image_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
390 if (json_variant_is_null(variant
)) {
395 if (!json_variant_is_string(variant
))
396 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
398 n
= json_variant_string(variant
);
399 if (empty_or_root(n
) || !path_is_valid(n
) || !path_is_absolute(n
))
400 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid image path.", strna(name
));
402 r
= free_and_strdup(s
, n
);
404 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
409 static int json_dispatch_umask(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
410 mode_t
*m
= userdata
;
413 if (json_variant_is_null(variant
)) {
418 if (!json_variant_is_unsigned(variant
))
419 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a number.", strna(name
));
421 k
= json_variant_unsigned(variant
);
423 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' outside of valid range 0…0777.", strna(name
));
429 static int json_dispatch_access_mode(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
430 mode_t
*m
= userdata
;
433 if (json_variant_is_null(variant
)) {
438 if (!json_variant_is_unsigned(variant
))
439 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a number.", strna(name
));
441 k
= json_variant_unsigned(variant
);
443 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' outside of valid range 0…07777.", strna(name
));
449 static int json_dispatch_environment(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
450 _cleanup_strv_free_
char **n
= NULL
;
451 char ***l
= userdata
;
455 if (json_variant_is_null(variant
)) {
460 if (!json_variant_is_array(variant
))
461 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array.", strna(name
));
463 for (i
= 0; i
< json_variant_elements(variant
); i
++) {
464 _cleanup_free_
char *c
= NULL
;
468 e
= json_variant_by_index(variant
, i
);
469 if (!json_variant_is_string(e
))
470 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of strings.", strna(name
));
472 assert_se(a
= json_variant_string(e
));
474 if (!env_assignment_is_valid(a
))
475 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of environment variables.", strna(name
));
479 return json_log_oom(variant
, flags
);
481 r
= strv_env_replace(&n
, c
);
483 return json_log_oom(variant
, flags
);
488 strv_free_and_replace(*l
, n
);
492 int json_dispatch_user_disposition(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
493 UserDisposition
*disposition
= userdata
, k
;
495 if (json_variant_is_null(variant
)) {
496 *disposition
= _USER_DISPOSITION_INVALID
;
500 if (!json_variant_is_string(variant
))
501 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
503 k
= user_disposition_from_string(json_variant_string(variant
));
505 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Disposition type '%s' not known.", json_variant_string(variant
));
511 static int json_dispatch_storage(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
512 UserStorage
*storage
= userdata
, k
;
514 if (json_variant_is_null(variant
)) {
515 *storage
= _USER_STORAGE_INVALID
;
519 if (!json_variant_is_string(variant
))
520 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
522 k
= user_storage_from_string(json_variant_string(variant
));
524 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Storage type '%s' not known.", json_variant_string(variant
));
530 static int json_dispatch_disk_size(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
531 uint64_t *size
= userdata
;
534 if (json_variant_is_null(variant
)) {
539 if (!json_variant_is_unsigned(variant
))
540 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an integer.", strna(name
));
542 k
= json_variant_unsigned(variant
);
543 if (k
< USER_DISK_SIZE_MIN
|| k
> USER_DISK_SIZE_MAX
)
544 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
);
550 static int json_dispatch_tasks_or_memory_max(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
551 uint64_t *limit
= userdata
;
554 if (json_variant_is_null(variant
)) {
559 if (!json_variant_is_unsigned(variant
))
560 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a integer.", strna(name
));
562 k
= json_variant_unsigned(variant
);
563 if (k
<= 0 || k
>= UINT64_MAX
)
564 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);
570 static int json_dispatch_weight(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
571 uint64_t *weight
= userdata
;
574 if (json_variant_is_null(variant
)) {
575 *weight
= UINT64_MAX
;
579 if (!json_variant_is_unsigned(variant
))
580 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a integer.", strna(name
));
582 k
= json_variant_unsigned(variant
);
583 if (k
<= CGROUP_WEIGHT_MIN
|| k
>= CGROUP_WEIGHT_MAX
)
584 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
);
590 int json_dispatch_user_group_list(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
591 _cleanup_strv_free_
char **l
= NULL
;
592 char ***list
= userdata
;
596 if (!json_variant_is_array(variant
))
597 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of strings.", strna(name
));
599 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
601 if (!json_variant_is_string(e
))
602 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
604 if (!valid_user_group_name(json_variant_string(e
), FLAGS_SET(flags
, JSON_RELAX
) ? VALID_USER_RELAX
: 0))
605 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a valid user/group name: %s", json_variant_string(e
));
607 r
= strv_extend(&l
, json_variant_string(e
));
609 return json_log(e
, flags
, r
, "Failed to append array element: %m");
612 r
= strv_extend_strv(list
, l
, true);
614 return json_log(variant
, flags
, r
, "Failed to merge user/group arrays: %m");
619 static int dispatch_secret(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
621 static const JsonDispatch secret_dispatch_table
[] = {
622 { "password", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, password
), 0 },
623 { "pkcs11Pin", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, pkcs11_pin
), 0 },
624 { "pkcs11ProtectedAuthenticationPathPermitted", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, pkcs11_protected_authentication_path_permitted
), 0 },
628 return json_dispatch(variant
, secret_dispatch_table
, NULL
, flags
, userdata
);
631 static int dispatch_pkcs11_uri(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
636 if (json_variant_is_null(variant
)) {
641 if (!json_variant_is_string(variant
))
642 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
644 n
= json_variant_string(variant
);
645 if (!pkcs11_uri_valid(n
))
646 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name
));
648 r
= free_and_strdup(s
, n
);
650 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
655 static int dispatch_pkcs11_uri_array(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
656 _cleanup_strv_free_
char **z
= NULL
;
657 char ***l
= userdata
;
661 if (json_variant_is_null(variant
)) {
666 if (json_variant_is_string(variant
)) {
669 n
= json_variant_string(variant
);
670 if (!pkcs11_uri_valid(n
))
671 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name
));
679 if (!json_variant_is_array(variant
))
680 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string or array of strings.", strna(name
));
682 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
685 if (!json_variant_is_string(e
))
686 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
688 n
= json_variant_string(e
);
689 if (!pkcs11_uri_valid(n
))
690 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
);
692 r
= strv_extend(&z
, n
);
698 strv_free_and_replace(*l
, z
);
702 static int dispatch_pkcs11_key_data(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
703 Pkcs11EncryptedKey
*k
= userdata
;
708 if (json_variant_is_null(variant
)) {
709 k
->data
= erase_and_free(k
->data
);
714 if (!json_variant_is_string(variant
))
715 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
717 r
= unbase64mem(json_variant_string(variant
), (size_t) -1, &b
, &l
);
719 return json_log(variant
, flags
, r
, "Failed to decode encrypted PKCS#11 key: %m");
721 erase_and_free(k
->data
);
728 static int dispatch_pkcs11_key(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
729 UserRecord
*h
= userdata
;
733 if (!json_variant_is_array(variant
))
734 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
736 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
737 Pkcs11EncryptedKey
*array
, *k
;
739 static const JsonDispatch pkcs11_key_dispatch_table
[] = {
740 { "uri", JSON_VARIANT_STRING
, dispatch_pkcs11_uri
, offsetof(Pkcs11EncryptedKey
, uri
), JSON_MANDATORY
},
741 { "data", JSON_VARIANT_STRING
, dispatch_pkcs11_key_data
, 0, JSON_MANDATORY
},
742 { "hashedPassword", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(Pkcs11EncryptedKey
, hashed_password
), JSON_MANDATORY
},
746 if (!json_variant_is_object(e
))
747 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
749 array
= reallocarray(h
->pkcs11_encrypted_key
, h
->n_pkcs11_encrypted_key
+ 1, sizeof(Pkcs11EncryptedKey
));
753 h
->pkcs11_encrypted_key
= array
;
754 k
= h
->pkcs11_encrypted_key
+ h
->n_pkcs11_encrypted_key
;
755 *k
= (Pkcs11EncryptedKey
) {};
757 r
= json_dispatch(e
, pkcs11_key_dispatch_table
, NULL
, flags
, k
);
759 pkcs11_encrypted_key_done(k
);
763 h
->n_pkcs11_encrypted_key
++;
769 static int dispatch_privileged(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
771 static const JsonDispatch privileged_dispatch_table
[] = {
772 { "passwordHint", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, password_hint
), 0 },
773 { "hashedPassword", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, hashed_password
), JSON_SAFE
},
774 { "sshAuthorizedKeys", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, ssh_authorized_keys
), 0 },
775 { "pkcs11EncryptedKey", JSON_VARIANT_ARRAY
, dispatch_pkcs11_key
, 0, 0 },
779 return json_dispatch(variant
, privileged_dispatch_table
, NULL
, flags
, userdata
);
782 static int dispatch_binding(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
784 static const JsonDispatch binding_dispatch_table
[] = {
785 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_image_path
, offsetof(UserRecord
, image_path
), 0 },
786 { "homeDirectory", JSON_VARIANT_STRING
, json_dispatch_home_directory
, offsetof(UserRecord
, home_directory
), 0 },
787 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
788 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
789 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
790 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
791 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
792 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
793 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
794 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
795 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
796 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
800 char smid
[SD_ID128_STRING_MAX
];
808 if (!json_variant_is_object(variant
))
809 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
811 r
= sd_id128_get_machine(&mid
);
813 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
815 m
= json_variant_by_key(variant
, sd_id128_to_string(mid
, smid
));
819 return json_dispatch(m
, binding_dispatch_table
, NULL
, flags
, userdata
);
822 int per_machine_id_match(JsonVariant
*ids
, JsonDispatchFlags flags
) {
826 r
= sd_id128_get_machine(&mid
);
828 return json_log(ids
, flags
, r
, "Failed to acquire machine ID: %m");
830 if (json_variant_is_string(ids
)) {
833 r
= sd_id128_from_string(json_variant_string(ids
), &k
);
835 json_log(ids
, flags
, r
, "%s is not a valid machine ID, ignoring: %m", json_variant_string(ids
));
839 return sd_id128_equal(mid
, k
);
842 if (json_variant_is_array(ids
)) {
845 JSON_VARIANT_ARRAY_FOREACH(e
, ids
) {
848 if (!json_variant_is_string(e
)) {
849 json_log(e
, flags
, 0, "Machine ID is not a string, ignoring: %m");
853 r
= sd_id128_from_string(json_variant_string(e
), &k
);
855 json_log(e
, flags
, r
, "%s is not a valid machine ID, ignoring: %m", json_variant_string(e
));
859 if (sd_id128_equal(mid
, k
))
866 json_log(ids
, flags
, 0, "Machine ID is not a string or array of strings, ignoring: %m");
870 int per_machine_hostname_match(JsonVariant
*hns
, JsonDispatchFlags flags
) {
871 _cleanup_free_
char *hn
= NULL
;
874 r
= gethostname_strict(&hn
);
876 json_log(hns
, flags
, r
, "No hostname set, not matching perMachine hostname record: %m");
880 return json_log(hns
, flags
, r
, "Failed to acquire hostname: %m");
882 if (json_variant_is_string(hns
))
883 return streq(json_variant_string(hns
), hn
);
885 if (json_variant_is_array(hns
)) {
888 JSON_VARIANT_ARRAY_FOREACH(e
, hns
) {
890 if (!json_variant_is_string(e
)) {
891 json_log(e
, flags
, 0, "Hostname is not a string, ignoring: %m");
895 if (streq(json_variant_string(hns
), hn
))
902 json_log(hns
, flags
, 0, "Hostname is not a string or array of strings, ignoring: %m");
906 static int dispatch_per_machine(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
908 static const JsonDispatch per_machine_dispatch_table
[] = {
909 { "matchMachineId", _JSON_VARIANT_TYPE_INVALID
, NULL
, 0, 0 },
910 { "matchHostname", _JSON_VARIANT_TYPE_INVALID
, NULL
, 0, 0 },
911 { "iconName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, icon_name
), JSON_SAFE
},
912 { "location", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, location
), 0 },
913 { "shell", JSON_VARIANT_STRING
, json_dispatch_filename_or_path
, offsetof(UserRecord
, shell
), 0 },
914 { "umask", JSON_VARIANT_UNSIGNED
, json_dispatch_umask
, offsetof(UserRecord
, umask
), 0 },
915 { "environment", JSON_VARIANT_ARRAY
, json_dispatch_environment
, offsetof(UserRecord
, environment
), 0 },
916 { "timeZone", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, time_zone
), JSON_SAFE
},
917 { "preferredLanguage", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, preferred_language
), JSON_SAFE
},
918 { "niceLevel", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_nice
, offsetof(UserRecord
, nice_level
), 0 },
919 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_rlimits
, offsetof(UserRecord
, rlimits
), 0 },
920 { "locked", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, locked
), 0 },
921 { "notBeforeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_before_usec
), 0 },
922 { "notAfterUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_after_usec
), 0 },
923 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
924 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_disk_size
, offsetof(UserRecord
, disk_size
), 0 },
925 { "diskSizeRelative", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size_relative
), 0 },
926 { "skeletonDirectory", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, skeleton_directory
), 0 },
927 { "accessMode", JSON_VARIANT_UNSIGNED
, json_dispatch_access_mode
, offsetof(UserRecord
, access_mode
), 0 },
928 { "tasksMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, tasks_max
), 0 },
929 { "memoryHigh", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_high
), 0 },
930 { "memoryMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_max
), 0 },
931 { "cpuWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, cpu_weight
), 0 },
932 { "ioWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, io_weight
), 0 },
933 { "mountNoDevices", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nodev
), 0 },
934 { "mountNoSuid", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nosuid
), 0 },
935 { "mountNoExecute", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, noexec
), 0 },
936 { "cifsDomain", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_domain
), JSON_SAFE
},
937 { "cifsUserName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_user_name
), JSON_SAFE
},
938 { "cifsService", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_service
), JSON_SAFE
},
939 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, image_path
), 0 },
940 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
941 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
942 { "memberOf", JSON_VARIANT_ARRAY
, json_dispatch_user_group_list
, offsetof(UserRecord
, member_of
), JSON_RELAX
},
943 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
944 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
945 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
946 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
947 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_discard
), 0, },
948 { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_offline_discard
), 0, },
949 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
950 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
951 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
952 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_hash_algorithm
), JSON_SAFE
},
953 { "luksPbkdfType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_type
), JSON_SAFE
},
954 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_time_cost_usec
), 0 },
955 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_memory_cost
), 0 },
956 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_parallel_threads
), 0 },
957 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_interval_usec
), 0 },
958 { "rateLimitBurst", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_burst
), 0 },
959 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, enforce_password_policy
), 0 },
960 { "autoLogin", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, auto_login
), 0 },
961 { "stopDelayUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, stop_delay_usec
), 0 },
962 { "killProcesses", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, kill_processes
), 0 },
963 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_min_usec
), 0 },
964 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_max_usec
), 0 },
965 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_warn_usec
), 0 },
966 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_inactive_usec
), 0 },
967 { "passwordChangeNow", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, password_change_now
), 0 },
968 { "pkcs11TokenUri", JSON_VARIANT_ARRAY
, dispatch_pkcs11_uri_array
, offsetof(UserRecord
, pkcs11_token_uri
), 0 },
978 if (!json_variant_is_array(variant
))
979 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array.", strna(name
));
981 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
982 bool matching
= false;
985 if (!json_variant_is_object(e
))
986 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
988 m
= json_variant_by_key(e
, "matchMachineId");
990 r
= per_machine_id_match(m
, flags
);
998 m
= json_variant_by_key(e
, "matchHostname");
1000 r
= per_machine_hostname_match(m
, flags
);
1011 r
= json_dispatch(e
, per_machine_dispatch_table
, NULL
, flags
, userdata
);
1019 static int dispatch_status(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1021 static const JsonDispatch status_dispatch_table
[] = {
1022 { "diskUsage", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_usage
), 0 },
1023 { "diskFree", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_free
), 0 },
1024 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size
), 0 },
1025 { "diskCeiling", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_ceiling
), 0 },
1026 { "diskFloor", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_floor
), 0 },
1027 { "state", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, state
), JSON_SAFE
},
1028 { "service", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, service
), JSON_SAFE
},
1029 { "signedLocally", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, signed_locally
), 0 },
1030 { "goodAuthenticationCounter", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, good_authentication_counter
), 0 },
1031 { "badAuthenticationCounter", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, bad_authentication_counter
), 0 },
1032 { "lastGoodAuthenticationUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_good_authentication_usec
), 0 },
1033 { "lastBadAuthenticationUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_bad_authentication_usec
), 0 },
1034 { "rateLimitBeginUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_begin_usec
), 0 },
1035 { "rateLimitCount", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_count
), 0 },
1036 { "removable", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, removable
), 0 },
1040 char smid
[SD_ID128_STRING_MAX
];
1048 if (!json_variant_is_object(variant
))
1049 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
1051 r
= sd_id128_get_machine(&mid
);
1053 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
1055 m
= json_variant_by_key(variant
, sd_id128_to_string(mid
, smid
));
1059 return json_dispatch(m
, status_dispatch_table
, NULL
, flags
, userdata
);
1062 int user_record_build_image_path(UserStorage storage
, const char *user_name_and_realm
, char **ret
) {
1066 assert(storage
>= 0);
1067 assert(user_name_and_realm
);
1070 if (storage
== USER_LUKS
)
1072 else if (IN_SET(storage
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
))
1073 suffix
= ".homedir";
1079 z
= strjoin("/home/", user_name_and_realm
, suffix
);
1087 static int user_record_augment(UserRecord
*h
, JsonDispatchFlags json_flags
) {
1092 if (!FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
))
1095 assert(h
->user_name
);
1097 if (!h
->user_name_and_realm_auto
&& h
->realm
) {
1098 h
->user_name_and_realm_auto
= strjoin(h
->user_name
, "@", h
->realm
);
1099 if (!h
->user_name_and_realm_auto
)
1100 return json_log_oom(h
->json
, json_flags
);
1103 /* Let's add in the following automatisms only for regular users, they don't make sense for any others */
1104 if (user_record_disposition(h
) != USER_REGULAR
)
1107 if (!h
->home_directory
&& !h
->home_directory_auto
) {
1108 h
->home_directory_auto
= path_join("/home/", h
->user_name
);
1109 if (!h
->home_directory_auto
)
1110 return json_log_oom(h
->json
, json_flags
);
1113 if (!h
->image_path
&& !h
->image_path_auto
) {
1114 r
= user_record_build_image_path(user_record_storage(h
), user_record_user_name_and_realm(h
), &h
->image_path_auto
);
1116 return json_log(h
->json
, json_flags
, r
, "Failed to determine default image path: %m");
1122 int user_group_record_mangle(
1124 UserRecordLoadFlags load_flags
,
1125 JsonVariant
**ret_variant
,
1126 UserRecordMask
*ret_mask
) {
1128 static const struct {
1129 UserRecordMask mask
;
1132 { USER_RECORD_PRIVILEGED
, "privileged" },
1133 { USER_RECORD_SECRET
, "secret" },
1134 { USER_RECORD_BINDING
, "binding" },
1135 { USER_RECORD_PER_MACHINE
, "perMachine" },
1136 { USER_RECORD_STATUS
, "status" },
1137 { USER_RECORD_SIGNATURE
, "signature" },
1140 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1141 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
;
1142 JsonVariant
*array
[ELEMENTSOF(mask_field
) * 2];
1143 size_t n_retain
= 0, i
;
1144 UserRecordMask m
= 0;
1147 assert((load_flags
& _USER_RECORD_MASK_MAX
) == 0); /* detect mistakes when accidentally passing
1148 * UserRecordMask bit masks as UserRecordLoadFlags
1152 assert(ret_variant
);
1155 /* Note that this function is shared with the group record parser, hence we try to be generic in our
1156 * log message wording here, to cover both cases. */
1158 if (!json_variant_is_object(v
))
1159 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is not a JSON object, refusing.");
1161 if (USER_RECORD_ALLOW_MASK(load_flags
) == 0) /* allow nothing? */
1162 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "Nothing allowed in record, refusing.");
1164 if (USER_RECORD_STRIP_MASK(load_flags
) == _USER_RECORD_MASK_MAX
) /* strip everything? */
1165 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "Stripping everything from record, refusing.");
1167 /* Check if we have the special sections and if they match our flags set */
1168 for (i
= 0; i
< ELEMENTSOF(mask_field
); i
++) {
1171 if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags
), mask_field
[i
].mask
)) {
1173 w
= json_variant_ref(v
);
1175 r
= json_variant_filter(&w
, STRV_MAKE(mask_field
[i
].name
));
1177 return json_log(w
, json_flags
, r
, "Failed to remove field from variant: %m");
1182 e
= json_variant_by_key_full(v
, mask_field
[i
].name
, &k
);
1184 if (!FLAGS_SET(USER_RECORD_ALLOW_MASK(load_flags
), mask_field
[i
].mask
))
1185 return json_log(e
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record contains '%s' field, which is not allowed.", mask_field
[i
].name
);
1187 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1188 array
[n_retain
++] = k
;
1189 array
[n_retain
++] = e
;
1192 m
|= mask_field
[i
].mask
;
1194 if (FLAGS_SET(USER_RECORD_REQUIRE_MASK(load_flags
), mask_field
[i
].mask
))
1195 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record lacks '%s' field, which is required.", mask_field
[i
].name
);
1199 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1200 /* If we are supposed to strip regular items, then let's instead just allocate a new object
1201 * with just the stuff we need. */
1203 w
= json_variant_unref(w
);
1204 r
= json_variant_new_object(&w
, array
, n_retain
);
1206 return json_log(v
, json_flags
, r
, "Failed to allocate new object: %m");
1208 /* And now check if there's anything else in the record */
1209 for (i
= 0; i
< json_variant_elements(v
); i
+= 2) {
1211 bool special
= false;
1214 assert_se(f
= json_variant_string(json_variant_by_index(v
, i
)));
1216 for (j
= 0; j
< ELEMENTSOF(mask_field
); j
++)
1217 if (streq(f
, mask_field
[j
].name
)) { /* already covered in the loop above */
1223 if ((load_flags
& (USER_RECORD_ALLOW_REGULAR
|USER_RECORD_REQUIRE_REGULAR
)) == 0)
1224 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record contains '%s' field, which is not allowed.", f
);
1226 m
|= USER_RECORD_REGULAR
;
1232 if (FLAGS_SET(load_flags
, USER_RECORD_REQUIRE_REGULAR
) && !FLAGS_SET(m
, USER_RECORD_REGULAR
))
1233 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record lacks basic identity fields, which are required.");
1236 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is empty.");
1239 *ret_variant
= TAKE_PTR(w
);
1241 *ret_variant
= json_variant_ref(v
);
1247 int user_record_load(UserRecord
*h
, JsonVariant
*v
, UserRecordLoadFlags load_flags
) {
1249 static const JsonDispatch user_dispatch_table
[] = {
1250 { "userName", JSON_VARIANT_STRING
, json_dispatch_user_group_name
, offsetof(UserRecord
, user_name
), JSON_RELAX
},
1251 { "realm", JSON_VARIANT_STRING
, json_dispatch_realm
, offsetof(UserRecord
, realm
), 0 },
1252 { "realName", JSON_VARIANT_STRING
, json_dispatch_gecos
, offsetof(UserRecord
, real_name
), 0 },
1253 { "emailAddress", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, email_address
), JSON_SAFE
},
1254 { "iconName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, icon_name
), JSON_SAFE
},
1255 { "location", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, location
), 0 },
1256 { "disposition", JSON_VARIANT_STRING
, json_dispatch_user_disposition
, offsetof(UserRecord
, disposition
), 0 },
1257 { "lastChangeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_change_usec
), 0 },
1258 { "lastPasswordChangeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_password_change_usec
), 0 },
1259 { "shell", JSON_VARIANT_STRING
, json_dispatch_filename_or_path
, offsetof(UserRecord
, shell
), 0 },
1260 { "umask", JSON_VARIANT_UNSIGNED
, json_dispatch_umask
, offsetof(UserRecord
, umask
), 0 },
1261 { "environment", JSON_VARIANT_ARRAY
, json_dispatch_environment
, offsetof(UserRecord
, environment
), 0 },
1262 { "timeZone", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, time_zone
), JSON_SAFE
},
1263 { "preferredLanguage", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, preferred_language
), JSON_SAFE
},
1264 { "niceLevel", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_nice
, offsetof(UserRecord
, nice_level
), 0 },
1265 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_rlimits
, offsetof(UserRecord
, rlimits
), 0 },
1266 { "locked", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, locked
), 0 },
1267 { "notBeforeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_before_usec
), 0 },
1268 { "notAfterUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_after_usec
), 0 },
1269 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
1270 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_disk_size
, offsetof(UserRecord
, disk_size
), 0 },
1271 { "diskSizeRelative", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size_relative
), 0 },
1272 { "skeletonDirectory", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, skeleton_directory
), 0 },
1273 { "accessMode", JSON_VARIANT_UNSIGNED
, json_dispatch_access_mode
, offsetof(UserRecord
, access_mode
), 0 },
1274 { "tasksMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, tasks_max
), 0 },
1275 { "memoryHigh", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_high
), 0 },
1276 { "memoryMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_max
), 0 },
1277 { "cpuWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, cpu_weight
), 0 },
1278 { "ioWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, io_weight
), 0 },
1279 { "mountNoDevices", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nodev
), 0 },
1280 { "mountNoSuid", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nosuid
), 0 },
1281 { "mountNoExecute", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, noexec
), 0 },
1282 { "cifsDomain", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_domain
), JSON_SAFE
},
1283 { "cifsUserName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_user_name
), JSON_SAFE
},
1284 { "cifsService", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_service
), JSON_SAFE
},
1285 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, image_path
), 0 },
1286 { "homeDirectory", JSON_VARIANT_STRING
, json_dispatch_home_directory
, offsetof(UserRecord
, home_directory
), 0 },
1287 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
1288 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
1289 { "memberOf", JSON_VARIANT_ARRAY
, json_dispatch_user_group_list
, offsetof(UserRecord
, member_of
), JSON_RELAX
},
1290 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
1291 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
1292 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
1293 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
1294 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_discard
), 0 },
1295 { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_offline_discard
), 0 },
1296 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
1297 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
1298 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
1299 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_hash_algorithm
), JSON_SAFE
},
1300 { "luksPbkdfType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_type
), JSON_SAFE
},
1301 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_time_cost_usec
), 0 },
1302 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_memory_cost
), 0 },
1303 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_parallel_threads
), 0 },
1304 { "service", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, service
), JSON_SAFE
},
1305 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_interval_usec
), 0 },
1306 { "rateLimitBurst", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_burst
), 0 },
1307 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, enforce_password_policy
), 0 },
1308 { "autoLogin", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, auto_login
), 0 },
1309 { "stopDelayUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, stop_delay_usec
), 0 },
1310 { "killProcesses", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, kill_processes
), 0 },
1311 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_min_usec
), 0 },
1312 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_max_usec
), 0 },
1313 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_warn_usec
), 0 },
1314 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_inactive_usec
), 0 },
1315 { "passwordChangeNow", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, password_change_now
), 0 },
1316 { "pkcs11TokenUri", JSON_VARIANT_ARRAY
, dispatch_pkcs11_uri_array
, offsetof(UserRecord
, pkcs11_token_uri
), 0 },
1318 { "secret", JSON_VARIANT_OBJECT
, dispatch_secret
, 0, 0 },
1319 { "privileged", JSON_VARIANT_OBJECT
, dispatch_privileged
, 0, 0 },
1321 /* Ignore the perMachine, binding, status stuff here, and process it later, so that it overrides whatever is set above */
1322 { "perMachine", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1323 { "binding", JSON_VARIANT_OBJECT
, NULL
, 0, 0 },
1324 { "status", JSON_VARIANT_OBJECT
, NULL
, 0, 0 },
1326 /* Ignore 'signature', we check it with explicit accessors instead */
1327 { "signature", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1331 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1337 /* Note that this call will leave a half-initialized record around on failure! */
1339 r
= user_group_record_mangle(v
, load_flags
, &h
->json
, &h
->mask
);
1343 r
= json_dispatch(h
->json
, user_dispatch_table
, NULL
, json_flags
, h
);
1347 /* During the parsing operation above we ignored the 'perMachine', 'binding' and 'status' fields,
1348 * since we want them to override the global options. Let's process them now. */
1350 r
= dispatch_per_machine("perMachine", json_variant_by_key(h
->json
, "perMachine"), json_flags
, h
);
1354 r
= dispatch_binding("binding", json_variant_by_key(h
->json
, "binding"), json_flags
, h
);
1358 r
= dispatch_status("status", json_variant_by_key(h
->json
, "status"), json_flags
, h
);
1362 if (FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
) && !h
->user_name
)
1363 return json_log(h
->json
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "User name field missing, refusing.");
1365 r
= user_record_augment(h
, json_flags
);
1372 int user_record_build(UserRecord
**ret
, ...) {
1373 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1374 _cleanup_(user_record_unrefp
) UserRecord
*u
= NULL
;
1381 r
= json_buildv(&v
, ap
);
1387 u
= user_record_new();
1391 r
= user_record_load(u
, v
, USER_RECORD_LOAD_FULL
);
1399 const char *user_record_user_name_and_realm(UserRecord
*h
) {
1402 /* Return the pre-initialized joined string if it is defined */
1403 if (h
->user_name_and_realm_auto
)
1404 return h
->user_name_and_realm_auto
;
1406 /* If it's not defined then we cannot have a realm */
1408 return h
->user_name
;
1411 UserStorage
user_record_storage(UserRecord
*h
) {
1414 if (h
->storage
>= 0)
1417 return USER_CLASSIC
;
1420 const char *user_record_file_system_type(UserRecord
*h
) {
1423 return h
->file_system_type
?: "ext4";
1426 const char *user_record_skeleton_directory(UserRecord
*h
) {
1429 return h
->skeleton_directory
?: "/etc/skel";
1432 mode_t
user_record_access_mode(UserRecord
*h
) {
1435 return h
->access_mode
!= (mode_t
) -1 ? h
->access_mode
: 0700;
1438 const char* user_record_home_directory(UserRecord
*h
) {
1441 if (h
->home_directory
)
1442 return h
->home_directory
;
1443 if (h
->home_directory_auto
)
1444 return h
->home_directory_auto
;
1446 /* The root user is special, hence be special about it */
1447 if (streq_ptr(h
->user_name
, "root"))
1453 const char *user_record_image_path(UserRecord
*h
) {
1457 return h
->image_path
;
1458 if (h
->image_path_auto
)
1459 return h
->image_path_auto
;
1461 return IN_SET(user_record_storage(h
), USER_CLASSIC
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
) ? user_record_home_directory(h
) : NULL
;
1464 const char *user_record_cifs_user_name(UserRecord
*h
) {
1467 return h
->cifs_user_name
?: h
->user_name
;
1470 unsigned long user_record_mount_flags(UserRecord
*h
) {
1473 return (h
->nosuid
? MS_NOSUID
: 0) |
1474 (h
->noexec
? MS_NOEXEC
: 0) |
1475 (h
->nodev
? MS_NODEV
: 0);
1478 const char *user_record_shell(UserRecord
*h
) {
1484 if (streq_ptr(h
->user_name
, "root"))
1487 if (user_record_disposition(h
) == USER_REGULAR
)
1493 const char *user_record_real_name(UserRecord
*h
) {
1496 return h
->real_name
?: h
->user_name
;
1499 bool user_record_luks_discard(UserRecord
*h
) {
1504 if (h
->luks_discard
>= 0)
1505 return h
->luks_discard
;
1507 ip
= user_record_image_path(h
);
1511 /* Use discard by default if we are referring to a real block device, but not when operating on a
1512 * loopback device. We want to optimize for SSD and flash storage after all, but we should be careful
1513 * when storing stuff on top of regular file systems in loopback files as doing discard then would
1514 * mean thin provisioning and we should not do that willy-nilly since it means we'll risk EIO later
1515 * on should the disk space to back our file systems not be available. */
1517 return path_startswith(ip
, "/dev/");
1520 bool user_record_luks_offline_discard(UserRecord
*h
) {
1525 if (h
->luks_offline_discard
>= 0)
1526 return h
->luks_offline_discard
;
1528 /* Discard while we are logged out should generally be a good idea, except when operating directly on
1529 * physical media, where we should just bind it to the online discard mode. */
1531 ip
= user_record_image_path(h
);
1535 if (path_startswith(ip
, "/dev/"))
1536 return user_record_luks_discard(h
);
1541 const char *user_record_luks_cipher(UserRecord
*h
) {
1544 return h
->luks_cipher
?: "aes";
1547 const char *user_record_luks_cipher_mode(UserRecord
*h
) {
1550 return h
->luks_cipher_mode
?: "xts-plain64";
1553 uint64_t user_record_luks_volume_key_size(UserRecord
*h
) {
1556 /* We return a value here that can be cast without loss into size_t which is what libcrypsetup expects */
1558 if (h
->luks_volume_key_size
== UINT64_MAX
)
1561 return MIN(h
->luks_volume_key_size
, SIZE_MAX
);
1564 const char* user_record_luks_pbkdf_type(UserRecord
*h
) {
1567 return h
->luks_pbkdf_type
?: "argon2i";
1570 uint64_t user_record_luks_pbkdf_time_cost_usec(UserRecord
*h
) {
1573 /* Returns a value with ms granularity, since that's what libcryptsetup expects */
1575 if (h
->luks_pbkdf_time_cost_usec
== UINT64_MAX
)
1576 return 500 * USEC_PER_MSEC
; /* We default to 500ms, in contrast to libcryptsetup's 2s, which is just awfully slow on every login */
1578 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_time_cost_usec
, USEC_PER_MSEC
), UINT32_MAX
) * USEC_PER_MSEC
;
1581 uint64_t user_record_luks_pbkdf_memory_cost(UserRecord
*h
) {
1584 /* Returns a value with kb granularity, since that's what libcryptsetup expects */
1586 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1587 return 64*1024*1024; /* We default to 64M, since this should work on smaller systems too */
1589 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_memory_cost
, 1024), UINT32_MAX
) * 1024;
1592 uint64_t user_record_luks_pbkdf_parallel_threads(UserRecord
*h
) {
1595 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1596 return 1; /* We default to 1, since this should work on smaller systems too */
1598 return MIN(h
->luks_pbkdf_parallel_threads
, UINT32_MAX
);
1601 const char *user_record_luks_pbkdf_hash_algorithm(UserRecord
*h
) {
1604 return h
->luks_pbkdf_hash_algorithm
?: "sha512";
1607 gid_t
user_record_gid(UserRecord
*h
) {
1610 if (gid_is_valid(h
->gid
))
1613 return (gid_t
) h
->uid
;
1616 UserDisposition
user_record_disposition(UserRecord
*h
) {
1619 if (h
->disposition
>= 0)
1620 return h
->disposition
;
1622 /* If not declared, derive from UID */
1624 if (!uid_is_valid(h
->uid
))
1625 return _USER_DISPOSITION_INVALID
;
1627 if (h
->uid
== 0 || h
->uid
== UID_NOBODY
)
1628 return USER_INTRINSIC
;
1630 if (uid_is_system(h
->uid
))
1633 if (uid_is_dynamic(h
->uid
))
1634 return USER_DYNAMIC
;
1636 if (uid_is_container(h
->uid
))
1637 return USER_CONTAINER
;
1639 if (h
->uid
> INT32_MAX
)
1640 return USER_RESERVED
;
1642 return USER_REGULAR
;
1645 int user_record_removable(UserRecord
*h
) {
1646 UserStorage storage
;
1649 if (h
->removable
>= 0)
1650 return h
->removable
;
1652 /* Refuse to decide for classic records */
1653 storage
= user_record_storage(h
);
1654 if (h
->storage
< 0 || h
->storage
== USER_CLASSIC
)
1657 /* For now consider only LUKS home directories with a reference by path as removable */
1658 return storage
== USER_LUKS
&& path_startswith(user_record_image_path(h
), "/dev/");
1661 uint64_t user_record_ratelimit_interval_usec(UserRecord
*h
) {
1664 if (h
->ratelimit_interval_usec
== UINT64_MAX
)
1665 return DEFAULT_RATELIMIT_INTERVAL_USEC
;
1667 return h
->ratelimit_interval_usec
;
1670 uint64_t user_record_ratelimit_burst(UserRecord
*h
) {
1673 if (h
->ratelimit_burst
== UINT64_MAX
)
1674 return DEFAULT_RATELIMIT_BURST
;
1676 return h
->ratelimit_burst
;
1679 bool user_record_can_authenticate(UserRecord
*h
) {
1682 /* Returns true if there's some form of property configured that the user can authenticate against */
1684 if (h
->n_pkcs11_encrypted_key
> 0)
1687 return !strv_isempty(h
->hashed_password
);
1690 uint64_t user_record_ratelimit_next_try(UserRecord
*h
) {
1693 /* Calculates when the it's possible to login next. Returns:
1695 * UINT64_MAX → Nothing known
1697 * Any other → Next time in CLOCK_REALTIME in usec (which could be in the past)
1700 if (h
->ratelimit_begin_usec
== UINT64_MAX
||
1701 h
->ratelimit_count
== UINT64_MAX
)
1704 if (h
->ratelimit_count
< user_record_ratelimit_burst(h
))
1707 return usec_add(h
->ratelimit_begin_usec
, user_record_ratelimit_interval_usec(h
));
1710 bool user_record_equal(UserRecord
*a
, UserRecord
*b
) {
1714 /* We assume that when a record is modified its JSON data is updated at the same time, hence it's
1715 * sufficient to compare the JSON data. */
1717 return json_variant_equal(a
->json
, b
->json
);
1720 bool user_record_compatible(UserRecord
*a
, UserRecord
*b
) {
1724 /* If either lacks a the regular section, we can't really decide, let's hence say they are
1726 if (!(a
->mask
& b
->mask
& USER_RECORD_REGULAR
))
1729 return streq_ptr(a
->user_name
, b
->user_name
) &&
1730 streq_ptr(a
->realm
, b
->realm
);
1733 int user_record_compare_last_change(UserRecord
*a
, UserRecord
*b
) {
1737 if (a
->last_change_usec
== b
->last_change_usec
)
1740 /* Always consider a record with a timestamp newer than one without */
1741 if (a
->last_change_usec
== UINT64_MAX
)
1743 if (b
->last_change_usec
== UINT64_MAX
)
1746 return CMP(a
->last_change_usec
, b
->last_change_usec
);
1749 int user_record_clone(UserRecord
*h
, UserRecordLoadFlags flags
, UserRecord
**ret
) {
1750 _cleanup_(user_record_unrefp
) UserRecord
*c
= NULL
;
1756 c
= user_record_new();
1760 r
= user_record_load(c
, h
->json
, flags
);
1768 int user_record_masked_equal(UserRecord
*a
, UserRecord
*b
, UserRecordMask mask
) {
1769 _cleanup_(user_record_unrefp
) UserRecord
*x
= NULL
, *y
= NULL
;
1775 /* Compares the two records, but ignores anything not listed in the specified mask */
1777 if ((a
->mask
& ~mask
) != 0) {
1778 r
= user_record_clone(a
, USER_RECORD_ALLOW(mask
) | USER_RECORD_STRIP(~mask
& _USER_RECORD_MASK_MAX
), &x
);
1785 if ((b
->mask
& ~mask
) != 0) {
1786 r
= user_record_clone(b
, USER_RECORD_ALLOW(mask
) | USER_RECORD_STRIP(~mask
& _USER_RECORD_MASK_MAX
), &y
);
1793 return user_record_equal(a
, b
);
1796 int user_record_test_blocked(UserRecord
*h
) {
1799 /* Checks whether access to the specified user shall be allowed at the moment. Returns:
1801 * -ESTALE: Record is from the future
1802 * -ENOLCK: Record is blocked
1803 * -EL2HLT: Record is not valid yet
1804 * -EL3HLT: Record is not valid anymore
1810 n
= now(CLOCK_REALTIME
);
1811 if (h
->last_change_usec
!= UINT64_MAX
&&
1812 h
->last_change_usec
> n
) /* Don't allow log ins when the record is from the future */
1818 if (h
->not_before_usec
!= UINT64_MAX
&& n
< h
->not_before_usec
)
1820 if (h
->not_after_usec
!= UINT64_MAX
&& n
> h
->not_after_usec
)
1826 int user_record_test_password_change_required(UserRecord
*h
) {
1827 bool change_permitted
;
1832 /* Checks whether the user must change the password when logging in
1834 -EKEYREVOKED: Change password now because admin said so
1835 -EOWNERDEAD: Change password now because it expired
1836 -EKEYREJECTED: Password is expired, no changing is allowed
1837 -EKEYEXPIRED: Password is about to expire, warn user
1838 -ENETDOWN: Record has expiration info but no password change timestamp
1839 -EROFS: No password change required nor permitted
1840 0: No password change required, but permitted
1843 /* If a password change request has been set explicitly, it overrides everything */
1844 if (h
->password_change_now
> 0)
1845 return -EKEYREVOKED
;
1847 n
= now(CLOCK_REALTIME
);
1849 /* Then, let's check if password changing is currently allowed at all */
1850 if (h
->password_change_min_usec
!= UINT64_MAX
) {
1852 /* Expiry configured but no password change timestamp known? */
1853 if (h
->last_password_change_usec
== UINT64_MAX
)
1856 if (h
->password_change_min_usec
>= UINT64_MAX
- h
->last_password_change_usec
)
1857 change_permitted
= false;
1859 change_permitted
= n
>= h
->last_password_change_usec
+ h
->password_change_min_usec
;
1862 change_permitted
= true;
1864 /* Let's check whether the password has expired. */
1865 if (!(h
->password_change_max_usec
== UINT64_MAX
||
1866 h
->password_change_max_usec
>= UINT64_MAX
- h
->last_password_change_usec
)) {
1868 uint64_t change_before
;
1870 /* Expiry configured but no password change timestamp known? */
1871 if (h
->last_password_change_usec
== UINT64_MAX
)
1874 /* Password is in inactive phase? */
1875 if (h
->password_change_inactive_usec
!= UINT64_MAX
&&
1876 h
->password_change_inactive_usec
< UINT64_MAX
- h
->password_change_max_usec
) {
1879 added
= h
->password_change_inactive_usec
+ h
->password_change_max_usec
;
1880 if (added
< UINT64_MAX
- h
->last_password_change_usec
&&
1881 n
>= h
->last_password_change_usec
+ added
)
1882 return -EKEYREJECTED
;
1885 /* Password needs to be changed now? */
1886 change_before
= h
->last_password_change_usec
+ h
->password_change_max_usec
;
1887 if (n
>= change_before
)
1888 return change_permitted
? -EOWNERDEAD
: -EKEYREJECTED
;
1891 if (h
->password_change_warn_usec
!= UINT64_MAX
&&
1892 (change_before
< h
->password_change_warn_usec
||
1893 n
>= change_before
- h
->password_change_warn_usec
))
1894 return change_permitted
? -EKEYEXPIRED
: -EROFS
;
1897 /* No password changing necessary */
1898 return change_permitted
? 0 : -EROFS
;
1901 static const char* const user_storage_table
[_USER_STORAGE_MAX
] = {
1902 [USER_CLASSIC
] = "classic",
1903 [USER_LUKS
] = "luks",
1904 [USER_DIRECTORY
] = "directory",
1905 [USER_SUBVOLUME
] = "subvolume",
1906 [USER_FSCRYPT
] = "fscrypt",
1907 [USER_CIFS
] = "cifs",
1910 DEFINE_STRING_TABLE_LOOKUP(user_storage
, UserStorage
);
1912 static const char* const user_disposition_table
[_USER_DISPOSITION_MAX
] = {
1913 [USER_INTRINSIC
] = "intrinsic",
1914 [USER_SYSTEM
] = "system",
1915 [USER_DYNAMIC
] = "dynamic",
1916 [USER_REGULAR
] = "regular",
1917 [USER_CONTAINER
] = "container",
1918 [USER_RESERVED
] = "reserved",
1921 DEFINE_STRING_TABLE_LOOKUP(user_disposition
, UserDisposition
);