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
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.
24 #include "eap_peer/eap_methods.h"
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
,
44 char *pos
, *end
, *sstart
;
46 while (fgets(s
, size
, stream
)) {
51 /* Skip white space from the beginning of line. */
52 while (*pos
== ' ' || *pos
== '\t' || *pos
== '\r')
55 /* Skip comment lines and empty lines */
56 if (*pos
== '#' || *pos
== '\n' || *pos
== '\0')
60 * Remove # comments unless they are within a double quoted
63 sstart
= os_strchr(pos
, '"');
65 sstart
= os_strrchr(sstart
+ 1, '"');
68 end
= os_strchr(sstart
, '#');
72 end
= pos
+ os_strlen(pos
) - 1;
74 /* Remove trailing white space. */
76 (*end
== '\n' || *end
== ' ' || *end
== '\t' ||
94 static int wpa_config_validate_network(struct wpa_ssid
*ssid
, int line
)
98 if (ssid
->passphrase
) {
100 wpa_printf(MSG_ERROR
, "Line %d: both PSK and "
101 "passphrase configured.", line
);
104 wpa_config_update_psk(ssid
);
107 if ((ssid
->key_mgmt
& (WPA_KEY_MGMT_PSK
| WPA_KEY_MGMT_FT_PSK
)) &&
109 wpa_printf(MSG_ERROR
, "Line %d: WPA-PSK accepted for key "
110 "management, but no PSK configured.", line
);
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 "
121 ssid
->group_cipher
&= ~WPA_CIPHER_CCMP
;
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",
136 ssid
= os_zalloc(sizeof(*ssid
));
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) {
149 pos2
= os_strchr(pos
, '=');
151 wpa_printf(MSG_ERROR
, "Line %d: Invalid SSID line "
152 "'%s'.", *line
, pos
);
159 if (os_strchr(pos2
+ 1, '"') == NULL
) {
160 wpa_printf(MSG_ERROR
, "Line %d: invalid "
161 "quotation '%s'.", *line
, pos2
);
167 if (wpa_config_set(ssid
, pos
, pos2
, *line
) < 0)
172 wpa_printf(MSG_ERROR
, "Line %d: network block was not "
173 "terminated properly.", *line
);
177 errors
+= wpa_config_validate_network(ssid
, *line
);
180 wpa_config_free_ssid(ssid
);
188 #ifndef CONFIG_NO_CONFIG_BLOBS
189 static struct wpa_config_blob
* wpa_config_read_blob(FILE *f
, int *line
,
192 struct wpa_config_blob
*blob
;
194 unsigned char *encoded
= NULL
, *nencoded
;
196 size_t encoded_len
= 0, len
;
198 wpa_printf(MSG_MSGDUMP
, "Line: %d - start of a new named blob '%s'",
201 while (wpa_config_get_line(buf
, sizeof(buf
), f
, line
, &pos
)) {
202 if (os_strcmp(pos
, "}") == 0) {
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 "
216 os_memcpy(encoded
+ encoded_len
, pos
, len
);
221 wpa_printf(MSG_ERROR
, "Line %d: blob was not terminated "
227 blob
= os_zalloc(sizeof(*blob
));
232 blob
->name
= os_strdup(name
);
233 blob
->data
= base64_decode(encoded
, encoded_len
, &blob
->len
);
236 if (blob
->name
== NULL
|| blob
->data
== NULL
) {
237 wpa_config_free_blob(blob
);
245 static int wpa_config_process_blob(struct wpa_config
*config
, FILE *f
,
246 int *line
, char *bname
)
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",
259 blob
= wpa_config_read_blob(f
, line
, bname
);
261 wpa_printf(MSG_ERROR
, "Line %d: failed to read blob %s",
265 wpa_config_set_blob(config
, blob
);
268 #endif /* CONFIG_NO_CONFIG_BLOBS */
271 #ifdef CONFIG_CTRL_IFACE
272 static int wpa_config_process_ctrl_interface(struct wpa_config
*config
,
275 os_free(config
->ctrl_interface
);
276 config
->ctrl_interface
= os_strdup(pos
);
277 wpa_printf(MSG_DEBUG
, "ctrl_interface='%s'", config
->ctrl_interface
);
282 static int wpa_config_process_ctrl_interface_group(struct wpa_config
*config
,
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
);
291 #endif /* CONFIG_CTRL_IFACE */
294 static int wpa_config_process_eapol_version(struct wpa_config
*config
,
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
);
303 wpa_printf(MSG_DEBUG
, "eapol_version=%d", config
->eapol_version
);
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
);
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
);
324 #ifdef EAP_TLS_OPENSSL
326 static int wpa_config_process_opensc_engine_path(struct wpa_config
*config
,
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
);
337 static int wpa_config_process_pkcs11_engine_path(struct wpa_config
*config
,
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
);
348 static int wpa_config_process_pkcs11_module_path(struct wpa_config
*config
,
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
);
358 #endif /* EAP_TLS_OPENSSL */
361 static int wpa_config_process_driver_param(struct wpa_config
*config
,
364 os_free(config
->driver_param
);
365 config
->driver_param
= os_strdup(pos
);
366 wpa_printf(MSG_DEBUG
, "driver_param='%s'", config
->driver_param
);
371 static int wpa_config_process_pmk_lifetime(struct wpa_config
*config
,
374 config
->dot11RSNAConfigPMKLifetime
= atoi(pos
);
375 wpa_printf(MSG_DEBUG
, "dot11RSNAConfigPMKLifetime=%d",
376 config
->dot11RSNAConfigPMKLifetime
);
381 static int wpa_config_process_pmk_reauth_threshold(struct wpa_config
*config
,
384 config
->dot11RSNAConfigPMKReauthThreshold
= atoi(pos
);
385 wpa_printf(MSG_DEBUG
, "dot11RSNAConfigPMKReauthThreshold=%d",
386 config
->dot11RSNAConfigPMKReauthThreshold
);
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
);
400 #ifndef CONFIG_NO_CONFIG_WRITE
401 static int wpa_config_process_update_config(struct wpa_config
*config
,
404 config
->update_config
= atoi(pos
);
405 wpa_printf(MSG_DEBUG
, "update_config=%d", config
->update_config
);
408 #endif /* CONFIG_NO_CONFIG_WRITE */
411 static int wpa_config_process_load_dynamic_eap(int line
, char *so
)
414 wpa_printf(MSG_DEBUG
, "load_dynamic_eap=%s", so
);
415 ret
= eap_peer_method_load(so
);
417 wpa_printf(MSG_DEBUG
, "This EAP type was already loaded - not "
420 wpa_printf(MSG_ERROR
, "Line %d: Failed to load dynamic EAP "
421 "method '%s'.", line
, so
);
429 static int wpa_config_process_global(struct wpa_config
*config
, char *pos
,
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
,
439 #endif /* CONFIG_CTRL_IFACE */
441 if (os_strncmp(pos
, "eapol_version=", 14) == 0)
442 return wpa_config_process_eapol_version(config
, line
,
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
,
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);
487 struct wpa_config
* wpa_config_read(const char *name
)
491 int errors
= 0, line
= 0;
492 struct wpa_ssid
*ssid
, *tail
= NULL
, *head
= NULL
;
493 struct wpa_config
*config
;
496 config
= wpa_config_alloc_empty(NULL
, NULL
);
499 wpa_printf(MSG_DEBUG
, "Reading configuration file '%s'", name
);
500 f
= fopen(name
, "r");
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
++);
510 wpa_printf(MSG_ERROR
, "Line %d: failed to "
511 "parse network block.", line
);
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.",
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)
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
);
547 wpa_config_debug_dump_networks(config
);
550 wpa_config_free(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
);
566 fprintf(f
, "\t%s=%s\n", field
, value
);
571 static void write_int(FILE *f
, const char *field
, int value
, int def
)
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");
584 fprintf(f
, "\tbssid=%s\n", value
);
589 static void write_psk(FILE *f
, struct wpa_ssid
*ssid
)
591 char *value
= wpa_config_get(ssid
, "psk");
594 fprintf(f
, "\tpsk=%s\n", value
);
599 static void write_proto(FILE *f
, struct wpa_ssid
*ssid
)
603 if (ssid
->proto
== DEFAULT_PROTO
)
606 value
= wpa_config_get(ssid
, "proto");
610 fprintf(f
, "\tproto=%s\n", value
);
615 static void write_key_mgmt(FILE *f
, struct wpa_ssid
*ssid
)
619 if (ssid
->key_mgmt
== DEFAULT_KEY_MGMT
)
622 value
= wpa_config_get(ssid
, "key_mgmt");
626 fprintf(f
, "\tkey_mgmt=%s\n", value
);
631 static void write_pairwise(FILE *f
, struct wpa_ssid
*ssid
)
635 if (ssid
->pairwise_cipher
== DEFAULT_PAIRWISE
)
638 value
= wpa_config_get(ssid
, "pairwise");
642 fprintf(f
, "\tpairwise=%s\n", value
);
647 static void write_group(FILE *f
, struct wpa_ssid
*ssid
)
651 if (ssid
->group_cipher
== DEFAULT_GROUP
)
654 value
= wpa_config_get(ssid
, "group");
658 fprintf(f
, "\tgroup=%s\n", value
);
663 static void write_auth_alg(FILE *f
, struct wpa_ssid
*ssid
)
667 if (ssid
->auth_alg
== 0)
670 value
= wpa_config_get(ssid
, "auth_alg");
674 fprintf(f
, "\tauth_alg=%s\n", value
);
679 #ifdef IEEE8021X_EAPOL
680 static void write_eap(FILE *f
, struct wpa_ssid
*ssid
)
684 value
= wpa_config_get(ssid
, "eap");
689 fprintf(f
, "\teap=%s\n", value
);
692 #endif /* IEEE8021X_EAPOL */
695 static void write_wep_key(FILE *f
, int idx
, struct wpa_ssid
*ssid
)
697 char field
[20], *value
;
700 res
= os_snprintf(field
, sizeof(field
), "wep_key%d", idx
);
701 if (res
< 0 || (size_t) res
>= sizeof(field
))
703 value
= wpa_config_get(ssid
, field
);
705 fprintf(f
, "\t%s=%s\n", field
, value
);
711 static void wpa_config_write_network(FILE *f
, struct wpa_ssid
*ssid
)
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)
723 write_bssid(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
733 STR(anonymous_identity
);
739 STR(private_key_passwd
);
742 STR(altsubject_match
);
747 STR(private_key2_passwd
);
750 STR(altsubject_match2
);
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
);
769 #ifdef IEEE8021X_EAPOL
770 INT_DEF(eap_workaround
, DEFAULT_EAP_WORKAROUND
);
772 INT_DEFe(fragment_size
, DEFAULT_FRAGMENT_SIZE
);
773 #endif /* IEEE8021X_EAPOL */
775 INT(proactive_key_caching
);
778 #ifdef CONFIG_IEEE80211W
780 #endif /* CONFIG_IEEE80211W */
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
);
798 fprintf(f
, "\nblob-base64-%s={\n%s}\n", blob
->name
, encoded
);
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
853 struct wpa_ssid
*ssid
;
854 #ifndef CONFIG_NO_CONFIG_BLOBS
855 struct wpa_config_blob
*blob
;
856 #endif /* CONFIG_NO_CONFIG_BLOBS */
859 wpa_printf(MSG_DEBUG
, "Writing configuration file '%s'", name
);
861 f
= fopen(name
, "w");
863 wpa_printf(MSG_DEBUG
, "Failed to open '%s' for writing", name
);
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
);
875 #ifndef CONFIG_NO_CONFIG_BLOBS
876 for (blob
= config
->blobs
; blob
; blob
= blob
->next
) {
877 ret
= wpa_config_write_blob(f
, blob
);
881 #endif /* CONFIG_NO_CONFIG_BLOBS */
885 wpa_printf(MSG_DEBUG
, "Configuration file '%s' written %ssuccessfully",
886 name
, ret
? "un" : "");
888 #else /* CONFIG_NO_CONFIG_WRITE */
890 #endif /* CONFIG_NO_CONFIG_WRITE */