networkconfig.cpp 19 KB

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