aboutsummaryrefslogtreecommitdiffstats
path: root/wpa_supplicant/wpa_gui/networkconfig.ui.h
blob: 8259bfc7eaaa182f62d9316daebd94a5774ee367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/

#include <stdlib.h>

enum {
    AUTH_NONE = 0,
    AUTH_IEEE8021X = 1,
    AUTH_WPA_PSK = 2,
    AUTH_WPA_EAP = 3,
    AUTH_WPA2_PSK = 4,
    AUTH_WPA2_EAP = 5
};

#define WPA_GUI_KEY_DATA "[key is configured]"

void NetworkConfig::init()
{
    wpagui = NULL;
    new_network = false;
}

void NetworkConfig::paramsFromScanResults(Q3ListViewItem *sel)
{
    new_network = true;

    /* SSID BSSID frequency signal flags */
    setCaption(sel->text(0));
    ssidEdit->setText(sel->text(0));
    
    QString flags = sel->text(4);
    int auth, encr = 0;
    if (flags.find("[WPA2-EAP") >= 0)
	auth = AUTH_WPA2_EAP;
    else if (flags.find("[WPA-EAP") >= 0)
	auth = AUTH_WPA_EAP;
    else if (flags.find("[WPA2-PSK") >= 0)
	auth = AUTH_WPA2_PSK;
    else if (flags.find("[WPA-PSK") >= 0)
	auth = AUTH_WPA_PSK;
    else
	auth = AUTH_NONE;
    
    if (flags.find("-CCMP") >= 0)
	encr = 1;
    else if (flags.find("-TKIP") >= 0)
	encr = 0;
    else if (flags.find("WEP") >= 0)
	encr = 1;
    else
	encr = 0;
 
    authSelect->setCurrentItem(auth);
    authChanged(auth);
    encrSelect->setCurrentItem(encr);

    getEapCapa();
}


void NetworkConfig::authChanged(int sel)
{
    pskEdit->setEnabled(sel == AUTH_WPA_PSK || sel == AUTH_WPA2_PSK);
    bool eap = sel == AUTH_IEEE8021X || sel == AUTH_WPA_EAP ||
	       sel == AUTH_WPA2_EAP;
    eapSelect->setEnabled(eap);
    identityEdit->setEnabled(eap);
    passwordEdit->setEnabled(eap);
    cacertEdit->setEnabled(eap);
   
    while (encrSelect->count())
	encrSelect->removeItem(0);
    
    if (sel == AUTH_NONE || sel == AUTH_IEEE8021X) {
	encrSelect->insertItem("None");
	encrSelect->insertItem("WEP");
	encrSelect->setCurrentItem(sel == AUTH_NONE ? 0 : 1);
    } else {
	encrSelect->insertItem("TKIP");
	encrSelect->insertItem("CCMP");
	encrSelect->setCurrentItem((sel == AUTH_WPA2_PSK ||
				    sel == AUTH_WPA2_EAP) ? 1 : 0);
    }
    
    wepEnabled(sel == AUTH_IEEE8021X);
}


