9db95d93027455733d309cd6e1d6d944c5163992
[mech_eap.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                 else
287                         setNetworkParam(id, "pcsc", "NULL", false);
288         }
289         if (phase2Select->isEnabled()) {
290                 QString eap = eapSelect->currentText();
291                 QString inner = phase2Select->currentText();
292                 char phase2[32];
293                 phase2[0] = '\0';
294                 if (eap.compare("PEAP") == 0) {
295                         if (inner.startsWith("EAP-"))
296                                 snprintf(phase2, sizeof(phase2), "auth=%s",
297                                          inner.right(inner.size() - 4).
298                                          toAscii().constData());
299                 } else if (eap.compare("TTLS") == 0) {
300                         if (inner.startsWith("EAP-"))
301                                 snprintf(phase2, sizeof(phase2), "autheap=%s",
302                                          inner.right(inner.size() - 4).
303                                          toAscii().constData());
304                         else
305                                 snprintf(phase2, sizeof(phase2), "auth=%s",
306                                          inner.toAscii().constData());
307                 } else if (eap.compare("FAST") == 0) {
308                         if (inner.startsWith("EAP-"))
309                                 snprintf(phase2, sizeof(phase2), "auth=%s",
310                                          inner.right(inner.size() - 4).
311                                          toAscii().constData());
312                         else if (inner.compare("GTC(auth) + MSCHAPv2(prov)") ==
313                                  0) {
314                                 snprintf(phase2, sizeof(phase2),
315                                          "auth=GTC MSCHAPV2");
316                         }
317                 }
318                 if (phase2[0])
319                         setNetworkParam(id, "phase2", phase2, true);
320                 else
321                         setNetworkParam(id, "phase2", "NULL", false);
322         } else
323                 setNetworkParam(id, "phase2", "NULL", false);
324         if (identityEdit->isEnabled() && identityEdit->text().length() > 0)
325                 setNetworkParam(id, "identity",
326                                 identityEdit->text().toAscii().constData(),
327                                 true);
328         else
329                 setNetworkParam(id, "identity", "NULL", false);
330         if (passwordEdit->isEnabled() && passwordEdit->text().length() > 0 &&
331             strcmp(passwordEdit->text().toAscii().constData(),
332                    WPA_GUI_KEY_DATA) != 0)
333                 setNetworkParam(id, "password",
334                                 passwordEdit->text().toAscii().constData(),
335                                 true);
336         else if (passwordEdit->text().length() == 0)
337                 setNetworkParam(id, "password", "NULL", false);
338         if (cacertEdit->isEnabled() && cacertEdit->text().length() > 0)
339                 setNetworkParam(id, "ca_cert",
340                                 cacertEdit->text().toAscii().constData(),
341                                 true);
342         else
343                 setNetworkParam(id, "ca_cert", "NULL", false);
344         writeWepKey(id, wep0Edit, 0);
345         writeWepKey(id, wep1Edit, 1);
346         writeWepKey(id, wep2Edit, 2);
347         writeWepKey(id, wep3Edit, 3);
348
349         if (wep0Radio->isEnabled() && wep0Radio->isChecked())
350                 setNetworkParam(id, "wep_tx_keyidx", "0", false);
351         else if (wep1Radio->isEnabled() && wep1Radio->isChecked())
352                 setNetworkParam(id, "wep_tx_keyidx", "1", false);
353         else if (wep2Radio->isEnabled() && wep2Radio->isChecked())
354                 setNetworkParam(id, "wep_tx_keyidx", "2", false);
355         else if (wep3Radio->isEnabled() && wep3Radio->isChecked())
356                 setNetworkParam(id, "wep_tx_keyidx", "3", false);
357
358         if (idstrEdit->isEnabled() && idstrEdit->text().length() > 0)
359                 setNetworkParam(id, "id_str",
360                                 idstrEdit->text().toAscii().constData(),
361                                 true);
362         else
363                 setNetworkParam(id, "id_str", "NULL", false);
364
365         if (prioritySpinBox->isEnabled()) {
366                 QString prio;
367                 prio = prio.setNum(prioritySpinBox->value());
368                 setNetworkParam(id, "priority", prio.toAscii().constData(),
369                                 false);
370         }
371
372         snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %d", id);
373         reply_len = sizeof(reply);
374         wpagui->ctrlRequest(cmd, reply, &reply_len);
375         if (strncmp(reply, "OK", 2) != 0) {
376                 QMessageBox::warning(this, "wpa_gui", "Failed to enable "
377                                      "network in wpa_supplicant\n"
378                                      "configuration.");
379                 /* Network was added, so continue anyway */
380         }
381         wpagui->triggerUpdate();
382         wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
383
384         close();
385 }
386
387
388 void NetworkConfig::setWpaGui(WpaGui *_wpagui)
389 {
390         wpagui = _wpagui;
391 }
392
393
394 int NetworkConfig::setNetworkParam(int id, const char *field,
395                                    const char *value, bool quote)
396 {
397         char reply[10], cmd[256];
398         size_t reply_len;
399         snprintf(cmd, sizeof(cmd), "SET_NETWORK %d %s %s%s%s",
400                  id, field, quote ? "\"" : "", value, quote ? "\"" : "");
401         reply_len = sizeof(reply);
402         wpagui->ctrlRequest(cmd, reply, &reply_len);
403         return strncmp(reply, "OK", 2) == 0 ? 0 : -1;
404 }
405
406
407 void NetworkConfig::encrChanged(const QString &sel)
408 {
409         wepEnabled(sel.indexOf("WEP") == 0);
410 }
411
412
413 void NetworkConfig::wepEnabled(bool enabled)
414 {
415         wep0Edit->setEnabled(enabled);
416         wep1Edit->setEnabled(enabled);
417         wep2Edit->setEnabled(enabled);
418         wep3Edit->setEnabled(enabled);
419         wep0Radio->setEnabled(enabled);
420         wep1Radio->setEnabled(enabled);
421         wep2Radio->setEnabled(enabled);
422         wep3Radio->setEnabled(enabled);
423 }
424
425
426 void NetworkConfig::writeWepKey(int network_id, QLineEdit *edit, int id)
427 {
428         char buf[10];
429         bool hex;
430         const char *txt, *pos;
431         size_t len;
432
433         if (!edit->isEnabled() || edit->text().isEmpty())
434                 return;
435
436         /*
437          * Assume hex key if only hex characters are present and length matches
438          * with 40, 104, or 128-bit key
439          */
440         txt = edit->text().toAscii().constData();
441         if (strcmp(txt, WPA_GUI_KEY_DATA) == 0)
442                 return;
443         len = strlen(txt);
444         if (len == 0)
445                 return;
446         pos = txt;
447         hex = true;
448         while (*pos) {
449                 if (!((*pos >= '0' && *pos <= '9') ||
450                       (*pos >= 'a' && *pos <= 'f') ||
451                       (*pos >= 'A' && *pos <= 'F'))) {
452                         hex = false;
453                         break;
454                 }
455                 pos++;
456         }
457         if (hex && len != 10 && len != 26 && len != 32)
458                 hex = false;
459         snprintf(buf, sizeof(buf), "wep_key%d", id);
460         setNetworkParam(network_id, buf, txt, !hex);
461 }
462
463
464 static int key_value_isset(const char *reply, size_t reply_len)
465 {
466     return reply_len > 0 && (reply_len < 4 || memcmp(reply, "FAIL", 4) != 0);
467 }
468
469
470 void NetworkConfig::paramsFromConfig(int network_id)
471 {
472         int i, res;
473
474         edit_network_id = network_id;
475         getEapCapa();
476
477         char reply[1024], cmd[256], *pos;
478         size_t reply_len;
479
480         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ssid", network_id);
481         reply_len = sizeof(reply) - 1;
482         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
483             reply_len >= 2 && reply[0] == '"') {
484                 reply[reply_len] = '\0';
485                 pos = strchr(reply + 1, '"');
486                 if (pos)
487                         *pos = '\0';
488                 ssidEdit->setText(reply + 1);
489         }
490
491         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d proto", network_id);
492         reply_len = sizeof(reply) - 1;
493         int wpa = 0;
494         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
495                 reply[reply_len] = '\0';
496                 if (strstr(reply, "RSN") || strstr(reply, "WPA2"))
497                         wpa = 2;
498                 else if (strstr(reply, "WPA"))
499                         wpa = 1;
500         }
501
502         int auth = AUTH_NONE, encr = 0;
503         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d key_mgmt", 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, "WPA-EAP"))
508                         auth = wpa & 2 ? AUTH_WPA2_EAP : AUTH_WPA_EAP;
509                 else if (strstr(reply, "WPA-PSK"))
510                         auth = wpa & 2 ? AUTH_WPA2_PSK : AUTH_WPA_PSK;
511                 else if (strstr(reply, "IEEE8021X")) {
512                         auth = AUTH_IEEE8021X;
513                         encr = 1;
514                 }
515         }
516
517         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d pairwise", network_id);
518         reply_len = sizeof(reply) - 1;
519         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
520                 reply[reply_len] = '\0';
521                 if (strstr(reply, "CCMP") && auth != AUTH_NONE)
522                         encr = 1;
523                 else if (strstr(reply, "TKIP"))
524                         encr = 0;
525                 else if (strstr(reply, "WEP"))
526                         encr = 1;
527                 else
528                         encr = 0;
529         }
530
531         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d psk", network_id);
532         reply_len = sizeof(reply) - 1;
533         res = wpagui->ctrlRequest(cmd, reply, &reply_len);
534         if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
535                 reply[reply_len] = '\0';
536                 pos = strchr(reply + 1, '"');
537                 if (pos)
538                         *pos = '\0';
539                 pskEdit->setText(reply + 1);
540         } else if (res >= 0 && key_value_isset(reply, reply_len)) {
541                 pskEdit->setText(WPA_GUI_KEY_DATA);
542         }
543
544         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d identity", network_id);
545         reply_len = sizeof(reply) - 1;
546         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
547             reply_len >= 2 && reply[0] == '"') {
548                 reply[reply_len] = '\0';
549                 pos = strchr(reply + 1, '"');
550                 if (pos)
551                         *pos = '\0';
552                 identityEdit->setText(reply + 1);
553         }
554
555         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d password", network_id);
556         reply_len = sizeof(reply) - 1;
557         res = wpagui->ctrlRequest(cmd, reply, &reply_len);
558         if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
559                 reply[reply_len] = '\0';
560                 pos = strchr(reply + 1, '"');
561                 if (pos)
562                         *pos = '\0';
563                 passwordEdit->setText(reply + 1);
564         } else if (res >= 0 && key_value_isset(reply, reply_len)) {
565                 passwordEdit->setText(WPA_GUI_KEY_DATA);
566         }
567
568         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ca_cert", network_id);
569         reply_len = sizeof(reply) - 1;
570         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
571             reply_len >= 2 && reply[0] == '"') {
572                 reply[reply_len] = '\0';
573                 pos = strchr(reply + 1, '"');
574                 if (pos)
575                         *pos = '\0';
576                 cacertEdit->setText(reply + 1);
577         }
578
579         enum { NO_INNER, PEAP_INNER, TTLS_INNER, FAST_INNER } eap = NO_INNER;
580         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d eap", network_id);
581         reply_len = sizeof(reply) - 1;
582         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
583             reply_len >= 1) {
584                 reply[reply_len] = '\0';
585                 for (i = 0; i < eapSelect->count(); i++) {
586                         if (eapSelect->itemText(i).compare(reply) == 0) {
587                                 eapSelect->setCurrentIndex(i);
588                                 if (strcmp(reply, "PEAP") == 0)
589                                         eap = PEAP_INNER;
590                                 else if (strcmp(reply, "TTLS") == 0)
591                                         eap = TTLS_INNER;
592                                 else if (strcmp(reply, "FAST") == 0)
593                                         eap = FAST_INNER;
594                                 break;
595                         }
596                 }
597         }
598
599         if (eap != NO_INNER) {
600                 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d phase2",
601                          network_id);
602                 reply_len = sizeof(reply) - 1;
603                 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
604                     reply_len >= 1) {
605                         reply[reply_len] = '\0';
606                         eapChanged(eapSelect->currentIndex());
607                 } else
608                         eap = NO_INNER;
609         }
610
611         char *val;
612         val = reply + 1;
613         while (*(val + 1))
614                 val++;
615         if (*val == '"')
616                 *val = '\0';
617
618         switch (eap) {
619         case PEAP_INNER:
620                 if (strncmp(reply, "\"auth=", 6))
621                         break;
622                 val = reply + 2;
623                 memcpy(val, "EAP-", 4);
624                 break;
625         case TTLS_INNER:
626                 if (strncmp(reply, "\"autheap=", 9) == 0) {
627                         val = reply + 5;
628                         memcpy(val, "EAP-", 4);
629                 } else if (strncmp(reply, "\"auth=", 6) == 0)
630                         val = reply + 6;
631                 break;
632         case FAST_INNER:
633                 if (strncmp(reply, "\"auth=", 6))
634                         break;
635                 if (strcmp(reply + 6, "GTC MSCHAPV2") == 0) {
636                         val = "GTC(auth) + MSCHAPv2(prov)";
637                         break;
638                 }
639                 val = reply + 2;
640                 memcpy(val, "EAP-", 4);
641                 break;
642         case NO_INNER:
643                 break;
644         }
645
646         for (i = 0; i < phase2Select->count(); i++) {
647                 if (phase2Select->itemText(i).compare(val) == 0) {
648                         phase2Select->setCurrentIndex(i);
649                         break;
650                 }
651         }
652
653         for (i = 0; i < 4; i++) {
654                 QLineEdit *wepEdit;
655                 switch (i) {
656                 default:
657                 case 0:
658                         wepEdit = wep0Edit;
659                         break;
660                 case 1:
661                         wepEdit = wep1Edit;
662                         break;
663                 case 2:
664                         wepEdit = wep2Edit;
665                         break;
666                 case 3:
667                         wepEdit = wep3Edit;
668                         break;
669                 }
670                 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_key%d",
671                          network_id, i);
672                 reply_len = sizeof(reply) - 1;
673                 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
674                 if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
675                         reply[reply_len] = '\0';
676                         pos = strchr(reply + 1, '"');
677                         if (pos)
678                                 *pos = '\0';
679                         if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
680                                 encr = 1;
681
682                         wepEdit->setText(reply + 1);
683                 } else if (res >= 0 && key_value_isset(reply, reply_len)) {
684                         if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
685                                 encr = 1;
686                         wepEdit->setText(WPA_GUI_KEY_DATA);
687                 }
688         }
689
690         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_tx_keyidx", network_id);
691         reply_len = sizeof(reply) - 1;
692         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1)
693         {
694                 reply[reply_len] = '\0';
695                 switch (atoi(reply)) {
696                 case 0:
697                         wep0Radio->setChecked(true);
698                         break;
699                 case 1:
700                         wep1Radio->setChecked(true);
701                         break;
702                 case 2:
703                         wep2Radio->setChecked(true);
704                         break;
705                 case 3:
706                         wep3Radio->setChecked(true);
707                         break;
708                 }
709         }
710
711         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d id_str", network_id);
712         reply_len = sizeof(reply) - 1;
713         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
714             reply_len >= 2 && reply[0] == '"') {
715                 reply[reply_len] = '\0';
716                 pos = strchr(reply + 1, '"');
717                 if (pos)
718                         *pos = '\0';
719                 idstrEdit->setText(reply + 1);
720         }
721
722         snprintf(cmd, sizeof(cmd), "GET_NETWORK %d priority", network_id);
723         reply_len = sizeof(reply) - 1;
724         if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1)
725         {
726                 reply[reply_len] = '\0';
727                 prioritySpinBox->setValue(atoi(reply));
728         }
729
730         authSelect->setCurrentIndex(auth);
731         authChanged(auth);
732         encrSelect->setCurrentIndex(encr);
733         if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
734                 wepEnabled(encr == 1);
735
736         removeButton->setEnabled(true);
737         addButton->setText("Save");
738 }
739
740
741 void NetworkConfig::removeNetwork()
742 {
743         char reply[10], cmd[256];
744         size_t reply_len;
745
746         if (QMessageBox::information(this, "wpa_gui",
747                                      "This will permanently remove the "
748                                      "network\n"
749                                      "from the configuration. Do you really "
750                                      "want\n"
751                                      "to remove this network?", "Yes", "No")
752             != 0)
753                 return;
754
755         snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %d", edit_network_id);
756         reply_len = sizeof(reply);
757         wpagui->ctrlRequest(cmd, reply, &reply_len);
758         if (strncmp(reply, "OK", 2) != 0) {
759                 QMessageBox::warning(this, "wpa_gui",
760                                      "Failed to remove network from "
761                                      "wpa_supplicant\n"
762                                      "configuration.");
763         } else {
764                 wpagui->triggerUpdate();
765                 wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
766         }
767
768         close();
769 }
770
771
772 void NetworkConfig::newNetwork()
773 {
774         new_network = true;
775         getEapCapa();
776 }
777
778
779 void NetworkConfig::getEapCapa()
780 {
781         char reply[256];
782         size_t reply_len;
783
784         if (wpagui == NULL)
785                 return;
786
787         reply_len = sizeof(reply) - 1;
788         if (wpagui->ctrlRequest("GET_CAPABILITY eap", reply, &reply_len) < 0)
789                 return;
790         reply[reply_len] = '\0';
791
792         QString res(reply);
793         QStringList types = res.split(QChar(' '));
794         eapSelect->insertItems(-1, types);
795 }