nm-dispatcher-utils.c 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * 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.
 *
 * Copyright (C) 2008 - 2011 Red Hat, Inc.
 */

21 22
#include "config.h"

23 24 25 26
#include <string.h>

#include <glib-object.h>

27
#include <nm-dbus-interface.h>
28 29 30 31 32
#include <nm-connection.h>
#include <nm-setting-ip4-config.h>
#include <nm-setting-ip6-config.h>
#include <nm-setting-connection.h>

33
#include "nm-dispatcher-api.h"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include "nm-utils.h"

#include "nm-dispatcher-utils.h"

static GSList *
construct_basic_items (GSList *list,
                       const char *uuid,
                       const char *id,
                       const char *iface,
                       const char *ip_iface)
{
	if (uuid)
		list = g_slist_prepend (list, g_strdup_printf ("CONNECTION_UUID=%s", uuid));
	if (id)
		list = g_slist_prepend (list, g_strdup_printf ("CONNECTION_ID=%s", id));
	if (iface)
		list = g_slist_prepend (list, g_strdup_printf ("DEVICE_IFACE=%s", iface));
	if (ip_iface)
		list = g_slist_prepend (list, g_strdup_printf ("DEVICE_IP_IFACE=%s", ip_iface));
	return list;
}

static GSList *
add_domains (GSList *items,
Dan Winship's avatar
Dan Winship committed
58
             GVariant *dict,
59 60 61
             const char *prefix,
             const char four_or_six)
{
Dan Winship's avatar
Dan Winship committed
62
	GVariant *val;
63
	char **domains = NULL;
64 65 66 67
	GString *tmp;
	guint i;

	/* Search domains */
Dan Winship's avatar
Dan Winship committed
68
	val = g_variant_lookup_value (dict, "domains", G_VARIANT_TYPE_STRING_ARRAY);
69
	if (!val)
70 71
		return items;

Dan Winship's avatar
Dan Winship committed
72 73 74 75
	domains = g_variant_dup_strv (val, NULL);
	g_variant_unref (val);
	if (!domains[0]) {
		g_strfreev (domains);
76
		return items;
Dan Winship's avatar
Dan Winship committed
77
	}
78 79 80

	tmp = g_string_new (NULL);
	g_string_append_printf (tmp, "%sIP%c_DOMAINS=", prefix, four_or_six);
81
	for (i = 0; domains[i]; i++) {
82 83
		if (i > 0)
			g_string_append_c (tmp, ' ');
84
		g_string_append (tmp, domains[i]);
85
	}
Dan Winship's avatar
Dan Winship committed
86
	items = g_slist_prepend (items, g_string_free (tmp, FALSE));
87

Dan Winship's avatar
Dan Winship committed
88
	g_strfreev (domains);
89 90 91 92
	return items;
}

