]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Merge pull request #6329 from poettering/random-mini-fixes
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 11 Jul 2017 15:25:26 +0000 (11:25 -0400)
committerGitHub <noreply@github.com>
Tue, 11 Jul 2017 15:25:26 +0000 (11:25 -0400)
random-util.c mini fixes

src/basic/log.c
src/basic/log.h
src/core/unit.c
src/resolve/resolved-bus.c
src/resolve/resolved-dns-question.c
src/shared/dns-domain.c
src/test/test-dns-domain.c

index ab1e6cac1e860d9d040cf31651026bcce9d1cb45..3fd53800a090e6d82037e1fa2a9c51beaa20afb4 100644 (file)
@@ -232,6 +232,8 @@ fail:
 int log_open(void) {
         int r;
 
+        /* Do not call from library code. */
+
         /* If we don't use the console we close it here, to not get
          * killed by SAK. If we don't use syslog we close it here so
          * that we are not confused by somebody deleting the socket in
@@ -306,6 +308,8 @@ void log_set_target(LogTarget target) {
 }
 
 void log_close(void) {
+        /* Do not call from library code. */
+
         log_close_journal();
         log_close_syslog();
         log_close_kmsg();
@@ -313,6 +317,8 @@ void log_close(void) {
 }
 
 void log_forget_fds(void) {
+        /* Do not call from library code. */
+
         console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
 }
 
@@ -1034,6 +1040,8 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
 }
 
 void log_parse_environment_realm(LogRealm realm) {
+        /* Do not call from library code. */
+
         const char *e;
 
         if (get_ctty_devnr(0, NULL) < 0)
index 57463fbb839c888890407a88710e8b46c9614011..ff5d776b1d7054f2acd7c3e73eca8b2cf737782e 100644 (file)
@@ -86,6 +86,11 @@ int log_get_max_level_realm(LogRealm realm) _pure_;
 #define log_get_max_level()                     \
         log_get_max_level_realm(LOG_REALM)
 
+/* Functions below that open and close logs or configure logging based on the
+ * environment should not be called from library code — this is always a job
+ * for the application itself.
+ */
+
 int log_open(void);
 void log_close(void);
 void log_forget_fds(void);
index db4934a82f22e60fc78ce2d3670667f3769655f4..b28eeb2262ff69dfe314f8b889db56301d8f55ca 100644 (file)
@@ -1832,6 +1832,10 @@ static void unit_check_binds_to(Unit *u) {
                 if (other->job)
                         continue;
 
+                if (!other->coldplugged)
+                        /* We might yet create a job for the other unit… */
+                        continue;
+
                 if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other)))
                         continue;
 
