networkconfig.cpp 22 KB

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