We are currently experiencing downtime impacting viewing & cloning the Mesa repo, and some GitLab pages returning 503. Please see #freedesktop on IRC for more updates.

reader.c 138 KB
Newer Older
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 3 4 5 6 7 8 9 10 11 12 13
/* NetworkManager system settings service
 *
 * 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.
 *
14 15 16
 * 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.
17
 *
18
 * Copyright (C) 2008 - 2013 Red Hat, Inc.
19 20
 */

21
#include <config.h>
22 23 24 25 26
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
27
#include <sys/wait.h>
28
#include <sys/inotify.h>
29 30 31
#include <errno.h>
#include <sys/ioctl.h>
#include <unistd.h>
32 33

#include <glib.h>
34
#include <glib/gi18n.h>
35
#include <nm-connection.h>
36
#include <nm-dbus-interface.h>
37 38
#include <nm-setting-connection.h>
#include <nm-setting-ip4-config.h>
Weiping Pan's avatar
Weiping Pan committed
39
#include <nm-setting-vlan.h>
40
#include <nm-setting-ip6-config.h>
41
#include <nm-setting-wired.h>
42
#include <nm-setting-wireless.h>
43
#include <nm-setting-8021x.h>
44
#include <nm-setting-bond.h>
45 46
#include <nm-setting-team.h>
#include <nm-setting-team-port.h>
47
#include <nm-setting-bridge.h>
48
#include <nm-setting-bridge-port.h>
49
#include <nm-setting-dcb.h>
50
#include <nm-setting-generic.h>
51
#include "nm-core-internal.h"
52
#include <nm-utils.h>
53

54
#include "nm-platform.h"
55
#include "nm-posix-signals.h"
56
#include "NetworkManagerUtils.h"
57
#include "nm-logging.h"
58

59
#include "common.h"
60
#include "shvar.h"
61
#include "utils.h"
62

63
#include "reader.h"
64

65
#define PARSE_WARNING(msg...) nm_log_warn (LOGD_SETTINGS, "    " msg)
66

67 68 69 70
static gboolean
get_int (const char *str, int *value)
{
	char *e;
71
	long int tmp;
72

73
	errno = 0;
74
	tmp = strtol (str, &e, 0);
75
	if (errno || *e != '\0' || tmp > G_MAXINT || tmp < G_MININT)
76
		return FALSE;
77 78 79
	*value = (int) tmp;
	return TRUE;
}
80

81 82 83 84 85 86 87 88 89 90 91
static gboolean
get_uint (const char *str, guint32 *value)
{
	char *e;
	long unsigned int tmp;

	errno = 0;
	tmp = strtoul (str, &e, 0);
	if (errno || *e != '\0')
		return FALSE;
	*value = (guint32) tmp;
92 93 94
	return TRUE;
}

95
static char *
96 97 98 99
make_connection_name (shvarFile *ifcfg,
                      const char *ifcfg_name,
                      const char *suggested,
                      const char *prefix)
100
{
101
	char *full_name = NULL, *name;
102 103 104 105 106 107 108 109 110

	/* If the ifcfg file already has a NAME, always use that */
	name = svGetValue (ifcfg, "NAME", FALSE);
	if (name && strlen (name))
		return name;

	/* Otherwise construct a new NAME */
	g_free (name);
	if (!prefix)
111
		prefix = _("System");
112 113 114 115 116

	/* For cosmetic reasons, if the suggested name is the same as
	 * the ifcfg files name, don't use it.  Mainly for wifi so that
	 * the SSID is shown in the connection ID instead of just "wlan0".
	 */
117 118 119 120
	if (suggested && strcmp (ifcfg_name, suggested))
		full_name = g_strdup_printf ("%s %s (%s)", prefix, suggested, ifcfg_name);
	else
		full_name = g_strdup_printf ("%s %s", prefix, ifcfg_name);
121 122 123 124

	return full_name;
}

125 126 127 128
static NMSetting *
make_connection_setting (const char *file,
                         shvarFile *ifcfg,
                         const char *type,
129 130
                         const char *suggested,
                         const char *prefix)
131 132
{
	NMSettingConnection *s_con;
133
	const char *ifcfg_name = NULL;
134
	char *new_id, *uuid = NULL, *zone = NULL, *value;
135

136
	ifcfg_name = utils_get_ifcfg_name (file, TRUE);
137 138 139 140
	if (!ifcfg_name)
		return NULL;

	s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());
141

142 143
	new_id = make_connection_name (ifcfg, ifcfg_name, suggested, prefix);
	g_object_set (s_con, NM_SETTING_CONNECTION_ID, new_id, NULL);
144
	g_free (new_id);
145

146
	/* Try for a UUID key before falling back to hashing the file name */
Dan Williams's avatar
Dan Williams committed
147
	uuid = svGetValue (ifcfg, "UUID", FALSE);
148 149 150 151
	if (!uuid || !strlen (uuid)) {
		g_free (uuid);
		uuid = nm_utils_uuid_generate_from_string (ifcfg->fileName);
	}
152

