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.
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 struct wpa_config * wpa_config_read(const char *name)
276 int errors = 0, line = 0;
277 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
278 struct wpa_config *config;
281 config = wpa_config_alloc_empty(NULL, NULL);
284 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
285 f = fopen(name, "r");
291 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
292 if (os_strcmp(pos, "network={") == 0) {
293 ssid = wpa_config_read_network(f, &line, id++);
295 wpa_printf(MSG_ERROR, "Line %d: failed to "
296 "parse network block.", line);
306 if (wpa_config_add_prio_network(config, ssid)) {
307 wpa_printf(MSG_ERROR, "Line %d: failed to add "
308 "network block to priority list.",
313 #ifndef CONFIG_NO_CONFIG_BLOBS
314 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
315 if (wpa_config_process_blob(config, f, &line, pos + 12)
320 #endif /* CONFIG_NO_CONFIG_BLOBS */
321 } else if (wpa_config_process_global(config, pos, line) < 0) {
322 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
323 "line '%s'.", line, pos);
332 wpa_config_debug_dump_networks(config);
334 #ifndef WPA_IGNORE_CONFIG_ERRORS
336 wpa_config_free(config);
340 #endif /* WPA_IGNORE_CONFIG_ERRORS */
346 #ifndef CONFIG_NO_CONFIG_WRITE
348 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
350 char *value = wpa_config_get(ssid, field);
353 fprintf(f, "\t%s=%s\n", field, value);
358 static void write_int(FILE *f, const char *field, int value, int def)
362 fprintf(f, "\t%s=%d\n", field, value);
366 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
368 char *value = wpa_config_get(ssid, "bssid");
371 fprintf(f, "\tbssid=%s\n", value);
376 static void write_psk(FILE *f, struct wpa_ssid *ssid)
378 char *value = wpa_config_get(ssid, "psk");
381 fprintf(f, "\tpsk=%s\n", value);
386 static void write_proto(FILE *f, struct wpa_ssid *ssid)
390 if (ssid->proto == DEFAULT_PROTO)
393 value = wpa_config_get(ssid, "proto");
397 fprintf(f, "\tproto=%s\n", value);
402 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
406 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
409 value = wpa_config_get(ssid, "key_mgmt");
413 fprintf(f, "\tkey_mgmt=%s\n", value);
418 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
422 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
425 value = wpa_config_get(ssid, "pairwise");
429 fprintf(f, "\tpairwise=%s\n", value);
434 static void write_group(FILE *f, struct wpa_ssid *ssid)
438 if (ssid->group_cipher == DEFAULT_GROUP)
441 value = wpa_config_get(ssid, "group");
445 fprintf(f, "\tgroup=%s\n", value);
450 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
454 if (ssid->auth_alg == 0)
457 value = wpa_config_get(ssid, "auth_alg");
461 fprintf(f, "\tauth_alg=%s\n", value);
466 #ifdef IEEE8021X_EAPOL
467 static void write_eap(FILE *f, struct wpa_ssid *ssid)
471 value = wpa_config_get(ssid, "eap");
476 fprintf(f, "\teap=%s\n", value);
479 #endif /* IEEE8021X_EAPOL */
482 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
484 char field[20], *value;
487 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
488 if (res < 0 || (size_t) res >= sizeof(field))
490 value = wpa_config_get(ssid, field);
492 fprintf(f, "\t%s=%s\n", field, value);
498 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
502 #define STR(t) write_str(f, #t, ssid)
503 #define INT(t) write_int(f, #t, ssid->t, 0)
504 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
505 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
506 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
510 write_bssid(f, ssid);
512 write_proto(f, ssid);
513 write_key_mgmt(f, ssid);
514 write_pairwise(f, ssid);
515 write_group(f, ssid);
516 write_auth_alg(f, ssid);
517 #ifdef IEEE8021X_EAPOL
520 STR(anonymous_identity);
526 STR(private_key_passwd);
529 STR(altsubject_match);
534 STR(private_key2_passwd);
537 STR(altsubject_match2);
553 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
554 #endif /* IEEE8021X_EAPOL */
555 for (i = 0; i < 4; i++)
556 write_wep_key(f, i, ssid);
559 #ifdef IEEE8021X_EAPOL
560 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
562 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
563 #endif /* IEEE8021X_EAPOL */
565 INT(proactive_key_caching);
568 #ifdef CONFIG_IEEE80211W
570 #endif /* CONFIG_IEEE80211W */
579 #ifndef CONFIG_NO_CONFIG_BLOBS
580 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
582 unsigned char *encoded;
584 encoded = base64_encode(blob->data, blob->len, NULL);
588 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
592 #endif /* CONFIG_NO_CONFIG_BLOBS */
595 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
597 #ifdef CONFIG_CTRL_IFACE
598 if (config->ctrl_interface)
599 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
600 if (config->ctrl_interface_group)
601 fprintf(f, "ctrl_interface_group=%s\n",
602 config->ctrl_interface_group);
603 #endif /* CONFIG_CTRL_IFACE */
604 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
605 fprintf(f, "eapol_version=%d\n", config->eapol_version);
606 if (config->ap_scan != DEFAULT_AP_SCAN)
607 fprintf(f, "ap_scan=%d\n", config->ap_scan);
608 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
609 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
610 if (config->opensc_engine_path)
611 fprintf(f, "opensc_engine_path=%s\n",
612 config->opensc_engine_path);
613 if (config->pkcs11_engine_path)
614 fprintf(f, "pkcs11_engine_path=%s\n",
615 config->pkcs11_engine_path);
616 if (config->pkcs11_module_path)
617 fprintf(f, "pkcs11_module_path=%s\n",
618 config->pkcs11_module_path);
619 if (config->driver_param)
620 fprintf(f, "driver_param=%s\n", config->driver_param);
621 if (config->dot11RSNAConfigPMKLifetime)
622 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
623 config->dot11RSNAConfigPMKLifetime);
624 if (config->dot11RSNAConfigPMKReauthThreshold)
625 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
626 config->dot11RSNAConfigPMKReauthThreshold);
627 if (config->dot11RSNAConfigSATimeout)
628 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
629 config->dot11RSNAConfigSATimeout);
630 if (config->update_config)
631 fprintf(f, "update_config=%d\n", config->update_config);
633 if (!is_nil_uuid(config->uuid)) {
635 uuid_bin2str(config->uuid, buf, sizeof(buf));
636 fprintf(f, "uuid=%s\n", buf);
638 if (config->device_name)
639 fprintf(f, "device_name=%s\n", config->device_name);
640 if (config->manufacturer)
641 fprintf(f, "manufacturer=%s\n", config->manufacturer);
642 if (config->model_name)
643 fprintf(f, "model_name=%s\n", config->model_name);
644 if (config->model_number)
645 fprintf(f, "model_number=%s\n", config->model_number);
646 if (config->serial_number)
647 fprintf(f, "serial_number=%s\n", config->serial_number);
649 char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
650 buf = wps_dev_type_bin2str(config->device_type,
652 fprintf(f, "device_type=%s\n", buf);
654 if (WPA_GET_BE32(config->os_version))
655 fprintf(f, "os_version=%08x\n",
656 WPA_GET_BE32(config->os_version));
657 if (config->config_methods)
658 fprintf(f, "config_methods=%s\n", config->config_methods);
659 if (config->wps_cred_processing)
660 fprintf(f, "wps_cred_processing=%d\n",
661 config->wps_cred_processing);
662 #endif /* CONFIG_WPS */
664 if (config->p2p_listen_reg_class)
665 fprintf(f, "p2p_listen_reg_class=%u\n",
666 config->p2p_listen_reg_class);
667 if (config->p2p_listen_channel)
668 fprintf(f, "p2p_listen_channel=%u\n",
669 config->p2p_listen_channel);
670 if (config->p2p_oper_reg_class)
671 fprintf(f, "p2p_oper_reg_class=%u\n",
672 config->p2p_oper_reg_class);
673 if (config->p2p_oper_channel)
674 fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
675 if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
676 fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
677 if (config->p2p_ssid_postfix)
678 fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
679 if (config->persistent_reconnect)
680 fprintf(f, "persistent_reconnect=%u\n",
681 config->persistent_reconnect);
682 if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
683 fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
684 if (config->p2p_group_idle)
685 fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
686 #endif /* CONFIG_P2P */
687 if (config->country[0] && config->country[1]) {
688 fprintf(f, "country=%c%c\n",
689 config->country[0], config->country[1]);
691 if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
692 fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
693 if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
694 fprintf(f, "bss_expiration_age=%u\n",
695 config->bss_expiration_age);
696 if (config->bss_expiration_scan_count !=
697 DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
698 fprintf(f, "bss_expiration_scan_count=%u\n",
699 config->bss_expiration_scan_count);
700 if (config->filter_ssids)
701 fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
702 if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
703 fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
704 if (config->disassoc_low_ack)
705 fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
706 #ifdef CONFIG_INTERWORKING
707 if (config->home_realm)
708 fprintf(f, "home_realm=%s\n", config->home_realm);
709 if (config->home_username)
710 fprintf(f, "home_username=%s\n", config->home_username);
711 if (config->home_password)
712 fprintf(f, "home_password=%s\n", config->home_password);
713 if (config->home_ca_cert)
714 fprintf(f, "home_ca_cert=%s\n", config->home_ca_cert);
715 if (config->home_imsi)
716 fprintf(f, "home_imsi=%s\n", config->home_imsi);
717 if (config->home_milenage)
718 fprintf(f, "home_milenage=%s\n", config->home_milenage);
719 if (config->interworking)
720 fprintf(f, "interworking=%u\n", config->interworking);
721 if (!is_zero_ether_addr(config->hessid))
722 fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
723 if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
724 fprintf(f, "access_network_type=%d\n",
725 config->access_network_type);
726 #endif /* CONFIG_INTERWORKING */
729 #endif /* CONFIG_NO_CONFIG_WRITE */
732 int wpa_config_write(const char *name, struct wpa_config *config)
734 #ifndef CONFIG_NO_CONFIG_WRITE
736 struct wpa_ssid *ssid;
737 #ifndef CONFIG_NO_CONFIG_BLOBS
738 struct wpa_config_blob *blob;
739 #endif /* CONFIG_NO_CONFIG_BLOBS */
742 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
744 f = fopen(name, "w");
746 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
750 wpa_config_write_global(f, config);
752 for (ssid = config->ssid; ssid; ssid = ssid->next) {
753 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
754 continue; /* do not save temporary networks */
755 fprintf(f, "\nnetwork={\n");
756 wpa_config_write_network(f, ssid);
760 #ifndef CONFIG_NO_CONFIG_BLOBS
761 for (blob = config->blobs; blob; blob = blob->next) {
762 ret = wpa_config_write_blob(f, blob);
766 #endif /* CONFIG_NO_CONFIG_BLOBS */
770 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
771 name, ret ? "un" : "");
773 #else /* CONFIG_NO_CONFIG_WRITE */
775 #endif /* CONFIG_NO_CONFIG_WRITE */