nm-setting-vpn.c 16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * Dan Williams <dcbw@redhat.com>
 * Tambet Ingo <tambet@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * (C) Copyright 2007 - 2008 Red Hat, Inc.
 * (C) Copyright 2007 - 2008 Novell, Inc.
 */
24 25

#include <string.h>
26 27
#include <errno.h>
#include <stdlib.h>
28 29 30 31
#include <dbus/dbus-glib.h>
#include "nm-setting-vpn.h"
#include "nm-param-spec-specialized.h"
#include "nm-utils.h"
32
#include "nm-dbus-glib-types.h"
33
#include "nm-setting-private.h"
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
GQuark
nm_setting_vpn_error_quark (void)
{
	static GQuark quark;

	if (G_UNLIKELY (!quark))
		quark = g_quark_from_static_string ("nm-setting-vpn-error-quark");
	return quark;
}

/* This should really be standard. */
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }

GType
nm_setting_vpn_error_get_type (void)
{
	static GType etype = 0;

	if (etype == 0) {
		static const GEnumValue values[] = {
			/* Unknown error. */
			ENUM_ENTRY (NM_SETTING_VPN_ERROR_UNKNOWN, "UnknownError"),
			/* The specified property was invalid. */
			ENUM_ENTRY (NM_SETTING_VPN_ERROR_INVALID_PROPERTY, "InvalidProperty"),
			/* The specified property was missing and is required. */
			ENUM_ENTRY (NM_SETTING_VPN_ERROR_MISSING_PROPERTY, "MissingProperty"),
			{ 0, 0, 0 }
		};
		etype = g_enum_register_static ("NMSettingVpnError", values);
	}
	return etype;
}


69 70
G_DEFINE_TYPE (NMSettingVPN, nm_setting_vpn, NM_TYPE_SETTING)

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#define NM_SETTING_VPN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_VPN, NMSettingVPNPrivate))

typedef struct {
	char *service_type;

	/* username of the user requesting this connection, thus
	 * it's really only valid for user connections, and it also
	 * should never be saved out to persistent config.
	 */
	char *user_name;

	/* The hash table is created at setting object
	 * init time and should not be replaced.  It is
	 * a char * -> char * mapping, and both the key
	 * and value are owned by the hash table, and should
	 * be allocated with functions whose value can be
	 * freed with g_free().  Should not contain secrets.
	 */
	GHashTable *data;

	/* The hash table is created at setting object
	 * init time and should not be replaced.  It is
	 * a char * -> char * mapping, and both the key
	 * and value are owned by the hash table, and should
	 * be allocated with functions whose value can be
	 * freed with g_free().  Should contain secrets only.
	 */
	GHashTable *secrets;
} NMSettingVPNPrivate;

101 102 103 104
enum {
	PROP_0,
	PROP_SERVICE_TYPE,
	PROP_USER_NAME,
105
	PROP_DATA,
106
	PROP_SECRETS,
107 108 109 110 111 112 113 114 115 116

	LAST_PROP
};

NMSetting *
nm_setting_vpn_new (void)
{
	return (NMSetting *) g_object_new (NM_TYPE_SETTING_VPN, NULL);
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
const char *
nm_setting_vpn_get_service_type (NMSettingVPN *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_VPN (setting), NULL);

	return NM_SETTING_VPN_GET_PRIVATE (setting)->service_type;
}

const char *
nm_setting_vpn_get_user_name (NMSettingVPN *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_VPN (setting), NULL);

	return NM_SETTING_VPN_GET_PRIVATE (setting)->user_name;
}

void
nm_setting_vpn_add_data_item (NMSettingVPN *setting,
                              const char *key,
                              const char *item)
{
	g_return_if_fail (NM_IS_SETTING_VPN (setting));
139 140 141 142
	g_return_if_fail (key != NULL);
	g_return_if_fail (strlen (key) > 0);
	g_return_if_fail (item != NULL);
	g_return_if_fail (strlen (item) > 0);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

	g_hash_table_insert (NM_SETTING_VPN_GET_PRIVATE (setting)->data,
	                     g_strdup (key), g_strdup (item));
}

const char *
nm_setting_vpn_get_data_item (NMSettingVPN *setting, const char *key)
{
	g_return_val_if_fail (NM_IS_SETTING_VPN (setting), NULL);

	return (const char *) g_hash_table_lookup (NM_SETTING_VPN_GET_PRIVATE (setting)->data, key);
}

void
nm_setting_vpn_remove_data_item (NMSettingVPN *setting, const char *key)
{
	g_return_if_fail (NM_IS_SETTING_VPN (setting));

	g_hash_table_remove (NM_SETTING_VPN_GET_PRIVATE (setting)->data, key);
}

