networkconfig.cpp 20 KB

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