static GSList *
Dan Winship's avatar
Dan Winship committed
93
construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix)
94
{
Dan Winship's avatar
Dan Winship committed
95
	GPtrArray *addresses, *routes;
96
	char **dns, **wins, *gateway;
97
	GString *tmp;
Dan Winship's avatar
Dan Winship committed
98 99
	GVariant *val;
	int i;
100 101 102 103 104 105 106 107

	if (ip4_config == NULL)
		return items;

	if (prefix == NULL)
		prefix = "";

	/* IP addresses */
Dan Winship's avatar
Dan Winship committed
108
	val = g_variant_lookup_value (ip4_config, "addresses", G_VARIANT_TYPE ("aau"));
109
	if (val) {
110 111 112
		addresses = nm_utils_ip4_addresses_from_variant (val, &gateway);
		if (!gateway)
			gateway = g_strdup ("0.0.0.0");
113

Dan Winship's avatar
Dan Winship committed
114
		for (i = 0; i < addresses->len; i++) {
115
			NMIPAddress *addr = addresses->pdata[i];
116
			char *addrtmp;
117

118 119 120
			addrtmp = g_strdup_printf ("%sIP4_ADDRESS_%d=%s/%d %s", prefix, i,
			                           nm_ip_address_get_address (addr),
			                           nm_ip_address_get_prefix (addr),
121
			                           gateway);
122 123
			items = g_slist_prepend (items, addrtmp);
		}
Dan Winship's avatar
Dan Winship committed
124 125
		if (addresses->len)
			items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ADDRESSES=%d", prefix, addresses->len));
126 127 128 129

		/* Write gateway to a separate variable, too. */
		items = g_slist_prepend (items, g_strdup_printf ("%sIP4_GATEWAY=%s", prefix, gateway));

Dan Winship's avatar
Dan Winship committed
130
		g_ptr_array_unref (addresses);
131
		g_free (gateway);
Dan Winship's avatar
Dan Winship committed
132
		g_variant_unref (val);
133
	}
134 135

	/* DNS servers */
Dan Winship's avatar
Dan Winship committed
136 137 138
	val = g_variant_lookup_value (ip4_config, "nameservers", G_VARIANT_TYPE ("au"));
	if (val) {
		dns = nm_utils_ip4_dns_from_variant (val);
139

Dan Winship's avatar
Dan Winship committed
140
		if (dns[0]) {
141 142
			tmp = g_string_new (NULL);
			g_string_append_printf (tmp, "%sIP4_NAMESERVERS=", prefix);
Dan Winship's avatar
Dan Winship committed
143 144
			for (i = 0; dns[i]; i++) {
				if (i != 0)
145
					g_string_append_c (tmp, ' ');
Dan Winship's avatar
Dan Winship committed
146
				g_string_append (tmp, dns[i]);
147
			}
Dan Winship's avatar
Dan Winship committed
148 149

			items = g_slist_prepend (items, g_string_free (tmp, FALSE));
150
		}
Dan Winship's avatar
Dan Winship committed
151 152
		g_strfreev (dns);
		g_variant_unref (val);
153 154 155 156 157 158
	}

	/* Search domains */
	items = add_domains (items, ip4_config, prefix, '4');

	/* WINS servers */
Dan Winship's avatar
Dan Winship committed
159 160 161
	val = g_variant_lookup_value (ip4_config, "wins-servers", G_VARIANT_TYPE ("au"));
	if (val) {
		wins = nm_utils_ip4_dns_from_variant (val);
162

Dan Winship's avatar
Dan Winship committed
163
		if (wins[0]) {
164 165 166
			tmp = g_string_new (NULL);
			g_string_append_printf (tmp, "%sIP4_WINS_SERVERS=", prefix);

Dan Winship's avatar
Dan Winship committed
167 168
			for (i = 0; wins[i]; i++) {
				if (i != 0)
169
					g_string_append_c (tmp, ' ');
Dan Winship's avatar
Dan Winship committed
170
				g_string_append (tmp, wins[i]);
171
			}
Dan Winship's avatar
Dan Winship committed
172 173

			items = g_slist_prepend (items, g_string_free (tmp, FALSE));
174
		}
Dan Winship's avatar
Dan Winship committed
175 176
		g_strfreev (wins);
		g_variant_unref (val);
177 178 179
	}

	/* Static routes */
Dan Winship's avatar
Dan Winship committed
180
	val = g_variant_lookup_value (ip4_config, "routes", G_VARIANT_TYPE ("aau"));
181
	if (val) {
Dan Winship's avatar
Dan Winship committed
182
		routes = nm_utils_ip4_routes_from_variant (val);
183

Dan Winship's avatar
Dan Winship committed
184
		for (i = 0; i < routes->len; i++) {
185 186
			NMIPRoute *route = routes->pdata[i];
			const char *next_hop;
187
			char *routetmp;
188

189 190 191
			next_hop = nm_ip_route_get_next_hop (route);
			if (!next_hop)
				next_hop = "0.0.0.0";
192

193
			routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %u", prefix, i,
194 195 196
			                            nm_ip_route_get_dest (route),
			                            nm_ip_route_get_prefix (route),
			                            next_hop,
197
			                            (guint32) MAX (0, nm_ip_route_get_metric (route)));
198 199
			items = g_slist_prepend (items, routetmp);
		}
Dan Winship's avatar
Dan Winship committed
200 201 202
		items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=%d", prefix, routes->len));
		g_ptr_array_unref (routes);
		g_variant_unref (val);
203 204
	} else
		items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=0", prefix));
205 206 207 208 209

	return items;
}

