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

#include <string.h>
22 23 24
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
25 26 27 28 29
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
30
#include <netinet/ether.h>
31

32 33 34
#include <nm-setting-connection.h>
#include <nm-setting-wired.h>
#include <nm-setting-wireless.h>
35 36
#include <nm-setting-8021x.h>
#include <nm-setting-ip4-config.h>
37
#include <nm-setting-ip6-config.h>
38
#include <nm-setting-pppoe.h>
Weiping Pan's avatar
Weiping Pan committed
39
#include <nm-setting-vlan.h>
40
#include <nm-utils.h>
41 42 43

#include "common.h"
#include "shvar.h"
44
#include "reader.h"
45
#include "writer.h"
46
#include "utils.h"
47
#include "crypto.h"
48 49 50 51

#define PLUGIN_WARN(pname, fmt, args...) \
	{ g_warning ("   " pname ": " fmt, ##args); }

Dan Williams's avatar
Dan Williams committed
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
static void
save_secret_flags (shvarFile *ifcfg,
                   const char *key,
                   NMSettingSecretFlags flags)
{
	GString *str;

	g_return_if_fail (ifcfg != NULL);
	g_return_if_fail (key != NULL);

	if (flags == NM_SETTING_SECRET_FLAG_NONE) {
		svSetValue (ifcfg, key, NULL, FALSE);
		return;
	}

	/* Convert flags bitfield into string representation */
	str = g_string_sized_new (20);
	if (flags & NM_SETTING_SECRET_FLAG_AGENT_OWNED)
		g_string_append (str, SECRET_FLAG_AGENT);

	if (flags & NM_SETTING_SECRET_FLAG_NOT_SAVED) {
		if (str->len)
			g_string_append_c (str, ' ');
		g_string_append (str, SECRET_FLAG_NOT_SAVED);
	}

	if (flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED) {
		if (str->len)
			g_string_append_c (str, ' ');
		g_string_append (str, SECRET_FLAG_NOT_REQUIRED);
	}

	svSetValue (ifcfg, key, str->len ? str->str : NULL, FALSE);
	g_string_free (str, TRUE);
}

88
static void
89 90 91
set_secret (shvarFile *ifcfg,
            const char *key,
            const char *value,
Dan Williams's avatar
Dan Williams committed
92
            const char *flags_key,
93 94
            NMSettingSecretFlags flags,
            gboolean verbatim)
95 96 97
{
	shvarFile *keyfile;
	
Dan Williams's avatar
Dan Williams committed
98 99 100 101 102 103
	/* Clear the secret from the ifcfg and the associated "keys" file */
	svSetValue (ifcfg, key, NULL, FALSE);

	/* Save secret flags */
	save_secret_flags (ifcfg, flags_key, flags);

104 105 106 107 108 109 110
	keyfile = utils_get_keys_ifcfg (ifcfg->fileName, TRUE);
	if (!keyfile) {
		PLUGIN_WARN (IFCFG_PLUGIN_NAME, "    warning: could not create key file for '%s'",
		             ifcfg->fileName);
		goto error;
	}

Dan Williams's avatar
Dan Williams committed
111
	/* Clear the secret from the associated "keys" file */
112 113
	svSetValue (keyfile, key, NULL, FALSE);

114
	/* Only write the secret if it's system owned and supposed to be saved */
115
	if (flags == NM_SETTING_SECRET_FLAG_NONE)
116
		svSetValue (keyfile, key, value, verbatim);
117

118
	if (svWriteFile (keyfile, 0600)) {
119 120
		PLUGIN_WARN (IFCFG_PLUGIN_NAME, "    warning: could not update key file '%s'",
		             keyfile->fileName);
121
		svCloseFile (keyfile);
122 123 124 125 126 127 128 129 130 131
		goto error;
	}
	svCloseFile (keyfile);
	return;

error:
	/* Try setting the secret in the actual ifcfg */
	svSetValue (ifcfg, key, value, FALSE);
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
static gboolean
write_secret_file (const char *path,
                   const char *data,
                   gsize len,
                   GError **error)
{
	char *tmppath;
	int fd = -1, written;
	gboolean success = FALSE;

	tmppath = g_malloc0 (strlen (path) + 10);
	memcpy (tmppath, path, strlen (path));
	strcat (tmppath, ".XXXXXX");

	errno = 0;
	fd = mkstemp (tmppath);
	if (fd < 0) {
149
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
150 151 152 153 154 155 156 157 158 159
		             "Could not create temporary file for '%s': %d",
		             path, errno);
		goto out;
	}

	/* Only readable by root */
	errno = 0;
	if (fchmod (fd, S_IRUSR | S_IWUSR)) {
		close (fd);
		unlink (tmppath);
160
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
161 162 163 164 165 166 167 168 169 170
		             "Could not set permissions for temporary file '%s': %d",
		             path, errno);
		goto out;
	}

	errno = 0;
	written = write (fd, data, len);
	if (written != len) {
		close (fd);
		unlink (tmppath);
171
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
172 173 174 175 176 177 178 179 180 181
		             "Could not write temporary file for '%s': %d",
		             path, errno);
		goto out;
	}
	close (fd);

	/* Try to rename */
	errno = 0;
	if (rename (tmppath, path)) {
		unlink (tmppath);
182
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
183 184 185 186 187 188 189
		             "Could not rename temporary file to '%s': %d",
		             path, errno);
		goto out;
	}
	success = TRUE;

out:
190
	g_free (tmppath);
191 192 193
	return success;
}

194 195
typedef struct ObjectType {
	const char *setting_key;
Dan Williams's avatar
Dan Williams committed
196 197 198
	NMSetting8021xCKScheme (*scheme_func)(NMSetting8021x *setting);
	const char *           (*path_func)  (NMSetting8021x *setting);
	const GByteArray *     (*blob_func)  (NMSetting8021x *setting);
199 200 201 202 203 204
	const char *ifcfg_key;
	const char *suffix;
} ObjectType;

static const ObjectType ca_type = {
	NM_SETTING_802_1X_CA_CERT,
205 206 207
	nm_setting_802_1x_get_ca_cert_scheme,
	nm_setting_802_1x_get_ca_cert_path,
	nm_setting_802_1x_get_ca_cert_blob,
208 209 210 211
	"IEEE_8021X_CA_CERT",
	"ca-cert.der"
};

212 213
static const ObjectType phase2_ca_type = {
	NM_SETTING_802_1X_PHASE2_CA_CERT,
214 215 216
	nm_setting_802_1x_get_phase2_ca_cert_scheme,
	nm_setting_802_1x_get_phase2_ca_cert_path,
	nm_setting_802_1x_get_phase2_ca_cert_blob,
217 218 219 220
	"IEEE_8021X_INNER_CA_CERT",
	"inner-ca-cert.der"
};

