From: Lennart Poettering Date: Wed, 19 Feb 2025 16:15:56 +0000 (+0100) Subject: user-record: add a concept of inverting per-host matching sections in user record X-Git-Tag: v258-rc1~1143^2~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ce94761debfab321d608e5c4ea876d7bc7f65097;p=thirdparty%2Fsystemd.git user-record: add a concept of inverting per-host matching sections in user record Sometimes it is useful to apply options on all hosts except some. Add a simple concept for that. --- diff --git a/src/shared/user-record.c b/src/shared/user-record.c index 89ba2fbef4c..0d2c261a097 100644 --- a/src/shared/user-record.c +++ b/src/shared/user-record.c @@ -1126,6 +1126,8 @@ int per_machine_id_match(sd_json_variant *ids, sd_json_dispatch_flags_t flags) { sd_id128_t mid; int r; + assert(ids); + r = sd_id128_get_machine(&mid); if (r < 0) return json_log(ids, flags, r, "Failed to acquire machine ID: %m"); @@ -1174,6 +1176,8 @@ int per_machine_hostname_match(sd_json_variant *hns, sd_json_dispatch_flags_t fl _cleanup_free_ char *hn = NULL; int r; + assert(hns); + r = gethostname_strict(&hn); if (r == -ENXIO) { json_log(hns, flags, r, "No hostname set, not matching perMachine hostname record: %m"); @@ -1221,6 +1225,15 @@ int per_machine_match(sd_json_variant *entry, sd_json_dispatch_flags_t flags) { return true; } + m = sd_json_variant_by_key(entry, "matchNotMachineId"); + if (m) { + r = per_machine_id_match(m, flags); + if (r < 0) + return r; + if (r == 0) + return true; + } + m = sd_json_variant_by_key(entry, "matchHostname"); if (m) { r = per_machine_hostname_match(m, flags); @@ -1230,6 +1243,15 @@ int per_machine_match(sd_json_variant *entry, sd_json_dispatch_flags_t flags) { return true; } + m = sd_json_variant_by_key(entry, "matchNotHostname"); + if (m) { + r = per_machine_hostname_match(m, flags); + if (r < 0) + return r; + if (r == 0) + return true; + } + return false; } @@ -1237,7 +1259,9 @@ static int dispatch_per_machine(const char *name, sd_json_variant *variant, sd_j static const sd_json_dispatch_field per_machine_dispatch_table[] = { { "matchMachineId", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, + { "matchNotMachineId", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, { "matchHostname", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, + { "matchNotHostname", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, { "blobDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, blob_directory), SD_JSON_STRICT }, { "blobManifest", SD_JSON_VARIANT_OBJECT, dispatch_blob_manifest, offsetof(UserRecord, blob_manifest), 0 }, { "iconName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, icon_name), SD_JSON_STRICT },