]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/nss-myhostname/nss-myhostname.c
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) {
b26c9041 77 *errnop = DISARM_PROTECT_ERRNO(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) {
b26c9041 99 *errnop = DISARM_PROTECT_ERRNO(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,
b26c9041 174 int *errnop, int *h_errnop, int* _saved_errno_p,
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);
b26c9041 188 assert(_saved_errno_p);
5502f0d9 189
9d485985 190 alen = FAMILY_ADDRESS_SIZE(af);
8041b5ba 191
8041b5ba
LP
192 for (a = addresses, n = 0, c = 0; n < n_addresses; a++, n++)
193 if (af == a->family)
194 c++;
4e8c8252 195
e8a7a315 196 l_canonical = strlen(canonical);
7bf7ce28 197 l_additional = strlen_ptr(additional);
e8a7a315
LP
198 ms = ALIGN(l_canonical+1)+
199 (additional ? ALIGN(l_additional+1) : 0) +
5502f0d9 200 sizeof(char*) +
e8a7a315 201 (additional ? sizeof(char*) : 0) +
d4c9895d 202 (c > 0 ? c : 1) * ALIGN(alen) +
5502f0d9 203 (c > 0 ? c+1 : 2) * sizeof(char*);
8041b5ba 204
4e8c8252 205 if (buflen < ms) {
b26c9041 206 *errnop = DISARM_PROTECT_ERRNO_INNER(ERANGE);
cda458a5 207 *h_errnop = NETDB_INTERNAL;
4e8c8252
LP
208 return NSS_STATUS_TRYAGAIN;
209 }
210
e8a7a315 211 /* First, fill in hostnames */
4e8c8252 212 r_name = buffer;
e8a7a315
LP
213 memcpy(r_name, canonical, l_canonical+1);
214 idx = ALIGN(l_canonical+1);
4e8c8252 215
e8a7a315
LP
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 */
4e8c8252 223 r_aliases = buffer + idx;
e8a7a315
LP
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 }
4e8c8252 232
8041b5ba 233 /* Third, add addresses */
4e8c8252 234 r_addr = buffer + idx;
8041b5ba
LP
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
5502f0d9 242 memcpy(r_addr + i*ALIGN(alen), &a->address, alen);
8041b5ba
LP
243 i++;
244 }
245
246 assert(i == c);
247 idx += c*ALIGN(alen);
248 } else {
249 if (af == AF_INET)
e8a7a315 250 *(uint32_t*) r_addr = local_address_ipv4;
8041b5ba
LP
251 else
252 memcpy(r_addr, LOCALADDRESS_IPV6, 16);
253
254 idx += ALIGN(alen);
255 }
4e8c8252
LP
256
257 /* Fourth, add address pointer array */
258 r_addr_list = buffer + idx;
8041b5ba 259 if (c > 0) {
d4c9895d 260 unsigned i;
8041b5ba 261
d4c9895d
LP
262 for (i = 0; i < c; i++)
263 ((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen);
8041b5ba 264
d4c9895d
LP
265 ((char**) r_addr_list)[i] = NULL;
266 idx += (c+1) * sizeof(char*);
8041b5ba
LP
267
268 } else {
269 ((char**) r_addr_list)[0] = r_addr;
270 ((char**) r_addr_list)[1] = NULL;
d4c9895d 271 idx += 2 * sizeof(char*);
8041b5ba 272 }
4e8c8252
LP
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
06202b9e
YW
289 /* Explicitly reset both *h_errnop and h_errno to work around
290 * https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */
e70df46b
LP
291 *h_errnop = NETDB_SUCCESS;
292 h_errno = 0;
293
4e8c8252 294 return NSS_STATUS_SUCCESS;
6b21f0cf
LP
295}
296
4e8c8252
LP
297enum 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) {
6b21f0cf 305
e80af1bd 306 _cleanup_free_ struct local_address *addresses = NULL;
e8a7a315 307 const char *canonical, *additional = NULL;
5502f0d9 308 _cleanup_free_ char *hn = NULL;
e9140aff 309 uint32_t local_address_ipv4 = 0;
e80af1bd 310 int n_addresses = 0;
5502f0d9 311
06202b9e 312 PROTECT_ERRNO;
0c5eb056
LP
313 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
314
5502f0d9
LP
315 assert(name);
316 assert(host);
317 assert(buffer);
318 assert(errnop);
319 assert(h_errnop);
6b21f0cf 320
4e8c8252
LP
321 if (af == AF_UNSPEC)
322 af = AF_INET;
6b21f0cf 323
ec2ce0c5 324 if (!IN_SET(af, AF_INET, AF_INET6)) {
b26c9041 325 *errnop = DISARM_PROTECT_ERRNO(EAFNOSUPPORT);
4e8c8252
LP
326 *h_errnop = NO_DATA;
327 return NSS_STATUS_UNAVAIL;
328 }
6b21f0cf 329
5502f0d9 330 if (is_localhost(name)) {
e8a7a315 331 canonical = "localhost";
8e38570e 332 local_address_ipv4 = htobe32(INADDR_LOOPBACK);
e9140aff 333
46a5e0e7 334 } else if (is_gateway_hostname(name)) {
e9140aff 335
1d050e1e 336 n_addresses = local_gateways(NULL, 0, af, &addresses);
e9140aff 337 if (n_addresses <= 0) {
e9140aff
LP
338 *h_errnop = HOST_NOT_FOUND;
339 return NSS_STATUS_NOTFOUND;
340 }
341
5248e7e1 342 canonical = "_gateway";
e9140aff 343
e8a7a315 344 } else {
5502f0d9
LP
345 hn = gethostname_malloc();
346 if (!hn) {
b26c9041 347 *errnop = DISARM_PROTECT_ERRNO(ENOMEM);
e8a7a315 348 *h_errnop = NO_RECOVERY;
5502f0d9 349 return NSS_STATUS_TRYAGAIN;
e8a7a315 350 }
6b21f0cf 351
5502f0d9 352 if (!streq(name, hn) && !streq_ptr(startswith(name, hn), ".")) {
e8a7a315
LP
353 *h_errnop = HOST_NOT_FOUND;
354 return NSS_STATUS_NOTFOUND;
355 }
356
1d050e1e 357 n_addresses = local_addresses(NULL, 0, af, &addresses);
e80af1bd
LP
358 if (n_addresses < 0)
359 n_addresses = 0;
e8a7a315
LP
360
361 canonical = hn;
362 additional = n_addresses <= 0 && af == AF_INET6 ? "localhost" : NULL;
363 local_address_ipv4 = LOCALADDRESS_IPV4;
4e8c8252 364 }
6b21f0cf 365
e8a7a315
LP
366 return fill_in_hostent(
367 canonical, additional,
368 af,
369 addresses, n_addresses,
370 local_address_ipv4,
371 host,
372 buffer, buflen,
b26c9041 373 errnop, h_errnop, &_saved_errno_,
e8a7a315
LP
374 ttlp,
375 canonp);
4e8c8252 376}
6b21f0cf 377
4e8c8252
LP
378enum nss_status _nss_myhostname_gethostbyaddr2_r(
379 const void* addr, socklen_t len,
380 int af,
381 struct hostent *host,
382 char *buffer, size_t buflen,
383 int *errnop, int *h_errnop,
384 int32_t *ttlp) {
6b21f0cf 385
e8a7a315 386 const char *canonical = NULL, *additional = NULL;
5502f0d9 387 uint32_t local_address_ipv4 = LOCALADDRESS_IPV4;
e80af1bd 388 _cleanup_free_ struct local_address *addresses = NULL;
5502f0d9 389 _cleanup_free_ char *hn = NULL;
e80af1bd
LP
390 int n_addresses = 0;
391 struct local_address *a;
3fdcecc8 392 bool additional_from_hostname = false;
e80af1bd 393 unsigned n;
5502f0d9 394
06202b9e 395 PROTECT_ERRNO;
0c5eb056
LP
396 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
397
5502f0d9
LP
398 assert(addr);
399 assert(host);
400 assert(buffer);
401 assert(errnop);
402 assert(h_errnop);
8041b5ba 403
555bd6e9 404 if (!IN_SET(af, AF_INET, AF_INET6)) {
b26c9041 405 *errnop = DISARM_PROTECT_ERRNO(EAFNOSUPPORT);
555bd6e9
LP
406 *h_errnop = NO_DATA;
407 return NSS_STATUS_UNAVAIL;
408 }
409
9d485985 410 if (len != FAMILY_ADDRESS_SIZE(af)) {
b26c9041 411 *errnop = DISARM_PROTECT_ERRNO(EINVAL);
8041b5ba
LP
412 *h_errnop = NO_RECOVERY;
413 return NSS_STATUS_UNAVAIL;
414 }
6b21f0cf 415
4e8c8252 416 if (af == AF_INET) {
8041b5ba
LP
417 if ((*(uint32_t*) addr) == LOCALADDRESS_IPV4)
418 goto found;
6b21f0cf 419
8e38570e 420 if ((*(uint32_t*) addr) == htobe32(INADDR_LOOPBACK)) {
e8a7a315 421 canonical = "localhost";
8e38570e 422 local_address_ipv4 = htobe32(INADDR_LOOPBACK);
e8a7a315
LP
423 goto found;
424 }
425
555bd6e9
LP
426 } else {
427 assert(af == AF_INET6);
8041b5ba 428
e8a7a315 429 if (memcmp(addr, LOCALADDRESS_IPV6, 16) == 0) {
3fdcecc8
LP
430 canonical = "localhost";
431 additional_from_hostname = true;
8041b5ba 432 goto found;
e8a7a315 433 }
4e8c8252
LP
434 }
435
1d050e1e 436 n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses);
68a9c7c4
ZJS
437 for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) {
438 if (af != a->family)
439 continue;
8041b5ba 440
68a9c7c4
ZJS
441 if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0)
442 goto found;
e9140aff
LP
443 }
444
97b11eed 445 addresses = mfree(addresses);
e9140aff 446
1d050e1e 447 n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses);
68a9c7c4
ZJS
448 for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) {
449 if (af != a->family)
450 continue;
e9140aff 451
68a9c7c4 452 if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0) {
5248e7e1 453 canonical = "_gateway";
68a9c7c4 454 goto found;
e9140aff 455 }
8041b5ba
LP
456 }
457
8041b5ba 458 *h_errnop = HOST_NOT_FOUND;
8041b5ba
LP
459 return NSS_STATUS_NOTFOUND;
460
461found:
82e4c2d6 462 if (!canonical || additional_from_hostname) {
3fdcecc8
LP
463 hn = gethostname_malloc();
464 if (!hn) {
b26c9041 465 *errnop = DISARM_PROTECT_ERRNO(ENOMEM);
3fdcecc8
LP
466 *h_errnop = NO_RECOVERY;
467 return NSS_STATUS_TRYAGAIN;
468 }
469
470 if (!canonical)
471 canonical = hn;
82e4c2d6 472 else
3fdcecc8
LP
473 additional = hn;
474 }
4e8c8252 475
e8a7a315
LP
476 return fill_in_hostent(
477 canonical, additional,
478 af,
479 addresses, n_addresses,
480 local_address_ipv4,
481 host,
482 buffer, buflen,
b26c9041 483 errnop, h_errnop, &_saved_errno_,
e8a7a315
LP
484 ttlp,
485 NULL);
4e8c8252 486}
6b21f0cf 487
c9fdc26e
LP
488NSS_GETHOSTBYNAME_FALLBACKS(myhostname);
489NSS_GETHOSTBYADDR_FALLBACKS(myhostname);