164 165 166 167 168 169 170 171
/**
 * nm_setting_vpn_foreach_data_item:
 * @setting: a #NMSettingVPN
 * @func: (scope call): an user provided function
 * @user_data:
 *
 * Iterates all data items stored in this setting
 */
172 173
void
nm_setting_vpn_foreach_data_item (NMSettingVPN *setting,
174
                                  NMVPNIterFunc func,
175 176 177 178 179 180 181 182 183 184 185 186 187 188
                                  gpointer user_data)
{
	g_return_if_fail (NM_IS_SETTING_VPN (setting));

	g_hash_table_foreach (NM_SETTING_VPN_GET_PRIVATE (setting)->data,
	                      (GHFunc) func, user_data);
}

void
nm_setting_vpn_add_secret (NMSettingVPN *setting,
                           const char *key,
                           const char *secret)
{
	g_return_if_fail (NM_IS_SETTING_VPN (setting));
189 190 191 192
	g_return_if_fail (key != NULL);
	g_return_if_fail (strlen (key) > 0);
	g_return_if_fail (secret != NULL);
	g_return_if_fail (strlen (secret) > 0);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

	g_hash_table_insert (NM_SETTING_VPN_GET_PRIVATE (setting)->secrets,
	                     g_strdup (key), g_strdup (secret));
}

const char *
nm_setting_vpn_get_secret (NMSettingVPN *setting, const char *key)
{
	g_return_val_if_fail (NM_IS_SETTING_VPN (setting), NULL);

	return (const char *) g_hash_table_lookup (NM_SETTING_VPN_GET_PRIVATE (setting)->secrets, key);
}

void
nm_setting_vpn_remove_secret (NMSettingVPN *setting, const char *key)
{
	g_return_if_fail (NM_IS_SETTING_VPN (setting));

	g_hash_table_remove (NM_SETTING_VPN_GET_PRIVATE (setting)->secrets, key);
}

214 215 216 217 218 219 220 221
/**
 * nm_setting_vpn_foreach_secret:
 * @setting: a #NMSettingVPN
 * @func: (scope call): an user provided function
 * @user_data:
 *
 * Iterates all secrets stored in this setting.
 */
