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 static int wpa_config_process_country(struct wpa_config *config, char *pos)
275 if (!pos[0] || !pos[1]) {
276 wpa_printf(MSG_DEBUG, "Invalid country set");
279 config->alpha2[0] = pos[0];
280 config->alpha2[1] = pos[1];
281 wpa_printf(MSG_DEBUG, "country='%c%c'",
282 config->alpha2[0], config->alpha2[1]);
287 #ifdef CONFIG_CTRL_IFACE
288 static int wpa_config_process_ctrl_interface(struct wpa_config *config,
291 os_free(config->ctrl_interface);
292 config->ctrl_interface = os_strdup(pos);
293 wpa_printf(MSG_DEBUG, "ctrl_interface='%s'", config->ctrl_interface);
298 static int wpa_config_process_ctrl_interface_group(struct wpa_config *config,
301 os_free(config->ctrl_interface_group);
302 config->ctrl_interface_group = os_strdup(pos);
303 wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' (DEPRECATED)",
304 config->ctrl_interface_group);
307 #endif /* CONFIG_CTRL_IFACE */
310 static int wpa_config_process_eapol_version(struct wpa_config *config,
313 config->eapol_version = atoi(pos);
314 if (config->eapol_version < 1 || config->eapol_version > 2) {
315 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): "
316 "'%s'.", line, config->eapol_version, pos);
319 wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version);
324 static int wpa_config_process_ap_scan(struct wpa_config *config, char *pos)
326 config->ap_scan = atoi(pos);
327 wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
332 static int wpa_config_process_fast_reauth(struct wpa_config *config, char *pos)
334 config->fast_reauth = atoi(pos);
335 wpa_printf(MSG_DEBUG, "fast_reauth=%d", config->fast_reauth);
340 #ifdef EAP_TLS_OPENSSL
342 static int wpa_config_process_opensc_engine_path(struct wpa_config *config,
345 os_free(config->opensc_engine_path);
346 config->opensc_engine_path = os_strdup(pos);
347 wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
348 config->opensc_engine_path);
353 static int wpa_config_process_pkcs11_engine_path(struct wpa_config *config,
356 os_free(config->pkcs11_engine_path);
357 config->pkcs11_engine_path = os_strdup(pos);
358 wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
359 config->pkcs11_engine_path);
364 static int wpa_config_process_pkcs11_module_path(struct wpa_config *config,
367 os_free(config->pkcs11_module_path);
368 config->pkcs11_module_path = os_strdup(pos);
369 wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
370 config->pkcs11_module_path);
374 #endif /* EAP_TLS_OPENSSL */
377 static int wpa_config_process_driver_param(struct wpa_config *config,
380 os_free(config->driver_param);
381 config->driver_param = os_strdup(pos);
382 wpa_printf(MSG_DEBUG, "driver_param='%s'", config->driver_param);
387 static int wpa_config_process_pmk_lifetime(struct wpa_config *config,
390 config->dot11RSNAConfigPMKLifetime = atoi(pos);
391 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
392 config->dot11RSNAConfigPMKLifetime);
397 static int wpa_config_process_pmk_reauth_threshold(struct wpa_config *config,
400 config->dot11RSNAConfigPMKReauthThreshold = atoi(pos);
401 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKReauthThreshold=%d",
402 config->dot11RSNAConfigPMKReauthThreshold);
407 static int wpa_config_process_sa_timeout(struct wpa_config *config, char *pos)
409 config->dot11RSNAConfigSATimeout = atoi(pos);
410 wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
411 config->dot11RSNAConfigSATimeout);
416 #ifndef CONFIG_NO_CONFIG_WRITE
417 static int wpa_config_process_update_config(struct wpa_config *config,
420 config->update_config = atoi(pos);
421 wpa_printf(MSG_DEBUG, "update_config=%d", config->update_config);
424 #endif /* CONFIG_NO_CONFIG_WRITE */
427 static int wpa_config_process_load_dynamic_eap(int line, char *so)
430 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
431 ret = eap_peer_method_load(so);
433 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
436 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
437 "method '%s'.", line, so);
446 static int wpa_config_process_uuid(struct wpa_config *config, int line,
450 if (uuid_str2bin(pos, config->uuid)) {
451 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
454 uuid_bin2str(config->uuid, buf, sizeof(buf));
455 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
458 #endif /* CONFIG_WPS */
461 static int wpa_config_process_global(struct wpa_config *config, char *pos,
464 #ifdef CONFIG_CTRL_IFACE
465 if (os_strncmp(pos, "ctrl_interface=", 15) == 0)
466 return wpa_config_process_ctrl_interface(config, pos + 15);
468 if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0)
469 return wpa_config_process_ctrl_interface_group(config,
471 #endif /* CONFIG_CTRL_IFACE */
473 if (os_strncmp(pos, "eapol_version=", 14) == 0)
474 return wpa_config_process_eapol_version(config, line,
477 if (os_strncmp(pos, "ap_scan=", 8) == 0)
478 return wpa_config_process_ap_scan(config, pos + 8);
480 if (os_strncmp(pos, "fast_reauth=", 12) == 0)
481 return wpa_config_process_fast_reauth(config, pos + 12);
483 #ifdef EAP_TLS_OPENSSL
484 if (os_strncmp(pos, "opensc_engine_path=", 19) == 0)
485 return wpa_config_process_opensc_engine_path(config, pos + 19);
487 if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0)
488 return wpa_config_process_pkcs11_engine_path(config, pos + 19);
490 if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0)
491 return wpa_config_process_pkcs11_module_path(config, pos + 19);
492 #endif /* EAP_TLS_OPENSSL */
494 if (os_strncmp(pos, "driver_param=", 13) == 0)
495 return wpa_config_process_driver_param(config, pos + 13);
497 if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) == 0)
498 return wpa_config_process_pmk_lifetime(config, pos + 27);
500 if (os_strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=", 34) == 0)
501 return wpa_config_process_pmk_reauth_threshold(config,
504 if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) == 0)
505 return wpa_config_process_sa_timeout(config, pos + 25);
507 #ifndef CONFIG_NO_CONFIG_WRITE
508 if (os_strncmp(pos, "update_config=", 14) == 0)
509 return wpa_config_process_update_config(config, pos + 14);
510 #endif /* CONFIG_NO_CONFIG_WRITE */
512 if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0)
513 return wpa_config_process_load_dynamic_eap(line, pos + 17);
516 if (os_strncmp(pos, "uuid=", 5) == 0)
517 return wpa_config_process_uuid(config, line, pos + 5);
518 #endif /* CONFIG_WPS */
520 if (os_strncmp(pos, "country=", 8) == 0)
521 return wpa_config_process_country(config, pos + 8);
527 struct wpa_config * wpa_config_read(const char *name)
531 int errors = 0, line = 0;
532 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
533 struct wpa_config *config;
536 config = wpa_config_alloc_empty(NULL, NULL);
539 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
540 f = fopen(name, "r");
546 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
547 if (os_strcmp(pos, "network={") == 0) {
548 ssid = wpa_config_read_network(f, &line, id++);
550 wpa_printf(MSG_ERROR, "Line %d: failed to "
551 "parse network block.", line);
561 if (wpa_config_add_prio_network(config, ssid)) {
562 wpa_printf(MSG_ERROR, "Line %d: failed to add "
563 "network block to priority list.",
568 #ifndef CONFIG_NO_CONFIG_BLOBS
569 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
570 if (wpa_config_process_blob(config, f, &line, pos + 12)
575 #endif /* CONFIG_NO_CONFIG_BLOBS */
576 } else if (wpa_config_process_global(config, pos, line) < 0) {
577 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
578 "line '%s'.", line, pos);
587 wpa_config_debug_dump_networks(config);
590 wpa_config_free(config);
599 #ifndef CONFIG_NO_CONFIG_WRITE
601 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
603 char *value = wpa_config_get(ssid, field);
606 fprintf(f, "\t%s=%s\n", field, value);
611 static void write_int(FILE *f, const char *field, int value, int def)
615 fprintf(f, "\t%s=%d\n", field, value);
619 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
621 char *value = wpa_config_get(ssid, "bssid");
624 fprintf(f, "\tbssid=%s\n", value);
629 static void write_psk(FILE *f, struct wpa_ssid *ssid)
631 char *value = wpa_config_get(ssid, "psk");
634 fprintf(f, "\tpsk=%s\n", value);
639 static void write_proto(FILE *f, struct wpa_ssid *ssid)
643 if (ssid->proto == DEFAULT_PROTO)
646 value = wpa_config_get(ssid, "proto");
650 fprintf(f, "\tproto=%s\n", value);
655 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
659 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
662 value = wpa_config_get(ssid, "key_mgmt");
666 fprintf(f, "\tkey_mgmt=%s\n", value);
671 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
675 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
678 value = wpa_config_get(ssid, "pairwise");
682 fprintf(f, "\tpairwise=%s\n", value);
687 static void write_group(FILE *f, struct wpa_ssid *ssid)
691 if (ssid->group_cipher == DEFAULT_GROUP)
694 value = wpa_config_get(ssid, "group");
698 fprintf(f, "\tgroup=%s\n", value);
703 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
707 if (ssid->auth_alg == 0)
710 value = wpa_config_get(ssid, "auth_alg");
714 fprintf(f, "\tauth_alg=%s\n", value);
719 #ifdef IEEE8021X_EAPOL
720 static void write_eap(FILE *f, struct wpa_ssid *ssid)
724 value = wpa_config_get(ssid, "eap");
729 fprintf(f, "\teap=%s\n", value);
732 #endif /* IEEE8021X_EAPOL */
735 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
737 char field[20], *value;
740 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
741 if (res < 0 || (size_t) res >= sizeof(field))
743 value = wpa_config_get(ssid, field);
745 fprintf(f, "\t%s=%s\n", field, value);
751 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
755 #define STR(t) write_str(f, #t, ssid)
756 #define INT(t) write_int(f, #t, ssid->t, 0)
757 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
758 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
759 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
763 write_bssid(f, ssid);
765 write_proto(f, ssid);
766 write_key_mgmt(f, ssid);
767 write_pairwise(f, ssid);
768 write_group(f, ssid);
769 write_auth_alg(f, ssid);
770 #ifdef IEEE8021X_EAPOL
773 STR(anonymous_identity);
779 STR(private_key_passwd);
782 STR(altsubject_match);
787 STR(private_key2_passwd);
790 STR(altsubject_match2);
806 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
807 #endif /* IEEE8021X_EAPOL */
808 for (i = 0; i < 4; i++)
809 write_wep_key(f, i, ssid);
812 #ifdef IEEE8021X_EAPOL
813 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
815 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
816 #endif /* IEEE8021X_EAPOL */
818 INT(proactive_key_caching);
821 #ifdef CONFIG_IEEE80211W
823 #endif /* CONFIG_IEEE80211W */
832 #ifndef CONFIG_NO_CONFIG_BLOBS
833 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
835 unsigned char *encoded;
837 encoded = base64_encode(blob->data, blob->len, NULL);
841 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
845 #endif /* CONFIG_NO_CONFIG_BLOBS */
848 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
850 #ifdef CONFIG_CTRL_IFACE
851 if (config->ctrl_interface)
852 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
853 if (config->ctrl_interface_group)
854 fprintf(f, "ctrl_interface_group=%s\n",
855 config->ctrl_interface_group);
856 #endif /* CONFIG_CTRL_IFACE */
857 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
858 fprintf(f, "eapol_version=%d\n", config->eapol_version);
859 if (config->ap_scan != DEFAULT_AP_SCAN)
860 fprintf(f, "ap_scan=%d\n", config->ap_scan);
861 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
862 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
863 #ifdef EAP_TLS_OPENSSL
864 if (config->opensc_engine_path)
865 fprintf(f, "opensc_engine_path=%s\n",
866 config->opensc_engine_path);
867 if (config->pkcs11_engine_path)
868 fprintf(f, "pkcs11_engine_path=%s\n",
869 config->pkcs11_engine_path);
870 if (config->pkcs11_module_path)
871 fprintf(f, "pkcs11_module_path=%s\n",
872 config->pkcs11_module_path);
873 #endif /* EAP_TLS_OPENSSL */
874 if (config->driver_param)
875 fprintf(f, "driver_param=%s\n", config->driver_param);
876 if (config->dot11RSNAConfigPMKLifetime)
877 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
878 config->dot11RSNAConfigPMKLifetime);
879 if (config->dot11RSNAConfigPMKReauthThreshold)
880 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
881 config->dot11RSNAConfigPMKReauthThreshold);
882 if (config->dot11RSNAConfigSATimeout)
883 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
884 config->dot11RSNAConfigSATimeout);
885 if (config->update_config)
886 fprintf(f, "update_config=%d\n", config->update_config);
888 if (is_nil_uuid(config->uuid)) {
890 uuid_bin2str(config->uuid, buf, sizeof(buf));
891 fprintf(f, "uuid=%s\n", buf);
893 #endif /* CONFIG_WPS */
894 if (config->alpha2[0] && config->alpha2[1]) {
895 fprintf(f, "country=%c%c\n",
896 config->alpha2[0], config->alpha2[1]);
900 #endif /* CONFIG_NO_CONFIG_WRITE */
903 int wpa_config_write(const char *name, struct wpa_config *config)
905 #ifndef CONFIG_NO_CONFIG_WRITE
907 struct wpa_ssid *ssid;
908 #ifndef CONFIG_NO_CONFIG_BLOBS
909 struct wpa_config_blob *blob;
910 #endif /* CONFIG_NO_CONFIG_BLOBS */
913 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
915 f = fopen(name, "w");
917 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
921 wpa_config_write_global(f, config);
923 for (ssid = config->ssid; ssid; ssid = ssid->next) {
924 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
925 continue; /* do not save temporary WPS networks */
926 fprintf(f, "\nnetwork={\n");
927 wpa_config_write_network(f, ssid);
931 #ifndef CONFIG_NO_CONFIG_BLOBS
932 for (blob = config->blobs; blob; blob = blob->next) {
933 ret = wpa_config_write_blob(f, blob);
937 #endif /* CONFIG_NO_CONFIG_BLOBS */
941 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
942 name, ret ? "un" : "");
944 #else /* CONFIG_NO_CONFIG_WRITE */
946 #endif /* CONFIG_NO_CONFIG_WRITE */