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