From 9e3bc6406b7e7d212e9aa32e6ba6c0becd1c6d13 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 18 Jun 2025 17:58:02 +0200 Subject: [PATCH] shared/bus-unit-util: add helper for ProtectHostnameEx and fix naming confusion MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit As with grandparent commit for ImportCredentialEx=, the whole series of commits that extended ProtectHostname was confused (6746f288548a240148c7c9643e14996bfe960017, cf48bde7aea52b18ac3fa218d3f60fd3d533ef66, e76fcd0e40a6910f4818a374c6a8d854d644ff93), because it added ProtectHostnameEx in places where parsing of ProtectHostname should be have been extended. Accept ProtectHostname=… with the new extended syntax, keep accepting ProtectHostnameEx=… for compat with release v257. Prefer sending ProtectHostname. Partially resolves https://github.com/systemd/systemd/issues/37174. --- src/shared/bus-unit-util.c | 49 ++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c index 4763679b157..c7fce7e77fe 100644 --- a/src/shared/bus-unit-util.c +++ b/src/shared/bus-unit-util.c @@ -2040,6 +2040,35 @@ static int bus_append_directory(sd_bus_message *m, const char *field, const char return 1; } +static int bus_append_protect_hostname(sd_bus_message *m, const char *field, const char *eq) { + int r; + + /* The command-line field is called "ProtectHostname". We also accept "ProtectHostnameEx" as the + * field name for backward compatibility. We set ProtectHostame or ProtectHostnameEx. */ + + r = parse_boolean(eq); + if (r >= 0) + r = sd_bus_message_append(m, "(sv)", "ProtectHostname", "b", r); + else { + const char *colon = strchr(eq, ':'); + if (colon) { + if (isempty(colon + 1)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse argument: %s=%s", field, eq); + + _cleanup_free_ char *p = strndup(eq, colon - eq); + if (!p) + return -ENOMEM; + + r = sd_bus_message_append(m, "(sv)", "ProtectHostnameEx", "(ss)", p, colon + 1); + } else + r = sd_bus_message_append(m, "(sv)", "ProtectHostnameEx", "(ss)", eq, NULL); + } + if (r < 0) + return bus_log_create_error(r); + + return 1; +} + static int bus_append_cgroup_property(sd_bus_message *m, const char *field, const char *eq) { if (STR_IN_SET(field, "DevicePolicy", "Slice", @@ -2225,7 +2254,6 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con "BindLogSockets", "CPUSchedulingResetOnFork", "LockPersonality", - "ProtectHostname", "MemoryKSM", "RestrictSUIDSGID", "RootEphemeral", @@ -2404,24 +2432,9 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con if (STR_IN_SET(field, "StateDirectory", "RuntimeDirectory", "CacheDirectory", "LogsDirectory")) return bus_append_directory(m, field, eq); - if (streq(field, "ProtectHostnameEx")) { - const char *colon = strchr(eq, ':'); - if (colon) { - if (isempty(colon + 1)) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse argument: %s=%s", field, eq); - - _cleanup_free_ char *p = strndup(eq, colon - eq); - if (!p) - return -ENOMEM; + if (STR_IN_SET(field, "ProtectHostname", "ProtectHostnameEx")) + return bus_append_protect_hostname(m, field, eq); - r = sd_bus_message_append(m, "(sv)", field, "(ss)", p, colon + 1); - } else - r = sd_bus_message_append(m, "(sv)", field, "(ss)", eq, NULL); - if (r < 0) - return bus_log_create_error(r); - - return 1; - } return 0; } -- 2.47.3