221 222
static const ObjectType client_type = {
	NM_SETTING_802_1X_CLIENT_CERT,
223 224 225
	nm_setting_802_1x_get_client_cert_scheme,
	nm_setting_802_1x_get_client_cert_path,
	nm_setting_802_1x_get_client_cert_blob,
226 227 228 229
	"IEEE_8021X_CLIENT_CERT",
	"client-cert.der"
};

230 231
static const ObjectType phase2_client_type = {
	NM_SETTING_802_1X_PHASE2_CLIENT_CERT,
232 233 234
	nm_setting_802_1x_get_phase2_client_cert_scheme,
	nm_setting_802_1x_get_phase2_client_cert_path,
	nm_setting_802_1x_get_phase2_client_cert_blob,
235 236 237 238
	"IEEE_8021X_INNER_CLIENT_CERT",
	"inner-client-cert.der"
};

239 240
static const ObjectType pk_type = {
	NM_SETTING_802_1X_PRIVATE_KEY,
241 242 243
	nm_setting_802_1x_get_private_key_scheme,
	nm_setting_802_1x_get_private_key_path,
	nm_setting_802_1x_get_private_key_blob,
244 245 246 247
	"IEEE_8021X_PRIVATE_KEY",
	"private-key.pem"
};

248 249
static const ObjectType phase2_pk_type = {
	NM_SETTING_802_1X_PHASE2_PRIVATE_KEY,
250 251 252
	nm_setting_802_1x_get_phase2_private_key_scheme,
	nm_setting_802_1x_get_phase2_private_key_path,
	nm_setting_802_1x_get_phase2_private_key_blob,
253 254 255 256
	"IEEE_8021X_INNER_PRIVATE_KEY",
	"inner-private-key.pem"
};

257 258
static const ObjectType p12_type = {
	NM_SETTING_802_1X_PRIVATE_KEY,
259 260 261
	nm_setting_802_1x_get_private_key_scheme,
	nm_setting_802_1x_get_private_key_path,
	nm_setting_802_1x_get_private_key_blob,
262 263 264 265
	"IEEE_8021X_PRIVATE_KEY",
	"private-key.p12"
};

266 267
static const ObjectType phase2_p12_type = {
	NM_SETTING_802_1X_PHASE2_PRIVATE_KEY,
268 269 270
	nm_setting_802_1x_get_phase2_private_key_scheme,
	nm_setting_802_1x_get_phase2_private_key_path,
	nm_setting_802_1x_get_phase2_private_key_blob,
271 272 273 274
	"IEEE_8021X_INNER_PRIVATE_KEY",
	"inner-private-key.p12"
};

275
static gboolean
276 277 278 279
write_object (NMSetting8021x *s_8021x,
              shvarFile *ifcfg,
              const ObjectType *objtype,
              GError **error)
280
{
281 282 283
	NMSetting8021xCKScheme scheme;
	const char *path = NULL;
	const GByteArray *blob = NULL;
284

285
	g_return_val_if_fail (ifcfg != NULL, FALSE);
286 287
	g_return_val_if_fail (objtype != NULL, FALSE);

Dan Williams's avatar
Dan Williams committed
288 289 290 291 292 293 294 295 296 297
	scheme = (*(objtype->scheme_func))(s_8021x);
	switch (scheme) {
	case NM_SETTING_802_1X_CK_SCHEME_BLOB:
		blob = (*(objtype->blob_func))(s_8021x);
		break;
	case NM_SETTING_802_1X_CK_SCHEME_PATH:
		path = (*(objtype->path_func))(s_8021x);
		break;
	default:
		break;
298
	}
299

Dan Williams's avatar
Dan Williams committed
300
	/* If certificate/private key wasn't sent, the connection may no longer be
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	 * 802.1x and thus we clear out the paths and certs.
	 */
	if (!path && !blob) {
		char *standard_file;
		int ignored;

		/* Since no cert/private key is now being used, delete any standard file
		 * that was created for this connection, but leave other files alone.
		 * Thus, for example,
		 * /etc/sysconfig/network-scripts/ca-cert-Test_Write_Wifi_WPA_EAP-TLS.der
		 * will be deleted, but /etc/pki/tls/cert.pem will not.
		 */
		standard_file = utils_cert_path (ifcfg->fileName, objtype->suffix);
		if (g_file_test (standard_file, G_FILE_TEST_EXISTS))
			ignored = unlink (standard_file);
		g_free (standard_file);
317

318
		svSetValue (ifcfg, objtype->ifcfg_key, NULL, FALSE);
319 320 321
		return TRUE;
	}

322 323 324 325 326 327
	/* If the object path was specified, prefer that over any raw cert data that
	 * may have been sent.
	 */
	if (path) {
		svSetValue (ifcfg, objtype->ifcfg_key, path, FALSE);
		return TRUE;
328 329
	}

Dan Williams's avatar
Dan Williams committed
330
	/* If it's raw certificate data, write the data out to the standard file */
331 332 333 334
	if (blob) {
		gboolean success;
		char *new_file;
		GError *write_error = NULL;
335

336
		new_file = utils_cert_path (ifcfg->fileName, objtype->suffix);
337
		if (!new_file) {
338
			g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
339
			             "Could not create file path for %s / %s",
340
			             NM_SETTING_802_1X_SETTING_NAME, objtype->setting_key);
341
			return FALSE;
342 343
		}

344 345 346 347 348 349 350
		/* Write the raw certificate data out to the standard file so that we
		 * can use paths from now on instead of pushing around the certificate
		 * data itself.
		 */
		success = write_secret_file (new_file, (const char *) blob->data, blob->len, &write_error);
		if (success) {
			svSetValue (ifcfg, objtype->ifcfg_key, new_file, FALSE);
351
			g_free (new_file);
352 353
			return TRUE;
		} else {
354
			g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
355 356
			             "Could not write certificate/key for %s / %s: %s",
			             NM_SETTING_802_1X_SETTING_NAME, objtype->setting_key,
357 358 359
			             (write_error && write_error->message) ? write_error->message : "(unknown)");
			g_clear_error (&write_error);
		}
360
		g_free (new_file);
361 362
	}

363
	return FALSE;
364 365
}

