2 * WPA Supplicant / Configuration backend: text file
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * This file implements a configuration backend for text files. All the
9 * configuration information is stored in a text file that uses a format
10 * described in the sample configuration file, wpa_supplicant.conf.
22 * wpa_config_get_line - Read the next configuration file line
23 * @s: Buffer for the line
24 * @size: The buffer length
25 * @stream: File stream to read from
26 * @line: Pointer to a variable storing the file line number
27 * @_pos: Buffer for the pointer to the beginning of data on the text line or
28 * %NULL if not needed (returned value used instead)
29 * Returns: Pointer to the beginning of data on the text line or %NULL if no
30 * more text lines are available.
32 * This function reads the next non-empty line from the configuration file and
33 * removes comments. The returned string is guaranteed to be null-terminated.
35 static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
38 char *pos, *end, *sstart;
40 while (fgets(s, size, stream)) {
45 /* Skip white space from the beginning of line. */
46 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
49 /* Skip comment lines and empty lines */
50 if (*pos == '#' || *pos == '\n' || *pos == '\0')
54 * Remove # comments unless they are within a double quoted
57 sstart = os_strchr(pos, '"');
59 sstart = os_strrchr(sstart + 1, '"');
62 end = os_strchr(sstart, '#');
66 end = pos + os_strlen(pos) - 1;
68 /* Remove trailing white space. */
70 (*end == '\n' || *end == ' ' || *end == '\t' ||
88 static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
92 if (ssid->passphrase) {
94 wpa_printf(MSG_ERROR, "Line %d: both PSK and "
95 "passphrase configured.", line);
98 wpa_config_update_psk(ssid);
101 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set) {
102 wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
103 "management, but no PSK configured.", line);
107 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
108 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
109 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
110 /* Group cipher cannot be stronger than the pairwise cipher. */
111 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
112 " list since it was not allowed for pairwise "
114 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
121 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
123 struct wpa_ssid *ssid;
124 int errors = 0, end = 0;
125 char buf[256], *pos, *pos2;
127 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
129 ssid = os_zalloc(sizeof(*ssid));
134 wpa_config_set_network_defaults(ssid);
136 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
137 if (os_strcmp(pos, "}") == 0) {
142 pos2 = os_strchr(pos, '=');
144 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
145 "'%s'.", *line, pos);
152 if (os_strchr(pos2 + 1, '"') == NULL) {
153 wpa_printf(MSG_ERROR, "Line %d: invalid "
154 "quotation '%s'.", *line, pos2);
160 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
165 wpa_printf(MSG_ERROR, "Line %d: network block was not "
166 "terminated properly.", *line);
170 errors += wpa_config_validate_network(ssid, *line);
173 wpa_config_free_ssid(ssid);
181 static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id)
183 struct wpa_cred *cred;
184 int errors = 0, end = 0;
185 char buf[256], *pos, *pos2;
187 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line);
188 cred = os_zalloc(sizeof(*cred));
193 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
194 if (os_strcmp(pos, "}") == 0) {
199 pos2 = os_strchr(pos, '=');
201 wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
202 "'%s'.", *line, pos);
209 if (os_strchr(pos2 + 1, '"') == NULL) {
210 wpa_printf(MSG_ERROR, "Line %d: invalid "
211 "quotation '%s'.", *line, pos2);
217 if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
222 wpa_printf(MSG_ERROR, "Line %d: cred block was not "
223 "terminated properly.", *line);
228 wpa_config_free_cred(cred);
236 #ifndef CONFIG_NO_CONFIG_BLOBS
237 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
240 struct wpa_config_blob *blob;
242 unsigned char *encoded = NULL, *nencoded;
244 size_t encoded_len = 0, len;
246 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
249 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
250 if (os_strcmp(pos, "}") == 0) {
255 len = os_strlen(pos);
256 nencoded = os_realloc(encoded, encoded_len + len);
257 if (nencoded == NULL) {
258 wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
264 os_memcpy(encoded + encoded_len, pos, len);
269 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
275 blob = os_zalloc(sizeof(*blob));
280 blob->name = os_strdup(name);
281 blob->data = base64_decode(encoded, encoded_len, &blob->len);
284 if (blob->name == NULL || blob->data == NULL) {
285 wpa_config_free_blob(blob);
293 static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
294 int *line, char *bname)
297 struct wpa_config_blob *blob;
299 name_end = os_strchr(bname, '=');
300 if (name_end == NULL) {
301 wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
307 blob = wpa_config_read_blob(f, line, bname);
309 wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
313 wpa_config_set_blob(config, blob);
316 #endif /* CONFIG_NO_CONFIG_BLOBS */
319 struct wpa_config * wpa_config_read(const char *name)
323 int errors = 0, line = 0;
324 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
325 struct wpa_cred *cred, *cred_tail = NULL, *cred_head = NULL;
326 struct wpa_config *config;
330 config = wpa_config_alloc_empty(NULL, NULL);
333 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
334 f = fopen(name, "r");
340 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
341 if (os_strcmp(pos, "network={") == 0) {
342 ssid = wpa_config_read_network(f, &line, id++);
344 wpa_printf(MSG_ERROR, "Line %d: failed to "
345 "parse network block.", line);
355 if (wpa_config_add_prio_network(config, ssid)) {
356 wpa_printf(MSG_ERROR, "Line %d: failed to add "
357 "network block to priority list.",
362 } else if (os_strcmp(pos, "cred={") == 0) {
363 cred = wpa_config_read_cred(f, &line, cred_id++);
365 wpa_printf(MSG_ERROR, "Line %d: failed to "
366 "parse cred block.", line);
370 if (cred_head == NULL) {
371 cred_head = cred_tail = cred;
373 cred_tail->next = cred;
376 #ifndef CONFIG_NO_CONFIG_BLOBS
377 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
378 if (wpa_config_process_blob(config, f, &line, pos + 12)
383 #endif /* CONFIG_NO_CONFIG_BLOBS */
384 } else if (wpa_config_process_global(config, pos, line) < 0) {
385 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
386 "line '%s'.", line, pos);
395 wpa_config_debug_dump_networks(config);
396 config->cred = cred_head;
398 #ifndef WPA_IGNORE_CONFIG_ERRORS
400 wpa_config_free(config);
404 #endif /* WPA_IGNORE_CONFIG_ERRORS */
410 #ifndef CONFIG_NO_CONFIG_WRITE
412 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
414 char *value = wpa_config_get(ssid, field);
417 fprintf(f, "\t%s=%s\n", field, value);
422 static void write_int(FILE *f, const char *field, int value, int def)
426 fprintf(f, "\t%s=%d\n", field, value);
430 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
432 char *value = wpa_config_get(ssid, "bssid");
435 fprintf(f, "\tbssid=%s\n", value);
440 static void write_psk(FILE *f, struct wpa_ssid *ssid)
442 char *value = wpa_config_get(ssid, "psk");
445 fprintf(f, "\tpsk=%s\n", value);
450 static void write_proto(FILE *f, struct wpa_ssid *ssid)
454 if (ssid->proto == DEFAULT_PROTO)
457 value = wpa_config_get(ssid, "proto");
461 fprintf(f, "\tproto=%s\n", value);
466 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
470 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
473 value = wpa_config_get(ssid, "key_mgmt");
477 fprintf(f, "\tkey_mgmt=%s\n", value);
482 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
486 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
489 value = wpa_config_get(ssid, "pairwise");
493 fprintf(f, "\tpairwise=%s\n", value);
498 static void write_group(FILE *f, struct wpa_ssid *ssid)
502 if (ssid->group_cipher == DEFAULT_GROUP)
505 value = wpa_config_get(ssid, "group");
509 fprintf(f, "\tgroup=%s\n", value);
514 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
518 if (ssid->auth_alg == 0)
521 value = wpa_config_get(ssid, "auth_alg");
525 fprintf(f, "\tauth_alg=%s\n", value);
530 #ifdef IEEE8021X_EAPOL
531 static void write_eap(FILE *f, struct wpa_ssid *ssid)
535 value = wpa_config_get(ssid, "eap");
540 fprintf(f, "\teap=%s\n", value);
543 #endif /* IEEE8021X_EAPOL */
546 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
548 char field[20], *value;
551 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
552 if (res < 0 || (size_t) res >= sizeof(field))
554 value = wpa_config_get(ssid, field);
556 fprintf(f, "\t%s=%s\n", field, value);
563 static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
565 char *value = wpa_config_get(ssid, "p2p_client_list");
568 fprintf(f, "\tp2p_client_list=%s\n", value);
571 #endif /* CONFIG_P2P */
574 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
578 #define STR(t) write_str(f, #t, ssid)
579 #define INT(t) write_int(f, #t, ssid->t, 0)
580 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
581 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
582 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
586 write_bssid(f, ssid);
588 write_proto(f, ssid);
589 write_key_mgmt(f, ssid);
590 write_pairwise(f, ssid);
591 write_group(f, ssid);
592 write_auth_alg(f, ssid);
593 #ifdef IEEE8021X_EAPOL
596 STR(anonymous_identity);
602 STR(private_key_passwd);
605 STR(altsubject_match);
610 STR(private_key2_passwd);
613 STR(altsubject_match2);
629 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
630 #endif /* IEEE8021X_EAPOL */
631 for (i = 0; i < 4; i++)
632 write_wep_key(f, i, ssid);
635 #ifdef IEEE8021X_EAPOL
636 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
638 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
639 #endif /* IEEE8021X_EAPOL */
641 INT(proactive_key_caching);
644 #ifdef CONFIG_IEEE80211W
646 #endif /* CONFIG_IEEE80211W */
649 write_p2p_client_list(f, ssid);
650 #endif /* CONFIG_P2P */
658 static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
661 fprintf(f, "\tpriority=%d\n", cred->priority);
663 fprintf(f, "\trealm=\"%s\"\n", cred->realm);
665 fprintf(f, "\tusername=\"%s\"\n", cred->username);
667 fprintf(f, "\tpassword=\"%s\"\n", cred->password);
669 fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
671 fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
673 fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
675 fprintf(f, "\tdomain=\"%s\"\n", cred->domain);
679 #ifndef CONFIG_NO_CONFIG_BLOBS
680 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
682 unsigned char *encoded;
684 encoded = base64_encode(blob->data, blob->len, NULL);
688 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
692 #endif /* CONFIG_NO_CONFIG_BLOBS */
695 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
697 #ifdef CONFIG_CTRL_IFACE
698 if (config->ctrl_interface)
699 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
700 if (config->ctrl_interface_group)
701 fprintf(f, "ctrl_interface_group=%s\n",
702 config->ctrl_interface_group);
703 #endif /* CONFIG_CTRL_IFACE */
704 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
705 fprintf(f, "eapol_version=%d\n", config->eapol_version);
706 if (config->ap_scan != DEFAULT_AP_SCAN)
707 fprintf(f, "ap_scan=%d\n", config->ap_scan);
708 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
709 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
710 if (config->opensc_engine_path)
711 fprintf(f, "opensc_engine_path=%s\n",
712 config->opensc_engine_path);
713 if (config->pkcs11_engine_path)
714 fprintf(f, "pkcs11_engine_path=%s\n",
715 config->pkcs11_engine_path);
716 if (config->pkcs11_module_path)
717 fprintf(f, "pkcs11_module_path=%s\n",
718 config->pkcs11_module_path);
719 if (config->driver_param)
720 fprintf(f, "driver_param=%s\n", config->driver_param);
721 if (config->dot11RSNAConfigPMKLifetime)
722 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
723 config->dot11RSNAConfigPMKLifetime);
724 if (config->dot11RSNAConfigPMKReauthThreshold)
725 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
726 config->dot11RSNAConfigPMKReauthThreshold);
727 if (config->dot11RSNAConfigSATimeout)
728 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
729 config->dot11RSNAConfigSATimeout);
730 if (config->update_config)
731 fprintf(f, "update_config=%d\n", config->update_config);
733 if (!is_nil_uuid(config->uuid)) {
735 uuid_bin2str(config->uuid, buf, sizeof(buf));
736 fprintf(f, "uuid=%s\n", buf);
738 if (config->device_name)
739 fprintf(f, "device_name=%s\n", config->device_name);
740 if (config->manufacturer)
741 fprintf(f, "manufacturer=%s\n", config->manufacturer);
742 if (config->model_name)
743 fprintf(f, "model_name=%s\n", config->model_name);
744 if (config->model_number)
745 fprintf(f, "model_number=%s\n", config->model_number);
746 if (config->serial_number)
747 fprintf(f, "serial_number=%s\n", config->serial_number);
749 char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
750 buf = wps_dev_type_bin2str(config->device_type,
752 if (os_strcmp(buf, "0-00000000-0") != 0)
753 fprintf(f, "device_type=%s\n", buf);
755 if (WPA_GET_BE32(config->os_version))
756 fprintf(f, "os_version=%08x\n",
757 WPA_GET_BE32(config->os_version));
758 if (config->config_methods)
759 fprintf(f, "config_methods=%s\n", config->config_methods);
760 if (config->wps_cred_processing)
761 fprintf(f, "wps_cred_processing=%d\n",
762 config->wps_cred_processing);
763 #endif /* CONFIG_WPS */
765 if (config->p2p_listen_reg_class)
766 fprintf(f, "p2p_listen_reg_class=%u\n",
767 config->p2p_listen_reg_class);
768 if (config->p2p_listen_channel)
769 fprintf(f, "p2p_listen_channel=%u\n",
770 config->p2p_listen_channel);
771 if (config->p2p_oper_reg_class)
772 fprintf(f, "p2p_oper_reg_class=%u\n",
773 config->p2p_oper_reg_class);
774 if (config->p2p_oper_channel)
775 fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
776 if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
777 fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
778 if (config->p2p_ssid_postfix)
779 fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
780 if (config->persistent_reconnect)
781 fprintf(f, "persistent_reconnect=%u\n",
782 config->persistent_reconnect);
783 if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
784 fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
785 if (config->p2p_group_idle)
786 fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
787 #endif /* CONFIG_P2P */
788 if (config->country[0] && config->country[1]) {
789 fprintf(f, "country=%c%c\n",
790 config->country[0], config->country[1]);
792 if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
793 fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
794 if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
795 fprintf(f, "bss_expiration_age=%u\n",
796 config->bss_expiration_age);
797 if (config->bss_expiration_scan_count !=
798 DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
799 fprintf(f, "bss_expiration_scan_count=%u\n",
800 config->bss_expiration_scan_count);
801 if (config->filter_ssids)
802 fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
803 if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
804 fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
805 if (config->disassoc_low_ack)
806 fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
807 #ifdef CONFIG_INTERWORKING
808 if (config->interworking)
809 fprintf(f, "interworking=%u\n", config->interworking);
810 if (!is_zero_ether_addr(config->hessid))
811 fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
812 if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
813 fprintf(f, "access_network_type=%d\n",
814 config->access_network_type);
815 #endif /* CONFIG_INTERWORKING */
818 #endif /* CONFIG_NO_CONFIG_WRITE */
821 int wpa_config_write(const char *name, struct wpa_config *config)
823 #ifndef CONFIG_NO_CONFIG_WRITE
825 struct wpa_ssid *ssid;
826 struct wpa_cred *cred;
827 #ifndef CONFIG_NO_CONFIG_BLOBS
828 struct wpa_config_blob *blob;
829 #endif /* CONFIG_NO_CONFIG_BLOBS */
832 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
834 f = fopen(name, "w");
836 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
840 wpa_config_write_global(f, config);
842 for (cred = config->cred; cred; cred = cred->next) {
843 fprintf(f, "\ncred={\n");
844 wpa_config_write_cred(f, cred);
848 for (ssid = config->ssid; ssid; ssid = ssid->next) {
849 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
850 continue; /* do not save temporary networks */
851 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
853 continue; /* do not save invalid network */
854 fprintf(f, "\nnetwork={\n");
855 wpa_config_write_network(f, ssid);
859 #ifndef CONFIG_NO_CONFIG_BLOBS
860 for (blob = config->blobs; blob; blob = blob->next) {
861 ret = wpa_config_write_blob(f, blob);
865 #endif /* CONFIG_NO_CONFIG_BLOBS */
869 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
870 name, ret ? "un" : "");
872 #else /* CONFIG_NO_CONFIG_WRITE */
874 #endif /* CONFIG_NO_CONFIG_WRITE */