]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
user-record: add helper for dispatching a disposition mask
authorLennart Poettering <lennart@poettering.net>
Tue, 21 Jan 2025 11:53:02 +0000 (12:53 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 27 Jan 2025 22:42:06 +0000 (23:42 +0100)
src/shared/user-record.c
src/shared/user-record.h

index 1e5c3f589b473bd713195a2fcf54c2e09019a38a..51439f970660011ae6b4f8c32c1cb5da89229fbd 100644 (file)
@@ -2796,6 +2796,39 @@ int user_record_match(UserRecord *u, const UserDBMatch *match) {
         return true;
 }
 
+int json_dispatch_dispositions_mask(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
+        uint64_t *mask = ASSERT_PTR(userdata);
+
+        if (sd_json_variant_is_null(variant)) {
+                *mask = UINT64_MAX;
+                return 0;
+        }
+
+        if (!sd_json_variant_is_array(variant))
+                return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name));
+
+        uint64_t m = 0;
+        for (size_t i = 0; i < sd_json_variant_elements(variant); i++) {
+                sd_json_variant *e;
+                const char *a;
+
+                e = sd_json_variant_by_index(variant, i);
+                if (!sd_json_variant_is_string(e))
+                        return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name));
+
+                assert_se(a = sd_json_variant_string(e));
+
+                UserDisposition d = user_disposition_from_string(a);
+                if (d < 0)
+                        return json_log(e, flags, d, "JSON field '%s' contains an invalid user disposition type: %s", strna(name), a);
+
+                m |= INDEX_TO_MASK(uint64_t, d);
+        }
+
+        *mask = m;
+        return 0;
+}
+
 static const char* const user_storage_table[_USER_STORAGE_MAX] = {
         [USER_CLASSIC]   = "classic",
         [USER_LUKS]      = "luks",
index fc8510c074050ca82e91b8c5f8b422ae6e3a2927..079e05029e0239cc1793efeb7574d44eb2b3232f 100644 (file)
@@ -510,6 +510,8 @@ int user_record_match(UserRecord *u, const UserDBMatch *match);
 
 bool user_record_matches_user_name(const UserRecord *u, const char *username);
 
+int json_dispatch_dispositions_mask(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata);
+
 const char* user_storage_to_string(UserStorage t) _const_;
 UserStorage user_storage_from_string(const char *s) _pure_;