366 367 368 369 370 371 372
static gboolean
write_8021x_certs (NMSetting8021x *s_8021x,
                   gboolean phase2,
                   shvarFile *ifcfg,
                   GError **error)
{
	const char *password = NULL;
373
	gboolean success = FALSE, is_pkcs12 = FALSE;
374
	const ObjectType *otype = NULL;
375
	NMSettingSecretFlags flags = NM_SETTING_SECRET_FLAG_NONE;
376 377

	/* CA certificate */
Dan Williams's avatar
Dan Williams committed
378
	if (!write_object (s_8021x, ifcfg, phase2 ? &phase2_ca_type : &ca_type, error))
379 380 381 382
		return FALSE;

	/* Private key */
	if (phase2) {
Dan Williams's avatar
Dan Williams committed
383 384 385 386
		otype = &phase2_pk_type;
		if (nm_setting_802_1x_get_phase2_private_key_format (s_8021x) == NM_SETTING_802_1X_CK_FORMAT_PKCS12) {
			otype = &phase2_p12_type;
			is_pkcs12 = TRUE;
387 388
		}
		password = nm_setting_802_1x_get_phase2_private_key_password (s_8021x);
389
		flags = nm_setting_802_1x_get_phase2_private_key_password_flags (s_8021x);
390
	} else {
Dan Williams's avatar
Dan Williams committed
391 392 393 394
		otype = &pk_type;
		if (nm_setting_802_1x_get_private_key_format (s_8021x) == NM_SETTING_802_1X_CK_FORMAT_PKCS12) {
			otype = &p12_type;
			is_pkcs12 = TRUE;
395 396
		}
		password = nm_setting_802_1x_get_private_key_password (s_8021x);
397
		flags = nm_setting_802_1x_get_private_key_password_flags (s_8021x);
398 399
	}

400
	/* Save the private key */
Dan Williams's avatar
Dan Williams committed
401
	if (!write_object (s_8021x, ifcfg, otype, error))
402 403 404
		goto out;

	/* Private key password */
Dan Williams's avatar
Dan Williams committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	if (phase2) {
		set_secret (ifcfg,
		            "IEEE_8021X_INNER_PRIVATE_KEY_PASSWORD",
		            password,
		            "IEEE_8021X_INNER_PRIVATE_KEY_PASSWORD_FLAGS",
		            flags,
		            FALSE);
	} else {
		set_secret (ifcfg,
		            "IEEE_8021X_PRIVATE_KEY_PASSWORD",
		            password,
		            "IEEE_8021X_PRIVATE_KEY_PASSWORD_FLAGS",
		            flags,
		            FALSE);
	}
420 421 422

	/* Client certificate */
	if (is_pkcs12) {
Dan Williams's avatar
Dan Williams committed
423 424 425
		/* Don't need a client certificate with PKCS#12 since the file is both
		 * the client certificate and the private key in one file.
		 */
426 427 428 429
		svSetValue (ifcfg,
		            phase2 ? "IEEE_8021X_INNER_CLIENT_CERT" : "IEEE_8021X_CLIENT_CERT",
		            NULL, FALSE);
	} else {
430
		/* Save the client certificate */
Dan Williams's avatar
Dan Williams committed
431
		if (!write_object (s_8021x, ifcfg, phase2 ? &phase2_client_type : &client_type, error))
432 433 434 435 436 437 438 439 440
			goto out;
	}

	success = TRUE;

out:
	return success;
}

441
static gboolean
442 443 444 445
write_8021x_setting (NMConnection *connection,
                     shvarFile *ifcfg,
                     gboolean wired,
                     GError **error)
446 447 448
{
	NMSetting8021x *s_8021x;
	const char *value;
449
	char *tmp = NULL;
450
	gboolean success = FALSE;
451
	GString *phase2_auth;
452

453
	s_8021x = nm_connection_get_setting_802_1x (connection);
454 455 456 457
	if (!s_8021x) {
		/* If wired, clear KEY_MGMT */
		if (wired)
			svSetValue (ifcfg, "KEY_MGMT", NULL, FALSE);
458
		return TRUE;
459 460 461 462 463
	}

	/* If wired, write KEY_MGMT */
	if (wired)
		svSetValue (ifcfg, "KEY_MGMT", "IEEE8021X", FALSE);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

	/* EAP method */
	if (nm_setting_802_1x_get_num_eap_methods (s_8021x)) {
		value = nm_setting_802_1x_get_eap_method (s_8021x, 0);
		if (value)
			tmp = g_ascii_strup (value, -1);
	}
	svSetValue (ifcfg, "IEEE_8021X_EAP_METHODS", tmp ? tmp : NULL, FALSE);
	g_free (tmp);

	svSetValue (ifcfg, "IEEE_8021X_IDENTITY",
	            nm_setting_802_1x_get_identity (s_8021x),
	            FALSE);

	svSetValue (ifcfg, "IEEE_8021X_ANON_IDENTITY",
	            nm_setting_802_1x_get_anonymous_identity (s_8021x),
	            FALSE);

482 483 484
	set_secret (ifcfg,
	            "IEEE_8021X_PASSWORD",
	            nm_setting_802_1x_get_password (s_8021x),
Dan Williams's avatar
Dan Williams committed
485
	            "IEEE_8021X_PASSWORD_FLAGS",
486 487
	            nm_setting_802_1x_get_password_flags (s_8021x),
	            FALSE);
488 489 490 491 492 493 494 495 496 497 498 499 500

	/* PEAP version */
	value = nm_setting_802_1x_get_phase1_peapver (s_8021x);
	svSetValue (ifcfg, "IEEE_8021X_PEAP_VERSION", NULL, FALSE);
	if (value && (!strcmp (value, "0") || !strcmp (value, "1")))
		svSetValue (ifcfg, "IEEE_8021X_PEAP_VERSION", value, FALSE);

	/* Force new PEAP label */
	value = nm_setting_802_1x_get_phase1_peaplabel (s_8021x);
	svSetValue (ifcfg, "IEEE_8021X_PEAP_FORCE_NEW_LABEL", NULL, FALSE);
	if (value && !strcmp (value, "1"))
		svSetValue (ifcfg, "IEEE_8021X_PEAP_FORCE_NEW_LABEL", "yes", FALSE);

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
	/* PAC file */
	value = nm_setting_802_1x_get_pac_file (s_8021x);
	svSetValue (ifcfg, "IEEE_8021X_PAC_FILE", NULL, FALSE);
	if (value)
		svSetValue (ifcfg, "IEEE_8021X_PAC_FILE", value, FALSE);

	/* FAST PAC provisioning */
	value = nm_setting_802_1x_get_phase1_fast_provisioning (s_8021x);
	svSetValue (ifcfg, "IEEE_8021X_FAST_PROVISIONING", NULL, FALSE);
	if (value) {
		if (strcmp (value, "1") == 0)
			svSetValue (ifcfg, "IEEE_8021X_FAST_PROVISIONING", "allow-unauth", FALSE);
		else if (strcmp (value, "2") == 0)
			svSetValue (ifcfg, "IEEE_8021X_FAST_PROVISIONING", "allow-auth", FALSE);
		else if (strcmp (value, "3") == 0)
			svSetValue (ifcfg, "IEEE_8021X_FAST_PROVISIONING", "allow-unauth allow-auth", FALSE);
	}

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	/* Phase2 auth methods */
	svSetValue (ifcfg, "IEEE_8021X_INNER_AUTH_METHODS", NULL, FALSE);
	phase2_auth = g_string_new (NULL);

	value = nm_setting_802_1x_get_phase2_auth (s_8021x);
	if (value) {
		tmp = g_ascii_strup (value, -1);
		g_string_append (phase2_auth, tmp);
		g_free (tmp);
	}

	value = nm_setting_802_1x_get_phase2_autheap (s_8021x);
	if (value) {
		if (phase2_auth->len)
			g_string_append_c (phase2_auth, ' ');

		tmp = g_ascii_strup (value, -1);
		g_string_append_printf (phase2_auth, "EAP-%s", tmp);
		g_free (tmp);
	}

540 541 542 543
	svSetValue (ifcfg, "IEEE_8021X_INNER_AUTH_METHODS",
	            phase2_auth->len ? phase2_auth->str : NULL,
	            FALSE);

544
	g_string_free (phase2_auth, TRUE);
545

546 547 548 549
	success = write_8021x_certs (s_8021x, FALSE, ifcfg, error);
	if (success) {
		/* phase2/inner certs */
		success = write_8021x_certs (s_8021x, TRUE, ifcfg, error);
550 551
	}

552 553 554 555 556 557 558 559 560 561 562 563
	return success;
}

