]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e3bff60a | 2 | |
4c89c718 MP |
3 | #include <errno.h> |
4 | #include <limits.h> | |
5 | #include <stdio.h> | |
6 | #include <string.h> | |
db2df898 | 7 | #include <sys/utsname.h> |
4c89c718 | 8 | #include <unistd.h> |
e3bff60a | 9 | |
52ad194e | 10 | #include "alloc-util.h" |
db2df898 MP |
11 | #include "fd-util.h" |
12 | #include "fileio.h" | |
e3bff60a | 13 | #include "hostname-util.h" |
4c89c718 | 14 | #include "macro.h" |
db2df898 | 15 | #include "string-util.h" |
e3bff60a MP |
16 | |
17 | bool hostname_is_set(void) { | |
18 | struct utsname u; | |
19 | ||
20 | assert_se(uname(&u) >= 0); | |
21 | ||
22 | if (isempty(u.nodename)) | |
23 | return false; | |
24 | ||
25 | /* This is the built-in kernel default host name */ | |
26 | if (streq(u.nodename, "(none)")) | |
27 | return false; | |
28 | ||
29 | return true; | |
30 | } | |
31 | ||
32 | char* gethostname_malloc(void) { | |
33 | struct utsname u; | |
34 | ||
aa27b158 MP |
35 | /* This call tries to return something useful, either the actual hostname |
36 | * or it makes something up. The only reason it might fail is OOM. | |
37 | * It might even return "localhost" if that's set. */ | |
38 | ||
e3bff60a MP |
39 | assert_se(uname(&u) >= 0); |
40 | ||
41 | if (isempty(u.nodename) || streq(u.nodename, "(none)")) | |
2897b343 | 42 | return strdup(FALLBACK_HOSTNAME); |
e3bff60a MP |
43 | |
44 | return strdup(u.nodename); | |
45 | } | |
46 | ||
aa27b158 MP |
47 | int gethostname_strict(char **ret) { |
48 | struct utsname u; | |
49 | char *k; | |
50 | ||
51 | /* This call will rather fail than make up a name. It will not return "localhost" either. */ | |
52 | ||
53 | assert_se(uname(&u) >= 0); | |
54 | ||
55 | if (isempty(u.nodename)) | |
56 | return -ENXIO; | |
57 | ||
58 | if (streq(u.nodename, "(none)")) | |
59 | return -ENXIO; | |
60 | ||
61 | if (is_localhost(u.nodename)) | |
62 | return -ENXIO; | |
63 | ||
64 | k = strdup(u.nodename); | |
65 | if (!k) | |
66 | return -ENOMEM; | |
67 | ||
68 | *ret = k; | |
69 | return 0; | |
70 | } | |
71 | ||
6e866b33 | 72 | bool valid_ldh_char(char c) { |
e3bff60a MP |
73 | return |
74 | (c >= 'a' && c <= 'z') || | |
75 | (c >= 'A' && c <= 'Z') || | |
76 | (c >= '0' && c <= '9') || | |
6e866b33 | 77 | c == '-'; |
e3bff60a MP |
78 | } |
79 | ||
13d276d0 MP |
80 | /** |
81 | * Check if s looks like a valid host name or FQDN. This does not do | |
82 | * full DNS validation, but only checks if the name is composed of | |
83 | * allowed characters and the length is not above the maximum allowed | |
84 | * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if | |
85 | * allow_trailing_dot is true and at least two components are present | |
86 | * in the name. Note that due to the restricted charset and length | |
87 | * this call is substantially more conservative than | |
4c89c718 | 88 | * dns_name_is_valid(). |
13d276d0 MP |
89 | */ |
90 | bool hostname_is_valid(const char *s, bool allow_trailing_dot) { | |
91 | unsigned n_dots = 0; | |
e3bff60a | 92 | const char *p; |
6e866b33 | 93 | bool dot, hyphen; |
e3bff60a MP |
94 | |
95 | if (isempty(s)) | |
96 | return false; | |
97 | ||
13d276d0 | 98 | /* Doesn't accept empty hostnames, hostnames with |
e3bff60a MP |
99 | * leading dots, and hostnames with multiple dots in a |
100 | * sequence. Also ensures that the length stays below | |
101 | * HOST_NAME_MAX. */ | |
102 | ||
6e866b33 | 103 | for (p = s, dot = hyphen = true; *p; p++) |
e3bff60a | 104 | if (*p == '.') { |
6e866b33 | 105 | if (dot || hyphen) |
e3bff60a MP |
106 | return false; |
107 | ||
108 | dot = true; | |
6e866b33 | 109 | hyphen = false; |
aa27b158 | 110 | n_dots++; |
6e866b33 MB |
111 | |
112 | } else if (*p == '-') { | |
113 | if (dot) | |
114 | return false; | |
115 | ||
116 | dot = false; | |
117 | hyphen = true; | |
118 | ||
e3bff60a | 119 | } else { |
6e866b33 | 120 | if (!valid_ldh_char(*p)) |
e3bff60a MP |
121 | return false; |
122 | ||
123 | dot = false; | |
6e866b33 | 124 | hyphen = false; |
e3bff60a | 125 | } |
e3bff60a | 126 | |
13d276d0 | 127 | if (dot && (n_dots < 2 || !allow_trailing_dot)) |
e3bff60a | 128 | return false; |
6e866b33 MB |
129 | if (hyphen) |
130 | return false; | |
e3bff60a | 131 | |
13d276d0 MP |
132 | if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on |
133 | * Linux, but DNS allows domain names | |
134 | * up to 255 characters */ | |
e3bff60a MP |
135 | return false; |
136 | ||
137 | return true; | |
138 | } | |
139 | ||
13d276d0 | 140 | char* hostname_cleanup(char *s) { |
e3bff60a | 141 | char *p, *d; |
6e866b33 | 142 | bool dot, hyphen; |
e3bff60a MP |
143 | |
144 | assert(s); | |
145 | ||
6e866b33 | 146 | for (p = s, d = s, dot = hyphen = true; *p && d - s < HOST_NAME_MAX; p++) |
e3bff60a | 147 | if (*p == '.') { |
6e866b33 | 148 | if (dot || hyphen) |
e3bff60a MP |
149 | continue; |
150 | ||
151 | *(d++) = '.'; | |
152 | dot = true; | |
6e866b33 MB |
153 | hyphen = false; |
154 | ||
155 | } else if (*p == '-') { | |
156 | if (dot) | |
157 | continue; | |
158 | ||
159 | *(d++) = '-'; | |
160 | dot = false; | |
161 | hyphen = true; | |
162 | ||
163 | } else if (valid_ldh_char(*p)) { | |
13d276d0 | 164 | *(d++) = *p; |
e3bff60a | 165 | dot = false; |
6e866b33 | 166 | hyphen = false; |
e3bff60a | 167 | } |
e3bff60a | 168 | |
6e866b33 MB |
169 | if (d > s && IN_SET(d[-1], '-', '.')) |
170 | /* The dot can occur at most once, but we might have multiple | |
171 | * hyphens, hence the loop */ | |
172 | d--; | |
173 | *d = 0; | |
e3bff60a | 174 | |
e3bff60a MP |
175 | return s; |
176 | } | |
177 | ||
178 | bool is_localhost(const char *hostname) { | |
179 | assert(hostname); | |
180 | ||
181 | /* This tries to identify local host and domain names | |
aa27b158 | 182 | * described in RFC6761 plus the redhatism of localdomain */ |
e3bff60a | 183 | |
13d276d0 MP |
184 | return strcaseeq(hostname, "localhost") || |
185 | strcaseeq(hostname, "localhost.") || | |
aa27b158 MP |
186 | strcaseeq(hostname, "localhost.localdomain") || |
187 | strcaseeq(hostname, "localhost.localdomain.") || | |
13d276d0 MP |
188 | endswith_no_case(hostname, ".localhost") || |
189 | endswith_no_case(hostname, ".localhost.") || | |
aa27b158 MP |
190 | endswith_no_case(hostname, ".localhost.localdomain") || |
191 | endswith_no_case(hostname, ".localhost.localdomain."); | |
13d276d0 MP |
192 | } |
193 | ||
194 | bool is_gateway_hostname(const char *hostname) { | |
195 | assert(hostname); | |
196 | ||
197 | /* This tries to identify the valid syntaxes for the our | |
198 | * synthetic "gateway" host. */ | |
199 | ||
200 | return | |
f5e65279 MB |
201 | strcaseeq(hostname, "_gateway") || strcaseeq(hostname, "_gateway.") |
202 | #if ENABLE_COMPAT_GATEWAY_HOSTNAME | |
203 | || strcaseeq(hostname, "gateway") || strcaseeq(hostname, "gateway.") | |
204 | #endif | |
205 | ; | |
e3bff60a MP |
206 | } |
207 | ||
208 | int sethostname_idempotent(const char *s) { | |
209 | char buf[HOST_NAME_MAX + 1] = {}; | |
210 | ||
211 | assert(s); | |
212 | ||
213 | if (gethostname(buf, sizeof(buf)) < 0) | |
214 | return -errno; | |
215 | ||
216 | if (streq(buf, s)) | |
217 | return 0; | |
218 | ||
219 | if (sethostname(s, strlen(s)) < 0) | |
220 | return -errno; | |
221 | ||
222 | return 1; | |
223 | } | |
224 | ||
52ad194e MB |
225 | int shorten_overlong(const char *s, char **ret) { |
226 | char *h, *p; | |
e3bff60a | 227 | |
52ad194e MB |
228 | /* Shorten an overlong name to HOST_NAME_MAX or to the first dot, |
229 | * whatever comes earlier. */ | |
e3bff60a | 230 | |
52ad194e MB |
231 | assert(s); |
232 | ||
233 | h = strdup(s); | |
234 | if (!h) | |
235 | return -ENOMEM; | |
236 | ||
237 | if (hostname_is_valid(h, false)) { | |
238 | *ret = h; | |
239 | return 0; | |
240 | } | |
241 | ||
242 | p = strchr(h, '.'); | |
243 | if (p) | |
244 | *p = 0; | |
245 | ||
246 | strshorten(h, HOST_NAME_MAX); | |
247 | ||
248 | if (!hostname_is_valid(h, false)) { | |
249 | free(h); | |
250 | return -EDOM; | |
251 | } | |
252 | ||
253 | *ret = h; | |
254 | return 1; | |
255 | } | |
256 | ||
257 | int read_etc_hostname_stream(FILE *f, char **ret) { | |
258 | int r; | |
e3bff60a | 259 | |
52ad194e MB |
260 | assert(f); |
261 | assert(ret); | |
262 | ||
263 | for (;;) { | |
264 | _cleanup_free_ char *line = NULL; | |
265 | char *p; | |
266 | ||
267 | r = read_line(f, LONG_LINE_MAX, &line); | |
268 | if (r < 0) | |
269 | return r; | |
270 | if (r == 0) /* EOF without any hostname? the file is empty, let's treat that exactly like no file at all: ENOENT */ | |
271 | return -ENOENT; | |
272 | ||
273 | p = strstrip(line); | |
274 | ||
275 | /* File may have empty lines or comments, ignore them */ | |
276 | if (!IN_SET(*p, '\0', '#')) { | |
277 | char *copy; | |
278 | ||
279 | hostname_cleanup(p); /* normalize the hostname */ | |
280 | ||
281 | if (!hostname_is_valid(p, true)) /* check that the hostname we return is valid */ | |
282 | return -EBADMSG; | |
283 | ||
284 | copy = strdup(p); | |
285 | if (!copy) | |
e3bff60a | 286 | return -ENOMEM; |
52ad194e MB |
287 | |
288 | *ret = copy; | |
289 | return 0; | |
e3bff60a MP |
290 | } |
291 | } | |
52ad194e | 292 | } |
e3bff60a | 293 | |
52ad194e MB |
294 | int read_etc_hostname(const char *path, char **ret) { |
295 | _cleanup_fclose_ FILE *f = NULL; | |
296 | ||
297 | assert(ret); | |
298 | ||
299 | if (!path) | |
300 | path = "/etc/hostname"; | |
301 | ||
302 | f = fopen(path, "re"); | |
303 | if (!f) | |
304 | return -errno; | |
305 | ||
306 | return read_etc_hostname_stream(f, ret); | |
e3bff60a | 307 | |
e3bff60a | 308 | } |