page-wifi-security.c 16.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager Connection editor -- Connection editor for NetworkManager
 *
 * 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.
 *
16 17 18
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
 *
Dan Winship's avatar
Dan Winship committed
20
 * Copyright 2008 - 2014 Red Hat, Inc.
21 22
 */

23 24
#include "config.h"

25 26 27 28 29 30
#include <string.h>

#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include "wireless-security.h"
31 32
#include "page-wifi.h"
#include "page-wifi-security.h"
33 34
#include "nm-connection-editor.h"

35
G_DEFINE_TYPE (CEPageWifiSecurity, ce_page_wifi_security, CE_TYPE_PAGE)
36

37 38 39 40 41 42 43
#define CE_PAGE_WIFI_SECURITY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CE_TYPE_PAGE_WIFI_SECURITY, CEPageWifiSecurityPrivate))

typedef struct {
	GtkSizeGroup *group;
	GtkComboBox *security_combo;
	gboolean adhoc;
} CEPageWifiSecurityPrivate;
44

45 46 47
#define S_NAME_COLUMN   0
#define S_SEC_COLUMN    1
#define S_ADHOC_VALID_COLUMN  2
48

49 50 51 52 53 54 55 56 57 58 59 60
static gboolean
find_proto (NMSettingWirelessSecurity *sec, const char *item)
{
	guint32 i;

	for (i = 0; i < nm_setting_wireless_security_get_num_protos (sec); i++) {
		if (!strcmp (item, nm_setting_wireless_security_get_proto (sec, i)))
			return TRUE;
	}
	return FALSE;
}

61 62 63
static NMUtilsSecurityType
get_default_type_for_security (NMSettingWirelessSecurity *sec)
{
64 65
	const char *key_mgmt, *auth_alg;

66
	g_return_val_if_fail (sec != NULL, NMU_SEC_NONE);
67

68 69 70
	key_mgmt = nm_setting_wireless_security_get_key_mgmt (sec);
	auth_alg = nm_setting_wireless_security_get_auth_alg (sec);

71
	/* No IEEE 802.1x */
72
	if (!strcmp (key_mgmt, "none"))
73
		return NMU_SEC_STATIC_WEP;
74

75 76
	if (!strcmp (key_mgmt, "ieee8021x")) {
		if (auth_alg && !strcmp (auth_alg, "leap"))
77 78 79 80
			return NMU_SEC_LEAP;
		return NMU_SEC_DYNAMIC_WEP;
	}

81 82 83
	if (   !strcmp (key_mgmt, "wpa-none")
	    || !strcmp (key_mgmt, "wpa-psk")) {
		if (find_proto (sec, "rsn"))
84
			return NMU_SEC_WPA2_PSK;
85
		else if (find_proto (sec, "wpa"))
86 87 88 89 90
			return NMU_SEC_WPA_PSK;
		else
			return NMU_SEC_WPA_PSK;
	}

91 92
	if (!strcmp (key_mgmt, "wpa-eap")) {
		if (find_proto (sec, "rsn"))
93
			return NMU_SEC_WPA2_ENTERPRISE;
94
		else if (find_proto (sec, "wpa"))
95 96 97 98 99 100 101 102 103 104 105
			return NMU_SEC_WPA_ENTERPRISE;
		else
			return NMU_SEC_WPA_ENTERPRISE;
	}

	return NMU_SEC_INVALID;
}

static void
stuff_changed_cb (WirelessSecurity *sec, gpointer user_data)
{
106
	ce_page_changed (CE_PAGE (user_data));
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

static void
wsec_size_group_clear (GtkSizeGroup *group)
{
	GSList *children;
	GSList *iter;

	g_return_if_fail (group != NULL);

	children = gtk_size_group_get_widgets (group);
	for (iter = children; iter; iter = g_slist_next (iter))
		gtk_size_group_remove_widget (group, GTK_WIDGET (iter->data));
}

122
static WirelessSecurity *
123
wireless_security_combo_get_active (CEPageWifiSecurity *self)
124
{
125
	CEPageWifiSecurityPrivate *priv = CE_PAGE_WIFI_SECURITY_GET_PRIVATE (self);
126 127 128 129
	GtkTreeIter iter;
	GtkTreeModel *model;
	WirelessSecurity *sec = NULL;

130 131
	model = gtk_combo_box_get_model (priv->security_combo);
	gtk_combo_box_get_active_iter (priv->security_combo, &iter);
132 133 134 135 136
	gtk_tree_model_get (model, &iter, S_SEC_COLUMN, &sec, -1);

	return sec;
}

137
static void
138
wireless_security_combo_changed (GtkComboBox *combo,
139 140
                                 gpointer user_data)
{
141
	CEPageWifiSecurity *self = CE_PAGE_WIFI_SECURITY (user_data);
142
	CEPageWifiSecurityPrivate *priv = CE_PAGE_WIFI_SECURITY_GET_PRIVATE (self);
143 144
	GtkWidget *vbox;
	GList *elt, *children;
145
	WirelessSecurity *sec;
146

147
	vbox = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (self)->builder, "wifi_security_vbox"));
