1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* NetworkManager Connection editor -- Connection editor for NetworkManager
4
* Dan Williams <dcbw@redhat.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License along
17
* with this program; if not, write to the Free Software Foundation, Inc.,
18
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
* (C) Copyright 2008 - 2011 Red Hat, Inc.
28
#include <glib/gi18n.h>
30
#include <NetworkManager.h>
31
#include <nm-setting-connection.h>
32
#include <nm-setting-wireless.h>
33
#include <nm-setting-wireless-security.h>
34
#include <nm-setting-8021x.h>
37
#include "wireless-security.h"
38
#include "page-wireless.h"
39
#include "page-wireless-security.h"
40
#include "nm-connection-editor.h"
43
G_DEFINE_TYPE (CEPageWirelessSecurity, ce_page_wireless_security, CE_TYPE_PAGE)
46
#define S_NAME_COLUMN 0
47
#define S_SEC_COLUMN 1
48
#define S_ADHOC_VALID_COLUMN 2
51
find_proto (NMSettingWirelessSecurity *sec, const char *item)
55
for (i = 0; i < nm_setting_wireless_security_get_num_protos (sec); i++) {
56
if (!strcmp (item, nm_setting_wireless_security_get_proto (sec, i)))
62
static NMUtilsSecurityType
63
get_default_type_for_security (NMSettingWirelessSecurity *sec)
65
const char *key_mgmt, *auth_alg;
67
g_return_val_if_fail (sec != NULL, NMU_SEC_NONE);
69
key_mgmt = nm_setting_wireless_security_get_key_mgmt (sec);
70
auth_alg = nm_setting_wireless_security_get_auth_alg (sec);
73
if (!strcmp (key_mgmt, "none"))
74
return NMU_SEC_STATIC_WEP;
76
if (!strcmp (key_mgmt, "ieee8021x")) {
77
if (auth_alg && !strcmp (auth_alg, "leap"))
79
return NMU_SEC_DYNAMIC_WEP;
82
if ( !strcmp (key_mgmt, "wpa-none")
83
|| !strcmp (key_mgmt, "wpa-psk")) {
84
if (find_proto (sec, "rsn"))
85
return NMU_SEC_WPA2_PSK;
86
else if (find_proto (sec, "wpa"))
87
return NMU_SEC_WPA_PSK;
89
return NMU_SEC_WPA_PSK;
92
if (!strcmp (key_mgmt, "wpa-eap")) {
93
if (find_proto (sec, "rsn"))
94
return NMU_SEC_WPA2_ENTERPRISE;
95
else if (find_proto (sec, "wpa"))
96
return NMU_SEC_WPA_ENTERPRISE;
98
return NMU_SEC_WPA_ENTERPRISE;
101
return NMU_SEC_INVALID;
105
stuff_changed_cb (WirelessSecurity *sec, gpointer user_data)
107
ce_page_changed (CE_PAGE (user_data));
111
wsec_size_group_clear (GtkSizeGroup *group)
116
g_return_if_fail (group != NULL);
118
children = gtk_size_group_get_widgets (group);
119
for (iter = children; iter; iter = g_slist_next (iter))
120
gtk_size_group_remove_widget (group, GTK_WIDGET (iter->data));
123
static WirelessSecurity *
124
wireless_security_combo_get_active (CEPageWirelessSecurity *self)
128
WirelessSecurity *sec = NULL;
130
model = gtk_combo_box_get_model (self->security_combo);
131
gtk_combo_box_get_active_iter (self->security_combo, &iter);
132
gtk_tree_model_get (model, &iter, S_SEC_COLUMN, &sec, -1);
138
wireless_security_combo_changed (GtkComboBox *combo,
141
CEPageWirelessSecurity *self = CE_PAGE_WIRELESS_SECURITY (user_data);
143
GList *elt, *children;
144
WirelessSecurity *sec;
146
vbox = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (self)->builder, "wireless_security_vbox"));
149
wsec_size_group_clear (self->group);
151
/* Remove any previous wireless security widgets */
152
children = gtk_container_get_children (GTK_CONTAINER (vbox));
153
for (elt = children; elt; elt = g_list_next (elt))
154
gtk_container_remove (GTK_CONTAINER (vbox), GTK_WIDGET (elt->data));
156
sec = wireless_security_combo_get_active (self);
158
GtkWidget *sec_widget;
159
GtkWidget *widget, *parent;
161
sec_widget = wireless_security_get_widget (sec);
162
g_assert (sec_widget);
163
parent = gtk_widget_get_parent (sec_widget);
165
gtk_container_remove (GTK_CONTAINER (parent), sec_widget);
167
widget = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (self)->builder, "wireless_security_combo_label"));
168
gtk_size_group_add_widget (self->group, widget);
169
wireless_security_add_to_size_group (sec, self->group);
171
gtk_container_add (GTK_CONTAINER (vbox), sec_widget);
172
wireless_security_unref (sec);
175
ce_page_changed (CE_PAGE (self));
179
add_security_item (CEPageWirelessSecurity *self,
180
WirelessSecurity *sec,
184
gboolean adhoc_valid)
186
wireless_security_set_changed_notify (sec, stuff_changed_cb, self);
187
gtk_list_store_append (model, iter);
188
gtk_list_store_set (model, iter,
191
S_ADHOC_VALID_COLUMN, adhoc_valid,
193
wireless_security_unref (sec);
197
set_sensitive (GtkCellLayout *cell_layout,
198
GtkCellRenderer *cell,
199
GtkTreeModel *tree_model,
203
gboolean *adhoc = data;
204
gboolean sensitive = TRUE, adhoc_valid = TRUE;
206
gtk_tree_model_get (tree_model, iter, S_ADHOC_VALID_COLUMN, &adhoc_valid, -1);
207
if (*adhoc && !adhoc_valid)
210
g_object_set (cell, "sensitive", sensitive, NULL);
214
finish_setup (CEPageWirelessSecurity *self, gpointer unused, GError *error, gpointer user_data)
216
CEPage *parent = CE_PAGE (self);
217
NMSettingWireless *s_wireless;
218
NMSettingWirelessSecurity *s_wireless_sec;
219
NMConnection *connection = parent->connection;
220
gboolean is_adhoc = FALSE;
221
GtkListStore *sec_model;
224
const char *security;
225
guint32 dev_caps = 0;
226
NMUtilsSecurityType default_type = NMU_SEC_NONE;
230
GtkCellRenderer *renderer;
235
s_wireless = nm_connection_get_setting_wireless (connection);
236
g_assert (s_wireless);
238
combo = GTK_COMBO_BOX (GTK_WIDGET (gtk_builder_get_object (parent->builder, "wireless_security_combo")));
240
dev_caps = NM_WIFI_DEVICE_CAP_CIPHER_WEP40
241
| NM_WIFI_DEVICE_CAP_CIPHER_WEP104
242
| NM_WIFI_DEVICE_CAP_CIPHER_TKIP
243
| NM_WIFI_DEVICE_CAP_CIPHER_CCMP
244
| NM_WIFI_DEVICE_CAP_WPA
245
| NM_WIFI_DEVICE_CAP_RSN;
247
mode = nm_setting_wireless_get_mode (s_wireless);
248
if (mode && !strcmp (mode, "adhoc"))
250
self->adhoc = is_adhoc;
252
s_wireless_sec = nm_connection_get_setting_wireless_security (connection);
254
security = nm_setting_wireless_get_security (s_wireless);
255
if (!security || strcmp (security, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME))
256
s_wireless_sec = NULL;
258
default_type = get_default_type_for_security (s_wireless_sec);
260
sec_model = gtk_list_store_new (3, G_TYPE_STRING, wireless_security_get_g_type (), G_TYPE_BOOLEAN);
262
if (nm_utils_security_valid (NMU_SEC_NONE, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
263
gtk_list_store_append (sec_model, &iter);
264
gtk_list_store_set (sec_model, &iter,
265
S_NAME_COLUMN, C_("Wifi/wired security", "None"),
266
S_ADHOC_VALID_COLUMN, TRUE,
268
if (default_type == NMU_SEC_NONE)
273
if (nm_utils_security_valid (NMU_SEC_STATIC_WEP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
274
WirelessSecurityWEPKey *ws_wep;
275
NMWepKeyType wep_type = NM_WEP_KEY_TYPE_KEY;
277
if (default_type == NMU_SEC_STATIC_WEP) {
278
NMSettingWirelessSecurity *s_wsec;
280
s_wsec = nm_connection_get_setting_wireless_security (connection);
282
wep_type = nm_setting_wireless_security_get_wep_key_type (s_wsec);
283
if (wep_type == NM_WEP_KEY_TYPE_UNKNOWN)
284
wep_type = NM_WEP_KEY_TYPE_KEY;
287
ws_wep = ws_wep_key_new (connection, NM_WEP_KEY_TYPE_KEY, FALSE, FALSE);
289
add_security_item (self, WIRELESS_SECURITY (ws_wep), sec_model,
290
&iter, _("WEP 40/128-bit Key (Hex or ASCII)"),
292
if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_KEY))
297
ws_wep = ws_wep_key_new (connection, NM_WEP_KEY_TYPE_PASSPHRASE, FALSE, FALSE);
299
add_security_item (self, WIRELESS_SECURITY (ws_wep), sec_model,
300
&iter, _("WEP 128-bit Passphrase"), TRUE);
301
if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_PASSPHRASE))
307
if (nm_utils_security_valid (NMU_SEC_LEAP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
308
WirelessSecurityLEAP *ws_leap;
310
ws_leap = ws_leap_new (connection, FALSE);
312
add_security_item (self, WIRELESS_SECURITY (ws_leap), sec_model,
313
&iter, _("LEAP"), FALSE);
314
if ((active < 0) && (default_type == NMU_SEC_LEAP))
320
if (nm_utils_security_valid (NMU_SEC_DYNAMIC_WEP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
321
WirelessSecurityDynamicWEP *ws_dynamic_wep;
323
ws_dynamic_wep = ws_dynamic_wep_new (connection, TRUE, FALSE);
324
if (ws_dynamic_wep) {
325
add_security_item (self, WIRELESS_SECURITY (ws_dynamic_wep), sec_model,
326
&iter, _("Dynamic WEP (802.1x)"), FALSE);
327
if ((active < 0) && (default_type == NMU_SEC_DYNAMIC_WEP))
333
if ( nm_utils_security_valid (NMU_SEC_WPA_PSK, dev_caps, FALSE, is_adhoc, 0, 0, 0)
334
|| nm_utils_security_valid (NMU_SEC_WPA2_PSK, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
335
WirelessSecurityWPAPSK *ws_wpa_psk;
337
ws_wpa_psk = ws_wpa_psk_new (connection, FALSE);
339
add_security_item (self, WIRELESS_SECURITY (ws_wpa_psk), sec_model,
340
&iter, _("WPA & WPA2 Personal"), FALSE);
341
if ((active < 0) && ((default_type == NMU_SEC_WPA_PSK) || (default_type == NMU_SEC_WPA2_PSK)))
347
if ( nm_utils_security_valid (NMU_SEC_WPA_ENTERPRISE, dev_caps, FALSE, is_adhoc, 0, 0, 0)
348
|| nm_utils_security_valid (NMU_SEC_WPA2_ENTERPRISE, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
349
WirelessSecurityWPAEAP *ws_wpa_eap;
351
ws_wpa_eap = ws_wpa_eap_new (connection, TRUE, FALSE);
353
add_security_item (self, WIRELESS_SECURITY (ws_wpa_eap), sec_model,
354
&iter, _("WPA & WPA2 Enterprise"), FALSE);
355
if ((active < 0) && ((default_type == NMU_SEC_WPA_ENTERPRISE) || (default_type == NMU_SEC_WPA2_ENTERPRISE)))
361
gtk_combo_box_set_model (combo, GTK_TREE_MODEL (sec_model));
362
gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo));
364
renderer = gtk_cell_renderer_text_new ();
365
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
366
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, "text", S_NAME_COLUMN, NULL);
367
gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo), renderer, set_sensitive, &self->adhoc, NULL);
369
gtk_combo_box_set_active (combo, active < 0 ? 0 : (guint32) active);
370
g_object_unref (G_OBJECT (sec_model));
372
self->security_combo = combo;
374
wireless_security_combo_changed (combo, self);
375
g_signal_connect (combo, "changed",
376
G_CALLBACK (wireless_security_combo_changed),
381
ce_page_wireless_security_new (NMConnection *connection,
382
GtkWindow *parent_window,
384
const char **out_secrets_setting_name,
387
CEPageWirelessSecurity *self;
388
NMSettingWireless *s_wireless;
389
NMSettingWirelessSecurity *s_wsec = NULL;
390
NMUtilsSecurityType default_type = NMU_SEC_NONE;
391
const char *security;
393
s_wireless = nm_connection_get_setting_wireless (connection);
395
g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("Could not load WiFi security user interface; missing WiFi setting."));
399
self = CE_PAGE_WIRELESS_SECURITY (ce_page_new (CE_TYPE_PAGE_WIRELESS_SECURITY,
403
UIDIR "/ce-page-wireless-security.ui",
404
"WirelessSecurityPage",
405
_("Wireless Security")));
407
g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("Could not load WiFi security user interface."));
411
self->group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
413
s_wsec = nm_connection_get_setting_wireless_security (connection);
415
security = nm_setting_wireless_get_security (s_wireless);
416
if (!security || strcmp (security, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME))
419
default_type = get_default_type_for_security (s_wsec);
421
/* Get secrets if the connection is not 802.1x enabled */
422
if ( default_type == NMU_SEC_STATIC_WEP
423
|| default_type == NMU_SEC_LEAP
424
|| default_type == NMU_SEC_WPA_PSK
425
|| default_type == NMU_SEC_WPA2_PSK) {
426
*out_secrets_setting_name = NM_SETTING_WIRELESS_SECURITY_SETTING_NAME;
429
/* Or if it is 802.1x enabled */
430
if ( default_type == NMU_SEC_DYNAMIC_WEP
431
|| default_type == NMU_SEC_WPA_ENTERPRISE
432
|| default_type == NMU_SEC_WPA2_ENTERPRISE) {
433
*out_secrets_setting_name = NM_SETTING_802_1X_SETTING_NAME;
436
g_signal_connect (self, "initialized", G_CALLBACK (finish_setup), NULL);
438
return CE_PAGE (self);
442
ce_page_wireless_security_init (CEPageWirelessSecurity *self)
444
self->disposed = FALSE;
448
dispose (GObject *object)
450
CEPageWirelessSecurity *self = CE_PAGE_WIRELESS_SECURITY (object);
455
self->disposed = TRUE;
458
g_object_unref (self->group);
460
G_OBJECT_CLASS (ce_page_wireless_security_parent_class)->dispose (object);
464
validate (CEPage *page, NMConnection *connection, GError **error)
466
CEPageWirelessSecurity *self = CE_PAGE_WIRELESS_SECURITY (page);
467
NMSettingWireless *s_wireless;
468
WirelessSecurity *sec;
469
gboolean valid = FALSE;
472
s_wireless = nm_connection_get_setting_wireless (connection);
473
g_assert (s_wireless);
475
/* Kernel Ad-Hoc WPA support is busted; it creates open networks. Disable
476
* WPA when Ad-Hoc is selected. set_sensitive() will pick up self->adhoc
477
* and do the right thing.
479
mode = nm_setting_wireless_get_mode (s_wireless);
480
if (g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC) == 0)
485
sec = wireless_security_combo_get_active (self);
487
const GByteArray *ssid = nm_setting_wireless_get_ssid (s_wireless);
490
/* FIXME: get failed property and error out of wireless security objects */
491
valid = wireless_security_validate (sec, ssid);
493
wireless_security_fill_connection (sec, connection);
495
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "Invalid wireless security");
497
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "Missing SSID");
502
if (!wireless_security_adhoc_compatible (sec)) {
503
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "Security not compatible with Ad-Hoc mode");
508
wireless_security_unref (sec);
510
/* No security, unencrypted */
511
g_object_set (s_wireless, NM_SETTING_WIRELESS_SEC, NULL, NULL);
512
nm_connection_remove_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
513
nm_connection_remove_setting (connection, NM_TYPE_SETTING_802_1X);
521
nag_user (CEPage *page)
523
WirelessSecurity *sec;
524
GtkWidget *nag = NULL;
526
sec = wireless_security_combo_get_active (CE_PAGE_WIRELESS_SECURITY (page));
528
nag = wireless_security_nag_user (sec);
529
wireless_security_unref (sec);
535
ce_page_wireless_security_class_init (CEPageWirelessSecurityClass *wireless_security_class)
537
GObjectClass *object_class = G_OBJECT_CLASS (wireless_security_class);
538
CEPageClass *parent_class = CE_PAGE_CLASS (wireless_security_class);
540
/* virtual methods */
541
object_class->dispose = dispose;
543
parent_class->validate = validate;
544
parent_class->nag_user = nag_user;