networkconfig.cpp 21 KB

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