]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolved: create private stub resolve file for /etc/resolv.conf integration (#7014)
authorDimitri John Ledkov <xnox@ubuntu.com>
Tue, 24 Oct 2017 13:28:41 +0000 (09:28 -0400)
committerLennart Poettering <lennart@poettering.net>
Tue, 24 Oct 2017 13:28:41 +0000 (15:28 +0200)
This creates a second private resolve.conf file which lists the stub resolver
and the resolved acquired search domains.

This runtime file should be used as a symlink target for /etc/resolv.conf such
that non-nss based applications can resolve search domains.

Fixes: #7009
NEWS
man/systemd-resolved.service.xml
src/resolve/resolved-resolv-conf.c
src/resolve/resolved-resolv-conf.h
src/resolve/resolved.c
tmpfiles.d/etc.conf.m4

diff --git a/NEWS b/NEWS
index 8054d88d9694929f408247361b9f107c82ed4faf..ecd12d32be2fb556c035399206d2404e26df5a32 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,14 @@ CHANGES WITH 236 in spe:
          has been extended to also set the dummy module option numdummies=0,
          resolving issues with the kernel creating dummy0.
 
+        * systemd-resolved now maintains a new dynamic
+          /run/systemd/resolve/stub-resolv.conf compatibility file. It is now
+          recommended to maintain /etc/resolv.conf as a symlink to this new
+          dynamic file. It points at the systemd-resolved stub DNS 127.0.0.53
+          resolver and it includes dynamically acquired search domains. This
+          achieves a more correct DNS resolution by software that bypasses
+          local DNS APIs (e.g. NSS).
+
 CHANGES WITH 235:
 
         * A new modprobe.d drop-in is now shipped by default that sets the
index d07d1968b4d1469bf4864fc097bce2c929cff617..c60f474aa8f2618266ff54b829fcea4c5386b77e 100644 (file)
@@ -94,7 +94,8 @@
     <citerefentry><refentrytitle>systemd.network</refentrytitle><manvolnum>5</manvolnum></citerefentry> for details
     about systemd's own configuration files for DNS servers. To improve compatibility,
     <filename>/etc/resolv.conf</filename> is read in order to discover configured system DNS servers, but only if it is
-    not a symlink to <filename>/run/systemd/resolve/resolv.conf</filename> (see below).</para>
+    not a symlink to <filename>/run/systemd/resolve/stub-resolv.conf</filename> or
+    <filename>/run/systemd/resolve/resolv.conf</filename> (see below).</para>
 
     <para><command>systemd-resolved</command> synthesizes DNS resource records (RRs) for the following cases:</para>
 
   <refsect1>
     <title><filename>/etc/resolv.conf</filename></title>
 
-    <para>Three modes of handling <filename>/etc/resolv.conf</filename> (see
+    <para>Four modes of handling <filename>/etc/resolv.conf</filename> (see
     <citerefentry project='man-pages'><refentrytitle>resolv.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>) are
     supported:</para>
 
     <itemizedlist>
+      <listitem><para><command>systemd-resolved</command> maintains the
+      <filename>/run/systemd/resolve/stub-resolv.conf</filename> file for compatibility with traditional Linux
+      programs. This file may be symlinked from <filename>/etc/resolv.conf</filename>. This file lists the 127.0.0.53
+      DNS stub (see above) as the only DNS server. It also contains a list of search domains that are in use by
+      systemd-resolved. The list of search domains is always kept up-to-date. Note that
+      <filename>/run/systemd/resolve/stub-resolv.conf</filename> should not be used directly by applications, but only
+      through a symlink from <filename>/etc/resolv.conf</filename>. This file may be symlinked from
+      <filename>/etc/resolv.conf</filename> in order to connect all local clients that bypass local DNS APIs to
+      <command>systemd-resolved</command> with correct search domains settings. This mode of operation is
+      recommended.</para></listitem>
+
       <listitem><para>A static file <filename>/usr/lib/systemd/resolv.conf</filename> is provided that lists
       the 127.0.0.53 DNS stub (see above) as only DNS server. This file may be symlinked from
       <filename>/etc/resolv.conf</filename> in order to connect all local clients that bypass local DNS APIs to
-      <command>systemd-resolved</command>. This mode of operation is recommended.</para></listitem>
+      <command>systemd-resolved</command>. This file does not contain any search domains.</para></listitem>
 
       <listitem><para><command>systemd-resolved</command> maintains the
       <filename>/run/systemd/resolve/resolv.conf</filename> file for compatibility with traditional Linux
index e3d6a3340934a7ce63e4f5b48241f00477d2e2fc..4944e299a49a6cab7528dfeb4848badcb5f3c398 100644 (file)
@@ -61,7 +61,13 @@ int manager_read_resolv_conf(Manager *m) {
                 return 0;
 
         /* Is it symlinked to our own file? */
-        if (stat(PRIVATE_RESOLV_CONF, &own) >= 0 &&
+        if (stat(PRIVATE_UPLINK_RESOLV_CONF, &own) >= 0 &&
+            st.st_dev == own.st_dev &&
+            st.st_ino == own.st_ino)
+                return 0;
+
+        /* Is it symlinked to our own stub file? */
+        if (stat(PRIVATE_STUB_RESOLV_CONF, &own) >= 0 &&
             st.st_dev == own.st_dev &&
             st.st_ino == own.st_ino)
                 return 0;
@@ -206,7 +212,7 @@ static void write_resolv_conf_search(
         fputs_unlocked("\n", f);
 }
 
-static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
+static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
         Iterator i;
 
         fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n#\n"
@@ -234,11 +240,25 @@ static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *doma
         return fflush_and_check(f);
 }
 
+static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
+        fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n#\n"
+              "# 127.0.0.53 is the systemd-resolved stub resolver.\n"
+              "# run \"systemd-resolve --status\" to see details about the actual nameservers.\n"
+              "nameserver 127.0.0.53\n\n", f);
+
+        if (!ordered_set_isempty(domains))
+                write_resolv_conf_search(domains, f);
+
+        return fflush_and_check(f);
+}
+
 int manager_write_resolv_conf(Manager *m) {
 
         _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
-        _cleanup_free_ char *temp_path = NULL;
-        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *temp_path_uplink = NULL;
+        _cleanup_free_ char *temp_path_stub = NULL;
+        _cleanup_fclose_ FILE *f_uplink = NULL;
+        _cleanup_fclose_ FILE *f_stub = NULL;
         int r;
 
         assert(m);
@@ -255,28 +275,44 @@ int manager_write_resolv_conf(Manager *m) {
         if (r < 0)
                 return log_warning_errno(r, "Failed to compile list of search domains: %m");
 
-        r = fopen_temporary_label(PRIVATE_RESOLV_CONF, PRIVATE_RESOLV_CONF, &f, &temp_path);
+        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");
+        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) fchmod(fileno(f_uplink), 0644);
+        (void) fchmod(fileno(f_stub), 0644);
 
-        (void) fchmod(fileno(f), 0644);
-
-        r = write_resolv_conf_contents(f, dns, domains);
+        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");
                 goto fail;
         }
 
-        if (rename(temp_path, PRIVATE_RESOLV_CONF) < 0) {
+        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;
         }
 
+        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;
+        }
+
+        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;
+        }
+
         return 0;
 
 fail:
