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