153 154 155 156 157
	g_object_set (s_con,
	              NM_SETTING_CONNECTION_TYPE, type,
	              NM_SETTING_CONNECTION_UUID, uuid,
	              NULL);
	g_free (uuid);
158

159 160 161 162 163 164 165
	value = svGetValue (ifcfg, "DEVICE", FALSE);
	if (value) {
		if (nm_utils_iface_valid_name (value)) {
			g_object_set (s_con,
			              NM_SETTING_CONNECTION_INTERFACE_NAME, value,
			              NULL);
		} else
166
			PARSE_WARNING ("invalid DEVICE name '%s'", value);
167 168 169
		g_free (value);
	}

170
	/* Missing ONBOOT is treated as "ONBOOT=true" by the old network service */
Dan Williams's avatar
Dan Williams committed
171
	g_object_set (s_con, NM_SETTING_CONNECTION_AUTOCONNECT,
172
	              svTrueValue (ifcfg, "ONBOOT", TRUE),
Dan Williams's avatar
Dan Williams committed
173
	              NULL);
174

175 176 177 178 179 180 181 182
	value = svGetValue (ifcfg, "USERS", FALSE);
	if (value) {
		char **items, **iter;

		items = g_strsplit_set (value, " ", -1);
		for (iter = items; iter && *iter; iter++) {
			if (strlen (*iter)) {
				if (!nm_setting_connection_add_permission (s_con, "user", *iter, NULL))
183
					PARSE_WARNING ("invalid USERS item '%s'", *iter);
184 185 186
			}
		}
		g_free (value);
187
		g_strfreev (items);
188 189
	}

190 191 192 193 194 195 196 197 198

	zone = svGetValue(ifcfg, "ZONE", FALSE);
	if (!zone || !strlen (zone)) {
		g_free (zone);
		zone = NULL;
	}
	g_object_set (s_con, NM_SETTING_CONNECTION_ZONE, zone, NULL);
	g_free (zone);

199 200 201 202 203 204 205 206
	value = svGetValue (ifcfg, "SECONDARY_UUIDS", FALSE);
	if (value) {
		char **items, **iter;

		items = g_strsplit_set (value, " \t", -1);
		for (iter = items; iter && *iter; iter++) {
			if (strlen (*iter)) {
				if (!nm_setting_connection_add_secondary (s_con, *iter))
207
					PARSE_WARNING ("secondary connection UUID '%s' already added", *iter);
208 209 210 211 212 213
			}
		}
		g_free (value);
		g_strfreev (items);
	}

214 215
	value = svGetValue (ifcfg, "BRIDGE", FALSE);
	if (value) {
216
		const char *old_value;
217

218
		if ((old_value = nm_setting_connection_get_master (s_con))) {
219
			PARSE_WARNING ("Already configured as slave of %s. Ignoring BRIDGE=\"%s\"",
220 221 222 223 224
			               old_value, value);
		} else {
			g_object_set (s_con, NM_SETTING_CONNECTION_MASTER, value, NULL);
			g_object_set (s_con, NM_SETTING_CONNECTION_SLAVE_TYPE,
			              NM_SETTING_BRIDGE_SETTING_NAME, NULL);
225 226 227 228
		}
		g_free (value);
	}

229 230 231 232 233 234 235 236 237 238 239
	value = svGetValue (ifcfg, "GATEWAY_PING_TIMEOUT", FALSE);
	if (value) {
		long int tmp;
		guint32 timeout;

		errno = 0;
		tmp = strtol (value, NULL, 10);
		if (errno == 0 && tmp >= 0 && tmp < G_MAXINT32) {
			timeout = (guint32) tmp;
			g_object_set (s_con, NM_SETTING_CONNECTION_GATEWAY_PING_TIMEOUT, timeout, NULL);
		} else
240
			PARSE_WARNING ("invalid GATEWAY_PING_TIMEOUT time");
241 242 243
		g_free (value);
	}

244
	return NM_SETTING (s_con);
245 246
}

247
static gboolean
248
read_mac_address (shvarFile *ifcfg, const char *key, gsize len,
249
                  GByteArray **array, GError **error)
250 251 252 253 254 255
{
	char *value = NULL;

	g_return_val_if_fail (ifcfg != NULL, FALSE);
	g_return_val_if_fail (array != NULL, FALSE);
	g_return_val_if_fail (*array == NULL, FALSE);
256 257
	if (error)
		g_return_val_if_fail (*error == NULL, FALSE);
258

259
	value = svGetValue (ifcfg, key, FALSE);
260 261 262 263 264
	if (!value || !strlen (value)) {
		g_free (value);
		return TRUE;
	}

265
	*array = nm_utils_hwaddr_atoba (value, len);
266
	if (!*array) {
267
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
268
		             "%s: the MAC address '%s' was invalid.", key, value);
269
		g_free (value);
270 271 272 273 274 275 276
		return FALSE;
	}

	g_free (value);
	return TRUE;
}

277
/* Returns TRUE on missing address or valid address */
278 279
static gboolean
read_ip4_address (shvarFile *ifcfg,
280 281 282
                  const char *tag,
                  guint32 *out_addr,
                  GError **error)
