~khurshid-alam/unity-control-center/use-usd-schemas

« back to all changes in this revision

Viewing changes to panels/network/wireless-security/eap-method-leap.c

  • Committer: Sebastien Bacher
  • Author(s): Khurshid Alam
  • Date: 2019-05-17 07:34:33 UTC
  • mfrom: (12920.1.1 unity-control-center)
  • Revision ID: seb128@ubuntu.com-20190517073433-ch2kybdhhzpkmvq4
Network: Port to libnm 1.2 (lp: #1744619)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
 
2
/* NetworkManager Applet -- allow user control over networking
 
3
 *
 
4
 * Dan Williams <dcbw@redhat.com>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 *
 
20
 * Copyright 2007 - 2014 Red Hat, Inc.
 
21
 */
 
22
 
 
23
#include "nm-default.h"
 
24
 
 
25
#include <ctype.h>
 
26
#include <string.h>
 
27
 
 
28
#include "eap-method.h"
 
29
#include "wireless-security.h"
 
30
#include "helpers.h"
 
31
#include "nma-ui-utils.h"
 
32
#include "utils.h"
 
33
 
 
34
struct _EAPMethodLEAP {
 
35
        EAPMethod parent;
 
36
 
 
37
        WirelessSecurity *ws_parent;
 
38
 
 
39
        gboolean editing_connection;
 
40
 
 
41
        GtkEntry *username_entry;
 
42
        GtkEntry *password_entry;
 
43
        GtkToggleButton *show_password;
 
44
};
 
45
 
 
46
static void
 
47
show_toggled_cb (GtkToggleButton *button, EAPMethodLEAP *method)
 
48
{
 
49
        gboolean visible;
 
50
 
 
51
        visible = gtk_toggle_button_get_active (button);
 
52
        gtk_entry_set_visibility (method->password_entry, visible);
 
53
}
 
54
 
 
55
static gboolean
 
56
validate (EAPMethod *parent, GError **error)
 
57
{
 
58
        EAPMethodLEAP *method = (EAPMethodLEAP *)parent;
 
59
        const char *text;
 
60
        gboolean ret = TRUE;
 
61
 
 
62
        text = gtk_entry_get_text (method->username_entry);
 
63
        if (!text || !strlen (text)) {
 
64
                widget_set_error (GTK_WIDGET (method->username_entry));
 
65
                g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("missing EAP-LEAP username"));
 
66
                ret = FALSE;
 
67
        } else
 
68
                widget_unset_error (GTK_WIDGET (method->username_entry));
 
69
 
 
70
        text = gtk_entry_get_text (method->password_entry);
 
71
        if (!text || !strlen (text)) {
 
72
                widget_set_error (GTK_WIDGET (method->password_entry));
 
73
                if (ret) {
 
74
                        g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("missing EAP-LEAP password"));
 
75
                        ret = FALSE;
 
76
                }
 
77
        } else
 
78
                widget_unset_error (GTK_WIDGET (method->password_entry));
 
79
 
 
80
        return ret;
 
81
}
 
82
 
 
83
static void
 
84
add_to_size_group (EAPMethod *parent, GtkSizeGroup *group)
 
85
{
 
86
        GtkWidget *widget;
 
87
 
 
88
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "eap_leap_username_label"));
 
89
        g_assert (widget);
 
90
        gtk_size_group_add_widget (group, widget);
 
91
 
 
92
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "eap_leap_password_label"));
 
93
        g_assert (widget);
 
94
        gtk_size_group_add_widget (group, widget);
 
95
}
 
96
 
 
97
static void
 
98
fill_connection (EAPMethod *parent, NMConnection *connection, NMSettingSecretFlags flags)
 