void NetworkConfig::addNetwork()
{
    char reply[10], cmd[256];
    size_t reply_len;
    int id;
    int psklen = pskEdit->text().length();
    int auth = authSelect->currentItem();

    if (auth == AUTH_WPA_PSK || auth == AUTH_WPA2_PSK) {
	if (psklen < 8 || psklen > 64) {
	    QMessageBox::warning(this, "wpa_gui", "WPA-PSK requires a passphrase "
				 "of 8 to 63 characters\n"
				 "or 64 hex digit PSK");
	    return;
	}
    }
        
    if (wpagui == NULL)
	return;
    
    memset(reply, 0, sizeof(reply));
    reply_len = sizeof(reply) - 1;
    
    if (new_network) {
	wpagui->ctrlRequest("ADD_NETWORK", reply, &reply_len);
	if (reply[0] == 'F') {
	    QMessageBox::warning(this, "wpa_gui", "Failed to add network to wpa_supplicant\n"
				 "configuration.");
	    return;
	}
	id = atoi(reply);
    } else {
	id = edit_network_id;
    }

    setNetworkParam(id, "ssid", ssidEdit->text().ascii(), true);
    
    if (idstrEdit->isEnabled())
	setNetworkParam(id, "id_str", idstrEdit->text().ascii(), true);

    const char *key_mgmt = NULL, *proto = NULL, *pairwise = NULL;
    switch (auth) {
    case AUTH_NONE:
	key_mgmt = "NONE";
	break;
    case AUTH_IEEE8021X:
	key_mgmt = "IEEE8021X";
	break;
    case AUTH_WPA_PSK:
	key_mgmt = "WPA-PSK";
	proto = "WPA";
	break;
    case AUTH_WPA_EAP:
	key_mgmt = "WPA-EAP";
	proto = "WPA";
	break;
    case AUTH_WPA2_PSK:
	key_mgmt = "WPA-PSK";
	proto = "WPA2";
	break;
    case AUTH_WPA2_EAP:
	key_mgmt = "WPA-EAP";
	proto = "WPA2";
	break;
    }
    
    if (auth == AUTH_WPA_PSK || auth == AUTH_WPA_EAP ||
	auth == AUTH_WPA2_PSK || auth == AUTH_WPA2_EAP) {
	int encr = encrSelect->currentItem();
	if (encr == 0)
	    pairwise = "TKIP";
	else
	    pairwise = "CCMP";
    }
    
    if (proto)
	setNetworkParam(id, "proto", proto, false);
    if (key_mgmt)
	setNetworkParam(id, "key_mgmt", key_mgmt, false);
    if (pairwise) {
	setNetworkParam(id, "pairwise", pairwise, false);
	setNetworkParam(id, "group", "TKIP CCMP WEP104 WEP40", false);
    }
    if (pskEdit->isEnabled() &&
	strcmp(passwordEdit->text().ascii(), WPA_GUI_KEY_DATA) != 0)
	setNetworkParam(id, "psk", pskEdit->text().ascii(), psklen != 64);
    if (eapSelect->isEnabled())
	setNetworkParam(id, "eap", eapSelect->currentText().ascii(), false);
    if (identityEdit->isEnabled())
	setNetworkParam(id, "identity", identityEdit->text().ascii(), true);
    if (passwordEdit->isEnabled() &&
	strcmp(passwordEdit->text().ascii(), WPA_GUI_KEY_DATA) != 0)
	setNetworkParam(id, "password", passwordEdit->text().ascii(), true);
    if (cacertEdit->isEnabled())
	setNetworkParam(id, "ca_cert", cacertEdit->text().ascii(), true);
    writeWepKey(id, wep0Edit, 0);
    writeWepKey(id, wep1Edit, 1);
    writeWepKey(id, wep2Edit, 2);
    writeWepKey(id, wep3Edit, 3);
  
    if (wep0Radio->isEnabled() && wep0Radio->isChecked())
	setNetworkParam(id, "wep_tx_keyidx", "0", false);
    else if (wep1Radio->isEnabled() && wep1Radio->isChecked())
	setNetworkParam(id, "wep_tx_keyidx", "1", false);
    else if (wep2Radio->isEnabled() && wep2Radio->isChecked())
	setNetworkParam(id, "wep_tx_keyidx", "2", false);
    else if (wep3Radio->isEnabled() && wep3Radio->isChecked())
	setNetworkParam(id, "wep_tx_keyidx", "3", false);

    snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %d", id);
    reply_len = sizeof(reply);
    wpagui->ctrlRequest(cmd, reply, &reply_len);
    if (strncmp(reply, "OK", 2) != 0) {
	QMessageBox::warning(this, "wpa_gui", "Failed to enable network in wpa_supplicant\n"
			     "configuration.");
	/* Network was added, so continue anyway */
    }
    wpagui->triggerUpdate();
    wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);

    close();
}


void NetworkConfig::setWpaGui( WpaGui *_wpagui )
{
    wpagui = _wpagui;
}


int NetworkConfig::setNetworkParam(int id, const char *field, const char *value, bool quote)
{
    char reply[10], cmd[256];
    size_t reply_len;
    snprintf(cmd, sizeof(cmd), "SET_NETWORK %d %s %s%s%s",
	     id, field, quote ? "\"" : "", value, quote ? "\"" : "");
    reply_len = sizeof(reply);
    wpagui->ctrlRequest(cmd, reply, &reply_len);
    return strncmp(reply, "OK", 2) == 0 ? 0 : -1;
}


void NetworkConfig::encrChanged( const QString &sel )
{
    wepEnabled(sel.find("WEP") == 0);
}


void NetworkConfig::wepEnabled( bool enabled )
{
    wep0Edit->setEnabled(enabled);
    wep1Edit->setEnabled(enabled);
    wep2Edit->setEnabled(enabled);
    wep3Edit->setEnabled(enabled);
    wep0Radio->setEnabled(enabled);
    wep1Radio->setEnabled(enabled);
    wep2Radio->setEnabled(enabled);
    wep3Radio->setEnabled(enabled);
}


