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);
268 #ifndef WPA_IGNORE_CONFIG_ERRORS
279 if (wpa_config_add_prio_network(config, ssid)) {
280 wpa_printf(MSG_ERROR, "Line %d: failed to add "
281 "network block to priority list.",
286 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
287 char *bname = pos + 12, *name_end;
288 struct wpa_config_blob *blob;
290 name_end = os_strchr(bname, '=');
291 if (name_end == NULL) {
292 wpa_printf(MSG_ERROR, "Line %d: no blob name "
299 blob = wpa_config_read_blob(f, &line, bname);
301 wpa_printf(MSG_ERROR, "Line %d: failed to read"
302 " blob %s", line, bname);
306 wpa_config_set_blob(config, blob);
307 #ifdef CONFIG_CTRL_IFACE
308 } else if (os_strncmp(pos, "ctrl_interface=", 15) == 0) {
309 os_free(config->ctrl_interface);
310 config->ctrl_interface = os_strdup(pos + 15);
311 wpa_printf(MSG_DEBUG, "ctrl_interface='%s'",
312 config->ctrl_interface);
313 } else if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0) {
314 os_free(config->ctrl_interface_group);
315 config->ctrl_interface_group = os_strdup(pos + 21);
316 wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' "
318 config->ctrl_interface_group);
319 #endif /* CONFIG_CTRL_IFACE */
320 } else if (os_strncmp(pos, "eapol_version=", 14) == 0) {
321 config->eapol_version = atoi(pos + 14);
322 if (config->eapol_version < 1 ||
323 config->eapol_version > 2) {
324 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL "
325 "version (%d): '%s'.",
326 line, config->eapol_version, pos);
330 wpa_printf(MSG_DEBUG, "eapol_version=%d",
331 config->eapol_version);
332 } else if (os_strncmp(pos, "ap_scan=", 8) == 0) {
333 config->ap_scan = atoi(pos + 8);
334 wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
335 } else if (os_strncmp(pos, "fast_reauth=", 12) == 0) {
336 config->fast_reauth = atoi(pos + 12);
337 wpa_printf(MSG_DEBUG, "fast_reauth=%d",
338 config->fast_reauth);
339 } else if (os_strncmp(pos, "opensc_engine_path=", 19) == 0) {
340 os_free(config->opensc_engine_path);
341 config->opensc_engine_path = os_strdup(pos + 19);
342 wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
343 config->opensc_engine_path);
344 } else if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0) {
345 os_free(config->pkcs11_engine_path);
346 config->pkcs11_engine_path = os_strdup(pos + 19);
347 wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
348 config->pkcs11_engine_path);
349 } else if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0) {
350 os_free(config->pkcs11_module_path);
351 config->pkcs11_module_path = os_strdup(pos + 19);
352 wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
353 config->pkcs11_module_path);
354 } else if (os_strncmp(pos, "driver_param=", 13) == 0) {
355 os_free(config->driver_param);
356 config->driver_param = os_strdup(pos + 13);
357 wpa_printf(MSG_DEBUG, "driver_param='%s'",
358 config->driver_param);
359 } else if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27)
361 config->dot11RSNAConfigPMKLifetime = atoi(pos + 27);
362 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
363 config->dot11RSNAConfigPMKLifetime);
364 } else if (os_strncmp(pos,
365 "dot11RSNAConfigPMKReauthThreshold=", 34)
367 config->dot11RSNAConfigPMKReauthThreshold =
369 wpa_printf(MSG_DEBUG,
370 "dot11RSNAConfigPMKReauthThreshold=%d",
371 config->dot11RSNAConfigPMKReauthThreshold);
372 } else if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) ==
374 config->dot11RSNAConfigSATimeout = atoi(pos + 25);
375 wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
376 config->dot11RSNAConfigSATimeout);
377 } else if (os_strncmp(pos, "update_config=", 14) == 0) {
378 config->update_config = atoi(pos + 14);
379 wpa_printf(MSG_DEBUG, "update_config=%d",
380 config->update_config);
381 } else if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0) {
384 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
385 ret = eap_peer_method_load(so);
387 wpa_printf(MSG_DEBUG, "This EAP type was "
388 "already loaded - not reloading.");
390 wpa_printf(MSG_ERROR, "Line %d: Failed to "
391 "load dynamic EAP method '%s'.",
396 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
397 "line '%s'.", line, pos);
406 wpa_config_debug_dump_networks(config);
408 #ifndef WPA_IGNORE_CONFIG_ERRORS
410 wpa_config_free(config);
419 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
421 char *value = wpa_config_get(ssid, field);
424 fprintf(f, "\t%s=%s\n", field, value);
429 static void write_int(FILE *f, const char *field, int value, int def)
433 fprintf(f, "\t%s=%d\n", field, value);
437 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
439 char *value = wpa_config_get(ssid, "bssid");
442 fprintf(f, "\tbssid=%s\n", value);
447 static void write_psk(FILE *f, struct wpa_ssid *ssid)
449 char *value = wpa_config_get(ssid, "psk");
452 fprintf(f, "\tpsk=%s\n", value);
457 static void write_proto(FILE *f, struct wpa_ssid *ssid)
461 if (ssid->proto == DEFAULT_PROTO)
464 value = wpa_config_get(ssid, "proto");
468 fprintf(f, "\tproto=%s\n", value);
473 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
477 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
480 value = wpa_config_get(ssid, "key_mgmt");
484 fprintf(f, "\tkey_mgmt=%s\n", value);
489 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
493 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
496 value = wpa_config_get(ssid, "pairwise");
500 fprintf(f, "\tpairwise=%s\n", value);
505 static void write_group(FILE *f, struct wpa_ssid *ssid)
509 if (ssid->group_cipher == DEFAULT_GROUP)
512 value = wpa_config_get(ssid, "group");
516 fprintf(f, "\tgroup=%s\n", value);
521 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
525 if (ssid->auth_alg == 0)
528 value = wpa_config_get(ssid, "auth_alg");
532 fprintf(f, "\tauth_alg=%s\n", value);
537 #ifdef IEEE8021X_EAPOL
538 static void write_eap(FILE *f, struct wpa_ssid *ssid)
542 value = wpa_config_get(ssid, "eap");
547 fprintf(f, "\teap=%s\n", value);
550 #endif /* IEEE8021X_EAPOL */
553 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
555 char field[20], *value;
557 os_snprintf(field, sizeof(field), "wep_key%d", idx);
558 value = wpa_config_get(ssid, field);
560 fprintf(f, "\t%s=%s\n", field, value);
566 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
570 #define STR(t) write_str(f, #t, ssid)
571 #define INT(t) write_int(f, #t, ssid->t, 0)
572 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
576 write_bssid(f, ssid);
578 write_proto(f, ssid);
579 write_key_mgmt(f, ssid);
580 write_pairwise(f, ssid);
581 write_group(f, ssid);
582 write_auth_alg(f, ssid);
583 #ifdef IEEE8021X_EAPOL
586 STR(anonymous_identity);
594 STR(private_key_passwd);
597 STR(altsubject_match);
602 STR(private_key2_passwd);
605 STR(altsubject_match2);
613 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
614 #endif /* IEEE8021X_EAPOL */
615 for (i = 0; i < 4; i++)
616 write_wep_key(f, i, ssid);
619 #ifdef IEEE8021X_EAPOL
620 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
622 INT_DEF(fragment_size, DEFAULT_FRAGMENT_SIZE);
623 #endif /* IEEE8021X_EAPOL */
625 INT(proactive_key_caching);
628 #ifdef CONFIG_IEEE80211W
630 #endif /* CONFIG_IEEE80211W */
639 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
641 unsigned char *encoded;
643 encoded = base64_encode(blob->data, blob->len, NULL);
647 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
653 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
655 #ifdef CONFIG_CTRL_IFACE
656 if (config->ctrl_interface)
657 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
658 if (config->ctrl_interface_group)
659 fprintf(f, "ctrl_interface_group=%s\n",
660 config->ctrl_interface_group);
661 #endif /* CONFIG_CTRL_IFACE */
662 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
663 fprintf(f, "eapol_version=%d\n", config->eapol_version);
664 if (config->ap_scan != DEFAULT_AP_SCAN)
665 fprintf(f, "ap_scan=%d\n", config->ap_scan);
666 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
667 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
668 if (config->opensc_engine_path)
669 fprintf(f, "opensc_engine_path=%s\n",
670 config->opensc_engine_path);
671 if (config->pkcs11_engine_path)
672 fprintf(f, "pkcs11_engine_path=%s\n",
673 config->pkcs11_engine_path);
674 if (config->pkcs11_module_path)
675 fprintf(f, "pkcs11_module_path=%s\n",
676 config->pkcs11_module_path);
677 if (config->driver_param)
678 fprintf(f, "driver_param=%s\n", config->driver_param);
679 if (config->dot11RSNAConfigPMKLifetime)
680 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
681 config->dot11RSNAConfigPMKLifetime);
682 if (config->dot11RSNAConfigPMKReauthThreshold)
683 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
684 config->dot11RSNAConfigPMKReauthThreshold);
685 if (config->dot11RSNAConfigSATimeout)
686 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
687 config->dot11RSNAConfigSATimeout);
688 if (config->update_config)
689 fprintf(f, "update_config=%d\n", config->update_config);
693 int wpa_config_write(const char *name, struct wpa_config *config)
696 struct wpa_ssid *ssid;
697 struct wpa_config_blob *blob;
700 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
702 f = fopen(name, "w");
704 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
708 wpa_config_write_global(f, config);
710 for (ssid = config->ssid; ssid; ssid = ssid->next) {
711 fprintf(f, "\nnetwork={\n");
712 wpa_config_write_network(f, ssid);
716 for (blob = config->blobs; blob; blob = blob->next) {
717 ret = wpa_config_write_blob(f, blob);
724 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
725 name, ret ? "un" : "");