1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "string-util.h"
11 #include "alloc-util.h"
12 #include "in-addr-util.h"
13 #include "hexdecoct.h"
15 #include "stdio-util.h"
17 #include "errno-list.h"
18 #include "hostname-util.h"
19 #include "local-addresses.h"
21 static const char* nss_status_to_string(enum nss_status status
, char *buf
, size_t buf_len
) {
23 case NSS_STATUS_TRYAGAIN
:
24 return "NSS_STATUS_TRYAGAIN";
25 case NSS_STATUS_UNAVAIL
:
26 return "NSS_STATUS_UNAVAIL";
27 case NSS_STATUS_NOTFOUND
:
28 return "NSS_STATUS_NOTFOUND";
29 case NSS_STATUS_SUCCESS
:
30 return "NSS_STATUS_SUCCESS";
31 case NSS_STATUS_RETURN
:
32 return "NSS_STATUS_RETURN";
34 snprintf(buf
, buf_len
, "%i", status
);
39 static const char* af_to_string(int family
, char *buf
, size_t buf_len
) {
42 if (family
== AF_UNSPEC
)
45 name
= af_to_name(family
);
49 snprintf(buf
, buf_len
, "%i", family
);
53 static void* open_handle(const char* dir
, const char* module
, int flags
) {
58 path
= strjoina(dir
, "/libnss_", module
, ".so.2");
59 if (access(path
, F_OK
) < 0)
60 path
= strjoina(dir
, "/.libs/libnss_", module
, ".so.2");
62 path
= strjoina("libnss_", module
, ".so.2");
64 handle
= dlopen(path
, flags
);
66 log_error("Failed to load module %s: %s", module
, dlerror());
70 static int print_gaih_addrtuples(const struct gaih_addrtuple
*tuples
) {
71 const struct gaih_addrtuple
*it
;
74 for (it
= tuples
; it
; it
= it
->next
) {
75 _cleanup_free_
char *a
= NULL
;
76 union in_addr_union u
;
78 char family_name
[DECIMAL_STR_MAX(int)];
79 char ifname
[IF_NAMESIZE
];
81 memcpy(&u
, it
->addr
, 16);
82 r
= in_addr_to_string(it
->family
, &u
, &a
);
83 assert_se(IN_SET(r
, 0, -EAFNOSUPPORT
));
84 if (r
== -EAFNOSUPPORT
)
85 assert_se(a
= hexmem(it
->addr
, 16));
90 if (if_indextoname(it
->scopeid
, ifname
) == NULL
) {
91 log_warning_errno(errno
, "if_indextoname(%d) failed: %m", it
->scopeid
);
93 xsprintf(ifname
, "%i", it
->scopeid
);
96 log_info(" \"%s\" %s %s %%%s",
98 af_to_string(it
->family
, family_name
, sizeof family_name
),
106 static void print_struct_hostent(struct hostent
*host
, const char *canon
) {
109 log_info(" \"%s\"", host
->h_name
);
110 STRV_FOREACH(s
, host
->h_aliases
)
111 log_info(" alias \"%s\"", *s
);
112 STRV_FOREACH(s
, host
->h_addr_list
) {
113 union in_addr_union u
;
114 _cleanup_free_
char *a
= NULL
;
115 char family_name
[DECIMAL_STR_MAX(int)];
118 assert_se((unsigned) host
->h_length
== FAMILY_ADDRESS_SIZE(host
->h_addrtype
));
119 memcpy(&u
, *s
, host
->h_length
);
120 r
= in_addr_to_string(host
->h_addrtype
, &u
, &a
);
123 af_to_string(host
->h_addrtype
, family_name
, sizeof family_name
),
127 log_info(" canonical: \"%s\"", canon
);
130 static void test_gethostbyname4_r(void *handle
, const char *module
, const char *name
) {
132 _nss_gethostbyname4_r_t f
;
134 struct gaih_addrtuple
*pat
= NULL
;
135 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
136 int32_t ttl
= INT32_MAX
; /* nss-dns wants to return the lowest ttl,
137 and will access this variable through *ttlp,
138 so we need to set it to something.
139 I'm not sure if this is a bug in nss-dns
141 enum nss_status status
;
142 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
145 fname
= strjoina("_nss_", module
, "_gethostbyname4_r");
146 f
= dlsym(handle
, fname
);
147 log_debug("dlsym(0x%p, %s) → 0x%p", handle
, fname
, f
);
150 status
= f(name
, &pat
, buffer
, sizeof buffer
, &errno1
, &errno2
, &ttl
);
151 if (status
== NSS_STATUS_SUCCESS
) {
152 log_info("%s(\"%s\") → status=%s%-20spat=buffer+0x%tx errno=%d/%s h_errno=%d/%s ttl=%"PRIi32
,
154 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
155 pat
? (char*) pat
- buffer
: 0,
156 errno1
, errno_to_name(errno1
) ?: "---",
157 errno2
, hstrerror(errno2
),
159 n
= print_gaih_addrtuples(pat
);
161 log_info("%s(\"%s\") → status=%s%-20spat=0x%p errno=%d/%s h_errno=%d/%s",
163 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
165 errno1
, errno_to_name(errno1
) ?: "---",
166 errno2
, hstrerror(errno2
));
170 if (STR_IN_SET(module
, "resolve", "mymachines") && status
== NSS_STATUS_UNAVAIL
)
173 if (STR_IN_SET(module
, "myhostname", "resolve") && streq(name
, "localhost")) {
174 assert_se(status
== NSS_STATUS_SUCCESS
);
179 static void test_gethostbyname3_r(void *handle
, const char *module
, const char *name
, int af
) {
181 _nss_gethostbyname3_r_t f
;
183 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
184 int32_t ttl
= INT32_MAX
; /* nss-dns wants to return the lowest ttl,
185 and will access this variable through *ttlp,
186 so we need to set it to something.
187 I'm not sure if this is a bug in nss-dns
189 enum nss_status status
;
190 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
193 char family_name
[DECIMAL_STR_MAX(int)];
195 fname
= strjoina("_nss_", module
, "_gethostbyname3_r");
196 f
= dlsym(handle
, fname
);
197 log_debug("dlsym(0x%p, %s) → 0x%p", handle
, fname
, f
);
200 status
= f(name
, af
, &host
, buffer
, sizeof buffer
, &errno1
, &errno2
, &ttl
, &canon
);
201 log_info("%s(\"%s\", %s) → status=%s%-20serrno=%d/%s h_errno=%d/%s ttl=%"PRIi32
,
202 fname
, name
, af_to_string(af
, family_name
, sizeof family_name
),
203 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
204 errno1
, errno_to_name(errno1
) ?: "---",
205 errno2
, hstrerror(errno2
),
207 if (status
== NSS_STATUS_SUCCESS
)
208 print_struct_hostent(&host
, canon
);
211 static void test_gethostbyname2_r(void *handle
, const char *module
, const char *name
, int af
) {
213 _nss_gethostbyname2_r_t f
;
215 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
216 enum nss_status status
;
217 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
219 char family_name
[DECIMAL_STR_MAX(int)];
221 fname
= strjoina("_nss_", module
, "_gethostbyname2_r");
222 f
= dlsym(handle
, fname
);
223 log_debug("dlsym(0x%p, %s) → 0x%p", handle
, fname
, f
);
226 status
= f(name
, af
, &host
, buffer
, sizeof buffer
, &errno1
, &errno2
);
227 log_info("%s(\"%s\", %s) → status=%s%-20serrno=%d/%s h_errno=%d/%s",
228 fname
, name
, af_to_string(af
, family_name
, sizeof family_name
),
229 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
230 errno1
, errno_to_name(errno1
) ?: "---",
231 errno2
, hstrerror(errno2
));
232 if (status
== NSS_STATUS_SUCCESS
)
233 print_struct_hostent(&host
, NULL
);
236 static void test_gethostbyname_r(void *handle
, const char *module
, const char *name
) {
238 _nss_gethostbyname_r_t f
;
240 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
241 enum nss_status status
;
242 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
245 fname
= strjoina("_nss_", module
, "_gethostbyname_r");
246 f
= dlsym(handle
, fname
);
247 log_debug("dlsym(0x%p, %s) → 0x%p", handle
, fname
, f
);
250 status
= f(name
, &host
, buffer
, sizeof buffer
, &errno1
, &errno2
);
251 log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s",
253 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
254 errno1
, errno_to_name(errno1
) ?: "---",
255 errno2
, hstrerror(errno2
));
256 if (status
== NSS_STATUS_SUCCESS
)
257 print_struct_hostent(&host
, NULL
);
260 static void test_gethostbyaddr2_r(void *handle
,
262 const void* addr
, socklen_t len
,
266 _nss_gethostbyaddr2_r_t f
;
268 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
269 enum nss_status status
;
270 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
272 int32_t ttl
= INT32_MAX
;
273 _cleanup_free_
char *addr_pretty
= NULL
;
275 fname
= strjoina("_nss_", module
, "_gethostbyaddr2_r");
276 f
= dlsym(handle
, fname
);
278 log_full_errno(f
? LOG_DEBUG
: LOG_INFO
, errno
,
279 "dlsym(0x%p, %s) → 0x%p: %m", handle
, fname
, f
);
283 assert_se(in_addr_to_string(af
, addr
, &addr_pretty
) >= 0);
285 status
= f(addr
, len
, af
, &host
, buffer
, sizeof buffer
, &errno1
, &errno2
, &ttl
);
286 log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s ttl=%"PRIi32
,
288 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
289 errno1
, errno_to_name(errno1
) ?: "---",
290 errno2
, hstrerror(errno2
),
292 if (status
== NSS_STATUS_SUCCESS
)
293 print_struct_hostent(&host
, NULL
);
296 static void test_gethostbyaddr_r(void *handle
,
298 const void* addr
, socklen_t len
,
302 _nss_gethostbyaddr_r_t f
;
304 int errno1
= 999, errno2
= 999; /* nss-dns doesn't set those */
305 enum nss_status status
;
306 char pretty_status
[DECIMAL_STR_MAX(enum nss_status
)];
308 _cleanup_free_
char *addr_pretty
= NULL
;
310 fname
= strjoina("_nss_", module
, "_gethostbyaddr_r");
311 f
= dlsym(handle
, fname
);
313 log_full_errno(f
? LOG_DEBUG
: LOG_INFO
, errno
,
314 "dlsym(0x%p, %s) → 0x%p: %m", handle
, fname
, f
);
318 assert_se(in_addr_to_string(af
, addr
, &addr_pretty
) >= 0);
320 status
= f(addr
, len
, af
, &host
, buffer
, sizeof buffer
, &errno1
, &errno2
);
321 log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s",
323 nss_status_to_string(status
, pretty_status
, sizeof pretty_status
), "\n",
324 errno1
, errno_to_name(errno1
) ?: "---",
325 errno2
, hstrerror(errno2
));
326 if (status
== NSS_STATUS_SUCCESS
)
327 print_struct_hostent(&host
, NULL
);
330 static void test_byname(void *handle
, const char *module
, const char *name
) {
331 test_gethostbyname4_r(handle
, module
, name
);
334 test_gethostbyname3_r(handle
, module
, name
, AF_INET
);
336 test_gethostbyname3_r(handle
, module
, name
, AF_INET6
);
338 test_gethostbyname3_r(handle
, module
, name
, AF_UNSPEC
);
340 test_gethostbyname3_r(handle
, module
, name
, AF_LOCAL
);
343 test_gethostbyname2_r(handle
, module
, name
, AF_INET
);
345 test_gethostbyname2_r(handle
, module
, name
, AF_INET6
);
347 test_gethostbyname2_r(handle
, module
, name
, AF_UNSPEC
);
349 test_gethostbyname2_r(handle
, module
, name
, AF_LOCAL
);
352 test_gethostbyname_r(handle
, module
, name
);
356 static void test_byaddr(void *handle
,
358 const void* addr
, socklen_t len
,
360 test_gethostbyaddr2_r(handle
, module
, addr
, len
, af
);
363 test_gethostbyaddr_r(handle
, module
, addr
, len
, af
);
367 static int make_addresses(struct local_address
**addresses
) {
370 _cleanup_free_
struct local_address
*addrs
= NULL
;
372 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addrs
);
374 log_info_errno(n
, "Failed to query local addresses: %m");
376 n_alloc
= n
; /* we _can_ do that */
377 if (!GREEDY_REALLOC(addrs
, n_alloc
, n
+ 3))
380 addrs
[n
++] = (struct local_address
) { .family
= AF_INET
,
381 .address
.in
= { htobe32(0x7F000001) } };
382 addrs
[n
++] = (struct local_address
) { .family
= AF_INET
,
383 .address
.in
= { htobe32(0x7F000002) } };
384 addrs
[n
++] = (struct local_address
) { .family
= AF_INET6
,
385 .address
.in6
= in6addr_loopback
};
389 static int test_one_module(const char* dir
,
392 struct local_address
*addresses
,
398 log_info("======== %s ========", module
);
400 handle
= open_handle(streq(module
, "dns") ? NULL
: dir
,
402 RTLD_LAZY
|RTLD_NODELETE
);
406 STRV_FOREACH(name
, names
)
407 test_byname(handle
, module
, *name
);
409 for (i
= 0; i
< n_addresses
; i
++)
410 test_byaddr(handle
, module
,
411 &addresses
[i
].address
,
412 FAMILY_ADDRESS_SIZE(addresses
[i
].family
),
413 addresses
[i
].family
);
420 static int parse_argv(int argc
, char **argv
,
423 struct local_address
**the_addresses
, int *n_addresses
) {
426 _cleanup_strv_free_
char **modules
= NULL
, **names
= NULL
;
427 _cleanup_free_
struct local_address
*addrs
= NULL
;
428 size_t n_allocated
= 0;
431 modules
= strv_new(argv
[1], NULL
);
434 #if ENABLE_NSS_MYHOSTNAME
437 #if ENABLE_NSS_RESOLVE
440 #if ENABLE_NSS_MYMACHINES
451 union in_addr_union address
;
453 STRV_FOREACH(name
, argv
+ 2) {
454 r
= in_addr_from_string_auto(*name
, &family
, &address
);
456 /* assume this is a name */
457 r
= strv_extend(&names
, *name
);
461 if (!GREEDY_REALLOC0(addrs
, n_allocated
, n
+ 1))
464 addrs
[n
++] = (struct local_address
) { .family
= family
,
465 .address
= address
};
469 _cleanup_free_
char *hostname
;
471 hostname
= gethostname_malloc();
475 names
= strv_new("localhost", "_gateway", "foo_no_such_host", hostname
, NULL
);
479 n
= make_addresses(&addrs
);
484 *the_modules
= modules
;
486 modules
= names
= NULL
;
487 *the_addresses
= addrs
;
493 int main(int argc
, char **argv
) {
494 _cleanup_free_
char *dir
= NULL
;
495 _cleanup_strv_free_
char **modules
= NULL
, **names
= NULL
;
496 _cleanup_free_
struct local_address
*addresses
= NULL
;
501 log_set_max_level(LOG_INFO
);
502 log_parse_environment();
504 r
= parse_argv(argc
, argv
, &modules
, &names
, &addresses
, &n_addresses
);
506 log_error_errno(r
, "Failed to parse arguments: %m");
510 dir
= dirname_malloc(argv
[0]);
514 STRV_FOREACH(module
, modules
) {
515 r
= test_one_module(dir
, *module
, names
, addresses
, n_addresses
);