static gboolean
write_wireless_security_setting (NMConnection *connection,
                                 shvarFile *ifcfg,
                                 gboolean adhoc,
                                 gboolean *no_8021x,
                                 GError **error)
{
	NMSettingWirelessSecurity *s_wsec;
	const char *key_mgmt, *auth_alg, *key, *proto, *cipher, *psk;
564
	gboolean wep = FALSE, wpa = FALSE, dynamic_wep = FALSE;
565 566 567 568
	char *tmp;
	guint32 i, num;
	GString *str;

569
	s_wsec = nm_connection_get_setting_wireless_security (connection);
570
	if (!s_wsec) {
571
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
572 573 574 575 576 577 578 579 580 581 582 583
		             "Missing '%s' setting", NM_SETTING_WIRELESS_SECURITY_SETTING_NAME);
		return FALSE;
	}

	key_mgmt = nm_setting_wireless_security_get_key_mgmt (s_wsec);
	g_assert (key_mgmt);

	auth_alg = nm_setting_wireless_security_get_auth_alg (s_wsec);

	svSetValue (ifcfg, "DEFAULTKEY", NULL, FALSE);

	if (!strcmp (key_mgmt, "none")) {
584
		svSetValue (ifcfg, "KEY_MGMT", NULL, FALSE);
585 586 587 588 589 590 591 592
		wep = TRUE;
		*no_8021x = TRUE;
	} else if (!strcmp (key_mgmt, "wpa-none") || !strcmp (key_mgmt, "wpa-psk")) {
		svSetValue (ifcfg, "KEY_MGMT", "WPA-PSK", FALSE);
		wpa = TRUE;
		*no_8021x = TRUE;
	} else if (!strcmp (key_mgmt, "ieee8021x")) {
		svSetValue (ifcfg, "KEY_MGMT", "IEEE8021X", FALSE);
593
		dynamic_wep = TRUE;
594 595 596 597 598 599 600 601 602 603 604 605 606
	} else if (!strcmp (key_mgmt, "wpa-eap")) {
		svSetValue (ifcfg, "KEY_MGMT", "WPA-EAP", FALSE);
		wpa = TRUE;
	}

	svSetValue (ifcfg, "SECURITYMODE", NULL, FALSE);
	if (auth_alg) {
		if (!strcmp (auth_alg, "shared"))
			svSetValue (ifcfg, "SECURITYMODE", "restricted", FALSE);
		else if (!strcmp (auth_alg, "open"))
			svSetValue (ifcfg, "SECURITYMODE", "open", FALSE);
		else if (!strcmp (auth_alg, "leap")) {
			svSetValue (ifcfg, "SECURITYMODE", "leap", FALSE);
607
			svSetValue (ifcfg, "IEEE_8021X_IDENTITY",
608 609
			            nm_setting_wireless_security_get_leap_username (s_wsec),
			            FALSE);
Dan Williams's avatar
Dan Williams committed
610 611
			set_secret (ifcfg,
			            "IEEE_8021X_PASSWORD",
612
			            nm_setting_wireless_security_get_leap_password (s_wsec),
Dan Williams's avatar
Dan Williams committed
613
			            "IEEE_8021X_PASSWORD_FLAGS",
614
			            nm_setting_wireless_security_get_leap_password_flags (s_wsec),
615
			            FALSE);
616 617 618 619
			*no_8021x = TRUE;
		}
	}

620 621
	/* WEP keys */

622
	/* Clear any default key */
Dan Williams's avatar
Dan Williams committed
623
	set_secret (ifcfg, "KEY", NULL, "WEP_KEY_FLAGS", NM_SETTING_SECRET_FLAG_NONE, FALSE);
624

625 626 627
	/* Clear existing keys */
	for (i = 0; i < 4; i++) {
		tmp = g_strdup_printf ("KEY_PASSPHRASE%d", i + 1);
Dan Williams's avatar
Dan Williams committed
628
		set_secret (ifcfg, tmp, NULL, "WEP_KEY_FLAGS", NM_SETTING_SECRET_FLAG_NONE, FALSE);
629 630 631
		g_free (tmp);

		tmp = g_strdup_printf ("KEY%d", i + 1);
Dan Williams's avatar
Dan Williams committed
632
		set_secret (ifcfg, tmp, NULL, "WEP_KEY_FLAGS", NM_SETTING_SECRET_FLAG_NONE, FALSE);
633 634 635 636
		g_free (tmp);
	}

	/* And write the new ones out */
