]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-nss-hosts.c
Merge pull request #31524 from poettering/secure-getenv-naming-fix
[thirdparty/systemd.git] / src / test / test-nss-hosts.c
index eddb04795133bc5682fbc653fa20a2e2c274941a..2f1810d93be9b5fbad93ef368d5e2f272bb7a62f 100644 (file)
@@ -7,6 +7,7 @@
 #include "af-list.h"
 #include "alloc-util.h"
 #include "dlfcn-util.h"
+#include "env-util.h"
 #include "errno-list.h"
 #include "format-util.h"
 #include "hexdecoct.h"
@@ -19,6 +20,7 @@
 #include "nss-util.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "socket-util.h"
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
@@ -36,19 +38,17 @@ static const char* af_to_string(int family, char *buf, size_t buf_len) {
         if (name)
                 return name;
 
-        snprintf(buf, buf_len, "%i", family);
+        (void) snprintf(buf, buf_len, "%i", family);
         return buf;
 }
 
 static int print_gaih_addrtuples(const struct gaih_addrtuple *tuples) {
-        int n = 0;
+        int r, n = 0;
 
         for (const struct gaih_addrtuple *it = tuples; it; it = it->next) {
                 _cleanup_free_ char *a = NULL;
                 union in_addr_union u;
-                int r;
                 char family_name[DECIMAL_STR_MAX(int)];
-                char ifname[IF_NAMESIZE + 1];
 
                 memcpy(&u, it->addr, 16);
                 r = in_addr_to_string(it->family, &u, &a);
@@ -56,28 +56,18 @@ static int print_gaih_addrtuples(const struct gaih_addrtuple *tuples) {
                 if (r == -EAFNOSUPPORT)
                         assert_se(a = hexmem(it->addr, 16));
 
-                if (it->scopeid == 0)
-                        goto numerical_index;
-
-                if (!format_ifname(it->scopeid, ifname)) {
-                        log_warning_errno(errno, "if_indextoname(%d) failed: %m", it->scopeid);
-                numerical_index:
-                        xsprintf(ifname, "%i", it->scopeid);
-                };
-
-                log_info("        \"%s\" %s %s %%%s",
+                log_info("        \"%s\" %s %s %s",
                          it->name,
                          af_to_string(it->family, family_name, sizeof family_name),
                          a,
-                         ifname);
-                n ++;
+                         FORMAT_IFNAME_FULL(it->scopeid, FORMAT_IFNAME_IFINDEX_WITH_PERCENT));
+
+                n++;
         }
         return n;
 }
 
 static void print_struct_hostent(struct hostent *host, const char *canon) {
-        char **s;
-
         log_info("        \"%s\"", host->h_name);
         STRV_FOREACH(s, host->h_aliases)
                 log_info("        alias \"%s\"", *s);
@@ -124,10 +114,10 @@ static void test_gethostbyname4_r(void *handle, const char *module, const char *
 
         status = f(name, &pat, buffer, sizeof buffer, &errno1, &errno2, &ttl);
         if (status == NSS_STATUS_SUCCESS) {
-                log_info("%s(\"%s\") → status=%s%-20spat=buffer+0x%tx errno=%d/%s h_errno=%d/%s ttl=%"PRIi32,
+                log_info("%s(\"%s\") → status=%s%-20spat=buffer+0x%"PRIxPTR" errno=%d/%s h_errno=%d/%s ttl=%"PRIi32,
                          fname, name,
                          nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n",
-                         pat ? (char*) pat - buffer : 0,
+                         pat ? (uintptr_t) pat - (uintptr_t) buffer : 0,
                          errno1, errno_to_name(errno1) ?: "---",
                          errno2, hstrerror(errno2),
                          ttl);
@@ -145,9 +135,18 @@ static void test_gethostbyname4_r(void *handle, const char *module, const char *
         if (STR_IN_SET(module, "resolve", "mymachines") && status == NSS_STATUS_UNAVAIL)
                 return;
 
-        if (STR_IN_SET(module, "myhostname", "resolve") && streq(name, "localhost")) {
-                assert_se(status == NSS_STATUS_SUCCESS);
-                assert_se(n == 2);
+        if (streq(name, "localhost")) {
+                if (streq(module, "myhostname")) {
+                        assert_se(status == NSS_STATUS_SUCCESS);
+                        assert_se(n == socket_ipv6_is_enabled() + 1);
+
+                } else if (streq(module, "resolve") && secure_getenv_bool("SYSTEMD_NSS_RESOLVE_SYNTHESIZE") != 0) {
+                        assert_se(status == NSS_STATUS_SUCCESS);
+                        if (socket_ipv6_is_enabled())
+                                assert_se(n == 2);
+                        else
+                                assert_se(n <= 2); /* Even if IPv6 is disabled, /etc/hosts may contain ::1. */
+                }
         }
 }
 
@@ -325,7 +324,7 @@ static void test_byname(void *handle, const char *module, const char *name) {
         puts("");
         test_gethostbyname3_r(handle, module, name, AF_UNSPEC);
         puts("");
-        test_gethostbyname3_r(handle, module, name, AF_LOCAL);
+        test_gethostbyname3_r(handle, module, name, AF_UNIX);
         puts("");
 
         test_gethostbyname2_r(handle, module, name, AF_INET);
@@ -334,7 +333,7 @@ static void test_byname(void *handle, const char *module, const char *name) {
         puts("");
         test_gethostbyname2_r(handle, module, name, AF_UNSPEC);
         puts("");
-        test_gethostbyname2_r(handle, module, name, AF_LOCAL);
+        test_gethostbyname2_r(handle, module, name, AF_UNIX);
         puts("");
 
         test_gethostbyname_r(handle, module, name);
@@ -368,7 +367,9 @@ static int make_addresses(struct local_address **addresses) {
                                               .address.in = { htobe32(0x7F000002) } };
         addrs[n++] = (struct local_address) { .family = AF_INET6,
                                               .address.in6 = in6addr_loopback };
-        return 0;
+
+        *addresses = TAKE_PTR(addrs);
+        return n;
 }
 
 static int test_one_module(const char *dir,
@@ -383,7 +384,6 @@ static int test_one_module(const char *dir,
         if (!handle)
                 return -EINVAL;
 
-        char **name;
         STRV_FOREACH(name, names)
                 test_byname(handle, module, *name);
 
@@ -427,11 +427,10 @@ static int parse_argv(int argc, char **argv,
 #if ENABLE_NSS_MYMACHINES
                                 "mymachines",
 #endif
-                                "dns");
+                                NULL);
         assert_se(modules);
 
         if (argc > 2) {
-                char **name;
                 int family;
                 union in_addr_union address;
 
@@ -450,9 +449,13 @@ static int parse_argv(int argc, char **argv,
                         }
                 }
         } else {
-                _cleanup_free_ char *hostname;
+                _cleanup_free_ char *hostname = NULL;
                 assert_se(hostname = gethostname_malloc());
-                assert_se(names = strv_new("localhost", "_gateway", "_outbound", "foo_no_such_host", hostname));
+                assert_se(names = strv_new("localhost",
+                                           "_gateway",
+                                           "_outbound",
+                                           hostname,
+                                           slow_tests_enabled() ? "foo_no_such_host" : NULL));
 
                 n = make_addresses(&addrs);
                 assert_se(n >= 0);
@@ -470,7 +473,6 @@ static int run(int argc, char **argv) {
         _cleanup_strv_free_ char **modules = NULL, **names = NULL;
         _cleanup_free_ struct local_address *addresses = NULL;
         int n_addresses = 0;
-        char **module;
         int r;
 
         test_setup_logging(LOG_INFO);