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