637 638 639 640 641 642
	if (wep) {
		/* Default WEP TX key index */
		tmp = g_strdup_printf ("%d", nm_setting_wireless_security_get_wep_tx_keyidx (s_wsec) + 1);
		svSetValue (ifcfg, "DEFAULTKEY", tmp, FALSE);
		g_free (tmp);

643 644 645 646 647
		for (i = 0; i < 4; i++) {
			NMWepKeyType key_type;

			key = nm_setting_wireless_security_get_wep_key (s_wsec, i);
			if (key) {
648 649
				char *ascii_key = NULL;

650 651 652 653 654 655 656
				/* Passphrase needs a different ifcfg key since with WEP, there
				 * are some passphrases that are indistinguishable from WEP hex
				 * keys.
				 */
				key_type = nm_setting_wireless_security_get_wep_key_type (s_wsec);
				if (key_type == NM_WEP_KEY_TYPE_PASSPHRASE)
					tmp = g_strdup_printf ("KEY_PASSPHRASE%d", i + 1);
657
				else {
658 659
					tmp = g_strdup_printf ("KEY%d", i + 1);

660 661 662 663 664 665 666
					/* Add 's:' prefix for ASCII keys */
					if (strlen (key) == 5 || strlen (key) == 13) {
						ascii_key = g_strdup_printf ("s:%s", key);
						key = ascii_key;
					}
				}

Dan Williams's avatar
Dan Williams committed
667 668 669 670 671 672
				set_secret (ifcfg,
				            tmp,
				            key,
				            "WEP_KEY_FLAGS",
				            nm_setting_wireless_security_get_wep_key_flags (s_wsec),
				            FALSE);
673
				g_free (tmp);
674
				g_free (ascii_key);
675 676
			}
		}
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
	}

	/* WPA protos */
	svSetValue (ifcfg, "WPA_ALLOW_WPA", NULL, FALSE);
	svSetValue (ifcfg, "WPA_ALLOW_WPA2", NULL, FALSE);
	num = nm_setting_wireless_security_get_num_protos (s_wsec);
	for (i = 0; i < num; i++) {
		proto = nm_setting_wireless_security_get_proto (s_wsec, i);
		if (proto && !strcmp (proto, "wpa"))
			svSetValue (ifcfg, "WPA_ALLOW_WPA", "yes", FALSE);
		else if (proto && !strcmp (proto, "rsn"))
			svSetValue (ifcfg, "WPA_ALLOW_WPA2", "yes", FALSE);
	}

	/* WPA Pairwise ciphers */
	svSetValue (ifcfg, "CIPHER_PAIRWISE", NULL, FALSE);
	str = g_string_new (NULL);
	num = nm_setting_wireless_security_get_num_pairwise (s_wsec);
695 696
	for (i = 0; i < num; i++) {
		if (i > 0)
697 698
			g_string_append_c (str, ' ');
		cipher = nm_setting_wireless_security_get_pairwise (s_wsec, i);
699 700 701 702 703 704 705 706 707

		/* Don't write out WEP40 or WEP104 if for some reason they are set; they
		 * are not valid pairwise ciphers.
		 */
		if (strcmp (cipher, "wep40") && strcmp (cipher, "wep104")) {
			tmp = g_ascii_strup (cipher, -1);
			g_string_append (str, tmp);
			g_free (tmp);
		}
708
	}
709
	if (strlen (str->str) && (dynamic_wep == FALSE))
710 711 712 713 714 715 716
		svSetValue (ifcfg, "CIPHER_PAIRWISE", str->str, FALSE);
	g_string_free (str, TRUE);

	/* WPA Group ciphers */
	svSetValue (ifcfg, "CIPHER_GROUP", NULL, FALSE);
	str = g_string_new (NULL);
	num = nm_setting_wireless_security_get_num_groups (s_wsec);
717 718
	for (i = 0; i < num; i++) {
		if (i > 0)
719 720 721 722 723 724
			g_string_append_c (str, ' ');
		cipher = nm_setting_wireless_security_get_group (s_wsec, i);
		tmp = g_ascii_strup (cipher, -1);
		g_string_append (str, tmp);
		g_free (tmp);
	}
725
	if (strlen (str->str) && (dynamic_wep == FALSE))
726 727 728 729
		svSetValue (ifcfg, "CIPHER_GROUP", str->str, FALSE);
	g_string_free (str, TRUE);

	/* WPA Passphrase */
730
	if (wpa) {
731
		char *quoted = NULL;
732 733 734 735

		psk = nm_setting_wireless_security_get_psk (s_wsec);
		if (psk && (strlen (psk) != 64)) {
			/* Quote the PSK since it's a passphrase */
736
			quoted = utils_single_quote_string (psk);
737
		}
738 739
		set_secret (ifcfg,
		            "WPA_PSK",
740
		            quoted ? quoted : psk,
Dan Williams's avatar
Dan Williams committed
741
		            "WPA_PSK_FLAGS",
742 743
		            nm_setting_wireless_security_get_psk_flags (s_wsec),
		            TRUE);
744
		g_free (quoted);
Dan Williams's avatar
Dan Williams committed
745 746 747 748 749 750 751 752
	} else {
		set_secret (ifcfg,
		            "WPA_PSK",
		            NULL,
		            "WPA_PSK_FLAGS",
		            NM_SETTING_SECRET_FLAG_NONE,
		            FALSE);
	}
753 754 755 756 757 758 759 760 761 762 763 764

	return TRUE;
}

static gboolean
write_wireless_setting (NMConnection *connection,
                        shvarFile *ifcfg,
                        gboolean *no_8021x,
                        GError **error)
{
	NMSettingWireless *s_wireless;
	char *tmp, *tmp2;
765
	const GByteArray *ssid, *device_mac, *cloned_mac, *bssid;
766 767 768 769
	const char *mode;
	char buf[33];
	guint32 mtu, chan, i;
	gboolean adhoc = FALSE, hex_ssid = FALSE;
770
	const GSList *macaddr_blacklist;
771

772
	s_wireless = nm_connection_get_setting_wireless (connection);
773
	if (!s_wireless) {
774
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
775 776 777 778
		             "Missing '%s' setting", NM_SETTING_WIRELESS_SETTING_NAME);
		return FALSE;
	}

779
	svSetValue (ifcfg, "HWADDR", NULL, FALSE);
780 781
	device_mac = nm_setting_wireless_get_mac_address (s_wireless);
	if (device_mac) {
782
		tmp = g_strdup_printf ("%02X:%02X:%02X:%02X:%02X:%02X",
783 784
		                       device_mac->data[0], device_mac->data[1], device_mac->data[2],
		                       device_mac->data[3], device_mac->data[4], device_mac->data[5]);
785 786 787 788
		svSetValue (ifcfg, "HWADDR", tmp, FALSE);
		g_free (tmp);
	}

789 790 791 792 793 794 795 796 797 798
	svSetValue (ifcfg, "MACADDR", NULL, FALSE);
	cloned_mac = nm_setting_wireless_get_cloned_mac_address (s_wireless);
	if (cloned_mac) {
		tmp = g_strdup_printf ("%02X:%02X:%02X:%02X:%02X:%02X",
		                       cloned_mac->data[0], cloned_mac->data[1], cloned_mac->data[2],
		                       cloned_mac->data[3], cloned_mac->data[4], cloned_mac->data[5]);
		svSetValue (ifcfg, "MACADDR", tmp, FALSE);
		g_free (tmp);
	}

799
	svSetValue (ifcfg, "HWADDR_BLACKLIST", NULL, FALSE);
