Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / wpa / wpa_supplicant / config_file.c
blobce9f0b62877453bc22d3c83bb17e4a33480fb990
1 /*
2 * WPA Supplicant / Configuration backend: text file
3 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
14 * This file implements a configuration backend for text files. All the
15 * configuration information is stored in a text file that uses a format
16 * described in the sample configuration file, wpa_supplicant.conf.
19 #include "includes.h"
21 #include "common.h"
22 #include "config.h"
23 #include "base64.h"
24 #include "eap_peer/eap_methods.h"
27 /**
28 * wpa_config_get_line - Read the next configuration file line
29 * @s: Buffer for the line
30 * @size: The buffer length
31 * @stream: File stream to read from
32 * @line: Pointer to a variable storing the file line number
33 * @_pos: Buffer for the pointer to the beginning of data on the text line or
34 * %NULL if not needed (returned value used instead)
35 * Returns: Pointer to the beginning of data on the text line or %NULL if no
36 * more text lines are available.
38 * This function reads the next non-empty line from the configuration file and
39 * removes comments. The returned string is guaranteed to be null-terminated.
41 static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
42 char **_pos)
44 char *pos, *end, *sstart;
46 while (fgets(s, size, stream)) {
47 (*line)++;
48 s[size - 1] = '\0';
49 pos = s;
51 /* Skip white space from the beginning of line. */
52 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
53 pos++;
55 /* Skip comment lines and empty lines */
56 if (*pos == '#' || *pos == '\n' || *pos == '\0')
57 continue;
60 * Remove # comments unless they are within a double quoted
61 * string.
63 sstart = os_strchr(pos, '"');
64 if (sstart)
65 sstart = os_strrchr(sstart + 1, '"');
66 if (!sstart)
67 sstart = pos;
68 end = os_strchr(sstart, '#');
69 if (end)
70 *end-- = '\0';
71 else
72 end = pos + os_strlen(pos) - 1;
74 /* Remove trailing white space. */
75 while (end > pos &&
76 (*end == '\n' || *end == ' ' || *end == '\t' ||
77 *end == '\r'))
78 *end-- = '\0';
80 if (*pos == '\0')
81 continue;
83 if (_pos)
84 *_pos = pos;
85 return pos;
88 if (_pos)
89 *_pos = NULL;
90 return NULL;
94 static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
96 int errors = 0;
98 if (ssid->passphrase) {
99 if (ssid->psk_set) {
100 wpa_printf(MSG_ERROR, "Line %d: both PSK and "
101 "passphrase configured.", line);
102 errors++;
104 wpa_config_update_psk(ssid);
107 if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK)) &&
108 !ssid->psk_set) {
109 wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
110 "management, but no PSK configured.", line);
111 errors++;
114 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
115 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
116 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
117 /* Group cipher cannot be stronger than the pairwise cipher. */
118 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
119 " list since it was not allowed for pairwise "
120 "cipher", line);
121 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
124 return errors;
128 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
130 struct wpa_ssid *ssid;
131 int errors = 0, end = 0;
132 char buf[256], *pos, *pos2;
134 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
135 *line);
136 ssid = os_zalloc(sizeof(*ssid));
137 if (ssid == NULL)
138 return NULL;
139 ssid->id = id;
141 wpa_config_set_network_defaults(ssid);
143 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
144 if (os_strcmp(pos, "}") == 0) {
145 end = 1;
146 break;
149 pos2 = os_strchr(pos, '=');
150 if (pos2 == NULL) {
151 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
152 "'%s'.", *line, pos);
153 errors++;
154 continue;
157 *pos2++ = '\0';
158 if (*pos2 == '"') {
159 if (os_strchr(pos2 + 1, '"') == NULL) {
160 wpa_printf(MSG_ERROR, "Line %d: invalid "
161 "quotation '%s'.", *line, pos2);
162 errors++;
163 continue;
167 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
168 errors++;
171 if (!end) {
172 wpa_printf(MSG_ERROR, "Line %d: network block was not "
173 "terminated properly.", *line);
174 errors++;
177 errors += wpa_config_validate_network(ssid, *line);
179 if (errors) {
180 wpa_config_free_ssid(ssid);
181 ssid = NULL;
184 return ssid;
188 #ifndef CONFIG_NO_CONFIG_BLOBS
189 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
190 const char *name)
192 struct wpa_config_blob *blob;
193 char buf[256], *pos;
194 unsigned char *encoded = NULL, *nencoded;
195 int end = 0;
196 size_t encoded_len = 0, len;
198 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
199 *line, name);
201 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
202 if (os_strcmp(pos, "}") == 0) {
203 end = 1;
204 break;
207 len = os_strlen(pos);
208 nencoded = os_realloc(encoded, encoded_len + len);
209 if (nencoded == NULL) {
210 wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
211 "blob", *line);
212 os_free(encoded);
213 return NULL;
215 encoded = nencoded;
216 os_memcpy(encoded + encoded_len, pos, len);
217 encoded_len += len;
220 if (!end) {
221 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
222 "properly", *line);
223 os_free(encoded);
224 return NULL;
227 blob = os_zalloc(sizeof(*blob));
228 if (blob == NULL) {
229 os_free(encoded);
230 return NULL;
232 blob->name = os_strdup(name);
233 blob->data = base64_decode(encoded, encoded_len, &blob->len);
234 os_free(encoded);
236 if (blob->name == NULL || blob->data == NULL) {
237 wpa_config_free_blob(blob);
238 return NULL;
241 return blob;
245 static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
246 int *line, char *bname)
248 char *name_end;
249 struct wpa_config_blob *blob;
251 name_end = os_strchr(bname, '=');
252 if (name_end == NULL) {
253 wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
254 *line);
255 return -1;
257 *name_end = '\0';
259 blob = wpa_config_read_blob(f, line, bname);
260 if (blob == NULL) {
261 wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
262 *line, bname);
263 return -1;
265 wpa_config_set_blob(config, blob);
266 return 0;
268 #endif /* CONFIG_NO_CONFIG_BLOBS */
271 #ifdef CONFIG_CTRL_IFACE
272 static int wpa_config_process_ctrl_interface(struct wpa_config *config,
273 char *pos)
275 os_free(config->ctrl_interface);
276 config->ctrl_interface = os_strdup(pos);
277 wpa_printf(MSG_DEBUG, "ctrl_interface='%s'", config->ctrl_interface);
278 return 0;
282 static int wpa_config_process_ctrl_interface_group(struct wpa_config *config,
283 char *pos)
285 os_free(config->ctrl_interface_group);
286 config->ctrl_interface_group = os_strdup(pos);
287 wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' (DEPRECATED)",
288 config->ctrl_interface_group);
289 return 0;
291 #endif /* CONFIG_CTRL_IFACE */
294 static int wpa_config_process_eapol_version(struct wpa_config *config,
295 int line, char *pos)
297 config->eapol_version = atoi(pos);
298 if (config->eapol_version < 1 || config->eapol_version > 2) {
299 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): "
300 "'%s'.", line, config->eapol_version, pos);
301 return -1;
303 wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version);
304 return 0;
308 static int wpa_config_process_ap_scan(struct wpa_config *config, char *pos)
310 config->ap_scan = atoi(pos);
311 wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
312 return 0;
316 static int wpa_config_process_fast_reauth(struct wpa_config *config, char *pos)
318 config->fast_reauth = atoi(pos);
319 wpa_printf(MSG_DEBUG, "fast_reauth=%d", config->fast_reauth);
320 return 0;
324 #ifdef EAP_TLS_OPENSSL
326 static int wpa_config_process_opensc_engine_path(struct wpa_config *config,
327 char *pos)
329 os_free(config->opensc_engine_path);
330 config->opensc_engine_path = os_strdup(pos);
331 wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
332 config->opensc_engine_path);
333 return 0;
337 static int wpa_config_process_pkcs11_engine_path(struct wpa_config *config,
338 char *pos)
340 os_free(config->pkcs11_engine_path);
341 config->pkcs11_engine_path = os_strdup(pos);
342 wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
343 config->pkcs11_engine_path);
344 return 0;
348 static int wpa_config_process_pkcs11_module_path(struct wpa_config *config,
349 char *pos)
351 os_free(config->pkcs11_module_path);
352 config->pkcs11_module_path = os_strdup(pos);
353 wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
354 config->pkcs11_module_path);
355 return 0;
358 #endif /* EAP_TLS_OPENSSL */
361 static int wpa_config_process_driver_param(struct wpa_config *config,
362 char *pos)
364 os_free(config->driver_param);
365 config->driver_param = os_strdup(pos);
366 wpa_printf(MSG_DEBUG, "driver_param='%s'", config->driver_param);
367 return 0;
371 static int wpa_config_process_pmk_lifetime(struct wpa_config *config,
372 char *pos)
374 config->dot11RSNAConfigPMKLifetime = atoi(pos);
375 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
376 config->dot11RSNAConfigPMKLifetime);
377 return 0;
381 static int wpa_config_process_pmk_reauth_threshold(struct wpa_config *config,
382 char *pos)
384 config->dot11RSNAConfigPMKReauthThreshold = atoi(pos);
385 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKReauthThreshold=%d",
386 config->dot11RSNAConfigPMKReauthThreshold);
387 return 0;
391 static int wpa_config_process_sa_timeout(struct wpa_config *config, char *pos)
393 config->dot11RSNAConfigSATimeout = atoi(pos);
394 wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
395 config->dot11RSNAConfigSATimeout);
396 return 0;
400 #ifndef CONFIG_NO_CONFIG_WRITE
401 static int wpa_config_process_update_config(struct wpa_config *config,
402 char *pos)
404 config->update_config = atoi(pos);
405 wpa_printf(MSG_DEBUG, "update_config=%d", config->update_config);
406 return 0;
408 #endif /* CONFIG_NO_CONFIG_WRITE */
411 static int wpa_config_process_load_dynamic_eap(int line, char *so)
413 int ret;
414 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
415 ret = eap_peer_method_load(so);
416 if (ret == -2) {
417 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
418 "reloading.");
419 } else if (ret) {
420 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
421 "method '%s'.", line, so);
422 return -1;
425 return 0;
429 static int wpa_config_process_global(struct wpa_config *config, char *pos,
430 int line)
432 #ifdef CONFIG_CTRL_IFACE
433 if (os_strncmp(pos, "ctrl_interface=", 15) == 0)
434 return wpa_config_process_ctrl_interface(config, pos + 15);
436 if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0)
437 return wpa_config_process_ctrl_interface_group(config,
438 pos + 21);
439 #endif /* CONFIG_CTRL_IFACE */
441 if (os_strncmp(pos, "eapol_version=", 14) == 0)
442 return wpa_config_process_eapol_version(config, line,
443 pos + 14);
445 if (os_strncmp(pos, "ap_scan=", 8) == 0)
446 return wpa_config_process_ap_scan(config, pos + 8);
448 if (os_strncmp(pos, "fast_reauth=", 12) == 0)
449 return wpa_config_process_fast_reauth(config, pos + 12);
451 #ifdef EAP_TLS_OPENSSL
452 if (os_strncmp(pos, "opensc_engine_path=", 19) == 0)
453 return wpa_config_process_opensc_engine_path(config, pos + 19);
455 if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0)
456 return wpa_config_process_pkcs11_engine_path(config, pos + 19);
458 if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0)
459 return wpa_config_process_pkcs11_module_path(config, pos + 19);
460 #endif /* EAP_TLS_OPENSSL */
462 if (os_strncmp(pos, "driver_param=", 13) == 0)
463 return wpa_config_process_driver_param(config, pos + 13);
465 if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) == 0)
466 return wpa_config_process_pmk_lifetime(config, pos + 27);
468 if (os_strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=", 34) == 0)
469 return wpa_config_process_pmk_reauth_threshold(config,
470 pos + 34);
472 if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) == 0)
473 return wpa_config_process_sa_timeout(config, pos + 25);
475 #ifndef CONFIG_NO_CONFIG_WRITE
476 if (os_strncmp(pos, "update_config=", 14) == 0)
477 return wpa_config_process_update_config(config, pos + 14);
478 #endif /* CONFIG_NO_CONFIG_WRITE */
480 if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0)
481 return wpa_config_process_load_dynamic_eap(line, pos + 17);
483 return -1;
487 struct wpa_config * wpa_config_read(const char *name)
489 FILE *f;
490 char buf[256], *pos;
491 int errors = 0, line = 0;
492 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
493 struct wpa_config *config;
494 int id = 0;
496 config = wpa_config_alloc_empty(NULL, NULL);
497 if (config == NULL)
498 return NULL;
499 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
500 f = fopen(name, "r");
501 if (f == NULL) {
502 os_free(config);
503 return NULL;
506 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
507 if (os_strcmp(pos, "network={") == 0) {
508 ssid = wpa_config_read_network(f, &line, id++);
509 if (ssid == NULL) {
510 wpa_printf(MSG_ERROR, "Line %d: failed to "
511 "parse network block.", line);
512 errors++;
513 continue;
515 if (head == NULL) {
516 head = tail = ssid;
517 } else {
518 tail->next = ssid;
519 tail = ssid;
521 if (wpa_config_add_prio_network(config, ssid)) {
522 wpa_printf(MSG_ERROR, "Line %d: failed to add "
523 "network block to priority list.",
524 line);
525 errors++;
526 continue;
528 #ifndef CONFIG_NO_CONFIG_BLOBS
529 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
530 if (wpa_config_process_blob(config, f, &line, pos + 12)
531 < 0) {
532 errors++;
533 continue;
535 #endif /* CONFIG_NO_CONFIG_BLOBS */
536 } else if (wpa_config_process_global(config, pos, line) < 0) {
537 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
538 "line '%s'.", line, pos);
539 errors++;
540 continue;
544 fclose(f);
546 config->ssid = head;
547 wpa_config_debug_dump_networks(config);
549 if (errors) {
550 wpa_config_free(config);
551 config = NULL;
552 head = NULL;
555 return config;
559 #ifndef CONFIG_NO_CONFIG_WRITE
561 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
563 char *value = wpa_config_get(ssid, field);
564 if (value == NULL)
565 return;
566 fprintf(f, "\t%s=%s\n", field, value);
567 os_free(value);
571 static void write_int(FILE *f, const char *field, int value, int def)
573 if (value == def)
574 return;
575 fprintf(f, "\t%s=%d\n", field, value);
579 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
581 char *value = wpa_config_get(ssid, "bssid");
582 if (value == NULL)
583 return;
584 fprintf(f, "\tbssid=%s\n", value);
585 os_free(value);
589 static void write_psk(FILE *f, struct wpa_ssid *ssid)
591 char *value = wpa_config_get(ssid, "psk");
592 if (value == NULL)
593 return;
594 fprintf(f, "\tpsk=%s\n", value);
595 os_free(value);
599 static void write_proto(FILE *f, struct wpa_ssid *ssid)
601 char *value;
603 if (ssid->proto == DEFAULT_PROTO)
604 return;
606 value = wpa_config_get(ssid, "proto");
607 if (value == NULL)
608 return;
609 if (value[0])
610 fprintf(f, "\tproto=%s\n", value);
611 os_free(value);
615 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
617 char *value;
619 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
620 return;
622 value = wpa_config_get(ssid, "key_mgmt");
623 if (value == NULL)
624 return;
625 if (value[0])
626 fprintf(f, "\tkey_mgmt=%s\n", value);
627 os_free(value);
631 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
633 char *value;
635 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
636 return;
638 value = wpa_config_get(ssid, "pairwise");
639 if (value == NULL)
640 return;
641 if (value[0])
642 fprintf(f, "\tpairwise=%s\n", value);
643 os_free(value);
647 static void write_group(FILE *f, struct wpa_ssid *ssid)
649 char *value;
651 if (ssid->group_cipher == DEFAULT_GROUP)
652 return;
654 value = wpa_config_get(ssid, "group");
655 if (value == NULL)
656 return;
657 if (value[0])
658 fprintf(f, "\tgroup=%s\n", value);
659 os_free(value);
663 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
665 char *value;
667 if (ssid->auth_alg == 0)
668 return;
670 value = wpa_config_get(ssid, "auth_alg");
671 if (value == NULL)
672 return;
673 if (value[0])
674 fprintf(f, "\tauth_alg=%s\n", value);
675 os_free(value);
679 #ifdef IEEE8021X_EAPOL
680 static void write_eap(FILE *f, struct wpa_ssid *ssid)
682 char *value;
684 value = wpa_config_get(ssid, "eap");
685 if (value == NULL)
686 return;
688 if (value[0])
689 fprintf(f, "\teap=%s\n", value);
690 os_free(value);
692 #endif /* IEEE8021X_EAPOL */
695 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
697 char field[20], *value;
698 int res;
700 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
701 if (res < 0 || (size_t) res >= sizeof(field))
702 return;
703 value = wpa_config_get(ssid, field);
704 if (value) {
705 fprintf(f, "\t%s=%s\n", field, value);
706 os_free(value);
711 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
713 int i;
715 #define STR(t) write_str(f, #t, ssid)
716 #define INT(t) write_int(f, #t, ssid->t, 0)
717 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
718 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
719 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
721 STR(ssid);
722 INT(scan_ssid);
723 write_bssid(f, ssid);
724 write_psk(f, ssid);
725 write_proto(f, ssid);
726 write_key_mgmt(f, ssid);
727 write_pairwise(f, ssid);
728 write_group(f, ssid);
729 write_auth_alg(f, ssid);
730 #ifdef IEEE8021X_EAPOL
731 write_eap(f, ssid);
732 STR(identity);
733 STR(anonymous_identity);
734 STR(password);
735 STR(ca_cert);
736 STR(ca_path);
737 STR(client_cert);
738 STR(private_key);
739 STR(private_key_passwd);
740 STR(dh_file);
741 STR(subject_match);
742 STR(altsubject_match);
743 STR(ca_cert2);
744 STR(ca_path2);
745 STR(client_cert2);
746 STR(private_key2);
747 STR(private_key2_passwd);
748 STR(dh_file2);
749 STR(subject_match2);
750 STR(altsubject_match2);
751 STR(phase1);
752 STR(phase2);
753 STR(pcsc);
754 STR(pin);
755 STR(engine_id);
756 STR(key_id);
757 STR(cert_id);
758 STR(ca_cert_id);
759 STR(key2_id);
760 STR(cert2_id);
761 STR(ca_cert2_id);
762 INTe(engine);
763 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
764 #endif /* IEEE8021X_EAPOL */
765 for (i = 0; i < 4; i++)
766 write_wep_key(f, i, ssid);
767 INT(wep_tx_keyidx);
768 INT(priority);
769 #ifdef IEEE8021X_EAPOL
770 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
771 STR(pac_file);
772 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
773 #endif /* IEEE8021X_EAPOL */
774 INT(mode);
775 INT(proactive_key_caching);
776 INT(disabled);
777 INT(peerkey);
778 #ifdef CONFIG_IEEE80211W
779 INT(ieee80211w);
780 #endif /* CONFIG_IEEE80211W */
781 STR(id_str);
783 #undef STR
784 #undef INT
785 #undef INT_DEF
789 #ifndef CONFIG_NO_CONFIG_BLOBS
790 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
792 unsigned char *encoded;
794 encoded = base64_encode(blob->data, blob->len, NULL);
795 if (encoded == NULL)
796 return -1;
798 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
799 os_free(encoded);
800 return 0;
802 #endif /* CONFIG_NO_CONFIG_BLOBS */
805 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
807 #ifdef CONFIG_CTRL_IFACE
808 if (config->ctrl_interface)
809 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
810 if (config->ctrl_interface_group)
811 fprintf(f, "ctrl_interface_group=%s\n",
812 config->ctrl_interface_group);
813 #endif /* CONFIG_CTRL_IFACE */
814 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
815 fprintf(f, "eapol_version=%d\n", config->eapol_version);
816 if (config->ap_scan != DEFAULT_AP_SCAN)
817 fprintf(f, "ap_scan=%d\n", config->ap_scan);
818 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
819 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
820 #ifdef EAP_TLS_OPENSSL
821 if (config->opensc_engine_path)
822 fprintf(f, "opensc_engine_path=%s\n",
823 config->opensc_engine_path);
824 if (config->pkcs11_engine_path)
825 fprintf(f, "pkcs11_engine_path=%s\n",
826 config->pkcs11_engine_path);
827 if (config->pkcs11_module_path)
828 fprintf(f, "pkcs11_module_path=%s\n",
829 config->pkcs11_module_path);
830 #endif /* EAP_TLS_OPENSSL */
831 if (config->driver_param)
832 fprintf(f, "driver_param=%s\n", config->driver_param);
833 if (config->dot11RSNAConfigPMKLifetime)
834 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
835 config->dot11RSNAConfigPMKLifetime);
836 if (config->dot11RSNAConfigPMKReauthThreshold)
837 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
838 config->dot11RSNAConfigPMKReauthThreshold);
839 if (config->dot11RSNAConfigSATimeout)
840 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
841 config->dot11RSNAConfigSATimeout);
842 if (config->update_config)
843 fprintf(f, "update_config=%d\n", config->update_config);
846 #endif /* CONFIG_NO_CONFIG_WRITE */
849 int wpa_config_write(const char *name, struct wpa_config *config)
851 #ifndef CONFIG_NO_CONFIG_WRITE
852 FILE *f;
853 struct wpa_ssid *ssid;
854 #ifndef CONFIG_NO_CONFIG_BLOBS
855 struct wpa_config_blob *blob;
856 #endif /* CONFIG_NO_CONFIG_BLOBS */
857 int ret = 0;
859 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
861 f = fopen(name, "w");
862 if (f == NULL) {
863 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
864 return -1;
867 wpa_config_write_global(f, config);
869 for (ssid = config->ssid; ssid; ssid = ssid->next) {
870 fprintf(f, "\nnetwork={\n");
871 wpa_config_write_network(f, ssid);
872 fprintf(f, "}\n");
875 #ifndef CONFIG_NO_CONFIG_BLOBS
876 for (blob = config->blobs; blob; blob = blob->next) {
877 ret = wpa_config_write_blob(f, blob);
878 if (ret)
879 break;
881 #endif /* CONFIG_NO_CONFIG_BLOBS */
883 fclose(f);
885 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
886 name, ret ? "un" : "");
887 return ret;
888 #else /* CONFIG_NO_CONFIG_WRITE */
889 return -1;
890 #endif /* CONFIG_NO_CONFIG_WRITE */