void NetworkConfig::writeWepKey( int network_id, QLineEdit *edit, int id )
{
    char buf[10];
    bool hex;
    const char *txt, *pos;
    size_t len;
  
    if (!edit->isEnabled() || edit->text().isEmpty())
	return;
    
    /*
        * Assume hex key if only hex characters are present and length matches
       * with 40, 104, or 128-bit key
       */
    txt = edit->text().ascii();
    if (strcmp(txt, WPA_GUI_KEY_DATA) == 0)
	return;
    len = strlen(txt);
    if (len == 0)
	return;
    pos = txt;
    hex = true;
    while (*pos) {
	if (!((*pos >= '0' && *pos <= '9') || (*pos >= 'a' && *pos <= 'f') ||
	      (*pos >= 'A' && *pos <= 'F'))) {
	    hex = false;
	    break;
	}
	pos++;
    }
    if (hex && len != 10 && len != 26 && len != 32)
	hex = false;
    snprintf(buf, sizeof(buf), "wep_key%d", id);
    setNetworkParam(network_id, buf, txt, !hex);
}


static int key_value_isset(const char *reply, size_t reply_len)
{
    return reply_len > 0 && (reply_len < 4 || memcmp(reply, "FAIL", 4) != 0);
}


void NetworkConfig::paramsFromConfig( int network_id )
{
    int i, res;

    edit_network_id = network_id;
    getEapCapa();
    
    char reply[1024], cmd[256], *pos;
    size_t reply_len;
    
    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ssid", network_id);
    reply_len = sizeof(reply) - 1;
    if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 2 &&
	reply[0] == '"') {
	reply[reply_len] = '\0';
	pos = strchr(reply + 1, '"');
	if (pos)
	    *pos = '\0';
	ssidEdit->setText(reply + 1);
    }

    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d id_str", network_id);
    reply_len = sizeof(reply) - 1;
    if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 2 &&
	reply[0] == '"') {
	reply[reply_len] = '\0';
	pos = strchr(reply + 1, '"');
	if (pos)
	    *pos = '\0';
	idstrEdit->setText(reply + 1);
    }
    
    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d proto", network_id);
    reply_len = sizeof(reply) - 1;
    int wpa = 0;
    if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
	reply[reply_len] = '\0';
	if (strstr(reply, "RSN") || strstr(reply, "WPA2"))
	    wpa = 2;
	else if (strstr(reply, "WPA"))
	    wpa = 1;
    }

    int auth = AUTH_NONE, encr = 0;
    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d key_mgmt", network_id);
    reply_len = sizeof(reply) - 1;
    if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
	reply[reply_len] = '\0';
	if (strstr(reply, "WPA-EAP"))
	    auth = wpa & 2 ? AUTH_WPA2_EAP : AUTH_WPA_EAP;
	else if (strstr(reply, "WPA-PSK"))
	    auth = wpa & 2 ? AUTH_WPA2_PSK : AUTH_WPA_PSK;
	else if (strstr(reply, "IEEE8021X")) {
	    auth = AUTH_IEEE8021X;
	    encr = 1;
	}
    }

    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d pairwise", network_id);
    reply_len = sizeof(reply) - 1;
    if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0) {
	reply[reply_len] = '\0';
	if (strstr(reply, "CCMP") && auth != AUTH_NONE)
	    encr = 1;
	else if (strstr(reply, "TKIP"))
	    encr = 0;
	else if (strstr(reply, "WEP"))
	    encr = 1;
	else
	    encr = 0;
    }

    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d psk", network_id);
    reply_len = sizeof(reply) - 1;
    res = wpagui->ctrlRequest(cmd, reply, &reply_len);
    if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
	reply[reply_len] = '\0';
	pos = strchr(reply + 1, '"');
	if (pos)
	    *pos = '\0';
	pskEdit->setText(reply + 1);
    } else if (res >= 0 && key_value_isset(reply, reply_len)) {
	pskEdit->setText(WPA_GUI_KEY_DATA);
    }

    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d identity", network_id);
    reply_len = sizeof(reply) - 1;
    if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 2 &&
	reply[0] == '"') {
	reply[reply_len] = '\0';
	pos = strchr(reply + 1, '"');
	if (pos)
	    *pos = '\0';
	identityEdit->setText(reply + 1);
    }

    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d password", network_id);
    reply_len = sizeof(reply) - 1;
    res = wpagui->ctrlRequest(cmd, reply, &reply_len);
    if (res >= 0 && reply_len >= 2 &&
	reply[0] == '"') {
	reply[reply_len] = '\0';
	pos = strchr(reply + 1, '"');
	if (pos)
	    *pos = '\0';
	passwordEdit->setText(reply + 1);
    } else if (res >= 0 && key_value_isset(reply, reply_len)) {
	passwordEdit->setText(WPA_GUI_KEY_DATA);
    }

    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d ca_cert", network_id);
    reply_len = sizeof(reply) - 1;
    if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 2 &&
	reply[0] == '"') {
	reply[reply_len] = '\0';
	pos = strchr(reply + 1, '"');
	if (pos)
	    *pos = '\0';
	cacertEdit->setText(reply + 1);
    }

    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d eap", network_id);
    reply_len = sizeof(reply) - 1;
    if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1) {
	reply[reply_len] = '\0';
	for (i = 0; i < eapSelect->count(); i++) {
	    if (eapSelect->text(i).compare(reply) == 0) {
		eapSelect->setCurrentItem(i);
		break;
	    }
	}
    }

    for (i = 0; i < 4; i++) {
	QLineEdit *wepEdit;
	switch (i) {
	default:
	case 0:
	    wepEdit = wep0Edit;
	    break;
	case 1:
	    wepEdit = wep1Edit;
	    break;
	case 2:
	    wepEdit = wep2Edit;
	    break;
	case 3:
	    wepEdit = wep3Edit;
	    break;
	}
	snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_key%d", network_id, i);
	reply_len = sizeof(reply) - 1;
	res = wpagui->ctrlRequest(cmd, reply, &reply_len);
	if (res >= 0 && reply_len >= 2 && reply[0] == '"') {
	    reply[reply_len] = '\0';
	    pos = strchr(reply + 1, '"');
	    if (pos)
		*pos = '\0';
	    if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
		encr = 1;

	    wepEdit->setText(reply + 1);
	} else if (res >= 0 && key_value_isset(reply, reply_len)) {
	    if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
		encr = 1;
	    wepEdit->setText(WPA_GUI_KEY_DATA);
	}
    }

    snprintf(cmd, sizeof(cmd), "GET_NETWORK %d wep_tx_keyidx", network_id);
    reply_len = sizeof(reply) - 1;
    if (wpagui->ctrlRequest(cmd, reply, &reply_len) >= 0 && reply_len >= 1) {
	reply[reply_len] = '\0';
	switch (atoi(reply)) {
	case 0:
	    wep0Radio->setChecked(true);
	    break;
	case 1:
	    wep1Radio->setChecked(true);
	    break;
	case 2:
	    wep2Radio->setChecked(true);
	    break;
	case 3:
	    wep3Radio->setChecked(true);
	    break;
	}
    }

    authSelect->setCurrentItem(auth);
    authChanged(auth);
    encrSelect->setCurrentItem(encr);
    if (auth == AUTH_NONE || auth == AUTH_IEEE8021X)
	wepEnabled(encr == 1);

    removeButton->setEnabled(true);
    addButton->setText("Save");
}


