]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/resolve/resolved-resolv-conf.c
tree-wide: define iterator inside of the macro
[thirdparty/systemd.git] / src / resolve / resolved-resolv-conf.c
index f8b5301c1ed8424469672cb29f9658df3fb15bf6..0de504636770e44eadecee35fbbe242bc867b61f 100644 (file)
@@ -1,65 +1,49 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <resolv.h>
-#include <stdio_ext.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include "alloc-util.h"
 #include "dns-domain.h"
 #include "fd-util.h"
-#include "fileio-label.h"
 #include "fileio.h"
+#include "fs-util.h"
 #include "ordered-set.h"
 #include "resolved-conf.h"
 #include "resolved-dns-server.h"
 #include "resolved-resolv-conf.h"
+#include "stat-util.h"
 #include "string-util.h"
 #include "strv.h"
-
-/* A resolv.conf file containing the DNS server and domain data we learnt from uplink, i.e. the full uplink data */
-#define PRIVATE_UPLINK_RESOLV_CONF "/run/systemd/resolve/resolv.conf"
-
-/* A resolv.conf file containing the domain data we learnt from uplink, but our own DNS server address. */
-#define PRIVATE_STUB_RESOLV_CONF "/run/systemd/resolve/stub-resolv.conf"
-
-/* A static resolv.conf file containing no domains, but only our own DNS server address */
-#define PRIVATE_STATIC_RESOLV_CONF ROOTLIBEXECDIR "/resolv.conf"
+#include "tmpfile-util-label.h"
 
 int manager_check_resolv_conf(const Manager *m) {
-        const char *path;
-        struct stat st;
-        int r;
+        struct stat st, own;
 
         assert(m);
 
         /* This warns only when our stub listener is disabled and /etc/resolv.conf is a symlink to
-         * PRIVATE_STATIC_RESOLV_CONF or PRIVATE_STUB_RESOLV_CONF. */
+         * PRIVATE_STATIC_RESOLV_CONF. */
 
         if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO)
                 return 0;
 
-        r = stat("/etc/resolv.conf", &st);
-        if (r < 0) {
+        if (stat("/etc/resolv.conf", &st) < 0) {
                 if (errno == ENOENT)
                         return 0;
 
                 return log_warning_errno(errno, "Failed to stat /etc/resolv.conf: %m");
         }
 
-        FOREACH_STRING(path,
-                       PRIVATE_STUB_RESOLV_CONF,
-                       PRIVATE_STATIC_RESOLV_CONF) {
-
-                struct stat own;
-
-                /* Is it symlinked to our own uplink file? */
-                if (stat(path, &own) >= 0 &&
-                    st.st_dev == own.st_dev &&
-                    st.st_ino == own.st_ino) {
-                        log_warning("DNSStubListener= is disabled, but /etc/resolv.conf is a symlink to %s "
-                                    "which expects DNSStubListener= to be enabled.", path);
-                        return -EOPNOTSUPP;
-                }
-        }
+        /* Is it symlinked to our own uplink file? */
+        if (stat(PRIVATE_STATIC_RESOLV_CONF, &own) >= 0 &&
+            st.st_dev == own.st_dev &&
+            st.st_ino == own.st_ino)
+                return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
+                                         "DNSStubListener= is disabled, but /etc/resolv.conf is a symlink to "
+                                         PRIVATE_STATIC_RESOLV_CONF " which expects DNSStubListener= to be enabled.");
 
         return 0;
 }
@@ -110,7 +94,7 @@ int manager_read_resolv_conf(Manager *m) {
         }
 
         /* Have we already seen the file? */
-        if (timespec_load(&st.st_mtim) == m->resolv_conf_mtime)
+        if (stat_inode_unmodified(&st, &m->resolv_conf_stat))
                 return 0;
 
         if (file_is_our_own(&st))
@@ -176,7 +160,7 @@ int manager_read_resolv_conf(Manager *m) {
                 log_syntax(NULL, LOG_DEBUG, "/etc/resolv.conf", n, 0, "Ignoring resolv.conf line: %s", l);
         }
 