148 149
	g_assert (vbox);

150
	wsec_size_group_clear (priv->group);
151

152
	/* Remove any previous wifi security widgets */
153 154 155 156
	children = gtk_container_get_children (GTK_CONTAINER (vbox));
	for (elt = children; elt; elt = g_list_next (elt))
		gtk_container_remove (GTK_CONTAINER (vbox), GTK_WIDGET (elt->data));

157
	sec = wireless_security_combo_get_active (self);
158 159
	if (sec) {
		GtkWidget *sec_widget;
160
		GtkWidget *widget, *parent;
161 162 163

		sec_widget = wireless_security_get_widget (sec);
		g_assert (sec_widget);
164 165 166
		parent = gtk_widget_get_parent (sec_widget);
		if (parent)
			gtk_container_remove (GTK_CONTAINER (parent), sec_widget);
167

168
		widget = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (self)->builder, "wifi_security_combo_label"));
169 170
		gtk_size_group_add_widget (priv->group, widget);
		wireless_security_add_to_size_group (sec, priv->group);
171 172 173 174

		gtk_container_add (GTK_CONTAINER (vbox), sec_widget);
		wireless_security_unref (sec);
	}
175 176

	ce_page_changed (CE_PAGE (self));
177 178 179
}

static void
180
add_security_item (CEPageWifiSecurity *self,
181 182 183
                   WirelessSecurity *sec,
                   GtkListStore *model,
                   GtkTreeIter *iter,
184 185
                   const char *text,
                   gboolean adhoc_valid)
