summaryrefslogtreecommitdiff
path: root/system-settings/plugins/ifcfg-suse/plugin.c
blob: c07ff2df60773ce9c781a67e71d0b03a94289943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */

/* NetworkManager system settings service
 *
 * Søren Sandmann <sandmann@daimi.au.dk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * (C) Copyright 2007 - 2009 Red Hat, Inc.
 * (C) Copyright 2007 - 2008 Novell, Inc.
 */

#include <config.h>
#include <gmodule.h>
#include <glib-object.h>
#include <glib/gi18n.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

#ifndef NO_GIO
#include <gio/gio.h>
#else
#include <gfilemonitor/gfilemonitor.h>
#endif

#include <nm-setting-connection.h>
#include <nm-setting-ip4-config.h>

#define G_UDEV_API_IS_SUBJECT_TO_CHANGE
#include <gudev/gudev.h>

#include "plugin.h"
#include "parser.h"
#include "nm-suse-connection.h"
#include "nm-system-config-interface.h"
#include "wireless-helper.h"

#define IFCFG_PLUGIN_NAME "ifcfg-suse"
#define IFCFG_PLUGIN_INFO "(C) 2008 Novell, Inc.  To report bugs please use the NetworkManager mailing list."
#define IFCFG_DIR SYSCONFDIR "/sysconfig/network"

static void system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);

G_DEFINE_TYPE_EXTENDED (SCPluginIfcfg, sc_plugin_ifcfg, G_TYPE_OBJECT, 0,
                        G_IMPLEMENT_INTERFACE (NM_TYPE_SYSTEM_CONFIG_INTERFACE,
                                               system_config_interface_init))

#define SC_PLUGIN_IFCFG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SC_TYPE_PLUGIN_IFCFG, SCPluginIfcfgPrivate))


#define IFCFG_FILE_PATH_TAG "ifcfg-file-path"

typedef struct {
	GUdevClient *client;

	gboolean initialized;
	GHashTable *connections;
	GHashTable *unmanaged_specs;

	guint32 default_gw;
	GFileMonitor *default_gw_monitor;
	guint default_gw_monitor_id;
} SCPluginIfcfgPrivate;

GQuark
ifcfg_plugin_error_quark (void)
{
	static GQuark error_quark = 0;

	if (G_UNLIKELY (error_quark == 0))
		error_quark = g_quark_from_static_string ("ifcfg-plugin-error-quark");

	return error_quark;
}

static void
update_one_connection (gpointer key, gpointer val, gpointer user_data)
{
	NMExportedConnection *exported = NM_EXPORTED_CONNECTION (val);
	NMConnection *connection;
	NMSettingIP4Config *ip4_config;

	connection = nm_exported_connection_get_connection (exported);
	ip4_config = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
	if (!ip4_config)
		return;

	if (nm_setting_ip4_config_get_num_addresses (ip4_config)) {
		/* suse only has one address per device */
		NMIP4Address *ip4_address = nm_setting_ip4_config_get_address (ip4_config, 0);
		SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (user_data);
		GHashTable *settings;

		if (nm_ip4_address_get_gateway (ip4_address) != priv->default_gw) {
			nm_ip4_address_set_gateway (ip4_address, priv->default_gw);
			settings = nm_connection_to_hash (connection);
			nm_exported_connection_signal_updated (exported, settings);
			g_hash_table_destroy (settings);
		}
	}
}

static void
update_connections (SCPluginIfcfg *self)
{
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);

	g_hash_table_foreach (priv->connections, update_one_connection, self);
}

static void
routes_changed (GFileMonitor *monitor,
			 GFile *file,
			 GFile *other_file,
			 GFileMonitorEvent event_type,
			 gpointer user_data)
{
	SCPluginIfcfg *self = SC_PLUGIN_IFCFG (user_data);
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
	char *filename;
	guint32 new_gw;

	switch (event_type) {
	case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
	case G_FILE_MONITOR_EVENT_DELETED:
		filename = g_file_get_path (file);
		new_gw = parser_parse_routes (filename);
		g_free (filename);

		if (priv->default_gw != new_gw) {
			priv->default_gw = new_gw;
			update_connections (self);
		}
		break;
	default:
		break;
	}
}

static void
monitor_routes (SCPluginIfcfg *self, const char *filename)
{
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
	GFile *file;
	GFileMonitor *monitor;

	file = g_file_new_for_path (filename);
	monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
	g_object_unref (file);

	if (monitor) {
		priv->default_gw_monitor_id = g_signal_connect (monitor, "changed", G_CALLBACK (routes_changed), self);
		priv->default_gw_monitor = monitor;
	}
}

