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.
23
#include "wpa_supplicant.h"
26
#include "eap_methods.h"
29
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' ||
76
static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
78
struct wpa_ssid *ssid;
79
int errors = 0, end = 0;
80
char buf[256], *pos, *pos2;
82
wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
84
ssid = wpa_zalloc(sizeof(*ssid));
89
wpa_config_set_network_defaults(ssid);
91
while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
92
if (strcmp(pos, "}") == 0) {
97
pos2 = strchr(pos, '=');
99
wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
100
"'%s'.", *line, pos);
107
if (strchr(pos2 + 1, '"') == NULL) {
108
wpa_printf(MSG_ERROR, "Line %d: invalid "
109
"quotation '%s'.", *line, pos2);
115
if (wpa_config_set(ssid, pos, pos2, *line) < 0)
120
wpa_printf(MSG_ERROR, "Line %d: network block was not "
121
"terminated properly.", *line);
125
if (ssid->passphrase) {
127
wpa_printf(MSG_ERROR, "Line %d: both PSK and "
128
"passphrase configured.", *line);
131
wpa_config_update_psk(ssid);
134
if ((ssid->key_mgmt & WPA_KEY_MGMT_PSK) && !ssid->psk_set) {
135
wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
136
"management, but no PSK configured.", *line);
140
if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
141
!(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
142
!(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
143
/* Group cipher cannot be stronger than the pairwise cipher. */
144
wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
145
" list since it was not allowed for pairwise "
147
ssid->group_cipher &= ~WPA_CIPHER_CCMP;
151
wpa_config_free_ssid(ssid);
159
static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
162
struct wpa_config_blob *blob;
164
unsigned char *encoded = NULL, *nencoded;
166
size_t encoded_len = 0, len;
168
wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
171
while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
172
if (strcmp(pos, "}") == 0) {
178
nencoded = realloc(encoded, encoded_len + len);
179
if (nencoded == NULL) {
180
wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
186
memcpy(encoded + encoded_len, pos, len);
191
wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
197
blob = wpa_zalloc(sizeof(*blob));
202
blob->name = strdup(name);
203
blob->data = base64_decode(encoded, encoded_len, &blob->len);
206
if (blob->name == NULL || blob->data == NULL) {
207
wpa_config_free_blob(blob);
215
struct wpa_config * wpa_config_read(const char *name)
219
int errors = 0, line = 0;
220
struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
221
struct wpa_config *config;
224
config = wpa_config_alloc_empty(NULL, NULL);
227
wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
228
f = fopen(name, "r");
234
while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
235
if (strcmp(pos, "network={") == 0) {
236
ssid = wpa_config_read_network(f, &line, id++);
238
wpa_printf(MSG_ERROR, "Line %d: failed to "
239
"parse network block.", line);
249
if (wpa_config_add_prio_network(config, ssid)) {
250
wpa_printf(MSG_ERROR, "Line %d: failed to add "
251
"network block to priority list.",
256
} else if (strncmp(pos, "blob-base64-", 12) == 0) {
257
char *name = pos + 12, *name_end;
258
struct wpa_config_blob *blob;
260
name_end = strchr(name, '=');
261
if (name_end == NULL) {
262
wpa_printf(MSG_ERROR, "Line %d: no blob name "
269
blob = wpa_config_read_blob(f, &line, name);
271
wpa_printf(MSG_ERROR, "Line %d: failed to read"
272
" blob %s", line, name);
276
wpa_config_set_blob(config, blob);
277
#ifdef CONFIG_CTRL_IFACE
278
} else if (strncmp(pos, "ctrl_interface=", 15) == 0) {
279
free(config->ctrl_interface);
280
config->ctrl_interface = strdup(pos + 15);
281
wpa_printf(MSG_DEBUG, "ctrl_interface='%s'",
282
config->ctrl_interface);
283
} else if (strncmp(pos, "ctrl_interface_group=", 21) == 0) {
284
free(config->ctrl_interface_group);
285
config->ctrl_interface_group = strdup(pos + 21);
286
wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' "
288
config->ctrl_interface_group);
289
#endif /* CONFIG_CTRL_IFACE */
290
} else if (strncmp(pos, "eapol_version=", 14) == 0) {
291
config->eapol_version = atoi(pos + 14);
292
if (config->eapol_version < 1 ||
293
config->eapol_version > 2) {
294
wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL "
295
"version (%d): '%s'.",
296
line, config->eapol_version, pos);
300
wpa_printf(MSG_DEBUG, "eapol_version=%d",
301
config->eapol_version);
302
} else if (strncmp(pos, "ap_scan=", 8) == 0) {
303
config->ap_scan = atoi(pos + 8);
304
wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
305
} else if (strncmp(pos, "fast_reauth=", 12) == 0) {
306
config->fast_reauth = atoi(pos + 12);
307
wpa_printf(MSG_DEBUG, "fast_reauth=%d",
308
config->fast_reauth);
309
} else if (strncmp(pos, "opensc_engine_path=", 19) == 0) {
310
free(config->opensc_engine_path);
311
config->opensc_engine_path = strdup(pos + 19);
312
wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
313
config->opensc_engine_path);
314
} else if (strncmp(pos, "pkcs11_engine_path=", 19) == 0) {
315
free(config->pkcs11_engine_path);
316
config->pkcs11_engine_path = strdup(pos + 19);
317
wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
318
config->pkcs11_engine_path);
319
} else if (strncmp(pos, "pkcs11_module_path=", 19) == 0) {
320
free(config->pkcs11_module_path);
321
config->pkcs11_module_path = strdup(pos + 19);
322
wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
323
config->pkcs11_module_path);
324
} else if (strncmp(pos, "driver_param=", 13) == 0) {
325
free(config->driver_param);
326
config->driver_param = strdup(pos + 13);
327
wpa_printf(MSG_DEBUG, "driver_param='%s'",
328
config->driver_param);
329
} else if (strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) ==
331
config->dot11RSNAConfigPMKLifetime = atoi(pos + 27);
332
wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
333
config->dot11RSNAConfigPMKLifetime);
334
} else if (strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=",
337
config->dot11RSNAConfigPMKReauthThreshold =
339
wpa_printf(MSG_DEBUG,
340
"dot11RSNAConfigPMKReauthThreshold=%d",
341
config->dot11RSNAConfigPMKReauthThreshold);
342
} else if (strncmp(pos, "dot11RSNAConfigSATimeout=", 25) ==
344
config->dot11RSNAConfigSATimeout = atoi(pos + 25);
345
wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
346
config->dot11RSNAConfigSATimeout);
347
} else if (strncmp(pos, "update_config=", 14) == 0) {
348
config->update_config = atoi(pos + 14);
349
wpa_printf(MSG_DEBUG, "update_config=%d",
350
config->update_config);
351
} else if (strncmp(pos, "load_dynamic_eap=", 17) == 0) {
354
wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
355
ret = eap_peer_method_load(so);
357
wpa_printf(MSG_DEBUG, "This EAP type was "
358
"already loaded - not reloading.");
360
wpa_printf(MSG_ERROR, "Line %d: Failed to "
361
"load dynamic EAP method '%s'.",
366
wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
367
"line '%s'.", line, pos);
376
for (prio = 0; prio < config->num_prio; prio++) {
377
ssid = config->pssid[prio];
378
wpa_printf(MSG_DEBUG, "Priority group %d",
381
wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
383
wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
388
wpa_config_free(config);
397
static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
399
char *value = wpa_config_get(ssid, field);
402
fprintf(f, "\t%s=%s\n", field, value);
407
static void write_int(FILE *f, const char *field, int value, int def)
411
fprintf(f, "\t%s=%d\n", field, value);
415
static void write_bssid(FILE *f, struct wpa_ssid *ssid)
417
char *value = wpa_config_get(ssid, "bssid");
420
fprintf(f, "\tbssid=%s\n", value);
425
static void write_psk(FILE *f, struct wpa_ssid *ssid)
427
char *value = wpa_config_get(ssid, "psk");
430
fprintf(f, "\tpsk=%s\n", value);
435
static void write_proto(FILE *f, struct wpa_ssid *ssid)
439
if (ssid->proto == DEFAULT_PROTO)
442
value = wpa_config_get(ssid, "proto");
446
fprintf(f, "\tproto=%s\n", value);
451
static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
455
if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
458
value = wpa_config_get(ssid, "key_mgmt");
462
fprintf(f, "\tkey_mgmt=%s\n", value);
467
static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
471
if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
474
value = wpa_config_get(ssid, "pairwise");
478
fprintf(f, "\tpairwise=%s\n", value);
483
static void write_group(FILE *f, struct wpa_ssid *ssid)
487
if (ssid->group_cipher == DEFAULT_GROUP)
490
value = wpa_config_get(ssid, "group");
494
fprintf(f, "\tgroup=%s\n", value);
499
static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
503
if (ssid->auth_alg == 0)
506
value = wpa_config_get(ssid, "auth_alg");
510
fprintf(f, "\tauth_alg=%s\n", value);
515
#ifdef IEEE8021X_EAPOL
516
static void write_eap(FILE *f, struct wpa_ssid *ssid)
520
value = wpa_config_get(ssid, "eap");
525
fprintf(f, "\teap=%s\n", value);
528
#endif /* IEEE8021X_EAPOL */
531
static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
533
char field[20], *value;
535
snprintf(field, sizeof(field), "wep_key%d", idx);
536
value = wpa_config_get(ssid, field);
538
fprintf(f, "\t%s=%s\n", field, value);
544
static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
548
#define STR(t) write_str(f, #t, ssid)
549
#define INT(t) write_int(f, #t, ssid->t, 0)
550
#define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
554
write_bssid(f, ssid);
556
write_proto(f, ssid);
557
write_key_mgmt(f, ssid);
558
write_pairwise(f, ssid);
559
write_group(f, ssid);
560
write_auth_alg(f, ssid);
561
#ifdef IEEE8021X_EAPOL
564
STR(anonymous_identity);
571
STR(private_key_passwd);
574
STR(altsubject_match);
578
STR(private_key2_passwd);
581
STR(altsubject_match2);
589
INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
590
#endif /* IEEE8021X_EAPOL */
591
for (i = 0; i < 4; i++)
592
write_wep_key(f, i, ssid);
595
#ifdef IEEE8021X_EAPOL
596
INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
598
INT_DEF(fragment_size, DEFAULT_FRAGMENT_SIZE);
599
#endif /* IEEE8021X_EAPOL */
601
INT(proactive_key_caching);
613
static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
615
unsigned char *encoded;
617
encoded = base64_encode(blob->data, blob->len, NULL);
621
fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
627
int wpa_config_write(const char *name, struct wpa_config *config)
630
struct wpa_ssid *ssid;
631
struct wpa_config_blob *blob;
634
wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
637
f = fopen(name, "w");
639
wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
643
#ifdef CONFIG_CTRL_IFACE
644
if (config->ctrl_interface)
645
fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
646
if (config->ctrl_interface_group)
647
fprintf(f, "ctrl_interface_group=%s\n",
648
config->ctrl_interface_group);
649
#endif /* CONFIG_CTRL_IFACE */
650
if (config->eapol_version != DEFAULT_EAPOL_VERSION)
651
fprintf(f, "eapol_version=%d\n", config->eapol_version);
652
if (config->ap_scan != DEFAULT_AP_SCAN)
653
fprintf(f, "ap_scan=%d\n", config->ap_scan);
654
if (config->fast_reauth != DEFAULT_FAST_REAUTH)
655
fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
656
if (config->opensc_engine_path)
657
fprintf(f, "opensc_engine_path=%s\n",
658
config->opensc_engine_path);
659
if (config->pkcs11_engine_path)
660
fprintf(f, "pkcs11_engine_path=%s\n",
661
config->pkcs11_engine_path);
662
if (config->pkcs11_module_path)
663
fprintf(f, "pkcs11_module_path=%s\n",
664
config->pkcs11_module_path);
665
if (config->driver_param)
666
fprintf(f, "driver_param=%s\n", config->driver_param);
667
if (config->dot11RSNAConfigPMKLifetime)
668
fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
669
config->dot11RSNAConfigPMKLifetime);
670
if (config->dot11RSNAConfigPMKReauthThreshold)
671
fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
672
config->dot11RSNAConfigPMKReauthThreshold);
673
if (config->dot11RSNAConfigSATimeout)
674
fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
675
config->dot11RSNAConfigSATimeout);
676
if (config->update_config)
677
fprintf(f, "update_config=%d\n", config->update_config);
679
for (ssid = config->ssid; ssid; ssid = ssid->next) {
680
fprintf(f, "\nnetwork={\n");
681
wpa_config_write_network(f, ssid);
685
for (blob = config->blobs; blob; blob = blob->next) {
686
ret = wpa_config_write_blob(f, blob);
693
wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
694
name, ret ? "un" : "");