From 091a364c802e34a58f3260c9cb5db9b75c62215c Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Sun, 18 May 2014 22:10:48 +0200 Subject: resolved: add daemon to manage resolv.conf Also remove the equivalent functionality from networkd. --- src/resolve/.gitignore | 2 + src/resolve/Makefile | 1 + src/resolve/resolved-gperf.gperf | 17 +++ src/resolve/resolved-manager.c | 320 +++++++++++++++++++++++++++++++++++++++ src/resolve/resolved.c | 86 +++++++++++ src/resolve/resolved.conf.in | 11 ++ src/resolve/resolved.h | 69 +++++++++ 7 files changed, 506 insertions(+) create mode 100644 src/resolve/.gitignore create mode 120000 src/resolve/Makefile create mode 100644 src/resolve/resolved-gperf.gperf create mode 100644 src/resolve/resolved-manager.c create mode 100644 src/resolve/resolved.c create mode 100644 src/resolve/resolved.conf.in create mode 100644 src/resolve/resolved.h (limited to 'src/resolve') diff --git a/src/resolve/.gitignore b/src/resolve/.gitignore new file mode 100644 index 0000000000..ca3016e6a3 --- /dev/null +++ b/src/resolve/.gitignore @@ -0,0 +1,2 @@ +/resolved-gperf.c +/resolved.conf diff --git a/src/resolve/Makefile b/src/resolve/Makefile new file mode 120000 index 0000000000..d0b0e8e008 --- /dev/null +++ b/src/resolve/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/src/resolve/resolved-gperf.gperf b/src/resolve/resolved-gperf.gperf new file mode 100644 index 0000000000..71e998051a --- /dev/null +++ b/src/resolve/resolved-gperf.gperf @@ -0,0 +1,17 @@ +%{ +#include +#include "conf-parser.h" +#include "resolved.h" +%} +struct ConfigPerfItem; +%null_strings +%language=ANSI-C +%define slot-name section_and_lvalue +%define hash-function-name resolved_gperf_hash +%define lookup-function-name resolved_gperf_lookup +%readonly-tables +%omit-struct-type +%struct-type +%includes +%% +Resolve.DNS, config_parse_dnsv, 0, offsetof(Manager, fallback_dns) diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c new file mode 100644 index 0000000000..ae173991da --- /dev/null +++ b/src/resolve/resolved-manager.c @@ -0,0 +1,320 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2014 Tom Gundersen + + systemd is free software; you can redistribute it and/or modify it + 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 . + ***/ + +#include +#include +#include + +#include "resolved.h" +#include "event-util.h" +#include "network-util.h" +#include "sd-dhcp-lease.h" +#include "dhcp-lease-internal.h" +#include "network-internal.h" +#include "conf-parser.h" +#include "mkdir.h" + +static int set_fallback_dns(Manager *m, const char *string) { + char *word, *state; + size_t length; + int r; + + assert(m); + assert(string); + + FOREACH_WORD_QUOTED(word, length, string, state) { + _cleanup_free_ Address *address = NULL; + Address *tail; + _cleanup_free_ char *addrstr = NULL; + + address = new0(Address, 1); + if (!address) + return -ENOMEM; + + addrstr = strndup(word, length); + if (!addrstr) + return -ENOMEM; + + r = net_parse_inaddr(addrstr, &address->family, &address->in_addr); + if (r < 0) { + log_debug("Ignoring invalid DNS address '%s'", addrstr); + continue; + } + + LIST_FIND_TAIL(addresses, m->fallback_dns, tail); + LIST_INSERT_AFTER(addresses, m->fallback_dns, tail, address); + address = NULL; + } + + return 0; +} + +int config_parse_dnsv( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + Manager *m = userdata; + Address *address; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(m); + + while ((address = m->fallback_dns)) { + LIST_REMOVE(addresses, m->fallback_dns, address); + free(address); + } + + set_fallback_dns(m, rvalue); + + return 0; +} + +static int manager_parse_config_file(Manager *m) { + static const char fn[] = "/etc/systemd/resolved.conf"; + _cleanup_fclose_ FILE *f = NULL; + int r; + + assert(m); + + f = fopen(fn, "re"); + if (!f) { + if (errno == ENOENT) + return 0; + + log_warning("Failed to open configuration file %s: %m", fn); + return -errno; + } + + r = config_parse(NULL, fn, f, "Resolve\0", config_item_perf_lookup, + (void*) resolved_gperf_lookup, false, false, m); + if (r < 0) + log_warning("Failed to parse configuration file: %s", strerror(-r)); + + return r; +} + +int manager_new(Manager **ret) { + _cleanup_manager_free_ Manager *m = NULL; + int r; + + m = new0(Manager, 1); + if (!m) + return -ENOMEM; + + r = set_fallback_dns(m, DNS_SERVERS); + if (r < 0) + return r; + + r = manager_parse_config_file(m); + if (r < 0) + return r; + + r = sd_event_default(&m->event); + if (r < 0) + return r; + + sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); + sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); + + sd_event_set_watchdog(m->event, true); + + *ret = m; + m = NULL; + + return 0; +} + +void manager_free(Manager *m) { + Address *address; + + if (!m) + return; + + sd_event_unref(m->event); + + while ((address = m->fallback_dns)) { + LIST_REMOVE(addresses, m->fallback_dns, address); + free(address); + } + + free(m); +} + +static void append_dns(FILE *f, void *dns, unsigned char family, unsigned *count) { + char buf[INET6_ADDRSTRLEN]; + const char *address; + + assert(f); + assert(dns); + assert(count); + + address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN); + if (!address) { + log_warning("Invalid DNS address. Ignoring."); + return; + } + + if (*count == MAXNS) + fputs("# Too many DNS servers configured, the following entries " + "may be ignored\n", f); + + fprintf(f, "nameserver %s\n", address); + + (*count) ++; +} + +int manager_update_resolv_conf(Manager *m) { + _cleanup_free_ char *temp_path = NULL; + _cleanup_fclose_ FILE *f = NULL; + _cleanup_free_ unsigned *indices = NULL; + Address *address; + unsigned count = 0; + int n, r, i; + + assert(m); + + r = fopen_temporary("/run/systemd/network/resolv.conf", &f, &temp_path); + if (r < 0) + return r; + + fchmod(fileno(f), 0644); + + 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); + + n = sd_network_get_ifindices(&indices); + if (n < 0) + n = 0; + + for (i = 0; i < n; i++) { + _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL; + struct in_addr *nameservers; + struct in6_addr *nameservers6; + size_t nameservers_size; + + r = sd_network_dhcp_use_dns(indices[i]); + if (r > 0) { + r = sd_network_get_dhcp_lease(indices[i], &lease); + if (r >= 0) { + r = sd_dhcp_lease_get_dns(lease, &nameservers, &nameservers_size); + if (r >= 0) { + unsigned j; + + for (j = 0; j < nameservers_size; j++) + append_dns(f, &nameservers[j], AF_INET, &count); + } + } + } + + r = sd_network_get_dns(indices[i], &nameservers, &nameservers_size); + if (r >= 0) { + unsigned j; + + for (j = 0; j < nameservers_size; j++) + append_dns(f, &nameservers[j], AF_INET, &count); + + free(nameservers); + } + + r = sd_network_get_dns6(indices[i], &nameservers6, &nameservers_size); + if (r >= 0) { + unsigned j; + + for (j = 0; j < nameservers_size; j++) + append_dns(f, &nameservers6[j], AF_INET6, &count); + + free(nameservers6); + } + } + + LIST_FOREACH(addresses, address, m->fallback_dns) + append_dns(f, &address->in_addr, address->family, &count); + + fflush(f); + + if (ferror(f) || rename(temp_path, "/run/systemd/network/resolv.conf") < 0) { + r = -errno; + unlink("/run/systemd/network/resolv.conf"); + unlink(temp_path); + return r; + } + + return 0; +} + +static int manager_network_event_handler(sd_event_source *s, int fd, uint32_t revents, + void *userdata) { + Manager *m = userdata; + int r; + + assert(m); + + r = manager_update_resolv_conf(m); + if (r < 0) + log_warning("Could not update resolv.conf: %s", strerror(-r)); + + sd_network_monitor_flush(m->network_monitor); + + return 0; +} + +int manager_network_monitor_listen(Manager *m) { + _cleanup_event_source_unref_ sd_event_source *event_source = NULL; + _cleanup_network_monitor_unref_ sd_network_monitor *monitor = NULL; + int r, fd, events; + + r = sd_network_monitor_new(NULL, &monitor); + if (r < 0) + return r; + + fd = sd_network_monitor_get_fd(monitor); + if (fd < 0) + return fd; + + events = sd_network_monitor_get_events(monitor); + if (events < 0) + return events; + + r = sd_event_add_io(m->event, &event_source, fd, events, + &manager_network_event_handler, m); + if (r < 0) + return r; + + m->network_monitor = monitor; + m->network_event_source = event_source; + monitor = NULL; + event_source = NULL; + + return 0; +} diff --git a/src/resolve/resolved.c b/src/resolve/resolved.c new file mode 100644 index 0000000000..82f43d6803 --- /dev/null +++ b/src/resolve/resolved.c @@ -0,0 +1,86 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2014 Tom Gundersen + + systemd is free software; you can redistribute it and/or modify it + 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 . +***/ + +#include "sd-event.h" +#include "sd-daemon.h" + +#include "resolved.h" + +#include "mkdir.h" + +int main(int argc, char *argv[]) { + _cleanup_manager_free_ Manager *m = NULL; + int r; + + log_set_target(LOG_TARGET_AUTO); + log_parse_environment(); + log_open(); + + umask(0022); + + if (argc != 1) { + log_error("This program takes no arguments."); + r = -EINVAL; + goto out; + } + + /* Always create the directory where resolv.conf will live */ + r = mkdir_label("/run/systemd/network", 0755); + if (r < 0) + log_error("Could not create runtime directory: %s", + strerror(-r)); + + r = manager_new(&m); + if (r < 0) { + log_error("Could not create manager: %s", strerror(-r)); + goto out; + } + + r = manager_network_monitor_listen(m); + if (r < 0) { + log_error("Could not listen for network events: %s", strerror(-r)); + goto out; + } + + /* write out default resolv.conf to avoid a + * dangling symlink */ + r = manager_update_resolv_conf(m); + if (r < 0) { + log_error("Could not create resolv.conf: %s", strerror(-r)); + goto out; + } + + sd_notify(false, + "READY=1\n" + "STATUS=Processing requests..."); + + r = sd_event_loop(m->event); + if (r < 0) { + log_error("Event loop failed: %s", strerror(-r)); + goto out; + } + +out: + sd_notify(false, + "STATUS=Shutting down..."); + + return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/src/resolve/resolved.conf.in b/src/resolve/resolved.conf.in new file mode 100644 index 0000000000..a2391954aa --- /dev/null +++ b/src/resolve/resolved.conf.in @@ -0,0 +1,11 @@ +# This file is part of systemd. +# +# systemd is free software; you can redistribute it and/or modify it +# 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. +# +# See resolved.conf(5) for details + +[Resolve] +#DNS=@DNS_SERVERS@ diff --git a/src/resolve/resolved.h b/src/resolve/resolved.h new file mode 100644 index 0000000000..984edc76c1 --- /dev/null +++ b/src/resolve/resolved.h @@ -0,0 +1,69 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2013 Tom Gundersen + + systemd is free software; you can redistribute it and/or modify it + 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 . +***/ + +#pragma once + +#include "sd-event.h" +#include "sd-network.h" + +#include "util.h" +#include "list.h" + +typedef struct Address Address; +typedef struct Manager Manager; + +struct Address { + unsigned char family; + + union { + struct in_addr in; + struct in6_addr in6; + } in_addr; + + LIST_FIELDS(Address, addresses); +}; + +struct Manager { + sd_event *event; + + LIST_HEAD(Address, fallback_dns); + + /* network */ + sd_event_source *network_event_source; + sd_network_monitor *network_monitor; +}; + +/* Manager */ + +int manager_new(Manager **ret); +void manager_free(Manager *m); + +int manager_update_resolv_conf(Manager *m); +int manager_network_monitor_listen(Manager *m); + +DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free); +#define _cleanup_manager_free_ _cleanup_(manager_freep) + +const struct ConfigPerfItem* resolved_gperf_lookup(const char *key, unsigned length); + +int config_parse_dnsv(const char *unit, const char *filename, unsigned line, + const char *section, unsigned section_line, const char *lvalue, + int ltype, const char *rvalue, void *data, void *userdata); -- cgit v1.2.3-54-g00ecf