diff options
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | docs/libnm/Makefile.am | 1 | ||||
-rw-r--r-- | libnm-core/nm-dbus-interface.h | 3 | ||||
-rw-r--r-- | libnm/libnm.ver | 10 | ||||
-rw-r--r-- | libnm/nm-client.c | 199 | ||||
-rw-r--r-- | libnm/nm-client.h | 29 | ||||
-rw-r--r-- | libnm/nm-dns-manager.c | 448 | ||||
-rw-r--r-- | libnm/nm-dns-manager.h | 72 | ||||
-rwxr-xr-x | tools/test-networkmanager-service.py | 37 |
9 files changed, 800 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am index d8e5104e9c..cc53b3deb0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -696,6 +696,7 @@ libnm_lib_h_priv = \ libnm/nm-device-private.h \ libnm/nm-dhcp4-config.h \ libnm/nm-dhcp6-config.h \ + libnm/nm-dns-manager.h \ libnm/nm-ip4-config.h \ libnm/nm-ip6-config.h \ libnm/nm-manager.h \ @@ -728,6 +729,7 @@ libnm_lib_c_real = \ libnm/nm-dhcp-config.c \ libnm/nm-dhcp4-config.c \ libnm/nm-dhcp6-config.c \ + libnm/nm-dns-manager.c \ libnm/nm-ip-config.c \ libnm/nm-ip4-config.c \ libnm/nm-ip6-config.c \ diff --git a/docs/libnm/Makefile.am b/docs/libnm/Makefile.am index 1aae42f53f..3e0eca49eb 100644 --- a/docs/libnm/Makefile.am +++ b/docs/libnm/Makefile.am @@ -42,6 +42,7 @@ IGNORE_HFILES= \ nm-device-private.h \ nm-dhcp4-config.h \ nm-dhcp6-config.h \ + nm-dns-manager.h \ nm-ip4-config.h \ nm-ip6-config.h \ nm-manager.h \ diff --git a/libnm-core/nm-dbus-interface.h b/libnm-core/nm-dbus-interface.h index ecb6e10620..69860ca4c4 100644 --- a/libnm-core/nm-dbus-interface.h +++ b/libnm-core/nm-dbus-interface.h @@ -83,6 +83,9 @@ #define NM_DBUS_INTERFACE_SECRET_AGENT NM_DBUS_INTERFACE ".SecretAgent" #define NM_DBUS_PATH_SECRET_AGENT "/org/freedesktop/NetworkManager/SecretAgent" +#define NM_DBUS_INTERFACE_DNS_MANAGER "org.freedesktop.NetworkManager.DnsManager" +#define NM_DBUS_PATH_DNS_MANAGER "/org/freedesktop/NetworkManager/DnsManager" + /** * NMCapability: * @NM_CAPABILITY_TEAM: Teams can be managed diff --git a/libnm/libnm.ver b/libnm/libnm.ver index b07b1d5672..cd4978f88b 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -1087,7 +1087,17 @@ global: libnm_1_6_0 { global: nm_capability_get_type; + nm_client_get_dns_configuration; + nm_client_get_dns_mode; + nm_client_get_dns_rc_manager; nm_connection_get_setting_proxy; + nm_dns_entry_get_domains; + nm_dns_entry_get_interface; + nm_dns_entry_get_nameservers; + nm_dns_entry_get_priority; + nm_dns_entry_get_type; + nm_dns_entry_get_vpn; + nm_dns_entry_unref; nm_setting_connection_get_autoconnect_retries; nm_setting_proxy_get_type; nm_setting_proxy_new; diff --git a/libnm/nm-client.c b/libnm/nm-client.c index 8d14d50e1b..4b31236e39 100644 --- a/libnm/nm-client.c +++ b/libnm/nm-client.c @@ -26,6 +26,7 @@ #include "nm-utils.h" #include "nm-client.h" #include "nm-manager.h" +#include "nm-dns-manager.h" #include "nm-remote-settings.h" #include "nm-device-ethernet.h" #include "nm-device-wifi.h" @@ -40,6 +41,7 @@ #include "introspection/org.freedesktop.NetworkManager.h" #include "introspection/org.freedesktop.NetworkManager.Device.Wireless.h" #include "introspection/org.freedesktop.NetworkManager.Device.h" +#include "introspection/org.freedesktop.NetworkManager.DnsManager.h" #include "introspection/org.freedesktop.NetworkManager.Settings.h" #include "introspection/org.freedesktop.NetworkManager.Settings.Connection.h" #include "introspection/org.freedesktop.NetworkManager.VPN.Connection.h" @@ -88,6 +90,7 @@ G_DEFINE_TYPE_WITH_CODE (NMClient, nm_client, G_TYPE_OBJECT, typedef struct { NMManager *manager; NMRemoteSettings *settings; + NMDnsManager *dns_manager; GDBusObjectManager *object_manager; GCancellable *new_object_manager_cancellable; } NMClientPrivate; @@ -115,6 +118,9 @@ enum { PROP_HOSTNAME, PROP_CAN_MODIFY, PROP_METERED, + PROP_DNS_MODE, + PROP_DNS_RC_MANAGER, + PROP_DNS_CONFIGURATION, LAST_PROP }; @@ -1717,6 +1723,85 @@ nm_client_reload_connections_finish (NMClient *client, /*****************************************************************************/ /** + * nm_client_get_dns_mode: + * @client: the #NMClient + * + * Gets the current DNS processing mode. + * + * Return value: the DNS processing mode, or %NULL in case the + * value is not available. + * + * Since: 1.6 + **/ +const char * +nm_client_get_dns_mode (NMClient *client) +{ + NMClientPrivate *priv; + + g_return_val_if_fail (NM_IS_CLIENT (client), NULL); + priv = NM_CLIENT_GET_PRIVATE (client); + + if (priv->dns_manager) + return nm_dns_manager_get_mode (priv->dns_manager); + else + return NULL; +} + +/** + * nm_client_get_dns_rc_manager: + * @client: the #NMClient + * + * Gets the current DNS resolv.conf manager. + * + * Return value: the resolv.conf manager or %NULL in case the + * value is not available. + * + * Since: 1.6 + **/ +const char * +nm_client_get_dns_rc_manager (NMClient *client) +{ + NMClientPrivate *priv; + + g_return_val_if_fail (NM_IS_CLIENT (client), NULL); + priv = NM_CLIENT_GET_PRIVATE (client); + + if (priv->dns_manager) + return nm_dns_manager_get_rc_manager (priv->dns_manager); + else + return NULL; +} + +/** + * nm_client_get_dns_configuration: + * @client: a #NMClient + * + * Gets the current DNS configuration + * + * Returns: (transfer none) (element-type NMDnsEntry): a #GPtrArray + * containing #NMDnsEntry elements or %NULL in case the value is not + * available. The returned array is owned by the #NMClient object + * and should not be modified. + * + * Since: 1.6 + **/ +const GPtrArray * +nm_client_get_dns_configuration (NMClient *client) +{ + NMClientPrivate *priv; + + g_return_val_if_fail (NM_IS_CLIENT (client), NULL); + priv = NM_CLIENT_GET_PRIVATE (client); + + if (priv->dns_manager) + return nm_dns_manager_get_configuration (priv->dns_manager); + else + return NULL; +} + +/*****************************************************************************/ + +/** * nm_client_new: * @cancellable: a #GCancellable, or %NULL * @error: location for a #GError, or %NULL @@ -1881,6 +1966,22 @@ manager_active_connection_removed (NMManager *manager, g_signal_emit (client, signals[ACTIVE_CONNECTION_REMOVED], 0, active_connection); } +static void +dns_notify (GObject *object, + GParamSpec *pspec, + gpointer client) +{ + char pname[128]; + + if (NM_IN_STRSET (pspec->name, + NM_DNS_MANAGER_MODE, + NM_DNS_MANAGER_RC_MANAGER, + NM_DNS_MANAGER_CONFIGURATION)) { + nm_sprintf_buf (pname, "dns-%s", pspec->name); + g_object_notify (client, pname); + } +} + /****************************************************************/ /* Object Initialization */ /****************************************************************/ @@ -1909,6 +2010,8 @@ proxy_type (GDBusObjectManagerClient *manager, return NMDBUS_TYPE_SETTINGS_CONNECTION_PROXY; else if (strcmp (interface_name, NM_DBUS_INTERFACE_SETTINGS) == 0) return NMDBUS_TYPE_SETTINGS_PROXY; + else if (strcmp (interface_name, NM_DBUS_INTERFACE_DNS_MANAGER) == 0) + return NMDBUS_TYPE_DNS_MANAGER_PROXY; else if (strcmp (interface_name, NM_DBUS_INTERFACE_VPN_CONNECTION) == 0) return NMDBUS_TYPE_VPN_CONNECTION_PROXY; @@ -1988,6 +2091,8 @@ obj_nm_for_gdbus_object (GDBusObject *object, GDBusObjectManager *object_manager type = NM_TYPE_REMOTE_CONNECTION; else if (strcmp (ifname, NM_DBUS_INTERFACE_SETTINGS) == 0) type = NM_TYPE_REMOTE_SETTINGS; + else if (strcmp (ifname, NM_DBUS_INTERFACE_DNS_MANAGER) == 0) + type = NM_TYPE_DNS_MANAGER; else if (strcmp (ifname, NM_DBUS_INTERFACE_VPN_CONNECTION) == 0) type = NM_TYPE_VPN_CONNECTION; else if (strcmp (ifname, NM_DBUS_INTERFACE_WIMAX_NSP) == 0) @@ -2046,6 +2151,7 @@ objects_created (NMClient *client, GDBusObjectManager *object_manager, GError ** NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (client); gs_unref_object GDBusObject *manager = NULL; gs_unref_object GDBusObject *settings = NULL; + gs_unref_object GDBusObject *dns_manager = NULL; NMObject *obj_nm; GList *objects, *iter; @@ -2119,6 +2225,23 @@ objects_created (NMClient *client, GDBusObjectManager *object_manager, GError ** g_signal_connect (priv->settings, "connection-removed", G_CALLBACK (settings_connection_removed), client); + dns_manager = g_dbus_object_manager_get_object (object_manager, NM_DBUS_PATH_DNS_MANAGER); + if (dns_manager) { + obj_nm = g_object_get_qdata (G_OBJECT (dns_manager), _nm_object_obj_nm_quark ()); + if (!obj_nm) { + g_set_error_literal (error, + NM_CLIENT_ERROR, + NM_CLIENT_ERROR_MANAGER_NOT_RUNNING, + "DNS manager object lacks the proper interface"); + return FALSE; + } + priv->dns_manager = NM_DNS_MANAGER (g_object_ref (obj_nm)); + + g_signal_connect (priv->dns_manager, "notify", + G_CALLBACK (dns_notify), client); + } + + /* The handlers don't really use the client instance. However * it makes it convenient to unhook them by data. */ g_signal_connect (object_manager, "object-added", @@ -2257,6 +2380,10 @@ unhook_om (NMClient *self) g_object_notify (G_OBJECT (self), NM_CLIENT_HOSTNAME); g_object_notify (G_OBJECT (self), NM_CLIENT_CAN_MODIFY); } + if (priv->dns_manager) { + g_signal_handlers_disconnect_by_data (priv->dns_manager, self); + g_clear_object (&priv->dns_manager); + } objects = g_dbus_object_manager_get_objects (priv->object_manager); for (iter = objects; iter; iter = iter->next) @@ -2415,6 +2542,11 @@ dispose (GObject *object) g_clear_object (&priv->settings); } + if (priv->dns_manager) { + g_signal_handlers_disconnect_by_data (priv->dns_manager, object); + g_clear_object (&priv->dns_manager); + } + if (priv->object_manager) { GList *objects, *iter; @@ -2547,6 +2679,25 @@ get_property (GObject *object, guint prop_id, else g_value_set_boolean (value, FALSE); break; + + /* DNS properties */ + case PROP_DNS_MODE: + case PROP_DNS_RC_MANAGER: + g_return_if_fail (pspec->name && strlen (pspec->name) > NM_STRLEN ("dns-")); + if (priv->dns_manager) + g_object_get_property (G_OBJECT (priv->dns_manager), + &pspec->name[NM_STRLEN ("dns-")], value); + else + g_value_set_string (value, NULL); + break; + case PROP_DNS_CONFIGURATION: + if (priv->dns_manager) { + g_object_get_property (G_OBJECT (priv->dns_manager), + NM_DNS_MANAGER_CONFIGURATION, + value); + } else + g_value_take_boxed (value, NULL); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -2838,6 +2989,54 @@ nm_client_class_init (NMClientClass *client_class) G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + /** + * NMClient:dns-mode: + * + * The current DNS processing mode. + * + * Since: 1.6 + **/ + g_object_class_install_property + (object_class, PROP_DNS_MODE, + g_param_spec_string (NM_CLIENT_DNS_MODE, "", "", + "", + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMClient:dns-rc-manager: + * + * The current resolv.conf management mode. + * + * Since: 1.6 + **/ + g_object_class_install_property + (object_class, PROP_DNS_RC_MANAGER, + g_param_spec_string (NM_CLIENT_DNS_RC_MANAGER, "", "", + "", + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMClient:dns-configuration: + * + * The current DNS configuration represented as an array of + * dictionaries. Each dictionary has the "nameservers", + * "priority" keys and, optionally, "interface" and "vpn". + * "nameservers" is the list of DNS servers, "priority" their + * relative priority, "interface" the interface on which these + * servers are contacted, "vpn" a boolean telling whether the + * configuration was obtained from a VPN connection. + * + * Since: 1.6 + **/ + g_object_class_install_property + (object_class, PROP_DNS_CONFIGURATION, + g_param_spec_boxed (NM_CLIENT_DNS_CONFIGURATION, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + /* signals */ /** diff --git a/libnm/nm-client.h b/libnm/nm-client.h index 5358ded4a0..65e0bac88e 100644 --- a/libnm/nm-client.h +++ b/libnm/nm-client.h @@ -58,6 +58,9 @@ G_BEGIN_DECLS #define NM_CLIENT_HOSTNAME "hostname" #define NM_CLIENT_CAN_MODIFY "can-modify" #define NM_CLIENT_METERED "metered" +#define NM_CLIENT_DNS_MODE "dns-mode" +#define NM_CLIENT_DNS_RC_MANAGER "dns-rc-manager" +#define NM_CLIENT_DNS_CONFIGURATION "dns-configuration" #define NM_CLIENT_DEVICE_ADDED "device-added" #define NM_CLIENT_DEVICE_REMOVED "device-removed" @@ -169,6 +172,25 @@ typedef enum { #define NM_CLIENT_ERROR nm_client_error_quark () GQuark nm_client_error_quark (void); +/* DNS stuff */ + +typedef struct NMDnsEntry NMDnsEntry; + +NM_AVAILABLE_IN_1_6 +GType nm_dns_entry_get_type (void); +NM_AVAILABLE_IN_1_6 +void nm_dns_entry_unref (NMDnsEntry *entry); +NM_AVAILABLE_IN_1_6 +const char * nm_dns_entry_get_interface (NMDnsEntry *entry); +NM_AVAILABLE_IN_1_6 +const char * const *nm_dns_entry_get_nameservers (NMDnsEntry *entry); +NM_AVAILABLE_IN_1_6 +const char * const *nm_dns_entry_get_domains (NMDnsEntry *entry); +NM_AVAILABLE_IN_1_6 +int nm_dns_entry_get_priority (NMDnsEntry *entry); +NM_AVAILABLE_IN_1_6 +gboolean nm_dns_entry_get_vpn (NMDnsEntry *entry); + /** * NMClient: */ @@ -359,6 +381,13 @@ gboolean nm_client_reload_connections_finish (NMClient *client, GAsyncResult *result, GError **error); +NM_AVAILABLE_IN_1_6 +const char *nm_client_get_dns_mode (NMClient *client); +NM_AVAILABLE_IN_1_6 +const char *nm_client_get_dns_rc_manager (NMClient *client); +NM_AVAILABLE_IN_1_6 +const GPtrArray *nm_client_get_dns_configuration (NMClient *client); + G_END_DECLS #endif /* __NM_CLIENT_H__ */ diff --git a/libnm/nm-dns-manager.c b/libnm/nm-dns-manager.c new file mode 100644 index 0000000000..6023b2e7fb --- /dev/null +++ b/libnm/nm-dns-manager.c @@ -0,0 +1,448 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This library 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 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2016 Red Hat, Inc. + */ + +#include "nm-default.h" + +#include "nm-dns-manager.h" + +#include <string.h> + +#include "nm-dbus-interface.h" +#include "nm-connection.h" + +#include "nm-client.h" +#include "nm-object-private.h" +#include "nm-dbus-helpers.h" +#include "nm-core-internal.h" + +#include "introspection/org.freedesktop.NetworkManager.DnsManager.h" + +G_DEFINE_TYPE (NMDnsManager, nm_dns_manager, NM_TYPE_OBJECT) + +#define NM_DNS_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DNS_MANAGER, NMDnsManagerPrivate)) + +typedef struct { + NMDBusDnsManager *proxy; + char *mode; + char *rc_manager; + GPtrArray *configuration; +} NMDnsManagerPrivate; + +enum { + PROP_0, + PROP_MODE, + PROP_RC_MANAGER, + PROP_CONFIGURATION, + + LAST_PROP +}; + +/***************************************************************************** + * NMDnsEntry + *****************************************************************************/ + +G_DEFINE_BOXED_TYPE (NMDnsEntry, nm_dns_entry, nm_dns_entry_dup, nm_dns_entry_unref) + +struct NMDnsEntry { + guint refcount; + + char *interface; + char **nameservers; + char **domains; + int priority; + gboolean vpn; +}; + +/** + * nm_dns_entry_new: + * + * Creates a new #NMDnsEntry object. + * + * Returns: (transfer full): the new #NMDnsEntry object, or %NULL on error + **/ +NMDnsEntry * +nm_dns_entry_new (const char *interface, + const char * const *nameservers, + const char * const *domains, + int priority, + gboolean vpn) +{ + NMDnsEntry *entry; + guint i, len; + + entry = g_slice_new0 (NMDnsEntry); + entry->refcount = 1; + + entry->interface = g_strdup (interface); + + if (nameservers) { + len = g_strv_length ((char **) nameservers); + entry->nameservers = g_new (char *, len + 1); + for (i = 0; i < len + 1; i++) + entry->nameservers[i] = g_strdup (nameservers[i]); + } + + if (domains) { + len = g_strv_length ((char **) domains); + entry->domains = g_new (char *, len + 1); + for (i = 0; i < len + 1; i++) + entry->domains[i] = g_strdup (domains[i]); + } + + entry->priority = priority; + entry->vpn = vpn; + + return entry; +} + +/** + * nm_dns_entry_dup: + * @entry: the #NMDnsEntry + * + * Creates a copy of @entry + * + * Returns: (transfer full): a copy of @entry + **/ +NMDnsEntry * +nm_dns_entry_dup (NMDnsEntry *entry) +{ + NMDnsEntry *copy; + + g_return_val_if_fail (entry != NULL, NULL); + g_return_val_if_fail (entry->refcount > 0, NULL); + + copy = nm_dns_entry_new (entry->interface, + (const char * const *) entry->nameservers, + (const char * const *) entry->domains, + entry->priority, + entry->vpn); + + return copy; +} + +/** + * nm_dns_entry_unref: + * @entry: the #NMDnsEntry + * + * Decreases the reference count of the object. If the reference count + * reaches zero, the object will be destroyed. + * + * Since: 1.6 + **/ +void +nm_dns_entry_unref (NMDnsEntry *entry) +{ + g_return_if_fail (entry != NULL); + g_return_if_fail (entry->refcount > 0); + + entry->refcount--; + if (entry->refcount == 0) { + g_free (entry->interface); + g_strfreev (entry->nameservers); + g_strfreev (entry->domains); + g_slice_free (NMDnsEntry, entry); + } +} + +/** + * nm_dns_entry_get_interface: + * @entry: the #NMDnsEntry + * + * Gets the interface on which name servers are contacted. + * + * Returns: (transfer none): the interface name + * + * Since: 1.6 + **/ +const char * +nm_dns_entry_get_interface (NMDnsEntry *entry) +{ + g_return_val_if_fail (entry, 0); + g_return_val_if_fail (entry->refcount > 0, 0); + + return entry->interface; +} + +/** + * nm_dns_entry_get_nameservers: + * @entry: the #NMDnsEntry + * + * Gets the list of name servers for this entry. + * + * Returns: (transfer none): the list of name servers + * + * Since: 1.6 + **/ +const char * const * +nm_dns_entry_get_nameservers (NMDnsEntry *entry) +{ + g_return_val_if_fail (entry, 0); + g_return_val_if_fail (entry->refcount > 0, 0); + + return (const char * const *) entry->nameservers; +} + +/** + * nm_dns_entry_get_domains: + * @entry: the #NMDnsEntry + * + * Gets the list of DNS domains. + * + * Returns: (transfer none): the list of DNS domains + * + * Since: 1.6 + **/ +const char * const * +nm_dns_entry_get_domains (NMDnsEntry *entry) +{ + g_return_val_if_fail (entry, 0); + g_return_val_if_fail (entry->refcount > 0, 0); + + return (const char * const *)entry->domains; +} + +/** + * nm_dns_entry_get_vpn: + * @entry: the #NMDnsEntry + * + * Gets whether the entry refers to VPN name servers. + * + * Returns: %TRUE if the entry refers to VPN name servers + * + * Since: 1.6 + **/ +gboolean +nm_dns_entry_get_vpn (NMDnsEntry *entry) +{ + g_return_val_if_fail (entry, 0); + g_return_val_if_fail (entry->refcount > 0, 0); + + return entry->vpn; +} + +/** + * nm_dns_entry_get_priority: + * @entry: the #NMDnsEntry + * + * Gets the priority of the entry + * + * Returns: the priority of the entry + * + * Since: 1.6 + **/ +int +nm_dns_entry_get_priority (NMDnsEntry *entry) +{ + g_return_val_if_fail (entry, 0); + g_return_val_if_fail (entry->refcount > 0, 0); + + return entry->priority; +} + +/*****************************************************************************/ + +static gboolean +demarshal_dns_configuration (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + NMDnsManagerPrivate *priv = NM_DNS_MANAGER_GET_PRIVATE (object); + GVariant *entry_var; + GVariantIter iter, *iterp; + NMDnsEntry *entry; + GPtrArray *array; + + g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("aa{sv}")), FALSE); + + g_variant_iter_init (&iter, value); + g_ptr_array_unref (priv->configuration); + priv->configuration = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_dns_entry_unref); + + while (g_variant_iter_next (&iter, "@a{sv}", &entry_var)) { + char **nameservers = NULL, **domains = NULL; + gboolean vpn = FALSE; + char *interface = NULL, *str; + gint priority; + + if ( !g_variant_lookup (entry_var, "nameservers", "as", &iterp) + || !g_variant_lookup (entry_var, "priority", "i", &priority)) { + g_warning ("Ignoring invalid DNS configuration"); + g_variant_unref (entry_var); + continue; + } + + array = g_ptr_array_new (); + while (g_variant_iter_next (iterp, "&s", &str)) + g_ptr_array_add (array, str); + g_ptr_array_add (array, NULL); + nameservers = (char **) g_ptr_array_free (array, FALSE); + g_variant_iter_free (iterp); + + if (g_variant_lookup (entry_var, "domains", "as", &iterp)) { + array = g_ptr_array_new (); + while (g_variant_iter_next (iterp, "&s", &str)) + g_ptr_array_add (array, str); + g_ptr_array_add (array, NULL); + domains = (char **) g_ptr_array_free (array, FALSE); + g_variant_iter_free (iterp); + } + + g_variant_lookup (entry_var, "interface", "&s", &interface); + g_variant_lookup (entry_var, "priority", "i", &priority); + g_variant_lookup (entry_var, "vpn", "b", &vpn); + + entry = nm_dns_entry_new (interface, + (const char * const *) nameservers, + (const char * const *) domains, + priority, + vpn); + if (!entry) { + g_warning ("Ignoring invalid DNS entry"); + g_variant_unref (entry_var); + continue; + } + + g_ptr_array_add (priv->configuration, entry); + } + + _nm_object_queue_notify (object, NM_DNS_MANAGER_CONFIGURATION); + + return TRUE; +} + +/*****************************************************************************/ +const char * +nm_dns_manager_get_mode (NMDnsManager *manager) +{ + return NM_DNS_MANAGER_GET_PRIVATE (manager)->mode; +} + +const char * +nm_dns_manager_get_rc_manager (NMDnsManager *manager) +{ + return NM_DNS_MANAGER_GET_PRIVATE (manager)->rc_manager; +} + +const GPtrArray * +nm_dns_manager_get_configuration (NMDnsManager *manager) +{ + return NM_DNS_MANAGER_GET_PRIVATE (manager)->configuration; +} +/*****************************************************************************/ + +static void +nm_dns_manager_init (NMDnsManager *self) +{ + NMDnsManagerPrivate *priv = NM_DNS_MANAGER_GET_PRIVATE (self); + + priv->configuration = g_ptr_array_new (); +} + +static void +init_dbus (NMObject *object) +{ + NMDnsManagerPrivate *priv = NM_DNS_MANAGER_GET_PRIVATE (object); + const NMPropertiesInfo property_info[] = { + { NM_DNS_MANAGER_MODE, &priv->mode }, + { NM_DNS_MANAGER_RC_MANAGER, &priv->rc_manager }, + { NM_DNS_MANAGER_CONFIGURATION, &priv->configuration, demarshal_dns_configuration }, + { NULL }, + }; + + NM_OBJECT_CLASS (nm_dns_manager_parent_class)->init_dbus (object); + + priv->proxy = NMDBUS_DNS_MANAGER (_nm_object_get_proxy (object, NM_DBUS_INTERFACE_DNS_MANAGER)); + _nm_object_register_properties (object, + NM_DBUS_INTERFACE_DNS_MANAGER, + property_info); +} + +static void +dispose (GObject *object) +{ + NMDnsManager *self = NM_DNS_MANAGER (object); + NMDnsManagerPrivate *priv = NM_DNS_MANAGER_GET_PRIVATE (self); + + g_clear_pointer (&priv->mode, g_free); + g_clear_pointer (&priv->rc_manager, g_free); + g_clear_pointer (&priv->configuration, g_ptr_array_unref); + + G_OBJECT_CLASS (nm_dns_manager_parent_class)->dispose (object); +} + +static void +get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + NMDnsManagerPrivate *priv = NM_DNS_MANAGER_GET_PRIVATE (object); + + switch (prop_id) { + case PROP_MODE: + g_value_set_string (value, priv->mode); + break; + case PROP_RC_MANAGER: + g_value_set_string (value, priv->rc_manager); + break; + case PROP_CONFIGURATION: + g_value_take_boxed (value, _nm_utils_copy_array (priv->configuration, + (NMUtilsCopyFunc) nm_dns_entry_dup, + (GDestroyNotify) nm_dns_entry_unref)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +nm_dns_manager_class_init (NMDnsManagerClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + NMObjectClass *nm_object_class = NM_OBJECT_CLASS (class); + + g_type_class_add_private (class, sizeof (NMDnsManagerPrivate)); + + /* Virtual methods */ + object_class->get_property = get_property; + object_class->dispose = dispose; + + nm_object_class->init_dbus = init_dbus; + + /* Properties */ + + g_object_class_install_property + (object_class, PROP_MODE, + g_param_spec_string (NM_DNS_MANAGER_MODE, "", "", + NULL, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property + (object_class, PROP_RC_MANAGER, + g_param_spec_string (NM_DNS_MANAGER_RC_MANAGER, "", "", + NULL, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property + (object_class, PROP_CONFIGURATION, + g_param_spec_boxed (NM_DNS_MANAGER_CONFIGURATION, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); +} diff --git a/libnm/nm-dns-manager.h b/libnm/nm-dns-manager.h new file mode 100644 index 0000000000..9ed3dfbed9 --- /dev/null +++ b/libnm/nm-dns-manager.h @@ -0,0 +1,72 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This library 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 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2016 Red Hat, Inc. + */ + +#ifndef __NM_DNS_MANAGER_H__ +#define __NM_DNS_MANAGER_H__ + +#include <nm-object.h> +#include "nm-client.h" + +G_BEGIN_DECLS + +#define NM_TYPE_DNS_MANAGER (nm_dns_manager_get_type ()) +#define NM_DNS_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DNS_MANAGER, NMDnsManager)) +#define NM_DNS_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DNS_MANAGER, NMDnsManagerClass)) +#define NM_IS_DNS_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DNS_MANAGER)) +#define NM_IS_DNS_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DNS_MANAGER)) +#define NM_DNS_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DNS_MANAGER, NMDnsManagerClass)) + +#define NM_DNS_MANAGER_MODE "mode" +#define NM_DNS_MANAGER_RC_MANAGER "rc-manager" +#define NM_DNS_MANAGER_CONFIGURATION "configuration" + +typedef struct _NMDnsManager NMDnsManager; +typedef struct _NMDnsManagerClass NMDnsManagerClass; + +/** + * NMDnsManager: + */ +struct _NMDnsManager { + NMObject parent; +}; + +struct _NMDnsManagerClass { + NMObjectClass parent; + + /*< private >*/ + gpointer padding[8]; +}; + +G_END_DECLS + +GType nm_dns_manager_get_type (void); + +const char *nm_dns_manager_get_mode (NMDnsManager *manager); +const char *nm_dns_manager_get_rc_manager (NMDnsManager *manager); +const GPtrArray *nm_dns_manager_get_configuration (NMDnsManager *manager); + +NMDnsEntry * nm_dns_entry_new (const char *interface, + const char * const *nameservers, + const char * const *domains, + int priority, + gboolean vpn); +NMDnsEntry * nm_dns_entry_dup (NMDnsEntry *entry); + +#endif /* __NM_DNS_MANAGER_H__ */ diff --git a/tools/test-networkmanager-service.py b/tools/test-networkmanager-service.py index edf7302cb0..68097778ea 100755 --- a/tools/test-networkmanager-service.py +++ b/tools/test-networkmanager-service.py @@ -1264,7 +1264,41 @@ class ObjectManager(dbus.service.Object): pass ################################################################### +IFACE_DNS_MANAGER = 'org.freedesktop.NetworkManager.DnsManager' +class DnsManager(ExportedObj): + def __init__(self, bus, object_path): + self.props = {} + self.props['Mode'] = "dnsmasq" + self.props['RcManager'] = "symlink" + self.props['Configuration'] = dbus.Array([ + dbus.Dictionary( + { 'nameservers' : dbus.Array(['1.2.3.4', '5.6.7.8'], 's'), + 'priority' : dbus.Int32(100) }, + 'sv') ], + 'a{sv}') + + self.add_dbus_interface(IFACE_DNS_MANAGER, self.__get_props, None) + ExportedObj.__init__(self, bus, object_path) + + @dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE, in_signature='s', out_signature='a{sv}') + def GetAll(self, iface): + if iface != IFACE_DNS_MANAGER: + raise UnknownInterfaceException() + return self.props + + @dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE, in_signature='ss', out_signature='v') + def Get(self, iface, name): + if iface != IFACE_DNS_MANAGER: + raise UnknownInterfaceException() + if not name in self.props.keys(): + raise UnknownPropertyException() + return self.props[name] + + def __get_props(self): + return self.props + +################################################################### def stdin_cb(io, condition): mainloop.quit() @@ -1276,13 +1310,14 @@ def main(): random.seed() - global manager, settings, agent_manager, object_manager, bus + global manager, settings, agent_manager, dns_manager, object_manager, bus bus = dbus.SessionBus() object_manager = ObjectManager(bus, "/org/freedesktop") manager = NetworkManager(bus, "/org/freedesktop/NetworkManager") settings = Settings(bus, "/org/freedesktop/NetworkManager/Settings") agent_manager = AgentManager(bus, "/org/freedesktop/NetworkManager/AgentManager") + dns_manager = DnsManager(bus, "/org/freedesktop/NetworkManager/DnsManager") if not bus.request_name("org.freedesktop.NetworkManager"): sys.exit(1) |