2 This file is part of systemd.
4 Copyright 2016 Zbigniew Jędrzejewski-Szmek
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/>.
26 #include "path-util.h"
27 #include "string-util.h"
28 #include "alloc-util.h"
29 #include "in-addr-util.h"
30 #include "hexdecoct.h"
32 #include "stdio-util.h"
34 #include "errno-list.h"
35 #include "hostname-util.h"
36 #include "local-addresses.h"
38 static const char* nss_status_to_string(enum nss_status status
, char *buf
, size_t buf_len
) {
40 case NSS_STATUS_TRYAGAIN
:
41 return "NSS_STATUS_TRYAGAIN";
42 case NSS_STATUS_UNAVAIL
:
43 return "NSS_STATUS_UNAVAIL";
44 case NSS_STATUS_NOTFOUND
:
45 return "NSS_STATUS_NOTFOUND";
46 case NSS_STATUS_SUCCESS
:
47 return "NSS_STATUS_SUCCESS";
48 case NSS_STATUS_RETURN
:
49 return "NSS_STATUS_RETURN";
51 snprintf(buf
, buf_len
, "%i", status
);
56 static const char* af_to_string(int family
, char *buf
, size_t buf_len
) {
59 if (family
== AF_UNSPEC
)
62 name
= af_to_name(family
);
66 snprintf(buf
, buf_len
, "%i", family
);
70 static void* open_handle(const char* dir
, const char* module
, int flags
) {
75 path
= strjoina(dir
, "/libnss_", module
, ".so.2");
76 if (access(path
, F_OK
) < 0)
77 path
= strjoina(dir
, "/.libs/libnss_", module
, ".so.2");
79 path
= strjoina("libnss_", module
, ".so.2");
81 handle
= dlopen(path
, flags
);
83 log_error("Failed to load module %s: %s", module
, dlerror());
87 static int print_gaih_addrtuples(const struct gaih_addrtuple
*tuples
) {
88 const struct gaih_addrtuple
*it
;
91 for (it
= tuples
; it
; it
= it
->next
) {
92 _cleanup_free_
char *a
= NULL
;
93 union in_addr_union u
;
95 char family_name
[DECIMAL_STR_MAX(int)];
96 char ifname
[IF_NAMESIZE
];
98 memcpy(&u
, it
->addr
, 16);
99 r
= in_addr_to_string(it
->family
, &u
, &a
);
100 assert_se(IN_SET(r
, 0, -EAFNOSUPPORT
));
101 if (r
== -EAFNOSUPPORT
)
102 assert_se((a
= hexmem(it
->addr
, 16)));
104 if (it
->scopeid
== 0)
105 goto numerical_index
;
107 if (if_indextoname(it
->scopeid
, ifname
) == NULL
) {
108 log_warning_errno(errno
, "if_indextoname(%d) failed: %m", it
->scopeid
);
110 xsprintf(ifname
, "%i", it
->scopeid
);
113 log_info(" \"%s\" %s %s %%%s",
115 af_to_string(it
->family
, family_name
, sizeof family_name
),
123 static void print_struct_hostent(struct hostent
*host
, const char *canon
) {
126 log_info(" \"%s\"", host
->h_name
);
127 STRV_FOREACH(s
, host
->h_aliases
)
128 log_info(" alias \"%s\"", *s
);
129 STRV_FOREACH(s
, host
->h_addr_list
) {
130 union in_addr_union u
;
131 _cleanup_free_
char *a
= NULL
;
132 char family_name
[DECIMAL_STR_MAX(int)];
135 assert_se((unsigned) host
->h_length
== FAMILY_ADDRESS_SIZE(host
->h_addrtype
));
136 memcpy(&u
, *s
, host
->h_length
);
137 r
= in_addr_to_string(host
->h_addrtype
, &u
, &a
);
140 af_to_string(host
->h_addrtype
, family_name
, sizeof family_name
),
144 log_info(" canonical: \"%s\"", canon
);
147 static void test_gethostbyname4_r(void *handle
, const char *module
, const char *name
) {
149 _nss_gethostbyname4_r_t f
;
151 struct gaih_addrtuple
*pat
= NULL
;
152 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
153 int32_t ttl
= INT32_MAX
; /* nss-dns wants to return the lowest ttl,
154 and will access this variable through *ttlp,
155 so we need to set it to something.
156 I'm not sure if this is a bug in nss-dns
158 enum nss_status status
;
159 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
162 fname
= strjoina("_nss_", module
, "_gethostbyname4_r");
163 f
= dlsym(handle
, fname
);
164 log_debug("dlsym(0x%p, %s) → 0x%p", handle
, fname
, f
);
167 status
= f(name
, &pat
, buffer
, sizeof buffer
, &errno1
, &errno2
, &ttl
);
168 if (status
== NSS_STATUS_SUCCESS
) {
169 log_info("%s(\"%s\") → status=%s%-20spat=buffer+0x%tx errno=%d/%s h_errno=%d/%s ttl=%"PRIi32
,
171 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
172 pat
? (char*) pat
- buffer
: 0,
173 errno1
, errno_to_name(errno1
) ?: "---",
174 errno2
, hstrerror(errno2
),
176 n
= print_gaih_addrtuples(pat
);
178 log_info("%s(\"%s\") → status=%s%-20spat=0x%p errno=%d/%s h_errno=%d/%s",
180 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
182 errno1
, errno_to_name(errno1
) ?: "---",
183 errno2
, hstrerror(errno2
));
187 if (STR_IN_SET(module
, "resolve", "mymachines") && status
== NSS_STATUS_UNAVAIL
)
190 if (STR_IN_SET(module
, "myhostname", "resolve") && streq(name
, "localhost")) {
191 assert_se(status
== NSS_STATUS_SUCCESS
);
197 static void test_gethostbyname3_r(void *handle
, const char *module
, const char *name
, int af
) {
199 _nss_gethostbyname3_r_t f
;
201 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
202 int32_t ttl
= INT32_MAX
; /* nss-dns wants to return the lowest ttl,
203 and will access this variable through *ttlp,
204 so we need to set it to something.
205 I'm not sure if this is a bug in nss-dns
207 enum nss_status status
;
208 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
211 char family_name
[DECIMAL_STR_MAX(int)];
213 fname
= strjoina("_nss_", module
, "_gethostbyname3_r");
214 f
= dlsym(handle
, fname
);
215 log_debug("dlsym(0x%p, %s) → 0x%p", handle
, fname
, f
);
218 status
= f(name
, af
, &host
, buffer
, sizeof buffer
, &errno1
, &errno2
, &ttl
, &canon
);
219 log_info("%s(\"%s\", %s) → status=%s%-20serrno=%d/%s h_errno=%d/%s ttl=%"PRIi32
,
220 fname
, name
, af_to_string(af
, family_name
, sizeof family_name
),
221 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
222 errno1
, errno_to_name(errno1
) ?: "---",
223 errno2
, hstrerror(errno2
),
225 if (status
== NSS_STATUS_SUCCESS
)
226 print_struct_hostent(&host
, canon
);
229 static void test_gethostbyname2_r(void *handle
, const char *module
, const char *name
, int af
) {
231 _nss_gethostbyname2_r_t f
;
233 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
234 enum nss_status status
;
235 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
237 char family_name
[DECIMAL_STR_MAX(int)];
239 fname
= strjoina("_nss_", module
, "_gethostbyname2_r");
240 f
= dlsym(handle
, fname
);
241 log_debug("dlsym(0x%p, %s) → 0x%p", handle
, fname
, f
);
244 status
= f(name
, af
, &host
, buffer
, sizeof buffer
, &errno1
, &errno2
);
245 log_info("%s(\"%s\", %s) → status=%s%-20serrno=%d/%s h_errno=%d/%s",
246 fname
, name
, af_to_string(af
, family_name
, sizeof family_name
),
247 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
248 errno1
, errno_to_name(errno1
) ?: "---",
249 errno2
, hstrerror(errno2
));
250 if (status
== NSS_STATUS_SUCCESS
)
251 print_struct_hostent(&host
, NULL
);
254 static void test_gethostbyname_r(void *handle
, const char *module
, const char *name
) {
256 _nss_gethostbyname_r_t f
;
258 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
259 enum nss_status status
;
260 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
263 fname
= strjoina("_nss_", module
, "_gethostbyname_r");
264 f
= dlsym(handle
, fname
);
265 log_debug("dlsym(0x%p, %s) → 0x%p", handle
, fname
, f
);
268 status
= f(name
, &host
, buffer
, sizeof buffer
, &errno1
, &errno2
);
269 log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s",
271 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
272 errno1
, errno_to_name(errno1
) ?: "---",
273 errno2
, hstrerror(errno2
));
274 if (status
== NSS_STATUS_SUCCESS
)
275 print_struct_hostent(&host
, NULL
);
278 static void test_gethostbyaddr2_r(void *handle
,
280 const void* addr
, socklen_t len
,
284 _nss_gethostbyaddr2_r_t f
;
286 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
287 enum nss_status status
;
288 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
290 int32_t ttl
= INT32_MAX
;
291 _cleanup_free_
char *addr_pretty
= NULL
;
293 fname
= strjoina("_nss_", module
, "_gethostbyaddr2_r");
294 f
= dlsym(handle
, fname
);
296 log_full_errno(f
? LOG_DEBUG
: LOG_INFO
, errno
,
297 "dlsym(0x%p, %s) → 0x%p: %m", handle
, fname
, f
);
301 assert_se(in_addr_to_string(af
, addr
, &addr_pretty
) >= 0);
303 status
= f(addr
, len
, af
, &host
, buffer
, sizeof buffer
, &errno1
, &errno2
, &ttl
);
304 log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s ttl=%"PRIi32
,
306 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
307 errno1
, errno_to_name(errno1
) ?: "---",
308 errno2
, hstrerror(errno2
),
310 if (status
== NSS_STATUS_SUCCESS
)
311 print_struct_hostent(&host
, NULL
);
314 static void test_gethostbyaddr_r(void *handle
,
316 const void* addr
, socklen_t len
,
320 _nss_gethostbyaddr_r_t f
;
322 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
323 enum nss_status status
;
324 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
326 _cleanup_free_
char *addr_pretty
= NULL
;
328 fname
= strjoina("_nss_", module
, "_gethostbyaddr_r");
329 f
= dlsym(handle
, fname
);
331 log_full_errno(f
? LOG_DEBUG
: LOG_INFO
, errno
,
332 "dlsym(0x%p, %s) → 0x%p: %m", handle
, fname
, f
);
336 assert_se(in_addr_to_string(af
, addr
, &addr_pretty
) >= 0);
338 status
= f(addr
, len
, af
, &host
, buffer
, sizeof buffer
, &errno1
, &errno2
);
339 log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s",
341 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
342 errno1
, errno_to_name(errno1
) ?: "---",
343 errno2
, hstrerror(errno2
));
344 if (status
== NSS_STATUS_SUCCESS
)
345 print_struct_hostent(&host
, NULL
);
348 static void test_byname(void *handle
, const char *module
, const char *name
) {
349 test_gethostbyname4_r(handle
, module
, name
);
352 test_gethostbyname3_r(handle
, module
, name
, AF_INET
);
354 test_gethostbyname3_r(handle
, module
, name
, AF_INET6
);
356 test_gethostbyname3_r(handle
, module
, name
, AF_UNSPEC
);
358 test_gethostbyname3_r(handle
, module
, name
, AF_LOCAL
);
361 test_gethostbyname2_r(handle
, module
, name
, AF_INET
);
363 test_gethostbyname2_r(handle
, module
, name
, AF_INET6
);
365 test_gethostbyname2_r(handle
, module
, name
, AF_UNSPEC
);
367 test_gethostbyname2_r(handle
, module
, name
, AF_LOCAL
);
370 test_gethostbyname_r(handle
, module
, name
);
374 static void test_byaddr(void *handle
,
376 const void* addr
, socklen_t len
,
378 test_gethostbyaddr2_r(handle
, module
, addr
, len
, af
);
381 test_gethostbyaddr_r(handle
, module
, addr
, len
, af
);
385 static int make_addresses(struct local_address
**addresses
) {
388 _cleanup_free_
struct local_address
*addrs
= NULL
;
390 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addrs
);
392 log_info_errno(n
, "Failed to query local addresses: %m");
394 n_alloc
= n
; /* we _can_ do that */
395 if (!GREEDY_REALLOC(addrs
, n_alloc
, n
+ 3))
398 addrs
[n
++] = (struct local_address
) { .family
= AF_INET
,
399 .address
.in
= { htobe32(0x7F000001) } };
400 addrs
[n
++] = (struct local_address
) { .family
= AF_INET
,
401 .address
.in
= { htobe32(0x7F000002) } };
402 addrs
[n
++] = (struct local_address
) { .family
= AF_INET6
,
403 .address
.in6
= in6addr_loopback
};
407 static int test_one_module(const char* dir
,
410 struct local_address
*addresses
,
417 log_info("======== %s ========", module
);
419 handle
= open_handle(streq(module
, "dns") ? NULL
: dir
,
421 RTLD_LAZY
|RTLD_NODELETE
);
425 STRV_FOREACH(name
, names
)
426 test_byname(handle
, module
, *name
);
428 for (i
= 0; i
< n_addresses
; i
++)
429 test_byaddr(handle
, module
,
430 &addresses
[i
].address
,
431 FAMILY_ADDRESS_SIZE(addresses
[i
].family
),
432 addresses
[i
].family
);
439 static int parse_argv(int argc
, char **argv
,
442 struct local_address
**the_addresses
, int *n_addresses
) {
445 _cleanup_strv_free_
char **modules
= NULL
, **names
= NULL
;
446 _cleanup_free_
struct local_address
*addrs
= NULL
;
447 size_t n_allocated
= 0;
450 modules
= strv_new(argv
[1], NULL
);
453 #if ENABLE_MYHOSTNAME
470 union in_addr_union address
;
472 STRV_FOREACH(name
, argv
+ 2) {
473 r
= in_addr_from_string_auto(*name
, &family
, &address
);
475 /* assume this is a name */
476 r
= strv_extend(&names
, *name
);
480 if (!GREEDY_REALLOC0(addrs
, n_allocated
, n
+ 1))
483 addrs
[n
++] = (struct local_address
) { .family
= family
,
484 .address
= address
};
488 _cleanup_free_
char *hostname
;
490 hostname
= gethostname_malloc();
494 names
= strv_new("localhost", "_gateway", "foo_no_such_host", hostname
, NULL
);
498 n
= make_addresses(&addrs
);
503 *the_modules
= modules
;
505 modules
= names
= NULL
;
506 *the_addresses
= addrs
;
512 int main(int argc
, char **argv
) {
513 _cleanup_free_
char *dir
= NULL
;
514 _cleanup_strv_free_
char **modules
= NULL
, **names
= NULL
;
515 _cleanup_free_
struct local_address
*addresses
= NULL
;
520 log_set_max_level(LOG_INFO
);
521 log_parse_environment();
523 r
= parse_argv(argc
, argv
, &modules
, &names
, &addresses
, &n_addresses
);
525 log_error_errno(r
, "Failed to parse arguments: %m");
529 dir
= dirname_malloc(argv
[0]);
533 STRV_FOREACH(module
, modules
) {
534 r
= test_one_module(dir
, *module
, names
, addresses
, n_addresses
);