networkconfig.cpp 20 KB

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