shared/bus-unit-util: add helper for ProtectHostnameEx and fix naming confusion

As with grandparent commit for ImportCredentialEx=, the whole series of commits
that extended ProtectHostname was confused (6746f28854,
cf48bde7ae, e76fcd0e40),
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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-07-03 19:35:33 +02:00
parent 779aed7443
commit 9e3bc6406b
+31 -18
View File
@@ -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);
if (STR_IN_SET(field, "ProtectHostname", "ProtectHostnameEx"))
return bus_append_protect_hostname(m, field, eq);
_cleanup_free_ char *p = strndup(eq, colon - eq);
if (!p)
return -ENOMEM;
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;
}