1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager system settings service
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Copyright (C) 2008 - 2011 Red Hat, Inc.
25 #include <glib/gstdio.h>
27 #include <nm-dbus-interface.h>
28 #include <nm-setting-connection.h>
29 #include <nm-setting-wired.h>
30 #include <nm-setting-wireless.h>
31 #include <nm-setting-gsm.h>
32 #include <nm-setting-cdma.h>
33 #include <nm-setting-pppoe.h>
34 #include <nm-setting-wireless-security.h>
35 #include <nm-setting-8021x.h>
36 #include <nm-platform.h>
37 #include <nm-logging.h>
40 #include "nm-config.h"
41 #include "nm-ifcfg-connection.h"
44 #include "nm-inotify-helper.h"
47 G_DEFINE_TYPE (NMIfcfgConnection, nm_ifcfg_connection, NM_TYPE_SETTINGS_CONNECTION)
49 #define NM_IFCFG_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_IFCFG_CONNECTION, NMIfcfgConnectionPrivate))
66 char *unrecognized_spec;
68 gulong devtimeout_link_changed_handler;
69 guint devtimeout_timeout_id;
70 } NMIfcfgConnectionPrivate;
75 PROP_UNRECOGNIZED_SPEC,
85 static guint signals[LAST_SIGNAL] = { 0 };
88 devtimeout_ready (gpointer user_data)
90 NMIfcfgConnection *self = user_data;
91 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (self);
93 priv->devtimeout_timeout_id = 0;
94 nm_settings_connection_set_ready (NM_SETTINGS_CONNECTION (self), TRUE);
99 link_changed (NMPlatform *platform, int ifindex, NMPlatformLink *link,
100 NMPlatformSignalChangeType change_type, NMPlatformReason reason,
103 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (self);
106 ifname = nm_connection_get_interface_name (self);
107 if (g_strcmp0 (link->name, ifname) != 0)
110 /* Shouldn't happen, but... */
111 if (change_type == NM_PLATFORM_SIGNAL_REMOVED)
114 nm_log_info (LOGD_SETTINGS, "Device %s appeared; connection '%s' now ready",
115 ifname, nm_connection_get_id (self));
117 g_signal_handler_disconnect (platform, priv->devtimeout_link_changed_handler);
118 priv->devtimeout_link_changed_handler = 0;
119 g_source_remove (priv->devtimeout_timeout_id);
121 /* Don't declare the connection ready right away, since NMManager may not have
122 * started processing the device yet.
124 priv->devtimeout_timeout_id = g_idle_add (devtimeout_ready, self);
128 devtimeout_expired (gpointer user_data)
130 NMIfcfgConnection *self = user_data;
131 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (self);
133 nm_log_info (LOGD_SETTINGS, "Device for connection '%s' did not appear before timeout",
134 nm_connection_get_id (NM_CONNECTION (self)));
136 g_signal_handler_disconnect (nm_platform_get (), priv->devtimeout_link_changed_handler);
137 priv->devtimeout_link_changed_handler = 0;
138 priv->devtimeout_timeout_id = 0;
140 nm_settings_connection_set_ready (NM_SETTINGS_CONNECTION (self), TRUE);
145 nm_ifcfg_connection_check_devtimeout (NMIfcfgConnection *self)
147 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (self);
148 NMSettingConnection *s_con;
152 s_con = nm_connection_get_setting_connection (NM_CONNECTION (self));
154 if (!nm_setting_connection_get_autoconnect (s_con))
156 ifname = nm_setting_connection_get_interface_name (s_con);
159 devtimeout = devtimeout_from_file (nm_ifcfg_connection_get_path (self));
163 if (nm_platform_link_get_ifindex (ifname) != 0)
166 /* ONBOOT=yes, DEVICE and DEVTIMEOUT are set, but device is not present */
167 nm_settings_connection_set_ready (NM_SETTINGS_CONNECTION (self), FALSE);
169 nm_log_info (LOGD_SETTINGS, "Waiting %u seconds for %s to appear for connection '%s'",
170 devtimeout, ifname, nm_connection_get_id (NM_CONNECTION (self)));
172 priv->devtimeout_link_changed_handler =
173 g_signal_connect (nm_platform_get (), NM_PLATFORM_SIGNAL_LINK_CHANGED,
174 G_CALLBACK (link_changed), self);
175 priv->devtimeout_timeout_id = g_timeout_add_seconds (devtimeout, devtimeout_expired, self);
179 files_changed_cb (NMInotifyHelper *ih,
180 struct inotify_event *evt,
184 NMIfcfgConnection *self = NM_IFCFG_CONNECTION (user_data);
185 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (self);
187 if ( (evt->wd != priv->file_wd)
188 && (evt->wd != priv->keyfile_wd)
189 && (evt->wd != priv->routefile_wd)
190 && (evt->wd != priv->route6file_wd))
193 /* push the event up to the plugin */
194 g_signal_emit (self, signals[IFCFG_CHANGED], 0);
198 nm_ifcfg_connection_new (NMConnection *source,
199 const char *full_path,
204 char *unhandled_spec = NULL;
205 const char *unmanaged_spec = NULL, *unrecognized_spec = NULL;
206 gboolean update_unsaved = TRUE;
208 g_assert (source || full_path);
210 /* If we're given a connection already, prefer that instead of re-reading */
212 tmp = g_object_ref (source);
214 tmp = connection_from_file (full_path,
220 /* If we just read the connection from disk, it's clearly not Unsaved */
221 update_unsaved = FALSE;
224 if (unhandled_spec && g_str_has_prefix (unhandled_spec, "unmanaged:"))
225 unmanaged_spec = unhandled_spec + strlen ("unmanaged:");
226 else if (unhandled_spec && g_str_has_prefix (unhandled_spec, "unrecognized:"))
227 unrecognized_spec = unhandled_spec + strlen ("unrecognized:");
229 object = (GObject *) g_object_new (NM_TYPE_IFCFG_CONNECTION,
230 NM_SETTINGS_CONNECTION_FILENAME, full_path,
231 NM_IFCFG_CONNECTION_UNMANAGED_SPEC, unmanaged_spec,
232 NM_IFCFG_CONNECTION_UNRECOGNIZED_SPEC, unrecognized_spec,
234 /* Update our settings with what was read from the file */
235 if (nm_settings_connection_replace_settings (NM_SETTINGS_CONNECTION (object),
239 nm_ifcfg_connection_check_devtimeout (NM_IFCFG_CONNECTION (object));
241 g_clear_object (&object);
243 g_object_unref (tmp);
244 g_free (unhandled_spec);
245 return (NMIfcfgConnection *) object;
249 path_watch_stop (NMIfcfgConnection *self)
251 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (self);
254 ih = nm_inotify_helper_get ();
256 if (priv->ih_event_id) {
257 g_signal_handler_disconnect (ih, priv->ih_event_id);
258 priv->ih_event_id = 0;
261 if (priv->file_wd >= 0) {
262 nm_inotify_helper_remove_watch (ih, priv->file_wd);
266 g_free (priv->keyfile);
267 priv->keyfile = NULL;
268 if (priv->keyfile_wd >= 0) {
269 nm_inotify_helper_remove_watch (ih, priv->keyfile_wd);
270 priv->keyfile_wd = -1;
273 g_free (priv->routefile);
274 priv->routefile = NULL;
275 if (priv->routefile_wd >= 0) {
276 nm_inotify_helper_remove_watch (ih, priv->routefile_wd);
277 priv->routefile_wd = -1;
280 g_free (priv->route6file);
281 priv->route6file = NULL;
282 if (priv->route6file_wd >= 0) {
283 nm_inotify_helper_remove_watch (ih, priv->route6file_wd);
284 priv->route6file_wd = -1;
289 filename_changed (GObject *object,
293 NMIfcfgConnection *self = NM_IFCFG_CONNECTION (object);
294 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (self);
295 const char *ifcfg_path;
297 path_watch_stop (self);
299 ifcfg_path = nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (self));
303 priv->keyfile = utils_get_keys_path (ifcfg_path);
304 priv->routefile = utils_get_route_path (ifcfg_path);
305 priv->route6file = utils_get_route6_path (ifcfg_path);
307 if (nm_config_get_monitor_connection_files (nm_config_get ())) {
308 NMInotifyHelper *ih = nm_inotify_helper_get ();
310 priv->ih_event_id = g_signal_connect (ih, "event", G_CALLBACK (files_changed_cb), self);
311 priv->file_wd = nm_inotify_helper_add_watch (ih, ifcfg_path);
312 priv->keyfile_wd = nm_inotify_helper_add_watch (ih, priv->keyfile);
313 priv->routefile_wd = nm_inotify_helper_add_watch (ih, priv->routefile);
314 priv->route6file_wd = nm_inotify_helper_add_watch (ih, priv->route6file);
319 nm_ifcfg_connection_get_unmanaged_spec (NMIfcfgConnection *self)
321 g_return_val_if_fail (NM_IS_IFCFG_CONNECTION (self), NULL);
323 return NM_IFCFG_CONNECTION_GET_PRIVATE (self)->unmanaged_spec;
327 nm_ifcfg_connection_get_unrecognized_spec (NMIfcfgConnection *self)
329 g_return_val_if_fail (NM_IS_IFCFG_CONNECTION (self), NULL);
331 return NM_IFCFG_CONNECTION_GET_PRIVATE (self)->unrecognized_spec;
335 replace_and_commit (NMSettingsConnection *connection,
336 NMConnection *new_connection,
337 NMSettingsConnectionCommitFunc callback,
340 const char *filename;
341 GError *error = NULL;
343 filename = nm_settings_connection_get_filename (connection);
344 if (filename && utils_has_complex_routes (filename)) {
346 error = g_error_new_literal (NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
347 "Cannot modify a connection that has an associated 'rule-' or 'rule6-' file");
348 callback (connection, error, user_data);
349 g_clear_error (&error);
353 NM_SETTINGS_CONNECTION_CLASS (nm_ifcfg_connection_parent_class)->replace_and_commit (connection, new_connection, callback, user_data);
357 commit_changes (NMSettingsConnection *connection,
358 NMSettingsConnectionCommitFunc callback,
361 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (connection);
362 GError *error = NULL;
363 NMConnection *reread;
364 gboolean same = FALSE, success = FALSE;
365 char *ifcfg_path = NULL;
366 const char *filename;
368 /* To ensure we don't rewrite files that are only changed from other
369 * processes on-disk, read the existing connection back in and only rewrite
370 * it if it's really changed.
372 filename = nm_settings_connection_get_filename (connection);
374 reread = connection_from_file (filename, NULL, NULL);
376 same = nm_connection_compare (NM_CONNECTION (connection),
378 NM_SETTING_COMPARE_FLAG_IGNORE_AGENT_OWNED_SECRETS |
379 NM_SETTING_COMPARE_FLAG_IGNORE_NOT_SAVED_SECRETS);
380 g_object_unref (reread);
382 /* Don't bother writing anything out if in-memory and on-disk data are the same */
384 /* But chain up to parent to handle success - emits updated signal */
385 NM_SETTINGS_CONNECTION_CLASS (nm_ifcfg_connection_parent_class)->commit_changes (connection, callback, user_data);
390 success = writer_update_connection (NM_CONNECTION (connection),
396 success = writer_new_connection (NM_CONNECTION (connection),
401 nm_settings_connection_set_filename (connection, ifcfg_path);
407 /* Chain up to parent to handle success */
408 NM_SETTINGS_CONNECTION_CLASS (nm_ifcfg_connection_parent_class)->commit_changes (connection, callback, user_data);
410 /* Otherwise immediate error */
411 callback (connection, error, user_data);
412 g_error_free (error);
417 do_delete (NMSettingsConnection *connection,
418 NMSettingsConnectionDeleteFunc callback,
421 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (connection);
422 const char *filename;
424 filename = nm_settings_connection_get_filename (connection);
428 g_unlink (priv->keyfile);
430 g_unlink (priv->routefile);
431 if (priv->route6file)
432 g_unlink (priv->route6file);
435 NM_SETTINGS_CONNECTION_CLASS (nm_ifcfg_connection_parent_class)->delete (connection, callback, user_data);
441 nm_ifcfg_connection_init (NMIfcfgConnection *connection)
443 g_signal_connect (connection, "notify::" NM_SETTINGS_CONNECTION_FILENAME,
444 G_CALLBACK (filename_changed), NULL);
448 set_property (GObject *object, guint prop_id,
449 const GValue *value, GParamSpec *pspec)
451 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (object);
454 case PROP_UNMANAGED_SPEC:
455 priv->unmanaged_spec = g_value_dup_string (value);
457 case PROP_UNRECOGNIZED_SPEC:
458 priv->unrecognized_spec = g_value_dup_string (value);
461 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
467 get_property (GObject *object, guint prop_id,
468 GValue *value, GParamSpec *pspec)
470 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (object);
473 case PROP_UNMANAGED_SPEC:
474 g_value_set_string (value, priv->unmanaged_spec);
476 case PROP_UNRECOGNIZED_SPEC:
477 g_value_set_string (value, priv->unrecognized_spec);
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486 dispose (GObject *object)
488 NMIfcfgConnectionPrivate *priv = NM_IFCFG_CONNECTION_GET_PRIVATE (object);
490 path_watch_stop (NM_IFCFG_CONNECTION (object));
492 if (priv->devtimeout_link_changed_handler) {
493 g_signal_handler_disconnect (nm_platform_get (),
494 priv->devtimeout_link_changed_handler);
495 priv->devtimeout_link_changed_handler = 0;
497 if (priv->devtimeout_timeout_id) {
498 g_source_remove (priv->devtimeout_timeout_id);
499 priv->devtimeout_timeout_id = 0;
502 G_OBJECT_CLASS (nm_ifcfg_connection_parent_class)->dispose (object);
506 nm_ifcfg_connection_class_init (NMIfcfgConnectionClass *ifcfg_connection_class)
508 GObjectClass *object_class = G_OBJECT_CLASS (ifcfg_connection_class);
509 NMSettingsConnectionClass *settings_class = NM_SETTINGS_CONNECTION_CLASS (ifcfg_connection_class);
511 g_type_class_add_private (ifcfg_connection_class, sizeof (NMIfcfgConnectionPrivate));
513 /* Virtual methods */
514 object_class->set_property = set_property;
515 object_class->get_property = get_property;
516 object_class->dispose = dispose;
517 settings_class->delete = do_delete;
518 settings_class->replace_and_commit = replace_and_commit;
519 settings_class->commit_changes = commit_changes;
522 g_object_class_install_property
523 (object_class, PROP_UNMANAGED_SPEC,
524 g_param_spec_string (NM_IFCFG_CONNECTION_UNMANAGED_SPEC, "", "",
527 G_PARAM_STATIC_STRINGS));
528 g_object_class_install_property
529 (object_class, PROP_UNRECOGNIZED_SPEC,
530 g_param_spec_string (NM_IFCFG_CONNECTION_UNRECOGNIZED_SPEC, "", "",
533 G_PARAM_STATIC_STRINGS));
535 signals[IFCFG_CHANGED] =
536 g_signal_new ("ifcfg-changed",
537 G_OBJECT_CLASS_TYPE (object_class),
540 g_cclosure_marshal_VOID__VOID,