800 801 802 803 804 805 806 807 808 809 810 811
	macaddr_blacklist = nm_setting_wireless_get_mac_address_blacklist (s_wireless);
	if (macaddr_blacklist) {
		const GSList *iter;
		GString *blacklist_str = g_string_new (NULL);

		for (iter = macaddr_blacklist; iter; iter = g_slist_next (iter)) {
			g_string_append (blacklist_str, iter->data);
			g_string_append_c (blacklist_str, ' ');

		}
		if (blacklist_str->len > 0)
			g_string_truncate (blacklist_str, blacklist_str->len - 1);
812
		svSetValue (ifcfg, "HWADDR_BLACKLIST", blacklist_str->str, FALSE);
813 814 815
		g_string_free (blacklist_str, TRUE);
	}

816
	svSetValue (ifcfg, "MTU", NULL, FALSE);
817 818 819 820 821 822 823 824 825
	mtu = nm_setting_wireless_get_mtu (s_wireless);
	if (mtu) {
		tmp = g_strdup_printf ("%u", mtu);
		svSetValue (ifcfg, "MTU", tmp, FALSE);
		g_free (tmp);
	}

	ssid = nm_setting_wireless_get_ssid (s_wireless);
	if (!ssid) {
826
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
827 828 829 830
		             "Missing SSID in '%s' setting", NM_SETTING_WIRELESS_SETTING_NAME);
		return FALSE;
	}
	if (!ssid->len || ssid->len > 32) {
831
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
832 833 834 835 836 837 838 839
		             "Invalid SSID in '%s' setting", NM_SETTING_WIRELESS_SETTING_NAME);
		return FALSE;
	}

	/* If the SSID contains any non-printable characters, we need to use the
	 * hex notation of the SSID instead.
	 */
	for (i = 0; i < ssid->len; i++) {
Dan Winship's avatar
Dan Winship committed
840
		if (!g_ascii_isprint (ssid->data[i])) {
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
			hex_ssid = TRUE;
			break;
		}
	}

	if (hex_ssid) {
		GString *str;

		/* Hex SSIDs don't get quoted */
		str = g_string_sized_new (ssid->len * 2 + 3);
		g_string_append (str, "0x");
		for (i = 0; i < ssid->len; i++)
			g_string_append_printf (str, "%02X", ssid->data[i]);
		svSetValue (ifcfg, "ESSID", str->str, TRUE);
		g_string_free (str, TRUE);
	} else {
857
		/* Printable SSIDs always get quoted */
858 859
		memset (buf, 0, sizeof (buf));
		memcpy (buf, ssid->data, ssid->len);
860 861 862 863 864 865 866 867 868 869 870
		tmp = svEscape (buf);

		/* svEscape will usually quote the string, but just for consistency,
		 * if svEscape doesn't quote the ESSID, we quote it ourselves.
		 */
		if (tmp[0] != '"' && tmp[strlen (tmp) - 1] != '"') {
			tmp2 = g_strdup_printf ("\"%s\"", tmp);
			svSetValue (ifcfg, "ESSID", tmp2, TRUE);
			g_free (tmp2);
		} else
			svSetValue (ifcfg, "ESSID", tmp, TRUE);
871 872 873 874 875 876 877 878 879 880
		g_free (tmp);
	}

	mode = nm_setting_wireless_get_mode (s_wireless);
	if (!mode || !strcmp (mode, "infrastructure")) {
		svSetValue (ifcfg, "MODE", "Managed", FALSE);
	} else if (!strcmp (mode, "adhoc")) {
		svSetValue (ifcfg, "MODE", "Ad-Hoc", FALSE);
		adhoc = TRUE;
	} else {
881
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
882 883 884 885 886
		             "Invalid mode '%s' in '%s' setting",
		             mode, NM_SETTING_WIRELESS_SETTING_NAME);
		return FALSE;
	}

887
	svSetValue (ifcfg, "CHANNEL", NULL, FALSE);
888 889
	chan = nm_setting_wireless_get_channel (s_wireless);
	if (chan) {
890
		tmp = g_strdup_printf ("%u", chan);
891 892 893 894
		svSetValue (ifcfg, "CHANNEL", tmp, FALSE);
		g_free (tmp);
	}

895
	svSetValue (ifcfg, "BSSID", NULL, FALSE);
896 897 898 899 900 901 902 903 904
	bssid = nm_setting_wireless_get_bssid (s_wireless);
	if (bssid) {
		tmp = g_strdup_printf ("%02X:%02X:%02X:%02X:%02X:%02X",
		                       bssid->data[0], bssid->data[1], bssid->data[2],
		                       bssid->data[3], bssid->data[4], bssid->data[5]);
		svSetValue (ifcfg, "BSSID", tmp, FALSE);
		g_free (tmp);
	}

905 906 907 908 909 910 911
	/* Ensure DEFAULTKEY and SECURITYMODE are cleared unless there's security;
	 * otherwise there's no way to detect WEP vs. open when WEP keys aren't
	 * saved.
	 */
	svSetValue (ifcfg, "DEFAULTKEY", NULL, FALSE);
	svSetValue (ifcfg, "SECURITYMODE", NULL, FALSE);

912 913 914
	if (nm_setting_wireless_get_security (s_wireless)) {
		if (!write_wireless_security_setting (connection, ifcfg, adhoc, no_8021x, error))
			return FALSE;
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
	} else {
		char *keys_path;

		/* Clear out wifi security keys */
		svSetValue (ifcfg, "KEY_MGMT", NULL, FALSE);
		svSetValue (ifcfg, "IEEE_8021X_IDENTITY", NULL, FALSE);
		set_secret (ifcfg, "IEEE_8021X_PASSWORD", NULL, "IEEE_8021X_PASSWORD_FLAGS", NM_SETTING_SECRET_FLAG_NONE, FALSE);
		svSetValue (ifcfg, "SECURITYMODE", NULL, FALSE);

		/* Clear existing keys */
		set_secret (ifcfg, "KEY", NULL, "WEP_KEY_FLAGS", NM_SETTING_SECRET_FLAG_NONE, FALSE);
		for (i = 0; i < 4; i++) {
			tmp = g_strdup_printf ("KEY_PASSPHRASE%d", i + 1);
			set_secret (ifcfg, tmp, NULL, "WEP_KEY_FLAGS", NM_SETTING_SECRET_FLAG_NONE, FALSE);
			g_free (tmp);
	
			tmp = g_strdup_printf ("KEY%d", i + 1);
			set_secret (ifcfg, tmp, NULL, "WEP_KEY_FLAGS", NM_SETTING_SECRET_FLAG_NONE, FALSE);
			g_free (tmp);
		}

		svSetValue (ifcfg, "DEFAULTKEY", NULL, FALSE);
		svSetValue (ifcfg, "WPA_ALLOW_WPA", NULL, FALSE);
		svSetValue (ifcfg, "WPA_ALLOW_WPA2", NULL, FALSE);
		svSetValue (ifcfg, "CIPHER_PAIRWISE", NULL, FALSE);
		svSetValue (ifcfg, "CIPHER_GROUP", NULL, FALSE);
		set_secret (ifcfg, "WPA_PSK", NULL, "WPA_PSK_FLAGS", NM_SETTING_SECRET_FLAG_NONE, FALSE);

		/* Kill any old keys file */
		keys_path = utils_get_keys_path (ifcfg->fileName);
		(void) unlink (keys_path);
		g_free (keys_path);
947 948 949 950 951 952 953
	}

	svSetValue (ifcfg, "TYPE", TYPE_WIRELESS, FALSE);

	return TRUE;
}

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
static gboolean
write_infiniband_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
{
	NMSettingInfiniband *s_infiniband;
	const GByteArray *mac;
	char *tmp;
	const char *transport_mode;
	guint32 mtu;

	s_infiniband = nm_connection_get_setting_infiniband (connection);
	if (!s_infiniband) {
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
		             "Missing '%s' setting", NM_SETTING_INFINIBAND_SETTING_NAME);
		return FALSE;
	}

	svSetValue (ifcfg, "HWADDR", NULL, FALSE);
	mac = nm_setting_infiniband_get_mac_address (s_infiniband);
	if (mac) {
		tmp = nm_utils_hwaddr_ntoa (mac->data, ARPHRD_INFINIBAND);
		svSetValue (ifcfg, "HWADDR", tmp, FALSE);
		g_free (tmp);
	}

	svSetValue (ifcfg, "MTU", NULL, FALSE);
	mtu = nm_setting_infiniband_get_mtu (s_infiniband);
	if (mtu) {
		tmp = g_strdup_printf ("%u", mtu);
		svSetValue (ifcfg, "MTU", tmp, FALSE);
		g_free (tmp);
	}

	transport_mode = nm_setting_infiniband_get_transport_mode (s_infiniband);
	svSetValue (ifcfg, "CONNECTED_MODE",
	            strcmp (transport_mode, "connected") == 0 ? "yes" : "no",
	            FALSE);

	svSetValue (ifcfg, "TYPE", TYPE_INFINIBAND, FALSE);

	return TRUE;
}

