wpa_supplicant: Add support for setting of a regulatory domain
[mech_eap.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 static int wpa_config_process_country(struct wpa_config *config, char *pos)
274 {
275         if (!pos[0] || !pos[1]) {
276                 wpa_printf(MSG_DEBUG, "Invalid country set");
277                 return -1;
278         }
279         config->alpha2[0] = pos[0];
280         config->alpha2[1] = pos[1];
281         wpa_printf(MSG_DEBUG, "country='%c%c'",
282                    config->alpha2[0], config->alpha2[1]);
283         return 0;
284 }
285
286
287 #ifdef CONFIG_CTRL_IFACE
288 static int wpa_config_process_ctrl_interface(struct wpa_config *config,
289                                              char *pos)
290 {
291         os_free(config->ctrl_interface);
292         config->ctrl_interface = os_strdup(pos);
293         wpa_printf(MSG_DEBUG, "ctrl_interface='%s'", config->ctrl_interface);
294         return 0;
295 }
296
297
298 static int wpa_config_process_ctrl_interface_group(struct wpa_config *config,
299                                                    char *pos)
300 {
301         os_free(config->ctrl_interface_group);
302         config->ctrl_interface_group = os_strdup(pos);
303         wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' (DEPRECATED)",
304                    config->ctrl_interface_group);
305         return 0;
306 }
307 #endif /* CONFIG_CTRL_IFACE */
308
309
310 static int wpa_config_process_eapol_version(struct wpa_config *config,
311                                             int line, char *pos)
312 {
313         config->eapol_version = atoi(pos);
314         if (config->eapol_version < 1 || config->eapol_version > 2) {
315                 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): "
316                            "'%s'.", line, config->eapol_version, pos);
317                 return -1;
318         }
319         wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version);
320         return 0;
321 }
322
323
324 static int wpa_config_process_ap_scan(struct wpa_config *config, char *pos)
325 {
326         config->ap_scan = atoi(pos);
327         wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
328         return 0;
329 }
330
331
332 static int wpa_config_process_fast_reauth(struct wpa_config *config, char *pos)
333 {
334         config->fast_reauth = atoi(pos);
335         wpa_printf(MSG_DEBUG, "fast_reauth=%d", config->fast_reauth);
336         return 0;
337 }
338
339
340 #ifdef EAP_TLS_OPENSSL
341
342 static int wpa_config_process_opensc_engine_path(struct wpa_config *config,
343                                                  char *pos)
344 {
345         os_free(config->opensc_engine_path);
346         config->opensc_engine_path = os_strdup(pos);
347         wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
348                    config->opensc_engine_path);
349         return 0;
350 }
351
352
353 static int wpa_config_process_pkcs11_engine_path(struct wpa_config *config,
354                                                  char *pos)
355 {
356         os_free(config->pkcs11_engine_path);
357         config->pkcs11_engine_path = os_strdup(pos);
358         wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
359                    config->pkcs11_engine_path);
360         return 0;
361 }
362
363
364 static int wpa_config_process_pkcs11_module_path(struct wpa_config *config,
365                                                  char *pos)
366 {
367         os_free(config->pkcs11_module_path);
368         config->pkcs11_module_path = os_strdup(pos);
369         wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
370                    config->pkcs11_module_path);
371         return 0;
372 }
373
374 #endif /* EAP_TLS_OPENSSL */
375
376
377 static int wpa_config_process_driver_param(struct wpa_config *config,
378                                            char *pos)
379 {
380         os_free(config->driver_param);
381         config->driver_param = os_strdup(pos);
382         wpa_printf(MSG_DEBUG, "driver_param='%s'", config->driver_param);
383         return 0;
384 }
385
386
387 static int wpa_config_process_pmk_lifetime(struct wpa_config *config,
388                                            char *pos)
389 {
390         config->dot11RSNAConfigPMKLifetime = atoi(pos);
391         wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
392                    config->dot11RSNAConfigPMKLifetime);
393         return 0;
394 }
395
396
397 static int wpa_config_process_pmk_reauth_threshold(struct wpa_config *config,
398                                                    char *pos)
399 {
400         config->dot11RSNAConfigPMKReauthThreshold = atoi(pos);
401         wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKReauthThreshold=%d",
402                    config->dot11RSNAConfigPMKReauthThreshold);
403         return 0;
404 }
405
406
407 static int wpa_config_process_sa_timeout(struct wpa_config *config, char *pos)
408 {
409         config->dot11RSNAConfigSATimeout = atoi(pos);
410         wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
411                    config->dot11RSNAConfigSATimeout);
412         return 0;
413 }
414
415
416 #ifndef CONFIG_NO_CONFIG_WRITE
417 static int wpa_config_process_update_config(struct wpa_config *config,
418                                             char *pos)
419 {
420         config->update_config = atoi(pos);
421         wpa_printf(MSG_DEBUG, "update_config=%d", config->update_config);
422         return 0;
423 }
424 #endif /* CONFIG_NO_CONFIG_WRITE */
425
426
427 static int wpa_config_process_load_dynamic_eap(int line, char *so)
428 {
429         int ret;
430         wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
431         ret = eap_peer_method_load(so);
432         if (ret == -2) {
433                 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
434                            "reloading.");
435         } else if (ret) {
436                 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
437                            "method '%s'.", line, so);
438                 return -1;
439         }
440
441         return 0;
442 }
443
444
445 #ifdef CONFIG_WPS
446 static int wpa_config_process_uuid(struct wpa_config *config, int line,
447                                    char *pos)
448 {
449         char buf[40];
450         if (uuid_str2bin(pos, config->uuid)) {
451                 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
452                 return -1;
453         }
454         uuid_bin2str(config->uuid, buf, sizeof(buf));
455         wpa_printf(MSG_DEBUG, "uuid=%s", buf);
456         return 0;
457 }
458 #endif /* CONFIG_WPS */
459
460
461 static int wpa_config_process_global(struct wpa_config *config, char *pos,
462                                      int line)
463 {
464 #ifdef CONFIG_CTRL_IFACE
465         if (os_strncmp(pos, "ctrl_interface=", 15) == 0)
466                 return wpa_config_process_ctrl_interface(config, pos + 15);
467
468         if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0)
469                 return wpa_config_process_ctrl_interface_group(config,
470                                                                pos + 21);
471 #endif /* CONFIG_CTRL_IFACE */
472
473         if (os_strncmp(pos, "eapol_version=", 14) == 0)
474                 return wpa_config_process_eapol_version(config, line,
475                                                         pos + 14);
476
477         if (os_strncmp(pos, "ap_scan=", 8) == 0)
478                 return wpa_config_process_ap_scan(config, pos + 8);
479
480         if (os_strncmp(pos, "fast_reauth=", 12) == 0)
481                 return wpa_config_process_fast_reauth(config, pos + 12);
482
483 #ifdef EAP_TLS_OPENSSL
484         if (os_strncmp(pos, "opensc_engine_path=", 19) == 0)
485                 return wpa_config_process_opensc_engine_path(config, pos + 19);
486
487         if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0)
488                 return wpa_config_process_pkcs11_engine_path(config, pos + 19);
489
490         if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0)
491                 return wpa_config_process_pkcs11_module_path(config, pos + 19);
492 #endif /* EAP_TLS_OPENSSL */
493
494         if (os_strncmp(pos, "driver_param=", 13) == 0)
495                 return wpa_config_process_driver_param(config, pos + 13);
496
497         if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) == 0)
498                 return wpa_config_process_pmk_lifetime(config, pos + 27);
499
500         if (os_strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=", 34) == 0)
501                 return wpa_config_process_pmk_reauth_threshold(config,
502                                                                pos + 34);
503
504         if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) == 0)
505                 return wpa_config_process_sa_timeout(config, pos + 25);
506
507 #ifndef CONFIG_NO_CONFIG_WRITE
508         if (os_strncmp(pos, "update_config=", 14) == 0)
509                 return wpa_config_process_update_config(config, pos + 14);
510 #endif /* CONFIG_NO_CONFIG_WRITE */
511
512         if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0)
513                 return wpa_config_process_load_dynamic_eap(line, pos + 17);
514
515 #ifdef CONFIG_WPS
516         if (os_strncmp(pos, "uuid=", 5) == 0)
517                 return wpa_config_process_uuid(config, line, pos + 5);
518 #endif /* CONFIG_WPS */
519
520         if (os_strncmp(pos, "country=", 8) == 0)
521                 return wpa_config_process_country(config, pos + 8);
522
523         return -1;
524 }
525
526
527 struct wpa_config * wpa_config_read(const char *name)
528 {
529         FILE *f;
530         char buf[256], *pos;
531         int errors = 0, line = 0;
532         struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
533         struct wpa_config *config;
534         int id = 0;
535
536         config = wpa_config_alloc_empty(NULL, NULL);
537         if (config == NULL)
538                 return NULL;
539         wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
540         f = fopen(name, "r");
541         if (f == NULL) {
542                 os_free(config);
543                 return NULL;
544         }
545
546         while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
547                 if (os_strcmp(pos, "network={") == 0) {
548                         ssid = wpa_config_read_network(f, &line, id++);
549                         if (ssid == NULL) {
550                                 wpa_printf(MSG_ERROR, "Line %d: failed to "
551                                            "parse network block.", line);
552                                 errors++;
553                                 continue;
554                         }
555                         if (head == NULL) {
556                                 head = tail = ssid;
557                         } else {
558                                 tail->next = ssid;
559                                 tail = ssid;
560                         }
561                         if (wpa_config_add_prio_network(config, ssid)) {
562                                 wpa_printf(MSG_ERROR, "Line %d: failed to add "
563                                            "network block to priority list.",
564                                            line);
565                                 errors++;
566                                 continue;
567                         }
568 #ifndef CONFIG_NO_CONFIG_BLOBS
569                 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
570                         if (wpa_config_process_blob(config, f, &line, pos + 12)
571                             < 0) {
572                                 errors++;
573                                 continue;
574                         }
575 #endif /* CONFIG_NO_CONFIG_BLOBS */
576                 } else if (wpa_config_process_global(config, pos, line) < 0) {
577                         wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
578                                    "line '%s'.", line, pos);
579                         errors++;
580                         continue;
581                 }
582         }
583
584         fclose(f);
585
586         config->ssid = head;
587         wpa_config_debug_dump_networks(config);
588
589         if (errors) {
590                 wpa_config_free(config);
591                 config = NULL;
592                 head = NULL;
593         }
594
595         return config;
596 }
597
598
599 #ifndef CONFIG_NO_CONFIG_WRITE
600
601 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
602 {
603         char *value = wpa_config_get(ssid, field);
604         if (value == NULL)
605                 return;
606         fprintf(f, "\t%s=%s\n", field, value);
607         os_free(value);
608 }
609
610
611 static void write_int(FILE *f, const char *field, int value, int def)
612 {
613         if (value == def)
614                 return;
615         fprintf(f, "\t%s=%d\n", field, value);
616 }
617
618
619 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
620 {
621         char *value = wpa_config_get(ssid, "bssid");
622         if (value == NULL)
623                 return;
624         fprintf(f, "\tbssid=%s\n", value);
625         os_free(value);
626 }
627
628
629 static void write_psk(FILE *f, struct wpa_ssid *ssid)
630 {
631         char *value = wpa_config_get(ssid, "psk");
632         if (value == NULL)
633                 return;
634         fprintf(f, "\tpsk=%s\n", value);
635         os_free(value);
636 }
637
638
639 static void write_proto(FILE *f, struct wpa_ssid *ssid)
640 {
641         char *value;
642
643         if (ssid->proto == DEFAULT_PROTO)
644                 return;
645
646         value = wpa_config_get(ssid, "proto");
647         if (value == NULL)
648                 return;
649         if (value[0])
650                 fprintf(f, "\tproto=%s\n", value);
651         os_free(value);
652 }
653
654
655 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
656 {
657         char *value;
658
659         if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
660                 return;
661
662         value = wpa_config_get(ssid, "key_mgmt");
663         if (value == NULL)
664                 return;
665         if (value[0])
666                 fprintf(f, "\tkey_mgmt=%s\n", value);
667         os_free(value);
668 }
669
670
671 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
672 {
673         char *value;
674
675         if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
676                 return;
677
678         value = wpa_config_get(ssid, "pairwise");
679         if (value == NULL)
680                 return;
681         if (value[0])
682                 fprintf(f, "\tpairwise=%s\n", value);
683         os_free(value);
684 }
685
686
687 static void write_group(FILE *f, struct wpa_ssid *ssid)
688 {
689         char *value;
690
691         if (ssid->group_cipher == DEFAULT_GROUP)
692                 return;
693
694         value = wpa_config_get(ssid, "group");
695         if (value == NULL)
696                 return;
697         if (value[0])
698                 fprintf(f, "\tgroup=%s\n", value);
699         os_free(value);
700 }
701
702
703 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
704 {
705         char *value;
706
707         if (ssid->auth_alg == 0)
708                 return;
709
710         value = wpa_config_get(ssid, "auth_alg");
711         if (value == NULL)
712                 return;
713         if (value[0])
714                 fprintf(f, "\tauth_alg=%s\n", value);
715         os_free(value);
716 }
717
718
719 #ifdef IEEE8021X_EAPOL
720 static void write_eap(FILE *f, struct wpa_ssid *ssid)
721 {
722         char *value;
723
724         value = wpa_config_get(ssid, "eap");
725         if (value == NULL)
726                 return;
727
728         if (value[0])
729                 fprintf(f, "\teap=%s\n", value);
730         os_free(value);
731 }
732 #endif /* IEEE8021X_EAPOL */
733
734
735 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
736 {
737         char field[20], *value;
738         int res;
739
740         res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
741         if (res < 0 || (size_t) res >= sizeof(field))
742                 return;
743         value = wpa_config_get(ssid, field);
744         if (value) {
745                 fprintf(f, "\t%s=%s\n", field, value);
746                 os_free(value);
747         }
748 }
749
750
751 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
752 {
753         int i;
754
755 #define STR(t) write_str(f, #t, ssid)
756 #define INT(t) write_int(f, #t, ssid->t, 0)
757 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
758 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
759 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
760
761         STR(ssid);
762         INT(scan_ssid);
763         write_bssid(f, ssid);
764         write_psk(f, ssid);
765         write_proto(f, ssid);
766         write_key_mgmt(f, ssid);
767         write_pairwise(f, ssid);
768         write_group(f, ssid);
769         write_auth_alg(f, ssid);
770 #ifdef IEEE8021X_EAPOL
771         write_eap(f, ssid);
772         STR(identity);
773         STR(anonymous_identity);
774         STR(password);
775         STR(ca_cert);
776         STR(ca_path);
777         STR(client_cert);
778         STR(private_key);
779         STR(private_key_passwd);
780         STR(dh_file);
781         STR(subject_match);
782         STR(altsubject_match);
783         STR(ca_cert2);
784         STR(ca_path2);
785         STR(client_cert2);
786         STR(private_key2);
787         STR(private_key2_passwd);
788         STR(dh_file2);
789         STR(subject_match2);
790         STR(altsubject_match2);
791         STR(phase1);
792         STR(phase2);
793         STR(pcsc);
794         STR(pin);
795         STR(engine_id);
796         STR(key_id);
797         STR(cert_id);
798         STR(ca_cert_id);
799         STR(key2_id);
800         STR(pin2);
801         STR(engine2_id);
802         STR(cert2_id);
803         STR(ca_cert2_id);
804         INTe(engine);
805         INTe(engine2);
806         INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
807 #endif /* IEEE8021X_EAPOL */
808         for (i = 0; i < 4; i++)
809                 write_wep_key(f, i, ssid);
810         INT(wep_tx_keyidx);
811         INT(priority);
812 #ifdef IEEE8021X_EAPOL
813         INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
814         STR(pac_file);
815         INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
816 #endif /* IEEE8021X_EAPOL */
817         INT(mode);
818         INT(proactive_key_caching);
819         INT(disabled);
820         INT(peerkey);
821 #ifdef CONFIG_IEEE80211W
822         INT(ieee80211w);
823 #endif /* CONFIG_IEEE80211W */
824         STR(id_str);
825
826 #undef STR
827 #undef INT
828 #undef INT_DEF
829 }
830
831
832 #ifndef CONFIG_NO_CONFIG_BLOBS
833 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
834 {
835         unsigned char *encoded;
836
837         encoded = base64_encode(blob->data, blob->len, NULL);
838         if (encoded == NULL)
839                 return -1;
840
841         fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
842         os_free(encoded);
843         return 0;
844 }
845 #endif /* CONFIG_NO_CONFIG_BLOBS */
846
847
848 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
849 {
850 #ifdef CONFIG_CTRL_IFACE
851         if (config->ctrl_interface)
852                 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
853         if (config->ctrl_interface_group)
854                 fprintf(f, "ctrl_interface_group=%s\n",
855                         config->ctrl_interface_group);
856 #endif /* CONFIG_CTRL_IFACE */
857         if (config->eapol_version != DEFAULT_EAPOL_VERSION)
858                 fprintf(f, "eapol_version=%d\n", config->eapol_version);
859         if (config->ap_scan != DEFAULT_AP_SCAN)
860                 fprintf(f, "ap_scan=%d\n", config->ap_scan);
861         if (config->fast_reauth != DEFAULT_FAST_REAUTH)
862                 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
863 #ifdef EAP_TLS_OPENSSL
864         if (config->opensc_engine_path)
865                 fprintf(f, "opensc_engine_path=%s\n",
866                         config->opensc_engine_path);
867         if (config->pkcs11_engine_path)
868                 fprintf(f, "pkcs11_engine_path=%s\n",
869                         config->pkcs11_engine_path);
870         if (config->pkcs11_module_path)
871                 fprintf(f, "pkcs11_module_path=%s\n",
872                         config->pkcs11_module_path);
873 #endif /* EAP_TLS_OPENSSL */
874         if (config->driver_param)
875                 fprintf(f, "driver_param=%s\n", config->driver_param);
876         if (config->dot11RSNAConfigPMKLifetime)
877                 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
878                         config->dot11RSNAConfigPMKLifetime);
879         if (config->dot11RSNAConfigPMKReauthThreshold)
880                 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
881                         config->dot11RSNAConfigPMKReauthThreshold);
882         if (config->dot11RSNAConfigSATimeout)
883                 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
884                         config->dot11RSNAConfigSATimeout);
885         if (config->update_config)
886                 fprintf(f, "update_config=%d\n", config->update_config);
887 #ifdef CONFIG_WPS
888         if (is_nil_uuid(config->uuid)) {
889                 char buf[40];
890                 uuid_bin2str(config->uuid, buf, sizeof(buf));
891                 fprintf(f, "uuid=%s\n", buf);
892         }
893 #endif /* CONFIG_WPS */
894         if (config->alpha2[0] && config->alpha2[1]) {
895                 fprintf(f, "country=%c%c\n",
896                         config->alpha2[0], config->alpha2[1]);
897         }
898 }
899
900 #endif /* CONFIG_NO_CONFIG_WRITE */
901
902
903 int wpa_config_write(const char *name, struct wpa_config *config)
904 {
905 #ifndef CONFIG_NO_CONFIG_WRITE
906         FILE *f;
907         struct wpa_ssid *ssid;
908 #ifndef CONFIG_NO_CONFIG_BLOBS
909         struct wpa_config_blob *blob;
910 #endif /* CONFIG_NO_CONFIG_BLOBS */
911         int ret = 0;
912
913         wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
914
915         f = fopen(name, "w");
916         if (f == NULL) {
917                 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
918                 return -1;
919         }
920
921         wpa_config_write_global(f, config);
922
923         for (ssid = config->ssid; ssid; ssid = ssid->next) {
924                 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
925                         continue; /* do not save temporary WPS networks */
926                 fprintf(f, "\nnetwork={\n");
927                 wpa_config_write_network(f, ssid);
928                 fprintf(f, "}\n");
929         }
930
931 #ifndef CONFIG_NO_CONFIG_BLOBS
932         for (blob = config->blobs; blob; blob = blob->next) {
933                 ret = wpa_config_write_blob(f, blob);
934                 if (ret)
935                         break;
936         }
937 #endif /* CONFIG_NO_CONFIG_BLOBS */
938
939         fclose(f);
940
941         wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
942                    name, ret ? "un" : "");
943         return ret;
944 #else /* CONFIG_NO_CONFIG_WRITE */
945         return -1;
946 #endif /* CONFIG_NO_CONFIG_WRITE */
947 }