We finally work with PSK/WEPKEY_0 entry and we now save our config.
[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 *var, const char *val)
100 {
101         GError *error;
102         char *str;
103         static gboolean warned = FALSE;
104         GtkWidget *dialog;
105
106         error = NULL;
107         str = g_strconcat("\"", val, "\"", NULL);
108         if (!dbus_g_proxy_call(dbus, "SetNetwork", &error,
109                                G_TYPE_STRING, ifname,
110                                G_TYPE_INT, id,
111                                G_TYPE_STRING, var,
112                                G_TYPE_STRING, str,
113                                G_TYPE_INVALID,
114                                G_TYPE_INVALID))
115         {
116                 g_warning("SetNetwork: %s", error->message);
117                 g_free(str);
118                 g_error_free(error);
119                 return -1;
120         }
121         g_free(str);
122
123         if (!dbus_g_proxy_call(dbus, "SelectNetwork", &error,
124                                G_TYPE_STRING, ifname,
125                                G_TYPE_INT, id,
126                                G_TYPE_INVALID,
127                                G_TYPE_INVALID))
128         {
129                 g_warning("SelectNetwork: %s", error->message);
130                 g_error_free(error);
131                 return -1;
132         }
133
134         if (!dbus_g_proxy_call(dbus, "SaveConfig", &error,
135                                G_TYPE_STRING, ifname,
136                                G_TYPE_INVALID,
137                                G_TYPE_INVALID))
138         {
139                 g_warning("SaveConfig: %s", error->message);
140                 if (!warned) {
141                         warned = TRUE;
142                         dialog = gtk_message_dialog_new(NULL,
143                                                         GTK_DIALOG_MODAL,
144                                                         GTK_MESSAGE_ERROR,
145                                                         GTK_BUTTONS_OK,
146                                                         _("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."));
147                         gtk_window_set_title(GTK_WINDOW(dialog),
148                                              _("Error saving configuration"));
149                         gtk_dialog_run(GTK_DIALOG(dialog));
150                         gtk_widget_destroy(dialog);
151                 }
152                 g_error_free(error);
153         }
154         return 0;
155 }
156
157 gboolean
158 wpa_configure(const struct if_ap *ifa)
159 {
160         GtkWidget *dialog, *label, *psk, *vbox, *hbox;
161         char *title;
162         const char *var;
163         gint result, id;
164
165         title = g_strdup_printf(_("Configuration for %s"), ifa->ssid);
166         dialog = gtk_dialog_new_with_buttons(title,
167                 NULL,
168                 GTK_DIALOG_MODAL,
169                 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
170                 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
171                 NULL);
172         g_free(title);
173         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
174         gtk_window_set_icon_name(GTK_WINDOW(dialog), "config-users");
175         vbox = GTK_DIALOG(dialog)->vbox;
176
177         hbox = gtk_hbox_new(FALSE, 2);
178         label = gtk_label_new(_("Pre Shared Key:"));
179         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
180         psk = gtk_entry_new();
181         gtk_entry_set_max_length(GTK_ENTRY(psk), 130);
182         gtk_box_pack_start(GTK_BOX(hbox), psk, TRUE, TRUE, 0);
183         gtk_container_add(GTK_CONTAINER(vbox), hbox);
184
185         gtk_widget_show_all(dialog);
186         result = gtk_dialog_run(GTK_DIALOG(dialog));
187         
188         id = -1;
189         if (result == GTK_RESPONSE_ACCEPT) {
190                 id = find_network(ifa->ifname, ifa->ssid);
191                 if (g_strcmp0(ifa->flags, "[WEP]") == 0)
192                         var = "wep_key0";
193                 else
194                         var = "psk";
195                 if (id != -1)
196                         id = configure_network(ifa->ifname, id, var,
197                                                gtk_entry_get_text(GTK_ENTRY(psk)));
198         }
199         gtk_widget_destroy(dialog);
200         return id == -1 ? FALSE : TRUE;
201 }