-        m->resolv_conf_mtime = timespec_load(&st.st_mtim);
+        m->resolv_conf_stat = st;
 
         /* Flush out all servers and search domains that are still
          * marked. Those are then ones that didn't appear in the new
@@ -217,6 +201,8 @@ clear:
 }
 
 static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
+        DnsScope *scope;
+
         assert(s);
         assert(f);
         assert(count);
@@ -226,13 +212,12 @@ static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
                 return;
         }
 
-        /* Check if the DNS server is limited to particular domains;
-         * resolv.conf does not have a syntax to express that, so it must not
-         * appear as a global name server to avoid routing unrelated domains to
-         * it (which is a privacy violation, will most probably fail anyway,
-         * and adds unnecessary load) */
-        if (dns_server_limited_domains(s)) {
-                log_debug("DNS server %s has route-only domains, not using as global name server", dns_server_string(s));
+        /* Check if the scope this DNS server belongs to is suitable as 'default' route for lookups; resolv.conf does
+         * not have a syntax to express that, so it must not appear as a global name server to avoid routing unrelated
+         * domains to it (which is a privacy violation, will most probably fail anyway, and adds unnecessary load) */
+        scope = dns_server_scope(s);
+        if (scope && !dns_scope_is_default_route(scope)) {
+                log_debug("Scope of DNS server %s has only route-only domains, not using as global name server", dns_server_string(s));
                 return;
         }
 
@@ -246,8 +231,6 @@ static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
 static void write_resolv_conf_search(
                 OrderedSet *domains,
                 FILE *f) {
-        unsigned length = 0, count = 0;
-        Iterator i;
         char *domain;
 
         assert(domains);
@@ -255,16 +238,7 @@ static void write_resolv_conf_search(
 
         fputs("search", f);
 
-        ORDERED_SET_FOREACH(domain, domains, i) {
-                if (++count > MAXDNSRCH) {
-                        fputs("\n# Too many search domains configured, remaining ones ignored.", f);
-                        break;
-                }
-                length += strlen(domain) + 1;
-                if (length > 256) {
-                        fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f);
-                        break;
-                }
+        ORDERED_SET_FOREACH(domain, domains) {
                 fputc(' ', f);
                 fputs(domain, f);
         }
@@ -273,16 +247,15 @@ static void write_resolv_conf_search(
 }
 
 static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
-        Iterator i;
 
         fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
               "#\n"
               "# This is a dynamic resolv.conf file for connecting local clients directly to\n"
               "# all known uplink DNS servers. This file lists all configured search domains.\n"
               "#\n"
-              "# Third party programs must not access this file directly, but only through the\n"
-              "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n"
-              "# replace this symlink by a static file or a different symlink.\n"
+              "# Third party programs should typically not access this file directly, but only\n"
+              "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
+              "# different way, replace this symlink by a static file or a different symlink.\n"
               "#\n"
               "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
               "# operation for /etc/resolv.conf.\n"
@@ -294,7 +267,7 @@ static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSe
                 unsigned count = 0;
                 DnsServer *s;
 
-                ORDERED_SET_FOREACH(s, dns, i)
+                ORDERED_SET_FOREACH(s, dns)
                         write_resolv_conf_server(s, f, &count);
         }
 
@@ -305,23 +278,24 @@ static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSe
 }
 
 static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
-        fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
-                       "#\n"
-                       "# This is a dynamic resolv.conf file for connecting local clients to the\n"
-                       "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
-                       "# configured search domains.\n"
-                       "#\n"
-                       "# Run \"resolvectl status\" to see details about the uplink DNS servers\n"
-                       "# currently in use.\n"
-                       "#\n"
-                       "# Third party programs must not access this file directly, but only through the\n"
-                       "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n"
-                       "# replace this symlink by a static file or a different symlink.\n"
-                       "#\n"
-                       "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
-                       "# operation for /etc/resolv.conf.\n"
-                       "\n"
-                       "nameserver 127.0.0.53\n", f);
+        fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n"
+              "#\n"
+              "# This is a dynamic resolv.conf file for connecting local clients to the\n"
+              "# internal DNS stub resolver of systemd-resolved. This file lists all\n"
+              "# configured search domains.\n"
+              "#\n"
+              "# Run \"resolvectl status\" to see details about the uplink DNS servers\n"
+              "# currently in use.\n"
+              "#\n"
+              "# Third party programs should typically not access this file directly, but only\n"
+              "# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a\n"
+              "# different way, replace this symlink by a static file or a different symlink.\n"
+              "#\n"
+              "# See man:systemd-resolved.service(8) for details about the supported modes of\n"
+              "# operation for /etc/resolv.conf.\n"
+              "\n"
+              "nameserver 127.0.0.53\n"
+              "options edns0 trust-ad\n", f);
 
         if (!ordered_set_isempty(domains))
                 write_resolv_conf_search(domains, f);