186
{
187
	wireless_security_set_changed_notify (sec, stuff_changed_cb, self);
188
	gtk_list_store_append (model, iter);
189 190 191 192 193
	gtk_list_store_set (model, iter,
	                    S_NAME_COLUMN, text,
	                    S_SEC_COLUMN, sec,
	                    S_ADHOC_VALID_COLUMN, adhoc_valid,
	                    -1);
194 195 196
	wireless_security_unref (sec);
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
static void
set_sensitive (GtkCellLayout *cell_layout,
               GtkCellRenderer *cell,
               GtkTreeModel *tree_model,
               GtkTreeIter *iter,
               gpointer data)
{
	gboolean *adhoc = data;
	gboolean sensitive = TRUE, adhoc_valid = TRUE;

	gtk_tree_model_get (tree_model, iter, S_ADHOC_VALID_COLUMN, &adhoc_valid, -1);
	if (*adhoc && !adhoc_valid)
		sensitive = FALSE;

	g_object_set (cell, "sensitive", sensitive, NULL);
}

214
static void
215
finish_setup (CEPageWifiSecurity *self, gpointer unused, GError *error, gpointer user_data)
216
{
217
	CEPage *parent = CE_PAGE (self);
218
	CEPageWifiSecurityPrivate *priv = CE_PAGE_WIFI_SECURITY_GET_PRIVATE (self);
219 220
	NMSettingWireless *s_wireless;
	NMSettingWirelessSecurity *s_wireless_sec;
221
	NMConnection *connection = parent->connection;
222 223 224
	gboolean is_adhoc = FALSE;
	GtkListStore *sec_model;
	GtkTreeIter iter;
225
	const char *mode;
226 227 228 229
	guint32 dev_caps = 0;
	NMUtilsSecurityType default_type = NMU_SEC_NONE;
	int active = -1;
	int item = 0;
230
	GtkComboBox *combo;
231
	GtkCellRenderer *renderer;
232

233 234
	if (error)
		return;
235

236
	s_wireless = nm_connection_get_setting_wireless (connection);
237
	g_assert (s_wireless);
238

239
	combo = GTK_COMBO_BOX (gtk_builder_get_object (parent->builder, "wifi_security_combo"));
240

241 242 243 244 245 246
	dev_caps =   NM_WIFI_DEVICE_CAP_CIPHER_WEP40
	           | NM_WIFI_DEVICE_CAP_CIPHER_WEP104
	           | NM_WIFI_DEVICE_CAP_CIPHER_TKIP
	           | NM_WIFI_DEVICE_CAP_CIPHER_CCMP
	           | NM_WIFI_DEVICE_CAP_WPA
	           | NM_WIFI_DEVICE_CAP_RSN;
247

248 249
	mode = nm_setting_wireless_get_mode (s_wireless);
	if (mode && !strcmp (mode, "adhoc"))
250
		is_adhoc = TRUE;
251
	priv->adhoc = is_adhoc;
252

253
	s_wireless_sec = nm_connection_get_setting_wireless_security (connection);
254

255
	if (s_wireless_sec)
256
		default_type = get_default_type_for_security (s_wireless_sec);
257

258
	sec_model = gtk_list_store_new (3, G_TYPE_STRING, wireless_security_get_type (), G_TYPE_BOOLEAN);
259 260 261 262

	if (nm_utils_security_valid (NMU_SEC_NONE, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
		gtk_list_store_append (sec_model, &iter);
		gtk_list_store_set (sec_model, &iter,
263
		                    S_NAME_COLUMN, C_("Wi-Fi/Ethernet security", "None"),
264
		                    S_ADHOC_VALID_COLUMN, TRUE,
265 266 267
		                    -1);
		if (default_type == NMU_SEC_NONE)
			active = item;
268
		item++;
269 270 271
	}

	if (nm_utils_security_valid (NMU_SEC_STATIC_WEP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
272
		WirelessSecurityWEPKey *ws_wep;
273
		NMWepKeyType wep_type = NM_WEP_KEY_TYPE_KEY;
274

275 276
		if (default_type == NMU_SEC_STATIC_WEP) {
			NMSettingWirelessSecurity *s_wsec;
277

278
			s_wsec = nm_connection_get_setting_wireless_security (connection);
279 280 281 282 283 284
			if (s_wsec)
				wep_type = nm_setting_wireless_security_get_wep_key_type (s_wsec);
			if (wep_type == NM_WEP_KEY_TYPE_UNKNOWN)
				wep_type = NM_WEP_KEY_TYPE_KEY;
		}

285
		ws_wep = ws_wep_key_new (connection, NM_WEP_KEY_TYPE_KEY, FALSE, FALSE);
286 287
		if (ws_wep) {
			add_security_item (self, WIRELESS_SECURITY (ws_wep), sec_model,
288 289
			                   &iter, _("WEP 40/128-bit Key (Hex or ASCII)"),
			                   TRUE);
290
			if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_KEY))
291
				active = item;
292
			item++;
293 294
		}

295
		ws_wep = ws_wep_key_new (connection, NM_WEP_KEY_TYPE_PASSPHRASE, FALSE, FALSE);
296 297
		if (ws_wep) {
			add_security_item (self, WIRELESS_SECURITY (ws_wep), sec_model,
298
			                   &iter, _("WEP 128-bit Passphrase"), TRUE);
299
			if ((active < 0) && (default_type == NMU_SEC_STATIC_WEP) && (wep_type == NM_WEP_KEY_TYPE_PASSPHRASE))
300
				active = item;
301
			item++;
302 303 304 305 306 307
		}
	}

	if (nm_utils_security_valid (NMU_SEC_LEAP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
		WirelessSecurityLEAP *ws_leap;

308
		ws_leap = ws_leap_new (connection, FALSE);
309
		if (ws_leap) {
310
			add_security_item (self, WIRELESS_SECURITY (ws_leap), sec_model,
311
			                   &iter, _("LEAP"), FALSE);
312
			if ((active < 0) && (default_type == NMU_SEC_LEAP))
313
				active = item;
314
			item++;
315 316 317 318 319 320
		}
	}

	if (nm_utils_security_valid (NMU_SEC_DYNAMIC_WEP, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
		WirelessSecurityDynamicWEP *ws_dynamic_wep;

321
		ws_dynamic_wep = ws_dynamic_wep_new (connection, TRUE, FALSE);
322
		if (ws_dynamic_wep) {
323
			add_security_item (self, WIRELESS_SECURITY (ws_dynamic_wep), sec_model,
324
			                   &iter, _("Dynamic WEP (802.1x)"), FALSE);
325
			if ((active < 0) && (default_type == NMU_SEC_DYNAMIC_WEP))
326
				active = item;
327
			item++;
328 329 330 331 332 333 334
		}
	}

	if (   nm_utils_security_valid (NMU_SEC_WPA_PSK, dev_caps, FALSE, is_adhoc, 0, 0, 0)
	    || nm_utils_security_valid (NMU_SEC_WPA2_PSK, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
		WirelessSecurityWPAPSK *ws_wpa_psk;

335
		ws_wpa_psk = ws_wpa_psk_new (connection, FALSE);
336
		if (ws_wpa_psk) {
337
			add_security_item (self, WIRELESS_SECURITY (ws_wpa_psk), sec_model,
338
			                   &iter, _("WPA & WPA2 Personal"), FALSE);
339
			if ((active < 0) && ((default_type == NMU_SEC_WPA_PSK) || (default_type == NMU_SEC_WPA2_PSK)))
340
				active = item;
341
			item++;
342 343 344 345 346 347 348
		}
	}

	if (   nm_utils_security_valid (NMU_SEC_WPA_ENTERPRISE, dev_caps, FALSE, is_adhoc, 0, 0, 0)
	    || nm_utils_security_valid (NMU_SEC_WPA2_ENTERPRISE, dev_caps, FALSE, is_adhoc, 0, 0, 0)) {
		WirelessSecurityWPAEAP *ws_wpa_eap;

349
		ws_wpa_eap = ws_wpa_eap_new (connection, TRUE, FALSE);
350
		if (ws_wpa_eap) {
351
			add_security_item (self, WIRELESS_SECURITY (ws_wpa_eap), sec_model,
352
			                   &iter, _("WPA & WPA2 Enterprise"), FALSE);
353
			if ((active < 0) && ((default_type == NMU_SEC_WPA_ENTERPRISE) || (default_type == NMU_SEC_WPA2_ENTERPRISE)))
354
				active = item;
355
			item++;
356 357 358
		}
	}

359
	gtk_combo_box_set_model (combo, GTK_TREE_MODEL (sec_model));
360 361 362 363 364
	gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo));

	renderer = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, "text", S_NAME_COLUMN, NULL);
365
	gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo), renderer, set_sensitive, &priv->adhoc, NULL);
