From e8bf89a90679274609c7e9b8fcb7b2ef13f89b83 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 2 Jan 2019 15:54:18 +0100 Subject: [PATCH] libnm: pass serialization flags to settings synth_func() We will need access to the serialization flags from within the synth_func(). That will be for WireGuard's peers. Peers are a list of complex, structured elements, and some fields (the peer's preshared-key) are secret and others are not. So when serializing the peers, we need to know whether to include secrets or not. Instead of letting _nm_setting_to_dbus() check the flags, pass them down. While at it, don't pass the property_name argument. Instead, pass the entire meta-data information we have. Most synth functions don't care about the property or the name either way. But we should not pre-filter information that we have at hand. Just pass it to the synth function. If the synth function would be public API, that would be a reason to be careful about what we pass. But it isn't and it only has one caller. So passing it along is fine. Also, do it now when adding the flags argument, as we touch all synth implementations anyway. --- libnm-core/nm-core-internal.h | 6 +++-- libnm-core/nm-setting-ip4-config.c | 43 +++++++++++++++++------------- libnm-core/nm-setting-ip6-config.c | 34 ++++++++++++----------- libnm-core/nm-setting-private.h | 6 +++-- libnm-core/nm-setting-wireless.c | 15 +++++++---- libnm-core/nm-setting.c | 18 ++++++++----- libnm-core/nm-utils-private.h | 6 +++-- libnm-core/nm-utils.c | 15 ++++++++--- 8 files changed, 88 insertions(+), 55 deletions(-) diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index 056269f36b..ec9b108318 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -582,9 +582,11 @@ typedef struct _NMSettInfoSetting NMSettInfoSetting; typedef GVariant *(*NMSettingPropertyGetFunc) (NMSetting *setting, const char *property); -typedef GVariant *(*NMSettingPropertySynthFunc) (NMSetting *setting, +typedef GVariant *(*NMSettingPropertySynthFunc) (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property); + NMSetting *setting, + NMConnectionSerializationFlags flags); typedef gboolean (*NMSettingPropertySetFunc) (NMSetting *setting, GVariant *connection_dict, const char *property, diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 36765484eb..adc8243403 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -376,9 +376,11 @@ ip4_addresses_set (NMSetting *setting, } static GVariant * -ip4_address_labels_get (NMSetting *setting, +ip4_address_labels_get (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property) + NMSetting *setting, + NMConnectionSerializationFlags flags) { NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting); gboolean have_labels = FALSE; @@ -386,6 +388,9 @@ ip4_address_labels_get (NMSetting *setting, GVariant *ret; int num_addrs, i; + if (flags & NM_CONNECTION_SERIALIZE_ONLY_SECRETS) + return NULL; + num_addrs = nm_setting_ip_config_get_num_addresses (s_ip); for (i = 0; i < num_addrs; i++) { NMIPAddress *addr = nm_setting_ip_config_get_address (s_ip, i); @@ -414,18 +419,19 @@ ip4_address_labels_get (NMSetting *setting, } static GVariant * -ip4_address_data_get (NMSetting *setting, +ip4_address_data_get (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property) + NMSetting *setting, + NMConnectionSerializationFlags flags) { - GPtrArray *addrs; - GVariant *ret; + gs_unref_ptrarray GPtrArray *addrs = NULL; + + if (flags & NM_CONNECTION_SERIALIZE_ONLY_SECRETS) + return NULL; g_object_get (setting, NM_SETTING_IP_CONFIG_ADDRESSES, &addrs, NULL); - ret = nm_utils_ip_addresses_to_variant (addrs); - g_ptr_array_unref (addrs); - - return ret; + return nm_utils_ip_addresses_to_variant (addrs); } static gboolean @@ -486,18 +492,19 @@ ip4_routes_set (NMSetting *setting, } static GVariant * -ip4_route_data_get (NMSetting *setting, +ip4_route_data_get (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property) + NMSetting *setting, + NMConnectionSerializationFlags flags) { - GPtrArray *routes; - GVariant *ret; + gs_unref_ptrarray GPtrArray *routes = NULL; + + if (flags & NM_CONNECTION_SERIALIZE_ONLY_SECRETS) + return NULL; g_object_get (setting, NM_SETTING_IP_CONFIG_ROUTES, &routes, NULL); - ret = nm_utils_ip_routes_to_variant (routes); - g_ptr_array_unref (routes); - - return ret; + return nm_utils_ip_routes_to_variant (routes); } static gboolean diff --git a/libnm-core/nm-setting-ip6-config.c b/libnm-core/nm-setting-ip6-config.c index fa65be910a..153df54f2f 100644 --- a/libnm-core/nm-setting-ip6-config.c +++ b/libnm-core/nm-setting-ip6-config.c @@ -374,18 +374,19 @@ ip6_addresses_set (NMSetting *setting, } static GVariant * -ip6_address_data_get (NMSetting *setting, +ip6_address_data_get (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property) + NMSetting *setting, + NMConnectionSerializationFlags flags) { - GPtrArray *addrs; - GVariant *ret; + gs_unref_ptrarray GPtrArray *addrs = NULL; + + if (flags & NM_CONNECTION_SERIALIZE_ONLY_SECRETS) + return NULL; g_object_get (setting, NM_SETTING_IP_CONFIG_ADDRESSES, &addrs, NULL); - ret = nm_utils_ip_addresses_to_variant (addrs); - g_ptr_array_unref (addrs); - - return ret; + return nm_utils_ip_addresses_to_variant (addrs); } static gboolean @@ -446,18 +447,19 @@ ip6_routes_set (NMSetting *setting, } static GVariant * -ip6_route_data_get (NMSetting *setting, +ip6_route_data_get (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property) + NMSetting *setting, + NMConnectionSerializationFlags flags) { - GPtrArray *routes; - GVariant *ret; + gs_unref_ptrarray GPtrArray *routes = NULL; + + if (flags & NM_CONNECTION_SERIALIZE_ONLY_SECRETS) + return NULL; g_object_get (setting, NM_SETTING_IP_CONFIG_ROUTES, &routes, NULL); - ret = nm_utils_ip_routes_to_variant (routes); - g_ptr_array_unref (routes); - - return ret; + return nm_utils_ip_routes_to_variant (routes); } static gboolean diff --git a/libnm-core/nm-setting-private.h b/libnm-core/nm-setting-private.h index 1e25226ede..c785fbc90f 100644 --- a/libnm-core/nm-setting-private.h +++ b/libnm-core/nm-setting-private.h @@ -82,9 +82,11 @@ gboolean _nm_setting_clear_secrets_with_flags (NMSetting *setting, #define NM_SETTING_PARAM_GENDATA_BACKED (1 << (7 + G_PARAM_USER_SHIFT)) -GVariant *_nm_setting_get_deprecated_virtual_interface_name (NMSetting *setting, +GVariant *_nm_setting_get_deprecated_virtual_interface_name (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property); + NMSetting *setting, + NMConnectionSerializationFlags flags); NMSettingVerifyResult _nm_setting_verify (NMSetting *setting, NMConnection *connection, diff --git a/libnm-core/nm-setting-wireless.c b/libnm-core/nm-setting-wireless.c index 31eda97fa8..02ff534b2f 100644 --- a/libnm-core/nm-setting-wireless.c +++ b/libnm-core/nm-setting-wireless.c @@ -949,17 +949,22 @@ compare_property (NMSetting *setting, /*****************************************************************************/ static GVariant * -nm_setting_wireless_get_security (NMSetting *setting, +nm_setting_wireless_get_security (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property_name) + NMSetting *setting, + NMConnectionSerializationFlags flags) { + if (flags & NM_CONNECTION_SERIALIZE_ONLY_SECRETS) + return NULL; + if (!connection) return NULL; - if (nm_connection_get_setting_wireless_security (connection)) - return g_variant_new_string (NM_SETTING_WIRELESS_SECURITY_SETTING_NAME); - else + if (!nm_connection_get_setting_wireless_security (connection)) return NULL; + + return g_variant_new_string (NM_SETTING_WIRELESS_SECURITY_SETTING_NAME); } /** diff --git a/libnm-core/nm-setting.c b/libnm-core/nm-setting.c index e5e9ab9655..5488eea816 100644 --- a/libnm-core/nm-setting.c +++ b/libnm-core/nm-setting.c @@ -662,10 +662,13 @@ _nm_setting_to_dbus (NMSetting *setting, NMConnection *connection, NMConnectionS if (!prop_spec) { if (!property->synth_func) continue; - - if (flags & NM_CONNECTION_SERIALIZE_ONLY_SECRETS) - continue; } else { + + /* For the moment, properties backed by a GObject property don't + * define a synth function. There is no problem supporting that, + * however, for now just disallow it. */ + nm_assert (!property->synth_func); + if (!(prop_spec->flags & G_PARAM_WRITABLE)) continue; @@ -686,9 +689,10 @@ _nm_setting_to_dbus (NMSetting *setting, NMConnection *connection, NMConnectionS } if (property->synth_func) - dbus_value = property->synth_func (setting, connection, property->name); + dbus_value = property->synth_func (sett_info, i, connection, setting, flags); else dbus_value = get_property_for_dbus (setting, property, TRUE); + if (dbus_value) { /* Allow dbus_value to be either floating or not. */ g_variant_take_ref (dbus_value); @@ -2031,9 +2035,11 @@ nm_setting_to_string (NMSetting *setting) } GVariant * -_nm_setting_get_deprecated_virtual_interface_name (NMSetting *setting, +_nm_setting_get_deprecated_virtual_interface_name (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property) + NMSetting *setting, + NMConnectionSerializationFlags flags) { NMSettingConnection *s_con; diff --git a/libnm-core/nm-utils-private.h b/libnm-core/nm-utils-private.h index b886730a35..58822cdeff 100644 --- a/libnm-core/nm-utils-private.h +++ b/libnm-core/nm-utils-private.h @@ -56,9 +56,11 @@ gboolean _nm_utils_hwaddr_cloned_not_set (NMSetting *setting, const char *property, NMSettingParseFlags parse_flags, GError **error); -GVariant * _nm_utils_hwaddr_cloned_data_synth (NMSetting *setting, +GVariant * _nm_utils_hwaddr_cloned_data_synth (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property); + NMSetting *setting, + NMConnectionSerializationFlags flags); gboolean _nm_utils_hwaddr_cloned_data_set (NMSetting *setting, GVariant *connection_dict, const char *property, diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 69657c9dce..4709971281 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -4234,13 +4234,18 @@ _nm_utils_hwaddr_cloned_not_set (NMSetting *setting, } GVariant * -_nm_utils_hwaddr_cloned_data_synth (NMSetting *setting, +_nm_utils_hwaddr_cloned_data_synth (const NMSettInfoSetting *sett_info, + guint property_idx, NMConnection *connection, - const char *property) + NMSetting *setting, + NMConnectionSerializationFlags flags) { gs_free char *addr = NULL; - nm_assert (nm_streq0 (property, "assigned-mac-address")); + if (flags & NM_CONNECTION_SERIALIZE_ONLY_SECRETS) + return NULL; + + nm_assert (nm_streq0 (sett_info->property_infos[property_idx].name, "assigned-mac-address")); g_object_get (setting, "cloned-mac-address", @@ -4261,7 +4266,9 @@ _nm_utils_hwaddr_cloned_data_synth (NMSetting *setting, * To preserve that behavior, serialize "" as NULL. */ - return addr && addr[0] ? g_variant_new_string (addr) : NULL; + return addr && addr[0] + ? g_variant_new_take_string (g_steal_pointer (&addr)) + : NULL; } gboolean