283
{
284
	char *value = NULL;
285
	guint32 ip4_addr;
286 287 288 289 290
	gboolean success = FALSE;

	g_return_val_if_fail (ifcfg != NULL, FALSE);
	g_return_val_if_fail (tag != NULL, FALSE);
	g_return_val_if_fail (out_addr != NULL, FALSE);
291 292
	if (error)
		g_return_val_if_fail (*error == NULL, FALSE);
293

294
	*out_addr = 0;
295

Dan Williams's avatar
Dan Williams committed
296
	value = svGetValue (ifcfg, tag, FALSE);
297
	if (!value)
298
		return TRUE;
299

300
	if (inet_pton (AF_INET, value, &ip4_addr) > 0) {
301
		*out_addr = ip4_addr;
302 303
		success = TRUE;
	} else {
304
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
305
		             "Invalid %s IP4 address '%s'", tag, value);
306
	}
307
	g_free (value);
308
	return success;
309 310
}

311
/* Returns TRUE on valid address, including unspecified (::) */
312 313
static gboolean
parse_ip6_address (const char *value,
314 315
                   struct in6_addr *out_addr,
                   GError **error)
316 317 318 319 320
{
	struct in6_addr ip6_addr;

	g_return_val_if_fail (value != NULL, FALSE);
	g_return_val_if_fail (out_addr != NULL, FALSE);
321 322
	if (error)
		g_return_val_if_fail (*error == NULL, FALSE);
323 324

	*out_addr = in6addr_any;
325
	if (inet_pton (AF_INET6, value, &ip6_addr) <= 0) {
326
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
327
		             "Invalid IP6 address '%s'", value);
328
		return FALSE;
329
	}
330 331 332

	*out_addr = ip6_addr;
	return TRUE;
333 334
}

335 336
static char *
get_numbered_tag (char *tag_name, int which)
337 338 339 340 341 342 343 344 345
{
	if (which == -1)
		return g_strdup (tag_name);
	return g_strdup_printf ("%s%u", tag_name, which);
}

static gboolean
is_any_ip4_address_defined (shvarFile *ifcfg)
{
346
	int i;
347 348

	for (i = -1; i <= 2; i++) {
349
		char *tag;
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
		char *value;

		tag = get_numbered_tag ("IPADDR", i);
		value = svGetValue (ifcfg, tag, FALSE);
		g_free (tag);
		if (value) {
			g_free (value);
			return TRUE;
		}

		tag = get_numbered_tag ("PREFIX", i);
		value = svGetValue (ifcfg, tag, FALSE);
		g_free(tag);
		if (value) {
			g_free (value);
			return TRUE;
		}

		tag = get_numbered_tag ("NETMASK", i);
		value = svGetValue (ifcfg, tag, FALSE);
		g_free(tag);
		if (value) {
			g_free (value);
			return TRUE;
		}
	}
	return FALSE;
}

379 380
/* Returns TRUE on missing address or valid address */
static gboolean
381 382
read_full_ip4_address (shvarFile *ifcfg,
                       const char *network_file,
383
                       gint32 which,
384
                       NMIP4Address *addr,
385 386 387 388 389 390 391 392
                       GError **error)
{
	char *ip_tag, *prefix_tag, *netmask_tag, *gw_tag;
	guint32 tmp;
	gboolean success = FALSE;
	shvarFile *network_ifcfg;
	char *value;

393 394 395
	g_return_val_if_fail (which >= -1, FALSE);
	g_return_val_if_fail (ifcfg != NULL, FALSE);
	g_return_val_if_fail (network_file != NULL, FALSE);
396
	g_return_val_if_fail (addr != NULL, FALSE);
397 398
	if (error)
		g_return_val_if_fail (*error == NULL, FALSE);
399

400 401 402 403
	ip_tag = get_numbered_tag ("IPADDR", which);
	prefix_tag = get_numbered_tag ("PREFIX", which);
	netmask_tag = get_numbered_tag ("NETMASK", which);
	gw_tag = get_numbered_tag ("GATEWAY", which);
404 405 406

	/* IP address */
	if (!read_ip4_address (ifcfg, ip_tag, &tmp, error))
407
		goto done;
408 409 410 411
	if (tmp)
		nm_ip4_address_set_address (addr, tmp);
	else if (!nm_ip4_address_get_address (addr)) {
		success = TRUE;
412
		goto done;
413
	}
414 415 416

	/* Gateway */
	if (!read_ip4_address (ifcfg, gw_tag, &tmp, error))
417
		goto done;
418 419 420 421 422 423
	if (tmp)
		nm_ip4_address_set_gateway (addr, tmp);
	else {
		gboolean read_success;

		/* If no gateway in the ifcfg, try /etc/sysconfig/network instead */
424
		network_ifcfg = svOpenFile (network_file, NULL);
425 426 427 428
		if (network_ifcfg) {
			read_success = read_ip4_address (network_ifcfg, "GATEWAY", &tmp, error);
			svCloseFile (network_ifcfg);
			if (!read_success)
429
				goto done;
430 431 432 433 434 435 436 437 438 439 440 441
			nm_ip4_address_set_gateway (addr, tmp);
		}
	}

	/* Prefix */
	value = svGetValue (ifcfg, prefix_tag, FALSE);
	if (value) {
		long int prefix;

		errno = 0;
		prefix = strtol (value, NULL, 10);
		if (errno || prefix <= 0 || prefix > 32) {
442
			g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
443 444
			             "Invalid IP4 prefix '%s'", value);
			g_free (value);
445
			goto done;
446 447 448 449 450 451 452 453
		}
		nm_ip4_address_set_prefix (addr, (guint32) prefix);
		g_free (value);
	}

	/* Fall back to NETMASK if no PREFIX was specified */
	if (!nm_ip4_address_get_prefix (addr)) {
		if (!read_ip4_address (ifcfg, netmask_tag, &tmp, error))
454
			goto done;
455 456
		if (tmp)
			nm_ip4_address_set_prefix (addr, nm_utils_ip4_netmask_to_prefix (tmp));
457 458
	}

