networkconfig.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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,
  24. Qt::WindowFlags)
  25. : QDialog(parent)
  26. {
  27. setupUi(this);
  28. encrSelect->setEnabled(false);
  29. connect(authSelect, SIGNAL(activated(int)), this,
  30. SLOT(authChanged(int)));
  31. connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
  32. connect(addButton, SIGNAL(clicked()), this, SLOT(addNetwork()));
  33. connect(encrSelect, SIGNAL(activated(const QString &)), this,
  34. SLOT(encrChanged(const QString &)));
  35. connect(removeButton, SIGNAL(clicked()), this, SLOT(removeNetwork()));
  36. connect(eapSelect, SIGNAL(activated(int)), this,
  37. SLOT(eapChanged(int)));
  38. connect(useWpsButton, SIGNAL(clicked()), this, SLOT(useWps()));
  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_OPEN;
  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. if (auth == AUTH_NONE_OPEN)
  74. auth = AUTH_NONE_WEP;
  75. } else
  76. encr = 0;
  77. authSelect->setCurrentIndex(auth);
  78. authChanged(auth);
  79. encrSelect->setCurrentIndex(encr);
  80. wepEnabled(auth == AUTH_NONE_WEP);
  81. getEapCapa();
  82. if (flags.indexOf("[WPS") >= 0)
  83. useWpsButton->setEnabled(true);
  84. bssid = sel->text(1);
  85. }
  86. void NetworkConfig::authChanged(int sel)
  87. {
  88. encrSelect->setEnabled(sel != AUTH_NONE_OPEN && sel != AUTH_NONE_WEP &&
  89. sel != AUTH_NONE_WEP_SHARED);
  90. pskEdit->setEnabled(sel == AUTH_WPA_PSK || sel == AUTH_WPA2_PSK);
  91. bool eap = sel == AUTH_IEEE8021X || sel == AUTH_WPA_EAP ||
  92. sel == AUTH_WPA2_EAP;
  93. eapSelect->setEnabled(eap);
  94. identityEdit->setEnabled(eap);
  95. passwordEdit->setEnabled(eap);
  96. cacertEdit->setEnabled(eap);
  97. phase2Select->setEnabled(eap);
  98. if (eap)
  99. eapChanged(eapSelect->currentIndex());
  100. while (encrSelect->count())
  101. encrSelect->removeItem(0);
  102. if (sel == AUTH_NONE_OPEN || sel == AUTH_NONE_WEP ||
  103. sel == AUTH_NONE_WEP_SHARED || sel == AUTH_IEEE8021X) {
  104. encrSelect->addItem("None");
  105. encrSelect->addItem("WEP");
  106. encrSelect->setCurrentIndex(sel == AUTH_NONE_OPEN ? 0 : 1);
  107. } else {
  108. encrSelect->addItem("TKIP");
  109. encrSelect->addItem("CCMP");
  110. encrSelect->setCurrentIndex((sel == AUTH_WPA2_PSK ||
  111. sel == AUTH_WPA2_EAP) ? 1 : 0);
  112. }
  113. wepEnabled(sel == AUTH_NONE_WEP || sel == AUTH_NONE_WEP_SHARED);
  114. }
  115. void NetworkConfig::eapChanged(int sel)
  116. {
  117. QString prev_val = phase2Select->currentText();
  118. while (phase2Select->count())
  119. phase2Select->removeItem(0);
  120. QStringList inner;
  121. inner << "PEAP" << "TTLS" << "FAST";
  122. if (!inner.contains(eapSelect->itemText(sel)))
  123. return;
  124. phase2Select->addItem("[ any ]");
  125. /* Add special cases based on outer method */
  126. if (eapSelect->currentText().compare("TTLS") == 0) {
  127. phase2Select->addItem("PAP");
  128. phase2Select->addItem("CHAP");
  129. phase2Select->addItem("MSCHAP");
  130. phase2Select->addItem("MSCHAPv2");
  131. } else if (eapSelect->currentText().compare("FAST") == 0)
  132. phase2Select->addItem("GTC(auth) + MSCHAPv2(prov)");
  133. /* Add all enabled EAP methods that can be used in the tunnel */
  134. int i;
  135. QStringList allowed;
  136. allowed << "MSCHAPV2" << "MD5" << "GTC" << "TLS" << "OTP" << "SIM"
  137. << "AKA";
  138. for (i = 0; i < eapSelect->count(); i++) {
  139. if (allowed.contains(eapSelect->itemText(i))) {
  140. phase2Select->addItem("EAP-" + eapSelect->itemText(i));
  141. }
  142. }
  143. for (i = 0; i < phase2Select->count(); i++) {
  144. if (phase2Select->itemText(i).compare(prev_val) == 0) {
  145. phase2Select->setCurrentIndex(i);
  146. break;
  147. }
  148. }
  149. }
  150. void NetworkConfig::addNetwork()
  151. {
  152. char reply[10], cmd[256];
  153. size_t reply_len;
  154. int id;
  155. int psklen = pskEdit->text().length();
  156. int auth = authSelect->currentIndex();
  157. if (auth == AUTH_WPA_PSK || auth == AUTH_WPA2_PSK) {
  158. if (psklen < 8 || psklen > 64) {
  159. QMessageBox::warning(
  160. this,
  161. tr("WPA Pre-Shared Key Error"),
  162. tr("WPA-PSK requires a passphrase of 8 to 63 "
  163. "characters\n"
  164. "or 64 hex digit PSK"));
  165. pskEdit->setFocus();
  166. return;
  167. }
  168. }
  169. if (idstrEdit->isEnabled() && !idstrEdit->text().isEmpty()) {
  170. QRegExp rx("^(\\w|-)+$");
  171. if (rx.indexIn(idstrEdit->text()) < 0) {
  172. QMessageBox::warning(
  173. this, tr("Network ID Error"),
  174. tr("Network ID String contains non-word "
  175. "characters.\n"
  176. "It must be a simple string, "
  177. "without spaces, containing\n"
  178. "only characters in this range: "
  179. "[A-Za-z0-9_-]\n"));
  180. idstrEdit->setFocus();
  181. return;
  182. }
  183. }
  184. if (wpagui == NULL)
  185. return;
  186. memset(reply, 0, sizeof(reply));
  187. reply_len = sizeof(reply) - 1;
  188. if (new_network) {
  189. wpagui->ctrlRequest("ADD_NETWORK", reply, &reply_len);
  190. if (reply[0] == 'F') {
  191. QMessageBox::warning(this, "wpa_gui",
  192. tr("Failed to add "
  193. "network to wpa_supplicant\n"
  194. "configuration."));
  195. return;
  196. }
  197. id = atoi(reply);
  198. } else
  199. id = edit_network_id;
  200. setNetworkParam(id, "ssid", ssidEdit->text().toLocal8Bit().constData(),
  201. true);
  202. const char *key_mgmt = NULL, *proto = NULL, *pairwise = NULL;
  203. switch (auth) {
  204. case AUTH_NONE_OPEN:
  205. case AUTH_NONE_WEP:
  206. case AUTH_NONE_WEP_SHARED:
  207. key_mgmt = "NONE";
  208. break;
  209. case AUTH_IEEE8021X:
  210. key_mgmt = "IEEE8021X";
  211. break;
  212. case AUTH_WPA_PSK:
  213. key_mgmt = "WPA-PSK";
  214. proto = "WPA";
  215. break;
  216. case AUTH_WPA_EAP:
  217. key_mgmt = "WPA-EAP";
  218. proto = "WPA";
  219. break;
  220. case AUTH_WPA2_PSK:
  221. key_mgmt = "WPA-PSK";
  222. proto = "WPA2";
  223. break;
  224. case AUTH_WPA2_EAP:
  225. key_mgmt = "WPA-EAP";
  226. proto = "WPA2";
  227. break;
  228. }
  229. if (auth == AUTH_NONE_WEP_SHARED)
  230. setNetworkParam(id, "auth_alg", "SHARED", false);
  231. else
  232. setNetworkParam(id, "auth_alg", "OPEN", false);
  233. if (auth == AUTH_WPA_PSK || auth == AUTH_WPA_EAP ||
  234. auth == AUTH_WPA2_PSK || auth == AUTH_WPA2_EAP) {
  235. int encr = encrSelect->currentIndex();
  236. if (encr == 0)
  237. pairwise = "TKIP";
  238. else
  239. pairwise = "CCMP";
  240. }
  241. if (proto)
  242. setNetworkParam(id, "proto", proto, false);
  243. if (key_mgmt)
  244. setNetworkParam(id, "key_mgmt", key_mgmt, false);
  245. if (pairwise) {
  246. setNetworkParam(id, "pairwise", pairwise, false);
  247. setNetworkParam(id, "group", "TKIP CCMP WEP104 WEP40", false);
  248. }
  249. if (pskEdit->isEnabled() &&
  250. strcmp(pskEdit->text().toLocal8Bit().constData(),
  251. WPA_GUI_KEY_DATA) != 0)
  252. setNetworkParam(id, "psk",
  253. pskEdit->text().toLocal8Bit().constData(),
  254. psklen != 64);
  255. if (eapSelect->isEnabled()) {
  256. const char *eap =
  257. eapSelect->currentText().toLocal8Bit().constData();
  258. setNetworkParam(id, "eap", eap, false);
  259. if (strcmp(eap, "SIM") == 0 || strcmp(eap, "AKA") == 0)
  260. setNetworkParam(id, "pcsc", "", true);
  261. else
  262. setNetworkParam(id, "pcsc", "NULL", false);
  263. }
  264. if (phase2Select->isEnabled()) {
  265. QString eap = eapSelect->currentText();
  266. QString inner = phase2Select->currentText();
  267. char phase2[32];
  268. phase2[0] = '\0';
  269. if (eap.compare("PEAP") == 0) {
  270. if (inner.startsWith("EAP-"))
  271. snprintf(phase2, sizeof(phase2), "auth=%s",
  272. inner.right(inner.size() - 4).
  273. toLocal8Bit().constData());
  274. } else if (eap.compare("TTLS") == 0) {
  275. if (inner.startsWith("EAP-"))
  276. snprintf(phase2, sizeof(phase2), "autheap=%s",
  277. inner.right(inner.size() - 4).
  278. toLocal8Bit().constData());
  279. else
  280. snprintf(phase2, sizeof(phase2), "auth=%s",
  281. inner.toLocal8Bit().constData());
  282. } else if (eap.compare("FAST") == 0) {
  283. const char *provisioning = NULL;
  284. if (inner.startsWith("EAP-")) {
  285. snprintf(phase2, sizeof(phase2), "auth=%s",
  286. inner.right(inner.size() - 4).
  287. toLocal8Bit().constData());
  288. provisioning = "fast_provisioning=2";
  289. } else if (inner.compare("GTC(auth) + MSCHAPv2(prov)")
  290. == 0) {
  291. snprintf(phase2, sizeof(phase2),
  292. "auth=GTC auth=MSCHAPV2");
  293. provisioning = "fast_provisioning=1";
  294. } else
  295. provisioning = "fast_provisioning=3";
  296. if (provisioning) {
  297. char blob[32];
  298. setNetworkParam(id, "phase1", provisioning,
  299. true);
  300. snprintf(blob, sizeof(blob),
  301. "blob://fast-pac-%d", id);
  302. setNetworkParam(id, "pac_file", blob, true);
  303. }
  304. }
  305. if (phase2[0])
  306. setNetworkParam(id, "phase2", phase2, true);
  307. else
  308. setNetworkParam(id, "phase2", "NULL", false);
  309. } else
  310. setNetworkParam(id, "phase2", "NULL", false);
  311. if (identityEdit->isEnabled() && identityEdit->text().length() > 0)
  312. setNetworkParam(id, "identity",
  313. identityEdit->text().toLocal8Bit().constData(),
  314. true);
  315. else
  316. setNetworkParam(id, "identity", "NULL", false);
  317. if (passwordEdit->isEnabled() && passwordEdit->text().length() > 0 &&
  318. strcmp(passwordEdit->text().toLocal8Bit().constData(),
  319. WPA_GUI_KEY_DATA) != 0)
  320. setNetworkParam(id, "password",
  321. passwordEdit->text().toLocal8Bit().constData(),
  322. true);
  323. else if (passwordEdit->text().length() == 0)
  324. setNetworkParam(id, "password", "NULL", false);
  325. if (cacertEdit->isEnabled() && cacertEdit->text().length() > 0)
  326. setNetworkParam(id, "ca_cert",
  327. cacertEdit->text().toLocal8Bit().constData(),
  328. true);
  329. else
  330. setNetworkParam(id, "ca_cert", "NULL", false);
  331. writeWepKey(id, wep0Edit, 0);
  332. writeWepKey(id, wep1Edit, 1);
  333. writeWepKey(id, wep2Edit, 2);
  334. writeWepKey(id, wep3Edit, 3);
  335. if (wep0Radio->isEnabled() && wep0Radio->isChecked())
  336. setNetworkParam(id, "wep_tx_keyidx", "0", false);
  337. else if (wep1Radio->isEnabled() && wep1Radio->isChecked())
  338. setNetworkParam(id, "wep_tx_keyidx", "1", false);
  339. else if (wep2Radio->isEnabled() && wep2Radio->isChecked())
  340. setNetworkParam(id, "wep_tx_keyidx", "2", false);
  341. else if (wep3Radio->isEnabled() && wep3Radio->isChecked())
  342. setNetworkParam(id, "wep_tx_keyidx", "3", false);
  343. if (idstrEdit->isEnabled() && idstrEdit->text().length() > 0)
  344. setNetworkParam(id, "id_str",
  345. idstrEdit->text().toLocal8Bit().constData(),
  346. true);
  347. else
  348. setNetworkParam(id, "id_str", "NULL", false);
  349. if (prioritySpinBox->isEnabled()) {
  350. QString prio;
  351. prio = prio.setNum(prioritySpinBox->value());
  352. setNetworkParam(id, "priority", prio.toLocal8Bit().constData(),
  353. false);
  354. }
  355. snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %d", id);
  356. reply_len = sizeof(reply);
  357. wpagui->ctrlRequest(cmd, reply, &reply_len);
  358. if (strncmp(reply, "OK", 2) != 0) {
  359. QMessageBox::warning(this, "wpa_gui",
  360. tr("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().toLocal8Bit().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(
  704. this, "wpa_gui",
  705. tr("This will permanently remove the network\n"
  706. "from the configuration. Do you really want\n"
  707. "to remove this network?"),
  708. tr("Yes"), tr("No")) != 0)
  709. return;
  710. snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %d", edit_network_id);
  711. reply_len = sizeof(reply);
  712. wpagui->ctrlRequest(cmd, reply, &reply_len);
  713. if (strncmp(reply, "OK", 2) != 0) {
  714. QMessageBox::warning(this, "wpa_gui",
  715. tr("Failed to remove network from "
  716. "wpa_supplicant\n"
  717. "configuration."));
  718. } else {
  719. wpagui->triggerUpdate();
  720. wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
  721. }
  722. close();
  723. }
  724. void NetworkConfig::newNetwork()
  725. {
  726. new_network = true;
  727. getEapCapa();
  728. }
  729. void NetworkConfig::getEapCapa()
  730. {
  731. char reply[256];
  732. size_t reply_len;
  733. if (wpagui == NULL)
  734. return;
  735. reply_len = sizeof(reply) - 1;
  736. if (wpagui->ctrlRequest("GET_CAPABILITY eap", reply, &reply_len) < 0)
  737. return;
  738. reply[reply_len] = '\0';
  739. QString res(reply);
  740. QStringList types = res.split(QChar(' '));
  741. eapSelect->insertItems(-1, types);
  742. }
  743. void NetworkConfig::useWps()
  744. {
  745. if (wpagui == NULL)
  746. return;
  747. wpagui->setBssFromScan(bssid);
  748. wpagui->wpsDialog();
  749. close();
  750. }