366

367
	gtk_combo_box_set_active (combo, active < 0 ? 0 : (guint32) active);
368 369
	g_object_unref (G_OBJECT (sec_model));

370
	priv->security_combo = combo;
371

372
	wireless_security_combo_changed (combo, self);
373
	g_signal_connect (combo, "changed",
374
	                  G_CALLBACK (wireless_security_combo_changed),
375
	                  self);
376 377 378
}

CEPage *
379 380 381 382 383
ce_page_wifi_security_new (NMConnection *connection,
                           GtkWindow *parent_window,
                           NMClient *client,
                           const char **out_secrets_setting_name,
                           GError **error)
384
{
385
	CEPageWifiSecurity *self;
386 387 388 389
	NMSettingWireless *s_wireless;
	NMSettingWirelessSecurity *s_wsec = NULL;
	NMUtilsSecurityType default_type = NMU_SEC_NONE;

390
	s_wireless = nm_connection_get_setting_wireless (connection);
391
	if (!s_wireless) {
392
		g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("Could not load Wi-Fi security user interface; missing Wi-Fi setting."));
393 394 395
		return NULL;
	}

396 397 398 399 400 401 402
	self = CE_PAGE_WIFI_SECURITY (ce_page_new (CE_TYPE_PAGE_WIFI_SECURITY,
	                                           connection,
	                                           parent_window,
	                                           client,
	                                           UIDIR "/ce-page-wifi-security.ui",
	                                           "WifiSecurityPage",
	                                           _("Wi-Fi Security")));
403
	if (!self) {
404
		g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("Could not load Wi-Fi security user interface."));
405 406 407
		return NULL;
	}

408
	CE_PAGE_WIFI_SECURITY_GET_PRIVATE (self)->group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
409

410
	s_wsec = nm_connection_get_setting_wireless_security (connection);