void NetworkConfig::removeNetwork()
{
    char reply[10], cmd[256];
    size_t reply_len;
    
    if (QMessageBox::information(this, "wpa_gui",
				 "This will permanently remove the network\n"
				 "from the configuration. Do you really want\n"
				 "to remove this network?", "Yes", "No") != 0)
	return;
    
    snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %d", edit_network_id);
    reply_len = sizeof(reply);
    wpagui->ctrlRequest(cmd, reply, &reply_len);
    if (strncmp(reply, "OK", 2) != 0) {
	QMessageBox::warning(this, "wpa_gui",
			     "Failed to remove network from wpa_supplicant\n"
			     "configuration.");
    } else {
	wpagui->triggerUpdate();
	wpagui->ctrlRequest("SAVE_CONFIG", reply, &reply_len);
    }

    close();
}


void NetworkConfig::newNetwork()
{
    new_network = true;
    getEapCapa();
}


void NetworkConfig::getEapCapa()
{
    char reply[256];
    size_t reply_len;
    
    if (wpagui == NULL)
	return;

    reply_len = sizeof(reply) - 1;
    if (wpagui->ctrlRequest("GET_CAPABILITY eap", reply, &reply_len) < 0)
	return;
    reply[reply_len] = '\0';
    
    QString res(reply);
    QStringList types = QStringList::split(QChar(' '), res);
    eapSelect->insertStringList(types);
}