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

« back to all changes in this revision

Viewing changes to panels/network/wireless-security/ws-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 <string.h>
 
26
 
 
27
#include "wireless-security.h"
 
28
#include "helpers.h"
 
29
#include "nma-ui-utils.h"
 
30
#include "utils.h"
 
31
 
 
32
struct _WirelessSecurityLEAP {
 
33
        WirelessSecurity parent;
 
34
        gboolean editing_connection;
 
35
        const char *password_flags_name;
 
36
};
 
37
 
 
38
static void
 
39
show_toggled_cb (GtkCheckButton *button, WirelessSecurity *sec)
 
40
{
 
41
        GtkWidget *widget;
 
42
        gboolean visible;
 
43
 
 
44
        widget = GTK_WIDGET (gtk_builder_get_object (sec->builder, "leap_password_entry"));
 
45
        g_assert (widget);
 
46
 
 
47
        visible = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
 
48
        gtk_entry_set_visibility (GTK_ENTRY (widget), visible);
 
49
}
 
50
 
 
51
static gboolean
 
52
validate (WirelessSecurity *parent, GError **error)
 
53
{
 
54
        GtkWidget *entry;
 
55
        const char *text;
 
56
        gboolean ret = TRUE;
 
57
 
 
58
        entry = GTK_WIDGET (gtk_builder_get_object (parent->builder, "leap_username_entry"));
 
59
        g_assert (entry);
 
60
        text = gtk_entry_get_text (GTK_ENTRY (entry));
 
61
        if (!text || !strlen (text)) {
 
62
                widget_set_error (entry);
 
63
                g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("missing leap-username"));
 
64
                ret = FALSE;
 
65
        } else
 
66
                widget_unset_error (entry);
 
67
 
 
68
        entry = GTK_WIDGET (gtk_builder_get_object (parent->builder, "leap_password_entry"));
 
69
        g_assert (entry);
 
70
        text = gtk_entry_get_text (GTK_ENTRY (entry));
 
71
        if (!text || !strlen (text)) {
 
72
                widget_set_error (entry);
 
73
                if (ret) {
 
74
                        g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("missing leap-password"));
 
75
                        ret = FALSE;
 
76
                }
 
77
        } else
 
78
                widget_unset_error (entry);
 
79
 
 
80
        return ret;
 
81
}
 
82
 
 
83
static void
 
84
add_to_size_group (WirelessSecurity *parent, GtkSizeGroup *group)
 
85
{
 
86
        GtkWidget *widget;
 
87
 
 
88
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "leap_username_label"));
 
89
        gtk_size_group_add_widget (group, widget);
 
90
 
 
91
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "leap_password_label"));
 
92
        gtk_size_group_add_widget (group, widget);
 
93
}
 
94
 
 
95
static void
 
96
fill_connection (WirelessSecurity *parent, NMConnection *connection)
 
97
{
 
98
        WirelessSecurityLEAP *sec = (WirelessSecurityLEAP *) parent;
 
99
        NMSettingWirelessSecurity *s_wireless_sec;
 
100
        NMSettingSecretFlags secret_flags;
 
101
        GtkWidget *widget, *passwd_entry;
 
102
        const char *leap_password = NULL, *leap_username = NULL;
 
103
 
 
104
        /* Blow away the old security setting by adding a clear one */
 
105
        s_wireless_sec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
 
106
        nm_connection_add_setting (connection, (NMSetting *) s_wireless_sec);
 
107
 
 
108
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "leap_username_entry"));
 
109
        leap_username = gtk_entry_get_text (GTK_ENTRY (widget));
 
110
 
 
111
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "leap_password_entry"));
 
112
        passwd_entry = widget;
 
113
        leap_password = gtk_entry_get_text (GTK_ENTRY (widget));
 
114
 
 
115
        g_object_set (s_wireless_sec,
 
116
                      NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x",
 
117
                      NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap",
 
118
                      NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, leap_username,
 
119
                      NM_SETTING_WIRELESS_SECURITY_LEAP_PASSWORD, leap_password,
 
120
                      NULL);
 
121
 
 
122
        /* Save LEAP_PASSWORD_FLAGS to the connection */
 
123
        secret_flags = nma_utils_menu_to_secret_flags (passwd_entry);
 
