]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nss-myhostname/nss-myhostname.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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 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 *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 *h_errnop = HOST_NOT_FOUND;
85 return NSS_STATUS_NOTFOUND;
86 }
87
88 n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses);
89 if (n_addresses < 0)
90 n_addresses = 0;
91
92 canonical = hn;
93 local_address_ipv4 = LOCALADDRESS_IPV4;
94 }
95
96 l = strlen(canonical);
97 ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * (n_addresses > 0 ? n_addresses : 2);
98 if (buflen < ms) {
99 *errnop = ERANGE;
100 *h_errnop = NETDB_INTERNAL;
101 return NSS_STATUS_TRYAGAIN;
102 }
103
104 /* First, fill in hostname */
105 r_name = buffer;
106 memcpy(r_name, canonical, l+1);
107 idx = ALIGN(l+1);
108
109 assert(n_addresses >= 0);
110 if (n_addresses == 0) {
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);
117 r_tuple->scopeid = 0;
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;
127 *(uint32_t*) r_tuple->addr = local_address_ipv4;
128 r_tuple->scopeid = 0;
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 */
135 for (a = addresses + n_addresses - 1, n = 0; (int) n < n_addresses; n++, a--) {
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;
140 r_tuple->scopeid = a->family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&a->address.in6) ? a->ifindex : 0;
141 memcpy(r_tuple->addr, &a->address, 16);
142
143 idx += ALIGN(sizeof(struct gaih_addrtuple));
144 r_tuple_prev = r_tuple;
145 }
146
147 /* Verify the size matches */
148 assert(idx == ms);
149
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;
155
156 if (ttlp)
157 *ttlp = 0;
158
159 /* Explicitly reset both *h_errnop and h_errno to work around
160 * https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */
161 *h_errnop = NETDB_SUCCESS;
162 h_errno = 0;
163
164 return NSS_STATUS_SUCCESS;
165 }
166
167 static enum nss_status fill_in_hostent(
168 const char *canonical, const char *additional,
169 int af,
170 struct local_address *addresses, unsigned n_addresses,
171 uint32_t local_address_ipv4,
172 struct hostent *result,
173 char *buffer, size_t buflen,
174 int *errnop, int *h_errnop,
175 int32_t *ttlp,
176 char **canonp) {
177
178 size_t l_canonical, l_additional, idx, ms, alen;
179 char *r_addr, *r_name, *r_aliases, *r_alias = NULL, *r_addr_list;
180 struct local_address *a;
181 unsigned n, c;
182
183 assert(canonical);
184 assert(result);
185 assert(buffer);
186 assert(errnop);
187 assert(h_errnop);
188
189 alen = FAMILY_ADDRESS_SIZE(af);
190
191 for (a = addresses, n = 0, c = 0; n < n_addresses; a++, n++)
192 if (af == a->family)
193 c++;
194
195 l_canonical = strlen(canonical);
196 l_additional = strlen_ptr(additional);
197 ms = ALIGN(l_canonical+1)+
198 (additional ? ALIGN(l_additional+1) : 0) +
199 sizeof(char*) +
200 (additional ? sizeof(char*) : 0) +
201 (c > 0 ? c : 1) * ALIGN(alen) +
202 (c > 0 ? c+1 : 2) * sizeof(char*);
203
204 if (buflen < ms) {
205 *errnop = ERANGE;
206 *h_errnop = NETDB_INTERNAL;
207 return NSS_STATUS_TRYAGAIN;
208 }
209
210 /* First, fill in hostnames */
211 r_name = buffer;
212 memcpy(r_name, canonical, l_canonical+1);
213 idx = ALIGN(l_canonical+1);
214
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 */
222 r_aliases = buffer + idx;
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 }
231
232 /* Third, add addresses */
233 r_addr = buffer + idx;
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
241 memcpy(r_addr + i*ALIGN(alen), &a->address, alen);
242 i++;
243 }
244
245 assert(i == c);
246 idx += c*ALIGN(alen);
247 } else {
248 if (af == AF_INET)
249 *(uint32_t*) r_addr = local_address_ipv4;
250 else
251 memcpy(r_addr, LOCALADDRESS_IPV6, 16);
252
253 idx += ALIGN(alen);
254 }
255
256 /* Fourth, add address pointer array */
257 r_addr_list = buffer + idx;
258 if (c > 0) {
259 unsigned i;
260
261 for (i = 0; i < c; i++)
262 ((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen);
263
264 ((char**) r_addr_list)[i] = NULL;
265 idx += (c+1) * sizeof(char*);
266
267 } else {
268 ((char**) r_addr_list)[0] = r_addr;
269 ((char**) r_addr_list)[1] = NULL;
270 idx += 2 * sizeof(char*);
271 }
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
288 /* Explicitly reset both *h_errnop and h_errno to work around
289 * https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */
290 *h_errnop = NETDB_SUCCESS;
291 h_errno = 0;
292
293 return NSS_STATUS_SUCCESS;
294 }
295
296 enum 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) {
304
305 _cleanup_free_ struct local_address *addresses = NULL;
306 const char *canonical, *additional = NULL;
307 _cleanup_free_ char *hn = NULL;
308 uint32_t local_address_ipv4 = 0;
309 int n_addresses = 0;
310
311 PROTECT_ERRNO;
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 *h_errnop = HOST_NOT_FOUND;
338 return NSS_STATUS_NOTFOUND;
339 }
340
341 canonical = "_gateway";
342
343 } else {
344 hn = gethostname_malloc();
345 if (!hn) {
346 *errnop = ENOMEM;
347 *h_errnop = NO_RECOVERY;
348 return NSS_STATUS_TRYAGAIN;
349 }
350
351 if (!streq(name, hn) && !streq_ptr(startswith(name, hn), ".")) {
352 *h_errnop = HOST_NOT_FOUND;
353 return NSS_STATUS_NOTFOUND;
354 }
355
356 n_addresses = local_addresses(NULL, 0, af, &addresses);
357 if (n_addresses < 0)
358 n_addresses = 0;
359
360 canonical = hn;
361 additional = n_addresses <= 0 && af == AF_INET6 ? "localhost" : NULL;
362 local_address_ipv4 = LOCALADDRESS_IPV4;
363 }
364
365 return fill_in_hostent(
366 canonical, additional,
367 af,
368 addresses, n_addresses,
369 local_address_ipv4,
370 host,
371 buffer, buflen,
372 errnop, h_errnop,
373 ttlp,
374 canonp);
375 }
376
377 enum 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) {
384
385 const char *canonical = NULL, *additional = NULL;
386 uint32_t local_address_ipv4 = LOCALADDRESS_IPV4;
387 _cleanup_free_ struct local_address *addresses = NULL;
388 _cleanup_free_ char *hn = NULL;
389 int n_addresses = 0;
390 struct local_address *a;
391 bool additional_from_hostname = false;
392 unsigned n;
393
394 PROTECT_ERRNO;
395 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
396
397 assert(addr);
398 assert(host);
399 assert(buffer);
400 assert(errnop);
401 assert(h_errnop);
402
403 if (!IN_SET(af, AF_INET, AF_INET6)) {
404 *errnop = EAFNOSUPPORT;
405 *h_errnop = NO_DATA;
406 return NSS_STATUS_UNAVAIL;
407 }
408
409 if (len != FAMILY_ADDRESS_SIZE(af)) {
410 *errnop = EINVAL;
411 *h_errnop = NO_RECOVERY;
412 return NSS_STATUS_UNAVAIL;
413 }
414
415 if (af == AF_INET) {
416 if ((*(uint32_t*) addr) == LOCALADDRESS_IPV4)
417 goto found;
418
419 if ((*(uint32_t*) addr) == htobe32(INADDR_LOOPBACK)) {
420 canonical = "localhost";
421 local_address_ipv4 = htobe32(INADDR_LOOPBACK);
422 goto found;
423 }
424
425 } else {
426 assert(af == AF_INET6);
427
428 if (memcmp(addr, LOCALADDRESS_IPV6, 16) == 0) {
429 canonical = "localhost";
430 additional_from_hostname = true;
431 goto found;
432 }
433 }
434
435 n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses);
436 for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) {
437 if (af != a->family)
438 continue;
439
440 if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0)
441 goto found;
442 }
443
444 addresses = mfree(addresses);
445
446 n_addresses = local_gateways(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 canonical = "_gateway";
453 goto found;
454 }
455 }
456
457 *h_errnop = HOST_NOT_FOUND;
458 return NSS_STATUS_NOTFOUND;
459
460 found:
461 if (!canonical || additional_from_hostname) {
462 hn = gethostname_malloc();
463 if (!hn) {
464 *errnop = ENOMEM;
465 *h_errnop = NO_RECOVERY;
466 return NSS_STATUS_TRYAGAIN;
467 }
468
469 if (!canonical)
470 canonical = hn;
471 else
472 additional = hn;
473 }
474
475 return fill_in_hostent(
476 canonical, additional,
477 af,
478 addresses, n_addresses,
479 local_address_ipv4,
480 host,
481 buffer, buflen,
482 errnop, h_errnop,
483 ttlp,
484 NULL);
485 }
486
487 NSS_GETHOSTBYNAME_FALLBACKS(myhostname);
488 NSS_GETHOSTBYADDR_FALLBACKS(myhostname);