Disconnect when same SSID selected (#1)
[dhcpcd-ui] / src / dhcpcd-gtk / wpa.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009-2015 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <errno.h>
28
29 #include "dhcpcd-gtk.h"
30
31 static GtkWidget *wpa_dialog, *wpa_err;
32
33 static void
34 wpa_show_err(const char *title, const char *txt)
35 {
36
37         if (wpa_err)
38                 gtk_widget_destroy(wpa_err);
39         wpa_err = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
40             GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", txt);
41         gtk_window_set_title(GTK_WINDOW(wpa_err), title);
42         gtk_dialog_run(GTK_DIALOG(wpa_err));
43         gtk_widget_destroy(wpa_err);
44 }
45
46 static void
47 onEnter(_unused GtkWidget *widget, gpointer *data)
48 {
49
50         gtk_dialog_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT);
51 }
52
53 void
54 wpa_abort(void)
55 {
56
57         if (wpa_err) {
58                 gtk_widget_destroy(wpa_err);
59                 wpa_err = NULL;
60         }
61         if (wpa_dialog) {
62                 gtk_widget_destroy(wpa_dialog);
63                 wpa_dialog = NULL;
64         }
65 }
66
67 static bool
68 wpa_conf(int werr)
69 {
70         const char *errt;
71
72         switch (werr) {
73         case DHCPCD_WPA_SUCCESS:
74                 return true;
75         case DHCPCD_WPA_ERR_DISCONN:
76                 errt = _("Failed to disconnect.");
77                 break;
78         case DHCPCD_WPA_ERR_RECONF:
79                 errt = _("Faile to reconfigure.");
80                 break;
81         case DHCPCD_WPA_ERR_SET:
82                 errt = _("Failed to set key management.");
83                 break;
84         case DHCPCD_WPA_ERR_SET_PSK:
85                 errt = _("Failed to set password, probably too short.");
86                 break;
87         case DHCPCD_WPA_ERR_ENABLE:
88                 errt = _("Failed to enable the network.");
89                 break;
90         case DHCPCD_WPA_ERR_SELECT:
91                 errt = _("Failed to select the network.");
92                 break;
93         case DHCPCD_WPA_ERR_ASSOC:
94                 errt = _("Failed to start association.");
95                 break;
96         case DHCPCD_WPA_ERR_WRITE:
97                 errt =_("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
98                 break;
99         default:
100                 errt = strerror(errno);
101                 break;
102         }
103         wpa_show_err(_("Error enabling network"), errt);
104         return false;
105 }
106
107 bool
108 wpa_configure(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan)
109 {
110         DHCPCD_WI_SCAN s;
111         DHCPCD_IF *i;
112         GtkWidget *label, *psk, *vbox, *hbox;
113         const char *var;
114         int result;
115         bool retval;
116
117         /* Take a copy of scan incase it's destroyed by a scan update */
118         memcpy(&s, scan, sizeof(s));
119         s.next = NULL;
120
121         i = dhcpcd_wpa_if(wpa);
122         if (i == NULL)
123                 return false;
124
125         /* Disconnect if same interface selected */
126         if (dhcpcd_wi_associated(i, &s))
127                 return dhcpcd_wpa_disconnect(wpa);
128
129         if (!(s.flags & WSF_PSK))
130                 return wpa_conf(dhcpcd_wpa_configure(wpa, &s, NULL));
131
132         if (wpa_dialog)
133                 gtk_widget_destroy(wpa_dialog);
134
135         wpa_dialog = gtk_dialog_new_with_buttons(s.ssid,
136             NULL,
137             GTK_DIALOG_MODAL,
138             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
139             GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
140             NULL);
141         gtk_window_set_position(GTK_WINDOW(wpa_dialog), GTK_WIN_POS_MOUSE);
142         gtk_window_set_resizable(GTK_WINDOW(wpa_dialog), false);
143         gtk_window_set_icon_name(GTK_WINDOW(wpa_dialog),
144             "network-wireless-encrypted");
145         gtk_dialog_set_default_response(GTK_DIALOG(wpa_dialog),
146             GTK_RESPONSE_ACCEPT);
147         vbox = gtk_dialog_get_content_area(GTK_DIALOG(wpa_dialog));
148
149         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
150         label = gtk_label_new(_("Pre Shared Key:"));
151         gtk_box_pack_start(GTK_BOX(hbox), label, false, false, 5);
152         psk = gtk_entry_new();
153         gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
154         g_signal_connect(G_OBJECT(psk), "activate",
155             G_CALLBACK(onEnter), wpa_dialog);
156         gtk_box_pack_start(GTK_BOX(hbox), psk, true, true, 5);
157         gtk_container_add(GTK_CONTAINER(vbox), hbox);
158
159         gtk_widget_show_all(wpa_dialog);
160         result = gtk_dialog_run(GTK_DIALOG(wpa_dialog));
161
162         retval = false;
163         if (result == GTK_RESPONSE_ACCEPT) {
164                 var = gtk_entry_get_text(GTK_ENTRY(psk));
165                 if (*var == '\0')
166                         retval = wpa_conf(dhcpcd_wpa_select(wpa, &s));
167                 else
168                         retval = wpa_conf(dhcpcd_wpa_configure(wpa, &s, var));
169         }
170         if (wpa_dialog) {
171                 gtk_widget_destroy(wpa_dialog);
172                 wpa_dialog = NULL;
173         }
174         return retval;
175 }