459 460
	/* Try to autodetermine the prefix for the address' class */
	if (!nm_ip4_address_get_prefix (addr)) {
461
		guint32 prefix = 0;
462

463
		prefix = nm_utils_ip4_get_default_prefix (nm_ip4_address_get_address (addr));
464 465 466
		nm_ip4_address_set_prefix (addr, prefix);

		value = svGetValue (ifcfg, ip_tag, FALSE);
467
		PARSE_WARNING ("missing %s, assuming %s/%u", prefix_tag, value, prefix);
468 469 470
		g_free (value);
	}

471
	/* Validate the prefix */
472
	if (nm_ip4_address_get_prefix (addr) > 32) {
473
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
474 475
		             "Missing or invalid IP4 prefix '%d'",
		             nm_ip4_address_get_prefix (addr));
476
		goto done;
477 478 479 480
	}

	success = TRUE;

481
done:
482 483 484 485
	g_free (ip_tag);
	g_free (prefix_tag);
	g_free (netmask_tag);
	g_free (gw_tag);
486 487

	return success;
488
}
489

490 491
/* Returns TRUE on missing route or valid route */
static gboolean
492 493 494
read_one_ip4_route (shvarFile *ifcfg,
                    const char *network_file,
                    guint32 which,
495
                    NMIP4Route **out_route,
496 497 498 499 500 501 502
                    GError **error)
{
	NMIP4Route *route;
	char *ip_tag, *netmask_tag, *gw_tag, *metric_tag, *value;
	guint32 tmp;
	gboolean success = FALSE;

503 504 505 506 507 508
	g_return_val_if_fail (ifcfg != NULL, FALSE);
	g_return_val_if_fail (network_file != NULL, FALSE);
	g_return_val_if_fail (out_route != NULL, FALSE);
	g_return_val_if_fail (*out_route == NULL, FALSE);
	if (error)
		g_return_val_if_fail (*error == NULL, FALSE);
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

	route = nm_ip4_route_new ();

	ip_tag = g_strdup_printf ("ADDRESS%u", which);
	netmask_tag = g_strdup_printf ("NETMASK%u", which);
	gw_tag = g_strdup_printf ("GATEWAY%u", which);
	metric_tag = g_strdup_printf ("METRIC%u", which);

	/* Destination */
	if (!read_ip4_address (ifcfg, ip_tag, &tmp, error))
		goto out;
	if (!tmp) {
		/* Check whether IP is missing or 0.0.0.0 */
		char *val;
		val = svGetValue (ifcfg, ip_tag, FALSE);
		if (!val) {
			nm_ip4_route_unref (route);
			route = NULL;
527
			success = TRUE;  /* missing route = success */
528 529 530 531 532 533 534 535 536
			goto out;
		}
		g_free (val);
	}
	nm_ip4_route_set_dest (route, tmp);

	/* Next hop */
	if (!read_ip4_address (ifcfg, gw_tag, &tmp, error))
		goto out;
537
	/* No need to check tmp, because we don't make distinction between missing GATEWAY IP and 0.0.0.0 */
538 539 540 541 542
	nm_ip4_route_set_next_hop (route, tmp);

	/* Prefix */
	if (!read_ip4_address (ifcfg, netmask_tag, &tmp, error))
		goto out;
543 544
	if (tmp)
		nm_ip4_route_set_prefix (route, nm_utils_ip4_netmask_to_prefix (tmp));
545 546 547 548

	/* Validate the prefix */
	if (  !nm_ip4_route_get_prefix (route)
	    || nm_ip4_route_get_prefix (route) > 32) {
549
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
550 551 552 553 554 555 556 557 558 559 560 561 562
		             "Missing or invalid IP4 prefix '%d'",
		             nm_ip4_route_get_prefix (route));
		goto out;
	}

	/* Metric */
	value = svGetValue (ifcfg, metric_tag, FALSE);
	if (value) {
		long int metric;

		errno = 0;
		metric = strtol (value, NULL, 10);
		if (errno || metric < 0) {
563
			g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
564 565 566 567 568 569 570 571
			             "Invalid IP4 route metric '%s'", value);
			g_free (value);
			goto out;
		}
		nm_ip4_route_set_metric (route, (guint32) metric);
		g_free (value);
	}