124
        nm_setting_set_secret_flags (NM_SETTING (s_wireless_sec), sec->password_flags_name,
 
125
                                     secret_flags, NULL);
 
126
 
 
127
        /* Update secret flags and popup when editing the connection */
 
128
        if (sec->editing_connection)
 
129
                nma_utils_update_password_storage (passwd_entry, secret_flags,
 
130
                                                   NM_SETTING (s_wireless_sec), sec->password_flags_name);
 
131
}
 
132
 
 
133
static void
 
134
update_secrets (WirelessSecurity *parent, NMConnection *connection)
 
135
{
 
136
        helper_fill_secret_entry (connection,
 
137
                                  parent->builder,
 
138
                                  "leap_password_entry",
 
139
                                  NM_TYPE_SETTING_WIRELESS_SECURITY,
 
140
                                  (HelperSecretFunc) nm_setting_wireless_security_get_leap_password);
 
141
}
 
142
 
 
143
WirelessSecurityLEAP *
 
144
ws_leap_new (NMConnection *connection, gboolean secrets_only)
 
145
{
 
146
        WirelessSecurity *parent;
 
147
        WirelessSecurityLEAP *sec;
 
148
        GtkWidget *widget;
 
149
        NMSettingWirelessSecurity *wsec = NULL;
 
150
 
 
151
        parent = wireless_security_init (sizeof (WirelessSecurityLEAP),
 
152
                                         validate,
 
153
                                         add_to_size_group,
 
154
                                         fill_connection,
 
155
                                         update_secrets,
 
156
                                         NULL,
 
157
                                         "/org/freedesktop/network-manager-applet/ws-leap.ui",
 
158
                                         "leap_notebook",
 
159
                                         "leap_username_entry");
 
160
        if (!parent)
 
161
                return NULL;
 
162
 
 
163
        if (connection) {
 
164
                wsec = nm_connection_get_setting_wireless_security (connection);
 
165
                if (wsec) {
 
166
                        const char *auth_alg;
 
167
 
 
168
                        /* Ignore if wireless security doesn't specify LEAP */
 
169
                        auth_alg = nm_setting_wireless_security_get_auth_alg (wsec);
 
170
                        if (!auth_alg || strcmp (auth_alg, "leap"))
 
171
                                wsec = NULL;
 
172
                }
 
173
        }
 
174
 
 
175
        parent->adhoc_compatible = FALSE;
 
176
        parent->hotspot_compatible = FALSE;
 
177
        sec = (WirelessSecurityLEAP *) parent;
 
178
        sec->editing_connection = secrets_only ? FALSE : TRUE;
 
179
        sec->password_flags_name = NM_SETTING_WIRELESS_SECURITY_LEAP_PASSWORD;
 
180
 
 
181
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "leap_password_entry"));
 
182
        g_assert (widget);
 
183
        g_signal_connect (G_OBJECT (widget), "changed",
 
184
                          (GCallback) wireless_security_changed_cb,
 
185
                          sec);
 
186
 
 
187
        /* Create password-storage popup menu for password entry under entry's secondary icon */
 
188
        nma_utils_setup_password_storage (widget, 0, (NMSetting *) wsec, sec->password_flags_name,
 
189
                                          FALSE, secrets_only);
 
190
 
 
191
        if (wsec)
 
192
                update_secrets (WIRELESS_SECURITY (sec), connection);
 
193
 
 
194
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "leap_username_entry"));
 
195
        g_assert (widget);
 
196
        g_signal_connect (G_OBJECT (widget), "changed",
 
197
                          (GCallback) wireless_security_changed_cb,
 
198
                          sec);
 
199
        if (wsec)
 
200
                gtk_entry_set_text (GTK_ENTRY (widget), nm_setting_wireless_security_get_leap_username (wsec));
 
201
 
 
202
        if (secrets_only)
 
203
                gtk_widget_hide (widget);
 
204
 
 
205
        widget = GTK_WIDGET (gtk_builder_get_object (parent->builder, "show_checkbutton_leap"));
 
206
        g_assert (widget);
 
207
        g_signal_connect (G_OBJECT (widget), "toggled",
 
208
                          (GCallback) show_toggled_cb,
 
209
                          sec);
 
210
 
 
211
        return sec;
 
212
}
 
213