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