]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nss-myhostname/nss-myhostname.c
Merge pull request #11617 from topimiettinen/backlight-handle-zero-file-load
[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 UNPROTECT_ERRNO;
78 *errnop = ENOMEM;
79 *h_errnop = NO_RECOVERY;
80 return NSS_STATUS_TRYAGAIN;
81 }
82
83 /* We respond to our local host name, our hostname suffixed with a single dot. */
84 if (!streq(name, hn) && !streq_ptr(startswith(name, hn), ".")) {
85 *h_errnop = HOST_NOT_FOUND;
86 return NSS_STATUS_NOTFOUND;
87 }
88
89 n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses);
90 if (n_addresses < 0)
91 n_addresses = 0;
92
93 canonical = hn;
94 local_address_ipv4 = LOCALADDRESS_IPV4;
95 }
96
97 l = strlen(canonical);
98 ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * (n_addresses > 0 ? n_addresses : 2);
99 if (buflen < ms) {
100 UNPROTECT_ERRNO;
101 *errnop = ERANGE;
102 *h_errnop = NETDB_INTERNAL;
103 return NSS_STATUS_TRYAGAIN;
104 }
105
106 /* First, fill in hostname */
107 r_name = buffer;
108 memcpy(r_name, canonical, l+1);
109 idx = ALIGN(l+1);
110
111 assert(n_addresses >= 0);
112 if (n_addresses == 0) {
113 /* Second, fill in IPv6 tuple */
114 r_tuple = (struct gaih_addrtuple*) (buffer + idx);
115 r_tuple->next = r_tuple_prev;
116 r_tuple->name = r_name;
117 r_tuple->family = AF_INET6;
118 memcpy(r_tuple->addr, LOCALADDRESS_IPV6, 16);
119 r_tuple->scopeid = 0;
120
121 idx += ALIGN(sizeof(struct gaih_addrtuple));
122 r_tuple_prev = r_tuple;
123
124 /* Third, fill in IPv4 tuple */
125 r_tuple = (struct gaih_addrtuple*) (buffer + idx);
126 r_tuple->next = r_tuple_prev;
127 r_tuple->name = r_name;
128 r_tuple->family = AF_INET;
129 *(uint32_t*) r_tuple->addr = local_address_ipv4;
130 r_tuple->scopeid = 0;
131
132 idx += ALIGN(sizeof(struct gaih_addrtuple));
133 r_tuple_prev = r_tuple;
134 }
135
136 /* Fourth, fill actual addresses in, but in backwards order */
137 for (a = addresses + n_addresses - 1, n = 0; (int) n < n_addresses; n++, a--) {
138 r_tuple = (struct gaih_addrtuple*) (buffer + idx);
139 r_tuple->next = r_tuple_prev;
140 r_tuple->name = r_name;
141 r_tuple->family = a->family;
142 r_tuple->scopeid = a->family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&a->address.in6) ? a->ifindex : 0;
143 memcpy(r_tuple->addr, &a->address, 16);
144
145 idx += ALIGN(sizeof(struct gaih_addrtuple));
146 r_tuple_prev = r_tuple;
147 }
148
149 /* Verify the size matches */
150 assert(idx == ms);
151
152 /* Nscd expects us to store the first record in **pat. */
153 if (*pat)
154 **pat = *r_tuple_prev;
155 else
156 *pat = r_tuple_prev;
157
158 if (ttlp)
159 *ttlp = 0;
160
161 /* Explicitly reset both *h_errnop and h_errno to work around
162 * https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */
163 *h_errnop = NETDB_SUCCESS;
164 h_errno = 0;
165
166 return NSS_STATUS_SUCCESS;
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 *h_errnop = HOST_NOT_FOUND;
344 return NSS_STATUS_NOTFOUND;
345 }
346
347 canonical = "_gateway";
348
349 } else {
350 hn = gethostname_malloc();
351 if (!hn) {
352 UNPROTECT_ERRNO;
353 *errnop = ENOMEM;
354 *h_errnop = NO_RECOVERY;
355 return NSS_STATUS_TRYAGAIN;
356 }
357
358 if (!streq(name, hn) && !streq_ptr(startswith(name, hn), ".")) {
359 *h_errnop = HOST_NOT_FOUND;
360 return NSS_STATUS_NOTFOUND;
361 }
362
363 n_addresses = local_addresses(NULL, 0, af, &addresses);
364 if (n_addresses < 0)
365 n_addresses = 0;
366
367 canonical = hn;
368 additional = n_addresses <= 0 && af == AF_INET6 ? "localhost" : NULL;
369 local_address_ipv4 = LOCALADDRESS_IPV4;
370 }
371
372 UNPROTECT_ERRNO;
373
374 return fill_in_hostent(
375 canonical, additional,
376 af,
377 addresses, n_addresses,
378 local_address_ipv4,
379 host,
380 buffer, buflen,
381 errnop, h_errnop,
382 ttlp,
383 canonp);
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);