2
* wpa_gui - NetworkConfig class
3
* Copyright (c) 2005-2006, Jouni Malinen <jkmaline@cc.hut.fi>
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.
9
* Alternatively, this software may be distributed under the terms of BSD
12
* See README and COPYING for more details.
15
#include <QMessageBox>
17
#include "networkconfig.h"
30
NetworkConfig::NetworkConfig(QWidget *parent, const char *, bool, Qt::WFlags)
35
connect(authSelect, SIGNAL(activated(int)), this,
36
SLOT(authChanged(int)));
37
connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
38
connect(addButton, SIGNAL(clicked()), this, SLOT(addNetwork()));
39
connect(encrSelect, SIGNAL(activated(const QString &)), this,
40
SLOT(encrChanged(const QString &)));
41
connect(removeButton, SIGNAL(clicked()), this, SLOT(removeNetwork()));
48
NetworkConfig::~NetworkConfig()
53
void NetworkConfig::languageChange()
59
void NetworkConfig::paramsFromScanResults(Q3ListViewItem *sel)
63
/* SSID BSSID frequency signal flags */
64
setCaption(sel->text(0));
65
ssidEdit->setText(sel->text(0));
67
QString flags = sel->text(4);
69
if (flags.find("[WPA2-EAP") >= 0)
71
else if (flags.find("[WPA-EAP") >= 0)
73
else if (flags.find("[WPA2-PSK") >= 0)
75
else if (flags.find("[WPA-PSK") >= 0)
80
if (flags.find("-CCMP") >= 0)
82
else if (flags.find("-TKIP") >= 0)
84
else if (flags.find("WEP") >= 0)
89
authSelect->setCurrentItem(auth);
91
encrSelect->setCurrentItem(encr);
97
void NetworkConfig::authChanged(int sel)
99
pskEdit->setEnabled(sel == AUTH_WPA_PSK || sel == AUTH_WPA2_PSK);
100
bool eap = sel == AUTH_IEEE8021X || sel == AUTH_WPA_EAP ||
101
sel == AUTH_WPA2_EAP;
102
eapSelect->setEnabled(eap);
103
identityEdit->setEnabled(eap);
104
passwordEdit->setEnabled(eap);
105
cacertEdit->setEnabled(eap);
107
while (encrSelect->count())
108
encrSelect->removeItem(0);
110
if (sel == AUTH_NONE || sel == AUTH_IEEE8021X) {
111
encrSelect->insertItem("None");
112
encrSelect->insertItem("WEP");
113
encrSelect->setCurrentItem(sel == AUTH_NONE ? 0 : 1);
115
encrSelect->insertItem("TKIP");
116
encrSelect->insertItem("CCMP");
117
encrSelect->setCurrentItem((sel == AUTH_WPA2_PSK ||
118
sel == AUTH_WPA2_EAP) ? 1 : 0);
121
wepEnabled(sel == AUTH_IEEE8021X);
125
void NetworkConfig::addNetwork()
127
char reply[10], cmd[256];
130
int psklen = pskEdit->text().length();
131
int auth = authSelect->currentItem();
133
if (auth == AUTH_WPA_PSK || auth == AUTH_WPA2_PSK) {
134
if (psklen < 8 || psklen > 64) {
135
QMessageBox::warning(this, "wpa_gui",
136
"WPA-PSK requires a passphrase "
137
"of 8 to 63 characters\n"
138
"or 64 hex digit PSK");
146
memset(reply, 0, sizeof(reply));
147
reply_len = sizeof(reply) - 1;
150
wpagui->ctrlRequest("ADD_NETWORK", reply, &reply_len);
151
if (reply[0] == 'F') {
152
QMessageBox::warning(this, "wpa_gui", "Failed to add "
153
"network to wpa_supplicant\n"
159
id = edit_network_id;
161
setNetworkParam(id, "ssid", ssidEdit->text().ascii(), true);
163
char *key_mgmt = NULL, *proto = NULL, *pairwise = NULL;
169
key_mgmt = "IEEE8021X";
172
key_mgmt = "WPA-PSK";
176
key_mgmt = "WPA-EAP";
180
key_mgmt = "WPA-PSK";
184
key_mgmt = "WPA-EAP";
189
if (auth == AUTH_WPA_PSK || auth == AUTH_WPA_EAP ||
190
auth == AUTH_WPA2_PSK || auth == AUTH_WPA2_EAP) {
191
int encr = encrSelect->currentItem();
199
setNetworkParam(id, "proto", proto, false);
201
setNetworkParam(id, "key_mgmt", key_mgmt, false);
203
setNetworkParam(id, "pairwise", pairwise, false);
204
setNetworkParam(id, "group", "TKIP CCMP WEP104 WEP40", false);
206
if (pskEdit->isEnabled())
207
setNetworkParam(id, "psk", pskEdit->text().ascii(),
209
if (eapSelect->isEnabled())
210
setNetworkParam(id, "eap", eapSelect->currentText().ascii(),
212
if (identityEdit->isEnabled())
213
setNetworkParam(id, "identity", identityEdit->text().ascii(),
215
if (passwordEdit->isEnabled())
216
setNetworkParam(id, "password", passwordEdit->text().ascii(),
218
if (cacertEdit->isEnabled())
219
setNetworkParam(id, "ca_cert", cacertEdit->text().ascii(),
221
writeWepKey(id, wep0Edit, 0);
222
writeWepKey(id, wep1Edit, 1);
223
writeWepKey(id, wep2Edit, 2);
224
writeWepKey(id, wep3Edit, 3);
226
if (wep0Radio->isEnabled() && wep0Radio->isChecked())
227
setNetworkParam(id, "wep_tx_keyidx", "0", false);
228
else if (wep1Radio->isEnabled() && wep1Radio->isChecked())
229
setNetworkParam(id, "wep_tx_keyidx", "1", false);
230
else if (wep2Radio->isEnabled() && wep2Radio->isChecked())
231
setNetworkParam(id, "wep_tx_keyidx", "2", false);
232
else if (wep3Radio->isEnabled() && wep3Radio->isChecked())
233
setNetworkParam(id, "wep_tx_keyidx", "3", false);
235
snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %d", id);
236
reply_len = sizeof(reply);
237
wpagui->ctrlRequest(cmd, reply, &reply_len);
238
if (strncmp(reply, "OK", 2) != 0) {
239
QMessageBox::warning(this, "wpa_gui", "Failed to enable "
240
"network in wpa_supplicant\n"
242
/* Network was added, so continue anyway */
244
wpagui->triggerUpdate();
245
wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
251
void NetworkConfig::setWpaGui(WpaGui *_wpagui)
257
int NetworkConfig::setNetworkParam(int id, const char *field,
258
const char *value, bool quote)
260
char reply[10], cmd[256];
262
snprintf(cmd, sizeof(cmd), "SET_NETWORK %d %s %s%s%s",
263
id, field, quote ? "\"" : "", value, quote ? "\"" : "");
264
reply_len = sizeof(reply);
265
wpagui->ctrlRequest(cmd, reply, &reply_len);
266
return strncmp(reply, "OK", 2) == 0 ? 0 : -1;
270
void NetworkConfig::encrChanged(const QString &sel)
272
wepEnabled(sel.find("WEP") == 0);
276
void NetworkConfig::wepEnabled(bool enabled)
278
wep0Edit->setEnabled(enabled);
279
wep1Edit->setEnabled(enabled);
280
wep2Edit->setEnabled(enabled);
281
wep3Edit->setEnabled(enabled);
282
wep0Radio->setEnabled(enabled);
283
wep1Radio->setEnabled(enabled);
284
wep2Radio->setEnabled(enabled);
285
wep3Radio->setEnabled(enabled);
289
void NetworkConfig::writeWepKey(int network_id, QLineEdit *edit, int id)
293
const char *txt, *pos;
296
if (!edit->isEnabled() || edit->text().isEmpty())
300
* Assume hex key if only hex characters are present and length matches
301
* with 40, 104, or 128-bit key
303
txt = edit->text().ascii();
310
if (!((*pos >= '0' && *pos <= '9') ||
311
(*pos >= 'a' && *pos <= 'f') ||
312
(*pos >= 'A' && *pos <= 'F'))) {
318
if (hex && len != 10 && len != 26 && len != 32)
320
snprintf(buf, sizeof(buf), "wep_key%d", id);
321
setNetworkParam(network_id, buf, txt, !hex);
325
void NetworkConfig::paramsFromConfig(int network_id)
329
edit_network_id = network_id;
332
char reply[1024], cmd[256], *pos;
335
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ssid", network_id);
336
reply_len = sizeof(reply);
337
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
338
reply_len >= 2 && reply[0] == '"') {
339
reply[reply_len] = '\0';
340
pos = strchr(reply + 1, '"');
343
ssidEdit->setText(reply + 1);
346
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d proto", network_id);
347
reply_len = sizeof(reply);
349
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
350
reply[reply_len] = '\0';
351
if (strstr(reply, "RSN") || strstr(reply, "WPA2"))
353
else if (strstr(reply, "WPA"))
357
int auth = AUTH_NONE, encr = 0;
358
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d key_mgmt", network_id);
359
reply_len = sizeof(reply);
360
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
361
reply[reply_len] = '\0';
362
if (strstr(reply, "WPA-EAP"))
363
auth = wpa & 2 ? AUTH_WPA2_EAP : AUTH_WPA_EAP;
364
else if (strstr(reply, "WPA-PSK"))
365
auth = wpa & 2 ? AUTH_WPA2_PSK : AUTH_WPA_PSK;
366
else if (strstr(reply, "IEEE8021X")) {
367
auth = AUTH_IEEE8021X;
372
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d pairwise", network_id);
373
reply_len = sizeof(reply);
374
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
375
reply[reply_len] = '\0';
376
if (strstr(reply, "CCMP"))
378
else if (strstr(reply, "TKIP"))
380
else if (strstr(reply, "WEP"))
386
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d psk", network_id);
387
reply_len = sizeof(reply);
388
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
389
reply_len >= 2 && reply[0] == '"') {
390
reply[reply_len] = '\0';
391
pos = strchr(reply + 1, '"');
394
pskEdit->setText(reply + 1);
397
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d identity", network_id);
398
reply_len = sizeof(reply);
399
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
400
reply_len >= 2 && reply[0] == '"') {
401
reply[reply_len] = '\0';
402
pos = strchr(reply + 1, '"');
405
identityEdit->setText(reply + 1);
408
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d password", network_id);
409
reply_len = sizeof(reply);
410
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
411
reply_len >= 2 && reply[0] == '"') {
412
reply[reply_len] = '\0';
413
pos = strchr(reply + 1, '"');
416
passwordEdit->setText(reply + 1);
419
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ca_cert", network_id);
420
reply_len = sizeof(reply);
421
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
422
reply_len >= 2 && reply[0] == '"') {
423
reply[reply_len] = '\0';
424
pos = strchr(reply + 1, '"');
427
cacertEdit->setText(reply + 1);
430
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d eap", network_id);
431
reply_len = sizeof(reply);
432
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
434
reply[reply_len] = '\0';
435
for (i = 0; i < eapSelect->count(); i++) {
436
if (eapSelect->text(i).compare(reply) == 0) {
437
eapSelect->setCurrentItem(i);
443
for (i = 0; i < 4; i++) {
444
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_key%d",
446
reply_len = sizeof(reply);
447
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 &&
448
reply_len >= 2 && reply[0] == '"') {
449
reply[reply_len] = '\0';
450
pos = strchr(reply + 1, '"');
453
if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
458
wep0Edit->setText(reply + 1);
461
wep1Edit->setText(reply + 1);
464
wep2Edit->setText(reply + 1);
467
wep3Edit->setText(reply + 1);
473
snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_tx_keyidx", network_id);
474
reply_len = sizeof(reply);
475
if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1)
477
reply[reply_len] = '\0';
478
switch (atoi(reply)) {
480
wep0Radio->setChecked(true);
483
wep1Radio->setChecked(true);
486
wep2Radio->setChecked(true);
489
wep3Radio->setChecked(true);
494
authSelect->setCurrentItem(auth);
496
encrSelect->setCurrentItem(encr);
497
if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
498
wepEnabled(encr == 1);
500
removeButton->setEnabled(true);
501
addButton->setText("Save");
505
void NetworkConfig::removeNetwork()
507
char reply[10], cmd[256];
510
if (QMessageBox::information(this, "wpa_gui",
511
"This will permanently remove the "
513
"from the configuration. Do you really "
515
"to remove this network?", "Yes", "No")
519
snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %d", edit_network_id);
520
reply_len = sizeof(reply);
521
wpagui->ctrlRequest(cmd, reply, &reply_len);
522
if (strncmp(reply, "OK", 2) != 0) {
523
QMessageBox::warning(this, "wpa_gui",
524
"Failed to remove network from "
528
wpagui->triggerUpdate();
529
wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
536
void NetworkConfig::newNetwork()
543
void NetworkConfig::getEapCapa()
551
reply_len = sizeof(reply) - 1;
552
if (wpagui->ctrlRequest("GET_CAPABILITY eap", reply, &reply_len) < 0)
554
reply[reply_len] = '\0';
557
QStringList types = QStringList::split(QChar(' '), res);
558
eapSelect->insertStringList(types);