nm-setting-vpn.c 7.36 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 24
/* -*- 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.
 */
25 26 27 28 29 30

#include <string.h>
#include <dbus/dbus-glib.h>
#include "nm-setting-vpn.h"
#include "nm-param-spec-specialized.h"
#include "nm-utils.h"
31
#include "nm-dbus-glib-types.h"
32

33 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
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;
}


67 68 69 70 71 72
G_DEFINE_TYPE (NMSettingVPN, nm_setting_vpn, NM_TYPE_SETTING)

enum {
	PROP_0,
	PROP_SERVICE_TYPE,
	PROP_USER_NAME,
73
	PROP_DATA,
74
	PROP_SECRETS,
75 76 77 78 79 80 81 82 83 84 85

	LAST_PROP
};

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

static gboolean
86
verify (NMSetting *setting, GSList *all_settings, GError **error)
87 88 89
{
	NMSettingVPN *self = NM_SETTING_VPN (setting);

90 91 92 93 94 95 96 97 98 99 100 101 102
	if (!self->service_type) {
		g_set_error (error,
		             NM_SETTING_VPN_ERROR,
		             NM_SETTING_VPN_ERROR_MISSING_PROPERTY,
		             NM_SETTING_VPN_SERVICE_TYPE);
		return FALSE;
	}

	if (!strlen (self->service_type)) {
		g_set_error (error,
		             NM_SETTING_VPN_ERROR,
		             NM_SETTING_VPN_ERROR_INVALID_PROPERTY,
		             NM_SETTING_VPN_SERVICE_TYPE);
103
		return FALSE;
104
	}
105 106

	/* default username can be NULL, but can't be zero-length */
107 108 109 110 111
	if (self->user_name && !strlen (self->user_name)) {
		g_set_error (error,
		             NM_SETTING_VPN_ERROR,
		             NM_SETTING_VPN_ERROR_INVALID_PROPERTY,
		             NM_SETTING_VPN_USER_NAME);
112
		return FALSE;
113
	}
114 115 116 117

	return TRUE;
}

118 119 120 121 122 123 124 125 126
static void
update_one_secret (NMSetting *setting, const char *key, GValue *value)
{
	NMSettingVPN *self = NM_SETTING_VPN (setting);

	g_return_if_fail (key != NULL);
	g_return_if_fail (value != NULL);
	g_return_if_fail (G_VALUE_HOLDS_STRING (value));

127 128 129 130 131 132 133 134 135 136 137
	g_hash_table_insert (self->secrets, g_strdup (key), g_value_dup_string (value));
}

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);
138 139
}

140 141 142
static void
nm_setting_vpn_init (NMSettingVPN *setting)
{
143
	g_object_set (setting, NM_SETTING_NAME, NM_SETTING_VPN_SETTING_NAME, NULL);
144 145

	setting->data = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
146
	setting->secrets = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, destroy_one_secret);
147 148 149 150 151 152 153 154 155
}

static void
finalize (GObject *object)
{
	NMSettingVPN *self = NM_SETTING_VPN (object);

	g_free (self->service_type);
	g_free (self->user_name);
156
	g_hash_table_destroy (self->data);
157
	g_hash_table_destroy (self->secrets);
158 159 160 161

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

162 163 164 165 166 167
static void
copy_hash (gpointer key, gpointer value, gpointer user_data)
{
	g_hash_table_insert ((GHashTable *) user_data, g_strdup (key), g_strdup (value));
}

168 169 170 171 172
static void
set_property (GObject *object, guint prop_id,
		    const GValue *value, GParamSpec *pspec)
{
	NMSettingVPN *setting = NM_SETTING_VPN (object);
173
	GHashTable *new_hash;
174 175 176 177 178 179 180 181 182 183

	switch (prop_id) {
	case PROP_SERVICE_TYPE:
		g_free (setting->service_type);
		setting->service_type = g_value_dup_string (value);
		break;
	case PROP_USER_NAME:
		g_free (setting->user_name);
		setting->user_name = g_value_dup_string (value);
		break;
184 185 186
	case PROP_DATA:
		/* Must make a deep copy of the hash table here... */
		g_hash_table_remove_all (setting->data);
187 188 189 190 191 192 193 194 195 196
		new_hash = g_value_get_boxed (value);
		if (new_hash)
			g_hash_table_foreach (new_hash, copy_hash, setting->data);
		break;
	case PROP_SECRETS:
		/* Must make a deep copy of the hash table here... */
		g_hash_table_remove_all (setting->secrets);
		new_hash = g_value_get_boxed (value);
		if (new_hash)
			g_hash_table_foreach (new_hash, copy_hash, setting->secrets);
197
		break;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	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);

	switch (prop_id) {
	case PROP_SERVICE_TYPE:
		g_value_set_string (value, setting->service_type);
		break;
	case PROP_USER_NAME:
		g_value_set_string (value, setting->user_name);
		break;
217 218 219
	case PROP_DATA:
		g_value_set_boxed (value, setting->data);
		break;
220 221 222
	case PROP_SECRETS:
		g_value_set_boxed (value, setting->secrets);
		break;
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	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);

	/* virtual methods */
	object_class->set_property = set_property;
	object_class->get_property = get_property;
	object_class->finalize     = finalize;
	parent_class->verify       = verify;
240
	parent_class->update_one_secret = update_one_secret;
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

	/* Properties */
	g_object_class_install_property
		(object_class, PROP_SERVICE_TYPE,
		 g_param_spec_string (NM_SETTING_VPN_SERVICE_TYPE,
						  "Service type",
						  "Service type",
						  NULL,
						  G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE));

	g_object_class_install_property
		(object_class, PROP_USER_NAME,
		 g_param_spec_string (NM_SETTING_VPN_USER_NAME,
						  "User name",
						  "User name",
						  NULL,
						  G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE));
258 259 260

	g_object_class_install_property
		(object_class, PROP_DATA,
261
		 _nm_param_spec_specialized (NM_SETTING_VPN_DATA,
262 263 264 265
							   "Data",
							   "VPN Service specific data",
							   DBUS_TYPE_G_MAP_OF_STRING,
							   G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE));
266 267 268 269 270 271 272 273

	g_object_class_install_property
		(object_class, PROP_SECRETS,
		 _nm_param_spec_specialized (NM_SETTING_VPN_SECRETS,
							   "Secrets",
							   "VPN Service specific secrets",
							   DBUS_TYPE_G_MAP_OF_STRING,
							   G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE | NM_SETTING_PARAM_SECRET));
274
}
275