xref: /freebsd/contrib/wpa/wpa_supplicant/config_file.c (revision 2b8331622f0b212cf3bb4fc4914a501e5321d506)
1 /*
2  * WPA Supplicant / Configuration backend: text file
3  * Copyright (c) 2003-2019, 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 #ifdef ANDROID
15 #include <sys/stat.h>
16 #endif /* ANDROID */
17 
18 #include "common.h"
19 #include "config.h"
20 #include "base64.h"
21 #include "uuid.h"
22 #include "common/ieee802_1x_defs.h"
23 #include "p2p/p2p.h"
24 #include "eap_peer/eap_methods.h"
25 #include "eap_peer/eap.h"
26 #include "utils/config.h"
27 
28 
29 static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
30 {
31 	int errors = 0;
32 
33 	if (ssid->passphrase) {
34 		if (ssid->psk_set) {
35 			wpa_printf(MSG_ERROR, "Line %d: both PSK and "
36 				   "passphrase configured.", line);
37 			errors++;
38 		}
39 		wpa_config_update_psk(ssid);
40 	}
41 
42 	if (ssid->disabled == 2)
43 		ssid->p2p_persistent_group = 1;
44 
45 	if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
46 	    !(ssid->pairwise_cipher & (WPA_CIPHER_CCMP | WPA_CIPHER_CCMP_256 |
47 				       WPA_CIPHER_GCMP | WPA_CIPHER_GCMP_256 |
48 				       WPA_CIPHER_NONE))) {
49 		/* Group cipher cannot be stronger than the pairwise cipher. */
50 		wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
51 			   " list since it was not allowed for pairwise "
52 			   "cipher", line);
53 		ssid->group_cipher &= ~WPA_CIPHER_CCMP;
54 	}
55 
56 	if (ssid->mode == WPAS_MODE_MESH &&
57 	    (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
58 	    ssid->key_mgmt != WPA_KEY_MGMT_SAE)) {
59 		wpa_printf(MSG_ERROR,
60 			   "Line %d: key_mgmt for mesh network should be open or SAE",
61 			   line);
62 		errors++;
63 	}
64 
65 #ifdef CONFIG_OCV
66 	if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
67 		wpa_printf(MSG_ERROR,
68 			   "Line %d: PMF needs to be enabled whenever using OCV",
69 			   line);
70 		errors++;
71 	}
72 #endif /* CONFIG_OCV */
73 
74 	return errors;
75 }
76 
77 
78 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
79 {
80 	struct wpa_ssid *ssid;
81 	int errors = 0, end = 0;
82 	char buf[2000], *pos, *pos2;
83 
84 	wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
85 		   *line);
86 	ssid = os_zalloc(sizeof(*ssid));
87 	if (ssid == NULL)
88 		return NULL;
89 	dl_list_init(&ssid->psk_list);
90 	ssid->id = id;
91 
92 	wpa_config_set_network_defaults(ssid);
93 
94 	while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
95 		if (os_strcmp(pos, "}") == 0) {
96 			end = 1;
97 			break;
98 		}
99 
100 		pos2 = os_strchr(pos, '=');
101 		if (pos2 == NULL) {
102 			wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
103 				   "'%s'.", *line, pos);
104 			errors++;
105 			continue;
106 		}
107 
108 		*pos2++ = '\0';
109 		if (*pos2 == '"') {
110 			if (os_strchr(pos2 + 1, '"') == NULL) {
111 				wpa_printf(MSG_ERROR, "Line %d: invalid "
112 					   "quotation '%s'.", *line, pos2);
113 				errors++;
114 				continue;
115 			}
116 		}
117 
118 		if (wpa_config_set(ssid, pos, pos2, *line) < 0) {
119 #ifndef CONFIG_WEP
120 			if (os_strcmp(pos, "wep_key0") == 0 ||
121 			    os_strcmp(pos, "wep_key1") == 0 ||
122 			    os_strcmp(pos, "wep_key2") == 0 ||
123 			    os_strcmp(pos, "wep_key3") == 0 ||
124 			    os_strcmp(pos, "wep_tx_keyidx") == 0) {
125 				wpa_printf(MSG_ERROR,
126 					   "Line %d: unsupported WEP parameter",
127 					   *line);
128 				ssid->disabled = 1;
129 				continue;
130 			}
131 #endif /* CONFIG_WEP */
132 			errors++;
133 		}
134 	}
135 
136 	if (!end) {
137 		wpa_printf(MSG_ERROR, "Line %d: network block was not "
138 			   "terminated properly.", *line);
139 		errors++;
140 	}
141 
142 	errors += wpa_config_validate_network(ssid, *line);
143 
144 	if (errors) {
145 		wpa_config_free_ssid(ssid);
146 		ssid = NULL;
147 	}
148 
149 	return ssid;
150 }
151 
152 
153 static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id)
154 {
155 	struct wpa_cred *cred;
156 	int errors = 0, end = 0;
157 	char buf[256], *pos, *pos2;
158 
159 	wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line);
160 	cred = os_zalloc(sizeof(*cred));
161 	if (cred == NULL)
162 		return NULL;
163 	cred->id = id;
164 	cred->sim_num = DEFAULT_USER_SELECTED_SIM;
165 
166 	while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
167 		if (os_strcmp(pos, "}") == 0) {
168 			end = 1;
169 			break;
170 		}
171 
172 		pos2 = os_strchr(pos, '=');
173 		if (pos2 == NULL) {
174 			wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
175 				   "'%s'.", *line, pos);
176 			errors++;
177 			continue;
178 		}
179 
180 		*pos2++ = '\0';
181 		if (*pos2 == '"') {
182 			if (os_strchr(pos2 + 1, '"') == NULL) {
183 				wpa_printf(MSG_ERROR, "Line %d: invalid "
184 					   "quotation '%s'.", *line, pos2);
185 				errors++;
186 				continue;
187 			}
188 		}
189 
190 		if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
191 			errors++;
192 	}
193 
194 	if (!end) {
195 		wpa_printf(MSG_ERROR, "Line %d: cred block was not "
196 			   "terminated properly.", *line);
197 		errors++;
198 	}
199 
200 	if (errors) {
201 		wpa_config_free_cred(cred);
202 		cred = NULL;
203 	}
204 
205 	return cred;
206 }
207 
208 
209 #ifndef CONFIG_NO_CONFIG_BLOBS
210 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
211 						     const char *name)
212 {
213 	struct wpa_config_blob *blob;
214 	char buf[256], *pos;
215 	char *encoded = NULL, *nencoded;
216 	int end = 0;
217 	size_t encoded_len = 0, len;
218 
219 	wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
220 		   *line, name);
221 
222 	while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
223 		if (os_strcmp(pos, "}") == 0) {
224 			end = 1;
225 			break;
226 		}
227 
228 		len = os_strlen(pos);
229 		nencoded = os_realloc(encoded, encoded_len + len);
230 		if (nencoded == NULL) {
231 			wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
232 				   "blob", *line);
233 			os_free(encoded);
234 			return NULL;
235 		}
236 		encoded = nencoded;
237 		os_memcpy(encoded + encoded_len, pos, len);
238 		encoded_len += len;
239 	}
240 
241 	if (!end || !encoded) {
242 		wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
243 			   "properly", *line);
244 		os_free(encoded);
245 		return NULL;
246 	}
247 
248 	blob = os_zalloc(sizeof(*blob));
249 	if (blob == NULL) {
250 		os_free(encoded);
251 		return NULL;
252 	}
253 	blob->name = os_strdup(name);
254 	blob->data = base64_decode(encoded, encoded_len, &blob->len);
255 	os_free(encoded);
256 
257 	if (blob->name == NULL || blob->data == NULL) {
258 		wpa_config_free_blob(blob);
259 		return NULL;
260 	}
261 
262 	return blob;
263 }
264 
265 
266 static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
267 				   int *line, char *bname)
268 {
269 	char *name_end;
270 	struct wpa_config_blob *blob;
271 
272 	name_end = os_strchr(bname, '=');
273 	if (name_end == NULL) {
274 		wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
275 			   *line);
276 		return -1;
277 	}
278 	*name_end = '\0';
279 
280 	blob = wpa_config_read_blob(f, line, bname);
281 	if (blob == NULL) {
282 		wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
283 			   *line, bname);
284 		return -1;
285 	}
286 	wpa_config_set_blob(config, blob);
287 	return 0;
288 }
289 #endif /* CONFIG_NO_CONFIG_BLOBS */
290 
291 
292 struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
293 {
294 	FILE *f;
295 	char buf[512], *pos;
296 	int errors = 0, line = 0;
297 	struct wpa_ssid *ssid, *tail, *head;
298 	struct wpa_cred *cred, *cred_tail, *cred_head;
299 	struct wpa_config *config;
300 	int id = 0;
301 	int cred_id = 0;
302 
303 	if (name == NULL)
304 		return NULL;
305 	if (cfgp)
306 		config = cfgp;
307 	else
308 		config = wpa_config_alloc_empty(NULL, NULL);
309 	if (config == NULL) {
310 		wpa_printf(MSG_ERROR, "Failed to allocate config file "
311 			   "structure");
312 		return NULL;
313 	}
314 	tail = head = config->ssid;
315 	while (tail && tail->next)
316 		tail = tail->next;
317 	cred_tail = cred_head = config->cred;
318 	while (cred_tail && cred_tail->next)
319 		cred_tail = cred_tail->next;
320 
321 	wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
322 	f = fopen(name, "r");
323 	if (f == NULL) {
324 		wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
325 			   "error: %s", name, strerror(errno));
326 		if (config != cfgp)
327 			os_free(config);
328 		return NULL;
329 	}
330 
331 	while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
332 		if (os_strcmp(pos, "network={") == 0) {
333 			ssid = wpa_config_read_network(f, &line, id++);
334 			if (ssid == NULL) {
335 				wpa_printf(MSG_ERROR, "Line %d: failed to "
336 					   "parse network block.", line);
337 				errors++;
338 				continue;
339 			}
340 			if (head == NULL) {
341 				head = tail = ssid;
342 			} else {
343 				tail->next = ssid;
344 				tail = ssid;
345 			}
346 			if (wpa_config_add_prio_network(config, ssid)) {
347 				wpa_printf(MSG_ERROR, "Line %d: failed to add "
348 					   "network block to priority list.",
349 					   line);
350 				errors++;
351 				continue;
352 			}
353 		} else if (os_strcmp(pos, "cred={") == 0) {
354 			cred = wpa_config_read_cred(f, &line, cred_id++);
355 			if (cred == NULL) {
356 				wpa_printf(MSG_ERROR, "Line %d: failed to "
357 					   "parse cred block.", line);
358 				errors++;
359 				continue;
360 			}
361 			if (cred_head == NULL) {
362 				cred_head = cred_tail = cred;
363 			} else {
364 				cred_tail->next = cred;
365 				cred_tail = cred;
366 			}
367 #ifndef CONFIG_NO_CONFIG_BLOBS
368 		} else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
369 			if (wpa_config_process_blob(config, f, &line, pos + 12)
370 			    < 0) {
371 				wpa_printf(MSG_ERROR, "Line %d: failed to "
372 					   "process blob.", line);
373 				errors++;
374 				continue;
375 			}
376 #endif /* CONFIG_NO_CONFIG_BLOBS */
377 		} else if (wpa_config_process_global(config, pos, line) < 0) {
378 			wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
379 				   "line '%s'.", line, pos);
380 			errors++;
381 			continue;
382 		}
383 	}
384 
385 	fclose(f);
386 
387 	config->ssid = head;
388 	wpa_config_debug_dump_networks(config);
389 	config->cred = cred_head;
390 
391 #ifndef WPA_IGNORE_CONFIG_ERRORS
392 	if (errors) {
393 		if (config != cfgp)
394 			wpa_config_free(config);
395 		config = NULL;
396 		head = NULL;
397 	}
398 #endif /* WPA_IGNORE_CONFIG_ERRORS */
399 
400 	return config;
401 }
402 
403 
404 #ifndef CONFIG_NO_CONFIG_WRITE
405 
406 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
407 {
408 	char *value = wpa_config_get(ssid, field);
409 	if (value == NULL)
410 		return;
411 	fprintf(f, "\t%s=%s\n", field, value);
412 	str_clear_free(value);
413 }
414 
415 
416 static void write_int(FILE *f, const char *field, int value, int def)
417 {
418 	if (value == def)
419 		return;
420 	fprintf(f, "\t%s=%d\n", field, value);
421 }
422 
423 
424 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
425 {
426 	char *value = wpa_config_get(ssid, "bssid");
427 	if (value == NULL)
428 		return;
429 	fprintf(f, "\tbssid=%s\n", value);
430 	os_free(value);
431 }
432 
433 
434 static void write_bssid_hint(FILE *f, struct wpa_ssid *ssid)
435 {
436 	char *value = wpa_config_get(ssid, "bssid_hint");
437 
438 	if (!value)
439 		return;
440 	fprintf(f, "\tbssid_hint=%s\n", value);
441 	os_free(value);
442 }
443 
444 
445 static void write_psk(FILE *f, struct wpa_ssid *ssid)
446 {
447 	char *value;
448 
449 	if (ssid->mem_only_psk)
450 		return;
451 
452 	value = wpa_config_get(ssid, "psk");
453 	if (value == NULL)
454 		return;
455 	fprintf(f, "\tpsk=%s\n", value);
456 	os_free(value);
457 }
458 
459 
460 static void write_proto(FILE *f, struct wpa_ssid *ssid)
461 {
462 	char *value;
463 
464 	if (ssid->proto == DEFAULT_PROTO)
465 		return;
466 
467 	value = wpa_config_get(ssid, "proto");
468 	if (value == NULL)
469 		return;
470 	if (value[0])
471 		fprintf(f, "\tproto=%s\n", value);
472 	os_free(value);
473 }
474 
475 
476 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
477 {
478 	char *value;
479 
480 	if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
481 		return;
482 
483 	value = wpa_config_get(ssid, "key_mgmt");
484 	if (value == NULL)
485 		return;
486 	if (value[0])
487 		fprintf(f, "\tkey_mgmt=%s\n", value);
488 	os_free(value);
489 }
490 
491 
492 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
493 {
494 	char *value;
495 
496 	if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
497 		return;
498 
499 	value = wpa_config_get(ssid, "pairwise");
500 	if (value == NULL)
501 		return;
502 	if (value[0])
503 		fprintf(f, "\tpairwise=%s\n", value);
504 	os_free(value);
505 }
506 
507 
508 static void write_group(FILE *f, struct wpa_ssid *ssid)
509 {
510 	char *value;
511 
512 	if (ssid->group_cipher == DEFAULT_GROUP)
513 		return;
514 
515 	value = wpa_config_get(ssid, "group");
516 	if (value == NULL)
517 		return;
518 	if (value[0])
519 		fprintf(f, "\tgroup=%s\n", value);
520 	os_free(value);
521 }
522 
523 
524 static void write_group_mgmt(FILE *f, struct wpa_ssid *ssid)
525 {
526 	char *value;
527 
528 	if (!ssid->group_mgmt_cipher)
529 		return;
530 
531 	value = wpa_config_get(ssid, "group_mgmt");
532 	if (!value)
533 		return;
534 	if (value[0])
535 		fprintf(f, "\tgroup_mgmt=%s\n", value);
536 	os_free(value);
537 }
538 
539 
540 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
541 {
542 	char *value;
543 
544 	if (ssid->auth_alg == 0)
545 		return;
546 
547 	value = wpa_config_get(ssid, "auth_alg");
548 	if (value == NULL)
549 		return;
550 	if (value[0])
551 		fprintf(f, "\tauth_alg=%s\n", value);
552 	os_free(value);
553 }
554 
555 
556 #ifdef IEEE8021X_EAPOL
557 static void write_eap(FILE *f, struct wpa_ssid *ssid)
558 {
559 	char *value;
560 
561 	value = wpa_config_get(ssid, "eap");
562 	if (value == NULL)
563 		return;
564 
565 	if (value[0])
566 		fprintf(f, "\teap=%s\n", value);
567 	os_free(value);
568 }
569 #endif /* IEEE8021X_EAPOL */
570 
571 
572 #ifdef CONFIG_WEP
573 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
574 {
575 	char field[20], *value;
576 	int res;
577 
578 	res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
579 	if (os_snprintf_error(sizeof(field), res))
580 		return;
581 	value = wpa_config_get(ssid, field);
582 	if (value) {
583 		fprintf(f, "\t%s=%s\n", field, value);
584 		os_free(value);
585 	}
586 }
587 #endif /* CONFIG_WEP */
588 
589 
590 #ifdef CONFIG_P2P
591 
592 static void write_go_p2p_dev_addr(FILE *f, struct wpa_ssid *ssid)
593 {
594 	char *value = wpa_config_get(ssid, "go_p2p_dev_addr");
595 	if (value == NULL)
596 		return;
597 	fprintf(f, "\tgo_p2p_dev_addr=%s\n", value);
598 	os_free(value);
599 }
600 
601 static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
602 {
603 	char *value = wpa_config_get(ssid, "p2p_client_list");
604 	if (value == NULL)
605 		return;
606 	fprintf(f, "\tp2p_client_list=%s\n", value);
607 	os_free(value);
608 }
609 
610 
611 static void write_psk_list(FILE *f, struct wpa_ssid *ssid)
612 {
613 	struct psk_list_entry *psk;
614 	char hex[32 * 2 + 1];
615 
616 	dl_list_for_each(psk, &ssid->psk_list, struct psk_list_entry, list) {
617 		wpa_snprintf_hex(hex, sizeof(hex), psk->psk, sizeof(psk->psk));
618 		fprintf(f, "\tpsk_list=%s" MACSTR "-%s\n",
619 			psk->p2p ? "P2P-" : "", MAC2STR(psk->addr), hex);
620 	}
621 }
622 
623 #endif /* CONFIG_P2P */
624 
625 
626 #ifdef CONFIG_MACSEC
627 
628 static void write_mka_cak(FILE *f, struct wpa_ssid *ssid)
629 {
630 	char *value;
631 
632 	if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
633 		return;
634 
635 	value = wpa_config_get(ssid, "mka_cak");
636 	if (!value)
637 		return;
638 	fprintf(f, "\tmka_cak=%s\n", value);
639 	os_free(value);
640 }
641 
642 
643 static void write_mka_ckn(FILE *f, struct wpa_ssid *ssid)
644 {
645 	char *value;
646 
647 	if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
648 		return;
649 
650 	value = wpa_config_get(ssid, "mka_ckn");
651 	if (!value)
652 		return;
653 	fprintf(f, "\tmka_ckn=%s\n", value);
654 	os_free(value);
655 }
656 
657 #endif /* CONFIG_MACSEC */
658 
659 
660 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
661 {
662 #define STR(t) write_str(f, #t, ssid)
663 #define INT(t) write_int(f, #t, ssid->t, 0)
664 #define INTe(t, m) write_int(f, #t, ssid->eap.m, 0)
665 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
666 #define INT_DEFe(t, m, def) write_int(f, #t, ssid->eap.m, def)
667 
668 	STR(ssid);
669 	INT(scan_ssid);
670 	write_bssid(f, ssid);
671 	write_bssid_hint(f, ssid);
672 	write_str(f, "bssid_ignore", ssid);
673 	write_str(f, "bssid_accept", ssid);
674 	write_psk(f, ssid);
675 	INT(mem_only_psk);
676 	STR(sae_password);
677 	STR(sae_password_id);
678 	write_int(f, "sae_pwe", ssid->sae_pwe, DEFAULT_SAE_PWE);
679 	write_proto(f, ssid);
680 	write_key_mgmt(f, ssid);
681 	INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
682 	write_pairwise(f, ssid);
683 	write_group(f, ssid);
684 	write_group_mgmt(f, ssid);
685 	write_auth_alg(f, ssid);
686 	STR(bgscan);
687 	STR(autoscan);
688 	STR(scan_freq);
689 #ifdef IEEE8021X_EAPOL
690 	write_eap(f, ssid);
691 	STR(identity);
692 	STR(anonymous_identity);
693 	STR(imsi_identity);
694 	STR(machine_identity);
695 	STR(password);
696 	STR(machine_password);
697 	STR(ca_cert);
698 	STR(ca_path);
699 	STR(client_cert);
700 	STR(private_key);
701 	STR(private_key_passwd);
702 	STR(dh_file);
703 	STR(subject_match);
704 	STR(check_cert_subject);
705 	STR(altsubject_match);
706 	STR(domain_suffix_match);
707 	STR(domain_match);
708 	STR(ca_cert2);
709 	STR(ca_path2);
710 	STR(client_cert2);
711 	STR(private_key2);
712 	STR(private_key2_passwd);
713 	STR(dh_file2);
714 	STR(subject_match2);
715 	STR(check_cert_subject2);
716 	STR(altsubject_match2);
717 	STR(domain_suffix_match2);
718 	STR(domain_match2);
719 	STR(machine_ca_cert);
720 	STR(machine_ca_path);
721 	STR(machine_client_cert);
722 	STR(machine_private_key);
723 	STR(machine_private_key_passwd);
724 	STR(machine_dh_file);
725 	STR(machine_subject_match);
726 	STR(machine_check_cert_subject);
727 	STR(machine_altsubject_match);
728 	STR(machine_domain_suffix_match);
729 	STR(machine_domain_match);
730 	STR(phase1);
731 	STR(phase2);
732 	STR(machine_phase2);
733 	STR(pcsc);
734 	STR(pin);
735 	STR(engine_id);
736 	STR(key_id);
737 	STR(cert_id);
738 	STR(ca_cert_id);
739 	STR(key2_id);
740 	STR(pin2);
741 	STR(engine2_id);
742 	STR(cert2_id);
743 	STR(ca_cert2_id);
744 	INTe(engine, cert.engine);
745 	INTe(engine2, phase2_cert.engine);
746 	INTe(machine_engine, machine_cert.engine);
747 	INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
748 	STR(openssl_ciphers);
749 	INTe(erp, erp);
750 #endif /* IEEE8021X_EAPOL */
751 #ifdef CONFIG_WEP
752 	{
753 		int i;
754 
755 		for (i = 0; i < 4; i++)
756 			write_wep_key(f, i, ssid);
757 		INT(wep_tx_keyidx);
758 	}
759 #endif /* CONFIG_WEP */
760 	INT(priority);
761 #ifdef IEEE8021X_EAPOL
762 	INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
763 	STR(pac_file);
764 	INT_DEFe(fragment_size, fragment_size, DEFAULT_FRAGMENT_SIZE);
765 	INTe(ocsp, cert.ocsp);
766 	INTe(ocsp2, phase2_cert.ocsp);
767 	INTe(machine_ocsp, machine_cert.ocsp);
768 	INT_DEFe(sim_num, sim_num, DEFAULT_USER_SELECTED_SIM);
769 #endif /* IEEE8021X_EAPOL */
770 	INT(mode);
771 	INT(no_auto_peer);
772 	INT(mesh_fwding);
773 	INT(frequency);
774 	INT(enable_edmg);
775 	INT(edmg_channel);
776 	INT(fixed_freq);
777 #ifdef CONFIG_ACS
778 	INT(acs);
779 #endif /* CONFIG_ACS */
780 	write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
781 	INT(disabled);
782 	INT(mixed_cell);
783 	INT_DEF(vht, 1);
784 	INT_DEF(ht, 1);
785 	INT(ht40);
786 	INT_DEF(he, 1);
787 	INT_DEF(max_oper_chwidth, DEFAULT_MAX_OPER_CHWIDTH);
788 	INT(vht_center_freq1);
789 	INT(vht_center_freq2);
790 	INT(pbss);
791 	INT(wps_disabled);
792 	INT(fils_dh_group);
793 	write_int(f, "ieee80211w", ssid->ieee80211w,
794 		  MGMT_FRAME_PROTECTION_DEFAULT);
795 	STR(id_str);
796 #ifdef CONFIG_P2P
797 	write_go_p2p_dev_addr(f, ssid);
798 	write_p2p_client_list(f, ssid);
799 	write_psk_list(f, ssid);
800 #endif /* CONFIG_P2P */
801 	INT(ap_max_inactivity);
802 	INT(dtim_period);
803 	INT(beacon_int);
804 #ifdef CONFIG_MACSEC
805 	INT(macsec_policy);
806 	write_mka_cak(f, ssid);
807 	write_mka_ckn(f, ssid);
808 	INT(macsec_integ_only);
809 	INT(macsec_replay_protect);
810 	INT(macsec_replay_window);
811 	INT(macsec_port);
812 	INT_DEF(mka_priority, DEFAULT_PRIO_NOT_KEY_SERVER);
813 #endif /* CONFIG_MACSEC */
814 #ifdef CONFIG_HS20
815 	INT(update_identifier);
816 	STR(roaming_consortium_selection);
817 #endif /* CONFIG_HS20 */
818 	write_int(f, "mac_addr", ssid->mac_addr, -1);
819 #ifdef CONFIG_MESH
820 	STR(mesh_basic_rates);
821 	INT_DEF(dot11MeshMaxRetries, DEFAULT_MESH_MAX_RETRIES);
822 	INT_DEF(dot11MeshRetryTimeout, DEFAULT_MESH_RETRY_TIMEOUT);
823 	INT_DEF(dot11MeshConfirmTimeout, DEFAULT_MESH_CONFIRM_TIMEOUT);
824 	INT_DEF(dot11MeshHoldingTimeout, DEFAULT_MESH_HOLDING_TIMEOUT);
825 	INT_DEF(mesh_rssi_threshold, DEFAULT_MESH_RSSI_THRESHOLD);
826 #endif /* CONFIG_MESH */
827 	INT(wpa_ptk_rekey);
828 	INT(wpa_deny_ptk0_rekey);
829 	INT(group_rekey);
830 	INT(ignore_broadcast_ssid);
831 #ifdef CONFIG_DPP
832 	STR(dpp_connector);
833 	STR(dpp_netaccesskey);
834 	INT(dpp_netaccesskey_expiry);
835 	STR(dpp_csign);
836 	STR(dpp_pp_key);
837 	INT(dpp_pfs);
838 #endif /* CONFIG_DPP */
839 	INT(owe_group);
840 	INT(owe_only);
841 	INT(owe_ptk_workaround);
842 	INT(multi_ap_backhaul_sta);
843 	INT(ft_eap_pmksa_caching);
844 	INT(beacon_prot);
845 	INT(transition_disable);
846 	INT(sae_pk);
847 #ifdef CONFIG_HT_OVERRIDES
848 	INT_DEF(disable_ht, DEFAULT_DISABLE_HT);
849 	INT_DEF(disable_ht40, DEFAULT_DISABLE_HT40);
850 	INT_DEF(disable_sgi, DEFAULT_DISABLE_SGI);
851 	INT_DEF(disable_ldpc, DEFAULT_DISABLE_LDPC);
852 	INT(ht40_intolerant);
853 	INT_DEF(tx_stbc, DEFAULT_TX_STBC);
854 	INT_DEF(rx_stbc, DEFAULT_RX_STBC);
855 	INT_DEF(disable_max_amsdu, DEFAULT_DISABLE_MAX_AMSDU);
856 	INT_DEF(ampdu_factor, DEFAULT_AMPDU_FACTOR);
857 	INT_DEF(ampdu_density, DEFAULT_AMPDU_DENSITY);
858 	STR(ht_mcs);
859 #endif /* CONFIG_HT_OVERRIDES */
860 #ifdef CONFIG_VHT_OVERRIDES
861 	INT(disable_vht);
862 	INT(vht_capa);
863 	INT(vht_capa_mask);
864 	INT_DEF(vht_rx_mcs_nss_1, -1);
865 	INT_DEF(vht_rx_mcs_nss_2, -1);
866 	INT_DEF(vht_rx_mcs_nss_3, -1);
867 	INT_DEF(vht_rx_mcs_nss_4, -1);
868 	INT_DEF(vht_rx_mcs_nss_5, -1);
869 	INT_DEF(vht_rx_mcs_nss_6, -1);
870 	INT_DEF(vht_rx_mcs_nss_7, -1);
871 	INT_DEF(vht_rx_mcs_nss_8, -1);
872 	INT_DEF(vht_tx_mcs_nss_1, -1);
873 	INT_DEF(vht_tx_mcs_nss_2, -1);
874 	INT_DEF(vht_tx_mcs_nss_3, -1);
875 	INT_DEF(vht_tx_mcs_nss_4, -1);
876 	INT_DEF(vht_tx_mcs_nss_5, -1);
877 	INT_DEF(vht_tx_mcs_nss_6, -1);
878 	INT_DEF(vht_tx_mcs_nss_7, -1);
879 	INT_DEF(vht_tx_mcs_nss_8, -1);
880 #endif /* CONFIG_VHT_OVERRIDES */
881 #ifdef CONFIG_HE_OVERRIDES
882 	INT(disable_he);
883 #endif /* CONFIG_HE_OVERRIDES */
884 
885 #undef STR
886 #undef INT
887 #undef INT_DEF
888 }
889 
890 
891 static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
892 {
893 	size_t i;
894 
895 	if (cred->priority)
896 		fprintf(f, "\tpriority=%d\n", cred->priority);
897 	if (cred->pcsc)
898 		fprintf(f, "\tpcsc=%d\n", cred->pcsc);
899 	if (cred->realm)
900 		fprintf(f, "\trealm=\"%s\"\n", cred->realm);
901 	if (cred->username)
902 		fprintf(f, "\tusername=\"%s\"\n", cred->username);
903 	if (cred->password && cred->ext_password)
904 		fprintf(f, "\tpassword=ext:%s\n", cred->password);
905 	else if (cred->password)
906 		fprintf(f, "\tpassword=\"%s\"\n", cred->password);
907 	if (cred->ca_cert)
908 		fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
909 	if (cred->client_cert)
910 		fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
911 	if (cred->private_key)
912 		fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
913 	if (cred->private_key_passwd)
914 		fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
915 			cred->private_key_passwd);
916 	if (cred->imsi)
917 		fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
918 	if (cred->milenage)
919 		fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
920 	for (i = 0; i < cred->num_domain; i++)
921 		fprintf(f, "\tdomain=\"%s\"\n", cred->domain[i]);
922 	if (cred->domain_suffix_match)
923 		fprintf(f, "\tdomain_suffix_match=\"%s\"\n",
924 			cred->domain_suffix_match);
925 	if (cred->roaming_consortium_len) {
926 		fprintf(f, "\troaming_consortium=");
927 		for (i = 0; i < cred->roaming_consortium_len; i++)
928 			fprintf(f, "%02x", cred->roaming_consortium[i]);
929 		fprintf(f, "\n");
930 	}
931 	if (cred->eap_method) {
932 		const char *name;
933 		name = eap_get_name(cred->eap_method[0].vendor,
934 				    cred->eap_method[0].method);
935 		if (name)
936 			fprintf(f, "\teap=%s\n", name);
937 	}
938 	if (cred->phase1)
939 		fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
940 	if (cred->phase2)
941 		fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
942 	if (cred->excluded_ssid) {
943 		size_t j;
944 		for (i = 0; i < cred->num_excluded_ssid; i++) {
945 			struct excluded_ssid *e = &cred->excluded_ssid[i];
946 			fprintf(f, "\texcluded_ssid=");
947 			for (j = 0; j < e->ssid_len; j++)
948 				fprintf(f, "%02x", e->ssid[j]);
949 			fprintf(f, "\n");
950 		}
951 	}
952 	if (cred->roaming_partner) {
953 		for (i = 0; i < cred->num_roaming_partner; i++) {
954 			struct roaming_partner *p = &cred->roaming_partner[i];
955 			fprintf(f, "\troaming_partner=\"%s,%d,%u,%s\"\n",
956 				p->fqdn, p->exact_match, p->priority,
957 				p->country);
958 		}
959 	}
960 	if (cred->update_identifier)
961 		fprintf(f, "\tupdate_identifier=%d\n", cred->update_identifier);
962 
963 	if (cred->provisioning_sp)
964 		fprintf(f, "\tprovisioning_sp=\"%s\"\n", cred->provisioning_sp);
965 	if (cred->sp_priority)
966 		fprintf(f, "\tsp_priority=%d\n", cred->sp_priority);
967 
968 	if (cred->min_dl_bandwidth_home)
969 		fprintf(f, "\tmin_dl_bandwidth_home=%u\n",
970 			cred->min_dl_bandwidth_home);
971 	if (cred->min_ul_bandwidth_home)
972 		fprintf(f, "\tmin_ul_bandwidth_home=%u\n",
973 			cred->min_ul_bandwidth_home);
974 	if (cred->min_dl_bandwidth_roaming)
975 		fprintf(f, "\tmin_dl_bandwidth_roaming=%u\n",
976 			cred->min_dl_bandwidth_roaming);
977 	if (cred->min_ul_bandwidth_roaming)
978 		fprintf(f, "\tmin_ul_bandwidth_roaming=%u\n",
979 			cred->min_ul_bandwidth_roaming);
980 
981 	if (cred->max_bss_load)
982 		fprintf(f, "\tmax_bss_load=%u\n",
983 			cred->max_bss_load);
984 
985 	if (cred->ocsp)
986 		fprintf(f, "\tocsp=%d\n", cred->ocsp);
987 
988 	if (cred->num_req_conn_capab) {
989 		for (i = 0; i < cred->num_req_conn_capab; i++) {
990 			int *ports;
991 
992 			fprintf(f, "\treq_conn_capab=%u",
993 				cred->req_conn_capab_proto[i]);
994 			ports = cred->req_conn_capab_port[i];
995 			if (ports) {
996 				int j;
997 				for (j = 0; ports[j] != -1; j++) {
998 					fprintf(f, "%s%d", j > 0 ? "," : ":",
999 						ports[j]);
1000 				}
1001 			}
1002 			fprintf(f, "\n");
1003 		}
1004 	}
1005 
1006 	if (cred->required_roaming_consortium_len) {
1007 		fprintf(f, "\trequired_roaming_consortium=");
1008 		for (i = 0; i < cred->required_roaming_consortium_len; i++)
1009 			fprintf(f, "%02x",
1010 				cred->required_roaming_consortium[i]);
1011 		fprintf(f, "\n");
1012 	}
1013 
1014 	if (cred->num_roaming_consortiums) {
1015 		size_t j;
1016 
1017 		fprintf(f, "\troaming_consortiums=\"");
1018 		for (i = 0; i < cred->num_roaming_consortiums; i++) {
1019 			if (i > 0)
1020 				fprintf(f, ",");
1021 			for (j = 0; j < cred->roaming_consortiums_len[i]; j++)
1022 				fprintf(f, "%02x",
1023 					cred->roaming_consortiums[i][j]);
1024 		}
1025 		fprintf(f, "\"\n");
1026 	}
1027 
1028 	if (cred->sim_num != DEFAULT_USER_SELECTED_SIM)
1029 		fprintf(f, "\tsim_num=%d\n", cred->sim_num);
1030 
1031 	if (cred->engine)
1032 		fprintf(f, "\tengine=%d\n", cred->engine);
1033 	if (cred->engine_id)
1034 		fprintf(f, "\tengine_id=\"%s\"\n", cred->engine_id);
1035 	if (cred->key_id)
1036 		fprintf(f, "\tkey_id=\"%s\"\n", cred->key_id);
1037 	if (cred->cert_id)
1038 		fprintf(f, "\tcert_id=\"%s\"\n", cred->cert_id);
1039 	if (cred->ca_cert_id)
1040 		fprintf(f, "\tca_cert_id=\"%s\"\n", cred->ca_cert_id);
1041 }
1042 
1043 
1044 #ifndef CONFIG_NO_CONFIG_BLOBS
1045 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
1046 {
1047 	char *encoded;
1048 
1049 	encoded = base64_encode(blob->data, blob->len, NULL);
1050 	if (encoded == NULL)
1051 		return -1;
1052 
1053 	fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
1054 	os_free(encoded);
1055 	return 0;
1056 }
1057 #endif /* CONFIG_NO_CONFIG_BLOBS */
1058 
1059 
1060 static void write_global_bin(FILE *f, const char *field,
1061 			     const struct wpabuf *val)
1062 {
1063 	size_t i;
1064 	const u8 *pos;
1065 
1066 	if (val == NULL)
1067 		return;
1068 
1069 	fprintf(f, "%s=", field);
1070 	pos = wpabuf_head(val);
1071 	for (i = 0; i < wpabuf_len(val); i++)
1072 		fprintf(f, "%02X", *pos++);
1073 	fprintf(f, "\n");
1074 }
1075 
1076 
1077 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
1078 {
1079 #ifdef CONFIG_CTRL_IFACE
1080 	if (config->ctrl_interface)
1081 		fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
1082 	if (config->ctrl_interface_group)
1083 		fprintf(f, "ctrl_interface_group=%s\n",
1084 			config->ctrl_interface_group);
1085 #endif /* CONFIG_CTRL_IFACE */
1086 	if (config->eapol_version != DEFAULT_EAPOL_VERSION)
1087 		fprintf(f, "eapol_version=%d\n", config->eapol_version);
1088 	if (config->ap_scan != DEFAULT_AP_SCAN)
1089 		fprintf(f, "ap_scan=%d\n", config->ap_scan);
1090 	if (config->disable_scan_offload)
1091 		fprintf(f, "disable_scan_offload=%d\n",
1092 			config->disable_scan_offload);
1093 	if (config->fast_reauth != DEFAULT_FAST_REAUTH)
1094 		fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
1095 	if (config->opensc_engine_path)
1096 		fprintf(f, "opensc_engine_path=%s\n",
1097 			config->opensc_engine_path);
1098 	if (config->pkcs11_engine_path)
1099 		fprintf(f, "pkcs11_engine_path=%s\n",
1100 			config->pkcs11_engine_path);
1101 	if (config->pkcs11_module_path)
1102 		fprintf(f, "pkcs11_module_path=%s\n",
1103 			config->pkcs11_module_path);
1104 	if (config->openssl_ciphers)
1105 		fprintf(f, "openssl_ciphers=%s\n", config->openssl_ciphers);
1106 	if (config->pcsc_reader)
1107 		fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
1108 	if (config->pcsc_pin)
1109 		fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
1110 	if (config->driver_param)
1111 		fprintf(f, "driver_param=%s\n", config->driver_param);
1112 	if (config->dot11RSNAConfigPMKLifetime)
1113 		fprintf(f, "dot11RSNAConfigPMKLifetime=%u\n",
1114 			config->dot11RSNAConfigPMKLifetime);
1115 	if (config->dot11RSNAConfigPMKReauthThreshold)
1116 		fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%u\n",
1117 			config->dot11RSNAConfigPMKReauthThreshold);
1118 	if (config->dot11RSNAConfigSATimeout)
1119 		fprintf(f, "dot11RSNAConfigSATimeout=%u\n",
1120 			config->dot11RSNAConfigSATimeout);
1121 	if (config->update_config)
1122 		fprintf(f, "update_config=%d\n", config->update_config);
1123 #ifdef CONFIG_WPS
1124 	if (!is_nil_uuid(config->uuid)) {
1125 		char buf[40];
1126 		uuid_bin2str(config->uuid, buf, sizeof(buf));
1127 		fprintf(f, "uuid=%s\n", buf);
1128 	}
1129 	if (config->auto_uuid)
1130 		fprintf(f, "auto_uuid=%d\n", config->auto_uuid);
1131 	if (config->device_name)
1132 		fprintf(f, "device_name=%s\n", config->device_name);
1133 	if (config->manufacturer)
1134 		fprintf(f, "manufacturer=%s\n", config->manufacturer);
1135 	if (config->model_name)
1136 		fprintf(f, "model_name=%s\n", config->model_name);
1137 	if (config->model_number)
1138 		fprintf(f, "model_number=%s\n", config->model_number);
1139 	if (config->serial_number)
1140 		fprintf(f, "serial_number=%s\n", config->serial_number);
1141 	{
1142 		char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
1143 		buf = wps_dev_type_bin2str(config->device_type,
1144 					   _buf, sizeof(_buf));
1145 		if (os_strcmp(buf, "0-00000000-0") != 0)
1146 			fprintf(f, "device_type=%s\n", buf);
1147 	}
1148 	if (WPA_GET_BE32(config->os_version))
1149 		fprintf(f, "os_version=%08x\n",
1150 			WPA_GET_BE32(config->os_version));
1151 	if (config->config_methods)
1152 		fprintf(f, "config_methods=%s\n", config->config_methods);
1153 	if (config->wps_cred_processing)
1154 		fprintf(f, "wps_cred_processing=%d\n",
1155 			config->wps_cred_processing);
1156 	if (config->wps_cred_add_sae)
1157 		fprintf(f, "wps_cred_add_sae=%d\n",
1158 			config->wps_cred_add_sae);
1159 	if (config->wps_vendor_ext_m1) {
1160 		int i, len = wpabuf_len(config->wps_vendor_ext_m1);
1161 		const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
1162 		if (len > 0) {
1163 			fprintf(f, "wps_vendor_ext_m1=");
1164 			for (i = 0; i < len; i++)
1165 				fprintf(f, "%02x", *p++);
1166 			fprintf(f, "\n");
1167 		}
1168 	}
1169 #endif /* CONFIG_WPS */
1170 #ifdef CONFIG_P2P
1171 	{
1172 		int i;
1173 		char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
1174 
1175 		for (i = 0; i < config->num_sec_device_types; i++) {
1176 			buf = wps_dev_type_bin2str(config->sec_device_type[i],
1177 						   _buf, sizeof(_buf));
1178 			if (buf)
1179 				fprintf(f, "sec_device_type=%s\n", buf);
1180 		}
1181 	}
1182 	if (config->p2p_listen_reg_class)
1183 		fprintf(f, "p2p_listen_reg_class=%d\n",
1184 			config->p2p_listen_reg_class);
1185 	if (config->p2p_listen_channel)
1186 		fprintf(f, "p2p_listen_channel=%d\n",
1187 			config->p2p_listen_channel);
1188 	if (config->p2p_oper_reg_class)
1189 		fprintf(f, "p2p_oper_reg_class=%d\n",
1190 			config->p2p_oper_reg_class);
1191 	if (config->p2p_oper_channel)
1192 		fprintf(f, "p2p_oper_channel=%d\n", config->p2p_oper_channel);
1193 	if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
1194 		fprintf(f, "p2p_go_intent=%d\n", config->p2p_go_intent);
1195 	if (config->p2p_ssid_postfix)
1196 		fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
1197 	if (config->persistent_reconnect)
1198 		fprintf(f, "persistent_reconnect=%d\n",
1199 			config->persistent_reconnect);
1200 	if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
1201 		fprintf(f, "p2p_intra_bss=%d\n", config->p2p_intra_bss);
1202 	if (config->p2p_group_idle)
1203 		fprintf(f, "p2p_group_idle=%d\n", config->p2p_group_idle);
1204 	if (config->p2p_passphrase_len)
1205 		fprintf(f, "p2p_passphrase_len=%u\n",
1206 			config->p2p_passphrase_len);
1207 	if (config->p2p_pref_chan) {
1208 		unsigned int i;
1209 		fprintf(f, "p2p_pref_chan=");
1210 		for (i = 0; i < config->num_p2p_pref_chan; i++) {
1211 			fprintf(f, "%s%u:%u", i > 0 ? "," : "",
1212 				config->p2p_pref_chan[i].op_class,
1213 				config->p2p_pref_chan[i].chan);
1214 		}
1215 		fprintf(f, "\n");
1216 	}
1217 	if (config->p2p_no_go_freq.num) {
1218 		char *val = freq_range_list_str(&config->p2p_no_go_freq);
1219 		if (val) {
1220 			fprintf(f, "p2p_no_go_freq=%s\n", val);
1221 			os_free(val);
1222 		}
1223 	}
1224 	if (config->p2p_add_cli_chan)
1225 		fprintf(f, "p2p_add_cli_chan=%d\n", config->p2p_add_cli_chan);
1226 	if (config->p2p_optimize_listen_chan !=
1227 	    DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN)
1228 		fprintf(f, "p2p_optimize_listen_chan=%d\n",
1229 			config->p2p_optimize_listen_chan);
1230 	if (config->p2p_go_ht40)
1231 		fprintf(f, "p2p_go_ht40=%d\n", config->p2p_go_ht40);
1232 	if (config->p2p_go_vht)
1233 		fprintf(f, "p2p_go_vht=%d\n", config->p2p_go_vht);
1234 	if (config->p2p_go_he)
1235 		fprintf(f, "p2p_go_he=%d\n", config->p2p_go_he);
1236 	if (config->p2p_go_edmg)
1237 		fprintf(f, "p2p_go_edmg=%d\n", config->p2p_go_edmg);
1238 	if (config->p2p_go_ctwindow != DEFAULT_P2P_GO_CTWINDOW)
1239 		fprintf(f, "p2p_go_ctwindow=%d\n", config->p2p_go_ctwindow);
1240 	if (config->p2p_disabled)
1241 		fprintf(f, "p2p_disabled=%d\n", config->p2p_disabled);
1242 	if (config->p2p_no_group_iface)
1243 		fprintf(f, "p2p_no_group_iface=%d\n",
1244 			config->p2p_no_group_iface);
1245 	if (config->p2p_ignore_shared_freq)
1246 		fprintf(f, "p2p_ignore_shared_freq=%d\n",
1247 			config->p2p_ignore_shared_freq);
1248 	if (config->p2p_cli_probe)
1249 		fprintf(f, "p2p_cli_probe=%d\n", config->p2p_cli_probe);
1250 	if (config->p2p_go_freq_change_policy != DEFAULT_P2P_GO_FREQ_MOVE)
1251 		fprintf(f, "p2p_go_freq_change_policy=%u\n",
1252 			config->p2p_go_freq_change_policy);
1253 
1254 	if (config->p2p_6ghz_disable)
1255 		fprintf(f, "p2p_6ghz_disable=%d\n", config->p2p_6ghz_disable);
1256 
1257 	if (WPA_GET_BE32(config->ip_addr_go))
1258 		fprintf(f, "ip_addr_go=%u.%u.%u.%u\n",
1259 			config->ip_addr_go[0], config->ip_addr_go[1],
1260 			config->ip_addr_go[2], config->ip_addr_go[3]);
1261 	if (WPA_GET_BE32(config->ip_addr_mask))
1262 		fprintf(f, "ip_addr_mask=%u.%u.%u.%u\n",
1263 			config->ip_addr_mask[0], config->ip_addr_mask[1],
1264 			config->ip_addr_mask[2], config->ip_addr_mask[3]);
1265 	if (WPA_GET_BE32(config->ip_addr_start))
1266 		fprintf(f, "ip_addr_start=%u.%u.%u.%u\n",
1267 			config->ip_addr_start[0], config->ip_addr_start[1],
1268 			config->ip_addr_start[2], config->ip_addr_start[3]);
1269 	if (WPA_GET_BE32(config->ip_addr_end))
1270 		fprintf(f, "ip_addr_end=%u.%u.%u.%u\n",
1271 			config->ip_addr_end[0], config->ip_addr_end[1],
1272 			config->ip_addr_end[2], config->ip_addr_end[3]);
1273 #endif /* CONFIG_P2P */
1274 	if (config->country[0] && config->country[1]) {
1275 		fprintf(f, "country=%c%c\n",
1276 			config->country[0], config->country[1]);
1277 	}
1278 	if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
1279 		fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
1280 	if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
1281 		fprintf(f, "bss_expiration_age=%u\n",
1282 			config->bss_expiration_age);
1283 	if (config->bss_expiration_scan_count !=
1284 	    DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
1285 		fprintf(f, "bss_expiration_scan_count=%u\n",
1286 			config->bss_expiration_scan_count);
1287 	if (config->filter_ssids)
1288 		fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
1289 	if (config->filter_rssi)
1290 		fprintf(f, "filter_rssi=%d\n", config->filter_rssi);
1291 	if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
1292 		fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
1293 	if (config->ap_isolate != DEFAULT_AP_ISOLATE)
1294 		fprintf(f, "ap_isolate=%u\n", config->ap_isolate);
1295 	if (config->disassoc_low_ack)
1296 		fprintf(f, "disassoc_low_ack=%d\n", config->disassoc_low_ack);
1297 #ifdef CONFIG_HS20
1298 	if (config->hs20)
1299 		fprintf(f, "hs20=1\n");
1300 #endif /* CONFIG_HS20 */
1301 #ifdef CONFIG_INTERWORKING
1302 	if (config->interworking)
1303 		fprintf(f, "interworking=%d\n", config->interworking);
1304 	if (!is_zero_ether_addr(config->hessid))
1305 		fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
1306 	if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
1307 		fprintf(f, "access_network_type=%d\n",
1308 			config->access_network_type);
1309 	if (config->go_interworking)
1310 		fprintf(f, "go_interworking=%d\n", config->go_interworking);
1311 	if (config->go_access_network_type)
1312 		fprintf(f, "go_access_network_type=%d\n",
1313 			config->go_access_network_type);
1314 	if (config->go_internet)
1315 		fprintf(f, "go_internet=%d\n", config->go_internet);
1316 	if (config->go_venue_group)
1317 		fprintf(f, "go_venue_group=%d\n", config->go_venue_group);
1318 	if (config->go_venue_type)
1319 		fprintf(f, "go_venue_type=%d\n", config->go_venue_type);
1320 #endif /* CONFIG_INTERWORKING */
1321 	if (config->pbc_in_m1)
1322 		fprintf(f, "pbc_in_m1=%d\n", config->pbc_in_m1);
1323 	if (config->wps_nfc_pw_from_config) {
1324 		if (config->wps_nfc_dev_pw_id)
1325 			fprintf(f, "wps_nfc_dev_pw_id=%d\n",
1326 				config->wps_nfc_dev_pw_id);
1327 		write_global_bin(f, "wps_nfc_dh_pubkey",
1328 				 config->wps_nfc_dh_pubkey);
1329 		write_global_bin(f, "wps_nfc_dh_privkey",
1330 				 config->wps_nfc_dh_privkey);
1331 		write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
1332 	}
1333 
1334 	if (config->ext_password_backend)
1335 		fprintf(f, "ext_password_backend=%s\n",
1336 			config->ext_password_backend);
1337 	if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
1338 		fprintf(f, "p2p_go_max_inactivity=%d\n",
1339 			config->p2p_go_max_inactivity);
1340 	if (config->auto_interworking)
1341 		fprintf(f, "auto_interworking=%d\n",
1342 			config->auto_interworking);
1343 	if (config->okc)
1344 		fprintf(f, "okc=%d\n", config->okc);
1345 	if (config->pmf)
1346 		fprintf(f, "pmf=%d\n", config->pmf);
1347 	if (config->dtim_period)
1348 		fprintf(f, "dtim_period=%d\n", config->dtim_period);
1349 	if (config->beacon_int)
1350 		fprintf(f, "beacon_int=%d\n", config->beacon_int);
1351 
1352 	if (config->sae_groups) {
1353 		int i;
1354 		fprintf(f, "sae_groups=");
1355 		for (i = 0; config->sae_groups[i] > 0; i++) {
1356 			fprintf(f, "%s%d", i > 0 ? " " : "",
1357 				config->sae_groups[i]);
1358 		}
1359 		fprintf(f, "\n");
1360 	}
1361 
1362 	if (config->sae_pwe)
1363 		fprintf(f, "sae_pwe=%d\n", config->sae_pwe);
1364 
1365 	if (config->sae_pmkid_in_assoc)
1366 		fprintf(f, "sae_pmkid_in_assoc=%d\n",
1367 			config->sae_pmkid_in_assoc);
1368 
1369 	if (config->ap_vendor_elements) {
1370 		int i, len = wpabuf_len(config->ap_vendor_elements);
1371 		const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
1372 		if (len > 0) {
1373 			fprintf(f, "ap_vendor_elements=");
1374 			for (i = 0; i < len; i++)
1375 				fprintf(f, "%02x", *p++);
1376 			fprintf(f, "\n");
1377 		}
1378 	}
1379 
1380 	if (config->ap_assocresp_elements) {
1381 		int i, len = wpabuf_len(config->ap_assocresp_elements);
1382 		const u8 *p = wpabuf_head_u8(config->ap_assocresp_elements);
1383 
1384 		if (len > 0) {
1385 			fprintf(f, "ap_assocresp_elements=");
1386 			for (i = 0; i < len; i++)
1387 				fprintf(f, "%02x", *p++);
1388 			fprintf(f, "\n");
1389 		}
1390 	}
1391 
1392 	if (config->ignore_old_scan_res)
1393 		fprintf(f, "ignore_old_scan_res=%d\n",
1394 			config->ignore_old_scan_res);
1395 
1396 	if (config->freq_list && config->freq_list[0]) {
1397 		int i;
1398 		fprintf(f, "freq_list=");
1399 		for (i = 0; config->freq_list[i]; i++) {
1400 			fprintf(f, "%s%d", i > 0 ? " " : "",
1401 				config->freq_list[i]);
1402 		}
1403 		fprintf(f, "\n");
1404 	}
1405 	if (config->initial_freq_list && config->initial_freq_list[0]) {
1406 		int i;
1407 		fprintf(f, "initial_freq_list=");
1408 		for (i = 0; config->initial_freq_list[i]; i++) {
1409 			fprintf(f, "%s%d", i > 0 ? " " : "",
1410 				config->initial_freq_list[i]);
1411 		}
1412 		fprintf(f, "\n");
1413 	}
1414 	if (config->scan_cur_freq != DEFAULT_SCAN_CUR_FREQ)
1415 		fprintf(f, "scan_cur_freq=%d\n", config->scan_cur_freq);
1416 
1417 	if (config->scan_res_valid_for_connect !=
1418 	    DEFAULT_SCAN_RES_VALID_FOR_CONNECT)
1419 		fprintf(f, "scan_res_valid_for_connect=%d\n",
1420 			config->scan_res_valid_for_connect);
1421 
1422 	if (config->sched_scan_interval)
1423 		fprintf(f, "sched_scan_interval=%u\n",
1424 			config->sched_scan_interval);
1425 
1426 	if (config->sched_scan_start_delay)
1427 		fprintf(f, "sched_scan_start_delay=%u\n",
1428 			config->sched_scan_start_delay);
1429 
1430 	if (config->external_sim)
1431 		fprintf(f, "external_sim=%d\n", config->external_sim);
1432 
1433 	if (config->tdls_external_control)
1434 		fprintf(f, "tdls_external_control=%d\n",
1435 			config->tdls_external_control);
1436 
1437 	if (config->wowlan_triggers)
1438 		fprintf(f, "wowlan_triggers=%s\n",
1439 			config->wowlan_triggers);
1440 
1441 	if (config->bgscan)
1442 		fprintf(f, "bgscan=\"%s\"\n", config->bgscan);
1443 
1444 	if (config->autoscan)
1445 		fprintf(f, "autoscan=%s\n", config->autoscan);
1446 
1447 	if (config->p2p_search_delay != DEFAULT_P2P_SEARCH_DELAY)
1448 		fprintf(f, "p2p_search_delay=%u\n",
1449 			config->p2p_search_delay);
1450 
1451 	if (config->mac_addr)
1452 		fprintf(f, "mac_addr=%d\n", config->mac_addr);
1453 
1454 	if (config->rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
1455 		fprintf(f, "rand_addr_lifetime=%u\n",
1456 			config->rand_addr_lifetime);
1457 
1458 	if (config->preassoc_mac_addr)
1459 		fprintf(f, "preassoc_mac_addr=%d\n", config->preassoc_mac_addr);
1460 
1461 	if (config->key_mgmt_offload != DEFAULT_KEY_MGMT_OFFLOAD)
1462 		fprintf(f, "key_mgmt_offload=%d\n", config->key_mgmt_offload);
1463 
1464 	if (config->user_mpm != DEFAULT_USER_MPM)
1465 		fprintf(f, "user_mpm=%d\n", config->user_mpm);
1466 
1467 	if (config->max_peer_links != DEFAULT_MAX_PEER_LINKS)
1468 		fprintf(f, "max_peer_links=%d\n", config->max_peer_links);
1469 
1470 	if (config->cert_in_cb != DEFAULT_CERT_IN_CB)
1471 		fprintf(f, "cert_in_cb=%d\n", config->cert_in_cb);
1472 
1473 	if (config->mesh_max_inactivity != DEFAULT_MESH_MAX_INACTIVITY)
1474 		fprintf(f, "mesh_max_inactivity=%d\n",
1475 			config->mesh_max_inactivity);
1476 
1477 	if (config->mesh_fwding != DEFAULT_MESH_FWDING)
1478 		fprintf(f, "mesh_fwding=%d\n", config->mesh_fwding);
1479 
1480 	if (config->dot11RSNASAERetransPeriod !=
1481 	    DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD)
1482 		fprintf(f, "dot11RSNASAERetransPeriod=%d\n",
1483 			config->dot11RSNASAERetransPeriod);
1484 
1485 	if (config->passive_scan)
1486 		fprintf(f, "passive_scan=%d\n", config->passive_scan);
1487 
1488 	if (config->reassoc_same_bss_optim)
1489 		fprintf(f, "reassoc_same_bss_optim=%d\n",
1490 			config->reassoc_same_bss_optim);
1491 
1492 	if (config->wps_priority)
1493 		fprintf(f, "wps_priority=%d\n", config->wps_priority);
1494 
1495 	if (config->wpa_rsc_relaxation != DEFAULT_WPA_RSC_RELAXATION)
1496 		fprintf(f, "wpa_rsc_relaxation=%d\n",
1497 			config->wpa_rsc_relaxation);
1498 
1499 	if (config->sched_scan_plans)
1500 		fprintf(f, "sched_scan_plans=%s\n", config->sched_scan_plans);
1501 
1502 #ifdef CONFIG_MBO
1503 	if (config->non_pref_chan)
1504 		fprintf(f, "non_pref_chan=%s\n", config->non_pref_chan);
1505 	if (config->mbo_cell_capa != DEFAULT_MBO_CELL_CAPA)
1506 		fprintf(f, "mbo_cell_capa=%u\n", config->mbo_cell_capa);
1507 	if (config->disassoc_imminent_rssi_threshold !=
1508 	    DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD)
1509 		fprintf(f, "disassoc_imminent_rssi_threshold=%d\n",
1510 			config->disassoc_imminent_rssi_threshold);
1511 	if (config->oce != DEFAULT_OCE_SUPPORT)
1512 		fprintf(f, "oce=%u\n", config->oce);
1513 #endif /* CONFIG_MBO */
1514 
1515 	if (config->gas_address3)
1516 		fprintf(f, "gas_address3=%d\n", config->gas_address3);
1517 
1518 	if (config->ftm_responder)
1519 		fprintf(f, "ftm_responder=%d\n", config->ftm_responder);
1520 	if (config->ftm_initiator)
1521 		fprintf(f, "ftm_initiator=%d\n", config->ftm_initiator);
1522 
1523 	if (config->osu_dir)
1524 		fprintf(f, "osu_dir=%s\n", config->osu_dir);
1525 
1526 	if (config->fst_group_id)
1527 		fprintf(f, "fst_group_id=%s\n", config->fst_group_id);
1528 	if (config->fst_priority)
1529 		fprintf(f, "fst_priority=%d\n", config->fst_priority);
1530 	if (config->fst_llt)
1531 		fprintf(f, "fst_llt=%d\n", config->fst_llt);
1532 
1533 	if (config->gas_rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
1534 		fprintf(f, "gas_rand_addr_lifetime=%u\n",
1535 			config->gas_rand_addr_lifetime);
1536 	if (config->gas_rand_mac_addr)
1537 		fprintf(f, "gas_rand_mac_addr=%d\n", config->gas_rand_mac_addr);
1538 	if (config->dpp_config_processing)
1539 		fprintf(f, "dpp_config_processing=%d\n",
1540 			config->dpp_config_processing);
1541 	if (config->coloc_intf_reporting)
1542 		fprintf(f, "coloc_intf_reporting=%d\n",
1543 			config->coloc_intf_reporting);
1544 	if (config->p2p_device_random_mac_addr)
1545 		fprintf(f, "p2p_device_random_mac_addr=%d\n",
1546 			config->p2p_device_random_mac_addr);
1547 	if (!is_zero_ether_addr(config->p2p_device_persistent_mac_addr))
1548 		fprintf(f, "p2p_device_persistent_mac_addr=" MACSTR "\n",
1549 			MAC2STR(config->p2p_device_persistent_mac_addr));
1550 	if (config->p2p_interface_random_mac_addr)
1551 		fprintf(f, "p2p_interface_random_mac_addr=%d\n",
1552 			config->p2p_interface_random_mac_addr);
1553 	if (config->disable_btm)
1554 		fprintf(f, "disable_btm=1\n");
1555 	if (config->extended_key_id != DEFAULT_EXTENDED_KEY_ID)
1556 		fprintf(f, "extended_key_id=%d\n",
1557 			config->extended_key_id);
1558 	if (config->wowlan_disconnect_on_deinit)
1559 		fprintf(f, "wowlan_disconnect_on_deinit=%d\n",
1560 			config->wowlan_disconnect_on_deinit);
1561 }
1562 
1563 #endif /* CONFIG_NO_CONFIG_WRITE */
1564 
1565 
1566 int wpa_config_write(const char *name, struct wpa_config *config)
1567 {
1568 #ifndef CONFIG_NO_CONFIG_WRITE
1569 	FILE *f;
1570 	struct wpa_ssid *ssid;
1571 	struct wpa_cred *cred;
1572 #ifndef CONFIG_NO_CONFIG_BLOBS
1573 	struct wpa_config_blob *blob;
1574 #endif /* CONFIG_NO_CONFIG_BLOBS */
1575 	int ret = 0;
1576 	const char *orig_name = name;
1577 	int tmp_len;
1578 	char *tmp_name;
1579 
1580 	if (!name) {
1581 		wpa_printf(MSG_ERROR, "No configuration file for writing");
1582 		return -1;
1583 	}
1584 
1585 	tmp_len = os_strlen(name) + 5; /* allow space for .tmp suffix */
1586 	tmp_name = os_malloc(tmp_len);
1587 	if (tmp_name) {
1588 		os_snprintf(tmp_name, tmp_len, "%s.tmp", name);
1589 		name = tmp_name;
1590 	}
1591 
1592 	wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
1593 
1594 	f = fopen(name, "w");
1595 	if (f == NULL) {
1596 		wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
1597 		os_free(tmp_name);
1598 		return -1;
1599 	}
1600 
1601 	wpa_config_write_global(f, config);
1602 
1603 	for (cred = config->cred; cred; cred = cred->next) {
1604 		if (cred->temporary)
1605 			continue;
1606 		fprintf(f, "\ncred={\n");
1607 		wpa_config_write_cred(f, cred);
1608 		fprintf(f, "}\n");
1609 	}
1610 
1611 	for (ssid = config->ssid; ssid; ssid = ssid->next) {
1612 		if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
1613 			continue; /* do not save temporary networks */
1614 		if (wpa_key_mgmt_wpa_psk_no_sae(ssid->key_mgmt) &&
1615 		    !ssid->psk_set && !ssid->passphrase)
1616 			continue; /* do not save invalid network */
1617 		if (wpa_key_mgmt_sae(ssid->key_mgmt) &&
1618 		    !ssid->passphrase && !ssid->sae_password)
1619 			continue; /* do not save invalid network */
1620 		fprintf(f, "\nnetwork={\n");
1621 		wpa_config_write_network(f, ssid);
1622 		fprintf(f, "}\n");
1623 	}
1624 
1625 #ifndef CONFIG_NO_CONFIG_BLOBS
1626 	for (blob = config->blobs; blob; blob = blob->next) {
1627 		ret = wpa_config_write_blob(f, blob);
1628 		if (ret)
1629 			break;
1630 	}
1631 #endif /* CONFIG_NO_CONFIG_BLOBS */
1632 
1633 	os_fdatasync(f);
1634 
1635 	fclose(f);
1636 
1637 	if (tmp_name) {
1638 		int chmod_ret = 0;
1639 
1640 #ifdef ANDROID
1641 		chmod_ret = chmod(tmp_name,
1642 				  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1643 #endif /* ANDROID */
1644 		if (chmod_ret != 0 || rename(tmp_name, orig_name) != 0)
1645 			ret = -1;
1646 
1647 		os_free(tmp_name);
1648 	}
1649 
1650 	wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
1651 		   orig_name, ret ? "un" : "");
1652 	return ret;
1653 #else /* CONFIG_NO_CONFIG_WRITE */
1654 	return -1;
1655 #endif /* CONFIG_NO_CONFIG_WRITE */
1656 }
1657