]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/hostname-util.c
b59e5425a5d381e401d2fbe3d1e716c9fc50f7ad
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/utsname.h>
28 #include "alloc-util.h"
32 #include "hostname-util.h"
34 #include "string-util.h"
36 bool hostname_is_set(void) {
39 assert_se(uname(&u
) >= 0);
41 if (isempty(u
.nodename
))
44 /* This is the built-in kernel default host name */
45 if (streq(u
.nodename
, "(none)"))
51 char* gethostname_malloc(void) {
54 /* This call tries to return something useful, either the actual hostname
55 * or it makes something up. The only reason it might fail is OOM.
56 * It might even return "localhost" if that's set. */
58 assert_se(uname(&u
) >= 0);
60 if (isempty(u
.nodename
) || streq(u
.nodename
, "(none)"))
61 return strdup(FALLBACK_HOSTNAME
);
63 return strdup(u
.nodename
);
66 int gethostname_strict(char **ret
) {
70 /* This call will rather fail than make up a name. It will not return "localhost" either. */
72 assert_se(uname(&u
) >= 0);
74 if (isempty(u
.nodename
))
77 if (streq(u
.nodename
, "(none)"))
80 if (is_localhost(u
.nodename
))
83 k
= strdup(u
.nodename
);
91 static bool hostname_valid_char(char c
) {
93 (c
>= 'a' && c
<= 'z') ||
94 (c
>= 'A' && c
<= 'Z') ||
95 (c
>= '0' && c
<= '9') ||
96 IN_SET(c
, '-', '_', '.');
100 * Check if s looks like a valid host name or FQDN. This does not do
101 * full DNS validation, but only checks if the name is composed of
102 * allowed characters and the length is not above the maximum allowed
103 * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
104 * allow_trailing_dot is true and at least two components are present
105 * in the name. Note that due to the restricted charset and length
106 * this call is substantially more conservative than
107 * dns_name_is_valid().
109 bool hostname_is_valid(const char *s
, bool allow_trailing_dot
) {
117 /* Doesn't accept empty hostnames, hostnames with
118 * leading dots, and hostnames with multiple dots in a
119 * sequence. Also ensures that the length stays below
122 for (p
= s
, dot
= true; *p
; p
++) {
130 if (!hostname_valid_char(*p
))
137 if (dot
&& (n_dots
< 2 || !allow_trailing_dot
))
140 if (p
-s
> HOST_NAME_MAX
) /* Note that HOST_NAME_MAX is 64 on
141 * Linux, but DNS allows domain names
142 * up to 255 characters */
148 char* hostname_cleanup(char *s
) {
154 strshorten(s
, HOST_NAME_MAX
);
156 for (p
= s
, d
= s
, dot
= true; *p
; p
++) {
163 } else if (hostname_valid_char(*p
)) {
177 bool is_localhost(const char *hostname
) {
180 /* This tries to identify local host and domain names
181 * described in RFC6761 plus the redhatism of localdomain */
183 return strcaseeq(hostname
, "localhost") ||
184 strcaseeq(hostname
, "localhost.") ||
185 strcaseeq(hostname
, "localhost.localdomain") ||
186 strcaseeq(hostname
, "localhost.localdomain.") ||
187 endswith_no_case(hostname
, ".localhost") ||
188 endswith_no_case(hostname
, ".localhost.") ||
189 endswith_no_case(hostname
, ".localhost.localdomain") ||
190 endswith_no_case(hostname
, ".localhost.localdomain.");
193 bool is_gateway_hostname(const char *hostname
) {
196 /* This tries to identify the valid syntaxes for the our
197 * synthetic "gateway" host. */
200 strcaseeq(hostname
, "_gateway") || strcaseeq(hostname
, "_gateway.")
201 #if ENABLE_COMPAT_GATEWAY_HOSTNAME
202 || strcaseeq(hostname
, "gateway") || strcaseeq(hostname
, "gateway.")
207 int sethostname_idempotent(const char *s
) {
208 char buf
[HOST_NAME_MAX
+ 1] = {};
212 if (gethostname(buf
, sizeof(buf
)) < 0)
218 if (sethostname(s
, strlen(s
)) < 0)
224 int shorten_overlong(const char *s
, char **ret
) {
227 /* Shorten an overlong name to HOST_NAME_MAX or to the first dot,
228 * whatever comes earlier. */
236 if (hostname_is_valid(h
, false)) {
245 strshorten(h
, HOST_NAME_MAX
);
247 if (!hostname_is_valid(h
, false)) {
256 int read_etc_hostname_stream(FILE *f
, char **ret
) {
263 _cleanup_free_
char *line
= NULL
;
266 r
= read_line(f
, LONG_LINE_MAX
, &line
);
269 if (r
== 0) /* EOF without any hostname? the file is empty, let's treat that exactly like no file at all: ENOENT */
274 /* File may have empty lines or comments, ignore them */
275 if (!IN_SET(*p
, '\0', '#')) {
278 hostname_cleanup(p
); /* normalize the hostname */
280 if (!hostname_is_valid(p
, true)) /* check that the hostname we return is valid */
293 int read_etc_hostname(const char *path
, char **ret
) {
294 _cleanup_fclose_
FILE *f
= NULL
;
299 path
= "/etc/hostname";
301 f
= fopen(path
, "re");
305 return read_etc_hostname_stream(f
, ret
);