static NMSettingsConnection * _internal_new_connection (SCPluginKeyfile *self, const char *full_path, NMConnection *source, GError **error) { SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self); NMKeyfileConnection *connection; g_return_val_if_fail (full_path != NULL, NULL); connection = nm_keyfile_connection_new (full_path, source, error); if (connection) { g_hash_table_insert (priv->hash, (gpointer) nm_keyfile_connection_get_path (connection), connection); } return (NMSettingsConnection *) connection; }
/* update_connection: * @self: the plugin instance * @source: if %NULL, this re-reads the connection from @full_path * and updates it. When passing @source, this adds a connection from * memory. * @full_path: the filename of the keyfile to be loaded * @connection: an existing connection that might be updated. * If given, @connection must be an existing connection that is currently * owned by the plugin. * @protect_existing_connection: if %TRUE, and !@connection, we don't allow updating * an existing connection with the same UUID. * If %TRUE and @connection, allow updating only if the reload would modify * @connection (without changing its UUID) or if we would create a new connection. * In other words, if this paramter is %TRUE, we only allow creating a * new connection (with an unseen UUID) or updating the passed in @connection * (whereas the UUID cannot change). * Note, that this allows for @connection to be replaced by a new connection. * @protected_connections: (allow-none): if given, we only update an * existing connection if it is not contained in this hash. * @error: error in case of failure * * Loads a connection from file @full_path. This can both be used to * load a connection initially or to update an existing connection. * * If you pass in an existing connection and the reloaded file happens * to have a different UUID, the connection is deleted. * Beware, that means that after the function, you have a dangling pointer * if the returned connection is different from @connection. * * Returns: the updated connection. * */ static NMKeyfileConnection * update_connection (SCPluginKeyfile *self, NMConnection *source, const char *full_path, NMKeyfileConnection *connection, gboolean protect_existing_connection, GHashTable *protected_connections, GError **error) { SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self); NMKeyfileConnection *connection_new; NMKeyfileConnection *connection_by_uuid; GError *local = NULL; const char *uuid; g_return_val_if_fail (!source || NM_IS_CONNECTION (source), NULL); g_return_val_if_fail (full_path || source, NULL); if (full_path) nm_log_dbg (LOGD_SETTINGS, "keyfile: loading from file \"%s\"...", full_path); connection_new = nm_keyfile_connection_new (source, full_path, &local); if (!connection_new) { /* Error; remove the connection */ if (source) nm_log_warn (LOGD_SETTINGS, "keyfile: error creating connection %s: %s", nm_connection_get_uuid (source), local->message); else nm_log_warn (LOGD_SETTINGS, "keyfile: error loading connection from file %s: %s", full_path, local->message); if ( connection && !protect_existing_connection && (!protected_connections || !g_hash_table_contains (protected_connections, connection))) remove_connection (self, connection); g_propagate_error (error, local); return NULL; } uuid = nm_connection_get_uuid (NM_CONNECTION (connection_new)); connection_by_uuid = g_hash_table_lookup (priv->connections, uuid); if ( connection && connection != connection_by_uuid) { if ( (protect_existing_connection && connection_by_uuid != NULL) || (protected_connections && g_hash_table_contains (protected_connections, connection))) { NMKeyfileConnection *conflicting = (protect_existing_connection && connection_by_uuid != NULL) ? connection_by_uuid : connection; if (source) nm_log_warn (LOGD_SETTINGS, "keyfile: cannot update protected "NM_KEYFILE_CONNECTION_LOG_FMT" connection due to conflicting UUID %s", NM_KEYFILE_CONNECTION_LOG_ARG (conflicting), uuid); else nm_log_warn (LOGD_SETTINGS, "keyfile: cannot load %s due to conflicting UUID for "NM_KEYFILE_CONNECTION_LOG_FMT, full_path, NM_KEYFILE_CONNECTION_LOG_ARG (conflicting)); g_object_unref (connection_new); g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "Cannot update protected connection due to conflicting UUID"); return NULL; } /* The new connection has a different UUID then the original one. * Remove @connection. */ remove_connection (self, connection); } if ( connection_by_uuid && ( (!connection && protect_existing_connection) || (protected_connections && g_hash_table_contains (protected_connections, connection_by_uuid)))) { if (source) nm_log_warn (LOGD_SETTINGS, "keyfile: cannot update connection due to conflicting UUID for "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_by_uuid)); else nm_log_warn (LOGD_SETTINGS, "keyfile: cannot load %s due to conflicting UUID for "NM_KEYFILE_CONNECTION_LOG_FMT, full_path, NM_KEYFILE_CONNECTION_LOG_ARG (connection_by_uuid)); g_object_unref (connection_new); g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "Skip updating protected connection during reload"); return NULL; } if (connection_by_uuid) { const char *old_path; old_path = nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_by_uuid)); if (nm_connection_compare (NM_CONNECTION (connection_by_uuid), NM_CONNECTION (connection_new), NM_SETTING_COMPARE_FLAG_IGNORE_AGENT_OWNED_SECRETS | NM_SETTING_COMPARE_FLAG_IGNORE_NOT_SAVED_SECRETS)) { /* Nothing to do... except updating the path. */ if (old_path && g_strcmp0 (old_path, full_path) != 0) nm_log_info (LOGD_SETTINGS, "keyfile: rename \"%s\" to "NM_KEYFILE_CONNECTION_LOG_FMT" without other changes", old_path, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); } else { /* An existing connection changed. */ if (source) nm_log_info (LOGD_SETTINGS, "keyfile: update "NM_KEYFILE_CONNECTION_LOG_FMT" from %s", NM_KEYFILE_CONNECTION_LOG_ARG (connection_new), NM_KEYFILE_CONNECTION_LOG_PATH (old_path)); else if (!g_strcmp0 (old_path, nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_new)))) nm_log_info (LOGD_SETTINGS, "keyfile: update "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); else if (old_path) nm_log_info (LOGD_SETTINGS, "keyfile: rename \"%s\" to "NM_KEYFILE_CONNECTION_LOG_FMT, old_path, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); else nm_log_info (LOGD_SETTINGS, "keyfile: update and persist "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); if (!nm_settings_connection_replace_settings (NM_SETTINGS_CONNECTION (connection_by_uuid), NM_CONNECTION (connection_new), FALSE, /* don't set Unsaved */ "keyfile-update", &local)) { /* Shouldn't ever get here as 'connection_new' was verified by the reader already * and the UUID did not change. */ g_assert_not_reached (); } g_assert_no_error (local); } nm_settings_connection_set_filename (NM_SETTINGS_CONNECTION (connection_by_uuid), full_path); g_object_unref (connection_new); return connection_by_uuid; } else { if (source) nm_log_info (LOGD_SETTINGS, "keyfile: add connection "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); else nm_log_info (LOGD_SETTINGS, "keyfile: new connection "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); g_hash_table_insert (priv->connections, g_strdup (uuid), connection_new); g_signal_connect (connection_new, NM_SETTINGS_CONNECTION_REMOVED, G_CALLBACK (connection_removed_cb), self); if (!source) { /* Only raise the signal if we were called without source, i.e. if we read the connection from file. * Otherwise, we were called by add_connection() which does not expect the signal. */ g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, connection_new); } return connection_new; } }