Move saving WPA configuration to libdhcpcd.
[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 void
32 wpa_dialog(const char *title, const char *txt)
33 {
34         GtkWidget *dialog;
35
36         dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
37             GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", txt);
38         gtk_window_set_title(GTK_WINDOW(dialog), title);
39         gtk_dialog_run(GTK_DIALOG(dialog));
40         gtk_widget_destroy(dialog);
41 }
42
43 static void
44 onEnter(_unused GtkWidget *widget, gpointer *data)
45 {
46         gtk_dialog_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT);
47 }
48
49 bool
50 wpa_configure(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *scan)
51 {
52         DHCPCD_WI_SCAN s;
53         GtkWidget *dialog, *label, *psk, *vbox, *hbox;
54         const char *var, *errt;
55         int result;
56         bool retval;
57
58         /* Take a copy of scan incase it's destroyed by a scan update */
59         memcpy(&s, scan, sizeof(s));
60         s.next = NULL;
61
62         dialog = gtk_dialog_new_with_buttons(s.ssid,
63             NULL,
64             GTK_DIALOG_MODAL,
65             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
66             GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
67             NULL);
68         gtk_window_set_resizable(GTK_WINDOW(dialog), false);
69         gtk_window_set_icon_name(GTK_WINDOW(dialog),
70             "network-wireless-encrypted");
71         gtk_dialog_set_default_response(GTK_DIALOG(dialog),
72             GTK_RESPONSE_ACCEPT);
73         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
74
75         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
76         label = gtk_label_new(_("Pre Shared Key:"));
77         gtk_box_pack_start(GTK_BOX(hbox), label, false, false, 0);
78         psk = gtk_entry_new();
79         gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
80         g_signal_connect(G_OBJECT(psk), "activate",
81             G_CALLBACK(onEnter), dialog);
82         gtk_box_pack_start(GTK_BOX(hbox), psk, true, true, 0);
83         gtk_container_add(GTK_CONTAINER(vbox), hbox);
84
85         gtk_widget_show_all(dialog);
86 again:
87         result = gtk_dialog_run(GTK_DIALOG(dialog));
88
89         retval = false;
90         if (result == GTK_RESPONSE_ACCEPT) {
91                 var = gtk_entry_get_text(GTK_ENTRY(psk));
92                 switch (dhcpcd_wpa_configure_psk(wpa, &s, var)) {
93                 case DHCPCD_WPA_SUCCESS:
94                         retval = true;
95                         break;
96                 case DHCPCD_WPA_ERR_SET:
97                         errt = _("Failed to set key management.");
98                         break;
99                 case DHCPCD_WPA_ERR_SET_PSK:
100                         errt = _("Failed to set password, probably too short.");
101                         break;
102                 case DHCPCD_WPA_ERR_ENABLE:
103                         errt = _("Failed to enable the network.");
104                         break;
105                 case DHCPCD_WPA_ERR_ASSOC:
106                         errt = _("Failed to start association.");
107                         break;
108                 case DHCPCD_WPA_ERR_WRITE:
109                         errt =_("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.");
110                         break;
111                 default:
112                         errt = strerror(errno);
113                         break;
114                 }
115                 if (!retval) {
116                         wpa_dialog(_("Error enabling network"), errt);
117                         goto again;
118                 }
119         }
120         gtk_widget_destroy(dialog);
121         return retval;
122 }