networkconfig.cpp 20 KB

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