2 * WPA Supplicant / Configuration backend: text file
3 * Copyright (c) 2003-2006, 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.
24 #include "eap_methods.h"
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
) && !ssid
->psk_set
) {
108 wpa_printf(MSG_ERROR
, "Line %d: WPA-PSK accepted for key "
109 "management, but no PSK configured.", line
);
113 if ((ssid
->group_cipher
& WPA_CIPHER_CCMP
) &&
114 !(ssid
->pairwise_cipher
& WPA_CIPHER_CCMP
) &&
115 !(ssid
->pairwise_cipher
& WPA_CIPHER_NONE
)) {
116 /* Group cipher cannot be stronger than the pairwise cipher. */
117 wpa_printf(MSG_DEBUG
, "Line %d: removed CCMP from group cipher"
118 " list since it was not allowed for pairwise "
120 ssid
->group_cipher
&= ~WPA_CIPHER_CCMP
;
127 static struct wpa_ssid
* wpa_config_read_network(FILE *f
, int *line
, int id
)
129 struct wpa_ssid
*ssid
;
130 int errors
= 0, end
= 0;
131 char buf
[256], *pos
, *pos2
;
133 wpa_printf(MSG_MSGDUMP
, "Line: %d - start of a new network block",
135 ssid
= os_zalloc(sizeof(*ssid
));
140 wpa_config_set_network_defaults(ssid
);
142 while (wpa_config_get_line(buf
, sizeof(buf
), f
, line
, &pos
)) {
143 if (os_strcmp(pos
, "}") == 0) {
148 pos2
= os_strchr(pos
, '=');
150 wpa_printf(MSG_ERROR
, "Line %d: Invalid SSID line "
151 "'%s'.", *line
, pos
);
158 if (os_strchr(pos2
+ 1, '"') == NULL
) {
159 wpa_printf(MSG_ERROR
, "Line %d: invalid "
160 "quotation '%s'.", *line
, pos2
);
166 if (wpa_config_set(ssid
, pos
, pos2
, *line
) < 0)
171 wpa_printf(MSG_ERROR
, "Line %d: network block was not "
172 "terminated properly.", *line
);
176 errors
+= wpa_config_validate_network(ssid
, *line
);
179 wpa_config_free_ssid(ssid
);
187 static struct wpa_config_blob
* wpa_config_read_blob(FILE *f
, int *line
,
190 struct wpa_config_blob
*blob
;
192 unsigned char *encoded
= NULL
, *nencoded
;
194 size_t encoded_len
= 0, len
;
196 wpa_printf(MSG_MSGDUMP
, "Line: %d - start of a new named blob '%s'",
199 while (wpa_config_get_line(buf
, sizeof(buf
), f
, line
, &pos
)) {
200 if (os_strcmp(pos
, "}") == 0) {
205 len
= os_strlen(pos
);
206 nencoded
= os_realloc(encoded
, encoded_len
+ len
);
207 if (nencoded
== NULL
) {
208 wpa_printf(MSG_ERROR
, "Line %d: not enough memory for "
214 os_memcpy(encoded
+ encoded_len
, pos
, len
);
219 wpa_printf(MSG_ERROR
, "Line %d: blob was not terminated "
225 blob
= os_zalloc(sizeof(*blob
));
230 blob
->name
= os_strdup(name
);
231 blob
->data
= base64_decode(encoded
, encoded_len
, &blob
->len
);
234 if (blob
->name
== NULL
|| blob
->data
== NULL
) {
235 wpa_config_free_blob(blob
);
243 struct wpa_config
* wpa_config_read(const char *name
)
247 int errors
= 0, line
= 0;
248 struct wpa_ssid
*ssid
, *tail
= NULL
, *head
= NULL
;
249 struct wpa_config
*config
;
252 config
= wpa_config_alloc_empty(NULL
, NULL
);
255 wpa_printf(MSG_DEBUG
, "Reading configuration file '%s'", name
);
256 f
= fopen(name
, "r");
262 while (wpa_config_get_line(buf
, sizeof(buf
), f
, &line
, &pos
)) {
263 if (os_strcmp(pos
, "network={") == 0) {
264 ssid
= wpa_config_read_network(f
, &line
, id
++);
266 wpa_printf(MSG_ERROR
, "Line %d: failed to "
267 "parse network block.", line
);
277 if (wpa_config_add_prio_network(config
, ssid
)) {
278 wpa_printf(MSG_ERROR
, "Line %d: failed to add "
279 "network block to priority list.",
284 } else if (os_strncmp(pos
, "blob-base64-", 12) == 0) {
285 char *bname
= pos
+ 12, *name_end
;
286 struct wpa_config_blob
*blob
;
288 name_end
= os_strchr(bname
, '=');
289 if (name_end
== NULL
) {
290 wpa_printf(MSG_ERROR
, "Line %d: no blob name "
297 blob
= wpa_config_read_blob(f
, &line
, bname
);
299 wpa_printf(MSG_ERROR
, "Line %d: failed to read"
300 " blob %s", line
, bname
);
304 wpa_config_set_blob(config
, blob
);
305 #ifdef CONFIG_CTRL_IFACE
306 } else if (os_strncmp(pos
, "ctrl_interface=", 15) == 0) {
307 os_free(config
->ctrl_interface
);
308 config
->ctrl_interface
= os_strdup(pos
+ 15);
309 wpa_printf(MSG_DEBUG
, "ctrl_interface='%s'",
310 config
->ctrl_interface
);
311 } else if (os_strncmp(pos
, "ctrl_interface_group=", 21) == 0) {
312 os_free(config
->ctrl_interface_group
);
313 config
->ctrl_interface_group
= os_strdup(pos
+ 21);
314 wpa_printf(MSG_DEBUG
, "ctrl_interface_group='%s' "
316 config
->ctrl_interface_group
);
317 #endif /* CONFIG_CTRL_IFACE */
318 } else if (os_strncmp(pos
, "eapol_version=", 14) == 0) {
319 config
->eapol_version
= atoi(pos
+ 14);
320 if (config
->eapol_version
< 1 ||
321 config
->eapol_version
> 2) {
322 wpa_printf(MSG_ERROR
, "Line %d: Invalid EAPOL "
323 "version (%d): '%s'.",
324 line
, config
->eapol_version
, pos
);
328 wpa_printf(MSG_DEBUG
, "eapol_version=%d",
329 config
->eapol_version
);
330 } else if (os_strncmp(pos
, "ap_scan=", 8) == 0) {
331 config
->ap_scan
= atoi(pos
+ 8);
332 wpa_printf(MSG_DEBUG
, "ap_scan=%d", config
->ap_scan
);
333 } else if (os_strncmp(pos
, "fast_reauth=", 12) == 0) {
334 config
->fast_reauth
= atoi(pos
+ 12);
335 wpa_printf(MSG_DEBUG
, "fast_reauth=%d",
336 config
->fast_reauth
);
337 } else if (os_strncmp(pos
, "opensc_engine_path=", 19) == 0) {
338 os_free(config
->opensc_engine_path
);
339 config
->opensc_engine_path
= os_strdup(pos
+ 19);
340 wpa_printf(MSG_DEBUG
, "opensc_engine_path='%s'",
341 config
->opensc_engine_path
);
342 } else if (os_strncmp(pos
, "pkcs11_engine_path=", 19) == 0) {
343 os_free(config
->pkcs11_engine_path
);
344 config
->pkcs11_engine_path
= os_strdup(pos
+ 19);
345 wpa_printf(MSG_DEBUG
, "pkcs11_engine_path='%s'",
346 config
->pkcs11_engine_path
);
347 } else if (os_strncmp(pos
, "pkcs11_module_path=", 19) == 0) {
348 os_free(config
->pkcs11_module_path
);
349 config
->pkcs11_module_path
= os_strdup(pos
+ 19);
350 wpa_printf(MSG_DEBUG
, "pkcs11_module_path='%s'",
351 config
->pkcs11_module_path
);
352 } else if (os_strncmp(pos
, "driver_param=", 13) == 0) {
353 os_free(config
->driver_param
);
354 config
->driver_param
= os_strdup(pos
+ 13);
355 wpa_printf(MSG_DEBUG
, "driver_param='%s'",
356 config
->driver_param
);
357 } else if (os_strncmp(pos
, "dot11RSNAConfigPMKLifetime=", 27)
359 config
->dot11RSNAConfigPMKLifetime
= atoi(pos
+ 27);
360 wpa_printf(MSG_DEBUG
, "dot11RSNAConfigPMKLifetime=%d",
361 config
->dot11RSNAConfigPMKLifetime
);
362 } else if (os_strncmp(pos
,
363 "dot11RSNAConfigPMKReauthThreshold=", 34)
365 config
->dot11RSNAConfigPMKReauthThreshold
=
367 wpa_printf(MSG_DEBUG
,
368 "dot11RSNAConfigPMKReauthThreshold=%d",
369 config
->dot11RSNAConfigPMKReauthThreshold
);
370 } else if (os_strncmp(pos
, "dot11RSNAConfigSATimeout=", 25) ==
372 config
->dot11RSNAConfigSATimeout
= atoi(pos
+ 25);
373 wpa_printf(MSG_DEBUG
, "dot11RSNAConfigSATimeout=%d",
374 config
->dot11RSNAConfigSATimeout
);
375 } else if (os_strncmp(pos
, "update_config=", 14) == 0) {
376 config
->update_config
= atoi(pos
+ 14);
377 wpa_printf(MSG_DEBUG
, "update_config=%d",
378 config
->update_config
);
379 } else if (os_strncmp(pos
, "load_dynamic_eap=", 17) == 0) {
382 wpa_printf(MSG_DEBUG
, "load_dynamic_eap=%s", so
);
383 ret
= eap_peer_method_load(so
);
385 wpa_printf(MSG_DEBUG
, "This EAP type was "
386 "already loaded - not reloading.");
388 wpa_printf(MSG_ERROR
, "Line %d: Failed to "
389 "load dynamic EAP method '%s'.",
394 wpa_printf(MSG_ERROR
, "Line %d: Invalid configuration "
395 "line '%s'.", line
, pos
);
404 wpa_config_debug_dump_networks(config
);
407 wpa_config_free(config
);
416 static void write_str(FILE *f
, const char *field
, struct wpa_ssid
*ssid
)
418 char *value
= wpa_config_get(ssid
, field
);
421 fprintf(f
, "\t%s=%s\n", field
, value
);
426 static void write_int(FILE *f
, const char *field
, int value
, int def
)
430 fprintf(f
, "\t%s=%d\n", field
, value
);
434 static void write_bssid(FILE *f
, struct wpa_ssid
*ssid
)
436 char *value
= wpa_config_get(ssid
, "bssid");
439 fprintf(f
, "\tbssid=%s\n", value
);
444 static void write_psk(FILE *f
, struct wpa_ssid
*ssid
)
446 char *value
= wpa_config_get(ssid
, "psk");
449 fprintf(f
, "\tpsk=%s\n", value
);
454 static void write_proto(FILE *f
, struct wpa_ssid
*ssid
)
458 if (ssid
->proto
== DEFAULT_PROTO
)
461 value
= wpa_config_get(ssid
, "proto");
465 fprintf(f
, "\tproto=%s\n", value
);
470 static void write_key_mgmt(FILE *f
, struct wpa_ssid
*ssid
)
474 if (ssid
->key_mgmt
== DEFAULT_KEY_MGMT
)
477 value
= wpa_config_get(ssid
, "key_mgmt");
481 fprintf(f
, "\tkey_mgmt=%s\n", value
);
486 static void write_pairwise(FILE *f
, struct wpa_ssid
*ssid
)
490 if (ssid
->pairwise_cipher
== DEFAULT_PAIRWISE
)
493 value
= wpa_config_get(ssid
, "pairwise");
497 fprintf(f
, "\tpairwise=%s\n", value
);
502 static void write_group(FILE *f
, struct wpa_ssid
*ssid
)
506 if (ssid
->group_cipher
== DEFAULT_GROUP
)
509 value
= wpa_config_get(ssid
, "group");
513 fprintf(f
, "\tgroup=%s\n", value
);
518 static void write_auth_alg(FILE *f
, struct wpa_ssid
*ssid
)
522 if (ssid
->auth_alg
== 0)
525 value
= wpa_config_get(ssid
, "auth_alg");
529 fprintf(f
, "\tauth_alg=%s\n", value
);
534 #ifdef IEEE8021X_EAPOL
535 static void write_eap(FILE *f
, struct wpa_ssid
*ssid
)
539 value
= wpa_config_get(ssid
, "eap");
544 fprintf(f
, "\teap=%s\n", value
);
547 #endif /* IEEE8021X_EAPOL */
550 static void write_wep_key(FILE *f
, int idx
, struct wpa_ssid
*ssid
)
552 char field
[20], *value
;
554 os_snprintf(field
, sizeof(field
), "wep_key%d", idx
);
555 value
= wpa_config_get(ssid
, field
);
557 fprintf(f
, "\t%s=%s\n", field
, value
);
563 static void wpa_config_write_network(FILE *f
, struct wpa_ssid
*ssid
)
567 #define STR(t) write_str(f, #t, ssid)
568 #define INT(t) write_int(f, #t, ssid->t, 0)
569 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
573 write_bssid(f
, ssid
);
575 write_proto(f
, ssid
);
576 write_key_mgmt(f
, ssid
);
577 write_pairwise(f
, ssid
);
578 write_group(f
, ssid
);
579 write_auth_alg(f
, ssid
);
580 #ifdef IEEE8021X_EAPOL
583 STR(anonymous_identity
);
591 STR(private_key_passwd
);
594 STR(altsubject_match
);
599 STR(private_key2_passwd
);
602 STR(altsubject_match2
);
610 INT_DEF(eapol_flags
, DEFAULT_EAPOL_FLAGS
);
611 #endif /* IEEE8021X_EAPOL */
612 for (i
= 0; i
< 4; i
++)
613 write_wep_key(f
, i
, ssid
);
616 #ifdef IEEE8021X_EAPOL
617 INT_DEF(eap_workaround
, DEFAULT_EAP_WORKAROUND
);
619 INT_DEF(fragment_size
, DEFAULT_FRAGMENT_SIZE
);
620 #endif /* IEEE8021X_EAPOL */
622 INT(proactive_key_caching
);
625 #ifdef CONFIG_IEEE80211W
627 #endif /* CONFIG_IEEE80211W */
636 static int wpa_config_write_blob(FILE *f
, struct wpa_config_blob
*blob
)
638 unsigned char *encoded
;
640 encoded
= base64_encode(blob
->data
, blob
->len
, NULL
);
644 fprintf(f
, "\nblob-base64-%s={\n%s}\n", blob
->name
, encoded
);
650 static void wpa_config_write_global(FILE *f
, struct wpa_config
*config
)
652 #ifdef CONFIG_CTRL_IFACE
653 if (config
->ctrl_interface
)
654 fprintf(f
, "ctrl_interface=%s\n", config
->ctrl_interface
);
655 if (config
->ctrl_interface_group
)
656 fprintf(f
, "ctrl_interface_group=%s\n",
657 config
->ctrl_interface_group
);
658 #endif /* CONFIG_CTRL_IFACE */
659 if (config
->eapol_version
!= DEFAULT_EAPOL_VERSION
)
660 fprintf(f
, "eapol_version=%d\n", config
->eapol_version
);
661 if (config
->ap_scan
!= DEFAULT_AP_SCAN
)
662 fprintf(f
, "ap_scan=%d\n", config
->ap_scan
);
663 if (config
->fast_reauth
!= DEFAULT_FAST_REAUTH
)
664 fprintf(f
, "fast_reauth=%d\n", config
->fast_reauth
);
665 if (config
->opensc_engine_path
)
666 fprintf(f
, "opensc_engine_path=%s\n",
667 config
->opensc_engine_path
);
668 if (config
->pkcs11_engine_path
)
669 fprintf(f
, "pkcs11_engine_path=%s\n",
670 config
->pkcs11_engine_path
);
671 if (config
->pkcs11_module_path
)
672 fprintf(f
, "pkcs11_module_path=%s\n",
673 config
->pkcs11_module_path
);
674 if (config
->driver_param
)
675 fprintf(f
, "driver_param=%s\n", config
->driver_param
);
676 if (config
->dot11RSNAConfigPMKLifetime
)
677 fprintf(f
, "dot11RSNAConfigPMKLifetime=%d\n",
678 config
->dot11RSNAConfigPMKLifetime
);
679 if (config
->dot11RSNAConfigPMKReauthThreshold
)
680 fprintf(f
, "dot11RSNAConfigPMKReauthThreshold=%d\n",
681 config
->dot11RSNAConfigPMKReauthThreshold
);
682 if (config
->dot11RSNAConfigSATimeout
)
683 fprintf(f
, "dot11RSNAConfigSATimeout=%d\n",
684 config
->dot11RSNAConfigSATimeout
);
685 if (config
->update_config
)
686 fprintf(f
, "update_config=%d\n", config
->update_config
);
690 int wpa_config_write(const char *name
, struct wpa_config
*config
)
693 struct wpa_ssid
*ssid
;
694 struct wpa_config_blob
*blob
;
697 wpa_printf(MSG_DEBUG
, "Writing configuration file '%s'", name
);
699 f
= fopen(name
, "w");
701 wpa_printf(MSG_DEBUG
, "Failed to open '%s' for writing", name
);
705 wpa_config_write_global(f
, config
);
707 for (ssid
= config
->ssid
; ssid
; ssid
= ssid
->next
) {
708 fprintf(f
, "\nnetwork={\n");
709 wpa_config_write_network(f
, ssid
);
713 for (blob
= config
->blobs
; blob
; blob
= blob
->next
) {
714 ret
= wpa_config_write_blob(f
, blob
);
721 wpa_printf(MSG_DEBUG
, "Configuration file '%s' written %ssuccessfully",
722 name
, ret
? "un" : "");