connections.c 299 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* nmcli - command-line tool to control NetworkManager
 *
 * 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.
 *
17
 * Copyright 2010 - 2018 Red Hat, Inc.
18 19
 */

20
#include "nm-default.h"
21

22 23
#include "connections.h"

24 25 26
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
27
#include <unistd.h>
28 29 30
#include <errno.h>
#include <signal.h>
#include <netinet/ether.h>
31 32
#include <readline/readline.h>
#include <readline/history.h>
33
#include <fcntl.h>
34

35
#include "nm-client-utils.h"
36 37 38
#include "nm-vpn-helpers.h"
#include "nm-meta-setting-access.h"
#include "nm-secret-agent-simple.h"
39

40
#include "utils.h"
41
#include "common.h"
42
#include "settings.h"
43
#include "devices.h"
44
#include "polkit-agent.h"
45

46 47 48 49 50 51 52
typedef enum {
	PROPERTY_INF_FLAG_NONE                      = 0x0,
	PROPERTY_INF_FLAG_DISABLED                  = 0x1, /* Don't ask due to runtime decision. */
	PROPERTY_INF_FLAG_ENABLED                   = 0x2, /* Override NM_META_PROPERTY_INF_FLAG_DONT_ASK due to runtime decision. */
	PROPERTY_INF_FLAG_ALL                       = 0x3,
} PropertyInfFlags;

53 54
typedef char *(*CompEntryFunc) (const char *, int);

55 56
typedef struct _OptionInfo OptionInfo;
struct _OptionInfo {
57
	const NMMetaSettingInfoEditor *setting_info;
58 59
	const char *property;
	const char *option;
60
	gboolean (*check_and_set)(NmCli *nmc, NMConnection *connection, const OptionInfo *option, const char *value, GError **error);
61
	CompEntryFunc generator_func;
62 63
};

64 65 66 67 68
/* define some prompts for connection editor */
#define EDITOR_PROMPT_SETTING  _("Setting name? ")
#define EDITOR_PROMPT_PROPERTY _("Property name? ")
#define EDITOR_PROMPT_CON_TYPE _("Enter connection type: ")

69
/* define some other prompts */
70

71 72 73 74
#define PROMPT_CONNECTION  _("Connection (name, UUID, or path): ")
#define PROMPT_VPN_CONNECTION  _("VPN connection (name, UUID, or path): ")
#define PROMPT_CONNECTIONS _("Connection(s) (name, UUID, or path): ")
#define PROMPT_ACTIVE_CONNECTIONS _("Connection(s) (name, UUID, path or apath): ")
75

76 77
#define BASE_PROMPT "nmcli> "

78 79
/*****************************************************************************/

Thomas Haller's avatar
Thomas Haller committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
NM_UTILS_LOOKUP_STR_DEFINE_STATIC (active_connection_state_to_string, NMActiveConnectionState,
	NM_UTILS_LOOKUP_DEFAULT (N_("unknown")),
	NM_UTILS_LOOKUP_ITEM (NM_ACTIVE_CONNECTION_STATE_ACTIVATING,   N_("activating")),
	NM_UTILS_LOOKUP_ITEM (NM_ACTIVE_CONNECTION_STATE_ACTIVATED,    N_("activated")),
	NM_UTILS_LOOKUP_ITEM (NM_ACTIVE_CONNECTION_STATE_DEACTIVATING, N_("deactivating")),
	NM_UTILS_LOOKUP_ITEM (NM_ACTIVE_CONNECTION_STATE_DEACTIVATED,  N_("deactivated")),
	NM_UTILS_LOOKUP_ITEM_IGNORE (NM_ACTIVE_CONNECTION_STATE_UNKNOWN),
)

NM_UTILS_LOOKUP_STR_DEFINE_STATIC (vpn_connection_state_to_string, NMVpnConnectionState,
	NM_UTILS_LOOKUP_DEFAULT (N_("unknown")),
	NM_UTILS_LOOKUP_ITEM (NM_VPN_CONNECTION_STATE_PREPARE,       N_("VPN connecting (prepare)")),
	NM_UTILS_LOOKUP_ITEM (NM_VPN_CONNECTION_STATE_NEED_AUTH,     N_("VPN connecting (need authentication)")),
	NM_UTILS_LOOKUP_ITEM (NM_VPN_CONNECTION_STATE_CONNECT,       N_("VPN connecting")),
	NM_UTILS_LOOKUP_ITEM (NM_VPN_CONNECTION_STATE_IP_CONFIG_GET, N_("VPN connecting (getting IP configuration)")),
	NM_UTILS_LOOKUP_ITEM (NM_VPN_CONNECTION_STATE_ACTIVATED,     N_("VPN connected")),
	NM_UTILS_LOOKUP_ITEM (NM_VPN_CONNECTION_STATE_FAILED,        N_("VPN connection failed")),
	NM_UTILS_LOOKUP_ITEM (NM_VPN_CONNECTION_STATE_DISCONNECTED,  N_("VPN disconnected")),
	NM_UTILS_LOOKUP_ITEM_IGNORE (NM_VPN_CONNECTION_STATE_UNKNOWN),
)

/* Essentially a version of nm_setting_connection_get_connection_type() that
 * prefers an alias instead of the settings name when in pretty print mode.
 * That is so that we print "wifi" instead of "802-11-wireless" in "nmcli c". */
static const char *
105
connection_type_to_display (const char *type, NMMetaAccessorGetType get_type)
Thomas Haller's avatar
Thomas Haller committed
106 107 108 109
{
	const NMMetaSettingInfoEditor *editor;
	int i;

110 111 112 113 114 115
	nm_assert (NM_IN_SET (get_type, NM_META_ACCESSOR_GET_TYPE_PRETTY, NM_META_ACCESSOR_GET_TYPE_PARSABLE));

	if (!type)
		return NULL;

	if (get_type != NM_META_ACCESSOR_GET_TYPE_PRETTY)
Thomas Haller's avatar
Thomas Haller committed
116 117 118 119
		return type;

	for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++) {
		editor = &nm_meta_setting_infos_editor[i];
120 121
		if (nm_streq (type, editor->general->setting_name))
			return editor->alias ?: type;
Thomas Haller's avatar
Thomas Haller committed
122 123 124 125
	}
	return type;
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
static int
active_connection_get_state_ord (NMActiveConnection *active)
{
	/* returns an integer related to @active's state, that can be used for sorting
	 * active connections based on their activation state. */
	if (!active)
		return -2;

	switch (nm_active_connection_get_state (active)) {
	case NM_ACTIVE_CONNECTION_STATE_UNKNOWN:      return 0;
	case NM_ACTIVE_CONNECTION_STATE_DEACTIVATED:  return 1;
	case NM_ACTIVE_CONNECTION_STATE_DEACTIVATING: return 2;
	case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:   return 3;
	case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:    return 4;
	}
	return -1;
}

static int
active_connection_cmp (NMActiveConnection *ac_a, NMActiveConnection *ac_b)
{
	NM_CMP_SELF (ac_a, ac_b);
	NM_CMP_DIRECT (active_connection_get_state_ord (ac_b),
	               active_connection_get_state_ord (ac_a));
	NM_CMP_DIRECT_STRCMP0 (nm_active_connection_get_id (ac_a), nm_active_connection_get_id (ac_b));
	NM_CMP_DIRECT_STRCMP0 (nm_active_connection_get_connection_type (ac_a), nm_active_connection_get_connection_type (ac_b));
	NM_CMP_DIRECT_STRCMP0 (nm_object_get_path (NM_OBJECT (ac_a)), nm_object_get_path (NM_OBJECT (ac_b)));
	return 0;
}

Thomas Haller's avatar
Thomas Haller committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
static char *
get_ac_device_string (NMActiveConnection *active)
{
	GString *dev_str;
	const GPtrArray *devices;
	int i;

	if (!active)
		return NULL;

	/* Get devices of the active connection */
	dev_str = g_string_new (NULL);
	devices = nm_active_connection_get_devices (active);
	for (i = 0; i < devices->len; i++) {
		NMDevice *device = g_ptr_array_index (devices, i);
		const char *dev_iface = nm_device_get_iface (device);

		if (dev_iface) {
			g_string_append (dev_str, dev_iface);
			g_string_append_c (dev_str, ',');
		}
	}
	if (dev_str->len > 0)
		g_string_truncate (dev_str, dev_str->len - 1);  /* Cut off last ',' */

	return g_string_free (dev_str, FALSE);
}

/*****************************************************************************/

Thomas Haller's avatar
Thomas Haller committed
186 187 188
/* FIXME: The same or similar code for VPN info appears also in nm-applet (applet-dialogs.c),
 * and in gnome-control-center as well. It could probably be shared somehow. */