static void
read_connection (SCPluginIfcfg *self, GUdevDevice *device, NMDeviceType dev_type)
{
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
	const char *iface, *address;
	guint32 ifindex;

	iface = g_udev_device_get_name (device);
	if (!iface)
		return;

	ifindex = (guint32) g_udev_device_get_property_as_uint64 (device, "IFINDEX");

	if (parser_ignore_device (iface)) {
		char *spec;

		address = g_udev_device_get_sysfs_attr (device, "address");
		if (address && (strlen (address) == 17)) {
			spec = g_strdup_printf ("mac:%s", address);
			g_hash_table_insert (priv->unmanaged_specs, GUINT_TO_POINTER (ifindex), spec);
			g_signal_emit_by_name (self, "unmanaged-devices-changed");
		} else
			PLUGIN_WARN (IFCFG_PLUGIN_NAME, "    (%s) error getting hardware address", iface);
	} else {
		NMSuseConnection *connection;

		connection = nm_suse_connection_new (iface, dev_type);
		if (connection) {
			g_hash_table_insert (priv->connections,
			                     GUINT_TO_POINTER (ifindex),
			                     connection);
			g_signal_emit_by_name (self, "connection-added", connection);
		}
	}
}

static void
read_connections_by_type (SCPluginIfcfg *self, NMDeviceType dev_type)
{
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
	GList *devices, *iter;

	if (   (dev_type != NM_DEVICE_TYPE_ETHERNET)
	    && (dev_type != NM_DEVICE_TYPE_WIFI))
		return;

	devices = g_udev_client_query_by_subsystem (priv->client, "net");
	for (iter = devices; iter; iter = g_list_next (iter)) {
		read_connection (self, G_UDEV_DEVICE (iter->data), dev_type);
		g_object_unref (G_UDEV_DEVICE (iter->data));
	}
	g_list_free (devices);
}

static gboolean
is_wireless (GUdevDevice *device)
{
	char phy80211_path[255];
	struct stat s;
	int fd;
	struct iwreq iwr;
	const char *ifname, *path;
	gboolean is_wifi = FALSE;

	ifname = g_udev_device_get_name (device);
	g_assert (ifname);

	fd = socket (PF_INET, SOCK_DGRAM, 0);
	strncpy (iwr.ifr_ifrn.ifrn_name, ifname, IFNAMSIZ);

	path = g_udev_device_get_sysfs_path (device);
	snprintf (phy80211_path, sizeof (phy80211_path), "%s/phy80211", path);

	if (   (ioctl (fd, SIOCGIWNAME, &iwr) == 0)
	    || (stat (phy80211_path, &s) == 0 && (s.st_mode & S_IFDIR)))
		is_wifi = TRUE;

	close (fd);
	return is_wifi;
}

static void
handle_uevent (GUdevClient *client,
               const char *action,
               GUdevDevice *device,
               gpointer user_data)
{
	SCPluginIfcfg *self = SC_PLUGIN_IFCFG (user_data);
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
	const char *subsys;
	gboolean wifi;

	g_return_if_fail (action != NULL);

	/* A bit paranoid */
	subsys = g_udev_device_get_subsystem (device);
	g_return_if_fail (subsys != NULL);
	g_return_if_fail (strcmp (subsys, "net") == 0);

	wifi = is_wireless (device);

	if (!strcmp (action, "add")) {
		read_connection (self,
		                 device,
		                 wifi ? NM_DEVICE_TYPE_WIFI : NM_DEVICE_TYPE_ETHERNET);
	} else if (!strcmp (action, "remove")) {
		NMExportedConnection *exported;
		guint32 ifindex;

		ifindex = (guint32) g_udev_device_get_property_as_uint64 (device, "IFINDEX");
		if (g_hash_table_remove (priv->unmanaged_specs, GUINT_TO_POINTER (ifindex)))
			g_signal_emit_by_name (self, "unmanaged-devices-changed");

		exported = (NMExportedConnection *) g_hash_table_lookup (priv->connections,
		                                                         GUINT_TO_POINTER (ifindex));
		if (exported) {
			nm_exported_connection_signal_removed (exported);
			g_hash_table_remove (priv->connections, GUINT_TO_POINTER (ifindex));
		}
	}
}