572
	*out_route = route;
573 574 575
	success = TRUE;

out:
576
	if (!success && route)
577 578 579 580 581 582
		nm_ip4_route_unref (route);

	g_free (ip_tag);
	g_free (netmask_tag);
	g_free (gw_tag);
	g_free (metric_tag);
583
	return success;
584 585 586 587 588 589 590 591 592 593 594
}

static gboolean
read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError **error)
{
	char *contents = NULL;
	gsize len = 0;
	char **lines = NULL, **iter;
	GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric;
	GMatchInfo *match_info;
	NMIP4Route *route;
595
	guint32 ip4_addr;
596
	char *dest = NULL, *prefix = NULL, *metric = NULL;
597 598 599 600 601 602 603 604 605 606 607 608 609
	long int prefix_int, metric_int;
	gboolean success = FALSE;

	const char *pattern_empty = "^\\s*(\\#.*)?$";
	const char *pattern_to1 = "^\\s*(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|default)"  /* IP or 'default' keyword */
	                          "(?:/(\\d{1,2}))?";                                         /* optional prefix */
	const char *pattern_to2 = "to\\s+(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|default)" /* IP or 'default' keyword */
	                          "(?:/(\\d{1,2}))?";                                         /* optional prefix */
	const char *pattern_via = "via\\s+(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})";       /* IP of gateway */
	const char *pattern_metric = "metric\\s+(\\d+)";                                      /* metric */

	g_return_val_if_fail (filename != NULL, FALSE);
	g_return_val_if_fail (s_ip4 != NULL, FALSE);
610 611
	if (error)
		g_return_val_if_fail (*error == NULL, FALSE);
612 613

	/* Read the route file */
614
	if (!g_file_get_contents (filename, &contents, &len, NULL) || !len) {
615
		g_free (contents);
616
		return TRUE;  /* missing/empty = success */
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
	}

	/* Create regexes for pieces to be matched */
	regex_to1 = g_regex_new (pattern_to1, 0, 0, NULL);
	regex_to2 = g_regex_new (pattern_to2, 0, 0, NULL);
	regex_via = g_regex_new (pattern_via, 0, 0, NULL);
	regex_metric = g_regex_new (pattern_metric, 0, 0, NULL);

	/* New NMIP4Route structure */
	route = nm_ip4_route_new ();

	/* Iterate through file lines */
	lines = g_strsplit_set (contents, "\n\r", -1);
	for (iter = lines; iter && *iter; iter++) {

		/* Skip empty lines */
		if (g_regex_match_simple (pattern_empty, *iter, 0, 0))
			continue;

		/* Destination */
		g_regex_match (regex_to1, *iter, 0, &match_info);
		if (!g_match_info_matches (match_info)) {
			g_match_info_free (match_info);
			g_regex_match (regex_to2, *iter, 0, &match_info);
			if (!g_match_info_matches (match_info)) {
				g_match_info_free (match_info);
643
				g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
644 645 646 647 648 649 650 651
					     "Missing IP4 route destination address in record: '%s'", *iter);
				goto error;
			}
		}
		dest = g_match_info_fetch (match_info, 1);
		if (!strcmp (dest, "default"))
			strcpy (dest, "0.0.0.0");
		if (inet_pton (AF_INET, dest, &ip4_addr) != 1) {
652
			g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
653 654 655 656
				     "Invalid IP4 route destination address '%s'", dest);
			g_free (dest);
			goto error;
		}
657
		nm_ip4_route_set_dest (route, ip4_addr);
658 659 660 661
		g_free (dest);

		/* Prefix - is optional; 32 if missing */
		prefix = g_match_info_fetch (match_info, 2);
662
		g_match_info_free (match_info);
663 664 665 666
		prefix_int = 32;
		if (prefix) {
			errno = 0;
			prefix_int = strtol (prefix, NULL, 10);
667
			if (errno || prefix_int <= 0 || prefix_int > 32) {
668
				g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
669 670 671 672 673 674 675 676 677 678
					     "Invalid IP4 route destination prefix '%s'", prefix);
				g_free (prefix);
				goto error;
			}
		}
		nm_ip4_route_set_prefix (route, (guint32) prefix_int);
		g_free (prefix);

		/* Next hop */
		g_regex_match (regex_via, *iter, 0, &match_info);
679 680 681 682 683 684 685 686 687 688
		if (g_match_info_matches (match_info)) {
			char *next_hop = g_match_info_fetch (match_info, 1);
			if (inet_pton (AF_INET, next_hop, &ip4_addr) != 1) {
				g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
				             "Invalid IP4 route gateway address '%s'",
				             next_hop);
				g_match_info_free (match_info);
				g_free (next_hop);
				goto error;
			}
689
			g_free (next_hop);
