wpa_gui-qt4: Added support for configuring Phase 2 method
[libeap.git] / wpa_supplicant / wpa_gui-qt4 / networkconfig.cpp
1 /*
2  * wpa_gui - NetworkConfig class
3  * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include <QMessageBox>
16
17 #include "networkconfig.h"
18 #include "wpagui.h"
19
20 enum {
21     AUTH_NONE = 0,
22     AUTH_IEEE8021X = 1,
23     AUTH_WPA_PSK = 2,
24     AUTH_WPA_EAP = 3,
25     AUTH_WPA2_PSK = 4,
26     AUTH_WPA2_EAP = 5
27 };
28
29 #define WPA_GUI_KEY_DATA "[key is configured]"
30
31
32 NetworkConfig::NetworkConfig(QWidget *parent, const char *, bool, Qt::WFlags)
33         : QDialog(parent)
34 {
35         setupUi(this);
36
37         connect(authSelect, SIGNAL(activated(int)), this,
38                 SLOT(authChanged(int)));
39         connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
40         connect(addButton, SIGNAL(clicked()), this, SLOT(addNetwork()));
41         connect(encrSelect, SIGNAL(activated(const QString &)), this,
42                 SLOT(encrChanged(const QString &)));
43         connect(removeButton, SIGNAL(clicked()), this, SLOT(removeNetwork()));
44         connect(eapSelect, SIGNAL(activated(int)), this,
45                 SLOT(eapChanged(int)));
46
47         wpagui = NULL;
48         new_network = false;
49 }
50
51
52 NetworkConfig::~NetworkConfig()
53 {
54 }
55
56
57 void NetworkConfig::languageChange()
58 {
59         retranslateUi(this);
60 }
61
62
63 void NetworkConfig::paramsFromScanResults(QTreeWidgetItem *sel)
64 {
65         new_network = true;
66
67         /* SSID BSSID frequency signal flags */
68         setWindowTitle(sel->text(0));
69         ssidEdit->setText(sel->text(0));
70
71         QString flags = sel->text(4);
72         int auth, encr = 0;
73         if (flags.indexOf("[WPA2-EAP") >= 0)
74                 auth = AUTH_WPA2_EAP;
75         else if (flags.indexOf("[WPA-EAP") >= 0)
76                 auth = AUTH_WPA_EAP;
77         else if (flags.indexOf("[WPA2-PSK") >= 0)
78                 auth = AUTH_WPA2_PSK;
79         else if (flags.indexOf("[WPA-PSK") >= 0)
80                 auth = AUTH_WPA_PSK;
81         else
82                 auth = AUTH_NONE;
83
84         if (flags.indexOf("-CCMP") >= 0)
85                 encr = 1;
86         else if (flags.indexOf("-TKIP") >= 0)
87                 encr = 0;
88         else if (flags.indexOf("WEP") >= 0)
89                 encr = 1;
90         else
91                 encr = 0;
92
93         authSelect->setCurrentIndex(auth);
94         authChanged(auth);
95         encrSelect->setCurrentIndex(encr);
96
97         wepEnabled(auth == AUTH_NONE && encr == 1);
98
99         getEapCapa();
100 }
101
102
103 void NetworkConfig::authChanged(int sel)
104 {
105         pskEdit->setEnabled(sel == AUTH_WPA_PSK || sel == AUTH_WPA2_PSK);
106         bool eap = sel == AUTH_IEEE8021X || sel == AUTH_WPA_EAP ||
107                 sel == AUTH_WPA2_EAP;
108         eapSelect->setEnabled(eap);
109         identityEdit->setEnabled(eap);
110         passwordEdit->setEnabled(eap);
111         cacertEdit->setEnabled(eap);
112         phase2Select->setEnabled(eap);
113         if (eap)
114                 eapChanged(eapSelect->currentIndex());
115
116         while (encrSelect->count())
117                 encrSelect->removeItem(0);
118
119         if (sel == AUTH_NONE || sel == AUTH_IEEE8021X) {
120                 encrSelect->addItem("None");
121                 encrSelect->addItem("WEP");
122                 encrSelect->setCurrentIndex(sel == AUTH_NONE ? 0 : 1);
123         } else {
124                 encrSelect->addItem("TKIP");
125                 encrSelect->addItem("CCMP");
126                 encrSelect->setCurrentIndex((sel == AUTH_WPA2_PSK ||
127                                              sel == AUTH_WPA2_EAP) ? 1 : 0);
128         }
129
130         wepEnabled(sel == AUTH_IEEE8021X);
131 }
132
133
134 void NetworkConfig::eapChanged(int sel)
135 {
136         QString prev_val = phase2Select->currentText();
137         while (phase2Select->count())
138                 phase2Select->removeItem(0);
139
140         QStringList inner;
141         inner << "PEAP" << "TTLS" << "FAST";
142         if (!inner.contains(eapSelect->itemText(sel)))
143                 return;
144
145         phase2Select->addItem("[ any ]");
146
147         /* Add special cases based on outer method */
148         if (eapSelect->currentText().compare("TTLS") == 0) {
149                 phase2Select->addItem("PAP");
150                 phase2Select->addItem("CHAP");
151                 phase2Select->addItem("MSCHAP");
152                 phase2Select->addItem("MSCHAPv2");
153         } else if (eapSelect->currentText().compare("FAST") == 0)
154                 phase2Select->addItem("GTC(auth) + MSCHAPv2(prov)");
155
156         /* Add all enabled EAP methods that can be used in the tunnel */
157         int i;
158         QStringList allowed;
159         allowed << "MSCHAPV2" << "MD5" << "GTC" << "TLS" << "OTP" << "SIM"
160                 << "AKA";
161         for (i = 0; i < eapSelect->count(); i++) {
162                 if (allowed.contains(eapSelect->itemText(i))) {
163                         phase2Select->addItem("EAP-" + eapSelect->itemText(i));
164                 }
165         }
166
167         for (i = 0; i < phase2Select->count(); i++) {
168                 if (phase2Select->itemText(i).compare(prev_val) == 0) {
169                         phase2Select->setCurrentIndex(i);
170                         break;
171                 }
172         }
173 }
174
175
176 void NetworkConfig::addNetwork()
177 {
178         char reply[10], cmd[256];
179         size_t reply_len;
180         int id;
181         int psklen = pskEdit->text().length();
182         int auth = authSelect->currentIndex();
183
184         if (auth == AUTH_WPA_PSK || auth == AUTH_WPA2_PSK) {
185                 if (psklen < 8 || psklen > 64) {
186                         QMessageBox::warning(this, "WPA Pre-Shared Key Error",
187                                              "WPA-PSK requires a passphrase "
188                                              "of 8 to 63 characters\n"
189                                              "or 64 hex digit PSK");
190                         pskEdit->setFocus();
191                         return;
192                 }
193         }
194
195         if (idstrEdit->isEnabled() && !idstrEdit->text().isEmpty()) {
196                 QRegExp rx("^(\\w|-)+$");
197                 if (rx.indexIn(idstrEdit->text()) < 0) {
198                         QMessageBox::warning(this, "Network ID Error",
199                                              "Network ID String contains "
200                                              "non-word characters.\n"
201                                              "It must be a simple string, "
202                                              "without spaces, containing\n"
203                                              "only characters in this range: "
204                                              "[A-Za-z0-9_-]\n");
205                         idstrEdit->setFocus();
206                         return;
207                 }
208         }
209
210         if (wpagui == NULL)
211                 return;
212
213         memset(reply, 0, sizeof(reply));
214         reply_len = sizeof(reply) - 1;
215
216         if (new_network) {
217                 wpagui->ctrlRequest("ADD_NETWORK", reply, &reply_len);
218                 if (reply[0] == 'F') {
219                         QMessageBox::warning(this, "wpa_gui", "Failed to add "
220                                              "network to wpa_supplicant\n"
221                                              "configuration.");
222                         return;
223                 }
224                 id = atoi(reply);
225         } else
226                 id = edit_network_id;
227
228         setNetworkParam(id, "ssid", ssidEdit->text().toAscii().constData(),
229                         true);
230
231         const char *key_mgmt = NULL, *proto = NULL, *pairwise = NULL;
232         switch (auth) {
233         case AUTH_NONE:
234                 key_mgmt = "NONE";
235                 break;
236         case AUTH_IEEE8021X:
237                 key_mgmt = "IEEE8021X";
238                 break;
239         case AUTH_WPA_PSK:
240                 key_mgmt = "WPA-PSK";
241                 proto = "WPA";
242                 break;
243         case AUTH_WPA_EAP:
244                 key_mgmt = "WPA-EAP";
245                 proto = "WPA";
246                 break;
247         case AUTH_WPA2_PSK:
248                 key_mgmt = "WPA-PSK";
249                 proto = "WPA2";
250                 break;
251         case AUTH_WPA2_EAP:
252                 key_mgmt = "WPA-EAP";
253                 proto = "WPA2";
254                 break;
255         }
256
257         if (auth == AUTH_WPA_PSK || auth == AUTH_WPA_EAP ||
258             auth == AUTH_WPA2_PSK || auth == AUTH_WPA2_EAP) {
259                 int encr = encrSelect->currentIndex();
260                 if (encr == 0)
261                         pairwise = "TKIP";
262                 else
263                         pairwise = "CCMP";
264         }
265
266         if (proto)
267                 setNetworkParam(id, "proto", proto, false);
268         if (key_mgmt)
269                 setNetworkParam(id, "key_mgmt", key_mgmt, false);
270         if (pairwise) {
271                 setNetworkParam(id, "pairwise", pairwise, false);
272                 setNetworkParam(id, "group", "TKIP CCMP WEP104 WEP40", false);
273         }
274         if (pskEdit->isEnabled() &&
275             strcmp(passwordEdit->text().toAscii().constData(),
276                    WPA_GUI_KEY_DATA) != 0)
277                 setNetworkParam(id, "psk",
278                                 pskEdit->text().toAscii().constData(),
279                                 psklen != 64);
280         if (eapSelect->isEnabled()) {
281                 const char *eap =
282                         eapSelect->currentText().toAscii().constData();
283                 setNetworkParam(id, "eap", eap, false);
284                 if (strcmp(eap, "SIM") == 0 || strcmp(eap, "AKA") == 0)
285                         setNetworkParam(id, "pcsc", "", true);
286         }
287         if (phase2Select->isEnabled()) {
288                 QString eap = eapSelect->currentText();
289                 QString inner = phase2Select->currentText();
290                 char phase2[32];
291                 phase2[0] = '\0';
292                 if (eap.compare("PEAP") == 0) {
293                         if (inner.startsWith("EAP-"))
294                                 snprintf(phase2, sizeof(phase2), "auth=%s",
295                                          inner.right(inner.size() - 4).
296                                          toAscii().constData());
297                 } else if (eap.compare("TTLS") == 0) {
298                         if (inner.startsWith("EAP-"))
299                                 snprintf(phase2, sizeof(phase2), "autheap=%s",
300                                          inner.right(inner.size() - 4).
301                                          toAscii().constData());
302                         else
303                                 snprintf(phase2, sizeof(phase2), "auth=%s",
304                                          inner.toAscii().constData());
305                 } else if (eap.compare("FAST") == 0) {
306                         if (inner.startsWith("EAP-"))
307                                 snprintf(phase2, sizeof(phase2), "auth=%s",
308                                          inner.right(inner.size() - 4).
309                                          toAscii().constData());
310                         else if (inner.compare("GTC(auth) + MSCHAPv2(prov)") ==
311                                  0) {
312                                 snprintf(phase2, sizeof(phase2),
313                                          "auth=GTC MSCHAPV2");
314                         }
315                 }
316                 setNetworkParam(id, "phase2", phase2, true);
317         }
318         if (identityEdit->isEnabled())
319                 setNetworkParam(id, "identity",
320                                 identityEdit->text().toAscii().constData(),
321                                 true);
322         if (passwordEdit->isEnabled() &&
323             strcmp(passwordEdit->text().toAscii().constData(),
324                    WPA_GUI_KEY_DATA) != 0)
325                 setNetworkParam(id, "password",
326                                 passwordEdit->text().toAscii().constData(),
327                                 true);
328         if (cacertEdit->isEnabled())
329                 setNetworkParam(id, "ca_cert",
330                                 cacertEdit->text().toAscii().constData(),
331                                 true);
332         writeWepKey(id, wep0Edit, 0);
333         writeWepKey(id, wep1Edit, 1);
334         writeWepKey(id, wep2Edit, 2);
335         writeWepKey(id, wep3Edit, 3);
336
337         if (wep0Radio->isEnabled() && wep0Radio->isChecked())
338                 setNetworkParam(id, "wep_tx_keyidx", "0", false);
339         else if (wep1Radio->isEnabled() && wep1Radio->isChecked())
340                 setNetworkParam(id, "wep_tx_keyidx", "1", false);
341         else if (wep2Radio->isEnabled() && wep2Radio->isChecked())
342                 setNetworkParam(id, "wep_tx_keyidx", "2", false);
343         else if (wep3Radio->isEnabled() && wep3Radio->isChecked())
344                 setNetworkParam(id, "wep_tx_keyidx", "3", false);
345
346         if (idstrEdit->isEnabled())
347                 setNetworkParam(id, "id_str",
348                                 idstrEdit->text().toAscii().constData(),
349                                 true);
350
351         if (prioritySpinBox->isEnabled()) {
352                 QString prio;
353                 prio = prio.setNum(prioritySpinBox->value());
354                 setNetworkParam(id, "priority", prio.toAscii().constData(),
355                                 false);
356         }
357
358         snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %d", id);
359         reply_len = sizeof(reply);
360         wpagui->ctrlRequest(cmd, reply, &reply_len);
361         if (strncmp(reply, "OK", 2) != 0) {
362                 QMessageBox::warning(this, "wpa_gui", "Failed to enable "
363                                      "network in wpa_supplicant\n"
364                                      "configuration.");
365                 /* Network was added, so continue anyway */
366         }
367         wpagui->triggerUpdate();
368         wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
369
370         close();
371 }
372
373
374 void NetworkConfig::setWpaGui(WpaGui *_wpagui)
375 {
376         wpagui = _wpagui;
377 }
378
379
380 int NetworkConfig::setNetworkParam(int id, const char *field,
381                                    const char *value, bool quote)
382 {
383         char reply[10], cmd[256];
384         size_t reply_len;
385         snprintf(cmd, sizeof(cmd), "SET_NETWORK %d %s %s%s%s",
386                  id, field, quote ? "\"" : "", value, quote ? "\"" : "");
387         reply_len = sizeof(reply);
388         wpagui->ctrlRequest(cmd, reply, &reply_len);
389         return strncmp(reply, "OK", 2) == 0 ? 0 : -1;
390 }
391
392
393 void NetworkConfig::encrChanged(const QString &sel)
394 {
395         wepEnabled(sel.indexOf("WEP") == 0);
396 }
397
398
399 void NetworkConfig::wepEnabled(bool enabled)
400 {
401         wep0Edit->setEnabled(enabled);
402         wep1Edit->setEnabled(enabled);
403         wep2Edit->setEnabled(enabled);
404         wep3Edit->setEnabled(enabled);
405         wep0Radio->setEnabled(enabled);
406         wep1Radio->setEnabled(enabled);
407         wep2Radio->setEnabled(enabled);
408         wep3Radio->setEnabled(enabled);
409 }
410
411
412 void NetworkConfig::writeWepKey(int network_id, QLineEdit *edit, int id)
413 {
414         char buf[10];
415         bool hex;
416         const char *txt, *pos;
417         size_t len;
418
419         if (!edit->isEnabled() || edit->text().isEmpty())
420                 return;
421
422         /*
423          * Assume hex key if only hex characters are present and length matches
424          * with 40, 104, or 128-bit key
425          */
426         txt = edit->text().toAscii().constData();
427         if (strcmp(txt, WPA_GUI_KEY_DATA) == 0)
428                 return;
429         len = strlen(txt);
430         if (len == 0)
431                 return;
432         pos = txt;
433         hex = true;
434         while (*pos) {
435                 if (!((*pos >= '0' && *pos <= '9') ||
436                       (*pos >= 'a' && *pos <= 'f') ||
437                       (*pos >= 'A' && *pos <= 'F'))) {
438                         hex = false;
439                         break;
440                 }
441                 pos++;
442         }
443         if (hex && len != 10 && len != 26 && len != 32)
444                 hex = false;
445         snprintf(buf, sizeof(buf), "wep_key%d", id);
446         setNetworkParam(network_id, buf, txt, !hex);
447 }
448
449
450 static int key_value_isset(const char *reply, size_t reply_len)
451 {
452     return reply_len > 0 && (reply_len < 4 || memcmp(reply, "FAIL", 4) != 0);
453 }
454
455
456 void NetworkConfig::paramsFromConfig(int network_id)
457 {
458         int i, res;
459
460         edit_network_id = network_id;
461         getEapCapa();
462
463         char reply[1024], cmd[256], *pos;
464         size_t reply_len;
465
466         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ssid", network_id);
467         reply_len = sizeof(reply) - 1;
468         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
469             reply_len >= 2 && reply[0] == '"') {
470                 reply[reply_len] = '\0';
471                 pos = strchr(reply + 1, '"');
472                 if (pos)
473                         *pos = '\0';
474                 ssidEdit->setText(reply + 1);
475         }
476
477         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d proto", network_id);
478         reply_len = sizeof(reply) - 1;
479         int wpa = 0;
480         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
481                 reply[reply_len] = '\0';
482                 if (strstr(reply, "RSN") || strstr(reply, "WPA2"))
483                         wpa = 2;
484                 else if (strstr(reply, "WPA"))
485                         wpa = 1;
486         }
487
488         int auth = AUTH_NONE, encr = 0;
489         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d key_mgmt", network_id);
490         reply_len = sizeof(reply) - 1;
491         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
492                 reply[reply_len] = '\0';
493                 if (strstr(reply, "WPA-EAP"))
494                         auth = wpa & 2 ? AUTH_WPA2_EAP : AUTH_WPA_EAP;
495                 else if (strstr(reply, "WPA-PSK"))
496                         auth = wpa & 2 ? AUTH_WPA2_PSK : AUTH_WPA_PSK;
497                 else if (strstr(reply, "IEEE8021X")) {
498                         auth = AUTH_IEEE8021X;
499                         encr = 1;
500                 }
501         }
502
503         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d pairwise", network_id);
504         reply_len = sizeof(reply) - 1;
505         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
506                 reply[reply_len] = '\0';
507                 if (strstr(reply, "CCMP") && auth != AUTH_NONE)
508                         encr = 1;
509                 else if (strstr(reply, "TKIP"))
510                         encr = 0;
511                 else if (strstr(reply, "WEP"))
512                         encr = 1;
513                 else
514                         encr = 0;
515         }
516
517         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d psk", network_id);
518         reply_len = sizeof(reply) - 1;
519         res = wpagui->ctrlRequest(cmd, reply, &reply_len);
520         if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
521                 reply[reply_len] = '\0';
522                 pos = strchr(reply + 1, '"');
523                 if (pos)
524                         *pos = '\0';
525                 pskEdit->setText(reply + 1);
526         } else if (res >= 0 && key_value_isset(reply, reply_len)) {
527                 pskEdit->setText(WPA_GUI_KEY_DATA);
528         }
529
530         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d identity", network_id);
531         reply_len = sizeof(reply) - 1;
532         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
533             reply_len >= 2 && reply[0] == '"') {
534                 reply[reply_len] = '\0';
535                 pos = strchr(reply + 1, '"');
536                 if (pos)
537                         *pos = '\0';
538                 identityEdit->setText(reply + 1);
539         }
540
541         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d password", network_id);
542         reply_len = sizeof(reply) - 1;
543         res = wpagui->ctrlRequest(cmd, reply, &reply_len);
544         if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
545                 reply[reply_len] = '\0';
546                 pos = strchr(reply + 1, '"');
547                 if (pos)
548                         *pos = '\0';
549                 passwordEdit->setText(reply + 1);
550         } else if (res >= 0 && key_value_isset(reply, reply_len)) {
551                 passwordEdit->setText(WPA_GUI_KEY_DATA);
552         }
553
554         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ca_cert", network_id);
555         reply_len = sizeof(reply) - 1;
556         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
557             reply_len >= 2 && reply[0] == '"') {
558                 reply[reply_len] = '\0';
559                 pos = strchr(reply + 1, '"');
560                 if (pos)
561                         *pos = '\0';
562                 cacertEdit->setText(reply + 1);
563         }
564
565         enum { NO_INNER, PEAP_INNER, TTLS_INNER, FAST_INNER } eap = NO_INNER;
566         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d eap", network_id);
567         reply_len = sizeof(reply) - 1;
568         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
569             reply_len >= 1) {
570                 reply[reply_len] = '\0';
571                 for (i = 0; i < eapSelect->count(); i++) {
572                         if (eapSelect->itemText(i).compare(reply) == 0) {
573                                 eapSelect->setCurrentIndex(i);
574                                 if (strcmp(reply, "PEAP") == 0)
575                                         eap = PEAP_INNER;
576                                 else if (strcmp(reply, "TTLS") == 0)
577                                         eap = TTLS_INNER;
578                                 else if (strcmp(reply, "FAST") == 0)
579                                         eap = FAST_INNER;
580                                 break;
581                         }
582                 }
583         }
584
585         if (eap != NO_INNER) {
586                 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d phase2",
587                          network_id);
588                 reply_len = sizeof(reply) - 1;
589                 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
590                     reply_len >= 1) {
591                         reply[reply_len] = '\0';
592                         eapChanged(eapSelect->currentIndex());
593                 } else
594                         eap = NO_INNER;
595         }
596
597         char *val;
598         val = reply + 1;
599         while (*(val + 1))
600                 val++;
601         if (*val == '"')
602                 *val = '\0';
603
604         switch (eap) {
605         case PEAP_INNER:
606                 if (strncmp(reply, "\"auth=", 6))
607                         break;
608                 val = reply + 2;
609                 memcpy(val, "EAP-", 4);
610                 break;
611         case TTLS_INNER:
612                 if (strncmp(reply, "\"autheap=", 9) == 0) {
613                         val = reply + 5;
614                         memcpy(val, "EAP-", 4);
615                 } else if (strncmp(reply, "\"auth=", 6) == 0)
616                         val = reply + 6;
617                 break;
618         case FAST_INNER:
619                 if (strncmp(reply, "\"auth=", 6))
620                         break;
621                 if (strcmp(reply + 6, "GTC MSCHAPV2") == 0) {
622                         val = "GTC(auth) + MSCHAPv2(prov)";
623                         break;
624                 }
625                 val = reply + 2;
626                 memcpy(val, "EAP-", 4);
627                 break;
628         case NO_INNER:
629                 break;
630         }
631
632         for (i = 0; i < phase2Select->count(); i++) {
633                 if (phase2Select->itemText(i).compare(val) == 0) {
634                         phase2Select->setCurrentIndex(i);
635                         break;
636                 }
637         }
638
639         for (i = 0; i < 4; i++) {
640                 QLineEdit *wepEdit;
641                 switch (i) {
642                 default:
643                 case 0:
644                         wepEdit = wep0Edit;
645                         break;
646                 case 1:
647                         wepEdit = wep1Edit;
648                         break;
649                 case 2:
650                         wepEdit = wep2Edit;
651                         break;
652                 case 3:
653                         wepEdit = wep3Edit;
654                         break;
655                 }
656                 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_key%d",
657                          network_id, i);
658                 reply_len = sizeof(reply) - 1;
659                 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
660                 if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
661                         reply[reply_len] = '\0';
662                         pos = strchr(reply + 1, '"');
663                         if (pos)
664                                 *pos = '\0';
665                         if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
666                                 encr = 1;
667
668                         wepEdit->setText(reply + 1);
669                 } else if (res >= 0 && key_value_isset(reply, reply_len)) {
670                         if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
671                                 encr = 1;
672                         wepEdit->setText(WPA_GUI_KEY_DATA);
673                 }
674         }
675
676         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_tx_keyidx", network_id);
677         reply_len = sizeof(reply) - 1;
678         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1)
679         {
680                 reply[reply_len] = '\0';
681                 switch (atoi(reply)) {
682                 case 0:
683                         wep0Radio->setChecked(true);
684                         break;
685                 case 1:
686                         wep1Radio->setChecked(true);
687                         break;
688                 case 2:
689                         wep2Radio->setChecked(true);
690                         break;
691                 case 3:
692                         wep3Radio->setChecked(true);
693                         break;
694                 }
695         }
696
697         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d id_str", network_id);
698         reply_len = sizeof(reply) - 1;
699         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
700             reply_len >= 2 && reply[0] == '"') {
701                 reply[reply_len] = '\0';
702                 pos = strchr(reply + 1, '"');
703                 if (pos)
704                         *pos = '\0';
705                 idstrEdit->setText(reply + 1);
706         }
707
708         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d priority", network_id);
709         reply_len = sizeof(reply) - 1;
710         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1)
711         {
712                 reply[reply_len] = '\0';
713                 prioritySpinBox->setValue(atoi(reply));
714         }
715
716         authSelect->setCurrentIndex(auth);
717         authChanged(auth);
718         encrSelect->setCurrentIndex(encr);
719         if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
720                 wepEnabled(encr == 1);
721
722         removeButton->setEnabled(true);
723         addButton->setText("Save");
724 }
725
726
727 void NetworkConfig::removeNetwork()
728 {
729         char reply[10], cmd[256];
730         size_t reply_len;
731
732         if (QMessageBox::information(this, "wpa_gui",
733                                      "This will permanently remove the "
734                                      "network\n"
735                                      "from the configuration. Do you really "
736                                      "want\n"
737                                      "to remove this network?", "Yes", "No")
738             != 0)
739                 return;
740
741         snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %d", edit_network_id);
742         reply_len = sizeof(reply);
743         wpagui->ctrlRequest(cmd, reply, &reply_len);
744         if (strncmp(reply, "OK", 2) != 0) {
745                 QMessageBox::warning(this, "wpa_gui",
746                                      "Failed to remove network from "
747                                      "wpa_supplicant\n"
748                                      "configuration.");
749         } else {
750                 wpagui->triggerUpdate();
751                 wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
752         }
753
754         close();
755 }
756
757
758 void NetworkConfig::newNetwork()
759 {
760         new_network = true;
761         getEapCapa();
762 }
763
764
765 void NetworkConfig::getEapCapa()
766 {
767         char reply[256];
768         size_t reply_len;
769
770         if (wpagui == NULL)
771                 return;
772
773         reply_len = sizeof(reply) - 1;
774         if (wpagui->ctrlRequest("GET_CAPABILITY eap", reply, &reply_len) < 0)
775                 return;
776         reply[reply_len] = '\0';
777
778         QString res(reply);
779         QStringList types = res.split(QChar(' '));
780         eapSelect->insertItems(-1, types);
781 }