]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nss-myhostname/nss-myhostname.c
Merge pull request #9274 from poettering/comment-header-cleanup
[thirdparty/systemd.git] / src / nss-myhostname / nss-myhostname.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <net/if.h>
5 #include <netdb.h>
6 #include <nss.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "alloc-util.h"
11 #include "hostname-util.h"
12 #include "local-addresses.h"
13 #include "macro.h"
14 #include "nss-util.h"
15 #include "signal-util.h"
16 #include "string-util.h"
17 #include "util.h"
18
19 /* We use 127.0.0.2 as IPv4 address. This has the advantage over
20 * 127.0.0.1 that it can be translated back to the local hostname. For
21 * IPv6 we use ::1 which unfortunately will not translate back to the
22 * hostname but instead something like "localhost" or so. */
23
24 #define LOCALADDRESS_IPV4 (htobe32(0x7F000002))
25 #define LOCALADDRESS_IPV6 &in6addr_loopback
26
27 NSS_GETHOSTBYNAME_PROTOTYPES(myhostname);
28 NSS_GETHOSTBYADDR_PROTOTYPES(myhostname);
29
30 enum nss_status _nss_myhostname_gethostbyname4_r(
31 const char *name,
32 struct gaih_addrtuple **pat,
33 char *buffer, size_t buflen,
34 int *errnop, int *h_errnop,
35 int32_t *ttlp) {
36
37 struct gaih_addrtuple *r_tuple, *r_tuple_prev = NULL;
38 _cleanup_free_ struct local_address *addresses = NULL;
39 _cleanup_free_ char *hn = NULL;
40 const char *canonical = NULL;
41 int n_addresses = 0;
42 uint32_t local_address_ipv4;
43 struct local_address *a;
44 size_t l, idx, ms;
45 char *r_name;
46 unsigned n;
47
48 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
49
50 assert(name);
51 assert(pat);
52 assert(buffer);
53 assert(errnop);
54 assert(h_errnop);
55
56 if (is_localhost(name)) {
57 /* We respond to 'localhost', so that /etc/hosts
58 * is optional */
59
60 canonical = "localhost";
61 local_address_ipv4 = htobe32(INADDR_LOOPBACK);
62
63 } else if (is_gateway_hostname(name)) {
64
65 n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses);
66 if (n_addresses <= 0) {
67 *errnop = ENOENT;
68 *h_errnop = HOST_NOT_FOUND;
69 return NSS_STATUS_NOTFOUND;
70 }
71
72 canonical = "_gateway";
73
74 } else {
75 hn = gethostname_malloc();
76 if (!hn) {
77 *errnop = ENOMEM;
78 *h_errnop = NO_RECOVERY;
79 return NSS_STATUS_TRYAGAIN;
80 }
81
82 /* We respond to our local host name, our hostname suffixed with a single dot. */
83 if (!streq(name, hn) && !streq_ptr(startswith(name, hn), ".")) {
84 *errnop = ENOENT;
85 *h_errnop = HOST_NOT_FOUND;
86 return NSS_STATUS_NOTFOUND;
87 }
88
89 n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses);
90 if (n_addresses < 0)
91 n_addresses = 0;
92
93 canonical = hn;
94 local_address_ipv4 = LOCALADDRESS_IPV4;
95 }
96
97 l = strlen(canonical);
98 ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * (n_addresses > 0 ? n_addresses : 2);
99 if (buflen < ms) {
100 *errnop = ERANGE;
101 *h_errnop = NETDB_INTERNAL;
102 return NSS_STATUS_TRYAGAIN;
103 }
104
105 /* First, fill in hostname */
106 r_name = buffer;
107 memcpy(r_name, canonical, l+1);
108 idx = ALIGN(l+1);
109
110 assert(n_addresses >= 0);
111 if (n_addresses == 0) {
112 /* Second, fill in IPv6 tuple */
113 r_tuple = (struct gaih_addrtuple*) (buffer + idx);
114 r_tuple->next = r_tuple_prev;
115 r_tuple->name = r_name;
116 r_tuple->family = AF_INET6;
117 memcpy(r_tuple->addr, LOCALADDRESS_IPV6, 16);
118 r_tuple->scopeid = 0;
119
120 idx += ALIGN(sizeof(struct gaih_addrtuple));
121 r_tuple_prev = r_tuple;
122
123 /* Third, fill in IPv4 tuple */
124 r_tuple = (struct gaih_addrtuple*) (buffer + idx);
125 r_tuple->next = r_tuple_prev;
126 r_tuple->name = r_name;
127 r_tuple->family = AF_INET;
128 *(uint32_t*) r_tuple->addr = local_address_ipv4;
129 r_tuple->scopeid = 0;
130
131 idx += ALIGN(sizeof(struct gaih_addrtuple));
132 r_tuple_prev = r_tuple;
133 }
134
135 /* Fourth, fill actual addresses in, but in backwards order */
136 for (a = addresses + n_addresses - 1, n = 0; (int) n < n_addresses; n++, a--) {
137 r_tuple = (struct gaih_addrtuple*) (buffer + idx);
138 r_tuple->next = r_tuple_prev;
139 r_tuple->name = r_name;
140 r_tuple->family = a->family;
141 r_tuple->scopeid = a->family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&a->address.in6) ? a->ifindex : 0;
142 memcpy(r_tuple->addr, &a->address, 16);
143
144 idx += ALIGN(sizeof(struct gaih_addrtuple));
145 r_tuple_prev = r_tuple;
146 }
147
148 /* Verify the size matches */
149 assert(idx == ms);
150
151 /* Nscd expects us to store the first record in **pat. */
152 if (*pat)
153 **pat = *r_tuple_prev;
154 else
155 *pat = r_tuple_prev;
156
157 if (ttlp)
158 *ttlp = 0;
159
160 /* Explicitly reset all error variables */
161 *errnop = 0;
162 *h_errnop = NETDB_SUCCESS;
163 h_errno = 0;
164
165 return NSS_STATUS_SUCCESS;
166 }
167
168 static enum nss_status fill_in_hostent(
169 const char *canonical, const char *additional,
170 int af,
171 struct local_address *addresses, unsigned n_addresses,
172 uint32_t local_address_ipv4,
173 struct hostent *result,
174 char *buffer, size_t buflen,
175 int *errnop, int *h_errnop,
176 int32_t *ttlp,
177 char **canonp) {
178
179 size_t l_canonical, l_additional, idx, ms, alen;
180 char *r_addr, *r_name, *r_aliases, *r_alias = NULL, *r_addr_list;
181 struct local_address *a;
182 unsigned n, c;
183
184 assert(canonical);
185 assert(result);
186 assert(buffer);
187 assert(errnop);
188 assert(h_errnop);
189
190 alen = FAMILY_ADDRESS_SIZE(af);
191
192 for (a = addresses, n = 0, c = 0; n < n_addresses; a++, n++)
193 if (af == a->family)
194 c++;
195
196 l_canonical = strlen(canonical);
197 l_additional = strlen_ptr(additional);
198 ms = ALIGN(l_canonical+1)+
199 (additional ? ALIGN(l_additional+1) : 0) +
200 sizeof(char*) +
201 (additional ? sizeof(char*) : 0) +
202 (c > 0 ? c : 1) * ALIGN(alen) +
203 (c > 0 ? c+1 : 2) * sizeof(char*);
204
205 if (buflen < ms) {
206 *errnop = ERANGE;
207 *h_errnop = NETDB_INTERNAL;
208 return NSS_STATUS_TRYAGAIN;
209 }
210
211 /* First, fill in hostnames */
212 r_name = buffer;
213 memcpy(r_name, canonical, l_canonical+1);
214 idx = ALIGN(l_canonical+1);
215
216 if (additional) {
217 r_alias = buffer + idx;
218 memcpy(r_alias, additional, l_additional+1);
219 idx += ALIGN(l_additional+1);
220 }
221
222 /* Second, create aliases array */
223 r_aliases = buffer + idx;
224 if (additional) {
225 ((char**) r_aliases)[0] = r_alias;
226 ((char**) r_aliases)[1] = NULL;
227 idx += 2*sizeof(char*);
228 } else {
229 ((char**) r_aliases)[0] = NULL;
230 idx += sizeof(char*);
231 }
232
233 /* Third, add addresses */
234 r_addr = buffer + idx;
235 if (c > 0) {
236 unsigned i = 0;
237
238 for (a = addresses, n = 0; n < n_addresses; a++, n++) {
239 if (af != a->family)
240 continue;
241
242 memcpy(r_addr + i*ALIGN(alen), &a->address, alen);
243 i++;
244 }
245
246 assert(i == c);
247 idx += c*ALIGN(alen);
248 } else {
249 if (af == AF_INET)
250 *(uint32_t*) r_addr = local_address_ipv4;
251 else
252 memcpy(r_addr, LOCALADDRESS_IPV6, 16);
253
254 idx += ALIGN(alen);
255 }
256
257 /* Fourth, add address pointer array */
258 r_addr_list = buffer + idx;
259 if (c > 0) {
260 unsigned i;
261
262 for (i = 0; i < c; i++)
263 ((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen);
264
265 ((char**) r_addr_list)[i] = NULL;
266 idx += (c+1) * sizeof(char*);
267
268 } else {
269 ((char**) r_addr_list)[0] = r_addr;
270 ((char**) r_addr_list)[1] = NULL;
271 idx += 2 * sizeof(char*);
272 }
273
274 /* Verify the size matches */
275 assert(idx == ms);
276
277 result->h_name = r_name;
278 result->h_aliases = (char**) r_aliases;
279 result->h_addrtype = af;
280 result->h_length = alen;
281 result->h_addr_list = (char**) r_addr_list;
282
283 if (ttlp)
284 *ttlp = 0;
285
286 if (canonp)
287 *canonp = r_name;
288
289 /* Explicitly reset all error variables */
290 *errnop = 0;
291 *h_errnop = NETDB_SUCCESS;
292 h_errno = 0;
293
294 return NSS_STATUS_SUCCESS;
295 }
296
297 enum nss_status _nss_myhostname_gethostbyname3_r(
298 const char *name,
299 int af,
300 struct hostent *host,
301 char *buffer, size_t buflen,
302 int *errnop, int *h_errnop,
303 int32_t *ttlp,
304 char **canonp) {
305
306 _cleanup_free_ struct local_address *addresses = NULL;
307 const char *canonical, *additional = NULL;
308 _cleanup_free_ char *hn = NULL;
309 uint32_t local_address_ipv4 = 0;
310 int n_addresses = 0;
311
312 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
313
314 assert(name);
315 assert(host);
316 assert(buffer);
317 assert(errnop);
318 assert(h_errnop);
319
320 if (af == AF_UNSPEC)
321 af = AF_INET;
322
323 if (!IN_SET(af, AF_INET, AF_INET6)) {
324 *errnop = EAFNOSUPPORT;
325 *h_errnop = NO_DATA;
326 return NSS_STATUS_UNAVAIL;
327 }
328
329 if (is_localhost(name)) {
330 canonical = "localhost";
331 local_address_ipv4 = htobe32(INADDR_LOOPBACK);
332
333 } else if (is_gateway_hostname(name)) {
334
335 n_addresses = local_gateways(NULL, 0, af, &addresses);
336 if (n_addresses <= 0) {
337 *errnop = ENOENT;
338 *h_errnop = HOST_NOT_FOUND;
339 return NSS_STATUS_NOTFOUND;
340 }
341
342 canonical = "_gateway";
343
344 } else {
345 hn = gethostname_malloc();
346 if (!hn) {
347 *errnop = ENOMEM;
348 *h_errnop = NO_RECOVERY;
349 return NSS_STATUS_TRYAGAIN;
350 }
351
352 if (!streq(name, hn) && !streq_ptr(startswith(name, hn), ".")) {
353 *errnop = ENOENT;
354 *h_errnop = HOST_NOT_FOUND;
355 return NSS_STATUS_NOTFOUND;
356 }
357
358 n_addresses = local_addresses(NULL, 0, af, &addresses);
359 if (n_addresses < 0)
360 n_addresses = 0;
361
362 canonical = hn;
363 additional = n_addresses <= 0 && af == AF_INET6 ? "localhost" : NULL;
364 local_address_ipv4 = LOCALADDRESS_IPV4;
365 }
366
367 return fill_in_hostent(
368 canonical, additional,
369 af,
370 addresses, n_addresses,
371 local_address_ipv4,
372 host,
373 buffer, buflen,
374 errnop, h_errnop,
375 ttlp,
376 canonp);
377 }
378
379 enum nss_status _nss_myhostname_gethostbyaddr2_r(
380 const void* addr, socklen_t len,
381 int af,
382 struct hostent *host,
383 char *buffer, size_t buflen,
384 int *errnop, int *h_errnop,
385 int32_t *ttlp) {
386
387 const char *canonical = NULL, *additional = NULL;
388 uint32_t local_address_ipv4 = LOCALADDRESS_IPV4;
389 _cleanup_free_ struct local_address *addresses = NULL;
390 _cleanup_free_ char *hn = NULL;
391 int n_addresses = 0;
392 struct local_address *a;
393 bool additional_from_hostname = false;
394 unsigned n;
395
396 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
397
398 assert(addr);
399 assert(host);
400 assert(buffer);
401 assert(errnop);
402 assert(h_errnop);
403
404 if (!IN_SET(af, AF_INET, AF_INET6)) {
405 *errnop = EAFNOSUPPORT;
406 *h_errnop = NO_DATA;
407 return NSS_STATUS_UNAVAIL;
408 }
409
410 if (len != FAMILY_ADDRESS_SIZE(af)) {
411 *errnop = EINVAL;
412 *h_errnop = NO_RECOVERY;
413 return NSS_STATUS_UNAVAIL;
414 }
415
416 if (af == AF_INET) {
417 if ((*(uint32_t*) addr) == LOCALADDRESS_IPV4)
418 goto found;
419
420 if ((*(uint32_t*) addr) == htobe32(INADDR_LOOPBACK)) {
421 canonical = "localhost";
422 local_address_ipv4 = htobe32(INADDR_LOOPBACK);
423 goto found;
424 }
425
426 } else {
427 assert(af == AF_INET6);
428
429 if (memcmp(addr, LOCALADDRESS_IPV6, 16) == 0) {
430 canonical = "localhost";
431 additional_from_hostname = true;
432 goto found;
433 }
434 }
435
436 n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses);
437 for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) {
438 if (af != a->family)
439 continue;
440
441 if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0)
442 goto found;
443 }
444
445 addresses = mfree(addresses);
446
447 n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses);
448 for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) {
449 if (af != a->family)
450 continue;
451
452 if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0) {
453 canonical = "_gateway";
454 goto found;
455 }
456 }
457
458 *errnop = ENOENT;
459 *h_errnop = HOST_NOT_FOUND;
460 return NSS_STATUS_NOTFOUND;
461
462 found:
463 if (!canonical || additional_from_hostname) {
464 hn = gethostname_malloc();
465 if (!hn) {
466 *errnop = ENOMEM;
467 *h_errnop = NO_RECOVERY;
468 return NSS_STATUS_TRYAGAIN;
469 }
470
471 if (!canonical)
472 canonical = hn;
473 else
474 additional = hn;
475 }
476
477 return fill_in_hostent(
478 canonical, additional,
479 af,
480 addresses, n_addresses,
481 local_address_ipv4,
482 host,
483 buffer, buflen,
484 errnop, h_errnop,
485 ttlp,
486 NULL);
487 }
488
489 NSS_GETHOSTBYNAME_FALLBACKS(myhostname);
490 NSS_GETHOSTBYADDR_FALLBACKS(myhostname);