99
{
 
100
        EAPMethodLEAP *method = (EAPMethodLEAP *) parent;
 
101
        NMSetting8021x *s_8021x;
 
102
        NMSettingSecretFlags secret_flags;
 
103
        GtkWidget *passwd_entry;
 
104
 
 
105
        s_8021x = nm_connection_get_setting_802_1x (connection);
 
106
        g_assert (s_8021x);
 
107
 
 
108
        nm_setting_802_1x_add_eap_method (s_8021x, "leap");
 
109
 
 
110
        g_object_set (s_8021x, NM_SETTING_802_1X_IDENTITY, gtk_entry_get_text (method->username_entry), NULL);
 
111
        g_object_set (s_8021x, NM_SETTING_802_1X_PASSWORD, gtk_entry_get_text (method->password_entry), NULL);
 
112
 
 
113
        passwd_entry = GTK_WIDGET (gtk_builder_get_object (parent->builder, "eap_leap_password_entry"));
 
114
        g_assert (passwd_entry);
 
115
 
 
116
        /* Save 802.1X password flags to the connection */
 
117
        secret_flags = nma_utils_menu_to_secret_flags (passwd_entry);
 
118
        nm_setting_set_secret_flags (NM_SETTING (s_8021x), parent->password_flags_name,
 
119
                                     secret_flags, NULL);
 
120
 
 
121
        /* Update secret flags and popup when editing the connection */
 
122
        if (method->editing_connection)
 
123
                nma_utils_update_password_storage (passwd_entry, secret_flags,
 
124
                                                   NM_SETTING (s_8021x), parent->password_flags_name);
 
125
}
 
126
 
 
127
static void
 
128
update_secrets (EAPMethod *parent, NMConnection *connection)
 
129
{
 
130
        helper_fill_secret_entry (connection,
 
131
                                  parent->builder,
 
132
                                  "eap_leap_password_entry",
 
133
                                  NM_TYPE_SETTING_802_1X,
 
134
                                  (HelperSecretFunc) nm_setting_802_1x_get_password);
 
135
}
 
136
 
 
137
/* Set the UI fields for user, password and show_password to the
 
138
 * values as provided by method->ws_parent. */
 
139
static void
 
140
set_userpass_ui (EAPMethodLEAP *method)
 
141
{
 
142
        if (method->ws_parent->username)
 
143
                gtk_entry_set_text (method->username_entry, method->ws_parent->username);
 
144
        else
 
145
                gtk_entry_set_text (method->username_entry, "");
 
146
 
 
147
        if (method->ws_parent->password && !method->ws_parent->always_ask)
 
148
                gtk_entry_set_text (method->password_entry, method->ws_parent->password);
 
149
        else
 
150
                gtk_entry_set_text (method->password_entry, "");
 
151
 
 
152
        gtk_toggle_button_set_active (method->show_password, method->ws_parent->show_password);
 
153
}
 
154
 
 
155
static void
 
156
widgets_realized (GtkWidget *widget, EAPMethodLEAP *method)
 
157
{
 
158
        set_userpass_ui (method);
 
159
}
 
160
 
 
161
static void
 
162
widgets_unrealized (GtkWidget *widget, EAPMethodLEAP *method)
 
163
{
 
164
        wireless_security_set_userpass (method->ws_parent,
 
165
                                        gtk_entry_get_text (method->username_entry),
 
166
                                        gtk_entry_get_text (method->password_entry),
 
167
                                        (gboolean) -1,
 
168
                                        gtk_toggle_button_get_active (method->show_password));
 
169
}
 
170
 
 
171
static void
 
172
destroy (EAPMethod *parent)
 
173
{
 
174
        EAPMethodLEAP *method = (EAPMethodLEAP *) parent;
 
175
        GtkWidget *widget;
 
176
 
 
177
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "eap_leap_notebook"));
 
178
        g_assert (widget);
 
179
        g_signal_handlers_disconnect_by_data (widget, method);
 
180
 
 
181
        g_signal_handlers_disconnect_by_data (method->username_entry, method->ws_parent);
 
