]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/hostname-util.c
tree-wide: make ++/-- usage consistent WRT spacing
[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 or it makes something up. The only
52 * reason it might mail is OOM. It might even return "localhost" if that's set. */
53
54 assert_se(uname(&u) >= 0);
55
56 if (isempty(u.nodename) || streq(u.nodename, "(none)"))
57 return strdup(u.sysname);
58
59 return strdup(u.nodename);
60 }
61
62 int gethostname_strict(char **ret) {
63 struct utsname u;
64 char *k;
65
66 /* This call will rather fail than make up a name. It will not return "localhost" either. */
67
68 assert_se(uname(&u) >= 0);
69
70 if (isempty(u.nodename))
71 return -ENXIO;
72
73 if (streq(u.nodename, "(none)"))
74 return -ENXIO;
75
76 if (is_localhost(u.nodename))
77 return -ENXIO;
78
79 k = strdup(u.nodename);
80 if (!k)
81 return -ENOMEM;
82
83 *ret = k;
84 return 0;
85 }
86
87 static bool hostname_valid_char(char c) {
88 return
89 (c >= 'a' && c <= 'z') ||
90 (c >= 'A' && c <= 'Z') ||
91 (c >= '0' && c <= '9') ||
92 c == '-' ||
93 c == '_' ||
94 c == '.';
95 }
96
97 /**
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().
106 */
107 bool hostname_is_valid(const char *s, bool allow_trailing_dot) {
108 unsigned n_dots = 0;
109 const char *p;
110 bool dot;
111
112 if (isempty(s))
113 return false;
114
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
118 * HOST_NAME_MAX. */
119
120 for (p = s, dot = true; *p; p++) {
121 if (*p == '.') {
122 if (dot)
123 return false;
124
125 dot = true;
126 n_dots++;
127 } else {
128 if (!hostname_valid_char(*p))
129 return false;
130
131 dot = false;
132 }
133 }
134
135 if (dot && (n_dots < 2 || !allow_trailing_dot))
136 return false;
137
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 */
141 return false;
142
143 return true;
144 }
145
146 char* hostname_cleanup(char *s) {
147 char *p, *d;
148 bool dot;
149
150 assert(s);
151
152 for (p = s, d = s, dot = true; *p; p++) {
153 if (*p == '.') {
154 if (dot)
155 continue;
156
157 *(d++) = '.';
158 dot = true;
159 } else if (hostname_valid_char(*p)) {
160 *(d++) = *p;
161 dot = false;
162 }
163
164 }
165
166 if (dot && d > s)
167 d[-1] = 0;
168 else
169 *d = 0;
170
171 strshorten(s, HOST_NAME_MAX);
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, "localdomain.") ||
185 strcaseeq(hostname, "localdomain") ||
186 endswith_no_case(hostname, ".localhost") ||
187 endswith_no_case(hostname, ".localhost.") ||
188 endswith_no_case(hostname, ".localdomain") ||
189 endswith_no_case(hostname, ".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 }