1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2015 Lennart Poettering
10 #include <sys/utsname.h>
13 //#include "alloc-util.h"
15 //#include "fd-util.h"
17 #include "hostname-util.h"
19 #include "string-util.h"
21 #if 0 /// UNNEEDED by elogind
22 bool hostname_is_set(void) {
25 assert_se(uname(&u) >= 0);
27 if (isempty(u.nodename))
30 /* This is the built-in kernel default host name */
31 if (streq(u.nodename, "(none)"))
38 char* gethostname_malloc(void) {
41 /* This call tries to return something useful, either the actual hostname
42 * or it makes something up. The only reason it might fail is OOM.
43 * It might even return "localhost" if that's set. */
45 assert_se(uname(&u) >= 0);
47 if (isempty(u.nodename) || streq(u.nodename, "(none)"))
48 #if 0 /// elogind has no hostnamed and such nonsense
49 return strdup(FALLBACK_HOSTNAME);
51 return strdup("localhost");
54 return strdup(u.nodename);
57 #if 0 /// UNNEEDED by elogind
58 int gethostname_strict(char **ret) {
62 /* This call will rather fail than make up a name. It will not return "localhost" either. */
64 assert_se(uname(&u) >= 0);
66 if (isempty(u.nodename))
69 if (streq(u.nodename, "(none)"))
72 if (is_localhost(u.nodename))
75 k = strdup(u.nodename);
84 static bool hostname_valid_char(char c) {
86 (c >= 'a' && c <= 'z') ||
87 (c >= 'A' && c <= 'Z') ||
88 (c >= '0' && c <= '9') ||
89 IN_SET(c, '-', '_', '.');
93 * Check if s looks like a valid host name or FQDN. This does not do
94 * full DNS validation, but only checks if the name is composed of
95 * allowed characters and the length is not above the maximum allowed
96 * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
97 * allow_trailing_dot is true and at least two components are present
98 * in the name. Note that due to the restricted charset and length
99 * this call is substantially more conservative than
100 * dns_name_is_valid().
102 bool hostname_is_valid(const char *s, bool allow_trailing_dot) {
110 /* Doesn't accept empty hostnames, hostnames with
111 * leading dots, and hostnames with multiple dots in a
112 * sequence. Also ensures that the length stays below
115 for (p = s, dot = true; *p; p++) {
123 if (!hostname_valid_char(*p))
130 if (dot && (n_dots < 2 || !allow_trailing_dot))
133 if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on
134 * Linux, but DNS allows domain names
135 * up to 255 characters */
141 #if 0 /// UNNEEDED by elogind
142 char* hostname_cleanup(char *s) {
148 strshorten(s, HOST_NAME_MAX);
150 for (p = s, d = s, dot = true; *p; p++) {
157 } else if (hostname_valid_char(*p)) {
172 bool is_localhost(const char *hostname) {
175 /* This tries to identify local host and domain names
176 * described in RFC6761 plus the redhatism of localdomain */
178 return strcaseeq(hostname, "localhost") ||
179 strcaseeq(hostname, "localhost.") ||
180 strcaseeq(hostname, "localhost.localdomain") ||
181 strcaseeq(hostname, "localhost.localdomain.") ||
182 endswith_no_case(hostname, ".localhost") ||
183 endswith_no_case(hostname, ".localhost.") ||
184 endswith_no_case(hostname, ".localhost.localdomain") ||
185 endswith_no_case(hostname, ".localhost.localdomain.");
188 #if 0 /// UNNEEDED by elogind
189 bool is_gateway_hostname(const char *hostname) {
192 /* This tries to identify the valid syntaxes for the our
193 * synthetic "gateway" host. */
196 strcaseeq(hostname, "_gateway") || strcaseeq(hostname, "_gateway.")
197 #if ENABLE_COMPAT_GATEWAY_HOSTNAME
198 || strcaseeq(hostname, "gateway") || strcaseeq(hostname, "gateway.")
203 int sethostname_idempotent(const char *s) {
204 char buf[HOST_NAME_MAX + 1] = {};
208 if (gethostname(buf, sizeof(buf)) < 0)
214 if (sethostname(s, strlen(s)) < 0)
220 int shorten_overlong(const char *s, char **ret) {
223 /* Shorten an overlong name to HOST_NAME_MAX or to the first dot,
224 * whatever comes earlier. */
232 if (hostname_is_valid(h, false)) {
241 strshorten(h, HOST_NAME_MAX);
243 if (!hostname_is_valid(h, false)) {
252 int read_etc_hostname_stream(FILE *f, char **ret) {
259 _cleanup_free_ char *line = NULL;
262 r = read_line(f, LONG_LINE_MAX, &line);
265 if (r == 0) /* EOF without any hostname? the file is empty, let's treat that exactly like no file at all: ENOENT */
270 /* File may have empty lines or comments, ignore them */
271 if (!IN_SET(*p, '\0', '#')) {
274 hostname_cleanup(p); /* normalize the hostname */
276 if (!hostname_is_valid(p, true)) /* check that the hostname we return is valid */
289 int read_etc_hostname(const char *path, char **ret) {
290 _cleanup_fclose_ FILE *f = NULL;
295 path = "/etc/hostname";
297 f = fopen(path, "re");
301 return read_etc_hostname_stream(f, ret);