@@ -330,7 +304,6 @@ static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet
 }
 
 int manager_write_resolv_conf(Manager *m) {
-
         _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
         _cleanup_free_ char *temp_path_uplink = NULL, *temp_path_stub = NULL;
         _cleanup_fclose_ FILE *f_uplink = NULL, *f_stub = NULL;
@@ -352,47 +325,49 @@ int manager_write_resolv_conf(Manager *m) {
 
         r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink);
         if (r < 0)
-                return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m");
+                return log_warning_errno(r, "Failed to open new %s for writing: %m", PRIVATE_UPLINK_RESOLV_CONF);
 
-        (void) __fsetlocking(f_uplink, FSETLOCKING_BYCALLER);
         (void) fchmod(fileno(f_uplink), 0644);
 
-        r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
-        if (r < 0)
-                return log_warning_errno(r, "Failed to open private stub-resolv.conf file for writing: %m");
-
-        (void) __fsetlocking(f_stub, FSETLOCKING_BYCALLER);
-        (void) fchmod(fileno(f_stub), 0644);
-
         r = write_uplink_resolv_conf_contents(f_uplink, dns, domains);
         if (r < 0) {
-                log_error_errno(r, "Failed to write private resolv.conf contents: %m");
+                log_error_errno(r, "Failed to write new %s: %m", PRIVATE_UPLINK_RESOLV_CONF);
                 goto fail;
         }
 
-        if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0) {
-                r = log_error_errno(errno, "Failed to move private resolv.conf file into place: %m");
-                goto fail;
-        }
+        if (m->dns_stub_listener_mode != DNS_STUB_LISTENER_NO) {
+                r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub);
+                if (r < 0) {
+                        log_warning_errno(r, "Failed to open new %s for writing: %m", PRIVATE_STUB_RESOLV_CONF);
+                        goto fail;
+                }
 
-        r = write_stub_resolv_conf_contents(f_stub, dns, domains);
-        if (r < 0) {
-                log_error_errno(r, "Failed to write private stub-resolv.conf contents: %m");
-                goto fail;
-        }
+                (void) fchmod(fileno(f_stub), 0644);
 
-        if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0) {
-                r = log_error_errno(errno, "Failed to move private stub-resolv.conf file into place: %m");
-                goto fail;
+                r = write_stub_resolv_conf_contents(f_stub, dns, domains);
+                if (r < 0) {
+                        log_error_errno(r, "Failed to write new %s: %m", PRIVATE_STUB_RESOLV_CONF);
+                        goto fail;
+                }
+
+                if (rename(temp_path_stub, PRIVATE_STUB_RESOLV_CONF) < 0)
+                        r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_STUB_RESOLV_CONF);
+
+        } else {
+                r = symlink_atomic(basename(PRIVATE_UPLINK_RESOLV_CONF), PRIVATE_STUB_RESOLV_CONF);
+                if (r < 0)
+                        log_error_errno(r, "Failed to symlink %s: %m", PRIVATE_STUB_RESOLV_CONF);
         }
 
-        return 0;
+        if (rename(temp_path_uplink, PRIVATE_UPLINK_RESOLV_CONF) < 0)
+                r = log_error_errno(errno, "Failed to move new %s into place: %m", PRIVATE_UPLINK_RESOLV_CONF);
 
-fail:
-        (void) unlink(PRIVATE_UPLINK_RESOLV_CONF);
-        (void) unlink(temp_path_uplink);
-        (void) unlink(PRIVATE_STUB_RESOLV_CONF);
-        (void) unlink(temp_path_stub);
+ fail:
+        if (r < 0) {
+                /* Something went wrong, perform cleanup... */
+                (void) unlink(temp_path_uplink);
+                (void) unlink(temp_path_stub);
+        }
 
         return r;
 }