189
static const char *
Thomas Haller's avatar
Thomas Haller committed
190 191
get_vpn_connection_type (NMConnection *connection)
{
192
	NMSettingVpn *s_vpn;
Thomas Haller's avatar
Thomas Haller committed
193 194
	const char *type, *p;

195 196 197 198
	s_vpn = nm_connection_get_setting_vpn (connection);
	if (!s_vpn)
		return NULL;

Thomas Haller's avatar
Thomas Haller committed
199 200 201 202
	/* The service type is in form of "org.freedesktop.NetworkManager.vpnc".
	 * Extract end part after last dot, e.g. "vpnc"
	 */
	type = nm_setting_vpn_get_service_type (nm_connection_get_setting_vpn (connection));
203 204
	if (!type)
		return NULL;
Thomas Haller's avatar
Thomas Haller committed
205
	p = strrchr (type, '.');
206
	return p ? p + 1 : type;
Thomas Haller's avatar
Thomas Haller committed
207 208 209 210 211 212 213 214 215 216
}

/* VPN parameters can be found at:
 * http://git.gnome.org/browse/network-manager-openvpn/tree/src/nm-openvpn-service.h
 * http://git.gnome.org/browse/network-manager-vpnc/tree/src/nm-vpnc-service.h
 * http://git.gnome.org/browse/network-manager-pptp/tree/src/nm-pptp-service.h
 * http://git.gnome.org/browse/network-manager-openconnect/tree/src/nm-openconnect-service.h
 * http://git.gnome.org/browse/network-manager-openswan/tree/src/nm-openswan-service.h
 * See also 'properties' directory in these plugins.
 */
217
static const char *
Thomas Haller's avatar
Thomas Haller committed
218 219
find_vpn_gateway_key (const char *vpn_type)
{
220 221 222 223 224 225 226 227 228 229 230
	if (vpn_type) {
		if (nm_streq (vpn_type, "openvpn"))     return "remote";
		if (nm_streq (vpn_type, "vpnc"))        return "IPSec gateway";
		if (nm_streq (vpn_type, "pptp"))        return "gateway";
		if (nm_streq (vpn_type, "openconnect")) return "gateway";
		if (nm_streq (vpn_type, "openswan"))    return "right";
		if (nm_streq (vpn_type, "libreswan"))   return "right";
		if (nm_streq (vpn_type, "ssh"))         return "remote";
		if (nm_streq (vpn_type, "l2tp"))        return "gateway";
	}
	return NULL;
Thomas Haller's avatar
Thomas Haller committed
231 232
}

233
static const char *
Thomas Haller's avatar
Thomas Haller committed
234 235
find_vpn_username_key (const char *vpn_type)
{
236 237 238 239 240 241 242 243 244 245
	if (vpn_type) {
		if (nm_streq (vpn_type, "openvpn"))     return "username";
		if (nm_streq (vpn_type, "vpnc"))        return "Xauth username";
		if (nm_streq (vpn_type, "pptp"))        return "user";
		if (nm_streq (vpn_type, "openconnect")) return "username";
		if (nm_streq (vpn_type, "openswan"))    return "leftxauthusername";
		if (nm_streq (vpn_type, "libreswan"))   return "leftxauthusername";
		if (nm_streq (vpn_type, "l2tp"))        return "user";
	}
	return NULL;
Thomas Haller's avatar
Thomas Haller committed
246 247 248 249 250 251 252
}

enum VpnDataItem {
	VPN_DATA_ITEM_GATEWAY,
	VPN_DATA_ITEM_USERNAME
};

253
static const char *
Thomas Haller's avatar
Thomas Haller committed
254 255
get_vpn_data_item (NMConnection *connection, enum VpnDataItem vpn_data_item)
{
256 257
	const char *type;
	const char *key = NULL;
Thomas Haller's avatar
Thomas Haller committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271

	type = get_vpn_connection_type (connection);

	switch (vpn_data_item) {
	case VPN_DATA_ITEM_GATEWAY:
		key = find_vpn_gateway_key (type);
		break;
	case VPN_DATA_ITEM_USERNAME:
		key = find_vpn_username_key (type);
		break;
	default:
		break;
	}

272 273
	if (!key)
		return NULL;
Thomas Haller's avatar
Thomas Haller committed
274 275 276 277 278
	return nm_setting_vpn_get_data_item (nm_connection_get_setting_vpn (connection), key);
}

/*****************************************************************************/

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
typedef struct {
	NMConnection *connection;
	NMActiveConnection *primary_active;
	GPtrArray *all_active;
	bool show_active_fields;
} MetagenConShowRowData;

static MetagenConShowRowData *
_metagen_con_show_row_data_new_for_connection (NMRemoteConnection *connection, gboolean show_active_fields)
{
	MetagenConShowRowData *row_data;

	row_data = g_slice_new0 (MetagenConShowRowData);
	row_data->connection = g_object_ref (NM_CONNECTION (connection));
	row_data->show_active_fields = show_active_fields;
	return row_data;
}

static MetagenConShowRowData *
_metagen_con_show_row_data_new_for_active_connection (NMRemoteConnection *connection, NMActiveConnection *active, gboolean show_active_fields)
{
	MetagenConShowRowData *row_data;

	row_data = g_slice_new0 (MetagenConShowRowData);
	if (connection)
		row_data->connection = g_object_ref (NM_CONNECTION (connection));
	row_data->primary_active = g_object_ref (active);
	row_data->show_active_fields = show_active_fields;
	return row_data;
}

static void
_metagen_con_show_row_data_add_active_connection (MetagenConShowRowData *row_data, NMActiveConnection *active)
{
	if (!row_data->primary_active) {
		row_data->primary_active = g_object_ref (active);
		return;
	}
	if (!row_data->all_active) {
		row_data->all_active = g_ptr_array_new_with_free_func (g_object_unref);
		g_ptr_array_add (row_data->all_active, g_object_ref (row_data->primary_active));
	}
	g_ptr_array_add (row_data->all_active, g_object_ref (active));
}

static void
_metagen_con_show_row_data_init_primary_active (MetagenConShowRowData *row_data)
{
	NMActiveConnection *ac, *best_ac;
	guint i;

	if (!row_data->all_active)
		return;

	best_ac = row_data->all_active->pdata[0];
	for (i = 1; i < row_data->all_active->len; i++) {
		ac = row_data->all_active->pdata[i];

		if (active_connection_get_state_ord (ac) > active_connection_get_state_ord (best_ac))
			best_ac = ac;
	}

	if (row_data->primary_active != best_ac) {
		g_object_unref (row_data->primary_active);
		row_data->primary_active = g_object_ref (best_ac);
	}
	g_clear_pointer (&row_data->all_active, g_ptr_array_unref);
}

static void
_metagen_con_show_row_data_destroy (gpointer data)
{
	MetagenConShowRowData *row_data = data;

	if (!row_data)
		return;

	g_clear_object (&row_data->connection);
	g_clear_object (&row_data->primary_active);
	g_clear_pointer (&row_data->all_active, g_ptr_array_unref);
	g_slice_free (MetagenConShowRowData, row_data);
}

static const char *
_con_show_fcn_get_id (NMConnection *c, NMActiveConnection *ac)
{
	NMSettingConnection *s_con = NULL;
	const char *s;

	if (c)
		s_con = nm_connection_get_setting_connection (c);

	s = s_con ? nm_setting_connection_get_id (s_con) : NULL;
	if (!s && ac) {
		/* note that if we have no s_con, that usually means that the user has no permissions
		 * to see the connection. We still fall to get the ID from the active-connection,
		 * which exposes it despite the user having no permissions.
		 *
		 * That might be unexpected, because the user is shown an ID, which he later
		 * is unable to resolve in other operations. */
		s = nm_active_connection_get_id (ac);
	}
	return s;
}

static const char *
_con_show_fcn_get_type (NMConnection *c, NMActiveConnection *ac, NMMetaAccessorGetType get_type)
{
	NMSettingConnection *s_con = NULL;
	const char *s;

	if (c)
		s_con = nm_connection_get_setting_connection (c);

	s = s_con ? nm_setting_connection_get_connection_type (s_con) : NULL;
	if (!s && ac) {
		/* see _con_show_fcn_get_id() for why we fallback to get the value
		 * from @ac. */
		s = nm_active_connection_get_connection_type (ac);
	}
	return connection_type_to_display (s, get_type);
}