static void
init (NMSystemConfigInterface *config)
{
	SCPluginIfcfg *self = SC_PLUGIN_IFCFG (config);
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
	const char *subsys[2] = { "net", NULL };

	priv->client = g_udev_client_new (subsys);
	if (!priv->client) {
		PLUGIN_WARN (IFCFG_PLUGIN_NAME, "    error initializing libgudev");
	} else
		g_signal_connect (priv->client, "uevent", G_CALLBACK (handle_uevent), self);
}

static void
get_connections_cb (gpointer key, gpointer val, gpointer user_data)
{
	GSList **list = (GSList **) user_data;

	*list = g_slist_prepend (*list, val);
}

static GSList *
get_connections (NMSystemConfigInterface *config)
{
	SCPluginIfcfg *self = SC_PLUGIN_IFCFG (config);
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
	GSList *list = NULL;

	if (!priv->initialized) {
		const char *filename;

		read_connections_by_type (self, NM_DEVICE_TYPE_ETHERNET);
		read_connections_by_type (self, NM_DEVICE_TYPE_WIFI);

		filename = SYSCONFDIR"/sysconfig/network/routes";
		monitor_routes (self, filename);
		priv->default_gw = parser_parse_routes (filename);
		if (priv->default_gw)
			update_connections (self);

		priv->initialized = TRUE;
	}

	g_hash_table_foreach (priv->connections, get_connections_cb, &list);

	return list;
}

static void
add_one_unmanaged_spec (gpointer key, gpointer val, gpointer user_data)
{
	GSList **list = (GSList **) key;

	*list = g_slist_prepend (*list, g_strdup ((const char *) val));
}

static GSList *
get_unmanaged_specs (NMSystemConfigInterface *config)
{
	SCPluginIfcfg *self = SC_PLUGIN_IFCFG (config);
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
	GSList *list = NULL;

	g_hash_table_foreach (priv->unmanaged_specs, add_one_unmanaged_spec, &list);
	return list;
}

static void
sc_plugin_ifcfg_init (SCPluginIfcfg *self)
{
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);

	priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
	priv->unmanaged_specs = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
}

static void
dispose (GObject *object)
{
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (object);

	g_hash_table_destroy (priv->connections);
	g_hash_table_destroy (priv->unmanaged_specs);

	if (priv->default_gw_monitor) {
		if (priv->default_gw_monitor_id)
			g_signal_handler_disconnect (priv->default_gw_monitor, priv->default_gw_monitor_id);

		g_file_monitor_cancel (priv->default_gw_monitor);
		g_object_unref (priv->default_gw_monitor);
	}

	if (priv->client)
		g_object_unref (priv->client);

	G_OBJECT_CLASS (sc_plugin_ifcfg_parent_class)->dispose (object);
}

static void
get_property (GObject *object, guint prop_id,
		    GValue *value, GParamSpec *pspec)
{
	switch (prop_id) {
	case NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME:
		g_value_set_string (value, IFCFG_PLUGIN_NAME);
		break;
	case NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO:
		g_value_set_string (value, IFCFG_PLUGIN_INFO);
		break;
	case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
		g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE);
		break;
	case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
		g_value_set_string (value, "");
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
sc_plugin_ifcfg_class_init (SCPluginIfcfgClass *req_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (req_class);

	g_type_class_add_private (req_class, sizeof (SCPluginIfcfgPrivate));

	object_class->get_property = get_property;
	object_class->dispose = dispose;

	g_object_class_override_property (object_class,
							    NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME,
							    NM_SYSTEM_CONFIG_INTERFACE_NAME);

	g_object_class_override_property (object_class,
							    NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO,
							    NM_SYSTEM_CONFIG_INTERFACE_INFO);

	g_object_class_override_property (object_class,
							    NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
							    NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);

	g_object_class_override_property (object_class,
							    NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
							    NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}

static void
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class)
{
	/* interface implementation */
	system_config_interface_class->get_connections = get_connections;
	system_config_interface_class->get_unmanaged_specs = get_unmanaged_specs;
	system_config_interface_class->init = init;
}

G_MODULE_EXPORT GObject *
nm_system_config_factory (void)
{
	static SCPluginIfcfg *singleton = NULL;

	if (!singleton)
		singleton = SC_PLUGIN_IFCFG (g_object_new (SC_TYPE_PLUGIN_IFCFG, NULL));
	else
		g_object_ref (singleton);

	return G_OBJECT (singleton);
}