222 223
void
nm_setting_vpn_foreach_secret (NMSettingVPN *setting,
224
                               NMVPNIterFunc func,
225 226 227 228 229 230 231 232
                               gpointer user_data)
{
	g_return_if_fail (NM_IS_SETTING_VPN (setting));

	g_hash_table_foreach (NM_SETTING_VPN_GET_PRIVATE (setting)->secrets,
	                      (GHFunc) func, user_data);
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
/**
 * nm_setting_vpn_get_secret_flags:
 * @setting: a #NMSettingVPN
 * @secret_name: the secret key name to get flags for
 * @out_flags: on success, the flags for the secret @secret_name
 *
 * For a given VPN secret, retrieves the #NMSettingSecretFlags describing how to
 * handle that secret.
 *
 * Returns: TRUE on success (if the secret flags data item was found), FALSE if
 * the secret flags data item was not found
 */
gboolean
nm_setting_vpn_get_secret_flags (NMSettingVPN *setting,
                                 const char *secret_name,
                                 NMSettingSecretFlags *out_flags)
{
	char *flags_key;
	unsigned long tmp;
	gboolean success = FALSE;
	gpointer val;

	g_return_val_if_fail (NM_IS_SETTING_VPN (setting), FALSE);
	g_return_val_if_fail (secret_name != NULL, FALSE);
	g_return_val_if_fail (out_flags != NULL, FALSE);

	flags_key = g_strdup_printf ("%s-flags", secret_name);
	g_assert (flags_key);
	if (g_hash_table_lookup_extended (NM_SETTING_VPN_GET_PRIVATE (setting)->data,
	                                  flags_key,
	                                  NULL,
	                                  &val)) {
		errno = 0;
		tmp = strtoul ((const char *) val, NULL, 10);
267
		if ((errno == 0) && (tmp <= NM_SETTING_SECRET_FLAGS_ALL)) {
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
			success = TRUE;
			*out_flags = (guint32) tmp;
		}
	}
	g_free (flags_key);
	return success;
}

/**
 * nm_setting_vpn_set_secret_flags:
 * @setting: a #NMSettingVPN
 * @secret_name: the secret key name to set flags for
 * @flags: the flags for the secret
 *
 * For a given VPN secret, sets the #NMSettingSecretFlags describing how to
 * handle that secret.
 */
void
nm_setting_vpn_set_secret_flags (NMSettingVPN *setting,
                                 const char *secret_name,
                                 NMSettingSecretFlags flags)
{
	char *key_name, *str_val;

	g_return_if_fail (NM_IS_SETTING_VPN (setting));
	g_return_if_fail (secret_name != NULL);

	key_name = g_strdup_printf ("%s-flags", secret_name);
	g_assert (key_name);
	str_val = g_strdup_printf ("%u", flags);
	g_assert (str_val);
	g_hash_table_insert (NM_SETTING_VPN_GET_PRIVATE (setting)->data, key_name, str_val);
}

302
static gboolean
303
verify (NMSetting *setting, GSList *all_settings, GError **error)
304
{
305
	NMSettingVPNPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
306

307
	if (!priv->service_type) {
308 309 310 311 312 313 314
		g_set_error (error,
		             NM_SETTING_VPN_ERROR,
		             NM_SETTING_VPN_ERROR_MISSING_PROPERTY,
		             NM_SETTING_VPN_SERVICE_TYPE);
		return FALSE;
	}

315
	if (!strlen (priv->service_type)) {
316 317 318 319
		g_set_error (error,
		             NM_SETTING_VPN_ERROR,
		             NM_SETTING_VPN_ERROR_INVALID_PROPERTY,
		             NM_SETTING_VPN_SERVICE_TYPE);
320
		return FALSE;
321
	}
322 323

	/* default username can be NULL, but can't be zero-length */
324
	if (priv->user_name && !strlen (priv->user_name)) {
325 326 327 328
		g_set_error (error,
		             NM_SETTING_VPN_ERROR,
		             NM_SETTING_VPN_ERROR_INVALID_PROPERTY,
		             NM_SETTING_VPN_USER_NAME);
329
		return FALSE;
330
	}
331 332 333 334

	return TRUE;
}

335 336
static gboolean
update_one_secret (NMSetting *setting, const char *key, GValue *value, GError **error)
337
{
338
	NMSettingVPNPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
339
	char *str;
340

341 342 343 344 345 346 347 348 349
	g_return_val_if_fail (key != NULL, FALSE);
	g_return_val_if_fail (value != NULL, FALSE);

	if (!G_VALUE_HOLDS_STRING (value)) {
		g_set_error (error, NM_SETTING_ERROR,
		             NM_SETTING_ERROR_PROPERTY_TYPE_MISMATCH,
		             "%s", key);
		return FALSE;
	}
350

351 352 353 354 355 356 357 358 359 360 361
	str = g_value_dup_string (value);
	if (!str || !strlen (str)) {
		g_set_error (error, NM_SETTING_ERROR,
		             NM_SETTING_ERROR_PROPERTY_TYPE_MISMATCH,
		             "Secret %s was empty", key);
		g_free (str);
		return FALSE;
	}

	g_hash_table_insert (priv->secrets, g_strdup (key), str);
	return TRUE;
362 363 364 365 366 367 368 369 370 371
}

static void
destroy_one_secret (gpointer data)
{
	char *secret = (char *) data;

	/* Don't leave the secret lying around in memory */
	memset (secret, 0, strlen (secret));
	g_free (secret);
372 373
}

374 375 376
static void
nm_setting_vpn_init (NMSettingVPN *setting)
{
377
	NMSettingVPNPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
378

379 380 381
	g_object_set (setting, NM_SETTING_NAME, NM_SETTING_VPN_SETTING_NAME, NULL);
	priv->data = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
	priv->secrets = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, destroy_one_secret);
382 383 384 385 386
}

static void
finalize (GObject *object)
{
387
	NMSettingVPNPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (object);
388

389 390 391 392
	g_free (priv->service_type);
	g_free (priv->user_name);
	g_hash_table_destroy (priv->data);
	g_hash_table_destroy (priv->secrets);
393 394 395 396

	G_OBJECT_CLASS (nm_setting_vpn_parent_class)->finalize (object);
}

397 398 399 400 401 402
static void
copy_hash (gpointer key, gpointer value, gpointer user_data)
{
	g_hash_table_insert ((GHashTable *) user_data, g_strdup (key), g_strdup (value));
}

