Bring over configure imporvements from dhcpcd.
[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 "dhcpcd-gtk.h"
28
29 static void
30 wpa_dialog(const char *title, const char *txt)
31 {
32         GtkWidget *dialog;
33
34         dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
35             GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", txt);
36         gtk_window_set_title(GTK_WINDOW(dialog), title);
37         gtk_dialog_run(GTK_DIALOG(dialog));
38         gtk_widget_destroy(dialog);
39 }
40
41 static bool
42 configure_network(DHCPCD_CONNECTION *con, DHCPCD_IF *i,
43     int id, const char *mgmt, const char *var, const char *val, bool quote)
44 {
45         char *str;
46         static bool warned = false;
47
48         if (!dhcpcd_wpa_set_network(con, i, id, "key_mgmt", mgmt))
49                 return false;
50         if (quote)
51                 str = g_strconcat("\"", val, "\"", NULL);
52         else
53                 str = NULL;
54         if (!dhcpcd_wpa_set_network(con, i, id, var, quote ? str : val)) {
55                 g_warning("libdhcpcd: %s", dhcpcd_error(con));
56                 dhcpcd_error_clear(con);
57                 g_free(str);
58                 wpa_dialog(_("Error setting password"),
59                     _("Failed to set password, probably too short."));
60                 return false;
61         }
62         g_free(str);
63         if (!dhcpcd_wpa_command(con, i, "EnableNetwork", id))
64                 return false;
65         if (!dhcpcd_wpa_command(con, i, "SaveConfig", -1)) {
66                 g_warning("libdhcpcd: %s", dhcpcd_error(con));
67                 dhcpcd_error_clear(con);
68                 if (!warned) {
69                         warned = true;
70                         wpa_dialog(_("Error saving configuration"),
71                             _("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."));
72                 }
73                 return false;
74         }
75 /*
76   if (!dbus_g_proxy_call(dbus, "Disconnect", &error,
77   G_TYPE_STRING, ifname,
78   G_TYPE_INVALID,
79   G_TYPE_INVALID))
80   {
81   g_warning("Disconnect: %s", error->message);
82   g_error_free(error);
83   }
84 */
85         if (!dhcpcd_wpa_command(con, i, "Reassociate", -1))
86                 return false;
87         return true;
88 }
89
90 static void
91 onEnter(_unused GtkWidget *widget, gpointer *data)
92 {
93         gtk_dialog_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT);
94 }
95
96 bool
97 wpa_configure(DHCPCD_CONNECTION *con, DHCPCD_IF *i, DHCPCD_WI_SCAN *s)
98 {
99         GtkWidget *dialog, *label, *psk, *vbox, *hbox;
100         const char *var, *mgt;
101         int result, id;
102         bool retval;
103
104         dialog = gtk_dialog_new_with_buttons(s->ssid,
105             NULL,
106             GTK_DIALOG_MODAL,
107             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
108             GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
109             NULL);
110         gtk_window_set_resizable(GTK_WINDOW(dialog), false);
111         gtk_window_set_icon_name(GTK_WINDOW(dialog),
112             "network-wireless-encrypted");
113         gtk_dialog_set_default_response(GTK_DIALOG(dialog),
114             GTK_RESPONSE_ACCEPT);
115         vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
116
117         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
118         label = gtk_label_new(_("Pre Shared Key:"));
119         gtk_box_pack_start(GTK_BOX(hbox), label, false, false, 0);
120         psk = gtk_entry_new();
121         gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
122         g_signal_connect(G_OBJECT(psk), "activate",
123             G_CALLBACK(onEnter), dialog);
124         gtk_box_pack_start(GTK_BOX(hbox), psk, true, true, 0);
125         gtk_container_add(GTK_CONTAINER(vbox), hbox);
126
127         gtk_widget_show_all(dialog);
128 again:
129         result = gtk_dialog_run(GTK_DIALOG(dialog));
130
131         id = -1;
132         retval = false;
133         if (result == GTK_RESPONSE_ACCEPT) {
134                 id = dhcpcd_wpa_find_network_new(con, i, s->ssid);
135                 if (g_strcmp0(s->flags, "[WEP]") == 0) {
136                         mgt = "NONE";
137                         var = "wep_key0";
138                 } else {
139                         mgt = "WPA-PSK";
140                         var = "psk";
141                 }
142                 if (id != -1) {
143                         retval = configure_network(con, i, id, mgt, var,
144                             gtk_entry_get_text(GTK_ENTRY(psk)), true);
145                 }
146                 if (!retval && dhcpcd_error(con)) {
147                         wpa_dialog(_("Error"), dhcpcd_error(con));
148                         goto again;
149                 }
150         }
151         gtk_widget_destroy(dialog);
152         return retval;
153 }