static GSList *
Dan Winship's avatar
Dan Winship committed
210
construct_device_dhcp4_items (GSList *items, GVariant *dhcp4_config)
211
{
Dan Winship's avatar
Dan Winship committed
212
	GVariantIter iter;
213
	const char *key, *tmp;
Dan Winship's avatar
Dan Winship committed
214
	GVariant *val;
215
	char *ucased;
216 217 218 219

	if (dhcp4_config == NULL)
		return items;

Dan Winship's avatar
Dan Winship committed
220 221
	g_variant_iter_init (&iter, dhcp4_config);
	while (g_variant_iter_next (&iter, "{&sv}", &key, &val)) {
222
		ucased = g_ascii_strup (key, -1);
Dan Winship's avatar
Dan Winship committed
223
		tmp = g_variant_get_string (val, NULL);
224 225
		items = g_slist_prepend (items, g_strdup_printf ("DHCP4_%s=%s", ucased, tmp));
		g_free (ucased);
226 227 228 229 230
	}
	return items;
}

static GSList *
Dan Winship's avatar
Dan Winship committed
231
construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix)
232
{
Dan Winship's avatar
Dan Winship committed
233
	GPtrArray *addresses, *routes;
234
	char **dns, *gateway = NULL;
235
	GString *tmp;
Dan Winship's avatar
Dan Winship committed
236 237
	GVariant *val;
	int i;
238 239 240 241 242 243 244 245

	if (ip6_config == NULL)
		return items;

	if (prefix == NULL)
		prefix = "";

	/* IP addresses */
Dan Winship's avatar
Dan Winship committed
246
	val = g_variant_lookup_value (ip6_config, "addresses", G_VARIANT_TYPE ("a(ayuay)"));
247
	if (val) {
248 249 250
		addresses = nm_utils_ip6_addresses_from_variant (val, &gateway);
		if (!gateway)
			gateway = g_strdup ("::");
251

Dan Winship's avatar
Dan Winship committed
252
		for (i = 0; i < addresses->len; i++) {
253
			NMIPAddress *addr = addresses->pdata[i];
254
			char *addrtmp;
255

256 257 258
			addrtmp = g_strdup_printf ("%sIP6_ADDRESS_%d=%s/%d %s", prefix, i,
			                           nm_ip_address_get_address (addr),
			                           nm_ip_address_get_prefix (addr),
259
			                           gateway);
260 261
			items = g_slist_prepend (items, addrtmp);
		}
Dan Winship's avatar
Dan Winship committed
262 263
		if (addresses->len)
			items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ADDRESSES=%d", prefix, addresses->len));
264 265 266 267

		/* Write gateway to a separate variable, too. */
		items = g_slist_prepend (items, g_strdup_printf ("%sIP6_GATEWAY=%s", prefix, gateway));

Dan Winship's avatar
Dan Winship committed
268
		g_ptr_array_unref (addresses);
269
		g_free (gateway);
Dan Winship's avatar
Dan Winship committed
270
		g_variant_unref (val);
271
	}
272 273

	/* DNS servers */
Dan Winship's avatar
Dan Winship committed
274
	val = g_variant_lookup_value (ip6_config, "nameservers", G_VARIANT_TYPE ("aay"));
275
	if (val) {
Dan Winship's avatar
Dan Winship committed
276
		dns = nm_utils_ip6_dns_from_variant (val);
277

Dan Winship's avatar
Dan Winship committed
278
		if (dns[0]) {
279 280
			tmp = g_string_new (NULL);
			g_string_append_printf (tmp, "%sIP6_NAMESERVERS=", prefix);
281

Dan Winship's avatar
Dan Winship committed
282 283
			for (i = 0; dns[i]; i++) {
				if (i != 0)
284
					g_string_append_c (tmp, ' ');
Dan Winship's avatar
Dan Winship committed
285
				g_string_append (tmp, dns[i]);
286
			}
Thomas Haller's avatar
Thomas Haller committed
287

Dan Winship's avatar
Dan Winship committed
288
			items = g_slist_prepend (items, g_string_free (tmp, FALSE));
289
		}
Dan Winship's avatar
Dan Winship committed
290 291
		g_strfreev (dns);
		g_variant_unref (val);
292 293 294 295 296 297
	}

	/* Search domains */
	items = add_domains (items, ip6_config, prefix, '6');

	/* Static routes */
Dan Winship's avatar
Dan Winship committed
298
	val = g_variant_lookup_value (ip6_config, "routes", G_VARIANT_TYPE ("a(ayuayu)"));
299
	if (val) {
Dan Winship's avatar
Dan Winship committed
300
		routes = nm_utils_ip6_routes_from_variant (val);
301

Dan Winship's avatar
Dan Winship committed
302
		for (i = 0; i < routes->len; i++) {
303 304
			NMIPRoute *route = routes->pdata[i];
			const char *next_hop;
305
			char *routetmp;
306

307 308 309
			next_hop = nm_ip_route_get_next_hop (route);
			if (!next_hop)
				next_hop = "::";
310

311
			routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %u", prefix, i,
312 313 314
			                            nm_ip_route_get_dest (route),
			                            nm_ip_route_get_prefix (route),
			                            next_hop,
315
			                            (guint32) MAX (0, nm_ip_route_get_metric (route)));
316 317
			items = g_slist_prepend (items, routetmp);
		}
Dan Winship's avatar
Dan Winship committed
318 319 320 321
		if (routes->len)
			items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ROUTES=%d", prefix, routes->len));
		g_ptr_array_unref (routes);
		g_variant_unref (val);
322
	}