403 404 405 406
static void
set_property (GObject *object, guint prop_id,
		    const GValue *value, GParamSpec *pspec)
{
407
	NMSettingVPNPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (object);
408
	GHashTable *new_hash;
409 410 411

	switch (prop_id) {
	case PROP_SERVICE_TYPE:
412 413
		g_free (priv->service_type);
		priv->service_type = g_value_dup_string (value);
414 415
		break;
	case PROP_USER_NAME:
416 417
		g_free (priv->user_name);
		priv->user_name = g_value_dup_string (value);
418
		break;
419 420
	case PROP_DATA:
		/* Must make a deep copy of the hash table here... */
421
		g_hash_table_remove_all (priv->data);
422 423
		new_hash = g_value_get_boxed (value);
		if (new_hash)
424
			g_hash_table_foreach (new_hash, copy_hash, priv->data);
425 426 427
		break;
	case PROP_SECRETS:
		/* Must make a deep copy of the hash table here... */
428
		g_hash_table_remove_all (priv->secrets);
429 430
		new_hash = g_value_get_boxed (value);
		if (new_hash)
431
			g_hash_table_foreach (new_hash, copy_hash, priv->secrets);
432
		break;
433 434 435 436 437 438 439 440 441 442 443
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
get_property (GObject *object, guint prop_id,
		    GValue *value, GParamSpec *pspec)
{
	NMSettingVPN *setting = NM_SETTING_VPN (object);
444
	NMSettingVPNPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
445 446 447

	switch (prop_id) {
	case PROP_SERVICE_TYPE:
448
		g_value_set_string (value, nm_setting_vpn_get_service_type (setting));
449 450
		break;
	case PROP_USER_NAME:
451
		g_value_set_string (value, nm_setting_vpn_get_user_name (setting));
452
		break;
453
	case PROP_DATA:
454
		g_value_set_boxed (value, priv->data);
455
		break;
456
	case PROP_SECRETS:
457
		g_value_set_boxed (value, priv->secrets);
458
		break;
459 460 461 462 463 464 465 466 467 468 469 470
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nm_setting_vpn_class_init (NMSettingVPNClass *setting_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
	NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);

471 472
	g_type_class_add_private (setting_class, sizeof (NMSettingVPNPrivate));

473 474 475 476 477
	/* virtual methods */
	object_class->set_property = set_property;
	object_class->get_property = get_property;
	object_class->finalize     = finalize;
	parent_class->verify       = verify;
478
	parent_class->update_one_secret = update_one_secret;
479 480

	/* Properties */
481 482 483 484 485
	/**
	 * NMSettingVPN:service-type:
	 *
	 * D-Bus service name of the VPN plugin that this setting uses to connect
	 * to its network.  i.e. org.freedesktop.NetworkManager.vpnc for the vpnc
486
	 * plugin.
487
	 **/
488 489 490 491
	g_object_class_install_property
		(object_class, PROP_SERVICE_TYPE,
		 g_param_spec_string (NM_SETTING_VPN_SERVICE_TYPE,
						  "Service type",
492 493 494 495
						  "D-Bus service name of the VPN plugin that this "
						  "setting uses to connect to its network.  i.e. "
						  "org.freedesktop.NetworkManager.vpnc for the vpnc "
						  "plugin.",
496 497 498
						  NULL,
						  G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE));

499 500 501 502 503 504 505 506
	/**
	 * NMSettinVPN:user-name:
	 *
	 * User name of the currently logged in user for connections provided by the
	 * user settings service.  This name is provided to the VPN plugin to use in
	 * lieu of a custom username provided by that VPN plugins specific
	 * configuration.  The VPN plugin itself decides which user name to use.
	 **/
507 508 509 510
	g_object_class_install_property
		(object_class, PROP_USER_NAME,
		 g_param_spec_string (NM_SETTING_VPN_USER_NAME,
						  "User name",
511 512 513 514 515 516
						  "User name of the currently logged in user for "
						  "connections provided by the user settings service.  "
						  "This name is provided to the VPN plugin to use in "
						  "lieu of a custom username provided by that VPN "
						  "plugins specific configuration.  The VPN plugin "
						  "itself decides which user name to use.",
517 518
						  NULL,
						  G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE));
519

520 521 522 523 524 525
	/**
	 * NMSettingVPN:data:
	 *
	 * Dictionary of key/value pairs of VPN plugin specific data.  Both keys
	 * and values must be strings.
	 **/
526 527
	g_object_class_install_property
		(object_class, PROP_DATA,
528
		 _nm_param_spec_specialized (NM_SETTING_VPN_DATA,
529
							   "Data",
530 531 532
							   "Dictionary of key/value pairs of VPN plugin "
							   "specific data.  Both keys and values must be "
							   "strings.",
533 534
							   DBUS_TYPE_G_MAP_OF_STRING,
							   G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE));
535

536 537 538 539 540 541
	/**
	 * NMSettingVPN:secrets:
	 *
	 * Dictionary of key/value pairs of VPN plugin specific secrets like
	 * passwords or private keys.  Both keys and values must be strings.
	 **/
542 543 544 545
	g_object_class_install_property
		(object_class, PROP_SECRETS,
		 _nm_param_spec_specialized (NM_SETTING_VPN_SECRETS,
							   "Secrets",
546 547 548
							   "Dictionary of key/value pairs of VPN plugin "
							   "specific secrets like passwords or private keys."
							   "  Both keys and values must be strings.",
549 550
							   DBUS_TYPE_G_MAP_OF_STRING,
							   G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE | NM_SETTING_PARAM_SECRET));
551
}
552