]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-nss-hosts.c
Merge pull request #23135 from DaanDeMeyer/journal-move
[thirdparty/systemd.git] / src / test / test-nss-hosts.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/if.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include "af-list.h"
8 #include "alloc-util.h"
9 #include "dlfcn-util.h"
10 #include "env-util.h"
11 #include "errno-list.h"
12 #include "format-util.h"
13 #include "hexdecoct.h"
14 #include "hostname-util.h"
15 #include "in-addr-util.h"
16 #include "local-addresses.h"
17 #include "log.h"
18 #include "main-func.h"
19 #include "nss-test-util.h"
20 #include "nss-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "socket-util.h"
24 #include "stdio-util.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "tests.h"
28
29 static size_t arg_bufsize = 1024;
30
31 static const char* af_to_string(int family, char *buf, size_t buf_len) {
32 const char *name;
33
34 if (family == AF_UNSPEC)
35 return "*";
36
37 name = af_to_name(family);
38 if (name)
39 return name;
40
41 (void) snprintf(buf, buf_len, "%i", family);
42 return buf;
43 }
44
45 static int print_gaih_addrtuples(const struct gaih_addrtuple *tuples) {
46 int r, n = 0;
47
48 for (const struct gaih_addrtuple *it = tuples; it; it = it->next) {
49 _cleanup_free_ char *a = NULL;
50 union in_addr_union u;
51 char family_name[DECIMAL_STR_MAX(int)];
52
53 memcpy(&u, it->addr, 16);
54 r = in_addr_to_string(it->family, &u, &a);
55 assert_se(IN_SET(r, 0, -EAFNOSUPPORT));
56 if (r == -EAFNOSUPPORT)
57 assert_se(a = hexmem(it->addr, 16));
58
59 log_info(" \"%s\" %s %s %s",
60 it->name,
61 af_to_string(it->family, family_name, sizeof family_name),
62 a,
63 FORMAT_IFNAME_FULL(it->scopeid, FORMAT_IFNAME_IFINDEX_WITH_PERCENT));
64
65 n++;
66 }
67 return n;
68 }
69
70 static void print_struct_hostent(struct hostent *host, const char *canon) {
71 log_info(" \"%s\"", host->h_name);
72 STRV_FOREACH(s, host->h_aliases)
73 log_info(" alias \"%s\"", *s);
74 STRV_FOREACH(s, host->h_addr_list) {
75 union in_addr_union u;
76 _cleanup_free_ char *a = NULL;
77 char family_name[DECIMAL_STR_MAX(int)];
78 int r;
79
80 assert_se((unsigned) host->h_length == FAMILY_ADDRESS_SIZE(host->h_addrtype));
81 memcpy(&u, *s, host->h_length);
82 r = in_addr_to_string(host->h_addrtype, &u, &a);
83 assert_se(r == 0);
84 log_info(" %s %s",
85 af_to_string(host->h_addrtype, family_name, sizeof family_name),
86 a);
87 }
88 if (canon)
89 log_info(" canonical: \"%s\"", canon);
90 }
91
92 static void test_gethostbyname4_r(void *handle, const char *module, const char *name) {
93 const char *fname;
94 _nss_gethostbyname4_r_t f;
95 char buffer[arg_bufsize];
96 struct gaih_addrtuple *pat = NULL;
97 int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */
98 int32_t ttl = INT32_MAX; /* nss-dns wants to return the lowest ttl,
99 and will access this variable through *ttlp,
100 so we need to set it to something.
101 I'm not sure if this is a bug in nss-dns
102 or not. */
103 enum nss_status status;
104 char pretty_status[DECIMAL_STR_MAX(enum nss_status)];
105 int n;
106
107 fname = strjoina("_nss_", module, "_gethostbyname4_r");
108 f = dlsym(handle, fname);
109 log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f);
110 if (!f) {
111 log_info("%s not defined", fname);
112 return;
113 }
114
115 status = f(name, &pat, buffer, sizeof buffer, &errno1, &errno2, &ttl);
116 if (status == NSS_STATUS_SUCCESS) {
117 log_info("%s(\"%s\") → status=%s%-20spat=buffer+0x%tx errno=%d/%s h_errno=%d/%s ttl=%"PRIi32,
118 fname, name,
119 nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n",
120 pat ? (char*) pat - buffer : 0,
121 errno1, errno_to_name(errno1) ?: "---",
122 errno2, hstrerror(errno2),
123 ttl);
124 n = print_gaih_addrtuples(pat);
125 } else {
126 log_info("%s(\"%s\") → status=%s%-20spat=0x%p errno=%d/%s h_errno=%d/%s",
127 fname, name,
128 nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n",
129 pat,
130 errno1, errno_to_name(errno1) ?: "---",
131 errno2, hstrerror(errno2));
132 n = 0;
133 }
134
135 if (STR_IN_SET(module, "resolve", "mymachines") && status == NSS_STATUS_UNAVAIL)
136 return;
137
138 if (streq(name, "localhost")) {
139 if (streq(module, "myhostname")) {
140 assert_se(status == NSS_STATUS_SUCCESS);
141 assert_se(n == socket_ipv6_is_enabled() + 1);
142
143 } else if (streq(module, "resolve") && getenv_bool_secure("SYSTEMD_NSS_RESOLVE_SYNTHESIZE") != 0) {
144 assert_se(status == NSS_STATUS_SUCCESS);
145 if (socket_ipv6_is_enabled())
146 assert_se(n == 2);
147 else
148 assert_se(n <= 2); /* Even if IPv6 is disabled, /etc/hosts may contain ::1. */
149 }
150 }
151 }
152
153 static void test_gethostbyname3_r(void *handle, const char *module, const char *name, int af) {
154 const char *fname;
155 _nss_gethostbyname3_r_t f;
156 char buffer[arg_bufsize];
157 int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */
158 int32_t ttl = INT32_MAX; /* nss-dns wants to return the lowest ttl,
159 and will access this variable through *ttlp,
160 so we need to set it to something.
161 I'm not sure if this is a bug in nss-dns
162 or not. */
163 enum nss_status status;
164 char pretty_status[DECIMAL_STR_MAX(enum nss_status)];
165 struct hostent host;
166 char *canon;
167 char family_name[DECIMAL_STR_MAX(int)];
168
169 fname = strjoina("_nss_", module, "_gethostbyname3_r");
170 f = dlsym(handle, fname);
171 log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f);
172 if (!f) {
173 log_info("%s not defined", fname);
174 return;
175 }
176
177 status = f(name, af, &host, buffer, sizeof buffer, &errno1, &errno2, &ttl, &canon);
178 log_info("%s(\"%s\", %s) → status=%s%-20serrno=%d/%s h_errno=%d/%s ttl=%"PRIi32,
179 fname, name, af_to_string(af, family_name, sizeof family_name),
180 nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n",
181 errno1, errno_to_name(errno1) ?: "---",
182 errno2, hstrerror(errno2),
183 ttl);
184 if (status == NSS_STATUS_SUCCESS)
185 print_struct_hostent(&host, canon);
186 }
187
188 static void test_gethostbyname2_r(void *handle, const char *module, const char *name, int af) {
189 const char *fname;
190 _nss_gethostbyname2_r_t f;
191 char buffer[arg_bufsize];
192 int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */
193 enum nss_status status;
194 char pretty_status[DECIMAL_STR_MAX(enum nss_status)];
195 struct hostent host;
196 char family_name[DECIMAL_STR_MAX(int)];
197
198 fname = strjoina("_nss_", module, "_gethostbyname2_r");
199 f = dlsym(handle, fname);
200 log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f);
201 if (!f) {
202 log_info("%s not defined", fname);
203 return;
204 }
205
206 status = f(name, af, &host, buffer, sizeof buffer, &errno1, &errno2);
207 log_info("%s(\"%s\", %s) → status=%s%-20serrno=%d/%s h_errno=%d/%s",
208 fname, name, af_to_string(af, family_name, sizeof family_name),
209 nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n",
210 errno1, errno_to_name(errno1) ?: "---",
211 errno2, hstrerror(errno2));
212 if (status == NSS_STATUS_SUCCESS)
213 print_struct_hostent(&host, NULL);
214 }
215
216 static void test_gethostbyname_r(void *handle, const char *module, const char *name) {
217 const char *fname;
218 _nss_gethostbyname_r_t f;
219 char buffer[arg_bufsize];
220 int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */
221 enum nss_status status;
222 char pretty_status[DECIMAL_STR_MAX(enum nss_status)];
223 struct hostent host;
224
225 fname = strjoina("_nss_", module, "_gethostbyname_r");
226 f = dlsym(handle, fname);
227 log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f);
228 if (!f) {
229 log_info("%s not defined", fname);
230 return;
231 }
232
233 status = f(name, &host, buffer, sizeof buffer, &errno1, &errno2);
234 log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s",
235 fname, name,
236 nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n",
237 errno1, errno_to_name(errno1) ?: "---",
238 errno2, hstrerror(errno2));
239 if (status == NSS_STATUS_SUCCESS)
240 print_struct_hostent(&host, NULL);
241 }
242
243 static void test_gethostbyaddr2_r(void *handle,
244 const char *module,
245 const void* addr, socklen_t len,
246 int af) {
247
248 const char *fname;
249 _nss_gethostbyaddr2_r_t f;
250 char buffer[arg_bufsize];
251 int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */
252 enum nss_status status;
253 char pretty_status[DECIMAL_STR_MAX(enum nss_status)];
254 struct hostent host;
255 int32_t ttl = INT32_MAX;
256 _cleanup_free_ char *addr_pretty = NULL;
257
258 fname = strjoina("_nss_", module, "_gethostbyaddr2_r");
259 f = dlsym(handle, fname);
260
261 log_full_errno(f ? LOG_DEBUG : LOG_INFO, errno,
262 "dlsym(0x%p, %s) → 0x%p: %m", handle, fname, f);
263 if (!f) {
264 log_info("%s not defined", fname);
265 return;
266 }
267
268 assert_se(in_addr_to_string(af, addr, &addr_pretty) >= 0);
269
270 status = f(addr, len, af, &host, buffer, sizeof buffer, &errno1, &errno2, &ttl);
271 log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s ttl=%"PRIi32,
272 fname, addr_pretty,
273 nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n",
274 errno1, errno_to_name(errno1) ?: "---",
275 errno2, hstrerror(errno2),
276 ttl);
277 if (status == NSS_STATUS_SUCCESS)
278 print_struct_hostent(&host, NULL);
279 }
280
281 static void test_gethostbyaddr_r(void *handle,
282 const char *module,
283 const void* addr, socklen_t len,
284 int af) {
285
286 const char *fname;
287 _nss_gethostbyaddr_r_t f;
288 char buffer[arg_bufsize];
289 int errno1 = 999, errno2 = 999; /* nss-dns doesn't set those */
290 enum nss_status status;
291 char pretty_status[DECIMAL_STR_MAX(enum nss_status)];
292 struct hostent host;
293 _cleanup_free_ char *addr_pretty = NULL;
294
295 fname = strjoina("_nss_", module, "_gethostbyaddr_r");
296 f = dlsym(handle, fname);
297
298 log_full_errno(f ? LOG_DEBUG : LOG_INFO, errno,
299 "dlsym(0x%p, %s) → 0x%p: %m", handle, fname, f);
300 if (!f) {
301 log_info("%s not defined", fname);
302 return;
303 }
304
305 assert_se(in_addr_to_string(af, addr, &addr_pretty) >= 0);
306
307 status = f(addr, len, af, &host, buffer, sizeof buffer, &errno1, &errno2);
308 log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s h_errno=%d/%s",
309 fname, addr_pretty,
310 nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n",
311 errno1, errno_to_name(errno1) ?: "---",
312 errno2, hstrerror(errno2));
313 if (status == NSS_STATUS_SUCCESS)
314 print_struct_hostent(&host, NULL);
315 }
316
317 static void test_byname(void *handle, const char *module, const char *name) {
318 test_gethostbyname4_r(handle, module, name);
319 puts("");
320
321 test_gethostbyname3_r(handle, module, name, AF_INET);
322 puts("");
323 test_gethostbyname3_r(handle, module, name, AF_INET6);
324 puts("");
325 test_gethostbyname3_r(handle, module, name, AF_UNSPEC);
326 puts("");
327 test_gethostbyname3_r(handle, module, name, AF_LOCAL);
328 puts("");
329
330 test_gethostbyname2_r(handle, module, name, AF_INET);
331 puts("");
332 test_gethostbyname2_r(handle, module, name, AF_INET6);
333 puts("");
334 test_gethostbyname2_r(handle, module, name, AF_UNSPEC);
335 puts("");
336 test_gethostbyname2_r(handle, module, name, AF_LOCAL);
337 puts("");
338
339 test_gethostbyname_r(handle, module, name);
340 puts("");
341 }
342
343 static void test_byaddr(void *handle,
344 const char *module,
345 const void* addr, socklen_t len,
346 int af) {
347 test_gethostbyaddr2_r(handle, module, addr, len, af);
348 puts("");
349
350 test_gethostbyaddr_r(handle, module, addr, len, af);
351 puts("");
352 }
353
354 static int make_addresses(struct local_address **addresses) {
355 int n;
356 _cleanup_free_ struct local_address *addrs = NULL;
357
358 n = local_addresses(NULL, 0, AF_UNSPEC, &addrs);
359 if (n < 0)
360 log_info_errno(n, "Failed to query local addresses: %m");
361
362 assert_se(GREEDY_REALLOC(addrs, n + 3));
363
364 addrs[n++] = (struct local_address) { .family = AF_INET,
365 .address.in = { htobe32(0x7F000001) } };
366 addrs[n++] = (struct local_address) { .family = AF_INET,
367 .address.in = { htobe32(0x7F000002) } };
368 addrs[n++] = (struct local_address) { .family = AF_INET6,
369 .address.in6 = in6addr_loopback };
370 return 0;
371 }
372
373 static int test_one_module(const char *dir,
374 const char *module,
375 char **names,
376 struct local_address *addresses,
377 int n_addresses) {
378
379 log_info("======== %s ========", module);
380
381 _cleanup_(dlclosep) void *handle = nss_open_handle(dir, module, RTLD_LAZY|RTLD_NODELETE);
382 if (!handle)
383 return -EINVAL;
384
385 STRV_FOREACH(name, names)
386 test_byname(handle, module, *name);
387
388 for (int i = 0; i < n_addresses; i++)
389 test_byaddr(handle, module,
390 &addresses[i].address,
391 FAMILY_ADDRESS_SIZE(addresses[i].family),
392 addresses[i].family);
393
394 log_info(" ");
395 return 0;
396 }
397
398 static int parse_argv(int argc, char **argv,
399 char ***the_modules,
400 char ***the_names,
401 struct local_address **the_addresses, int *n_addresses) {
402
403 _cleanup_strv_free_ char **modules = NULL, **names = NULL;
404 _cleanup_free_ struct local_address *addrs = NULL;
405 const char *p;
406 int r, n = 0;
407
408 p = getenv("SYSTEMD_TEST_NSS_BUFSIZE");
409 if (p) {
410 r = safe_atozu(p, &arg_bufsize);
411 if (r < 0)
412 return log_error_errno(r, "Failed to parse $SYSTEMD_TEST_NSS_BUFSIZE");
413 }
414
415 if (argc > 1)
416 modules = strv_new(argv[1]);
417 else
418 modules = strv_new(
419 #if ENABLE_NSS_MYHOSTNAME
420 "myhostname",
421 #endif
422 #if ENABLE_NSS_RESOLVE
423 "resolve",
424 #endif
425 #if ENABLE_NSS_MYMACHINES
426 "mymachines",
427 #endif
428 NULL);
429 assert_se(modules);
430
431 if (argc > 2) {
432 int family;
433 union in_addr_union address;
434
435 STRV_FOREACH(name, argv + 2) {
436 r = in_addr_from_string_auto(*name, &family, &address);
437 if (r < 0) {
438 /* assume this is a name */
439 r = strv_extend(&names, *name);
440 if (r < 0)
441 return r;
442 } else {
443 assert_se(GREEDY_REALLOC0(addrs, n + 1));
444
445 addrs[n++] = (struct local_address) { .family = family,
446 .address = address };
447 }
448 }
449 } else {
450 _cleanup_free_ char *hostname;
451 assert_se(hostname = gethostname_malloc());
452 assert_se(names = strv_new("localhost", "_gateway", "_outbound", "foo_no_such_host", hostname));
453
454 n = make_addresses(&addrs);
455 assert_se(n >= 0);
456 }
457
458 *the_modules = TAKE_PTR(modules);
459 *the_names = TAKE_PTR(names);
460 *the_addresses = TAKE_PTR(addrs);
461 *n_addresses = n;
462 return 0;
463 }
464
465 static int run(int argc, char **argv) {
466 _cleanup_free_ char *dir = NULL;
467 _cleanup_strv_free_ char **modules = NULL, **names = NULL;
468 _cleanup_free_ struct local_address *addresses = NULL;
469 int n_addresses = 0;
470 int r;
471
472 test_setup_logging(LOG_INFO);
473
474 r = parse_argv(argc, argv, &modules, &names, &addresses, &n_addresses);
475 if (r < 0)
476 return log_error_errno(r, "Failed to parse arguments: %m");
477
478 assert_se(path_extract_directory(argv[0], &dir) >= 0);
479
480 STRV_FOREACH(module, modules) {
481 r = test_one_module(dir, *module, names, addresses, n_addresses);
482 if (r < 0)
483 return r;
484 }
485
486 return 0;
487 }
488
489 DEFINE_MAIN_FUNCTION(run);