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
->token_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 { "tokenPin", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, token_pin
), 0 },
624 { "pkcs11Pin", /* legacy alias */ _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, token_pin
), 0 },
625 { "pkcs11ProtectedAuthenticationPathPermitted", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, pkcs11_protected_authentication_path_permitted
), 0 },
629 return json_dispatch(variant
, secret_dispatch_table
, NULL
, flags
, userdata
);
632 static int dispatch_pkcs11_uri(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
637 if (json_variant_is_null(variant
)) {
642 if (!json_variant_is_string(variant
))
643 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
645 n
= json_variant_string(variant
);
646 if (!pkcs11_uri_valid(n
))
647 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name
));
649 r
= free_and_strdup(s
, n
);
651 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
656 static int dispatch_pkcs11_uri_array(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
657 _cleanup_strv_free_
char **z
= NULL
;
658 char ***l
= userdata
;
662 if (json_variant_is_null(variant
)) {
667 if (json_variant_is_string(variant
)) {
670 n
= json_variant_string(variant
);
671 if (!pkcs11_uri_valid(n
))
672 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name
));
680 if (!json_variant_is_array(variant
))
681 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string or array of strings.", strna(name
));
683 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
686 if (!json_variant_is_string(e
))
687 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
689 n
= json_variant_string(e
);
690 if (!pkcs11_uri_valid(n
))
691 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
);
693 r
= strv_extend(&z
, n
);
699 strv_free_and_replace(*l
, z
);
703 static int dispatch_pkcs11_key_data(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
704 Pkcs11EncryptedKey
*k
= userdata
;
709 if (json_variant_is_null(variant
)) {
710 k
->data
= erase_and_free(k
->data
);
715 if (!json_variant_is_string(variant
))
716 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
718 r
= unbase64mem(json_variant_string(variant
), (size_t) -1, &b
, &l
);
720 return json_log(variant
, flags
, r
, "Failed to decode encrypted PKCS#11 key: %m");
722 erase_and_free(k
->data
);
729 static int dispatch_pkcs11_key(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
730 UserRecord
*h
= userdata
;
734 if (!json_variant_is_array(variant
))
735 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
737 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
738 Pkcs11EncryptedKey
*array
, *k
;
740 static const JsonDispatch pkcs11_key_dispatch_table
[] = {
741 { "uri", JSON_VARIANT_STRING
, dispatch_pkcs11_uri
, offsetof(Pkcs11EncryptedKey
, uri
), JSON_MANDATORY
},
742 { "data", JSON_VARIANT_STRING
, dispatch_pkcs11_key_data
, 0, JSON_MANDATORY
},
743 { "hashedPassword", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(Pkcs11EncryptedKey
, hashed_password
), JSON_MANDATORY
},
747 if (!json_variant_is_object(e
))
748 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
750 array
= reallocarray(h
->pkcs11_encrypted_key
, h
->n_pkcs11_encrypted_key
+ 1, sizeof(Pkcs11EncryptedKey
));
754 h
->pkcs11_encrypted_key
= array
;
755 k
= h
->pkcs11_encrypted_key
+ h
->n_pkcs11_encrypted_key
;
756 *k
= (Pkcs11EncryptedKey
) {};
758 r
= json_dispatch(e
, pkcs11_key_dispatch_table
, NULL
, flags
, k
);
760 pkcs11_encrypted_key_done(k
);
764 h
->n_pkcs11_encrypted_key
++;
770 static int dispatch_privileged(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
772 static const JsonDispatch privileged_dispatch_table
[] = {
773 { "passwordHint", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, password_hint
), 0 },
774 { "hashedPassword", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, hashed_password
), JSON_SAFE
},
775 { "sshAuthorizedKeys", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, ssh_authorized_keys
), 0 },
776 { "pkcs11EncryptedKey", JSON_VARIANT_ARRAY
, dispatch_pkcs11_key
, 0, 0 },
780 return json_dispatch(variant
, privileged_dispatch_table
, NULL
, flags
, userdata
);
783 static int dispatch_binding(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
785 static const JsonDispatch binding_dispatch_table
[] = {
786 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_image_path
, offsetof(UserRecord
, image_path
), 0 },
787 { "homeDirectory", JSON_VARIANT_STRING
, json_dispatch_home_directory
, offsetof(UserRecord
, home_directory
), 0 },
788 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
789 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
790 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
791 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
792 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
793 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
794 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
795 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
796 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
797 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
801 char smid
[SD_ID128_STRING_MAX
];
809 if (!json_variant_is_object(variant
))
810 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
812 r
= sd_id128_get_machine(&mid
);
814 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
816 m
= json_variant_by_key(variant
, sd_id128_to_string(mid
, smid
));
820 return json_dispatch(m
, binding_dispatch_table
, NULL
, flags
, userdata
);
823 int per_machine_id_match(JsonVariant
*ids
, JsonDispatchFlags flags
) {
827 r
= sd_id128_get_machine(&mid
);
829 return json_log(ids
, flags
, r
, "Failed to acquire machine ID: %m");
831 if (json_variant_is_string(ids
)) {
834 r
= sd_id128_from_string(json_variant_string(ids
), &k
);
836 json_log(ids
, flags
, r
, "%s is not a valid machine ID, ignoring: %m", json_variant_string(ids
));
840 return sd_id128_equal(mid
, k
);
843 if (json_variant_is_array(ids
)) {
846 JSON_VARIANT_ARRAY_FOREACH(e
, ids
) {
849 if (!json_variant_is_string(e
)) {
850 json_log(e
, flags
, 0, "Machine ID is not a string, ignoring: %m");
854 r
= sd_id128_from_string(json_variant_string(e
), &k
);
856 json_log(e
, flags
, r
, "%s is not a valid machine ID, ignoring: %m", json_variant_string(e
));
860 if (sd_id128_equal(mid
, k
))
867 json_log(ids
, flags
, 0, "Machine ID is not a string or array of strings, ignoring: %m");
871 int per_machine_hostname_match(JsonVariant
*hns
, JsonDispatchFlags flags
) {
872 _cleanup_free_
char *hn
= NULL
;
875 r
= gethostname_strict(&hn
);
877 json_log(hns
, flags
, r
, "No hostname set, not matching perMachine hostname record: %m");
881 return json_log(hns
, flags
, r
, "Failed to acquire hostname: %m");
883 if (json_variant_is_string(hns
))
884 return streq(json_variant_string(hns
), hn
);
886 if (json_variant_is_array(hns
)) {
889 JSON_VARIANT_ARRAY_FOREACH(e
, hns
) {
891 if (!json_variant_is_string(e
)) {
892 json_log(e
, flags
, 0, "Hostname is not a string, ignoring: %m");
896 if (streq(json_variant_string(hns
), hn
))
903 json_log(hns
, flags
, 0, "Hostname is not a string or array of strings, ignoring: %m");
907 static int dispatch_per_machine(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
909 static const JsonDispatch per_machine_dispatch_table
[] = {
910 { "matchMachineId", _JSON_VARIANT_TYPE_INVALID
, NULL
, 0, 0 },
911 { "matchHostname", _JSON_VARIANT_TYPE_INVALID
, NULL
, 0, 0 },
912 { "iconName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, icon_name
), JSON_SAFE
},
913 { "location", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, location
), 0 },
914 { "shell", JSON_VARIANT_STRING
, json_dispatch_filename_or_path
, offsetof(UserRecord
, shell
), 0 },
915 { "umask", JSON_VARIANT_UNSIGNED
, json_dispatch_umask
, offsetof(UserRecord
, umask
), 0 },
916 { "environment", JSON_VARIANT_ARRAY
, json_dispatch_environment
, offsetof(UserRecord
, environment
), 0 },
917 { "timeZone", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, time_zone
), JSON_SAFE
},
918 { "preferredLanguage", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, preferred_language
), JSON_SAFE
},
919 { "niceLevel", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_nice
, offsetof(UserRecord
, nice_level
), 0 },
920 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_rlimits
, offsetof(UserRecord
, rlimits
), 0 },
921 { "locked", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, locked
), 0 },
922 { "notBeforeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_before_usec
), 0 },
923 { "notAfterUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_after_usec
), 0 },
924 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
925 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_disk_size
, offsetof(UserRecord
, disk_size
), 0 },
926 { "diskSizeRelative", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size_relative
), 0 },
927 { "skeletonDirectory", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, skeleton_directory
), 0 },
928 { "accessMode", JSON_VARIANT_UNSIGNED
, json_dispatch_access_mode
, offsetof(UserRecord
, access_mode
), 0 },
929 { "tasksMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, tasks_max
), 0 },
930 { "memoryHigh", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_high
), 0 },
931 { "memoryMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_max
), 0 },
932 { "cpuWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, cpu_weight
), 0 },
933 { "ioWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, io_weight
), 0 },
934 { "mountNoDevices", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nodev
), 0 },
935 { "mountNoSuid", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nosuid
), 0 },
936 { "mountNoExecute", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, noexec
), 0 },
937 { "cifsDomain", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_domain
), JSON_SAFE
},
938 { "cifsUserName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_user_name
), JSON_SAFE
},
939 { "cifsService", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_service
), JSON_SAFE
},
940 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, image_path
), 0 },
941 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
942 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
943 { "memberOf", JSON_VARIANT_ARRAY
, json_dispatch_user_group_list
, offsetof(UserRecord
, member_of
), JSON_RELAX
},
944 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
945 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
946 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
947 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
948 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_discard
), 0, },
949 { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_offline_discard
), 0, },
950 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
951 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
952 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
953 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_hash_algorithm
), JSON_SAFE
},
954 { "luksPbkdfType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_type
), JSON_SAFE
},
955 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_time_cost_usec
), 0 },
956 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_memory_cost
), 0 },
957 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_parallel_threads
), 0 },
958 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_interval_usec
), 0 },
959 { "rateLimitBurst", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_burst
), 0 },
960 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, enforce_password_policy
), 0 },
961 { "autoLogin", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, auto_login
), 0 },
962 { "stopDelayUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, stop_delay_usec
), 0 },
963 { "killProcesses", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, kill_processes
), 0 },
964 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_min_usec
), 0 },
965 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_max_usec
), 0 },
966 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_warn_usec
), 0 },
967 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_inactive_usec
), 0 },
968 { "passwordChangeNow", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, password_change_now
), 0 },
969 { "pkcs11TokenUri", JSON_VARIANT_ARRAY
, dispatch_pkcs11_uri_array
, offsetof(UserRecord
, pkcs11_token_uri
), 0 },
979 if (!json_variant_is_array(variant
))
980 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array.", strna(name
));
982 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
983 bool matching
= false;
986 if (!json_variant_is_object(e
))
987 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
989 m
= json_variant_by_key(e
, "matchMachineId");
991 r
= per_machine_id_match(m
, flags
);
999 m
= json_variant_by_key(e
, "matchHostname");
1001 r
= per_machine_hostname_match(m
, flags
);
1012 r
= json_dispatch(e
, per_machine_dispatch_table
, NULL
, flags
, userdata
);
1020 static int dispatch_status(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1022 static const JsonDispatch status_dispatch_table
[] = {
1023 { "diskUsage", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_usage
), 0 },
1024 { "diskFree", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_free
), 0 },
1025 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size
), 0 },
1026 { "diskCeiling", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_ceiling
), 0 },
1027 { "diskFloor", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_floor
), 0 },
1028 { "state", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, state
), JSON_SAFE
},
1029 { "service", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, service
), JSON_SAFE
},
1030 { "signedLocally", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, signed_locally
), 0 },
1031 { "goodAuthenticationCounter", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, good_authentication_counter
), 0 },
1032 { "badAuthenticationCounter", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, bad_authentication_counter
), 0 },
1033 { "lastGoodAuthenticationUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_good_authentication_usec
), 0 },
1034 { "lastBadAuthenticationUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_bad_authentication_usec
), 0 },
1035 { "rateLimitBeginUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_begin_usec
), 0 },
1036 { "rateLimitCount", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_count
), 0 },
1037 { "removable", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, removable
), 0 },
1041 char smid
[SD_ID128_STRING_MAX
];
1049 if (!json_variant_is_object(variant
))
1050 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
1052 r
= sd_id128_get_machine(&mid
);
1054 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
1056 m
= json_variant_by_key(variant
, sd_id128_to_string(mid
, smid
));
1060 return json_dispatch(m
, status_dispatch_table
, NULL
, flags
, userdata
);
1063 int user_record_build_image_path(UserStorage storage
, const char *user_name_and_realm
, char **ret
) {
1067 assert(storage
>= 0);
1068 assert(user_name_and_realm
);
1071 if (storage
== USER_LUKS
)
1073 else if (IN_SET(storage
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
))
1074 suffix
= ".homedir";
1080 z
= strjoin("/home/", user_name_and_realm
, suffix
);
1088 static int user_record_augment(UserRecord
*h
, JsonDispatchFlags json_flags
) {
1093 if (!FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
))
1096 assert(h
->user_name
);
1098 if (!h
->user_name_and_realm_auto
&& h
->realm
) {
1099 h
->user_name_and_realm_auto
= strjoin(h
->user_name
, "@", h
->realm
);
1100 if (!h
->user_name_and_realm_auto
)
1101 return json_log_oom(h
->json
, json_flags
);
1104 /* Let's add in the following automatisms only for regular users, they don't make sense for any others */
1105 if (user_record_disposition(h
) != USER_REGULAR
)
1108 if (!h
->home_directory
&& !h
->home_directory_auto
) {
1109 h
->home_directory_auto
= path_join("/home/", h
->user_name
);
1110 if (!h
->home_directory_auto
)
1111 return json_log_oom(h
->json
, json_flags
);
1114 if (!h
->image_path
&& !h
->image_path_auto
) {
1115 r
= user_record_build_image_path(user_record_storage(h
), user_record_user_name_and_realm(h
), &h
->image_path_auto
);
1117 return json_log(h
->json
, json_flags
, r
, "Failed to determine default image path: %m");
1123 int user_group_record_mangle(
1125 UserRecordLoadFlags load_flags
,
1126 JsonVariant
**ret_variant
,
1127 UserRecordMask
*ret_mask
) {
1129 static const struct {
1130 UserRecordMask mask
;
1133 { USER_RECORD_PRIVILEGED
, "privileged" },
1134 { USER_RECORD_SECRET
, "secret" },
1135 { USER_RECORD_BINDING
, "binding" },
1136 { USER_RECORD_PER_MACHINE
, "perMachine" },
1137 { USER_RECORD_STATUS
, "status" },
1138 { USER_RECORD_SIGNATURE
, "signature" },
1141 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1142 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
;
1143 JsonVariant
*array
[ELEMENTSOF(mask_field
) * 2];
1144 size_t n_retain
= 0, i
;
1145 UserRecordMask m
= 0;
1148 assert((load_flags
& _USER_RECORD_MASK_MAX
) == 0); /* detect mistakes when accidentally passing
1149 * UserRecordMask bit masks as UserRecordLoadFlags
1153 assert(ret_variant
);
1156 /* Note that this function is shared with the group record parser, hence we try to be generic in our
1157 * log message wording here, to cover both cases. */
1159 if (!json_variant_is_object(v
))
1160 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is not a JSON object, refusing.");
1162 if (USER_RECORD_ALLOW_MASK(load_flags
) == 0) /* allow nothing? */
1163 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "Nothing allowed in record, refusing.");
1165 if (USER_RECORD_STRIP_MASK(load_flags
) == _USER_RECORD_MASK_MAX
) /* strip everything? */
1166 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "Stripping everything from record, refusing.");
1168 /* Check if we have the special sections and if they match our flags set */
1169 for (i
= 0; i
< ELEMENTSOF(mask_field
); i
++) {
1172 if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags
), mask_field
[i
].mask
)) {
1174 w
= json_variant_ref(v
);
1176 r
= json_variant_filter(&w
, STRV_MAKE(mask_field
[i
].name
));
1178 return json_log(w
, json_flags
, r
, "Failed to remove field from variant: %m");
1183 e
= json_variant_by_key_full(v
, mask_field
[i
].name
, &k
);
1185 if (!FLAGS_SET(USER_RECORD_ALLOW_MASK(load_flags
), mask_field
[i
].mask
))
1186 return json_log(e
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record contains '%s' field, which is not allowed.", mask_field
[i
].name
);
1188 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1189 array
[n_retain
++] = k
;
1190 array
[n_retain
++] = e
;
1193 m
|= mask_field
[i
].mask
;
1195 if (FLAGS_SET(USER_RECORD_REQUIRE_MASK(load_flags
), mask_field
[i
].mask
))
1196 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record lacks '%s' field, which is required.", mask_field
[i
].name
);
1200 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1201 /* If we are supposed to strip regular items, then let's instead just allocate a new object
1202 * with just the stuff we need. */
1204 w
= json_variant_unref(w
);
1205 r
= json_variant_new_object(&w
, array
, n_retain
);
1207 return json_log(v
, json_flags
, r
, "Failed to allocate new object: %m");
1209 /* And now check if there's anything else in the record */
1210 for (i
= 0; i
< json_variant_elements(v
); i
+= 2) {
1212 bool special
= false;
1215 assert_se(f
= json_variant_string(json_variant_by_index(v
, i
)));
1217 for (j
= 0; j
< ELEMENTSOF(mask_field
); j
++)
1218 if (streq(f
, mask_field
[j
].name
)) { /* already covered in the loop above */
1224 if ((load_flags
& (USER_RECORD_ALLOW_REGULAR
|USER_RECORD_REQUIRE_REGULAR
)) == 0)
1225 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record contains '%s' field, which is not allowed.", f
);
1227 m
|= USER_RECORD_REGULAR
;
1233 if (FLAGS_SET(load_flags
, USER_RECORD_REQUIRE_REGULAR
) && !FLAGS_SET(m
, USER_RECORD_REGULAR
))
1234 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record lacks basic identity fields, which are required.");
1237 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is empty.");
1240 *ret_variant
= TAKE_PTR(w
);
1242 *ret_variant
= json_variant_ref(v
);
1248 int user_record_load(UserRecord
*h
, JsonVariant
*v
, UserRecordLoadFlags load_flags
) {
1250 static const JsonDispatch user_dispatch_table
[] = {
1251 { "userName", JSON_VARIANT_STRING
, json_dispatch_user_group_name
, offsetof(UserRecord
, user_name
), JSON_RELAX
},
1252 { "realm", JSON_VARIANT_STRING
, json_dispatch_realm
, offsetof(UserRecord
, realm
), 0 },
1253 { "realName", JSON_VARIANT_STRING
, json_dispatch_gecos
, offsetof(UserRecord
, real_name
), 0 },
1254 { "emailAddress", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, email_address
), JSON_SAFE
},
1255 { "iconName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, icon_name
), JSON_SAFE
},
1256 { "location", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, location
), 0 },
1257 { "disposition", JSON_VARIANT_STRING
, json_dispatch_user_disposition
, offsetof(UserRecord
, disposition
), 0 },
1258 { "lastChangeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_change_usec
), 0 },
1259 { "lastPasswordChangeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_password_change_usec
), 0 },
1260 { "shell", JSON_VARIANT_STRING
, json_dispatch_filename_or_path
, offsetof(UserRecord
, shell
), 0 },
1261 { "umask", JSON_VARIANT_UNSIGNED
, json_dispatch_umask
, offsetof(UserRecord
, umask
), 0 },
1262 { "environment", JSON_VARIANT_ARRAY
, json_dispatch_environment
, offsetof(UserRecord
, environment
), 0 },
1263 { "timeZone", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, time_zone
), JSON_SAFE
},
1264 { "preferredLanguage", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, preferred_language
), JSON_SAFE
},
1265 { "niceLevel", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_nice
, offsetof(UserRecord
, nice_level
), 0 },
1266 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_rlimits
, offsetof(UserRecord
, rlimits
), 0 },
1267 { "locked", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, locked
), 0 },
1268 { "notBeforeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_before_usec
), 0 },
1269 { "notAfterUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_after_usec
), 0 },
1270 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
1271 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_disk_size
, offsetof(UserRecord
, disk_size
), 0 },
1272 { "diskSizeRelative", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size_relative
), 0 },
1273 { "skeletonDirectory", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, skeleton_directory
), 0 },
1274 { "accessMode", JSON_VARIANT_UNSIGNED
, json_dispatch_access_mode
, offsetof(UserRecord
, access_mode
), 0 },
1275 { "tasksMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, tasks_max
), 0 },
1276 { "memoryHigh", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_high
), 0 },
1277 { "memoryMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_max
), 0 },
1278 { "cpuWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, cpu_weight
), 0 },
1279 { "ioWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, io_weight
), 0 },
1280 { "mountNoDevices", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nodev
), 0 },
1281 { "mountNoSuid", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nosuid
), 0 },
1282 { "mountNoExecute", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, noexec
), 0 },
1283 { "cifsDomain", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_domain
), JSON_SAFE
},
1284 { "cifsUserName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_user_name
), JSON_SAFE
},
1285 { "cifsService", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_service
), JSON_SAFE
},
1286 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, image_path
), 0 },
1287 { "homeDirectory", JSON_VARIANT_STRING
, json_dispatch_home_directory
, offsetof(UserRecord
, home_directory
), 0 },
1288 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
1289 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
1290 { "memberOf", JSON_VARIANT_ARRAY
, json_dispatch_user_group_list
, offsetof(UserRecord
, member_of
), JSON_RELAX
},
1291 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
1292 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
1293 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
1294 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
1295 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_discard
), 0 },
1296 { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_offline_discard
), 0 },
1297 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
1298 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
1299 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
1300 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_hash_algorithm
), JSON_SAFE
},
1301 { "luksPbkdfType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_type
), JSON_SAFE
},
1302 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_time_cost_usec
), 0 },
1303 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_memory_cost
), 0 },
1304 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_parallel_threads
), 0 },
1305 { "service", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, service
), JSON_SAFE
},
1306 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_interval_usec
), 0 },
1307 { "rateLimitBurst", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_burst
), 0 },
1308 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, enforce_password_policy
), 0 },
1309 { "autoLogin", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, auto_login
), 0 },
1310 { "stopDelayUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, stop_delay_usec
), 0 },
1311 { "killProcesses", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, kill_processes
), 0 },
1312 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_min_usec
), 0 },
1313 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_max_usec
), 0 },
1314 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_warn_usec
), 0 },
1315 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_inactive_usec
), 0 },
1316 { "passwordChangeNow", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, password_change_now
), 0 },
1317 { "pkcs11TokenUri", JSON_VARIANT_ARRAY
, dispatch_pkcs11_uri_array
, offsetof(UserRecord
, pkcs11_token_uri
), 0 },
1319 { "secret", JSON_VARIANT_OBJECT
, dispatch_secret
, 0, 0 },
1320 { "privileged", JSON_VARIANT_OBJECT
, dispatch_privileged
, 0, 0 },
1322 /* Ignore the perMachine, binding, status stuff here, and process it later, so that it overrides whatever is set above */
1323 { "perMachine", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1324 { "binding", JSON_VARIANT_OBJECT
, NULL
, 0, 0 },
1325 { "status", JSON_VARIANT_OBJECT
, NULL
, 0, 0 },
1327 /* Ignore 'signature', we check it with explicit accessors instead */
1328 { "signature", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1332 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1338 /* Note that this call will leave a half-initialized record around on failure! */
1340 r
= user_group_record_mangle(v
, load_flags
, &h
->json
, &h
->mask
);
1344 r
= json_dispatch(h
->json
, user_dispatch_table
, NULL
, json_flags
, h
);
1348 /* During the parsing operation above we ignored the 'perMachine', 'binding' and 'status' fields,
1349 * since we want them to override the global options. Let's process them now. */
1351 r
= dispatch_per_machine("perMachine", json_variant_by_key(h
->json
, "perMachine"), json_flags
, h
);
1355 r
= dispatch_binding("binding", json_variant_by_key(h
->json
, "binding"), json_flags
, h
);
1359 r
= dispatch_status("status", json_variant_by_key(h
->json
, "status"), json_flags
, h
);
1363 if (FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
) && !h
->user_name
)
1364 return json_log(h
->json
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "User name field missing, refusing.");
1366 r
= user_record_augment(h
, json_flags
);
1373 int user_record_build(UserRecord
**ret
, ...) {
1374 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1375 _cleanup_(user_record_unrefp
) UserRecord
*u
= NULL
;
1382 r
= json_buildv(&v
, ap
);
1388 u
= user_record_new();
1392 r
= user_record_load(u
, v
, USER_RECORD_LOAD_FULL
);
1400 const char *user_record_user_name_and_realm(UserRecord
*h
) {
1403 /* Return the pre-initialized joined string if it is defined */
1404 if (h
->user_name_and_realm_auto
)
1405 return h
->user_name_and_realm_auto
;
1407 /* If it's not defined then we cannot have a realm */
1409 return h
->user_name
;
1412 UserStorage
user_record_storage(UserRecord
*h
) {
1415 if (h
->storage
>= 0)
1418 return USER_CLASSIC
;
1421 const char *user_record_file_system_type(UserRecord
*h
) {
1424 return h
->file_system_type
?: "ext4";
1427 const char *user_record_skeleton_directory(UserRecord
*h
) {
1430 return h
->skeleton_directory
?: "/etc/skel";
1433 mode_t
user_record_access_mode(UserRecord
*h
) {
1436 return h
->access_mode
!= (mode_t
) -1 ? h
->access_mode
: 0700;
1439 const char* user_record_home_directory(UserRecord
*h
) {
1442 if (h
->home_directory
)
1443 return h
->home_directory
;
1444 if (h
->home_directory_auto
)
1445 return h
->home_directory_auto
;
1447 /* The root user is special, hence be special about it */
1448 if (streq_ptr(h
->user_name
, "root"))
1454 const char *user_record_image_path(UserRecord
*h
) {
1458 return h
->image_path
;
1459 if (h
->image_path_auto
)
1460 return h
->image_path_auto
;
1462 return IN_SET(user_record_storage(h
), USER_CLASSIC
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
) ? user_record_home_directory(h
) : NULL
;
1465 const char *user_record_cifs_user_name(UserRecord
*h
) {
1468 return h
->cifs_user_name
?: h
->user_name
;
1471 unsigned long user_record_mount_flags(UserRecord
*h
) {
1474 return (h
->nosuid
? MS_NOSUID
: 0) |
1475 (h
->noexec
? MS_NOEXEC
: 0) |
1476 (h
->nodev
? MS_NODEV
: 0);
1479 const char *user_record_shell(UserRecord
*h
) {
1485 if (streq_ptr(h
->user_name
, "root"))
1488 if (user_record_disposition(h
) == USER_REGULAR
)
1494 const char *user_record_real_name(UserRecord
*h
) {
1497 return h
->real_name
?: h
->user_name
;
1500 bool user_record_luks_discard(UserRecord
*h
) {
1505 if (h
->luks_discard
>= 0)
1506 return h
->luks_discard
;
1508 ip
= user_record_image_path(h
);
1512 /* Use discard by default if we are referring to a real block device, but not when operating on a
1513 * loopback device. We want to optimize for SSD and flash storage after all, but we should be careful
1514 * when storing stuff on top of regular file systems in loopback files as doing discard then would
1515 * mean thin provisioning and we should not do that willy-nilly since it means we'll risk EIO later
1516 * on should the disk space to back our file systems not be available. */
1518 return path_startswith(ip
, "/dev/");
1521 bool user_record_luks_offline_discard(UserRecord
*h
) {
1526 if (h
->luks_offline_discard
>= 0)
1527 return h
->luks_offline_discard
;
1529 /* Discard while we are logged out should generally be a good idea, except when operating directly on
1530 * physical media, where we should just bind it to the online discard mode. */
1532 ip
= user_record_image_path(h
);
1536 if (path_startswith(ip
, "/dev/"))
1537 return user_record_luks_discard(h
);
1542 const char *user_record_luks_cipher(UserRecord
*h
) {
1545 return h
->luks_cipher
?: "aes";
1548 const char *user_record_luks_cipher_mode(UserRecord
*h
) {
1551 return h
->luks_cipher_mode
?: "xts-plain64";
1554 uint64_t user_record_luks_volume_key_size(UserRecord
*h
) {
1557 /* We return a value here that can be cast without loss into size_t which is what libcrypsetup expects */
1559 if (h
->luks_volume_key_size
== UINT64_MAX
)
1562 return MIN(h
->luks_volume_key_size
, SIZE_MAX
);
1565 const char* user_record_luks_pbkdf_type(UserRecord
*h
) {
1568 return h
->luks_pbkdf_type
?: "argon2i";
1571 uint64_t user_record_luks_pbkdf_time_cost_usec(UserRecord
*h
) {
1574 /* Returns a value with ms granularity, since that's what libcryptsetup expects */
1576 if (h
->luks_pbkdf_time_cost_usec
== UINT64_MAX
)
1577 return 500 * USEC_PER_MSEC
; /* We default to 500ms, in contrast to libcryptsetup's 2s, which is just awfully slow on every login */
1579 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_time_cost_usec
, USEC_PER_MSEC
), UINT32_MAX
) * USEC_PER_MSEC
;
1582 uint64_t user_record_luks_pbkdf_memory_cost(UserRecord
*h
) {
1585 /* Returns a value with kb granularity, since that's what libcryptsetup expects */
1587 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1588 return 64*1024*1024; /* We default to 64M, since this should work on smaller systems too */
1590 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_memory_cost
, 1024), UINT32_MAX
) * 1024;
1593 uint64_t user_record_luks_pbkdf_parallel_threads(UserRecord
*h
) {
1596 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1597 return 1; /* We default to 1, since this should work on smaller systems too */
1599 return MIN(h
->luks_pbkdf_parallel_threads
, UINT32_MAX
);
1602 const char *user_record_luks_pbkdf_hash_algorithm(UserRecord
*h
) {
1605 return h
->luks_pbkdf_hash_algorithm
?: "sha512";
1608 gid_t
user_record_gid(UserRecord
*h
) {
1611 if (gid_is_valid(h
->gid
))
1614 return (gid_t
) h
->uid
;
1617 UserDisposition
user_record_disposition(UserRecord
*h
) {
1620 if (h
->disposition
>= 0)
1621 return h
->disposition
;
1623 /* If not declared, derive from UID */
1625 if (!uid_is_valid(h
->uid
))
1626 return _USER_DISPOSITION_INVALID
;
1628 if (h
->uid
== 0 || h
->uid
== UID_NOBODY
)
1629 return USER_INTRINSIC
;
1631 if (uid_is_system(h
->uid
))
1634 if (uid_is_dynamic(h
->uid
))
1635 return USER_DYNAMIC
;
1637 if (uid_is_container(h
->uid
))
1638 return USER_CONTAINER
;
1640 if (h
->uid
> INT32_MAX
)
1641 return USER_RESERVED
;
1643 return USER_REGULAR
;
1646 int user_record_removable(UserRecord
*h
) {
1647 UserStorage storage
;
1650 if (h
->removable
>= 0)
1651 return h
->removable
;
1653 /* Refuse to decide for classic records */
1654 storage
= user_record_storage(h
);
1655 if (h
->storage
< 0 || h
->storage
== USER_CLASSIC
)
1658 /* For now consider only LUKS home directories with a reference by path as removable */
1659 return storage
== USER_LUKS
&& path_startswith(user_record_image_path(h
), "/dev/");
1662 uint64_t user_record_ratelimit_interval_usec(UserRecord
*h
) {
1665 if (h
->ratelimit_interval_usec
== UINT64_MAX
)
1666 return DEFAULT_RATELIMIT_INTERVAL_USEC
;
1668 return h
->ratelimit_interval_usec
;
1671 uint64_t user_record_ratelimit_burst(UserRecord
*h
) {
1674 if (h
->ratelimit_burst
== UINT64_MAX
)
1675 return DEFAULT_RATELIMIT_BURST
;
1677 return h
->ratelimit_burst
;
1680 bool user_record_can_authenticate(UserRecord
*h
) {
1683 /* Returns true if there's some form of property configured that the user can authenticate against */
1685 if (h
->n_pkcs11_encrypted_key
> 0)
1688 return !strv_isempty(h
->hashed_password
);
1691 uint64_t user_record_ratelimit_next_try(UserRecord
*h
) {
1694 /* Calculates when the it's possible to login next. Returns:
1696 * UINT64_MAX → Nothing known
1698 * Any other → Next time in CLOCK_REALTIME in usec (which could be in the past)
1701 if (h
->ratelimit_begin_usec
== UINT64_MAX
||
1702 h
->ratelimit_count
== UINT64_MAX
)
1705 if (h
->ratelimit_count
< user_record_ratelimit_burst(h
))
1708 return usec_add(h
->ratelimit_begin_usec
, user_record_ratelimit_interval_usec(h
));
1711 bool user_record_equal(UserRecord
*a
, UserRecord
*b
) {
1715 /* We assume that when a record is modified its JSON data is updated at the same time, hence it's
1716 * sufficient to compare the JSON data. */
1718 return json_variant_equal(a
->json
, b
->json
);
1721 bool user_record_compatible(UserRecord
*a
, UserRecord
*b
) {
1725 /* If either lacks a the regular section, we can't really decide, let's hence say they are
1727 if (!(a
->mask
& b
->mask
& USER_RECORD_REGULAR
))
1730 return streq_ptr(a
->user_name
, b
->user_name
) &&
1731 streq_ptr(a
->realm
, b
->realm
);
1734 int user_record_compare_last_change(UserRecord
*a
, UserRecord
*b
) {
1738 if (a
->last_change_usec
== b
->last_change_usec
)
1741 /* Always consider a record with a timestamp newer than one without */
1742 if (a
->last_change_usec
== UINT64_MAX
)
1744 if (b
->last_change_usec
== UINT64_MAX
)
1747 return CMP(a
->last_change_usec
, b
->last_change_usec
);
1750 int user_record_clone(UserRecord
*h
, UserRecordLoadFlags flags
, UserRecord
**ret
) {
1751 _cleanup_(user_record_unrefp
) UserRecord
*c
= NULL
;
1757 c
= user_record_new();
1761 r
= user_record_load(c
, h
->json
, flags
);
1769 int user_record_masked_equal(UserRecord
*a
, UserRecord
*b
, UserRecordMask mask
) {
1770 _cleanup_(user_record_unrefp
) UserRecord
*x
= NULL
, *y
= NULL
;
1776 /* Compares the two records, but ignores anything not listed in the specified mask */
1778 if ((a
->mask
& ~mask
) != 0) {
1779 r
= user_record_clone(a
, USER_RECORD_ALLOW(mask
) | USER_RECORD_STRIP(~mask
& _USER_RECORD_MASK_MAX
), &x
);
1786 if ((b
->mask
& ~mask
) != 0) {
1787 r
= user_record_clone(b
, USER_RECORD_ALLOW(mask
) | USER_RECORD_STRIP(~mask
& _USER_RECORD_MASK_MAX
), &y
);
1794 return user_record_equal(a
, b
);
1797 int user_record_test_blocked(UserRecord
*h
) {
1800 /* Checks whether access to the specified user shall be allowed at the moment. Returns:
1802 * -ESTALE: Record is from the future
1803 * -ENOLCK: Record is blocked
1804 * -EL2HLT: Record is not valid yet
1805 * -EL3HLT: Record is not valid anymore
1811 n
= now(CLOCK_REALTIME
);
1812 if (h
->last_change_usec
!= UINT64_MAX
&&
1813 h
->last_change_usec
> n
) /* Don't allow log ins when the record is from the future */
1819 if (h
->not_before_usec
!= UINT64_MAX
&& n
< h
->not_before_usec
)
1821 if (h
->not_after_usec
!= UINT64_MAX
&& n
> h
->not_after_usec
)
1827 int user_record_test_password_change_required(UserRecord
*h
) {
1828 bool change_permitted
;
1833 /* Checks whether the user must change the password when logging in
1835 -EKEYREVOKED: Change password now because admin said so
1836 -EOWNERDEAD: Change password now because it expired
1837 -EKEYREJECTED: Password is expired, no changing is allowed
1838 -EKEYEXPIRED: Password is about to expire, warn user
1839 -ENETDOWN: Record has expiration info but no password change timestamp
1840 -EROFS: No password change required nor permitted
1841 0: No password change required, but permitted
1844 /* If a password change request has been set explicitly, it overrides everything */
1845 if (h
->password_change_now
> 0)
1846 return -EKEYREVOKED
;
1848 n
= now(CLOCK_REALTIME
);
1850 /* Then, let's check if password changing is currently allowed at all */
1851 if (h
->password_change_min_usec
!= UINT64_MAX
) {
1853 /* Expiry configured but no password change timestamp known? */
1854 if (h
->last_password_change_usec
== UINT64_MAX
)
1857 if (h
->password_change_min_usec
>= UINT64_MAX
- h
->last_password_change_usec
)
1858 change_permitted
= false;
1860 change_permitted
= n
>= h
->last_password_change_usec
+ h
->password_change_min_usec
;
1863 change_permitted
= true;
1865 /* Let's check whether the password has expired. */
1866 if (!(h
->password_change_max_usec
== UINT64_MAX
||
1867 h
->password_change_max_usec
>= UINT64_MAX
- h
->last_password_change_usec
)) {
1869 uint64_t change_before
;
1871 /* Expiry configured but no password change timestamp known? */
1872 if (h
->last_password_change_usec
== UINT64_MAX
)
1875 /* Password is in inactive phase? */
1876 if (h
->password_change_inactive_usec
!= UINT64_MAX
&&
1877 h
->password_change_inactive_usec
< UINT64_MAX
- h
->password_change_max_usec
) {
1880 added
= h
->password_change_inactive_usec
+ h
->password_change_max_usec
;
1881 if (added
< UINT64_MAX
- h
->last_password_change_usec
&&
1882 n
>= h
->last_password_change_usec
+ added
)
1883 return -EKEYREJECTED
;
1886 /* Password needs to be changed now? */
1887 change_before
= h
->last_password_change_usec
+ h
->password_change_max_usec
;
1888 if (n
>= change_before
)
1889 return change_permitted
? -EOWNERDEAD
: -EKEYREJECTED
;
1892 if (h
->password_change_warn_usec
!= UINT64_MAX
&&
1893 (change_before
< h
->password_change_warn_usec
||
1894 n
>= change_before
- h
->password_change_warn_usec
))
1895 return change_permitted
? -EKEYEXPIRED
: -EROFS
;
1898 /* No password changing necessary */
1899 return change_permitted
? 0 : -EROFS
;
1902 static const char* const user_storage_table
[_USER_STORAGE_MAX
] = {
1903 [USER_CLASSIC
] = "classic",
1904 [USER_LUKS
] = "luks",
1905 [USER_DIRECTORY
] = "directory",
1906 [USER_SUBVOLUME
] = "subvolume",
1907 [USER_FSCRYPT
] = "fscrypt",
1908 [USER_CIFS
] = "cifs",
1911 DEFINE_STRING_TABLE_LOOKUP(user_storage
, UserStorage
);
1913 static const char* const user_disposition_table
[_USER_DISPOSITION_MAX
] = {
1914 [USER_INTRINSIC
] = "intrinsic",
1915 [USER_SYSTEM
] = "system",
1916 [USER_DYNAMIC
] = "dynamic",
1917 [USER_REGULAR
] = "regular",
1918 [USER_CONTAINER
] = "container",
1919 [USER_RESERVED
] = "reserved",
1922 DEFINE_STRING_TABLE_LOOKUP(user_disposition
, UserDisposition
);