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-wifi.h"
39
#include "page-wifi-security.h"
40
#include "nm-connection-editor.h"
43
G_DEFINE_TYPE (CEPageWifiSecurity, ce_page_wifi_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-psk")) {
83
if (find_proto (sec, "rsn"))
84
return NMU_SEC_WPA2_PSK;
85
else if (find_proto (sec, "wpa"))
86
return NMU_SEC_WPA_PSK;
88
return NMU_SEC_WPA_PSK;
91
if (!strcmp (key_mgmt, "wpa-eap")) {
92
if (find_proto (sec, "rsn"))
93
return NMU_SEC_WPA2_ENTERPRISE;
94
else if (find_proto (sec, "wpa"))
95
return NMU_SEC_WPA_ENTERPRISE;
97
return NMU_SEC_WPA_ENTERPRISE;
100
return NMU_SEC_INVALID;
104
stuff_changed_cb (WirelessSecurity *sec, gpointer user_data)
106
ce_page_changed (CE_PAGE (user_data));
110
wsec_size_group_clear (GtkSizeGroup *group)
115
g_return_if_fail (group != NULL);
117
children = gtk_size_group_get_widgets (group);
118
for (iter = children; iter; iter = g_slist_next (iter))
119
gtk_size_group_remove_widget (group, GTK_WIDGET (iter->data));
122
static WirelessSecurity *
123
wireless_security_combo_get_active (CEPageWifiSecurity *self)
127
WirelessSecurity *sec = NULL;
129
model = gtk_combo_box_get_model (self->security_combo);
130
gtk_combo_box_get_active_iter (self->security_combo, &iter);
131
gtk_tree_model_get (model, &iter, S_SEC_COLUMN, &sec, -1);
137
wireless_security_combo_changed (GtkComboBox *combo,
140
CEPageWifiSecurity *self = CE_PAGE_WIFI_SECURITY (user_data);
142
GList *elt, *children;
143
WirelessSecurity *sec;
145
vbox = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (self)->builder, "wifi_security_vbox"));
148
wsec_size_group_clear (self->group);
150
/* Remove any previous wifi security widgets */
151
children = gtk_container_get_children (GTK_CONTAINER (vbox));
152
for (elt = children; elt; elt = g_list_next (elt))
153
gtk_container_remove (GTK_CONTAINER (vbox), GTK_WIDGET (elt->data));
155
sec = wireless_security_combo_get_active (self);
157
GtkWidget *sec_widget;
158
GtkWidget *widget, *parent;
160
sec_widget = wireless_security_get_widget (sec);
161
g_assert (sec_widget);
162
parent = gtk_widget_get_parent (sec_widget);
164
gtk_container_remove (GTK_CONTAINER (parent), sec_widget);
166
widget = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (self)->builder, "wifi_security_combo_label"));
167
gtk_size_group_add_widget (self->group, widget);
168
wireless_security_add_to_size_group (sec, self->group);
170
gtk_container_add (GTK_CONTAINER (vbox), sec_widget);
171
wireless_security_unref (sec);
174
ce_page_changed (CE_PAGE (self));
178
add_security_item (CEPageWifiSecurity *self,
179
WirelessSecurity *sec,
183
gboolean adhoc_valid)
185
wireless_security_set_changed_notify (sec, stuff_changed_cb, self);
186
gtk_list_store_append (model, iter);
187
gtk_list_store_set (model, iter,
190
S_ADHOC_VALID_COLUMN, adhoc_valid,
192
wireless_security_unref (sec);
196
set_sensitive (GtkCellLayout *cell_layout,
197
GtkCellRenderer *cell,
198
GtkTreeModel *tree_model,
202
gboolean *adhoc = data;
203
gboolean sensitive = TRUE, adhoc_valid = TRUE;
205
gtk_tree_model_get (tree_model, iter, S_ADHOC_VALID_COLUMN, &adhoc_valid, -1);
206
if (*adhoc && !adhoc_valid)
209
g_object_set (cell, "sensitive", sensitive, NULL);
213
finish_setup (CEPageWifiSecurity *self, gpointer unused, GError *error, gpointer user_data)
215
CEPage *parent = CE_PAGE (self);
216
NMSettingWireless *s_wireless;
217
NMSettingWirelessSecurity *s_wireless_sec;
218
NMConnection *connection = parent->connection;
219
gboolean is_adhoc = FALSE;
220
GtkListStore *sec_model;
223
const char *security;
224
guint32 dev_caps = 0;
225
NMUtilsSecurityType default_type = NMU_SEC_NONE;
229
GtkCellRenderer *renderer;
234
s_wireless = nm_connection_get_setting_wireless (connection);
235
g_assert (s_wireless);
237
combo = GTK_COMBO_BOX (GTK_WIDGET (gtk_builder_get_object (parent->builder, "wifi_security_combo")));
239
dev_caps = NM_WIFI_DEVICE_CAP_CIPHER_WEP40
240
| NM_WIFI_DEVICE_CAP_CIPHER_WEP104
241
| NM_WIFI_DEVICE_CAP_CIPHER_TKIP
242
| NM_WIFI_DEVICE_CAP_CIPHER_CCMP
243
| NM_WIFI_DEVICE_CAP_WPA
244
| NM_WIFI_DEVICE_CAP_RSN;
246
mode = nm_setting_wireless_get_mode (s_wireless);
247
if (mode && !strcmp (mode, "adhoc"))
249
self->adhoc = is_adhoc;
251
s_wireless_sec = nm_connection_get_setting_wireless_security (connection);
253
security = nm_setting_wireless_get_security (s_wireless);
254
if (!security || strcmp (security, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME))
255
s_wireless_sec = NULL;
257
default_type = get_default_type_for_security (s_wireless_sec);
259
sec_model = gtk_list_store_new (3, G_TYPE_STRING, wireless_security_get_g_type (), G_TYPE_BOOLEAN);
261
if (nm_utils_security_valid (NMU_SEC_NONE, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
262
gtk_list_store_append (sec_model, &iter);
263
gtk_list_store_set (sec_model, &iter,
264
S_NAME_COLUMN, C_("Wi-Fi/Ethernet security", "None"),
265
S_ADHOC_VALID_COLUMN, TRUE,
267
if (default_type == NMU_SEC_NONE)
272
if (nm_utils_security_valid (NMU_SEC_STATIC_WEP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
273
WirelessSecurityWEPKey *ws_wep;
274
NMWepKeyType wep_type = NM_WEP_KEY_TYPE_KEY;
276
if (default_type == NMU_SEC_STATIC_WEP) {
277
NMSettingWirelessSecurity *s_wsec;
279
s_wsec = nm_connection_get_setting_wireless_security (connection);
281
wep_type = nm_setting_wireless_security_get_wep_key_type (s_wsec);
282
if (wep_type == NM_WEP_KEY_TYPE_UNKNOWN)
283
wep_type = NM_WEP_KEY_TYPE_KEY;
286
ws_wep = ws_wep_key_new (connection, NM_WEP_KEY_TYPE_KEY, FALSE, FALSE);
288
add_security_item (self, WIRELESS_SECURITY (ws_wep), sec_model,
289
&iter, _("WEP 40/128-bit Key (Hex or ASCII)"),
291
if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_KEY))
296
ws_wep = ws_wep_key_new (connection, NM_WEP_KEY_TYPE_PASSPHRASE, FALSE, FALSE);
298
add_security_item (self, WIRELESS_SECURITY (ws_wep), sec_model,
299
&iter, _("WEP 128-bit Passphrase"), TRUE);
300
if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_PASSPHRASE))
306
if (nm_utils_security_valid (NMU_SEC_LEAP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
307
WirelessSecurityLEAP *ws_leap;
309
ws_leap = ws_leap_new (connection, FALSE);
311
add_security_item (self, WIRELESS_SECURITY (ws_leap), sec_model,
312
&iter, _("LEAP"), FALSE);
313
if ((active < 0) && (default_type == NMU_SEC_LEAP))
319
if (nm_utils_security_valid (NMU_SEC_DYNAMIC_WEP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
320
WirelessSecurityDynamicWEP *ws_dynamic_wep;
322
ws_dynamic_wep = ws_dynamic_wep_new (connection, TRUE, FALSE);
323
if (ws_dynamic_wep) {
324
add_security_item (self, WIRELESS_SECURITY (ws_dynamic_wep), sec_model,
325
&iter, _("Dynamic WEP (802.1x)"), FALSE);
326
if ((active < 0) && (default_type == NMU_SEC_DYNAMIC_WEP))
332
if ( nm_utils_security_valid (NMU_SEC_WPA_PSK, dev_caps, FALSE, is_adhoc, 0, 0, 0)
333
|| nm_utils_security_valid (NMU_SEC_WPA2_PSK, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
334
WirelessSecurityWPAPSK *ws_wpa_psk;
336
ws_wpa_psk = ws_wpa_psk_new (connection, FALSE);
338
add_security_item (self, WIRELESS_SECURITY (ws_wpa_psk), sec_model,
339
&iter, _("WPA & WPA2 Personal"), TRUE);
340
if ((active < 0) && ((default_type == NMU_SEC_WPA_PSK) || (default_type == NMU_SEC_WPA2_PSK)))
346
if ( nm_utils_security_valid (NMU_SEC_WPA_ENTERPRISE, dev_caps, FALSE, is_adhoc, 0, 0, 0)
347
|| nm_utils_security_valid (NMU_SEC_WPA2_ENTERPRISE, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
348
WirelessSecurityWPAEAP *ws_wpa_eap;
350
ws_wpa_eap = ws_wpa_eap_new (connection, TRUE, FALSE);
352
add_security_item (self, WIRELESS_SECURITY (ws_wpa_eap), sec_model,
353
&iter, _("WPA & WPA2 Enterprise"), FALSE);
354
if ((active < 0) && ((default_type == NMU_SEC_WPA_ENTERPRISE) || (default_type == NMU_SEC_WPA2_ENTERPRISE)))
360
gtk_combo_box_set_model (combo, GTK_TREE_MODEL (sec_model));
361
gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo));
363
renderer = gtk_cell_renderer_text_new ();
364
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
365
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, "text", S_NAME_COLUMN, NULL);
366
gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo), renderer, set_sensitive, &self->adhoc, NULL);
368
gtk_combo_box_set_active (combo, active < 0 ? 0 : (guint32) active);
369
g_object_unref (G_OBJECT (sec_model));
371
self->security_combo = combo;
373
wireless_security_combo_changed (combo, self);
374
g_signal_connect (combo, "changed",
375
G_CALLBACK (wireless_security_combo_changed),
380
ce_page_wifi_security_new (NMConnection *connection,
381
GtkWindow *parent_window,
383
NMRemoteSettings *settings,
384
const char **out_secrets_setting_name,
387
CEPageWifiSecurity *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 Wi-Fi security user interface; missing Wi-Fi setting."));
399
self = CE_PAGE_WIFI_SECURITY (ce_page_new (CE_TYPE_PAGE_WIFI_SECURITY,
404
UIDIR "/ce-page-wifi-security.ui",
406
_("Wi-Fi Security")));
408
g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("Could not load Wi-Fi security user interface."));
412
self->group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
414
s_wsec = nm_connection_get_setting_wireless_security (connection);
416
security = nm_setting_wireless_get_security (s_wireless);
417
if (!security || strcmp (security, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME))
420
default_type = get_default_type_for_security (s_wsec);
422
/* Get secrets if the connection is not 802.1x enabled */
423
if ( default_type == NMU_SEC_STATIC_WEP
424
|| default_type == NMU_SEC_LEAP
425
|| default_type == NMU_SEC_WPA_PSK
426
|| default_type == NMU_SEC_WPA2_PSK) {
427
*out_secrets_setting_name = NM_SETTING_WIRELESS_SECURITY_SETTING_NAME;
430
/* Or if it is 802.1x enabled */
431
if ( default_type == NMU_SEC_DYNAMIC_WEP
432
|| default_type == NMU_SEC_WPA_ENTERPRISE
433
|| default_type == NMU_SEC_WPA2_ENTERPRISE) {
434
*out_secrets_setting_name = NM_SETTING_802_1X_SETTING_NAME;
437
g_signal_connect (self, "initialized", G_CALLBACK (finish_setup), NULL);
439
return CE_PAGE (self);
443
ce_page_wifi_security_init (CEPageWifiSecurity *self)
445
self->disposed = FALSE;
449
dispose (GObject *object)
451
CEPageWifiSecurity *self = CE_PAGE_WIFI_SECURITY (object);
456
self->disposed = TRUE;
459
g_object_unref (self->group);
461
G_OBJECT_CLASS (ce_page_wifi_security_parent_class)->dispose (object);
465
validate (CEPage *page, NMConnection *connection, GError **error)
467
CEPageWifiSecurity *self = CE_PAGE_WIFI_SECURITY (page);
468
NMSettingWireless *s_wireless;
469
WirelessSecurity *sec;
470
gboolean valid = FALSE;
473
s_wireless = nm_connection_get_setting_wireless (connection);
474
g_assert (s_wireless);
476
mode = nm_setting_wireless_get_mode (s_wireless);
477
if (g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC) == 0)
482
sec = wireless_security_combo_get_active (self);
484
const GByteArray *ssid = nm_setting_wireless_get_ssid (s_wireless);
487
/* FIXME: get failed property and error out of wifi security objects */
488
valid = wireless_security_validate (sec, ssid);
490
wireless_security_fill_connection (sec, connection);
492
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "Invalid Wi-Fi security");
494
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "Missing SSID");
499
if (!wireless_security_adhoc_compatible (sec)) {
500
g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, "Security not compatible with Ad-Hoc mode");
505
wireless_security_unref (sec);
507
/* No security, unencrypted */
508
g_object_set (s_wireless, NM_SETTING_WIRELESS_SEC, NULL, NULL);
509
nm_connection_remove_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
510
nm_connection_remove_setting (connection, NM_TYPE_SETTING_802_1X);
518
nag_user (CEPage *page)
520
WirelessSecurity *sec;
521
GtkWidget *nag = NULL;
523
sec = wireless_security_combo_get_active (CE_PAGE_WIFI_SECURITY (page));
525
nag = wireless_security_nag_user (sec);
526
wireless_security_unref (sec);
532
ce_page_wifi_security_class_init (CEPageWifiSecurityClass *wireless_security_class)
534
GObjectClass *object_class = G_OBJECT_CLASS (wireless_security_class);
535
CEPageClass *parent_class = CE_PAGE_CLASS (wireless_security_class);
537
/* virtual methods */
538
object_class->dispose = dispose;
540
parent_class->validate = validate;
541
parent_class->nag_user = nag_user;