]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/resolve/resolved-manager.c
Merge pull request #179 from l10n-tw/master
[thirdparty/systemd.git] / src / resolve / resolved-manager.c
index 9f61c9d9a294a3e03a03eb3d1153d527f7be5de8..aa78885ac315ed786bfa0d12cfc3378dd549f363 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
  ***/
 
-#include <arpa/inet.h>
 #include <resolv.h>
-#include <net/if.h>
 #include <sys/ioctl.h>
-#include <sys/poll.h>
+#include <poll.h>
 #include <netinet/in.h>
 
 #include "rtnl-util.h"
-#include "event-util.h"
-#include "network-util.h"
 #include "network-internal.h"
-#include "conf-parser.h"
 #include "socket-util.h"
 #include "af-list.h"
 #include "utf8.h"
 #include "fileio-label.h"
+#include "ordered-set.h"
+#include "random-util.h"
+#include "hostname-util.h"
 
-#include "resolved-dns-domain.h"
+#include "dns-domain.h"
 #include "resolved-conf.h"
 #include "resolved-bus.h"
 #include "resolved-manager.h"
@@ -196,8 +194,8 @@ static int manager_rtnl_listen(Manager *m) {
 
         assert(m);
 
-        /* First, subscibe to interfaces coming and going */
-        r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR);
+        /* First, subscribe to interfaces coming and going */
+        r = sd_rtnl_open(&m->rtnl);
         if (r < 0)
                 return r;
 
@@ -370,7 +368,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;
         }
 
@@ -379,10 +377,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_errno(r, "Failed to add hostname event source: %m");
-                        return r;
-                }
+                else
+                        return log_error_errno(r, "Failed to add hostname event source: %m");
         }
 
         r = determine_hostname(&m->hostname);
@@ -538,11 +534,11 @@ Manager *manager_free(Manager *m) {
         while (m->dns_queries)
                 dns_query_free(m->dns_queries);
 
-        dns_scope_free(m->unicast_scope);
-
         manager_flush_dns_servers(m, DNS_SERVER_SYSTEM);
         manager_flush_dns_servers(m, DNS_SERVER_FALLBACK);
 
+        dns_scope_free(m->unicast_scope);
+
         hashmap_free(m->links);
         hashmap_free(m->dns_transactions);
 
@@ -593,7 +589,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;
         }
@@ -616,13 +612,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;
         }
@@ -669,6 +665,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:
@@ -699,8 +705,11 @@ static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
         (*count) ++;
 }
 
-static void write_resolv_conf_search(const char *domain, FILE *f,
-                                     unsigned *count, unsigned *length) {
+static void write_resolv_conf_search(
+                const char *domain, FILE *f,
+                unsigned *count,
+                unsigned *length) {
+
         assert(domain);
         assert(f);
         assert(length);
@@ -721,7 +730,7 @@ static void write_resolv_conf_search(const char *domain, FILE *f,
         (*count) ++;
 }
 
-static int write_resolv_conf_contents(FILE *f, Set *dns, Set *domains) {
+static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
         Iterator i;
 
         fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
@@ -730,22 +739,22 @@ static int write_resolv_conf_contents(FILE *f, Set *dns, Set *domains) {
               "# 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))
+        if (ordered_set_isempty(dns))
                 fputs("# No DNS servers known.\n", f);
         else {
                 DnsServer *s;
                 unsigned count = 0;
 
-                SET_FOREACH(s, dns, i)
+                ORDERED_SET_FOREACH(s, dns, i)
                         write_resolv_conf_server(s, f, &count);
         }
 
-        if (!set_isempty(domains)) {
+        if (!ordered_set_isempty(domains)) {
                 unsigned length = 0, count = 0;
                 char *domain;
 
                 fputs("search", f);
-                SET_FOREACH(domain, domains, i)
+                ORDERED_SET_FOREACH(domain, domains, i)
                         write_resolv_conf_search(domain, f, &count, &length);
                 fputs("\n", f);
         }
@@ -753,12 +762,11 @@ static int write_resolv_conf_contents(FILE *f, Set *dns, Set *domains) {
         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;
+        _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
         DnsServer *s;
         Iterator i;
         Link *l;
@@ -770,17 +778,17 @@ 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_ops);
+        dns = ordered_set_new(&dns_server_hash_ops);
         if (!dns)
                 return -ENOMEM;
 
-        domains = set_new(&dns_name_hash_ops);
+        domains = ordered_set_new(&dns_name_hash_ops);
         if (!domains)
                 return -ENOMEM;
 
         /* First add the system-wide servers */
         LIST_FOREACH(servers, s, m->dns_servers) {
-                r = set_put(dns, s);
+                r = ordered_set_put(dns, s);
                 if (r == -EEXIST)
                         continue;
                 if (r < 0)
@@ -792,7 +800,7 @@ int manager_write_resolv_conf(Manager *m) {
                 char **domain;
 
                 LIST_FOREACH(servers, s, l->dns_servers) {
-                        r = set_put(dns, s);
+                        r = ordered_set_put(dns, s);
                         if (r == -EEXIST)
                                 continue;
                         if (r < 0)
@@ -803,7 +811,7 @@ int manager_write_resolv_conf(Manager *m) {
                         continue;
 
                 STRV_FOREACH(domain, l->unicast_scope->domains) {
-                        r = set_put(domains, *domain);
+                        r = ordered_set_put(domains, *domain);
                         if (r == -EEXIST)
                                 continue;
                         if (r < 0)
@@ -812,9 +820,9 @@ int manager_write_resolv_conf(Manager *m) {
         }
 
         /* If we found nothing, add the fallback servers */
-        if (set_isempty(dns)) {
+        if (ordered_set_isempty(dns)) {
                 LIST_FOREACH(servers, s, m->fallback_dns_servers) {
-                        r = set_put(dns, s);
+                        r = ordered_set_put(dns, s);
                         if (r == -EEXIST)
                                 continue;
                         if (r < 0)
@@ -840,8 +848,8 @@ int manager_write_resolv_conf(Manager *m) {
         return 0;
 
 fail:
-        unlink(path);
-        unlink(temp_path);
+        (void) unlink(path);
+        (void) unlink(temp_path);
         return r;
 }
 
@@ -912,7 +920,7 @@ int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
         } else
                 return -EAFNOSUPPORT;
 
-        for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
+        CMSG_FOREACH(cmsg, &mh) {
 
                 if (cmsg->cmsg_level == IPPROTO_IPV6) {
                         assert(p->family == AF_INET6);