When connecting to a SSID, reload the configuration, save it and then
[dhcpcd-ui] / src / dhcpcd-gtk / wpa.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009-2014 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(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan, const char *psk)
69 {
70         const char *errt;
71
72         switch (dhcpcd_wpa_configure_psk(wpa, scan, psk)) {
73         case DHCPCD_WPA_SUCCESS:
74                 return true;
75         case DHCPCD_WPA_ERR_SET:
76                 errt = _("Failed to set key management.");
77                 break;
78         case DHCPCD_WPA_ERR_SET_PSK:
79                 errt = _("Failed to set password, probably too short.");
80                 break;
81         case DHCPCD_WPA_ERR_ENABLE:
82                 errt = _("Failed to enable the network.");
83                 break;
84         case DHCPCD_WPA_ERR_SELECT:
85                 errt = _("Failed to select the network.");
86                 break;
87         case DHCPCD_WPA_ERR_ASSOC:
88                 errt = _("Failed to start association.");
89                 break;
90         case DHCPCD_WPA_ERR_WRITE:
91                 errt =_("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
92                 break;
93         default:
94                 errt = strerror(errno);
95                 break;
96         }
97         wpa_show_err(_("Error enabling network"), errt);
98         return false;
99 }
100
101 bool
102 wpa_configure(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan)
103 {
104         DHCPCD_WI_SCAN s;
105         GtkWidget *label, *psk, *vbox, *hbox;
106         const char *var;
107         int result;
108         bool retval;
109
110         /* Take a copy of scan incase it's destroyed by a scan update */
111         memcpy(&s, scan, sizeof(s));
112         s.next = NULL;
113
114         if (!(s.flags & WSF_PSK))
115                 return wpa_conf(wpa, &s, NULL);
116
117         if (wpa_dialog)
118                 gtk_widget_destroy(wpa_dialog);
119
120         wpa_dialog = gtk_dialog_new_with_buttons(s.ssid,
121             NULL,
122             GTK_DIALOG_MODAL,
123             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
124             GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
125             NULL);
126         gtk_window_set_resizable(GTK_WINDOW(wpa_dialog), false);
127         gtk_window_set_icon_name(GTK_WINDOW(wpa_dialog),
128             "network-wireless-encrypted");
129         gtk_dialog_set_default_response(GTK_DIALOG(wpa_dialog),
130             GTK_RESPONSE_ACCEPT);
131         vbox = gtk_dialog_get_content_area(GTK_DIALOG(wpa_dialog));
132
133         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
134         label = gtk_label_new(_("Pre Shared Key:"));
135         gtk_box_pack_start(GTK_BOX(hbox), label, false, false, 5);
136         psk = gtk_entry_new();
137         gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
138         g_signal_connect(G_OBJECT(psk), "activate",
139             G_CALLBACK(onEnter), wpa_dialog);
140         gtk_box_pack_start(GTK_BOX(hbox), psk, true, true, 5);
141         gtk_container_add(GTK_CONTAINER(vbox), hbox);
142
143         gtk_widget_show_all(wpa_dialog);
144         result = gtk_dialog_run(GTK_DIALOG(wpa_dialog));
145
146         retval = false;
147         if (result == GTK_RESPONSE_ACCEPT) {
148                 var = gtk_entry_get_text(GTK_ENTRY(psk));
149                 retval = wpa_conf(wpa, &s, var);
150         }
151         if (wpa_dialog) {
152                 gtk_widget_destroy(wpa_dialog);
153                 wpa_dialog = NULL;
154         }
155         return retval;
156 }