]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nss-myhostname/nss-myhostname.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "alloc-util.h"
10 #include "errno-util.h"
11 #include "hostname-util.h"
12 #include "local-addresses.h"
15 #include "signal-util.h"
16 #include "string-util.h"
18 /* We use 127.0.0.2 as IPv4 address. This has the advantage over
19 * 127.0.0.1 that it can be translated back to the local hostname. For
20 * IPv6 we use ::1 which unfortunately will not translate back to the
21 * hostname but instead something like "localhost" or so. */
23 #define LOCALADDRESS_IPV4 (htobe32(0x7F000002))
24 #define LOCALADDRESS_IPV6 &in6addr_loopback
26 NSS_GETHOSTBYNAME_PROTOTYPES(myhostname
);
27 NSS_GETHOSTBYADDR_PROTOTYPES(myhostname
);
29 enum nss_status
_nss_myhostname_gethostbyname4_r(
31 struct gaih_addrtuple
**pat
,
32 char *buffer
, size_t buflen
,
33 int *errnop
, int *h_errnop
,
36 struct gaih_addrtuple
*r_tuple
, *r_tuple_prev
= NULL
;
37 _cleanup_free_
struct local_address
*addresses
= NULL
;
38 _cleanup_free_
char *hn
= NULL
;
39 const char *canonical
= NULL
;
41 uint32_t local_address_ipv4
;
42 struct local_address
*a
;
48 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK
);
56 if (is_localhost(name
)) {
57 /* We respond to 'localhost', so that /etc/hosts
60 canonical
= "localhost";
61 local_address_ipv4
= htobe32(INADDR_LOOPBACK
);
63 } else if (is_gateway_hostname(name
)) {
65 n_addresses
= local_gateways(NULL
, 0, AF_UNSPEC
, &addresses
);
69 canonical
= "_gateway";
72 hn
= gethostname_malloc();
76 *h_errnop
= NO_RECOVERY
;
77 return NSS_STATUS_TRYAGAIN
;
80 /* We respond to our local hostname, our hostname suffixed with a single dot. */
81 if (!streq(name
, hn
) && !streq_ptr(startswith(name
, hn
), "."))
84 n_addresses
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
89 local_address_ipv4
= LOCALADDRESS_IPV4
;
92 l
= strlen(canonical
);
93 ms
= ALIGN(l
+1) + ALIGN(sizeof(struct gaih_addrtuple
)) * (n_addresses
> 0 ? n_addresses
: 2);
97 *h_errnop
= NETDB_INTERNAL
;
98 return NSS_STATUS_TRYAGAIN
;
101 /* First, fill in hostname */
103 memcpy(r_name
, canonical
, l
+1);
106 assert(n_addresses
>= 0);
107 if (n_addresses
== 0) {
108 /* Second, fill in IPv6 tuple */
109 r_tuple
= (struct gaih_addrtuple
*) (buffer
+ idx
);
110 r_tuple
->next
= r_tuple_prev
;
111 r_tuple
->name
= r_name
;
112 r_tuple
->family
= AF_INET6
;
113 memcpy(r_tuple
->addr
, LOCALADDRESS_IPV6
, 16);
114 r_tuple
->scopeid
= 0;
116 idx
+= ALIGN(sizeof(struct gaih_addrtuple
));
117 r_tuple_prev
= r_tuple
;
119 /* Third, fill in IPv4 tuple */
120 r_tuple
= (struct gaih_addrtuple
*) (buffer
+ idx
);
121 r_tuple
->next
= r_tuple_prev
;
122 r_tuple
->name
= r_name
;
123 r_tuple
->family
= AF_INET
;
124 *(uint32_t*) r_tuple
->addr
= local_address_ipv4
;
125 r_tuple
->scopeid
= 0;
127 idx
+= ALIGN(sizeof(struct gaih_addrtuple
));
128 r_tuple_prev
= r_tuple
;
131 /* Fourth, fill actual addresses in, but in backwards order */
132 for (a
= addresses
+ n_addresses
- 1, n
= 0; (int) n
< n_addresses
; n
++, a
--) {
133 r_tuple
= (struct gaih_addrtuple
*) (buffer
+ idx
);
134 r_tuple
->next
= r_tuple_prev
;
135 r_tuple
->name
= r_name
;
136 r_tuple
->family
= a
->family
;
137 r_tuple
->scopeid
= a
->family
== AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&a
->address
.in6
) ? a
->ifindex
: 0;
138 memcpy(r_tuple
->addr
, &a
->address
, 16);
140 idx
+= ALIGN(sizeof(struct gaih_addrtuple
));
141 r_tuple_prev
= r_tuple
;
144 /* Verify the size matches */
147 /* Nscd expects us to store the first record in **pat. */
149 **pat
= *r_tuple_prev
;
156 /* Explicitly reset both *h_errnop and h_errno to work around
157 * https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */
158 *h_errnop
= NETDB_SUCCESS
;
161 return NSS_STATUS_SUCCESS
;
164 *h_errnop
= HOST_NOT_FOUND
;
165 return NSS_STATUS_NOTFOUND
;
168 static enum nss_status
fill_in_hostent(
169 const char *canonical
, const char *additional
,
171 struct local_address
*addresses
, unsigned n_addresses
,
172 uint32_t local_address_ipv4
,
173 struct hostent
*result
,
174 char *buffer
, size_t buflen
,
175 int *errnop
, int *h_errnop
,
179 size_t l_canonical
, l_additional
, idx
, ms
, alen
;
180 char *r_addr
, *r_name
, *r_aliases
, *r_alias
= NULL
, *r_addr_list
;
181 struct local_address
*a
;
192 alen
= FAMILY_ADDRESS_SIZE(af
);
194 for (a
= addresses
, n
= 0, c
= 0; n
< n_addresses
; a
++, n
++)
198 l_canonical
= strlen(canonical
);
199 l_additional
= strlen_ptr(additional
);
200 ms
= ALIGN(l_canonical
+1)+
201 (additional
? ALIGN(l_additional
+1) : 0) +
203 (additional
? sizeof(char*) : 0) +
204 (c
> 0 ? c
: 1) * ALIGN(alen
) +
205 (c
> 0 ? c
+1 : 2) * sizeof(char*);
210 *h_errnop
= NETDB_INTERNAL
;
211 return NSS_STATUS_TRYAGAIN
;
214 /* First, fill in hostnames */
216 memcpy(r_name
, canonical
, l_canonical
+1);
217 idx
= ALIGN(l_canonical
+1);
220 r_alias
= buffer
+ idx
;
221 memcpy(r_alias
, additional
, l_additional
+1);
222 idx
+= ALIGN(l_additional
+1);
225 /* Second, create aliases array */
226 r_aliases
= buffer
+ idx
;
228 ((char**) r_aliases
)[0] = r_alias
;
229 ((char**) r_aliases
)[1] = NULL
;
230 idx
+= 2*sizeof(char*);
232 ((char**) r_aliases
)[0] = NULL
;
233 idx
+= sizeof(char*);
236 /* Third, add addresses */
237 r_addr
= buffer
+ idx
;
241 for (a
= addresses
, n
= 0; n
< n_addresses
; a
++, n
++) {
245 memcpy(r_addr
+ i
*ALIGN(alen
), &a
->address
, alen
);
250 idx
+= c
*ALIGN(alen
);
253 *(uint32_t*) r_addr
= local_address_ipv4
;
255 memcpy(r_addr
, LOCALADDRESS_IPV6
, 16);
260 /* Fourth, add address pointer array */
261 r_addr_list
= buffer
+ idx
;
265 for (i
= 0; i
< c
; i
++)
266 ((char**) r_addr_list
)[i
] = r_addr
+ i
*ALIGN(alen
);
268 ((char**) r_addr_list
)[i
] = NULL
;
269 idx
+= (c
+1) * sizeof(char*);
272 ((char**) r_addr_list
)[0] = r_addr
;
273 ((char**) r_addr_list
)[1] = NULL
;
274 idx
+= 2 * sizeof(char*);
277 /* Verify the size matches */
280 result
->h_name
= r_name
;
281 result
->h_aliases
= (char**) r_aliases
;
282 result
->h_addrtype
= af
;
283 result
->h_length
= alen
;
284 result
->h_addr_list
= (char**) r_addr_list
;
292 /* Explicitly reset both *h_errnop and h_errno to work around
293 * https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */
294 *h_errnop
= NETDB_SUCCESS
;
297 return NSS_STATUS_SUCCESS
;
300 enum nss_status
_nss_myhostname_gethostbyname3_r(
303 struct hostent
*host
,
304 char *buffer
, size_t buflen
,
305 int *errnop
, int *h_errnop
,
309 _cleanup_free_
struct local_address
*addresses
= NULL
;
310 const char *canonical
, *additional
= NULL
;
311 _cleanup_free_
char *hn
= NULL
;
312 uint32_t local_address_ipv4
= 0;
316 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK
);
327 if (!IN_SET(af
, AF_INET
, AF_INET6
)) {
329 *errnop
= EAFNOSUPPORT
;
331 return NSS_STATUS_UNAVAIL
;
334 if (is_localhost(name
)) {
335 canonical
= "localhost";
336 local_address_ipv4
= htobe32(INADDR_LOOPBACK
);
338 } else if (is_gateway_hostname(name
)) {
340 n_addresses
= local_gateways(NULL
, 0, af
, &addresses
);
341 if (n_addresses
<= 0)
344 canonical
= "_gateway";
347 hn
= gethostname_malloc();
351 *h_errnop
= NO_RECOVERY
;
352 return NSS_STATUS_TRYAGAIN
;
355 if (!streq(name
, hn
) && !streq_ptr(startswith(name
, hn
), "."))
358 n_addresses
= local_addresses(NULL
, 0, af
, &addresses
);
363 additional
= n_addresses
<= 0 && af
== AF_INET6
? "localhost" : NULL
;
364 local_address_ipv4
= LOCALADDRESS_IPV4
;
369 return fill_in_hostent(
370 canonical
, additional
,
372 addresses
, n_addresses
,
381 *h_errnop
= HOST_NOT_FOUND
;
382 return NSS_STATUS_NOTFOUND
;
385 enum nss_status
_nss_myhostname_gethostbyaddr2_r(
386 const void* addr
, socklen_t len
,
388 struct hostent
*host
,
389 char *buffer
, size_t buflen
,
390 int *errnop
, int *h_errnop
,
393 const char *canonical
= NULL
, *additional
= NULL
;
394 uint32_t local_address_ipv4
= LOCALADDRESS_IPV4
;
395 _cleanup_free_
struct local_address
*addresses
= NULL
;
396 _cleanup_free_
char *hn
= NULL
;
398 struct local_address
*a
;
399 bool additional_from_hostname
= false;
403 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK
);
411 if (!IN_SET(af
, AF_INET
, AF_INET6
)) {
413 *errnop
= EAFNOSUPPORT
;
415 return NSS_STATUS_UNAVAIL
;
418 if (len
!= FAMILY_ADDRESS_SIZE(af
)) {
421 *h_errnop
= NO_RECOVERY
;
422 return NSS_STATUS_UNAVAIL
;
426 if ((*(uint32_t*) addr
) == LOCALADDRESS_IPV4
)
429 if ((*(uint32_t*) addr
) == htobe32(INADDR_LOOPBACK
)) {
430 canonical
= "localhost";
431 local_address_ipv4
= htobe32(INADDR_LOOPBACK
);
436 assert(af
== AF_INET6
);
438 if (memcmp(addr
, LOCALADDRESS_IPV6
, 16) == 0) {
439 canonical
= "localhost";
440 additional_from_hostname
= true;
445 n_addresses
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
446 for (a
= addresses
, n
= 0; (int) n
< n_addresses
; n
++, a
++) {
450 if (memcmp(addr
, &a
->address
, FAMILY_ADDRESS_SIZE(af
)) == 0)
454 addresses
= mfree(addresses
);
456 n_addresses
= local_gateways(NULL
, 0, AF_UNSPEC
, &addresses
);
457 for (a
= addresses
, n
= 0; (int) n
< n_addresses
; n
++, a
++) {
461 if (memcmp(addr
, &a
->address
, FAMILY_ADDRESS_SIZE(af
)) == 0) {
462 canonical
= "_gateway";
467 *h_errnop
= HOST_NOT_FOUND
;
468 return NSS_STATUS_NOTFOUND
;
471 if (!canonical
|| additional_from_hostname
) {
472 hn
= gethostname_malloc();
476 *h_errnop
= NO_RECOVERY
;
477 return NSS_STATUS_TRYAGAIN
;
487 return fill_in_hostent(
488 canonical
, additional
,
490 addresses
, n_addresses
,
499 NSS_GETHOSTBYNAME_FALLBACKS(myhostname
);
500 NSS_GETHOSTBYADDR_FALLBACKS(myhostname
);