/***
This file is part of systemd.
Copyright 2014 Tom Gundersen <teg@jklm.no>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <resolv.h>
#include "alloc-util.h"
#include "dns-domain.h"
#include "fd-util.h"
#include "fileio-label.h"
#include "fileio.h"
#include "ordered-set.h"
#include "resolved-conf.h"
#include "resolved-resolv-conf.h"
#include "string-util.h"
#include "strv.h"
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;
if (r < 0) {
return 0;
goto clear;
}
/* Have we already seen the file? */
if (t == m->resolv_conf_mtime)
return 0;
/* Is it symlinked to our own file? */
return 0;
if (!f) {
return 0;
goto clear;
}
goto clear;
}
const char *a;
char *l;
if (*l == '#' || *l == ';')
continue;
a = first_word(l, "nameserver");
if (a) {
r = manager_add_dns_server_by_string(m, DNS_SERVER_SYSTEM, a);
if (r < 0)
log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
continue;
}
a = first_word(l, "domain");
if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
a = first_word(l, "search");
if (a) {
r = manager_parse_search_domains_and_warn(m, a);
if (r < 0)
log_warning_errno(r, "Failed to parse search domain string '%s', ignoring.", a);
}
}
m->resolv_conf_mtime = t;
/* Flush out all servers and search domains that are still
* marked. Those are then ones that didn't appear in the new
* /etc/resolv.conf */
/* 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);
/* Unconditionally flush the cache when /etc/resolv.conf is
* modified, even if the data it contained was completely
* identical to the previous version we used. We do this
* because altering /etc/resolv.conf is typically done when
* the network configuration changes, and that should be
* enough to flush the global unicast DNS cache. */
if (m->unicast_scope)
return 0;
return r;
}
assert(s);
assert(f);
(void) dns_server_string(s);
if (!s->server_string) {
log_warning("Our of memory, or invalid DNS address. Ignoring server.");
return;
}
fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
(*count) ++;
}
static void write_resolv_conf_search(
const char *domain,
FILE *f,
unsigned *count,
unsigned *length) {
assert(f);
fputs(" # Too many search domains configured, remaining ones ignored.", f);
if (*length <= 256)
fputs(" # Total length of all search domains is too long, remaining ones ignored.", f);
return;
}
(*count) ++;
fputc(' ', f);
}
Iterator i;
fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
"# Third party programs must not access this file directly, but\n"
"# only through the symlink at /etc/resolv.conf. To manage\n"
"# resolv.conf(5) in a different way, replace the symlink by a\n"
"# static file or a different symlink.\n\n", f);
if (ordered_set_isempty(dns))
fputs("# No DNS servers known.\n", f);
else {
unsigned count = 0;
DnsServer *s;
ORDERED_SET_FOREACH(s, dns, i)
write_resolv_conf_server(s, f, &count);
}
if (!ordered_set_isempty(domains)) {
char *domain;
fputs("search", f);
fputs("\n", f);
}
return fflush_and_check(f);
}
int r;
assert(m);
/* Read the system /etc/resolv.conf first */
/* Add the full list to a set, to filter out duplicates */
r = manager_compile_dns_servers(m, &dns);
if (r < 0)
return r;
r = manager_compile_search_domains(m, &domains);
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
goto fail;
r = -errno;
goto fail;
}
return 0;
fail:
(void) unlink(PRIVATE_RESOLV_CONF);
return r;
}