networkconfig.cpp 20 KB

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