]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/nss-myhostname/nss-myhostname.c
2 This file is part of systemd.
4 Copyright 2008-2011 Lennart Poettering
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.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
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/>.
27 #include "alloc-util.h"
28 #include "hostname-util.h"
29 #include "local-addresses.h"
32 #include "signal-util.h"
33 #include "string-util.h"
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
39 * hostname but instead something like "localhost" or so. */
41 #define LOCALADDRESS_IPV4 (htobe32(0x7F000002))
42 #define LOCALADDRESS_IPV6 &in6addr_loopback
44 NSS_GETHOSTBYNAME_PROTOTYPES(myhostname
);
45 NSS_GETHOSTBYADDR_PROTOTYPES(myhostname
);
47 enum nss_status
_nss_myhostname_gethostbyname4_r(
49 struct gaih_addrtuple
**pat
,
50 char *buffer
, size_t buflen
,
51 int *errnop
, int *h_errnop
,
54 struct gaih_addrtuple
*r_tuple
, *r_tuple_prev
= NULL
;
55 _cleanup_free_
struct local_address
*addresses
= NULL
;
56 _cleanup_free_
char *hn
= NULL
;
57 const char *canonical
= NULL
;
59 uint32_t local_address_ipv4
;
60 struct local_address
*a
;
65 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK
);
73 if (is_localhost(name
)) {
74 /* We respond to 'localhost', so that /etc/hosts
77 canonical
= "localhost";
78 local_address_ipv4
= htobe32(INADDR_LOOPBACK
);
80 } else if (is_gateway_hostname(name
)) {
82 n_addresses
= local_gateways(NULL
, 0, AF_UNSPEC
, &addresses
);
83 if (n_addresses
<= 0) {
85 *h_errnop
= HOST_NOT_FOUND
;
86 return NSS_STATUS_NOTFOUND
;
89 canonical
= "_gateway";
92 hn
= gethostname_malloc();
95 *h_errnop
= NO_RECOVERY
;
96 return NSS_STATUS_TRYAGAIN
;
99 /* We respond to our local host name, our hostname suffixed with a single dot. */
100 if (!streq(name
, hn
) && !streq_ptr(startswith(name
, hn
), ".")) {
102 *h_errnop
= HOST_NOT_FOUND
;
103 return NSS_STATUS_NOTFOUND
;
106 n_addresses
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
111 local_address_ipv4
= LOCALADDRESS_IPV4
;
114 l
= strlen(canonical
);
115 ms
= ALIGN(l
+1) + ALIGN(sizeof(struct gaih_addrtuple
)) * (n_addresses
> 0 ? n_addresses
: 2);
118 *h_errnop
= NETDB_INTERNAL
;
119 return NSS_STATUS_TRYAGAIN
;
122 /* First, fill in hostname */
124 memcpy(r_name
, canonical
, l
+1);
127 assert(n_addresses
>= 0);
128 if (n_addresses
== 0) {
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);
135 r_tuple
->scopeid
= 0;
137 idx
+= ALIGN(sizeof(struct gaih_addrtuple
));
138 r_tuple_prev
= r_tuple
;
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
;
145 *(uint32_t*) r_tuple
->addr
= local_address_ipv4
;
146 r_tuple
->scopeid
= 0;
148 idx
+= ALIGN(sizeof(struct gaih_addrtuple
));
149 r_tuple_prev
= r_tuple
;
152 /* Fourth, fill actual addresses in, but in backwards order */
153 for (a
= addresses
+ n_addresses
- 1, n
= 0; (int) n
< n_addresses
; n
++, a
--) {
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
;
158 r_tuple
->scopeid
= a
->family
== AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&a
->address
.in6
) ? a
->ifindex
: 0;
159 memcpy(r_tuple
->addr
, &a
->address
, 16);
161 idx
+= ALIGN(sizeof(struct gaih_addrtuple
));
162 r_tuple_prev
= r_tuple
;
165 /* Verify the size matches */
168 /* Nscd expects us to store the first record in **pat. */
170 **pat
= *r_tuple_prev
;
177 /* Explicitly reset all error variables */
179 *h_errnop
= NETDB_SUCCESS
;
182 return NSS_STATUS_SUCCESS
;
185 static enum nss_status
fill_in_hostent(
186 const char *canonical
, const char *additional
,
188 struct local_address
*addresses
, unsigned n_addresses
,
189 uint32_t local_address_ipv4
,
190 struct hostent
*result
,
191 char *buffer
, size_t buflen
,
192 int *errnop
, int *h_errnop
,
196 size_t l_canonical
, l_additional
, idx
, ms
, alen
;
197 char *r_addr
, *r_name
, *r_aliases
, *r_alias
= NULL
, *r_addr_list
;
198 struct local_address
*a
;
207 alen
= FAMILY_ADDRESS_SIZE(af
);
209 for (a
= addresses
, n
= 0, c
= 0; n
< n_addresses
; a
++, n
++)
213 l_canonical
= strlen(canonical
);
214 l_additional
= strlen_ptr(additional
);
215 ms
= ALIGN(l_canonical
+1)+
216 (additional
? ALIGN(l_additional
+1) : 0) +
218 (additional
? sizeof(char*) : 0) +
219 (c
> 0 ? c
: 1) * ALIGN(alen
) +
220 (c
> 0 ? c
+1 : 2) * sizeof(char*);
224 *h_errnop
= NETDB_INTERNAL
;
225 return NSS_STATUS_TRYAGAIN
;
228 /* First, fill in hostnames */
230 memcpy(r_name
, canonical
, l_canonical
+1);
231 idx
= ALIGN(l_canonical
+1);
234 r_alias
= buffer
+ idx
;
235 memcpy(r_alias
, additional
, l_additional
+1);
236 idx
+= ALIGN(l_additional
+1);
239 /* Second, create aliases array */
240 r_aliases
= buffer
+ idx
;
242 ((char**) r_aliases
)[0] = r_alias
;
243 ((char**) r_aliases
)[1] = NULL
;
244 idx
+= 2*sizeof(char*);
246 ((char**) r_aliases
)[0] = NULL
;
247 idx
+= sizeof(char*);
250 /* Third, add addresses */
251 r_addr
= buffer
+ idx
;
255 for (a
= addresses
, n
= 0; n
< n_addresses
; a
++, n
++) {
259 memcpy(r_addr
+ i
*ALIGN(alen
), &a
->address
, alen
);
264 idx
+= c
*ALIGN(alen
);
267 *(uint32_t*) r_addr
= local_address_ipv4
;
269 memcpy(r_addr
, LOCALADDRESS_IPV6
, 16);
274 /* Fourth, add address pointer array */
275 r_addr_list
= buffer
+ idx
;
279 for (i
= 0; i
< c
; i
++)
280 ((char**) r_addr_list
)[i
] = r_addr
+ i
*ALIGN(alen
);
282 ((char**) r_addr_list
)[i
] = NULL
;
283 idx
+= (c
+1) * sizeof(char*);
286 ((char**) r_addr_list
)[0] = r_addr
;
287 ((char**) r_addr_list
)[1] = NULL
;
288 idx
+= 2 * sizeof(char*);
291 /* Verify the size matches */
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
;
306 /* Explicitly reset all error variables */
308 *h_errnop
= NETDB_SUCCESS
;
311 return NSS_STATUS_SUCCESS
;
314 enum nss_status
_nss_myhostname_gethostbyname3_r(
317 struct hostent
*host
,
318 char *buffer
, size_t buflen
,
319 int *errnop
, int *h_errnop
,
323 _cleanup_free_
struct local_address
*addresses
= NULL
;
324 const char *canonical
, *additional
= NULL
;
325 _cleanup_free_
char *hn
= NULL
;
326 uint32_t local_address_ipv4
= 0;
329 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK
);
340 if (!IN_SET(af
, AF_INET
, AF_INET6
)) {
341 *errnop
= EAFNOSUPPORT
;
343 return NSS_STATUS_UNAVAIL
;
346 if (is_localhost(name
)) {
347 canonical
= "localhost";
348 local_address_ipv4
= htobe32(INADDR_LOOPBACK
);
350 } else if (is_gateway_hostname(name
)) {
352 n_addresses
= local_gateways(NULL
, 0, af
, &addresses
);
353 if (n_addresses
<= 0) {
355 *h_errnop
= HOST_NOT_FOUND
;
356 return NSS_STATUS_NOTFOUND
;
359 canonical
= "_gateway";
362 hn
= gethostname_malloc();
365 *h_errnop
= NO_RECOVERY
;
366 return NSS_STATUS_TRYAGAIN
;
369 if (!streq(name
, hn
) && !streq_ptr(startswith(name
, hn
), ".")) {
371 *h_errnop
= HOST_NOT_FOUND
;
372 return NSS_STATUS_NOTFOUND
;
375 n_addresses
= local_addresses(NULL
, 0, af
, &addresses
);
380 additional
= n_addresses
<= 0 && af
== AF_INET6
? "localhost" : NULL
;
381 local_address_ipv4
= LOCALADDRESS_IPV4
;
384 return fill_in_hostent(
385 canonical
, additional
,
387 addresses
, n_addresses
,
396 enum nss_status
_nss_myhostname_gethostbyaddr2_r(
397 const void* addr
, socklen_t len
,
399 struct hostent
*host
,
400 char *buffer
, size_t buflen
,
401 int *errnop
, int *h_errnop
,
404 const char *canonical
= NULL
, *additional
= NULL
;
405 uint32_t local_address_ipv4
= LOCALADDRESS_IPV4
;
406 _cleanup_free_
struct local_address
*addresses
= NULL
;
407 _cleanup_free_
char *hn
= NULL
;
409 struct local_address
*a
;
410 bool additional_from_hostname
= false;
413 BLOCK_SIGNALS(NSS_SIGNALS_BLOCK
);
421 if (!IN_SET(af
, AF_INET
, AF_INET6
)) {
422 *errnop
= EAFNOSUPPORT
;
424 return NSS_STATUS_UNAVAIL
;
427 if (len
!= FAMILY_ADDRESS_SIZE(af
)) {
429 *h_errnop
= NO_RECOVERY
;
430 return NSS_STATUS_UNAVAIL
;
434 if ((*(uint32_t*) addr
) == LOCALADDRESS_IPV4
)
437 if ((*(uint32_t*) addr
) == htobe32(INADDR_LOOPBACK
)) {
438 canonical
= "localhost";
439 local_address_ipv4
= htobe32(INADDR_LOOPBACK
);
444 assert(af
== AF_INET6
);
446 if (memcmp(addr
, LOCALADDRESS_IPV6
, 16) == 0) {
447 canonical
= "localhost";
448 additional_from_hostname
= true;
453 n_addresses
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
454 for (a
= addresses
, n
= 0; (int) n
< n_addresses
; n
++, a
++) {
458 if (memcmp(addr
, &a
->address
, FAMILY_ADDRESS_SIZE(af
)) == 0)
462 addresses
= mfree(addresses
);
464 n_addresses
= local_gateways(NULL
, 0, AF_UNSPEC
, &addresses
);
465 for (a
= addresses
, n
= 0; (int) n
< n_addresses
; n
++, a
++) {
469 if (memcmp(addr
, &a
->address
, FAMILY_ADDRESS_SIZE(af
)) == 0) {
470 canonical
= "_gateway";
476 *h_errnop
= HOST_NOT_FOUND
;
477 return NSS_STATUS_NOTFOUND
;
480 if (!canonical
|| additional_from_hostname
) {
481 hn
= gethostname_malloc();
484 *h_errnop
= NO_RECOVERY
;
485 return NSS_STATUS_TRYAGAIN
;
494 return fill_in_hostent(
495 canonical
, additional
,
497 addresses
, n_addresses
,
506 NSS_GETHOSTBYNAME_FALLBACKS(myhostname
);
507 NSS_GETHOSTBYADDR_FALLBACKS(myhostname
);