summaryrefslogtreecommitdiff
path: root/src/nm-gconf-wso.c
blob: e3dd6667f8b6d7cb4a23097a49f83e10e2eae16a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
67
68
69
70
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
267
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
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
/* NetworkManager -- Network link manager
 *
 * Dan Williams <dcbw@redhat.com>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * (C) Copyright 2005 Red Hat, Inc.
 */

#include <glib.h>
#include <glib/gi18n.h>
#include <dbus/dbus.h>
#include <gconf/gconf-client.h>
#include <iwlib.h>

#include "applet.h"
#include "nm-gconf-wso.h"
#include "nm-gconf-wso-private.h"
#include "nm-gconf-wso-wep.h"
#include "nm-gconf-wso-wpa-eap.h"
#include "nm-gconf-wso-wpa-psk.h"
#include "nm-gconf-wso-leap.h"
#include "gconf-helpers.h"
#include "wireless-security-option.h"


#define NM_GCONF_WSO_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_GCONF_WSO, NMGConfWSOPrivate))

struct _NMGConfWSOPrivate
{
	int		we_cipher;
	char *	key;

	gboolean	dispose_has_run;
};

static NMGConfWSO *
nm_gconf_wso_new (int we_cipher)
{
	NMGConfWSO * security;

	security = g_object_new (NM_TYPE_GCONF_WSO, NULL);
	security->priv->we_cipher = we_cipher;
	security->priv->key = NULL;
	return security;
}


NMGConfWSO *
nm_gconf_wso_new_deserialize_dbus (DBusMessageIter *iter)
{
	NMGConfWSO * security = NULL;
	int we_cipher;

	g_return_val_if_fail (iter != NULL, NULL);
	/* We require the WE cipher (an INT32) first */
	g_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_INT32, NULL);

	/* Get and validate WE cipher */
	dbus_message_iter_get_basic (iter, &we_cipher);

	if (we_cipher == IW_AUTH_CIPHER_NONE)
		security = nm_gconf_wso_new (we_cipher);
	else
	{
		/* Advance to start of cipher-dependent options */
		if (!dbus_message_iter_next (iter))
			goto out;

		switch (we_cipher)
		{
			case IW_AUTH_CIPHER_WEP40:
			case IW_AUTH_CIPHER_WEP104:
				security = NM_GCONF_WSO (nm_gconf_wso_wep_new_deserialize_dbus (iter, we_cipher));
				break;

			case NM_AUTH_TYPE_WPA_PSK_AUTO:
			case IW_AUTH_CIPHER_TKIP:
			case IW_AUTH_CIPHER_CCMP:
				security = NM_GCONF_WSO (nm_gconf_wso_wpa_psk_new_deserialize_dbus (iter, we_cipher));
				break;

			case NM_AUTH_TYPE_WPA_EAP:
				security = NM_GCONF_WSO (nm_gconf_wso_wpa_eap_new_deserialize_dbus (iter, we_cipher));
				break;

			case NM_AUTH_TYPE_LEAP:
				security = NM_GCONF_WSO (nm_gconf_wso_leap_new_deserialize_dbus (iter, we_cipher));
				break;

			default:
				break;
		}
	}

out:
	return security;
}

