summaryrefslogtreecommitdiff
path: root/src/nm-ap-security-wep.c
blob: 30f5a0385ba3023c3528a753202bf7f17e46d63d (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
/* 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 <iwlib.h>

#include "nm-ap-security.h"
#include "nm-ap-security-wep.h"
#include "nm-ap-security-private.h"
#include "dbus-helpers.h"
#include "nm-device-802-11-wireless.h"
#include "nm-utils.h"
#include "nm-supplicant-config.h"

#define NM_AP_SECURITY_WEP_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_AP_SECURITY_WEP, NMAPSecurityWEPPrivate))

struct _NMAPSecurityWEPPrivate
{
	int		auth_algorithm;
};

static int get_auth_algorithm (NMAPSecurityWEP *security)
{
	return security->priv->auth_algorithm;
}

static void set_description (NMAPSecurityWEP *security)
{
	NMAPSecurity * parent = NM_AP_SECURITY (security);

	if (nm_ap_security_get_we_cipher (parent) == IW_AUTH_CIPHER_WEP40)
		nm_ap_security_set_description (parent, _("40-bit WEP"));
	else
		nm_ap_security_set_description (parent, _("104-bit WEP"));

}

NMAPSecurityWEP *
nm_ap_security_wep_new_deserialize (DBusMessageIter *iter, int we_cipher)
{
	NMAPSecurityWEP *	security = NULL;
	char *			key = NULL;
	int				key_len;
	int				auth_algorithm;

	g_return_val_if_fail (iter != NULL, NULL);
	g_return_val_if_fail ((we_cipher == IW_AUTH_CIPHER_WEP40) || (we_cipher == IW_AUTH_CIPHER_WEP104), NULL);

	if (!nmu_security_deserialize_wep (iter, &key, &key_len, &auth_algorithm))
		goto out;

	/* Success, build up our security object */
	security = g_object_new (NM_TYPE_AP_SECURITY_WEP, NULL);
	nm_ap_security_set_we_cipher (NM_AP_SECURITY (security), we_cipher);
	if (key)
		nm_ap_security_set_key (NM_AP_SECURITY (security), key, key_len);
	security->priv->auth_algorithm = auth_algorithm;

	set_description (security);

out:
	return security;
}

NMAPSecurityWEP *
nm_ap_security_wep_new (int we_cipher)
{
	NMAPSecurityWEP *security;

	g_return_val_if_fail ((we_cipher == IW_AUTH_CIPHER_WEP40) || (we_cipher == IW_AUTH_CIPHER_WEP104), NULL);

	security = g_object_new (NM_TYPE_AP_SECURITY_WEP, NULL);
	nm_ap_security_set_we_cipher (NM_AP_SECURITY (security), we_cipher);
	security->priv->auth_algorithm = IW_AUTH_ALG_OPEN_SYSTEM;

	set_description (security);

	return security;
}

static int 
real_serialize (NMAPSecurity *instance, DBusMessageIter *iter)
{
	NMAPSecurityWEP *	self = NM_AP_SECURITY_WEP (instance);

	if (!nmu_security_serialize_wep (iter,
			nm_ap_security_get_key (instance),
			self->priv->auth_algorithm))
		return -1;
	return 0;
}

static gboolean 
real_write_supplicant_config (NMAPSecurity *instance,
                              NMSupplicantConfig * config,
                              gboolean adhoc)
{
	gboolean     success = FALSE;
	const char * key = nm_ap_security_get_key (instance);
	char *       bin_key = NULL;

	/* WEP network setup */
	if (!nm_supplicant_config_add_option (config, "key_mgmt", "NONE", -1))
		goto out;

	/*
	 * If the user selected "Shared" (aka restricted) key, set it explicitly.  Otherwise,
	 * let wpa_supplicant default to the right thing, which is an open key.
	 */
	if (get_auth_algorithm (NM_AP_SECURITY_WEP (instance)) == IW_AUTH_ALG_SHARED_KEY) {
		if (!nm_supplicant_config_add_option (config, "auth_alg", "SHARED", -1))
			goto out;
	}

	bin_key = cipher_hexstr2bin(key, strlen (key));
	if (bin_key == NULL) {
		nm_warning ("Not enough memory to convert key.");
		goto out;
	}

	if (!nm_supplicant_config_add_option (config, "wep_key0", bin_key, -1)) {
		g_free (bin_key);
		goto out;
	}
	g_free (bin_key);

	if (!nm_supplicant_config_add_option (config, "wep_tx_keyidx", "0", -1))
		goto out;

	success = TRUE;

out:
	return success;
}

static guint32
real_get_default_capabilities (NMAPSecurity *instance)
{
	guint32	caps = NM_802_11_CAP_NONE;

	switch (nm_ap_security_get_we_cipher (instance))
	{
		case IW_AUTH_CIPHER_WEP40:
			caps |= (NM_802_11_CAP_PROTO_WEP | NM_802_11_CAP_CIPHER_WEP40);
			break;
		case IW_AUTH_CIPHER_WEP104:
			caps |= (NM_802_11_CAP_PROTO_WEP | NM_802_11_CAP_CIPHER_WEP104);
			break;
		default:
			break;
	}
	return caps;
}

static gboolean
real_get_authentication_required (NMAPSecurity *instance)
{
	/* WEP really requires authentication in Shared mode only */
	return (get_auth_algorithm (NM_AP_SECURITY_WEP (instance)) == IW_AUTH_ALG_SHARED_KEY);
}

static NMAPSecurity *
real_copy_constructor (NMAPSecurity *instance)
{
	NMAPSecurityWEP * dst = g_object_new (NM_TYPE_AP_SECURITY_WEP, NULL);
	NMAPSecurityWEP * self = NM_AP_SECURITY_WEP (instance);

	dst->priv->auth_algorithm = self->priv->auth_algorithm;
	nm_ap_security_copy_properties (NM_AP_SECURITY (self), NM_AP_SECURITY (dst));
	return NM_AP_SECURITY (dst);
}

static void
nm_ap_security_wep_init (NMAPSecurityWEP * self)
{
	self->priv = NM_AP_SECURITY_WEP_GET_PRIVATE (self);
	self->priv->auth_algorithm = IW_AUTH_ALG_OPEN_SYSTEM;
}

static void
nm_ap_security_wep_class_init (NMAPSecurityWEPClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	NMAPSecurityClass *par_class = NM_AP_SECURITY_CLASS (klass);

	par_class->copy_constructor_func = real_copy_constructor;
	par_class->serialize_func = real_serialize;
	par_class->write_supplicant_config_func = real_write_supplicant_config;
	par_class->get_default_capabilities_func = real_get_default_capabilities;
	par_class->get_authentication_required_func = real_get_authentication_required;

	g_type_class_add_private (object_class, sizeof (NMAPSecurityWEPPrivate));
}

GType
nm_ap_security_wep_get_type (void)
{
	static GType type = 0;
	if (type == 0) {
		static const GTypeInfo info = {
			sizeof (NMAPSecurityWEPClass),
			NULL,	/* base_init */
			NULL,	/* base_finalize */
			(GClassInitFunc) nm_ap_security_wep_class_init,
			NULL,	/* class_finalize */
			NULL,	/* class_data */
			sizeof (NMAPSecurityWEP),
			0,		/* n_preallocs */
			(GInstanceInitFunc) nm_ap_security_wep_init,
			NULL		/* value_table */
		};
		type = g_type_register_static (NM_TYPE_AP_SECURITY,
								 "NMAPSecurityWEP",
								 &info, 0);
	}
	return type;
}