690 691 692
		} else {
			/* we don't make distinction between missing GATEWAY IP and 0.0.0.0 */
			ip4_addr = 0;
693
		}
694
		nm_ip4_route_set_next_hop (route, ip4_addr);
695
		g_match_info_free (match_info);
696 697 698 699 700 701 702 703 704 705

		/* Metric */
		g_regex_match (regex_metric, *iter, 0, &match_info);
		metric_int = 0;
		if (g_match_info_matches (match_info)) {
			metric = g_match_info_fetch (match_info, 1);
			errno = 0;
			metric_int = strtol (metric, NULL, 10);
			if (errno || metric_int < 0) {
				g_match_info_free (match_info);
706
				g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
707 708 709 710 711 712 713 714 715 716 717
				             "Invalid IP4 route metric '%s'", metric);
				g_free (metric);
				goto error;
			}
			g_free (metric);
		}

		nm_ip4_route_set_metric (route, (guint32) metric_int);
		g_match_info_free (match_info);

		if (!nm_setting_ip4_config_add_route (s_ip4, route))
718
			PARSE_WARNING ("duplicate IP4 route");
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735

	}

	success = TRUE;

error:
	g_free (contents);
	g_strfreev (lines);
	nm_ip4_route_unref (route);
	g_regex_unref (regex_to1);
	g_regex_unref (regex_to2);
	g_regex_unref (regex_via);
	g_regex_unref (regex_metric);

	return success;
}

736
static gboolean
737 738 739 740
parse_full_ip6_address (shvarFile *ifcfg,
                        const char *network_file,
                        const char *addr_str,
                        int i,
741
                        NMIP6Address **out_address,
742
                        GError **error)
743
{
744
	NMIP6Address *addr = NULL;
745
	char **list;
746 747 748
	char *ip_val, *prefix_val;
	shvarFile *network_ifcfg;
	char *value = NULL;
749 750 751
	struct in6_addr tmp = IN6ADDR_ANY_INIT;
	gboolean success = FALSE;

752 753 754 755 756
	g_return_val_if_fail (addr_str != NULL, FALSE);
	g_return_val_if_fail (out_address != NULL, FALSE);
	g_return_val_if_fail (*out_address == NULL, FALSE);
	if (error)
		g_return_val_if_fail (*error == NULL, FALSE);
757

758
	/* Split the address and prefix */
759 760
	list = g_strsplit_set (addr_str, "/", 2);
	if (g_strv_length (list) < 1) {
761
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
762 763 764 765
		             "Invalid IP6 address '%s'", addr_str);
		goto error;
	}

766 767
	ip_val = list[0];
	prefix_val = list[1];
768 769 770

	addr = nm_ip6_address_new ();
	/* IP address */
771 772 773 774 775 776
	if (!parse_ip6_address (ip_val, &tmp, error))
		goto error;
	if (IN6_IS_ADDR_UNSPECIFIED (&tmp)) {
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
		             "Invalid IP6 address '%s'", ip_val);
		goto error;
777 778 779 780
	}
	nm_ip6_address_set_address (addr, &tmp);

	/* Prefix */
781
	if (prefix_val) {
782 783 784
		long int prefix;

		errno = 0;
785
		prefix = strtol (prefix_val, NULL, 10);
786
		if (errno || prefix <= 0 || prefix > 128) {
787
			g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
788
			             "Invalid IP6 prefix '%s'", prefix_val);
789 790 791 792 793 794 795 796
			goto error;
		}
		nm_ip6_address_set_prefix (addr, (guint32) prefix);
	} else {
		/* Missing prefix is treated as prefix of 64 */
		nm_ip6_address_set_prefix (addr, 64);
	}

797 798 799 800 801 802 803 804 805 806
	/* Gateway */
	tmp = in6addr_any;
	value = svGetValue (ifcfg, "IPV6_DEFAULTGW", FALSE);
	if (i != 0) {
		/* We don't support gateways for IPV6ADDR_SECONDARIES yet */
		g_free (value);
		value = NULL;
	}
	if (!value) {
		/* If no gateway in the ifcfg, try global /etc/sysconfig/network instead */
807
		network_ifcfg = svOpenFile (network_file, NULL);
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
		if (network_ifcfg) {
			value = svGetValue (network_ifcfg, "IPV6_DEFAULTGW", FALSE);
			svCloseFile (network_ifcfg);
		}
	}
	if (value) {
		char *ptr;

		if ((ptr = strchr (value, '%')) != NULL)
			*ptr = '\0';  /* remove %interface prefix if present */
		if (!parse_ip6_address (value, &tmp, error))
			goto error;
		nm_ip6_address_set_gateway (addr, &tmp);
	}

823
	*out_address = addr;
824 825 826
	success = TRUE;

error:
827
	if (!success && addr)
828 829 830
		nm_ip6_address_unref (addr);

	g_strfreev (list);
831
	g_free (value);
832
	return success;
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
}

/* IPv6 address is very complex to describe completely by a regular expression,
 * so don't try to, rather use looser syntax to comprise all possibilities
 * NOTE: The regexes below don't describe all variants allowed by 'ip route add',
 * namely destination IP without 'to' keyword is recognized just at line start.
 */
