resolved-conf.c revision b938cb902c3b5bca807a94b277672c64d6767886
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen/***
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen This file is part of systemd.
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen Copyright 2014 Tom Gundersen <teg@jklm.no>
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen systemd is free software; you can redistribute it and/or modify it
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen under the terms of the GNU Lesser General Public License as published by
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen (at your option) any later version.
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen systemd is distributed in the hope that it will be useful, but
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen Lesser General Public License for more details.
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen You should have received a copy of the GNU Lesser General Public License
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen ***/
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "alloc-util.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "conf-parser.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "def.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "extract-word.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "parse-util.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "resolved-conf.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "string-util.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenint manager_parse_dns_server(Manager *m, DnsServerType type, const char *string) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen DnsServer *first;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int r;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(m);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(string);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen first = type == DNS_SERVER_FALLBACK ? m->fallback_dns_servers : m->dns_servers;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen for(;;) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen _cleanup_free_ char *word = NULL;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen union in_addr_union addr;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen bool found = false;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen DnsServer *s;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int family;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen r = extract_first_word(&string, &word, NULL, 0);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (r < 0)
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return log_error_errno(r, "Failed to parse resolved dns server syntax \"%s\": %m", string);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (r == 0)
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen break;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen r = in_addr_from_string_auto(word, &family, &addr);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (r < 0) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen log_warning("Ignoring invalid DNS address '%s'", word);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen continue;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen /* Filter out duplicates */
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen LIST_FOREACH(servers, s, first)
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (s->family == family && in_addr_equal(family, &s->address, &addr)) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen found = true;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen break;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (found)
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen continue;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen r = dns_server_new(m, NULL, type, NULL, family, &addr);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (r < 0)
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return r;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return 0;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen}
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenint config_parse_dnsv(
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen const char *unit,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen const char *filename,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen unsigned line,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen const char *section,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen unsigned section_line,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen const char *lvalue,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int ltype,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen const char *rvalue,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen void *data,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen void *userdata) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen Manager *m = userdata;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int r;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(filename);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(lvalue);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(rvalue);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(m);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (isempty(rvalue))
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen /* Empty assignment means clear the list */
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen manager_flush_dns_servers(m, ltype);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen else {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen /* Otherwise, add to the list */
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen r = manager_parse_dns_server(m, ltype, rvalue);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (r < 0) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse DNS server string '%s'. Ignoring.", rvalue);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return 0;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen /* If we have a manual setting, then we stop reading
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen * /etc/resolv.conf */
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (ltype == DNS_SERVER_SYSTEM)
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen m->read_resolv_conf = false;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return 0;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen}
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenint config_parse_support(
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen const char *unit,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen const char *filename,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen unsigned line,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen const char *section,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen unsigned section_line,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen const char *lvalue,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int ltype,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen const char *rvalue,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen void *data,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen void *userdata) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen Support support, *v = data;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int r;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(filename);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(lvalue);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(rvalue);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen support = support_from_string(rvalue);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (support < 0) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen r = parse_boolean(rvalue);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (r < 0) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse support level '%s'. Ignoring.", rvalue);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return 0;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen support = r ? SUPPORT_YES : SUPPORT_NO;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen *v = support;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return 0;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen}
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenint manager_parse_config_file(Manager *m) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(m);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return config_parse_many("/etc/systemd/resolved.conf",
CONF_DIRS_NULSTR("systemd/resolved.conf"),
"Resolve\0",
config_item_perf_lookup, resolved_gperf_lookup,
false, m);
}