chiark / gitweb /
resolved: when rereading /etc/resolv.conf, always start using first DNS server again
[elogind.git] / src / resolve / resolved-manager.c
index 04ee204074107d54dc2e99c6d90acfa0f4b2d866..2edfb9f7fdee20b50f071e94d9bc19d85e7f53cb 100644 (file)
@@ -34,6 +34,7 @@
 #include "socket-util.h"
 #include "af-list.h"
 #include "utf8.h"
+#include "fileio-label.h"
 
 #include "resolved-dns-domain.h"
 #include "resolved-conf.h"
@@ -99,7 +100,7 @@ static int manager_process_link(sd_rtnl *rtnl, sd_rtnl_message *mm, void *userda
         return 0;
 
 fail:
-        log_warning("Failed to process RTNL link message: %s", strerror(-r));
+        log_warning_errno(r, "Failed to process RTNL link message: %m");
         return 0;
 }
 
@@ -184,7 +185,7 @@ static int manager_process_address(sd_rtnl *rtnl, sd_rtnl_message *mm, void *use
         return 0;
 
 fail:
-        log_warning("Failed to process RTNL address message: %s", strerror(-r));
+        log_warning_errno(r, "Failed to process RTNL address message: %m");
         return 0;
 }
 
@@ -277,12 +278,12 @@ static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *
         HASHMAP_FOREACH(l, m->links, i) {
                 r = link_update_monitor(l);
                 if (r < 0)
-                        log_warning("Failed to update monitor information for %i: %s", l->ifindex, strerror(-r));
+                        log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex);
         }
 
         r = manager_write_resolv_conf(m);
         if (r < 0)
-                log_warning("Could not update resolv.conf: %s", strerror(-r));
+                log_warning_errno(r, "Could not update resolv.conf: %m");
 
         return 0;
 }
