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