static gconstpointer
_metagen_con_show_get_fcn (NMC_META_GENERIC_INFO_GET_FCN_ARGS)
{
	const MetagenConShowRowData *row_data = target;
	NMConnection *c = row_data->connection;
	NMActiveConnection *ac = row_data->primary_active;
	NMSettingConnection *s_con = NULL;
	const char *s;
	char *s_mut;

	NMC_HANDLE_COLOR (  ac
413
	                  ? nmc_active_connection_state_to_color (nm_active_connection_get_state (ac))
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
	                  : NM_META_COLOR_CONNECTION_UNKNOWN);

	if (c)
		s_con = nm_connection_get_setting_connection (c);

	if (!row_data->show_active_fields) {
		/* we are not supposed to show any fields of the active connection.
		 * We only tracked the primary_active to get the coloring right.
		 * From now on, there is no active connection. */
		ac = NULL;

		/* in this mode, we expect that we are called only with connections that
		 * have a [connection] setting and a UUID. Otherwise, the connection is
		 * effectively invisible to the user, and should be hidden.
		 *
		 * But in that case, we expect that the caller pre-filtered this row out.
		 * So assert(). */
		nm_assert (s_con);
		nm_assert (nm_setting_connection_get_uuid (s_con));
	}

	nm_assert (NM_IN_SET (get_type, NM_META_ACCESSOR_GET_TYPE_PRETTY, NM_META_ACCESSOR_GET_TYPE_PARSABLE));

	switch (info->info_type) {
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_NAME:
		return _con_show_fcn_get_id (c, ac);
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_UUID:
		s = s_con ? nm_setting_connection_get_uuid (s_con) : NULL;
		if (!s && ac) {
			/* see _con_show_fcn_get_id() for why we fallback to get the value
			 * from @ac. */
			s = nm_active_connection_get_uuid (ac);
		}
		return s;
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_TYPE:
		return _con_show_fcn_get_type (c, ac, get_type);
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_TIMESTAMP:
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_TIMESTAMP_REAL:
		if (!s_con)
			return NULL;
		{
			guint64 timestamp;
			time_t timestamp_real;

			timestamp = nm_setting_connection_get_timestamp (s_con);

			if (info->info_type == NMC_GENERIC_INFO_TYPE_CON_SHOW_TIMESTAMP)
				return (*out_to_free = g_strdup_printf ("%" G_GUINT64_FORMAT, timestamp));
			else {
				if (!timestamp) {
					if (get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY)
						return _("never");
					return "never";
				}
				timestamp_real = timestamp;
				s_mut = g_malloc0 (128);
				strftime (s_mut, 64, "%c", localtime (&timestamp_real));
				return (*out_to_free = s_mut);
			}
		}
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_AUTOCONNECT:
		if (!s_con)
			return NULL;
		return nmc_meta_generic_get_bool (nm_setting_connection_get_autoconnect (s_con), get_type);
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_AUTOCONNECT_PRIORITY:
		if (!s_con)
			return NULL;
		return (*out_to_free = g_strdup_printf ("%d", nm_setting_connection_get_autoconnect_priority (s_con)));
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_READONLY:
		if (!s_con)
			return NULL;
		return nmc_meta_generic_get_bool (nm_setting_connection_get_read_only (s_con), get_type);
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_DBUS_PATH:
		if (!c)
			return NULL;
		return nm_connection_get_path (c);
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_ACTIVE:
		return nmc_meta_generic_get_bool (!!ac, get_type);
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_DEVICE:
		if (ac)
			return (*out_to_free = get_ac_device_string (ac));
		return NULL;
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_STATE:
		return nmc_meta_generic_get_str_i18n (ac
		                                        ? active_connection_state_to_string (nm_active_connection_get_state (ac))
		                                        : NULL,
		                                      get_type);
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_ACTIVE_PATH:
		if (ac)
			return nm_object_get_path (NM_OBJECT (ac));
		return NULL;
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_SLAVE:
		if (!s_con)
			return NULL;
		return nm_setting_connection_get_slave_type (s_con);
509 510 511 512
	case NMC_GENERIC_INFO_TYPE_CON_SHOW_FILENAME:
		if (!NM_IS_REMOTE_CONNECTION (c))
			return NULL;
		return nm_remote_connection_get_filename (NM_REMOTE_CONNECTION (c));
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	default:
		break;
	}

	g_return_val_if_reached (NULL);
}

const NmcMetaGenericInfo *const metagen_con_show[_NMC_GENERIC_INFO_TYPE_CON_SHOW_NUM + 1] = {
#define _METAGEN_CON_SHOW(type, name) \
	[type] = NMC_META_GENERIC(name, .info_type = type, .get_fcn = _metagen_con_show_get_fcn)
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_NAME,                 "NAME"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_UUID,                 "UUID"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_TYPE,                 "TYPE"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_TIMESTAMP,            "TIMESTAMP"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_TIMESTAMP_REAL,       "TIMESTAMP-REAL"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_AUTOCONNECT,          "AUTOCONNECT"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_AUTOCONNECT_PRIORITY, "AUTOCONNECT-PRIORITY"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_READONLY,             "READONLY"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_DBUS_PATH,            "DBUS-PATH"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_ACTIVE,               "ACTIVE"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_DEVICE,               "DEVICE"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_STATE,                "STATE"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_ACTIVE_PATH,          "ACTIVE-PATH"),
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_SLAVE,                "SLAVE"),
537
	_METAGEN_CON_SHOW (NMC_GENERIC_INFO_TYPE_CON_SHOW_FILENAME,             "FILENAME"),
538
};
539
#define NMC_FIELDS_CON_SHOW_COMMON  "NAME,UUID,TYPE,DEVICE"
540

541 542
/*****************************************************************************/

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
static gconstpointer
_metagen_con_active_general_get_fcn (NMC_META_GENERIC_INFO_GET_FCN_ARGS)
{
	NMActiveConnection *ac = target;
	NMConnection *c;
	NMSettingConnection *s_con = NULL;
	NMDevice *dev;
	guint i;
	const char *s;

	NMC_HANDLE_COLOR (NM_META_COLOR_NONE);

	nm_assert (NM_IN_SET (get_type, NM_META_ACCESSOR_GET_TYPE_PRETTY, NM_META_ACCESSOR_GET_TYPE_PARSABLE));

	c = NM_CONNECTION (nm_active_connection_get_connection (ac));
	if (c)
		s_con = nm_connection_get_setting_connection (c);

	switch (info->info_type) {
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_NAME:
		return nm_active_connection_get_id (ac);
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_UUID:
		return nm_active_connection_get_uuid (ac);
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_DEVICES:
		{
			GString *str = NULL;
			const GPtrArray *devices;

			s = NULL;
			devices = nm_active_connection_get_devices (ac);
			if (devices) {
				for (i = 0; i < devices->len; i++) {
					NMDevice *device = devices->pdata[i];
					const char *iface;

					iface = nm_device_get_iface (device);
					if (!iface)
						continue;
					if (!s) {
						s = iface;
						continue;
					}
					if (!str)
						str = g_string_new (s);
					g_string_append_c (str, ',');
					g_string_append (str, iface);
				}
			}
			if (str)
				return (*out_to_free = g_string_free (str, FALSE));
			return s;
		}
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_STATE:
		return nmc_meta_generic_get_str_i18n (active_connection_state_to_string (nm_active_connection_get_state (ac)),
		                                      get_type);
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_DEFAULT:
		return nmc_meta_generic_get_bool (nm_active_connection_get_default (ac), get_type);
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_DEFAULT6:
		return nmc_meta_generic_get_bool (nm_active_connection_get_default6 (ac), get_type);
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_SPEC_OBJECT:
		return nm_active_connection_get_specific_object_path (ac);
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_VPN:
		return nmc_meta_generic_get_bool (NM_IS_VPN_CONNECTION (ac), get_type);
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_DBUS_PATH:
		return nm_object_get_path (NM_OBJECT (ac));
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_CON_PATH:
		return c ? nm_connection_get_path (c) : NULL;
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_ZONE:
		/* this is really ugly, because the zone is not a property of the active-connection,
		 * but the settings-connection profile. There is no guarantee, that they agree. */
		return s_con ? nm_setting_connection_get_zone (s_con) : NULL;
	case NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_MASTER_PATH:
		dev = nm_active_connection_get_master (ac);
		return dev ? nm_object_get_path (NM_OBJECT (dev)) : NULL;
	default:
		break;
	}

	g_return_val_if_reached (NULL);
}

const NmcMetaGenericInfo *const metagen_con_active_general[_NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_NUM + 1] = {
#define _METAGEN_CON_ACTIVE_GENERAL(type, name) \
	[type] = NMC_META_GENERIC(name, .info_type = type, .get_fcn = _metagen_con_active_general_get_fcn)
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_NAME,        "NAME"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_UUID,        "UUID"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_DEVICES,     "DEVICES"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_STATE,       "STATE"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_DEFAULT,     "DEFAULT"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_DEFAULT6,    "DEFAULT6"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_SPEC_OBJECT, "SPEC-OBJECT"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_VPN,         "VPN"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_DBUS_PATH,   "DBUS-PATH"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_CON_PATH,    "CON-PATH"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_ZONE,        "ZONE"),
	_METAGEN_CON_ACTIVE_GENERAL (NMC_GENERIC_INFO_TYPE_CON_ACTIVE_GENERAL_MASTER_PATH, "MASTER-PATH"),
639
};
640 641 642

