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