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 struct global_parse_data {
275 int (*parser)(const struct global_parse_data *data,
276 struct wpa_config *config, int line, const char *value);
277 void *param1, *param2, *param3;
281 static int wpa_config_parse_int(const struct global_parse_data *data,
282 struct wpa_config *config, int line,
286 dst = (int *) (((u8 *) config) + (long) data->param1);
288 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
290 if (data->param2 && *dst < (long) data->param2) {
291 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
292 "min_value=%ld)", line, data->name, *dst,
293 (long) data->param2);
294 *dst = (long) data->param2;
298 if (data->param3 && *dst > (long) data->param3) {
299 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
300 "max_value=%ld)", line, data->name, *dst,
301 (long) data->param3);
302 *dst = (long) data->param3;
310 static int wpa_config_parse_str(const struct global_parse_data *data,
311 struct wpa_config *config, int line,
317 len = os_strlen(pos);
318 if (data->param2 && len < (size_t) data->param2) {
319 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
320 "min_len=%ld)", line, data->name,
321 (unsigned long) len, (long) data->param2);
325 if (data->param3 && len > (size_t) data->param3) {
326 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
327 "max_len=%ld)", line, data->name,
328 (unsigned long) len, (long) data->param3);
332 tmp = os_strdup(pos);
336 dst = (char **) (((u8 *) config) + (long) data->param1);
339 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
345 static int wpa_config_process_country(const struct global_parse_data *data,
346 struct wpa_config *config, int line,
349 if (!pos[0] || !pos[1]) {
350 wpa_printf(MSG_DEBUG, "Invalid country set");
353 config->country[0] = pos[0];
354 config->country[1] = pos[1];
355 wpa_printf(MSG_DEBUG, "country='%c%c'",
356 config->country[0], config->country[1]);
361 static int wpa_config_process_load_dynamic_eap(
362 const struct global_parse_data *data, struct wpa_config *config,
363 int line, const char *so)
366 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
367 ret = eap_peer_method_load(so);
369 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
372 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
373 "method '%s'.", line, so);
383 static int wpa_config_process_uuid(const struct global_parse_data *data,
384 struct wpa_config *config, int line,
388 if (uuid_str2bin(pos, config->uuid)) {
389 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
392 uuid_bin2str(config->uuid, buf, sizeof(buf));
393 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
398 static int wpa_config_process_os_version(const struct global_parse_data *data,
399 struct wpa_config *config, int line,
402 if (hexstr2bin(pos, config->os_version, 4)) {
403 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
406 wpa_printf(MSG_DEBUG, "os_version=%08x",
407 WPA_GET_BE32(config->os_version));
411 #endif /* CONFIG_WPS */
417 /* OFFSET: Get offset of a variable within the wpa_config structure */
418 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
420 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
421 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
422 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f)
423 #define INT(f) _INT(f), NULL, NULL
424 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
425 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
426 #define STR(f) _STR(f), NULL, NULL
427 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
429 static const struct global_parse_data global_fields[] = {
430 #ifdef CONFIG_CTRL_IFACE
431 { STR(ctrl_interface) },
432 { STR(ctrl_interface_group) } /* deprecated */,
433 #endif /* CONFIG_CTRL_IFACE */
434 { INT_RANGE(eapol_version, 1, 2) },
436 { INT(fast_reauth) },
437 #ifdef EAP_TLS_OPENSSL
438 { STR(opensc_engine_path) },
439 { STR(pkcs11_engine_path) },
440 { STR(pkcs11_module_path) },
441 #endif /* EAP_TLS_OPENSSL */
442 { STR(driver_param) },
443 { INT(dot11RSNAConfigPMKLifetime) },
444 { INT(dot11RSNAConfigPMKReauthThreshold) },
445 { INT(dot11RSNAConfigSATimeout) },
446 #ifndef CONFIG_NO_CONFIG_WRITE
447 { INT(update_config) },
448 #endif /* CONFIG_NO_CONFIG_WRITE */
449 { FUNC_NO_VAR(load_dynamic_eap) },
452 { STR_RANGE(device_name, 0, 32) },
453 { STR_RANGE(manufacturer, 0, 64) },
454 { STR_RANGE(model_name, 0, 32) },
455 { STR_RANGE(model_number, 0, 32) },
456 { STR_RANGE(serial_number, 0, 32) },
457 { STR(device_type) },
458 { FUNC(os_version) },
459 { INT_RANGE(wps_cred_processing, 0, 2) },
460 #endif /* CONFIG_WPS */
471 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
474 static int wpa_config_process_global(struct wpa_config *config, char *pos,
480 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
481 const struct global_parse_data *field = &global_fields[i];
482 size_t flen = os_strlen(field->name);
483 if (os_strncmp(pos, field->name, flen) != 0 ||
487 if (field->parser(field, config, line, pos + flen + 1)) {
488 wpa_printf(MSG_ERROR, "Line %d: failed to "
489 "parse '%s'.", line, pos);
494 if (i == NUM_GLOBAL_FIELDS) {
495 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
504 struct wpa_config * wpa_config_read(const char *name)
508 int errors = 0, line = 0;
509 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
510 struct wpa_config *config;
513 config = wpa_config_alloc_empty(NULL, NULL);
516 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
517 f = fopen(name, "r");
523 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
524 if (os_strcmp(pos, "network={") == 0) {
525 ssid = wpa_config_read_network(f, &line, id++);
527 wpa_printf(MSG_ERROR, "Line %d: failed to "
528 "parse network block.", line);
538 if (wpa_config_add_prio_network(config, ssid)) {
539 wpa_printf(MSG_ERROR, "Line %d: failed to add "
540 "network block to priority list.",
545 #ifndef CONFIG_NO_CONFIG_BLOBS
546 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
547 if (wpa_config_process_blob(config, f, &line, pos + 12)
552 #endif /* CONFIG_NO_CONFIG_BLOBS */
553 } else if (wpa_config_process_global(config, pos, line) < 0) {
554 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
555 "line '%s'.", line, pos);
564 wpa_config_debug_dump_networks(config);
567 wpa_config_free(config);
576 #ifndef CONFIG_NO_CONFIG_WRITE
578 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
580 char *value = wpa_config_get(ssid, field);
583 fprintf(f, "\t%s=%s\n", field, value);
588 static void write_int(FILE *f, const char *field, int value, int def)
592 fprintf(f, "\t%s=%d\n", field, value);
596 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
598 char *value = wpa_config_get(ssid, "bssid");
601 fprintf(f, "\tbssid=%s\n", value);
606 static void write_psk(FILE *f, struct wpa_ssid *ssid)
608 char *value = wpa_config_get(ssid, "psk");
611 fprintf(f, "\tpsk=%s\n", value);
616 static void write_proto(FILE *f, struct wpa_ssid *ssid)
620 if (ssid->proto == DEFAULT_PROTO)
623 value = wpa_config_get(ssid, "proto");
627 fprintf(f, "\tproto=%s\n", value);
632 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
636 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
639 value = wpa_config_get(ssid, "key_mgmt");
643 fprintf(f, "\tkey_mgmt=%s\n", value);
648 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
652 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
655 value = wpa_config_get(ssid, "pairwise");
659 fprintf(f, "\tpairwise=%s\n", value);
664 static void write_group(FILE *f, struct wpa_ssid *ssid)
668 if (ssid->group_cipher == DEFAULT_GROUP)
671 value = wpa_config_get(ssid, "group");
675 fprintf(f, "\tgroup=%s\n", value);
680 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
684 if (ssid->auth_alg == 0)
687 value = wpa_config_get(ssid, "auth_alg");
691 fprintf(f, "\tauth_alg=%s\n", value);
696 #ifdef IEEE8021X_EAPOL
697 static void write_eap(FILE *f, struct wpa_ssid *ssid)
701 value = wpa_config_get(ssid, "eap");
706 fprintf(f, "\teap=%s\n", value);
709 #endif /* IEEE8021X_EAPOL */
712 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
714 char field[20], *value;
717 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
718 if (res < 0 || (size_t) res >= sizeof(field))
720 value = wpa_config_get(ssid, field);
722 fprintf(f, "\t%s=%s\n", field, value);
728 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
732 #define STR(t) write_str(f, #t, ssid)
733 #define INT(t) write_int(f, #t, ssid->t, 0)
734 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
735 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
736 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
740 write_bssid(f, ssid);
742 write_proto(f, ssid);
743 write_key_mgmt(f, ssid);
744 write_pairwise(f, ssid);
745 write_group(f, ssid);
746 write_auth_alg(f, ssid);
747 #ifdef IEEE8021X_EAPOL
750 STR(anonymous_identity);
756 STR(private_key_passwd);
759 STR(altsubject_match);
764 STR(private_key2_passwd);
767 STR(altsubject_match2);
783 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
784 #endif /* IEEE8021X_EAPOL */
785 for (i = 0; i < 4; i++)
786 write_wep_key(f, i, ssid);
789 #ifdef IEEE8021X_EAPOL
790 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
792 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
793 #endif /* IEEE8021X_EAPOL */
795 INT(proactive_key_caching);
798 #ifdef CONFIG_IEEE80211W
800 #endif /* CONFIG_IEEE80211W */
809 #ifndef CONFIG_NO_CONFIG_BLOBS
810 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
812 unsigned char *encoded;
814 encoded = base64_encode(blob->data, blob->len, NULL);
818 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
822 #endif /* CONFIG_NO_CONFIG_BLOBS */
825 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
827 #ifdef CONFIG_CTRL_IFACE
828 if (config->ctrl_interface)
829 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
830 if (config->ctrl_interface_group)
831 fprintf(f, "ctrl_interface_group=%s\n",
832 config->ctrl_interface_group);
833 #endif /* CONFIG_CTRL_IFACE */
834 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
835 fprintf(f, "eapol_version=%d\n", config->eapol_version);
836 if (config->ap_scan != DEFAULT_AP_SCAN)
837 fprintf(f, "ap_scan=%d\n", config->ap_scan);
838 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
839 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
840 #ifdef EAP_TLS_OPENSSL
841 if (config->opensc_engine_path)
842 fprintf(f, "opensc_engine_path=%s\n",
843 config->opensc_engine_path);
844 if (config->pkcs11_engine_path)
845 fprintf(f, "pkcs11_engine_path=%s\n",
846 config->pkcs11_engine_path);
847 if (config->pkcs11_module_path)
848 fprintf(f, "pkcs11_module_path=%s\n",
849 config->pkcs11_module_path);
850 #endif /* EAP_TLS_OPENSSL */
851 if (config->driver_param)
852 fprintf(f, "driver_param=%s\n", config->driver_param);
853 if (config->dot11RSNAConfigPMKLifetime)
854 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
855 config->dot11RSNAConfigPMKLifetime);
856 if (config->dot11RSNAConfigPMKReauthThreshold)
857 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
858 config->dot11RSNAConfigPMKReauthThreshold);
859 if (config->dot11RSNAConfigSATimeout)
860 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
861 config->dot11RSNAConfigSATimeout);
862 if (config->update_config)
863 fprintf(f, "update_config=%d\n", config->update_config);
865 if (!is_nil_uuid(config->uuid)) {
867 uuid_bin2str(config->uuid, buf, sizeof(buf));
868 fprintf(f, "uuid=%s\n", buf);
870 if (config->device_name)
871 fprintf(f, "device_name=%s\n", config->device_name);
872 if (config->manufacturer)
873 fprintf(f, "manufacturer=%s\n", config->manufacturer);
874 if (config->model_name)
875 fprintf(f, "model_name=%s\n", config->model_name);
876 if (config->model_number)
877 fprintf(f, "model_number=%s\n", config->model_number);
878 if (config->serial_number)
879 fprintf(f, "serial_number=%s\n", config->serial_number);
880 if (config->device_type)
881 fprintf(f, "device_type=%s\n", config->device_type);
882 if (WPA_GET_BE32(config->os_version))
883 fprintf(f, "os_version=%08x\n",
884 WPA_GET_BE32(config->os_version));
885 if (config->wps_cred_processing)
886 fprintf(f, "wps_cred_processing=%d\n",
887 config->wps_cred_processing);
888 #endif /* CONFIG_WPS */
889 if (config->country[0] && config->country[1]) {
890 fprintf(f, "country=%c%c\n",
891 config->country[0], config->country[1]);
895 #endif /* CONFIG_NO_CONFIG_WRITE */
898 int wpa_config_write(const char *name, struct wpa_config *config)
900 #ifndef CONFIG_NO_CONFIG_WRITE
902 struct wpa_ssid *ssid;
903 #ifndef CONFIG_NO_CONFIG_BLOBS
904 struct wpa_config_blob *blob;
905 #endif /* CONFIG_NO_CONFIG_BLOBS */
908 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
910 f = fopen(name, "w");
912 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
916 wpa_config_write_global(f, config);
918 for (ssid = config->ssid; ssid; ssid = ssid->next) {
919 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
920 continue; /* do not save temporary WPS networks */
921 fprintf(f, "\nnetwork={\n");
922 wpa_config_write_network(f, ssid);
926 #ifndef CONFIG_NO_CONFIG_BLOBS
927 for (blob = config->blobs; blob; blob = blob->next) {
928 ret = wpa_config_write_blob(f, blob);
932 #endif /* CONFIG_NO_CONFIG_BLOBS */
936 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
937 name, ret ? "un" : "");
939 #else /* CONFIG_NO_CONFIG_WRITE */
941 #endif /* CONFIG_NO_CONFIG_WRITE */