diff options
Diffstat (limited to 'src/resolve/resolved-resolv-conf.c')
-rw-r--r-- | src/resolve/resolved-resolv-conf.c | 149 |
1 files changed, 115 insertions, 34 deletions
diff --git a/src/resolve/resolved-resolv-conf.c b/src/resolve/resolved-resolv-conf.c index e3d6a33409..bad04d6a29 100644 --- a/src/resolve/resolved-resolv-conf.c +++ b/src/resolve/resolved-resolv-conf.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. @@ -18,6 +19,7 @@ ***/ #include <resolv.h> +#include <stdio_ext.h> #include "alloc-util.h" #include "dns-domain.h" @@ -31,11 +33,41 @@ #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 sever address */ +#define PRIVATE_STATIC_RESOLV_CONF ROOTLIBEXECDIR "/resolv.conf" + +static bool file_is_our_own(const struct stat *st) { + const char *path; + + assert(st); + + FOREACH_STRING(path, + PRIVATE_UPLINK_RESOLV_CONF, + 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) + return true; + } + + return false; +} + int manager_read_resolv_conf(Manager *m) { _cleanup_fclose_ FILE *f = NULL; - struct stat st, own; + struct stat st; char line[LINE_MAX]; - usec_t t; int r; assert(m); @@ -56,14 +88,10 @@ int manager_read_resolv_conf(Manager *m) { } /* Have we already seen the file? */ - t = timespec_load(&st.st_mtim); - if (t == m->resolv_conf_mtime) + if (timespec_load(&st.st_mtim) == m->resolv_conf_mtime) return 0; - /* Is it symlinked to our own file? */ - if (stat(PRIVATE_RESOLV_CONF, &own) >= 0 && - st.st_dev == own.st_dev && - st.st_ino == own.st_ino) + if (file_is_our_own(&st)) return 0; f = fopen("/etc/resolv.conf", "re"); @@ -80,6 +108,9 @@ int manager_read_resolv_conf(Manager *m) { goto clear; } + if (file_is_our_own(&st)) + return 0; + dns_server_mark_all(m->dns_servers); dns_search_domain_mark_all(m->search_domains); @@ -110,7 +141,7 @@ int manager_read_resolv_conf(Manager *m) { } } - m->resolv_conf_mtime = t; + m->resolv_conf_mtime = timespec_load(&st.st_mtim); /* Flush out all servers and search domains that are still * marked. Those are then ones that didn't appear in the new @@ -171,7 +202,7 @@ static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) { } if (*count == MAXNS) - fputs_unlocked("# Too many DNS servers configured, the following entries may be ignored.\n", f); + fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f); (*count)++; fprintf(f, "nameserver %s\n", dns_server_string(s)); @@ -187,39 +218,43 @@ static void write_resolv_conf_search( assert(domains); assert(f); - fputs_unlocked("search", f); + fputs("search", f); ORDERED_SET_FOREACH(domain, domains, i) { if (++count > MAXDNSRCH) { - fputs_unlocked("\n# Too many search domains configured, remaining ones ignored.", f); + fputs("\n# Too many search domains configured, remaining ones ignored.", f); break; } length += strlen(domain) + 1; if (length > 256) { - fputs_unlocked("\n# Total length of all search domains is too long, remaining ones ignored.", f); + fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f); break; } - fputc_unlocked(' ', f); - fputs_unlocked(domain, f); + fputc(' ', f); + fputs(domain, f); } - fputs_unlocked("\n", f); + fputs("\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" - "# This is a dynamic resolv.conf file for connecting local clients directly to\n" - "# all known DNS servers.\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", 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 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" + "#\n" + "# See man:systemd-resolved.service(8) for details about the supported modes of\n" + "# operation for /etc/resolv.conf.\n" + "\n", f); if (ordered_set_isempty(dns)) - fputs_unlocked("# No DNS servers known.\n", f); + fputs("# No DNS servers known.\n", f); else { unsigned count = 0; DnsServer *s; @@ -234,11 +269,36 @@ 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_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 \"systemd-resolve --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); + + 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, *temp_path_stub = NULL; + _cleanup_fclose_ FILE *f_uplink = NULL, *f_stub = NULL; int r; assert(m); @@ -255,28 +315,49 @@ 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"); - (void) fchmod(fileno(f), 0644); + (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_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; } |