diff options
| author | Michael Biebl <biebl@debian.org> | 2019-03-26 23:25:23 +0100 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2019-03-26 23:25:23 +0100 |
| commit | 9a6dcbf895f9da01768e64b73cec88c16157d91e (patch) | |
| tree | a359958930d731e9f1b59344642e10754419fe84 /src/settings | |
| parent | 964ae8cc391520440cf5aa13e2b9cc34850ea6c2 (diff) | |
New upstream version 1.16.0 upstream/1.16.0
Diffstat (limited to 'src/settings')
46 files changed, 619 insertions, 615 deletions
diff --git a/src/settings/nm-agent-manager.c b/src/settings/nm-agent-manager.c index 953d3ce4..8924c39f 100644 --- a/src/settings/nm-agent-manager.c +++ b/src/settings/nm-agent-manager.c @@ -22,7 +22,6 @@ #include "nm-agent-manager.h" -#include <string.h> #include <pwd.h> #include "nm-common-macros.h" @@ -1059,49 +1058,6 @@ _con_get_request_start_validated (NMAuthChain *chain, } static void -has_system_secrets_check (NMSetting *setting, - const char *key, - const GValue *value, - GParamFlags flags, - gpointer user_data) -{ - NMSettingSecretFlags secret_flags = NM_SETTING_SECRET_FLAG_NONE; - gboolean *has_system = user_data; - - if (!(flags & NM_SETTING_PARAM_SECRET)) - return; - - /* Clear out system-owned or always-ask secrets */ - if (NM_IS_SETTING_VPN (setting) && !strcmp (key, NM_SETTING_VPN_SECRETS)) { - GHashTableIter iter; - const char *secret_name = NULL; - - /* VPNs are special; need to handle each secret separately */ - g_hash_table_iter_init (&iter, (GHashTable *) g_value_get_boxed (value)); - while (g_hash_table_iter_next (&iter, (gpointer *) &secret_name, NULL)) { - secret_flags = NM_SETTING_SECRET_FLAG_NONE; - nm_setting_get_secret_flags (setting, secret_name, &secret_flags, NULL); - if (secret_flags == NM_SETTING_SECRET_FLAG_NONE) - *has_system = TRUE; - } - } else { - if (!nm_setting_get_secret_flags (setting, key, &secret_flags, NULL)) - g_return_if_reached (); - if (secret_flags == NM_SETTING_SECRET_FLAG_NONE) - *has_system = TRUE; - } -} - -static gboolean -has_system_secrets (NMConnection *connection) -{ - gboolean has_system = FALSE; - - nm_connection_for_each_setting_value (connection, has_system_secrets_check, &has_system); - return has_system; -} - -static void _con_get_request_start (Request *req) { NMAgentManager *self; @@ -1121,7 +1077,8 @@ _con_get_request_start (Request *req) * unprivileged users. */ if ( (req->con.get.flags != NM_SECRET_AGENT_GET_SECRETS_FLAG_NONE) - && (req->con.get.existing_secrets || has_system_secrets (req->con.connection))) { + && ( req->con.get.existing_secrets + || _nm_connection_aggregate (req->con.connection, NM_CONNECTION_AGGREGATE_ANY_SYSTEM_SECRET_FLAGS, NULL))) { _LOGD (NULL, "("LOG_REQ_FMT") request has system secrets; checking agent %s for MODIFY", LOG_REQ_ARG (req), agent_dbus_owner); diff --git a/src/settings/nm-settings-connection.c b/src/settings/nm-settings-connection.c index 6f47e14d..8d1f9583 100644 --- a/src/settings/nm-settings-connection.c +++ b/src/settings/nm-settings-connection.c @@ -23,8 +23,6 @@ #include "nm-settings-connection.h" -#include <string.h> - #include "c-list/src/c-list.h" #include "nm-common-macros.h" @@ -208,141 +206,6 @@ nm_settings_connection_get_last_secret_agent_version_id (NMSettingsConnection *s /*****************************************************************************/ -/* Return TRUE to keep, FALSE to drop */ -typedef gboolean (*ForEachSecretFunc) (NMSettingSecretFlags flags, - gpointer user_data); - -/* Returns always a non-NULL, non-floating variant that must - * be unrefed by the caller. */ -static GVariant * -for_each_secret (NMConnection *self, - GVariant *secrets, - gboolean remove_non_secrets, - ForEachSecretFunc callback, - gpointer callback_data) -{ - GVariantBuilder secrets_builder, setting_builder; - GVariantIter secrets_iter, *setting_iter; - const char *setting_name; - - /* This function, given a dict of dicts representing new secrets of - * an NMConnection, walks through each toplevel dict (which represents a - * NMSetting), and for each setting, walks through that setting dict's - * properties. For each property that's a secret, it will check that - * secret's flags in the backing NMConnection object, and call a supplied - * callback. - * - * The one complexity is that the VPN setting's 'secrets' property is - * *also* a dict (since the key/value pairs are arbitrary and known - * only to the VPN plugin itself). That means we have three levels of - * dicts that we potentially have to traverse here. When we hit the - * VPN setting's 'secrets' property, we special-case that and iterate over - * each item in that 'secrets' dict, calling the supplied callback - * each time. - */ - - g_return_val_if_fail (callback, NULL); - - g_variant_iter_init (&secrets_iter, secrets); - g_variant_builder_init (&secrets_builder, NM_VARIANT_TYPE_CONNECTION); - while (g_variant_iter_next (&secrets_iter, "{&sa{sv}}", &setting_name, &setting_iter)) { - NMSetting *setting; - const char *secret_name; - GVariant *val; - - setting = nm_connection_get_setting_by_name (self, setting_name); - if (setting == NULL) { - g_variant_iter_free (setting_iter); - continue; - } - - g_variant_builder_init (&setting_builder, NM_VARIANT_TYPE_SETTING); - while (g_variant_iter_next (setting_iter, "{&sv}", &secret_name, &val)) { - NMSettingSecretFlags secret_flags = NM_SETTING_SECRET_FLAG_NONE; - - /* VPN secrets need slightly different treatment here since the - * "secrets" property is actually a hash table of secrets. - */ - if (NM_IS_SETTING_VPN (setting) && !g_strcmp0 (secret_name, NM_SETTING_VPN_SECRETS)) { - GVariantBuilder vpn_secrets_builder; - GVariantIter vpn_secrets_iter; - const char *vpn_secret_name, *secret; - - /* Iterate through each secret from the VPN dict in the overall secrets dict */ - g_variant_builder_init (&vpn_secrets_builder, G_VARIANT_TYPE ("a{ss}")); - g_variant_iter_init (&vpn_secrets_iter, val); - while (g_variant_iter_next (&vpn_secrets_iter, "{&s&s}", &vpn_secret_name, &secret)) { - if (!nm_setting_get_secret_flags (setting, vpn_secret_name, &secret_flags, NULL)) { - if (!remove_non_secrets) - g_variant_builder_add (&vpn_secrets_builder, "{ss}", vpn_secret_name, secret); - continue; - } - - if (callback (secret_flags, callback_data)) - g_variant_builder_add (&vpn_secrets_builder, "{ss}", vpn_secret_name, secret); - } - - g_variant_builder_add (&setting_builder, "{sv}", - secret_name, g_variant_builder_end (&vpn_secrets_builder)); - } else { - if (!nm_setting_get_secret_flags (setting, secret_name, &secret_flags, NULL)) { - if (!remove_non_secrets) - g_variant_builder_add (&setting_builder, "{sv}", secret_name, val); - continue; - } - if (callback (secret_flags, callback_data)) - g_variant_builder_add (&setting_builder, "{sv}", secret_name, val); - } - g_variant_unref (val); - } - - g_variant_iter_free (setting_iter); - g_variant_builder_add (&secrets_builder, "{sa{sv}}", setting_name, &setting_builder); - } - - return g_variant_ref_sink (g_variant_builder_end (&secrets_builder)); -} - -typedef gboolean (*FindSecretFunc) (NMSettingSecretFlags flags, - gpointer user_data); - -typedef struct { - FindSecretFunc find_func; - gpointer find_func_data; - gboolean found; -} FindSecretData; - -static gboolean -find_secret_for_each_func (NMSettingSecretFlags flags, - gpointer user_data) -{ - FindSecretData *data = user_data; - - if (!data->found) - data->found = data->find_func (flags, data->find_func_data); - return FALSE; -} - -static gboolean -find_secret (NMConnection *self, - GVariant *secrets, - FindSecretFunc callback, - gpointer callback_data) -{ - FindSecretData data; - GVariant *dummy; - - data.find_func = callback; - data.find_func_data = callback_data; - data.found = FALSE; - - dummy = for_each_secret (self, secrets, FALSE, find_secret_for_each_func, &data); - g_variant_unref (dummy); - return data.found; -} - -/*****************************************************************************/ - static void set_visible (NMSettingsConnection *self, gboolean new_visible) { @@ -790,7 +653,7 @@ out: else if (new_connection) _LOGI ("write: successfully updated (%s)", logmsg_change); else - _LOGI ("write: successfully commited (%s)", logmsg_change); + _LOGI ("write: successfully committed (%s)", logmsg_change); } return TRUE; } @@ -938,8 +801,8 @@ typedef struct { } ForEachSecretFlags; static gboolean -validate_secret_flags (NMSettingSecretFlags flags, - gpointer user_data) +validate_secret_flags_cb (NMSettingSecretFlags flags, + gpointer user_data) { ForEachSecretFlags *cmp_flags = user_data; @@ -950,6 +813,18 @@ validate_secret_flags (NMSettingSecretFlags flags, return TRUE; } +static GVariant * +validate_secret_flags (NMConnection *connection, + GVariant *secrets, + ForEachSecretFlags *cmp_flags) +{ + return g_variant_ref_sink (_nm_connection_for_each_secret (connection, + secrets, + TRUE, + validate_secret_flags_cb, + cmp_flags)); +} + static gboolean secret_is_system_owned (NMSettingSecretFlags flags, gpointer user_data) @@ -992,7 +867,7 @@ get_cmp_flags (NMSettingsConnection *self, /* only needed for logging */ * save those system-owned secrets. If not, discard them and use the * existing secrets, or fail the connection. */ - *agent_had_system = find_secret (connection, secrets, secret_is_system_owned, NULL); + *agent_had_system = _nm_connection_find_secret (connection, secrets, secret_is_system_owned, NULL); if (*agent_had_system) { if (flags == NM_SECRET_AGENT_GET_SECRETS_FLAG_NONE) { /* No user interaction was allowed when requesting secrets; the @@ -1151,14 +1026,14 @@ get_secrets_done_cb (NMAgentManager *manager, /* Update the connection with our existing secrets from backing storage */ nm_connection_clear_secrets (nm_settings_connection_get_connection (self)); if (!dict || nm_connection_update_secrets (nm_settings_connection_get_connection (self), setting_name, dict, &local)) { - GVariant *filtered_secrets; + gs_unref_variant GVariant *filtered_secrets = NULL; /* Update the connection with the agent's secrets; by this point if any * system-owned secrets exist in 'secrets' the agent that provided them * will have been authenticated, so those secrets can replace the existing * system secrets. */ - filtered_secrets = for_each_secret (nm_settings_connection_get_connection (self), secrets, TRUE, validate_secret_flags, &cmp_flags); + filtered_secrets = validate_secret_flags (nm_settings_connection_get_connection (self), secrets, &cmp_flags); if (nm_connection_update_secrets (nm_settings_connection_get_connection (self), setting_name, filtered_secrets, &local)) { /* Now that all secrets are updated, copy and cache new secrets, * then save them to backing storage. @@ -1194,7 +1069,6 @@ get_secrets_done_cb (NMAgentManager *manager, call_id, local->message); } - g_variant_unref (filtered_secrets); } else { _LOGD ("(%s:%p) failed to update with existing secrets: %s", setting_name, @@ -1218,11 +1092,10 @@ get_secrets_done_cb (NMAgentManager *manager, nm_connection_clear_secrets (applied_connection); if (!dict || nm_connection_update_secrets (applied_connection, setting_name, dict, NULL)) { - GVariant *filtered_secrets; + gs_unref_variant GVariant *filtered_secrets = NULL; - filtered_secrets = for_each_secret (applied_connection, secrets, TRUE, validate_secret_flags, &cmp_flags); + filtered_secrets = validate_secret_flags (applied_connection, secrets, &cmp_flags); nm_connection_update_secrets (applied_connection, setting_name, filtered_secrets, NULL); - g_variant_unref (filtered_secrets); } } @@ -1338,7 +1211,7 @@ nm_settings_connection_get_secrets (NMSettingsConnection *self, /* we remember the current version-id of the secret-agents. The version-id is strictly increasing, * as new agents register the number. We know hence, that this request was made against a certain * set of secret-agents. - * If after making this request a new secret-agent registeres, the version-id increases. + * If after making this request a new secret-agent registers, the version-id increases. * Then we know that the this request probably did not yet include the latest secret-agent. */ priv->last_secret_agent_version_id = nm_agent_manager_get_agent_version_id (priv->agent_mgr); @@ -1660,38 +1533,6 @@ typedef struct { } UpdateInfo; static void -has_some_secrets_cb (NMSetting *setting, - const char *key, - const GValue *value, - GParamFlags flags, - gpointer user_data) -{ - GParamSpec *pspec; - - if (NM_IS_SETTING_VPN (setting)) { - if (nm_setting_vpn_get_num_secrets (NM_SETTING_VPN(setting))) - *((gboolean *) user_data) = TRUE; - return; - } - - pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (setting)), key); - if (pspec) { - if ( (flags & NM_SETTING_PARAM_SECRET) - && !g_param_value_defaults (pspec, (GValue *)value)) - *((gboolean *) user_data) = TRUE; - } -} - -static gboolean -any_secrets_present (NMConnection *self) -{ - gboolean has_secrets = FALSE; - - nm_connection_for_each_setting_value (self, has_some_secrets_cb, &has_secrets); - return has_secrets; -} - -static void cached_secrets_to_connection (NMSettingsConnection *self, NMConnection *connection) { NMSettingsConnectionPrivate *priv = NM_SETTINGS_CONNECTION_GET_PRIVATE (self); @@ -1758,7 +1599,7 @@ update_auth_cb (NMSettingsConnection *self, } if (info->new_settings) { - if (!any_secrets_present (info->new_settings)) { + if (!_nm_connection_aggregate (info->new_settings, NM_CONNECTION_AGGREGATE_ANY_SECRETS, NULL)) { /* If the new connection has no secrets, we do not want to remove all * secrets, rather we keep all the existing ones. Do that by merging * them in to the new connection. @@ -1910,6 +1751,9 @@ settings_connection_update (NMSettingsConnection *self, &error); if (!tmp) goto error; + + if (!nm_connection_verify_secrets (tmp, &error)) + goto error; } } @@ -2809,7 +2653,7 @@ _autoconnect_retries_set (NMSettingsConnection *self, /* NOTE: the blocked time must be identical for all connections, otherwise * the tracking of resetting the retry count in NMPolicy needs adjustment * in _connection_autoconnect_retries_set() (as it would need to re-evaluate - * the next-timeout everytime a connection gets blocked). */ + * the next-timeout every time a connection gets blocked). */ priv->autoconnect_retries_blocked_until = nm_utils_get_monotonic_timestamp_s () + AUTOCONNECT_RESET_RETRIES_TIMER; } } diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index 2253d56a..fd1d316a 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -29,8 +29,6 @@ #include <unistd.h> #include <sys/stat.h> -#include <errno.h> -#include <string.h> #include <gmodule.h> #include <pwd.h> @@ -129,6 +127,8 @@ typedef struct { NMHostnameManager *hostname_manager; + NMSettingsConnection *startup_complete_blocked_by; + guint connections_len; bool started:1; @@ -182,19 +182,23 @@ static void check_startup_complete (NMSettings *self) { NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); - NMSettingsConnection *conn; + NMSettingsConnection *sett_conn; if (priv->startup_complete) return; - c_list_for_each_entry (conn, &priv->connections_lst_head, _connections_lst) { - if (!nm_settings_connection_get_ready (conn)) + c_list_for_each_entry (sett_conn, &priv->connections_lst_head, _connections_lst) { + if (!nm_settings_connection_get_ready (sett_conn)) { + nm_g_object_ref_set (&priv->startup_complete_blocked_by, sett_conn); return; + } } + g_clear_object (&priv->startup_complete_blocked_by); + /* the connection_ready_changed signal handler is no longer needed. */ - c_list_for_each_entry (conn, &priv->connections_lst_head, _connections_lst) - g_signal_handlers_disconnect_by_func (conn, G_CALLBACK (connection_ready_changed), self); + c_list_for_each_entry (sett_conn, &priv->connections_lst_head, _connections_lst) + g_signal_handlers_disconnect_by_func (sett_conn, G_CALLBACK (connection_ready_changed), self); priv->startup_complete = TRUE; _notify (self, PROP_STARTUP_COMPLETE); @@ -369,10 +373,10 @@ _clear_connections_cached_list (NMSettingsPrivate *priv) /** * nm_settings_get_connections: * @self: the #NMSettings - * @out_len: (out): (allow-none): returns the number of returned + * @out_len: (out) (allow-none): returns the number of returned * connections. * - * Returns: (transfer-none): a list of NMSettingsConnections. The list is + * Returns: (transfer none): a list of NMSettingsConnections. The list is * unsorted and NULL terminated. The result is never %NULL, in case of no * connections, it returns an empty list. * The returned list is cached internally, only valid until the next @@ -632,7 +636,7 @@ add_plugin_load_file (NMSettings *self, const char *pname, GError **error) if (stat (path, &st) != 0) { errsv = errno; - _LOGW ("could not load plugin '%s' from file '%s': %s", pname, path, strerror (errsv)); + _LOGW ("could not load plugin '%s' from file '%s': %s", pname, path, nm_strerror_native (errsv)); return TRUE; } if (!S_ISREG (st.st_mode)) { @@ -840,10 +844,10 @@ connection_removed (NMSettingsConnection *connection, gpointer user_data) if (priv->connections_loaded) g_signal_emit (self, signals[CONNECTION_REMOVED], 0, connection); - g_object_unref (connection); - check_startup_complete (self); + g_object_unref (connection); + g_object_unref (self); /* Balanced by a ref in claim_connection() */ } @@ -1754,12 +1758,17 @@ nm_settings_device_removed (NMSettings *self, NMDevice *device, gboolean quittin /*****************************************************************************/ -gboolean -nm_settings_get_startup_complete (NMSettings *self) +const char * +nm_settings_get_startup_complete_blocked_reason (NMSettings *self) { NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); + const char *uuid = NULL; - return priv->startup_complete; + if (priv->startup_complete) + return NULL; + if (priv->startup_complete_blocked_by) + uuid = nm_settings_connection_get_uuid (priv->startup_complete_blocked_by); + return uuid ?: "unknown"; } /*****************************************************************************/ @@ -1845,7 +1854,7 @@ get_property (GObject *object, guint prop_id, g_value_set_boxed (value, NULL); break; case PROP_STARTUP_COMPLETE: - g_value_set_boolean (value, nm_settings_get_startup_complete (self)); + g_value_set_boolean (value, !nm_settings_get_startup_complete_blocked_reason (self)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -1878,6 +1887,8 @@ dispose (GObject *object) NMSettings *self = NM_SETTINGS (object); NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); + g_clear_object (&priv->startup_complete_blocked_by); + g_slist_free_full (priv->auths, (GDestroyNotify) nm_auth_chain_destroy); priv->auths = NULL; diff --git a/src/settings/nm-settings.h b/src/settings/nm-settings.h index 38d8ad4e..eb74c09c 100644 --- a/src/settings/nm-settings.h +++ b/src/settings/nm-settings.h @@ -112,6 +112,6 @@ void nm_settings_device_added (NMSettings *self, NMDevice *device); void nm_settings_device_removed (NMSettings *self, NMDevice *device, gboolean quitting); -gboolean nm_settings_get_startup_complete (NMSettings *self); +const char *nm_settings_get_startup_complete_blocked_reason (NMSettings *self); #endif /* __NM_SETTINGS_H__ */ diff --git a/src/settings/plugins/ibft/meson.build b/src/settings/plugins/ibft/meson.build index c7dbe459..c33b24cc 100644 --- a/src/settings/plugins/ibft/meson.build +++ b/src/settings/plugins/ibft/meson.build @@ -8,7 +8,7 @@ libnms_ibft_core = static_library( sources = files( 'nms-ibft-connection.c', - 'nms-ibft-plugin.c' + 'nms-ibft-plugin.c', ) libnm_settings_plugin_ibft = shared_module( @@ -19,7 +19,7 @@ libnm_settings_plugin_ibft = shared_module( link_args: ldflags_linker_script_settings, link_depends: linker_script_settings, install: true, - install_dir: nm_plugindir + install_dir: nm_plugindir, ) core_plugins += libnm_settings_plugin_ibft @@ -29,7 +29,7 @@ core_plugins += libnm_settings_plugin_ibft run_target( 'check-local-symbols-settings-ibft', command: [check_so_symbols, libnm_settings_plugin_ibft.full_path()], - depends: libnm_settings_plugin_ibft + depends: libnm_settings_plugin_ibft, ) check-local-symbols-settings-ibft: src/settings/plugins/ibft/libnm-settings-plugin-ibft.la diff --git a/src/settings/plugins/ibft/nms-ibft-connection.c b/src/settings/plugins/ibft/nms-ibft-connection.c index fb7f18f8..a36d8a31 100644 --- a/src/settings/plugins/ibft/nms-ibft-connection.c +++ b/src/settings/plugins/ibft/nms-ibft-connection.c @@ -22,7 +22,6 @@ #include "nms-ibft-connection.h" -#include <string.h> #include <net/ethernet.h> #include <netinet/ether.h> #include <glib/gstdio.h> diff --git a/src/settings/plugins/ibft/nms-ibft-plugin.c b/src/settings/plugins/ibft/nms-ibft-plugin.c index 69dd3733..00b25068 100644 --- a/src/settings/plugins/ibft/nms-ibft-plugin.c +++ b/src/settings/plugins/ibft/nms-ibft-plugin.c @@ -22,9 +22,7 @@ #include "nms-ibft-plugin.h" -#include <string.h> #include <unistd.h> -#include <errno.h> #include <gmodule.h> #include "nm-setting-connection.h" diff --git a/src/settings/plugins/ibft/nms-ibft-reader.c b/src/settings/plugins/ibft/nms-ibft-reader.c index ac5824a1..c6c14376 100644 --- a/src/settings/plugins/ibft/nms-ibft-reader.c +++ b/src/settings/plugins/ibft/nms-ibft-reader.c @@ -23,13 +23,11 @@ #include "nms-ibft-reader.h" #include <stdlib.h> -#include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/wait.h> #include <sys/inotify.h> -#include <errno.h> #include <sys/ioctl.h> #include <unistd.h> diff --git a/src/settings/plugins/ibft/tests/meson.build b/src/settings/plugins/ibft/tests/meson.build index 8b5e143a..e2f9ca7e 100644 --- a/src/settings/plugins/ibft/tests/meson.build +++ b/src/settings/plugins/ibft/tests/meson.build @@ -6,11 +6,11 @@ exe = executable( test_unit, test_unit + '.c', dependencies: test_nm_dep, - link_with: libnms_ibft_core + link_with: libnms_ibft_core, ) test( 'ibft/' + test_unit, test_script, - args: test_args + [exe.full_path()] + args: test_args + [exe.full_path()], ) diff --git a/src/settings/plugins/ibft/tests/test-ibft.c b/src/settings/plugins/ibft/tests/test-ibft.c index 5e46be2e..4c45f574 100644 --- a/src/settings/plugins/ibft/tests/test-ibft.c +++ b/src/settings/plugins/ibft/tests/test-ibft.c @@ -23,7 +23,6 @@ #include <stdio.h> #include <stdarg.h> #include <unistd.h> -#include <string.h> #include <netinet/ether.h> #include <netinet/in.h> #include <arpa/inet.h> diff --git a/src/settings/plugins/ifcfg-rh/meson.build b/src/settings/plugins/ifcfg-rh/meson.build index e84ae80c..9024782a 100644 --- a/src/settings/plugins/ifcfg-rh/meson.build +++ b/src/settings/plugins/ifcfg-rh/meson.build @@ -1,6 +1,6 @@ install_data( 'nm-ifcfg-rh.conf', - install_dir: dbus_conf_dir + install_dir: dbus_conf_dir, ) name = 'nmdbus-ifcfg-rh' @@ -9,7 +9,7 @@ dbus_sources = gnome.gdbus_codegen( name, 'nm-ifcfg-rh.xml', interface_prefix: 'com.redhat', - namespace: 'NMDBus' + namespace: 'NMDBus', ) libnmdbus_ifcfg_rh = static_library( @@ -23,11 +23,11 @@ core_sources = files( 'nms-ifcfg-rh-reader.c', 'nms-ifcfg-rh-utils.c', 'nms-ifcfg-rh-writer.c', - 'shvar.c' + 'shvar.c', ) deps = [ - nm_dep + nm_dep, ] libnms_ifcfg_rh_core = static_library( @@ -46,7 +46,7 @@ libnm_settings_plugin_ifcfg_rh = shared_module( link_args: ldflags_linker_script_settings, link_depends: linker_script_settings, install: true, - install_dir: nm_plugindir + install_dir: nm_plugindir, ) core_plugins += libnm_settings_plugin_ifcfg_rh @@ -56,7 +56,7 @@ core_plugins += libnm_settings_plugin_ifcfg_rh run_target( 'check-local-symbols-settings-ifcfg-rh', command: [check_so_symbols, libnm_settings_plugin_ifcfg_rh.full_path()], - depends: libnm_settings_plugin_ifcfg_rh + depends: libnm_settings_plugin_ifcfg_rh, ) check-local-symbols-settings-ifcfg-rh: src/settings/plugins/ifcfg-rh/libnm-settings-plugin-ifcfg-rh.la diff --git a/src/settings/plugins/ifcfg-rh/nm-inotify-helper.c b/src/settings/plugins/ifcfg-rh/nm-inotify-helper.c index e7a74a1a..04cbb5bc 100644 --- a/src/settings/plugins/ifcfg-rh/nm-inotify-helper.c +++ b/src/settings/plugins/ifcfg-rh/nm-inotify-helper.c @@ -23,9 +23,7 @@ #include "nm-inotify-helper.h" #include <unistd.h> -#include <string.h> #include <sys/inotify.h> -#include <errno.h> #include "NetworkManagerUtils.h" @@ -143,12 +141,12 @@ init_inotify (NMInotifyHelper *self) { NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self); GIOChannel *channel; + int errsv; priv->ifd = inotify_init1 (IN_CLOEXEC); if (priv->ifd == -1) { - int errsv = errno; - - nm_log_warn (LOGD_SETTINGS, "couldn't initialize inotify: %s (%d)", strerror (errsv), errsv); + errsv = errno; + nm_log_warn (LOGD_SETTINGS, "couldn't initialize inotify: %s (%d)", nm_strerror_native (errsv), errsv); return FALSE; } diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-connection.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-connection.c index ca319ddc..4f769c5f 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-connection.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-connection.c @@ -22,7 +22,6 @@ #include "nms-ifcfg-rh-connection.h" -#include <string.h> #include <sys/inotify.h> #include <glib/gstdio.h> diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-plugin.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-plugin.c index 6cac8cb6..89272edb 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-plugin.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-plugin.c @@ -25,9 +25,7 @@ #include "nms-ifcfg-rh-plugin.h" -#include <string.h> #include <unistd.h> -#include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <gmodule.h> @@ -539,7 +537,7 @@ read_connections (SettingsPluginIfcfg *plugin) * iterating over the files. * * To have sensible, reproducible behavior, sort the paths by last modification - * time prefering older files. + * time preferring older files. */ paths = _paths_from_connections (priv->connections); g_ptr_array_sort_with_data (filenames, (GCompareDataFunc) _sort_paths, paths); @@ -602,12 +600,9 @@ load_connection (NMSettingsPlugin *config, { SettingsPluginIfcfg *plugin = SETTINGS_PLUGIN_IFCFG (config); NMIfcfgConnection *connection; - int dir_len = strlen (IFCFG_DIR); char *ifcfg_path; - if ( strncmp (filename, IFCFG_DIR, dir_len) != 0 - || filename[dir_len] != '/' - || strchr (filename + dir_len + 1, '/') != NULL) + if (!nm_utils_file_is_in_path (filename, IFCFG_DIR)) return FALSE; /* get the real ifcfg-path. This allows us to properly @@ -987,7 +982,7 @@ config_changed_cb (NMConfig *config, * won't be offered. * * On SIGHUP and SIGUSR1 try to re-connect to D-Bus. So in the unlikely - * event that the D-Bus conneciton is broken, that allows for recovery + * event that the D-Bus connection is broken, that allows for recovery * without need for restarting NetworkManager. */ if (!NM_FLAGS_ANY (changes, NM_CONFIG_CHANGE_CAUSE_SIGHUP | NM_CONFIG_CHANGE_CAUSE_SIGUSR1)) diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c index 261dfea2..7c1db225 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c @@ -23,13 +23,11 @@ #include "nms-ifcfg-rh-reader.h" #include <stdlib.h> -#include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/wait.h> #include <sys/inotify.h> -#include <errno.h> #include <sys/ioctl.h> #include <unistd.h> @@ -189,7 +187,7 @@ _secret_password_raw_to_bytes (const char *ifcfg_key, password_raw += 2; secret = nm_secret_buf_new (strlen (password_raw) / 2 + 3); - if (!_nm_utils_hexstr2bin_full (password_raw, FALSE, FALSE, ":", 0, secret->bin, secret->len, &len)) { + if (!nm_utils_hexstr2bin_full (password_raw, FALSE, FALSE, ":", 0, secret->bin, secret->len, &len)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid hex password in %s", ifcfg_key); @@ -670,7 +668,7 @@ read_full_ip4_address (shvarFile *ifcfg, &has_key, &a, error)) return FALSE; if (has_key) - *out_gateway = g_strdup (nm_utils_inet4_ntop (a, inet_buf)); + *out_gateway = nm_utils_inet4_ntop_dup (a); } /* Prefix */ @@ -801,7 +799,7 @@ enum { * @options_route: (in-out): when line is from the OPTIONS setting, this is a pre-created * route object that is completed with the settings from options. Otherwise, * it shall point to %NULL and a new route is created and returned. - * @out_route: (out): (transfer-full): (allow-none): the parsed %NMIPRoute instance. + * @out_route: (out) (transfer-full) (allow-none): the parsed %NMIPRoute instance. * In case a @options_route is passed in, it returns the input route that was modified * in-place. But the caller must unref the returned route in either case. * @error: the failure description. @@ -810,7 +808,7 @@ enum { * setting, and one for initscript's handle_ip_file(), which takes the lines * and passes them to `ip route add`. The modes are similar, but certain properties * are not allowed for OPTIONS. - * The mode is differenciated by having an @options_route argument. + * The mode is differentiated by having an @options_route argument. * * Returns: returns a negative errno on failure. On success, it returns 0 * and @out_route. @@ -873,7 +871,7 @@ parse_route_line (const char *line, }; nm_assert (line); - nm_assert (NM_IN_SET (addr_family, AF_INET, AF_INET6)); + nm_assert_addr_family (addr_family); nm_assert (!options_route || nm_ip_route_get_family (options_route) == addr_family); /* initscripts read the legacy route file line-by-line and @@ -1018,6 +1016,7 @@ parse_line_type_addr_with_prefix: if (info->type == PARSE_LINE_TYPE_ADDR) { if (!nm_utils_parse_inaddr_bin (addr_family, s, + NULL, &info->v.addr.addr)) { if ( info == &infos[PARSE_LINE_ATTR_ROUTE_VIA] && nm_streq (s, "(null)")) { @@ -1045,6 +1044,7 @@ parse_line_type_addr_with_prefix: prefix = 0; } else if (!nm_utils_parse_inaddr_prefix_bin (addr_family, s, + NULL, &info->v.addr.addr, &prefix)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, @@ -1150,7 +1150,7 @@ next: : "")); break; case PARSE_LINE_TYPE_FLAG: - /* NOTE: the flag (for "onlink") only allows to explictly set "TRUE". + /* NOTE: the flag (for "onlink") only allows to explicitly set "TRUE". * There is no way to express an explicit "FALSE" setting * of this attribute, hence, the file format cannot encode * that configuration. */ @@ -1425,8 +1425,8 @@ make_user_setting (shvarFile *ifcfg) has_user_data = TRUE; } - return has_user_data - ? g_steal_pointer (&s_user) + return has_user_data + ? NM_SETTING (g_steal_pointer (&s_user)) : NULL; } @@ -1527,7 +1527,6 @@ make_ip4_setting (shvarFile *ifcfg, gboolean never_default; gint64 timeout; int priority; - char inet_buf[NM_UTILS_INET_ADDRSTRLEN]; const char *const *item; guint32 route_table; @@ -1612,7 +1611,7 @@ make_ip4_setting (shvarFile *ifcfg, NULL); if (nm_streq (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) - return g_steal_pointer (&s_ip4); + return NM_SETTING (g_steal_pointer (&s_ip4)); /* Handle DHCP settings */ nm_clear_g_free (&value); @@ -1679,7 +1678,7 @@ make_ip4_setting (shvarFile *ifcfg, PARSE_WARNING ("ignoring GATEWAY (/etc/sysconfig/network) for %s " "because the connection has no static addresses", f); } else - gateway = g_strdup (nm_utils_inet4_ntop (a, inet_buf)); + gateway = nm_utils_inet4_ntop_dup (a); } } } @@ -1803,7 +1802,7 @@ make_ip4_setting (shvarFile *ifcfg, } g_object_set (s_ip4, NM_SETTING_IP_CONFIG_DAD_TIMEOUT, (int) timeout, NULL); - return g_steal_pointer (&s_ip4); + return NM_SETTING (g_steal_pointer (&s_ip4)); } static void @@ -2945,7 +2944,7 @@ make_wep_setting (shvarFile *ifcfg, return NULL; } - return g_steal_pointer (&s_wsec); + return NM_SETTING (g_steal_pointer (&s_wsec)); } static gboolean @@ -3160,7 +3159,7 @@ eap_tls_reader (const char *eap_method, /* FIXME: writer does not actually write IEEE_8021X_CLIENT_CERT_PASSWORD and other * certificate related passwords. It should, because otherwise persisting such profiles * to ifcfg looses information. As this currently only matters for PKCS11 URIs, it seems - * a seldomly used feature so that it is not fixed yet. */ + * a seldom used feature so that it is not fixed yet. */ _secret_set_from_ifcfg (s_8021x, ifcfg, keys_ifcfg, @@ -3535,7 +3534,7 @@ fill_8021x (shvarFile *ifcfg, goto next; /* Some EAP methods don't provide keying material, thus they - * cannot be used with WiFi unless they are an inner method + * cannot be used with Wi-Fi unless they are an inner method * used with TTLS or PEAP or whatever. */ if (wifi && eap->wifi_phase2_only) { @@ -3611,7 +3610,7 @@ make_wpa_setting (shvarFile *ifcfg, gs_unref_object NMSettingWirelessSecurity *wsec = NULL; gs_free char *value = NULL; const char *v; - gboolean wpa_psk = FALSE, wpa_eap = FALSE, ieee8021x = FALSE; + gboolean wpa_psk = FALSE, wpa_sae = FALSE, wpa_eap = FALSE, ieee8021x = FALSE; int i_val; GError *local = NULL; @@ -3619,9 +3618,10 @@ make_wpa_setting (shvarFile *ifcfg, v = svGetValueStr (ifcfg, "KEY_MGMT", &value); wpa_psk = nm_streq0 (v, "WPA-PSK"); + wpa_sae = nm_streq0 (v, "SAE"); wpa_eap = nm_streq0 (v, "WPA-EAP"); ieee8021x = nm_streq0 (v, "IEEE8021X"); - if (!wpa_psk && !wpa_eap && !ieee8021x) + if (!wpa_psk && !wpa_sae && !wpa_eap && !ieee8021x) return NULL; /* Not WPA or Dynamic WEP */ /* WPS */ @@ -3635,7 +3635,7 @@ make_wpa_setting (shvarFile *ifcfg, NULL); /* Pairwise and Group ciphers (only relevant for WPA/RSN) */ - if (wpa_psk || wpa_eap) { + if (wpa_psk || wpa_sae || wpa_eap) { fill_wpa_ciphers (ifcfg, wsec, FALSE, adhoc); fill_wpa_ciphers (ifcfg, wsec, TRUE, adhoc); } @@ -3658,7 +3658,7 @@ make_wpa_setting (shvarFile *ifcfg, nm_setting_wireless_security_add_proto (wsec, "rsn"); } - if (wpa_psk) { + if (wpa_psk || wpa_sae) { NMSettingSecretFlags psk_flags; psk_flags = _secret_read_ifcfg_flags (ifcfg, "WPA_PSK_FLAGS"); @@ -3679,8 +3679,12 @@ make_wpa_setting (shvarFile *ifcfg, if (adhoc) g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-none", NULL); - else + else if (wpa_psk) g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", NULL); + else if (wpa_sae) + g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "sae", NULL); + else + g_assert_not_reached (); } else if (wpa_eap || ieee8021x) { /* Adhoc mode is mutually exclusive with any 802.1x-based authentication */ if (adhoc) { @@ -3945,9 +3949,8 @@ make_wireless_setting (shvarFile *ifcfg, value = svGetValueStr_cp (ifcfg, "CHANNEL"); if (value) { - errno = 0; chan = _nm_utils_ascii_str_to_int64 (value, 10, 1, 196, 0); - if (errno || (chan == 0)) { + if (chan == 0) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid wireless channel '%s'", value); g_free (value); @@ -4991,7 +4994,7 @@ handle_bridge_option (NMSetting *setting, } else { v = _nm_utils_ascii_str_to_int64 (value, 10, 0, 1, -1); if (v == -1) { - error_message = g_strerror (errno); + error_message = nm_strerror_native (errno); goto warn; } } @@ -5003,7 +5006,7 @@ handle_bridge_option (NMSetting *setting, case G_TYPE_UINT: v = _nm_utils_ascii_str_to_int64 (value, 10, 0, G_MAXUINT, -1); if (v == -1) { - error_message = g_strerror (errno); + error_message = nm_strerror_native (errno); goto warn; } if (!nm_g_object_set_property_uint (G_OBJECT (setting), m[i].property_name, v, NULL)) { @@ -5228,16 +5231,14 @@ is_vlan_device (const char *name, shvarFile *parsed) static gboolean is_wifi_device (const char *name, shvarFile *parsed) { - int ifindex; + const NMPlatformLink *pllink; g_return_val_if_fail (name != NULL, FALSE); g_return_val_if_fail (parsed != NULL, FALSE); - ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, name); - if (ifindex == 0) - return FALSE; - - return nm_platform_link_get_type (NM_PLATFORM_GET, ifindex) == NM_LINK_TYPE_WIFI; + pllink = nm_platform_link_get_by_ifname (NM_PLATFORM_GET, name); + return pllink + && pllink->type == NM_LINK_TYPE_WIFI; } static void @@ -5383,7 +5384,7 @@ make_vlan_setting (shvarFile *ifcfg, parse_prio_map_list (s_vlan, ifcfg, "VLAN_INGRESS_PRIORITY_MAP", NM_VLAN_INGRESS_MAP); parse_prio_map_list (s_vlan, ifcfg, "VLAN_EGRESS_PRIORITY_MAP", NM_VLAN_EGRESS_MAP); - return g_steal_pointer (&s_vlan); + return NM_SETTING (g_steal_pointer (&s_vlan)); } static NMConnection * diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c index 49096d26..22c9061b 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c @@ -23,7 +23,6 @@ #include "nms-ifcfg-rh-utils.h" #include <stdlib.h> -#include <string.h> #include "nm-core-internal.h" #include "NetworkManagerUtils.h" diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c index f5be7520..ee7fd161 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c @@ -22,11 +22,9 @@ #include "nms-ifcfg-rh-writer.h" -#include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> -#include <errno.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> @@ -603,6 +601,10 @@ write_wireless_security_setting (NMConnection *connection, svSetValueStr (ifcfg, "KEY_MGMT", "WPA-PSK"); wpa = TRUE; *no_8021x = TRUE; + } else if (!strcmp (key_mgmt, "sae")) { + svSetValueStr (ifcfg, "KEY_MGMT", "SAE"); + wpa = TRUE; + *no_8021x = TRUE; } else if (!strcmp (key_mgmt, "ieee8021x")) { svSetValueStr (ifcfg, "KEY_MGMT", "IEEE8021X"); dynamic_wep = TRUE; diff --git a/src/settings/plugins/ifcfg-rh/shvar.c b/src/settings/plugins/ifcfg-rh/shvar.c index fe82fbdd..f3d58e26 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.c +++ b/src/settings/plugins/ifcfg-rh/shvar.c @@ -27,11 +27,9 @@ #include "shvar.h" -#include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> @@ -215,9 +213,9 @@ _escape_ansic (const char *source) /*****************************************************************************/ -#define _char_req_escape(ch) NM_IN_SET (ch, '\"', '\\', '$', '`') -#define _char_req_escape_old(ch) NM_IN_SET (ch, '\"', '\\', '\'', '$', '`', '~') -#define _char_req_quotes(ch) NM_IN_SET (ch, ' ', '\'', '~', '\t', '|', '&', ';', '(', ')', '<', '>') +#define _char_req_escape(ch) NM_IN_SET (ch, '"', '\\', '$', '`') +#define _char_req_escape_old(ch) NM_IN_SET (ch, '"', '\\', '\'', '$', '`', '~') +#define _char_req_quotes(ch) NM_IN_SET (ch, ' ', '\'', '~', '\t', '|', '&', ';', '(', ')', '<', '>') const char * svEscape (const char *s, char **to_free) @@ -330,7 +328,7 @@ _gstr_init (GString **str, const char *value, gsize i) * Unescaping usually does not extend the length of a string, * so we might be tempted to allocate a fixed buffer of length * (strlen(value)+CONST). - * However, due to $'\Ux' escapes, the maxium length is some + * However, due to $'\Ux' escapes, the maximum length is some * (FACTOR*strlen(value) + CONST), which is non trivial to get * right in all cases. Also, we would have to provision for the * very unlikely extreme case. @@ -453,7 +451,7 @@ svUnescape (const char *value, char **to_free) if (NM_IN_SET (value[i], '$', '`', '"', '\\')) { /* Drop the backslash. */ } else if (NM_IN_SET (value[i], '\'', '~')) { - /* '\'' and '~' in double qoutes are not handled special by shell. + /* '\'' and '~' in double quotes are not handled special by shell. * However, old versions of svEscape() would wrongly use double-quoting * with backslash escaping for these characters (expecting svUnescape() * to remove the backslash). @@ -649,7 +647,7 @@ void _nmtst_svFileSetName (shvarFile *s, const char *fileName) { /* changing the file name is not supported for regular - * operation. Only allowed to use in tests, othewise, + * operation. Only allowed to use in tests, otherwise, * the filename is immutable. */ g_free (s->fileName); s->fileName = g_strdup (fileName); @@ -815,7 +813,7 @@ svOpenFileInternal (const char *name, gboolean create, GError **error) g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errsv), "Could not read file '%s': %s", - name, strerror (errsv)); + name, nm_strerror_native (errsv)); return NULL; } @@ -1317,34 +1315,32 @@ svWriteFile (shvarFile *s, int mode, GError **error) FILE *f; int tmpfd; CList *current; + int errsv; if (s->modified) { if (s->fd == -1) s->fd = open (s->fileName, O_WRONLY | O_CREAT | O_CLOEXEC, mode); if (s->fd == -1) { - int errsv = errno; - + errsv = errno; g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errsv), "Could not open file '%s' for writing: %s", - s->fileName, strerror (errsv)); + s->fileName, nm_strerror_native (errsv)); return FALSE; } if (ftruncate (s->fd, 0) < 0) { - int errsv = errno; - + errsv = errno; g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errsv), "Could not overwrite file '%s': %s", - s->fileName, strerror (errsv)); + s->fileName, nm_strerror_native (errsv)); return FALSE; } tmpfd = fcntl (s->fd, F_DUPFD_CLOEXEC, 0); if (tmpfd == -1) { - int errsv = errno; - + errsv = errno; g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errsv), "Internal error writing file '%s': %s", - s->fileName, strerror (errsv)); + s->fileName, nm_strerror_native (errsv)); return FALSE; } f = fdopen (tmpfd, "w"); diff --git a/src/settings/plugins/ifcfg-rh/tests/meson.build b/src/settings/plugins/ifcfg-rh/tests/meson.build index 29bc9699..f65494bb 100644 --- a/src/settings/plugins/ifcfg-rh/tests/meson.build +++ b/src/settings/plugins/ifcfg-rh/tests/meson.build @@ -6,12 +6,12 @@ exe = executable( test_unit, test_unit + '.c', dependencies: test_nm_dep, - link_with: libnms_ifcfg_rh_core + link_with: libnms_ifcfg_rh_core, ) test( 'ifcfg-rh/' + test_unit, test_script, timeout: 90, - args: test_args + [exe.full_path()] + args: test_args + [exe.full_path()], ) diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_Band_A.cexpected b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_Band_A.cexpected index a95a58db..ddbd986f 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_Band_A.cexpected +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_Band_A.cexpected @@ -13,6 +13,6 @@ IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy -NAME="Test Write WiFi Band A" +NAME="Test Write Wi-Fi Band A" UUID=${UUID} ONBOOT=yes diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_Hidden.cexpected b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_Hidden.cexpected index cf325f35..495a24d3 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_Hidden.cexpected +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_Hidden.cexpected @@ -12,6 +12,6 @@ IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy -NAME="Test Write WiFi Hidden" +NAME="Test Write Wi-Fi Hidden" UUID=${UUID} ONBOOT=yes diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_always.cexpected b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_always.cexpected index f3704f10..aec6918a 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_always.cexpected +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_always.cexpected @@ -13,6 +13,6 @@ IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy -NAME="Test Write WiFi MAC always" +NAME="Test Write Wi-Fi MAC always" UUID=${UUID} ONBOOT=yes diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_default.cexpected b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_default.cexpected index 005c6179..9d47163f 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_default.cexpected +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_default.cexpected @@ -12,6 +12,6 @@ IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy -NAME="Test Write WiFi MAC default" +NAME="Test Write Wi-Fi MAC default" UUID=${UUID} ONBOOT=yes diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_missing.cexpected b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_missing.cexpected index dff17ef2..43c07ddb 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_missing.cexpected +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_missing.cexpected @@ -13,6 +13,6 @@ IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy -NAME="Test Write WiFi MAC missing" +NAME="Test Write Wi-Fi MAC missing" UUID=${UUID} ONBOOT=yes diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_never.cexpected b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_never.cexpected index 94274cf9..21f2e2de 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_never.cexpected +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_WiFi_MAC_never.cexpected @@ -13,6 +13,6 @@ IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy -NAME="Test Write WiFi MAC never" +NAME="Test Write Wi-Fi MAC never" UUID=${UUID} ONBOOT=yes diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-wifi-sae b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-wifi-sae new file mode 100644 index 00000000..68afbe97 --- /dev/null +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-wifi-sae @@ -0,0 +1,5 @@ +TYPE=Wireless +DEVICE=wlan1 +ESSID=blahblah +MODE=Managed +KEY_MGMT=SAE diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-write-unknown-4 b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-write-unknown-4 index a7156e33..2c1b7fb4 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-write-unknown-4 +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-write-unknown-4 @@ -6,7 +6,7 @@ # expected. # # Also note that setting NAME will replace the last -# occurance, and delete all previous once. +# occurrence, and delete all previous once. #L1 NAME=l2 diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-write-unknown-4.expected b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-write-unknown-4.expected index 674df840..cf3f45bd 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-write-unknown-4.expected +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-write-unknown-4.expected @@ -6,7 +6,7 @@ # expected. # # Also note that setting NAME will replace the last -# occurance, and delete all previous once. +# occurrence, and delete all previous once. #L1 diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/keys-test-wifi-sae b/src/settings/plugins/ifcfg-rh/tests/network-scripts/keys-test-wifi-sae new file mode 100644 index 00000000..5a9569ed --- /dev/null +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/keys-test-wifi-sae @@ -0,0 +1 @@ +WPA_PSK="The king is dead." diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/network-test-wired-never-default b/src/settings/plugins/ifcfg-rh/tests/network-scripts/network-test-wired-never-default index 9a292679..4347405e 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/network-test-wired-never-default +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/network-test-wired-never-default @@ -1,4 +1,4 @@ GATEWAYDEV=eth0 -# when devices in IPV6_DEFAULTDEV and IPV6_DEFAULTGW don't match the one in IPV6_DEFAULTGW is prefered +# when devices in IPV6_DEFAULTDEV and IPV6_DEFAULTGW don't match the one in IPV6_DEFAULTGW is preferred IPV6_DEFAULTDEV=eth4 IPV6_DEFAULTGW=2001::1234%eth0 diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index 472bb8a6..b352fbfc 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -23,7 +23,6 @@ #include <stdio.h> #include <stdarg.h> #include <unistd.h> -#include <string.h> #include <linux/pkt_sched.h> #include <netinet/in.h> #include <arpa/inet.h> @@ -123,7 +122,7 @@ _assert_reread_same_FIXME (NMConnection *connection, NMConnection *reread) /* dummy path for an "expected" file, meaning: don't check for expected * written ifcfg file. */ -static const char const NO_EXPECTED[1]; +static const char NO_EXPECTED[1]; static void _assert_expected_content (NMConnection *connection, const char *filename, const char *expected) @@ -2374,7 +2373,7 @@ test_read_wifi_open (void) g_assert_cmpstr (nm_setting_wireless_get_mode (s_wireless), ==, "infrastructure"); g_assert_cmpint (nm_setting_wireless_get_channel (s_wireless), ==, 1); - /* ===== WiFi SECURITY SETTING ===== */ + /* ===== Wi-Fi SECURITY SETTING ===== */ s_wsec = nm_connection_get_setting_wireless_security (connection); g_assert (s_wsec == NULL); @@ -2967,6 +2966,45 @@ test_read_wifi_wpa_psk (void) } static void +test_read_wifi_sae (void) +{ + gs_unref_object NMConnection *connection = NULL; + NMSettingConnection *s_con; + NMSettingWireless *s_wireless; + NMSettingWirelessSecurity *s_wsec; + GBytes *ssid; + const char *expected_ssid = "blahblah"; + + connection = _connection_from_file (TEST_IFCFG_DIR"/ifcfg-test-wifi-sae", + NULL, TYPE_WIRELESS, NULL); + + s_con = nm_connection_get_setting_connection (connection); + g_assert (s_con); + g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "System blahblah (test-wifi-sae)"); + + g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, 0); + g_assert (nm_setting_connection_get_autoconnect (s_con)); + + s_wireless = nm_connection_get_setting_wireless (connection); + g_assert (s_wireless); + + g_assert_cmpint (nm_setting_wireless_get_mtu (s_wireless), ==, 0); + + ssid = nm_setting_wireless_get_ssid (s_wireless); + g_assert (ssid); + g_assert_cmpmem (g_bytes_get_data (ssid, NULL), g_bytes_get_size (ssid), expected_ssid, strlen (expected_ssid)); + + g_assert (!nm_setting_wireless_get_bssid (s_wireless)); + g_assert_cmpstr (nm_setting_wireless_get_mode (s_wireless), ==, "infrastructure"); + + s_wsec = nm_connection_get_setting_wireless_security (connection); + g_assert (s_wsec); + g_assert_cmpstr (nm_setting_wireless_security_get_key_mgmt (s_wsec), ==, "sae"); + g_assert_cmpstr (nm_setting_wireless_security_get_psk (s_wsec), ==, "The king is dead."); + g_assert (!nm_setting_wireless_security_get_auth_alg (s_wsec)); +} + +static void test_read_wifi_wpa_psk_2 (void) { NMConnection *connection; @@ -3275,7 +3313,7 @@ test_read_wifi_dynamic_wep_leap (void) s_wifi = nm_connection_get_setting_wireless (connection); g_assert (s_wifi); - /* ===== WiFi SECURITY SETTING ===== */ + /* ===== Wi-Fi SECURITY SETTING ===== */ s_wsec = nm_connection_get_setting_wireless_security (connection); g_assert (s_wsec); @@ -3501,7 +3539,7 @@ test_write_wifi_hidden (void) nm_connection_add_setting (connection, NM_SETTING (s_con)); g_object_set (s_con, - NM_SETTING_CONNECTION_ID, "Test Write WiFi Hidden", + NM_SETTING_CONNECTION_ID, "Test Write Wi-Fi Hidden", NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_a (), NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NULL); @@ -3584,7 +3622,7 @@ test_write_wifi_mac_random (gconstpointer user_data) s_con = (NMSettingConnection *) nm_setting_connection_new (); nm_connection_add_setting (connection, NM_SETTING (s_con)); - val = g_strdup_printf ("Test Write WiFi MAC %s", name); + val = g_strdup_printf ("Test Write Wi-Fi MAC %s", name); g_object_set (s_con, NM_SETTING_CONNECTION_ID, val, NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_a (), @@ -3803,7 +3841,7 @@ test_write_wifi_band_a (void) nm_connection_add_setting (connection, NM_SETTING (s_con)); g_object_set (s_con, - NM_SETTING_CONNECTION_ID, "Test Write WiFi Band A", + NM_SETTING_CONNECTION_ID, "Test Write Wi-Fi Band A", NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_a (), NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NULL); @@ -7402,8 +7440,6 @@ test_write_mobile_broadband (gconstpointer data) /* GSM setting */ s_gsm = (NMSettingGsm *) nm_setting_gsm_new (); nm_connection_add_setting (connection, NM_SETTING (s_gsm)); - - g_object_set (s_gsm, NM_SETTING_GSM_NUMBER, "*99#", NULL); } else { /* CDMA setting */ s_cdma = (NMSettingCdma *) nm_setting_cdma_new (); @@ -9983,10 +10019,14 @@ NMTST_DEFINE (); int main (int argc, char **argv) { + int errsv; + nmtst_init_assert_logging (&argc, &argv, "INFO", "DEFAULT"); - if (g_mkdir_with_parents (TEST_SCRATCH_DIR_TMP, 0755) != 0) - g_error ("failure to create test directory \"%s\": %s", TEST_SCRATCH_DIR_TMP, g_strerror (errno)); + if (g_mkdir_with_parents (TEST_SCRATCH_DIR_TMP, 0755) != 0) { + errsv = errno; + g_error ("failure to create test directory \"%s\": %s", TEST_SCRATCH_DIR_TMP, nm_strerror_native (errsv)); + } g_test_add_func (TPATH "svUnescape", test_svUnescape); @@ -10083,6 +10123,7 @@ int main (int argc, char **argv) g_test_add_func (TPATH "wifi/read/wpa-psk/unquoted2", test_read_wifi_wpa_psk_unquoted2); g_test_add_func (TPATH "wifi/read/wpa-psk/adhoc", test_read_wifi_wpa_psk_adhoc); g_test_add_func (TPATH "wifi/read/wpa-psk/hex", test_read_wifi_wpa_psk_hex); + g_test_add_func (TPATH "wifi/read/sae", test_read_wifi_sae); g_test_add_func (TPATH "wifi/read/dynamic-wep/leap", test_read_wifi_dynamic_wep_leap); g_test_add_func (TPATH "wifi/read/wpa/eap/tls", test_read_wifi_wpa_eap_tls); g_test_add_func (TPATH "wifi/read/wpa/eap/ttls/tls", test_read_wifi_wpa_eap_ttls_tls); diff --git a/src/settings/plugins/ifupdown/meson.build b/src/settings/plugins/ifupdown/meson.build index 826c7458..42edd438 100644 --- a/src/settings/plugins/ifupdown/meson.build +++ b/src/settings/plugins/ifupdown/meson.build @@ -1,11 +1,11 @@ sources = files( 'nms-ifupdown-interface-parser.c', - 'nms-ifupdown-parser.c' + 'nms-ifupdown-parser.c', ) deps = [ libudev_dep, - nm_dep + nm_dep, ] libnms_ifupdown_core = static_library( @@ -16,7 +16,7 @@ libnms_ifupdown_core = static_library( sources = files( 'nms-ifupdown-connection.c', - 'nms-ifupdown-plugin.c' + 'nms-ifupdown-plugin.c', ) libnm_settings_plugin_ifupdown = shared_module( @@ -27,7 +27,7 @@ libnm_settings_plugin_ifupdown = shared_module( link_args: ldflags_linker_script_settings, link_depends: linker_script_settings, install: true, - install_dir: nm_plugindir + install_dir: nm_plugindir, ) core_plugins += libnm_settings_plugin_ifupdown @@ -37,7 +37,7 @@ core_plugins += libnm_settings_plugin_ifupdown run_target( 'check-local-symbols-settings-ifupdown', command: [check_so_symbols, libnm_settings_plugin_ifupdown.full_path()], - depends: libnm_settings_plugin_ifupdown + depends: libnm_settings_plugin_ifupdown, ) check-local-symbols-settings-ifupdown: src/settings/plugins/ifupdown/libnm-settings-plugin-ifupdown.la diff --git a/src/settings/plugins/ifupdown/nms-ifupdown-connection.c b/src/settings/plugins/ifupdown/nms-ifupdown-connection.c index 1b817044..d06078a9 100644 --- a/src/settings/plugins/ifupdown/nms-ifupdown-connection.c +++ b/src/settings/plugins/ifupdown/nms-ifupdown-connection.c @@ -24,7 +24,6 @@ #include "nms-ifupdown-connection.h" -#include <string.h> #include <glib/gstdio.h> #include "nm-dbus-interface.h" diff --git a/src/settings/plugins/ifupdown/nms-ifupdown-interface-parser.c b/src/settings/plugins/ifupdown/nms-ifupdown-interface-parser.c index 73ecc2f9..6587fc84 100644 --- a/src/settings/plugins/ifupdown/nms-ifupdown-interface-parser.c +++ b/src/settings/plugins/ifupdown/nms-ifupdown-interface-parser.c @@ -26,7 +26,6 @@ #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <wordexp.h> #include <libgen.h> diff --git a/src/settings/plugins/ifupdown/nms-ifupdown-parser.c b/src/settings/plugins/ifupdown/nms-ifupdown-parser.c index 369fa70d..fd5561ae 100644 --- a/src/settings/plugins/ifupdown/nms-ifupdown-parser.c +++ b/src/settings/plugins/ifupdown/nms-ifupdown-parser.c @@ -25,10 +25,8 @@ #include "nms-ifupdown-parser.h" -#include <string.h> #include <arpa/inet.h> #include <stdlib.h> -#include <errno.h> #include <ctype.h> #include "nm-core-internal.h" @@ -63,7 +61,7 @@ _ifupdownplugin_guess_connection_type (if_block *block) { const char *ret_type = NULL; - if(nm_streq0 (ifparser_getkey (block, "inet"), "ppp")) + if (nm_streq0 (ifparser_getkey (block, "inet"), "ppp")) ret_type = NM_SETTING_PPP_SETTING_NAME; else { if_data *ifb; @@ -75,7 +73,7 @@ _ifupdownplugin_guess_connection_type (if_block *block) break; } } - if(!ret_type) + if (!ret_type) ret_type = NM_SETTING_WIRED_SETTING_NAME; } diff --git a/src/settings/plugins/ifupdown/nms-ifupdown-plugin.c b/src/settings/plugins/ifupdown/nms-ifupdown-plugin.c index f0e64d1d..99a59477 100644 --- a/src/settings/plugins/ifupdown/nms-ifupdown-plugin.c +++ b/src/settings/plugins/ifupdown/nms-ifupdown-plugin.c @@ -26,7 +26,6 @@ #include "nms-ifupdown-plugin.h" -#include <string.h> #include <arpa/inet.h> #include <gmodule.h> diff --git a/src/settings/plugins/ifupdown/tests/meson.build b/src/settings/plugins/ifupdown/tests/meson.build index 5a2383d9..9b844c75 100644 --- a/src/settings/plugins/ifupdown/tests/meson.build +++ b/src/settings/plugins/ifupdown/tests/meson.build @@ -4,11 +4,11 @@ exe = executable( test_unit, test_unit + '.c', dependencies: test_nm_dep, - link_with: libnms_ifupdown_core + link_with: libnms_ifupdown_core, ) test( 'ifupdown/' + test_unit, test_script, - args: test_args + [exe.full_path()] + args: test_args + [exe.full_path()], ) diff --git a/src/settings/plugins/ifupdown/tests/test-ifupdown.c b/src/settings/plugins/ifupdown/tests/test-ifupdown.c index 82ee1c4a..674cb19c 100644 --- a/src/settings/plugins/ifupdown/tests/test-ifupdown.c +++ b/src/settings/plugins/ifupdown/tests/test-ifupdown.c @@ -20,8 +20,6 @@ #include "nm-default.h" -#include <string.h> - #include "nm-core-internal.h" #include "settings/plugins/ifupdown/nms-ifupdown-interface-parser.h" diff --git a/src/settings/plugins/keyfile/nms-keyfile-connection.c b/src/settings/plugins/keyfile/nms-keyfile-connection.c index 7511f206..3b362978 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-connection.c +++ b/src/settings/plugins/keyfile/nms-keyfile-connection.c @@ -23,7 +23,6 @@ #include "nms-keyfile-connection.h" -#include <string.h> #include <glib/gstdio.h> #include "nm-dbus-interface.h" diff --git a/src/settings/plugins/keyfile/nms-keyfile-plugin.c b/src/settings/plugins/keyfile/nms-keyfile-plugin.c index 346b78c0..c13cc1ff 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-plugin.c +++ b/src/settings/plugins/keyfile/nms-keyfile-plugin.c @@ -26,8 +26,6 @@ #include <sys/stat.h> #include <unistd.h> #include <sys/types.h> -#include <string.h> - #include <glib/gstdio.h> #include "nm-connection.h" @@ -36,6 +34,7 @@ #include "nm-utils.h" #include "nm-config.h" #include "nm-core-internal.h" +#include "nm-keyfile-internal.h" #include "settings/nm-settings-plugin.h" @@ -171,7 +170,6 @@ update_connection (NMSKeyfilePlugin *self, NMSKeyfileConnection *connection_by_uuid; GError *local = NULL; const char *uuid; - int dir_len; g_return_val_if_fail (!source || NM_IS_CONNECTION (source), NULL); g_return_val_if_fail (full_path || source, NULL); @@ -179,17 +177,8 @@ update_connection (NMSKeyfilePlugin *self, if (full_path) _LOGD ("loading from file \"%s\"...", full_path); - if (g_str_has_prefix (full_path, nms_keyfile_utils_get_path ())) { - dir_len = strlen (nms_keyfile_utils_get_path ()); - } else if (g_str_has_prefix (full_path, NM_CONFIG_KEYFILE_PATH_IN_MEMORY)) { - dir_len = NM_STRLEN (NM_CONFIG_KEYFILE_PATH_IN_MEMORY); - } else { - /* Just make sure the file name is not going go pass the following check. */ - dir_len = strlen (full_path); - } - - if ( full_path[dir_len] != '/' - || strchr (full_path + dir_len + 1, '/') != NULL) { + if ( !nm_utils_file_is_in_path (full_path, nms_keyfile_utils_get_path ()) + && !nm_utils_file_is_in_path (full_path, NM_KEYFILE_PATH_NAME_RUN)) { g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "File not in recognized system-connections directory"); return FALSE; @@ -322,7 +311,7 @@ dir_changed (GFileMonitor *monitor, gboolean exists; full_path = g_file_get_path (file); - if (nms_keyfile_utils_should_ignore_file (full_path, FALSE)) { + if (nm_keyfile_utils_ignore_filename (full_path, FALSE)) { g_free (full_path); return; } @@ -444,7 +433,7 @@ _read_dir (GPtrArray *filenames, } while ((item = g_dir_read_name (dir))) { - if (nms_keyfile_utils_should_ignore_file (item, require_extension)) + if (nm_keyfile_utils_ignore_filename (item, require_extension)) continue; g_ptr_array_add (filenames, g_build_filename (path, item, NULL)); } @@ -467,7 +456,7 @@ read_connections (NMSettingsPlugin *config) filenames = g_ptr_array_new_with_free_func (g_free); - _read_dir (filenames, NM_CONFIG_KEYFILE_PATH_IN_MEMORY, TRUE); + _read_dir (filenames, NM_KEYFILE_PATH_NAME_RUN, TRUE); _read_dir (filenames, nms_keyfile_utils_get_path (), FALSE); alive_connections = g_hash_table_new (nm_direct_hash, NULL); @@ -476,7 +465,7 @@ read_connections (NMSettingsPlugin *config) * iterating over the files. * * To have sensible, reproducible behavior, sort the paths by last modification - * time prefering older files. + * time preferring older files. */ paths = _paths_from_connections (priv->connections); g_ptr_array_sort_with_data (filenames, (GCompareDataFunc) _sort_paths, paths); @@ -523,35 +512,6 @@ get_connections (NMSettingsPlugin *config) } static gboolean -_file_is_in_path (const char *abs_filename, - const char *abs_path) -{ - gsize l; - - /* FIXME: ensure that both paths are at least normalized (coalescing ".", - * duplicate '/', and trailing '/'). */ - - nm_assert (abs_filename && abs_filename[0] == '/'); - nm_assert (abs_path && abs_path[0] == '/'); - - l = strlen (abs_path); - if (strncmp (abs_filename, abs_path, l) != 0) - return FALSE; - - abs_filename += l; - while (abs_filename[0] == '/') - abs_filename++; - - if (!abs_filename[0]) - return FALSE; - - if (strchr (abs_filename, '/')) - return FALSE; - - return TRUE; -} - -static gboolean load_connection (NMSettingsPlugin *config, const char *filename) { @@ -559,25 +519,14 @@ load_connection (NMSettingsPlugin *config, NMSKeyfileConnection *connection; gboolean require_extension; - /* the test whether to require a file extension tries to figure out whether - * the provided filename is inside /etc or /run. - * - * However, on Posix a filename just resolves to an Inode, and there can - * be any kind of paths that point to the same Inode. It's not generally possible - * to check for that (unless, we would stat all files in the target directory - * and see whether their inode matches). - * - * So, when loading the file do something simpler: require that the path - * starts with the well-known prefix. This rejects symlinks or hard links - * which would actually also point to the same file. */ - if (_file_is_in_path (filename, nms_keyfile_utils_get_path ())) + if (nm_utils_file_is_in_path (filename, nms_keyfile_utils_get_path ())) require_extension = FALSE; - else if (_file_is_in_path (filename, NM_CONFIG_KEYFILE_PATH_IN_MEMORY)) + else if (nm_utils_file_is_in_path (filename, NM_KEYFILE_PATH_NAME_RUN)) require_extension = TRUE; else return FALSE; - if (nms_keyfile_utils_should_ignore_file (filename, require_extension)) + if (nm_keyfile_utils_ignore_filename (filename, require_extension)) return FALSE; connection = update_connection (self, NULL, filename, find_by_path (self, filename), TRUE, NULL, NULL); diff --git a/src/settings/plugins/keyfile/nms-keyfile-reader.c b/src/settings/plugins/keyfile/nms-keyfile-reader.c index 580a857a..5778f13c 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-reader.c +++ b/src/settings/plugins/keyfile/nms-keyfile-reader.c @@ -23,7 +23,6 @@ #include "nms-keyfile-reader.h" #include <sys/stat.h> -#include <string.h> #include "nm-keyfile-internal.h" @@ -142,11 +141,11 @@ nms_keyfile_reader_from_keyfile (GKeyFile *key_file, if (!connection) return NULL; - if (g_str_has_suffix (filename, NMS_KEYFILE_PATH_SUFFIX_NMCONNECTION)) { + if (g_str_has_suffix (filename, NM_KEYFILE_PATH_SUFFIX_NMCONNECTION)) { gsize l = strlen (filename); - if (l > NM_STRLEN (NMS_KEYFILE_PATH_SUFFIX_NMCONNECTION)) - filename_id = g_strndup (filename, l - NM_STRLEN (NMS_KEYFILE_PATH_SUFFIX_NMCONNECTION)); + if (l > NM_STRLEN (NM_KEYFILE_PATH_SUFFIX_NMCONNECTION)) + filename_id = g_strndup (filename, l - NM_STRLEN (NM_KEYFILE_PATH_SUFFIX_NMCONNECTION)); } nm_keyfile_read_ensure_id (connection, filename_id ?: filename); @@ -172,7 +171,8 @@ nms_keyfile_reader_from_file (const char *full_filename, nm_assert (full_filename && full_filename[0] == '/'); nm_assert (!profile_dir || profile_dir[0] == '/'); - if (!nms_keyfile_utils_check_file_permissions (full_filename, + if (!nms_keyfile_utils_check_file_permissions (NMS_KEYFILE_FILETYPE_KEYFILE, + full_filename, NULL, error)) return NULL; diff --git a/src/settings/plugins/keyfile/nms-keyfile-utils.c b/src/settings/plugins/keyfile/nms-keyfile-utils.c index c3bfcdee..3c4b0288 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-utils.c +++ b/src/settings/plugins/keyfile/nms-keyfile-utils.c @@ -23,111 +23,223 @@ #include "nms-keyfile-utils.h" #include <stdlib.h> -#include <string.h> #include <sys/stat.h> +#include "nm-keyfile-internal.h" +#include "nm-utils.h" #include "nm-setting-wired.h" #include "nm-setting-wireless.h" #include "nm-setting-wireless-security.h" #include "nm-config.h" -#define NM_CONFIG_KEYFILE_PATH_DEFAULT NMCONFDIR "/system-connections" - /*****************************************************************************/ -static const char temp_letters[] = -"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - -/* - * Check '.[a-zA-Z0-9]{6}' file suffix used for temporary files by g_file_set_contents() (mkstemp()). - */ -static gboolean -check_mkstemp_suffix (const char *path) +char * +nms_keyfile_loaded_uuid_filename (const char *dirname, + const char *uuid, + gboolean temporary) { - const char *ptr; - - g_return_val_if_fail (path != NULL, FALSE); + char filename[250]; + + nm_assert (dirname && dirname[0] == '/'); + nm_assert (uuid && nm_utils_is_uuid (uuid) && !strchr (uuid, '/')); + + if (g_snprintf (filename, + sizeof (filename), + "%s%s%s%s", + NM_KEYFILE_PATH_PREFIX_NMLOADED, + uuid, + NM_KEYFILE_PATH_SUFFIX_NMCONNECTION, + temporary ? "~" : "") >= sizeof (filename)) { + /* valid uuids are limited in length. The buffer should always be large + * enough. */ + nm_assert_not_reached (); + return NULL; + } - /* Matches *.[a-zA-Z0-9]{6} suffix of mkstemp()'s temporary files */ - ptr = strrchr (path, '.'); - if (ptr && (strspn (ptr + 1, temp_letters) == 6) && (! ptr[7])) - return TRUE; - return FALSE; + return g_build_filename (dirname, filename, NULL); } -static gboolean -check_prefix_dot (const char *base) +gboolean +nms_keyfile_loaded_uuid_read (const char *dirname, + const char *filename, + char **out_full_filename, + char **out_uuid, + char **out_loaded_path) { - nm_assert (base && base[0]); + const char *uuid; + const char *tmp; + gsize len; + gs_free char *full_filename = NULL; + gs_free char *ln = NULL; + + nm_assert (dirname && dirname[0] == '/'); + nm_assert (filename && filename[0] && !strchr (filename, '/')); + + if (filename[0] != '.') { + /* the hidden-uuid filename must start with '.'. That is, + * so that it does not conflict with regular keyfiles according + * to nm_keyfile_utils_ignore_filename(). */ + return FALSE; + } + + len = strlen (filename); + if ( len <= NM_STRLEN (NM_KEYFILE_PATH_PREFIX_NMLOADED) + || memcmp (filename, NM_KEYFILE_PATH_PREFIX_NMLOADED, NM_STRLEN (NM_KEYFILE_PATH_PREFIX_NMLOADED)) != 0) { + /* the filename does not have the right prefix. */ + return FALSE; + } + + tmp = &filename[NM_STRLEN (NM_KEYFILE_PATH_PREFIX_NMLOADED)]; + len -= NM_STRLEN (NM_KEYFILE_PATH_PREFIX_NMLOADED); + + if ( len <= NM_STRLEN (NM_KEYFILE_PATH_SUFFIX_NMCONNECTION) + || memcmp (&tmp[len - NM_STRLEN (NM_KEYFILE_PATH_SUFFIX_NMCONNECTION)], + NM_KEYFILE_PATH_SUFFIX_NMCONNECTION, + NM_STRLEN (NM_KEYFILE_PATH_SUFFIX_NMCONNECTION)) != 0) { + /* the file does not have the right suffix. */ + return FALSE; + } + len -= NM_STRLEN (NM_KEYFILE_PATH_SUFFIX_NMCONNECTION); + + if (!NM_IN_SET (len, 36, 40)) { + /* the remaining part of the filename has not the right length to + * contain a UUID (according to nm_utils_is_uuid()). */ + return FALSE; + } + + uuid = nm_strndup_a (100, tmp, len, NULL); + if (!nm_utils_is_uuid (uuid)) + return FALSE; - return base[0] == '.'; + full_filename = g_build_filename (dirname, filename, NULL); + + if (!nms_keyfile_utils_check_file_permissions (NMS_KEYFILE_FILETYPE_NMLOADED, + full_filename, + NULL, + NULL)) + return FALSE; + + ln = nm_utils_read_link_absolute (full_filename, NULL); + if (!ln) + return FALSE; + + NM_SET_OUT (out_uuid, g_strdup (uuid)); + NM_SET_OUT (out_full_filename, g_steal_pointer (&full_filename)); + NM_SET_OUT (out_loaded_path, g_steal_pointer (&ln)); + return TRUE; } -static gboolean -check_suffix (const char *base, const char *tag) +gboolean +nms_keyfile_loaded_uuid_read_from_file (const char *full_filename, + char **out_dirname, + char **out_filename, + char **out_uuid, + char **out_loaded_path) { - int len, tag_len; + gs_free char *dirname = NULL; + gs_free char *filename = NULL; - g_return_val_if_fail (base != NULL, TRUE); - g_return_val_if_fail (tag != NULL, TRUE); + nm_assert (full_filename && full_filename[0] == '/'); - len = strlen (base); - tag_len = strlen (tag); - if ((len > tag_len) && !g_ascii_strcasecmp (base + len - tag_len, tag)) - return TRUE; - return FALSE; -} + filename = g_path_get_basename (full_filename); + dirname = g_path_get_dirname (full_filename); -#define SWP_TAG ".swp" -#define SWPX_TAG ".swpx" -#define PEM_TAG ".pem" -#define DER_TAG ".der" + if (!nms_keyfile_loaded_uuid_read (dirname, + filename, + NULL, + out_uuid, + out_loaded_path)) + return FALSE; + + NM_SET_OUT (out_dirname, g_steal_pointer (&dirname)); + NM_SET_OUT (out_filename, g_steal_pointer (&filename)); + return TRUE; +} gboolean -nms_keyfile_utils_should_ignore_file (const char *filename, gboolean require_extension) +nms_keyfile_loaded_uuid_write (const char *dirname, + const char *uuid, + const char *loaded_path, + gboolean allow_relative, + char **out_full_filename) { - gs_free char *base = NULL; - - g_return_val_if_fail (filename != NULL, TRUE); - - base = g_path_get_basename (filename); - g_return_val_if_fail (base != NULL, TRUE); - - /* Ignore hidden and backup files */ - /* should_ignore_file() must mirror escape_filename() */ - if (check_prefix_dot (base) || check_suffix (base, "~")) - return TRUE; - /* Ignore temporary files */ - if (check_mkstemp_suffix (base)) - return TRUE; - /* Ignore 802.1x certificates and keys */ - if (check_suffix (base, PEM_TAG) || check_suffix (base, DER_TAG)) - return TRUE; - - if (require_extension) { - gsize l = strlen (base); - - if ( l <= NM_STRLEN (NMS_KEYFILE_PATH_SUFFIX_NMCONNECTION) - || !g_str_has_suffix (base, NMS_KEYFILE_PATH_SUFFIX_NMCONNECTION)) - return TRUE; + gs_free char *full_filename_tmp = NULL; + gs_free char *full_filename = NULL; + + nm_assert (dirname && dirname[0] == '/'); + nm_assert (uuid && nm_utils_is_uuid (uuid) && !strchr (uuid, '/')); + nm_assert (!loaded_path || loaded_path[0] == '/'); + + full_filename_tmp = nms_keyfile_loaded_uuid_filename (dirname, uuid, TRUE); + + nm_assert (g_str_has_suffix (full_filename_tmp, "~")); + nm_assert (nm_utils_file_is_in_path (full_filename_tmp, dirname)); + + (void) unlink (full_filename_tmp); + + if (!loaded_path) { + gboolean success = TRUE; + + full_filename_tmp[strlen (full_filename_tmp) - 1] = '\0'; + if (unlink (full_filename_tmp) != 0) + success = NM_IN_SET (errno, ENOENT); + NM_SET_OUT (out_full_filename, g_steal_pointer (&full_filename_tmp)); + return success; + } + + if (allow_relative) { + const char *f; + + f = nm_utils_file_is_in_path (loaded_path, dirname); + if (f) { + /* @loaded_path points to a file directly in @dirname. + * Don't use absolute paths. */ + loaded_path = f; + } + } + + if (symlink (loaded_path, full_filename_tmp) != 0) { + full_filename_tmp[strlen (full_filename_tmp) - 1] = '\0'; + NM_SET_OUT (out_full_filename, g_steal_pointer (&full_filename_tmp)); + return FALSE; } - return FALSE; + full_filename = g_strdup (full_filename_tmp); + full_filename[strlen (full_filename) - 1] = '\0'; + if (rename (full_filename_tmp, full_filename) != 0) { + (void) unlink (full_filename_tmp); + NM_SET_OUT (out_full_filename, g_steal_pointer (&full_filename)); + return FALSE; + } + + NM_SET_OUT (out_full_filename, g_steal_pointer (&full_filename)); + return TRUE; } /*****************************************************************************/ gboolean -nms_keyfile_utils_check_file_permissions_stat (const struct stat *st, +nms_keyfile_utils_check_file_permissions_stat (NMSKeyfileFiletype filetype, + const struct stat *st, GError **error) { g_return_val_if_fail (st, FALSE); - if (!S_ISREG (st->st_mode)) { - g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "file is not a regular file"); - return FALSE; - } + if (filetype == NMS_KEYFILE_FILETYPE_KEYFILE) { + if (!S_ISREG (st->st_mode)) { + g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "file is not a regular file"); + return FALSE; + } + } else if (filetype == NMS_KEYFILE_FILETYPE_NMLOADED) { + if (!S_ISLNK (st->st_mode)) { + g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "file is not a slink"); + return FALSE; + } + } else + g_return_val_if_reached (FALSE); if (!NM_FLAGS_HAS (nm_utils_get_testing (), NM_UTILS_TEST_NO_KEYFILE_OWNER_CHECK)) { if (st->st_uid != 0) { @@ -137,7 +249,8 @@ nms_keyfile_utils_check_file_permissions_stat (const struct stat *st, return FALSE; } - if (st->st_mode & 0077) { + if ( filetype == NMS_KEYFILE_FILETYPE_KEYFILE + && (st->st_mode & 0077)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "File permissions (%03o) are insecure", st->st_mode); @@ -149,7 +262,8 @@ nms_keyfile_utils_check_file_permissions_stat (const struct stat *st, } gboolean -nms_keyfile_utils_check_file_permissions (const char *filename, +nms_keyfile_utils_check_file_permissions (NMSKeyfileFiletype filetype, + const char *filename, struct stat *out_st, GError **error) { @@ -158,14 +272,24 @@ nms_keyfile_utils_check_file_permissions (const char *filename, g_return_val_if_fail (filename && filename[0] == '/', FALSE); - if (stat (filename, &st) != 0) { - errsv = errno; - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "cannot access file: %s", g_strerror (errsv)); - return FALSE; - } + if (filetype == NMS_KEYFILE_FILETYPE_KEYFILE) { + if (stat (filename, &st) != 0) { + errsv = errno; + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "cannot access file: %s", nm_strerror_native (errsv)); + return FALSE; + } + } else if (filetype == NMS_KEYFILE_FILETYPE_NMLOADED) { + if (lstat (filename, &st) != 0) { + errsv = errno; + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "cannot access file: %s", nm_strerror_native (errsv)); + return FALSE; + } + } else + g_return_val_if_reached (FALSE); - if (!nms_keyfile_utils_check_file_permissions_stat (&st, error)) + if (!nms_keyfile_utils_check_file_permissions_stat (filetype, &st, error)) return FALSE; NM_SET_OUT (out_st, st); @@ -174,50 +298,6 @@ nms_keyfile_utils_check_file_permissions (const char *filename, /*****************************************************************************/ -char * -nms_keyfile_utils_escape_filename (const char *filename, - gboolean with_extension) -{ - GString *str; - const char *f = filename; - /* keyfile used to escape with '*', do not change that behavior. - * - * But for newly added escapings, use '_' instead. - * Also, @with_extension is new-style. */ - const char ESCAPE_CHAR = with_extension ? '_' : '*'; - const char ESCAPE_CHAR2 = '_'; - - g_return_val_if_fail (filename && filename[0], NULL); - - str = g_string_sized_new (60); - - /* Convert '/' to ESCAPE_CHAR */ - for (f = filename; f[0]; f++) { - if (f[0] == '/') - g_string_append_c (str, ESCAPE_CHAR); - else - g_string_append_c (str, f[0]); - } - - /* escape_filename() must avoid anything that should_ignore_file() would reject. - * We can escape here more aggressivly then what we would read back. */ - if (check_prefix_dot (str->str)) - str->str[0] = ESCAPE_CHAR2; - if (check_suffix (str->str, "~")) - str->str[str->len - 1] = ESCAPE_CHAR2; - if ( check_mkstemp_suffix (str->str) - || check_suffix (str->str, PEM_TAG) - || check_suffix (str->str, DER_TAG)) - g_string_append_c (str, ESCAPE_CHAR2); - - if (with_extension) - g_string_append (str, NMS_KEYFILE_PATH_SUFFIX_NMCONNECTION); - - return g_string_free (str, FALSE);; -} - -/*****************************************************************************/ - const char * nms_keyfile_utils_get_path (void) { @@ -229,7 +309,7 @@ nms_keyfile_utils_get_path (void) NM_CONFIG_KEYFILE_KEY_KEYFILE_PATH, NM_CONFIG_GET_VALUE_STRIP | NM_CONFIG_GET_VALUE_NO_EMPTY); if (!path) - path = g_strdup (""NM_CONFIG_KEYFILE_PATH_DEFAULT""); + path = g_strdup (""NM_KEYFILE_PATH_NAME_ETC_DEFAULT""); } return path; } diff --git a/src/settings/plugins/keyfile/nms-keyfile-utils.h b/src/settings/plugins/keyfile/nms-keyfile-utils.h index 297dd4ea..bc601dad 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-utils.h +++ b/src/settings/plugins/keyfile/nms-keyfile-utils.h @@ -23,27 +23,52 @@ #include "NetworkManagerUtils.h" -#define NM_CONFIG_KEYFILE_PATH_IN_MEMORY NMRUNDIR "/system-connections" - -#define NMS_KEYFILE_PATH_SUFFIX_NMCONNECTION ".nmconnection" - #define NMS_KEYFILE_CONNECTION_LOG_PATH(path) ((path) ?: "in-memory") #define NMS_KEYFILE_CONNECTION_LOG_FMT "%s (%s,\"%s\")" #define NMS_KEYFILE_CONNECTION_LOG_ARG(con) NMS_KEYFILE_CONNECTION_LOG_PATH (nm_settings_connection_get_filename ((NMSettingsConnection *) (con))), nm_settings_connection_get_uuid ((NMSettingsConnection *) (con)), nm_settings_connection_get_id ((NMSettingsConnection *) (con)) #define NMS_KEYFILE_CONNECTION_LOG_FMTD "%s (%s,\"%s\",%p)" #define NMS_KEYFILE_CONNECTION_LOG_ARGD(con) NMS_KEYFILE_CONNECTION_LOG_PATH (nm_settings_connection_get_filename ((NMSettingsConnection *) (con))), nm_settings_connection_get_uuid ((NMSettingsConnection *) (con)), nm_settings_connection_get_id ((NMSettingsConnection *) (con)), (con) -gboolean nms_keyfile_utils_should_ignore_file (const char *filename, gboolean require_extension); - -char *nms_keyfile_utils_escape_filename (const char *filename, gboolean with_extension); +typedef enum { + NMS_KEYFILE_FILETYPE_KEYFILE, + NMS_KEYFILE_FILETYPE_NMLOADED, +} NMSKeyfileFiletype; const char *nms_keyfile_utils_get_path (void); +/*****************************************************************************/ + +char *nms_keyfile_loaded_uuid_filename (const char *dirname, + const char *uuid, + gboolean temporary); + +gboolean nms_keyfile_loaded_uuid_read (const char *dirname, + const char *filename, + char **out_full_filename, + char **out_uuid, + char **out_loaded_path); + +gboolean nms_keyfile_loaded_uuid_read_from_file (const char *full_filename, + char **out_dirname, + char **out_filename, + char **out_uuid, + char **out_loaded_path); + +gboolean nms_keyfile_loaded_uuid_write (const char *dirname, + const char *uuid, + const char *loaded_path, + gboolean allow_relative, + char **out_full_filename); + +/*****************************************************************************/ + struct stat; -gboolean nms_keyfile_utils_check_file_permissions_stat (const struct stat *st, +gboolean nms_keyfile_utils_check_file_permissions_stat (NMSKeyfileFiletype filetype, + const struct stat *st, GError **error); -gboolean nms_keyfile_utils_check_file_permissions (const char *filename, +gboolean nms_keyfile_utils_check_file_permissions (NMSKeyfileFiletype filetype, + const char *filename, struct stat *out_st, GError **error); diff --git a/src/settings/plugins/keyfile/nms-keyfile-writer.c b/src/settings/plugins/keyfile/nms-keyfile-writer.c index df26ea60..8c75d8c7 100644 --- a/src/settings/plugins/keyfile/nms-keyfile-writer.c +++ b/src/settings/plugins/keyfile/nms-keyfile-writer.c @@ -26,8 +26,6 @@ #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> -#include <errno.h> -#include <string.h> #include "nm-keyfile-internal.h" @@ -177,27 +175,30 @@ _internal_write_connection (NMConnection *connection, uid_t owner_uid, pid_t owner_grp, const char *existing_path, + gboolean existing_path_read_only, gboolean force_rename, char **out_path, NMConnection **out_reread, gboolean *out_reread_same, GError **error) { - gs_unref_keyfile GKeyFile *key_file = NULL; - gs_free char *data = NULL; - gsize len; + gs_unref_keyfile GKeyFile *kf_file = NULL; + gs_free char *kf_content_buf = NULL; + gsize kf_content_len; gs_free char *path = NULL; const char *id; WriteInfo info = { 0 }; GError *local_err = NULL; int errsv; - gboolean rename = force_rename; + gboolean rename; g_return_val_if_fail (!out_path || !*out_path, FALSE); g_return_val_if_fail (keyfile_dir && keyfile_dir[0] == '/', FALSE); - if (existing_path && !g_str_has_prefix (existing_path, keyfile_dir)) - rename = TRUE; + rename = force_rename + || existing_path_read_only + || ( existing_path + && !nm_utils_file_is_in_path (existing_path, keyfile_dir)); switch (_nm_connection_verify (connection, error)) { case NM_SETTING_VERIFY_NORMALIZABLE: @@ -214,11 +215,11 @@ _internal_write_connection (NMConnection *connection, info.keyfile_dir = keyfile_dir; - key_file = nm_keyfile_write (connection, _handler_write, &info, error); - if (!key_file) + kf_file = nm_keyfile_write (connection, _handler_write, &info, error); + if (!kf_file) return FALSE; - data = g_key_file_to_data (key_file, &len, error); - if (!data) + kf_content_buf = g_key_file_to_data (kf_file, &kf_content_len, error); + if (!kf_content_buf) return FALSE; if (!g_file_test (keyfile_dir, G_FILE_TEST_IS_DIR)) @@ -227,13 +228,14 @@ _internal_write_connection (NMConnection *connection, /* If we have existing file path, use it. Else generate one from * connection's ID. */ - if (existing_path != NULL && !rename) { + if ( existing_path + && !rename) path = g_strdup (existing_path); - } else { - char *filename_escaped = nms_keyfile_utils_escape_filename (id, with_extension); + else { + gs_free char *filename_escaped = NULL; + filename_escaped = nm_keyfile_utils_create_filename (id, with_extension); path = g_build_filename (keyfile_dir, filename_escaped, NULL); - g_free (filename_escaped); } /* If a file with this path already exists (but isn't the existing path @@ -243,32 +245,34 @@ _internal_write_connection (NMConnection *connection, * there's a race here, but there's not a lot we can do about it, and * we shouldn't get more than one connection with the same UUID either. */ - if (g_strcmp0 (path, existing_path) != 0 && g_file_test (path, G_FILE_TEST_EXISTS)) { + if ( !nm_streq0 (path, existing_path) + && g_file_test (path, G_FILE_TEST_EXISTS)) { guint i; gboolean name_found = FALSE; /* A keyfile with this connection's ID already exists. Pick another name. */ for (i = 0; i < 100; i++) { - char *filename, *filename_escaped; + gs_free char *filename_escaped = NULL; + gs_free char *filename = NULL; if (i == 0) filename = g_strdup_printf ("%s-%s", id, nm_connection_get_uuid (connection)); else filename = g_strdup_printf ("%s-%s-%u", id, nm_connection_get_uuid (connection), i); - filename_escaped = nms_keyfile_utils_escape_filename (filename, with_extension); + filename_escaped = nm_keyfile_utils_create_filename (filename, with_extension); g_free (path); path = g_strdup_printf ("%s/%s", keyfile_dir, filename_escaped); - g_free (filename); - g_free (filename_escaped); - if (g_strcmp0 (path, existing_path) == 0 || !g_file_test (path, G_FILE_TEST_EXISTS)) { + + if ( nm_streq0 (path, existing_path) + || !g_file_test (path, G_FILE_TEST_EXISTS)) { name_found = TRUE; break; } } if (!name_found) { - if (existing_path == NULL) { + if (existing_path_read_only || !existing_path) { /* this really should not happen, we tried hard to find an unused name... bail out. */ g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "could not find suitable keyfile file name (%s already used)", path); @@ -281,13 +285,7 @@ _internal_write_connection (NMConnection *connection, } } - /* In case of updating the connection and changing the file path, - * we need to remove the old one, not to end up with two connections. - */ - if (existing_path != NULL && strcmp (path, existing_path) != 0) - unlink (existing_path); - - nm_utils_file_set_contents (path, data, len, 0600, &local_err); + nm_utils_file_set_contents (path, kf_content_buf, kf_content_len, 0600, &local_err); if (local_err) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "error writing to file '%s': %s", @@ -300,17 +298,24 @@ _internal_write_connection (NMConnection *connection, errsv = errno; g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "error chowning '%s': %s (%d)", - path, g_strerror (errsv), errsv); + path, nm_strerror_native (errsv), errsv); unlink (path); return FALSE; } - if (out_reread || out_reread_same) - { + /* In case of updating the connection and changing the file path, + * we need to remove the old one, not to end up with two connections. + */ + if ( existing_path + && !existing_path_read_only + && !nm_streq (path, existing_path)) + unlink (existing_path); + + if (out_reread || out_reread_same) { gs_unref_object NMConnection *reread = NULL; gboolean reread_same = FALSE; - reread = nms_keyfile_reader_from_keyfile (key_file, path, NULL, profile_dir, FALSE, NULL); + reread = nms_keyfile_reader_from_keyfile (kf_file, path, NULL, profile_dir, FALSE, NULL); nm_assert (NM_IS_CONNECTION (reread)); @@ -356,7 +361,7 @@ nms_keyfile_writer_connection (NMConnection *connection, if (save_to_disk) keyfile_dir = nms_keyfile_utils_get_path (); else - keyfile_dir = NM_CONFIG_KEYFILE_PATH_IN_MEMORY; + keyfile_dir = NM_KEYFILE_PATH_NAME_RUN; return _internal_write_connection (connection, keyfile_dir, @@ -365,6 +370,7 @@ nms_keyfile_writer_connection (NMConnection *connection, 0, 0, existing_path, + FALSE, force_rename, out_path, out_reread, @@ -390,6 +396,7 @@ nms_keyfile_writer_test_connection (NMConnection *connection, owner_grp, NULL, FALSE, + FALSE, out_path, out_reread, out_reread_same, diff --git a/src/settings/plugins/keyfile/tests/meson.build b/src/settings/plugins/keyfile/tests/meson.build index 8b94b256..752b6d7b 100644 --- a/src/settings/plugins/keyfile/tests/meson.build +++ b/src/settings/plugins/keyfile/tests/meson.build @@ -11,5 +11,6 @@ exe = executable( test( 'keyfile/' + test_unit, test_script, - args: test_args + [exe.full_path()] + args: test_args + [exe.full_path()], + timeout: default_test_timeout, ) diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index 4a0e01b3..baecac13 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -23,7 +23,6 @@ #include <stdio.h> #include <stdarg.h> #include <unistd.h> -#include <string.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/socket.h> @@ -1177,7 +1176,6 @@ test_write_bt_dun_connection (void) NM_SETTING_GSM_APN, "internet2.voicestream.com", NM_SETTING_GSM_USERNAME, "george.clinton", NM_SETTING_GSM_PASSWORD, "parliament", - NM_SETTING_GSM_NUMBER, "*99#", NULL); write_test_connection_and_reread (connection, TRUE); @@ -1259,7 +1257,6 @@ test_write_gsm_connection (void) NM_SETTING_GSM_APN, "internet2.voicestream.com", NM_SETTING_GSM_USERNAME, "george.clinton.again", NM_SETTING_GSM_PASSWORD, "parliament2", - NM_SETTING_GSM_NUMBER, "*99#", NM_SETTING_GSM_PIN, "123456", NM_SETTING_GSM_NETWORK_ID, "254098", NM_SETTING_GSM_HOME_ONLY, TRUE, @@ -2073,7 +2070,7 @@ test_write_new_wireless_group_names (void) NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NULL); - /* WiFi setting */ + /* Wi-Fi setting */ s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); @@ -2084,7 +2081,7 @@ test_write_new_wireless_group_names (void) NULL); g_bytes_unref (ssid); - /* WiFi security setting */ + /* Wi-Fi security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); nm_connection_add_setting (connection, NM_SETTING (s_wsec)); g_object_set (s_wsec, @@ -2333,7 +2330,6 @@ test_write_flags_property (void) s_gsm = nm_setting_gsm_new (); nm_connection_add_setting (connection, s_gsm); g_object_set (s_gsm, - NM_SETTING_GSM_NUMBER, "#99*", NM_SETTING_GSM_APN, "myapn", NM_SETTING_GSM_USERNAME, "adfasdfasdf", NM_SETTING_GSM_PASSWORD_FLAGS, NM_SETTING_SECRET_FLAG_NOT_SAVED | NM_SETTING_SECRET_FLAG_NOT_REQUIRED, @@ -2466,18 +2462,18 @@ _escape_filename (gboolean with_extension, const char *filename, gboolean would_ g_assert (filename && filename[0]); - if (!!would_be_ignored != !!nms_keyfile_utils_should_ignore_file (filename, with_extension)) { + if (!!would_be_ignored != !!nm_keyfile_utils_ignore_filename (filename, with_extension)) { if (would_be_ignored) g_error ("We expect filename \"%s\" to be ignored, but it isn't", filename); else g_error ("We expect filename \"%s\" not to be ignored, but it is", filename); } - esc = nms_keyfile_utils_escape_filename (filename, with_extension); + esc = nm_keyfile_utils_create_filename (filename, with_extension); g_assert (esc && esc[0]); g_assert (!strchr (esc, '/')); - if (nms_keyfile_utils_should_ignore_file (esc, with_extension)) + if (nm_keyfile_utils_ignore_filename (esc, with_extension)) g_error ("Escaping filename \"%s\" yielded \"%s\", but this is ignored", filename, esc); } @@ -2503,21 +2499,131 @@ test_nm_keyfile_plugin_utils_escape_filename (void) _escape_filename (FALSE, ".#emacs-locking", TRUE); _escape_filename (FALSE, "file-with-tilde~", TRUE); _escape_filename (FALSE, ".file-with-dot", TRUE); + _escape_filename (FALSE, "/some/path/with/trailing/slash/", TRUE); + _escape_filename (FALSE, "/some/path/without/trailing/slash", FALSE); _escape_filename (TRUE, "lala", TRUE); } /*****************************************************************************/ +static void +_assert_keyfile_loaded_uuid (const char *dirname, + const char *uuid, + const char *loaded_path, + gboolean allow_relative, + const char *exp_full_filename, + const char *exp_uuid, + const char *exp_symlink_target, + const char *exp_loaded_path) +{ + gs_free char *full_filename = NULL; + gs_free char *symlink_target = NULL; + gs_free char *uuid2 = NULL; + gs_free char *loaded_path2 = NULL; + gs_free char *dirname3 = NULL; + gs_free char *filename3 = NULL; + gs_free char *uuid3 = NULL; + gs_free char *loaded_path3 = NULL; + gboolean success; + gs_free char *filename = NULL; + + g_assert (dirname && dirname[0] == '/'); + g_assert (exp_full_filename && exp_full_filename[0]); + g_assert (!exp_loaded_path || exp_loaded_path[0] == '/'); + + filename = g_path_get_basename (exp_full_filename); + + full_filename = nms_keyfile_loaded_uuid_filename (dirname, uuid, FALSE); + g_assert_cmpstr (full_filename, ==, full_filename); + nm_clear_g_free (&full_filename); + + + g_assert (nms_keyfile_loaded_uuid_write (dirname, uuid, loaded_path, allow_relative, &full_filename)); + g_assert_cmpstr (full_filename, ==, exp_full_filename); + nm_clear_g_free (&full_filename); + + if (exp_symlink_target) + g_assert (g_file_test (exp_full_filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_SYMLINK)); + else + g_assert (!g_file_test (exp_full_filename, G_FILE_TEST_EXISTS)); + symlink_target = g_file_read_link (exp_full_filename, NULL); + g_assert_cmpstr (symlink_target, ==, exp_symlink_target); + + + success = nms_keyfile_loaded_uuid_read (dirname, filename, &full_filename, &uuid2, &loaded_path2); + g_assert_cmpint (!!exp_uuid, ==, success); + if (success) + g_assert_cmpstr (full_filename, ==, exp_full_filename); + else + g_assert_cmpstr (full_filename, ==, NULL); + nm_clear_g_free (&full_filename); + g_assert_cmpstr (uuid2, ==, exp_uuid); + g_assert_cmpstr (loaded_path2, ==, exp_loaded_path); + + + success = nms_keyfile_loaded_uuid_read_from_file (exp_full_filename, &dirname3, &filename3, &uuid3, &loaded_path3); + g_assert_cmpint (!!exp_uuid, ==, success); + if (success) { + g_assert_cmpstr (dirname3, ==, dirname); + g_assert_cmpstr (filename3, ==, filename); + } else { + g_assert_cmpstr (dirname3, ==, NULL); + g_assert_cmpstr (filename3, ==, NULL); + } + g_assert_cmpstr (uuid3, ==, exp_uuid); + g_assert_cmpstr (loaded_path3, ==, exp_loaded_path); +} + +static void +test_loaded_uuid (void) +{ + const char *uuid = "3c03fd17-ddc3-4100-a954-88b6fafff959"; + gs_free char *filename = g_strdup_printf ("%s%s%s", + NM_KEYFILE_PATH_PREFIX_NMLOADED, + uuid, + NM_KEYFILE_PATH_SUFFIX_NMCONNECTION); + gs_free char *full_filename = g_strdup_printf ("%s/%s", + TEST_SCRATCH_DIR, + filename); + const char *loaded_path0 = NM_KEYFILE_PATH_NMLOADED_NULL; + const char *loaded_path1 = "/some/where/but/not/scratch/dir"; + const char *filename2 = "foo1"; + gs_free char *loaded_path2 = g_strdup_printf ("%s/%s", + TEST_SCRATCH_DIR, + filename2); + + _assert_keyfile_loaded_uuid (TEST_SCRATCH_DIR, uuid, NULL, FALSE, full_filename, NULL, NULL, NULL); + _assert_keyfile_loaded_uuid (TEST_SCRATCH_DIR, uuid, NULL, TRUE, full_filename, NULL, NULL, NULL); + + _assert_keyfile_loaded_uuid (TEST_SCRATCH_DIR, uuid, loaded_path0, FALSE, full_filename, uuid, loaded_path0, loaded_path0); + _assert_keyfile_loaded_uuid (TEST_SCRATCH_DIR, uuid, loaded_path0, TRUE, full_filename, uuid, loaded_path0, loaded_path0); + + _assert_keyfile_loaded_uuid (TEST_SCRATCH_DIR, uuid, loaded_path1, FALSE, full_filename, uuid, loaded_path1, loaded_path1); + _assert_keyfile_loaded_uuid (TEST_SCRATCH_DIR, uuid, loaded_path1, TRUE, full_filename, uuid, loaded_path1, loaded_path1); + + _assert_keyfile_loaded_uuid (TEST_SCRATCH_DIR, uuid, loaded_path2, FALSE, full_filename, uuid, loaded_path2, loaded_path2); + _assert_keyfile_loaded_uuid (TEST_SCRATCH_DIR, uuid, loaded_path2, TRUE, full_filename, uuid, filename2, loaded_path2); + + (void) unlink (full_filename); +} + +/*****************************************************************************/ + NMTST_DEFINE (); int main (int argc, char **argv) { + int errsv; + _nm_utils_set_testing (NM_UTILS_TEST_NO_KEYFILE_OWNER_CHECK); + nmtst_init_assert_logging (&argc, &argv, "INFO", "DEFAULT"); - if (g_mkdir_with_parents (TEST_SCRATCH_DIR, 0755) != 0) - g_error ("failure to create test directory \"%s\": %s", TEST_SCRATCH_DIR, g_strerror (errno)); + if (g_mkdir_with_parents (TEST_SCRATCH_DIR, 0755) != 0) { + errsv = errno; + g_error ("failure to create test directory \"%s\": %s", TEST_SCRATCH_DIR, nm_strerror_native (errsv)); + } /* The tests */ g_test_add_func ("/keyfile/test_read_valid_wired_connection", test_read_valid_wired_connection); @@ -2591,6 +2697,7 @@ int main (int argc, char **argv) g_test_add_func ("/keyfile/test_nm_keyfile_plugin_utils_escape_filename", test_nm_keyfile_plugin_utils_escape_filename); + g_test_add_func ("/keyfile/test_loaded_uuid", test_loaded_uuid); + return g_test_run (); } - |