996 997 998 999
static gboolean
write_wired_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
{
	NMSettingWired *s_wired;
1000
	const GByteArray *device_mac, *cloned_mac;
1001
	char *tmp;
1002
	const char *nettype, *portname, *ctcprot, *s390_key, *s390_val;
1003
	guint32 mtu, num_opts, i;
Dan Williams's avatar
Dan Williams committed
1004
	const GPtrArray *s390_subchannels;
1005
	GString *str;
1006
	const GSList *macaddr_blacklist;
1007

1008
	s_wired = nm_connection_get_setting_wired (connection);
1009
	if (!s_wired) {
1010
		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
1011 1012 1013 1014
		             "Missing '%s' setting", NM_SETTING_WIRED_SETTING_NAME);
		return FALSE;
	}

1015
	svSetValue (ifcfg, "HWADDR", NULL, FALSE);
1016 1017
	device_mac = nm_setting_wired_get_mac_address (s_wired);
	if (device_mac) {
1018
		tmp = g_strdup_printf ("%02X:%02X:%02X:%02X:%02X:%02X",
1019 1020
		                       device_mac->data[0], device_mac->data[1], device_mac->data[2],
		                       device_mac->data[3], device_mac->data[4], device_mac->data[5]);
1021 1022 1023 1024
		svSetValue (ifcfg, "HWADDR", tmp, FALSE);
		g_free (tmp);
	}

1025
	svSetValue (ifcfg, "MACADDR", NULL, FALSE);
1026 1027 1028 1029 1030 1031 1032 1033 1034
	cloned_mac = nm_setting_wired_get_cloned_mac_address (s_wired);
	if (cloned_mac) {
		tmp = g_strdup_printf ("%02X:%02X:%02X:%02X:%02X:%02X",
		                       cloned_mac->data[0], cloned_mac->data[1], cloned_mac->data[2],
		                       cloned_mac->data[3], cloned_mac->data[4], cloned_mac->data[5]);
		svSetValue (ifcfg, "MACADDR", tmp, FALSE);
		g_free (tmp);
	}

1035
	svSetValue (ifcfg, "HWADDR_BLACKLIST", NULL, FALSE);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
	macaddr_blacklist = nm_setting_wired_get_mac_address_blacklist (s_wired);
	if (macaddr_blacklist) {
		const GSList *iter;
		GString *blacklist_str = g_string_new (NULL);

		for (iter = macaddr_blacklist; iter; iter = g_slist_next (iter)) {
			g_string_append (blacklist_str, iter->data);
			g_string_append_c (blacklist_str, ' ');

		}
		if (blacklist_str->len > 0)
			g_string_truncate (blacklist_str, blacklist_str->len - 1);
1048
		svSetValue (ifcfg, "HWADDR_BLACKLIST", blacklist_str->str, FALSE);
1049 1050 1051
		g_string_free (blacklist_str, TRUE);
	}

1052
	svSetValue (ifcfg, "MTU", NULL, FALSE);
1053 1054 1055 1056 1057 1058 1059
	mtu = nm_setting_wired_get_mtu (s_wired);
	if (mtu) {
		tmp = g_strdup_printf ("%u", mtu);
		svSetValue (ifcfg, "MTU", tmp, FALSE);
		g_free (tmp);
	}

1060
	svSetValue (ifcfg, "SUBCHANNELS", NULL, FALSE);
Dan Williams's avatar
Dan Williams committed
1061 1062
	s390_subchannels = nm_setting_wired_get_s390_subchannels (s_wired);
	if (s390_subchannels) {
1063
		tmp = NULL;
Dan Williams's avatar
Dan Williams committed
1064
	    if (s390_subchannels->len == 2) {
1065
			tmp = g_strdup_printf ("%s,%s",
Dan Williams's avatar
Dan Williams committed
1066 1067 1068
				                   (const char *) g_ptr_array_index (s390_subchannels, 0),
				                   (const char *) g_ptr_array_index (s390_subchannels, 1));
	    } else if (s390_subchannels->len == 3) {
1069
			tmp = g_strdup_printf ("%s,%s,%s",
Dan Williams's avatar
Dan Williams committed
1070 1071 1072
				                   (const char *) g_ptr_array_index (s390_subchannels, 0),
				                   (const char *) g_ptr_array_index (s390_subchannels, 1),
				                   (const char *) g_ptr_array_index (s390_subchannels, 2));
1073 1074 1075 1076 1077
		}
		svSetValue (ifcfg, "SUBCHANNELS", tmp, FALSE);
		g_free (tmp);
	}

1078 1079 1080 1081 1082 1083
	svSetValue (ifcfg, "NETTYPE", NULL, FALSE);
	nettype = nm_setting_wired_get_s390_nettype (s_wired);
	if (nettype)
		svSetValue (ifcfg, "NETTYPE", nettype, FALSE);

	svSetValue (ifcfg, "PORTNAME", NULL, FALSE);
