Make wpa_config_parse_string() a shared function
[mech_eap.git] / wpa_supplicant / config.c
1 /*
2  * WPA Supplicant / Configuration parser and common functions
3  * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "utils/uuid.h"
13 #include "crypto/sha1.h"
14 #include "rsn_supp/wpa.h"
15 #include "eap_peer/eap.h"
16 #include "p2p/p2p.h"
17 #include "config.h"
18
19
20 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
21 #define NO_CONFIG_WRITE
22 #endif
23
24 /*
25  * Structure for network configuration parsing. This data is used to implement
26  * a generic parser for each network block variable. The table of configuration
27  * variables is defined below in this file (ssid_fields[]).
28  */
29 struct parse_data {
30         /* Configuration variable name */
31         char *name;
32
33         /* Parser function for this variable */
34         int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
35                       int line, const char *value);
36
37 #ifndef NO_CONFIG_WRITE
38         /* Writer function (i.e., to get the variable in text format from
39          * internal presentation). */
40         char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
41 #endif /* NO_CONFIG_WRITE */
42
43         /* Variable specific parameters for the parser. */
44         void *param1, *param2, *param3, *param4;
45
46         /* 0 = this variable can be included in debug output and ctrl_iface
47          * 1 = this variable contains key/private data and it must not be
48          *     included in debug output unless explicitly requested. In
49          *     addition, this variable will not be readable through the
50          *     ctrl_iface.
51          */
52         int key_data;
53 };
54
55
56 static int wpa_config_parse_str(const struct parse_data *data,
57                                 struct wpa_ssid *ssid,
58                                 int line, const char *value)
59 {
60         size_t res_len, *dst_len;
61         char **dst, *tmp;
62
63         if (os_strcmp(value, "NULL") == 0) {
64                 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
65                            data->name);
66                 tmp = NULL;
67                 res_len = 0;
68                 goto set;
69         }
70
71         tmp = wpa_config_parse_string(value, &res_len);
72         if (tmp == NULL) {
73                 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
74                            line, data->name,
75                            data->key_data ? "[KEY DATA REMOVED]" : value);
76                 return -1;
77         }
78
79         if (data->key_data) {
80                 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
81                                       (u8 *) tmp, res_len);
82         } else {
83                 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
84                                   (u8 *) tmp, res_len);
85         }
86
87         if (data->param3 && res_len < (size_t) data->param3) {
88                 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
89                            "min_len=%ld)", line, data->name,
90                            (unsigned long) res_len, (long) data->param3);
91                 os_free(tmp);
92                 return -1;
93         }
94
95         if (data->param4 && res_len > (size_t) data->param4) {
96                 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
97                            "max_len=%ld)", line, data->name,
98                            (unsigned long) res_len, (long) data->param4);
99                 os_free(tmp);
100                 return -1;
101         }
102
103 set:
104         dst = (char **) (((u8 *) ssid) + (long) data->param1);
105         dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
106         os_free(*dst);
107         *dst = tmp;
108         if (data->param2)
109                 *dst_len = res_len;
110
111         return 0;
112 }
113
114
115 #ifndef NO_CONFIG_WRITE
116 static int is_hex(const u8 *data, size_t len)
117 {
118         size_t i;
119
120         for (i = 0; i < len; i++) {
121                 if (data[i] < 32 || data[i] >= 127)
122                         return 1;
123         }
124         return 0;
125 }
126
127
128 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
129 {
130         char *buf;
131
132         buf = os_malloc(len + 3);
133         if (buf == NULL)
134                 return NULL;
135         buf[0] = '"';
136         os_memcpy(buf + 1, value, len);
137         buf[len + 1] = '"';
138         buf[len + 2] = '\0';
139
140         return buf;
141 }
142
143
144 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
145 {
146         char *buf;
147
148         buf = os_zalloc(2 * len + 1);
149         if (buf == NULL)
150                 return NULL;
151         wpa_snprintf_hex(buf, 2 * len + 1, value, len);
152
153         return buf;
154 }
155
156
157 static char * wpa_config_write_string(const u8 *value, size_t len)
158 {
159         if (value == NULL)
160                 return NULL;
161
162         if (is_hex(value, len))
163                 return wpa_config_write_string_hex(value, len);
164         else
165                 return wpa_config_write_string_ascii(value, len);
166 }
167
168
169 static char * wpa_config_write_str(const struct parse_data *data,
170                                    struct wpa_ssid *ssid)
171 {
172         size_t len;
173         char **src;
174
175         src = (char **) (((u8 *) ssid) + (long) data->param1);
176         if (*src == NULL)
177                 return NULL;
178
179         if (data->param2)
180                 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
181         else
182                 len = os_strlen(*src);
183
184         return wpa_config_write_string((const u8 *) *src, len);
185 }
186 #endif /* NO_CONFIG_WRITE */
187
188
189 static int wpa_config_parse_int(const struct parse_data *data,
190                                 struct wpa_ssid *ssid,
191                                 int line, const char *value)
192 {
193         int *dst;
194
195         dst = (int *) (((u8 *) ssid) + (long) data->param1);
196         *dst = atoi(value);
197         wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
198
199         if (data->param3 && *dst < (long) data->param3) {
200                 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
201                            "min_value=%ld)", line, data->name, *dst,
202                            (long) data->param3);
203                 *dst = (long) data->param3;
204                 return -1;
205         }
206
207         if (data->param4 && *dst > (long) data->param4) {
208                 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
209                            "max_value=%ld)", line, data->name, *dst,
210                            (long) data->param4);
211                 *dst = (long) data->param4;
212                 return -1;
213         }
214
215         return 0;
216 }
217
218
219 #ifndef NO_CONFIG_WRITE
220 static char * wpa_config_write_int(const struct parse_data *data,
221                                    struct wpa_ssid *ssid)
222 {
223         int *src, res;
224         char *value;
225
226         src = (int *) (((u8 *) ssid) + (long) data->param1);
227
228         value = os_malloc(20);
229         if (value == NULL)
230                 return NULL;
231         res = os_snprintf(value, 20, "%d", *src);
232         if (res < 0 || res >= 20) {
233                 os_free(value);
234                 return NULL;
235         }
236         value[20 - 1] = '\0';
237         return value;
238 }
239 #endif /* NO_CONFIG_WRITE */
240
241
242 static int wpa_config_parse_bssid(const struct parse_data *data,
243                                   struct wpa_ssid *ssid, int line,
244                                   const char *value)
245 {
246         if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
247             os_strcmp(value, "any") == 0) {
248                 ssid->bssid_set = 0;
249                 wpa_printf(MSG_MSGDUMP, "BSSID any");
250                 return 0;
251         }
252         if (hwaddr_aton(value, ssid->bssid)) {
253                 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
254                            line, value);
255                 return -1;
256         }
257         ssid->bssid_set = 1;
258         wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
259         return 0;
260 }
261
262
263 #ifndef NO_CONFIG_WRITE
264 static char * wpa_config_write_bssid(const struct parse_data *data,
265                                      struct wpa_ssid *ssid)
266 {
267         char *value;
268         int res;
269
270         if (!ssid->bssid_set)
271                 return NULL;
272
273         value = os_malloc(20);
274         if (value == NULL)
275                 return NULL;
276         res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
277         if (res < 0 || res >= 20) {
278                 os_free(value);
279                 return NULL;
280         }
281         value[20 - 1] = '\0';
282         return value;
283 }
284 #endif /* NO_CONFIG_WRITE */
285
286
287 static int wpa_config_parse_psk(const struct parse_data *data,
288                                 struct wpa_ssid *ssid, int line,
289                                 const char *value)
290 {
291 #ifdef CONFIG_EXT_PASSWORD
292         if (os_strncmp(value, "ext:", 4) == 0) {
293                 os_free(ssid->passphrase);
294                 ssid->passphrase = NULL;
295                 ssid->psk_set = 0;
296                 os_free(ssid->ext_psk);
297                 ssid->ext_psk = os_strdup(value + 4);
298                 if (ssid->ext_psk == NULL)
299                         return -1;
300                 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
301                            ssid->ext_psk);
302                 return 0;
303         }
304 #endif /* CONFIG_EXT_PASSWORD */
305
306         if (*value == '"') {
307 #ifndef CONFIG_NO_PBKDF2
308                 const char *pos;
309                 size_t len;
310
311                 value++;
312                 pos = os_strrchr(value, '"');
313                 if (pos)
314                         len = pos - value;
315                 else
316                         len = os_strlen(value);
317                 if (len < 8 || len > 63) {
318                         wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
319                                    "length %lu (expected: 8..63) '%s'.",
320                                    line, (unsigned long) len, value);
321                         return -1;
322                 }
323                 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
324                                       (u8 *) value, len);
325                 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
326                     os_memcmp(ssid->passphrase, value, len) == 0)
327                         return 0;
328                 ssid->psk_set = 0;
329                 os_free(ssid->passphrase);
330                 ssid->passphrase = os_malloc(len + 1);
331                 if (ssid->passphrase == NULL)
332                         return -1;
333                 os_memcpy(ssid->passphrase, value, len);
334                 ssid->passphrase[len] = '\0';
335                 return 0;
336 #else /* CONFIG_NO_PBKDF2 */
337                 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
338                            "supported.", line);
339                 return -1;
340 #endif /* CONFIG_NO_PBKDF2 */
341         }
342
343         if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
344             value[PMK_LEN * 2] != '\0') {
345                 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
346                            line, value);
347                 return -1;
348         }
349
350         os_free(ssid->passphrase);
351         ssid->passphrase = NULL;
352
353         ssid->psk_set = 1;
354         wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
355         return 0;
356 }
357
358
359 #ifndef NO_CONFIG_WRITE
360 static char * wpa_config_write_psk(const struct parse_data *data,
361                                    struct wpa_ssid *ssid)
362 {
363 #ifdef CONFIG_EXT_PASSWORD
364         if (ssid->ext_psk) {
365                 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
366                 char *buf = os_malloc(len);
367                 if (buf == NULL)
368                         return NULL;
369                 os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
370                 return buf;
371         }
372 #endif /* CONFIG_EXT_PASSWORD */
373
374         if (ssid->passphrase)
375                 return wpa_config_write_string_ascii(
376                         (const u8 *) ssid->passphrase,
377                         os_strlen(ssid->passphrase));
378
379         if (ssid->psk_set)
380                 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
381
382         return NULL;
383 }
384 #endif /* NO_CONFIG_WRITE */
385
386
387 static int wpa_config_parse_proto(const struct parse_data *data,
388                                   struct wpa_ssid *ssid, int line,
389                                   const char *value)
390 {
391         int val = 0, last, errors = 0;
392         char *start, *end, *buf;
393
394         buf = os_strdup(value);
395         if (buf == NULL)
396                 return -1;
397         start = buf;
398
399         while (*start != '\0') {
400                 while (*start == ' ' || *start == '\t')
401                         start++;
402                 if (*start == '\0')
403                         break;
404                 end = start;
405                 while (*end != ' ' && *end != '\t' && *end != '\0')
406                         end++;
407                 last = *end == '\0';
408                 *end = '\0';
409                 if (os_strcmp(start, "WPA") == 0)
410                         val |= WPA_PROTO_WPA;
411                 else if (os_strcmp(start, "RSN") == 0 ||
412                          os_strcmp(start, "WPA2") == 0)
413                         val |= WPA_PROTO_RSN;
414                 else {
415                         wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
416                                    line, start);
417                         errors++;
418                 }
419
420                 if (last)
421                         break;
422                 start = end + 1;
423         }
424         os_free(buf);
425
426         if (val == 0) {
427                 wpa_printf(MSG_ERROR,
428                            "Line %d: no proto values configured.", line);
429                 errors++;
430         }
431
432         wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
433         ssid->proto = val;
434         return errors ? -1 : 0;
435 }
436
437
438 #ifndef NO_CONFIG_WRITE
439 static char * wpa_config_write_proto(const struct parse_data *data,
440                                      struct wpa_ssid *ssid)
441 {
442         int first = 1, ret;
443         char *buf, *pos, *end;
444
445         pos = buf = os_zalloc(10);
446         if (buf == NULL)
447                 return NULL;
448         end = buf + 10;
449
450         if (ssid->proto & WPA_PROTO_WPA) {
451                 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
452                 if (ret < 0 || ret >= end - pos)
453                         return buf;
454                 pos += ret;
455                 first = 0;
456         }
457
458         if (ssid->proto & WPA_PROTO_RSN) {
459                 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
460                 if (ret < 0 || ret >= end - pos)
461                         return buf;
462                 pos += ret;
463                 first = 0;
464         }
465
466         return buf;
467 }
468 #endif /* NO_CONFIG_WRITE */
469
470
471 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
472                                      struct wpa_ssid *ssid, int line,
473                                      const char *value)
474 {
475         int val = 0, last, errors = 0;
476         char *start, *end, *buf;
477
478         buf = os_strdup(value);
479         if (buf == NULL)
480                 return -1;
481         start = buf;
482
483         while (*start != '\0') {
484                 while (*start == ' ' || *start == '\t')
485                         start++;
486                 if (*start == '\0')
487                         break;
488                 end = start;
489                 while (*end != ' ' && *end != '\t' && *end != '\0')
490                         end++;
491                 last = *end == '\0';
492                 *end = '\0';
493                 if (os_strcmp(start, "WPA-PSK") == 0)
494                         val |= WPA_KEY_MGMT_PSK;
495                 else if (os_strcmp(start, "WPA-EAP") == 0)
496                         val |= WPA_KEY_MGMT_IEEE8021X;
497                 else if (os_strcmp(start, "IEEE8021X") == 0)
498                         val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
499                 else if (os_strcmp(start, "NONE") == 0)
500                         val |= WPA_KEY_MGMT_NONE;
501                 else if (os_strcmp(start, "WPA-NONE") == 0)
502                         val |= WPA_KEY_MGMT_WPA_NONE;
503 #ifdef CONFIG_IEEE80211R
504                 else if (os_strcmp(start, "FT-PSK") == 0)
505                         val |= WPA_KEY_MGMT_FT_PSK;
506                 else if (os_strcmp(start, "FT-EAP") == 0)
507                         val |= WPA_KEY_MGMT_FT_IEEE8021X;
508 #endif /* CONFIG_IEEE80211R */
509 #ifdef CONFIG_IEEE80211W
510                 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
511                         val |= WPA_KEY_MGMT_PSK_SHA256;
512                 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
513                         val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
514 #endif /* CONFIG_IEEE80211W */
515 #ifdef CONFIG_WPS
516                 else if (os_strcmp(start, "WPS") == 0)
517                         val |= WPA_KEY_MGMT_WPS;
518 #endif /* CONFIG_WPS */
519                 else {
520                         wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
521                                    line, start);
522                         errors++;
523                 }
524
525                 if (last)
526                         break;
527                 start = end + 1;
528         }
529         os_free(buf);
530
531         if (val == 0) {
532                 wpa_printf(MSG_ERROR,
533                            "Line %d: no key_mgmt values configured.", line);
534                 errors++;
535         }
536
537         wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
538         ssid->key_mgmt = val;
539         return errors ? -1 : 0;
540 }
541
542
543 #ifndef NO_CONFIG_WRITE
544 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
545                                         struct wpa_ssid *ssid)
546 {
547         char *buf, *pos, *end;
548         int ret;
549
550         pos = buf = os_zalloc(50);
551         if (buf == NULL)
552                 return NULL;
553         end = buf + 50;
554
555         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
556                 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
557                                   pos == buf ? "" : " ");
558                 if (ret < 0 || ret >= end - pos) {
559                         end[-1] = '\0';
560                         return buf;
561                 }
562                 pos += ret;
563         }
564
565         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
566                 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
567                                   pos == buf ? "" : " ");
568                 if (ret < 0 || ret >= end - pos) {
569                         end[-1] = '\0';
570                         return buf;
571                 }
572                 pos += ret;
573         }
574
575         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
576                 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
577                                   pos == buf ? "" : " ");
578                 if (ret < 0 || ret >= end - pos) {
579                         end[-1] = '\0';
580                         return buf;
581                 }
582                 pos += ret;
583         }
584
585         if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
586                 ret = os_snprintf(pos, end - pos, "%sNONE",
587                                   pos == buf ? "" : " ");
588                 if (ret < 0 || ret >= end - pos) {
589                         end[-1] = '\0';
590                         return buf;
591                 }
592                 pos += ret;
593         }
594
595         if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
596                 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
597                                   pos == buf ? "" : " ");
598                 if (ret < 0 || ret >= end - pos) {
599                         end[-1] = '\0';
600                         return buf;
601                 }
602                 pos += ret;
603         }
604
605 #ifdef CONFIG_IEEE80211R
606         if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
607                 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
608                                    pos == buf ? "" : " ");
609
610         if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
611                 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
612                                    pos == buf ? "" : " ");
613 #endif /* CONFIG_IEEE80211R */
614
615 #ifdef CONFIG_IEEE80211W
616         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
617                 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
618                                    pos == buf ? "" : " ");
619
620         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
621                 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
622                                    pos == buf ? "" : " ");
623 #endif /* CONFIG_IEEE80211W */
624
625 #ifdef CONFIG_WPS
626         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
627                 pos += os_snprintf(pos, end - pos, "%sWPS",
628                                    pos == buf ? "" : " ");
629 #endif /* CONFIG_WPS */
630
631         return buf;
632 }
633 #endif /* NO_CONFIG_WRITE */
634
635
636 static int wpa_config_parse_cipher(int line, const char *value)
637 {
638         int val = 0, last;
639         char *start, *end, *buf;
640
641         buf = os_strdup(value);
642         if (buf == NULL)
643                 return -1;
644         start = buf;
645
646         while (*start != '\0') {
647                 while (*start == ' ' || *start == '\t')
648                         start++;
649                 if (*start == '\0')
650                         break;
651                 end = start;
652                 while (*end != ' ' && *end != '\t' && *end != '\0')
653                         end++;
654                 last = *end == '\0';
655                 *end = '\0';
656                 if (os_strcmp(start, "CCMP") == 0)
657                         val |= WPA_CIPHER_CCMP;
658                 else if (os_strcmp(start, "TKIP") == 0)
659                         val |= WPA_CIPHER_TKIP;
660                 else if (os_strcmp(start, "WEP104") == 0)
661                         val |= WPA_CIPHER_WEP104;
662                 else if (os_strcmp(start, "WEP40") == 0)
663                         val |= WPA_CIPHER_WEP40;
664                 else if (os_strcmp(start, "NONE") == 0)
665                         val |= WPA_CIPHER_NONE;
666                 else {
667                         wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
668                                    line, start);
669                         os_free(buf);
670                         return -1;
671                 }
672
673                 if (last)
674                         break;
675                 start = end + 1;
676         }
677         os_free(buf);
678
679         if (val == 0) {
680                 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
681                            line);
682                 return -1;
683         }
684         return val;
685 }
686
687
688 #ifndef NO_CONFIG_WRITE
689 static char * wpa_config_write_cipher(int cipher)
690 {
691         char *buf, *pos, *end;
692         int ret;
693
694         pos = buf = os_zalloc(50);
695         if (buf == NULL)
696                 return NULL;
697         end = buf + 50;
698
699         if (cipher & WPA_CIPHER_CCMP) {
700                 ret = os_snprintf(pos, end - pos, "%sCCMP",
701                                   pos == buf ? "" : " ");
702                 if (ret < 0 || ret >= end - pos) {
703                         end[-1] = '\0';
704                         return buf;
705                 }
706                 pos += ret;
707         }
708
709         if (cipher & WPA_CIPHER_TKIP) {
710                 ret = os_snprintf(pos, end - pos, "%sTKIP",
711                                   pos == buf ? "" : " ");
712                 if (ret < 0 || ret >= end - pos) {
713                         end[-1] = '\0';
714                         return buf;
715                 }
716                 pos += ret;
717         }
718
719         if (cipher & WPA_CIPHER_WEP104) {
720                 ret = os_snprintf(pos, end - pos, "%sWEP104",
721                                   pos == buf ? "" : " ");
722                 if (ret < 0 || ret >= end - pos) {
723                         end[-1] = '\0';
724                         return buf;
725                 }
726                 pos += ret;
727         }
728
729         if (cipher & WPA_CIPHER_WEP40) {
730                 ret = os_snprintf(pos, end - pos, "%sWEP40",
731                                   pos == buf ? "" : " ");
732                 if (ret < 0 || ret >= end - pos) {
733                         end[-1] = '\0';
734                         return buf;
735                 }
736                 pos += ret;
737         }
738
739         if (cipher & WPA_CIPHER_NONE) {
740                 ret = os_snprintf(pos, end - pos, "%sNONE",
741                                   pos == buf ? "" : " ");
742                 if (ret < 0 || ret >= end - pos) {
743                         end[-1] = '\0';
744                         return buf;
745                 }
746                 pos += ret;
747         }
748
749         return buf;
750 }
751 #endif /* NO_CONFIG_WRITE */
752
753
754 static int wpa_config_parse_pairwise(const struct parse_data *data,
755                                      struct wpa_ssid *ssid, int line,
756                                      const char *value)
757 {
758         int val;
759         val = wpa_config_parse_cipher(line, value);
760         if (val == -1)
761                 return -1;
762         if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {
763                 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
764                            "(0x%x).", line, val);
765                 return -1;
766         }
767
768         wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
769         ssid->pairwise_cipher = val;
770         return 0;
771 }
772
773
774 #ifndef NO_CONFIG_WRITE
775 static char * wpa_config_write_pairwise(const struct parse_data *data,
776                                         struct wpa_ssid *ssid)
777 {
778         return wpa_config_write_cipher(ssid->pairwise_cipher);
779 }
780 #endif /* NO_CONFIG_WRITE */
781
782
783 static int wpa_config_parse_group(const struct parse_data *data,
784                                   struct wpa_ssid *ssid, int line,
785                                   const char *value)
786 {
787         int val;
788         val = wpa_config_parse_cipher(line, value);
789         if (val == -1)
790                 return -1;
791         if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |
792                     WPA_CIPHER_WEP40)) {
793                 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
794                            "(0x%x).", line, val);
795                 return -1;
796         }
797
798         wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
799         ssid->group_cipher = val;
800         return 0;
801 }
802
803
804 #ifndef NO_CONFIG_WRITE
805 static char * wpa_config_write_group(const struct parse_data *data,
806                                      struct wpa_ssid *ssid)
807 {
808         return wpa_config_write_cipher(ssid->group_cipher);
809 }
810 #endif /* NO_CONFIG_WRITE */
811
812
813 static int wpa_config_parse_auth_alg(const struct parse_data *data,
814                                      struct wpa_ssid *ssid, int line,
815                                      const char *value)
816 {
817         int val = 0, last, errors = 0;
818         char *start, *end, *buf;
819
820         buf = os_strdup(value);
821         if (buf == NULL)
822                 return -1;
823         start = buf;
824
825         while (*start != '\0') {
826                 while (*start == ' ' || *start == '\t')
827                         start++;
828                 if (*start == '\0')
829                         break;
830                 end = start;
831                 while (*end != ' ' && *end != '\t' && *end != '\0')
832                         end++;
833                 last = *end == '\0';
834                 *end = '\0';
835                 if (os_strcmp(start, "OPEN") == 0)
836                         val |= WPA_AUTH_ALG_OPEN;
837                 else if (os_strcmp(start, "SHARED") == 0)
838                         val |= WPA_AUTH_ALG_SHARED;
839                 else if (os_strcmp(start, "LEAP") == 0)
840                         val |= WPA_AUTH_ALG_LEAP;
841                 else {
842                         wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
843                                    line, start);
844                         errors++;
845                 }
846
847                 if (last)
848                         break;
849                 start = end + 1;
850         }
851         os_free(buf);
852
853         if (val == 0) {
854                 wpa_printf(MSG_ERROR,
855                            "Line %d: no auth_alg values configured.", line);
856                 errors++;
857         }
858
859         wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
860         ssid->auth_alg = val;
861         return errors ? -1 : 0;
862 }
863
864
865 #ifndef NO_CONFIG_WRITE
866 static char * wpa_config_write_auth_alg(const struct parse_data *data,
867                                         struct wpa_ssid *ssid)
868 {
869         char *buf, *pos, *end;
870         int ret;
871
872         pos = buf = os_zalloc(30);
873         if (buf == NULL)
874                 return NULL;
875         end = buf + 30;
876
877         if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
878                 ret = os_snprintf(pos, end - pos, "%sOPEN",
879                                   pos == buf ? "" : " ");
880                 if (ret < 0 || ret >= end - pos) {
881                         end[-1] = '\0';
882                         return buf;
883                 }
884                 pos += ret;
885         }
886
887         if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
888                 ret = os_snprintf(pos, end - pos, "%sSHARED",
889                                   pos == buf ? "" : " ");
890                 if (ret < 0 || ret >= end - pos) {
891                         end[-1] = '\0';
892                         return buf;
893                 }
894                 pos += ret;
895         }
896
897         if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
898                 ret = os_snprintf(pos, end - pos, "%sLEAP",
899                                   pos == buf ? "" : " ");
900                 if (ret < 0 || ret >= end - pos) {
901                         end[-1] = '\0';
902                         return buf;
903                 }
904                 pos += ret;
905         }
906
907         return buf;
908 }
909 #endif /* NO_CONFIG_WRITE */
910
911
912 static int * wpa_config_parse_freqs(const struct parse_data *data,
913                                     struct wpa_ssid *ssid, int line,
914                                     const char *value)
915 {
916         int *freqs;
917         size_t used, len;
918         const char *pos;
919
920         used = 0;
921         len = 10;
922         freqs = os_zalloc((len + 1) * sizeof(int));
923         if (freqs == NULL)
924                 return NULL;
925
926         pos = value;
927         while (pos) {
928                 while (*pos == ' ')
929                         pos++;
930                 if (used == len) {
931                         int *n;
932                         size_t i;
933                         n = os_realloc(freqs, (len * 2 + 1) * sizeof(int));
934                         if (n == NULL) {
935                                 os_free(freqs);
936                                 return NULL;
937                         }
938                         for (i = len; i <= len * 2; i++)
939                                 n[i] = 0;
940                         freqs = n;
941                         len *= 2;
942                 }
943
944                 freqs[used] = atoi(pos);
945                 if (freqs[used] == 0)
946                         break;
947                 used++;
948                 pos = os_strchr(pos + 1, ' ');
949         }
950
951         return freqs;
952 }
953
954
955 static int wpa_config_parse_scan_freq(const struct parse_data *data,
956                                       struct wpa_ssid *ssid, int line,
957                                       const char *value)
958 {
959         int *freqs;
960
961         freqs = wpa_config_parse_freqs(data, ssid, line, value);
962         if (freqs == NULL)
963                 return -1;
964         os_free(ssid->scan_freq);
965         ssid->scan_freq = freqs;
966
967         return 0;
968 }
969
970
971 static int wpa_config_parse_freq_list(const struct parse_data *data,
972                                       struct wpa_ssid *ssid, int line,
973                                       const char *value)
974 {
975         int *freqs;
976
977         freqs = wpa_config_parse_freqs(data, ssid, line, value);
978         if (freqs == NULL)
979                 return -1;
980         os_free(ssid->freq_list);
981         ssid->freq_list = freqs;
982
983         return 0;
984 }
985
986
987 #ifndef NO_CONFIG_WRITE
988 static char * wpa_config_write_freqs(const struct parse_data *data,
989                                      const int *freqs)
990 {
991         char *buf, *pos, *end;
992         int i, ret;
993         size_t count;
994
995         if (freqs == NULL)
996                 return NULL;
997
998         count = 0;
999         for (i = 0; freqs[i]; i++)
1000                 count++;
1001
1002         pos = buf = os_zalloc(10 * count + 1);
1003         if (buf == NULL)
1004                 return NULL;
1005         end = buf + 10 * count + 1;
1006
1007         for (i = 0; freqs[i]; i++) {
1008                 ret = os_snprintf(pos, end - pos, "%s%u",
1009                                   i == 0 ? "" : " ", freqs[i]);
1010                 if (ret < 0 || ret >= end - pos) {
1011                         end[-1] = '\0';
1012                         return buf;
1013                 }
1014                 pos += ret;
1015         }
1016
1017         return buf;
1018 }
1019
1020
1021 static char * wpa_config_write_scan_freq(const struct parse_data *data,
1022                                          struct wpa_ssid *ssid)
1023 {
1024         return wpa_config_write_freqs(data, ssid->scan_freq);
1025 }
1026
1027
1028 static char * wpa_config_write_freq_list(const struct parse_data *data,
1029                                          struct wpa_ssid *ssid)
1030 {
1031         return wpa_config_write_freqs(data, ssid->freq_list);
1032 }
1033 #endif /* NO_CONFIG_WRITE */
1034
1035
1036 #ifdef IEEE8021X_EAPOL
1037 static int wpa_config_parse_eap(const struct parse_data *data,
1038                                 struct wpa_ssid *ssid, int line,
1039                                 const char *value)
1040 {
1041         int last, errors = 0;
1042         char *start, *end, *buf;
1043         struct eap_method_type *methods = NULL, *tmp;
1044         size_t num_methods = 0;
1045
1046         buf = os_strdup(value);
1047         if (buf == NULL)
1048                 return -1;
1049         start = buf;
1050
1051         while (*start != '\0') {
1052                 while (*start == ' ' || *start == '\t')
1053                         start++;
1054                 if (*start == '\0')
1055                         break;
1056                 end = start;
1057                 while (*end != ' ' && *end != '\t' && *end != '\0')
1058                         end++;
1059                 last = *end == '\0';
1060                 *end = '\0';
1061                 tmp = methods;
1062                 methods = os_realloc(methods,
1063                                      (num_methods + 1) * sizeof(*methods));
1064                 if (methods == NULL) {
1065                         os_free(tmp);
1066                         os_free(buf);
1067                         return -1;
1068                 }
1069                 methods[num_methods].method = eap_peer_get_type(
1070                         start, &methods[num_methods].vendor);
1071                 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1072                     methods[num_methods].method == EAP_TYPE_NONE) {
1073                         wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1074                                    "'%s'", line, start);
1075                         wpa_printf(MSG_ERROR, "You may need to add support for"
1076                                    " this EAP method during wpa_supplicant\n"
1077                                    "build time configuration.\n"
1078                                    "See README for more information.");
1079                         errors++;
1080                 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1081                            methods[num_methods].method == EAP_TYPE_LEAP)
1082                         ssid->leap++;
1083                 else
1084                         ssid->non_leap++;
1085                 num_methods++;
1086                 if (last)
1087                         break;
1088                 start = end + 1;
1089         }
1090         os_free(buf);
1091
1092         tmp = methods;
1093         methods = os_realloc(methods, (num_methods + 1) * sizeof(*methods));
1094         if (methods == NULL) {
1095                 os_free(tmp);
1096                 return -1;
1097         }
1098         methods[num_methods].vendor = EAP_VENDOR_IETF;
1099         methods[num_methods].method = EAP_TYPE_NONE;
1100         num_methods++;
1101
1102         wpa_hexdump(MSG_MSGDUMP, "eap methods",
1103                     (u8 *) methods, num_methods * sizeof(*methods));
1104         os_free(ssid->eap.eap_methods);
1105         ssid->eap.eap_methods = methods;
1106         return errors ? -1 : 0;
1107 }
1108
1109
1110 static char * wpa_config_write_eap(const struct parse_data *data,
1111                                    struct wpa_ssid *ssid)
1112 {
1113         int i, ret;
1114         char *buf, *pos, *end;
1115         const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1116         const char *name;
1117
1118         if (eap_methods == NULL)
1119                 return NULL;
1120
1121         pos = buf = os_zalloc(100);
1122         if (buf == NULL)
1123                 return NULL;
1124         end = buf + 100;
1125
1126         for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1127                      eap_methods[i].method != EAP_TYPE_NONE; i++) {
1128                 name = eap_get_name(eap_methods[i].vendor,
1129                                     eap_methods[i].method);
1130                 if (name) {
1131                         ret = os_snprintf(pos, end - pos, "%s%s",
1132                                           pos == buf ? "" : " ", name);
1133                         if (ret < 0 || ret >= end - pos)
1134                                 break;
1135                         pos += ret;
1136                 }
1137         }
1138
1139         end[-1] = '\0';
1140
1141         return buf;
1142 }
1143
1144
1145 static int wpa_config_parse_password(const struct parse_data *data,
1146                                      struct wpa_ssid *ssid, int line,
1147                                      const char *value)
1148 {
1149         u8 *hash;
1150
1151         if (os_strcmp(value, "NULL") == 0) {
1152                 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1153                 os_free(ssid->eap.password);
1154                 ssid->eap.password = NULL;
1155                 ssid->eap.password_len = 0;
1156                 return 0;
1157         }
1158
1159 #ifdef CONFIG_EXT_PASSWORD
1160         if (os_strncmp(value, "ext:", 4) == 0) {
1161                 char *name = os_strdup(value + 4);
1162                 if (name == NULL)
1163                         return -1;
1164                 os_free(ssid->eap.password);
1165                 ssid->eap.password = (u8 *) name;
1166                 ssid->eap.password_len = os_strlen(name);
1167                 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1168                 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1169                 return 0;
1170         }
1171 #endif /* CONFIG_EXT_PASSWORD */
1172
1173         if (os_strncmp(value, "hash:", 5) != 0) {
1174                 char *tmp;
1175                 size_t res_len;
1176
1177                 tmp = wpa_config_parse_string(value, &res_len);
1178                 if (tmp == NULL) {
1179                         wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1180                                    "password.", line);
1181                         return -1;
1182                 }
1183                 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1184                                       (u8 *) tmp, res_len);
1185
1186                 os_free(ssid->eap.password);
1187                 ssid->eap.password = (u8 *) tmp;
1188                 ssid->eap.password_len = res_len;
1189                 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1190                 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1191
1192                 return 0;
1193         }
1194
1195
1196         /* NtPasswordHash: hash:<32 hex digits> */
1197         if (os_strlen(value + 5) != 2 * 16) {
1198                 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1199                            "(expected 32 hex digits)", line);
1200                 return -1;
1201         }
1202
1203         hash = os_malloc(16);
1204         if (hash == NULL)
1205                 return -1;
1206
1207         if (hexstr2bin(value + 5, hash, 16)) {
1208                 os_free(hash);
1209                 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1210                 return -1;
1211         }
1212
1213         wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1214
1215         os_free(ssid->eap.password);
1216         ssid->eap.password = hash;
1217         ssid->eap.password_len = 16;
1218         ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1219         ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1220
1221         return 0;
1222 }
1223
1224
1225 static char * wpa_config_write_password(const struct parse_data *data,
1226                                         struct wpa_ssid *ssid)
1227 {
1228         char *buf;
1229
1230         if (ssid->eap.password == NULL)
1231                 return NULL;
1232
1233 #ifdef CONFIG_EXT_PASSWORD
1234         if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1235                 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1236                 if (buf == NULL)
1237                         return NULL;
1238                 os_memcpy(buf, "ext:", 4);
1239                 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1240                 return buf;
1241         }
1242 #endif /* CONFIG_EXT_PASSWORD */
1243
1244         if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1245                 return wpa_config_write_string(
1246                         ssid->eap.password, ssid->eap.password_len);
1247         }
1248
1249         buf = os_malloc(5 + 32 + 1);
1250         if (buf == NULL)
1251                 return NULL;
1252
1253         os_memcpy(buf, "hash:", 5);
1254         wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1255
1256         return buf;
1257 }
1258 #endif /* IEEE8021X_EAPOL */
1259
1260
1261 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1262                                     const char *value, int idx)
1263 {
1264         char *buf, title[20];
1265         int res;
1266
1267         buf = wpa_config_parse_string(value, len);
1268         if (buf == NULL) {
1269                 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1270                            line, idx, value);
1271                 return -1;
1272         }
1273         if (*len > MAX_WEP_KEY_LEN) {
1274                 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1275                            line, idx, value);
1276                 os_free(buf);
1277                 return -1;
1278         }
1279         if (*len && *len != 5 && *len != 13 && *len != 16) {
1280                 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1281                            "this network block will be ignored",
1282                            line, (unsigned int) *len);
1283         }
1284         os_memcpy(key, buf, *len);
1285         os_free(buf);
1286         res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1287         if (res >= 0 && (size_t) res < sizeof(title))
1288                 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1289         return 0;
1290 }
1291
1292
1293 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1294                                      struct wpa_ssid *ssid, int line,
1295                                      const char *value)
1296 {
1297         return wpa_config_parse_wep_key(ssid->wep_key[0],
1298                                         &ssid->wep_key_len[0], line,
1299                                         value, 0);
1300 }
1301
1302
1303 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1304                                      struct wpa_ssid *ssid, int line,
1305                                      const char *value)
1306 {
1307         return wpa_config_parse_wep_key(ssid->wep_key[1],
1308                                         &ssid->wep_key_len[1], line,
1309                                         value, 1);
1310 }
1311
1312
1313 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1314                                      struct wpa_ssid *ssid, int line,
1315                                      const char *value)
1316 {
1317         return wpa_config_parse_wep_key(ssid->wep_key[2],
1318                                         &ssid->wep_key_len[2], line,
1319                                         value, 2);
1320 }
1321
1322
1323 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1324                                      struct wpa_ssid *ssid, int line,
1325                                      const char *value)
1326 {
1327         return wpa_config_parse_wep_key(ssid->wep_key[3],
1328                                         &ssid->wep_key_len[3], line,
1329                                         value, 3);
1330 }
1331
1332
1333 #ifndef NO_CONFIG_WRITE
1334 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1335 {
1336         if (ssid->wep_key_len[idx] == 0)
1337                 return NULL;
1338         return wpa_config_write_string(ssid->wep_key[idx],
1339                                        ssid->wep_key_len[idx]);
1340 }
1341
1342
1343 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1344                                         struct wpa_ssid *ssid)
1345 {
1346         return wpa_config_write_wep_key(ssid, 0);
1347 }
1348
1349
1350 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1351                                         struct wpa_ssid *ssid)
1352 {
1353         return wpa_config_write_wep_key(ssid, 1);
1354 }
1355
1356
1357 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1358                                         struct wpa_ssid *ssid)
1359 {
1360         return wpa_config_write_wep_key(ssid, 2);
1361 }
1362
1363
1364 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1365                                         struct wpa_ssid *ssid)
1366 {
1367         return wpa_config_write_wep_key(ssid, 3);
1368 }
1369 #endif /* NO_CONFIG_WRITE */
1370
1371
1372 #ifdef CONFIG_P2P
1373
1374 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1375                                             struct wpa_ssid *ssid, int line,
1376                                             const char *value)
1377 {
1378         const char *pos;
1379         u8 *buf, *n, addr[ETH_ALEN];
1380         size_t count;
1381
1382         buf = NULL;
1383         count = 0;
1384
1385         pos = value;
1386         while (pos && *pos) {
1387                 while (*pos == ' ')
1388                         pos++;
1389
1390                 if (hwaddr_aton(pos, addr)) {
1391                         wpa_printf(MSG_ERROR, "Line %d: Invalid "
1392                                    "p2p_client_list address '%s'.",
1393                                    line, value);
1394                         /* continue anyway */
1395                 } else {
1396                         n = os_realloc(buf, (count + 1) * ETH_ALEN);
1397                         if (n == NULL) {
1398                                 os_free(buf);
1399                                 return -1;
1400                         }
1401                         buf = n;
1402                         os_memcpy(buf + count * ETH_ALEN, addr, ETH_ALEN);
1403                         count++;
1404                         wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1405                                     addr, ETH_ALEN);
1406                 }
1407
1408                 pos = os_strchr(pos, ' ');
1409         }
1410
1411         os_free(ssid->p2p_client_list);
1412         ssid->p2p_client_list = buf;
1413         ssid->num_p2p_clients = count;
1414
1415         return 0;
1416 }
1417
1418
1419 #ifndef NO_CONFIG_WRITE
1420 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1421                                                struct wpa_ssid *ssid)
1422 {
1423         char *value, *end, *pos;
1424         int res;
1425         size_t i;
1426
1427         if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1428                 return NULL;
1429
1430         value = os_malloc(20 * ssid->num_p2p_clients);
1431         if (value == NULL)
1432                 return NULL;
1433         pos = value;
1434         end = value + 20 * ssid->num_p2p_clients;
1435
1436         for (i = 0; i < ssid->num_p2p_clients; i++) {
1437                 res = os_snprintf(pos, end - pos, MACSTR " ",
1438                                   MAC2STR(ssid->p2p_client_list +
1439                                           i * ETH_ALEN));
1440                 if (res < 0 || res >= end - pos) {
1441                         os_free(value);
1442                         return NULL;
1443                 }
1444                 pos += res;
1445         }
1446
1447         if (pos > value)
1448                 pos[-1] = '\0';
1449
1450         return value;
1451 }
1452 #endif /* NO_CONFIG_WRITE */
1453
1454 #endif /* CONFIG_P2P */
1455
1456 /* Helper macros for network block parser */
1457
1458 #ifdef OFFSET
1459 #undef OFFSET
1460 #endif /* OFFSET */
1461 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1462 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1463
1464 /* STR: Define a string variable for an ASCII string; f = field name */
1465 #ifdef NO_CONFIG_WRITE
1466 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1467 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1468 #else /* NO_CONFIG_WRITE */
1469 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1470 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1471 #endif /* NO_CONFIG_WRITE */
1472 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1473 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1474 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1475 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1476
1477 /* STR_LEN: Define a string variable with a separate variable for storing the
1478  * data length. Unlike STR(), this can be used to store arbitrary binary data
1479  * (i.e., even nul termination character). */
1480 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1481 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1482 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1483 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1484 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1485
1486 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1487  * explicitly specified. */
1488 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1489 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1490 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1491
1492 #ifdef NO_CONFIG_WRITE
1493 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1494 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1495 #else /* NO_CONFIG_WRITE */
1496 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1497         OFFSET(f), (void *) 0
1498 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1499         OFFSET(eap.f), (void *) 0
1500 #endif /* NO_CONFIG_WRITE */
1501
1502 /* INT: Define an integer variable */
1503 #define INT(f) _INT(f), NULL, NULL, 0
1504 #define INTe(f) _INTe(f), NULL, NULL, 0
1505
1506 /* INT_RANGE: Define an integer variable with allowed value range */
1507 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1508
1509 /* FUNC: Define a configuration variable that uses a custom function for
1510  * parsing and writing the value. */
1511 #ifdef NO_CONFIG_WRITE
1512 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1513 #else /* NO_CONFIG_WRITE */
1514 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1515         NULL, NULL, NULL, NULL
1516 #endif /* NO_CONFIG_WRITE */
1517 #define FUNC(f) _FUNC(f), 0
1518 #define FUNC_KEY(f) _FUNC(f), 1
1519
1520 /*
1521  * Table of network configuration variables. This table is used to parse each
1522  * network configuration variable, e.g., each line in wpa_supplicant.conf file
1523  * that is inside a network block.
1524  *
1525  * This table is generated using the helper macros defined above and with
1526  * generous help from the C pre-processor. The field name is stored as a string
1527  * into .name and for STR and INT types, the offset of the target buffer within
1528  * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1529  * offset to the field containing the length of the configuration variable.
1530  * .param3 and .param4 can be used to mark the allowed range (length for STR
1531  * and value for INT).
1532  *
1533  * For each configuration line in wpa_supplicant.conf, the parser goes through
1534  * this table and select the entry that matches with the field name. The parser
1535  * function (.parser) is then called to parse the actual value of the field.
1536  *
1537  * This kind of mechanism makes it easy to add new configuration parameters,
1538  * since only one line needs to be added into this table and into the
1539  * struct wpa_ssid definition if the new variable is either a string or
1540  * integer. More complex types will need to use their own parser and writer
1541  * functions.
1542  */
1543 static const struct parse_data ssid_fields[] = {
1544         { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1545         { INT_RANGE(scan_ssid, 0, 1) },
1546         { FUNC(bssid) },
1547         { FUNC_KEY(psk) },
1548         { FUNC(proto) },
1549         { FUNC(key_mgmt) },
1550         { INT(bg_scan_period) },
1551         { FUNC(pairwise) },
1552         { FUNC(group) },
1553         { FUNC(auth_alg) },
1554         { FUNC(scan_freq) },
1555         { FUNC(freq_list) },
1556 #ifdef IEEE8021X_EAPOL
1557         { FUNC(eap) },
1558         { STR_LENe(identity) },
1559         { STR_LENe(anonymous_identity) },
1560         { FUNC_KEY(password) },
1561         { STRe(ca_cert) },
1562         { STRe(ca_path) },
1563         { STRe(client_cert) },
1564         { STRe(private_key) },
1565         { STR_KEYe(private_key_passwd) },
1566         { STRe(dh_file) },
1567         { STRe(subject_match) },
1568         { STRe(altsubject_match) },
1569         { STRe(ca_cert2) },
1570         { STRe(ca_path2) },
1571         { STRe(client_cert2) },
1572         { STRe(private_key2) },
1573         { STR_KEYe(private_key2_passwd) },
1574         { STRe(dh_file2) },
1575         { STRe(subject_match2) },
1576         { STRe(altsubject_match2) },
1577         { STRe(phase1) },
1578         { STRe(phase2) },
1579         { STRe(pcsc) },
1580         { STR_KEYe(pin) },
1581         { STRe(engine_id) },
1582         { STRe(key_id) },
1583         { STRe(cert_id) },
1584         { STRe(ca_cert_id) },
1585         { STR_KEYe(pin2) },
1586         { STRe(engine2_id) },
1587         { STRe(key2_id) },
1588         { STRe(cert2_id) },
1589         { STRe(ca_cert2_id) },
1590         { INTe(engine) },
1591         { INTe(engine2) },
1592         { INT(eapol_flags) },
1593 #endif /* IEEE8021X_EAPOL */
1594         { FUNC_KEY(wep_key0) },
1595         { FUNC_KEY(wep_key1) },
1596         { FUNC_KEY(wep_key2) },
1597         { FUNC_KEY(wep_key3) },
1598         { INT(wep_tx_keyidx) },
1599         { INT(priority) },
1600 #ifdef IEEE8021X_EAPOL
1601         { INT(eap_workaround) },
1602         { STRe(pac_file) },
1603         { INTe(fragment_size) },
1604 #endif /* IEEE8021X_EAPOL */
1605         { INT_RANGE(mode, 0, 4) },
1606         { INT_RANGE(proactive_key_caching, 0, 1) },
1607         { INT_RANGE(disabled, 0, 2) },
1608         { STR(id_str) },
1609 #ifdef CONFIG_IEEE80211W
1610         { INT_RANGE(ieee80211w, 0, 2) },
1611 #endif /* CONFIG_IEEE80211W */
1612         { INT_RANGE(peerkey, 0, 1) },
1613         { INT_RANGE(mixed_cell, 0, 1) },
1614         { INT_RANGE(frequency, 0, 10000) },
1615         { INT(wpa_ptk_rekey) },
1616         { STR(bgscan) },
1617         { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
1618 #ifdef CONFIG_P2P
1619         { FUNC(p2p_client_list) },
1620 #endif /* CONFIG_P2P */
1621 #ifdef CONFIG_HT_OVERRIDES
1622         { INT_RANGE(disable_ht, 0, 1) },
1623         { INT_RANGE(disable_ht40, -1, 1) },
1624         { INT_RANGE(disable_max_amsdu, -1, 1) },
1625         { INT_RANGE(ampdu_factor, -1, 3) },
1626         { INT_RANGE(ampdu_density, -1, 7) },
1627         { STR(ht_mcs) },
1628 #endif /* CONFIG_HT_OVERRIDES */
1629         { INT(ap_max_inactivity) },
1630         { INT(dtim_period) },
1631 };
1632
1633 #undef OFFSET
1634 #undef _STR
1635 #undef STR
1636 #undef STR_KEY
1637 #undef _STR_LEN
1638 #undef STR_LEN
1639 #undef STR_LEN_KEY
1640 #undef _STR_RANGE
1641 #undef STR_RANGE
1642 #undef STR_RANGE_KEY
1643 #undef _INT
1644 #undef INT
1645 #undef INT_RANGE
1646 #undef _FUNC
1647 #undef FUNC
1648 #undef FUNC_KEY
1649 #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1650
1651
1652 /**
1653  * wpa_config_add_prio_network - Add a network to priority lists
1654  * @config: Configuration data from wpa_config_read()
1655  * @ssid: Pointer to the network configuration to be added to the list
1656  * Returns: 0 on success, -1 on failure
1657  *
1658  * This function is used to add a network block to the priority list of
1659  * networks. This must be called for each network when reading in the full
1660  * configuration. In addition, this can be used indirectly when updating
1661  * priorities by calling wpa_config_update_prio_list().
1662  */
1663 int wpa_config_add_prio_network(struct wpa_config *config,
1664                                 struct wpa_ssid *ssid)
1665 {
1666         int prio;
1667         struct wpa_ssid *prev, **nlist;
1668
1669         /*
1670          * Add to an existing priority list if one is available for the
1671          * configured priority level for this network.
1672          */
1673         for (prio = 0; prio < config->num_prio; prio++) {
1674                 prev = config->pssid[prio];
1675                 if (prev->priority == ssid->priority) {
1676                         while (prev->pnext)
1677                                 prev = prev->pnext;
1678                         prev->pnext = ssid;
1679                         return 0;
1680                 }
1681         }
1682
1683         /* First network for this priority - add a new priority list */
1684         nlist = os_realloc(config->pssid,
1685                            (config->num_prio + 1) * sizeof(struct wpa_ssid *));
1686         if (nlist == NULL)
1687                 return -1;
1688
1689         for (prio = 0; prio < config->num_prio; prio++) {
1690                 if (nlist[prio]->priority < ssid->priority) {
1691                         os_memmove(&nlist[prio + 1], &nlist[prio],
1692                                    (config->num_prio - prio) *
1693                                    sizeof(struct wpa_ssid *));
1694                         break;
1695                 }
1696         }
1697
1698         nlist[prio] = ssid;
1699         config->num_prio++;
1700         config->pssid = nlist;
1701
1702         return 0;
1703 }
1704
1705
1706 /**
1707  * wpa_config_update_prio_list - Update network priority list
1708  * @config: Configuration data from wpa_config_read()
1709  * Returns: 0 on success, -1 on failure
1710  *
1711  * This function is called to update the priority list of networks in the
1712  * configuration when a network is being added or removed. This is also called
1713  * if a priority for a network is changed.
1714  */
1715 int wpa_config_update_prio_list(struct wpa_config *config)
1716 {
1717         struct wpa_ssid *ssid;
1718         int ret = 0;
1719
1720         os_free(config->pssid);
1721         config->pssid = NULL;
1722         config->num_prio = 0;
1723
1724         ssid = config->ssid;
1725         while (ssid) {
1726                 ssid->pnext = NULL;
1727                 if (wpa_config_add_prio_network(config, ssid) < 0)
1728                         ret = -1;
1729                 ssid = ssid->next;
1730         }
1731
1732         return ret;
1733 }
1734
1735
1736 #ifdef IEEE8021X_EAPOL
1737 static void eap_peer_config_free(struct eap_peer_config *eap)
1738 {
1739         os_free(eap->eap_methods);
1740         os_free(eap->identity);
1741         os_free(eap->anonymous_identity);
1742         os_free(eap->password);
1743         os_free(eap->ca_cert);
1744         os_free(eap->ca_path);
1745         os_free(eap->client_cert);
1746         os_free(eap->private_key);
1747         os_free(eap->private_key_passwd);
1748         os_free(eap->dh_file);
1749         os_free(eap->subject_match);
1750         os_free(eap->altsubject_match);
1751         os_free(eap->ca_cert2);
1752         os_free(eap->ca_path2);
1753         os_free(eap->client_cert2);
1754         os_free(eap->private_key2);
1755         os_free(eap->private_key2_passwd);
1756         os_free(eap->dh_file2);
1757         os_free(eap->subject_match2);
1758         os_free(eap->altsubject_match2);
1759         os_free(eap->phase1);
1760         os_free(eap->phase2);
1761         os_free(eap->pcsc);
1762         os_free(eap->pin);
1763         os_free(eap->engine_id);
1764         os_free(eap->key_id);
1765         os_free(eap->cert_id);
1766         os_free(eap->ca_cert_id);
1767         os_free(eap->key2_id);
1768         os_free(eap->cert2_id);
1769         os_free(eap->ca_cert2_id);
1770         os_free(eap->pin2);
1771         os_free(eap->engine2_id);
1772         os_free(eap->otp);
1773         os_free(eap->pending_req_otp);
1774         os_free(eap->pac_file);
1775         os_free(eap->new_password);
1776 }
1777 #endif /* IEEE8021X_EAPOL */
1778
1779
1780 /**
1781  * wpa_config_free_ssid - Free network/ssid configuration data
1782  * @ssid: Configuration data for the network
1783  *
1784  * This function frees all resources allocated for the network configuration
1785  * data.
1786  */
1787 void wpa_config_free_ssid(struct wpa_ssid *ssid)
1788 {
1789         os_free(ssid->ssid);
1790         os_free(ssid->passphrase);
1791         os_free(ssid->ext_psk);
1792 #ifdef IEEE8021X_EAPOL
1793         eap_peer_config_free(&ssid->eap);
1794 #endif /* IEEE8021X_EAPOL */
1795         os_free(ssid->id_str);
1796         os_free(ssid->scan_freq);
1797         os_free(ssid->freq_list);
1798         os_free(ssid->bgscan);
1799         os_free(ssid->p2p_client_list);
1800 #ifdef CONFIG_HT_OVERRIDES
1801         os_free(ssid->ht_mcs);
1802 #endif /* CONFIG_HT_OVERRIDES */
1803         os_free(ssid);
1804 }
1805
1806
1807 void wpa_config_free_cred(struct wpa_cred *cred)
1808 {
1809         os_free(cred->realm);
1810         os_free(cred->username);
1811         os_free(cred->password);
1812         os_free(cred->ca_cert);
1813         os_free(cred->client_cert);
1814         os_free(cred->private_key);
1815         os_free(cred->private_key_passwd);
1816         os_free(cred->imsi);
1817         os_free(cred->milenage);
1818         os_free(cred->domain);
1819         os_free(cred->eap_method);
1820         os_free(cred->phase1);
1821         os_free(cred->phase2);
1822         os_free(cred);
1823 }
1824
1825
1826 /**
1827  * wpa_config_free - Free configuration data
1828  * @config: Configuration data from wpa_config_read()
1829  *
1830  * This function frees all resources allocated for the configuration data by
1831  * wpa_config_read().
1832  */
1833 void wpa_config_free(struct wpa_config *config)
1834 {
1835 #ifndef CONFIG_NO_CONFIG_BLOBS
1836         struct wpa_config_blob *blob, *prevblob;
1837 #endif /* CONFIG_NO_CONFIG_BLOBS */
1838         struct wpa_ssid *ssid, *prev = NULL;
1839         struct wpa_cred *cred, *cprev;
1840
1841         ssid = config->ssid;
1842         while (ssid) {
1843                 prev = ssid;
1844                 ssid = ssid->next;
1845                 wpa_config_free_ssid(prev);
1846         }
1847
1848         cred = config->cred;
1849         while (cred) {
1850                 cprev = cred;
1851                 cred = cred->next;
1852                 wpa_config_free_cred(cprev);
1853         }
1854
1855 #ifndef CONFIG_NO_CONFIG_BLOBS
1856         blob = config->blobs;
1857         prevblob = NULL;
1858         while (blob) {
1859                 prevblob = blob;
1860                 blob = blob->next;
1861                 wpa_config_free_blob(prevblob);
1862         }
1863 #endif /* CONFIG_NO_CONFIG_BLOBS */
1864
1865         wpabuf_free(config->wps_vendor_ext_m1);
1866         os_free(config->ctrl_interface);
1867         os_free(config->ctrl_interface_group);
1868         os_free(config->opensc_engine_path);
1869         os_free(config->pkcs11_engine_path);
1870         os_free(config->pkcs11_module_path);
1871         os_free(config->pcsc_reader);
1872         os_free(config->pcsc_pin);
1873         os_free(config->driver_param);
1874         os_free(config->device_name);
1875         os_free(config->manufacturer);
1876         os_free(config->model_name);
1877         os_free(config->model_number);
1878         os_free(config->serial_number);
1879         os_free(config->config_methods);
1880         os_free(config->p2p_ssid_postfix);
1881         os_free(config->pssid);
1882         os_free(config->p2p_pref_chan);
1883         os_free(config->autoscan);
1884         wpabuf_free(config->wps_nfc_dh_pubkey);
1885         wpabuf_free(config->wps_nfc_dh_privkey);
1886         wpabuf_free(config->wps_nfc_dev_pw);
1887         os_free(config->ext_password_backend);
1888         os_free(config);
1889 }
1890
1891
1892 /**
1893  * wpa_config_foreach_network - Iterate over each configured network
1894  * @config: Configuration data from wpa_config_read()
1895  * @func: Callback function to process each network
1896  * @arg: Opaque argument to pass to callback function
1897  *
1898  * Iterate over the set of configured networks calling the specified
1899  * function for each item. We guard against callbacks removing the
1900  * supplied network.
1901  */
1902 void wpa_config_foreach_network(struct wpa_config *config,
1903                                 void (*func)(void *, struct wpa_ssid *),
1904                                 void *arg)
1905 {
1906         struct wpa_ssid *ssid, *next;
1907
1908         ssid = config->ssid;
1909         while (ssid) {
1910                 next = ssid->next;
1911                 func(arg, ssid);
1912                 ssid = next;
1913         }
1914 }
1915
1916
1917 /**
1918  * wpa_config_get_network - Get configured network based on id
1919  * @config: Configuration data from wpa_config_read()
1920  * @id: Unique network id to search for
1921  * Returns: Network configuration or %NULL if not found
1922  */
1923 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1924 {
1925         struct wpa_ssid *ssid;
1926
1927         ssid = config->ssid;
1928         while (ssid) {
1929                 if (id == ssid->id)
1930                         break;
1931                 ssid = ssid->next;
1932         }
1933
1934         return ssid;
1935 }
1936
1937
1938 /**
1939  * wpa_config_add_network - Add a new network with empty configuration
1940  * @config: Configuration data from wpa_config_read()
1941  * Returns: The new network configuration or %NULL if operation failed
1942  */
1943 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1944 {
1945         int id;
1946         struct wpa_ssid *ssid, *last = NULL;
1947
1948         id = -1;
1949         ssid = config->ssid;
1950         while (ssid) {
1951                 if (ssid->id > id)
1952                         id = ssid->id;
1953                 last = ssid;
1954                 ssid = ssid->next;
1955         }
1956         id++;
1957
1958         ssid = os_zalloc(sizeof(*ssid));
1959         if (ssid == NULL)
1960                 return NULL;
1961         ssid->id = id;
1962         if (last)
1963                 last->next = ssid;
1964         else
1965                 config->ssid = ssid;
1966
1967         wpa_config_update_prio_list(config);
1968
1969         return ssid;
1970 }
1971
1972
1973 /**
1974  * wpa_config_remove_network - Remove a configured network based on id
1975  * @config: Configuration data from wpa_config_read()
1976  * @id: Unique network id to search for
1977  * Returns: 0 on success, or -1 if the network was not found
1978  */
1979 int wpa_config_remove_network(struct wpa_config *config, int id)
1980 {
1981         struct wpa_ssid *ssid, *prev = NULL;
1982
1983         ssid = config->ssid;
1984         while (ssid) {
1985                 if (id == ssid->id)
1986                         break;
1987                 prev = ssid;
1988                 ssid = ssid->next;
1989         }
1990
1991         if (ssid == NULL)
1992                 return -1;
1993
1994         if (prev)
1995                 prev->next = ssid->next;
1996         else
1997                 config->ssid = ssid->next;
1998
1999         wpa_config_update_prio_list(config);
2000         wpa_config_free_ssid(ssid);
2001         return 0;
2002 }
2003
2004
2005 /**
2006  * wpa_config_set_network_defaults - Set network default values
2007  * @ssid: Pointer to network configuration data
2008  */
2009 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2010 {
2011         ssid->proto = DEFAULT_PROTO;
2012         ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2013         ssid->group_cipher = DEFAULT_GROUP;
2014         ssid->key_mgmt = DEFAULT_KEY_MGMT;
2015         ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
2016 #ifdef IEEE8021X_EAPOL
2017         ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2018         ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2019         ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
2020 #endif /* IEEE8021X_EAPOL */
2021 #ifdef CONFIG_HT_OVERRIDES
2022         ssid->disable_ht = DEFAULT_DISABLE_HT;
2023         ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
2024         ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2025         ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2026         ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2027 #endif /* CONFIG_HT_OVERRIDES */
2028 }
2029
2030
2031 /**
2032  * wpa_config_set - Set a variable in network configuration
2033  * @ssid: Pointer to network configuration data
2034  * @var: Variable name, e.g., "ssid"
2035  * @value: Variable value
2036  * @line: Line number in configuration file or 0 if not used
2037  * Returns: 0 on success, -1 on failure
2038  *
2039  * This function can be used to set network configuration variables based on
2040  * both the configuration file and management interface input. The value
2041  * parameter must be in the same format as the text-based configuration file is
2042  * using. For example, strings are using double quotation marks.
2043  */
2044 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2045                    int line)
2046 {
2047         size_t i;
2048         int ret = 0;
2049
2050         if (ssid == NULL || var == NULL || value == NULL)
2051                 return -1;
2052
2053         for (i = 0; i < NUM_SSID_FIELDS; i++) {
2054                 const struct parse_data *field = &ssid_fields[i];
2055                 if (os_strcmp(var, field->name) != 0)
2056                         continue;
2057
2058                 if (field->parser(field, ssid, line, value)) {
2059                         if (line) {
2060                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
2061                                            "parse %s '%s'.", line, var, value);
2062                         }
2063                         ret = -1;
2064                 }
2065                 break;
2066         }
2067         if (i == NUM_SSID_FIELDS) {
2068                 if (line) {
2069                         wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2070                                    "'%s'.", line, var);
2071                 }
2072                 ret = -1;
2073         }
2074
2075         return ret;
2076 }
2077
2078
2079 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2080                           const char *value)
2081 {
2082         size_t len;
2083         char *buf;
2084         int ret;
2085
2086         len = os_strlen(value);
2087         buf = os_malloc(len + 3);
2088         if (buf == NULL)
2089                 return -1;
2090         buf[0] = '"';
2091         os_memcpy(buf + 1, value, len);
2092         buf[len + 1] = '"';
2093         buf[len + 2] = '\0';
2094         ret = wpa_config_set(ssid, var, buf, 0);
2095         os_free(buf);
2096         return ret;
2097 }
2098
2099
2100 /**
2101  * wpa_config_get_all - Get all options from network configuration
2102  * @ssid: Pointer to network configuration data
2103  * @get_keys: Determines if keys/passwords will be included in returned list
2104  *      (if they may be exported)
2105  * Returns: %NULL terminated list of all set keys and their values in the form
2106  * of [key1, val1, key2, val2, ... , NULL]
2107  *
2108  * This function can be used to get list of all configured network properties.
2109  * The caller is responsible for freeing the returned list and all its
2110  * elements.
2111  */
2112 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2113 {
2114         const struct parse_data *field;
2115         char *key, *value;
2116         size_t i;
2117         char **props;
2118         int fields_num;
2119
2120         get_keys = get_keys && ssid->export_keys;
2121
2122         props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
2123         if (!props)
2124                 return NULL;
2125
2126         fields_num = 0;
2127         for (i = 0; i < NUM_SSID_FIELDS; i++) {
2128                 field = &ssid_fields[i];
2129                 if (field->key_data && !get_keys)
2130                         continue;
2131                 value = field->writer(field, ssid);
2132                 if (value == NULL)
2133                         continue;
2134                 if (os_strlen(value) == 0) {
2135                         os_free(value);
2136                         continue;
2137                 }
2138
2139                 key = os_strdup(field->name);
2140                 if (key == NULL) {
2141                         os_free(value);
2142                         goto err;
2143                 }
2144
2145                 props[fields_num * 2] = key;
2146                 props[fields_num * 2 + 1] = value;
2147
2148                 fields_num++;
2149         }
2150
2151         return props;
2152
2153 err:
2154         value = *props;
2155         while (value)
2156                 os_free(value++);
2157         os_free(props);
2158         return NULL;
2159 }
2160
2161
2162 #ifndef NO_CONFIG_WRITE
2163 /**
2164  * wpa_config_get - Get a variable in network configuration
2165  * @ssid: Pointer to network configuration data
2166  * @var: Variable name, e.g., "ssid"
2167  * Returns: Value of the variable or %NULL on failure
2168  *
2169  * This function can be used to get network configuration variables. The
2170  * returned value is a copy of the configuration variable in text format, i.e,.
2171  * the same format that the text-based configuration file and wpa_config_set()
2172  * are using for the value. The caller is responsible for freeing the returned
2173  * value.
2174  */
2175 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2176 {
2177         size_t i;
2178
2179         if (ssid == NULL || var == NULL)
2180                 return NULL;
2181
2182         for (i = 0; i < NUM_SSID_FIELDS; i++) {
2183                 const struct parse_data *field = &ssid_fields[i];
2184                 if (os_strcmp(var, field->name) == 0)
2185                         return field->writer(field, ssid);
2186         }
2187
2188         return NULL;
2189 }
2190
2191
2192 /**
2193  * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2194  * @ssid: Pointer to network configuration data
2195  * @var: Variable name, e.g., "ssid"
2196  * Returns: Value of the variable or %NULL on failure
2197  *
2198  * This function can be used to get network configuration variable like
2199  * wpa_config_get(). The only difference is that this functions does not expose
2200  * key/password material from the configuration. In case a key/password field
2201  * is requested, the returned value is an empty string or %NULL if the variable
2202  * is not set or "*" if the variable is set (regardless of its value). The
2203  * returned value is a copy of the configuration variable in text format, i.e,.
2204  * the same format that the text-based configuration file and wpa_config_set()
2205  * are using for the value. The caller is responsible for freeing the returned
2206  * value.
2207  */
2208 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2209 {
2210         size_t i;
2211
2212         if (ssid == NULL || var == NULL)
2213                 return NULL;
2214
2215         for (i = 0; i < NUM_SSID_FIELDS; i++) {
2216                 const struct parse_data *field = &ssid_fields[i];
2217                 if (os_strcmp(var, field->name) == 0) {
2218                         char *res = field->writer(field, ssid);
2219                         if (field->key_data) {
2220                                 if (res && res[0]) {
2221                                         wpa_printf(MSG_DEBUG, "Do not allow "
2222                                                    "key_data field to be "
2223                                                    "exposed");
2224                                         os_free(res);
2225                                         return os_strdup("*");
2226                                 }
2227
2228                                 os_free(res);
2229                                 return NULL;
2230                         }
2231                         return res;
2232                 }
2233         }
2234
2235         return NULL;
2236 }
2237 #endif /* NO_CONFIG_WRITE */
2238
2239
2240 /**
2241  * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2242  * @ssid: Pointer to network configuration data
2243  *
2244  * This function must be called to update WPA PSK when either SSID or the
2245  * passphrase has changed for the network configuration.
2246  */
2247 void wpa_config_update_psk(struct wpa_ssid *ssid)
2248 {
2249 #ifndef CONFIG_NO_PBKDF2
2250         pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
2251                     ssid->psk, PMK_LEN);
2252         wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2253                         ssid->psk, PMK_LEN);
2254         ssid->psk_set = 1;
2255 #endif /* CONFIG_NO_PBKDF2 */
2256 }
2257
2258
2259 int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2260                         const char *value, int line)
2261 {
2262         char *val;
2263         size_t len;
2264
2265         if (os_strcmp(var, "priority") == 0) {
2266                 cred->priority = atoi(value);
2267                 return 0;
2268         }
2269
2270         if (os_strcmp(var, "pcsc") == 0) {
2271                 cred->pcsc = atoi(value);
2272                 return 0;
2273         }
2274
2275         if (os_strcmp(var, "eap") == 0) {
2276                 struct eap_method_type method;
2277                 method.method = eap_peer_get_type(value, &method.vendor);
2278                 if (method.vendor == EAP_VENDOR_IETF &&
2279                     method.method == EAP_TYPE_NONE) {
2280                         wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2281                                    "for a credential", line, value);
2282                         return -1;
2283                 }
2284                 os_free(cred->eap_method);
2285                 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2286                 if (cred->eap_method == NULL)
2287                         return -1;
2288                 os_memcpy(cred->eap_method, &method, sizeof(method));
2289                 return 0;
2290         }
2291
2292         if (os_strcmp(var, "password") == 0 &&
2293             os_strncmp(value, "ext:", 4) == 0) {
2294                 os_free(cred->password);
2295                 cred->password = os_strdup(value);
2296                 cred->ext_password = 1;
2297                 return 0;
2298         }
2299
2300         val = wpa_config_parse_string(value, &len);
2301         if (val == NULL) {
2302                 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2303                            "value '%s'.", line, var, value);
2304                 return -1;
2305         }
2306
2307         if (os_strcmp(var, "realm") == 0) {
2308                 os_free(cred->realm);
2309                 cred->realm = val;
2310                 return 0;
2311         }
2312
2313         if (os_strcmp(var, "username") == 0) {
2314                 os_free(cred->username);
2315                 cred->username = val;
2316                 return 0;
2317         }
2318
2319         if (os_strcmp(var, "password") == 0) {
2320                 os_free(cred->password);
2321                 cred->password = val;
2322                 cred->ext_password = 0;
2323                 return 0;
2324         }
2325
2326         if (os_strcmp(var, "ca_cert") == 0) {
2327                 os_free(cred->ca_cert);
2328                 cred->ca_cert = val;
2329                 return 0;
2330         }
2331
2332         if (os_strcmp(var, "client_cert") == 0) {
2333                 os_free(cred->client_cert);
2334                 cred->client_cert = val;
2335                 return 0;
2336         }
2337
2338         if (os_strcmp(var, "private_key") == 0) {
2339                 os_free(cred->private_key);
2340                 cred->private_key = val;
2341                 return 0;
2342         }
2343
2344         if (os_strcmp(var, "private_key_passwd") == 0) {
2345                 os_free(cred->private_key_passwd);
2346                 cred->private_key_passwd = val;
2347                 return 0;
2348         }
2349
2350         if (os_strcmp(var, "imsi") == 0) {
2351                 os_free(cred->imsi);
2352                 cred->imsi = val;
2353                 return 0;
2354         }
2355
2356         if (os_strcmp(var, "milenage") == 0) {
2357                 os_free(cred->milenage);
2358                 cred->milenage = val;
2359                 return 0;
2360         }
2361
2362         if (os_strcmp(var, "domain") == 0) {
2363                 os_free(cred->domain);
2364                 cred->domain = val;
2365                 return 0;
2366         }
2367
2368         if (os_strcmp(var, "phase1") == 0) {
2369                 os_free(cred->phase1);
2370                 cred->phase1 = val;
2371                 return 0;
2372         }
2373
2374         if (os_strcmp(var, "phase2") == 0) {
2375                 os_free(cred->phase2);
2376                 cred->phase2 = val;
2377                 return 0;
2378         }
2379
2380         if (os_strcmp(var, "roaming_consortium") == 0) {
2381                 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2382                         wpa_printf(MSG_ERROR, "Line %d: invalid "
2383                                    "roaming_consortium length %d (3..15 "
2384                                    "expected)", line, (int) len);
2385                         os_free(val);
2386                         return -1;
2387                 }
2388                 os_memcpy(cred->roaming_consortium, val, len);
2389                 cred->roaming_consortium_len = len;
2390                 os_free(val);
2391                 return 0;
2392         }
2393
2394         if (line) {
2395                 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2396                            line, var);
2397         }
2398
2399         os_free(val);
2400
2401         return -1;
2402 }
2403
2404
2405 struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
2406 {
2407         struct wpa_cred *cred;
2408
2409         cred = config->cred;
2410         while (cred) {
2411                 if (id == cred->id)
2412                         break;
2413                 cred = cred->next;
2414         }
2415
2416         return cred;
2417 }
2418
2419
2420 struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
2421 {
2422         int id;
2423         struct wpa_cred *cred, *last = NULL;
2424
2425         id = -1;
2426         cred = config->cred;
2427         while (cred) {
2428                 if (cred->id > id)
2429                         id = cred->id;
2430                 last = cred;
2431                 cred = cred->next;
2432         }
2433         id++;
2434
2435         cred = os_zalloc(sizeof(*cred));
2436         if (cred == NULL)
2437                 return NULL;
2438         cred->id = id;
2439         if (last)
2440                 last->next = cred;
2441         else
2442                 config->cred = cred;
2443
2444         return cred;
2445 }
2446
2447
2448 int wpa_config_remove_cred(struct wpa_config *config, int id)
2449 {
2450         struct wpa_cred *cred, *prev = NULL;
2451
2452         cred = config->cred;
2453         while (cred) {
2454                 if (id == cred->id)
2455                         break;
2456                 prev = cred;
2457                 cred = cred->next;
2458         }
2459
2460         if (cred == NULL)
2461                 return -1;
2462
2463         if (prev)
2464                 prev->next = cred->next;
2465         else
2466                 config->cred = cred->next;
2467
2468         wpa_config_free_cred(cred);
2469         return 0;
2470 }
2471
2472
2473 #ifndef CONFIG_NO_CONFIG_BLOBS
2474 /**
2475  * wpa_config_get_blob - Get a named configuration blob
2476  * @config: Configuration data from wpa_config_read()
2477  * @name: Name of the blob
2478  * Returns: Pointer to blob data or %NULL if not found
2479  */
2480 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2481                                                    const char *name)
2482 {
2483         struct wpa_config_blob *blob = config->blobs;
2484
2485         while (blob) {
2486                 if (os_strcmp(blob->name, name) == 0)
2487                         return blob;
2488                 blob = blob->next;
2489         }
2490         return NULL;
2491 }
2492
2493
2494 /**
2495  * wpa_config_set_blob - Set or add a named configuration blob
2496  * @config: Configuration data from wpa_config_read()
2497  * @blob: New value for the blob
2498  *
2499  * Adds a new configuration blob or replaces the current value of an existing
2500  * blob.
2501  */
2502 void wpa_config_set_blob(struct wpa_config *config,
2503                          struct wpa_config_blob *blob)
2504 {
2505         wpa_config_remove_blob(config, blob->name);
2506         blob->next = config->blobs;
2507         config->blobs = blob;
2508 }
2509
2510
2511 /**
2512  * wpa_config_free_blob - Free blob data
2513  * @blob: Pointer to blob to be freed
2514  */
2515 void wpa_config_free_blob(struct wpa_config_blob *blob)
2516 {
2517         if (blob) {
2518                 os_free(blob->name);
2519                 os_free(blob->data);
2520                 os_free(blob);
2521         }
2522 }
2523
2524
2525 /**
2526  * wpa_config_remove_blob - Remove a named configuration blob
2527  * @config: Configuration data from wpa_config_read()
2528  * @name: Name of the blob to remove
2529  * Returns: 0 if blob was removed or -1 if blob was not found
2530  */
2531 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2532 {
2533         struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2534
2535         while (pos) {
2536                 if (os_strcmp(pos->name, name) == 0) {
2537                         if (prev)
2538                                 prev->next = pos->next;
2539                         else
2540                                 config->blobs = pos->next;
2541                         wpa_config_free_blob(pos);
2542                         return 0;
2543                 }
2544                 prev = pos;
2545                 pos = pos->next;
2546         }
2547
2548         return -1;
2549 }
2550 #endif /* CONFIG_NO_CONFIG_BLOBS */
2551
2552
2553 /**
2554  * wpa_config_alloc_empty - Allocate an empty configuration
2555  * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2556  * socket
2557  * @driver_param: Driver parameters
2558  * Returns: Pointer to allocated configuration data or %NULL on failure
2559  */
2560 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2561                                            const char *driver_param)
2562 {
2563         struct wpa_config *config;
2564
2565         config = os_zalloc(sizeof(*config));
2566         if (config == NULL)
2567                 return NULL;
2568         config->eapol_version = DEFAULT_EAPOL_VERSION;
2569         config->ap_scan = DEFAULT_AP_SCAN;
2570         config->fast_reauth = DEFAULT_FAST_REAUTH;
2571         config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2572         config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
2573         config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2574         config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2575         config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2576         config->max_num_sta = DEFAULT_MAX_NUM_STA;
2577         config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
2578
2579         if (ctrl_interface)
2580                 config->ctrl_interface = os_strdup(ctrl_interface);
2581         if (driver_param)
2582                 config->driver_param = os_strdup(driver_param);
2583
2584         return config;
2585 }
2586
2587
2588 #ifndef CONFIG_NO_STDOUT_DEBUG
2589 /**
2590  * wpa_config_debug_dump_networks - Debug dump of configured networks
2591  * @config: Configuration data from wpa_config_read()
2592  */
2593 void wpa_config_debug_dump_networks(struct wpa_config *config)
2594 {
2595         int prio;
2596         struct wpa_ssid *ssid;
2597
2598         for (prio = 0; prio < config->num_prio; prio++) {
2599                 ssid = config->pssid[prio];
2600                 wpa_printf(MSG_DEBUG, "Priority group %d",
2601                            ssid->priority);
2602                 while (ssid) {
2603                         wpa_printf(MSG_DEBUG, "   id=%d ssid='%s'",
2604                                    ssid->id,
2605                                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2606                         ssid = ssid->pnext;
2607                 }
2608         }
2609 }
2610 #endif /* CONFIG_NO_STDOUT_DEBUG */
2611
2612
2613 struct global_parse_data {
2614         char *name;
2615         int (*parser)(const struct global_parse_data *data,
2616                       struct wpa_config *config, int line, const char *value);
2617         void *param1, *param2, *param3;
2618         unsigned int changed_flag;
2619 };
2620
2621
2622 static int wpa_global_config_parse_int(const struct global_parse_data *data,
2623                                        struct wpa_config *config, int line,
2624                                        const char *pos)
2625 {
2626         int *dst;
2627         dst = (int *) (((u8 *) config) + (long) data->param1);
2628         *dst = atoi(pos);
2629         wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2630
2631         if (data->param2 && *dst < (long) data->param2) {
2632                 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2633                            "min_value=%ld)", line, data->name, *dst,
2634                            (long) data->param2);
2635                 *dst = (long) data->param2;
2636                 return -1;
2637         }
2638
2639         if (data->param3 && *dst > (long) data->param3) {
2640                 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2641                            "max_value=%ld)", line, data->name, *dst,
2642                            (long) data->param3);
2643                 *dst = (long) data->param3;
2644                 return -1;
2645         }
2646
2647         return 0;
2648 }
2649
2650
2651 static int wpa_global_config_parse_str(const struct global_parse_data *data,
2652                                        struct wpa_config *config, int line,
2653                                        const char *pos)
2654 {
2655         size_t len;
2656         char **dst, *tmp;
2657
2658         len = os_strlen(pos);
2659         if (data->param2 && len < (size_t) data->param2) {
2660                 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2661                            "min_len=%ld)", line, data->name,
2662                            (unsigned long) len, (long) data->param2);
2663                 return -1;
2664         }
2665
2666         if (data->param3 && len > (size_t) data->param3) {
2667                 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2668                            "max_len=%ld)", line, data->name,
2669                            (unsigned long) len, (long) data->param3);
2670                 return -1;
2671         }
2672
2673         tmp = os_strdup(pos);
2674         if (tmp == NULL)
2675                 return -1;
2676
2677         dst = (char **) (((u8 *) config) + (long) data->param1);
2678         os_free(*dst);
2679         *dst = tmp;
2680         wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2681
2682         return 0;
2683 }
2684
2685
2686 static int wpa_global_config_parse_bin(const struct global_parse_data *data,
2687                                        struct wpa_config *config, int line,
2688                                        const char *pos)
2689 {
2690         size_t len;
2691         struct wpabuf **dst, *tmp;
2692
2693         len = os_strlen(pos);
2694         if (len & 0x01)
2695                 return -1;
2696
2697         tmp = wpabuf_alloc(len / 2);
2698         if (tmp == NULL)
2699                 return -1;
2700
2701         if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
2702                 wpabuf_free(tmp);
2703                 return -1;
2704         }
2705
2706         dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
2707         wpabuf_free(*dst);
2708         *dst = tmp;
2709         wpa_printf(MSG_DEBUG, "%s", data->name);
2710
2711         return 0;
2712 }
2713
2714
2715 static int wpa_config_process_country(const struct global_parse_data *data,
2716                                       struct wpa_config *config, int line,
2717                                       const char *pos)
2718 {
2719         if (!pos[0] || !pos[1]) {
2720                 wpa_printf(MSG_DEBUG, "Invalid country set");
2721                 return -1;
2722         }
2723         config->country[0] = pos[0];
2724         config->country[1] = pos[1];
2725         wpa_printf(MSG_DEBUG, "country='%c%c'",
2726                    config->country[0], config->country[1]);
2727         return 0;
2728 }
2729
2730
2731 static int wpa_config_process_load_dynamic_eap(
2732         const struct global_parse_data *data, struct wpa_config *config,
2733         int line, const char *so)
2734 {
2735         int ret;
2736         wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2737         ret = eap_peer_method_load(so);
2738         if (ret == -2) {
2739                 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2740                            "reloading.");
2741         } else if (ret) {
2742                 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
2743                            "method '%s'.", line, so);
2744                 return -1;
2745         }
2746
2747         return 0;
2748 }
2749
2750
2751 #ifdef CONFIG_WPS
2752
2753 static int wpa_config_process_uuid(const struct global_parse_data *data,
2754                                    struct wpa_config *config, int line,
2755                                    const char *pos)
2756 {
2757         char buf[40];
2758         if (uuid_str2bin(pos, config->uuid)) {
2759                 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
2760                 return -1;
2761         }
2762         uuid_bin2str(config->uuid, buf, sizeof(buf));
2763         wpa_printf(MSG_DEBUG, "uuid=%s", buf);
2764         return 0;
2765 }
2766
2767
2768 static int wpa_config_process_device_type(
2769         const struct global_parse_data *data,
2770         struct wpa_config *config, int line, const char *pos)
2771 {
2772         return wps_dev_type_str2bin(pos, config->device_type);
2773 }
2774
2775
2776 static int wpa_config_process_os_version(const struct global_parse_data *data,
2777                                          struct wpa_config *config, int line,
2778                                          const char *pos)
2779 {
2780         if (hexstr2bin(pos, config->os_version, 4)) {
2781                 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
2782                 return -1;
2783         }
2784         wpa_printf(MSG_DEBUG, "os_version=%08x",
2785                    WPA_GET_BE32(config->os_version));
2786         return 0;
2787 }
2788
2789
2790 static int wpa_config_process_wps_vendor_ext_m1(
2791         const struct global_parse_data *data,
2792         struct wpa_config *config, int line, const char *pos)
2793 {
2794         struct wpabuf *tmp;
2795         int len = os_strlen(pos) / 2;
2796         u8 *p;
2797
2798         if (!len) {
2799                 wpa_printf(MSG_ERROR, "Line %d: "
2800                            "invalid wps_vendor_ext_m1", line);
2801                 return -1;
2802         }
2803
2804         tmp = wpabuf_alloc(len);
2805         if (tmp) {
2806                 p = wpabuf_put(tmp, len);
2807
2808                 if (hexstr2bin(pos, p, len)) {
2809                         wpa_printf(MSG_ERROR, "Line %d: "
2810                                    "invalid wps_vendor_ext_m1", line);
2811                         wpabuf_free(tmp);
2812                         return -1;
2813                 }
2814
2815                 wpabuf_free(config->wps_vendor_ext_m1);
2816                 config->wps_vendor_ext_m1 = tmp;
2817         } else {
2818                 wpa_printf(MSG_ERROR, "Can not allocate "
2819                            "memory for wps_vendor_ext_m1");
2820                 return -1;
2821         }
2822
2823         return 0;
2824 }
2825
2826 #endif /* CONFIG_WPS */
2827
2828 #ifdef CONFIG_P2P
2829 static int wpa_config_process_sec_device_type(
2830         const struct global_parse_data *data,
2831         struct wpa_config *config, int line, const char *pos)
2832 {
2833         int idx;
2834
2835         if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
2836                 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
2837                            "items", line);
2838                 return -1;
2839         }
2840
2841         idx = config->num_sec_device_types;
2842
2843         if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
2844                 return -1;
2845
2846         config->num_sec_device_types++;
2847         return 0;
2848 }
2849
2850
2851 static int wpa_config_process_p2p_pref_chan(
2852         const struct global_parse_data *data,
2853         struct wpa_config *config, int line, const char *pos)
2854 {
2855         struct p2p_channel *pref = NULL, *n;
2856         unsigned int num = 0;
2857         const char *pos2;
2858         u8 op_class, chan;
2859
2860         /* format: class:chan,class:chan,... */
2861
2862         while (*pos) {
2863                 op_class = atoi(pos);
2864                 pos2 = os_strchr(pos, ':');
2865                 if (pos2 == NULL)
2866                         goto fail;
2867                 pos2++;
2868                 chan = atoi(pos2);
2869
2870                 n = os_realloc(pref, (num + 1) * sizeof(struct p2p_channel));
2871                 if (n == NULL)
2872                         goto fail;
2873                 pref = n;
2874                 pref[num].op_class = op_class;
2875                 pref[num].chan = chan;
2876                 num++;
2877
2878                 pos = os_strchr(pos2, ',');
2879                 if (pos == NULL)
2880                         break;
2881                 pos++;
2882         }
2883
2884         os_free(config->p2p_pref_chan);
2885         config->p2p_pref_chan = pref;
2886         config->num_p2p_pref_chan = num;
2887         wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
2888                     (u8 *) config->p2p_pref_chan,
2889                     config->num_p2p_pref_chan * sizeof(struct p2p_channel));
2890
2891         return 0;
2892
2893 fail:
2894         os_free(pref);
2895         wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
2896         return -1;
2897 }
2898 #endif /* CONFIG_P2P */
2899
2900
2901 static int wpa_config_process_hessid(
2902         const struct global_parse_data *data,
2903         struct wpa_config *config, int line, const char *pos)
2904 {
2905         if (hwaddr_aton2(pos, config->hessid) < 0) {
2906                 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
2907                            line, pos);
2908                 return -1;
2909         }
2910
2911         return 0;
2912 }
2913
2914
2915 #ifdef OFFSET
2916 #undef OFFSET
2917 #endif /* OFFSET */
2918 /* OFFSET: Get offset of a variable within the wpa_config structure */
2919 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
2920
2921 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
2922 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
2923 #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
2924 #define INT(f) _INT(f), NULL, NULL
2925 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
2926 #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
2927 #define STR(f) _STR(f), NULL, NULL
2928 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
2929 #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
2930
2931 static const struct global_parse_data global_fields[] = {
2932 #ifdef CONFIG_CTRL_IFACE
2933         { STR(ctrl_interface), 0 },
2934         { STR(ctrl_interface_group), 0 } /* deprecated */,
2935 #endif /* CONFIG_CTRL_IFACE */
2936         { INT_RANGE(eapol_version, 1, 2), 0 },
2937         { INT(ap_scan), 0 },
2938         { INT(disable_scan_offload), 0 },
2939         { INT(fast_reauth), 0 },
2940         { STR(opensc_engine_path), 0 },
2941         { STR(pkcs11_engine_path), 0 },
2942         { STR(pkcs11_module_path), 0 },
2943         { STR(pcsc_reader), 0 },
2944         { STR(pcsc_pin), 0 },
2945         { STR(driver_param), 0 },
2946         { INT(dot11RSNAConfigPMKLifetime), 0 },
2947         { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
2948         { INT(dot11RSNAConfigSATimeout), 0 },
2949 #ifndef CONFIG_NO_CONFIG_WRITE
2950         { INT(update_config), 0 },
2951 #endif /* CONFIG_NO_CONFIG_WRITE */
2952         { FUNC_NO_VAR(load_dynamic_eap), 0 },
2953 #ifdef CONFIG_WPS
2954         { FUNC(uuid), CFG_CHANGED_UUID },
2955         { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
2956         { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
2957         { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
2958         { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
2959         { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
2960         { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
2961         { FUNC(os_version), CFG_CHANGED_OS_VERSION },
2962         { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
2963         { INT_RANGE(wps_cred_processing, 0, 2), 0 },
2964         { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
2965 #endif /* CONFIG_WPS */
2966 #ifdef CONFIG_P2P
2967         { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
2968         { INT(p2p_listen_reg_class), 0 },
2969         { INT(p2p_listen_channel), 0 },
2970         { INT(p2p_oper_reg_class), 0 },
2971         { INT(p2p_oper_channel), 0 },
2972         { INT_RANGE(p2p_go_intent, 0, 15), 0 },
2973         { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
2974         { INT_RANGE(persistent_reconnect, 0, 1), 0 },
2975         { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
2976         { INT(p2p_group_idle), 0 },
2977         { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
2978 #endif /* CONFIG_P2P */
2979         { FUNC(country), CFG_CHANGED_COUNTRY },
2980         { INT(bss_max_count), 0 },
2981         { INT(bss_expiration_age), 0 },
2982         { INT(bss_expiration_scan_count), 0 },
2983         { INT_RANGE(filter_ssids, 0, 1), 0 },
2984         { INT_RANGE(filter_rssi, -100, 0), 0 },
2985         { INT(max_num_sta), 0 },
2986         { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
2987 #ifdef CONFIG_HS20
2988         { INT_RANGE(hs20, 0, 1), 0 },
2989 #endif /* CONFIG_HS20 */
2990         { INT_RANGE(interworking, 0, 1), 0 },
2991         { FUNC(hessid), 0 },
2992         { INT_RANGE(access_network_type, 0, 15), 0 },
2993         { INT_RANGE(pbc_in_m1, 0, 1), 0 },
2994         { STR(autoscan), 0 },
2995         { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff), 0 },
2996         { BIN(wps_nfc_dh_pubkey), 0 },
2997         { BIN(wps_nfc_dh_privkey), 0 },
2998         { BIN(wps_nfc_dev_pw), 0 },
2999         { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND }
3000 };
3001
3002 #undef FUNC
3003 #undef _INT
3004 #undef INT
3005 #undef INT_RANGE
3006 #undef _STR
3007 #undef STR
3008 #undef STR_RANGE
3009 #undef BIN
3010 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
3011
3012
3013 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3014 {
3015         size_t i;
3016         int ret = 0;
3017
3018         for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3019                 const struct global_parse_data *field = &global_fields[i];
3020                 size_t flen = os_strlen(field->name);
3021                 if (os_strncmp(pos, field->name, flen) != 0 ||
3022                     pos[flen] != '=')
3023                         continue;
3024
3025                 if (field->parser(field, config, line, pos + flen + 1)) {
3026                         wpa_printf(MSG_ERROR, "Line %d: failed to "
3027                                    "parse '%s'.", line, pos);
3028                         ret = -1;
3029                 }
3030                 config->changed_parameters |= field->changed_flag;
3031                 break;
3032         }
3033         if (i == NUM_GLOBAL_FIELDS) {
3034                 if (line < 0)
3035                         return -1;
3036                 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
3037                            line, pos);
3038                 ret = -1;
3039         }
3040
3041         return ret;
3042 }