323 324 325 326 327

	return items;
}

static GSList *
Dan Winship's avatar
Dan Winship committed
328
construct_device_dhcp6_items (GSList *items, GVariant *dhcp6_config)
329
{
Dan Winship's avatar
Dan Winship committed
330
	GVariantIter iter;
331
	const char *key, *tmp;
Dan Winship's avatar
Dan Winship committed
332
	GVariant *val;
333
	char *ucased;
334 335 336 337

	if (dhcp6_config == NULL)
		return items;

Dan Winship's avatar
Dan Winship committed
338 339
	g_variant_iter_init (&iter, dhcp6_config);
	while (g_variant_iter_next (&iter, "{&sv}", &key, &val)) {
340
		ucased = g_ascii_strup (key, -1);
Dan Winship's avatar
Dan Winship committed
341
		tmp = g_variant_get_string (val, NULL);
342 343
		items = g_slist_prepend (items, g_strdup_printf ("DHCP6_%s=%s", ucased, tmp));
		g_free (ucased);
344 345 346 347 348 349
	}
	return items;
}

char **
nm_dispatcher_utils_construct_envp (const char *action,
Dan Winship's avatar
Dan Winship committed
350 351 352 353 354 355 356
                                    GVariant *connection_dict,
                                    GVariant *connection_props,
                                    GVariant *device_props,
                                    GVariant *device_ip4_props,
                                    GVariant *device_ip6_props,
                                    GVariant *device_dhcp4_props,
                                    GVariant *device_dhcp6_props,
357
                                    const char *vpn_ip_iface,
Dan Winship's avatar
Dan Winship committed
358 359
                                    GVariant *vpn_ip4_props,
                                    GVariant *vpn_ip6_props,
360 361 362
                                    char **out_iface)
{
	const char *iface = NULL, *ip_iface = NULL;
363
	const char *uuid = NULL, *id = NULL, *path;
364
	NMDeviceState dev_state = NM_DEVICE_STATE_UNKNOWN;
Dan Winship's avatar
Dan Winship committed
365
	GVariant *value;
366
	char **envp = NULL, *path_item;
367 368
	GSList *items = NULL, *iter;
	guint i;
Dan Winship's avatar
Dan Winship committed
369
	GVariant *con_setting;
370 371 372 373 374 375 376

	g_return_val_if_fail (action != NULL, NULL);
	g_return_val_if_fail (out_iface != NULL, NULL);
	g_return_val_if_fail (*out_iface == NULL, NULL);

	/* Hostname changes don't require a device nor contain a connection */
	if (!strcmp (action, "hostname"))
377
		goto done;
378

379 380 381 382 383 384
	/* Canonicalize the VPN interface name; "" is used when passing it through
	 * D-Bus so make sure that's fixed up here.
	 */
	if (vpn_ip_iface && !strlen (vpn_ip_iface))
		vpn_ip_iface = NULL;

385
	/* interface name */
Dan Winship's avatar
Dan Winship committed
386
	if (!g_variant_lookup (device_props, NMD_DEVICE_PROPS_INTERFACE, "&s", &iface)) {
387 388 389
		g_warning ("Missing or invalid required value " NMD_DEVICE_PROPS_INTERFACE "!");
		return NULL;
	}
Dan Winship's avatar
Dan Winship committed
390
	if (!*iface)
391
		iface = NULL;
392 393

	/* IP interface name */
Dan Winship's avatar
Dan Winship committed
394
	value = g_variant_lookup_value (device_props, NMD_DEVICE_PROPS_IP_INTERFACE, NULL);
395
	if (value) {
Dan Winship's avatar
Dan Winship committed
396 397
		if (!g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) {
			g_warning ("Invalid value " NMD_DEVICE_PROPS_IP_INTERFACE "!");
398 399
			return NULL;
		}
Dan Winship's avatar
Dan Winship committed
400 401
		g_variant_unref (value);
		g_variant_lookup (device_props, NMD_DEVICE_PROPS_IP_INTERFACE, "&s", &ip_iface);
Thomas Haller's avatar
Thomas Haller committed
402
	}
403 404

	/* Device type */
Dan Winship's avatar
Dan Winship committed
405
	if (!g_variant_lookup (device_props, NMD_DEVICE_PROPS_TYPE, "u", NULL)) {
406 407 408 409 410
		g_warning ("Missing or invalid required value " NMD_DEVICE_PROPS_TYPE "!");
		return NULL;
	}

	/* Device state */
Dan Winship's avatar
Dan Winship committed
411 412
	value = g_variant_lookup_value (device_props, NMD_DEVICE_PROPS_STATE, G_VARIANT_TYPE_UINT32);
	if (!value) {
413 414 415
		g_warning ("Missing or invalid required value " NMD_DEVICE_PROPS_STATE "!");
		return NULL;
	}
Dan Winship's avatar
Dan Winship committed
416 417
	dev_state = g_variant_get_uint32 (value);
	g_variant_unref (value);
418 419

	/* device itself */
Dan Winship's avatar
Dan Winship committed
420
	if (!g_variant_lookup (device_props, NMD_DEVICE_PROPS_PATH, "o", NULL)) {
421 422 423 424
		g_warning ("Missing or invalid required value " NMD_DEVICE_PROPS_PATH "!");
		return NULL;
	}

Dan Winship's avatar
Dan Winship committed
425
	/* UUID and ID */
426
	con_setting = g_variant_lookup_value (connection_dict, NM_SETTING_CONNECTION_SETTING_NAME, NM_VARIANT_TYPE_SETTING);
Dan Winship's avatar
Dan Winship committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	if (!con_setting) {
		g_warning ("Failed to read connection setting");
		return NULL;
	}

	if (!g_variant_lookup (con_setting, NM_SETTING_CONNECTION_UUID, "&s", &uuid)) {
		g_warning ("Connection hash did not contain the UUID");
		g_variant_unref (con_setting);
		return NULL;
	}

	if (!g_variant_lookup (con_setting, NM_SETTING_CONNECTION_ID, "&s", &id)) {
		g_warning ("Connection hash did not contain the ID");
		g_variant_unref (con_setting);
		return NULL;
	}

444
	items = construct_basic_items (items, uuid, id, iface, ip_iface);
Dan Winship's avatar
Dan Winship committed
445
	g_variant_unref (con_setting);
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

	/* Device it's aren't valid if the device isn't activated */
	if (iface && (dev_state == NM_DEVICE_STATE_ACTIVATED)) {
		items = construct_ip4_items (items, device_ip4_props, NULL);
		items = construct_ip6_items (items, device_ip6_props, NULL);
		items = construct_device_dhcp4_items (items, device_dhcp4_props);
		items = construct_device_dhcp6_items (items, device_dhcp6_props);
	}

	if (vpn_ip_iface) {
		items = g_slist_prepend (items, g_strdup_printf ("VPN_IP_IFACE=%s", vpn_ip_iface));
		items = construct_ip4_items (items, vpn_ip4_props, "VPN_");
		items = construct_ip6_items (items, vpn_ip6_props, "VPN_");
	}

	/* Backwards compat: 'iface' is set in this order:
	 * 1) VPN interface name
	 * 2) Device IP interface name
	 * 3) Device interface anme
	 */
	if (vpn_ip_iface)
		*out_iface = g_strdup (vpn_ip_iface);
	else if (ip_iface)
		*out_iface = g_strdup (ip_iface);
	else
		*out_iface = g_strdup (iface);

473 474 475 476 477 478 479 480 481 482 483 484 485
 done:
	path = g_getenv ("PATH");
	if (path) {
		path_item = g_strdup_printf ("PATH=%s", path);
		items = g_slist_prepend (items, path_item);
	}

	/* Convert the list to an environment pointer */
	envp = g_new0 (char *, g_slist_length (items) + 1);
	for (iter = items, i = 0; iter; iter = g_slist_next (iter), i++)
		envp[i] = (char *) iter->data;
	g_slist_free (items);

486 487 488
	return envp;
}