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