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