/*****************************************************************************/

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
static gconstpointer
_metagen_con_active_vpn_get_fcn (NMC_META_GENERIC_INFO_GET_FCN_ARGS)
{
	NMActiveConnection *ac = target;
	NMConnection *c;
	NMSettingVpn *s_vpn = NULL;
	NMVpnConnectionState vpn_state;
	guint i;
	const char *s;
	char **arr = NULL;

	nm_assert (NM_IS_VPN_CONNECTION (ac));

	NMC_HANDLE_COLOR (NM_META_COLOR_NONE);

	nm_assert (NM_IN_SET (get_type, NM_META_ACCESSOR_GET_TYPE_PRETTY, NM_META_ACCESSOR_GET_TYPE_PARSABLE));

	c = NM_CONNECTION (nm_active_connection_get_connection (ac));
	if (c)
		s_vpn = nm_connection_get_setting_vpn (c);

	switch (info->info_type) {
	case NMC_GENERIC_INFO_TYPE_CON_VPN_TYPE:
		return c ? get_vpn_connection_type (c) : NULL;
	case NMC_GENERIC_INFO_TYPE_CON_VPN_USERNAME:
		if (   s_vpn
		    && (s = nm_setting_vpn_get_user_name (s_vpn)))
			return s;
		return c ? get_vpn_data_item (c, VPN_DATA_ITEM_USERNAME) : NULL;
	case NMC_GENERIC_INFO_TYPE_CON_VPN_GATEWAY:
		return c ? get_vpn_data_item (c, VPN_DATA_ITEM_GATEWAY) : NULL;
	case NMC_GENERIC_INFO_TYPE_CON_VPN_BANNER:
		s = nm_vpn_connection_get_banner (NM_VPN_CONNECTION (ac));
		if (s)
			return (*out_to_free = g_strescape (s, ""));
		return NULL;
	case NMC_GENERIC_INFO_TYPE_CON_VPN_VPN_STATE:
		vpn_state = nm_vpn_connection_get_vpn_state (NM_VPN_CONNECTION (ac));
		return (*out_to_free = nmc_meta_generic_get_enum_with_detail (NMC_META_GENERIC_GET_ENUM_TYPE_DASH,
		                                                              vpn_state,
		                                                              vpn_connection_state_to_string (vpn_state),
		                                                              get_type));
	case NMC_GENERIC_INFO_TYPE_CON_VPN_CFG:
		if (!NM_FLAGS_HAS (get_flags, NM_META_ACCESSOR_GET_FLAGS_ACCEPT_STRV))
			return NULL;
		if (s_vpn) {
			gs_free char **arr2 = NULL;
			guint n;

			arr2 = (char **) nm_setting_vpn_get_data_keys (s_vpn, &n);
			if (!n)
				goto arr_out;

			nm_assert (arr2 && !arr2[n]);
			for (i = 0; i < n; i++) {
				const char *k = arr2[i];
				const char *v;

				nm_assert (k);
				v = nm_setting_vpn_get_data_item (s_vpn, k);
				/* update the arr array in-place. Previously it contained
				 * the constant keys, now it contains the strdup'ed output text. */
				arr2[i] = g_strdup_printf ("%s = %s", k, v);
			}

			arr = g_steal_pointer (&arr2);
		}
		goto arr_out;
	default:
		break;
	}

	g_return_val_if_reached (NULL);

arr_out:
	NM_SET_OUT (out_is_default, !arr || !arr[0]);
	*out_flags |= NM_META_ACCESSOR_GET_OUT_FLAGS_STRV;
	*out_to_free = arr;
	return arr;
}

const NmcMetaGenericInfo *const metagen_con_active_vpn[_NMC_GENERIC_INFO_TYPE_CON_ACTIVE_VPN_NUM + 1] = {
#define _METAGEN_CON_ACTIVE_VPN(type, name) \
	[type] = NMC_META_GENERIC(name, .info_type = type, .get_fcn = _metagen_con_active_vpn_get_fcn)
	_METAGEN_CON_ACTIVE_VPN (NMC_GENERIC_INFO_TYPE_CON_VPN_TYPE,      "TYPE"),
	_METAGEN_CON_ACTIVE_VPN (NMC_GENERIC_INFO_TYPE_CON_VPN_USERNAME,  "USERNAME"),
	_METAGEN_CON_ACTIVE_VPN (NMC_GENERIC_INFO_TYPE_CON_VPN_GATEWAY,   "GATEWAY"),
	_METAGEN_CON_ACTIVE_VPN (NMC_GENERIC_INFO_TYPE_CON_VPN_BANNER,    "BANNER"),
	_METAGEN_CON_ACTIVE_VPN (NMC_GENERIC_INFO_TYPE_CON_VPN_VPN_STATE, "VPN-STATE"),
	_METAGEN_CON_ACTIVE_VPN (NMC_GENERIC_INFO_TYPE_CON_VPN_CFG,       "CFG"),
};

/*****************************************************************************/

737
#define NMC_FIELDS_SETTINGS_NAMES_ALL    NM_SETTING_CONNECTION_SETTING_NAME","\
738
                                         NM_SETTING_MATCH_SETTING_NAME","\
739 740 741 742 743 744 745 746 747
                                         NM_SETTING_WIRED_SETTING_NAME","\
                                         NM_SETTING_802_1X_SETTING_NAME","\
                                         NM_SETTING_WIRELESS_SETTING_NAME","\
                                         NM_SETTING_WIRELESS_SECURITY_SETTING_NAME","\
                                         NM_SETTING_IP4_CONFIG_SETTING_NAME","\
                                         NM_SETTING_IP6_CONFIG_SETTING_NAME","\
                                         NM_SETTING_SERIAL_SETTING_NAME","\
                                         NM_SETTING_PPP_SETTING_NAME","\
                                         NM_SETTING_PPPOE_SETTING_NAME","\
748
                                         NM_SETTING_ADSL_SETTING_NAME","\
749 750 751 752
                                         NM_SETTING_GSM_SETTING_NAME","\
                                         NM_SETTING_CDMA_SETTING_NAME","\
                                         NM_SETTING_BLUETOOTH_SETTING_NAME","\
                                         NM_SETTING_OLPC_MESH_SETTING_NAME","\
753
                                         NM_SETTING_VPN_SETTING_NAME","\
Jiří Klimeš's avatar
Jiří Klimeš committed
754
                                         NM_SETTING_INFINIBAND_SETTING_NAME","\
Jiří Klimeš's avatar
Jiří Klimeš committed
755
                                         NM_SETTING_BOND_SETTING_NAME","\
Thomas Graf's avatar
Thomas Graf committed
756 757
                                         NM_SETTING_VLAN_SETTING_NAME","\
                                         NM_SETTING_BRIDGE_SETTING_NAME","\
758 759
                                         NM_SETTING_BRIDGE_PORT_SETTING_NAME","\
                                         NM_SETTING_TEAM_SETTING_NAME","\
760
                                         NM_SETTING_TEAM_PORT_SETTING_NAME"," \
761
                                         NM_SETTING_OVS_BRIDGE_SETTING_NAME","\
762
                                         NM_SETTING_OVS_INTERFACE_SETTING_NAME","\
763
                                         NM_SETTING_OVS_PATCH_SETTING_NAME","\
764
                                         NM_SETTING_OVS_PORT_SETTING_NAME","\
765
                                         NM_SETTING_DCB_SETTING_NAME"," \
766
                                         NM_SETTING_TUN_SETTING_NAME"," \
767
                                         NM_SETTING_IP_TUNNEL_SETTING_NAME"," \
Beniamino Galvani's avatar
Beniamino Galvani committed
768
                                         NM_SETTING_MACSEC_SETTING_NAME"," \
769
                                         NM_SETTING_MACVLAN_SETTING_NAME"," \
770
                                         NM_SETTING_VXLAN_SETTING_NAME"," \
771
                                         NM_SETTING_WPAN_SETTING_NAME","\
772
                                         NM_SETTING_6LOWPAN_SETTING_NAME","\
773
                                         NM_SETTING_PROXY_SETTING_NAME"," \
774
                                         NM_SETTING_TC_CONFIG_SETTING_NAME"," \
775 776
                                         NM_SETTING_SRIOV_SETTING_NAME"," \
                                         NM_SETTING_ETHTOOL_SETTING_NAME
777 778
                                         // NM_SETTING_DUMMY_SETTING_NAME
                                         // NM_SETTING_WIMAX_SETTING_NAME
779

780
const NmcMetaGenericInfo *const nmc_fields_con_active_details_groups[] = {
781
	NMC_META_GENERIC_WITH_NESTED ("GENERAL", metagen_con_active_general),                /* 0 */
782
	NMC_META_GENERIC_WITH_NESTED ("IP4",     metagen_ip4_config),                        /* 1 */
783
	NMC_META_GENERIC_WITH_NESTED ("DHCP4",   metagen_dhcp_config),                       /* 2 */
784
	NMC_META_GENERIC_WITH_NESTED ("IP6",     metagen_ip6_config),                        /* 3 */
785
	NMC_META_GENERIC_WITH_NESTED ("DHCP6",   metagen_dhcp_config),                       /* 4 */
786
	NMC_META_GENERIC_WITH_NESTED ("VPN",     metagen_con_active_vpn),                    /* 5 */
787
	NULL,
788 789
};

790 791 792 793 794
/* Pseudo group names for 'connection show <con>' */
/* e.g.: nmcli -f profile con show my-eth0 */
/* e.g.: nmcli -f active con show my-eth0 */
#define CON_SHOW_DETAIL_GROUP_PROFILE "profile"
#define CON_SHOW_DETAIL_GROUP_ACTIVE  "active"
795

