chiark / gitweb /
machine: make sure unpriviliged "machinectl status" can show the machine's OS version
[elogind.git] / src / resolve / resolved-manager.c
index 1e86c1003b84c6cb2e547e7ce3656dfdc5f9e2fc..1b6dc8a4a3a685c7de188a0113a3d4f609db9faf 100644 (file)
@@ -184,7 +184,6 @@ fail:
         return 0;
 }
 
-
 static int manager_rtnl_listen(Manager *m) {
         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
         sd_rtnl_message *i;
@@ -360,7 +359,6 @@ static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, voi
 }
 
 static int manager_watch_hostname(Manager *m) {
-        _cleanup_free_ char *h = NULL;
         int r;
 
         assert(m);
@@ -410,6 +408,7 @@ int manager_new(Manager **ret) {
         m->hostname_fd = -1;
 
         m->llmnr_support = SUPPORT_YES;
+        m->read_resolv_conf = true;
 
         r = manager_parse_dns_server(m, DNS_SERVER_FALLBACK, DNS_SERVERS);
         if (r < 0)
@@ -520,6 +519,110 @@ Manager *manager_free(Manager *m) {
         return NULL;
 }
 
+int manager_read_resolv_conf(Manager *m) {
+        _cleanup_fclose_ FILE *f = NULL;
+        struct stat st, own;
+        char line[LINE_MAX];
+        DnsServer *s, *nx;
+        usec_t t;
+        int r;
+
+        assert(m);
+
+        /* Reads the system /etc/resolv.conf, if it exists and is not
+         * symlinked to our own resolv.conf instance */
+
+        if (!m->read_resolv_conf)
+                return 0;
+
+        r = stat("/etc/resolv.conf", &st);
+        if (r < 0) {
+                if (errno != ENOENT)
+                        log_warning("Failed to open /etc/resolv.conf: %m");
+                r = -errno;
+                goto clear;
+        }
+
+        /* Have we already seen the file? */
+        t = timespec_load(&st.st_mtim);
+        if (t == m->resolv_conf_mtime)
+                return 0;
+
+        m->resolv_conf_mtime = t;
+
+        /* Is it symlinked to our own file? */
+        if (stat("/run/systemd/resolve/resolv.conf", &own) >= 0 &&
+            st.st_dev == own.st_dev &&
+            st.st_ino == own.st_ino) {
+                r = 0;
+                goto clear;
+        }
+
+        f = fopen("/etc/resolv.conf", "re");
+        if (!f) {
+                if (errno != ENOENT)
+                        log_warning("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");
+                r = -errno;
+                goto clear;
+        }
+
+        LIST_FOREACH(servers, s, m->dns_servers)
+                s->marked = true;
+
+        FOREACH_LINE(line, f, r = -errno; goto clear) {
+                union in_addr_union address;
+                int family;
+                char *l;
+                const char *a;
+
+                truncate_nl(line);
+
+                l = strstrip(line);
+                if (*l == '#' || *l == ';')
+                        continue;
+
+                a = first_word(l, "nameserver");
+                if (!a)
+                        continue;
+
+                r = in_addr_from_string_auto(a, &family, &address);
+                if (r < 0) {
+                        log_warning("Failed to parse name server %s.", a);
+                        continue;
+                }
+
+                LIST_FOREACH(servers, s, m->dns_servers)
+                        if (s->family == family && in_addr_equal(family, &s->address, &address) > 0)
+                                break;
+
+                if (s)
+                        s->marked = false;
+                else {
+                        r = dns_server_new(m, NULL, DNS_SERVER_SYSTEM, NULL, family, &address);
+                        if (r < 0)
+                                goto clear;
+                }
+        }
+
+        LIST_FOREACH_SAFE(servers, s, nx, m->dns_servers)
+                if (s->marked)
+                        dns_server_free(s);
+
+        return 0;
+
+clear:
+        while (m->dns_servers)
+                dns_server_free(m->dns_servers);
+
+        return r;
+}
+
 static void write_resolve_conf_server(DnsServer *s, FILE *f, unsigned *count) {
         _cleanup_free_ char *t  = NULL;
         int r;
@@ -553,6 +656,9 @@ int manager_write_resolv_conf(Manager *m) {
 
         assert(m);
 
+        /* Read the system /etc/resolv.conf first */
+        manager_read_resolv_conf(m);
+
         r = fopen_temporary(path, &f, &temp_path);
         if (r < 0)
                 return r;
@@ -946,24 +1052,24 @@ int manager_send(Manager *m, int fd, int ifindex, int family, const union in_add
         return -EAFNOSUPPORT;
 }
 
-bool manager_known_dns_server(Manager *m, int family, const union in_addr_union *in_addr) {
+DnsServer* manager_find_dns_server(Manager *m, int family, const union in_addr_union *in_addr) {
         DnsServer *s;
 
         assert(m);
         assert(in_addr);
 
         LIST_FOREACH(servers, s, m->dns_servers)
-                if (s->family == family && in_addr_equal(family, &s->address, in_addr))
-                        return true;
+                if (s->family == family && in_addr_equal(family, &s->address, in_addr) > 0)
+                        return s;
 
         LIST_FOREACH(servers, s, m->fallback_dns_servers)
-                if (s->family == family && in_addr_equal(family, &s->address, in_addr))
-                        return true;
+                if (s->family == family && in_addr_equal(family, &s->address, in_addr) > 0)
+                        return s;
 
-        return false;
+        return NULL;
 }
 
-static DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) {
+DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) {
         assert(m);
 
         if (m->current_dns_server == s)
@@ -974,10 +1080,13 @@ static DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) {
 
                 in_addr_to_string(s->family, &s->address, &ip);
                 log_info("Switching to system DNS server %s.", strna(ip));
-        } else
-                log_info("No system DNS server set.");
+        }
 
         m->current_dns_server = s;
+
+        if (m->unicast_scope)
+                dns_cache_flush(&m->unicast_scope->cache);
+
         return s;
 }
 
@@ -985,6 +1094,9 @@ DnsServer *manager_get_dns_server(Manager *m) {
         Link *l;
         assert(m);
 
+        /* Try to read updates resolv.conf */
+        manager_read_resolv_conf(m);
+
         if (!m->current_dns_server)
                 manager_set_dns_server(m, m->dns_servers);