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"
30 * wpa_config_get_line - Read the next configuration file line
31 * @s: Buffer for the line
32 * @size: The buffer length
33 * @stream: File stream to read from
34 * @line: Pointer to a variable storing the file line number
35 * @_pos: Buffer for the pointer to the beginning of data on the text line or
36 * %NULL if not needed (returned value used instead)
37 * Returns: Pointer to the beginning of data on the text line or %NULL if no
38 * more text lines are available.
40 * This function reads the next non-empty line from the configuration file and
41 * removes comments. The returned string is guaranteed to be null-terminated.
43 static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
46 char *pos, *end, *sstart;
48 while (fgets(s, size, stream)) {
53 /* Skip white space from the beginning of line. */
54 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
57 /* Skip comment lines and empty lines */
58 if (*pos == '#' || *pos == '\n' || *pos == '\0')
62 * Remove # comments unless they are within a double quoted
65 sstart = os_strchr(pos, '"');
67 sstart = os_strrchr(sstart + 1, '"');
70 end = os_strchr(sstart, '#');
74 end = pos + os_strlen(pos) - 1;
76 /* Remove trailing white space. */
78 (*end == '\n' || *end == ' ' || *end == '\t' ||
96 static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
100 if (ssid->passphrase) {
102 wpa_printf(MSG_ERROR, "Line %d: both PSK and "
103 "passphrase configured.", line);
106 wpa_config_update_psk(ssid);
109 if ((ssid->key_mgmt & WPA_KEY_MGMT_PSK) && !ssid->psk_set) {
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 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
192 struct wpa_config_blob *blob;
194 unsigned char *encoded = NULL, *nencoded;
196 size_t encoded_len = 0, len;
198 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
201 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
202 if (os_strcmp(pos, "}") == 0) {
207 len = os_strlen(pos);
208 nencoded = os_realloc(encoded, encoded_len + len);
209 if (nencoded == NULL) {
210 wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
216 os_memcpy(encoded + encoded_len, pos, len);
221 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
227 blob = os_zalloc(sizeof(*blob));
232 blob->name = os_strdup(name);
233 blob->data = base64_decode(encoded, encoded_len, &blob->len);
236 if (blob->name == NULL || blob->data == NULL) {
237 wpa_config_free_blob(blob);
245 struct wpa_config * wpa_config_read(const char *name)
249 int errors = 0, line = 0;
250 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
251 struct wpa_config *config;
254 config = wpa_config_alloc_empty(NULL, NULL);
257 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
258 f = fopen(name, "r");
263 /* When creating the config file, give group read/write access
264 * to allow backup and restoring the file.
266 chmod(name, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
268 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
269 if (os_strcmp(pos, "network={") == 0) {
270 ssid = wpa_config_read_network(f, &line, id++);
272 wpa_printf(MSG_ERROR, "Line %d: failed to "
273 "parse network block.", line);
274 #ifndef WPA_IGNORE_CONFIG_ERRORS
285 if (wpa_config_add_prio_network(config, ssid)) {
286 wpa_printf(MSG_ERROR, "Line %d: failed to add "
287 "network block to priority list.",
292 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
293 char *bname = pos + 12, *name_end;
294 struct wpa_config_blob *blob;
296 name_end = os_strchr(bname, '=');
297 if (name_end == NULL) {
298 wpa_printf(MSG_ERROR, "Line %d: no blob name "
305 blob = wpa_config_read_blob(f, &line, bname);
307 wpa_printf(MSG_ERROR, "Line %d: failed to read"
308 " blob %s", line, bname);
312 wpa_config_set_blob(config, blob);
313 #ifdef CONFIG_CTRL_IFACE
314 } else if (os_strncmp(pos, "ctrl_interface=", 15) == 0) {
315 os_free(config->ctrl_interface);
316 config->ctrl_interface = os_strdup(pos + 15);
317 wpa_printf(MSG_DEBUG, "ctrl_interface='%s'",
318 config->ctrl_interface);
319 } else if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0) {
320 os_free(config->ctrl_interface_group);
321 config->ctrl_interface_group = os_strdup(pos + 21);
322 wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' "
324 config->ctrl_interface_group);
325 #endif /* CONFIG_CTRL_IFACE */
326 } else if (os_strncmp(pos, "eapol_version=", 14) == 0) {
327 config->eapol_version = atoi(pos + 14);
328 if (config->eapol_version < 1 ||
329 config->eapol_version > 2) {
330 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL "
331 "version (%d): '%s'.",
332 line, config->eapol_version, pos);
336 wpa_printf(MSG_DEBUG, "eapol_version=%d",
337 config->eapol_version);
338 } else if (os_strncmp(pos, "ap_scan=", 8) == 0) {
339 config->ap_scan = atoi(pos + 8);
340 wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
341 } else if (os_strncmp(pos, "fast_reauth=", 12) == 0) {
342 config->fast_reauth = atoi(pos + 12);
343 wpa_printf(MSG_DEBUG, "fast_reauth=%d",
344 config->fast_reauth);
345 } else if (os_strncmp(pos, "opensc_engine_path=", 19) == 0) {
346 os_free(config->opensc_engine_path);
347 config->opensc_engine_path = os_strdup(pos + 19);
348 wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
349 config->opensc_engine_path);
350 } else if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0) {
351 os_free(config->pkcs11_engine_path);
352 config->pkcs11_engine_path = os_strdup(pos + 19);
353 wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
354 config->pkcs11_engine_path);
355 } else if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0) {
356 os_free(config->pkcs11_module_path);
357 config->pkcs11_module_path = os_strdup(pos + 19);
358 wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
359 config->pkcs11_module_path);
360 } else if (os_strncmp(pos, "driver_param=", 13) == 0) {
361 os_free(config->driver_param);
362 config->driver_param = os_strdup(pos + 13);
363 wpa_printf(MSG_DEBUG, "driver_param='%s'",
364 config->driver_param);
365 } else if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27)
367 config->dot11RSNAConfigPMKLifetime = atoi(pos + 27);
368 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
369 config->dot11RSNAConfigPMKLifetime);
370 } else if (os_strncmp(pos,
371 "dot11RSNAConfigPMKReauthThreshold=", 34)
373 config->dot11RSNAConfigPMKReauthThreshold =
375 wpa_printf(MSG_DEBUG,
376 "dot11RSNAConfigPMKReauthThreshold=%d",
377 config->dot11RSNAConfigPMKReauthThreshold);
378 } else if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) ==
380 config->dot11RSNAConfigSATimeout = atoi(pos + 25);
381 wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
382 config->dot11RSNAConfigSATimeout);
383 } else if (os_strncmp(pos, "update_config=", 14) == 0) {
384 config->update_config = atoi(pos + 14);
385 wpa_printf(MSG_DEBUG, "update_config=%d",
386 config->update_config);
387 } else if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0) {
390 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
391 ret = eap_peer_method_load(so);
393 wpa_printf(MSG_DEBUG, "This EAP type was "
394 "already loaded - not reloading.");
396 wpa_printf(MSG_ERROR, "Line %d: Failed to "
397 "load dynamic EAP method '%s'.",
402 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
403 "line '%s'.", line, pos);
412 wpa_config_debug_dump_networks(config);
414 #ifndef WPA_IGNORE_CONFIG_ERRORS
416 wpa_config_free(config);
425 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
427 char *value = wpa_config_get(ssid, field);
430 fprintf(f, "\t%s=%s\n", field, value);
435 static void write_int(FILE *f, const char *field, int value, int def)
439 fprintf(f, "\t%s=%d\n", field, value);
443 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
445 char *value = wpa_config_get(ssid, "bssid");
448 fprintf(f, "\tbssid=%s\n", value);
453 static void write_psk(FILE *f, struct wpa_ssid *ssid)
455 char *value = wpa_config_get(ssid, "psk");
458 fprintf(f, "\tpsk=%s\n", value);
463 static void write_proto(FILE *f, struct wpa_ssid *ssid)
467 if (ssid->proto == DEFAULT_PROTO)
470 value = wpa_config_get(ssid, "proto");
474 fprintf(f, "\tproto=%s\n", value);
479 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
483 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
486 value = wpa_config_get(ssid, "key_mgmt");
490 fprintf(f, "\tkey_mgmt=%s\n", value);
495 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
499 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
502 value = wpa_config_get(ssid, "pairwise");
506 fprintf(f, "\tpairwise=%s\n", value);
511 static void write_group(FILE *f, struct wpa_ssid *ssid)
515 if (ssid->group_cipher == DEFAULT_GROUP)
518 value = wpa_config_get(ssid, "group");
522 fprintf(f, "\tgroup=%s\n", value);
527 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
531 if (ssid->auth_alg == 0)
534 value = wpa_config_get(ssid, "auth_alg");
538 fprintf(f, "\tauth_alg=%s\n", value);
543 #ifdef IEEE8021X_EAPOL
544 static void write_eap(FILE *f, struct wpa_ssid *ssid)
548 value = wpa_config_get(ssid, "eap");
553 fprintf(f, "\teap=%s\n", value);
556 #endif /* IEEE8021X_EAPOL */
559 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
561 char field[20], *value;
563 os_snprintf(field, sizeof(field), "wep_key%d", idx);
564 value = wpa_config_get(ssid, field);
566 fprintf(f, "\t%s=%s\n", field, value);
572 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
576 #define STR(t) write_str(f, #t, ssid)
577 #define INT(t) write_int(f, #t, ssid->t, 0)
578 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
582 write_bssid(f, ssid);
584 write_proto(f, ssid);
585 write_key_mgmt(f, ssid);
586 write_pairwise(f, ssid);
587 write_group(f, ssid);
588 write_auth_alg(f, ssid);
589 #ifdef IEEE8021X_EAPOL
592 STR(anonymous_identity);
600 STR(private_key_passwd);
603 STR(altsubject_match);
608 STR(private_key2_passwd);
611 STR(altsubject_match2);
619 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
620 #endif /* IEEE8021X_EAPOL */
621 for (i = 0; i < 4; i++)
622 write_wep_key(f, i, ssid);
625 #ifdef IEEE8021X_EAPOL
626 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
628 INT_DEF(fragment_size, DEFAULT_FRAGMENT_SIZE);
629 #endif /* IEEE8021X_EAPOL */
631 INT(proactive_key_caching);
634 #ifdef CONFIG_IEEE80211W
636 #endif /* CONFIG_IEEE80211W */
645 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
647 unsigned char *encoded;
649 encoded = base64_encode(blob->data, blob->len, NULL);
653 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
659 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
661 #ifdef CONFIG_CTRL_IFACE
662 if (config->ctrl_interface)
663 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
664 if (config->ctrl_interface_group)
665 fprintf(f, "ctrl_interface_group=%s\n",
666 config->ctrl_interface_group);
667 #endif /* CONFIG_CTRL_IFACE */
668 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
669 fprintf(f, "eapol_version=%d\n", config->eapol_version);
670 if (config->ap_scan != DEFAULT_AP_SCAN)
671 fprintf(f, "ap_scan=%d\n", config->ap_scan);
672 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
673 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
674 if (config->opensc_engine_path)
675 fprintf(f, "opensc_engine_path=%s\n",
676 config->opensc_engine_path);
677 if (config->pkcs11_engine_path)
678 fprintf(f, "pkcs11_engine_path=%s\n",
679 config->pkcs11_engine_path);
680 if (config->pkcs11_module_path)
681 fprintf(f, "pkcs11_module_path=%s\n",
682 config->pkcs11_module_path);
683 if (config->driver_param)
684 fprintf(f, "driver_param=%s\n", config->driver_param);
685 if (config->dot11RSNAConfigPMKLifetime)
686 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
687 config->dot11RSNAConfigPMKLifetime);
688 if (config->dot11RSNAConfigPMKReauthThreshold)
689 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
690 config->dot11RSNAConfigPMKReauthThreshold);
691 if (config->dot11RSNAConfigSATimeout)
692 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
693 config->dot11RSNAConfigSATimeout);
694 if (config->update_config)
695 fprintf(f, "update_config=%d\n", config->update_config);
699 int wpa_config_write(const char *name, struct wpa_config *config)
702 struct wpa_ssid *ssid;
703 struct wpa_config_blob *blob;
706 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
708 f = fopen(name, "w");
710 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
714 wpa_config_write_global(f, config);
716 for (ssid = config->ssid; ssid; ssid = ssid->next) {
717 fprintf(f, "\nnetwork={\n");
718 wpa_config_write_network(f, ssid);
722 for (blob = config->blobs; blob; blob = blob->next) {
723 ret = wpa_config_write_blob(f, blob);
730 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
731 name, ret ? "un" : "");