networkconfig.cpp 22 KB

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