2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <bits/local_lim.h>
25 #include <sys/utsname.h>
28 //#include "fd-util.h"
30 #include "hostname-util.h"
32 #include "string-util.h"
34 #if 0 /// UNNEEDED by elogind
35 bool hostname_is_set(void) {
38 assert_se(uname(&u) >= 0);
40 if (isempty(u.nodename))
43 /* This is the built-in kernel default host name */
44 if (streq(u.nodename, "(none)"))
50 char* gethostname_malloc(void) {
53 assert_se(uname(&u) >= 0);
55 if (isempty(u.nodename) || streq(u.nodename, "(none)"))
56 return strdup(u.sysname);
58 return strdup(u.nodename);
62 static bool hostname_valid_char(char c) {
64 (c >= 'a' && c <= 'z') ||
65 (c >= 'A' && c <= 'Z') ||
66 (c >= '0' && c <= '9') ||
73 * Check if s looks like a valid host name or FQDN. This does not do
74 * full DNS validation, but only checks if the name is composed of
75 * allowed characters and the length is not above the maximum allowed
76 * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
77 * allow_trailing_dot is true and at least two components are present
78 * in the name. Note that due to the restricted charset and length
79 * this call is substantially more conservative than
80 * dns_name_is_valid().
82 bool hostname_is_valid(const char *s, bool allow_trailing_dot) {
90 /* Doesn't accept empty hostnames, hostnames with
91 * leading dots, and hostnames with multiple dots in a
92 * sequence. Also ensures that the length stays below
95 for (p = s, dot = true; *p; p++) {
103 if (!hostname_valid_char(*p))
110 if (dot && (n_dots < 2 || !allow_trailing_dot))
113 if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on
114 * Linux, but DNS allows domain names
115 * up to 255 characters */
121 #if 0 /// UNNEEDED by elogind
122 char* hostname_cleanup(char *s) {
128 for (p = s, d = s, dot = true; *p; p++) {
135 } else if (hostname_valid_char(*p)) {
147 strshorten(s, HOST_NAME_MAX);
153 bool is_localhost(const char *hostname) {
156 /* This tries to identify local host and domain names
157 * described in RFC6761 plus the redhatism of .localdomain */
159 return strcaseeq(hostname, "localhost") ||
160 strcaseeq(hostname, "localhost.") ||
161 strcaseeq(hostname, "localdomain.") ||
162 strcaseeq(hostname, "localdomain") ||
163 endswith_no_case(hostname, ".localhost") ||
164 endswith_no_case(hostname, ".localhost.") ||
165 endswith_no_case(hostname, ".localdomain") ||
166 endswith_no_case(hostname, ".localdomain.");
169 #if 0 /// UNNEEDED by elogind
170 bool is_gateway_hostname(const char *hostname) {
173 /* This tries to identify the valid syntaxes for the our
174 * synthetic "gateway" host. */
177 strcaseeq(hostname, "gateway") ||
178 strcaseeq(hostname, "gateway.");
181 int sethostname_idempotent(const char *s) {
182 char buf[HOST_NAME_MAX + 1] = {};
186 if (gethostname(buf, sizeof(buf)) < 0)
192 if (sethostname(s, strlen(s)) < 0)
198 int read_hostname_config(const char *path, char **hostname) {
199 _cleanup_fclose_ FILE *f = NULL;
206 f = fopen(path, "re");
210 /* may have comments, ignore them */
211 FOREACH_LINE(l, f, return -errno) {
213 if (l[0] != '\0' && l[0] != '#') {
214 /* found line with value */
215 name = hostname_cleanup(l);
224 /* no non-empty line found */