NMGConfWSO *
nm_gconf_wso_new_deserialize_gconf (GConfClient *client,
                                    const char *network)
{
	NMGConfWSO * security = NULL;
	int we_cipher;

	g_return_val_if_fail (client != NULL, NULL);
	g_return_val_if_fail (network != NULL, NULL);
	if (!nm_gconf_get_int_helper (client,
							GCONF_PATH_WIRELESS_NETWORKS,
							"we_cipher",
							network,
							&we_cipher))
		goto out;

	if (we_cipher == IW_AUTH_CIPHER_NONE)
		security = nm_gconf_wso_new (we_cipher);
	else
	{
		switch (we_cipher)
		{
			case IW_AUTH_CIPHER_WEP40:
			case IW_AUTH_CIPHER_WEP104:
				security = NM_GCONF_WSO (nm_gconf_wso_wep_new_deserialize_gconf (client, network, we_cipher));
				break;

			case NM_AUTH_TYPE_WPA_PSK_AUTO:
			case IW_AUTH_CIPHER_TKIP:
			case IW_AUTH_CIPHER_CCMP:
				security = NM_GCONF_WSO (nm_gconf_wso_wpa_psk_new_deserialize_gconf (client, network, we_cipher));
				break;

			case NM_AUTH_TYPE_WPA_EAP:
				security = NM_GCONF_WSO (nm_gconf_wso_wpa_eap_new_deserialize_gconf (client, network, we_cipher));
				break;

			case NM_AUTH_TYPE_LEAP:
				security = NM_GCONF_WSO (nm_gconf_wso_leap_new_deserialize_gconf (client, network, we_cipher));
				break;

			default:
				break;
		}
	}

out:
	return security;
}

/* HACK: to convert the WirelessSecurityOption -> NMGConfWSO,
 * we serialize the WSO to a dbus message then deserialize
 * it into an NMGConfWSO.
 */
NMGConfWSO *
nm_gconf_wso_new_from_wso (WirelessSecurityOption *opt,
                           const char *ssid)
{
	DBusMessage *		message;
	DBusMessageIter	iter;
	NMGConfWSO *		gconf_wso = NULL;

	g_return_val_if_fail (opt != NULL, NULL);
	g_return_val_if_fail (ssid != NULL, NULL);

	message = dbus_message_new_method_call (NMI_DBUS_SERVICE, NMI_DBUS_PATH, NMI_DBUS_INTERFACE, "foobar");
	if (!wso_append_dbus_params (opt, ssid, message))
		goto out;

	dbus_message_iter_init (message, &iter);
	gconf_wso = nm_gconf_wso_new_deserialize_dbus (&iter);

out:
	dbus_message_unref (message);
	return gconf_wso;
}

void
nm_gconf_wso_set_we_cipher (NMGConfWSO *self,
                            int we_cipher)
{
	g_return_if_fail (self != NULL);

	/* Ensure the cipher is valid */
	g_return_if_fail (
		   (we_cipher == NM_AUTH_TYPE_WPA_PSK_AUTO)
		|| (we_cipher == NM_AUTH_TYPE_WPA_EAP)
		|| (we_cipher == NM_AUTH_TYPE_LEAP)
		|| (we_cipher == IW_AUTH_CIPHER_NONE)
		|| (we_cipher == IW_AUTH_CIPHER_WEP40)
		|| (we_cipher == IW_AUTH_CIPHER_WEP104)
		|| (we_cipher == IW_AUTH_CIPHER_TKIP)
		|| (we_cipher == IW_AUTH_CIPHER_CCMP));

	self->priv->we_cipher = we_cipher;
}

void
nm_gconf_wso_set_key (NMGConfWSO *self,
                      const char *key,
                      int key_len)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (key != NULL);
	g_return_if_fail (key_len > 0);

	if (self->priv->key)
		g_free (self->priv->key);
	self->priv->key = g_malloc0 (key_len + 1);
	memcpy (self->priv->key, key, key_len);
}

static gboolean 
real_serialize_dbus (NMGConfWSO *self,
                     DBusMessageIter *iter)
{
	/* Nothing to do */
	return TRUE;
}

static int 
real_serialize_gconf (NMGConfWSO *self,
                      GConfClient *client,
                      const char *network)
{
	/* Nothing to do */
	return TRUE;
}

int
nm_gconf_wso_get_we_cipher (NMGConfWSO *self)
{
	g_return_val_if_fail (self != NULL, -1);

	return self->priv->we_cipher;
}

const char *
nm_gconf_wso_get_key (NMGConfWSO *self)
{
	g_return_val_if_fail (self != NULL, NULL);

	return self->priv->key;
}

