1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <sys/utsname.h>
10 //#include "alloc-util.h"
12 //#include "fd-util.h"
14 #include "hostname-util.h"
16 #include "string-util.h"
18 #if 0 /// UNNEEDED by elogind
19 bool hostname_is_set(void) {
22 assert_se(uname(&u) >= 0);
24 if (isempty(u.nodename))
27 /* This is the built-in kernel default host name */
28 if (streq(u.nodename, "(none)"))
35 char* gethostname_malloc(void) {
38 /* This call tries to return something useful, either the actual hostname
39 * or it makes something up. The only reason it might fail is OOM.
40 * It might even return "localhost" if that's set. */
42 assert_se(uname(&u) >= 0);
44 if (isempty(u.nodename) || streq(u.nodename, "(none)"))
45 #if 0 /// elogind has no hostnamed and such nonsense
46 return strdup(FALLBACK_HOSTNAME);
48 return strdup("localhost");
51 return strdup(u.nodename);
54 #if 0 /// UNNEEDED by elogind
55 int gethostname_strict(char **ret) {
59 /* This call will rather fail than make up a name. It will not return "localhost" either. */
61 assert_se(uname(&u) >= 0);
63 if (isempty(u.nodename))
66 if (streq(u.nodename, "(none)"))
69 if (is_localhost(u.nodename))
72 k = strdup(u.nodename);
81 static bool hostname_valid_char(char c) {
83 (c >= 'a' && c <= 'z') ||
84 (c >= 'A' && c <= 'Z') ||
85 (c >= '0' && c <= '9') ||
86 IN_SET(c, '-', '_', '.');
90 * Check if s looks like a valid host name or FQDN. This does not do
91 * full DNS validation, but only checks if the name is composed of
92 * allowed characters and the length is not above the maximum allowed
93 * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
94 * allow_trailing_dot is true and at least two components are present
95 * in the name. Note that due to the restricted charset and length
96 * this call is substantially more conservative than
97 * dns_name_is_valid().
99 bool hostname_is_valid(const char *s, bool allow_trailing_dot) {
107 /* Doesn't accept empty hostnames, hostnames with
108 * leading dots, and hostnames with multiple dots in a
109 * sequence. Also ensures that the length stays below
112 for (p = s, dot = true; *p; p++) {
120 if (!hostname_valid_char(*p))
127 if (dot && (n_dots < 2 || !allow_trailing_dot))
130 if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on
131 * Linux, but DNS allows domain names
132 * up to 255 characters */
138 #if 0 /// UNNEEDED by elogind
139 char* hostname_cleanup(char *s) {
145 strshorten(s, HOST_NAME_MAX);
147 for (p = s, d = s, dot = true; *p; p++) {
154 } else if (hostname_valid_char(*p)) {
169 bool is_localhost(const char *hostname) {
172 /* This tries to identify local host and domain names
173 * described in RFC6761 plus the redhatism of localdomain */
175 return strcaseeq(hostname, "localhost") ||
176 strcaseeq(hostname, "localhost.") ||
177 strcaseeq(hostname, "localhost.localdomain") ||
178 strcaseeq(hostname, "localhost.localdomain.") ||
179 endswith_no_case(hostname, ".localhost") ||
180 endswith_no_case(hostname, ".localhost.") ||
181 endswith_no_case(hostname, ".localhost.localdomain") ||
182 endswith_no_case(hostname, ".localhost.localdomain.");
185 #if 0 /// UNNEEDED by elogind
186 bool is_gateway_hostname(const char *hostname) {
189 /* This tries to identify the valid syntaxes for the our
190 * synthetic "gateway" host. */
193 strcaseeq(hostname, "_gateway") || strcaseeq(hostname, "_gateway.")
194 #if ENABLE_COMPAT_GATEWAY_HOSTNAME
195 || strcaseeq(hostname, "gateway") || strcaseeq(hostname, "gateway.")
200 int sethostname_idempotent(const char *s) {
201 char buf[HOST_NAME_MAX + 1] = {};
205 if (gethostname(buf, sizeof(buf)) < 0)
211 if (sethostname(s, strlen(s)) < 0)
217 int shorten_overlong(const char *s, char **ret) {
220 /* Shorten an overlong name to HOST_NAME_MAX or to the first dot,
221 * whatever comes earlier. */
229 if (hostname_is_valid(h, false)) {
238 strshorten(h, HOST_NAME_MAX);
240 if (!hostname_is_valid(h, false)) {
249 int read_etc_hostname_stream(FILE *f, char **ret) {
256 _cleanup_free_ char *line = NULL;
259 r = read_line(f, LONG_LINE_MAX, &line);
262 if (r == 0) /* EOF without any hostname? the file is empty, let's treat that exactly like no file at all: ENOENT */
267 /* File may have empty lines or comments, ignore them */
268 if (!IN_SET(*p, '\0', '#')) {
271 hostname_cleanup(p); /* normalize the hostname */
273 if (!hostname_is_valid(p, true)) /* check that the hostname we return is valid */
286 int read_etc_hostname(const char *path, char **ret) {
287 _cleanup_fclose_ FILE *f = NULL;
292 path = "/etc/hostname";
294 f = fopen(path, "re");
298 return read_etc_hostname_stream(f, ret);