summaryrefslogtreecommitdiff
path: root/src/connection-editor/page-8021x-security.c
blob: a0b171ea888d0bed5d62c28e13d7cb4d96cb68b4 (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
238
239
240
241
242
243
244
245
246
247
248
/* -*- 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.
 *
 * 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.
 *
 * Copyright 2008 - 2014 Red Hat, Inc.
 */

#include "nm-default.h"

#include <string.h>

#include "wireless-security.h"
#include "page-ethernet.h"
#include "page-8021x-security.h"
#include "nm-connection-editor.h"

G_DEFINE_TYPE (CEPage8021xSecurity, ce_page_8021x_security, CE_TYPE_PAGE)

#define CE_PAGE_8021X_SECURITY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CE_TYPE_PAGE_8021X_SECURITY, CEPage8021xSecurityPrivate))

typedef struct {
	GtkToggleButton *enabled;
	GtkWidget *security_widget;
	WirelessSecurity *security;

	gboolean initial_have_8021x;
} CEPage8021xSecurityPrivate;

static void
stuff_changed (WirelessSecurity *sec, gpointer user_data)
{
	ce_page_changed (CE_PAGE (user_data));
}

static void
enable_toggled (GtkToggleButton *button, gpointer user_data)
{
	CEPage8021xSecurityPrivate *priv = CE_PAGE_8021X_SECURITY_GET_PRIVATE (user_data);

	gtk_widget_set_sensitive (priv->security_widget, gtk_toggle_button_get_active (priv->enabled));
	ce_page_changed (CE_PAGE (user_data));
}

static void
finish_setup (CEPage8021xSecurity *self, gpointer unused, GError *error, gpointer user_data)
{
	CEPage *parent = CE_PAGE (self);
	CEPage8021xSecurityPrivate *priv = CE_PAGE_8021X_SECURITY_GET_PRIVATE (self);
	GtkWidget *parent_container;

	if (error)
		return;

	priv->security = (WirelessSecurity *) ws_wpa_eap_new (parent->connection, TRUE, FALSE);
	if (!priv->security) {
		g_warning ("Could not load 802.1X user interface.");
		return;
	}

	wireless_security_set_changed_notify (priv->security, stuff_changed, self);
	priv->security_widget = wireless_security_get_widget (priv->security);
	parent_container = gtk_widget_get_parent (priv->security_widget);
	if (parent_container)
		gtk_container_remove (GTK_CONTAINER (parent_container), priv->security_widget);

	gtk_toggle_button_set_active (priv->enabled, priv->initial_have_8021x);
	g_signal_connect (priv->enabled, "toggled", G_CALLBACK (enable_toggled), self);
	gtk_widget_set_sensitive (priv->security_widget, priv->initial_have_8021x);

	gtk_box_pack_start (GTK_BOX (parent->page), GTK_WIDGET (priv->enabled), FALSE, TRUE, 12);
	gtk_box_pack_start (GTK_BOX (parent->page), priv->security_widget, TRUE, TRUE, 0);
	gtk_widget_show_all (parent->page);
}

CEPage *
ce_page_8021x_security_new (NMConnectionEditor *editor,
                            NMConnection *connection,
                            GtkWindow *parent_window,
                            NMClient *client,
                            const char **out_secrets_setting_name,
                            GError **error)
{
	CEPage8021xSecurity *self;
	CEPage8021xSecurityPrivate *priv;
	CEPage *parent;

	self = CE_PAGE_8021X_SECURITY (ce_page_new (CE_TYPE_PAGE_8021X_SECURITY,
	                                            editor,
	                                            connection,
	                                            parent_window,
	                                            client,
	                                            NULL,
	                                            NULL,
	                                            _("802.1X Security")));
	if (!self) {
		g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("Could not load 802.1X Security user interface."));
		return NULL;
	}

	parent = CE_PAGE (self);
	priv = CE_PAGE_8021X_SECURITY_GET_PRIVATE (self);

	parent->page = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
	g_object_ref_sink (G_OBJECT (parent->page));
	gtk_container_set_border_width (GTK_CONTAINER (parent->page), 6);

	if (nm_connection_get_setting_802_1x (connection))
		priv->initial_have_8021x = TRUE;

	priv->enabled = GTK_TOGGLE_BUTTON (gtk_check_button_new_with_mnemonic (_("Use 802.1_X security for this connection")));

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

	if (priv->initial_have_8021x)
		*out_secrets_setting_name = NM_SETTING_802_1X_SETTING_NAME;

	return CE_PAGE (self);
}