1084
	portname = nm_setting_wired_get_s390_option_by_key (s_wired, "portname");
1085 1086 1087
	if (portname)
		svSetValue (ifcfg, "PORTNAME", portname, FALSE);

1088 1089 1090 1091 1092
	svSetValue (ifcfg, "CTCPROT", NULL, FALSE);
	ctcprot = nm_setting_wired_get_s390_option_by_key (s_wired, "ctcprot");
	if (ctcprot)
		svSetValue (ifcfg, "CTCPROT", ctcprot, FALSE);

1093
	svSetValue (ifcfg, "OPTIONS", NULL, FALSE);
1094 1095 1096 1097 1098 1099 1100
	num_opts = nm_setting_wired_get_num_s390_options (s_wired);
	if (s390_subchannels && num_opts) {
		str = g_string_sized_new (30);
		for (i = 0; i < num_opts; i++) {
			nm_setting_wired_get_s390_option (s_wired, i, &s390_key, &s390_val);

			/* portname is handled separately */
1101
			if (!strcmp (s390_key, "portname") || !strcmp (s390_key, "ctcprot"))
1102 1103 1104 1105 1106
				continue;

			if (str->len)
				g_string_append_c (str, ' ');
			g_string_append_printf (str, "%s=%s", s390_key, s390_val);
1107
		}
1108 1109
		if (str->len)
			svSetValue (ifcfg, "OPTIONS", str->str, FALSE);
1110 1111 1112
		g_string_free (str, TRUE);
	}

1113 1114 1115 1116
	svSetValue (ifcfg, "TYPE", TYPE_ETHERNET, FALSE);

	return TRUE;
}
1117

1118 1119
static GString *
vlan_priority_maplist_to_stringlist (NMSettingVlan *s_vlan, NMVlanPriorityMap map)
Weiping Pan's avatar
Weiping Pan committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
{
	GSList *strlist = NULL, *iter;
	GString *value = NULL;

	if (map == NM_VLAN_INGRESS_MAP)
		g_object_get (G_OBJECT (s_vlan), NM_SETTING_VLAN_INGRESS_PRIORITY_MAP, &strlist, NULL);
	else if (map == NM_VLAN_EGRESS_MAP)
		g_object_get (G_OBJECT (s_vlan), NM_SETTING_VLAN_EGRESS_PRIORITY_MAP, &strlist, NULL);
	else
		return NULL;

	value = g_string_new ("");
	for (iter = strlist; iter; iter = g_slist_next (iter))
		g_string_append_printf (value, "%s%s", value->len ? "," : "", (const char *) iter->data);

	nm_utils_slist_free (strlist, g_free);

	return value;
}

static gboolean
1141
write_vlan_setting (NMConnection *connection, shvarFile *ifcfg, gboolean *wired, GError **error)
Weiping Pan's avatar
Weiping Pan committed
1142 1143 1144
{
	NMSettingVlan *s_vlan;
	NMSettingConnection *s_con;
1145
	NMSettingWired *s_wired;
Dan Williams's avatar
Dan Williams committed
1146
	char *tmp;
Weiping Pan's avatar
Weiping Pan committed
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
	guint32 vlan_flags = 0;
	GString *text = NULL;

	s_con = nm_connection_get_setting_connection (connection);
	if (!s_con) {
		g_set_error_literal (error, IFCFG_PLUGIN_ERROR, 0, "Missing connection setting");
		return FALSE;
	}

	s_vlan = nm_connection_get_setting_vlan (connection);
	if (!s_vlan) {
		g_set_error_literal (error, IFCFG_PLUGIN_ERROR, 0, "Missing VLAN setting");
		return FALSE;
	}

	svSetValue (ifcfg, "VLAN", "yes", FALSE);
	svSetValue (ifcfg, "TYPE", TYPE_VLAN, FALSE);
	svSetValue (ifcfg, "DEVICE", nm_setting_vlan_get_interface_name (s_vlan), FALSE);
Dan Williams's avatar
Dan Williams committed
1165 1166 1167 1168 1169 1170
	svSetValue (ifcfg, "PHYSDEV", nm_setting_vlan_get_parent (s_vlan), FALSE);
	svSetValue (ifcfg, "MASTER", nm_setting_connection_get_master (s_con), FALSE);

	tmp = g_strdup_printf ("%d", nm_setting_vlan_get_id (s_vlan));
	svSetValue (ifcfg, "VLAN_ID", tmp, FALSE);
	g_free (tmp);
Weiping Pan's avatar
Weiping Pan committed
1171 1172 1173 1174 1175 1176 1177

	vlan_flags = nm_setting_vlan_get_flags (s_vlan);
	if (vlan_flags & NM_VLAN_FLAG_REORDER_HEADERS)
		svSetValue (ifcfg, "REORDER_HDR", "1", FALSE);
	else
		svSetValue (ifcfg, "REORDER_HDR", "0", FALSE);

Dan Williams's avatar
Dan Williams committed
1178
	svSetValue (ifcfg, "VLAN_FLAGS", NULL, FALSE);
Weiping Pan's avatar
Weiping Pan committed
1179 1180 1181 1182 1183
	if (vlan_flags & NM_VLAN_FLAG_GVRP) {
		if (vlan_flags & NM_VLAN_FLAG_LOOSE_BINDING)
			svSetValue (ifcfg, "VLAN_FLAGS", "GVRP,LOOSE_BINDING", FALSE);
		else
			svSetValue (ifcfg, "VLAN_FLAGS", "GVRP", FALSE);
Dan Williams's avatar
Dan Williams committed
1184 1185
	} else if (vlan_flags & NM_VLAN_FLAG_LOOSE_BINDING)
		svSetValue (ifcfg, "VLAN_FLAGS", "LOOSE_BINDING", FALSE);
Weiping Pan's avatar
Weiping Pan committed
1186 1187

	text = vlan_priority_maplist_to_stringlist (s_vlan, NM_VLAN_INGRESS_MAP);
Dan Williams's avatar
Dan Williams committed
1188 1189 1190
	svSetValue (ifcfg, "VLAN_INGRESS_PRIORITY_MAP", text ? text->str : NULL, FALSE);
	if (text)
		g_string_free (text, TRUE);
Weiping Pan's avatar
Weiping Pan committed
1191 1192

	text = vlan_priority_maplist_to_stringlist (s_vlan, NM_VLAN_EGRESS_MAP);
Dan Williams's avatar
Dan Williams committed
1193 1194 1195
	svSetValue (ifcfg, "VLAN_EGRESS_PRIORITY_MAP", text ? text->str : NULL, FALSE);
	if (text)
		g_string_free (text, TRUE);
Weiping Pan's avatar
Weiping Pan committed
1196

1197 1198 1199 1200 1201 1202 1203