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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* Mu Qiao <qiaomuf@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 1999-2010 Gentoo Foundation, Inc.
*/
#include <string.h>
#include <stdlib.h>
#include <nm-system-config-interface.h>
#include "wpa_parser.h"
#include "net_parser.h"
#include "net_utils.h"
/* Security information */
static GHashTable *wsec_table = NULL;
/* Global information used for writing */
static GHashTable *wsec_global_table = NULL;
static gboolean wpa_parser_data_changed = FALSE;
static long
wpa_get_long (GHashTable *table, const char *key)
{
return atol (g_hash_table_lookup (table, key));
}
static void
destroy_security (GHashTable * network)
{
gpointer key, value;
GHashTableIter iter;
g_return_if_fail (network);
g_hash_table_iter_init (&iter, network);
while (g_hash_table_iter_next (&iter, &key, &value)) {
g_free (key);
g_free (value);
}
g_hash_table_destroy (network);
}
static GHashTable *
add_security (GHashTable *security)
{
GHashTable *oldsecurity;
const char *ssid, *value;
char *ssid_key;
gboolean is_hex_ssid;
/* Every security information should have a ssid */
ssid = g_hash_table_lookup (security, "ssid");
if (!ssid) {
destroy_security (security);
return NULL;
}
/* Hex format begins with " */
is_hex_ssid = (ssid[0] != '"');
if ((value = g_hash_table_lookup (security, "disabled")) != NULL) {
if (strcmp (value, "1") == 0)
return NULL;
}
/* Default priority is 1 */
if (g_hash_table_lookup (security, "priority") == NULL)
g_hash_table_insert (security, g_strdup ("priority"),
g_strdup ("1"));
oldsecurity = g_hash_table_lookup (wsec_table, ssid);
/* Security with lower priority will be ignored */
if (oldsecurity != NULL) {
if (wpa_get_long (oldsecurity, "priority") >=
wpa_get_long (security, "priority")) {
destroy_security (security);
return NULL;
} else {
g_hash_table_remove (wsec_table, ssid);
destroy_security (oldsecurity);
}
}
/* format ssid */
ssid_key =
is_hex_ssid ? g_strdup_printf ("0x%s",
ssid) :
strip_string (g_strdup (ssid), '"');
g_hash_table_insert (wsec_table, ssid_key, security);
return security;
}
static void
add_key_value (GHashTable * network, gchar * line)
{
gchar **key_value;
if (g_str_has_prefix (line, "network={"))
line += 9;
strip_string (line, '{');
strip_string (line, '}');
if (line[0] == '\0')
return;
key_value = g_strsplit (line, "=", 2);
if (g_strv_length (key_value) != 2) {
g_strfreev (key_value);
return;
}
g_strstrip (key_value[0]);
g_strstrip (key_value[1]);
/* Reserve quotes for psk, wep_key, ssid
* Quotes will determine whether they are hex format */
if (strcmp (key_value[0], "psk") != 0
&& !g_str_has_prefix (key_value[0], "wep_key")
&& strcmp (key_value[0], "ssid") != 0)
strip_string (key_value[1], '"');
g_hash_table_insert (network, g_strdup (key_value[0]),
g_strdup (key_value[1]));
g_strfreev (key_value);
}
static void
add_one_wep_key (GHashTable * table, int key_num, gchar * one_wep_key)
{
if (one_wep_key[0] == 's') {
//asc key
g_hash_table_insert (table,
g_strdup_printf ("wep_key%d", key_num - 1),
g_strdup_printf ("\"%s\"",
one_wep_key + 2));
} else {
gchar buf[30];
int i = 0, j = 0;
//hex key
while (one_wep_key[i] != '\0') {
if (one_wep_key[i] != '-')
buf[j++] = one_wep_key[i];
i++;
}
buf[j] = '\0';
g_hash_table_insert (table,
g_strdup_printf ("wep_key%d", key_num - 1),
g_strdup (buf));
}
}
/* Reading wep security information from /etc/conf.d/net.
* This should not be used in future, use wpa_supplicant instead. */
static void
add_keys_from_net ()
{
GList *names = ifnet_get_connection_names ();
GList *iter = names;
gchar *wep_keys = "(\\[([1-4])\\]\\s+(s:\\w{5}|s:\\w{13}|"
"([\\da-fA-F]{4}\\-){2}[\\da-fA-F]{2}|"
"([\\da-fA-F]{4}\\-){6}[\\da-fA-F]{2})\\s+)";
gchar *key_method =
"\\s+key\\s+\\[([1-4])\\]\\s+enc\\s+(open|restricted)";
GRegex *regex_keys = g_regex_new (wep_keys, 0, 0, NULL);
GRegex *regex_method = g_regex_new (key_method, 0, 0, NULL);
GMatchInfo *keys_info;
GMatchInfo *method_info;
while (iter) {
gchar *conn_name = iter->data;
GHashTable *table;
const char *key_str;
if ((key_str = ifnet_get_data (conn_name, "key")) == NULL) {
iter = g_list_next (iter);
continue;
}
wpa_add_security (conn_name);
table = _get_hash_table (conn_name);
/* Give lowest priority */
wpa_set_data (conn_name, "priority", "0");
g_regex_match (regex_keys, key_str, 0, &keys_info);
/* add wep keys */
while (g_match_info_matches (keys_info)) {
gchar *key_num = g_match_info_fetch (keys_info, 2);
gchar *one_wep_key = g_match_info_fetch (keys_info, 3);
add_one_wep_key (table, atoi (key_num), one_wep_key);
g_free (key_num);
g_free (one_wep_key);
g_match_info_next (keys_info, NULL);
}
g_match_info_free (keys_info);
g_regex_match (regex_method, key_str, 0, &method_info);
/* set default key index and auth alg */
if (g_match_info_matches (method_info)) {
gchar *default_idx =
g_match_info_fetch (method_info, 1);
gchar *method = g_match_info_fetch (method_info, 2);
default_idx[0]--;
g_hash_table_insert (table, g_strdup ("wep_tx_keyidx"),
default_idx);
g_hash_table_insert (table, g_strdup ("auth_alg"),
g_ascii_strup (method, -1));
}
g_match_info_free (method_info);
add_security (table);
iter = g_list_next (iter);
}
g_list_free (names);
g_regex_unref (regex_keys);
g_regex_unref (regex_method);
}
static void
add_global_data (gchar * line)
{
gchar **key_value;
g_strstrip (line);
key_value = g_strsplit (line, "=", 2);
if (g_strv_length (key_value) != 2) {
PLUGIN_WARN (IFNET_PLUGIN_NAME, "Can't handle this line: %s\n",
line);
g_strfreev (key_value);
return;
}
g_hash_table_insert (wsec_global_table,
g_strdup (g_strstrip (key_value[0])),
g_strdup (g_strstrip (key_value[1])));
g_strfreev (key_value);
}
void
wpa_parser_init (const char *wpa_supplicant_conf)
{
GIOChannel *channel = NULL;
gchar *line;
gboolean complete = FALSE;
wpa_parser_data_changed = FALSE;
wsec_table = g_hash_table_new (g_str_hash, g_str_equal);
wsec_global_table = g_hash_table_new (g_str_hash, g_str_equal);
if (g_file_test (wpa_supplicant_conf, G_FILE_TEST_IS_REGULAR))
channel =
g_io_channel_new_file (wpa_supplicant_conf, "r", NULL);
if (channel == NULL) {
PLUGIN_WARN (IFNET_PLUGIN_NAME,
"Can't open %s for wireless security",
wpa_supplicant_conf);
return;
}
while (g_io_channel_read_line (channel, &line, NULL, NULL, NULL)
!= G_IO_STATUS_EOF) {
g_strstrip (line);
if (line[0] != '#' && line[0] != '\0') {
if (strstr (line, "network={") == NULL) {
add_global_data (line);
g_free (line);
continue;
} else {
GHashTable *network =
g_hash_table_new (g_str_hash, g_str_equal);
do {
gchar *quote_start, *quote_end = NULL, *comment;
if (line[0] == '#' || line[0] == '\0') {
g_free (line);
continue;
}
/* ignore inline comments unless inside
a double-quoted string */
if ((quote_start = strchr (line, '"')) != NULL)
quote_end = strrchr (quote_start + 1, '"');
if ((comment = strchr ((quote_end != NULL) ?
quote_end : line, '#')) != NULL)
*comment = '\0';
if (strstr (line, "}") != NULL)
complete = TRUE;
add_key_value (network, line);
g_free (line);
} while (complete == FALSE
&&
g_io_channel_read_line
(channel, &line, NULL,
NULL, NULL) != G_IO_STATUS_EOF);
add_security (network);
//EOF in inner loop
if (complete == FALSE) {
g_free (line);
break;
}
complete = FALSE;
}
} else
g_free (line);
}
g_io_channel_shutdown (channel, FALSE, NULL);
g_io_channel_unref (channel);
add_keys_from_net ();
}
const char *
wpa_get_value (const char *ssid, const char *key)
{
GHashTable *target = g_hash_table_lookup (wsec_table, ssid);
if (target)
return g_hash_table_lookup (target, key);
return NULL;
}
gboolean
exist_ssid (const char *ssid)
{
return g_hash_table_lookup (wsec_table, ssid) != NULL;
}
GHashTable *
_get_hash_table (const char *ssid)
{
return g_hash_table_lookup (wsec_table, ssid);
}
static gchar *quoted_keys[] =
{ "identity", "cert", "private", "phase", "password", NULL };
/* tell whether the key needs quotes when writing is performed */
static gboolean
need_quote (gchar * key)
{
int i = 0;
while (quoted_keys[i] != NULL) {
if (strstr (key, quoted_keys[i]))
return TRUE;
i++;
}
return FALSE;
}
gboolean
wpa_flush_to_file (const char *config_file)
{
GIOChannel *channel;
GError **error = NULL;
gpointer key, value, ssid, security;
GHashTableIter iter, iter_security;
gchar *out_line;
gsize bytes_written;
gboolean result = FALSE;
if (!wpa_parser_data_changed)
return TRUE;
if (!wsec_table || !wsec_global_table)
return FALSE;
backup_file (config_file);
channel = g_io_channel_new_file (config_file, "w", NULL);
if (!channel) {
PLUGIN_WARN (IFNET_PLUGIN_NAME,
"Can't open file %s for writing", config_file);
return FALSE;
}
g_hash_table_iter_init (&iter, wsec_global_table);
PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Writing to %s", config_file);
g_io_channel_write_chars (channel,
"#Generated by NetworkManager\n"
"###### Global Configuration ######\n",
-1, &bytes_written, error);
/* Writing global information */
while (g_hash_table_iter_next (&iter, &key, &value)) {
out_line =
g_strdup_printf ("%s=%s\n", (gchar *) key, (gchar *) value);
g_io_channel_write_chars (channel, out_line, -1, &bytes_written,
error);
if (bytes_written == 0 || (error && *error))
break;
g_free (out_line);
}
if (error && *error) {
PLUGIN_WARN (IFNET_PLUGIN_NAME, "Found error: %s",
(*error)->message);
goto done;
}
g_io_channel_write_chars (channel,
"\n###### Security Configuration ######\n",
-1, &bytes_written, error);
g_hash_table_iter_init (&iter, wsec_table);
/* Writing security */
while (g_hash_table_iter_next (&iter, &ssid, &security)) {
g_hash_table_iter_init (&iter_security,
(GHashTable *) security);
g_io_channel_write_chars (channel, "network={\n", -1,
&bytes_written, error);
while (g_hash_table_iter_next (&iter_security, &key, &value)) {
out_line =
g_strdup_printf (need_quote ((gchar *) key) ?
"\t%s=\"%s\"\n" : "\t%s=%s\n",
(gchar *) key, (gchar *) value);
g_io_channel_write_chars (channel, out_line, -1,
&bytes_written, error);
if (bytes_written == 0 || (error && *error))
break;
g_free (out_line);
}
g_io_channel_write_chars (channel,
"}\n\n", -1, &bytes_written, error);
}
if (error && *error) {
PLUGIN_WARN (IFNET_PLUGIN_NAME, "Found error: %s",
(*error)->message);
goto done;
}
g_io_channel_flush (channel, error);
if (error && *error) {
PLUGIN_WARN (IFNET_PLUGIN_NAME, "Found error: %s",
(*error)->message);
goto done;
}
wpa_parser_data_changed = FALSE;
result = TRUE;
done:
g_io_channel_shutdown (channel, FALSE, NULL);
g_io_channel_unref (channel);
return result;
}
/* If value is NULL, this method will delete old key value pair */
void
wpa_set_data (const char *ssid, const char *key, const char *value)
{
gpointer old_key = NULL, old_value = NULL;
GHashTable *security = g_hash_table_lookup (wsec_table, ssid);
gchar * stripped = NULL;
g_return_if_fail (security != NULL);
if (value){
stripped = g_strdup(value);
if (strcmp (key, "ssid") != 0 && strcmp (key, "psk") != 0
&& !g_str_has_prefix (key, "wep_key"))
strip_string (stripped, '"');
}
/* Remove old key value pairs */
if (g_hash_table_lookup_extended
(security, key, &old_key, &old_value)) {
if (stripped && !strcmp(old_value, stripped)){
g_free (stripped);
return;
}
g_hash_table_remove (security, old_key);
g_free (old_key);
g_free (old_value);
} else if (!value)
return;
/* Add new key value */
if (stripped)
g_hash_table_insert (security, g_strdup (key), stripped);
wpa_parser_data_changed = TRUE;
}
gboolean
wpa_has_security (const char *ssid)
{
return g_hash_table_lookup (wsec_table, ssid) != NULL;
}
gboolean
wpa_add_security (const char *ssid)
{
if (wpa_has_security (ssid))
return TRUE;
else {
GHashTable *security =
g_hash_table_new (g_str_hash, g_str_equal);
gchar *ssid_i;
PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Adding security for %s",
ssid);
if (g_str_has_prefix (ssid, "0x")) {
/* hex ssid */
ssid_i = g_strdup (ssid + 2);
} else {
/* ascii ssid requires quotes */
ssid_i = g_strdup_printf ("\"%s\"", ssid);
}
g_hash_table_insert (security, strdup ("ssid"), ssid_i);
g_hash_table_insert (security, strdup ("priority"),
strdup ("1"));
g_hash_table_insert (wsec_table, g_strdup (ssid), security);
wpa_parser_data_changed = TRUE;
return TRUE;
}
}
gboolean
wpa_delete_security (const char *ssid)
{
gpointer old_key, old_value;
g_return_val_if_fail (wsec_table != NULL && ssid != NULL, FALSE);
PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Deleting security for %s", ssid);
if (!g_hash_table_lookup_extended
(wsec_table, ssid, &old_key, &old_value))
return FALSE;
g_hash_table_remove (wsec_table, old_key);
g_free (old_key);
destroy_security ((GHashTable *) old_value);
wpa_parser_data_changed = TRUE;
return TRUE;
}
void
wpa_parser_destroy (void)
{
GHashTableIter iter;
gpointer key;
gpointer value;
/* Destroy security */
if (wsec_table) {
g_hash_table_iter_init (&iter, wsec_table);
while (g_hash_table_iter_next (&iter, &key, &value)) {
destroy_security ((GHashTable *) value);
g_free (key);
}
g_hash_table_destroy (wsec_table);
wsec_table = NULL;
}
/* Destroy global data */
if (wsec_global_table) {
g_hash_table_iter_init (&iter, wsec_global_table);
while (g_hash_table_iter_next (&iter, &key, &value)) {
g_free (key);
g_free (value);
}
g_hash_table_destroy (wsec_global_table);
wsec_global_table = NULL;
}
}
|