]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/hostname-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/utsname.h>
27 #include "hostname-util.h"
28 #include "string-util.h"
31 bool hostname_is_set(void) {
34 assert_se(uname(&u
) >= 0);
36 if (isempty(u
.nodename
))
39 /* This is the built-in kernel default host name */
40 if (streq(u
.nodename
, "(none)"))
46 char* gethostname_malloc(void) {
49 assert_se(uname(&u
) >= 0);
51 if (isempty(u
.nodename
) || streq(u
.nodename
, "(none)"))
52 return strdup(u
.sysname
);
54 return strdup(u
.nodename
);
57 static bool hostname_valid_char(char c
) {
59 (c
>= 'a' && c
<= 'z') ||
60 (c
>= 'A' && c
<= 'Z') ||
61 (c
>= '0' && c
<= '9') ||
68 * Check if s looks like a valid host name or FQDN. This does not do
69 * full DNS validation, but only checks if the name is composed of
70 * allowed characters and the length is not above the maximum allowed
71 * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
72 * allow_trailing_dot is true and at least two components are present
73 * in the name. Note that due to the restricted charset and length
74 * this call is substantially more conservative than
75 * dns_domain_is_valid().
77 bool hostname_is_valid(const char *s
, bool allow_trailing_dot
) {
85 /* Doesn't accept empty hostnames, hostnames with
86 * leading dots, and hostnames with multiple dots in a
87 * sequence. Also ensures that the length stays below
90 for (p
= s
, dot
= true; *p
; p
++) {
98 if (!hostname_valid_char(*p
))
105 if (dot
&& (n_dots
< 2 || !allow_trailing_dot
))
108 if (p
-s
> HOST_NAME_MAX
) /* Note that HOST_NAME_MAX is 64 on
109 * Linux, but DNS allows domain names
110 * up to 255 characters */
116 char* hostname_cleanup(char *s
) {
122 for (p
= s
, d
= s
, dot
= true; *p
; p
++) {
129 } else if (hostname_valid_char(*p
)) {
141 strshorten(s
, HOST_NAME_MAX
);
146 bool is_localhost(const char *hostname
) {
149 /* This tries to identify local host and domain names
150 * described in RFC6761 plus the redhatism of .localdomain */
152 return strcaseeq(hostname
, "localhost") ||
153 strcaseeq(hostname
, "localhost.") ||
154 strcaseeq(hostname
, "localdomain.") ||
155 strcaseeq(hostname
, "localdomain") ||
156 endswith_no_case(hostname
, ".localhost") ||
157 endswith_no_case(hostname
, ".localhost.") ||
158 endswith_no_case(hostname
, ".localdomain") ||
159 endswith_no_case(hostname
, ".localdomain.");
162 bool is_gateway_hostname(const char *hostname
) {
165 /* This tries to identify the valid syntaxes for the our
166 * synthetic "gateway" host. */
169 strcaseeq(hostname
, "gateway") ||
170 strcaseeq(hostname
, "gateway.");
173 int sethostname_idempotent(const char *s
) {
174 char buf
[HOST_NAME_MAX
+ 1] = {};
178 if (gethostname(buf
, sizeof(buf
)) < 0)
184 if (sethostname(s
, strlen(s
)) < 0)
190 int read_hostname_config(const char *path
, char **hostname
) {
191 _cleanup_fclose_
FILE *f
= NULL
;
198 f
= fopen(path
, "re");
202 /* may have comments, ignore them */
203 FOREACH_LINE(l
, f
, return -errno
) {
205 if (l
[0] != '\0' && l
[0] != '#') {
206 /* found line with value */
207 name
= hostname_cleanup(l
);
216 /* no non-empty line found */