796
static guint progress_id = 0;  /* ID of event source for displaying progress */
797

798
/* for readline TAB completion in editor */
799 800
typedef struct {
	NmCli *nmc;
801
	char *con_type;
802 803
	NMConnection *connection;
	NMSetting *setting;
804
	const char *property;
805
	char **words;
806
} TabCompletionInfo;
807 808 809 810

static TabCompletionInfo nmc_tab_completion;

/*****************************************************************************/
811

812 813 814
static void
usage (void)
{
815
	g_printerr (_("Usage: nmcli connection { COMMAND | help }\n\n"
816
	              "COMMAND := { show | up | down | add | modify | clone | edit | delete | monitor | reload | load | import | export }\n\n"
817
	              "  show [--active] [--order <order spec>]\n"
818
	              "  show [--active] [id | uuid | path | apath] <ID> ...\n\n"
819
	              "  up [[id | uuid | path] <ID>] [ifname <ifname>] [ap <BSSID>] [passwd-file <file with passwords>]\n\n"
820
	              "  down [id | uuid | path | apath] <ID> ...\n\n"
821
	              "  add COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS SLAVE_OPTIONS IP_OPTIONS [-- ([+|-]<setting>.<property> <value>)+]\n\n"
822
	              "  modify [--temporary] [id | uuid | path] <ID> ([+|-]<setting>.<property> <value>)+\n\n"
823
	              "  clone [--temporary] [id | uuid | path ] <ID> <new name>\n\n"
824 825 826
	              "  edit [id | uuid | path] <ID>\n"
	              "  edit [type <new_con_type>] [con-name <new_con_name>]\n\n"
	              "  delete [id | uuid | path] <ID>\n\n"
827
	              "  monitor [id | uuid | path] <ID> ...\n\n"
828
	              "  reload\n\n"
829
	              "  load <filename> [ <filename>... ]\n\n"
830 831
	              "  import [--temporary] type <type> file <file to import>\n\n"
	              "  export [id | uuid | path] <ID> [<output file>]\n\n"));
832 833 834 835 836
}

static void
usage_connection_show (void)
{
837 838
	g_printerr (_("Usage: nmcli connection show { ARGUMENTS | help }\n"
	              "\n"
839
	              "ARGUMENTS := [--active] [--order <order spec>]\n"
840 841 842 843
	              "\n"
	              "List in-memory and on-disk connection profiles, some of which may also be\n"
	              "active if a device is using that connection profile. Without a parameter, all\n"
	              "profiles are listed. When --active option is specified, only the active\n"
844
	              "profiles are shown. --order allows custom connection ordering (see manual page).\n"
845
	              "\n"
846
	              "ARGUMENTS := [--active] [id | uuid | path | apath] <ID> ...\n"
847 848 849 850 851
	              "\n"
	              "Show details for specified connections. By default, both static configuration\n"
	              "and active connection data are displayed. It is possible to filter the output\n"
	              "using global '--fields' option. Refer to the manual page for more information.\n"
	              "When --active option is specified, only the active profiles are taken into\n"
852
	              "account. Use global --show-secrets option to reveal associated secrets as well.\n"));
853 854 855 856 857
}

static void
usage_connection_up (void)
{
858 859
	g_printerr (_("Usage: nmcli connection up { ARGUMENTS | help }\n"
	              "\n"
860
	              "ARGUMENTS := [id | uuid | path] <ID> [ifname <ifname>] [ap <BSSID>] [nsp <name>] [passwd-file <file with passwords>]\n"
861 862 863 864
	              "\n"
	              "Activate a connection on a device. The profile to activate is identified by its\n"
	              "name, UUID or D-Bus path.\n"
	              "\n"
865
	              "ARGUMENTS := ifname <ifname> [ap <BSSID>] [nsp <name>] [passwd-file <file with passwords>]\n"
866 867 868 869
	              "\n"
	              "Activate a device with a connection. The connection profile is selected\n"
	              "automatically by NetworkManager.\n"
	              "\n"
870 871 872 873
	              "ifname      - specifies the device to active the connection on\n"
	              "ap          - specifies AP to connect to (only valid for Wi-Fi)\n"
	              "nsp         - specifies NSP to connect to (only valid for WiMAX)\n"
	              "passwd-file - file with password(s) required to activate the connection\n\n"));
874 875 876 877 878
}

static void
usage_connection_down (void)
{
879 880
	g_printerr (_("Usage: nmcli connection down { ARGUMENTS | help }\n"
	              "\n"
881
	              "ARGUMENTS := [id | uuid | path | apath] <ID> ...\n"
882 883 884 885
	              "\n"
	              "Deactivate a connection from a device (without preventing the device from\n"
	              "further auto-activation). The profile to deactivate is identified by its name,\n"
	              "UUID or D-Bus path.\n\n"));
886 887 888 889 890
}

static void
usage_connection_add (void)
{
891 892
	g_printerr (_("Usage: nmcli connection add { ARGUMENTS | help }\n"
	              "\n"
893
	              "ARGUMENTS := COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS SLAVE_OPTIONS IP_OPTIONS [-- ([+|-]<setting>.<property> <value>)+]\n\n"
894 895 896 897
	              "  COMMON_OPTIONS:\n"
	              "                  type <type>\n"
	              "                  ifname <interface name> | \"*\"\n"
	              "                  [con-name <connection name>]\n"
898 899
	              "                  [autoconnect yes|no]\n"
	              "                  [save yes|no]\n"
900 901
	              "                  [master <master (ifname, or connection UUID or name)>]\n"
	              "                  [slave-type <master connection type>]\n\n"
902 903 904 905 906 907 908
	              "  TYPE_SPECIFIC_OPTIONS:\n"
	              "    ethernet:     [mac <MAC address>]\n"
	              "                  [cloned-mac <cloned MAC address>]\n"
	              "                  [mtu <MTU>]\n\n"
	              "    wifi:         ssid <SSID>\n"
	              "                  [mac <MAC address>]\n"
	              "                  [cloned-mac <cloned MAC address>]\n"
909 910
	              "                  [mtu <MTU>]\n"
	              "                  [mode infrastructure|ap|adhoc]\n\n"
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
	              "    wimax:        [mac <MAC address>]\n"
	              "                  [nsp <NSP>]\n\n"
	              "    pppoe:        username <PPPoE username>\n"
	              "                  [password <PPPoE password>]\n"
	              "                  [service <PPPoE service name>]\n"
	              "                  [mtu <MTU>]\n"
	              "                  [mac <MAC address>]\n\n"
	              "    gsm:          apn <APN>\n"
	              "                  [user <username>]\n"
	              "                  [password <password>]\n\n"
	              "    cdma:         [user <username>]\n"
	              "                  [password <password>]\n\n"
	              "    infiniband:   [mac <MAC address>]\n"
	              "                  [mtu <MTU>]\n"
	              "                  [transport-mode datagram | connected]\n"
	              "                  [parent <ifname>]\n"
	              "                  [p-key <IPoIB P_Key>]\n\n"
	              "    bluetooth:    [addr <bluetooth address>]\n"
929
	              "                  [bt-type panu|nap|dun-gsm|dun-cdma]\n\n"
930
	              "    vlan:         dev <parent device (connection UUID, ifname, or MAC)>\n"
931 932 933 934 935 936 937 938 939 940 941 942
	              "                  id <VLAN ID>\n"
	              "                  [flags <VLAN flags>]\n"
	              "                  [ingress <ingress priority mapping>]\n"
	              "                  [egress <egress priority mapping>]\n"
	              "                  [mtu <MTU>]\n\n"
	              "    bond:         [mode balance-rr (0) | active-backup (1) | balance-xor (2) | broadcast (3) |\n"
	              "                        802.3ad    (4) | balance-tlb   (5) | balance-alb (6)]\n"
	              "                  [primary <ifname>]\n"
	              "                  [miimon <num>]\n"
	              "                  [downdelay <num>]\n"
	              "                  [updelay <num>]\n"
	              "                  [arp-interval <num>]\n"
943 944
	              "                  [arp-ip-target <num>]\n"
	              "                  [lacp-rate slow (0) | fast (1)]\n\n"
945 946 947 948 949 950 951 952 953 954
	              "    bond-slave:   master <master (ifname, or connection UUID or name)>\n\n"
	              "    team:         [config <file>|<raw JSON data>]\n\n"
	              "    team-slave:   master <master (ifname, or connection UUID or name)>\n"
	              "                  [config <file>|<raw JSON data>]\n\n"
	              "    bridge:       [stp yes|no]\n"
	              "                  [priority <num>]\n"
	              "                  [forward-delay <2-30>]\n"
	              "                  [hello-time <1-10>]\n"
	              "                  [max-age <6-40>]\n"
	              "                  [ageing-time <0-1000000>]\n"
955
	              "                  [multicast-snooping yes|no]\n"
956 957 958 959 960 961 962 963 964 965
	              "                  [mac <MAC address>]\n\n"
	              "    bridge-slave: master <master (ifname, or connection UUID or name)>\n"
	              "                  [priority <0-63>]\n"
	              "                  [path-cost <1-65535>]\n"
	              "                  [hairpin yes|no]\n\n"
	              "    vpn:          vpn-type vpnc|openvpn|pptp|openconnect|openswan|libreswan|ssh|l2tp|iodine|...\n"
	              "                  [user <username>]\n\n"
	              "    olpc-mesh:    ssid <SSID>\n"
	              "                  [channel <1-13>]\n"
	              "                  [dhcp-anycast <MAC address>]\n\n"
966 967 968 969
	              "    adsl:         username <username>\n"
	              "                  protocol pppoa|pppoe|ipoatm\n"
	              "                  [password <password>]\n"
	              "                  [encapsulation vcmux|llc]\n\n"
970 971 972 973 974 975
	              "    tun:          mode tun|tap\n"
	              "                  [owner <UID>]\n"
	              "                  [group <GID>]\n"
	              "                  [pi yes|no]\n"
	              "                  [vnet-hdr yes|no]\n"
	              "                  [multi-queue yes|no]\n\n"
976
	              "    ip-tunnel:    mode ipip|gre|sit|isatap|vti|ip6ip6|ipip6|ip6gre|vti6\n"
977 978 979
	              "                  remote <remote endpoint IP>\n"
	              "                  [local <local endpoint IP>]\n"
	              "                  [dev <parent device (ifname or connection UUID)>]\n\n"
Beniamino Galvani's avatar
Beniamino Galvani committed
980 981 982 983 984
	              "    macsec:       dev <parent device (connection UUID, ifname, or MAC)>\n"
	              "                  mode <psk|eap>\n"
	              "                  [cak <key> ckn <key>]\n"
	              "                  [encrypt yes|no]\n"
	              "                  [port 1-65534]\n\n\n"
985
	              "    macvlan:      dev <parent device (connection UUID, ifname, or MAC)>\n"
986 987
	              "                  mode vepa|bridge|private|passthru|source\n"
	              "                  [tap yes|no]\n\n"
988 989 990
	              "    vxlan:        id <VXLAN ID>\n"
	              "                  remote <IP of multicast group or remote address>\n"
	              "                  [local <source IP>]\n"
991
	              "                  [dev <parent device (ifname or connection UUID)>]\n"
992 993 994
	              "                  [source-port-min <0-65535>]\n"
	              "                  [source-port-max <0-65535>]\n"
	              "                  [destination-port <0-65535>]\n\n"
995 996 997 998
	              "    wpan:         [short-addr <0x0000-0xffff>]\n"
	              "                  [pan-id <0x0000-0xffff>]\n"
	              "                  [page <default|0-31>]\n"
	              "                  [channel <default|0-26>]\n"
999
	              "                  [mac <MAC address>]\n\n"
1000
	              "    6lowpan:      dev <parent device (connection UUID, ifname, or MAC)>\n"
1001
	              "    dummy:\n\n"
1002 1003 1004 1005 1006
	              "  SLAVE_OPTIONS:\n"
	              "    bridge:       [priority <0-63>]\n"
	              "                  [path-cost <1-65535>]\n"
	              "                  [hairpin yes|no]\n\n"
	              "    team:         [config <file>|<raw JSON data>]\n\n"
1007 1008 1009
	              "  IP_OPTIONS:\n"
	              "                  [ip4 <IPv4 address>] [gw4 <IPv4 gateway>]\n"
	              "                  [ip6 <IPv6 address>] [gw6 <IPv6 gateway>]\n\n"));
1010 1011 1012 1013 1014
}

