Add the humble beginnings for a preferences dialog.
[dhcpcd-ui] / wpa.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009 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 #include "wpa.h"
29
30 static gint
31 find_network(const char *ifname, const char *ssid)
32 {
33         GType otype;
34         GError *error;
35         gint id;
36         size_t i;
37         GPtrArray *array;
38         GValueArray *varray;
39         GValue *val;
40         const char *str;
41         char *nssid;
42
43         otype = dbus_g_type_get_struct("GValueArray",
44             G_TYPE_INT,
45             G_TYPE_STRING,
46             G_TYPE_STRING,
47             G_TYPE_STRING,
48             G_TYPE_INVALID);
49         otype = dbus_g_type_get_collection("GPtrArray", otype);
50         error = NULL;
51         if (!dbus_g_proxy_call(dbus, "ListNetworks", &error,
52                 G_TYPE_STRING, ifname, G_TYPE_INVALID,
53                 otype, &array, G_TYPE_INVALID))
54         {
55                 g_warning("ListNetworks: %s", error->message);
56                 g_error_free(error);
57                 return -1;
58         }
59
60         for (i = 0; i < array->len; i++) {
61                 varray = g_ptr_array_index(array, i);
62                 val = g_value_array_get_nth(varray, 1);
63                 str = g_value_get_string(val);
64                 if (g_strcmp0(str, ssid) == 0) {
65                         val = g_value_array_get_nth(varray, 0);
66                         return g_value_get_int(val);
67                 }
68         }
69
70         if (!dbus_g_proxy_call(dbus, "AddNetwork", &error,
71                 G_TYPE_STRING, ifname, G_TYPE_INVALID,
72                 G_TYPE_INT, &id, G_TYPE_INVALID))
73         {
74                 g_warning("AddNetwork: %s", error->message);
75                 g_error_free(error);
76                 return -1;
77         }
78
79         nssid = g_strconcat("\"", ssid, "\"", NULL);
80         if (!dbus_g_proxy_call(dbus, "SetNetwork", &error,
81                 G_TYPE_STRING, ifname,
82                 G_TYPE_INT, id,
83                 G_TYPE_STRING, "ssid",
84                 G_TYPE_STRING, nssid,
85                 G_TYPE_INVALID,
86                 G_TYPE_INVALID))
87         {
88                 g_warning("SetNetwork: %s", error->message);
89                 g_free(nssid);
90                 g_error_free(error);
91                 return -1;
92         }
93         g_free(nssid);
94
95         return id;
96 }
97
98 static int
99 configure_network(const char *ifname, int id, const char *mgmt,
100     const char *var, const char *val,
101     gboolean quote)
102 {
103         GError *error;
104         char *str;
105         static gboolean warned = FALSE;
106         GtkWidget *dialog;
107
108         if (id == -1)
109                 return -1;
110
111         dbus_g_proxy_call(dbus, "SetNetwork", &error,
112             G_TYPE_STRING, ifname,
113             G_TYPE_INT, id,
114             G_TYPE_STRING, "key_mgmt",
115             G_TYPE_STRING, mgmt,
116             G_TYPE_INVALID,
117             G_TYPE_INVALID);
118
119         error = NULL;
120         if (quote)
121                 str = g_strconcat("\"", val, "\"", NULL);
122         else
123                 str = NULL;
124         if (!dbus_g_proxy_call(dbus, "SetNetwork", &error,
125                 G_TYPE_STRING, ifname,
126                 G_TYPE_INT, id,
127                 G_TYPE_STRING, var,
128                 G_TYPE_STRING, quote ? str : val,
129                 G_TYPE_INVALID,
130                 G_TYPE_INVALID))
131         {
132                 g_warning("SetNetwork: %s", error->message);
133                 g_free(str);
134                 g_error_free(error);
135                 dialog = gtk_message_dialog_new(NULL,
136                     GTK_DIALOG_MODAL,
137                     GTK_MESSAGE_ERROR,
138                     GTK_BUTTONS_OK,
139                     _("Failed to set password, probably too short."));
140                 gtk_window_set_title(GTK_WINDOW(dialog),
141                     _("Error setting password"));
142                 gtk_dialog_run(GTK_DIALOG(dialog));
143                 gtk_widget_destroy(dialog);
144                 return -1;
145         }
146         g_free(str);
147
148         if (!dbus_g_proxy_call(dbus, "EnableNetwork", &error,
149                 G_TYPE_STRING, ifname,
150                 G_TYPE_INT, id,
151                 G_TYPE_INVALID,
152                 G_TYPE_INVALID))
153         {
154                 g_warning("EnableNetwork: %s", error->message);
155                 g_error_free(error);
156                 return -1;
157         }
158
159         if (!dbus_g_proxy_call(dbus, "SaveConfig", &error,
160                 G_TYPE_STRING, ifname,
161                 G_TYPE_INVALID,
162                 G_TYPE_INVALID))
163         {
164                 g_warning("SaveConfig: %s", error->message);
165                 if (!warned) {
166                         warned = TRUE;
167                         dialog = gtk_message_dialog_new(NULL,
168                             GTK_DIALOG_MODAL,
169                             GTK_MESSAGE_ERROR,
170                             GTK_BUTTONS_OK,
171                             _("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."));
172                         gtk_window_set_title(GTK_WINDOW(dialog),
173                             _("Error saving configuration"));
174                         gtk_dialog_run(GTK_DIALOG(dialog));
175                         gtk_widget_destroy(dialog);
176                 }
177                 g_error_free(error);
178         }
179 /*
180   if (!dbus_g_proxy_call(dbus, "Disconnect", &error,
181   G_TYPE_STRING, ifname,
182   G_TYPE_INVALID,
183   G_TYPE_INVALID))
184   {
185   g_warning("Disconnect: %s", error->message);
186   g_error_free(error);
187   }
188 */
189         if (!dbus_g_proxy_call(dbus, "Reassociate", &error,
190                 G_TYPE_STRING, ifname,
191                 G_TYPE_INVALID,
192                 G_TYPE_INVALID))
193         {
194                 g_warning("Reassociate: %s", error->message);
195                 g_error_free(error);
196         }
197         
198         return 0;
199 }
200
201 static void
202 onEnter(_unused GtkWidget *widget, gpointer *data)
203 {
204         gtk_dialog_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT);
205 }
206
207 gboolean
208 wpa_configure(const struct if_ap *ifa)
209 {
210         GtkWidget *dialog, *label, *psk, *vbox, *hbox;
211         const char *var, *mgt;
212         gint result, id, retval;
213
214         dialog = gtk_dialog_new_with_buttons(ifa->ssid,
215             NULL,
216             GTK_DIALOG_MODAL,
217             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
218             GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
219             NULL);
220         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
221         gtk_window_set_icon_name(GTK_WINDOW(dialog), "config-users");
222         gtk_dialog_set_default_response(GTK_DIALOG(dialog),
223             GTK_RESPONSE_ACCEPT);
224         vbox = GTK_DIALOG(dialog)->vbox;
225
226         hbox = gtk_hbox_new(FALSE, 2);
227         label = gtk_label_new(_("Pre Shared Key:"));
228         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
229         psk = gtk_entry_new();
230         gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
231         g_signal_connect(G_OBJECT(psk), "activate", G_CALLBACK(onEnter), dialog);
232         gtk_box_pack_start(GTK_BOX(hbox), psk, TRUE, TRUE, 0);
233         gtk_container_add(GTK_CONTAINER(vbox), hbox);
234
235         gtk_widget_show_all(dialog);
236         result = gtk_dialog_run(GTK_DIALOG(dialog));
237         
238         id = -1;
239         retval = -1;
240         if (result == GTK_RESPONSE_ACCEPT) {
241                 id = find_network(ifa->ifname, ifa->ssid);
242                 if (g_strcmp0(ifa->flags, "[WEP]") == 0) {
243                         mgt = "NONE";
244                         var = "wep_key0";
245                 } else {
246                         mgt = "WPA-PSK";
247                         var = "psk";
248                 }
249                 if (id != -1) {
250                         retval = configure_network(ifa->ifname, id, mgt, var,
251                             gtk_entry_get_text(GTK_ENTRY(psk)), TRUE);
252                 }
253         }
254         gtk_widget_destroy(dialog);
255         return retval == -1 ? FALSE : TRUE;
256 }