2 * WPA Supplicant / Configuration backend: text file
3 * Copyright (c) 2003-2006, Jouni Malinen <jkmaline@cc.hut.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.
25 #include "wpa_supplicant.h"
30 static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line)
32 char *pos, *end, *sstart;
34 while (fgets(s, size, stream)) {
39 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
41 if (*pos == '#' || *pos == '\n' || *pos == '\0' ||
45 /* Remove # comments unless they are within a double quoted
46 * string. Remove trailing white space. */
47 sstart = strchr(pos, '"');
49 sstart = strrchr(sstart + 1, '"');
52 end = strchr(sstart, '#');
56 end = pos + strlen(pos) - 1;
58 (*end == '\n' || *end == ' ' || *end == '\t' ||
72 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
74 struct wpa_ssid *ssid;
75 int errors = 0, end = 0;
76 char buf[256], *pos, *pos2;
78 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
80 ssid = (struct wpa_ssid *) malloc(sizeof(*ssid));
83 memset(ssid, 0, sizeof(*ssid));
86 wpa_config_set_network_defaults(ssid);
88 while ((pos = wpa_config_get_line(buf, sizeof(buf), f, line))) {
89 if (strcmp(pos, "}") == 0) {
94 pos2 = strchr(pos, '=');
96 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
104 if (strchr(pos2 + 1, '"') == NULL) {
105 wpa_printf(MSG_ERROR, "Line %d: invalid "
106 "quotation '%s'.", *line, pos2);
112 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
117 wpa_printf(MSG_ERROR, "Line %d: network block was not "
118 "terminated properly.", *line);
122 if (ssid->passphrase) {
124 wpa_printf(MSG_ERROR, "Line %d: both PSK and "
125 "passphrase configured.", *line);
128 wpa_config_update_psk(ssid);
131 if ((ssid->key_mgmt & WPA_KEY_MGMT_PSK) && !ssid->psk_set) {
132 wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
133 "management, but no PSK configured.", *line);
137 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
138 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP)) {
139 /* Group cipher cannot be stronger than the pairwise cipher. */
140 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
141 " list since it was not allowed for pairwise "
143 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
147 wpa_config_free_ssid(ssid);
155 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
158 struct wpa_config_blob *blob;
160 unsigned char *encoded = NULL, *nencoded;
162 size_t encoded_len = 0, len;
164 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
167 while ((pos = wpa_config_get_line(buf, sizeof(buf), f, line))) {
168 if (strcmp(pos, "}") == 0) {
174 nencoded = realloc(encoded, encoded_len + len);
175 if (nencoded == NULL) {
176 wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
182 memcpy(encoded + encoded_len, pos, len);
187 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
193 blob = malloc(sizeof(*blob));
198 memset(blob, 0, sizeof(*blob));
199 blob->name = strdup(name);
200 blob->data = base64_decode(encoded, encoded_len, &blob->len);
203 if (blob->name == NULL || blob->data == NULL) {
204 wpa_config_free_blob(blob);
212 struct wpa_config * wpa_config_read(const char *name)
216 int errors = 0, line = 0;
217 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
218 struct wpa_config *config;
221 config = wpa_config_alloc_empty(NULL, NULL);
224 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
225 f = fopen(name, "r");
231 while ((pos = wpa_config_get_line(buf, sizeof(buf), f, &line))) {
232 if (strcmp(pos, "network={") == 0) {
233 ssid = wpa_config_read_network(f, &line, id++);
235 wpa_printf(MSG_ERROR, "Line %d: failed to "
236 "parse network block.", line);
246 if (wpa_config_add_prio_network(config, ssid)) {
247 wpa_printf(MSG_ERROR, "Line %d: failed to add "
248 "network block to priority list.",
253 } else if (strncmp(pos, "blob-base64-", 12) == 0) {
254 char *name = pos + 12, *name_end;
255 struct wpa_config_blob *blob;
257 name_end = strchr(name, '=');
258 if (name_end == NULL) {
259 wpa_printf(MSG_ERROR, "Line %d: no blob name "
266 blob = wpa_config_read_blob(f, &line, name);
268 wpa_printf(MSG_ERROR, "Line %d: failed to read"
269 " blob %s", line, name);
273 wpa_config_set_blob(config, blob);
274 #ifdef CONFIG_CTRL_IFACE
275 } else if (strncmp(pos, "ctrl_interface=", 15) == 0) {
276 free(config->ctrl_interface);
277 config->ctrl_interface = strdup(pos + 15);
278 wpa_printf(MSG_DEBUG, "ctrl_interface='%s'",
279 config->ctrl_interface);
280 #ifndef CONFIG_CTRL_IFACE_UDP
281 } else if (strncmp(pos, "ctrl_interface_group=", 21) == 0) {
284 const char *group = pos + 21;
286 grp = getgrnam(group);
288 config->ctrl_interface_gid = grp->gr_gid;
289 config->ctrl_interface_gid_set = 1;
290 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
291 " (from group name '%s')",
292 (int) config->ctrl_interface_gid,
297 /* Group name not found - try to parse this as gid */
298 config->ctrl_interface_gid = strtol(group, &endp, 10);
299 if (*group == '\0' || *endp != '\0') {
300 wpa_printf(MSG_DEBUG, "Line %d: Invalid group "
301 "'%s'", line, group);
305 config->ctrl_interface_gid_set = 1;
306 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
307 (int) config->ctrl_interface_gid);
308 #endif /* CONFIG_CTRL_IFACE_UDP */
309 #endif /* CONFIG_CTRL_IFACE */
310 } else if (strncmp(pos, "eapol_version=", 14) == 0) {
311 config->eapol_version = atoi(pos + 14);
312 if (config->eapol_version < 1 ||
313 config->eapol_version > 2) {
314 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL "
315 "version (%d): '%s'.",
316 line, config->eapol_version, pos);
320 wpa_printf(MSG_DEBUG, "eapol_version=%d",
321 config->eapol_version);
322 } else if (strncmp(pos, "ap_scan=", 8) == 0) {
323 config->ap_scan = atoi(pos + 8);
324 wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
325 } else if (strncmp(pos, "fast_reauth=", 12) == 0) {
326 config->fast_reauth = atoi(pos + 12);
327 wpa_printf(MSG_DEBUG, "fast_reauth=%d",
328 config->fast_reauth);
329 } else if (strncmp(pos, "opensc_engine_path=", 19) == 0) {
330 free(config->opensc_engine_path);
331 config->opensc_engine_path = strdup(pos + 19);
332 wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
333 config->opensc_engine_path);
334 } else if (strncmp(pos, "pkcs11_engine_path=", 19) == 0) {
335 free(config->pkcs11_engine_path);
336 config->pkcs11_engine_path = strdup(pos + 19);
337 wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
338 config->pkcs11_engine_path);
339 } else if (strncmp(pos, "pkcs11_module_path=", 19) == 0) {
340 free(config->pkcs11_module_path);
341 config->pkcs11_module_path = strdup(pos + 19);
342 wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
343 config->pkcs11_module_path);
344 } else if (strncmp(pos, "driver_param=", 13) == 0) {
345 free(config->driver_param);
346 config->driver_param = strdup(pos + 13);
347 wpa_printf(MSG_DEBUG, "driver_param='%s'",
348 config->driver_param);
349 } else if (strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) ==
351 config->dot11RSNAConfigPMKLifetime = atoi(pos + 27);
352 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
353 config->dot11RSNAConfigPMKLifetime);
354 } else if (strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=",
357 config->dot11RSNAConfigPMKReauthThreshold =
359 wpa_printf(MSG_DEBUG,
360 "dot11RSNAConfigPMKReauthThreshold=%d",
361 config->dot11RSNAConfigPMKReauthThreshold);
362 } else if (strncmp(pos, "dot11RSNAConfigSATimeout=", 25) ==
364 config->dot11RSNAConfigSATimeout = atoi(pos + 25);
365 wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
366 config->dot11RSNAConfigSATimeout);
367 } else if (strncmp(pos, "update_config=", 14) == 0) {
368 config->update_config = atoi(pos + 14);
369 wpa_printf(MSG_DEBUG, "update_config=%d",
370 config->update_config);
372 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
373 "line '%s'.", line, pos);
382 for (prio = 0; prio < config->num_prio; prio++) {
383 ssid = config->pssid[prio];
384 wpa_printf(MSG_DEBUG, "Priority group %d",
387 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
389 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
394 wpa_config_free(config);
403 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
405 char *value = wpa_config_get(ssid, field);
408 fprintf(f, "\t%s=%s\n", field, value);
413 static void write_int(FILE *f, const char *field, int value, int def)
417 fprintf(f, "\t%s=%d\n", field, value);
421 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
423 char *value = wpa_config_get(ssid, "bssid");
426 fprintf(f, "\tbssid=%s\n", value);
431 static void write_psk(FILE *f, struct wpa_ssid *ssid)
433 char *value = wpa_config_get(ssid, "psk");
436 fprintf(f, "\tpsk=%s\n", value);
441 static void write_proto(FILE *f, struct wpa_ssid *ssid)
445 if (ssid->proto == DEFAULT_PROTO)
448 value = wpa_config_get(ssid, "proto");
452 fprintf(f, "\tproto=%s\n", value);
457 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
461 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
464 value = wpa_config_get(ssid, "key_mgmt");
468 fprintf(f, "\tkey_mgmt=%s\n", value);
473 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
477 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
480 value = wpa_config_get(ssid, "pairwise");
484 fprintf(f, "\tpairwise=%s\n", value);
489 static void write_group(FILE *f, struct wpa_ssid *ssid)
493 if (ssid->group_cipher == DEFAULT_GROUP)
496 value = wpa_config_get(ssid, "group");
500 fprintf(f, "\tgroup=%s\n", value);
505 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
509 if (ssid->auth_alg == 0)
512 value = wpa_config_get(ssid, "auth_alg");
516 fprintf(f, "\tauth_alg=%s\n", value);
521 static void write_eap(FILE *f, struct wpa_ssid *ssid)
525 value = wpa_config_get(ssid, "eap");
530 fprintf(f, "\teap=%s\n", value);
535 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
537 char field[20], *value;
539 snprintf(field, sizeof(field), "wep_key%d", idx);
540 value = wpa_config_get(ssid, field);
542 fprintf(f, "\t%s=%s\n", field, value);
548 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
552 #define STR(t) write_str(f, #t, ssid)
553 #define INT(t) write_int(f, #t, ssid->t, 0)
554 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
558 write_bssid(f, ssid);
560 write_proto(f, ssid);
561 write_key_mgmt(f, ssid);
562 write_pairwise(f, ssid);
563 write_group(f, ssid);
564 write_auth_alg(f, ssid);
567 STR(anonymous_identity);
574 STR(private_key_passwd);
577 STR(altsubject_match);
581 STR(private_key2_passwd);
584 STR(altsubject_match2);
592 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
593 for (i = 0; i < 4; i++)
594 write_wep_key(f, i, ssid);
597 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
600 INT(proactive_key_caching);
609 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
611 unsigned char *encoded;
613 encoded = base64_encode(blob->data, blob->len, NULL);
617 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
623 int wpa_config_write(const char *name, struct wpa_config *config)
626 struct wpa_ssid *ssid;
627 struct wpa_config_blob *blob;
630 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
633 f = fopen(name, "w");
635 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
639 #ifdef CONFIG_CTRL_IFACE
640 if (config->ctrl_interface)
641 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
642 #ifndef CONFIG_CTRL_IFACE_UDP
643 if (config->ctrl_interface_gid_set) {
644 fprintf(f, "ctrl_interface_group=%d\n",
645 (int) config->ctrl_interface_gid);
647 #endif /* CONFIG_CTRL_IFACE_UDP */
648 #endif /* CONFIG_CTRL_IFACE */
649 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
650 fprintf(f, "eapol_version=%d\n", config->eapol_version);
651 if (config->ap_scan != DEFAULT_AP_SCAN)
652 fprintf(f, "ap_scan=%d\n", config->ap_scan);
653 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
654 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
655 if (config->opensc_engine_path)
656 fprintf(f, "opensc_engine_path=%s\n",
657 config->opensc_engine_path);
658 if (config->pkcs11_engine_path)
659 fprintf(f, "pkcs11_engine_path=%s\n",
660 config->pkcs11_engine_path);
661 if (config->pkcs11_module_path)
662 fprintf(f, "pkcs11_module_path=%s\n",
663 config->pkcs11_module_path);
664 if (config->driver_param)
665 fprintf(f, "driver_param=%s\n", config->driver_param);
666 if (config->dot11RSNAConfigPMKLifetime)
667 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
668 config->dot11RSNAConfigPMKLifetime);
669 if (config->dot11RSNAConfigPMKReauthThreshold)
670 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
671 config->dot11RSNAConfigPMKReauthThreshold);
672 if (config->dot11RSNAConfigSATimeout)
673 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
674 config->dot11RSNAConfigSATimeout);
675 if (config->update_config)
676 fprintf(f, "update_config=%d\n", config->update_config);
678 for (ssid = config->ssid; ssid; ssid = ssid->next) {
679 fprintf(f, "\nnetwork={\n");
680 wpa_config_write_network(f, ssid);
684 for (blob = config->blobs; blob; blob = blob->next) {
685 ret = wpa_config_write_blob(f, blob);
692 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
693 name, ret ? "un" : "");