static void
clear_widget_errors (GtkWidget *widget,
                     gpointer   user_data)
{
	if (GTK_IS_CONTAINER (widget)) {
		gtk_container_forall (GTK_CONTAINER (widget),
		                      clear_widget_errors,
		                      NULL);
	} else {
		widget_unset_error (widget);
	}
}

static gboolean
ce_page_validate_v (CEPage *page, NMConnection *connection, GError **error)
{
	CEPage8021xSecurityPrivate *priv = CE_PAGE_8021X_SECURITY_GET_PRIVATE (page);
	gboolean valid = TRUE;

	if (gtk_toggle_button_get_active (priv->enabled)) {
		NMConnection *tmp_connection;
		NMSetting *s_8021x;

		valid = wireless_security_validate (priv->security, error);
		if (valid) {
			NMSetting *s_con;

			/* Here's a nice hack to work around the fact that ws_802_1x_fill_connection needs wireless setting. */
			tmp_connection = nm_simple_connection_new ();
			nm_connection_add_setting (tmp_connection, nm_setting_wireless_new ());

			/* temp connection needs a 'connection' setting too, since most of
			 * the EAP methods need the UUID for CA cert ignore stuff.
			 */
			s_con = nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
			nm_connection_add_setting (tmp_connection, nm_setting_duplicate (s_con));

			ws_802_1x_fill_connection (priv->security, "wpa_eap_auth_combo", tmp_connection);

			s_8021x = nm_connection_get_setting (tmp_connection, NM_TYPE_SETTING_802_1X);
			nm_connection_add_setting (connection, NM_SETTING (g_object_ref (s_8021x)));

			/* Remove the 8021x setting to prevent the clearing of secrets when the
			 * simple-connection is destroyed.
			 */
			nm_connection_remove_setting (tmp_connection, NM_TYPE_SETTING_802_1X);
			g_object_unref (tmp_connection);
		}
	} else {
		gtk_container_forall (GTK_CONTAINER (priv->security_widget),
		                      clear_widget_errors,
		                      NULL);
		nm_connection_remove_setting (connection, NM_TYPE_SETTING_802_1X);
		valid = TRUE;
	}

	return valid;
}

static gboolean
inter_page_change (CEPage *page)
{
	CEPage8021xSecurityPrivate *priv = CE_PAGE_8021X_SECURITY_GET_PRIVATE (page);
	gpointer macsec_mode;

	if (nm_connection_editor_inter_page_get_value (page->editor,
	                                               INTER_PAGE_CHANGE_MACSEC_MODE,
	                                               &macsec_mode)) {
		gtk_toggle_button_set_active (priv->enabled,
		                              GPOINTER_TO_UINT (macsec_mode) == NM_SETTING_MACSEC_MODE_EAP);
		enable_toggled (priv->enabled, page);
	}

	return TRUE;
}

static void
ce_page_8021x_security_init (CEPage8021xSecurity *self)
{
}

static void
dispose (GObject *object)
{
	CEPage *parent = CE_PAGE (object);
	CEPage8021xSecurityPrivate *priv = CE_PAGE_8021X_SECURITY_GET_PRIVATE (object);

	if (priv->security_widget) {
		gtk_container_remove (GTK_CONTAINER (parent->page), priv->security_widget);
		priv->security_widget = NULL;
	}

	if (priv->security) {
		wireless_security_unref (priv->security);
		priv->security = NULL;
	}

	G_OBJECT_CLASS (ce_page_8021x_security_parent_class)->dispose (object);
}

static void
ce_page_8021x_security_class_init (CEPage8021xSecurityClass *security_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (security_class);
	CEPageClass *parent_class = CE_PAGE_CLASS (security_class);

	g_type_class_add_private (object_class, sizeof (CEPage8021xSecurityPrivate));

	/* virtual methods */
	object_class->dispose = dispose;

	parent_class->ce_page_validate_v = ce_page_validate_v;
	parent_class->inter_page_change = inter_page_change;
}