1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager system settings service
4 * Søren Sandmann <sandmann@daimi.au.dk>
5 * Dan Williams <dcbw@redhat.com>
6 * Tambet Ingo <tambet@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * (C) Copyright 2007 - 2011 Red Hat, Inc.
23 * (C) Copyright 2008 Novell, Inc.
26 #include "nm-default.h"
36 #include <selinux/selinux.h>
39 #include "nm-dbus-interface.h"
40 #include "nm-connection.h"
41 #include "nm-setting-8021x.h"
42 #include "nm-setting-bluetooth.h"
43 #include "nm-setting-cdma.h"
44 #include "nm-setting-connection.h"
45 #include "nm-setting-gsm.h"
46 #include "nm-setting-ip4-config.h"
47 #include "nm-setting-ip6-config.h"
48 #include "nm-setting-olpc-mesh.h"
49 #include "nm-setting-ppp.h"
50 #include "nm-setting-pppoe.h"
51 #include "nm-setting-serial.h"
52 #include "nm-setting-vpn.h"
53 #include "nm-setting-wired.h"
54 #include "nm-setting-adsl.h"
55 #include "nm-setting-wireless.h"
56 #include "nm-setting-wireless-security.h"
57 #include "nm-setting-bond.h"
59 #include "nm-core-internal.h"
61 #include "nm-device-ethernet.h"
62 #include "nm-settings.h"
63 #include "nm-settings-connection.h"
64 #include "nm-settings-plugin.h"
65 #include "nm-bus-manager.h"
66 #include "nm-auth-utils.h"
67 #include "nm-auth-subject.h"
68 #include "nm-session-monitor.h"
69 #include "plugins/keyfile/plugin.h"
70 #include "nm-agent-manager.h"
71 #include "nm-connection-provider.h"
72 #include "nm-config.h"
73 #include "nm-audit-manager.h"
74 #include "NetworkManagerUtils.h"
75 #include "nm-dispatcher.h"
77 #include "nmdbus-settings.h"
79 #define _NMLOG_DOMAIN LOGD_SETTINGS
80 #define _NMLOG_PREFIX_NAME "settings"
81 #define _NMLOG(level, ...) \
83 nm_log ((level), _NMLOG_DOMAIN, \
84 "%s" _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
85 _NMLOG_PREFIX_NAME": " \
86 _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
89 /* LINKER CRACKROCK */
90 #define EXPORT(sym) void * __export_##sym = &sym;
92 #include "nm-inotify-helper.h"
93 EXPORT(nm_inotify_helper_get_type)
94 EXPORT(nm_inotify_helper_get)
95 EXPORT(nm_inotify_helper_add_watch)
96 EXPORT(nm_inotify_helper_remove_watch)
98 EXPORT(nm_settings_connection_get_type)
99 EXPORT(nm_settings_connection_replace_settings)
100 EXPORT(nm_settings_connection_replace_and_commit)
101 /* END LINKER CRACKROCK */
103 #define HOSTNAMED_SERVICE_NAME "org.freedesktop.hostname1"
104 #define HOSTNAMED_SERVICE_PATH "/org/freedesktop/hostname1"
105 #define HOSTNAMED_SERVICE_INTERFACE "org.freedesktop.hostname1"
107 #define HOSTNAME_FILE_DEFAULT "/etc/hostname"
108 #define HOSTNAME_FILE_SUSE "/etc/HOSTNAME"
109 #define HOSTNAME_FILE_GENTOO "/etc/conf.d/hostname"
110 #define IFCFG_DIR SYSCONFDIR "/sysconfig/network"
111 #define CONF_DHCP IFCFG_DIR "/dhcp"
113 #define PLUGIN_MODULE_PATH "plugin-module-path"
115 #if defined(HOSTNAME_PERSIST_SUSE)
116 #define HOSTNAME_FILE HOSTNAME_FILE_SUSE
117 #elif defined(HOSTNAME_PERSIST_GENTOO)
118 #define HOSTNAME_FILE HOSTNAME_FILE_GENTOO
120 #define HOSTNAME_FILE HOSTNAME_FILE_DEFAULT
123 static void claim_connection (NMSettings *self,
124 NMSettingsConnection *connection);
126 static void unmanaged_specs_changed (NMSettingsPlugin *config, gpointer user_data);
127 static void unrecognized_specs_changed (NMSettingsPlugin *config, gpointer user_data);
129 static void connection_provider_iface_init (NMConnectionProviderInterface *cp_iface);
131 G_DEFINE_TYPE_EXTENDED (NMSettings, nm_settings, NM_TYPE_EXPORTED_OBJECT, 0,
132 G_IMPLEMENT_INTERFACE (NM_TYPE_CONNECTION_PROVIDER, connection_provider_iface_init))
136 NMAgentManager *agent_mgr;
143 gboolean connections_loaded;
144 GHashTable *connections;
145 GSList *unmanaged_specs;
146 GSList *unrecognized_specs;
147 GSList *get_connections_cache;
150 gboolean startup_complete;
155 GFileMonitor *monitor;
156 GFileMonitor *dhcp_monitor;
158 gulong dhcp_monitor_id;
159 GDBusProxy *hostnamed_proxy;
163 #define NM_SETTINGS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTINGS, NMSettingsPrivate))
168 CONNECTION_UPDATED_BY_USER,
170 CONNECTION_VISIBILITY_CHANGED,
173 NEW_CONNECTION, /* exported, not used internally */
176 static guint signals[LAST_SIGNAL] = { 0 };
180 PROP_UNMANAGED_SPECS,
184 PROP_STARTUP_COMPLETE,
190 check_startup_complete (NMSettings *self)
192 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
194 NMSettingsConnection *conn;
196 if (priv->startup_complete)
199 g_hash_table_iter_init (&iter, priv->connections);
200 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &conn)) {
201 if (!nm_settings_connection_get_ready (conn))
205 priv->startup_complete = TRUE;
206 g_object_notify (G_OBJECT (self), NM_SETTINGS_STARTUP_COMPLETE);
210 connection_ready_changed (NMSettingsConnection *conn,
214 NMSettings *self = NM_SETTINGS (user_data);
216 if (nm_settings_connection_get_ready (conn))
217 check_startup_complete (self);
221 plugin_connection_added (NMSettingsPlugin *config,
222 NMSettingsConnection *connection,
225 claim_connection (NM_SETTINGS (user_data), connection);
229 load_connections (NMSettings *self)
231 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
234 for (iter = priv->plugins; iter; iter = g_slist_next (iter)) {
235 NMSettingsPlugin *plugin = NM_SETTINGS_PLUGIN (iter->data);
236 GSList *plugin_connections;
239 plugin_connections = nm_settings_plugin_get_connections (plugin);
241 // FIXME: ensure connections from plugins loaded with a lower priority
242 // get rejected when they conflict with connections from a higher
245 for (elt = plugin_connections; elt; elt = g_slist_next (elt))
246 claim_connection (self, NM_SETTINGS_CONNECTION (elt->data));
248 g_slist_free (plugin_connections);
250 g_signal_connect (plugin, NM_SETTINGS_PLUGIN_CONNECTION_ADDED,
251 G_CALLBACK (plugin_connection_added), self);
252 g_signal_connect (plugin, NM_SETTINGS_PLUGIN_UNMANAGED_SPECS_CHANGED,
253 G_CALLBACK (unmanaged_specs_changed), self);
254 g_signal_connect (plugin, NM_SETTINGS_PLUGIN_UNRECOGNIZED_SPECS_CHANGED,
255 G_CALLBACK (unrecognized_specs_changed), self);
258 priv->connections_loaded = TRUE;
260 unmanaged_specs_changed (NULL, self);
261 unrecognized_specs_changed (NULL, self);
265 nm_settings_for_each_connection (NMSettings *self,
266 NMSettingsForEachFunc for_each_func,
269 NMSettingsPrivate *priv;
273 g_return_if_fail (NM_IS_SETTINGS (self));
274 g_return_if_fail (for_each_func != NULL);
276 priv = NM_SETTINGS_GET_PRIVATE (self);
278 g_hash_table_iter_init (&iter, priv->connections);
279 while (g_hash_table_iter_next (&iter, NULL, &data))
280 for_each_func (self, NM_SETTINGS_CONNECTION (data), user_data);
284 impl_settings_list_connections (NMSettings *self,
285 GDBusMethodInvocation *context)
287 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
288 GPtrArray *connections;
292 connections = g_ptr_array_sized_new (g_hash_table_size (priv->connections) + 1);
293 g_hash_table_iter_init (&iter, priv->connections);
294 while (g_hash_table_iter_next (&iter, &key, NULL))
295 g_ptr_array_add (connections, key);
296 g_ptr_array_add (connections, NULL);
298 g_dbus_method_invocation_return_value (context,
299 g_variant_new ("(^ao)", connections->pdata));
300 g_ptr_array_unref (connections);
303 NMSettingsConnection *
304 nm_settings_get_connection_by_uuid (NMSettings *self, const char *uuid)
306 NMSettingsPrivate *priv;
307 NMSettingsConnection *candidate;
310 g_return_val_if_fail (NM_IS_SETTINGS (self), NULL);
311 g_return_val_if_fail (uuid != NULL, NULL);
313 priv = NM_SETTINGS_GET_PRIVATE (self);
315 g_hash_table_iter_init (&iter, priv->connections);
316 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &candidate)) {
317 if (g_strcmp0 (uuid, nm_settings_connection_get_uuid (candidate)) == 0)
325 impl_settings_get_connection_by_uuid (NMSettings *self,
326 GDBusMethodInvocation *context,
329 NMSettingsConnection *connection = NULL;
330 NMAuthSubject *subject = NULL;
331 GError *error = NULL;
332 char *error_desc = NULL;
334 connection = nm_settings_get_connection_by_uuid (self, uuid);
336 error = g_error_new_literal (NM_SETTINGS_ERROR,
337 NM_SETTINGS_ERROR_INVALID_CONNECTION,
338 "No connection with the UUID was found.");
342 subject = nm_auth_subject_new_unix_process_from_context (context);
344 error = g_error_new_literal (NM_SETTINGS_ERROR,
345 NM_SETTINGS_ERROR_PERMISSION_DENIED,
346 "Unable to determine UID of request.");
350 if (!nm_auth_is_subject_in_acl (NM_CONNECTION (connection),
353 error = g_error_new_literal (NM_SETTINGS_ERROR,
354 NM_SETTINGS_ERROR_PERMISSION_DENIED,
360 g_clear_object (&subject);
361 g_dbus_method_invocation_return_value (
363 g_variant_new ("(o)", nm_connection_get_path (NM_CONNECTION (connection))));
368 g_dbus_method_invocation_take_error (context, error);
369 g_clear_object (&subject);
373 connection_sort (gconstpointer pa, gconstpointer pb)
375 NMConnection *a = NM_CONNECTION (pa);
376 NMSettingConnection *con_a;
377 NMConnection *b = NM_CONNECTION (pb);
378 NMSettingConnection *con_b;
379 guint64 ts_a = 0, ts_b = 0;
380 gboolean can_ac_a, can_ac_b;
382 con_a = nm_connection_get_setting_connection (a);
384 con_b = nm_connection_get_setting_connection (b);
387 can_ac_a = !!nm_setting_connection_get_autoconnect (con_a);
388 can_ac_b = !!nm_setting_connection_get_autoconnect (con_b);
389 if (can_ac_a != can_ac_b)
390 return can_ac_a ? -1 : 1;
392 nm_settings_connection_get_timestamp (NM_SETTINGS_CONNECTION (pa), &ts_a);
393 nm_settings_connection_get_timestamp (NM_SETTINGS_CONNECTION (pb), &ts_b);
396 else if (ts_a == ts_b)
401 /* Returns a list of NMSettingsConnections.
402 * The list is sorted in the order suitable for auto-connecting, i.e.
403 * first go connections with autoconnect=yes and most recent timestamp.
404 * Caller must free the list with g_slist_free().
407 nm_settings_get_connections (NMSettings *self)
410 gpointer data = NULL;
413 g_return_val_if_fail (NM_IS_SETTINGS (self), NULL);
415 g_hash_table_iter_init (&iter, NM_SETTINGS_GET_PRIVATE (self)->connections);
416 while (g_hash_table_iter_next (&iter, NULL, &data))
417 list = g_slist_insert_sorted (list, data, connection_sort);
421 NMSettingsConnection *
422 nm_settings_get_connection_by_path (NMSettings *self, const char *path)
424 NMSettingsPrivate *priv;
426 g_return_val_if_fail (NM_IS_SETTINGS (self), NULL);
427 g_return_val_if_fail (path != NULL, NULL);
429 priv = NM_SETTINGS_GET_PRIVATE (self);
431 return (NMSettingsConnection *) g_hash_table_lookup (priv->connections, path);
435 nm_settings_has_connection (NMSettings *self, NMSettingsConnection *connection)
437 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
441 g_hash_table_iter_init (&iter, priv->connections);
442 while (g_hash_table_iter_next (&iter, NULL, &data))
443 if (data == connection)
450 nm_settings_get_unmanaged_specs (NMSettings *self)
452 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
454 return priv->unmanaged_specs;
457 static NMSettingsPlugin *
458 get_plugin (NMSettings *self, guint32 capability)
460 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
463 g_return_val_if_fail (self != NULL, NULL);
465 /* Do any of the plugins support the given capability? */
466 for (iter = priv->plugins; iter; iter = iter->next) {
467 NMSettingsPluginCapabilities caps = NM_SETTINGS_PLUGIN_CAP_NONE;
469 g_object_get (G_OBJECT (iter->data), NM_SETTINGS_PLUGIN_CAPABILITIES, &caps, NULL);
470 if (NM_FLAGS_ALL (caps, capability))
471 return NM_SETTINGS_PLUGIN (iter->data);
477 #if defined(HOSTNAME_PERSIST_GENTOO)
479 read_hostname_gentoo (const char *path)
481 gchar *contents = NULL, *result = NULL, *tmp;
482 gchar **all_lines = NULL;
485 if (!g_file_get_contents (path, &contents, NULL, NULL))
487 all_lines = g_strsplit (contents, "\n", 0);
488 line_num = g_strv_length (all_lines);
489 for (i = 0; i < line_num; i++) {
490 g_strstrip (all_lines[i]);
491 if (all_lines[i][0] == '#' || all_lines[i][0] == '\0')
493 if (g_str_has_prefix (all_lines[i], "hostname=")) {
494 tmp = &all_lines[i][NM_STRLEN ("hostname=")];
495 result = g_shell_unquote (tmp, NULL);
499 g_strfreev (all_lines);
505 #if defined(HOSTNAME_PERSIST_SUSE)
507 hostname_is_dynamic (void)
511 gboolean dynamic = FALSE;
513 channel = g_io_channel_new_file (CONF_DHCP, "r", NULL);
517 while (g_io_channel_read_line (channel, &str, NULL, NULL, NULL) != G_IO_STATUS_EOF) {
520 if (g_str_has_prefix (str, "DHCLIENT_SET_HOSTNAME="))
521 dynamic = strcmp (&str[NM_STRLEN ("DHCLIENT_SET_HOSTNAME=")], "\"yes\"") == 0;
526 g_io_channel_shutdown (channel, FALSE, NULL);
527 g_io_channel_unref (channel);
533 /* Returns an allocated string which the caller owns and must eventually free */
535 nm_settings_get_hostname (NMSettings *self)
537 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
538 char *hostname = NULL;
543 if (priv->hostname.hostnamed_proxy) {
544 hostname = g_strdup (priv->hostname.value);
548 #if defined(HOSTNAME_PERSIST_GENTOO)
549 hostname = read_hostname_gentoo (priv->hostname.file);
552 #if defined(HOSTNAME_PERSIST_SUSE)
553 if (priv->hostname.dhcp_monitor_id && hostname_is_dynamic ())
556 if (g_file_get_contents (priv->hostname.file, &hostname, NULL, NULL))
557 g_strchomp (hostname);
559 #endif /* HOSTNAME_PERSIST_GENTOO */
562 if (hostname && !hostname[0]) {
571 find_spec (GSList *spec_list, const char *spec)
575 for (iter = spec_list; iter; iter = g_slist_next (iter)) {
576 if (!strcmp ((const char *) iter->data, spec))
583 update_specs (NMSettings *self, GSList **specs_ptr,
584 GSList * (*get_specs_func) (NMSettingsPlugin *))
586 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
589 g_slist_free_full (*specs_ptr, g_free);
592 for (iter = priv->plugins; iter; iter = g_slist_next (iter)) {
593 GSList *specs, *specs_iter;
595 specs = get_specs_func (NM_SETTINGS_PLUGIN (iter->data));
596 for (specs_iter = specs; specs_iter; specs_iter = specs_iter->next) {
597 if (!find_spec (*specs_ptr, (const char *) specs_iter->data)) {
598 *specs_ptr = g_slist_prepend (*specs_ptr, specs_iter->data);
600 g_free (specs_iter->data);
603 g_slist_free (specs);
608 unmanaged_specs_changed (NMSettingsPlugin *config,
611 NMSettings *self = NM_SETTINGS (user_data);
612 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
614 update_specs (self, &priv->unmanaged_specs,
615 nm_settings_plugin_get_unmanaged_specs);
616 g_object_notify (G_OBJECT (self), NM_SETTINGS_UNMANAGED_SPECS);
620 unrecognized_specs_changed (NMSettingsPlugin *config,
623 NMSettings *self = NM_SETTINGS (user_data);
624 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
626 update_specs (self, &priv->unrecognized_specs,
627 nm_settings_plugin_get_unrecognized_specs);
631 add_plugin (NMSettings *self, NMSettingsPlugin *plugin)
633 NMSettingsPrivate *priv;
638 g_return_val_if_fail (NM_IS_SETTINGS (self), FALSE);
639 g_return_val_if_fail (NM_IS_SETTINGS_PLUGIN (plugin), FALSE);
641 priv = NM_SETTINGS_GET_PRIVATE (self);
643 if (g_slist_find (priv->plugins, plugin)) {
644 /* don't add duplicates. */
648 priv->plugins = g_slist_append (priv->plugins, g_object_ref (plugin));
649 nm_settings_plugin_init (plugin);
651 g_object_get (G_OBJECT (plugin),
652 NM_SETTINGS_PLUGIN_NAME, &pname,
653 NM_SETTINGS_PLUGIN_INFO, &pinfo,
656 path = g_object_get_data (G_OBJECT (plugin), PLUGIN_MODULE_PATH);
658 _LOGI ("loaded plugin %s: %s%s%s%s", pname, pinfo,
659 NM_PRINT_FMT_QUOTED (path, " (", path, ")", ""));
667 find_plugin (GSList *list, const char *pname)
672 g_return_val_if_fail (pname != NULL, NULL);
674 for (iter = list; iter && !obj; iter = g_slist_next (iter)) {
675 NMSettingsPlugin *plugin = NM_SETTINGS_PLUGIN (iter->data);
676 char *list_pname = NULL;
678 g_object_get (G_OBJECT (plugin),
679 NM_SETTINGS_PLUGIN_NAME,
682 if (list_pname && !strcmp (pname, list_pname))
683 obj = G_OBJECT (plugin);
692 add_keyfile_plugin (NMSettings *self)
694 gs_unref_object GObject *keyfile_plugin = NULL;
696 keyfile_plugin = nm_settings_keyfile_plugin_new ();
697 g_assert (keyfile_plugin);
698 if (!add_plugin (self, NM_SETTINGS_PLUGIN (keyfile_plugin)))
699 g_return_if_reached ();
703 load_plugins (NMSettings *self, const char **plugins, GError **error)
707 gboolean keyfile_added = FALSE;
708 gboolean success = TRUE;
709 gboolean add_ibft = FALSE;
710 gboolean has_no_ibft;
711 gssize idx_no_ibft, idx_ibft;
713 idx_ibft = _nm_utils_strv_find_first ((char **) plugins, -1, "ibft");
714 idx_no_ibft = _nm_utils_strv_find_first ((char **) plugins, -1, "no-ibft");
715 has_no_ibft = idx_no_ibft >= 0 && idx_no_ibft > idx_ibft;
716 #if WITH_SETTINGS_PLUGIN_IBFT
717 add_ibft = idx_no_ibft < 0 && idx_ibft < 0;
720 for (iter = plugins; iter && *iter; iter++) {
721 const char *pname = *iter;
724 if (!*pname || strchr (pname, '/')) {
725 _LOGW ("ignore invalid plugin \"%s\"", pname);
729 if (!strcmp (pname, "ifcfg-suse")) {
730 _LOGW ("skipping deprecated plugin ifcfg-suse");
734 if (!strcmp (pname, "no-ibft"))
736 if (has_no_ibft && !strcmp (pname, "ibft"))
739 /* keyfile plugin is built-in now */
740 if (strcmp (pname, "keyfile") == 0) {
741 if (!keyfile_added) {
742 add_keyfile_plugin (self);
743 keyfile_added = TRUE;
748 if (_nm_utils_strv_find_first ((char **) plugins,
751 /* the plugin is already mentioned in the list previously.
752 * Don't load a duplicate. */
756 if (find_plugin (list, pname))
762 gs_free char *full_name = NULL;
763 gs_free char *path = NULL;
764 GObject * (*factory_func) (void);
768 full_name = g_strdup_printf ("nm-settings-plugin-%s", pname);
769 path = g_module_build_path (NMPLUGINDIR, full_name);
771 if (stat (path, &st) != 0) {
773 _LOGW ("could not load plugin '%s' from file '%s': %s", pname, path, strerror (errsv));
776 if (!S_ISREG (st.st_mode)) {
777 _LOGW ("could not load plugin '%s' from file '%s': not a file", pname, path);
780 if (st.st_uid != 0) {
781 _LOGW ("could not load plugin '%s' from file '%s': file must be owned by root", pname, path);
784 if (st.st_mode & (S_IWGRP | S_IWOTH | S_ISUID)) {
785 _LOGW ("could not load plugin '%s' from file '%s': invalid file permissions", pname, path);
789 plugin = g_module_open (path, G_MODULE_BIND_LOCAL);
791 _LOGW ("could not load plugin '%s' from file '%s': %s",
792 pname, path, g_module_error ());
796 /* errors after this point are fatal, because we loaded the shared library already. */
798 if (!g_module_symbol (plugin, "nm_settings_plugin_factory", (gpointer) (&factory_func))) {
799 g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
800 "Could not find plugin '%s' factory function.",
803 g_module_close (plugin);
807 obj = (*factory_func) ();
808 if (!obj || !NM_IS_SETTINGS_PLUGIN (obj)) {
809 g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
810 "Plugin '%s' returned invalid system config object.",
813 g_module_close (plugin);
817 g_module_make_resident (plugin);
818 g_object_weak_ref (obj, (GWeakNotify) g_module_close, plugin);
819 g_object_set_data_full (obj, PLUGIN_MODULE_PATH, path, g_free);
821 if (add_plugin (self, NM_SETTINGS_PLUGIN (obj)))
822 list = g_slist_append (list, obj);
824 g_object_unref (obj);
827 if (add_ibft && !strcmp (pname, "ifcfg-rh")) {
828 /* The plugin ibft is not explicitly mentioned but we just enabled "ifcfg-rh".
829 * Enable "ibft" by default after "ifcfg-rh". */
836 /* If keyfile plugin was not among configured plugins, add it as the last one */
838 add_keyfile_plugin (self);
840 g_slist_free_full (list, g_object_unref);
846 connection_updated (NMSettingsConnection *connection, gpointer user_data)
848 /* Re-emit for listeners like NMPolicy */
849 g_signal_emit (NM_SETTINGS (user_data),
850 signals[CONNECTION_UPDATED],
853 g_signal_emit_by_name (NM_SETTINGS (user_data), NM_CP_SIGNAL_CONNECTION_UPDATED, connection);
857 connection_updated_by_user (NMSettingsConnection *connection, gpointer user_data)
859 /* Re-emit for listeners like NMPolicy */
860 g_signal_emit (NM_SETTINGS (user_data),
861 signals[CONNECTION_UPDATED_BY_USER],
867 connection_visibility_changed (NMSettingsConnection *connection,
871 /* Re-emit for listeners like NMPolicy */
872 g_signal_emit (NM_SETTINGS (user_data),
873 signals[CONNECTION_VISIBILITY_CHANGED],
879 connection_removed (NMSettingsConnection *connection, gpointer user_data)
881 NMSettings *self = NM_SETTINGS (user_data);
882 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
883 const char *cpath = nm_connection_get_path (NM_CONNECTION (connection));
885 if (!g_hash_table_lookup (priv->connections, cpath))
886 g_return_if_reached ();
887 g_object_ref (connection);
889 /* Disconnect signal handlers, as plugins might still keep references
890 * to the connection (and thus the signal handlers would still be live)
891 * even after NMSettings has dropped all its references.
894 g_signal_handlers_disconnect_by_func (connection, G_CALLBACK (connection_removed), self);
895 g_signal_handlers_disconnect_by_func (connection, G_CALLBACK (connection_updated), self);
896 g_signal_handlers_disconnect_by_func (connection, G_CALLBACK (connection_updated_by_user), self);
897 g_signal_handlers_disconnect_by_func (connection, G_CALLBACK (connection_visibility_changed), self);
898 g_signal_handlers_disconnect_by_func (connection, G_CALLBACK (connection_ready_changed), self);
899 g_object_unref (self);
901 /* Forget about the connection internally */
902 g_hash_table_remove (priv->connections, (gpointer) cpath);
905 g_signal_emit (self, signals[CONNECTION_REMOVED], 0, connection);
907 /* Re-emit for listeners like NMPolicy */
908 g_signal_emit_by_name (self, NM_CP_SIGNAL_CONNECTION_REMOVED, connection);
909 g_object_notify (G_OBJECT (self), NM_SETTINGS_CONNECTIONS);
910 if (nm_exported_object_is_exported (NM_EXPORTED_OBJECT (connection)))
911 nm_exported_object_unexport (NM_EXPORTED_OBJECT (connection));
913 check_startup_complete (self);
915 g_object_unref (connection);
919 secret_agent_registered (NMAgentManager *agent_mgr,
920 NMSecretAgent *agent,
923 /* Re-emit for listeners like NMPolicy */
924 g_signal_emit (NM_SETTINGS (user_data),
925 signals[AGENT_REGISTERED],
930 #define NM_DBUS_SERVICE_OPENCONNECT "org.freedesktop.NetworkManager.openconnect"
931 #define NM_OPENCONNECT_KEY_GATEWAY "gateway"
932 #define NM_OPENCONNECT_KEY_COOKIE "cookie"
933 #define NM_OPENCONNECT_KEY_GWCERT "gwcert"
934 #define NM_OPENCONNECT_KEY_XMLCONFIG "xmlconfig"
935 #define NM_OPENCONNECT_KEY_LASTHOST "lasthost"
936 #define NM_OPENCONNECT_KEY_AUTOCONNECT "autoconnect"
937 #define NM_OPENCONNECT_KEY_CERTSIGS "certsigs"
940 openconnect_migrate_hack (NMConnection *connection)
943 NMSettingSecretFlags flags = NM_SETTING_SECRET_FLAG_NOT_SAVED;
945 /* Huge hack. There were some openconnect changes that needed to happen
946 * pretty late, too late to get into distros. Migration has already
947 * happened for many people, and their secret flags are wrong. But we
948 * don't want to requrie re-migration, so we have to fix it up here. Ugh.
951 s_vpn = nm_connection_get_setting_vpn (connection);
955 if (g_strcmp0 (nm_setting_vpn_get_service_type (s_vpn), NM_DBUS_SERVICE_OPENCONNECT) == 0) {
956 /* These are different for every login session, and should not be stored */
957 nm_setting_set_secret_flags (NM_SETTING (s_vpn), NM_OPENCONNECT_KEY_GATEWAY, flags, NULL);
958 nm_setting_set_secret_flags (NM_SETTING (s_vpn), NM_OPENCONNECT_KEY_COOKIE, flags, NULL);
959 nm_setting_set_secret_flags (NM_SETTING (s_vpn), NM_OPENCONNECT_KEY_GWCERT, flags, NULL);
961 /* These are purely internal data for the auth-dialog, and should be stored */
963 nm_setting_set_secret_flags (NM_SETTING (s_vpn), NM_OPENCONNECT_KEY_XMLCONFIG, flags, NULL);
964 nm_setting_set_secret_flags (NM_SETTING (s_vpn), NM_OPENCONNECT_KEY_LASTHOST, flags, NULL);
965 nm_setting_set_secret_flags (NM_SETTING (s_vpn), NM_OPENCONNECT_KEY_AUTOCONNECT, flags, NULL);
966 nm_setting_set_secret_flags (NM_SETTING (s_vpn), NM_OPENCONNECT_KEY_CERTSIGS, flags, NULL);
971 claim_connection (NMSettings *self, NMSettingsConnection *connection)
973 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
974 GError *error = NULL;
978 NMSettingsConnection *existing;
980 g_return_if_fail (NM_IS_SETTINGS_CONNECTION (connection));
981 g_return_if_fail (nm_connection_get_path (NM_CONNECTION (connection)) == NULL);
983 g_hash_table_iter_init (&iter, priv->connections);
984 while (g_hash_table_iter_next (&iter, NULL, &data)) {
985 /* prevent duplicates */
986 if (data == connection)
990 if (!nm_connection_normalize (NM_CONNECTION (connection), NULL, NULL, &error)) {
991 _LOGW ("plugin provided invalid connection: %s", error->message);
992 g_error_free (error);
996 existing = nm_settings_get_connection_by_uuid (self, nm_settings_connection_get_uuid (connection));
998 /* Cannot add duplicate connections per UUID. Just return without action and
1001 * This means, that plugins must not provide duplicate connections (UUID).
1002 * In fact, none of the plugins currently would do that.
1004 * But globaly, over different setting plugins, there could be duplicates
1005 * without the individual plugins being aware. Don't handle that at all, just
1006 * error out. That should not happen unless the admin misconfigured the system
1007 * to create conflicting connections. */
1008 _LOGW ("plugin provided duplicate connection with UUID %s",
1009 nm_settings_connection_get_uuid (connection));
1013 /* Read timestamp from look-aside file and put it into the connection's data */
1014 nm_settings_connection_read_and_fill_timestamp (connection);
1016 /* Read seen-bssids from look-aside file and put it into the connection's data */
1017 nm_settings_connection_read_and_fill_seen_bssids (connection);
1019 /* Ensure it's initial visibility is up-to-date */
1020 nm_settings_connection_recheck_visibility (connection);
1022 /* Evil openconnect migration hack */
1023 openconnect_migrate_hack (NM_CONNECTION (connection));
1025 g_object_ref (self);
1026 g_signal_connect (connection, NM_SETTINGS_CONNECTION_REMOVED,
1027 G_CALLBACK (connection_removed), self);
1028 g_signal_connect (connection, NM_SETTINGS_CONNECTION_UPDATED,
1029 G_CALLBACK (connection_updated), self);
1030 g_signal_connect (connection, NM_SETTINGS_CONNECTION_UPDATED_BY_USER,
1031 G_CALLBACK (connection_updated_by_user), self);
1032 g_signal_connect (connection, "notify::" NM_SETTINGS_CONNECTION_VISIBLE,
1033 G_CALLBACK (connection_visibility_changed),
1035 if (!priv->startup_complete) {
1036 g_signal_connect (connection, "notify::" NM_SETTINGS_CONNECTION_READY,
1037 G_CALLBACK (connection_ready_changed),
1041 /* Export the connection over D-Bus */
1042 g_warn_if_fail (nm_connection_get_path (NM_CONNECTION (connection)) == NULL);
1043 path = nm_exported_object_export (NM_EXPORTED_OBJECT (connection));
1044 nm_connection_set_path (NM_CONNECTION (connection), path);
1046 g_hash_table_insert (priv->connections,
1047 (gpointer) nm_connection_get_path (NM_CONNECTION (connection)),
1048 g_object_ref (connection));
1050 nm_utils_log_connection_diff (NM_CONNECTION (connection), NULL, LOGL_DEBUG, LOGD_CORE, "new connection", "++ ");
1052 /* Only emit the individual connection-added signal after connections
1053 * have been initially loaded.
1055 if (priv->connections_loaded) {
1056 /* Internal added signal */
1057 g_signal_emit (self, signals[CONNECTION_ADDED], 0, connection);
1058 g_signal_emit_by_name (self, NM_CP_SIGNAL_CONNECTION_ADDED, connection);
1059 g_object_notify (G_OBJECT (self), NM_SETTINGS_CONNECTIONS);
1061 /* Exported D-Bus signal */
1062 g_signal_emit (self, signals[NEW_CONNECTION], 0, connection);
1067 * nm_settings_add_connection:
1068 * @self: the #NMSettings object
1069 * @connection: the source connection to create a new #NMSettingsConnection from
1070 * @save_to_disk: %TRUE to save the connection to disk immediately, %FALSE to
1072 * @error: on return, a location to store any errors that may occur
1074 * Creates a new #NMSettingsConnection for the given source @connection.
1075 * The returned object is owned by @self and the caller must reference
1076 * the object to continue using it.
1078 * Returns: the new #NMSettingsConnection or %NULL
1080 NMSettingsConnection *
1081 nm_settings_add_connection (NMSettings *self,
1082 NMConnection *connection,
1083 gboolean save_to_disk,
1086 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
1088 NMSettingsConnection *added = NULL;
1089 GHashTableIter citer;
1090 NMConnection *candidate = NULL;
1092 /* Make sure a connection with this UUID doesn't already exist */
1093 g_hash_table_iter_init (&citer, priv->connections);
1094 while (g_hash_table_iter_next (&citer, NULL, (gpointer *) &candidate)) {
1095 if (g_strcmp0 (nm_connection_get_uuid (connection),
1096 nm_connection_get_uuid (candidate)) == 0) {
1097 g_set_error_literal (error,
1099 NM_SETTINGS_ERROR_UUID_EXISTS,
1100 "A connection with this UUID already exists.");
1105 /* 1) plugin writes the NMConnection to disk
1106 * 2) plugin creates a new NMSettingsConnection subclass with the settings
1107 * from the NMConnection and returns it to the settings service
1108 * 3) settings service exports the new NMSettingsConnection subclass
1109 * 4) plugin notices that something on the filesystem has changed
1110 * 5) plugin reads the changes and ignores them because they will
1111 * contain the same data as the connection it already knows about
1113 for (iter = priv->plugins; iter; iter = g_slist_next (iter)) {
1114 NMSettingsPlugin *plugin = NM_SETTINGS_PLUGIN (iter->data);
1115 GError *add_error = NULL;
1117 added = nm_settings_plugin_add_connection (plugin, connection, save_to_disk, &add_error);
1119 claim_connection (self, added);
1122 _LOGD ("Failed to add %s/'%s': %s",
1123 nm_connection_get_uuid (connection),
1124 nm_connection_get_id (connection),
1125 add_error->message);
1126 g_clear_error (&add_error);
1129 g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
1130 "No plugin supported adding this connection");
1134 static NMConnection *
1135 _nm_connection_provider_add_connection (NMConnectionProvider *provider,
1136 NMConnection *connection,
1137 gboolean save_to_disk,
1140 g_assert (NM_IS_CONNECTION_PROVIDER (provider) && NM_IS_SETTINGS (provider));
1141 return NM_CONNECTION (nm_settings_add_connection (NM_SETTINGS (provider), connection, save_to_disk, error));
1145 secrets_filter_cb (NMSetting *setting,
1147 NMSettingSecretFlags flags,
1150 NMSettingSecretFlags filter_flags = GPOINTER_TO_UINT (user_data);
1152 /* Returns TRUE to remove the secret */
1154 /* Can't use bitops with SECRET_FLAG_NONE so handle that specifically */
1155 if ( (flags == NM_SETTING_SECRET_FLAG_NONE)
1156 && (filter_flags == NM_SETTING_SECRET_FLAG_NONE))
1159 /* Otherwise if the secret has at least one of the desired flags keep it */
1160 return (flags & filter_flags) ? FALSE : TRUE;
1164 send_agent_owned_secrets (NMSettings *self,
1165 NMSettingsConnection *connection,
1166 NMAuthSubject *subject)
1168 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
1169 NMConnection *for_agent;
1171 /* Dupe the connection so we can clear out non-agent-owned secrets,
1172 * as agent-owned secrets are the only ones we send back to be saved.
1173 * Only send secrets to agents of the same UID that called update too.
1175 for_agent = nm_simple_connection_new_clone (NM_CONNECTION (connection));
1176 nm_connection_clear_secrets_with_flags (for_agent,
1178 GUINT_TO_POINTER (NM_SETTING_SECRET_FLAG_AGENT_OWNED));
1179 nm_agent_manager_save_secrets (priv->agent_mgr,
1180 nm_connection_get_path (NM_CONNECTION (connection)),
1183 g_object_unref (for_agent);
1187 pk_add_cb (NMAuthChain *chain,
1188 GError *chain_error,
1189 GDBusMethodInvocation *context,
1192 NMSettings *self = NM_SETTINGS (user_data);
1193 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
1194 NMAuthCallResult result;
1195 GError *error = NULL;
1196 NMConnection *connection = NULL;
1197 NMSettingsConnection *added = NULL;
1198 NMSettingsAddCallback callback;
1199 gpointer callback_data;
1200 NMAuthSubject *subject;
1202 gboolean save_to_disk;
1206 priv->auths = g_slist_remove (priv->auths, chain);
1208 perm = nm_auth_chain_get_data (chain, "perm");
1210 result = nm_auth_chain_get_result (chain, perm);
1213 error = g_error_new (NM_SETTINGS_ERROR,
1214 NM_SETTINGS_ERROR_FAILED,
1215 "Error checking authorization: %s",
1216 chain_error->message);
1217 } else if (result != NM_AUTH_CALL_RESULT_YES) {
1218 error = g_error_new_literal (NM_SETTINGS_ERROR,
1219 NM_SETTINGS_ERROR_PERMISSION_DENIED,
1220 "Insufficient privileges.");
1223 connection = nm_auth_chain_get_data (chain, "connection");
1224 g_assert (connection);
1225 save_to_disk = GPOINTER_TO_UINT (nm_auth_chain_get_data (chain, "save-to-disk"));
1226 added = nm_settings_add_connection (self, connection, save_to_disk, &error);
1229 callback = nm_auth_chain_get_data (chain, "callback");
1230 callback_data = nm_auth_chain_get_data (chain, "callback-data");
1231 subject = nm_auth_chain_get_data (chain, "subject");
1233 callback (self, added, error, context, subject, callback_data);
1235 /* Send agent-owned secrets to the agents */
1236 if (!error && added && nm_settings_has_connection (self, added))
1237 send_agent_owned_secrets (self, added, subject);
1239 g_clear_error (&error);
1240 nm_auth_chain_unref (chain);
1243 /* FIXME: remove if/when kernel supports adhoc wpa */
1245 is_adhoc_wpa (NMConnection *connection)
1247 NMSettingWireless *s_wifi;
1248 NMSettingWirelessSecurity *s_wsec;
1249 const char *mode, *key_mgmt;
1251 /* The kernel doesn't support Ad-Hoc WPA connections well at this time,
1252 * and turns them into open networks. It's been this way since at least
1253 * 2.6.30 or so; until that's fixed, disable WPA-protected Ad-Hoc networks.
1256 s_wifi = nm_connection_get_setting_wireless (connection);
1260 mode = nm_setting_wireless_get_mode (s_wifi);
1261 if (g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC) != 0)
1264 s_wsec = nm_connection_get_setting_wireless_security (connection);
1268 key_mgmt = nm_setting_wireless_security_get_key_mgmt (s_wsec);
1269 if (g_strcmp0 (key_mgmt, "wpa-none") != 0)
1276 nm_settings_add_connection_dbus (NMSettings *self,
1277 NMConnection *connection,
1278 gboolean save_to_disk,
1279 GDBusMethodInvocation *context,
1280 NMSettingsAddCallback callback,
1283 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
1284 NMSettingConnection *s_con;
1285 NMAuthSubject *subject = NULL;
1287 GError *error = NULL, *tmp_error = NULL;
1288 char *error_desc = NULL;
1291 g_return_if_fail (connection != NULL);
1292 g_return_if_fail (context != NULL);
1294 /* Connection must be valid, of course */
1295 if (!nm_connection_verify (connection, &tmp_error)) {
1296 error = g_error_new (NM_SETTINGS_ERROR,
1297 NM_SETTINGS_ERROR_INVALID_CONNECTION,
1298 "The connection was invalid: %s",
1299 tmp_error->message);
1300 g_error_free (tmp_error);
1304 /* The kernel doesn't support Ad-Hoc WPA connections well at this time,
1305 * and turns them into open networks. It's been this way since at least
1306 * 2.6.30 or so; until that's fixed, disable WPA-protected Ad-Hoc networks.
1308 if (is_adhoc_wpa (connection)) {
1309 error = g_error_new_literal (NM_SETTINGS_ERROR,
1310 NM_SETTINGS_ERROR_INVALID_CONNECTION,
1311 "WPA Ad-Hoc disabled due to kernel bugs");
1315 /* Do any of the plugins support adding? */
1316 if (!get_plugin (self, NM_SETTINGS_PLUGIN_CAP_MODIFY_CONNECTIONS)) {
1317 error = g_error_new_literal (NM_SETTINGS_ERROR,
1318 NM_SETTINGS_ERROR_NOT_SUPPORTED,
1319 "None of the registered plugins support add.");
1323 subject = nm_auth_subject_new_unix_process_from_context (context);
1325 error = g_error_new_literal (NM_SETTINGS_ERROR,
1326 NM_SETTINGS_ERROR_PERMISSION_DENIED,
1327 "Unable to determine UID of request.");
1331 /* Ensure the caller's username exists in the connection's permissions,
1332 * or that the permissions is empty (ie, visible by everyone).
1334 if (!nm_auth_is_subject_in_acl (connection,
1337 error = g_error_new_literal (NM_SETTINGS_ERROR,
1338 NM_SETTINGS_ERROR_PERMISSION_DENIED,
1340 g_free (error_desc);
1344 /* If the caller is the only user in the connection's permissions, then
1345 * we use the 'modify.own' permission instead of 'modify.system'. If the
1346 * request affects more than just the caller, require 'modify.system'.
1348 s_con = nm_connection_get_setting_connection (connection);
1350 if (nm_setting_connection_get_num_permissions (s_con) == 1)
1351 perm = NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN;
1353 perm = NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM;
1355 /* Validate the user request */
1356 chain = nm_auth_chain_new_subject (subject, context, pk_add_cb, self);
1358 error = g_error_new_literal (NM_SETTINGS_ERROR,
1359 NM_SETTINGS_ERROR_PERMISSION_DENIED,
1360 "Unable to authenticate the request.");
1364 priv->auths = g_slist_append (priv->auths, chain);
1365 nm_auth_chain_add_call (chain, perm, TRUE);
1366 nm_auth_chain_set_data (chain, "perm", (gpointer) perm, NULL);
1367 nm_auth_chain_set_data (chain, "connection", g_object_ref (connection), g_object_unref);
1368 nm_auth_chain_set_data (chain, "callback", callback, NULL);
1369 nm_auth_chain_set_data (chain, "callback-data", user_data, NULL);
1370 nm_auth_chain_set_data (chain, "subject", g_object_ref (subject), g_object_unref);
1371 nm_auth_chain_set_data (chain, "save-to-disk", GUINT_TO_POINTER (save_to_disk), NULL);
1375 callback (self, NULL, error, context, subject, user_data);
1377 g_clear_error (&error);
1378 g_clear_object (&subject);
1382 impl_settings_add_connection_add_cb (NMSettings *self,
1383 NMSettingsConnection *connection,
1385 GDBusMethodInvocation *context,
1386 NMAuthSubject *subject,
1390 g_dbus_method_invocation_return_gerror (context, error);
1391 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ADD, NULL, FALSE, subject, error->message);
1393 g_dbus_method_invocation_return_value (
1395 g_variant_new ("(o)", nm_connection_get_path (NM_CONNECTION (connection))));
1396 nm_audit_log_connection_op (NM_AUDIT_OP_CONN_ADD, connection, TRUE,
1402 impl_settings_add_connection_helper (NMSettings *self,
1403 GDBusMethodInvocation *context,
1405 gboolean save_to_disk)
1407 NMConnection *connection;
1408 GError *error = NULL;
1410 connection = _nm_simple_connection_new_from_dbus (settings,
1411 NM_SETTING_PARSE_FLAGS_NORMALIZE,
1415 if (!nm_connection_verify_secrets (connection, &error))
1418 nm_settings_add_connection_dbus (self,
1422 impl_settings_add_connection_add_cb,
1424 g_object_unref (connection);
1430 g_dbus_method_invocation_take_error (context, error);
1434 impl_settings_add_connection (NMSettings *self,
1435 GDBusMethodInvocation *context,
1438 impl_settings_add_connection_helper (self, context, settings, TRUE);
1442 impl_settings_add_connection_unsaved (NMSettings *self,
1443 GDBusMethodInvocation *context,
1446 impl_settings_add_connection_helper (self, context, settings, FALSE);
1450 ensure_root (NMBusManager *dbus_mgr,
1451 GDBusMethodInvocation *context)
1454 GError *error = NULL;
1456 if (!nm_bus_manager_get_caller_info (dbus_mgr, context, NULL, &caller_uid, NULL)) {
1457 error = g_error_new_literal (NM_SETTINGS_ERROR,
1458 NM_SETTINGS_ERROR_PERMISSION_DENIED,
1459 "Unable to determine request UID.");
1460 g_dbus_method_invocation_take_error (context, error);
1463 if (caller_uid != 0) {
1464 error = g_error_new_literal (NM_SETTINGS_ERROR,
1465 NM_SETTINGS_ERROR_PERMISSION_DENIED,
1466 "Permission denied");
1467 g_dbus_method_invocation_take_error (context, error);
1475 impl_settings_load_connections (NMSettings *self,
1476 GDBusMethodInvocation *context,
1479 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
1480 GPtrArray *failures;
1484 if (!ensure_root (nm_bus_manager_get (), context))
1487 failures = g_ptr_array_new ();
1489 for (i = 0; filenames[i]; i++) {
1490 for (iter = priv->plugins; iter; iter = g_slist_next (iter)) {
1491 NMSettingsPlugin *plugin = NM_SETTINGS_PLUGIN (iter->data);
1493 if (nm_settings_plugin_load_connection (plugin, filenames[i]))
1498 if (!g_path_is_absolute (filenames[i]))
1499 _LOGW ("connection filename '%s' is not an absolute path", filenames[i]);
1500 g_ptr_array_add (failures, (char *) filenames[i]);
1504 g_ptr_array_add (failures, NULL);
1505 g_dbus_method_invocation_return_value (
1507 g_variant_new ("(b^as)",
1510 g_ptr_array_unref (failures);
1514 impl_settings_reload_connections (NMSettings *self,
1515 GDBusMethodInvocation *context)
1517 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
1520 if (!ensure_root (nm_bus_manager_get (), context))
1523 for (iter = priv->plugins; iter; iter = g_slist_next (iter)) {
1524 NMSettingsPlugin *plugin = NM_SETTINGS_PLUGIN (iter->data);
1526 nm_settings_plugin_reload_connections (plugin);
1529 g_dbus_method_invocation_return_value (context, g_variant_new ("(b)", TRUE));
1534 NMSettingsSetHostnameCb cb;
1539 set_transient_hostname_done (GObject *object,
1543 GDBusProxy *proxy = G_DBUS_PROXY (object);
1544 gs_free SetHostnameInfo *info = user_data;
1545 gs_unref_variant GVariant *result = NULL;
1546 gs_free_error GError *error = NULL;
1548 result = g_dbus_proxy_call_finish (proxy, res, &error);
1551 _LOGW ("couldn't set the system hostname to '%s' using hostnamed: %s",
1552 info->hostname, error->message);
1555 info->cb (info->hostname, !error, info->user_data);
1556 g_free (info->hostname);
1560 nm_settings_set_transient_hostname (NMSettings *self,
1561 const char *hostname,
1562 NMSettingsSetHostnameCb cb,
1565 NMSettingsPrivate *priv;
1566 SetHostnameInfo *info;
1568 g_return_if_fail (NM_IS_SETTINGS (self));
1569 priv = NM_SETTINGS_GET_PRIVATE (self);
1571 if (!priv->hostname.hostnamed_proxy) {
1572 cb (hostname, FALSE, user_data);
1576 info = g_new0 (SetHostnameInfo, 1);
1577 info->hostname = g_strdup (hostname);
1579 info->user_data = user_data;
1581 g_dbus_proxy_call (priv->hostname.hostnamed_proxy,
1583 g_variant_new ("(sb)", hostname, FALSE),
1584 G_DBUS_CALL_FLAGS_NONE,
1587 set_transient_hostname_done,
1592 write_hostname (NMSettingsPrivate *priv, const char *hostname)
1596 gs_free_error GError *error = NULL;
1597 const char *file = priv->hostname.file;
1598 gs_free char *link_path = NULL;
1599 gs_unref_variant GVariant *var = NULL;
1600 struct stat file_stat;
1602 security_context_t se_ctx_prev = NULL, se_ctx = NULL;
1606 if (priv->hostname.hostnamed_proxy) {
1607 var = g_dbus_proxy_call_sync (priv->hostname.hostnamed_proxy,
1608 "SetStaticHostname",
1609 g_variant_new ("(sb)", hostname, FALSE),
1610 G_DBUS_CALL_FLAGS_NONE,
1615 _LOGW ("could not set hostname: %s", error->message);
1620 /* If the hostname file is a symbolic link, follow it to find where the
1621 * real file is located, otherwise g_file_set_contents will attempt to
1622 * replace the link with a plain file.
1624 if ( lstat (file, &file_stat) == 0
1625 && S_ISLNK (file_stat.st_mode)
1626 && (link_path = g_file_read_link (file, NULL)))
1630 /* Get default context for hostname file and set it for fscreate */
1631 if (stat (file, &file_stat) == 0)
1632 st_mode = file_stat.st_mode;
1633 matchpathcon (file, st_mode, &se_ctx);
1634 matchpathcon_fini ();
1635 getfscreatecon (&se_ctx_prev);
1636 setfscreatecon (se_ctx);
1639 #if defined (HOSTNAME_PERSIST_GENTOO)
1640 hostname_eol = g_strdup_printf ("#Generated by NetworkManager\n"
1641 "hostname=\"%s\"\n", hostname);
1643 hostname_eol = g_strdup_printf ("%s\n", hostname);
1646 /* FIXME: g_file_set_contents() writes first to a temporary file
1647 * and renames it atomically. We should hack g_file_set_contents()
1648 * to set the SELINUX labels before renaming the file. */
1649 ret = g_file_set_contents (file, hostname_eol, -1, &error);
1652 /* Restore previous context and cleanup */
1653 setfscreatecon (se_ctx_prev);
1655 freecon (se_ctx_prev);
1658 g_free (hostname_eol);
1661 _LOGW ("could not save hostname to %s: %s", file, error->message);
1669 pk_hostname_cb (NMAuthChain *chain,
1670 GError *chain_error,
1671 GDBusMethodInvocation *context,
1674 NMSettings *self = NM_SETTINGS (user_data);
1675 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
1676 NMAuthCallResult result;
1677 GError *error = NULL;
1678 const char *hostname;
1682 priv->auths = g_slist_remove (priv->auths, chain);
1684 result = nm_auth_chain_get_result (chain, NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME);
1686 /* If our NMSettingsConnection is already gone, do nothing */
1688 error = g_error_new (NM_SETTINGS_ERROR,
1689 NM_SETTINGS_ERROR_FAILED,
1690 "Error checking authorization: %s",
1691 chain_error->message);
1692 } else if (result != NM_AUTH_CALL_RESULT_YES) {
1693 error = g_error_new_literal (NM_SETTINGS_ERROR,
1694 NM_SETTINGS_ERROR_PERMISSION_DENIED,
1695 "Insufficient privileges.");
1697 hostname = nm_auth_chain_get_data (chain, "hostname");
1699 if (!write_hostname (priv, hostname)) {
1700 error = g_error_new_literal (NM_SETTINGS_ERROR,
1701 NM_SETTINGS_ERROR_FAILED,
1702 "Saving the hostname failed.");
1707 g_dbus_method_invocation_take_error (context, error);
1709 g_dbus_method_invocation_return_value (context, NULL);
1711 nm_auth_chain_unref (chain);
1715 validate_hostname (const char *hostname)
1718 gboolean dot = TRUE;
1720 if (!hostname || !hostname[0])
1723 for (p = hostname; *p; p++) {
1729 if (!g_ascii_isalnum (*p) && (*p != '-') && (*p != '_'))
1738 return (p - hostname <= HOST_NAME_MAX);
1742 impl_settings_save_hostname (NMSettings *self,
1743 GDBusMethodInvocation *context,
1744 const char *hostname)
1746 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
1748 GError *error = NULL;
1750 /* Minimal validation of the hostname */
1751 if (!validate_hostname (hostname)) {
1752 error = g_error_new_literal (NM_SETTINGS_ERROR,
1753 NM_SETTINGS_ERROR_INVALID_HOSTNAME,
1754 "The hostname was too long or contained invalid characters.");
1758 chain = nm_auth_chain_new_context (context, pk_hostname_cb, self);
1760 error = g_error_new_literal (NM_SETTINGS_ERROR,
1761 NM_SETTINGS_ERROR_PERMISSION_DENIED,
1762 "Unable to authenticate the request.");
1766 priv->auths = g_slist_append (priv->auths, chain);
1767 nm_auth_chain_add_call (chain, NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME, TRUE);
1768 nm_auth_chain_set_data (chain, "hostname", g_strdup (hostname), g_free);
1772 g_dbus_method_invocation_take_error (context, error);
1776 hostname_maybe_changed (NMSettings *settings)
1778 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (settings);
1781 new_hostname = nm_settings_get_hostname (settings);
1783 if ( (new_hostname && !priv->hostname.value)
1784 || (!new_hostname && priv->hostname.value)
1785 || (priv->hostname.value && new_hostname && strcmp (priv->hostname.value, new_hostname))) {
1787 _LOGI ("hostname changed from %s%s%s to %s%s%s",
1788 NM_PRINT_FMT_QUOTED (priv->hostname.value, "\"", priv->hostname.value, "\"", "(none)"),
1789 NM_PRINT_FMT_QUOTED (new_hostname, "\"", new_hostname, "\"", "(none)"));
1790 g_free (priv->hostname.value);
1791 priv->hostname.value = new_hostname;
1792 g_object_notify (G_OBJECT (settings), NM_SETTINGS_HOSTNAME);
1794 g_free (new_hostname);
1798 hostname_file_changed_cb (GFileMonitor *monitor,
1801 GFileMonitorEvent event_type,
1804 hostname_maybe_changed (user_data);
1808 have_connection_for_device (NMSettings *self, NMDevice *device)
1810 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
1811 GHashTableIter iter;
1813 NMSettingConnection *s_con;
1814 NMSettingWired *s_wired;
1815 const char *setting_hwaddr;
1816 const char *device_hwaddr;
1818 g_return_val_if_fail (NM_IS_SETTINGS (self), FALSE);
1820 device_hwaddr = nm_device_get_hw_address (device);
1822 /* Find a wired connection locked to the given MAC address, if any */
1823 g_hash_table_iter_init (&iter, priv->connections);
1824 while (g_hash_table_iter_next (&iter, NULL, &data)) {
1825 NMConnection *connection = NM_CONNECTION (data);
1826 const char *ctype, *iface;
1828 if (!nm_device_check_connection_compatible (device, connection))
1831 s_con = nm_connection_get_setting_connection (connection);
1833 iface = nm_setting_connection_get_interface_name (s_con);
1834 if (iface && strcmp (iface, nm_device_get_iface (device)) != 0)
1837 ctype = nm_setting_connection_get_connection_type (s_con);
1838 if ( strcmp (ctype, NM_SETTING_WIRED_SETTING_NAME)
1839 && strcmp (ctype, NM_SETTING_PPPOE_SETTING_NAME))
1842 s_wired = nm_connection_get_setting_wired (connection);
1844 if (!s_wired && !strcmp (ctype, NM_SETTING_PPPOE_SETTING_NAME)) {
1845 /* No wired setting; therefore the PPPoE connection applies to any device */
1849 g_assert (s_wired != NULL);
1851 setting_hwaddr = nm_setting_wired_get_mac_address (s_wired);
1852 if (setting_hwaddr) {
1853 /* A connection mac-locked to this device */
1855 && nm_utils_hwaddr_matches (setting_hwaddr, -1, device_hwaddr, -1))
1858 /* A connection that applies to any wired device */
1863 /* See if there's a known non-NetworkManager configuration for the device */
1864 if (nm_device_spec_match_list (device, priv->unrecognized_specs))
1870 #define DEFAULT_WIRED_CONNECTION_TAG "default-wired-connection"
1871 #define DEFAULT_WIRED_DEVICE_TAG "default-wired-device"
1873 static void default_wired_clear_tag (NMSettings *self,
1875 NMSettingsConnection *connection,
1876 gboolean add_to_no_auto_default);
1879 default_wired_connection_removed_cb (NMSettingsConnection *connection, NMSettings *self)
1883 /* When the default wired connection is removed (either deleted or saved to
1884 * a new persistent connection by a plugin), write the MAC address of the
1885 * wired device to the config file and don't create a new default wired
1886 * connection for that device again.
1888 device = g_object_get_data (G_OBJECT (connection), DEFAULT_WIRED_DEVICE_TAG);
1890 default_wired_clear_tag (self, device, connection, TRUE);
1894 default_wired_connection_updated_by_user_cb (NMSettingsConnection *connection, NMSettings *self)
1898 /* The connection has been changed by the user, it should no longer be
1899 * considered a default wired connection, and should no longer affect
1900 * the no-auto-default configuration option.
1902 device = g_object_get_data (G_OBJECT (connection), DEFAULT_WIRED_DEVICE_TAG);
1904 default_wired_clear_tag (self, device, connection, FALSE);
1908 default_wired_clear_tag (NMSettings *self,
1910 NMSettingsConnection *connection,
1911 gboolean add_to_no_auto_default)
1913 g_return_if_fail (NM_IS_SETTINGS (self));
1914 g_return_if_fail (NM_IS_DEVICE (device));
1915 g_return_if_fail (NM_IS_CONNECTION (connection));
1916 g_return_if_fail (device == g_object_get_data (G_OBJECT (connection), DEFAULT_WIRED_DEVICE_TAG));
1917 g_return_if_fail (connection == g_object_get_data (G_OBJECT (device), DEFAULT_WIRED_CONNECTION_TAG));
1919 g_object_set_data (G_OBJECT (connection), DEFAULT_WIRED_DEVICE_TAG, NULL);
1920 g_object_set_data (G_OBJECT (device), DEFAULT_WIRED_CONNECTION_TAG, NULL);
1922 g_signal_handlers_disconnect_by_func (connection, G_CALLBACK (default_wired_connection_removed_cb), self);
1923 g_signal_handlers_disconnect_by_func (connection, G_CALLBACK (default_wired_connection_updated_by_user_cb), self);
1925 if (add_to_no_auto_default)
1926 nm_config_set_no_auto_default_for_device (NM_SETTINGS_GET_PRIVATE (self)->config, device);
1930 device_realized (NMDevice *device, GParamSpec *pspec, NMSettings *self)
1932 NMConnection *connection;
1933 NMSettingsConnection *added;
1934 GError *error = NULL;
1936 if (!nm_device_is_real (device))
1939 g_signal_handlers_disconnect_by_func (device,
1940 G_CALLBACK (device_realized),
1943 /* If the device isn't managed or it already has a default wired connection,
1946 if ( !nm_device_get_managed (device, FALSE)
1947 || g_object_get_data (G_OBJECT (device), DEFAULT_WIRED_CONNECTION_TAG)
1948 || have_connection_for_device (self, device))
1951 connection = nm_device_new_default_connection (device);
1955 /* Add the connection */
1956 added = nm_settings_add_connection (self, connection, FALSE, &error);
1957 g_object_unref (connection);
1960 _LOGW ("(%s) couldn't create default wired connection: %s",
1961 nm_device_get_iface (device),
1963 g_clear_error (&error);
1967 g_object_set_data (G_OBJECT (added), DEFAULT_WIRED_DEVICE_TAG, device);
1968 g_object_set_data (G_OBJECT (device), DEFAULT_WIRED_CONNECTION_TAG, added);
1970 g_signal_connect (added, NM_SETTINGS_CONNECTION_UPDATED_BY_USER,
1971 G_CALLBACK (default_wired_connection_updated_by_user_cb), self);
1972 g_signal_connect (added, NM_SETTINGS_CONNECTION_REMOVED,
1973 G_CALLBACK (default_wired_connection_removed_cb), self);
1975 _LOGI ("(%s): created default wired connection '%s'",
1976 nm_device_get_iface (device),
1977 nm_settings_connection_get_id (added));
1981 nm_settings_device_added (NMSettings *self, NMDevice *device)
1983 if (nm_device_is_real (device))
1984 device_realized (device, NULL, self);
1986 g_signal_connect_after (device, "notify::" NM_DEVICE_REAL,
1987 G_CALLBACK (device_realized),
1993 nm_settings_device_removed (NMSettings *self, NMDevice *device, gboolean quitting)
1995 NMSettingsConnection *connection;
1997 g_signal_handlers_disconnect_by_func (device,
1998 G_CALLBACK (device_realized),
2001 connection = g_object_get_data (G_OBJECT (device), DEFAULT_WIRED_CONNECTION_TAG);
2003 default_wired_clear_tag (self, device, connection, FALSE);
2005 /* Don't delete the default wired connection on shutdown, so that it
2006 * remains up and can be assumed if NM starts again.
2008 if (quitting == FALSE)
2009 nm_settings_connection_delete (connection, NULL, NULL);
2013 /***************************************************************/
2015 /* GCompareFunc helper for sorting "best" connections.
2016 * The function sorts connections in ascending timestamp order.
2017 * That means an older connection (lower timestamp) goes before
2021 nm_settings_sort_connections (gconstpointer a, gconstpointer b)
2023 NMSettingsConnection *ac = (NMSettingsConnection *) a;
2024 NMSettingsConnection *bc = (NMSettingsConnection *) b;
2025 guint64 ats = 0, bts = 0;
2034 /* In the future we may use connection priorities in addition to timestamps */
2035 nm_settings_connection_get_timestamp (ac, &ats);
2036 nm_settings_connection_get_timestamp (bc, &bts);
2046 get_best_connections (NMConnectionProvider *provider,
2047 guint max_requested,
2050 NMConnectionFilterFunc func,
2053 NMSettings *self = NM_SETTINGS (provider);
2054 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
2055 GSList *sorted = NULL;
2056 GHashTableIter iter;
2057 NMSettingsConnection *connection;
2061 g_hash_table_iter_init (&iter, priv->connections);
2062 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &connection)) {
2065 if (ctype1 && !nm_connection_is_type (NM_CONNECTION (connection), ctype1))
2067 if (ctype2 && !nm_connection_is_type (NM_CONNECTION (connection), ctype2))
2069 if (func && !func (provider, NM_CONNECTION (connection), func_data))
2072 /* Don't bother with a connection that's older than the oldest one in the list */
2073 if (max_requested && added >= max_requested) {
2074 nm_settings_connection_get_timestamp (connection, &cur_ts);
2075 if (cur_ts <= oldest)
2079 /* List is sorted with oldest first */
2080 sorted = g_slist_insert_sorted (sorted, connection, nm_settings_sort_connections);
2083 if (max_requested && added > max_requested) {
2084 /* Over the limit, remove the oldest one */
2085 sorted = g_slist_delete_link (sorted, sorted);
2089 nm_settings_connection_get_timestamp (NM_SETTINGS_CONNECTION (sorted->data), &oldest);
2092 return g_slist_reverse (sorted);
2095 static const GSList *
2096 get_connections (NMConnectionProvider *provider)
2098 GSList *list = NULL;
2099 NMSettings *self = NM_SETTINGS (provider);
2100 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
2102 list = _nm_utils_hash_values_to_slist (priv->connections);
2104 /* Cache the list every call so we can keep it 'const' for callers */
2105 g_slist_free (priv->get_connections_cache);
2106 priv->get_connections_cache = list;
2110 static NMConnection *
2111 cp_get_connection_by_uuid (NMConnectionProvider *provider, const char *uuid)
2113 return NM_CONNECTION (nm_settings_get_connection_by_uuid (NM_SETTINGS (provider), uuid));
2116 /***************************************************************/
2119 nm_settings_get_startup_complete (NMSettings *self)
2121 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
2123 return priv->startup_complete;
2126 /***************************************************************/
2129 hostnamed_properties_changed (GDBusProxy *proxy,
2130 GVariant *changed_properties,
2131 char **invalidated_properties,
2134 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (user_data);
2135 GVariant *v_hostname;
2136 const char *hostname;
2138 v_hostname = g_dbus_proxy_get_cached_property (priv->hostname.hostnamed_proxy,
2143 hostname = g_variant_get_string (v_hostname, NULL);
2145 if (g_strcmp0 (priv->hostname.value, hostname) != 0) {
2146 _LOGI ("hostname changed from %s%s%s to %s%s%s",
2147 NM_PRINT_FMT_QUOTED (priv->hostname.value, "\"", priv->hostname.value, "\"", "(none)"),
2148 NM_PRINT_FMT_QUOTED (hostname, "\"", hostname, "\"", "(none)"));
2149 g_free (priv->hostname.value);
2150 priv->hostname.value = g_strdup (hostname);
2151 g_object_notify (G_OBJECT (user_data), NM_SETTINGS_HOSTNAME);
2152 nm_dispatcher_call (DISPATCHER_ACTION_HOSTNAME, NULL, NULL, NULL, NULL, NULL, NULL);
2155 g_variant_unref (v_hostname);
2159 setup_hostname_file_monitors (NMSettings *self)
2161 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
2162 GFileMonitor *monitor;
2165 priv->hostname.file = HOSTNAME_FILE;
2166 priv->hostname.value = nm_settings_get_hostname (self);
2168 /* monitor changes to hostname file */
2169 file = g_file_new_for_path (priv->hostname.file);
2170 monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
2171 g_object_unref (file);
2173 priv->hostname.monitor_id = g_signal_connect (monitor, "changed",
2174 G_CALLBACK (hostname_file_changed_cb),
2176 priv->hostname.monitor = monitor;
2179 #if defined (HOSTNAME_PERSIST_SUSE)
2180 /* monitor changes to dhcp file to know whether the hostname is valid */
2181 file = g_file_new_for_path (CONF_DHCP);
2182 monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
2183 g_object_unref (file);
2185 priv->hostname.dhcp_monitor_id = g_signal_connect (monitor, "changed",
2186 G_CALLBACK (hostname_file_changed_cb),
2188 priv->hostname.dhcp_monitor = monitor;
2192 hostname_maybe_changed (self);
2196 nm_settings_new (void)
2199 NMSettingsPrivate *priv;
2201 self = g_object_new (NM_TYPE_SETTINGS, NULL);
2203 priv = NM_SETTINGS_GET_PRIVATE (self);
2205 priv->config = nm_config_get ();
2207 nm_exported_object_export (NM_EXPORTED_OBJECT (self));
2212 nm_settings_start (NMSettings *self, GError **error)
2214 NMSettingsPrivate *priv;
2217 GError *local_error = NULL;
2219 priv = NM_SETTINGS_GET_PRIVATE (self);
2221 /* Load the plugins; fail if a plugin is not found. */
2222 if (!load_plugins (self, nm_config_get_plugins (priv->config), error)) {
2223 g_object_unref (self);
2227 load_connections (self);
2228 check_startup_complete (self);
2230 proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, 0, NULL,
2231 HOSTNAMED_SERVICE_NAME, HOSTNAMED_SERVICE_PATH,
2232 HOSTNAMED_SERVICE_INTERFACE, NULL, &local_error);
2234 variant = g_dbus_proxy_get_cached_property (proxy, "StaticHostname");
2236 _LOGI ("hostname: using hostnamed");
2237 priv->hostname.hostnamed_proxy = proxy;
2238 g_signal_connect (proxy, "g-properties-changed",
2239 G_CALLBACK (hostnamed_properties_changed), self);
2240 hostnamed_properties_changed (proxy, NULL, NULL, self);
2241 g_variant_unref (variant);
2243 _LOGI ("hostname: couldn't get property from hostnamed");
2244 g_object_unref (proxy);
2247 _LOGI ("hostname: hostnamed not used as proxy creation failed with: %s",
2248 local_error->message);
2249 g_clear_error (&local_error);
2252 if (!priv->hostname.hostnamed_proxy)
2253 setup_hostname_file_monitors (self);
2255 priv->started = TRUE;
2256 g_object_notify (G_OBJECT (self), NM_SETTINGS_HOSTNAME);
2261 connection_provider_iface_init (NMConnectionProviderInterface *cp_iface)
2263 cp_iface->get_best_connections = get_best_connections;
2264 cp_iface->get_connections = get_connections;
2265 cp_iface->add_connection = _nm_connection_provider_add_connection;
2266 cp_iface->get_connection_by_uuid = cp_get_connection_by_uuid;
2270 nm_settings_init (NMSettings *self)
2272 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
2274 priv->connections = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
2276 /* Hold a reference to the agent manager so it stays alive; the only
2277 * other holders are NMSettingsConnection objects which are often
2278 * transient, and we don't want the agent manager to get destroyed and
2281 priv->agent_mgr = g_object_ref (nm_agent_manager_get ());
2283 g_signal_connect (priv->agent_mgr, "agent-registered", G_CALLBACK (secret_agent_registered), self);
2287 dispose (GObject *object)
2289 NMSettings *self = NM_SETTINGS (object);
2290 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
2292 g_slist_free_full (priv->auths, (GDestroyNotify) nm_auth_chain_unref);
2295 g_object_unref (priv->agent_mgr);
2297 if (priv->hostname.hostnamed_proxy) {
2298 g_signal_handlers_disconnect_by_func (priv->hostname.hostnamed_proxy,
2299 G_CALLBACK (hostnamed_properties_changed),
2301 g_clear_object (&priv->hostname.hostnamed_proxy);
2304 if (priv->hostname.monitor) {
2305 if (priv->hostname.monitor_id)
2306 g_signal_handler_disconnect (priv->hostname.monitor, priv->hostname.monitor_id);
2308 g_file_monitor_cancel (priv->hostname.monitor);
2309 g_clear_object (&priv->hostname.monitor);
2312 if (priv->hostname.dhcp_monitor) {
2313 if (priv->hostname.dhcp_monitor_id)
2314 g_signal_handler_disconnect (priv->hostname.dhcp_monitor,
2315 priv->hostname.dhcp_monitor_id);
2317 g_file_monitor_cancel (priv->hostname.dhcp_monitor);
2318 g_clear_object (&priv->hostname.dhcp_monitor);
2321 g_clear_pointer (&priv->hostname.value, g_free);
2323 G_OBJECT_CLASS (nm_settings_parent_class)->dispose (object);
2327 finalize (GObject *object)
2329 NMSettings *self = NM_SETTINGS (object);
2330 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
2332 g_hash_table_destroy (priv->connections);
2333 g_slist_free (priv->get_connections_cache);
2335 g_slist_free_full (priv->unmanaged_specs, g_free);
2336 g_slist_free_full (priv->unrecognized_specs, g_free);
2338 g_slist_free_full (priv->plugins, g_object_unref);
2340 G_OBJECT_CLASS (nm_settings_parent_class)->finalize (object);
2344 get_property (GObject *object, guint prop_id,
2345 GValue *value, GParamSpec *pspec)
2347 NMSettings *self = NM_SETTINGS (object);
2348 NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
2349 const GSList *specs, *iter;
2350 GHashTableIter citer;
2355 case PROP_UNMANAGED_SPECS:
2356 array = g_ptr_array_new ();
2357 specs = nm_settings_get_unmanaged_specs (self);
2358 for (iter = specs; iter; iter = g_slist_next (iter))
2359 g_ptr_array_add (array, g_strdup (iter->data));
2360 g_ptr_array_add (array, NULL);
2361 g_value_take_boxed (value, (char **) g_ptr_array_free (array, FALSE));
2364 g_value_take_string (value, nm_settings_get_hostname (self));
2366 /* Don't ever pass NULL through D-Bus */
2367 if (!g_value_get_string (value))
2368 g_value_set_static_string (value, "");
2370 case PROP_CAN_MODIFY:
2371 g_value_set_boolean (value, !!get_plugin (self, NM_SETTINGS_PLUGIN_CAP_MODIFY_CONNECTIONS));
2373 case PROP_CONNECTIONS:
2374 array = g_ptr_array_sized_new (g_hash_table_size (priv->connections) + 1);
2375 g_hash_table_iter_init (&citer, priv->connections);
2376 while (g_hash_table_iter_next (&citer, (gpointer) &path, NULL))
2377 g_ptr_array_add (array, g_strdup (path));
2378 g_ptr_array_add (array, NULL);
2379 g_value_take_boxed (value, (char **) g_ptr_array_free (array, FALSE));
2381 case PROP_STARTUP_COMPLETE:
2382 g_value_set_boolean (value, nm_settings_get_startup_complete (self));
2385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2391 nm_settings_class_init (NMSettingsClass *class)
2393 GObjectClass *object_class = G_OBJECT_CLASS (class);
2394 NMExportedObjectClass *exported_object_class = NM_EXPORTED_OBJECT_CLASS (class);
2396 g_type_class_add_private (class, sizeof (NMSettingsPrivate));
2398 exported_object_class->export_path = NM_DBUS_PATH_SETTINGS;
2400 /* virtual methods */
2401 object_class->get_property = get_property;
2402 object_class->dispose = dispose;
2403 object_class->finalize = finalize;
2407 g_object_class_install_property
2408 (object_class, PROP_UNMANAGED_SPECS,
2409 g_param_spec_boxed (NM_SETTINGS_UNMANAGED_SPECS, "", "",
2412 G_PARAM_STATIC_STRINGS));
2414 g_object_class_install_property
2415 (object_class, PROP_HOSTNAME,
2416 g_param_spec_string (NM_SETTINGS_HOSTNAME, "", "",
2419 G_PARAM_STATIC_STRINGS));
2421 g_object_class_install_property
2422 (object_class, PROP_CAN_MODIFY,
2423 g_param_spec_boolean (NM_SETTINGS_CAN_MODIFY, "", "",
2426 G_PARAM_STATIC_STRINGS));
2428 g_object_class_install_property
2429 (object_class, PROP_CONNECTIONS,
2430 g_param_spec_boxed (NM_SETTINGS_CONNECTIONS, "", "",
2433 G_PARAM_STATIC_STRINGS));
2436 signals[CONNECTION_ADDED] =
2437 g_signal_new (NM_SETTINGS_SIGNAL_CONNECTION_ADDED,
2438 G_OBJECT_CLASS_TYPE (object_class),
2440 G_STRUCT_OFFSET (NMSettingsClass, connection_added),
2442 g_cclosure_marshal_VOID__OBJECT,
2443 G_TYPE_NONE, 1, NM_TYPE_SETTINGS_CONNECTION);
2445 signals[CONNECTION_UPDATED] =
2446 g_signal_new (NM_SETTINGS_SIGNAL_CONNECTION_UPDATED,
2447 G_OBJECT_CLASS_TYPE (object_class),
2449 G_STRUCT_OFFSET (NMSettingsClass, connection_updated),
2451 g_cclosure_marshal_VOID__OBJECT,
2452 G_TYPE_NONE, 1, NM_TYPE_SETTINGS_CONNECTION);
2454 signals[CONNECTION_UPDATED_BY_USER] =
2455 g_signal_new (NM_SETTINGS_SIGNAL_CONNECTION_UPDATED_BY_USER,
2456 G_OBJECT_CLASS_TYPE (object_class),
2460 g_cclosure_marshal_VOID__OBJECT,
2461 G_TYPE_NONE, 1, NM_TYPE_SETTINGS_CONNECTION);
2463 signals[CONNECTION_REMOVED] =
2464 g_signal_new (NM_SETTINGS_SIGNAL_CONNECTION_REMOVED,
2465 G_OBJECT_CLASS_TYPE (object_class),
2467 G_STRUCT_OFFSET (NMSettingsClass, connection_removed),
2469 g_cclosure_marshal_VOID__OBJECT,
2470 G_TYPE_NONE, 1, NM_TYPE_SETTINGS_CONNECTION);
2472 signals[CONNECTION_VISIBILITY_CHANGED] =
2473 g_signal_new (NM_SETTINGS_SIGNAL_CONNECTION_VISIBILITY_CHANGED,
2474 G_OBJECT_CLASS_TYPE (object_class),
2476 G_STRUCT_OFFSET (NMSettingsClass, connection_visibility_changed),
2478 g_cclosure_marshal_VOID__OBJECT,
2479 G_TYPE_NONE, 1, NM_TYPE_SETTINGS_CONNECTION);
2481 signals[AGENT_REGISTERED] =
2482 g_signal_new (NM_SETTINGS_SIGNAL_AGENT_REGISTERED,
2483 G_OBJECT_CLASS_TYPE (object_class),
2485 G_STRUCT_OFFSET (NMSettingsClass, agent_registered),
2487 g_cclosure_marshal_VOID__OBJECT,
2488 G_TYPE_NONE, 1, NM_TYPE_SECRET_AGENT);
2491 signals[NEW_CONNECTION] =
2492 g_signal_new ("new-connection",
2493 G_OBJECT_CLASS_TYPE (object_class),
2494 G_SIGNAL_RUN_FIRST, 0, NULL, NULL,
2495 g_cclosure_marshal_VOID__OBJECT,
2496 G_TYPE_NONE, 1, NM_TYPE_SETTINGS_CONNECTION);
2498 nm_exported_object_class_add_interface (NM_EXPORTED_OBJECT_CLASS (class),
2499 NMDBUS_TYPE_SETTINGS_SKELETON,
2500 "ListConnections", impl_settings_list_connections,
2501 "GetConnectionByUuid", impl_settings_get_connection_by_uuid,
2502 "AddConnection", impl_settings_add_connection,
2503 "AddConnectionUnsaved", impl_settings_add_connection_unsaved,
2504 "LoadConnections", impl_settings_load_connections,
2505 "ReloadConnections", impl_settings_reload_connections,
2506 "SaveHostname", impl_settings_save_hostname,