Fixed wpa_config_parse_string() not to modify const string.
[libeap.git] / wpa_supplicant / config.c
1 /*
2  * WPA Supplicant / Configuration parser and common functions
3  * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "wpa.h"
19 #include "sha1.h"
20 #include "eap_peer/eap.h"
21 #include "config.h"
22
23
24 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
25 #define NO_CONFIG_WRITE
26 #endif
27
28 /*
29  * Structure for network configuration parsing. This data is used to implement
30  * a generic parser for each network block variable. The table of configuration
31  * variables is defined below in this file (ssid_fields[]).
32  */
33 struct parse_data {
34         /* Configuration variable name */
35         char *name;
36
37         /* Parser function for this variable */
38         int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
39                       int line, const char *value);
40
41 #ifndef NO_CONFIG_WRITE
42         /* Writer function (i.e., to get the variable in text format from
43          * internal presentation). */
44         char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
45 #endif /* NO_CONFIG_WRITE */
46
47         /* Variable specific parameters for the parser. */
48         void *param1, *param2, *param3, *param4;
49
50         /* 0 = this variable can be included in debug output and ctrl_iface
51          * 1 = this variable contains key/private data and it must not be
52          *     included in debug output unless explicitly requested. In
53          *     addition, this variable will not be readable through the
54          *     ctrl_iface.
55          */
56         int key_data;
57 };
58
59
60 static char * wpa_config_parse_string(const char *value, size_t *len)
61 {
62         if (*value == '"') {
63                 const char *pos;
64                 char *str;
65                 value++;
66                 pos = os_strrchr(value, '"');
67                 if (pos == NULL || pos[1] != '\0')
68                         return NULL;
69                 *len = pos - value;
70                 str = os_malloc(*len + 1);
71                 if (str == NULL)
72                         return NULL;
73                 os_memcpy(str, value, *len);
74                 str[*len] = '\0';
75                 return str;
76         } else {
77                 u8 *str;
78                 size_t tlen, hlen = os_strlen(value);
79                 if (hlen & 1)
80                         return NULL;
81                 tlen = hlen / 2;
82                 str = os_malloc(tlen + 1);
83                 if (str == NULL)
84                         return NULL;
85                 if (hexstr2bin(value, str, tlen)) {
86                         os_free(str);
87                         return NULL;
88                 }
89                 str[tlen] = '\0';
90                 *len = tlen;
91                 return (char *) str;
92         }
93 }
94
95
96 static int wpa_config_parse_str(const struct parse_data *data,
97                                 struct wpa_ssid *ssid,
98                                 int line, const char *value)
99 {
100         size_t res_len, *dst_len;
101         char **dst, *tmp;
102
103         if (os_strcmp(value, "NULL") == 0) {
104                 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
105                            data->name);
106                 tmp = NULL;
107                 res_len = 0;
108                 goto set;
109         }
110
111         tmp = wpa_config_parse_string(value, &res_len);
112         if (tmp == NULL) {
113                 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
114                            line, data->name,
115                            data->key_data ? "[KEY DATA REMOVED]" : value);
116                 return -1;
117         }
118
119         if (data->key_data) {
120                 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
121                                       (u8 *) tmp, res_len);
122         } else {
123                 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
124                                   (u8 *) tmp, res_len);
125         }
126
127         if (data->param3 && res_len < (size_t) data->param3) {
128                 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
129                            "min_len=%ld)", line, data->name,
130                            (unsigned long) res_len, (long) data->param3);
131                 os_free(tmp);
132                 return -1;
133         }
134
135         if (data->param4 && res_len > (size_t) data->param4) {
136                 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
137                            "max_len=%ld)", line, data->name,
138                            (unsigned long) res_len, (long) data->param4);
139                 os_free(tmp);
140                 return -1;
141         }
142
143 set:
144         dst = (char **) (((u8 *) ssid) + (long) data->param1);
145         dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
146         os_free(*dst);
147         *dst = tmp;
148         if (data->param2)
149                 *dst_len = res_len;
150
151         return 0;
152 }
153
154
155 #ifndef NO_CONFIG_WRITE
156 static int is_hex(const u8 *data, size_t len)
157 {
158         size_t i;
159
160         for (i = 0; i < len; i++) {
161                 if (data[i] < 32 || data[i] >= 127)
162                         return 1;
163         }
164         return 0;
165 }
166
167
168 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
169 {
170         char *buf;
171
172         buf = os_malloc(len + 3);
173         if (buf == NULL)
174                 return NULL;
175         buf[0] = '"';
176         os_memcpy(buf + 1, value, len);
177         buf[len + 1] = '"';
178         buf[len + 2] = '\0';
179
180         return buf;
181 }
182
183
184 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
185 {
186         char *buf;
187
188         buf = os_zalloc(2 * len + 1);
189         if (buf == NULL)
190                 return NULL;
191         wpa_snprintf_hex(buf, 2 * len + 1, value, len);
192
193         return buf;
194 }
195
196
197 static char * wpa_config_write_string(const u8 *value, size_t len)
198 {
199         if (value == NULL)
200                 return NULL;
201
202         if (is_hex(value, len))
203                 return wpa_config_write_string_hex(value, len);
204         else
205                 return wpa_config_write_string_ascii(value, len);
206 }
207
208
209 static char * wpa_config_write_str(const struct parse_data *data,
210                                    struct wpa_ssid *ssid)
211 {
212         size_t len;
213         char **src;
214
215         src = (char **) (((u8 *) ssid) + (long) data->param1);
216         if (*src == NULL)
217                 return NULL;
218
219         if (data->param2)
220                 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
221         else
222                 len = os_strlen(*src);
223
224         return wpa_config_write_string((const u8 *) *src, len);
225 }
226 #endif /* NO_CONFIG_WRITE */
227
228
229 static int wpa_config_parse_int(const struct parse_data *data,
230                                 struct wpa_ssid *ssid,
231                                 int line, const char *value)
232 {
233         int *dst;
234
235         dst = (int *) (((u8 *) ssid) + (long) data->param1);
236         *dst = atoi(value);
237         wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
238
239         if (data->param3 && *dst < (long) data->param3) {
240                 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
241                            "min_value=%ld)", line, data->name, *dst,
242                            (long) data->param3);
243                 *dst = (long) data->param3;
244                 return -1;
245         }
246
247         if (data->param4 && *dst > (long) data->param4) {
248                 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
249                            "max_value=%ld)", line, data->name, *dst,
250                            (long) data->param4);
251                 *dst = (long) data->param4;
252                 return -1;
253         }
254
255         return 0;
256 }
257
258
259 #ifndef NO_CONFIG_WRITE
260 static char * wpa_config_write_int(const struct parse_data *data,
261                                    struct wpa_ssid *ssid)
262 {
263         int *src, res;
264         char *value;
265
266         src = (int *) (((u8 *) ssid) + (long) data->param1);
267
268         value = os_malloc(20);
269         if (value == NULL)
270                 return NULL;
271         res = os_snprintf(value, 20, "%d", *src);
272         if (res < 0 || res >= 20) {
273                 os_free(value);
274                 return NULL;
275         }
276         value[20 - 1] = '\0';
277         return value;
278 }
279 #endif /* NO_CONFIG_WRITE */
280
281
282 static int wpa_config_parse_bssid(const struct parse_data *data,
283                                   struct wpa_ssid *ssid, int line,
284                                   const char *value)
285 {
286         if (hwaddr_aton(value, ssid->bssid)) {
287                 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
288                            line, value);
289                 return -1;
290         }
291         ssid->bssid_set = 1;
292         wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
293         return 0;
294 }
295
296
297 #ifndef NO_CONFIG_WRITE
298 static char * wpa_config_write_bssid(const struct parse_data *data,
299                                      struct wpa_ssid *ssid)
300 {
301         char *value;
302         int res;
303
304         if (!ssid->bssid_set)
305                 return NULL;
306
307         value = os_malloc(20);
308         if (value == NULL)
309                 return NULL;
310         res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
311         if (res < 0 || res >= 20) {
312                 os_free(value);
313                 return NULL;
314         }
315         value[20 - 1] = '\0';
316         return value;
317 }
318 #endif /* NO_CONFIG_WRITE */
319
320
321 static int wpa_config_parse_psk(const struct parse_data *data,
322                                 struct wpa_ssid *ssid, int line,
323                                 const char *value)
324 {
325         if (*value == '"') {
326 #ifndef CONFIG_NO_PBKDF2
327                 const char *pos;
328                 size_t len;
329
330                 value++;
331                 pos = os_strrchr(value, '"');
332                 if (pos)
333                         len = pos - value;
334                 else
335                         len = os_strlen(value);
336                 if (len < 8 || len > 63) {
337                         wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
338                                    "length %lu (expected: 8..63) '%s'.",
339                                    line, (unsigned long) len, value);
340                         return -1;
341                 }
342                 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
343                                       (u8 *) value, len);
344                 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
345                     os_memcmp(ssid->passphrase, value, len) == 0)
346                         return 0;
347                 ssid->psk_set = 0;
348                 os_free(ssid->passphrase);
349                 ssid->passphrase = os_malloc(len + 1);
350                 if (ssid->passphrase == NULL)
351                         return -1;
352                 os_memcpy(ssid->passphrase, value, len);
353                 ssid->passphrase[len] = '\0';
354                 return 0;
355 #else /* CONFIG_NO_PBKDF2 */
356                 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
357                            "supported.", line);
358                 return -1;
359 #endif /* CONFIG_NO_PBKDF2 */
360         }
361
362         if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
363             value[PMK_LEN * 2] != '\0') {
364                 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
365                            line, value);
366                 return -1;
367         }
368
369         os_free(ssid->passphrase);
370         ssid->passphrase = NULL;
371
372         ssid->psk_set = 1;
373         wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
374         return 0;
375 }
376
377
378 #ifndef NO_CONFIG_WRITE
379 static char * wpa_config_write_psk(const struct parse_data *data,
380                                    struct wpa_ssid *ssid)
381 {
382         if (ssid->passphrase)
383                 return wpa_config_write_string_ascii(
384                         (const u8 *) ssid->passphrase,
385                         os_strlen(ssid->passphrase));
386
387         if (ssid->psk_set)
388                 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
389
390         return NULL;
391 }
392 #endif /* NO_CONFIG_WRITE */
393
394
395 static int wpa_config_parse_proto(const struct parse_data *data,
396                                   struct wpa_ssid *ssid, int line,
397                                   const char *value)
398 {
399         int val = 0, last, errors = 0;
400         char *start, *end, *buf;
401
402         buf = os_strdup(value);
403         if (buf == NULL)
404                 return -1;
405         start = buf;
406
407         while (*start != '\0') {
408                 while (*start == ' ' || *start == '\t')
409                         start++;
410                 if (*start == '\0')
411                         break;
412                 end = start;
413                 while (*end != ' ' && *end != '\t' && *end != '\0')
414                         end++;
415                 last = *end == '\0';
416                 *end = '\0';
417                 if (os_strcmp(start, "WPA") == 0)
418                         val |= WPA_PROTO_WPA;
419                 else if (os_strcmp(start, "RSN") == 0 ||
420                          os_strcmp(start, "WPA2") == 0)
421                         val |= WPA_PROTO_RSN;
422                 else {
423                         wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
424                                    line, start);
425                         errors++;
426                 }
427
428                 if (last)
429                         break;
430                 start = end + 1;
431         }
432         os_free(buf);
433
434         if (val == 0) {
435                 wpa_printf(MSG_ERROR,
436                            "Line %d: no proto values configured.", line);
437                 errors++;
438         }
439
440         wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
441         ssid->proto = val;
442         return errors ? -1 : 0;
443 }
444
445
446 #ifndef NO_CONFIG_WRITE
447 static char * wpa_config_write_proto(const struct parse_data *data,
448                                      struct wpa_ssid *ssid)
449 {
450         int first = 1, ret;
451         char *buf, *pos, *end;
452
453         pos = buf = os_zalloc(10);
454         if (buf == NULL)
455                 return NULL;
456         end = buf + 10;
457
458         if (ssid->proto & WPA_PROTO_WPA) {
459                 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
460                 if (ret < 0 || ret >= end - pos)
461                         return buf;
462                 pos += ret;
463                 first = 0;
464         }
465
466         if (ssid->proto & WPA_PROTO_RSN) {
467                 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
468                 if (ret < 0 || ret >= end - pos)
469                         return buf;
470                 pos += ret;
471                 first = 0;
472         }
473
474         return buf;
475 }
476 #endif /* NO_CONFIG_WRITE */
477
478
479 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
480                                      struct wpa_ssid *ssid, int line,
481                                      const char *value)
482 {
483         int val = 0, last, errors = 0;
484         char *start, *end, *buf;
485
486         buf = os_strdup(value);
487         if (buf == NULL)
488                 return -1;
489         start = buf;
490
491         while (*start != '\0') {
492                 while (*start == ' ' || *start == '\t')
493                         start++;
494                 if (*start == '\0')
495                         break;
496                 end = start;
497                 while (*end != ' ' && *end != '\t' && *end != '\0')
498                         end++;
499                 last = *end == '\0';
500                 *end = '\0';
501                 if (os_strcmp(start, "WPA-PSK") == 0)
502                         val |= WPA_KEY_MGMT_PSK;
503                 else if (os_strcmp(start, "WPA-EAP") == 0)
504                         val |= WPA_KEY_MGMT_IEEE8021X;
505                 else if (os_strcmp(start, "IEEE8021X") == 0)
506                         val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
507                 else if (os_strcmp(start, "NONE") == 0)
508                         val |= WPA_KEY_MGMT_NONE;
509                 else if (os_strcmp(start, "WPA-NONE") == 0)
510                         val |= WPA_KEY_MGMT_WPA_NONE;
511 #ifdef CONFIG_IEEE80211R
512                 else if (os_strcmp(start, "FT-PSK") == 0)
513                         val |= WPA_KEY_MGMT_FT_PSK;
514                 else if (os_strcmp(start, "FT-EAP") == 0)
515                         val |= WPA_KEY_MGMT_FT_IEEE8021X;
516 #endif /* CONFIG_IEEE80211R */
517 #ifdef CONFIG_IEEE80211W
518                 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
519                         val |= WPA_KEY_MGMT_PSK_SHA256;
520                 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
521                         val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
522 #endif /* CONFIG_IEEE80211W */
523 #ifdef CONFIG_WPS
524                 else if (os_strcmp(start, "WPS") == 0)
525                         val |= WPA_KEY_MGMT_WPS;
526 #endif /* CONFIG_WPS */
527                 else {
528                         wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
529                                    line, start);
530                         errors++;
531                 }
532
533                 if (last)
534                         break;
535                 start = end + 1;
536         }
537         os_free(buf);
538
539         if (val == 0) {
540                 wpa_printf(MSG_ERROR,
541                            "Line %d: no key_mgmt values configured.", line);
542                 errors++;
543         }
544
545         wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
546         ssid->key_mgmt = val;
547         return errors ? -1 : 0;
548 }
549
550
551 #ifndef NO_CONFIG_WRITE
552 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
553                                         struct wpa_ssid *ssid)
554 {
555         char *buf, *pos, *end;
556         int ret;
557
558         pos = buf = os_zalloc(50);
559         if (buf == NULL)
560                 return NULL;
561         end = buf + 50;
562
563         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
564                 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
565                                   pos == buf ? "" : " ");
566                 if (ret < 0 || ret >= end - pos) {
567                         end[-1] = '\0';
568                         return buf;
569                 }
570                 pos += ret;
571         }
572
573         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
574                 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
575                                   pos == buf ? "" : " ");
576                 if (ret < 0 || ret >= end - pos) {
577                         end[-1] = '\0';
578                         return buf;
579                 }
580                 pos += ret;
581         }
582
583         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
584                 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
585                                   pos == buf ? "" : " ");
586                 if (ret < 0 || ret >= end - pos) {
587                         end[-1] = '\0';
588                         return buf;
589                 }
590                 pos += ret;
591         }
592
593         if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
594                 ret = os_snprintf(pos, end - pos, "%sNONE",
595                                   pos == buf ? "" : " ");
596                 if (ret < 0 || ret >= end - pos) {
597                         end[-1] = '\0';
598                         return buf;
599                 }
600                 pos += ret;
601         }
602
603         if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
604                 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
605                                   pos == buf ? "" : " ");
606                 if (ret < 0 || ret >= end - pos) {
607                         end[-1] = '\0';
608                         return buf;
609                 }
610                 pos += ret;
611         }
612
613 #ifdef CONFIG_IEEE80211R
614         if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
615                 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
616                                    pos == buf ? "" : " ");
617
618         if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
619                 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
620                                    pos == buf ? "" : " ");
621 #endif /* CONFIG_IEEE80211R */
622
623 #ifdef CONFIG_IEEE80211W
624         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
625                 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
626                                    pos == buf ? "" : " ");
627
628         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
629                 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
630                                    pos == buf ? "" : " ");
631 #endif /* CONFIG_IEEE80211W */
632
633         return buf;
634 }
635 #endif /* NO_CONFIG_WRITE */
636
637
638 static int wpa_config_parse_cipher(int line, const char *value)
639 {
640         int val = 0, last;
641         char *start, *end, *buf;
642
643         buf = os_strdup(value);
644         if (buf == NULL)
645                 return -1;
646         start = buf;
647
648         while (*start != '\0') {
649                 while (*start == ' ' || *start == '\t')
650                         start++;
651                 if (*start == '\0')
652                         break;
653                 end = start;
654                 while (*end != ' ' && *end != '\t' && *end != '\0')
655                         end++;
656                 last = *end == '\0';
657                 *end = '\0';
658                 if (os_strcmp(start, "CCMP") == 0)
659                         val |= WPA_CIPHER_CCMP;
660                 else if (os_strcmp(start, "TKIP") == 0)
661                         val |= WPA_CIPHER_TKIP;
662                 else if (os_strcmp(start, "WEP104") == 0)
663                         val |= WPA_CIPHER_WEP104;
664                 else if (os_strcmp(start, "WEP40") == 0)
665                         val |= WPA_CIPHER_WEP40;
666                 else if (os_strcmp(start, "NONE") == 0)
667                         val |= WPA_CIPHER_NONE;
668                 else {
669                         wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
670                                    line, start);
671                         os_free(buf);
672                         return -1;
673                 }
674
675                 if (last)
676                         break;
677                 start = end + 1;
678         }
679         os_free(buf);
680
681         if (val == 0) {
682                 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
683                            line);
684                 return -1;
685         }
686         return val;
687 }
688
689
690 #ifndef NO_CONFIG_WRITE
691 static char * wpa_config_write_cipher(int cipher)
692 {
693         char *buf, *pos, *end;
694         int ret;
695
696         pos = buf = os_zalloc(50);
697         if (buf == NULL)
698                 return NULL;
699         end = buf + 50;
700
701         if (cipher & WPA_CIPHER_CCMP) {
702                 ret = os_snprintf(pos, end - pos, "%sCCMP",
703                                   pos == buf ? "" : " ");
704                 if (ret < 0 || ret >= end - pos) {
705                         end[-1] = '\0';
706                         return buf;
707                 }
708                 pos += ret;
709         }
710
711         if (cipher & WPA_CIPHER_TKIP) {
712                 ret = os_snprintf(pos, end - pos, "%sTKIP",
713                                   pos == buf ? "" : " ");
714                 if (ret < 0 || ret >= end - pos) {
715                         end[-1] = '\0';
716                         return buf;
717                 }
718                 pos += ret;
719         }
720
721         if (cipher & WPA_CIPHER_WEP104) {
722                 ret = os_snprintf(pos, end - pos, "%sWEP104",
723                                   pos == buf ? "" : " ");
724                 if (ret < 0 || ret >= end - pos) {
725                         end[-1] = '\0';
726                         return buf;
727                 }
728                 pos += ret;
729         }
730
731         if (cipher & WPA_CIPHER_WEP40) {
732                 ret = os_snprintf(pos, end - pos, "%sWEP40",
733                                   pos == buf ? "" : " ");
734                 if (ret < 0 || ret >= end - pos) {
735                         end[-1] = '\0';
736                         return buf;
737                 }
738                 pos += ret;
739         }
740
741         if (cipher & WPA_CIPHER_NONE) {
742                 ret = os_snprintf(pos, end - pos, "%sNONE",
743                                   pos == buf ? "" : " ");
744                 if (ret < 0 || ret >= end - pos) {
745                         end[-1] = '\0';
746                         return buf;
747                 }
748                 pos += ret;
749         }
750
751         return buf;
752 }
753 #endif /* NO_CONFIG_WRITE */
754
755
756 static int wpa_config_parse_pairwise(const struct parse_data *data,
757                                      struct wpa_ssid *ssid, int line,
758                                      const char *value)
759 {
760         int val;
761         val = wpa_config_parse_cipher(line, value);
762         if (val == -1)
763                 return -1;
764         if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {
765                 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
766                            "(0x%x).", line, val);
767                 return -1;
768         }
769
770         wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
771         ssid->pairwise_cipher = val;
772         return 0;
773 }
774
775
776 #ifndef NO_CONFIG_WRITE
777 static char * wpa_config_write_pairwise(const struct parse_data *data,
778                                         struct wpa_ssid *ssid)
779 {
780         return wpa_config_write_cipher(ssid->pairwise_cipher);
781 }
782 #endif /* NO_CONFIG_WRITE */
783
784
785 static int wpa_config_parse_group(const struct parse_data *data,
786                                   struct wpa_ssid *ssid, int line,
787                                   const char *value)
788 {
789         int val;
790         val = wpa_config_parse_cipher(line, value);
791         if (val == -1)
792                 return -1;
793         if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |
794                     WPA_CIPHER_WEP40)) {
795                 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
796                            "(0x%x).", line, val);
797                 return -1;
798         }
799
800         wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
801         ssid->group_cipher = val;
802         return 0;
803 }
804
805
806 #ifndef NO_CONFIG_WRITE
807 static char * wpa_config_write_group(const struct parse_data *data,
808                                      struct wpa_ssid *ssid)
809 {
810         return wpa_config_write_cipher(ssid->group_cipher);
811 }
812 #endif /* NO_CONFIG_WRITE */
813
814
815 static int wpa_config_parse_auth_alg(const struct parse_data *data,
816                                      struct wpa_ssid *ssid, int line,
817                                      const char *value)
818 {
819         int val = 0, last, errors = 0;
820         char *start, *end, *buf;
821
822         buf = os_strdup(value);
823         if (buf == NULL)
824                 return -1;
825         start = buf;
826
827         while (*start != '\0') {
828                 while (*start == ' ' || *start == '\t')
829                         start++;
830                 if (*start == '\0')
831                         break;
832                 end = start;
833                 while (*end != ' ' && *end != '\t' && *end != '\0')
834                         end++;
835                 last = *end == '\0';
836                 *end = '\0';
837                 if (os_strcmp(start, "OPEN") == 0)
838                         val |= WPA_AUTH_ALG_OPEN;
839                 else if (os_strcmp(start, "SHARED") == 0)
840                         val |= WPA_AUTH_ALG_SHARED;
841                 else if (os_strcmp(start, "LEAP") == 0)
842                         val |= WPA_AUTH_ALG_LEAP;
843                 else {
844                         wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
845                                    line, start);
846                         errors++;
847                 }
848
849                 if (last)
850                         break;
851                 start = end + 1;
852         }
853         os_free(buf);
854
855         if (val == 0) {
856                 wpa_printf(MSG_ERROR,
857                            "Line %d: no auth_alg values configured.", line);
858                 errors++;
859         }
860
861         wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
862         ssid->auth_alg = val;
863         return errors ? -1 : 0;
864 }
865
866
867 #ifndef NO_CONFIG_WRITE
868 static char * wpa_config_write_auth_alg(const struct parse_data *data,
869                                         struct wpa_ssid *ssid)
870 {
871         char *buf, *pos, *end;
872         int ret;
873
874         pos = buf = os_zalloc(30);
875         if (buf == NULL)
876                 return NULL;
877         end = buf + 30;
878
879         if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
880                 ret = os_snprintf(pos, end - pos, "%sOPEN",
881                                   pos == buf ? "" : " ");
882                 if (ret < 0 || ret >= end - pos) {
883                         end[-1] = '\0';
884                         return buf;
885                 }
886                 pos += ret;
887         }
888
889         if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
890                 ret = os_snprintf(pos, end - pos, "%sSHARED",
891                                   pos == buf ? "" : " ");
892                 if (ret < 0 || ret >= end - pos) {
893                         end[-1] = '\0';
894                         return buf;
895                 }
896                 pos += ret;
897         }
898
899         if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
900                 ret = os_snprintf(pos, end - pos, "%sLEAP",
901                                   pos == buf ? "" : " ");
902                 if (ret < 0 || ret >= end - pos) {
903                         end[-1] = '\0';
904                         return buf;
905                 }
906                 pos += ret;
907         }
908
909         return buf;
910 }
911 #endif /* NO_CONFIG_WRITE */
912
913
914 #ifdef IEEE8021X_EAPOL
915 static int wpa_config_parse_eap(const struct parse_data *data,
916                                 struct wpa_ssid *ssid, int line,
917                                 const char *value)
918 {
919         int last, errors = 0;
920         char *start, *end, *buf;
921         struct eap_method_type *methods = NULL, *tmp;
922         size_t num_methods = 0;
923
924         buf = os_strdup(value);
925         if (buf == NULL)
926                 return -1;
927         start = buf;
928
929         while (*start != '\0') {
930                 while (*start == ' ' || *start == '\t')
931                         start++;
932                 if (*start == '\0')
933                         break;
934                 end = start;
935                 while (*end != ' ' && *end != '\t' && *end != '\0')
936                         end++;
937                 last = *end == '\0';
938                 *end = '\0';
939                 tmp = methods;
940                 methods = os_realloc(methods,
941                                      (num_methods + 1) * sizeof(*methods));
942                 if (methods == NULL) {
943                         os_free(tmp);
944                         os_free(buf);
945                         return -1;
946                 }
947                 methods[num_methods].method = eap_peer_get_type(
948                         start, &methods[num_methods].vendor);
949                 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
950                     methods[num_methods].method == EAP_TYPE_NONE) {
951                         wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
952                                    "'%s'", line, start);
953                         wpa_printf(MSG_ERROR, "You may need to add support for"
954                                    " this EAP method during wpa_supplicant\n"
955                                    "build time configuration.\n"
956                                    "See README for more information.");
957                         errors++;
958                 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
959                            methods[num_methods].method == EAP_TYPE_LEAP)
960                         ssid->leap++;
961                 else
962                         ssid->non_leap++;
963                 num_methods++;
964                 if (last)
965                         break;
966                 start = end + 1;
967         }
968         os_free(buf);
969
970         tmp = methods;
971         methods = os_realloc(methods, (num_methods + 1) * sizeof(*methods));
972         if (methods == NULL) {
973                 os_free(tmp);
974                 return -1;
975         }
976         methods[num_methods].vendor = EAP_VENDOR_IETF;
977         methods[num_methods].method = EAP_TYPE_NONE;
978         num_methods++;
979
980         wpa_hexdump(MSG_MSGDUMP, "eap methods",
981                     (u8 *) methods, num_methods * sizeof(*methods));
982         ssid->eap.eap_methods = methods;
983         return errors ? -1 : 0;
984 }
985
986
987 static char * wpa_config_write_eap(const struct parse_data *data,
988                                    struct wpa_ssid *ssid)
989 {
990         int i, ret;
991         char *buf, *pos, *end;
992         const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
993         const char *name;
994
995         if (eap_methods == NULL)
996                 return NULL;
997
998         pos = buf = os_zalloc(100);
999         if (buf == NULL)
1000                 return NULL;
1001         end = buf + 100;
1002
1003         for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1004                      eap_methods[i].method != EAP_TYPE_NONE; i++) {
1005                 name = eap_get_name(eap_methods[i].vendor,
1006                                     eap_methods[i].method);
1007                 if (name) {
1008                         ret = os_snprintf(pos, end - pos, "%s%s",
1009                                           pos == buf ? "" : " ", name);
1010                         if (ret < 0 || ret >= end - pos)
1011                                 break;
1012                         pos += ret;
1013                 }
1014         }
1015
1016         end[-1] = '\0';
1017
1018         return buf;
1019 }
1020
1021
1022 static int wpa_config_parse_password(const struct parse_data *data,
1023                                      struct wpa_ssid *ssid, int line,
1024                                      const char *value)
1025 {
1026         u8 *hash;
1027
1028         if (os_strcmp(value, "NULL") == 0) {
1029                 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1030                 os_free(ssid->eap.password);
1031                 ssid->eap.password = NULL;
1032                 ssid->eap.password_len = 0;
1033                 return 0;
1034         }
1035
1036         if (os_strncmp(value, "hash:", 5) != 0) {
1037                 char *tmp;
1038                 size_t res_len;
1039
1040                 tmp = wpa_config_parse_string(value, &res_len);
1041                 if (tmp == NULL) {
1042                         wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1043                                    "password.", line);
1044                         return -1;
1045                 }
1046                 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
1047                                   (u8 *) tmp, res_len);
1048
1049                 os_free(ssid->eap.password);
1050                 ssid->eap.password = (u8 *) tmp;
1051                 ssid->eap.password_len = res_len;
1052                 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1053
1054                 return 0;
1055         }
1056
1057
1058         /* NtPasswordHash: hash:<32 hex digits> */
1059         if (os_strlen(value + 5) != 2 * 16) {
1060                 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1061                            "(expected 32 hex digits)", line);
1062                 return -1;
1063         }
1064
1065         hash = os_malloc(16);
1066         if (hash == NULL)
1067                 return -1;
1068
1069         if (hexstr2bin(value + 5, hash, 16)) {
1070                 os_free(hash);
1071                 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1072                 return -1;
1073         }
1074
1075         wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1076
1077         os_free(ssid->eap.password);
1078         ssid->eap.password = hash;
1079         ssid->eap.password_len = 16;
1080         ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1081
1082         return 0;
1083 }
1084
1085
1086 static char * wpa_config_write_password(const struct parse_data *data,
1087                                         struct wpa_ssid *ssid)
1088 {
1089         char *buf;
1090
1091         if (ssid->eap.password == NULL)
1092                 return NULL;
1093
1094         if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1095                 return wpa_config_write_string(
1096                         ssid->eap.password, ssid->eap.password_len);
1097         }
1098
1099         buf = os_malloc(5 + 32 + 1);
1100         if (buf == NULL)
1101                 return NULL;
1102
1103         os_memcpy(buf, "hash:", 5);
1104         wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1105
1106         return buf;
1107 }
1108 #endif /* IEEE8021X_EAPOL */
1109
1110
1111 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1112                                     const char *value, int idx)
1113 {
1114         char *buf, title[20];
1115         int res;
1116
1117         buf = wpa_config_parse_string(value, len);
1118         if (buf == NULL) {
1119                 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1120                            line, idx, value);
1121                 return -1;
1122         }
1123         if (*len > MAX_WEP_KEY_LEN) {
1124                 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1125                            line, idx, value);
1126                 os_free(buf);
1127                 return -1;
1128         }
1129         os_memcpy(key, buf, *len);
1130         os_free(buf);
1131         res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1132         if (res >= 0 && (size_t) res < sizeof(title))
1133                 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1134         return 0;
1135 }
1136
1137
1138 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1139                                      struct wpa_ssid *ssid, int line,
1140                                      const char *value)
1141 {
1142         return wpa_config_parse_wep_key(ssid->wep_key[0],
1143                                         &ssid->wep_key_len[0], line,
1144                                         value, 0);
1145 }
1146
1147
1148 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1149                                      struct wpa_ssid *ssid, int line,
1150                                      const char *value)
1151 {
1152         return wpa_config_parse_wep_key(ssid->wep_key[1],
1153                                         &ssid->wep_key_len[1], line,
1154                                         value, 1);
1155 }
1156
1157
1158 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1159                                      struct wpa_ssid *ssid, int line,
1160                                      const char *value)
1161 {
1162         return wpa_config_parse_wep_key(ssid->wep_key[2],
1163                                         &ssid->wep_key_len[2], line,
1164                                         value, 2);
1165 }
1166
1167
1168 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1169                                      struct wpa_ssid *ssid, int line,
1170                                      const char *value)
1171 {
1172         return wpa_config_parse_wep_key(ssid->wep_key[3],
1173                                         &ssid->wep_key_len[3], line,
1174                                         value, 3);
1175 }
1176
1177
1178 #ifndef NO_CONFIG_WRITE
1179 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1180 {
1181         if (ssid->wep_key_len[idx] == 0)
1182                 return NULL;
1183         return wpa_config_write_string(ssid->wep_key[idx],
1184                                        ssid->wep_key_len[idx]);
1185 }
1186
1187
1188 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1189                                         struct wpa_ssid *ssid)
1190 {
1191         return wpa_config_write_wep_key(ssid, 0);
1192 }
1193
1194
1195 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1196                                         struct wpa_ssid *ssid)
1197 {
1198         return wpa_config_write_wep_key(ssid, 1);
1199 }
1200
1201
1202 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1203                                         struct wpa_ssid *ssid)
1204 {
1205         return wpa_config_write_wep_key(ssid, 2);
1206 }
1207
1208
1209 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1210                                         struct wpa_ssid *ssid)
1211 {
1212         return wpa_config_write_wep_key(ssid, 3);
1213 }
1214 #endif /* NO_CONFIG_WRITE */
1215
1216
1217 /* Helper macros for network block parser */
1218
1219 #ifdef OFFSET
1220 #undef OFFSET
1221 #endif /* OFFSET */
1222 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1223 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1224
1225 /* STR: Define a string variable for an ASCII string; f = field name */
1226 #ifdef NO_CONFIG_WRITE
1227 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1228 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1229 #else /* NO_CONFIG_WRITE */
1230 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1231 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1232 #endif /* NO_CONFIG_WRITE */
1233 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1234 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1235 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1236 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1237
1238 /* STR_LEN: Define a string variable with a separate variable for storing the
1239  * data length. Unlike STR(), this can be used to store arbitrary binary data
1240  * (i.e., even nul termination character). */
1241 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1242 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1243 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1244 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1245 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1246
1247 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1248  * explicitly specified. */
1249 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1250 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1251 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1252
1253 #ifdef NO_CONFIG_WRITE
1254 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1255 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1256 #else /* NO_CONFIG_WRITE */
1257 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1258         OFFSET(f), (void *) 0
1259 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1260         OFFSET(eap.f), (void *) 0
1261 #endif /* NO_CONFIG_WRITE */
1262
1263 /* INT: Define an integer variable */
1264 #define INT(f) _INT(f), NULL, NULL, 0
1265 #define INTe(f) _INTe(f), NULL, NULL, 0
1266
1267 /* INT_RANGE: Define an integer variable with allowed value range */
1268 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1269
1270 /* FUNC: Define a configuration variable that uses a custom function for
1271  * parsing and writing the value. */
1272 #ifdef NO_CONFIG_WRITE
1273 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1274 #else /* NO_CONFIG_WRITE */
1275 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1276         NULL, NULL, NULL, NULL
1277 #endif /* NO_CONFIG_WRITE */
1278 #define FUNC(f) _FUNC(f), 0
1279 #define FUNC_KEY(f) _FUNC(f), 1
1280
1281 /*
1282  * Table of network configuration variables. This table is used to parse each
1283  * network configuration variable, e.g., each line in wpa_supplicant.conf file
1284  * that is inside a network block.
1285  *
1286  * This table is generated using the helper macros defined above and with
1287  * generous help from the C pre-processor. The field name is stored as a string
1288  * into .name and for STR and INT types, the offset of the target buffer within
1289  * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1290  * offset to the field containing the length of the configuration variable.
1291  * .param3 and .param4 can be used to mark the allowed range (length for STR
1292  * and value for INT).
1293  *
1294  * For each configuration line in wpa_supplicant.conf, the parser goes through
1295  * this table and select the entry that matches with the field name. The parser
1296  * function (.parser) is then called to parse the actual value of the field.
1297  *
1298  * This kind of mechanism makes it easy to add new configuration parameters,
1299  * since only one line needs to be added into this table and into the
1300  * struct wpa_ssid definition if the new variable is either a string or
1301  * integer. More complex types will need to use their own parser and writer
1302  * functions.
1303  */
1304 static const struct parse_data ssid_fields[] = {
1305         { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1306         { INT_RANGE(scan_ssid, 0, 1) },
1307         { FUNC(bssid) },
1308         { FUNC_KEY(psk) },
1309         { FUNC(proto) },
1310         { FUNC(key_mgmt) },
1311         { FUNC(pairwise) },
1312         { FUNC(group) },
1313         { FUNC(auth_alg) },
1314 #ifdef IEEE8021X_EAPOL
1315         { FUNC(eap) },
1316         { STR_LENe(identity) },
1317         { STR_LENe(anonymous_identity) },
1318         { FUNC(password) },
1319         { STRe(ca_cert) },
1320         { STRe(ca_path) },
1321         { STRe(client_cert) },
1322         { STRe(private_key) },
1323         { STR_KEYe(private_key_passwd) },
1324         { STRe(dh_file) },
1325         { STRe(subject_match) },
1326         { STRe(altsubject_match) },
1327         { STRe(ca_cert2) },
1328         { STRe(ca_path2) },
1329         { STRe(client_cert2) },
1330         { STRe(private_key2) },
1331         { STR_KEYe(private_key2_passwd) },
1332         { STRe(dh_file2) },
1333         { STRe(subject_match2) },
1334         { STRe(altsubject_match2) },
1335         { STRe(phase1) },
1336         { STRe(phase2) },
1337         { STRe(pcsc) },
1338         { STR_KEYe(pin) },
1339         { STRe(engine_id) },
1340         { STRe(key_id) },
1341         { STRe(cert_id) },
1342         { STRe(ca_cert_id) },
1343         { STR_KEYe(pin2) },
1344         { STRe(engine2_id) },
1345         { STRe(key2_id) },
1346         { STRe(cert2_id) },
1347         { STRe(ca_cert2_id) },
1348         { INTe(engine) },
1349         { INTe(engine2) },
1350         { INT(eapol_flags) },
1351 #endif /* IEEE8021X_EAPOL */
1352         { FUNC_KEY(wep_key0) },
1353         { FUNC_KEY(wep_key1) },
1354         { FUNC_KEY(wep_key2) },
1355         { FUNC_KEY(wep_key3) },
1356         { INT(wep_tx_keyidx) },
1357         { INT(priority) },
1358 #ifdef IEEE8021X_EAPOL
1359         { INT(eap_workaround) },
1360         { STRe(pac_file) },
1361         { INTe(fragment_size) },
1362 #endif /* IEEE8021X_EAPOL */
1363         { INT_RANGE(mode, 0, 1) },
1364         { INT_RANGE(proactive_key_caching, 0, 1) },
1365         { INT_RANGE(disabled, 0, 1) },
1366         { STR(id_str) },
1367 #ifdef CONFIG_IEEE80211W
1368         { INT_RANGE(ieee80211w, 0, 2) },
1369 #endif /* CONFIG_IEEE80211W */
1370         { INT_RANGE(peerkey, 0, 1) },
1371         { INT_RANGE(mixed_cell, 0, 1) },
1372         { INT_RANGE(frequency, 0, 10000) },
1373         { INT(wpa_ptk_rekey) }
1374 };
1375
1376 #undef OFFSET
1377 #undef _STR
1378 #undef STR
1379 #undef STR_KEY
1380 #undef _STR_LEN
1381 #undef STR_LEN
1382 #undef STR_LEN_KEY
1383 #undef _STR_RANGE
1384 #undef STR_RANGE
1385 #undef STR_RANGE_KEY
1386 #undef _INT
1387 #undef INT
1388 #undef INT_RANGE
1389 #undef _FUNC
1390 #undef FUNC
1391 #undef FUNC_KEY
1392 #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1393
1394
1395 /**
1396  * wpa_config_add_prio_network - Add a network to priority lists
1397  * @config: Configuration data from wpa_config_read()
1398  * @ssid: Pointer to the network configuration to be added to the list
1399  * Returns: 0 on success, -1 on failure
1400  *
1401  * This function is used to add a network block to the priority list of
1402  * networks. This must be called for each network when reading in the full
1403  * configuration. In addition, this can be used indirectly when updating
1404  * priorities by calling wpa_config_update_prio_list().
1405  */
1406 int wpa_config_add_prio_network(struct wpa_config *config,
1407                                 struct wpa_ssid *ssid)
1408 {
1409         int prio;
1410         struct wpa_ssid *prev, **nlist;
1411
1412         /*
1413          * Add to an existing priority list if one is available for the
1414          * configured priority level for this network.
1415          */
1416         for (prio = 0; prio < config->num_prio; prio++) {
1417                 prev = config->pssid[prio];
1418                 if (prev->priority == ssid->priority) {
1419                         while (prev->pnext)
1420                                 prev = prev->pnext;
1421                         prev->pnext = ssid;
1422                         return 0;
1423                 }
1424         }
1425
1426         /* First network for this priority - add a new priority list */
1427         nlist = os_realloc(config->pssid,
1428                            (config->num_prio + 1) * sizeof(struct wpa_ssid *));
1429         if (nlist == NULL)
1430                 return -1;
1431
1432         for (prio = 0; prio < config->num_prio; prio++) {
1433                 if (nlist[prio]->priority < ssid->priority)
1434                         break;
1435         }
1436
1437         os_memmove(&nlist[prio + 1], &nlist[prio],
1438                    (config->num_prio - prio) * sizeof(struct wpa_ssid *));
1439
1440         nlist[prio] = ssid;
1441         config->num_prio++;
1442         config->pssid = nlist;
1443
1444         return 0;
1445 }
1446
1447
1448 /**
1449  * wpa_config_update_prio_list - Update network priority list
1450  * @config: Configuration data from wpa_config_read()
1451  * Returns: 0 on success, -1 on failure
1452  *
1453  * This function is called to update the priority list of networks in the
1454  * configuration when a network is being added or removed. This is also called
1455  * if a priority for a network is changed.
1456  */
1457 static int wpa_config_update_prio_list(struct wpa_config *config)
1458 {
1459         struct wpa_ssid *ssid;
1460         int ret = 0;
1461
1462         os_free(config->pssid);
1463         config->pssid = NULL;
1464         config->num_prio = 0;
1465
1466         ssid = config->ssid;
1467         while (ssid) {
1468                 ssid->pnext = NULL;
1469                 if (wpa_config_add_prio_network(config, ssid) < 0)
1470                         ret = -1;
1471                 ssid = ssid->next;
1472         }
1473
1474         return ret;
1475 }
1476
1477
1478 #ifdef IEEE8021X_EAPOL
1479 static void eap_peer_config_free(struct eap_peer_config *eap)
1480 {
1481         os_free(eap->eap_methods);
1482         os_free(eap->identity);
1483         os_free(eap->anonymous_identity);
1484         os_free(eap->password);
1485         os_free(eap->ca_cert);
1486         os_free(eap->ca_path);
1487         os_free(eap->client_cert);
1488         os_free(eap->private_key);
1489         os_free(eap->private_key_passwd);
1490         os_free(eap->dh_file);
1491         os_free(eap->subject_match);
1492         os_free(eap->altsubject_match);
1493         os_free(eap->ca_cert2);
1494         os_free(eap->ca_path2);
1495         os_free(eap->client_cert2);
1496         os_free(eap->private_key2);
1497         os_free(eap->private_key2_passwd);
1498         os_free(eap->dh_file2);
1499         os_free(eap->subject_match2);
1500         os_free(eap->altsubject_match2);
1501         os_free(eap->phase1);
1502         os_free(eap->phase2);
1503         os_free(eap->pcsc);
1504         os_free(eap->pin);
1505         os_free(eap->engine_id);
1506         os_free(eap->key_id);
1507         os_free(eap->cert_id);
1508         os_free(eap->ca_cert_id);
1509         os_free(eap->key2_id);
1510         os_free(eap->cert2_id);
1511         os_free(eap->ca_cert2_id);
1512         os_free(eap->pin2);
1513         os_free(eap->engine2_id);
1514         os_free(eap->otp);
1515         os_free(eap->pending_req_otp);
1516         os_free(eap->pac_file);
1517         os_free(eap->new_password);
1518 }
1519 #endif /* IEEE8021X_EAPOL */
1520
1521
1522 /**
1523  * wpa_config_free_ssid - Free network/ssid configuration data
1524  * @ssid: Configuration data for the network
1525  *
1526  * This function frees all resources allocated for the network configuration
1527  * data.
1528  */
1529 void wpa_config_free_ssid(struct wpa_ssid *ssid)
1530 {
1531         os_free(ssid->ssid);
1532         os_free(ssid->passphrase);
1533 #ifdef IEEE8021X_EAPOL
1534         eap_peer_config_free(&ssid->eap);
1535 #endif /* IEEE8021X_EAPOL */
1536         os_free(ssid->id_str);
1537         os_free(ssid);
1538 }
1539
1540
1541 /**
1542  * wpa_config_free - Free configuration data
1543  * @config: Configuration data from wpa_config_read()
1544  *
1545  * This function frees all resources allocated for the configuration data by
1546  * wpa_config_read().
1547  */
1548 void wpa_config_free(struct wpa_config *config)
1549 {
1550 #ifndef CONFIG_NO_CONFIG_BLOBS
1551         struct wpa_config_blob *blob, *prevblob;
1552 #endif /* CONFIG_NO_CONFIG_BLOBS */
1553         struct wpa_ssid *ssid, *prev = NULL;
1554         ssid = config->ssid;
1555         while (ssid) {
1556                 prev = ssid;
1557                 ssid = ssid->next;
1558                 wpa_config_free_ssid(prev);
1559         }
1560
1561 #ifndef CONFIG_NO_CONFIG_BLOBS
1562         blob = config->blobs;
1563         prevblob = NULL;
1564         while (blob) {
1565                 prevblob = blob;
1566                 blob = blob->next;
1567                 wpa_config_free_blob(prevblob);
1568         }
1569 #endif /* CONFIG_NO_CONFIG_BLOBS */
1570
1571         os_free(config->ctrl_interface);
1572         os_free(config->ctrl_interface_group);
1573 #ifdef EAP_TLS_OPENSSL
1574         os_free(config->opensc_engine_path);
1575         os_free(config->pkcs11_engine_path);
1576         os_free(config->pkcs11_module_path);
1577 #endif /* EAP_TLS_OPENSSL */
1578         os_free(config->driver_param);
1579         os_free(config->pssid);
1580         os_free(config);
1581 }
1582
1583
1584 /**
1585  * wpa_config_get_network - Get configured network based on id
1586  * @config: Configuration data from wpa_config_read()
1587  * @id: Unique network id to search for
1588  * Returns: Network configuration or %NULL if not found
1589  */
1590 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1591 {
1592         struct wpa_ssid *ssid;
1593
1594         ssid = config->ssid;
1595         while (ssid) {
1596                 if (id == ssid->id)
1597                         break;
1598                 ssid = ssid->next;
1599         }
1600
1601         return ssid;
1602 }
1603
1604
1605 /**
1606  * wpa_config_add_network - Add a new network with empty configuration
1607  * @config: Configuration data from wpa_config_read()
1608  * Returns: The new network configuration or %NULL if operation failed
1609  */
1610 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1611 {
1612         int id;
1613         struct wpa_ssid *ssid, *last = NULL;
1614
1615         id = -1;
1616         ssid = config->ssid;
1617         while (ssid) {
1618                 if (ssid->id > id)
1619                         id = ssid->id;
1620                 last = ssid;
1621                 ssid = ssid->next;
1622         }
1623         id++;
1624
1625         ssid = os_zalloc(sizeof(*ssid));
1626         if (ssid == NULL)
1627                 return NULL;
1628         ssid->id = id;
1629         if (last)
1630                 last->next = ssid;
1631         else
1632                 config->ssid = ssid;
1633
1634         wpa_config_update_prio_list(config);
1635
1636         return ssid;
1637 }
1638
1639
1640 /**
1641  * wpa_config_remove_network - Remove a configured network based on id
1642  * @config: Configuration data from wpa_config_read()
1643  * @id: Unique network id to search for
1644  * Returns: 0 on success, or -1 if the network was not found
1645  */
1646 int wpa_config_remove_network(struct wpa_config *config, int id)
1647 {
1648         struct wpa_ssid *ssid, *prev = NULL;
1649
1650         ssid = config->ssid;
1651         while (ssid) {
1652                 if (id == ssid->id)
1653                         break;
1654                 prev = ssid;
1655                 ssid = ssid->next;
1656         }
1657
1658         if (ssid == NULL)
1659                 return -1;
1660
1661         if (prev)
1662                 prev->next = ssid->next;
1663         else
1664                 config->ssid = ssid->next;
1665
1666         wpa_config_update_prio_list(config);
1667         wpa_config_free_ssid(ssid);
1668         return 0;
1669 }
1670
1671
1672 /**
1673  * wpa_config_set_network_defaults - Set network default values
1674  * @ssid: Pointer to network configuration data
1675  */
1676 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
1677 {
1678         ssid->proto = DEFAULT_PROTO;
1679         ssid->pairwise_cipher = DEFAULT_PAIRWISE;
1680         ssid->group_cipher = DEFAULT_GROUP;
1681         ssid->key_mgmt = DEFAULT_KEY_MGMT;
1682 #ifdef IEEE8021X_EAPOL
1683         ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
1684         ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
1685         ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
1686 #endif /* IEEE8021X_EAPOL */
1687 }
1688
1689
1690 /**
1691  * wpa_config_set - Set a variable in network configuration
1692  * @ssid: Pointer to network configuration data
1693  * @var: Variable name, e.g., "ssid"
1694  * @value: Variable value
1695  * @line: Line number in configuration file or 0 if not used
1696  * Returns: 0 on success, -1 on failure
1697  *
1698  * This function can be used to set network configuration variables based on
1699  * both the configuration file and management interface input. The value
1700  * parameter must be in the same format as the text-based configuration file is
1701  * using. For example, strings are using double quotation marks.
1702  */
1703 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
1704                    int line)
1705 {
1706         size_t i;
1707         int ret = 0;
1708
1709         if (ssid == NULL || var == NULL || value == NULL)
1710                 return -1;
1711
1712         for (i = 0; i < NUM_SSID_FIELDS; i++) {
1713                 const struct parse_data *field = &ssid_fields[i];
1714                 if (os_strcmp(var, field->name) != 0)
1715                         continue;
1716
1717                 if (field->parser(field, ssid, line, value)) {
1718                         if (line) {
1719                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
1720                                            "parse %s '%s'.", line, var, value);
1721                         }
1722                         ret = -1;
1723                 }
1724                 break;
1725         }
1726         if (i == NUM_SSID_FIELDS) {
1727                 if (line) {
1728                         wpa_printf(MSG_ERROR, "Line %d: unknown network field "
1729                                    "'%s'.", line, var);
1730                 }
1731                 ret = -1;
1732         }
1733
1734         return ret;
1735 }
1736
1737
1738 #ifndef NO_CONFIG_WRITE
1739 /**
1740  * wpa_config_get - Get a variable in network configuration
1741  * @ssid: Pointer to network configuration data
1742  * @var: Variable name, e.g., "ssid"
1743  * Returns: Value of the variable or %NULL on failure
1744  *
1745  * This function can be used to get network configuration variables. The
1746  * returned value is a copy of the configuration variable in text format, i.e,.
1747  * the same format that the text-based configuration file and wpa_config_set()
1748  * are using for the value. The caller is responsible for freeing the returned
1749  * value.
1750  */
1751 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
1752 {
1753         size_t i;
1754
1755         if (ssid == NULL || var == NULL)
1756                 return NULL;
1757
1758         for (i = 0; i < NUM_SSID_FIELDS; i++) {
1759                 const struct parse_data *field = &ssid_fields[i];
1760                 if (os_strcmp(var, field->name) == 0)
1761                         return field->writer(field, ssid);
1762         }
1763
1764         return NULL;
1765 }
1766
1767
1768 /**
1769  * wpa_config_get_no_key - Get a variable in network configuration (no keys)
1770  * @ssid: Pointer to network configuration data
1771  * @var: Variable name, e.g., "ssid"
1772  * Returns: Value of the variable or %NULL on failure
1773  *
1774  * This function can be used to get network configuration variable like
1775  * wpa_config_get(). The only difference is that this functions does not expose
1776  * key/password material from the configuration. In case a key/password field
1777  * is requested, the returned value is an empty string or %NULL if the variable
1778  * is not set or "*" if the variable is set (regardless of its value). The
1779  * returned value is a copy of the configuration variable in text format, i.e,.
1780  * the same format that the text-based configuration file and wpa_config_set()
1781  * are using for the value. The caller is responsible for freeing the returned
1782  * value.
1783  */
1784 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
1785 {
1786         size_t i;
1787
1788         if (ssid == NULL || var == NULL)
1789                 return NULL;
1790
1791         for (i = 0; i < NUM_SSID_FIELDS; i++) {
1792                 const struct parse_data *field = &ssid_fields[i];
1793                 if (os_strcmp(var, field->name) == 0) {
1794                         char *res = field->writer(field, ssid);
1795                         if (field->key_data) {
1796                                 if (res && res[0]) {
1797                                         wpa_printf(MSG_DEBUG, "Do not allow "
1798                                                    "key_data field to be "
1799                                                    "exposed");
1800                                         os_free(res);
1801                                         return os_strdup("*");
1802                                 }
1803
1804                                 os_free(res);
1805                                 return NULL;
1806                         }
1807                         return res;
1808                 }
1809         }
1810
1811         return NULL;
1812 }
1813 #endif /* NO_CONFIG_WRITE */
1814
1815
1816 /**
1817  * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
1818  * @ssid: Pointer to network configuration data
1819  *
1820  * This function must be called to update WPA PSK when either SSID or the
1821  * passphrase has changed for the network configuration.
1822  */
1823 void wpa_config_update_psk(struct wpa_ssid *ssid)
1824 {
1825 #ifndef CONFIG_NO_PBKDF2
1826         pbkdf2_sha1(ssid->passphrase,
1827                     (char *) ssid->ssid, ssid->ssid_len, 4096,
1828                     ssid->psk, PMK_LEN);
1829         wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1830                         ssid->psk, PMK_LEN);
1831         ssid->psk_set = 1;
1832 #endif /* CONFIG_NO_PBKDF2 */
1833 }
1834
1835
1836 #ifndef CONFIG_NO_CONFIG_BLOBS
1837 /**
1838  * wpa_config_get_blob - Get a named configuration blob
1839  * @config: Configuration data from wpa_config_read()
1840  * @name: Name of the blob
1841  * Returns: Pointer to blob data or %NULL if not found
1842  */
1843 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
1844                                                    const char *name)
1845 {
1846         struct wpa_config_blob *blob = config->blobs;
1847
1848         while (blob) {
1849                 if (os_strcmp(blob->name, name) == 0)
1850                         return blob;
1851                 blob = blob->next;
1852         }
1853         return NULL;
1854 }
1855
1856
1857 /**
1858  * wpa_config_set_blob - Set or add a named configuration blob
1859  * @config: Configuration data from wpa_config_read()
1860  * @blob: New value for the blob
1861  *
1862  * Adds a new configuration blob or replaces the current value of an existing
1863  * blob.
1864  */
1865 void wpa_config_set_blob(struct wpa_config *config,
1866                          struct wpa_config_blob *blob)
1867 {
1868         wpa_config_remove_blob(config, blob->name);
1869         blob->next = config->blobs;
1870         config->blobs = blob;
1871 }
1872
1873
1874 /**
1875  * wpa_config_free_blob - Free blob data
1876  * @blob: Pointer to blob to be freed
1877  */
1878 void wpa_config_free_blob(struct wpa_config_blob *blob)
1879 {
1880         if (blob) {
1881                 os_free(blob->name);
1882                 os_free(blob->data);
1883                 os_free(blob);
1884         }
1885 }
1886
1887
1888 /**
1889  * wpa_config_remove_blob - Remove a named configuration blob
1890  * @config: Configuration data from wpa_config_read()
1891  * @name: Name of the blob to remove
1892  * Returns: 0 if blob was removed or -1 if blob was not found
1893  */
1894 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
1895 {
1896         struct wpa_config_blob *pos = config->blobs, *prev = NULL;
1897
1898         while (pos) {
1899                 if (os_strcmp(pos->name, name) == 0) {
1900                         if (prev)
1901                                 prev->next = pos->next;
1902                         else
1903                                 config->blobs = pos->next;
1904                         wpa_config_free_blob(pos);
1905                         return 0;
1906                 }
1907                 prev = pos;
1908                 pos = pos->next;
1909         }
1910
1911         return -1;
1912 }
1913 #endif /* CONFIG_NO_CONFIG_BLOBS */
1914
1915
1916 /**
1917  * wpa_config_alloc_empty - Allocate an empty configuration
1918  * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
1919  * socket
1920  * @driver_param: Driver parameters
1921  * Returns: Pointer to allocated configuration data or %NULL on failure
1922  */
1923 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
1924                                            const char *driver_param)
1925 {
1926         struct wpa_config *config;
1927
1928         config = os_zalloc(sizeof(*config));
1929         if (config == NULL)
1930                 return NULL;
1931         config->eapol_version = DEFAULT_EAPOL_VERSION;
1932         config->ap_scan = DEFAULT_AP_SCAN;
1933         config->fast_reauth = DEFAULT_FAST_REAUTH;
1934
1935         if (ctrl_interface)
1936                 config->ctrl_interface = os_strdup(ctrl_interface);
1937         if (driver_param)
1938                 config->driver_param = os_strdup(driver_param);
1939
1940         return config;
1941 }
1942
1943
1944 #ifndef CONFIG_NO_STDOUT_DEBUG
1945 /**
1946  * wpa_config_debug_dump_networks - Debug dump of configured networks
1947  * @config: Configuration data from wpa_config_read()
1948  */
1949 void wpa_config_debug_dump_networks(struct wpa_config *config)
1950 {
1951         int prio;
1952         struct wpa_ssid *ssid;
1953
1954         for (prio = 0; prio < config->num_prio; prio++) {
1955                 ssid = config->pssid[prio];
1956                 wpa_printf(MSG_DEBUG, "Priority group %d",
1957                            ssid->priority);
1958                 while (ssid) {
1959                         wpa_printf(MSG_DEBUG, "   id=%d ssid='%s'",
1960                                    ssid->id,
1961                                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1962                         ssid = ssid->pnext;
1963                 }
1964         }
1965 }
1966 #endif /* CONFIG_NO_STDOUT_DEBUG */