]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nss-myhostname/nss-myhostname.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
11 #include "errno-util.h"
12 #include "hostname-util.h"
13 #include "local-addresses.h"
16 #include "signal-util.h"
17 #include "string-util.h"
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. */
24 #define LOCALADDRESS_IPV4 (htobe32(0x7F000002))
25 #define LOCALADDRESS_IPV6 &in6addr_loopback
27 NSS_GETHOSTBYNAME_PROTOTYPES(myhostname
);
28 NSS_GETHOSTBYADDR_PROTOTYPES(myhostname
);
30 enum nss_status
_nss_myhostname_gethostbyname4_r(
32 struct gaih_addrtuple
**pat
,
33 char *buffer
, size_t buflen
,
34 int *errnop
, int *h_errnop
,
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
;
42 uint32_t local_address_ipv4
;
43 struct local_address
*a
;
49 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK
);
57 if (is_localhost(name
)) {
58 /* We respond to 'localhost', so that /etc/hosts
61 canonical
= "localhost";
62 local_address_ipv4
= htobe32(INADDR_LOOPBACK
);
64 } else if (is_gateway_hostname(name
)) {
66 n_addresses
= local_gateways(NULL
, 0, AF_UNSPEC
, &addresses
);
70 canonical
= "_gateway";
73 hn
= gethostname_malloc();
77 *h_errnop
= NO_RECOVERY
;
78 return NSS_STATUS_TRYAGAIN
;
81 /* We respond to our local host name, our hostname suffixed with a single dot. */
82 if (!streq(name
, hn
) && !streq_ptr(startswith(name
, hn
), "."))
85 n_addresses
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
90 local_address_ipv4
= LOCALADDRESS_IPV4
;
93 l
= strlen(canonical
);
94 ms
= ALIGN(l
+1) + ALIGN(sizeof(struct gaih_addrtuple
)) * (n_addresses
> 0 ? n_addresses
: 2);
98 *h_errnop
= NETDB_INTERNAL
;
99 return NSS_STATUS_TRYAGAIN
;
102 /* First, fill in hostname */
104 memcpy(r_name
, canonical
, l
+1);
107 assert(n_addresses
>= 0);
108 if (n_addresses
== 0) {
109 /* Second, fill in IPv6 tuple */
110 r_tuple
= (struct gaih_addrtuple
*) (buffer
+ idx
);
111 r_tuple
->next
= r_tuple_prev
;
112 r_tuple
->name
= r_name
;
113 r_tuple
->family
= AF_INET6
;
114 memcpy(r_tuple
->addr
, LOCALADDRESS_IPV6
, 16);
115 r_tuple
->scopeid
= 0;
117 idx
+= ALIGN(sizeof(struct gaih_addrtuple
));
118 r_tuple_prev
= r_tuple
;
120 /* Third, fill in IPv4 tuple */
121 r_tuple
= (struct gaih_addrtuple
*) (buffer
+ idx
);
122 r_tuple
->next
= r_tuple_prev
;
123 r_tuple
->name
= r_name
;
124 r_tuple
->family
= AF_INET
;
125 *(uint32_t*) r_tuple
->addr
= local_address_ipv4
;
126 r_tuple
->scopeid
= 0;
128 idx
+= ALIGN(sizeof(struct gaih_addrtuple
));
129 r_tuple_prev
= r_tuple
;
132 /* Fourth, fill actual addresses in, but in backwards order */
133 for (a
= addresses
+ n_addresses
- 1, n
= 0; (int) n
< n_addresses
; n
++, a
--) {
134 r_tuple
= (struct gaih_addrtuple
*) (buffer
+ idx
);
135 r_tuple
->next
= r_tuple_prev
;
136 r_tuple
->name
= r_name
;
137 r_tuple
->family
= a
->family
;
138 r_tuple
->scopeid
= a
->family
== AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&a
->address
.in6
) ? a
->ifindex
: 0;
139 memcpy(r_tuple
->addr
, &a
->address
, 16);
141 idx
+= ALIGN(sizeof(struct gaih_addrtuple
));
142 r_tuple_prev
= r_tuple
;
145 /* Verify the size matches */
148 /* Nscd expects us to store the first record in **pat. */
150 **pat
= *r_tuple_prev
;
157 /* Explicitly reset both *h_errnop and h_errno to work around
158 * https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */
159 *h_errnop
= NETDB_SUCCESS
;
162 return NSS_STATUS_SUCCESS
;
165 *h_errnop
= HOST_NOT_FOUND
;
166 return NSS_STATUS_NOTFOUND
;
169 static enum nss_status
fill_in_hostent(
170 const char *canonical
, const char *additional
,
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
,
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
;
193 alen
= FAMILY_ADDRESS_SIZE(af
);
195 for (a
= addresses
, n
= 0, c
= 0; n
< n_addresses
; a
++, n
++)
199 l_canonical
= strlen(canonical
);
200 l_additional
= strlen_ptr(additional
);
201 ms
= ALIGN(l_canonical
+1)+
202 (additional
? ALIGN(l_additional
+1) : 0) +
204 (additional
? sizeof(char*) : 0) +
205 (c
> 0 ? c
: 1) * ALIGN(alen
) +
206 (c
> 0 ? c
+1 : 2) * sizeof(char*);
211 *h_errnop
= NETDB_INTERNAL
;
212 return NSS_STATUS_TRYAGAIN
;
215 /* First, fill in hostnames */
217 memcpy(r_name
, canonical
, l_canonical
+1);
218 idx
= ALIGN(l_canonical
+1);
221 r_alias
= buffer
+ idx
;
222 memcpy(r_alias
, additional
, l_additional
+1);
223 idx
+= ALIGN(l_additional
+1);
226 /* Second, create aliases array */
227 r_aliases
= buffer
+ idx
;
229 ((char**) r_aliases
)[0] = r_alias
;
230 ((char**) r_aliases
)[1] = NULL
;
231 idx
+= 2*sizeof(char*);
233 ((char**) r_aliases
)[0] = NULL
;
234 idx
+= sizeof(char*);
237 /* Third, add addresses */
238 r_addr
= buffer
+ idx
;
242 for (a
= addresses
, n
= 0; n
< n_addresses
; a
++, n
++) {
246 memcpy(r_addr
+ i
*ALIGN(alen
), &a
->address
, alen
);
251 idx
+= c
*ALIGN(alen
);
254 *(uint32_t*) r_addr
= local_address_ipv4
;
256 memcpy(r_addr
, LOCALADDRESS_IPV6
, 16);
261 /* Fourth, add address pointer array */
262 r_addr_list
= buffer
+ idx
;
266 for (i
= 0; i
< c
; i
++)
267 ((char**) r_addr_list
)[i
] = r_addr
+ i
*ALIGN(alen
);
269 ((char**) r_addr_list
)[i
] = NULL
;
270 idx
+= (c
+1) * sizeof(char*);
273 ((char**) r_addr_list
)[0] = r_addr
;
274 ((char**) r_addr_list
)[1] = NULL
;
275 idx
+= 2 * sizeof(char*);
278 /* Verify the size matches */
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
;
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
;
298 return NSS_STATUS_SUCCESS
;
301 enum nss_status
_nss_myhostname_gethostbyname3_r(
304 struct hostent
*host
,
305 char *buffer
, size_t buflen
,
306 int *errnop
, int *h_errnop
,
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;
317 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK
);
328 if (!IN_SET(af
, AF_INET
, AF_INET6
)) {
330 *errnop
= EAFNOSUPPORT
;
332 return NSS_STATUS_UNAVAIL
;
335 if (is_localhost(name
)) {
336 canonical
= "localhost";
337 local_address_ipv4
= htobe32(INADDR_LOOPBACK
);
339 } else if (is_gateway_hostname(name
)) {
341 n_addresses
= local_gateways(NULL
, 0, af
, &addresses
);
342 if (n_addresses
<= 0)
345 canonical
= "_gateway";
348 hn
= gethostname_malloc();
352 *h_errnop
= NO_RECOVERY
;
353 return NSS_STATUS_TRYAGAIN
;
356 if (!streq(name
, hn
) && !streq_ptr(startswith(name
, hn
), "."))
359 n_addresses
= local_addresses(NULL
, 0, af
, &addresses
);
364 additional
= n_addresses
<= 0 && af
== AF_INET6
? "localhost" : NULL
;
365 local_address_ipv4
= LOCALADDRESS_IPV4
;
370 return fill_in_hostent(
371 canonical
, additional
,
373 addresses
, n_addresses
,
382 *h_errnop
= HOST_NOT_FOUND
;
383 return NSS_STATUS_NOTFOUND
;
386 enum nss_status
_nss_myhostname_gethostbyaddr2_r(
387 const void* addr
, socklen_t len
,
389 struct hostent
*host
,
390 char *buffer
, size_t buflen
,
391 int *errnop
, int *h_errnop
,
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
;
399 struct local_address
*a
;
400 bool additional_from_hostname
= false;
404 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK
);
412 if (!IN_SET(af
, AF_INET
, AF_INET6
)) {
414 *errnop
= EAFNOSUPPORT
;
416 return NSS_STATUS_UNAVAIL
;
419 if (len
!= FAMILY_ADDRESS_SIZE(af
)) {
422 *h_errnop
= NO_RECOVERY
;
423 return NSS_STATUS_UNAVAIL
;
427 if ((*(uint32_t*) addr
) == LOCALADDRESS_IPV4
)
430 if ((*(uint32_t*) addr
) == htobe32(INADDR_LOOPBACK
)) {
431 canonical
= "localhost";
432 local_address_ipv4
= htobe32(INADDR_LOOPBACK
);
437 assert(af
== AF_INET6
);
439 if (memcmp(addr
, LOCALADDRESS_IPV6
, 16) == 0) {
440 canonical
= "localhost";
441 additional_from_hostname
= true;
446 n_addresses
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
447 for (a
= addresses
, n
= 0; (int) n
< n_addresses
; n
++, a
++) {
451 if (memcmp(addr
, &a
->address
, FAMILY_ADDRESS_SIZE(af
)) == 0)
455 addresses
= mfree(addresses
);
457 n_addresses
= local_gateways(NULL
, 0, AF_UNSPEC
, &addresses
);
458 for (a
= addresses
, n
= 0; (int) n
< n_addresses
; n
++, a
++) {
462 if (memcmp(addr
, &a
->address
, FAMILY_ADDRESS_SIZE(af
)) == 0) {
463 canonical
= "_gateway";
468 *h_errnop
= HOST_NOT_FOUND
;
469 return NSS_STATUS_NOTFOUND
;
472 if (!canonical
|| additional_from_hostname
) {
473 hn
= gethostname_malloc();
477 *h_errnop
= NO_RECOVERY
;
478 return NSS_STATUS_TRYAGAIN
;
488 return fill_in_hostent(
489 canonical
, additional
,
491 addresses
, n_addresses
,
500 NSS_GETHOSTBYNAME_FALLBACKS(myhostname
);
501 NSS_GETHOSTBYADDR_FALLBACKS(myhostname
);