page-8021x-security.c 6.72 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-ethernet.h"
#include "page-8021x-security.h"
33 34
#include "nm-connection-editor.h"

35
G_DEFINE_TYPE (CEPage8021xSecurity, ce_page_8021x_security, CE_TYPE_PAGE)
36

37
#define CE_PAGE_8021X_SECURITY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CE_TYPE_PAGE_8021X_SECURITY, CEPage8021xSecurityPrivate))
38 39 40 41 42 43

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

44
	gboolean initial_have_8021x;
45
} CEPage8021xSecurityPrivate;
46 47 48 49 50 51 52 53 54 55

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)
{
56
	CEPage8021xSecurityPrivate *priv = CE_PAGE_8021X_SECURITY_GET_PRIVATE (user_data);
57 58 59 60 61

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

62
static void
63
finish_setup (CEPage8021xSecurity *self, gpointer unused, GError *error, gpointer user_data)
64 65
{
	CEPage *parent = CE_PAGE (self);
66
	CEPage8021xSecurityPrivate *priv = CE_PAGE_8021X_SECURITY_GET_PRIVATE (self);
67
	GtkWidget *parent_container;
68 69 70 71

	if (error)
		return;

72
	priv->security = (WirelessSecurity *) ws_wpa_eap_new (parent->connection, TRUE, FALSE);
73
	if (!priv->security) {
74
		g_warning ("Could not load 802.1x user interface.");
75 76 77 78 79
		return;
	}

	wireless_security_set_changed_notify (priv->security, stuff_changed, self);
	priv->security_widget = wireless_security_get_widget (priv->security);
80 81 82
	parent_container = gtk_widget_get_parent (priv->security_widget);
	if (parent_container)
		gtk_container_remove (GTK_CONTAINER (parent_container), priv->security_widget);
83 84

	gtk_toggle_button_set_active (priv->enabled, priv->initial_have_8021x);
85
	g_signal_connect (priv->enabled, "toggled", G_CALLBACK (enable_toggled), self);
86 87 88 89 90 91 92 93
	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 *
94
ce_page_8021x_security_new (NMConnection *connection,
95
                            GtkWindow *parent_window,
96
                            NMClient *client,
97 98
                            const char **out_secrets_setting_name,
                            GError **error)
99
{
100 101
	CEPage8021xSecurity *self;
	CEPage8021xSecurityPrivate *priv;
102 103
	CEPage *parent;

104
	self = CE_PAGE_8021X_SECURITY (ce_page_new (CE_TYPE_PAGE_8021X_SECURITY,
105 106
	                                            connection,
	                                            parent_window,
107
	                                            client,
108 109 110 111
	                                            NULL,
	                                            NULL,
	                                            _("802.1x Security")));
	if (!self) {
112
		g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("Could not load 802.1x Security user interface."));
113 114
		return NULL;
	}
115 116

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

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

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

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

128
	g_signal_connect (self, "initialized", G_CALLBACK (finish_setup), NULL);
129 130 131

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

133
	return CE_PAGE (self);
134 135 136
}

static gboolean
137
ce_page_validate_v (CEPage *page, NMConnection *connection, GError **error)
138
{
139
	CEPage8021xSecurityPrivate *priv = CE_PAGE_8021X_SECURITY_GET_PRIVATE (page);
140 141 142 143 144 145
	gboolean valid = TRUE;

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

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

150
			/* Here's a nice hack to work around the fact that ws_802_1x_fill_connection needs wireless setting. */
Dan Winship's avatar
Dan Winship committed
151
			tmp_connection = nm_simple_connection_new ();
152
			nm_connection_add_setting (tmp_connection, nm_setting_wireless_new ());
153 154 155 156 157 158 159

			/* 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));

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

162 163
			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)));
164

165
			g_object_unref (tmp_connection);
166
		}
167
	} else {
168
		nm_connection_remove_setting (connection, NM_TYPE_SETTING_802_1X);
169
		valid = TRUE;
170 171 172
	}

	return valid;
173 174 175
}

static void
176
ce_page_8021x_security_init (CEPage8021xSecurity *self)
177 178 179 180 181 182
{
}

static void
dispose (GObject *object)
{
183
	CEPage8021xSecurityPrivate *priv = CE_PAGE_8021X_SECURITY_GET_PRIVATE (object);
184

185
	if (priv->security) {
186
		wireless_security_unref (priv->security);
187 188
		priv->security = NULL;
	}
189

190
	G_OBJECT_CLASS (ce_page_8021x_security_parent_class)->dispose (object);
191 192 193
}

static void
194
ce_page_8021x_security_class_init (CEPage8021xSecurityClass *security_class)
195
{
196 197
	GObjectClass *object_class = G_OBJECT_CLASS (security_class);
	CEPageClass *parent_class = CE_PAGE_CLASS (security_class);
198

199
	g_type_class_add_private (object_class, sizeof (CEPage8021xSecurityPrivate));
200 201 202 203

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

204
	parent_class->ce_page_validate_v = ce_page_validate_v;
205
}