summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-02-17 13:51:27 +0100
committerGitHub <noreply@github.com>2017-02-17 13:51:27 +0100
commitea2aa0343f91f3cd2842129e94dbf05525732e7f (patch)
tree277f0db1b4c19bfdb8304d5f35b6168e0e8f4475
parentf73e6ee687213d8f78a93a9519901d0fe314c228 (diff)
parenta25b0dc82d10f8c53ac362f12f9a492062404b34 (diff)
Merge pull request #5366 from poettering/default-hostname-fix
fallback hostname fixes
-rw-r--r--src/basic/hostname-util.c2
-rw-r--r--src/core/hostname-setup.c6
-rw-r--r--src/resolve/resolved-manager.c78
3 files changed, 64 insertions, 22 deletions
diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c
index e44a357287..a94037b303 100644
--- a/src/basic/hostname-util.c
+++ b/src/basic/hostname-util.c
@@ -55,7 +55,7 @@ char* gethostname_malloc(void) {
assert_se(uname(&u) >= 0);
if (isempty(u.nodename) || streq(u.nodename, "(none)"))
- return strdup(u.sysname);
+ return strdup(FALLBACK_HOSTNAME);
return strdup(u.nodename);
}
diff --git a/src/core/hostname-setup.c b/src/core/hostname-setup.c
index 68be52856b..845e31e1c5 100644
--- a/src/core/hostname-setup.c
+++ b/src/core/hostname-setup.c
@@ -31,10 +31,10 @@
#include "util.h"
int hostname_setup(void) {
- int r;
_cleanup_free_ char *b = NULL;
- const char *hn;
bool enoent = false;
+ const char *hn;
+ int r;
r = read_hostname_config("/etc/hostname", &b);
if (r < 0) {
@@ -56,7 +56,7 @@ int hostname_setup(void) {
if (enoent)
log_info("No hostname configured.");
- hn = "localhost";
+ hn = FALLBACK_HOSTNAME;
}
r = sethostname_idempotent(hn);
diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c
index 791c125613..c4e4409fe3 100644
--- a/src/resolve/resolved-manager.c
+++ b/src/resolve/resolved-manager.c
@@ -332,19 +332,18 @@ static int determine_hostname(char **full_hostname, char **llmnr_hostname, char
assert(llmnr_hostname);
assert(mdns_hostname);
- /* Extract and normalize the first label of the locally
- * configured hostname, and check it's not "localhost". */
+ /* Extract and normalize the first label of the locally configured hostname, and check it's not "localhost". */
- h = gethostname_malloc();
- if (!h)
- return log_oom();
+ r = gethostname_strict(&h);
+ if (r < 0)
+ return log_debug_errno(r, "Can't determine system hostname: %m");
p = h;
r = dns_label_unescape(&p, label, sizeof(label));
if (r < 0)
return log_error_errno(r, "Failed to unescape host name: %m");
if (r == 0) {
- log_error("Couldn't find a single label in hosntame.");
+ log_error("Couldn't find a single label in hostname.");
return -EINVAL;
}
@@ -381,6 +380,57 @@ static int determine_hostname(char **full_hostname, char **llmnr_hostname, char
return 0;
}
+static const 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. */
+
+ if (is_localhost(FALLBACK_HOSTNAME))
+ return "linux";
+
+ return FALLBACK_HOSTNAME;
+}
+
+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;
+ const char *p;
+ int r;
+
+ assert(full_hostname);
+ assert(llmnr_hostname);
+ assert(mdns_hostname);
+
+ p = fallback_hostname();
+ r = dns_label_unescape(&p, label, sizeof(label));
+ if (r < 0)
+ return log_error_errno(r, "Failed to unescape fallback host name: %m");
+
+ assert(r > 0); /* The fallback hostname must have at least one label */
+
+ r = dns_label_escape_new(label, r, &n);
+ if (r < 0)
+ return log_error_errno(r, "Failed to escape fallback hostname: %m");
+
+ r = dns_name_concat(n, "local", &m);
+ 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 = n;
+ n = NULL;
+
+ *mdns_hostname = m;
+ m = NULL;
+
+ *full_hostname = h;
+
+ return 0;
+}
+
static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
_cleanup_free_ char *full_hostname = NULL, *llmnr_hostname = NULL, *mdns_hostname = NULL;
Manager *m = userdata;
@@ -432,19 +482,11 @@ static int manager_watch_hostname(Manager *m) {
r = determine_hostname(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname);
if (r < 0) {
- log_info("Defaulting to hostname 'linux'.");
-
- m->full_hostname = strdup("linux");
- if (!m->full_hostname)
- return log_oom();
-
- m->llmnr_hostname = strdup("linux");
- if (!m->llmnr_hostname)
- return log_oom();
+ log_info("Defaulting to hostname '%s'.", fallback_hostname());
- m->mdns_hostname = strdup("linux.local");
- if (!m->mdns_hostname)
- return log_oom();
+ r = make_fallback_hostnames(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname);
+ if (r < 0)
+ return r;
} else
log_info("Using system hostname '%s'.", m->full_hostname);