]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/hostname-util.c
Merge pull request #5373 from poettering/coredump-timestamp-fixes
[thirdparty/systemd.git] / src / basic / hostname-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2015 Lennart Poettering
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/utsname.h>
25 #include <unistd.h>
26
27 #include "fd-util.h"
28 #include "fileio.h"
29 #include "hostname-util.h"
30 #include "macro.h"
31 #include "string-util.h"
32
33 bool hostname_is_set(void) {
34 struct utsname u;
35
36 assert_se(uname(&u) >= 0);
37
38 if (isempty(u.nodename))
39 return false;
40
41 /* This is the built-in kernel default host name */
42 if (streq(u.nodename, "(none)"))
43 return false;
44
45 return true;
46 }
47
48 char* gethostname_malloc(void) {
49 struct utsname u;
50
51 /* This call tries to return something useful, either the actual hostname
52 * or it makes something up. The only reason it might fail is OOM.
53 * It might even return "localhost" if that's set. */
54
55 assert_se(uname(&u) >= 0);
56
57 if (isempty(u.nodename) || streq(u.nodename, "(none)"))
58 return strdup(FALLBACK_HOSTNAME);
59
60 return strdup(u.nodename);
61 }
62
63 int gethostname_strict(char **ret) {
64 struct utsname u;
65 char *k;
66
67 /* This call will rather fail than make up a name. It will not return "localhost" either. */
68
69 assert_se(uname(&u) >= 0);
70
71 if (isempty(u.nodename))
72 return -ENXIO;
73
74 if (streq(u.nodename, "(none)"))
75 return -ENXIO;
76
77 if (is_localhost(u.nodename))
78 return -ENXIO;
79
80 k = strdup(u.nodename);
81 if (!k)
82 return -ENOMEM;
83
84 *ret = k;
85 return 0;
86 }
87
88 static bool hostname_valid_char(char c) {
89 return
90 (c >= 'a' && c <= 'z') ||
91 (c >= 'A' && c <= 'Z') ||
92 (c >= '0' && c <= '9') ||
93 c == '-' ||
94 c == '_' ||
95 c == '.';
96 }
97
98 /**
99 * Check if s looks like a valid host name or FQDN. This does not do
100 * full DNS validation, but only checks if the name is composed of
101 * allowed characters and the length is not above the maximum allowed
102 * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
103 * allow_trailing_dot is true and at least two components are present
104 * in the name. Note that due to the restricted charset and length
105 * this call is substantially more conservative than
106 * dns_name_is_valid().
107 */
108 bool hostname_is_valid(const char *s, bool allow_trailing_dot) {
109 unsigned n_dots = 0;
110 const char *p;
111 bool dot;
112
113 if (isempty(s))
114 return false;
115
116 /* Doesn't accept empty hostnames, hostnames with
117 * leading dots, and hostnames with multiple dots in a
118 * sequence. Also ensures that the length stays below
119 * HOST_NAME_MAX. */
120
121 for (p = s, dot = true; *p; p++) {
122 if (*p == '.') {
123 if (dot)
124 return false;
125
126 dot = true;
127 n_dots++;
128 } else {
129 if (!hostname_valid_char(*p))
130 return false;
131
132 dot = false;
133 }
134 }
135
136 if (dot && (n_dots < 2 || !allow_trailing_dot))
137 return false;
138
139 if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on
140 * Linux, but DNS allows domain names
141 * up to 255 characters */
142 return false;
143
144 return true;
145 }
146
147 char* hostname_cleanup(char *s) {
148 char *p, *d;
149 bool dot;
150
151 assert(s);
152
153 strshorten(s, HOST_NAME_MAX);
154
155 for (p = s, d = s, dot = true; *p; p++) {
156 if (*p == '.') {
157 if (dot)
158 continue;
159
160 *(d++) = '.';
161 dot = true;
162 } else if (hostname_valid_char(*p)) {
163 *(d++) = *p;
164 dot = false;
165 }
166 }
167
168 if (dot && d > s)
169 d[-1] = 0;
170 else
171 *d = 0;
172
173 return s;
174 }
175
176 bool is_localhost(const char *hostname) {
177 assert(hostname);
178
179 /* This tries to identify local host and domain names
180 * described in RFC6761 plus the redhatism of localdomain */
181
182 return strcaseeq(hostname, "localhost") ||
183 strcaseeq(hostname, "localhost.") ||
184 strcaseeq(hostname, "localhost.localdomain") ||
185 strcaseeq(hostname, "localhost.localdomain.") ||
186 endswith_no_case(hostname, ".localhost") ||
187 endswith_no_case(hostname, ".localhost.") ||
188 endswith_no_case(hostname, ".localhost.localdomain") ||
189 endswith_no_case(hostname, ".localhost.localdomain.");
190 }
191
192 bool is_gateway_hostname(const char *hostname) {
193 assert(hostname);
194
195 /* This tries to identify the valid syntaxes for the our
196 * synthetic "gateway" host. */
197
198 return
199 strcaseeq(hostname, "gateway") ||
200 strcaseeq(hostname, "gateway.");
201 }
202
203 int sethostname_idempotent(const char *s) {
204 char buf[HOST_NAME_MAX + 1] = {};
205
206 assert(s);
207
208 if (gethostname(buf, sizeof(buf)) < 0)
209 return -errno;
210
211 if (streq(buf, s))
212 return 0;
213
214 if (sethostname(s, strlen(s)) < 0)
215 return -errno;
216
217 return 1;
218 }
219
220 int read_hostname_config(const char *path, char **hostname) {
221 _cleanup_fclose_ FILE *f = NULL;
222 char l[LINE_MAX];
223 char *name = NULL;
224
225 assert(path);
226 assert(hostname);
227
228 f = fopen(path, "re");
229 if (!f)
230 return -errno;
231
232 /* may have comments, ignore them */
233 FOREACH_LINE(l, f, return -errno) {
234 truncate_nl(l);
235 if (l[0] != '\0' && l[0] != '#') {
236 /* found line with value */
237 name = hostname_cleanup(l);
238 name = strdup(name);
239 if (!name)
240 return -ENOMEM;
241 break;
242 }
243 }
244
245 if (!name)
246 /* no non-empty line found */
247 return -ENOENT;
248
249 *hostname = name;
250 return 0;
251 }