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