]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/hostname-util.c
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>
30 #include "hostname-util.h"
32 #include "string-util.h"
34 bool hostname_is_set(void) {
37 assert_se(uname(&u
) >= 0);
39 if (isempty(u
.nodename
))
42 /* This is the built-in kernel default host name */
43 if (streq(u
.nodename
, "(none)"))
49 char* gethostname_malloc(void) {
52 /* This call tries to return something useful, either the actual hostname
53 * or it makes something up. The only reason it might fail is OOM.
54 * It might even return "localhost" if that's set. */
56 assert_se(uname(&u
) >= 0);
58 if (isempty(u
.nodename
) || streq(u
.nodename
, "(none)"))
59 return strdup(FALLBACK_HOSTNAME
);
61 return strdup(u
.nodename
);
64 int gethostname_strict(char **ret
) {
68 /* This call will rather fail than make up a name. It will not return "localhost" either. */
70 assert_se(uname(&u
) >= 0);
72 if (isempty(u
.nodename
))
75 if (streq(u
.nodename
, "(none)"))
78 if (is_localhost(u
.nodename
))
81 k
= strdup(u
.nodename
);
89 static bool hostname_valid_char(char c
) {
91 (c
>= 'a' && c
<= 'z') ||
92 (c
>= 'A' && c
<= 'Z') ||
93 (c
>= '0' && c
<= '9') ||
94 IN_SET(c
, '-', '_', '.');
98 * Check if s looks like a valid host name or FQDN. This does not do
99 * full DNS validation, but only checks if the name is composed of
100 * allowed characters and the length is not above the maximum allowed
101 * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
102 * allow_trailing_dot is true and at least two components are present
103 * in the name. Note that due to the restricted charset and length
104 * this call is substantially more conservative than
105 * dns_name_is_valid().
107 bool hostname_is_valid(const char *s
, bool allow_trailing_dot
) {
115 /* Doesn't accept empty hostnames, hostnames with
116 * leading dots, and hostnames with multiple dots in a
117 * sequence. Also ensures that the length stays below
120 for (p
= s
, dot
= true; *p
; p
++) {
128 if (!hostname_valid_char(*p
))
135 if (dot
&& (n_dots
< 2 || !allow_trailing_dot
))
138 if (p
-s
> HOST_NAME_MAX
) /* Note that HOST_NAME_MAX is 64 on
139 * Linux, but DNS allows domain names
140 * up to 255 characters */
146 char* hostname_cleanup(char *s
) {
152 strshorten(s
, HOST_NAME_MAX
);
154 for (p
= s
, d
= s
, dot
= true; *p
; p
++) {
161 } else if (hostname_valid_char(*p
)) {
175 bool is_localhost(const char *hostname
) {
178 /* This tries to identify local host and domain names
179 * described in RFC6761 plus the redhatism of localdomain */
181 return strcaseeq(hostname
, "localhost") ||
182 strcaseeq(hostname
, "localhost.") ||
183 strcaseeq(hostname
, "localhost.localdomain") ||
184 strcaseeq(hostname
, "localhost.localdomain.") ||
185 endswith_no_case(hostname
, ".localhost") ||
186 endswith_no_case(hostname
, ".localhost.") ||
187 endswith_no_case(hostname
, ".localhost.localdomain") ||
188 endswith_no_case(hostname
, ".localhost.localdomain.");
191 bool is_gateway_hostname(const char *hostname
) {
194 /* This tries to identify the valid syntaxes for the our
195 * synthetic "gateway" host. */
198 strcaseeq(hostname
, "_gateway") || strcaseeq(hostname
, "_gateway.")
199 #if ENABLE_COMPAT_GATEWAY_HOSTNAME
200 || strcaseeq(hostname
, "gateway") || strcaseeq(hostname
, "gateway.")
205 int sethostname_idempotent(const char *s
) {
206 char buf
[HOST_NAME_MAX
+ 1] = {};
210 if (gethostname(buf
, sizeof(buf
)) < 0)
216 if (sethostname(s
, strlen(s
)) < 0)
222 int read_hostname_config(const char *path
, char **hostname
) {
223 _cleanup_fclose_
FILE *f
= NULL
;
230 f
= fopen(path
, "re");
234 /* may have comments, ignore them */
235 FOREACH_LINE(l
, f
, return -errno
) {
237 if (!IN_SET(l
[0], '\0', '#')) {
238 /* found line with value */
239 name
= hostname_cleanup(l
);
248 /* no non-empty line found */