networkconfig.cpp 21 KB

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