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_volume_key_size
= UINT64_MAX
,
56 .luks_pbkdf_time_cost_usec
= UINT64_MAX
,
57 .luks_pbkdf_memory_cost
= UINT64_MAX
,
58 .luks_pbkdf_parallel_threads
= UINT64_MAX
,
59 .disk_usage
= UINT64_MAX
,
60 .disk_free
= UINT64_MAX
,
61 .disk_ceiling
= UINT64_MAX
,
62 .disk_floor
= UINT64_MAX
,
64 .good_authentication_counter
= UINT64_MAX
,
65 .bad_authentication_counter
= UINT64_MAX
,
66 .last_good_authentication_usec
= UINT64_MAX
,
67 .last_bad_authentication_usec
= UINT64_MAX
,
68 .ratelimit_begin_usec
= UINT64_MAX
,
69 .ratelimit_count
= UINT64_MAX
,
70 .ratelimit_interval_usec
= UINT64_MAX
,
71 .ratelimit_burst
= UINT64_MAX
,
73 .enforce_password_policy
= -1,
75 .stop_delay_usec
= UINT64_MAX
,
77 .password_change_min_usec
= UINT64_MAX
,
78 .password_change_max_usec
= UINT64_MAX
,
79 .password_change_warn_usec
= UINT64_MAX
,
80 .password_change_inactive_usec
= UINT64_MAX
,
81 .password_change_now
= -1,
82 .pkcs11_protected_authentication_path_permitted
= -1,
88 static void pkcs11_encrypted_key_done(Pkcs11EncryptedKey
*k
) {
93 erase_and_free(k
->data
);
94 erase_and_free(k
->hashed_password
);
97 static UserRecord
* user_record_free(UserRecord
*h
) {
103 free(h
->user_name_and_realm_auto
);
105 free(h
->email_address
);
106 erase_and_free(h
->password_hint
);
112 strv_free(h
->environment
);
114 free(h
->preferred_language
);
115 rlimit_free_all(h
->rlimits
);
117 free(h
->skeleton_directory
);
119 strv_free_erase(h
->hashed_password
);
120 strv_free_erase(h
->ssh_authorized_keys
);
121 strv_free_erase(h
->password
);
122 strv_free_erase(h
->pkcs11_pin
);
124 free(h
->cifs_service
);
125 free(h
->cifs_user_name
);
126 free(h
->cifs_domain
);
129 free(h
->image_path_auto
);
130 free(h
->home_directory
);
131 free(h
->home_directory_auto
);
133 strv_free(h
->member_of
);
135 free(h
->file_system_type
);
136 free(h
->luks_cipher
);
137 free(h
->luks_cipher_mode
);
138 free(h
->luks_pbkdf_hash_algorithm
);
139 free(h
->luks_pbkdf_type
);
144 strv_free(h
->pkcs11_token_uri
);
145 for (size_t i
= 0; i
< h
->n_pkcs11_encrypted_key
; i
++)
146 pkcs11_encrypted_key_done(h
->pkcs11_encrypted_key
+ i
);
147 free(h
->pkcs11_encrypted_key
);
149 json_variant_unref(h
->json
);
154 DEFINE_TRIVIAL_REF_UNREF_FUNC(UserRecord
, user_record
, user_record_free
);
156 int json_dispatch_realm(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
161 if (json_variant_is_null(variant
)) {
166 if (!json_variant_is_string(variant
))
167 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
169 n
= json_variant_string(variant
);
170 r
= dns_name_is_valid(n
);
172 return json_log(variant
, flags
, r
, "Failed to check if JSON field '%s' is a valid DNS domain.", strna(name
));
174 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid DNS domain.", strna(name
));
176 r
= free_and_strdup(s
, n
);
178 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
183 static int json_dispatch_gecos(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
188 if (json_variant_is_null(variant
)) {
193 if (!json_variant_is_string(variant
))
194 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
196 n
= json_variant_string(variant
);
198 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid GECOS compatible real name.", strna(name
));
200 r
= free_and_strdup(s
, n
);
202 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
207 static int json_dispatch_nice(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
211 if (json_variant_is_null(variant
)) {
216 if (!json_variant_is_integer(variant
))
217 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
219 m
= json_variant_integer(variant
);
220 if (m
< PRIO_MIN
|| m
>= PRIO_MAX
)
221 return json_log(variant
, flags
, SYNTHETIC_ERRNO(ERANGE
), "JSON field '%s' is not a valid nice level.", strna(name
));
227 static int json_dispatch_rlimit_value(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
228 rlim_t
*ret
= userdata
;
230 if (json_variant_is_null(variant
))
231 *ret
= RLIM_INFINITY
;
232 else if (json_variant_is_unsigned(variant
)) {
235 w
= json_variant_unsigned(variant
);
236 if (w
== RLIM_INFINITY
|| (uintmax_t) w
!= json_variant_unsigned(variant
))
237 return json_log(variant
, flags
, SYNTHETIC_ERRNO(ERANGE
), "Resource limit value '%s' is out of range.", name
);
241 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit value '%s' is not an unsigned integer.", name
);
246 static int json_dispatch_rlimits(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
247 struct rlimit
** limits
= userdata
;
254 if (json_variant_is_null(variant
)) {
255 rlimit_free_all(limits
);
259 if (!json_variant_is_object(variant
))
260 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
262 JSON_VARIANT_OBJECT_FOREACH(key
, value
, variant
) {
263 JsonVariant
*jcur
, *jmax
;
268 p
= startswith(key
, "RLIMIT_");
272 l
= rlimit_from_string(p
);
274 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' not known.", key
);
276 if (!json_variant_is_object(value
))
277 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' has invalid value.", key
);
279 if (json_variant_elements(value
) != 4)
280 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' value is does not have two fields as expected.", key
);
282 jcur
= json_variant_by_key(value
, "cur");
284 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' lacks 'cur' field.", key
);
285 r
= json_dispatch_rlimit_value("cur", jcur
, flags
, &rl
.rlim_cur
);
289 jmax
= json_variant_by_key(value
, "max");
291 return json_log(value
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Resource limit '%s' lacks 'max' field.", key
);
292 r
= json_dispatch_rlimit_value("max", jmax
, flags
, &rl
.rlim_max
);
299 limits
[l
] = newdup(struct rlimit
, &rl
, 1);
308 static int json_dispatch_filename_or_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
315 if (json_variant_is_null(variant
)) {
320 if (!json_variant_is_string(variant
))
321 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
323 n
= json_variant_string(variant
);
324 if (!filename_is_valid(n
) && !path_is_normalized(n
))
325 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid file name or normalized path.", strna(name
));
327 r
= free_and_strdup(s
, n
);
329 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
334 static int json_dispatch_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
339 if (json_variant_is_null(variant
)) {
344 if (!json_variant_is_string(variant
))
345 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
347 n
= json_variant_string(variant
);
348 if (!path_is_normalized(n
))
349 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a normalized file system path.", strna(name
));
350 if (!path_is_absolute(n
))
351 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an absolute file system path.", strna(name
));
353 r
= free_and_strdup(s
, n
);
355 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
360 static int json_dispatch_home_directory(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
365 if (json_variant_is_null(variant
)) {
370 if (!json_variant_is_string(variant
))
371 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
373 n
= json_variant_string(variant
);
375 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid home directory path.", strna(name
));
377 r
= free_and_strdup(s
, n
);
379 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
384 static int json_dispatch_image_path(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
389 if (json_variant_is_null(variant
)) {
394 if (!json_variant_is_string(variant
))
395 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
397 n
= json_variant_string(variant
);
398 if (empty_or_root(n
) || !path_is_valid(n
) || !path_is_absolute(n
))
399 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid image path.", strna(name
));
401 r
= free_and_strdup(s
, n
);
403 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
408 static int json_dispatch_umask(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
409 mode_t
*m
= userdata
;
412 if (json_variant_is_null(variant
)) {
417 if (!json_variant_is_unsigned(variant
))
418 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a number.", strna(name
));
420 k
= json_variant_unsigned(variant
);
422 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' outside of valid range 0…0777.", strna(name
));
428 static int json_dispatch_access_mode(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
429 mode_t
*m
= userdata
;
432 if (json_variant_is_null(variant
)) {
437 if (!json_variant_is_unsigned(variant
))
438 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a number.", strna(name
));
440 k
= json_variant_unsigned(variant
);
442 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' outside of valid range 0…07777.", strna(name
));
448 static int json_dispatch_environment(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
449 _cleanup_strv_free_
char **n
= NULL
;
450 char ***l
= userdata
;
454 if (json_variant_is_null(variant
)) {
459 if (!json_variant_is_array(variant
))
460 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array.", strna(name
));
462 for (i
= 0; i
< json_variant_elements(variant
); i
++) {
463 _cleanup_free_
char *c
= NULL
;
467 e
= json_variant_by_index(variant
, i
);
468 if (!json_variant_is_string(e
))
469 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of strings.", strna(name
));
471 assert_se(a
= json_variant_string(e
));
473 if (!env_assignment_is_valid(a
))
474 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of environment variables.", strna(name
));
478 return json_log_oom(variant
, flags
);
480 r
= strv_env_replace(&n
, c
);
482 return json_log_oom(variant
, flags
);
487 strv_free_and_replace(*l
, n
);
491 int json_dispatch_user_disposition(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
492 UserDisposition
*disposition
= userdata
, k
;
494 if (json_variant_is_null(variant
)) {
495 *disposition
= _USER_DISPOSITION_INVALID
;
499 if (!json_variant_is_string(variant
))
500 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
502 k
= user_disposition_from_string(json_variant_string(variant
));
504 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Disposition type '%s' not known.", json_variant_string(variant
));
510 static int json_dispatch_storage(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
511 UserStorage
*storage
= userdata
, k
;
513 if (json_variant_is_null(variant
)) {
514 *storage
= _USER_STORAGE_INVALID
;
518 if (!json_variant_is_string(variant
))
519 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
521 k
= user_storage_from_string(json_variant_string(variant
));
523 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "Storage type '%s' not known.", json_variant_string(variant
));
529 static int json_dispatch_disk_size(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
530 uint64_t *size
= userdata
;
533 if (json_variant_is_null(variant
)) {
538 if (!json_variant_is_unsigned(variant
))
539 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an integer.", strna(name
));
541 k
= json_variant_unsigned(variant
);
542 if (k
< USER_DISK_SIZE_MIN
|| k
> USER_DISK_SIZE_MAX
)
543 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
);
549 static int json_dispatch_tasks_or_memory_max(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
550 uint64_t *limit
= userdata
;
553 if (json_variant_is_null(variant
)) {
558 if (!json_variant_is_unsigned(variant
))
559 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a integer.", strna(name
));
561 k
= json_variant_unsigned(variant
);
562 if (k
<= 0 || k
>= UINT64_MAX
)
563 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);
569 static int json_dispatch_weight(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
570 uint64_t *weight
= userdata
;
573 if (json_variant_is_null(variant
)) {
574 *weight
= UINT64_MAX
;
578 if (!json_variant_is_unsigned(variant
))
579 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a integer.", strna(name
));
581 k
= json_variant_unsigned(variant
);
582 if (k
<= CGROUP_WEIGHT_MIN
|| k
>= CGROUP_WEIGHT_MAX
)
583 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
);
589 int json_dispatch_user_group_list(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
590 _cleanup_strv_free_
char **l
= NULL
;
591 char ***list
= userdata
;
595 if (!json_variant_is_array(variant
))
596 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of strings.", strna(name
));
598 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
600 if (!json_variant_is_string(e
))
601 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
603 if (!valid_user_group_name(json_variant_string(e
), FLAGS_SET(flags
, JSON_RELAX
) ? VALID_USER_RELAX
: 0))
604 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a valid user/group name: %s", json_variant_string(e
));
606 r
= strv_extend(&l
, json_variant_string(e
));
608 return json_log(e
, flags
, r
, "Failed to append array element: %m");
611 r
= strv_extend_strv(list
, l
, true);
613 return json_log(variant
, flags
, r
, "Failed to merge user/group arrays: %m");
618 static int dispatch_secret(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
620 static const JsonDispatch secret_dispatch_table
[] = {
621 { "password", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, password
), 0 },
622 { "pkcs11Pin", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, pkcs11_pin
), 0 },
623 { "pkcs11ProtectedAuthenticationPathPermitted", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, pkcs11_protected_authentication_path_permitted
), 0 },
627 return json_dispatch(variant
, secret_dispatch_table
, NULL
, flags
, userdata
);
630 static int dispatch_pkcs11_uri(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
635 if (json_variant_is_null(variant
)) {
640 if (!json_variant_is_string(variant
))
641 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
643 n
= json_variant_string(variant
);
644 if (!pkcs11_uri_valid(n
))
645 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name
));
647 r
= free_and_strdup(s
, n
);
649 return json_log(variant
, flags
, r
, "Failed to allocate string: %m");
654 static int dispatch_pkcs11_uri_array(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
655 _cleanup_strv_free_
char **z
= NULL
;
656 char ***l
= userdata
;
660 if (json_variant_is_null(variant
)) {
665 if (json_variant_is_string(variant
)) {
668 n
= json_variant_string(variant
);
669 if (!pkcs11_uri_valid(n
))
670 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name
));
678 if (!json_variant_is_array(variant
))
679 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string or array of strings.", strna(name
));
681 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
684 if (!json_variant_is_string(e
))
685 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not a string.");
687 n
= json_variant_string(e
);
688 if (!pkcs11_uri_valid(n
))
689 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
);
691 r
= strv_extend(&z
, n
);
697 strv_free_and_replace(*l
, z
);
701 static int dispatch_pkcs11_key_data(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
702 Pkcs11EncryptedKey
*k
= userdata
;
707 if (json_variant_is_null(variant
)) {
708 k
->data
= mfree(k
->data
);
713 if (!json_variant_is_string(variant
))
714 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not a string.", strna(name
));
716 r
= unbase64mem(json_variant_string(variant
), (size_t) -1, &b
, &l
);
718 return json_log(variant
, flags
, r
, "Failed to decode encrypted PKCS#11 key: %m");
720 erase_and_free(k
->data
);
727 static int dispatch_pkcs11_key(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
728 UserRecord
*h
= userdata
;
732 if (!json_variant_is_array(variant
))
733 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
735 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
736 Pkcs11EncryptedKey
*array
, *k
;
738 static const JsonDispatch pkcs11_key_dispatch_table
[] = {
739 { "uri", JSON_VARIANT_STRING
, dispatch_pkcs11_uri
, offsetof(Pkcs11EncryptedKey
, uri
), JSON_MANDATORY
},
740 { "data", JSON_VARIANT_STRING
, dispatch_pkcs11_key_data
, 0, JSON_MANDATORY
},
741 { "hashedPassword", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(Pkcs11EncryptedKey
, hashed_password
), JSON_MANDATORY
},
745 if (!json_variant_is_object(e
))
746 return json_log(e
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON array element is not an object.");
748 array
= reallocarray(h
->pkcs11_encrypted_key
, h
->n_pkcs11_encrypted_key
+ 1, sizeof(Pkcs11EncryptedKey
));
752 h
->pkcs11_encrypted_key
= array
;
753 k
= h
->pkcs11_encrypted_key
+ h
->n_pkcs11_encrypted_key
;
754 *k
= (Pkcs11EncryptedKey
) {};
756 r
= json_dispatch(e
, pkcs11_key_dispatch_table
, NULL
, flags
, k
);
758 pkcs11_encrypted_key_done(k
);
762 h
->n_pkcs11_encrypted_key
++;
768 static int dispatch_privileged(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
770 static const JsonDispatch privileged_dispatch_table
[] = {
771 { "passwordHint", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, password_hint
), 0 },
772 { "hashedPassword", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, hashed_password
), JSON_SAFE
},
773 { "sshAuthorizedKeys", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_strv
, offsetof(UserRecord
, ssh_authorized_keys
), 0 },
774 { "pkcs11EncryptedKey", JSON_VARIANT_ARRAY
, dispatch_pkcs11_key
, 0, 0 },
778 return json_dispatch(variant
, privileged_dispatch_table
, NULL
, flags
, userdata
);
781 static int dispatch_binding(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
783 static const JsonDispatch binding_dispatch_table
[] = {
784 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_image_path
, offsetof(UserRecord
, image_path
), 0 },
785 { "homeDirectory", JSON_VARIANT_STRING
, json_dispatch_home_directory
, offsetof(UserRecord
, home_directory
), 0 },
786 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
787 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
788 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
789 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
790 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
791 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
792 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
793 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
794 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
795 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
799 char smid
[SD_ID128_STRING_MAX
];
807 if (!json_variant_is_object(variant
))
808 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
810 r
= sd_id128_get_machine(&mid
);
812 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
814 m
= json_variant_by_key(variant
, sd_id128_to_string(mid
, smid
));
818 return json_dispatch(m
, binding_dispatch_table
, NULL
, flags
, userdata
);
821 int per_machine_id_match(JsonVariant
*ids
, JsonDispatchFlags flags
) {
825 r
= sd_id128_get_machine(&mid
);
827 return json_log(ids
, flags
, r
, "Failed to acquire machine ID: %m");
829 if (json_variant_is_string(ids
)) {
832 r
= sd_id128_from_string(json_variant_string(ids
), &k
);
834 json_log(ids
, flags
, r
, "%s is not a valid machine ID, ignoring: %m", json_variant_string(ids
));
838 return sd_id128_equal(mid
, k
);
841 if (json_variant_is_array(ids
)) {
844 JSON_VARIANT_ARRAY_FOREACH(e
, ids
) {
847 if (!json_variant_is_string(e
)) {
848 json_log(e
, flags
, 0, "Machine ID is not a string, ignoring: %m");
852 r
= sd_id128_from_string(json_variant_string(e
), &k
);
854 json_log(e
, flags
, r
, "%s is not a valid machine ID, ignoring: %m", json_variant_string(e
));
858 if (sd_id128_equal(mid
, k
))
865 json_log(ids
, flags
, 0, "Machine ID is not a string or array of strings, ignoring: %m");
869 int per_machine_hostname_match(JsonVariant
*hns
, JsonDispatchFlags flags
) {
870 _cleanup_free_
char *hn
= NULL
;
873 r
= gethostname_strict(&hn
);
875 json_log(hns
, flags
, r
, "No hostname set, not matching perMachine hostname record: %m");
879 return json_log(hns
, flags
, r
, "Failed to acquire hostname: %m");
881 if (json_variant_is_string(hns
))
882 return streq(json_variant_string(hns
), hn
);
884 if (json_variant_is_array(hns
)) {
887 JSON_VARIANT_ARRAY_FOREACH(e
, hns
) {
889 if (!json_variant_is_string(e
)) {
890 json_log(e
, flags
, 0, "Hostname is not a string, ignoring: %m");
894 if (streq(json_variant_string(hns
), hn
))
901 json_log(hns
, flags
, 0, "Hostname is not a string or array of strings, ignoring: %m");
905 static int dispatch_per_machine(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
907 static const JsonDispatch per_machine_dispatch_table
[] = {
908 { "matchMachineId", _JSON_VARIANT_TYPE_INVALID
, NULL
, 0, 0 },
909 { "matchHostname", _JSON_VARIANT_TYPE_INVALID
, NULL
, 0, 0 },
910 { "iconName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, icon_name
), JSON_SAFE
},
911 { "location", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, location
), 0 },
912 { "shell", JSON_VARIANT_STRING
, json_dispatch_filename_or_path
, offsetof(UserRecord
, shell
), 0 },
913 { "umask", JSON_VARIANT_UNSIGNED
, json_dispatch_umask
, offsetof(UserRecord
, umask
), 0 },
914 { "environment", JSON_VARIANT_ARRAY
, json_dispatch_environment
, offsetof(UserRecord
, environment
), 0 },
915 { "timeZone", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, time_zone
), JSON_SAFE
},
916 { "preferredLanguage", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, preferred_language
), JSON_SAFE
},
917 { "niceLevel", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_nice
, offsetof(UserRecord
, nice_level
), 0 },
918 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_rlimits
, offsetof(UserRecord
, rlimits
), 0 },
919 { "locked", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, locked
), 0 },
920 { "notBeforeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_before_usec
), 0 },
921 { "notAfterUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_after_usec
), 0 },
922 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
923 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_disk_size
, offsetof(UserRecord
, disk_size
), 0 },
924 { "diskSizeRelative", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size_relative
), 0 },
925 { "skeletonDirectory", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, skeleton_directory
), 0 },
926 { "accessMode", JSON_VARIANT_UNSIGNED
, json_dispatch_access_mode
, offsetof(UserRecord
, access_mode
), 0 },
927 { "tasksMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, tasks_max
), 0 },
928 { "memoryHigh", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_high
), 0 },
929 { "memoryMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_max
), 0 },
930 { "cpuWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, cpu_weight
), 0 },
931 { "ioWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, io_weight
), 0 },
932 { "mountNoDevices", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nodev
), 0 },
933 { "mountNoSuid", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nosuid
), 0 },
934 { "mountNoExecute", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, noexec
), 0 },
935 { "cifsDomain", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_domain
), JSON_SAFE
},
936 { "cifsUserName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_user_name
), JSON_SAFE
},
937 { "cifsService", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_service
), JSON_SAFE
},
938 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, image_path
), 0 },
939 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
940 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
941 { "memberOf", JSON_VARIANT_ARRAY
, json_dispatch_user_group_list
, offsetof(UserRecord
, member_of
), JSON_RELAX
},
942 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
943 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
944 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
945 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
946 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_discard
), 0, },
947 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
948 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
949 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
950 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_hash_algorithm
), JSON_SAFE
},
951 { "luksPbkdfType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_type
), JSON_SAFE
},
952 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_time_cost_usec
), 0 },
953 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_memory_cost
), 0 },
954 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_parallel_threads
), 0 },
955 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_interval_usec
), 0 },
956 { "rateLimitBurst", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_burst
), 0 },
957 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, enforce_password_policy
), 0 },
958 { "autoLogin", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, auto_login
), 0 },
959 { "stopDelayUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, stop_delay_usec
), 0 },
960 { "killProcesses", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, kill_processes
), 0 },
961 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_min_usec
), 0 },
962 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_max_usec
), 0 },
963 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_warn_usec
), 0 },
964 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_inactive_usec
), 0 },
965 { "passwordChangeNow", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, password_change_now
), 0 },
966 { "pkcs11TokenUri", JSON_VARIANT_ARRAY
, dispatch_pkcs11_uri_array
, offsetof(UserRecord
, pkcs11_token_uri
), 0 },
976 if (!json_variant_is_array(variant
))
977 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array.", strna(name
));
979 JSON_VARIANT_ARRAY_FOREACH(e
, variant
) {
980 bool matching
= false;
983 if (!json_variant_is_object(e
))
984 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an array of objects.", strna(name
));
986 m
= json_variant_by_key(e
, "matchMachineId");
988 r
= per_machine_id_match(m
, flags
);
996 m
= json_variant_by_key(e
, "matchHostname");
998 r
= per_machine_hostname_match(m
, flags
);
1009 r
= json_dispatch(e
, per_machine_dispatch_table
, NULL
, flags
, userdata
);
1017 static int dispatch_status(const char *name
, JsonVariant
*variant
, JsonDispatchFlags flags
, void *userdata
) {
1019 static const JsonDispatch status_dispatch_table
[] = {
1020 { "diskUsage", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_usage
), 0 },
1021 { "diskFree", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_free
), 0 },
1022 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size
), 0 },
1023 { "diskCeiling", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_ceiling
), 0 },
1024 { "diskFloor", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_floor
), 0 },
1025 { "state", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, state
), JSON_SAFE
},
1026 { "service", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, service
), JSON_SAFE
},
1027 { "signedLocally", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, signed_locally
), 0 },
1028 { "goodAuthenticationCounter", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, good_authentication_counter
), 0 },
1029 { "badAuthenticationCounter", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, bad_authentication_counter
), 0 },
1030 { "lastGoodAuthenticationUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_good_authentication_usec
), 0 },
1031 { "lastBadAuthenticationUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_bad_authentication_usec
), 0 },
1032 { "rateLimitBeginUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_begin_usec
), 0 },
1033 { "rateLimitCount", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_count
), 0 },
1034 { "removable", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, removable
), 0 },
1038 char smid
[SD_ID128_STRING_MAX
];
1046 if (!json_variant_is_object(variant
))
1047 return json_log(variant
, flags
, SYNTHETIC_ERRNO(EINVAL
), "JSON field '%s' is not an object.", strna(name
));
1049 r
= sd_id128_get_machine(&mid
);
1051 return json_log(variant
, flags
, r
, "Failed to determine machine ID: %m");
1053 m
= json_variant_by_key(variant
, sd_id128_to_string(mid
, smid
));
1057 return json_dispatch(m
, status_dispatch_table
, NULL
, flags
, userdata
);
1060 static int user_record_augment(UserRecord
*h
, JsonDispatchFlags json_flags
) {
1063 if (!FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
))
1066 assert(h
->user_name
);
1068 if (!h
->user_name_and_realm_auto
&& h
->realm
) {
1069 h
->user_name_and_realm_auto
= strjoin(h
->user_name
, "@", h
->realm
);
1070 if (!h
->user_name_and_realm_auto
)
1071 return json_log_oom(h
->json
, json_flags
);
1074 /* Let's add in the following automatisms only for regular users, they don't make sense for any others */
1075 if (user_record_disposition(h
) != USER_REGULAR
)
1078 if (!h
->home_directory
&& !h
->home_directory_auto
) {
1079 h
->home_directory_auto
= path_join("/home/", h
->user_name
);
1080 if (!h
->home_directory_auto
)
1081 return json_log_oom(h
->json
, json_flags
);
1084 if (!h
->image_path
&& !h
->image_path_auto
) {
1086 UserStorage storage
;
1088 storage
= user_record_storage(h
);
1089 if (storage
== USER_LUKS
)
1091 else if (IN_SET(storage
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
))
1092 suffix
= ".homedir";
1097 h
->image_path_auto
= strjoin("/home/", user_record_user_name_and_realm(h
), suffix
);
1098 if (!h
->image_path_auto
)
1099 return json_log_oom(h
->json
, json_flags
);
1106 int user_group_record_mangle(
1108 UserRecordLoadFlags load_flags
,
1109 JsonVariant
**ret_variant
,
1110 UserRecordMask
*ret_mask
) {
1112 static const struct {
1113 UserRecordMask mask
;
1116 { USER_RECORD_PRIVILEGED
, "privileged" },
1117 { USER_RECORD_SECRET
, "secret" },
1118 { USER_RECORD_BINDING
, "binding" },
1119 { USER_RECORD_PER_MACHINE
, "perMachine" },
1120 { USER_RECORD_STATUS
, "status" },
1121 { USER_RECORD_SIGNATURE
, "signature" },
1124 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1125 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
;
1126 JsonVariant
*array
[ELEMENTSOF(mask_field
) * 2];
1127 size_t n_retain
= 0, i
;
1128 UserRecordMask m
= 0;
1131 assert((load_flags
& _USER_RECORD_MASK_MAX
) == 0); /* detect mistakes when accidentally passing
1132 * UserRecordMask bit masks as UserRecordLoadFlags
1136 assert(ret_variant
);
1139 /* Note that this function is shared with the group record parser, hence we try to be generic in our
1140 * log message wording here, to cover both cases. */
1142 if (!json_variant_is_object(v
))
1143 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is not a JSON object, refusing.");
1145 if (USER_RECORD_ALLOW_MASK(load_flags
) == 0) /* allow nothing? */
1146 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "Nothing allowed in record, refusing.");
1148 if (USER_RECORD_STRIP_MASK(load_flags
) == _USER_RECORD_MASK_MAX
) /* strip everything? */
1149 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "Stripping everything from record, refusing.");
1151 /* Check if we have the special sections and if they match our flags set */
1152 for (i
= 0; i
< ELEMENTSOF(mask_field
); i
++) {
1155 if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags
), mask_field
[i
].mask
)) {
1157 w
= json_variant_ref(v
);
1159 r
= json_variant_filter(&w
, STRV_MAKE(mask_field
[i
].name
));
1161 return json_log(w
, json_flags
, r
, "Failed to remove field from variant: %m");
1166 e
= json_variant_by_key_full(v
, mask_field
[i
].name
, &k
);
1168 if (!FLAGS_SET(USER_RECORD_ALLOW_MASK(load_flags
), mask_field
[i
].mask
))
1169 return json_log(e
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record contains '%s' field, which is not allowed.", mask_field
[i
].name
);
1171 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1172 array
[n_retain
++] = k
;
1173 array
[n_retain
++] = e
;
1176 m
|= mask_field
[i
].mask
;
1178 if (FLAGS_SET(USER_RECORD_REQUIRE_MASK(load_flags
), mask_field
[i
].mask
))
1179 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record lacks '%s' field, which is required.", mask_field
[i
].name
);
1183 if (FLAGS_SET(load_flags
, USER_RECORD_STRIP_REGULAR
)) {
1184 /* If we are supposed to strip regular items, then let's instead just allocate a new object
1185 * with just the stuff we need. */
1187 w
= json_variant_unref(w
);
1188 r
= json_variant_new_object(&w
, array
, n_retain
);
1190 return json_log(v
, json_flags
, r
, "Failed to allocate new object: %m");
1192 /* And now check if there's anything else in the record */
1193 for (i
= 0; i
< json_variant_elements(v
); i
+= 2) {
1195 bool special
= false;
1198 assert_se(f
= json_variant_string(json_variant_by_index(v
, i
)));
1200 for (j
= 0; j
< ELEMENTSOF(mask_field
); j
++)
1201 if (streq(f
, mask_field
[j
].name
)) { /* already covered in the loop above */
1207 if ((load_flags
& (USER_RECORD_ALLOW_REGULAR
|USER_RECORD_REQUIRE_REGULAR
)) == 0)
1208 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record contains '%s' field, which is not allowed.", f
);
1210 m
|= USER_RECORD_REGULAR
;
1216 if (FLAGS_SET(load_flags
, USER_RECORD_REQUIRE_REGULAR
) && !FLAGS_SET(m
, USER_RECORD_REGULAR
))
1217 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record lacks basic identity fields, which are required.");
1220 return json_log(v
, json_flags
, SYNTHETIC_ERRNO(EBADMSG
), "Record is empty.");
1223 *ret_variant
= TAKE_PTR(w
);
1225 *ret_variant
= json_variant_ref(v
);
1231 int user_record_load(UserRecord
*h
, JsonVariant
*v
, UserRecordLoadFlags load_flags
) {
1233 static const JsonDispatch user_dispatch_table
[] = {
1234 { "userName", JSON_VARIANT_STRING
, json_dispatch_user_group_name
, offsetof(UserRecord
, user_name
), JSON_RELAX
},
1235 { "realm", JSON_VARIANT_STRING
, json_dispatch_realm
, offsetof(UserRecord
, realm
), 0 },
1236 { "realName", JSON_VARIANT_STRING
, json_dispatch_gecos
, offsetof(UserRecord
, real_name
), 0 },
1237 { "emailAddress", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, email_address
), JSON_SAFE
},
1238 { "iconName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, icon_name
), JSON_SAFE
},
1239 { "location", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, location
), 0 },
1240 { "disposition", JSON_VARIANT_STRING
, json_dispatch_user_disposition
, offsetof(UserRecord
, disposition
), 0 },
1241 { "lastChangeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_change_usec
), 0 },
1242 { "lastPasswordChangeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, last_password_change_usec
), 0 },
1243 { "shell", JSON_VARIANT_STRING
, json_dispatch_filename_or_path
, offsetof(UserRecord
, shell
), 0 },
1244 { "umask", JSON_VARIANT_UNSIGNED
, json_dispatch_umask
, offsetof(UserRecord
, umask
), 0 },
1245 { "environment", JSON_VARIANT_ARRAY
, json_dispatch_environment
, offsetof(UserRecord
, environment
), 0 },
1246 { "timeZone", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, time_zone
), JSON_SAFE
},
1247 { "preferredLanguage", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, preferred_language
), JSON_SAFE
},
1248 { "niceLevel", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_nice
, offsetof(UserRecord
, nice_level
), 0 },
1249 { "resourceLimits", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_rlimits
, offsetof(UserRecord
, rlimits
), 0 },
1250 { "locked", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, locked
), 0 },
1251 { "notBeforeUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_before_usec
), 0 },
1252 { "notAfterUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, not_after_usec
), 0 },
1253 { "storage", JSON_VARIANT_STRING
, json_dispatch_storage
, offsetof(UserRecord
, storage
), 0 },
1254 { "diskSize", JSON_VARIANT_UNSIGNED
, json_dispatch_disk_size
, offsetof(UserRecord
, disk_size
), 0 },
1255 { "diskSizeRelative", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, disk_size_relative
), 0 },
1256 { "skeletonDirectory", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, skeleton_directory
), 0 },
1257 { "accessMode", JSON_VARIANT_UNSIGNED
, json_dispatch_access_mode
, offsetof(UserRecord
, access_mode
), 0 },
1258 { "tasksMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, tasks_max
), 0 },
1259 { "memoryHigh", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_high
), 0 },
1260 { "memoryMax", JSON_VARIANT_UNSIGNED
, json_dispatch_tasks_or_memory_max
, offsetof(UserRecord
, memory_max
), 0 },
1261 { "cpuWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, cpu_weight
), 0 },
1262 { "ioWeight", JSON_VARIANT_UNSIGNED
, json_dispatch_weight
, offsetof(UserRecord
, io_weight
), 0 },
1263 { "mountNoDevices", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nodev
), 0 },
1264 { "mountNoSuid", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, nosuid
), 0 },
1265 { "mountNoExecute", JSON_VARIANT_BOOLEAN
, json_dispatch_boolean
, offsetof(UserRecord
, noexec
), 0 },
1266 { "cifsDomain", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_domain
), JSON_SAFE
},
1267 { "cifsUserName", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_user_name
), JSON_SAFE
},
1268 { "cifsService", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, cifs_service
), JSON_SAFE
},
1269 { "imagePath", JSON_VARIANT_STRING
, json_dispatch_path
, offsetof(UserRecord
, image_path
), 0 },
1270 { "homeDirectory", JSON_VARIANT_STRING
, json_dispatch_home_directory
, offsetof(UserRecord
, home_directory
), 0 },
1271 { "uid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, uid
), 0 },
1272 { "gid", JSON_VARIANT_UNSIGNED
, json_dispatch_uid_gid
, offsetof(UserRecord
, gid
), 0 },
1273 { "memberOf", JSON_VARIANT_ARRAY
, json_dispatch_user_group_list
, offsetof(UserRecord
, member_of
), JSON_RELAX
},
1274 { "fileSystemType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, file_system_type
), JSON_SAFE
},
1275 { "partitionUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, partition_uuid
), 0 },
1276 { "luksUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, luks_uuid
), 0 },
1277 { "fileSystemUuid", JSON_VARIANT_STRING
, json_dispatch_id128
, offsetof(UserRecord
, file_system_uuid
), 0 },
1278 { "luksDiscard", _JSON_VARIANT_TYPE_INVALID
, json_dispatch_tristate
, offsetof(UserRecord
, luks_discard
), 0 },
1279 { "luksCipher", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher
), JSON_SAFE
},
1280 { "luksCipherMode", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_cipher_mode
), JSON_SAFE
},
1281 { "luksVolumeKeySize", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_volume_key_size
), 0 },
1282 { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_hash_algorithm
), JSON_SAFE
},
1283 { "luksPbkdfType", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, luks_pbkdf_type
), JSON_SAFE
},
1284 { "luksPbkdfTimeCostUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_time_cost_usec
), 0 },
1285 { "luksPbkdfMemoryCost", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_memory_cost
), 0 },
1286 { "luksPbkdfParallelThreads", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, luks_pbkdf_parallel_threads
), 0 },
1287 { "service", JSON_VARIANT_STRING
, json_dispatch_string
, offsetof(UserRecord
, service
), JSON_SAFE
},
1288 { "rateLimitIntervalUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_interval_usec
), 0 },
1289 { "rateLimitBurst", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, ratelimit_burst
), 0 },
1290 { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, enforce_password_policy
), 0 },
1291 { "autoLogin", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, auto_login
), 0 },
1292 { "stopDelayUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, stop_delay_usec
), 0 },
1293 { "killProcesses", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, kill_processes
), 0 },
1294 { "passwordChangeMinUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_min_usec
), 0 },
1295 { "passwordChangeMaxUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_max_usec
), 0 },
1296 { "passwordChangeWarnUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_warn_usec
), 0 },
1297 { "passwordChangeInactiveUSec", JSON_VARIANT_UNSIGNED
, json_dispatch_uint64
, offsetof(UserRecord
, password_change_inactive_usec
), 0 },
1298 { "passwordChangeNow", JSON_VARIANT_BOOLEAN
, json_dispatch_tristate
, offsetof(UserRecord
, password_change_now
), 0 },
1299 { "pkcs11TokenUri", JSON_VARIANT_ARRAY
, dispatch_pkcs11_uri_array
, offsetof(UserRecord
, pkcs11_token_uri
), 0 },
1301 { "secret", JSON_VARIANT_OBJECT
, dispatch_secret
, 0, 0 },
1302 { "privileged", JSON_VARIANT_OBJECT
, dispatch_privileged
, 0, 0 },
1304 /* Ignore the perMachine, binding, status stuff here, and process it later, so that it overrides whatever is set above */
1305 { "perMachine", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1306 { "binding", JSON_VARIANT_OBJECT
, NULL
, 0, 0 },
1307 { "status", JSON_VARIANT_OBJECT
, NULL
, 0, 0 },
1309 /* Ignore 'signature', we check it with explicit accessors instead */
1310 { "signature", JSON_VARIANT_ARRAY
, NULL
, 0, 0 },
1314 JsonDispatchFlags json_flags
= USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags
);
1320 /* Note that this call will leave a half-initialized record around on failure! */
1322 r
= user_group_record_mangle(v
, load_flags
, &h
->json
, &h
->mask
);
1326 r
= json_dispatch(h
->json
, user_dispatch_table
, NULL
, json_flags
, h
);
1330 /* During the parsing operation above we ignored the 'perMachine', 'binding' and 'status' fields,
1331 * since we want them to override the global options. Let's process them now. */
1333 r
= dispatch_per_machine("perMachine", json_variant_by_key(h
->json
, "perMachine"), json_flags
, h
);
1337 r
= dispatch_binding("binding", json_variant_by_key(h
->json
, "binding"), json_flags
, h
);
1341 r
= dispatch_status("status", json_variant_by_key(h
->json
, "status"), json_flags
, h
);
1345 if (FLAGS_SET(h
->mask
, USER_RECORD_REGULAR
) && !h
->user_name
)
1346 return json_log(h
->json
, json_flags
, SYNTHETIC_ERRNO(EINVAL
), "User name field missing, refusing.");
1348 r
= user_record_augment(h
, json_flags
);
1355 int user_record_build(UserRecord
**ret
, ...) {
1356 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1357 _cleanup_(user_record_unrefp
) UserRecord
*u
= NULL
;
1364 r
= json_buildv(&v
, ap
);
1370 u
= user_record_new();
1374 r
= user_record_load(u
, v
, USER_RECORD_LOAD_FULL
);
1382 const char *user_record_user_name_and_realm(UserRecord
*h
) {
1385 /* Return the pre-initialized joined string if it is defined */
1386 if (h
->user_name_and_realm_auto
)
1387 return h
->user_name_and_realm_auto
;
1389 /* If it's not defined then we cannot have a realm */
1391 return h
->user_name
;
1394 UserStorage
user_record_storage(UserRecord
*h
) {
1397 if (h
->storage
>= 0)
1400 return USER_CLASSIC
;
1403 const char *user_record_file_system_type(UserRecord
*h
) {
1406 return h
->file_system_type
?: "ext4";
1409 const char *user_record_skeleton_directory(UserRecord
*h
) {
1412 return h
->skeleton_directory
?: "/etc/skel";
1415 mode_t
user_record_access_mode(UserRecord
*h
) {
1418 return h
->access_mode
!= (mode_t
) -1 ? h
->access_mode
: 0700;
1421 const char* user_record_home_directory(UserRecord
*h
) {
1424 if (h
->home_directory
)
1425 return h
->home_directory
;
1426 if (h
->home_directory_auto
)
1427 return h
->home_directory_auto
;
1429 /* The root user is special, hence be special about it */
1430 if (streq_ptr(h
->user_name
, "root"))
1436 const char *user_record_image_path(UserRecord
*h
) {
1440 return h
->image_path
;
1441 if (h
->image_path_auto
)
1442 return h
->image_path_auto
;
1444 return IN_SET(user_record_storage(h
), USER_CLASSIC
, USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
) ? user_record_home_directory(h
) : NULL
;
1447 const char *user_record_cifs_user_name(UserRecord
*h
) {
1450 return h
->cifs_user_name
?: h
->user_name
;
1453 unsigned long user_record_mount_flags(UserRecord
*h
) {
1456 return (h
->nosuid
? MS_NOSUID
: 0) |
1457 (h
->noexec
? MS_NOEXEC
: 0) |
1458 (h
->nodev
? MS_NODEV
: 0);
1461 const char *user_record_shell(UserRecord
*h
) {
1467 if (streq_ptr(h
->user_name
, "root"))
1470 if (user_record_disposition(h
) == USER_REGULAR
)
1476 const char *user_record_real_name(UserRecord
*h
) {
1479 return h
->real_name
?: h
->user_name
;
1482 bool user_record_luks_discard(UserRecord
*h
) {
1487 if (h
->luks_discard
>= 0)
1488 return h
->luks_discard
;
1490 ip
= user_record_image_path(h
);
1494 /* Use discard by default if we are referring to a real block device, but not when operating on a
1495 * loopback device. We want to optimize for SSD and flash storage after all, but we should be careful
1496 * when storing stuff on top of regular file systems in loopback files as doing discard then would
1497 * mean thin provisioning and we should not do that willy-nilly since it means we'll risk EIO later
1498 * on should the disk space to back our file systems not be available. */
1500 return path_startswith(ip
, "/dev/");
1503 const char *user_record_luks_cipher(UserRecord
*h
) {
1506 return h
->luks_cipher
?: "aes";
1509 const char *user_record_luks_cipher_mode(UserRecord
*h
) {
1512 return h
->luks_cipher_mode
?: "xts-plain64";
1515 uint64_t user_record_luks_volume_key_size(UserRecord
*h
) {
1518 /* We return a value here that can be cast without loss into size_t which is what libcrypsetup expects */
1520 if (h
->luks_volume_key_size
== UINT64_MAX
)
1523 return MIN(h
->luks_volume_key_size
, SIZE_MAX
);
1526 const char* user_record_luks_pbkdf_type(UserRecord
*h
) {
1529 return h
->luks_pbkdf_type
?: "argon2i";
1532 uint64_t user_record_luks_pbkdf_time_cost_usec(UserRecord
*h
) {
1535 /* Returns a value with ms granularity, since that's what libcryptsetup expects */
1537 if (h
->luks_pbkdf_time_cost_usec
== UINT64_MAX
)
1538 return 500 * USEC_PER_MSEC
; /* We default to 500ms, in contrast to libcryptsetup's 2s, which is just awfully slow on every login */
1540 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_time_cost_usec
, USEC_PER_MSEC
), UINT32_MAX
) * USEC_PER_MSEC
;
1543 uint64_t user_record_luks_pbkdf_memory_cost(UserRecord
*h
) {
1546 /* Returns a value with kb granularity, since that's what libcryptsetup expects */
1548 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1549 return 64*1024*1024; /* We default to 64M, since this should work on smaller systems too */
1551 return MIN(DIV_ROUND_UP(h
->luks_pbkdf_memory_cost
, 1024), UINT32_MAX
) * 1024;
1554 uint64_t user_record_luks_pbkdf_parallel_threads(UserRecord
*h
) {
1557 if (h
->luks_pbkdf_memory_cost
== UINT64_MAX
)
1558 return 1; /* We default to 1, since this should work on smaller systems too */
1560 return MIN(h
->luks_pbkdf_parallel_threads
, UINT32_MAX
);
1563 const char *user_record_luks_pbkdf_hash_algorithm(UserRecord
*h
) {
1566 return h
->luks_pbkdf_hash_algorithm
?: "sha512";
1569 gid_t
user_record_gid(UserRecord
*h
) {
1572 if (gid_is_valid(h
->gid
))
1575 return (gid_t
) h
->uid
;
1578 UserDisposition
user_record_disposition(UserRecord
*h
) {
1581 if (h
->disposition
>= 0)
1582 return h
->disposition
;
1584 /* If not declared, derive from UID */
1586 if (!uid_is_valid(h
->uid
))
1587 return _USER_DISPOSITION_INVALID
;
1589 if (h
->uid
== 0 || h
->uid
== UID_NOBODY
)
1590 return USER_INTRINSIC
;
1592 if (uid_is_system(h
->uid
))
1595 if (uid_is_dynamic(h
->uid
))
1596 return USER_DYNAMIC
;
1598 if (uid_is_container(h
->uid
))
1599 return USER_CONTAINER
;
1601 if (h
->uid
> INT32_MAX
)
1602 return USER_RESERVED
;
1604 return USER_REGULAR
;
1607 int user_record_removable(UserRecord
*h
) {
1608 UserStorage storage
;
1611 if (h
->removable
>= 0)
1612 return h
->removable
;
1614 /* Refuse to decide for classic records */
1615 storage
= user_record_storage(h
);
1616 if (h
->storage
< 0 || h
->storage
== USER_CLASSIC
)
1619 /* For now consider only LUKS home directories with a reference by path as removable */
1620 return storage
== USER_LUKS
&& path_startswith(user_record_image_path(h
), "/dev/");
1623 uint64_t user_record_ratelimit_interval_usec(UserRecord
*h
) {
1626 if (h
->ratelimit_interval_usec
== UINT64_MAX
)
1627 return DEFAULT_RATELIMIT_INTERVAL_USEC
;
1629 return h
->ratelimit_interval_usec
;
1632 uint64_t user_record_ratelimit_burst(UserRecord
*h
) {
1635 if (h
->ratelimit_burst
== UINT64_MAX
)
1636 return DEFAULT_RATELIMIT_BURST
;
1638 return h
->ratelimit_burst
;
1641 bool user_record_can_authenticate(UserRecord
*h
) {
1644 /* Returns true if there's some form of property configured that the user can authenticate against */
1646 if (h
->n_pkcs11_encrypted_key
> 0)
1649 return !strv_isempty(h
->hashed_password
);
1652 uint64_t user_record_ratelimit_next_try(UserRecord
*h
) {
1655 /* Calculates when the it's possible to login next. Returns:
1657 * UINT64_MAX → Nothing known
1659 * Any other → Next time in CLOCK_REALTIME in usec (which could be in the past)
1662 if (h
->ratelimit_begin_usec
== UINT64_MAX
||
1663 h
->ratelimit_count
== UINT64_MAX
)
1666 if (h
->ratelimit_count
< user_record_ratelimit_burst(h
))
1669 return usec_add(h
->ratelimit_begin_usec
, user_record_ratelimit_interval_usec(h
));
1672 bool user_record_equal(UserRecord
*a
, UserRecord
*b
) {
1676 /* We assume that when a record is modified its JSON data is updated at the same time, hence it's
1677 * sufficient to compare the JSON data. */
1679 return json_variant_equal(a
->json
, b
->json
);
1682 bool user_record_compatible(UserRecord
*a
, UserRecord
*b
) {
1686 /* If either lacks a the regular section, we can't really decide, let's hence say they are
1688 if (!(a
->mask
& b
->mask
& USER_RECORD_REGULAR
))
1691 return streq_ptr(a
->user_name
, b
->user_name
) &&
1692 streq_ptr(a
->realm
, b
->realm
);
1695 int user_record_compare_last_change(UserRecord
*a
, UserRecord
*b
) {
1699 if (a
->last_change_usec
== b
->last_change_usec
)
1702 /* Always consider a record with a timestamp newer than one without */
1703 if (a
->last_change_usec
== UINT64_MAX
)
1705 if (b
->last_change_usec
== UINT64_MAX
)
1708 return CMP(a
->last_change_usec
, b
->last_change_usec
);
1711 int user_record_clone(UserRecord
*h
, UserRecordLoadFlags flags
, UserRecord
**ret
) {
1712 _cleanup_(user_record_unrefp
) UserRecord
*c
= NULL
;
1718 c
= user_record_new();
1722 r
= user_record_load(c
, h
->json
, flags
);
1730 int user_record_masked_equal(UserRecord
*a
, UserRecord
*b
, UserRecordMask mask
) {
1731 _cleanup_(user_record_unrefp
) UserRecord
*x
= NULL
, *y
= NULL
;
1737 /* Compares the two records, but ignores anything not listed in the specified mask */
1739 if ((a
->mask
& ~mask
) != 0) {
1740 r
= user_record_clone(a
, USER_RECORD_ALLOW(mask
) | USER_RECORD_STRIP(~mask
& _USER_RECORD_MASK_MAX
), &x
);
1747 if ((b
->mask
& ~mask
) != 0) {
1748 r
= user_record_clone(b
, USER_RECORD_ALLOW(mask
) | USER_RECORD_STRIP(~mask
& _USER_RECORD_MASK_MAX
), &y
);
1755 return user_record_equal(a
, b
);
1758 int user_record_test_blocked(UserRecord
*h
) {
1761 /* Checks whether access to the specified user shall be allowed at the moment. Returns:
1763 * -ESTALE: Record is from the future
1764 * -ENOLCK: Record is blocked
1765 * -EL2HLT: Record is not valid yet
1766 * -EL3HLT: Record is not valid anymore
1772 n
= now(CLOCK_REALTIME
);
1773 if (h
->last_change_usec
!= UINT64_MAX
&&
1774 h
->last_change_usec
> n
) /* Don't allow log ins when the record is from the future */
1780 if (h
->not_before_usec
!= UINT64_MAX
&& n
< h
->not_before_usec
)
1782 if (h
->not_after_usec
!= UINT64_MAX
&& n
> h
->not_after_usec
)
1788 int user_record_test_password_change_required(UserRecord
*h
) {
1789 bool change_permitted
;
1794 /* Checks whether the user must change the password when logging in
1796 -EKEYREVOKED: Change password now because admin said so
1797 -EOWNERDEAD: Change password now because it expired
1798 -EKEYREJECTED: Password is expired, no changing is allowed
1799 -EKEYEXPIRED: Password is about to expire, warn user
1800 -ENETDOWN: Record has expiration info but no password change timestamp
1801 -EROFS: No password change required nor permitted
1802 0: No password change required, but permitted
1805 /* If a password change request has been set explicitly, it overrides everything */
1806 if (h
->password_change_now
> 0)
1807 return -EKEYREVOKED
;
1809 n
= now(CLOCK_REALTIME
);
1811 /* Then, let's check if password changing is currently allowed at all */
1812 if (h
->password_change_min_usec
!= UINT64_MAX
) {
1814 /* Expiry configured but no password change timestamp known? */
1815 if (h
->last_password_change_usec
== UINT64_MAX
)
1818 if (h
->password_change_min_usec
>= UINT64_MAX
- h
->last_password_change_usec
)
1819 change_permitted
= false;
1821 change_permitted
= n
>= h
->last_password_change_usec
+ h
->password_change_min_usec
;
1824 change_permitted
= true;
1826 /* Let's check whether the password has expired. */
1827 if (!(h
->password_change_max_usec
== UINT64_MAX
||
1828 h
->password_change_max_usec
>= UINT64_MAX
- h
->last_password_change_usec
)) {
1830 uint64_t change_before
;
1832 /* Expiry configured but no password change timestamp known? */
1833 if (h
->last_password_change_usec
== UINT64_MAX
)
1836 /* Password is in inactive phase? */
1837 if (h
->password_change_inactive_usec
!= UINT64_MAX
&&
1838 h
->password_change_inactive_usec
< UINT64_MAX
- h
->password_change_max_usec
) {
1841 added
= h
->password_change_inactive_usec
+ h
->password_change_max_usec
;
1842 if (added
< UINT64_MAX
- h
->last_password_change_usec
&&
1843 n
>= h
->last_password_change_usec
+ added
)
1844 return -EKEYREJECTED
;
1847 /* Password needs to be changed now? */
1848 change_before
= h
->last_password_change_usec
+ h
->password_change_max_usec
;
1849 if (n
>= change_before
)
1850 return change_permitted
? -EOWNERDEAD
: -EKEYREJECTED
;
1853 if (h
->password_change_warn_usec
!= UINT64_MAX
&&
1854 (change_before
< h
->password_change_warn_usec
||
1855 n
>= change_before
- h
->password_change_warn_usec
))
1856 return change_permitted
? -EKEYEXPIRED
: -EROFS
;
1859 /* No password changing necessary */
1860 return change_permitted
? 0 : -EROFS
;
1863 static const char* const user_storage_table
[_USER_STORAGE_MAX
] = {
1864 [USER_CLASSIC
] = "classic",
1865 [USER_LUKS
] = "luks",
1866 [USER_DIRECTORY
] = "directory",
1867 [USER_SUBVOLUME
] = "subvolume",
1868 [USER_FSCRYPT
] = "fscrypt",
1869 [USER_CIFS
] = "cifs",
1872 DEFINE_STRING_TABLE_LOOKUP(user_storage
, UserStorage
);
1874 static const char* const user_disposition_table
[_USER_DISPOSITION_MAX
] = {
1875 [USER_INTRINSIC
] = "intrinsic",
1876 [USER_SYSTEM
] = "system",
1877 [USER_DYNAMIC
] = "dynamic",
1878 [USER_REGULAR
] = "regular",
1879 [USER_CONTAINER
] = "container",
1880 [USER_RESERVED
] = "reserved",
1883 DEFINE_STRING_TABLE_LOOKUP(user_disposition
, UserDisposition
);