Added preliminary Wi-Fi Protected Setup (WPS) implementation
[libeap.git] / wpa_supplicant / config_file.c
1 /*
2  * WPA Supplicant / Configuration backend: text file
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  * This file implements a configuration backend for text files. All the
15  * configuration information is stored in a text file that uses a format
16  * described in the sample configuration file, wpa_supplicant.conf.
17  */
18
19 #include "includes.h"
20
21 #include "common.h"
22 #include "config.h"
23 #include "base64.h"
24 #include "eap_peer/eap_methods.h"
25
26
27 /**
28  * wpa_config_get_line - Read the next configuration file line
29  * @s: Buffer for the line
30  * @size: The buffer length
31  * @stream: File stream to read from
32  * @line: Pointer to a variable storing the file line number
33  * @_pos: Buffer for the pointer to the beginning of data on the text line or
34  * %NULL if not needed (returned value used instead)
35  * Returns: Pointer to the beginning of data on the text line or %NULL if no
36  * more text lines are available.
37  *
38  * This function reads the next non-empty line from the configuration file and
39  * removes comments. The returned string is guaranteed to be null-terminated.
40  */
41 static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
42                                   char **_pos)
43 {
44         char *pos, *end, *sstart;
45
46         while (fgets(s, size, stream)) {
47                 (*line)++;
48                 s[size - 1] = '\0';
49                 pos = s;
50
51                 /* Skip white space from the beginning of line. */
52                 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
53                         pos++;
54
55                 /* Skip comment lines and empty lines */
56                 if (*pos == '#' || *pos == '\n' || *pos == '\0')
57                         continue;
58
59                 /*
60                  * Remove # comments unless they are within a double quoted
61                  * string.
62                  */
63                 sstart = os_strchr(pos, '"');
64                 if (sstart)
65                         sstart = os_strrchr(sstart + 1, '"');
66                 if (!sstart)
67                         sstart = pos;
68                 end = os_strchr(sstart, '#');
69                 if (end)
70                         *end-- = '\0';
71                 else
72                         end = pos + os_strlen(pos) - 1;
73
74                 /* Remove trailing white space. */
75                 while (end > pos &&
76                        (*end == '\n' || *end == ' ' || *end == '\t' ||
77                         *end == '\r'))
78                         *end-- = '\0';
79
80                 if (*pos == '\0')
81                         continue;
82
83                 if (_pos)
84                         *_pos = pos;
85                 return pos;
86         }
87
88         if (_pos)
89                 *_pos = NULL;
90         return NULL;
91 }
92
93
94 static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
95 {
96         int errors = 0;
97
98         if (ssid->passphrase) {
99                 if (ssid->psk_set) {
100                         wpa_printf(MSG_ERROR, "Line %d: both PSK and "
101                                    "passphrase configured.", line);
102                         errors++;
103                 }
104                 wpa_config_update_psk(ssid);
105         }
106
107         if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
108                                WPA_KEY_MGMT_PSK_SHA256)) &&
109             !ssid->psk_set) {
110                 wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
111                            "management, but no PSK configured.", line);
112                 errors++;
113         }
114
115         if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
116             !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
117             !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
118                 /* Group cipher cannot be stronger than the pairwise cipher. */
119                 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
120                            " list since it was not allowed for pairwise "
121                            "cipher", line);
122                 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
123         }
124
125         return errors;
126 }
127
128
129 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
130 {
131         struct wpa_ssid *ssid;
132         int errors = 0, end = 0;
133         char buf[256], *pos, *pos2;
134
135         wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
136                    *line);
137         ssid = os_zalloc(sizeof(*ssid));
138         if (ssid == NULL)
139                 return NULL;
140         ssid->id = id;
141
142         wpa_config_set_network_defaults(ssid);
143
144         while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
145                 if (os_strcmp(pos, "}") == 0) {
146                         end = 1;
147                         break;
148                 }
149
150                 pos2 = os_strchr(pos, '=');
151                 if (pos2 == NULL) {
152                         wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
153                                    "'%s'.", *line, pos);
154                         errors++;
155                         continue;
156                 }
157
158                 *pos2++ = '\0';
159                 if (*pos2 == '"') {
160                         if (os_strchr(pos2 + 1, '"') == NULL) {
161                                 wpa_printf(MSG_ERROR, "Line %d: invalid "
162                                            "quotation '%s'.", *line, pos2);
163                                 errors++;
164                                 continue;
165                         }
166                 }
167
168                 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
169                         errors++;
170         }
171
172         if (!end) {
173                 wpa_printf(MSG_ERROR, "Line %d: network block was not "
174                            "terminated properly.", *line);
175                 errors++;
176         }
177
178         errors += wpa_config_validate_network(ssid, *line);
179
180         if (errors) {
181                 wpa_config_free_ssid(ssid);
182                 ssid = NULL;
183         }
184
185         return ssid;
186 }
187
188
189 #ifndef CONFIG_NO_CONFIG_BLOBS
190 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
191                                                      const char *name)
192 {
193         struct wpa_config_blob *blob;
194         char buf[256], *pos;
195         unsigned char *encoded = NULL, *nencoded;
196         int end = 0;
197         size_t encoded_len = 0, len;
198
199         wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
200                    *line, name);
201
202         while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
203                 if (os_strcmp(pos, "}") == 0) {
204                         end = 1;
205                         break;
206                 }
207
208                 len = os_strlen(pos);
209                 nencoded = os_realloc(encoded, encoded_len + len);
210                 if (nencoded == NULL) {
211                         wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
212                                    "blob", *line);
213                         os_free(encoded);
214                         return NULL;
215                 }
216                 encoded = nencoded;
217                 os_memcpy(encoded + encoded_len, pos, len);
218                 encoded_len += len;
219         }
220
221         if (!end) {
222                 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
223                            "properly", *line);
224                 os_free(encoded);
225                 return NULL;
226         }
227
228         blob = os_zalloc(sizeof(*blob));
229         if (blob == NULL) {
230                 os_free(encoded);
231                 return NULL;
232         }
233         blob->name = os_strdup(name);
234         blob->data = base64_decode(encoded, encoded_len, &blob->len);
235         os_free(encoded);
236
237         if (blob->name == NULL || blob->data == NULL) {
238                 wpa_config_free_blob(blob);
239                 return NULL;
240         }
241
242         return blob;
243 }
244
245
246 static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
247                                    int *line, char *bname)
248 {
249         char *name_end;
250         struct wpa_config_blob *blob;
251
252         name_end = os_strchr(bname, '=');
253         if (name_end == NULL) {
254                 wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
255                            *line);
256                 return -1;
257         }
258         *name_end = '\0';
259
260         blob = wpa_config_read_blob(f, line, bname);
261         if (blob == NULL) {
262                 wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
263                            *line, bname);
264                 return -1;
265         }
266         wpa_config_set_blob(config, blob);
267         return 0;
268 }
269 #endif /* CONFIG_NO_CONFIG_BLOBS */
270
271
272 #ifdef CONFIG_CTRL_IFACE
273 static int wpa_config_process_ctrl_interface(struct wpa_config *config,
274                                              char *pos)
275 {
276         os_free(config->ctrl_interface);
277         config->ctrl_interface = os_strdup(pos);
278         wpa_printf(MSG_DEBUG, "ctrl_interface='%s'", config->ctrl_interface);
279         return 0;
280 }
281
282
283 static int wpa_config_process_ctrl_interface_group(struct wpa_config *config,
284                                                    char *pos)
285 {
286         os_free(config->ctrl_interface_group);
287         config->ctrl_interface_group = os_strdup(pos);
288         wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' (DEPRECATED)",
289                    config->ctrl_interface_group);
290         return 0;
291 }
292 #endif /* CONFIG_CTRL_IFACE */
293
294
295 static int wpa_config_process_eapol_version(struct wpa_config *config,
296                                             int line, char *pos)
297 {
298         config->eapol_version = atoi(pos);
299         if (config->eapol_version < 1 || config->eapol_version > 2) {
300                 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): "
301                            "'%s'.", line, config->eapol_version, pos);
302                 return -1;
303         }
304         wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version);
305         return 0;
306 }
307
308
309 static int wpa_config_process_ap_scan(struct wpa_config *config, char *pos)
310 {
311         config->ap_scan = atoi(pos);
312         wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
313         return 0;
314 }
315
316
317 static int wpa_config_process_fast_reauth(struct wpa_config *config, char *pos)
318 {
319         config->fast_reauth = atoi(pos);
320         wpa_printf(MSG_DEBUG, "fast_reauth=%d", config->fast_reauth);
321         return 0;
322 }
323
324
325 #ifdef EAP_TLS_OPENSSL
326
327 static int wpa_config_process_opensc_engine_path(struct wpa_config *config,
328                                                  char *pos)
329 {
330         os_free(config->opensc_engine_path);
331         config->opensc_engine_path = os_strdup(pos);
332         wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
333                    config->opensc_engine_path);
334         return 0;
335 }
336
337
338 static int wpa_config_process_pkcs11_engine_path(struct wpa_config *config,
339                                                  char *pos)
340 {
341         os_free(config->pkcs11_engine_path);
342         config->pkcs11_engine_path = os_strdup(pos);
343         wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
344                    config->pkcs11_engine_path);
345         return 0;
346 }
347
348
349 static int wpa_config_process_pkcs11_module_path(struct wpa_config *config,
350                                                  char *pos)
351 {
352         os_free(config->pkcs11_module_path);
353         config->pkcs11_module_path = os_strdup(pos);
354         wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
355                    config->pkcs11_module_path);
356         return 0;
357 }
358
359 #endif /* EAP_TLS_OPENSSL */
360
361
362 static int wpa_config_process_driver_param(struct wpa_config *config,
363                                            char *pos)
364 {
365         os_free(config->driver_param);
366         config->driver_param = os_strdup(pos);
367         wpa_printf(MSG_DEBUG, "driver_param='%s'", config->driver_param);
368         return 0;
369 }
370
371
372 static int wpa_config_process_pmk_lifetime(struct wpa_config *config,
373                                            char *pos)
374 {
375         config->dot11RSNAConfigPMKLifetime = atoi(pos);
376         wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
377                    config->dot11RSNAConfigPMKLifetime);
378         return 0;
379 }
380
381
382 static int wpa_config_process_pmk_reauth_threshold(struct wpa_config *config,
383                                                    char *pos)
384 {
385         config->dot11RSNAConfigPMKReauthThreshold = atoi(pos);
386         wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKReauthThreshold=%d",
387                    config->dot11RSNAConfigPMKReauthThreshold);
388         return 0;
389 }
390
391
392 static int wpa_config_process_sa_timeout(struct wpa_config *config, char *pos)
393 {
394         config->dot11RSNAConfigSATimeout = atoi(pos);
395         wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
396                    config->dot11RSNAConfigSATimeout);
397         return 0;
398 }
399
400
401 #ifndef CONFIG_NO_CONFIG_WRITE
402 static int wpa_config_process_update_config(struct wpa_config *config,
403                                             char *pos)
404 {
405         config->update_config = atoi(pos);
406         wpa_printf(MSG_DEBUG, "update_config=%d", config->update_config);
407         return 0;
408 }
409 #endif /* CONFIG_NO_CONFIG_WRITE */
410
411
412 static int wpa_config_process_load_dynamic_eap(int line, char *so)
413 {
414         int ret;
415         wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
416         ret = eap_peer_method_load(so);
417         if (ret == -2) {
418                 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
419                            "reloading.");
420         } else if (ret) {
421                 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
422                            "method '%s'.", line, so);
423                 return -1;
424         }
425
426         return 0;
427 }
428
429
430 static int wpa_config_process_global(struct wpa_config *config, char *pos,
431                                      int line)
432 {
433 #ifdef CONFIG_CTRL_IFACE
434         if (os_strncmp(pos, "ctrl_interface=", 15) == 0)
435                 return wpa_config_process_ctrl_interface(config, pos + 15);
436
437         if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0)
438                 return wpa_config_process_ctrl_interface_group(config,
439                                                                pos + 21);
440 #endif /* CONFIG_CTRL_IFACE */
441
442         if (os_strncmp(pos, "eapol_version=", 14) == 0)
443                 return wpa_config_process_eapol_version(config, line,
444                                                         pos + 14);
445
446         if (os_strncmp(pos, "ap_scan=", 8) == 0)
447                 return wpa_config_process_ap_scan(config, pos + 8);
448
449         if (os_strncmp(pos, "fast_reauth=", 12) == 0)
450                 return wpa_config_process_fast_reauth(config, pos + 12);
451
452 #ifdef EAP_TLS_OPENSSL
453         if (os_strncmp(pos, "opensc_engine_path=", 19) == 0)
454                 return wpa_config_process_opensc_engine_path(config, pos + 19);
455
456         if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0)
457                 return wpa_config_process_pkcs11_engine_path(config, pos + 19);
458
459         if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0)
460                 return wpa_config_process_pkcs11_module_path(config, pos + 19);
461 #endif /* EAP_TLS_OPENSSL */
462
463         if (os_strncmp(pos, "driver_param=", 13) == 0)
464                 return wpa_config_process_driver_param(config, pos + 13);
465
466         if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) == 0)
467                 return wpa_config_process_pmk_lifetime(config, pos + 27);
468
469         if (os_strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=", 34) == 0)
470                 return wpa_config_process_pmk_reauth_threshold(config,
471                                                                pos + 34);
472
473         if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) == 0)
474                 return wpa_config_process_sa_timeout(config, pos + 25);
475
476 #ifndef CONFIG_NO_CONFIG_WRITE
477         if (os_strncmp(pos, "update_config=", 14) == 0)
478                 return wpa_config_process_update_config(config, pos + 14);
479 #endif /* CONFIG_NO_CONFIG_WRITE */
480
481         if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0)
482                 return wpa_config_process_load_dynamic_eap(line, pos + 17);
483
484         return -1;
485 }
486
487
488 struct wpa_config * wpa_config_read(const char *name)
489 {
490         FILE *f;
491         char buf[256], *pos;
492         int errors = 0, line = 0;
493         struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
494         struct wpa_config *config;
495         int id = 0;
496
497         config = wpa_config_alloc_empty(NULL, NULL);
498         if (config == NULL)
499                 return NULL;
500         wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
501         f = fopen(name, "r");
502         if (f == NULL) {
503                 os_free(config);
504                 return NULL;
505         }
506
507         while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
508                 if (os_strcmp(pos, "network={") == 0) {
509                         ssid = wpa_config_read_network(f, &line, id++);
510                         if (ssid == NULL) {
511                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
512                                            "parse network block.", line);
513                                 errors++;
514                                 continue;
515                         }
516                         if (head == NULL) {
517                                 head = tail = ssid;
518                         } else {
519                                 tail->next = ssid;
520                                 tail = ssid;
521                         }
522                         if (wpa_config_add_prio_network(config, ssid)) {
523                                 wpa_printf(MSG_ERROR, "Line %d: failed to add "
524                                            "network block to priority list.",
525                                            line);
526                                 errors++;
527                                 continue;
528                         }
529 #ifndef CONFIG_NO_CONFIG_BLOBS
530                 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
531                         if (wpa_config_process_blob(config, f, &line, pos + 12)
532                             < 0) {
533                                 errors++;
534                                 continue;
535                         }
536 #endif /* CONFIG_NO_CONFIG_BLOBS */
537                 } else if (wpa_config_process_global(config, pos, line) < 0) {
538                         wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
539                                    "line '%s'.", line, pos);
540                         errors++;
541                         continue;
542                 }
543         }
544
545         fclose(f);
546
547         config->ssid = head;
548         wpa_config_debug_dump_networks(config);
549
550         if (errors) {
551                 wpa_config_free(config);
552                 config = NULL;
553                 head = NULL;
554         }
555
556         return config;
557 }
558
559
560 #ifndef CONFIG_NO_CONFIG_WRITE
561
562 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
563 {
564         char *value = wpa_config_get(ssid, field);
565         if (value == NULL)
566                 return;
567         fprintf(f, "\t%s=%s\n", field, value);
568         os_free(value);
569 }
570
571
572 static void write_int(FILE *f, const char *field, int value, int def)
573 {
574         if (value == def)
575                 return;
576         fprintf(f, "\t%s=%d\n", field, value);
577 }
578
579
580 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
581 {
582         char *value = wpa_config_get(ssid, "bssid");
583         if (value == NULL)
584                 return;
585         fprintf(f, "\tbssid=%s\n", value);
586         os_free(value);
587 }
588
589
590 static void write_psk(FILE *f, struct wpa_ssid *ssid)
591 {
592         char *value = wpa_config_get(ssid, "psk");
593         if (value == NULL)
594                 return;
595         fprintf(f, "\tpsk=%s\n", value);
596         os_free(value);
597 }
598
599
600 static void write_proto(FILE *f, struct wpa_ssid *ssid)
601 {
602         char *value;
603
604         if (ssid->proto == DEFAULT_PROTO)
605                 return;
606
607         value = wpa_config_get(ssid, "proto");
608         if (value == NULL)
609                 return;
610         if (value[0])
611                 fprintf(f, "\tproto=%s\n", value);
612         os_free(value);
613 }
614
615
616 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
617 {
618         char *value;
619
620         if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
621                 return;
622
623         value = wpa_config_get(ssid, "key_mgmt");
624         if (value == NULL)
625                 return;
626         if (value[0])
627                 fprintf(f, "\tkey_mgmt=%s\n", value);
628         os_free(value);
629 }
630
631
632 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
633 {
634         char *value;
635
636         if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
637                 return;
638
639         value = wpa_config_get(ssid, "pairwise");
640         if (value == NULL)
641                 return;
642         if (value[0])
643                 fprintf(f, "\tpairwise=%s\n", value);
644         os_free(value);
645 }
646
647
648 static void write_group(FILE *f, struct wpa_ssid *ssid)
649 {
650         char *value;
651
652         if (ssid->group_cipher == DEFAULT_GROUP)
653                 return;
654
655         value = wpa_config_get(ssid, "group");
656         if (value == NULL)
657                 return;
658         if (value[0])
659                 fprintf(f, "\tgroup=%s\n", value);
660         os_free(value);
661 }
662
663
664 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
665 {
666         char *value;
667
668         if (ssid->auth_alg == 0)
669                 return;
670
671         value = wpa_config_get(ssid, "auth_alg");
672         if (value == NULL)
673                 return;
674         if (value[0])
675                 fprintf(f, "\tauth_alg=%s\n", value);
676         os_free(value);
677 }
678
679
680 #ifdef IEEE8021X_EAPOL
681 static void write_eap(FILE *f, struct wpa_ssid *ssid)
682 {
683         char *value;
684
685         value = wpa_config_get(ssid, "eap");
686         if (value == NULL)
687                 return;
688
689         if (value[0])
690                 fprintf(f, "\teap=%s\n", value);
691         os_free(value);
692 }
693 #endif /* IEEE8021X_EAPOL */
694
695
696 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
697 {
698         char field[20], *value;
699         int res;
700
701         res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
702         if (res < 0 || (size_t) res >= sizeof(field))
703                 return;
704         value = wpa_config_get(ssid, field);
705         if (value) {
706                 fprintf(f, "\t%s=%s\n", field, value);
707                 os_free(value);
708         }
709 }
710
711
712 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
713 {
714         int i;
715
716 #define STR(t) write_str(f, #t, ssid)
717 #define INT(t) write_int(f, #t, ssid->t, 0)
718 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
719 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
720 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
721
722         STR(ssid);
723         INT(scan_ssid);
724         write_bssid(f, ssid);
725         write_psk(f, ssid);
726         write_proto(f, ssid);
727         write_key_mgmt(f, ssid);
728         write_pairwise(f, ssid);
729         write_group(f, ssid);
730         write_auth_alg(f, ssid);
731 #ifdef IEEE8021X_EAPOL
732         write_eap(f, ssid);
733         STR(identity);
734         STR(anonymous_identity);
735         STR(password);
736         STR(ca_cert);
737         STR(ca_path);
738         STR(client_cert);
739         STR(private_key);
740         STR(private_key_passwd);
741         STR(dh_file);
742         STR(subject_match);
743         STR(altsubject_match);
744         STR(ca_cert2);
745         STR(ca_path2);
746         STR(client_cert2);
747         STR(private_key2);
748         STR(private_key2_passwd);
749         STR(dh_file2);
750         STR(subject_match2);
751         STR(altsubject_match2);
752         STR(phase1);
753         STR(phase2);
754         STR(pcsc);
755         STR(pin);
756         STR(engine_id);
757         STR(key_id);
758         STR(cert_id);
759         STR(ca_cert_id);
760         STR(key2_id);
761         STR(pin2);
762         STR(engine2_id);
763         STR(cert2_id);
764         STR(ca_cert2_id);
765         INTe(engine);
766         INTe(engine2);
767         INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
768 #endif /* IEEE8021X_EAPOL */
769         for (i = 0; i < 4; i++)
770                 write_wep_key(f, i, ssid);
771         INT(wep_tx_keyidx);
772         INT(priority);
773 #ifdef IEEE8021X_EAPOL
774         INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
775         STR(pac_file);
776         INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
777 #endif /* IEEE8021X_EAPOL */
778         INT(mode);
779         INT(proactive_key_caching);
780         INT(disabled);
781         INT(peerkey);
782 #ifdef CONFIG_IEEE80211W
783         INT(ieee80211w);
784 #endif /* CONFIG_IEEE80211W */
785         STR(id_str);
786
787 #undef STR
788 #undef INT
789 #undef INT_DEF
790 }
791
792
793 #ifndef CONFIG_NO_CONFIG_BLOBS
794 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
795 {
796         unsigned char *encoded;
797
798         encoded = base64_encode(blob->data, blob->len, NULL);
799         if (encoded == NULL)
800                 return -1;
801
802         fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
803         os_free(encoded);
804         return 0;
805 }
806 #endif /* CONFIG_NO_CONFIG_BLOBS */
807
808
809 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
810 {
811 #ifdef CONFIG_CTRL_IFACE
812         if (config->ctrl_interface)
813                 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
814         if (config->ctrl_interface_group)
815                 fprintf(f, "ctrl_interface_group=%s\n",
816                         config->ctrl_interface_group);
817 #endif /* CONFIG_CTRL_IFACE */
818         if (config->eapol_version != DEFAULT_EAPOL_VERSION)
819                 fprintf(f, "eapol_version=%d\n", config->eapol_version);
820         if (config->ap_scan != DEFAULT_AP_SCAN)
821                 fprintf(f, "ap_scan=%d\n", config->ap_scan);
822         if (config->fast_reauth != DEFAULT_FAST_REAUTH)
823                 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
824 #ifdef EAP_TLS_OPENSSL
825         if (config->opensc_engine_path)
826                 fprintf(f, "opensc_engine_path=%s\n",
827                         config->opensc_engine_path);
828         if (config->pkcs11_engine_path)
829                 fprintf(f, "pkcs11_engine_path=%s\n",
830                         config->pkcs11_engine_path);
831         if (config->pkcs11_module_path)
832                 fprintf(f, "pkcs11_module_path=%s\n",
833                         config->pkcs11_module_path);
834 #endif /* EAP_TLS_OPENSSL */
835         if (config->driver_param)
836                 fprintf(f, "driver_param=%s\n", config->driver_param);
837         if (config->dot11RSNAConfigPMKLifetime)
838                 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
839                         config->dot11RSNAConfigPMKLifetime);
840         if (config->dot11RSNAConfigPMKReauthThreshold)
841                 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
842                         config->dot11RSNAConfigPMKReauthThreshold);
843         if (config->dot11RSNAConfigSATimeout)
844                 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
845                         config->dot11RSNAConfigSATimeout);
846         if (config->update_config)
847                 fprintf(f, "update_config=%d\n", config->update_config);
848 }
849
850 #endif /* CONFIG_NO_CONFIG_WRITE */
851
852
853 int wpa_config_write(const char *name, struct wpa_config *config)
854 {
855 #ifndef CONFIG_NO_CONFIG_WRITE
856         FILE *f;
857         struct wpa_ssid *ssid;
858 #ifndef CONFIG_NO_CONFIG_BLOBS
859         struct wpa_config_blob *blob;
860 #endif /* CONFIG_NO_CONFIG_BLOBS */
861         int ret = 0;
862
863         wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
864
865         f = fopen(name, "w");
866         if (f == NULL) {
867                 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
868                 return -1;
869         }
870
871         wpa_config_write_global(f, config);
872
873         for (ssid = config->ssid; ssid; ssid = ssid->next) {
874                 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
875                         continue; /* do not save temporary WPS networks */
876                 fprintf(f, "\nnetwork={\n");
877                 wpa_config_write_network(f, ssid);
878                 fprintf(f, "}\n");
879         }
880
881 #ifndef CONFIG_NO_CONFIG_BLOBS
882         for (blob = config->blobs; blob; blob = blob->next) {
883                 ret = wpa_config_write_blob(f, blob);
884                 if (ret)
885                         break;
886         }
887 #endif /* CONFIG_NO_CONFIG_BLOBS */
888
889         fclose(f);
890
891         wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
892                    name, ret ? "un" : "");
893         return ret;
894 #else /* CONFIG_NO_CONFIG_WRITE */
895         return -1;
896 #endif /* CONFIG_NO_CONFIG_WRITE */
897 }