411 412 413 414 415 416 417 418 419

	if (s_wsec)
		default_type = get_default_type_for_security (s_wsec);

	/* Get secrets if the connection is not 802.1x enabled */
	if (   default_type == NMU_SEC_STATIC_WEP
	    || default_type == NMU_SEC_LEAP
	    || default_type == NMU_SEC_WPA_PSK
	    || default_type == NMU_SEC_WPA2_PSK) {
420
		*out_secrets_setting_name = NM_SETTING_WIRELESS_SECURITY_SETTING_NAME;
421 422 423 424 425 426
	}

	/* Or if it is 802.1x enabled */
	if (   default_type == NMU_SEC_DYNAMIC_WEP
	    || default_type == NMU_SEC_WPA_ENTERPRISE
	    || default_type == NMU_SEC_WPA2_ENTERPRISE) {
427
		*out_secrets_setting_name = NM_SETTING_802_1X_SETTING_NAME;
428 429
	}

430
	g_signal_connect (self, "initialized", G_CALLBACK (finish_setup), NULL);
431

432
	return CE_PAGE (self);
433 434 435
}

static void
436
ce_page_wifi_security_init (CEPageWifiSecurity *self)
437 438 439 440 441 442
{
}

static void
dispose (GObject *object)
{
443
	CEPageWifiSecurityPrivate *priv = CE_PAGE_WIFI_SECURITY_GET_PRIVATE (object);
444

445
	g_clear_object (&priv->group);
446

447
	G_OBJECT_CLASS (ce_page_wifi_security_parent_class)->dispose (object);
448 449
}

450
static gboolean
451
ce_page_validate_v (CEPage *page, NMConnection *connection, GError **error)
452
{
453
	CEPageWifiSecurity *self = CE_PAGE_WIFI_SECURITY (page);
454
	CEPageWifiSecurityPrivate *priv = CE_PAGE_WIFI_SECURITY_GET_PRIVATE (self);
455
	NMSettingWireless *s_wireless;
456 457
	WirelessSecurity *sec;
	gboolean valid = FALSE;
458
	const char *mode;
459

460
	s_wireless = nm_connection_get_setting_wireless (connection);
461
	g_assert (s_wireless);
462

463
	/* Kernel Ad-Hoc WPA support is busted; it creates open networks.  Disable
464
	 * WPA when Ad-Hoc is selected.  set_sensitive() will pick up priv->adhoc
465 466 467 468
	 * and do the right thing.
	 */
	mode = nm_setting_wireless_get_mode (s_wireless);
	if (g_strcmp0 (mode, NM_SETTING_WIRELESS_MODE_ADHOC) == 0)
469
		priv->adhoc = TRUE;
470
	else
471
		priv->adhoc = FALSE;
472

473
	sec = wireless_security_combo_get_active (self);
474
	if (sec) {
Dan Winship's avatar
Dan Winship committed
475
		GBytes *ssid = nm_setting_wireless_get_ssid (s_wireless);
476 477

		if (ssid) {
478
			valid = wireless_security_validate (sec, error);
479 480
			if (valid)
				wireless_security_fill_connection (sec, connection);
481
		} else {
482
			g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, _("missing SSID"));
483 484 485
			valid = FALSE;
		}

486
		if (priv->adhoc) {
487
			if (!wireless_security_adhoc_compatible (sec)) {
488
				g_set_error (error, NMA_ERROR, NMA_ERROR_GENERIC, _("Security not compatible with Ad-Hoc mode"));
489 490 491
				valid = FALSE;
			}
		}
Dan Winship's avatar
Dan Winship committed
492 493

		wireless_security_unref (sec);
494
	} else {
495
		/* No security, unencrypted */
496 497
		nm_connection_remove_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
		nm_connection_remove_setting (connection, NM_TYPE_SETTING_802_1X);
498
		valid = TRUE;
499
	}
500 501

	return valid;
502 503
}

504
static void
505
ce_page_wifi_security_class_init (CEPageWifiSecurityClass *wireless_security_class)
506 507
{
	GObjectClass *object_class = G_OBJECT_CLASS (wireless_security_class);
508
	CEPageClass *parent_class = CE_PAGE_CLASS (wireless_security_class);
509

510 511
	g_type_class_add_private (object_class, sizeof (CEPageWifiSecurityPrivate));

512 513
	/* virtual methods */
	object_class->dispose = dispose;
514

515
	parent_class->ce_page_validate_v = ce_page_validate_v;
516
}