2965326125d737baa6fb1c402353caa45efe29d3
[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 bool
44 configure_network(DHCPCD_WPA *wpa,
45     int id, const char *mgmt, const char *var, const char *val, bool quote)
46 {
47         char *str;
48         static bool warned = false;
49
50         if (!dhcpcd_wpa_network_set(wpa, id, "key_mgmt", mgmt)) {
51                 g_warning("libdhcpcd: %s", strerror(errno));
52                 wpa_dialog(_("Error"),
53                     _("Failed to set key management"));
54                 return false;
55         }
56         if (quote)
57                 str = g_strconcat("\"", val, "\"", NULL);
58         else
59                 str = NULL;
60         if (!dhcpcd_wpa_network_set(wpa, id, var, quote ? str : val)) {
61                 g_warning("libdhcpcd: %s", strerror(errno));
62                 g_free(str);
63                 wpa_dialog(_("Error setting password"),
64                     _("Failed to set password, probably too short."));
65                 return false;
66         }
67         g_free(str);
68         if (!dhcpcd_wpa_network_enable(wpa, id)) {
69                 wpa_dialog(_("Error enabling network"), strerror(errno));
70                 return false;
71         }
72         if (!dhcpcd_wpa_config_write(wpa)) {
73                 g_warning("libdhcpcd: %s", strerror(errno));
74                 if (!warned) {
75                         warned = true;
76                         wpa_dialog(_("Error saving configuration"),
77                             _("Failed to save wpa_supplicant configuration.\n\nYou should add update_config=1 to /etc/wpa_supplicant.conf.\nThis warning will not appear again until program restarted."));
78                 }
79                 return false;
80         }
81         return dhcpcd_wpa_reassociate(wpa);
82 }
83
84 static void
85 onEnter(_unused GtkWidget *widget, gpointer *data)
86 {
87         gtk_dialog_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT);
88 }
89
90 bool
91 wpa_configure(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s)
92 {
93         GtkWidget *dialog, *label, *psk, *vbox, *hbox;
94         const char *var, *mgt;
95         int result, id;
96         bool retval;
97
98         dialog = gtk_dialog_new_with_buttons(s->ssid,
99             NULL,
100             GTK_DIALOG_MODAL,
101             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
102             GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
103             NULL);
104         gtk_window_set_resizable(GTK_WINDOW(dialog), false);
105         gtk_window_set_icon_name(GTK_WINDOW(dialog),
106             "network-wireless-encrypted");
107         gtk_dialog_set_default_response(GTK_DIALOG(dialog),
108             GTK_RESPONSE_ACCEPT);
109         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
110
111         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
112         label = gtk_label_new(_("Pre Shared Key:"));
113         gtk_box_pack_start(GTK_BOX(hbox), label, false, false, 0);
114         psk = gtk_entry_new();
115         gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
116         g_signal_connect(G_OBJECT(psk), "activate",
117             G_CALLBACK(onEnter), dialog);
118         gtk_box_pack_start(GTK_BOX(hbox), psk, true, true, 0);
119         gtk_container_add(GTK_CONTAINER(vbox), hbox);
120
121         gtk_widget_show_all(dialog);
122 again:
123         result = gtk_dialog_run(GTK_DIALOG(dialog));
124
125         id = -1;
126         retval = false;
127         if (result == GTK_RESPONSE_ACCEPT) {
128                 id = dhcpcd_wpa_network_find_new(wpa, s->ssid);
129                 if (g_strcmp0(s->flags, "[WEP]") == 0) {
130                         mgt = "NONE";
131                         var = "wep_key0";
132                 } else {
133                         mgt = "WPA-PSK";
134                         var = "psk";
135                 }
136                 if (id != -1) {
137                         configure_network(wpa, id, mgt, var,
138                             gtk_entry_get_text(GTK_ENTRY(psk)), true);
139                 }
140                 if (!retval)
141                         goto again;
142         }
143         gtk_widget_destroy(dialog);
144         return retval;
145 }