@@ -369,7 +370,7 @@ static int manager_watch_hostname(Manager *m) {
 
         m->hostname_fd = open("/proc/sys/kernel/hostname", O_RDONLY|O_CLOEXEC|O_NDELAY|O_NOCTTY);
         if (m->hostname_fd < 0) {
-                log_warning("Failed to watch hostname: %m");
+                log_warning_errno(errno, "Failed to watch hostname: %m");
                 return 0;
         }
 
@@ -378,10 +379,8 @@ static int manager_watch_hostname(Manager *m) {
                 if (r == -EPERM)
                         /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */
                         m->hostname_fd = safe_close(m->hostname_fd);
-                else {
-                        log_error("Failed to add hostname event source: %s", strerror(-r));
-                        return r;
-                }
+                else
+                        return log_error_errno(r, "Failed to add hostname event source: %m");
         }
 
         r = determine_hostname(&m->hostname);
@@ -449,7 +448,7 @@ static int manager_llmnr_start(Manager *m) {
         return 0;
 
 eaddrinuse:
-        log_warning("There appears to be another LLMNR respondering running. Turning off LLMNR support.");
+        log_warning("There appears to be another LLMNR responder running. Turning off LLMNR support.");
         m->llmnr_support = SUPPORT_NO;
         manager_llmnr_stop(m);
 
@@ -592,7 +591,7 @@ int manager_read_resolv_conf(Manager *m) {
         r = stat("/etc/resolv.conf", &st);
         if (r < 0) {
                 if (errno != ENOENT)
-                        log_warning("Failed to open /etc/resolv.conf: %m");
+                        log_warning_errno(errno, "Failed to open /etc/resolv.conf: %m");
                 r = -errno;
                 goto clear;
         }
@@ -615,13 +614,13 @@ int manager_read_resolv_conf(Manager *m) {
         f = fopen("/etc/resolv.conf", "re");
         if (!f) {
                 if (errno != ENOENT)
-                        log_warning("Failed to open /etc/resolv.conf: %m");
+                        log_warning_errno(errno, "Failed to open /etc/resolv.conf: %m");
                 r = -errno;
                 goto clear;
         }
 
         if (fstat(fileno(f), &st) < 0) {
-                log_error("Failed to stat open file: %m");
+                log_error_errno(errno, "Failed to stat open file: %m");
                 r = -errno;
                 goto clear;
         }
@@ -668,6 +667,16 @@ int manager_read_resolv_conf(Manager *m) {
                 if (s->marked)
                         dns_server_free(s);
 
+        /* Whenever /etc/resolv.conf changes, start using the first
+         * DNS server of it. This is useful to deal with broken
+         * network managing implementations (like NetworkManager),
+         * that when connecting to a VPN place both the VPN DNS
+         * servers and the local ones in /etc/resolv.conf. Without
+         * resetting the DNS server to use back to the first entry we
+         * will continue to use the local one thus being unable to
+         * resolve VPN domains. */
+        manager_set_dns_server(m, m->dns_servers);
+
         return 0;
 
 clear:
@@ -687,7 +696,7 @@ static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
 
         r = in_addr_to_string(s->family, &s->address, &t);
         if (r < 0) {
-                log_warning("Invalid DNS address. Ignoring: %s", strerror(-r));
+                log_warning_errno(r, "Invalid DNS address. Ignoring: %m");
                 return;
         }
 
@@ -699,7 +708,7 @@ static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
 }
 
 static void write_resolv_conf_search(const char *domain, FILE *f,
-                                     unsigned *length, unsigned *count) {
+                                     unsigned *count, unsigned *length) {
         assert(domain);
         assert(f);
         assert(length);
@@ -720,12 +729,44 @@ static void write_resolv_conf_search(const char *domain, FILE *f,
         (*count) ++;
 }
 
+static int write_resolv_conf_contents(FILE *f, Set *dns, Set *domains) {
+        Iterator i;
+
+        fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
+              "# Third party programs must not access this file directly, but\n"
+              "# only through the symlink at /etc/resolv.conf. To manage\n"
+              "# resolv.conf(5) in a different way, replace the symlink by a\n"
+              "# static file or a different symlink.\n\n", f);
+
+        if (set_isempty(dns))
+                fputs("# No DNS servers known.\n", f);
+        else {
+                DnsServer *s;
+                unsigned count = 0;
+
+                SET_FOREACH(s, dns, i)
+                        write_resolv_conf_server(s, f, &count);
+        }
+
+        if (!set_isempty(domains)) {
+                unsigned length = 0, count = 0;
+                char *domain;
+
+                fputs("search", f);
+                SET_FOREACH(domain, domains, i)
+                        write_resolv_conf_search(domain, f, &count, &length);
+                fputs("\n", f);
+        }
+
+        return fflush_and_check(f);
+}
+
+
 int manager_write_resolv_conf(Manager *m) {
         static const char path[] = "/run/systemd/resolve/resolv.conf";
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_set_free_ Set *dns = NULL, *domains = NULL;
-        unsigned count = 0;
         DnsServer *s;
         Iterator i;
         Link *l;
@@ -737,11 +778,11 @@ int manager_write_resolv_conf(Manager *m) {
         manager_read_resolv_conf(m);
 
         /* Add the full list to a set, to filter out duplicates */
-        dns = set_new(dns_server_hash_func, dns_server_compare_func);
+        dns = set_new(&dns_server_hash_ops);
         if (!dns)
                 return -ENOMEM;
 
-        domains = set_new(dns_name_hash_func, dns_name_compare_func);
+        domains = set_new(&dns_name_hash_ops);
         if (!domains)
                 return -ENOMEM;
 
@@ -789,38 +830,13 @@ int manager_write_resolv_conf(Manager *m) {
                 }
         }
 
-        r = fopen_temporary(path, &f, &temp_path);
+        r = fopen_temporary_label(path, path, &f, &temp_path);
         if (r < 0)
                 return r;
 
         fchmod(fileno(f), 0644);
 
-        fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
-              "# Third party programs must not access this file directly, but\n"
-              "# only through the symlink at /etc/resolv.conf. To manage\n"
-              "# resolv.conf(5) in a different way, replace the symlink by a\n"
-              "# static file or a different symlink.\n\n", f);
-
-        if (set_isempty(dns))
-                fputs("# No DNS servers known.\n", f);
-        else {
-                SET_FOREACH(s, dns, i)
-                        write_resolv_conf_server(s, f, &count);
-        }
-
-        if (!set_isempty(domains)) {
-                unsigned length = 0;
-                char *domain;
-
-                count = 0;
-
-                fputs("search", f);
-                SET_FOREACH(domain, domains, i)
-                        write_resolv_conf_search(domain, f, &count, &length);
-                fputs("\n", f);
-        }
-
-        r = fflush_and_check(f);
+        r = write_resolv_conf_contents(f, dns, domains);
         if (r < 0)
                 goto fail;
 
@@ -841,7 +857,7 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
         _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
         union {
                 struct cmsghdr header; /* For alignment */
-                uint8_t buffer[CMSG_SPACE(CONST_MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo)))
+                uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo))
                                + CMSG_SPACE(int) /* ttl/hoplimit */
                                + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
         } control;
@@ -952,7 +968,7 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
          * device if the packet came from the local host since it
          * avoids the routing table in such a case. Let's unset the
          * interface index in such a case. */
-        if (p->ifindex > 0 && manager_ifindex_is_loopback(m, p->ifindex) != 0)
+        if (p->ifindex == LOOPBACK_IFINDEX)
                 p->ifindex = 0;
 
         /* If we don't know the interface index still, we look for the
@@ -1687,20 +1703,6 @@ fail:
         return r;
 }
 
-int manager_ifindex_is_loopback(Manager *m, int ifindex) {
-        Link *l;
-        assert(m);
-
-        if (ifindex <= 0)
-                return -EINVAL;
-
-        l = hashmap_get(m->links, INT_TO_PTR(ifindex));
-        if (l->flags & IFF_LOOPBACK)
-                return 1;
-
-        return 0;
-}
-
 int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) {
         LinkAddress *a;