static void
usage_connection_modify (void)
{
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
	g_printerr (_("Usage: nmcli connection modify { ARGUMENTS | help }\n"
	              "\n"
	              "ARGUMENTS := [id | uuid | path] <ID> ([+|-]<setting>.<property> <value>)+\n"
	              "\n"
	              "Modify one or more properties of the connection profile.\n"
	              "The profile is identified by its name, UUID or D-Bus path. For multi-valued\n"
	              "properties you can use optional '+' or '-' prefix to the property name.\n"
	              "The '+' sign allows appending items instead of overwriting the whole value.\n"
	              "The '-' sign allows removing selected items instead of the whole value.\n"
	              "\n"
	              "Examples:\n"
	              "nmcli con mod home-wifi wifi.ssid rakosnicek\n"
	              "nmcli con mod em1-1 ipv4.method manual ipv4.addr \"192.168.1.2/24, 10.10.1.5/8\"\n"
	              "nmcli con mod em1-1 +ipv4.dns 8.8.4.4\n"
	              "nmcli con mod em1-1 -ipv4.dns 1\n"
	              "nmcli con mod em1-1 -ipv6.addr \"abbe::cafe/56\"\n"
	              "nmcli con mod bond0 +bond.options mii=500\n"
	              "nmcli con mod bond0 -bond.options downdelay\n\n"));
1033 1034
}

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
static void
usage_connection_clone (void)
{
	g_printerr (_("Usage: nmcli connection clone { ARGUMENTS | help }\n"
	              "\n"
	              "ARGUMENTS := [--temporary] [id | uuid | path] <ID> <new name>\n"
	              "\n"
	              "Clone an existing connection profile. The newly created connection will be\n"
	              "the exact copy of the <ID>, except the uuid property (will be generated) and\n"
	              "id (provided as <new name> argument).\n\n"));
}

1047 1048 1049
static void
usage_connection_edit (void)
{
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	g_printerr (_("Usage: nmcli connection edit { ARGUMENTS | help }\n"
	              "\n"
	              "ARGUMENTS := [id | uuid | path] <ID>\n"
	              "\n"
	              "Edit an existing connection profile in an interactive editor.\n"
	              "The profile is identified by its name, UUID or D-Bus path\n"
	              "\n"
	              "ARGUMENTS := [type <new connection type>] [con-name <new connection name>]\n"
	              "\n"
	              "Add a new connection profile in an interactive editor.\n\n"));
1060 1061 1062 1063 1064
}

static void
usage_connection_delete (void)
{
1065 1066 1067 1068 1069 1070
	g_printerr (_("Usage: nmcli connection delete { ARGUMENTS | help }\n"
	              "\n"
	              "ARGUMENTS := [id | uuid | path] <ID>\n"
	              "\n"
	              "Delete a connection profile.\n"
	              "The profile is identified by its name, UUID or D-Bus path.\n\n"));
1071 1072
}

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
static void
usage_connection_monitor (void)
{
	g_printerr (_("Usage: nmcli connection monitor { ARGUMENTS | help }\n"
	              "\n"
	              "ARGUMENTS := [id | uuid | path] <ID> ...\n"
	              "\n"
	              "Monitor connection profile activity.\n"
	              "This command prints a line whenever the specified connection changes.\n"
	              "Monitors all connection profiles in case none is specified.\n\n"));
}

1085 1086 1087
static void
usage_connection_reload (void)
{
1088 1089 1090
	g_printerr (_("Usage: nmcli connection reload { help }\n"
	              "\n"
	              "Reload all connection files from disk.\n\n"));
1091 1092 1093 1094 1095
}

static void
usage_connection_load (void)
{
1096 1097 1098 1099 1100 1101 1102
	g_printerr (_("Usage: nmcli connection load { ARGUMENTS | help }\n"
	              "\n"
	              "ARGUMENTS := <filename> [<filename>...]\n"
	              "\n"
	              "Load/reload one or more connection files from disk. Use this after manually\n"
	              "editing a connection file to ensure that NetworkManager is aware of its latest\n"
	              "state.\n\n"));
1103 1104
}

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
static void
usage_connection_import (void)
{
	g_printerr (_("Usage: nmcli connection import { ARGUMENTS | help }\n"
	              "\n"
	              "ARGUMENTS := [--temporary] type <type> file <file to import>\n"
	              "\n"
	              "Import an external/foreign configuration as a NetworkManager connection profile.\n"
	              "The type of the input file is specified by type option.\n"
	              "Only VPN configurations are supported at the moment. The configuration\n"
	              "is imported by NetworkManager VPN plugins.\n\n"));
}

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
static void
usage_connection_export (void)
{
	g_printerr (_("Usage: nmcli connection export { ARGUMENTS | help }\n"
	              "\n"
	              "ARGUMENTS := [id | uuid | path] <ID> [<output file>]\n"
	              "\n"
	              "Export a connection. Only VPN connections are supported at the moment.\n"
	              "The data are directed to standard output or to a file if a name is given.\n\n"));
}

1129 1130 1131
static void
quit (void)
{
1132
	if (nm_clear_g_source (&progress_id))
1133
		nmc_terminal_erase_line ();
1134
	g_main_loop_quit (loop);
1135 1136
}

1137
static char *
1138 1139 1140
construct_header_name (const char *base, const char *spec)
{
	if (spec == NULL)
1141
		return g_strdup (base);
1142

1143
	return g_strdup_printf ("%s (%s)", base, spec);
1144 1145
}

