2 * wpa_gui - NetworkConfig class
3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #include <QMessageBox>
18 #include "networkconfig.h"
30 #define WPA_GUI_KEY_DATA "[key is configured]"
33 NetworkConfig::NetworkConfig(QWidget *parent, const char *, bool, Qt::WFlags)
38 connect(authSelect, SIGNAL(activated(int)), this,
39 SLOT(authChanged(int)));
40 connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
41 connect(addButton, SIGNAL(clicked()), this, SLOT(addNetwork()));
42 connect(encrSelect, SIGNAL(activated(const QString &)), this,
43 SLOT(encrChanged(const QString &)));
44 connect(removeButton, SIGNAL(clicked()), this, SLOT(removeNetwork()));
45 connect(eapSelect, SIGNAL(activated(int)), this,
46 SLOT(eapChanged(int)));
53 NetworkConfig::~NetworkConfig()
58 void NetworkConfig::languageChange()
64 void NetworkConfig::paramsFromScanResults(QTreeWidgetItem *sel)
68 /* SSID BSSID frequency signal flags */
69 setWindowTitle(sel->text(0));
70 ssidEdit->setText(sel->text(0));
72 QString flags = sel->text(4);
74 if (flags.indexOf("[WPA2-EAP") >= 0)
76 else if (flags.indexOf("[WPA-EAP") >= 0)
78 else if (flags.indexOf("[WPA2-PSK") >= 0)
80 else if (flags.indexOf("[WPA-PSK") >= 0)
85 if (flags.indexOf("-CCMP") >= 0)
87 else if (flags.indexOf("-TKIP") >= 0)
89 else if (flags.indexOf("WEP") >= 0)
94 authSelect->setCurrentIndex(auth);
96 encrSelect->setCurrentIndex(encr);
98 wepEnabled(auth == AUTH_NONE && encr == 1);
104 void NetworkConfig::authChanged(int sel)
106 pskEdit->setEnabled(sel == AUTH_WPA_PSK || sel == AUTH_WPA2_PSK);
107 bool eap = sel == AUTH_IEEE8021X || sel == AUTH_WPA_EAP ||
108 sel == AUTH_WPA2_EAP;
109 eapSelect->setEnabled(eap);
110 identityEdit->setEnabled(eap);
111 passwordEdit->setEnabled(eap);
112 cacertEdit->setEnabled(eap);
113 phase2Select->setEnabled(eap);
115 eapChanged(eapSelect->currentIndex());
117 while (encrSelect->count())
118 encrSelect->removeItem(0);
120 if (sel == AUTH_NONE || sel == AUTH_IEEE8021X) {
121 encrSelect->addItem("None");
122 encrSelect->addItem("WEP");
123 encrSelect->setCurrentIndex(sel == AUTH_NONE ? 0 : 1);
125 encrSelect->addItem("TKIP");
126 encrSelect->addItem("CCMP");
127 encrSelect->setCurrentIndex((sel == AUTH_WPA2_PSK ||
128 sel == AUTH_WPA2_EAP) ? 1 : 0);
131 wepEnabled(sel == AUTH_IEEE8021X);
135 void NetworkConfig::eapChanged(int sel)
137 QString prev_val = phase2Select->currentText();
138 while (phase2Select->count())
139 phase2Select->removeItem(0);
142 inner << "PEAP" << "TTLS" << "FAST";
143 if (!inner.contains(eapSelect->itemText(sel)))
146 phase2Select->addItem("[ any ]");
148 /* Add special cases based on outer method */
149 if (eapSelect->currentText().compare("TTLS") == 0) {
150 phase2Select->addItem("PAP");
151 phase2Select->addItem("CHAP");
152 phase2Select->addItem("MSCHAP");
153 phase2Select->addItem("MSCHAPv2");
154 } else if (eapSelect->currentText().compare("FAST") == 0)
155 phase2Select->addItem("GTC(auth) + MSCHAPv2(prov)");
157 /* Add all enabled EAP methods that can be used in the tunnel */
160 allowed << "MSCHAPV2" << "MD5" << "GTC" << "TLS" << "OTP" << "SIM"
162 for (i = 0; i < eapSelect->count(); i++) {
163 if (allowed.contains(eapSelect->itemText(i))) {
164 phase2Select->addItem("EAP-" + eapSelect->itemText(i));
168 for (i = 0; i < phase2Select->count(); i++) {
169 if (phase2Select->itemText(i).compare(prev_val) == 0) {
170 phase2Select->setCurrentIndex(i);
177 void NetworkConfig::addNetwork()
179 char reply[10], cmd[256];
182 int psklen = pskEdit->text().length();
183 int auth = authSelect->currentIndex();
185 if (auth == AUTH_WPA_PSK || auth == AUTH_WPA2_PSK) {
186 if (psklen < 8 || psklen > 64) {
187 QMessageBox::warning(this, "WPA Pre-Shared Key Error",
188 "WPA-PSK requires a passphrase "
189 "of 8 to 63 characters\n"
190 "or 64 hex digit PSK");
196 if (idstrEdit->isEnabled() && !idstrEdit->text().isEmpty()) {
197 QRegExp rx("^(\\w|-)+$");
198 if (rx.indexIn(idstrEdit->text()) < 0) {
199 QMessageBox::warning(this, "Network ID Error",
200 "Network ID String contains "
201 "non-word characters.\n"
202 "It must be a simple string, "
203 "without spaces, containing\n"
204 "only characters in this range: "
206 idstrEdit->setFocus();
214 memset(reply, 0, sizeof(reply));
215 reply_len = sizeof(reply) - 1;
218 wpagui->ctrlRequest("ADD_NETWORK", reply, &reply_len);
219 if (reply[0] == 'F') {
220 QMessageBox::warning(this, "wpa_gui", "Failed to add "
221 "network to wpa_supplicant\n"
227 id = edit_network_id;
229 setNetworkParam(id, "ssid", ssidEdit->text().toAscii().constData(),
232 const char *key_mgmt = NULL, *proto = NULL, *pairwise = NULL;
238 key_mgmt = "IEEE8021X";
241 key_mgmt = "WPA-PSK";
245 key_mgmt = "WPA-EAP";
249 key_mgmt = "WPA-PSK";
253 key_mgmt = "WPA-EAP";
258 if (auth == AUTH_WPA_PSK || auth == AUTH_WPA_EAP ||
259 auth == AUTH_WPA2_PSK || auth == AUTH_WPA2_EAP) {
260 int encr = encrSelect->currentIndex();
268 setNetworkParam(id, "proto", proto, false);
270 setNetworkParam(id, "key_mgmt", key_mgmt, false);
272 setNetworkParam(id, "pairwise", pairwise, false);
273 setNetworkParam(id, "group", "TKIP CCMP WEP104 WEP40", false);
275 if (pskEdit->isEnabled() &&
276 strcmp(passwordEdit->text().toAscii().constData(),
277 WPA_GUI_KEY_DATA) != 0)
278 setNetworkParam(id, "psk",
279 pskEdit->text().toAscii().constData(),
281 if (eapSelect->isEnabled()) {
283 eapSelect->currentText().toAscii().constData();
284 setNetworkParam(id, "eap", eap, false);
285 if (strcmp(eap, "SIM") == 0 || strcmp(eap, "AKA") == 0)
286 setNetworkParam(id, "pcsc", "", true);
288 setNetworkParam(id, "pcsc", "NULL", false);
290 if (phase2Select->isEnabled()) {
291 QString eap = eapSelect->currentText();
292 QString inner = phase2Select->currentText();
295 if (eap.compare("PEAP") == 0) {
296 if (inner.startsWith("EAP-"))
297 snprintf(phase2, sizeof(phase2), "auth=%s",
298 inner.right(inner.size() - 4).
299 toAscii().constData());
300 } else if (eap.compare("TTLS") == 0) {
301 if (inner.startsWith("EAP-"))
302 snprintf(phase2, sizeof(phase2), "autheap=%s",
303 inner.right(inner.size() - 4).
304 toAscii().constData());
306 snprintf(phase2, sizeof(phase2), "auth=%s",
307 inner.toAscii().constData());
308 } else if (eap.compare("FAST") == 0) {
309 const char *provisioning = NULL;
310 if (inner.startsWith("EAP-")) {
311 snprintf(phase2, sizeof(phase2), "auth=%s",
312 inner.right(inner.size() - 4).
313 toAscii().constData());
314 provisioning = "fast_provisioning=2";
315 } else if (inner.compare("GTC(auth) + MSCHAPv2(prov)")
317 snprintf(phase2, sizeof(phase2),
318 "auth=GTC auth=MSCHAPV2");
319 provisioning = "fast_provisioning=1";
321 provisioning = "fast_provisioning=3";
324 setNetworkParam(id, "phase1", provisioning,
326 snprintf(blob, sizeof(blob),
327 "blob://fast-pac-%d", id);
328 setNetworkParam(id, "pac_file", blob, true);
332 setNetworkParam(id, "phase2", phase2, true);
334 setNetworkParam(id, "phase2", "NULL", false);
336 setNetworkParam(id, "phase2", "NULL", false);
337 if (identityEdit->isEnabled() && identityEdit->text().length() > 0)
338 setNetworkParam(id, "identity",
339 identityEdit->text().toAscii().constData(),
342 setNetworkParam(id, "identity", "NULL", false);
343 if (passwordEdit->isEnabled() && passwordEdit->text().length() > 0 &&
344 strcmp(passwordEdit->text().toAscii().constData(),
345 WPA_GUI_KEY_DATA) != 0)
346 setNetworkParam(id, "password",
347 passwordEdit->text().toAscii().constData(),
349 else if (passwordEdit->text().length() == 0)
350 setNetworkParam(id, "password", "NULL", false);
351 if (cacertEdit->isEnabled() && cacertEdit->text().length() > 0)
352 setNetworkParam(id, "ca_cert",
353 cacertEdit->text().toAscii().constData(),
356 setNetworkParam(id, "ca_cert", "NULL", false);
357 writeWepKey(id, wep0Edit, 0);
358 writeWepKey(id, wep1Edit, 1);
359 writeWepKey(id, wep2Edit, 2);
360 writeWepKey(id, wep3Edit, 3);
362 if (wep0Radio->isEnabled() && wep0Radio->isChecked())
363 setNetworkParam(id, "wep_tx_keyidx", "0", false);
364 else if (wep1Radio->isEnabled() && wep1Radio->isChecked())
365 setNetworkParam(id, "wep_tx_keyidx", "1", false);
366 else if (wep2Radio->isEnabled() && wep2Radio->isChecked())
367 setNetworkParam(id, "wep_tx_keyidx", "2", false);
368 else if (wep3Radio->isEnabled() && wep3Radio->isChecked())
369 setNetworkParam(id, "wep_tx_keyidx", "3", false);
371 if (idstrEdit->isEnabled() && idstrEdit->text().length() > 0)
372 setNetworkParam(id, "id_str",
373 idstrEdit->text().toAscii().constData(),
376 setNetworkParam(id, "id_str", "NULL", false);
378 if (prioritySpinBox->isEnabled()) {
380 prio = prio.setNum(prioritySpinBox->value());
381 setNetworkParam(id, "priority", prio.toAscii().constData(),
385 snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %d", id);
386 reply_len = sizeof(reply);
387 wpagui->ctrlRequest(cmd, reply, &reply_len);
388 if (strncmp(reply, "OK", 2) != 0) {
389 QMessageBox::warning(this, "wpa_gui", "Failed to enable "
390 "network in wpa_supplicant\n"
392 /* Network was added, so continue anyway */
394 wpagui->triggerUpdate();
395 wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
401 void NetworkConfig::setWpaGui(WpaGui *_wpagui)
407 int NetworkConfig::setNetworkParam(int id, const char *field,
408 const char *value, bool quote)
410 char reply[10], cmd[256];
412 snprintf(cmd, sizeof(cmd), "SET_NETWORK %d %s %s%s%s",
413 id, field, quote ? "\"" : "", value, quote ? "\"" : "");
414 reply_len = sizeof(reply);
415 wpagui->ctrlRequest(cmd, reply, &reply_len);
416 return strncmp(reply, "OK", 2) == 0 ? 0 : -1;
420 void NetworkConfig::encrChanged(const QString &sel)
422 wepEnabled(sel.indexOf("WEP") == 0);
426 void NetworkConfig::wepEnabled(bool enabled)
428 wep0Edit->setEnabled(enabled);
429 wep1Edit->setEnabled(enabled);
430 wep2Edit->setEnabled(enabled);
431 wep3Edit->setEnabled(enabled);
432 wep0Radio->setEnabled(enabled);
433 wep1Radio->setEnabled(enabled);
434 wep2Radio->setEnabled(enabled);
435 wep3Radio->setEnabled(enabled);
439 void NetworkConfig::writeWepKey(int network_id, QLineEdit *edit, int id)
443 const char *txt, *pos;
446 if (!edit->isEnabled() || edit->text().isEmpty())
450 * Assume hex key if only hex characters are present and length matches
451 * with 40, 104, or 128-bit key
453 txt = edit->text().toAscii().constData();
454 if (strcmp(txt, WPA_GUI_KEY_DATA) == 0)
462 if (!((*pos >= '0' && *pos <= '9') ||
463 (*pos >= 'a' && *pos <= 'f') ||
464 (*pos >= 'A' && *pos <= 'F'))) {
470 if (hex && len != 10 && len != 26 && len != 32)
472 snprintf(buf, sizeof(buf), "wep_key%d", id);
473 setNetworkParam(network_id, buf, txt, !hex);
477 static int key_value_isset(const char *reply, size_t reply_len)
479 return reply_len > 0 && (reply_len < 4 || memcmp(reply, "FAIL", 4) != 0);
483 void NetworkConfig::paramsFromConfig(int network_id)
487 edit_network_id = network_id;
490 char reply[1024], cmd[256], *pos;
493 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ssid", network_id);
494 reply_len = sizeof(reply) - 1;
495 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
496 reply_len >= 2 && reply[0] == '"') {
497 reply[reply_len] = '\0';
498 pos = strchr(reply + 1, '"');
501 ssidEdit->setText(reply + 1);
504 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d proto", network_id);
505 reply_len = sizeof(reply) - 1;
507 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
508 reply[reply_len] = '\0';
509 if (strstr(reply, "RSN") || strstr(reply, "WPA2"))
511 else if (strstr(reply, "WPA"))
515 int auth = AUTH_NONE, encr = 0;
516 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d key_mgmt", network_id);
517 reply_len = sizeof(reply) - 1;
518 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
519 reply[reply_len] = '\0';
520 if (strstr(reply, "WPA-EAP"))
521 auth = wpa & 2 ? AUTH_WPA2_EAP : AUTH_WPA_EAP;
522 else if (strstr(reply, "WPA-PSK"))
523 auth = wpa & 2 ? AUTH_WPA2_PSK : AUTH_WPA_PSK;
524 else if (strstr(reply, "IEEE8021X")) {
525 auth = AUTH_IEEE8021X;
530 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d pairwise", network_id);
531 reply_len = sizeof(reply) - 1;
532 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
533 reply[reply_len] = '\0';
534 if (strstr(reply, "CCMP") && auth != AUTH_NONE)
536 else if (strstr(reply, "TKIP"))
538 else if (strstr(reply, "WEP"))
544 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d psk", network_id);
545 reply_len = sizeof(reply) - 1;
546 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
547 if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
548 reply[reply_len] = '\0';
549 pos = strchr(reply + 1, '"');
552 pskEdit->setText(reply + 1);
553 } else if (res >= 0 && key_value_isset(reply, reply_len)) {
554 pskEdit->setText(WPA_GUI_KEY_DATA);
557 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d identity", network_id);
558 reply_len = sizeof(reply) - 1;
559 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
560 reply_len >= 2 && reply[0] == '"') {
561 reply[reply_len] = '\0';
562 pos = strchr(reply + 1, '"');
565 identityEdit->setText(reply + 1);
568 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d password", network_id);
569 reply_len = sizeof(reply) - 1;
570 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
571 if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
572 reply[reply_len] = '\0';
573 pos = strchr(reply + 1, '"');
576 passwordEdit->setText(reply + 1);
577 } else if (res >= 0 && key_value_isset(reply, reply_len)) {
578 passwordEdit->setText(WPA_GUI_KEY_DATA);
581 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ca_cert", network_id);
582 reply_len = sizeof(reply) - 1;
583 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
584 reply_len >= 2 && reply[0] == '"') {
585 reply[reply_len] = '\0';
586 pos = strchr(reply + 1, '"');
589 cacertEdit->setText(reply + 1);
592 enum { NO_INNER, PEAP_INNER, TTLS_INNER, FAST_INNER } eap = NO_INNER;
593 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d eap", network_id);
594 reply_len = sizeof(reply) - 1;
595 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
597 reply[reply_len] = '\0';
598 for (i = 0; i < eapSelect->count(); i++) {
599 if (eapSelect->itemText(i).compare(reply) == 0) {
600 eapSelect->setCurrentIndex(i);
601 if (strcmp(reply, "PEAP") == 0)
603 else if (strcmp(reply, "TTLS") == 0)
605 else if (strcmp(reply, "FAST") == 0)
612 if (eap != NO_INNER) {
613 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d phase2",
615 reply_len = sizeof(reply) - 1;
616 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
618 reply[reply_len] = '\0';
619 eapChanged(eapSelect->currentIndex());
633 if (strncmp(reply, "\"auth=", 6))
636 memcpy(val, "EAP-", 4);
639 if (strncmp(reply, "\"autheap=", 9) == 0) {
641 memcpy(val, "EAP-", 4);
642 } else if (strncmp(reply, "\"auth=", 6) == 0)
646 if (strncmp(reply, "\"auth=", 6))
648 if (strcmp(reply + 6, "GTC auth=MSCHAPV2") == 0) {
649 val = "GTC(auth) + MSCHAPv2(prov)";
653 memcpy(val, "EAP-", 4);
659 for (i = 0; i < phase2Select->count(); i++) {
660 if (phase2Select->itemText(i).compare(val) == 0) {
661 phase2Select->setCurrentIndex(i);
666 for (i = 0; i < 4; i++) {
683 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_key%d",
685 reply_len = sizeof(reply) - 1;
686 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
687 if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
688 reply[reply_len] = '\0';
689 pos = strchr(reply + 1, '"');
692 if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
695 wepEdit->setText(reply + 1);
696 } else if (res >= 0 && key_value_isset(reply, reply_len)) {
697 if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
699 wepEdit->setText(WPA_GUI_KEY_DATA);
703 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_tx_keyidx", network_id);
704 reply_len = sizeof(reply) - 1;
705 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1)
707 reply[reply_len] = '\0';
708 switch (atoi(reply)) {
710 wep0Radio->setChecked(true);
713 wep1Radio->setChecked(true);
716 wep2Radio->setChecked(true);
719 wep3Radio->setChecked(true);
724 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d id_str", network_id);
725 reply_len = sizeof(reply) - 1;
726 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
727 reply_len >= 2 && reply[0] == '"') {
728 reply[reply_len] = '\0';
729 pos = strchr(reply + 1, '"');
732 idstrEdit->setText(reply + 1);
735 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d priority", network_id);
736 reply_len = sizeof(reply) - 1;
737 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1)
739 reply[reply_len] = '\0';
740 prioritySpinBox->setValue(atoi(reply));
743 authSelect->setCurrentIndex(auth);
745 encrSelect->setCurrentIndex(encr);
746 if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
747 wepEnabled(encr == 1);
749 removeButton->setEnabled(true);
750 addButton->setText("Save");
754 void NetworkConfig::removeNetwork()
756 char reply[10], cmd[256];
759 if (QMessageBox::information(this, "wpa_gui",
760 "This will permanently remove the "
762 "from the configuration. Do you really "
764 "to remove this network?", "Yes", "No")
768 snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %d", edit_network_id);
769 reply_len = sizeof(reply);
770 wpagui->ctrlRequest(cmd, reply, &reply_len);
771 if (strncmp(reply, "OK", 2) != 0) {
772 QMessageBox::warning(this, "wpa_gui",
773 "Failed to remove network from "
777 wpagui->triggerUpdate();
778 wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
785 void NetworkConfig::newNetwork()
792 void NetworkConfig::getEapCapa()
800 reply_len = sizeof(reply) - 1;
801 if (wpagui->ctrlRequest("GET_CAPABILITY eap", reply, &reply_len) < 0)
803 reply[reply_len] = '\0';
806 QStringList types = res.split(QChar(' '));
807 eapSelect->insertItems(-1, types);