summaryrefslogtreecommitdiff
path: root/src/dns-manager/nm-dns-systemd-resolved.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dns-manager/nm-dns-systemd-resolved.c')
-rw-r--r--src/dns-manager/nm-dns-systemd-resolved.c403
1 files changed, 403 insertions, 0 deletions
diff --git a/src/dns-manager/nm-dns-systemd-resolved.c b/src/dns-manager/nm-dns-systemd-resolved.c
new file mode 100644
index 0000000000..f7c90a1b7c
--- /dev/null
+++ b/src/dns-manager/nm-dns-systemd-resolved.c
@@ -0,0 +1,403 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+/*
+ * Copyright (C) 2010 Dan Williams <dcbw@redhat.com>
+ * Copyright (C) 2016 Sjoerd Simons <sjoerd@luon.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include "nm-default.h"
+#include "nm-dns-systemd-resolved.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <arpa/inet.h>
+#include <sys/stat.h>
+#include <linux/if.h>
+
+#include "nm-core-internal.h"
+#include "nm-platform.h"
+#include "nm-utils.h"
+#include "nm-ip4-config.h"
+#include "nm-ip6-config.h"
+#include "nm-bus-manager.h"
+#include "nm-manager.h"
+#include "nm-device.h"
+#include "NetworkManagerUtils.h"
+
+G_DEFINE_TYPE (NMDnsSystemdResolved, nm_dns_systemd_resolved, NM_TYPE_DNS_PLUGIN)
+
+#define NM_DNS_SYSTEMD_RESOLVED_GET_PRIVATE(o) \
+ (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DNS_SYSTEMD_RESOLVED, \
+ NMDnsSystemdResolvedPrivate))
+
+#define SYSTEMD_RESOLVED_DBUS_SERVICE "org.freedesktop.resolve1"
+#define SYSTEMD_RESOLVED_DBUS_PATH "/org/freedesktop/resolve1"
+
+typedef struct {
+ int ifindex;
+ GList *configs;
+} InterfaceConfig;
+
+typedef struct {
+ GDBusProxy *resolve;
+ GCancellable *init_cancellable;
+ GCancellable *update_cancellable;
+ GQueue dns_updates;
+ GQueue domain_updates;
+} NMDnsSystemdResolvedPrivate;
+
+/*****************************************************************************/
+
+#define _NMLOG_DOMAIN LOGD_DNS
+#define _NMLOG_PREFIX_NAME "systemd-resolved"
+#define _NMLOG(level, ...) \
+ G_STMT_START { \
+ nm_log ((level), _NMLOG_DOMAIN, \
+ "%s[%p]: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
+ _NMLOG_PREFIX_NAME, \
+ (self) \
+ _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
+ } G_STMT_END
+
+/*****************************************************************************/
+
+static void
+call_done (GObject *source, GAsyncResult *r, gpointer user_data)
+{
+ GVariant *v;
+ GError *error = NULL;
+ NMDnsSystemdResolved *self = NM_DNS_SYSTEMD_RESOLVED (user_data);
+
+ v = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), r, &error);
+
+ if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+ return;
+
+ if (error != NULL) {
+ _LOGW ("Failed: %s\n", error->message);
+ g_error_free (error);
+ }
+}
+
+static void
+add_interface_configuration (NMDnsSystemdResolved *self,
+ GArray *interfaces,
+ const NMDnsIPConfigData *data)
+{
+ int i;
+ InterfaceConfig *ic = NULL;
+ int ifindex;
+ NMDevice *device;
+
+ if (NM_IS_IP4_CONFIG (data->config))
+ ifindex = nm_ip4_config_get_ifindex (data->config);
+ else if (NM_IS_IP6_CONFIG (data->config))
+ ifindex = nm_ip6_config_get_ifindex (data->config);
+ else
+ g_return_if_reached ();
+
+ device = nm_manager_get_device_by_ifindex (nm_manager_get (), ifindex);
+
+ if (!nm_device_get_managed (device, FALSE))
+ return;
+
+ for (i = 0; i < interfaces->len; i++) {
+ InterfaceConfig *tic = &g_array_index (interfaces, InterfaceConfig, i);
+ if (ifindex == tic->ifindex) {
+ ic = tic;
+ break;
+ }
+ }
+
+ if (!ic) {
+ g_array_set_size (interfaces, interfaces->len + 1);
+ ic = &g_array_index (interfaces, InterfaceConfig,
+ interfaces->len - 1);
+ ic->ifindex = ifindex;
+ }
+
+ ic->configs = g_list_append (ic->configs, data->config);
+}
+
+static void
+update_add_ip6_config (NMDnsSystemdResolved *self,
+ GVariantBuilder *dns,
+ GVariantBuilder *domains,
+ const NMIP6Config *config) {
+ int i;
+
+ for (i = 0 ; i < nm_ip6_config_get_num_nameservers (config); i++) {
+ const struct in6_addr *ip;
+
+ g_variant_builder_open (dns, G_VARIANT_TYPE ("(iay)"));
+ g_variant_builder_add (dns, "i", AF_INET6);
+ ip = nm_ip6_config_get_nameserver (config, i),
+
+ g_variant_builder_add_value (dns, g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, ip, 16, 1));
+ g_variant_builder_close (dns);
+ }
+
+ for (i = 0; i < nm_ip6_config_get_num_searches (config); i++) {
+ g_variant_builder_add (domains, "(sb)",
+ nm_ip6_config_get_search(config, i),
+ FALSE);
+ }
+
+ for (i = 0; i < nm_ip6_config_get_num_domains (config); i++) {
+ g_variant_builder_add (domains, "(sb)",
+ nm_ip6_config_get_domain (config, i),
+ TRUE);
+ }
+}
+
+static void
+update_add_ip4_config (NMDnsSystemdResolved *self,
+ GVariantBuilder *dns,
+ GVariantBuilder *domains,
+ const NMIP4Config *config) {
+ int i;
+
+ for (i = 0 ; i < nm_ip4_config_get_num_nameservers (config); i++) {
+ guint32 ns;
+
+ g_variant_builder_open (dns, G_VARIANT_TYPE ("(iay)"));
+ g_variant_builder_add (dns, "i", AF_INET);
+ ns = nm_ip4_config_get_nameserver (config, i),
+
+ g_variant_builder_add_value (dns, g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &ns, 4, 1));
+ g_variant_builder_close (dns);
+ }
+
+ for (i = 0; i < nm_ip4_config_get_num_searches (config); i++) {
+ g_variant_builder_add (domains, "(sb)",
+ nm_ip4_config_get_search (config, i),
+ FALSE);
+ }
+
+ for (i = 0; i < nm_ip4_config_get_num_domains (config); i++) {
+ g_variant_builder_add (domains, "(sb)",
+ nm_ip4_config_get_domain (config, i),
+ TRUE);
+ }
+}
+
+static void
+free_pending_updates (NMDnsSystemdResolved *self)
+{
+ NMDnsSystemdResolvedPrivate *priv = NM_DNS_SYSTEMD_RESOLVED_GET_PRIVATE (self);
+ GVariant *v;
+
+ while ((v = g_queue_pop_head(&priv->dns_updates)) != NULL)
+ g_variant_unref (v);
+
+ while ((v = g_queue_pop_head(&priv->domain_updates)) != NULL)
+ g_variant_unref (v);
+}
+
+static void
+prepare_one_interface (NMDnsSystemdResolved *self, InterfaceConfig *ic)
+{
+ NMDnsSystemdResolvedPrivate *priv = NM_DNS_SYSTEMD_RESOLVED_GET_PRIVATE (self);
+ GVariantBuilder dns, domains;
+ GList *l;
+
+ g_variant_builder_init (&dns, G_VARIANT_TYPE ("(ia(iay))"));
+ g_variant_builder_add (&dns, "i", ic->ifindex);
+ g_variant_builder_open (&dns, G_VARIANT_TYPE ("a(iay)"));
+
+ g_variant_builder_init (&domains, G_VARIANT_TYPE ("(ia(sb))"));
+ g_variant_builder_add (&domains, "i", ic->ifindex);
+ g_variant_builder_open (&domains, G_VARIANT_TYPE ("a(sb)"));
+
+ for (l = ic->configs ; l != NULL ; l = g_list_next (l)) {
+ if (NM_IS_IP4_CONFIG (l->data))
+ update_add_ip4_config (self, &dns, &domains, l->data);
+ else if (NM_IS_IP6_CONFIG (l->data))
+ update_add_ip6_config (self, &dns, &domains, l->data);
+ else
+ g_assert_not_reached ();
+ }
+ g_variant_builder_close (&dns);
+ g_variant_builder_close (&domains);
+
+ g_queue_push_tail (&priv->dns_updates,
+ g_variant_ref_sink(g_variant_builder_end (&dns)));
+ g_queue_push_tail (&priv->domain_updates,
+ g_variant_ref_sink(g_variant_builder_end (&domains)));
+}
+
+static void
+send_updates (NMDnsSystemdResolved *self)
+{
+ NMDnsSystemdResolvedPrivate *priv = NM_DNS_SYSTEMD_RESOLVED_GET_PRIVATE (self);
+ GVariant *v;
+
+ if (!priv->resolve)
+ return;
+
+ while ((v = g_queue_pop_head(&priv->dns_updates)) != NULL) {
+ g_dbus_proxy_call (priv->resolve, "SetLinkDNS", v,
+ G_DBUS_CALL_FLAGS_NONE,
+ -1, priv->update_cancellable, call_done, self);
+ g_variant_unref (v);
+ }
+
+ while ((v = g_queue_pop_head(&priv->domain_updates)) != NULL) {
+ g_dbus_proxy_call (priv->resolve, "SetLinkDomains", v,
+ G_DBUS_CALL_FLAGS_NONE,
+ -1, priv->update_cancellable, call_done, self);
+ g_variant_unref (v);
+ }
+}
+
+static gboolean
+update (NMDnsPlugin *plugin,
+ const NMDnsIPConfigData **configs,
+ const NMGlobalDnsConfig *global_config,
+ const char *hostname)
+{
+ NMDnsSystemdResolved *self = NM_DNS_SYSTEMD_RESOLVED (plugin);
+ NMDnsSystemdResolvedPrivate *priv = NM_DNS_SYSTEMD_RESOLVED_GET_PRIVATE (self);
+ GArray *interfaces = g_array_new (TRUE, TRUE, sizeof(InterfaceConfig));
+ const NMDnsIPConfigData **c;
+ int i;
+
+ for (c = configs; *c != NULL; c++)
+ add_interface_configuration (self, interfaces, *c);
+
+ free_pending_updates (self);
+ g_clear_object (&priv->update_cancellable);
+
+ priv->update_cancellable = g_cancellable_new ();
+
+ for (i = 0; i < interfaces->len; i++) {
+ InterfaceConfig *ic = &g_array_index (interfaces, InterfaceConfig, i);
+ prepare_one_interface (self, ic);
+ g_list_free (ic->configs);
+ }
+
+ g_array_free (interfaces, TRUE);
+
+ send_updates (self);
+
+ return TRUE;
+}
+
+/****************************************************************/
+
+static gboolean
+is_caching (NMDnsPlugin *plugin)
+{
+ return TRUE;
+}
+
+static const char *
+get_name (NMDnsPlugin *plugin)
+{
+ return "systemd-resolved";
+}
+
+/****************************************************************/
+
+NMDnsPlugin *
+nm_dns_systemd_resolved_new (void)
+{
+ return g_object_new (NM_TYPE_DNS_SYSTEMD_RESOLVED, NULL);
+}
+
+static void
+resolved_proxy_created (GObject *source, GAsyncResult *r, gpointer user_data)
+{
+ NMDnsSystemdResolved *self = NM_DNS_SYSTEMD_RESOLVED (user_data);
+ NMDnsSystemdResolvedPrivate *priv = NM_DNS_SYSTEMD_RESOLVED_GET_PRIVATE (self);
+ GError *error = NULL;
+
+ g_clear_object (&priv->init_cancellable);
+
+ priv->resolve = g_dbus_proxy_new_finish(r, &error);
+
+ if (priv->resolve == NULL) {
+ _LOGW ("failed to connect to resolved via DBus: %s", error->message);
+ g_signal_emit_by_name (self, NM_DNS_PLUGIN_FAILED);
+ g_error_free (error);
+ return;
+ }
+
+ send_updates(self);
+}
+
+
+static void
+nm_dns_systemd_resolved_init (NMDnsSystemdResolved *self)
+{
+ NMDnsSystemdResolvedPrivate *priv = NM_DNS_SYSTEMD_RESOLVED_GET_PRIVATE (self);
+ NMBusManager *dbus_mgr;
+ GDBusConnection *connection;
+
+ g_queue_init(&priv->dns_updates);
+ g_queue_init(&priv->domain_updates);
+
+ dbus_mgr = nm_bus_manager_get ();
+ g_return_if_fail (dbus_mgr);
+
+ connection = nm_bus_manager_get_connection (dbus_mgr);
+ g_return_if_fail (connection);
+
+ priv->init_cancellable = g_cancellable_new ();
+ g_dbus_proxy_new (connection,
+ G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
+ G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+ NULL,
+ SYSTEMD_RESOLVED_DBUS_SERVICE,
+ SYSTEMD_RESOLVED_DBUS_PATH,
+ SYSTEMD_RESOLVED_DBUS_SERVICE ".Manager",
+ priv->init_cancellable,
+ resolved_proxy_created,
+ self);
+}
+
+static void
+dispose (GObject *object)
+{
+ NMDnsSystemdResolved *self = NM_DNS_SYSTEMD_RESOLVED (object);
+ NMDnsSystemdResolvedPrivate *priv = NM_DNS_SYSTEMD_RESOLVED_GET_PRIVATE (self);
+
+ free_pending_updates (self);
+ g_clear_object (&priv->resolve);
+ nm_clear_g_cancellable (&priv->init_cancellable);
+ nm_clear_g_cancellable (&priv->update_cancellable);
+
+ G_OBJECT_CLASS (nm_dns_systemd_resolved_parent_class)->dispose (object);
+}
+
+static void
+nm_dns_systemd_resolved_class_init (NMDnsSystemdResolvedClass *dns_class)
+{
+ NMDnsPluginClass *plugin_class = NM_DNS_PLUGIN_CLASS (dns_class);
+ GObjectClass *object_class = G_OBJECT_CLASS (dns_class);
+
+ g_type_class_add_private (dns_class, sizeof (NMDnsSystemdResolvedPrivate));
+
+ object_class->dispose = dispose;
+
+ plugin_class->is_caching = is_caching;
+ plugin_class->update = update;
+ plugin_class->get_name = get_name;
+}
+