2 * WPA Supplicant / Configuration backend: text file
3 * Copyright (c) 2003-2008, 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 |
108 WPA_KEY_MGMT_PSK_SHA256)) &&
110 wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
111 "management, but no PSK configured.", line);
115 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
116 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
117 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
118 /* Group cipher cannot be stronger than the pairwise cipher. */
119 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
120 " list since it was not allowed for pairwise "
122 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
129 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
131 struct wpa_ssid *ssid;
132 int errors = 0, end = 0;
133 char buf[256], *pos, *pos2;
135 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
137 ssid = os_zalloc(sizeof(*ssid));
142 wpa_config_set_network_defaults(ssid);
144 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
145 if (os_strcmp(pos, "}") == 0) {
150 pos2 = os_strchr(pos, '=');
152 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
153 "'%s'.", *line, pos);
160 if (os_strchr(pos2 + 1, '"') == NULL) {
161 wpa_printf(MSG_ERROR, "Line %d: invalid "
162 "quotation '%s'.", *line, pos2);
168 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
173 wpa_printf(MSG_ERROR, "Line %d: network block was not "
174 "terminated properly.", *line);
178 errors += wpa_config_validate_network(ssid, *line);
181 wpa_config_free_ssid(ssid);
189 #ifndef CONFIG_NO_CONFIG_BLOBS
190 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
193 struct wpa_config_blob *blob;
195 unsigned char *encoded = NULL, *nencoded;
197 size_t encoded_len = 0, len;
199 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
202 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
203 if (os_strcmp(pos, "}") == 0) {
208 len = os_strlen(pos);
209 nencoded = os_realloc(encoded, encoded_len + len);
210 if (nencoded == NULL) {
211 wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
217 os_memcpy(encoded + encoded_len, pos, len);
222 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
228 blob = os_zalloc(sizeof(*blob));
233 blob->name = os_strdup(name);
234 blob->data = base64_decode(encoded, encoded_len, &blob->len);
237 if (blob->name == NULL || blob->data == NULL) {
238 wpa_config_free_blob(blob);
246 static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
247 int *line, char *bname)
250 struct wpa_config_blob *blob;
252 name_end = os_strchr(bname, '=');
253 if (name_end == NULL) {
254 wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
260 blob = wpa_config_read_blob(f, line, bname);
262 wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
266 wpa_config_set_blob(config, blob);
269 #endif /* CONFIG_NO_CONFIG_BLOBS */
272 #ifdef CONFIG_CTRL_IFACE
273 static int wpa_config_process_ctrl_interface(struct wpa_config *config,
276 os_free(config->ctrl_interface);
277 config->ctrl_interface = os_strdup(pos);
278 wpa_printf(MSG_DEBUG, "ctrl_interface='%s'", config->ctrl_interface);
283 static int wpa_config_process_ctrl_interface_group(struct wpa_config *config,
286 os_free(config->ctrl_interface_group);
287 config->ctrl_interface_group = os_strdup(pos);
288 wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' (DEPRECATED)",
289 config->ctrl_interface_group);
292 #endif /* CONFIG_CTRL_IFACE */
295 static int wpa_config_process_eapol_version(struct wpa_config *config,
298 config->eapol_version = atoi(pos);
299 if (config->eapol_version < 1 || config->eapol_version > 2) {
300 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): "
301 "'%s'.", line, config->eapol_version, pos);
304 wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version);
309 static int wpa_config_process_ap_scan(struct wpa_config *config, char *pos)
311 config->ap_scan = atoi(pos);
312 wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
317 static int wpa_config_process_fast_reauth(struct wpa_config *config, char *pos)
319 config->fast_reauth = atoi(pos);
320 wpa_printf(MSG_DEBUG, "fast_reauth=%d", config->fast_reauth);
325 #ifdef EAP_TLS_OPENSSL
327 static int wpa_config_process_opensc_engine_path(struct wpa_config *config,
330 os_free(config->opensc_engine_path);
331 config->opensc_engine_path = os_strdup(pos);
332 wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
333 config->opensc_engine_path);
338 static int wpa_config_process_pkcs11_engine_path(struct wpa_config *config,
341 os_free(config->pkcs11_engine_path);
342 config->pkcs11_engine_path = os_strdup(pos);
343 wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
344 config->pkcs11_engine_path);
349 static int wpa_config_process_pkcs11_module_path(struct wpa_config *config,
352 os_free(config->pkcs11_module_path);
353 config->pkcs11_module_path = os_strdup(pos);
354 wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
355 config->pkcs11_module_path);
359 #endif /* EAP_TLS_OPENSSL */
362 static int wpa_config_process_driver_param(struct wpa_config *config,
365 os_free(config->driver_param);
366 config->driver_param = os_strdup(pos);
367 wpa_printf(MSG_DEBUG, "driver_param='%s'", config->driver_param);
372 static int wpa_config_process_pmk_lifetime(struct wpa_config *config,
375 config->dot11RSNAConfigPMKLifetime = atoi(pos);
376 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
377 config->dot11RSNAConfigPMKLifetime);
382 static int wpa_config_process_pmk_reauth_threshold(struct wpa_config *config,
385 config->dot11RSNAConfigPMKReauthThreshold = atoi(pos);
386 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKReauthThreshold=%d",
387 config->dot11RSNAConfigPMKReauthThreshold);
392 static int wpa_config_process_sa_timeout(struct wpa_config *config, char *pos)
394 config->dot11RSNAConfigSATimeout = atoi(pos);
395 wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
396 config->dot11RSNAConfigSATimeout);
401 #ifndef CONFIG_NO_CONFIG_WRITE
402 static int wpa_config_process_update_config(struct wpa_config *config,
405 config->update_config = atoi(pos);
406 wpa_printf(MSG_DEBUG, "update_config=%d", config->update_config);
409 #endif /* CONFIG_NO_CONFIG_WRITE */
412 static int wpa_config_process_load_dynamic_eap(int line, char *so)
415 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
416 ret = eap_peer_method_load(so);
418 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
421 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
422 "method '%s'.", line, so);
430 static int wpa_config_process_global(struct wpa_config *config, char *pos,
433 #ifdef CONFIG_CTRL_IFACE
434 if (os_strncmp(pos, "ctrl_interface=", 15) == 0)
435 return wpa_config_process_ctrl_interface(config, pos + 15);
437 if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0)
438 return wpa_config_process_ctrl_interface_group(config,
440 #endif /* CONFIG_CTRL_IFACE */
442 if (os_strncmp(pos, "eapol_version=", 14) == 0)
443 return wpa_config_process_eapol_version(config, line,
446 if (os_strncmp(pos, "ap_scan=", 8) == 0)
447 return wpa_config_process_ap_scan(config, pos + 8);
449 if (os_strncmp(pos, "fast_reauth=", 12) == 0)
450 return wpa_config_process_fast_reauth(config, pos + 12);
452 #ifdef EAP_TLS_OPENSSL
453 if (os_strncmp(pos, "opensc_engine_path=", 19) == 0)
454 return wpa_config_process_opensc_engine_path(config, pos + 19);
456 if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0)
457 return wpa_config_process_pkcs11_engine_path(config, pos + 19);
459 if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0)
460 return wpa_config_process_pkcs11_module_path(config, pos + 19);
461 #endif /* EAP_TLS_OPENSSL */
463 if (os_strncmp(pos, "driver_param=", 13) == 0)
464 return wpa_config_process_driver_param(config, pos + 13);
466 if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) == 0)
467 return wpa_config_process_pmk_lifetime(config, pos + 27);
469 if (os_strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=", 34) == 0)
470 return wpa_config_process_pmk_reauth_threshold(config,
473 if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) == 0)
474 return wpa_config_process_sa_timeout(config, pos + 25);
476 #ifndef CONFIG_NO_CONFIG_WRITE
477 if (os_strncmp(pos, "update_config=", 14) == 0)
478 return wpa_config_process_update_config(config, pos + 14);
479 #endif /* CONFIG_NO_CONFIG_WRITE */
481 if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0)
482 return wpa_config_process_load_dynamic_eap(line, pos + 17);
488 struct wpa_config * wpa_config_read(const char *name)
492 int errors = 0, line = 0;
493 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
494 struct wpa_config *config;
497 config = wpa_config_alloc_empty(NULL, NULL);
500 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
501 f = fopen(name, "r");
507 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
508 if (os_strcmp(pos, "network={") == 0) {
509 ssid = wpa_config_read_network(f, &line, id++);
511 wpa_printf(MSG_ERROR, "Line %d: failed to "
512 "parse network block.", line);
522 if (wpa_config_add_prio_network(config, ssid)) {
523 wpa_printf(MSG_ERROR, "Line %d: failed to add "
524 "network block to priority list.",
529 #ifndef CONFIG_NO_CONFIG_BLOBS
530 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
531 if (wpa_config_process_blob(config, f, &line, pos + 12)
536 #endif /* CONFIG_NO_CONFIG_BLOBS */
537 } else if (wpa_config_process_global(config, pos, line) < 0) {
538 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
539 "line '%s'.", line, pos);
548 wpa_config_debug_dump_networks(config);
551 wpa_config_free(config);
560 #ifndef CONFIG_NO_CONFIG_WRITE
562 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
564 char *value = wpa_config_get(ssid, field);
567 fprintf(f, "\t%s=%s\n", field, value);
572 static void write_int(FILE *f, const char *field, int value, int def)
576 fprintf(f, "\t%s=%d\n", field, value);
580 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
582 char *value = wpa_config_get(ssid, "bssid");
585 fprintf(f, "\tbssid=%s\n", value);
590 static void write_psk(FILE *f, struct wpa_ssid *ssid)
592 char *value = wpa_config_get(ssid, "psk");
595 fprintf(f, "\tpsk=%s\n", value);
600 static void write_proto(FILE *f, struct wpa_ssid *ssid)
604 if (ssid->proto == DEFAULT_PROTO)
607 value = wpa_config_get(ssid, "proto");
611 fprintf(f, "\tproto=%s\n", value);
616 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
620 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
623 value = wpa_config_get(ssid, "key_mgmt");
627 fprintf(f, "\tkey_mgmt=%s\n", value);
632 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
636 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
639 value = wpa_config_get(ssid, "pairwise");
643 fprintf(f, "\tpairwise=%s\n", value);
648 static void write_group(FILE *f, struct wpa_ssid *ssid)
652 if (ssid->group_cipher == DEFAULT_GROUP)
655 value = wpa_config_get(ssid, "group");
659 fprintf(f, "\tgroup=%s\n", value);
664 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
668 if (ssid->auth_alg == 0)
671 value = wpa_config_get(ssid, "auth_alg");
675 fprintf(f, "\tauth_alg=%s\n", value);
680 #ifdef IEEE8021X_EAPOL
681 static void write_eap(FILE *f, struct wpa_ssid *ssid)
685 value = wpa_config_get(ssid, "eap");
690 fprintf(f, "\teap=%s\n", value);
693 #endif /* IEEE8021X_EAPOL */
696 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
698 char field[20], *value;
701 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
702 if (res < 0 || (size_t) res >= sizeof(field))
704 value = wpa_config_get(ssid, field);
706 fprintf(f, "\t%s=%s\n", field, value);
712 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
716 #define STR(t) write_str(f, #t, ssid)
717 #define INT(t) write_int(f, #t, ssid->t, 0)
718 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
719 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
720 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
724 write_bssid(f, ssid);
726 write_proto(f, ssid);
727 write_key_mgmt(f, ssid);
728 write_pairwise(f, ssid);
729 write_group(f, ssid);
730 write_auth_alg(f, ssid);
731 #ifdef IEEE8021X_EAPOL
734 STR(anonymous_identity);
740 STR(private_key_passwd);
743 STR(altsubject_match);
748 STR(private_key2_passwd);
751 STR(altsubject_match2);
764 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
765 #endif /* IEEE8021X_EAPOL */
766 for (i = 0; i < 4; i++)
767 write_wep_key(f, i, ssid);
770 #ifdef IEEE8021X_EAPOL
771 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
773 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
774 #endif /* IEEE8021X_EAPOL */
776 INT(proactive_key_caching);
779 #ifdef CONFIG_IEEE80211W
781 #endif /* CONFIG_IEEE80211W */
790 #ifndef CONFIG_NO_CONFIG_BLOBS
791 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
793 unsigned char *encoded;
795 encoded = base64_encode(blob->data, blob->len, NULL);
799 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
803 #endif /* CONFIG_NO_CONFIG_BLOBS */
806 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
808 #ifdef CONFIG_CTRL_IFACE
809 if (config->ctrl_interface)
810 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
811 if (config->ctrl_interface_group)
812 fprintf(f, "ctrl_interface_group=%s\n",
813 config->ctrl_interface_group);
814 #endif /* CONFIG_CTRL_IFACE */
815 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
816 fprintf(f, "eapol_version=%d\n", config->eapol_version);
817 if (config->ap_scan != DEFAULT_AP_SCAN)
818 fprintf(f, "ap_scan=%d\n", config->ap_scan);
819 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
820 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
821 #ifdef EAP_TLS_OPENSSL
822 if (config->opensc_engine_path)
823 fprintf(f, "opensc_engine_path=%s\n",
824 config->opensc_engine_path);
825 if (config->pkcs11_engine_path)
826 fprintf(f, "pkcs11_engine_path=%s\n",
827 config->pkcs11_engine_path);
828 if (config->pkcs11_module_path)
829 fprintf(f, "pkcs11_module_path=%s\n",
830 config->pkcs11_module_path);
831 #endif /* EAP_TLS_OPENSSL */
832 if (config->driver_param)
833 fprintf(f, "driver_param=%s\n", config->driver_param);
834 if (config->dot11RSNAConfigPMKLifetime)
835 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
836 config->dot11RSNAConfigPMKLifetime);
837 if (config->dot11RSNAConfigPMKReauthThreshold)
838 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
839 config->dot11RSNAConfigPMKReauthThreshold);
840 if (config->dot11RSNAConfigSATimeout)
841 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
842 config->dot11RSNAConfigSATimeout);
843 if (config->update_config)
844 fprintf(f, "update_config=%d\n", config->update_config);
847 #endif /* CONFIG_NO_CONFIG_WRITE */
850 int wpa_config_write(const char *name, struct wpa_config *config)
852 #ifndef CONFIG_NO_CONFIG_WRITE
854 struct wpa_ssid *ssid;
855 #ifndef CONFIG_NO_CONFIG_BLOBS
856 struct wpa_config_blob *blob;
857 #endif /* CONFIG_NO_CONFIG_BLOBS */
860 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
862 f = fopen(name, "w");
864 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
868 wpa_config_write_global(f, config);
870 for (ssid = config->ssid; ssid; ssid = ssid->next) {
871 fprintf(f, "\nnetwork={\n");
872 wpa_config_write_network(f, ssid);
876 #ifndef CONFIG_NO_CONFIG_BLOBS
877 for (blob = config->blobs; blob; blob = blob->next) {
878 ret = wpa_config_write_blob(f, blob);
882 #endif /* CONFIG_NO_CONFIG_BLOBS */
886 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
887 name, ret ? "un" : "");
889 #else /* CONFIG_NO_CONFIG_WRITE */
891 #endif /* CONFIG_NO_CONFIG_WRITE */