#define IPV6_ADDR_REGEX "[0-9A-Fa-f:.]+"

static gboolean
read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **error)
{
	char *contents = NULL;
	gsize len = 0;
	char **lines = NULL, **iter;
	GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric;
	GMatchInfo *match_info;
	NMIP6Route *route;
	struct in6_addr ip6_addr;
852
	char *dest = NULL, *prefix = NULL, *metric = NULL;
853 854 855 856
	long int prefix_int, metric_int;
	gboolean success = FALSE;

	const char *pattern_empty = "^\\s*(\\#.*)?$";
857
	const char *pattern_to1 = "^\\s*(default|" IPV6_ADDR_REGEX ")"  /* IPv6 or 'default' keyword */
858
	                          "(?:/(\\d{1,3}))?";                   /* optional prefix */
859
	const char *pattern_to2 = "to\\s+(default|" IPV6_ADDR_REGEX ")" /* IPv6 or 'default' keyword */
860
	                          "(?:/(\\d{1,3}))?";                   /* optional prefix */
861 862 863 864 865
	const char *pattern_via = "via\\s+(" IPV6_ADDR_REGEX ")";       /* IPv6 of gateway */
	const char *pattern_metric = "metric\\s+(\\d+)";                /* metric */

	g_return_val_if_fail (filename != NULL, FALSE);
	g_return_val_if_fail (s_ip6 != NULL, FALSE);
866 867
	if (error)
		g_return_val_if_fail (*error == NULL, FALSE);
868 869

	/* Read the route file */
870
	if (!g_file_get_contents (filename, &contents, &len, NULL) || !len) {
871
		g_free (contents);
872
		return TRUE;  /* missing/empty = success */
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
	}

	/* Create regexes for pieces to be matched */
	regex_to1 = g_regex_new (pattern_to1, 0, 0, NULL);
	regex_to2 = g_regex_new (pattern_to2, 0, 0, NULL);
	regex_via = g_regex_new (pattern_via, 0, 0, NULL);
	regex_metric = g_regex_new (pattern_metric, 0, 0, NULL);

	/* New NMIP6Route structure */
	route = nm_ip6_route_new ();

	/* Iterate through file lines */
	lines = g_strsplit_set (contents, "\n\r", -1);
	for (iter = lines; iter && *iter; iter++) {

		/* Skip empty lines */
		if (g_regex_match_simple (pattern_empty, *iter, 0, 0))
			continue;

		/* Destination */
		g_regex_match (regex_to1, *iter, 0, &match_info);
		if (!g_match_info_matches (match_info)) {
			g_match_info_free (match_info);
			g_regex_match (regex_to2, *iter, 0, &match_info);
			if (!g_match_info_matches (match_info)) {
				g_match_info_free (match_info);
899
				g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
900 901 902 903 904
					     "Missing IP6 route destination address in record: '%s'", *iter);
				goto error;
			}
		}
		dest = g_match_info_fetch (match_info, 1);
905 906 907 908
		if (!g_strcmp0 (dest, "default")) {
			/* Ignore default route - NM handles it internally */
			g_free (dest);
			g_match_info_free (match_info);
909
			PARSE_WARNING ("ignoring manual default route: '%s' (%s)", *iter, filename);
910 911
			continue;
		}
912
		if (inet_pton (AF_INET6, dest, &ip6_addr) != 1) {
913
			g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
914 915 916 917 918 919 920 921 922
				     "Invalid IP6 route destination address '%s'", dest);
			g_free (dest);
			goto error;
		}
		nm_ip6_route_set_dest (route, &ip6_addr);
		g_free (dest);

		/* Prefix - is optional; 128 if missing */
		prefix = g_match_info_fetch (match_info, 2);
923
		g_match_info_free (match_info);
924 925 926 927
		prefix_int = 128;
		if (prefix) {
			errno = 0;
			prefix_int = strtol (prefix, NULL, 10);
928
			if (errno || prefix_int <= 0 || prefix_int > 128) {
929
				g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
930 931 932 933 934 935 936 937 938 939
					     "Invalid IP6 route destination prefix '%s'", prefix);
				g_free (prefix);
				goto error;
			}
		}
		nm_ip6_route_set_prefix (route, (guint32) prefix_int);
		g_free (prefix);

		/* Next hop */
		g_regex_match (regex_via, *iter, 0, &match_info);
940 941 942 943 944 945 946 947 948 949
		if (g_match_info_matches (match_info)) {
			char *next_hop = g_match_info_fetch (match_info, 1);
			if (inet_pton (AF_INET6, next_hop, &ip6_addr) != 1) {
				g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
				             "Invalid IPv6 route nexthop address '%s'",
				             next_hop);
				g_match_info_free (match_info);
				g_free (next_hop);
				goto error;
			}
950
			g_free (next_hop);
951 952 953
		} else {
			/* Missing "via" is taken as :: */
			ip6_addr = in6addr_any;
954 955
		}
		nm_ip6_route_set_next_hop (route, &ip6_addr);
