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