resolved-resolv-conf.c revision a51c10485af349eb15faa4d1a63b9818bcf3e589
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
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"
int manager_read_resolv_conf(Manager *m) {
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) {
r = 0;
else
goto clear;
}
/* Have we already seen the file? */
if (t == m->resolv_conf_mtime)
return 0;
m->resolv_conf_mtime = t;
/* Is it symlinked to our own file? */
r = 0;
goto clear;
}
if (!f) {
r = 0;
else
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);
}
}
/* 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);
return 0;
return r;
}
_cleanup_free_ char *t = NULL;
int r;
assert(s);
assert(f);
if (r < 0) {
log_warning_errno(r, "Invalid DNS address. Ignoring: %m");
return;
}
fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
(*count) ++;
fprintf(f, "nameserver %s\n", t);
}
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 manager_write_resolv_conf(Manager *m) {
static const char path[] = "/run/systemd/resolve/resolv.conf";
DnsSearchDomain *d;
DnsServer *s;
Iterator i;
Link *l;
int r;
assert(m);
/* Read the system /etc/resolv.conf first */
/* Add the full list to a set, to filter out duplicates */
if (!dns)
return -ENOMEM;
if (!domains)
return -ENOMEM;
/* First add the system-wide servers and domains */
r = ordered_set_put(dns, s);
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
/* Then, add the per-link servers and domains */
HASHMAP_FOREACH(l, m->links, i) {
r = ordered_set_put(dns, s);
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
}
/* If we found nothing, add the fallback servers */
if (ordered_set_isempty(dns)) {
r = ordered_set_put(dns, s);
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
}
if (r < 0)
return r;
if (r < 0)
goto fail;
r = -errno;
goto fail;
}
return 0;
fail:
return r;
}