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