1146 1147 1148 1149 1150 1151 1152 1153 1154
static int
get_ac_for_connection_cmp (gconstpointer pa, gconstpointer pb, gpointer user_data)
{
	NMActiveConnection *ac_a = *((NMActiveConnection *const*) pa);
	NMActiveConnection *ac_b = *((NMActiveConnection *const*) pb);

	return active_connection_cmp (ac_a, ac_b);
}

1155
static NMActiveConnection *
1156
get_ac_for_connection (const GPtrArray *active_cons, NMConnection *connection, GPtrArray **out_result)
1157
{
1158 1159 1160
	guint i;
	NMActiveConnection *best_candidate = NULL;
	GPtrArray *result = out_result ? *out_result : NULL;
1161

1162
	for (i = 0; i < active_cons->len; i++) {
1163
		NMActiveConnection *candidate = g_ptr_array_index (active_cons, i);
1164
		NMRemoteConnection *con;
1165

1166
		con = nm_active_connection_get_connection (candidate);
1167 1168
		if (NM_CONNECTION (con) != connection)
			continue;
1169 1170 1171 1172 1173 1174

		if (!out_result)
			return candidate;
		if (!result)
			result = g_ptr_array_new_with_free_func (g_object_unref);
		g_ptr_array_add (result, g_object_ref (candidate));
1175
	}
1176

1177 1178 1179 1180 1181
	if (result) {
		g_ptr_array_sort_with_data (result, get_ac_for_connection_cmp, NULL);
		best_candidate = result->pdata[0];
	}

1182 1183
	NM_SET_OUT (out_result, result);
	return best_candidate;
1184 1185
}

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
typedef struct {
	GMainLoop *loop;
	NMConnection *local;
	const char *setting_name;
} GetSecretsData;

static void
got_secrets (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
	NMRemoteConnection *remote = NM_REMOTE_CONNECTION (source_object);
	GetSecretsData *data = user_data;
	GVariant *secrets;
	GError *error = NULL;

	secrets = nm_remote_connection_get_secrets_finish (remote, res, NULL);
	if (secrets) {
		if (!nm_connection_update_secrets (data->local, NULL, secrets, &error) && error) {
			g_printerr (_("Error updating secrets for %s: %s\n"),
			            data->setting_name, error->message);
			g_clear_error (&error);
		}
		g_variant_unref (secrets);
	}

	g_main_loop_quit (data->loop);
}

1213
/* Put secrets into local connection. */
1214
static void
1215
update_secrets_in_connection (NMRemoteConnection *remote, NMConnection *local)
1216
{
1217
	GetSecretsData data = { 0, };
1218
	int i;
1219 1220 1221

	data.local = local;
	data.loop = g_main_loop_new (NULL, FALSE);
1222

1223
	for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++) {
1224 1225 1226 1227 1228 1229 1230
		data.setting_name = nm_meta_setting_infos[i].setting_name;
		nm_remote_connection_get_secrets_async (remote,
		                                        nm_meta_setting_infos[i].setting_name,
		                                        NULL,
		                                        got_secrets,
		                                        &data);
		g_main_loop_run (data.loop);
1231
	}
1232 1233

	g_main_loop_unref (data.loop);
1234 1235
}

1236
static gboolean
1237
nmc_connection_profile_details (NMConnection *connection, NmCli *nmc)
1238 1239 1240
{
	GError *error = NULL;
	GArray *print_settings_array;
1241
	GPtrArray *prop_array = NULL;
1242 1243 1244 1245
	int i;
	char *fields_str;
	char *fields_all =    NMC_FIELDS_SETTINGS_NAMES_ALL;
	char *fields_common = NMC_FIELDS_SETTINGS_NAMES_ALL;
1246
	const char *base_hdr = _("Connection profile details");
1247 1248 1249 1250 1251 1252 1253 1254 1255
	gboolean was_output = FALSE;

	if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
		fields_str = fields_common;
	else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0)
		fields_str = fields_all;
	else
		fields_str = nmc->required_fields;

1256 1257
	print_settings_array = parse_output_fields (fields_str, (const NMMetaAbstractInfo *const*) nm_meta_setting_infos_editor_p (),
	                                            TRUE, &prop_array, &error);
1258
	if (error) {
1259
		g_string_printf (nmc->return_text, _("Error: 'connection show': %s"), error->message);
1260 1261 1262 1263
		g_error_free (error);
		nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
		return FALSE;
	}
1264
	g_assert (print_settings_array);
1265

1266
	/* Main header */
1267
	{
1268
		gs_free char *header_name = NULL;
1269 1270
		gs_free NmcOutputField *row = NULL;
		gs_unref_array GArray *out_indices = NULL;
1271

1272 1273
		header_name = construct_header_name (base_hdr, nm_connection_get_id (connection));
		out_indices = parse_output_fields (NMC_FIELDS_SETTINGS_NAMES_ALL,
1274 1275 1276 1277 1278 1279
		                                   (const NMMetaAbstractInfo *const*) nm_meta_setting_infos_editor_p (),
		                                   FALSE, NULL, NULL);

		row = g_new0 (NmcOutputField, _NM_META_SETTING_TYPE_NUM + 1);
		for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++)
			row[i].info = (const NMMetaAbstractInfo *) &nm_meta_setting_infos_editor[i];
1280

1281
		print_required_fields (&nmc->nmc_config, NMC_OF_FLAG_MAIN_HEADER_ONLY,
1282
		                       out_indices, header_name,
1283
		                       0, row);
1284
	}
1285 1286 1287

	/* Loop through the required settings and print them. */
	for (i = 0; i < print_settings_array->len; i++) {
1288
		NMSetting *setting;
1289
		int section_idx = g_array_index (print_settings_array, int, i);
1290
		const char *prop_name = (const char *) g_ptr_array_index (prop_array, i);
1291

1292 1293 1294
		if (   NM_IN_SET (nmc->nmc_config.print_output, NMC_PRINT_NORMAL, NMC_PRINT_PRETTY)
		    && !nmc->nmc_config.multiline_output
		    && was_output)
1295
			g_print ("\n"); /* Empty line */
1296 1297 1298

		was_output = FALSE;

1299
		setting = nm_connection_get_setting_by_name (connection, nm_meta_setting_infos_editor[section_idx].general->setting_name);
1300
		if (setting) {
1301
			setting_details (&nmc->nmc_config, setting, prop_name);
1302
			was_output = TRUE;
Thomas Graf's avatar
Thomas Graf committed
1303
		}
1304 1305
	}

1306
	g_array_free (print_settings_array, TRUE);
1307 1308
	if (prop_array)
		g_ptr_array_free (prop_array, TRUE);
1309

1310
	return TRUE;
1311 1312
}

1313 1314
NMMetaColor
nmc_active_connection_state_to_color (NMActiveConnectionState state)
1315 1316
{
	if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATING)
1317
		return NM_META_COLOR_CONNECTION_ACTIVATING;
1318
	else if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATED)
1319
		return NM_META_COLOR_CONNECTION_ACTIVATED;
1320
	else if (state > NM_ACTIVE_CONNECTION_STATE_ACTIVATED)
1321 1322 1323
		return NM_META_COLOR_CONNECTION_DISCONNECTING;
	else
		return NM_META_COLOR_CONNECTION_UNKNOWN;
1324 1325
}

1326
static gboolean
1327
nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc)
1328
{
1329
	GError *error = NULL;
1330
	GArray *print_groups;
1331
	GPtrArray *group_fields = NULL;
1332
	int i;
1333
	const char *fields_str = NULL;
1334
	const char *base_hdr = _("Activate connection details");
1335
	gboolean was_output = FALSE;
1336

1337 1338 1339
	if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0) {
	} else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0) {
	} else
1340 1341
		fields_str = nmc->required_fields;

1342
	print_groups = parse_output_fields (fields_str, (const NMMetaAbstractInfo *const*) nmc_fields_con_active_details_groups, TRUE, &group_fields, &error);
1343
	if (error) {
1344
		g_string_printf (nmc->return_text, _("Error: 'connection show': %s"), error->message);
1345
		g_error_free (error);
1346
		nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
1347 1348
		return FALSE;
	}
1349
	g_assert (print_groups);
1350

1351
	/* Main header */
1352
	{
1353
		gs_free char *header_name = NULL;
1354 1355
		gs_free NmcOutputField *row = NULL;
		gs_unref_array GArray *out_indices = NULL;
1356

1357
		header_name = construct_header_name (base_hdr, nm_active_connection_get_uuid (acon));
1358 1359 1360 1361 1362 1363 1364
		out_indices = parse_output_fields (NULL,
		                                   (const NMMetaAbstractInfo *const*) nmc_fields_con_active_details_groups,
		                                   FALSE, NULL, NULL);

		row = g_new0 (NmcOutputField, G_N_ELEMENTS (nmc_fields_con_active_details_groups) + 1);
		for (i = 0; nmc_fields_con_active_details_groups[i]; i++)
			row[i].info = (const NMMetaAbstractInfo *) nmc_fields_con_active_details_groups[i];
1365

1366
		print_required_fields (&nmc->nmc_config, NMC_OF_FLAG_MAIN_HEADER_ONLY,
1367
		                       out_indices, header_name,
1368
		                       0, row);
1369
	}