956
		g_match_info_free (match_info);
957 958 959 960 961 962 963 964 965 966

		/* Metric */
		g_regex_match (regex_metric, *iter, 0, &match_info);
		metric_int = 0;
		if (g_match_info_matches (match_info)) {
			metric = g_match_info_fetch (match_info, 1);
			errno = 0;
			metric_int = strtol (metric, NULL, 10);
			if (errno || metric_int < 0 || metric_int > G_MAXUINT32) {
				g_match_info_free (match_info);
967
				g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
968 969 970 971 972 973 974 975 976 977 978
				             "Invalid IP6 route metric '%s'", metric);
				g_free (metric);
				goto error;
			}
			g_free (metric);
		}

		nm_ip6_route_set_metric (route, (guint32) metric_int);
		g_match_info_free (match_info);

		if (!nm_setting_ip6_config_add_route (s_ip6, route))
979
			PARSE_WARNING ("duplicate IP6 route");
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
	}

	success = TRUE;

error:
	g_free (contents);
	g_strfreev (lines);
	nm_ip6_route_unref (route);
	g_regex_unref (regex_to1);
	g_regex_unref (regex_to2);
	g_regex_unref (regex_via);
	g_regex_unref (regex_metric);

	return success;
}

996

997
static NMSetting *
998 999 1000
make_ip4_setting (shvarFile *ifcfg,
                  const char *network_file,
                  GError **error)
1001
{
1002 1003
	NMSettingIP4Config *s_ip4 = NULL;
	char *value = NULL;
1004
	char *route_path = NULL;
1005
	char *method;
1006
	gint32 i;
1007
	shvarFile *network_ifcfg;
1008
	shvarFile *route_ifcfg;
1009
	gboolean never_default = FALSE;
1010 1011

	s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
1012

1013 1014 1015 1016 1017 1018 1019 1020
	/* First check if DEFROUTE is set for this device; DEFROUTE has the
	 * opposite meaning from never-default. The default if DEFROUTE is not
	 * specified is DEFROUTE=yes which means that this connection can be used
	 * as a default route
	 */
	never_default = !svTrueValue (ifcfg, "DEFROUTE", TRUE);

	/* Then check if GATEWAYDEV; it's global and overrides DEFROUTE */
1021
	network_ifcfg = svOpenFile (network_file, NULL);
1022 1023 1024 1025
	if (network_ifcfg) {
		char *gatewaydev;

		/* Get the connection ifcfg device name and the global gateway device */
Dan Williams's avatar
Dan Williams committed
1026 1027
		value = svGetValue (ifcfg, "DEVICE", FALSE);
		gatewaydev = svGetValue (network_ifcfg, "GATEWAYDEV", FALSE);
1028 1029 1030 1031

		/* If there was a global gateway device specified, then only connections
		 * for that device can be the default connection.
		 */
1032 1033
		if (gatewaydev && value)
			never_default = !!strcmp (value, gatewaydev);
1034 1035 1036 1037 1038

		g_free (gatewaydev);
		g_free (value);
		svCloseFile (network_ifcfg);
	}
1039

Dan Williams's avatar
Dan Williams committed
1040
	value = svGetValue (ifcfg, "BOOTPROTO", FALSE);
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

	if (!value || !*value || !g_ascii_strcasecmp (value, "none")) {
		if (is_any_ip4_address_defined (ifcfg))
			method = NM_SETTING_IP4_CONFIG_METHOD_MANUAL;
		else
			method = NM_SETTING_IP4_CONFIG_METHOD_DISABLED;
	} else if (!g_ascii_strcasecmp (value, "bootp") || !g_ascii_strcasecmp (value, "dhcp")) {
		method = NM_SETTING_IP4_CONFIG_METHOD_AUTO;
	} else if (!g_ascii_strcasecmp (value, "static")) {
		method = NM_SETTING_IP4_CONFIG_METHOD_MANUAL;
	} else if (!g_ascii_strcasecmp (value, "autoip")) {
		g_free (value);
		g_object_set (s_ip4,
		              NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL,
		              NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, never_default,
		              NULL);
		return NM_SETTING (s_ip4);
	} else if (!g_ascii_strcasecmp (value, "shared")) {
		g_free (value);
		g_object_set (s_ip4,
		              NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_SHARED,
		              NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, never_default,
		              NULL);
		return NM_SETTING (s_ip4);
	} else {
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
		             "Unknown BOOTPROTO '%s'", value);
		g_free (value);
		goto done;
1070
	}
1071
	g_free (value);
1072

1073 1074 1075 1076 1077
	g_object_set (s_ip4,
	              NM_SETTING_IP4_CONFIG_METHOD, method,
	              NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, !svTrueValue (ifcfg, "PEERDNS", TRUE),
	              NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, !svTrueValue (ifcfg, "PEERROUTES", TRUE),
	              NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, never_default,
1078
	              NM_SETTING_IP4_CONFIG_MAY_FAIL, !svTrueValue (ifcfg, "IPV4_FAILURE_FATAL", FALSE),