]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/bus-unit-util: add helper for ProtectHostnameEx and fix naming confusion
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 18 Jun 2025 15:58:02 +0000 (17:58 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 3 Jul 2025 17:35:33 +0000 (19:35 +0200)
As with grandparent commit for ImportCredentialEx=, the whole series of commits
that extended ProtectHostname was confused (6746f288548a240148c7c9643e14996bfe960017,
cf48bde7aea52b18ac3fa218d3f60fd3d533ef66e76fcd0e40a6910f4818a374c6a8d854d644ff93),
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

index 4763679b157c0b0541f562bd79e3749947676eec..c7fce7e77fee0136312fbbb7890a2fea71ce1b42 100644 (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);
-
-                        _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;
 }