1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2012 Red Hat, Inc
5
* Licensed under the GNU General Public License Version 2
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
#include <glib-object.h>
25
#include <glib/gi18n.h>
27
#include <NetworkManager.h>
29
#include "wireless-security.h"
30
#include "ce-page-security.h"
32
G_DEFINE_TYPE (CEPageSecurity, ce_page_security, CE_TYPE_PAGE)
41
find_proto (NMSettingWirelessSecurity *sec, const char *item)
45
for (i = 0; i < nm_setting_wireless_security_get_num_protos (sec); i++) {
46
if (!strcmp (item, nm_setting_wireless_security_get_proto (sec, i)))
52
static NMUtilsSecurityType
53
get_default_type_for_security (NMSettingWirelessSecurity *sec)
55
const char *key_mgmt, *auth_alg;
57
g_return_val_if_fail (sec != NULL, NMU_SEC_NONE);
59
key_mgmt = nm_setting_wireless_security_get_key_mgmt (sec);
60
auth_alg = nm_setting_wireless_security_get_auth_alg (sec);
63
if (!strcmp (key_mgmt, "none"))
64
return NMU_SEC_STATIC_WEP;
66
if (!strcmp (key_mgmt, "ieee8021x")) {
67
if (auth_alg && !strcmp (auth_alg, "leap"))
69
return NMU_SEC_DYNAMIC_WEP;
72
if ( !strcmp (key_mgmt, "wpa-none")
73
|| !strcmp (key_mgmt, "wpa-psk")) {
74
if (find_proto (sec, "rsn"))
75
return NMU_SEC_WPA2_PSK;
76
else if (find_proto (sec, "wpa"))
77
return NMU_SEC_WPA_PSK;
79
return NMU_SEC_WPA_PSK;
82
if (!strcmp (key_mgmt, "wpa-eap")) {
83
if (find_proto (sec, "rsn"))
84
return NMU_SEC_WPA2_ENTERPRISE;
85
else if (find_proto (sec, "wpa"))
86
return NMU_SEC_WPA_ENTERPRISE;
88
return NMU_SEC_WPA_ENTERPRISE;
91
return NMU_SEC_INVALID;
94
static WirelessSecurity *
95
security_combo_get_active (CEPageSecurity *page)
99
WirelessSecurity *sec = NULL;
101
model = gtk_combo_box_get_model (page->security_combo);
102
gtk_combo_box_get_active_iter (page->security_combo, &iter);
103
gtk_tree_model_get (model, &iter, S_SEC_COLUMN, &sec, -1);
109
wsec_size_group_clear (GtkSizeGroup *group)
114
g_return_if_fail (group != NULL);
116
children = gtk_size_group_get_widgets (group);
117
for (iter = children; iter; iter = g_slist_next (iter))
118
gtk_size_group_remove_widget (group, GTK_WIDGET (iter->data));
122
security_combo_changed (GtkComboBox *combo,
125
CEPageSecurity *page = CE_PAGE_SECURITY (user_data);
128
WirelessSecurity *sec;
130
wsec_size_group_clear (page->group);
132
vbox = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (page)->builder, "vbox"));
133
children = gtk_container_get_children (GTK_CONTAINER (vbox));
134
for (l = children; l; l = l->next) {
135
gtk_container_remove (GTK_CONTAINER (vbox), GTK_WIDGET (l->data));
138
sec = security_combo_get_active (page);
140
GtkWidget *sec_widget;
143
sec_widget = wireless_security_get_widget (sec);
144
g_assert (sec_widget);
145
parent = gtk_widget_get_parent (sec_widget);
147
gtk_container_remove (GTK_CONTAINER (parent), sec_widget);
149
gtk_size_group_add_widget (page->group, page->security_heading);
150
wireless_security_add_to_size_group (sec, page->group);
152
gtk_container_add (GTK_CONTAINER (vbox), sec_widget);
153
wireless_security_unref (sec);
156
ce_page_changed (CE_PAGE (page));
160
stuff_changed_cb (WirelessSecurity *sec, gpointer user_data)
162
ce_page_changed (CE_PAGE (user_data));
166
add_security_item (CEPageSecurity *page,
167
WirelessSecurity *sec,
171
gboolean adhoc_valid)
173
wireless_security_set_changed_notify (sec, stuff_changed_cb, page);
174
gtk_list_store_append (model, iter);
175
gtk_list_store_set (model, iter,
178
S_ADHOC_VALID_COLUMN, adhoc_valid,
180
wireless_security_unref (sec);
184
set_sensitive (GtkCellLayout *cell_layout,
185
GtkCellRenderer *cell,
186
GtkTreeModel *tree_model,
190
gboolean *adhoc = data;
191
gboolean sensitive = TRUE, adhoc_valid = TRUE;
193
gtk_tree_model_get (tree_model, iter, S_ADHOC_VALID_COLUMN, &adhoc_valid, -1);
194
if (*adhoc && !adhoc_valid)
197
g_object_set (cell, "sensitive", sensitive, NULL);
201
finish_setup (CEPageSecurity *page)
203
NMConnection *connection = CE_PAGE (page)->connection;
204
NMSettingWireless *sw;
205
NMSettingWirelessSecurity *sws;
206
gboolean is_adhoc = FALSE;
207
GtkListStore *sec_model;
210
guint32 dev_caps = 0;
211
NMUtilsSecurityType default_type = NMU_SEC_NONE;
215
GtkCellRenderer *renderer;
217
sw = nm_connection_get_setting_wireless (connection);
220
page->group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
222
page->security_heading = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (page)->builder, "heading_sec"));
223
page->security_combo = combo = GTK_COMBO_BOX (gtk_builder_get_object (CE_PAGE (page)->builder, "combo_sec"));
225
dev_caps = NM_WIFI_DEVICE_CAP_CIPHER_WEP40
226
| NM_WIFI_DEVICE_CAP_CIPHER_WEP104
227
| NM_WIFI_DEVICE_CAP_CIPHER_TKIP
228
| NM_WIFI_DEVICE_CAP_CIPHER_CCMP
229
| NM_WIFI_DEVICE_CAP_WPA
230
| NM_WIFI_DEVICE_CAP_RSN;
232
mode = nm_setting_wireless_get_mode (sw);
233
if (mode && !strcmp (mode, "adhoc"))
235
page->adhoc = is_adhoc;
237
sws = nm_connection_get_setting_wireless_security (connection);
239
default_type = get_default_type_for_security (sws);
241
sec_model = gtk_list_store_new (3, G_TYPE_STRING, WIRELESS_TYPE_SECURITY, G_TYPE_BOOLEAN);
243
if (nm_utils_security_valid (NMU_SEC_NONE, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
244
gtk_list_store_insert_with_values (sec_model, &iter, -1,
245
S_NAME_COLUMN, C_("Wi-Fi/Ethernet security", "None"),
246
S_ADHOC_VALID_COLUMN, TRUE,
248
if (default_type == NMU_SEC_NONE)
253
if (nm_utils_security_valid (NMU_SEC_STATIC_WEP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
254
WirelessSecurityWEPKey *ws_wep;
255
NMWepKeyType wep_type = NM_WEP_KEY_TYPE_KEY;
257
if (default_type == NMU_SEC_STATIC_WEP) {
258
sws = nm_connection_get_setting_wireless_security (connection);
260
wep_type = nm_setting_wireless_security_get_wep_key_type (sws);
261
if (wep_type == NM_WEP_KEY_TYPE_UNKNOWN)
262
wep_type = NM_WEP_KEY_TYPE_KEY;
265
ws_wep = ws_wep_key_new (connection, NM_WEP_KEY_TYPE_KEY, FALSE, FALSE);
267
add_security_item (page, WIRELESS_SECURITY (ws_wep), sec_model,
268
&iter, _("WEP 40/128-bit Key (Hex or ASCII)"),
270
if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_KEY))
275
ws_wep = ws_wep_key_new (connection, NM_WEP_KEY_TYPE_PASSPHRASE, FALSE, FALSE);
277
add_security_item (page, WIRELESS_SECURITY (ws_wep), sec_model,
278
&iter, _("WEP 128-bit Passphrase"), TRUE);
279
if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_PASSPHRASE))
285
if (nm_utils_security_valid (NMU_SEC_LEAP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
286
WirelessSecurityLEAP *ws_leap;
288
ws_leap = ws_leap_new (connection, FALSE);
290
add_security_item (page, WIRELESS_SECURITY (ws_leap), sec_model,
291
&iter, _("LEAP"), FALSE);
292
if ((active < 0) && (default_type == NMU_SEC_LEAP))
298
if (nm_utils_security_valid (NMU_SEC_DYNAMIC_WEP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
299
WirelessSecurityDynamicWEP *ws_dynamic_wep;
301
ws_dynamic_wep = ws_dynamic_wep_new (connection, TRUE, FALSE);
302
if (ws_dynamic_wep) {
303
add_security_item (page, WIRELESS_SECURITY (ws_dynamic_wep), sec_model,
304
&iter, _("Dynamic WEP (802.1x)"), FALSE);
305
if ((active < 0) && (default_type == NMU_SEC_DYNAMIC_WEP))
311
if (nm_utils_security_valid (NMU_SEC_WPA_PSK, dev_caps, FALSE, is_adhoc, 0, 0, 0) ||
312
nm_utils_security_valid (NMU_SEC_WPA2_PSK, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
313
WirelessSecurityWPAPSK *ws_wpa_psk;
315
ws_wpa_psk = ws_wpa_psk_new (connection, FALSE);
317
add_security_item (page, WIRELESS_SECURITY (ws_wpa_psk), sec_model,
318
&iter, _("WPA & WPA2 Personal"), FALSE);
319
if ((active < 0) && ((default_type == NMU_SEC_WPA_PSK) || (default_type == NMU_SEC_WPA2_PSK)))
325
if (nm_utils_security_valid (NMU_SEC_WPA_ENTERPRISE, dev_caps, FALSE, is_adhoc, 0, 0, 0) ||
326
nm_utils_security_valid (NMU_SEC_WPA2_ENTERPRISE, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
327
WirelessSecurityWPAEAP *ws_wpa_eap;
329
ws_wpa_eap = ws_wpa_eap_new (connection, TRUE, FALSE);
331
add_security_item (page, WIRELESS_SECURITY (ws_wpa_eap), sec_model,
332
&iter, _("WPA & WPA2 Enterprise"), FALSE);
333
if ((active < 0) && ((default_type == NMU_SEC_WPA_ENTERPRISE) || (default_type == NMU_SEC_WPA2_ENTERPRISE)))
339
gtk_combo_box_set_model (combo, GTK_TREE_MODEL (sec_model));
340
gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo));
342
renderer = gtk_cell_renderer_text_new ();
343
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
344
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, "text", S_NAME_COLUMN, NULL);
345
gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo), renderer, set_sensitive, &page->adhoc, NULL);
347
gtk_combo_box_set_active (combo, active < 0 ? 0 : (guint32) active);
348
g_object_unref (G_OBJECT (sec_model));
350
page->security_combo = combo;
352
security_combo_changed (combo, page);
353
g_signal_connect (combo, "changed",
354
G_CALLBACK (security_combo_changed), page);
358
validate (CEPage *page,
359
NMConnection *connection,
362
NMSettingWireless *sw;
363
WirelessSecurity *sec;
364
gboolean valid = FALSE;
367
sw = nm_connection_get_setting_wireless (connection);
369
mode = nm_setting_wireless_get_mode (sw);
370
if (g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC) == 0)
371
CE_PAGE_SECURITY (page)->adhoc = TRUE;
373
CE_PAGE_SECURITY (page)->adhoc = FALSE;
375
sec = security_combo_get_active (CE_PAGE_SECURITY (page));
377
GBytes *ssid = nm_setting_wireless_get_ssid (sw);
380
/* FIXME: get failed property and error out of wifi security objects */
381
valid = wireless_security_validate (sec, error);
383
wireless_security_fill_connection (sec, connection);
385
g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_SETTING, "Missing SSID");
389
if (CE_PAGE_SECURITY (page)->adhoc) {
390
if (!wireless_security_adhoc_compatible (sec)) {
392
g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_SETTING, "Security not compatible with Ad-Hoc mode");
397
wireless_security_unref (sec);
399
/* No security, unencrypted */
400
nm_connection_remove_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
401
nm_connection_remove_setting (connection, NM_TYPE_SETTING_802_1X);
409
ce_page_security_init (CEPageSecurity *page)
414
dispose (GObject *object)
416
CEPageSecurity *page = CE_PAGE_SECURITY (object);
418
g_clear_object (&page->group);
420
G_OBJECT_CLASS (ce_page_security_parent_class)->dispose (object);
424
ce_page_security_class_init (CEPageSecurityClass *class)
426
GObjectClass *object_class = G_OBJECT_CLASS (class);
427
CEPageClass *page_class = CE_PAGE_CLASS (class);
429
object_class->dispose = dispose;
430
page_class->validate = validate;
434
ce_page_security_new (NMConnection *connection,
437
CEPageSecurity *page;
438
NMUtilsSecurityType default_type = NMU_SEC_NONE;
439
NMSettingWirelessSecurity *sws;
441
page = CE_PAGE_SECURITY (ce_page_new (CE_TYPE_PAGE_SECURITY,
444
"/org/gnome/control-center/network/security-page.ui",
447
sws = nm_connection_get_setting_wireless_security (connection);
449
default_type = get_default_type_for_security (sws);
451
if (default_type == NMU_SEC_STATIC_WEP ||
452
default_type == NMU_SEC_LEAP ||
453
default_type == NMU_SEC_WPA_PSK ||
454
default_type == NMU_SEC_WPA2_PSK) {
455
CE_PAGE (page)->security_setting = NM_SETTING_WIRELESS_SECURITY_SETTING_NAME;
458
if (default_type == NMU_SEC_DYNAMIC_WEP ||
459
default_type == NMU_SEC_WPA_ENTERPRISE ||
460
default_type == NMU_SEC_WPA2_ENTERPRISE) {
461
CE_PAGE (page)->security_setting = NM_SETTING_802_1X_SETTING_NAME;
464
g_signal_connect (page, "initialized", G_CALLBACK (finish_setup), NULL);
466
return CE_PAGE (page);