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