-        (void) unlink(PRIVATE_RESOLV_CONF);
-        (void) unlink(temp_path);
+        (void) unlink(PRIVATE_UPLINK_RESOLV_CONF);
+        (void) unlink(temp_path_uplink);
+        (void) unlink(PRIVATE_STUB_RESOLV_CONF);
+        (void) unlink(temp_path_stub);
 
         return r;
 }
index 75fa080e4c303cabd72d91571726015006a179f8..6dc1afe764586082b74d1677a439f5bd84c46e89 100644 (file)
@@ -21,7 +21,8 @@
 
 #include "resolved-manager.h"
 
-#define PRIVATE_RESOLV_CONF "/run/systemd/resolve/resolv.conf"
+#define PRIVATE_UPLINK_RESOLV_CONF "/run/systemd/resolve/resolv.conf"
+#define PRIVATE_STUB_RESOLV_CONF "/run/systemd/resolve/stub-resolv.conf"
 
 int manager_read_resolv_conf(Manager *m);
 int manager_write_resolv_conf(Manager *m);
index d6f916bbda8c0c28bc572a8d629a22416bcef3a8..9ff877498c41d66080c724100e3edaf0489306e3 100644 (file)
@@ -117,10 +117,6 @@ int main(int argc, char *argv[]) {
         sd_event_get_exit_code(m->event, &r);
 
 finish:
-        /* systemd-nspawn checks for private resolv.conf to decide whether
-           or not to mount it into the container. So just delete it. */
-        (void) unlink(PRIVATE_RESOLV_CONF);
-
         sd_notify(false,
                   "STOPPING=1\n"
                   "STATUS=Shutting down...");
index 35e3809f576ff55bca25fefe812250467cb68474..df8d42101ca51a419be8b9556daa6b9384d98011 100644 (file)
@@ -14,7 +14,7 @@ m4_ifdef(`HAVE_SMACK_RUN_LABEL',
 t /etc/mtab - - - - security.SMACK64=_
 )m4_dnl
 m4_ifdef(`ENABLE_RESOLVE',
-L! /etc/resolv.conf - - - - ../usr/lib/systemd/resolv.conf
+L! /etc/resolv.conf - - - - ../run/systemd/resolve/stub-resolv.conf
 )m4_dnl
 C /etc/nsswitch.conf - - - -
 m4_ifdef(`HAVE_PAM',