182
        g_signal_handlers_disconnect_by_data (method->password_entry, method->ws_parent);
 
183
        g_signal_handlers_disconnect_by_data (method->show_password, method);
 
184
}
 
185
 
 
186
EAPMethodLEAP *
 
187
eap_method_leap_new (WirelessSecurity *ws_parent,
 
188
                     NMConnection *connection,
 
189
                     gboolean secrets_only)
 
190
{
 
191
        EAPMethodLEAP *method;
 
192
        EAPMethod *parent;
 
193
        GtkWidget *widget;
 
194
        NMSetting8021x *s_8021x = NULL;
 
195
 
 
196
        parent = eap_method_init (sizeof (EAPMethodLEAP),
 
197
                                  validate,
 
198
                                  add_to_size_group,
 
199
                                  fill_connection,
 
200
                                  update_secrets,
 
201
                                  destroy,
 
202
                                  "/org/freedesktop/network-manager-applet/eap-method-leap.ui",
 
203
                                  "eap_leap_notebook",
 
204
                                  "eap_leap_username_entry",
 
205
                                  FALSE);
 
206
        if (!parent)
 
207
                return NULL;
 
208
 
 
209
        parent->password_flags_name = NM_SETTING_802_1X_PASSWORD;
 
210
        method = (EAPMethodLEAP *) parent;
 
211
        method->editing_connection = secrets_only ? FALSE : TRUE;
 
212
        method->ws_parent = ws_parent;
 
213
 
 
214
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "eap_leap_notebook"));
 
215
        g_assert (widget);
 
216
        g_signal_connect (G_OBJECT (widget), "realize",
 
217
                          (GCallback) widgets_realized,
 
218
                          method);
 
219
        g_signal_connect (G_OBJECT (widget), "unrealize",
 
220
                          (GCallback) widgets_unrealized,
 
221
                          method);
 
222
 
 
223
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "eap_leap_username_entry"));
 
224
        g_assert (widget);
 
225
        method->username_entry = GTK_ENTRY (widget);
 
226
        g_signal_connect (G_OBJECT (widget), "changed",
 
227
                          (GCallback) wireless_security_changed_cb,
 
228
                          ws_parent);
 
229
 
 
230
        if (secrets_only)
 
231
                gtk_widget_set_sensitive (widget, FALSE);
 
232
 
 
233
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "eap_leap_password_entry"));
 
234
        g_assert (widget);
 
235
        method->password_entry = GTK_ENTRY (widget);
 
236
        g_signal_connect (G_OBJECT (widget), "changed",
 
237
                          (GCallback) wireless_security_changed_cb,
 
238
                          ws_parent);
 
239
 
 
240
        /* Create password-storage popup menu for password entry under entry's secondary icon */
 
241
        if (connection)
 
242
                s_8021x = nm_connection_get_setting_802_1x (connection);
 
243
        nma_utils_setup_password_storage (widget, 0, (NMSetting *) s_8021x, parent->password_flags_name,
 
244
                                          FALSE, secrets_only);
 
245
 
 
246
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "show_checkbutton_eapleap"));
 
247
        g_assert (widget);
 
248
        method->show_password = GTK_TOGGLE_BUTTON (widget);
 
249
        g_signal_connect (G_OBJECT (widget), "toggled",
 
250
                          (GCallback) show_toggled_cb,
 
251
                          parent);
 
252
 
 
253
        /* Initialize the UI fields with the security settings from method->ws_parent.
 
254
         * This will be done again when the widget gets realized. It must be done here as well,
 
255
         * because the outer dialog will ask to 'validate' the connection before the security tab
 
256
         * is shown/realized (to enable the 'Apply' button).
 
257
         * As 'validate' accesses the contents of the UI fields, they must be initialized now, even
 
258
         * if the widgets are not yet visible. */
 
259
        set_userpass_ui (method);
 
260
 
 
261
        return method;
 
262
}
 
263