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.
25 #include "eap_peer/eap_methods.h"
29 * wpa_config_get_line - Read the next configuration file line
30 * @s: Buffer for the line
31 * @size: The buffer length
32 * @stream: File stream to read from
33 * @line: Pointer to a variable storing the file line number
34 * @_pos: Buffer for the pointer to the beginning of data on the text line or
35 * %NULL if not needed (returned value used instead)
36 * Returns: Pointer to the beginning of data on the text line or %NULL if no
37 * more text lines are available.
39 * This function reads the next non-empty line from the configuration file and
40 * removes comments. The returned string is guaranteed to be null-terminated.
42 static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
45 char *pos, *end, *sstart;
47 while (fgets(s, size, stream)) {
52 /* Skip white space from the beginning of line. */
53 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
56 /* Skip comment lines and empty lines */
57 if (*pos == '#' || *pos == '\n' || *pos == '\0')
61 * Remove # comments unless they are within a double quoted
64 sstart = os_strchr(pos, '"');
66 sstart = os_strrchr(sstart + 1, '"');
69 end = os_strchr(sstart, '#');
73 end = pos + os_strlen(pos) - 1;
75 /* Remove trailing white space. */
77 (*end == '\n' || *end == ' ' || *end == '\t' ||
95 static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
99 if (ssid->passphrase) {
101 wpa_printf(MSG_ERROR, "Line %d: both PSK and "
102 "passphrase configured.", line);
105 wpa_config_update_psk(ssid);
108 if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
109 WPA_KEY_MGMT_PSK_SHA256)) &&
111 wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
112 "management, but no PSK configured.", line);
116 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
117 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
118 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
119 /* Group cipher cannot be stronger than the pairwise cipher. */
120 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
121 " list since it was not allowed for pairwise "
123 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
130 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
132 struct wpa_ssid *ssid;
133 int errors = 0, end = 0;
134 char buf[256], *pos, *pos2;
136 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
138 ssid = os_zalloc(sizeof(*ssid));
143 wpa_config_set_network_defaults(ssid);
145 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
146 if (os_strcmp(pos, "}") == 0) {
151 pos2 = os_strchr(pos, '=');
153 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
154 "'%s'.", *line, pos);
161 if (os_strchr(pos2 + 1, '"') == NULL) {
162 wpa_printf(MSG_ERROR, "Line %d: invalid "
163 "quotation '%s'.", *line, pos2);
169 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
174 wpa_printf(MSG_ERROR, "Line %d: network block was not "
175 "terminated properly.", *line);
179 errors += wpa_config_validate_network(ssid, *line);
182 wpa_config_free_ssid(ssid);
190 #ifndef CONFIG_NO_CONFIG_BLOBS
191 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
194 struct wpa_config_blob *blob;
196 unsigned char *encoded = NULL, *nencoded;
198 size_t encoded_len = 0, len;
200 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
203 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
204 if (os_strcmp(pos, "}") == 0) {
209 len = os_strlen(pos);
210 nencoded = os_realloc(encoded, encoded_len + len);
211 if (nencoded == NULL) {
212 wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
218 os_memcpy(encoded + encoded_len, pos, len);
223 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
229 blob = os_zalloc(sizeof(*blob));
234 blob->name = os_strdup(name);
235 blob->data = base64_decode(encoded, encoded_len, &blob->len);
238 if (blob->name == NULL || blob->data == NULL) {
239 wpa_config_free_blob(blob);
247 static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
248 int *line, char *bname)
251 struct wpa_config_blob *blob;
253 name_end = os_strchr(bname, '=');
254 if (name_end == NULL) {
255 wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
261 blob = wpa_config_read_blob(f, line, bname);
263 wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
267 wpa_config_set_blob(config, blob);
270 #endif /* CONFIG_NO_CONFIG_BLOBS */
273 #ifdef CONFIG_CTRL_IFACE
274 static int wpa_config_process_ctrl_interface(struct wpa_config *config,
277 os_free(config->ctrl_interface);
278 config->ctrl_interface = os_strdup(pos);
279 wpa_printf(MSG_DEBUG, "ctrl_interface='%s'", config->ctrl_interface);
284 static int wpa_config_process_ctrl_interface_group(struct wpa_config *config,
287 os_free(config->ctrl_interface_group);
288 config->ctrl_interface_group = os_strdup(pos);
289 wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' (DEPRECATED)",
290 config->ctrl_interface_group);
293 #endif /* CONFIG_CTRL_IFACE */
296 static int wpa_config_process_eapol_version(struct wpa_config *config,
299 config->eapol_version = atoi(pos);
300 if (config->eapol_version < 1 || config->eapol_version > 2) {
301 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): "
302 "'%s'.", line, config->eapol_version, pos);
305 wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version);
310 static int wpa_config_process_ap_scan(struct wpa_config *config, char *pos)
312 config->ap_scan = atoi(pos);
313 wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
318 static int wpa_config_process_fast_reauth(struct wpa_config *config, char *pos)
320 config->fast_reauth = atoi(pos);
321 wpa_printf(MSG_DEBUG, "fast_reauth=%d", config->fast_reauth);
326 #ifdef EAP_TLS_OPENSSL
328 static int wpa_config_process_opensc_engine_path(struct wpa_config *config,
331 os_free(config->opensc_engine_path);
332 config->opensc_engine_path = os_strdup(pos);
333 wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
334 config->opensc_engine_path);
339 static int wpa_config_process_pkcs11_engine_path(struct wpa_config *config,
342 os_free(config->pkcs11_engine_path);
343 config->pkcs11_engine_path = os_strdup(pos);
344 wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
345 config->pkcs11_engine_path);
350 static int wpa_config_process_pkcs11_module_path(struct wpa_config *config,
353 os_free(config->pkcs11_module_path);
354 config->pkcs11_module_path = os_strdup(pos);
355 wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
356 config->pkcs11_module_path);
360 #endif /* EAP_TLS_OPENSSL */
363 static int wpa_config_process_driver_param(struct wpa_config *config,
366 os_free(config->driver_param);
367 config->driver_param = os_strdup(pos);
368 wpa_printf(MSG_DEBUG, "driver_param='%s'", config->driver_param);
373 static int wpa_config_process_pmk_lifetime(struct wpa_config *config,
376 config->dot11RSNAConfigPMKLifetime = atoi(pos);
377 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
378 config->dot11RSNAConfigPMKLifetime);
383 static int wpa_config_process_pmk_reauth_threshold(struct wpa_config *config,
386 config->dot11RSNAConfigPMKReauthThreshold = atoi(pos);
387 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKReauthThreshold=%d",
388 config->dot11RSNAConfigPMKReauthThreshold);
393 static int wpa_config_process_sa_timeout(struct wpa_config *config, char *pos)
395 config->dot11RSNAConfigSATimeout = atoi(pos);
396 wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
397 config->dot11RSNAConfigSATimeout);
402 #ifndef CONFIG_NO_CONFIG_WRITE
403 static int wpa_config_process_update_config(struct wpa_config *config,
406 config->update_config = atoi(pos);
407 wpa_printf(MSG_DEBUG, "update_config=%d", config->update_config);
410 #endif /* CONFIG_NO_CONFIG_WRITE */
413 static int wpa_config_process_load_dynamic_eap(int line, char *so)
416 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
417 ret = eap_peer_method_load(so);
419 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
422 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
423 "method '%s'.", line, so);
432 static int wpa_config_process_uuid(struct wpa_config *config, int line,
436 if (uuid_str2bin(pos, config->uuid)) {
437 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
440 uuid_bin2str(config->uuid, buf, sizeof(buf));
441 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
444 #endif /* CONFIG_WPS */
447 static int wpa_config_process_global(struct wpa_config *config, char *pos,
450 #ifdef CONFIG_CTRL_IFACE
451 if (os_strncmp(pos, "ctrl_interface=", 15) == 0)
452 return wpa_config_process_ctrl_interface(config, pos + 15);
454 if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0)
455 return wpa_config_process_ctrl_interface_group(config,
457 #endif /* CONFIG_CTRL_IFACE */
459 if (os_strncmp(pos, "eapol_version=", 14) == 0)
460 return wpa_config_process_eapol_version(config, line,
463 if (os_strncmp(pos, "ap_scan=", 8) == 0)
464 return wpa_config_process_ap_scan(config, pos + 8);
466 if (os_strncmp(pos, "fast_reauth=", 12) == 0)
467 return wpa_config_process_fast_reauth(config, pos + 12);
469 #ifdef EAP_TLS_OPENSSL
470 if (os_strncmp(pos, "opensc_engine_path=", 19) == 0)
471 return wpa_config_process_opensc_engine_path(config, pos + 19);
473 if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0)
474 return wpa_config_process_pkcs11_engine_path(config, pos + 19);
476 if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0)
477 return wpa_config_process_pkcs11_module_path(config, pos + 19);
478 #endif /* EAP_TLS_OPENSSL */
480 if (os_strncmp(pos, "driver_param=", 13) == 0)
481 return wpa_config_process_driver_param(config, pos + 13);
483 if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) == 0)
484 return wpa_config_process_pmk_lifetime(config, pos + 27);
486 if (os_strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=", 34) == 0)
487 return wpa_config_process_pmk_reauth_threshold(config,
490 if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) == 0)
491 return wpa_config_process_sa_timeout(config, pos + 25);
493 #ifndef CONFIG_NO_CONFIG_WRITE
494 if (os_strncmp(pos, "update_config=", 14) == 0)
495 return wpa_config_process_update_config(config, pos + 14);
496 #endif /* CONFIG_NO_CONFIG_WRITE */
498 if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0)
499 return wpa_config_process_load_dynamic_eap(line, pos + 17);
502 if (os_strncmp(pos, "uuid=", 5) == 0)
503 return wpa_config_process_uuid(config, line, pos + 5);
504 #endif /* CONFIG_WPS */
510 struct wpa_config * wpa_config_read(const char *name)
514 int errors = 0, line = 0;
515 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
516 struct wpa_config *config;
519 config = wpa_config_alloc_empty(NULL, NULL);
522 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
523 f = fopen(name, "r");
529 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
530 if (os_strcmp(pos, "network={") == 0) {
531 ssid = wpa_config_read_network(f, &line, id++);
533 wpa_printf(MSG_ERROR, "Line %d: failed to "
534 "parse network block.", line);
544 if (wpa_config_add_prio_network(config, ssid)) {
545 wpa_printf(MSG_ERROR, "Line %d: failed to add "
546 "network block to priority list.",
551 #ifndef CONFIG_NO_CONFIG_BLOBS
552 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
553 if (wpa_config_process_blob(config, f, &line, pos + 12)
558 #endif /* CONFIG_NO_CONFIG_BLOBS */
559 } else if (wpa_config_process_global(config, pos, line) < 0) {
560 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
561 "line '%s'.", line, pos);
570 wpa_config_debug_dump_networks(config);
573 wpa_config_free(config);
582 #ifndef CONFIG_NO_CONFIG_WRITE
584 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
586 char *value = wpa_config_get(ssid, field);
589 fprintf(f, "\t%s=%s\n", field, value);
594 static void write_int(FILE *f, const char *field, int value, int def)
598 fprintf(f, "\t%s=%d\n", field, value);
602 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
604 char *value = wpa_config_get(ssid, "bssid");
607 fprintf(f, "\tbssid=%s\n", value);
612 static void write_psk(FILE *f, struct wpa_ssid *ssid)
614 char *value = wpa_config_get(ssid, "psk");
617 fprintf(f, "\tpsk=%s\n", value);
622 static void write_proto(FILE *f, struct wpa_ssid *ssid)
626 if (ssid->proto == DEFAULT_PROTO)
629 value = wpa_config_get(ssid, "proto");
633 fprintf(f, "\tproto=%s\n", value);
638 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
642 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
645 value = wpa_config_get(ssid, "key_mgmt");
649 fprintf(f, "\tkey_mgmt=%s\n", value);
654 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
658 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
661 value = wpa_config_get(ssid, "pairwise");
665 fprintf(f, "\tpairwise=%s\n", value);
670 static void write_group(FILE *f, struct wpa_ssid *ssid)
674 if (ssid->group_cipher == DEFAULT_GROUP)
677 value = wpa_config_get(ssid, "group");
681 fprintf(f, "\tgroup=%s\n", value);
686 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
690 if (ssid->auth_alg == 0)
693 value = wpa_config_get(ssid, "auth_alg");
697 fprintf(f, "\tauth_alg=%s\n", value);
702 #ifdef IEEE8021X_EAPOL
703 static void write_eap(FILE *f, struct wpa_ssid *ssid)
707 value = wpa_config_get(ssid, "eap");
712 fprintf(f, "\teap=%s\n", value);
715 #endif /* IEEE8021X_EAPOL */
718 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
720 char field[20], *value;
723 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
724 if (res < 0 || (size_t) res >= sizeof(field))
726 value = wpa_config_get(ssid, field);
728 fprintf(f, "\t%s=%s\n", field, value);
734 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
738 #define STR(t) write_str(f, #t, ssid)
739 #define INT(t) write_int(f, #t, ssid->t, 0)
740 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
741 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
742 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
746 write_bssid(f, ssid);
748 write_proto(f, ssid);
749 write_key_mgmt(f, ssid);
750 write_pairwise(f, ssid);
751 write_group(f, ssid);
752 write_auth_alg(f, ssid);
753 #ifdef IEEE8021X_EAPOL
756 STR(anonymous_identity);
762 STR(private_key_passwd);
765 STR(altsubject_match);
770 STR(private_key2_passwd);
773 STR(altsubject_match2);
789 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
790 #endif /* IEEE8021X_EAPOL */
791 for (i = 0; i < 4; i++)
792 write_wep_key(f, i, ssid);
795 #ifdef IEEE8021X_EAPOL
796 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
798 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
799 #endif /* IEEE8021X_EAPOL */
801 INT(proactive_key_caching);
804 #ifdef CONFIG_IEEE80211W
806 #endif /* CONFIG_IEEE80211W */
815 #ifndef CONFIG_NO_CONFIG_BLOBS
816 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
818 unsigned char *encoded;
820 encoded = base64_encode(blob->data, blob->len, NULL);
824 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
828 #endif /* CONFIG_NO_CONFIG_BLOBS */
831 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
833 #ifdef CONFIG_CTRL_IFACE
834 if (config->ctrl_interface)
835 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
836 if (config->ctrl_interface_group)
837 fprintf(f, "ctrl_interface_group=%s\n",
838 config->ctrl_interface_group);
839 #endif /* CONFIG_CTRL_IFACE */
840 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
841 fprintf(f, "eapol_version=%d\n", config->eapol_version);
842 if (config->ap_scan != DEFAULT_AP_SCAN)
843 fprintf(f, "ap_scan=%d\n", config->ap_scan);
844 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
845 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
846 #ifdef EAP_TLS_OPENSSL
847 if (config->opensc_engine_path)
848 fprintf(f, "opensc_engine_path=%s\n",
849 config->opensc_engine_path);
850 if (config->pkcs11_engine_path)
851 fprintf(f, "pkcs11_engine_path=%s\n",
852 config->pkcs11_engine_path);
853 if (config->pkcs11_module_path)
854 fprintf(f, "pkcs11_module_path=%s\n",
855 config->pkcs11_module_path);
856 #endif /* EAP_TLS_OPENSSL */
857 if (config->driver_param)
858 fprintf(f, "driver_param=%s\n", config->driver_param);
859 if (config->dot11RSNAConfigPMKLifetime)
860 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
861 config->dot11RSNAConfigPMKLifetime);
862 if (config->dot11RSNAConfigPMKReauthThreshold)
863 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
864 config->dot11RSNAConfigPMKReauthThreshold);
865 if (config->dot11RSNAConfigSATimeout)
866 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
867 config->dot11RSNAConfigSATimeout);
868 if (config->update_config)
869 fprintf(f, "update_config=%d\n", config->update_config);
871 if (is_nil_uuid(config->uuid)) {
873 uuid_bin2str(config->uuid, buf, sizeof(buf));
874 fprintf(f, "uuid=%s\n", buf);
876 #endif /* CONFIG_WPS */
879 #endif /* CONFIG_NO_CONFIG_WRITE */
882 int wpa_config_write(const char *name, struct wpa_config *config)
884 #ifndef CONFIG_NO_CONFIG_WRITE
886 struct wpa_ssid *ssid;
887 #ifndef CONFIG_NO_CONFIG_BLOBS
888 struct wpa_config_blob *blob;
889 #endif /* CONFIG_NO_CONFIG_BLOBS */
892 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
894 f = fopen(name, "w");
896 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
900 wpa_config_write_global(f, config);
902 for (ssid = config->ssid; ssid; ssid = ssid->next) {
903 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
904 continue; /* do not save temporary WPS networks */
905 fprintf(f, "\nnetwork={\n");
906 wpa_config_write_network(f, ssid);
910 #ifndef CONFIG_NO_CONFIG_BLOBS
911 for (blob = config->blobs; blob; blob = blob->next) {
912 ret = wpa_config_write_blob(f, blob);
916 #endif /* CONFIG_NO_CONFIG_BLOBS */
920 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
921 name, ret ? "un" : "");
923 #else /* CONFIG_NO_CONFIG_WRITE */
925 #endif /* CONFIG_NO_CONFIG_WRITE */