gboolean
nm_gconf_wso_serialize_dbus (NMGConfWSO *self,
                             DBusMessageIter *iter)
{
	dbus_int32_t	dbus_we_cipher;

	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (iter != NULL, FALSE);

	if (self->priv->dispose_has_run)
		return FALSE;

	/* First arg: WE cipher (INT32) */
	dbus_we_cipher = (dbus_int32_t) self->priv->we_cipher;
	dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &dbus_we_cipher);

	return NM_GCONF_WSO_GET_CLASS (self)->serialize_dbus_func (self, iter);
}

gboolean
nm_gconf_wso_serialize_gconf (NMGConfWSO *self,
                              GConfClient *client,
                              const char *network)
{
	char *		key;

	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (client != NULL, FALSE);
	g_return_val_if_fail (network != NULL, FALSE);

	if (self->priv->dispose_has_run)
		return FALSE;

	key = g_strdup_printf ("%s/%s/we_cipher", GCONF_PATH_WIRELESS_NETWORKS, network);
	gconf_client_set_int (client, key, self->priv->we_cipher, NULL);
	g_free (key);

	/* Encryption key doesn't get serialized since its stored in the keyring */

	return NM_GCONF_WSO_GET_CLASS (self)->serialize_gconf_func (self, client, network);
}

static void
nm_gconf_wso_init (NMGConfWSO * self)
{
	self->priv = NM_GCONF_WSO_GET_PRIVATE (self);
	self->priv->dispose_has_run = FALSE;
	self->priv->we_cipher = IW_AUTH_CIPHER_NONE;
	self->priv->key = NULL;
}

static void
nm_gconf_wso_dispose (GObject *object)
{
	NMGConfWSO *		self = (NMGConfWSO *) object;
	NMGConfWSOClass *	klass;
	GObjectClass *		parent_class;  

	if (self->priv->dispose_has_run)
		/* If dispose did already run, return. */
		return;

	/* Make sure dispose does not run twice. */
	self->priv->dispose_has_run = TRUE;

	/* 
	 * In dispose, you are supposed to free all types referenced from this
	 * object which might themselves hold a reference to self. Generally,
	 * the most simple solution is to unref all members on which you own a 
	 * reference.
	 */

	/* Chain up to the parent class */
	klass = NM_GCONF_WSO_CLASS (g_type_class_peek (NM_TYPE_GCONF_WSO));
	parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
	parent_class->dispose (object);
}

static void
nm_gconf_wso_finalize (GObject *object)
{
	NMGConfWSO *		self = (NMGConfWSO *) object;
	NMGConfWSOClass *	klass;
	GObjectClass *		parent_class;  

	/* Complete object destruction */
	g_free (self->priv->key);

	/* Chain up to the parent class */
	klass = NM_GCONF_WSO_CLASS (g_type_class_peek (NM_TYPE_GCONF_WSO));
	parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
	parent_class->finalize (object);
}


static void
nm_gconf_wso_class_init (NMGConfWSOClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = nm_gconf_wso_dispose;
	object_class->finalize = nm_gconf_wso_finalize;

	klass->serialize_dbus_func = real_serialize_dbus;
	klass->serialize_gconf_func = real_serialize_gconf;

	g_type_class_add_private (object_class, sizeof (NMGConfWSOPrivate));
}

GType
nm_gconf_wso_get_type (void)
{
	static GType type = 0;
	if (type == 0) {
		static const GTypeInfo info = {
			sizeof (NMGConfWSOClass),
			NULL,	/* base_init */
			NULL,	/* base_finalize */
			(GClassInitFunc) nm_gconf_wso_class_init,
			NULL,	/* class_finalize */
			NULL,	/* class_data */
			sizeof (NMGConfWSO),
			0,		/* n_preallocs */
			(GInstanceInitFunc) nm_gconf_wso_init,
			NULL		/* value_table */
		};
		type = g_type_register_static (G_TYPE_OBJECT,
								"NMGConfWSO",
								&info, 0);
	}
	return type;
}