1370 1371 1372 1373

	/* Loop through the groups and print them. */
	for (i = 0; i < print_groups->len; i++) {
		int group_idx = g_array_index (print_groups, int, i);
1374
		char *group_fld = (char *) g_ptr_array_index (group_fields, i);
1375

1376
		if (   NM_IN_SET (nmc->nmc_config.print_output, NMC_PRINT_NORMAL, NMC_PRINT_PRETTY)
1377 1378 1379
		    && !nmc->nmc_config.multiline_output
		    && was_output)
			g_print ("\n");
1380 1381 1382

		was_output = FALSE;

1383 1384
		if (nmc_fields_con_active_details_groups[group_idx]->nested == metagen_con_active_general) {
			gs_free char *f = NULL;
1385

1386 1387
			if (group_fld)
				f = g_strdup_printf ("GENERAL.%s", group_fld);
1388

1389 1390 1391
			nmc_print (&nmc->nmc_config,
			           (gpointer[]) { acon, NULL },
			           NULL,
1392
			           NULL,
1393 1394 1395
			           NMC_META_GENERIC_GROUP ("GENERAL", metagen_con_active_general, N_("GROUP")),
			           f,
			           NULL);
1396
			was_output = TRUE;
1397
			continue;
1398 1399
		}

1400
		/* IP4 */
1401
		if (strcasecmp (nmc_fields_con_active_details_groups[group_idx]->name,  nmc_fields_con_active_details_groups[1]->name) == 0) {
1402
			gboolean b1 = FALSE;
1403
			NMIPConfig *cfg4 = nm_active_connection_get_ip4_config (acon);
1404

1405
			b1 = print_ip_config (cfg4, AF_INET, &nmc->nmc_config, group_fld);
1406
			was_output = was_output || b1;
1407 1408 1409
		}

		/* DHCP4 */
1410
		if (strcasecmp (nmc_fields_con_active_details_groups[group_idx]->name,  nmc_fields_con_active_details_groups[2]->name) == 0) {
1411
			gboolean b1 = FALSE;
1412
			NMDhcpConfig *dhcp4 = nm_active_connection_get_dhcp4_config (acon);
1413

1414
			b1 = print_dhcp_config (dhcp4, AF_INET, &nmc->nmc_config, group_fld);
1415
			was_output = was_output || b1;
1416 1417 1418
		}

		/* IP6 */
1419
		if (strcasecmp (nmc_fields_con_active_details_groups[group_idx]->name,  nmc_fields_con_active_details_groups[3]->name) == 0) {
1420
			gboolean b1 = FALSE;
1421
			NMIPConfig *cfg6 = nm_active_connection_get_ip6_config (acon);
1422

1423
			b1 = print_ip_config (cfg6, AF_INET6, &nmc->nmc_config, group_fld);
1424
			was_output = was_output || b1;
1425 1426 1427
		}

		/* DHCP6 */
1428
		if (strcasecmp (nmc_fields_con_active_details_groups[group_idx]->name,  nmc_fields_con_active_details_groups[4]->name) == 0) {
1429
			gboolean b1 = FALSE;
1430
			NMDhcpConfig *dhcp6 = nm_active_connection_get_dhcp6_config (acon);
1431

1432
			b1 = print_dhcp_config (dhcp6, AF_INET6, &nmc->nmc_config, group_fld);
1433
			was_output = was_output || b1;
1434 1435
		}

1436 1437 1438 1439 1440
		if (nmc_fields_con_active_details_groups[group_idx]->nested == metagen_con_active_vpn) {
			if (NM_IS_VPN_CONNECTION (acon)) {
				nmc_print (&nmc->nmc_config,
				           (gpointer[]) { acon, NULL },
				           NULL,
1441
				           NULL,
1442 1443 1444 1445
				           NMC_META_GENERIC_GROUP ("VPN", metagen_con_active_vpn, N_("NAME")),
				           group_fld,
				           NULL);
				was_output = TRUE;
1446
			}
1447
			continue;
1448
		}
1449 1450
	}

1451
	g_array_free (print_groups, TRUE);
1452 1453
	if (group_fields)
		g_ptr_array_free (group_fields, TRUE);
1454 1455 1456 1457

	return TRUE;
}

1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
static gboolean
split_required_fields_for_con_show (const char *input,
                                    char **profile_flds,
                                    char **active_flds,
                                    GError **error)
{
	char **fields, **iter;
	char *dot;
	GString *str1, *str2;
	gboolean found;
	gboolean group_profile = FALSE;
	gboolean group_active = FALSE;
	gboolean success = TRUE;
	gboolean is_all, is_common;
	int i;

	if (!input) {
		*profile_flds = NULL;
		*active_flds = NULL;
		return TRUE;
	}

	str1 = g_string_new (NULL);
	str2 = g_string_new (NULL);

	/* Split supplied fields string */
	fields = g_strsplit_set (input, ",", -1);
	for (iter = fields; iter && *iter; iter++) {
		g_strstrip (*iter);
		dot = strchr (*iter, '.');
		if (dot)
			*dot = '\0';

		is_all = !dot && strcasecmp (*iter, "all") == 0;
		is_common = !dot && strcasecmp (*iter, "common") == 0;

		found = FALSE;

1496
		for (i = 0; i < _NM_META_SETTING_TYPE_NUM; i++) {
1497
			if (   is_all || is_common
1498
			    || !strcasecmp (*iter, nm_meta_setting_infos[i].setting_name)) {
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
				if (dot)
					*dot = '.';
				g_string_append (str1, *iter);
				g_string_append_c (str1, ',');
				found = TRUE;
				break;
			}
		}
		if (found)
			continue;
1509
		for (i = 0; nmc_fields_con_active_details_groups[i]; i++) {
1510
			if (   is_all || is_common
1511
			    || !strcasecmp (*iter, nmc_fields_con_active_details_groups[i]->name)) {
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
				if (dot)
					*dot = '.';
				g_string_append (str2, *iter);
				g_string_append_c (str2, ',');
				found = TRUE;
				break;
			}
		}
		if (!found) {
			if (dot)
				*dot = '.';
			if (!strcasecmp (*iter, CON_SHOW_DETAIL_GROUP_PROFILE))
				group_profile = TRUE;
			else if (!strcasecmp (*iter, CON_SHOW_DETAIL_GROUP_ACTIVE))
				group_active = TRUE;
			else {
1528 1529
				char *allowed1 = nm_meta_abstract_infos_get_names_str ((const NMMetaAbstractInfo *const*) nm_meta_setting_infos_editor_p (), NULL);
				char *allowed2 = nm_meta_abstract_infos_get_names_str ((const NMMetaAbstractInfo *const*) nmc_fields_con_active_details_groups, NULL);
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
				g_set_error (error, NMCLI_ERROR, 0, _("invalid field '%s'; allowed fields: %s and %s, or %s,%s"),
				             *iter, allowed1, allowed2, CON_SHOW_DETAIL_GROUP_PROFILE, CON_SHOW_DETAIL_GROUP_ACTIVE);
				g_free (allowed1);
				g_free (allowed2);
				success = FALSE;
				break;
			}
		}
	}
	if (fields)
		g_strfreev (fields);

	/* Handle pseudo groups: profile, active */
	if (success && group_profile) {
		if (str1->len > 0) {
1545
			g_set_error (error, NMCLI_ERROR, 0, _("'%s' has to be alone"),
1546 1547 1548 1549 1550 1551 1552
			             CON_SHOW_DETAIL_GROUP_PROFILE);
			success = FALSE;
		} else
			g_string_assign (str1, "all,");
	}
	if (success && group_active) {
		if (str2->len > 0) {
1553
			g_set_error (error, NMCLI_ERROR, 0, _("'%s' has to be alone"),
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
			             CON_SHOW_DETAIL_GROUP_ACTIVE);
			success = FALSE;
		} else
			g_string_assign (str2, "all,");
	}

	if (success) {
		if (str1->len > 0)
			g_string_truncate (str1, str1->len - 1);
		if (str2->len > 0)
			g_string_truncate (str2, str2->len - 1);
		*profile_flds = g_string_free (str1, str1->len == 0);
		*active_flds = g_string_free (str2, str2->len == 0);
	} else {
		g_string_free (str1, TRUE);
		g_string_free (str2, TRUE);
	}
	return success;
}

1574
typedef enum {
1575 1576 1577 1578 1579 1580 1581 1582
	NMC_SORT_ACTIVE     =  1,
	NMC_SORT_ACTIVE_INV = -1,
	NMC_SORT_NAME       =  2,
	NMC_SORT_NAME_INV   = -2,
	NMC_SORT_TYPE       =  3,
	NMC_SORT_TYPE_INV   = -3,
	NMC_SORT_PATH       =  4,
	NMC_SORT_PATH_INV   = -4,
1583 1584
} NmcSortOrder;

1585 1586 1587
typedef struct {
	NmCli *nmc;
	const GArray *order;
1588 1589