]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Allow the fallback hostname to be overriden using an environment variable
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 15 Feb 2021 10:50:55 +0000 (11:50 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 22 Feb 2021 19:10:55 +0000 (20:10 +0100)
See https://bugzilla.redhat.com/show_bug.cgi?id=1893417 for the back story:
the fallback hostname matters a lot in certain environments. Right now the only
way to configure the fallback hostname is by recompiling systemd, which is
obviously problematic in case when the fallback hostname shall differ between
different editions of the same distro that share a single compiled rpm.

By making this overridable through an envvar, we're providing an escape hatch
without making this a top-level api. Later on a way to set this through
os-release is added, but I think the approach with the variable is still
useful. It it very convenient for testing, or to override settings only in a
particular service, etc.

docs/ENVIRONMENT.md
src/basic/hostname-util.c
src/basic/hostname-util.h
src/hostname/hostnamed.c
src/resolve/resolved-manager.c
src/shared/hostname-setup.c
src/test/test-hostname-util.c

index 82bb52b344c2f6c5fecdcf13d85bf926298fafd9..07a65864bdebfcf04c9818b239af923092e13355 100644 (file)
@@ -55,6 +55,10 @@ All tools:
 * `$SYSTEMD_EFI_OPTIONS` — if set, used instead of the string in the
   SystemdOptions EFI variable. Analogous to `$SYSTEMD_PROC_CMDLINE`.
 
+* `$SYSTEMD_DEFAULT_HOSTNAME` — override the compiled-in fallback hostname
+  (relevant in particular for the system manager and `systemd-hostnamed`).
+  Must be a valid hostname (either a single label or a FQDN).
+
 * `$SYSTEMD_IN_INITRD=[auto|lenient|0|1]` — if set, specifies initrd detection
   method. Defaults to `auto`. Behavior is defined as follows:
   `auto`: Checks if `/etc/initrd-release` exists, and a temporary fs is mounted
index d7aba2c263029b284b5f83606c3a889c810a350a..5109605e0c5f0ca9ec1488d8bb89d3a8f794a028 100644 (file)
@@ -3,14 +3,27 @@
 #include <errno.h>
 #include <limits.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/utsname.h>
 #include <unistd.h>
 
 #include "alloc-util.h"
 #include "hostname-util.h"
+#include "os-util.h"
 #include "string-util.h"
 #include "strv.h"
 
+char* get_default_hostname(void) {
+        const char *e = secure_getenv("SYSTEMD_DEFAULT_HOSTNAME");
+        if (e) {
+                if (hostname_is_valid(e, 0))
+                        return strdup(e);
+                log_debug("Invalid hostname in $SYSTEMD_DEFAULT_HOSTNAME, ignoring: %s", e);
+        }
+
+        return strdup(FALLBACK_HOSTNAME);
+}
+
 char* gethostname_malloc(void) {
         struct utsname u;
         const char *s;
@@ -23,7 +36,7 @@ char* gethostname_malloc(void) {
 
         s = u.nodename;
         if (isempty(s) || streq(s, "(none)"))
-                s = FALLBACK_HOSTNAME;
+                return get_default_hostname();
 
         return strdup(s);
 }
@@ -31,6 +44,7 @@ char* gethostname_malloc(void) {
 char* gethostname_short_malloc(void) {
         struct utsname u;
         const char *s;
+        _cleanup_free_ char *f = NULL;
 
         /* Like above, but kills the FQDN part if present. */
 
@@ -38,7 +52,10 @@ char* gethostname_short_malloc(void) {
 
         s = u.nodename;
         if (isempty(s) || streq(s, "(none)") || s[0] == '.') {
-                s = FALLBACK_HOSTNAME;
+                s = f = get_default_hostname();
+                if (!s)
+                        return NULL;
+
                 assert(s[0] != '.');
         }
 
index 6cff9c1d4cc8cd7b4dc384860b9fbe1204e5e743..576ca083e011ef879f4d7b551c6c3a1a1e434354 100644 (file)
@@ -7,6 +7,7 @@
 #include "macro.h"
 #include "strv.h"
 
+char* get_default_hostname(void);
 char* gethostname_malloc(void);
 char* gethostname_short_malloc(void);
 int gethostname_strict(char **ret);
index eb06957397c6fba6a17d82c153cae65e85738fb4..9937d159f4e271157ff85f7a8c1f2a00cf9d2aeb 100644 (file)
@@ -322,6 +322,7 @@ static int context_update_kernel_hostname(
                 Context *c,
                 const char *transient_hn) {
 
+        _cleanup_free_ char *_hn_free = NULL;
         const char *hn;
         HostnameSource hns;
         int r;
@@ -340,7 +341,10 @@ static int context_update_kernel_hostname(
 
         /* ... and the ultimate fallback */
         } else {
-                hn = FALLBACK_HOSTNAME;
+                hn = _hn_free = get_default_hostname();
+                if (!hn)
+                        return log_oom();
+
                 hns = HOSTNAME_FALLBACK;
         }
 
@@ -502,16 +506,20 @@ static int property_get_hostname(
                 void *userdata,
                 sd_bus_error *error) {
 
-        _cleanup_free_ char *current = NULL;
+        _cleanup_free_ char *hn = NULL;
         int r;
 
-        r = gethostname_strict(&current);
-        if (r == -ENXIO)
-                return sd_bus_message_append(reply, "s", FALLBACK_HOSTNAME);
-        if (r < 0)
-                return r;
+        r = gethostname_strict(&hn);
+        if (r < 0) {
+                if (r != -ENXIO)
+                        return r;
 
-        return sd_bus_message_append(reply, "s", current);
+                hn = get_default_hostname();
+                if (!hn)
+                        return -ENOMEM;
+        }
+
+        return sd_bus_message_append(reply, "s", hn);
 }
 
 static int property_get_static_hostname(
@@ -531,7 +539,21 @@ static int property_get_static_hostname(
         return sd_bus_message_append(reply, "s", c->data[PROP_STATIC_HOSTNAME]);
 }
 
-static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_fallback_hostname, "s", FALLBACK_HOSTNAME);
+static int property_get_fallback_hostname(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        _cleanup_free_ char *hn = get_default_hostname();
+        if (!hn)
+                return log_oom();
+
+        return sd_bus_message_append(reply, "s", hn);
+}
 
 static int property_get_hostname_source(
                 sd_bus *bus,
index 2ed9f2d47b5d2c537790a52e8ba67cf0bb42427b..ae46d2dcd8541cf4015790c82765d022a85fbcc0 100644 (file)
@@ -442,20 +442,25 @@ static int determine_hostname(char **full_hostname, char **llmnr_hostname, char
         return 0;
 }
 
-static const char *fallback_hostname(void) {
+static char* fallback_hostname(void) {
 
-        /* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it to be
-         * "localhost" even if that's the compiled in hostname. In this case, let's revert to "linux" instead. */
+        /* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it
+         * to be "localhost" even if that's the default hostname. In this case, let's revert to "linux"
+         * instead. */
 
-        if (is_localhost(FALLBACK_HOSTNAME))
-                return "linux";
+        _cleanup_free_ char *n = get_default_hostname();
+        if (!n)
+                return NULL;
+
+        if (is_localhost(n))
+                return strdup("linux");
 
-        return FALLBACK_HOSTNAME;
+        return TAKE_PTR(n);
 }
 
 static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) {
-        _cleanup_free_ char *n = NULL, *m = NULL;
-        char label[DNS_LABEL_MAX], *h;
+        _cleanup_free_ char *h = NULL, *n = NULL, *m = NULL;
+        char label[DNS_LABEL_MAX];
         const char *p;
         int r;
 
@@ -463,7 +468,10 @@ static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname,
         assert(llmnr_hostname);
         assert(mdns_hostname);
 
-        p = fallback_hostname();
+        p = h = fallback_hostname();
+        if (!h)
+                return log_oom();
+
         r = dns_label_unescape(&p, label, sizeof label, 0);
         if (r < 0)
                 return log_error_errno(r, "Failed to unescape fallback hostname: %m");
@@ -478,14 +486,9 @@ static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname,
         if (r < 0)
                 return log_error_errno(r, "Failed to concatenate mDNS hostname: %m");
 
-        h = strdup(fallback_hostname());
-        if (!h)
-                return log_oom();
-
         *llmnr_hostname = TAKE_PTR(n);
         *mdns_hostname = TAKE_PTR(m);
-
-        *full_hostname = h;
+        *full_hostname = TAKE_PTR(h);
 
         return 0;
 }
index 5cdcdbd93c870a734f4507ebfbe7daf924202327..0cc1a268bdfac5b05988d73f6ebcd00612850c5e 100644 (file)
@@ -194,7 +194,7 @@ int hostname_setup(bool really) {
                 }
         }
 
-        if (isempty(hn)) {
+        if (!hn) {
                 /* Don't override the hostname if it is already set and not explicitly configured */
 
                 char buf[HOST_NAME_MAX + 1] = {};
@@ -206,7 +206,10 @@ int hostname_setup(bool really) {
                 if (enoent)
                         log_info("No hostname configured, using fallback hostname.");
 
-                hn = FALLBACK_HOSTNAME;
+                hn = b = get_default_hostname();
+                if (!hn)
+                        return log_oom();
+
                 source = HOSTNAME_FALLBACK;
 
         }
index 24c8ed9e3b30f3c5d007c269beff7cf3c03a4e2d..6d62958d67f7875eaa0cbe77d89bf68ef214b4f9 100644 (file)
@@ -10,6 +10,8 @@
 #include "tmpfile-util.h"
 
 static void test_hostname_is_valid(void) {
+        log_info("/* %s */", __func__);
+
         assert_se(hostname_is_valid("foobar", 0));
         assert_se(hostname_is_valid("foobar.com", 0));
         assert_se(!hostname_is_valid("foobar.com.", 0));
@@ -49,6 +51,8 @@ static void test_hostname_is_valid(void) {
 static void test_hostname_cleanup(void) {
         char *s;
 
+        log_info("/* %s */", __func__);
+
         s = strdupa("foobar");
         assert_se(streq(hostname_cleanup(s), "foobar"));
         s = strdupa("foobar.com");
@@ -94,6 +98,8 @@ static void test_hostname_cleanup(void) {
 static void test_hostname_malloc(void) {
         _cleanup_free_ char *h = NULL, *l = NULL;
 
+        log_info("/* %s */", __func__);
+
         assert_se(h = gethostname_malloc());
         log_info("hostname_malloc: \"%s\"", h);
 
@@ -101,21 +107,27 @@ static void test_hostname_malloc(void) {
         log_info("hostname_short_malloc: \"%s\"", l);
 }
 
-static void test_fallback_hostname(void) {
+static void test_default_hostname(void) {
+        log_info("/* %s */", __func__);
+
         if (!hostname_is_valid(FALLBACK_HOSTNAME, 0)) {
                 log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
                 exit(EXIT_FAILURE);
         }
+
+        _cleanup_free_ char *n = get_default_hostname();
+        assert_se(n);
+        log_info("get_default_hostname: \"%s\"", n);
+        assert_se(hostname_is_valid(n, 0));
 }
 
 int main(int argc, char *argv[]) {
-        test_setup_logging(LOG_INFO);
+        test_setup_logging(LOG_DEBUG);
 
         test_hostname_is_valid();
         test_hostname_cleanup();
         test_hostname_malloc();
-
-        test_fallback_hostname();
+        test_default_hostname();
 
         return 0;
 }