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.
15 #include <QMessageBox>
17 #include "networkconfig.h"
29 #define WPA_GUI_KEY_DATA "[key is configured]"
32 NetworkConfig::NetworkConfig(QWidget *parent, const char *, bool, Qt::WFlags)
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)));
52 NetworkConfig::~NetworkConfig()
57 void NetworkConfig::languageChange()
63 void NetworkConfig::paramsFromScanResults(QTreeWidgetItem *sel)
67 /* SSID BSSID frequency signal flags */
68 setWindowTitle(sel->text(0));
69 ssidEdit->setText(sel->text(0));
71 QString flags = sel->text(4);
73 if (flags.indexOf("[WPA2-EAP") >= 0)
75 else if (flags.indexOf("[WPA-EAP") >= 0)
77 else if (flags.indexOf("[WPA2-PSK") >= 0)
79 else if (flags.indexOf("[WPA-PSK") >= 0)
84 if (flags.indexOf("-CCMP") >= 0)
86 else if (flags.indexOf("-TKIP") >= 0)
88 else if (flags.indexOf("WEP") >= 0)
93 authSelect->setCurrentIndex(auth);
95 encrSelect->setCurrentIndex(encr);
97 wepEnabled(auth == AUTH_NONE && encr == 1);
103 void NetworkConfig::authChanged(int sel)
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);
114 eapChanged(eapSelect->currentIndex());
116 while (encrSelect->count())
117 encrSelect->removeItem(0);
119 if (sel == AUTH_NONE || sel == AUTH_IEEE8021X) {
120 encrSelect->addItem("None");
121 encrSelect->addItem("WEP");
122 encrSelect->setCurrentIndex(sel == AUTH_NONE ? 0 : 1);
124 encrSelect->addItem("TKIP");
125 encrSelect->addItem("CCMP");
126 encrSelect->setCurrentIndex((sel == AUTH_WPA2_PSK ||
127 sel == AUTH_WPA2_EAP) ? 1 : 0);
130 wepEnabled(sel == AUTH_IEEE8021X);
134 void NetworkConfig::eapChanged(int sel)
136 QString prev_val = phase2Select->currentText();
137 while (phase2Select->count())
138 phase2Select->removeItem(0);
141 inner << "PEAP" << "TTLS" << "FAST";
142 if (!inner.contains(eapSelect->itemText(sel)))
145 phase2Select->addItem("[ any ]");
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)");
156 /* Add all enabled EAP methods that can be used in the tunnel */
159 allowed << "MSCHAPV2" << "MD5" << "GTC" << "TLS" << "OTP" << "SIM"
161 for (i = 0; i < eapSelect->count(); i++) {
162 if (allowed.contains(eapSelect->itemText(i))) {
163 phase2Select->addItem("EAP-" + eapSelect->itemText(i));
167 for (i = 0; i < phase2Select->count(); i++) {
168 if (phase2Select->itemText(i).compare(prev_val) == 0) {
169 phase2Select->setCurrentIndex(i);
176 void NetworkConfig::addNetwork()
178 char reply[10], cmd[256];
181 int psklen = pskEdit->text().length();
182 int auth = authSelect->currentIndex();
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");
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: "
205 idstrEdit->setFocus();
213 memset(reply, 0, sizeof(reply));
214 reply_len = sizeof(reply) - 1;
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"
226 id = edit_network_id;
228 setNetworkParam(id, "ssid", ssidEdit->text().toAscii().constData(),
231 const char *key_mgmt = NULL, *proto = NULL, *pairwise = NULL;
237 key_mgmt = "IEEE8021X";
240 key_mgmt = "WPA-PSK";
244 key_mgmt = "WPA-EAP";
248 key_mgmt = "WPA-PSK";
252 key_mgmt = "WPA-EAP";
257 if (auth == AUTH_WPA_PSK || auth == AUTH_WPA_EAP ||
258 auth == AUTH_WPA2_PSK || auth == AUTH_WPA2_EAP) {
259 int encr = encrSelect->currentIndex();
267 setNetworkParam(id, "proto", proto, false);
269 setNetworkParam(id, "key_mgmt", key_mgmt, false);
271 setNetworkParam(id, "pairwise", pairwise, false);
272 setNetworkParam(id, "group", "TKIP CCMP WEP104 WEP40", false);
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(),
280 if (eapSelect->isEnabled()) {
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);
287 setNetworkParam(id, "pcsc", "NULL", false);
289 if (phase2Select->isEnabled()) {
290 QString eap = eapSelect->currentText();
291 QString inner = phase2Select->currentText();
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());
305 snprintf(phase2, sizeof(phase2), "auth=%s",
306 inner.toAscii().constData());
307 } else if (eap.compare("FAST") == 0) {
308 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)")
316 snprintf(phase2, sizeof(phase2),
317 "auth=GTC MSCHAPV2");
318 provisioning = "fast_provisioning=1";
322 setNetworkParam(id, "phase1", provisioning,
324 snprintf(blob, sizeof(blob),
325 "blob://fast-pac-%d", id);
326 setNetworkParam(id, "pac_file", blob, true);
330 setNetworkParam(id, "phase2", phase2, true);
332 setNetworkParam(id, "phase2", "NULL", false);
334 setNetworkParam(id, "phase2", "NULL", false);
335 if (identityEdit->isEnabled() && identityEdit->text().length() > 0)
336 setNetworkParam(id, "identity",
337 identityEdit->text().toAscii().constData(),
340 setNetworkParam(id, "identity", "NULL", false);
341 if (passwordEdit->isEnabled() && passwordEdit->text().length() > 0 &&
342 strcmp(passwordEdit->text().toAscii().constData(),
343 WPA_GUI_KEY_DATA) != 0)
344 setNetworkParam(id, "password",
345 passwordEdit->text().toAscii().constData(),
347 else if (passwordEdit->text().length() == 0)
348 setNetworkParam(id, "password", "NULL", false);
349 if (cacertEdit->isEnabled() && cacertEdit->text().length() > 0)
350 setNetworkParam(id, "ca_cert",
351 cacertEdit->text().toAscii().constData(),
354 setNetworkParam(id, "ca_cert", "NULL", false);
355 writeWepKey(id, wep0Edit, 0);
356 writeWepKey(id, wep1Edit, 1);
357 writeWepKey(id, wep2Edit, 2);
358 writeWepKey(id, wep3Edit, 3);
360 if (wep0Radio->isEnabled() && wep0Radio->isChecked())
361 setNetworkParam(id, "wep_tx_keyidx", "0", false);
362 else if (wep1Radio->isEnabled() && wep1Radio->isChecked())
363 setNetworkParam(id, "wep_tx_keyidx", "1", false);
364 else if (wep2Radio->isEnabled() && wep2Radio->isChecked())
365 setNetworkParam(id, "wep_tx_keyidx", "2", false);
366 else if (wep3Radio->isEnabled() && wep3Radio->isChecked())
367 setNetworkParam(id, "wep_tx_keyidx", "3", false);
369 if (idstrEdit->isEnabled() && idstrEdit->text().length() > 0)
370 setNetworkParam(id, "id_str",
371 idstrEdit->text().toAscii().constData(),
374 setNetworkParam(id, "id_str", "NULL", false);
376 if (prioritySpinBox->isEnabled()) {
378 prio = prio.setNum(prioritySpinBox->value());
379 setNetworkParam(id, "priority", prio.toAscii().constData(),
383 snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %d", id);
384 reply_len = sizeof(reply);
385 wpagui->ctrlRequest(cmd, reply, &reply_len);
386 if (strncmp(reply, "OK", 2) != 0) {
387 QMessageBox::warning(this, "wpa_gui", "Failed to enable "
388 "network in wpa_supplicant\n"
390 /* Network was added, so continue anyway */
392 wpagui->triggerUpdate();
393 wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
399 void NetworkConfig::setWpaGui(WpaGui *_wpagui)
405 int NetworkConfig::setNetworkParam(int id, const char *field,
406 const char *value, bool quote)
408 char reply[10], cmd[256];
410 snprintf(cmd, sizeof(cmd), "SET_NETWORK %d %s %s%s%s",
411 id, field, quote ? "\"" : "", value, quote ? "\"" : "");
412 reply_len = sizeof(reply);
413 wpagui->ctrlRequest(cmd, reply, &reply_len);
414 return strncmp(reply, "OK", 2) == 0 ? 0 : -1;
418 void NetworkConfig::encrChanged(const QString &sel)
420 wepEnabled(sel.indexOf("WEP") == 0);
424 void NetworkConfig::wepEnabled(bool enabled)
426 wep0Edit->setEnabled(enabled);
427 wep1Edit->setEnabled(enabled);
428 wep2Edit->setEnabled(enabled);
429 wep3Edit->setEnabled(enabled);
430 wep0Radio->setEnabled(enabled);
431 wep1Radio->setEnabled(enabled);
432 wep2Radio->setEnabled(enabled);
433 wep3Radio->setEnabled(enabled);
437 void NetworkConfig::writeWepKey(int network_id, QLineEdit *edit, int id)
441 const char *txt, *pos;
444 if (!edit->isEnabled() || edit->text().isEmpty())
448 * Assume hex key if only hex characters are present and length matches
449 * with 40, 104, or 128-bit key
451 txt = edit->text().toAscii().constData();
452 if (strcmp(txt, WPA_GUI_KEY_DATA) == 0)
460 if (!((*pos >= '0' && *pos <= '9') ||
461 (*pos >= 'a' && *pos <= 'f') ||
462 (*pos >= 'A' && *pos <= 'F'))) {
468 if (hex && len != 10 && len != 26 && len != 32)
470 snprintf(buf, sizeof(buf), "wep_key%d", id);
471 setNetworkParam(network_id, buf, txt, !hex);
475 static int key_value_isset(const char *reply, size_t reply_len)
477 return reply_len > 0 && (reply_len < 4 || memcmp(reply, "FAIL", 4) != 0);
481 void NetworkConfig::paramsFromConfig(int network_id)
485 edit_network_id = network_id;
488 char reply[1024], cmd[256], *pos;
491 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ssid", network_id);
492 reply_len = sizeof(reply) - 1;
493 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
494 reply_len >= 2 && reply[0] == '"') {
495 reply[reply_len] = '\0';
496 pos = strchr(reply + 1, '"');
499 ssidEdit->setText(reply + 1);
502 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d proto", network_id);
503 reply_len = sizeof(reply) - 1;
505 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
506 reply[reply_len] = '\0';
507 if (strstr(reply, "RSN") || strstr(reply, "WPA2"))
509 else if (strstr(reply, "WPA"))
513 int auth = AUTH_NONE, encr = 0;
514 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d key_mgmt", network_id);
515 reply_len = sizeof(reply) - 1;
516 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
517 reply[reply_len] = '\0';
518 if (strstr(reply, "WPA-EAP"))
519 auth = wpa & 2 ? AUTH_WPA2_EAP : AUTH_WPA_EAP;
520 else if (strstr(reply, "WPA-PSK"))
521 auth = wpa & 2 ? AUTH_WPA2_PSK : AUTH_WPA_PSK;
522 else if (strstr(reply, "IEEE8021X")) {
523 auth = AUTH_IEEE8021X;
528 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d pairwise", network_id);
529 reply_len = sizeof(reply) - 1;
530 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
531 reply[reply_len] = '\0';
532 if (strstr(reply, "CCMP") && auth != AUTH_NONE)
534 else if (strstr(reply, "TKIP"))
536 else if (strstr(reply, "WEP"))
542 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d psk", network_id);
543 reply_len = sizeof(reply) - 1;
544 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
545 if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
546 reply[reply_len] = '\0';
547 pos = strchr(reply + 1, '"');
550 pskEdit->setText(reply + 1);
551 } else if (res >= 0 && key_value_isset(reply, reply_len)) {
552 pskEdit->setText(WPA_GUI_KEY_DATA);
555 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d identity", network_id);
556 reply_len = sizeof(reply) - 1;
557 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
558 reply_len >= 2 && reply[0] == '"') {
559 reply[reply_len] = '\0';
560 pos = strchr(reply + 1, '"');
563 identityEdit->setText(reply + 1);
566 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d password", network_id);
567 reply_len = sizeof(reply) - 1;
568 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
569 if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
570 reply[reply_len] = '\0';
571 pos = strchr(reply + 1, '"');
574 passwordEdit->setText(reply + 1);
575 } else if (res >= 0 && key_value_isset(reply, reply_len)) {
576 passwordEdit->setText(WPA_GUI_KEY_DATA);
579 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ca_cert", network_id);
580 reply_len = sizeof(reply) - 1;
581 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
582 reply_len >= 2 && reply[0] == '"') {
583 reply[reply_len] = '\0';
584 pos = strchr(reply + 1, '"');
587 cacertEdit->setText(reply + 1);
590 enum { NO_INNER, PEAP_INNER, TTLS_INNER, FAST_INNER } eap = NO_INNER;
591 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d eap", network_id);
592 reply_len = sizeof(reply) - 1;
593 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
595 reply[reply_len] = '\0';
596 for (i = 0; i < eapSelect->count(); i++) {
597 if (eapSelect->itemText(i).compare(reply) == 0) {
598 eapSelect->setCurrentIndex(i);
599 if (strcmp(reply, "PEAP") == 0)
601 else if (strcmp(reply, "TTLS") == 0)
603 else if (strcmp(reply, "FAST") == 0)
610 if (eap != NO_INNER) {
611 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d phase2",
613 reply_len = sizeof(reply) - 1;
614 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
616 reply[reply_len] = '\0';
617 eapChanged(eapSelect->currentIndex());
631 if (strncmp(reply, "\"auth=", 6))
634 memcpy(val, "EAP-", 4);
637 if (strncmp(reply, "\"autheap=", 9) == 0) {
639 memcpy(val, "EAP-", 4);
640 } else if (strncmp(reply, "\"auth=", 6) == 0)
644 if (strncmp(reply, "\"auth=", 6))
646 if (strcmp(reply + 6, "GTC MSCHAPV2") == 0) {
647 val = "GTC(auth) + MSCHAPv2(prov)";
651 memcpy(val, "EAP-", 4);
657 for (i = 0; i < phase2Select->count(); i++) {
658 if (phase2Select->itemText(i).compare(val) == 0) {
659 phase2Select->setCurrentIndex(i);
664 for (i = 0; i < 4; i++) {
681 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_key%d",
683 reply_len = sizeof(reply) - 1;
684 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
685 if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
686 reply[reply_len] = '\0';
687 pos = strchr(reply + 1, '"');
690 if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
693 wepEdit->setText(reply + 1);
694 } else if (res >= 0 && key_value_isset(reply, reply_len)) {
695 if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
697 wepEdit->setText(WPA_GUI_KEY_DATA);
701 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_tx_keyidx", network_id);
702 reply_len = sizeof(reply) - 1;
703 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1)
705 reply[reply_len] = '\0';
706 switch (atoi(reply)) {
708 wep0Radio->setChecked(true);
711 wep1Radio->setChecked(true);
714 wep2Radio->setChecked(true);
717 wep3Radio->setChecked(true);
722 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d id_str", network_id);
723 reply_len = sizeof(reply) - 1;
724 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
725 reply_len >= 2 && reply[0] == '"') {
726 reply[reply_len] = '\0';
727 pos = strchr(reply + 1, '"');
730 idstrEdit->setText(reply + 1);
733 snprintf(cmd, sizeof(cmd), "GET_NETWORK %d priority", network_id);
734 reply_len = sizeof(reply) - 1;
735 if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1)
737 reply[reply_len] = '\0';
738 prioritySpinBox->setValue(atoi(reply));
741 authSelect->setCurrentIndex(auth);
743 encrSelect->setCurrentIndex(encr);
744 if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
745 wepEnabled(encr == 1);
747 removeButton->setEnabled(true);
748 addButton->setText("Save");
752 void NetworkConfig::removeNetwork()
754 char reply[10], cmd[256];
757 if (QMessageBox::information(this, "wpa_gui",
758 "This will permanently remove the "
760 "from the configuration. Do you really "
762 "to remove this network?", "Yes", "No")
766 snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %d", edit_network_id);
767 reply_len = sizeof(reply);
768 wpagui->ctrlRequest(cmd, reply, &reply_len);
769 if (strncmp(reply, "OK", 2) != 0) {
770 QMessageBox::warning(this, "wpa_gui",
771 "Failed to remove network from "
775 wpagui->triggerUpdate();
776 wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
783 void NetworkConfig::newNetwork()
790 void NetworkConfig::getEapCapa()
798 reply_len = sizeof(reply) - 1;
799 if (wpagui->ctrlRequest("GET_CAPABILITY eap", reply, &reply_len) < 0)
801 reply[reply_len] = '\0';
804 QStringList types = res.split(QChar(' '));
805 eapSelect->insertItems(-1, types);