index efa16ad93d68559eb746d93f0d71538c5bd334b8..5aa23485761ec9821889e03d096e055f596ec291 100644 (file)
@@ -345,10 +345,10 @@ static int bus_method_resolve_hostname(sd_bus_message *message, void *userdata,
                 return r;
 
         r = dns_question_new_address(&question_idna, family, hostname, true);
-        if (r < 0)
+        if (r < 0 && r != -EALREADY)
                 return r;
 
-        r = dns_query_new(m, &q, question_utf8, question_idna, ifindex, flags);
+        r = dns_query_new(m, &q, question_utf8, question_idna ?: question_utf8, ifindex, flags);
         if (r < 0)
                 return r;
 
index af29f73164342df23423260df5022ab787e8a844..24f3e8e351f0a2f78deed1f46e77be5d5dc13702 100644 (file)
@@ -309,8 +309,14 @@ int dns_question_new_address(DnsQuestion **ret, int family, const char *name, bo
                 r = dns_name_apply_idna(name, &buf);
                 if (r < 0)
                         return r;
-                if (r > 0)
+                if (r > 0 && !streq(name, buf))
                         name = buf;
+                else
+                        /* We did not manage to create convert the idna name, or it's
+                         * the same as the original name. We assume the caller already
+                         * created an uncoverted question, so let's not repeat work
+                         * unnecessarily. */
+                        return -EALREADY;
         }
 
         q = dns_question_new(family == AF_UNSPEC ? 2 : 1);
index 40aec3a1ea77cfbc5169b6203d6ddc0607284c29..12c4d65dd3f66f82311ee4d977d272b43a860ed6 100644 (file)
@@ -1282,10 +1282,13 @@ int dns_name_apply_idna(const char *name, char **ret) {
                            IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL);
         if (r == IDN2_OK)
                 return 1; /* *ret has been written */
-        else if (IN_SET(r, IDN2_TOO_BIG_DOMAIN, IDN2_TOO_BIG_LABEL))
+        log_debug("idn2_lookup_u8(\"%s\") failed: %s", name, idn2_strerror(r));
+        if (r == IDN2_2HYPHEN)
+                /* The name has two hypens — forbidden by IDNA2008 in some cases */
+                return 0;
+        if (IN_SET(r, IDN2_TOO_BIG_DOMAIN, IDN2_TOO_BIG_LABEL))
                 return -ENOSPC;
-        else
-                return -EINVAL;
+        return -EINVAL;
 #elif defined(HAVE_LIBIDN)
         _cleanup_free_ char *buf = NULL;
         size_t n = 0, allocated = 0;
@@ -1322,7 +1325,7 @@ int dns_name_apply_idna(const char *name, char **ret) {
                 else
                         buf[n++] = '.';
 
-                n +=r;
+                n += r;
         }
 
         if (n > DNS_HOSTNAME_MAX)
@@ -1335,7 +1338,7 @@ int dns_name_apply_idna(const char *name, char **ret) {
         *ret = buf;
         buf = NULL;
 
-        return (int) n;
+        return 1;
 #else
         return 0;
 #endif
index d86add94db5daf9a12aa1c5c16a859b3728e0365..11cf0b1f0b2459ed543975a6348bf9507eed7594 100644 (file)
@@ -607,24 +607,53 @@ static void test_dns_name_common_suffix(void) {
         test_dns_name_common_suffix_one("FOO.BAR", "tEST.bAR", "BAR");
 }
 
-static void test_dns_name_apply_idna_one(const char *s, const char *result) {
-#if defined(HAVE_LIBIDN2) || defined(HAVE_LIBIDN)
+static void test_dns_name_apply_idna_one(const char *s, int expected, const char *result) {
         _cleanup_free_ char *buf = NULL;
-        assert_se(dns_name_apply_idna(s, &buf) >= 0);
-        assert_se(dns_name_equal(buf, result) > 0);
-#endif
+        int r;
+
+        r = dns_name_apply_idna(s, &buf);
+        log_debug("dns_name_apply_idna: \"%s\" → %d/\"%s\" (expected %d/\"%s\")",
+                  s, r, strnull(buf), expected, strnull(result));
+
+        assert_se(r == expected);
+        if (expected == 1)
+                assert_se(dns_name_equal(buf, result) == 1);
 }
 
 static void test_dns_name_apply_idna(void) {
-        test_dns_name_apply_idna_one("", "");
-        test_dns_name_apply_idna_one("foo", "foo");
-        test_dns_name_apply_idna_one("foo.", "foo");
-        test_dns_name_apply_idna_one("foo.bar", "foo.bar");
-        test_dns_name_apply_idna_one("foo.bar.", "foo.bar");
-        test_dns_name_apply_idna_one("föö", "xn--f-1gaa");
-        test_dns_name_apply_idna_one("föö.", "xn--f-1gaa");
-        test_dns_name_apply_idna_one("föö.bär", "xn--f-1gaa.xn--br-via");
-        test_dns_name_apply_idna_one("föö.bär.", "xn--f-1gaa.xn--br-via");
+#if defined HAVE_LIBIDN2 || defined HAVE_LIBIDN
+        const int ret = 1;
+#else
+        const int ret = 0;
+#endif
+
+        /* IDNA2008 forbids names with hyphens in third and fourth positions
+         * (https://tools.ietf.org/html/rfc5891#section-4.2.3.1).
+         * IDNA2003 does not have this restriction
+         * (https://tools.ietf.org/html/rfc3490#section-5).
+         * This means that when using libidn we will transform and test more
+         * labels. If registrars follow IDNA2008 we'll just be performing a
+         * useless lookup.
+         */
+#if defined HAVE_LIBIDN
+        const int ret2 = 1;
+#else
+        const int ret2 = 0;
+#endif
+
+        test_dns_name_apply_idna_one("", ret, "");
+        test_dns_name_apply_idna_one("foo", ret, "foo");
+        test_dns_name_apply_idna_one("foo.", ret, "foo");
+        test_dns_name_apply_idna_one("foo.bar", ret, "foo.bar");
+        test_dns_name_apply_idna_one("foo.bar.", ret, "foo.bar");
+        test_dns_name_apply_idna_one("föö", ret, "xn--f-1gaa");
+        test_dns_name_apply_idna_one("föö.", ret, "xn--f-1gaa");
+        test_dns_name_apply_idna_one("föö.bär", ret, "xn--f-1gaa.xn--br-via");
+        test_dns_name_apply_idna_one("föö.bär.", ret, "xn--f-1gaa.xn--br-via");
+        test_dns_name_apply_idna_one("xn--f-1gaa.xn--br-via", ret, "xn--f-1gaa.xn--br-via");
+
+        test_dns_name_apply_idna_one("r3---sn-ab5l6ne7.googlevideo.com", ret2,
+                                     ret2 ? "r3---sn-ab5l6ne7.googlevideo.com" : "");
 }
 
 static void test_dns_name_is_valid_or_address(void) {
@@ -640,6 +669,9 @@ static void test_dns_name_is_valid_or_address(void) {
 }
 
 int main(int argc, char *argv[]) {
+        log_set_max_level(LOG_DEBUG);
+        log_parse_environment();
+        log_open();
 
         test_dns_label_unescape();
         test_dns_label_unescape_suffix();