blob: e32bf5af04cb02d2989389783dd4b2e942799598 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Configuration backend: text file
Dmitry Shmidt04949592012-07-19 12:16:46 -07003 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
8 * This file implements a configuration backend for text files. All the
9 * configuration information is stored in a text file that uses a format
10 * described in the sample configuration file, wpa_supplicant.conf.
11 */
12
13#include "includes.h"
14
15#include "common.h"
16#include "config.h"
17#include "base64.h"
18#include "uuid.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070019#include "p2p/p2p.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080020#include "eap_peer/eap_methods.h"
21#include "eap_peer/eap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022
23
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070024static int newline_terminated(const char *buf, size_t buflen)
25{
26 size_t len = os_strlen(buf);
27 if (len == 0)
28 return 0;
29 if (len == buflen - 1 && buf[buflen - 1] != '\r' &&
30 buf[len - 1] != '\n')
31 return 0;
32 return 1;
33}
34
35
36static void skip_line_end(FILE *stream)
37{
38 char buf[100];
39 while (fgets(buf, sizeof(buf), stream)) {
40 buf[sizeof(buf) - 1] = '\0';
41 if (newline_terminated(buf, sizeof(buf)))
42 return;
43 }
44}
45
46
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047/**
48 * wpa_config_get_line - Read the next configuration file line
49 * @s: Buffer for the line
50 * @size: The buffer length
51 * @stream: File stream to read from
52 * @line: Pointer to a variable storing the file line number
53 * @_pos: Buffer for the pointer to the beginning of data on the text line or
54 * %NULL if not needed (returned value used instead)
55 * Returns: Pointer to the beginning of data on the text line or %NULL if no
56 * more text lines are available.
57 *
58 * This function reads the next non-empty line from the configuration file and
59 * removes comments. The returned string is guaranteed to be null-terminated.
60 */
61static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
62 char **_pos)
63{
64 char *pos, *end, *sstart;
65
66 while (fgets(s, size, stream)) {
67 (*line)++;
68 s[size - 1] = '\0';
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070069 if (!newline_terminated(s, size)) {
70 /*
71 * The line was truncated - skip rest of it to avoid
72 * confusing error messages.
73 */
74 wpa_printf(MSG_INFO, "Long line in configuration file "
75 "truncated");
76 skip_line_end(stream);
77 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078 pos = s;
79
80 /* Skip white space from the beginning of line. */
81 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
82 pos++;
83
84 /* Skip comment lines and empty lines */
85 if (*pos == '#' || *pos == '\n' || *pos == '\0')
86 continue;
87
88 /*
89 * Remove # comments unless they are within a double quoted
90 * string.
91 */
92 sstart = os_strchr(pos, '"');
93 if (sstart)
94 sstart = os_strrchr(sstart + 1, '"');
95 if (!sstart)
96 sstart = pos;
97 end = os_strchr(sstart, '#');
98 if (end)
99 *end-- = '\0';
100 else
101 end = pos + os_strlen(pos) - 1;
102
103 /* Remove trailing white space. */
104 while (end > pos &&
105 (*end == '\n' || *end == ' ' || *end == '\t' ||
106 *end == '\r'))
107 *end-- = '\0';
108
109 if (*pos == '\0')
110 continue;
111
112 if (_pos)
113 *_pos = pos;
114 return pos;
115 }
116
117 if (_pos)
118 *_pos = NULL;
119 return NULL;
120}
121
122
123static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
124{
125 int errors = 0;
126
127 if (ssid->passphrase) {
128 if (ssid->psk_set) {
129 wpa_printf(MSG_ERROR, "Line %d: both PSK and "
130 "passphrase configured.", line);
131 errors++;
132 }
133 wpa_config_update_psk(ssid);
134 }
135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
137 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
138 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
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 "
142 "cipher", line);
143 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
144 }
145
146 return errors;
147}
148
149
150static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
151{
152 struct wpa_ssid *ssid;
153 int errors = 0, end = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700154 char buf[2000], *pos, *pos2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155
156 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
157 *line);
158 ssid = os_zalloc(sizeof(*ssid));
159 if (ssid == NULL)
160 return NULL;
161 ssid->id = id;
162
163 wpa_config_set_network_defaults(ssid);
164
165 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
166 if (os_strcmp(pos, "}") == 0) {
167 end = 1;
168 break;
169 }
170
171 pos2 = os_strchr(pos, '=');
172 if (pos2 == NULL) {
173 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
174 "'%s'.", *line, pos);
175 errors++;
176 continue;
177 }
178
179 *pos2++ = '\0';
180 if (*pos2 == '"') {
181 if (os_strchr(pos2 + 1, '"') == NULL) {
182 wpa_printf(MSG_ERROR, "Line %d: invalid "
183 "quotation '%s'.", *line, pos2);
184 errors++;
185 continue;
186 }
187 }
188
189 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
190 errors++;
191 }
192
193 if (!end) {
194 wpa_printf(MSG_ERROR, "Line %d: network block was not "
195 "terminated properly.", *line);
196 errors++;
197 }
198
199 errors += wpa_config_validate_network(ssid, *line);
200
201 if (errors) {
202 wpa_config_free_ssid(ssid);
203 ssid = NULL;
204 }
205
206 return ssid;
207}
208
209
Dmitry Shmidt04949592012-07-19 12:16:46 -0700210static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id)
211{
212 struct wpa_cred *cred;
213 int errors = 0, end = 0;
214 char buf[256], *pos, *pos2;
215
216 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line);
217 cred = os_zalloc(sizeof(*cred));
218 if (cred == NULL)
219 return NULL;
220 cred->id = id;
221
222 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
223 if (os_strcmp(pos, "}") == 0) {
224 end = 1;
225 break;
226 }
227
228 pos2 = os_strchr(pos, '=');
229 if (pos2 == NULL) {
230 wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
231 "'%s'.", *line, pos);
232 errors++;
233 continue;
234 }
235
236 *pos2++ = '\0';
237 if (*pos2 == '"') {
238 if (os_strchr(pos2 + 1, '"') == NULL) {
239 wpa_printf(MSG_ERROR, "Line %d: invalid "
240 "quotation '%s'.", *line, pos2);
241 errors++;
242 continue;
243 }
244 }
245
246 if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
247 errors++;
248 }
249
250 if (!end) {
251 wpa_printf(MSG_ERROR, "Line %d: cred block was not "
252 "terminated properly.", *line);
253 errors++;
254 }
255
256 if (errors) {
257 wpa_config_free_cred(cred);
258 cred = NULL;
259 }
260
261 return cred;
262}
263
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265#ifndef CONFIG_NO_CONFIG_BLOBS
266static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
267 const char *name)
268{
269 struct wpa_config_blob *blob;
270 char buf[256], *pos;
271 unsigned char *encoded = NULL, *nencoded;
272 int end = 0;
273 size_t encoded_len = 0, len;
274
275 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
276 *line, name);
277
278 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
279 if (os_strcmp(pos, "}") == 0) {
280 end = 1;
281 break;
282 }
283
284 len = os_strlen(pos);
285 nencoded = os_realloc(encoded, encoded_len + len);
286 if (nencoded == NULL) {
287 wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
288 "blob", *line);
289 os_free(encoded);
290 return NULL;
291 }
292 encoded = nencoded;
293 os_memcpy(encoded + encoded_len, pos, len);
294 encoded_len += len;
295 }
296
297 if (!end) {
298 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
299 "properly", *line);
300 os_free(encoded);
301 return NULL;
302 }
303
304 blob = os_zalloc(sizeof(*blob));
305 if (blob == NULL) {
306 os_free(encoded);
307 return NULL;
308 }
309 blob->name = os_strdup(name);
310 blob->data = base64_decode(encoded, encoded_len, &blob->len);
311 os_free(encoded);
312
313 if (blob->name == NULL || blob->data == NULL) {
314 wpa_config_free_blob(blob);
315 return NULL;
316 }
317
318 return blob;
319}
320
321
322static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
323 int *line, char *bname)
324{
325 char *name_end;
326 struct wpa_config_blob *blob;
327
328 name_end = os_strchr(bname, '=');
329 if (name_end == NULL) {
330 wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
331 *line);
332 return -1;
333 }
334 *name_end = '\0';
335
336 blob = wpa_config_read_blob(f, line, bname);
337 if (blob == NULL) {
338 wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
339 *line, bname);
340 return -1;
341 }
342 wpa_config_set_blob(config, blob);
343 return 0;
344}
345#endif /* CONFIG_NO_CONFIG_BLOBS */
346
347
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700348struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349{
350 FILE *f;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700351 char buf[512], *pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 int errors = 0, line = 0;
353 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700354 struct wpa_cred *cred, *cred_tail = NULL, *cred_head = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 struct wpa_config *config;
356 int id = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357 int cred_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700359 if (name == NULL)
360 return NULL;
361 if (cfgp)
362 config = cfgp;
363 else
364 config = wpa_config_alloc_empty(NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700365 if (config == NULL) {
366 wpa_printf(MSG_ERROR, "Failed to allocate config file "
367 "structure");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700369 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700370 head = config->ssid;
371 cred_head = config->cred;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
374 f = fopen(name, "r");
375 if (f == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700376 wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
377 "error: %s", name, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 os_free(config);
379 return NULL;
380 }
381
382 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
383 if (os_strcmp(pos, "network={") == 0) {
384 ssid = wpa_config_read_network(f, &line, id++);
385 if (ssid == NULL) {
386 wpa_printf(MSG_ERROR, "Line %d: failed to "
387 "parse network block.", line);
388 errors++;
389 continue;
390 }
391 if (head == NULL) {
392 head = tail = ssid;
393 } else {
394 tail->next = ssid;
395 tail = ssid;
396 }
397 if (wpa_config_add_prio_network(config, ssid)) {
398 wpa_printf(MSG_ERROR, "Line %d: failed to add "
399 "network block to priority list.",
400 line);
401 errors++;
402 continue;
403 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700404 } else if (os_strcmp(pos, "cred={") == 0) {
405 cred = wpa_config_read_cred(f, &line, cred_id++);
406 if (cred == NULL) {
407 wpa_printf(MSG_ERROR, "Line %d: failed to "
408 "parse cred block.", line);
409 errors++;
410 continue;
411 }
412 if (cred_head == NULL) {
413 cred_head = cred_tail = cred;
414 } else {
415 cred_tail->next = cred;
416 cred_tail = cred;
417 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418#ifndef CONFIG_NO_CONFIG_BLOBS
419 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
420 if (wpa_config_process_blob(config, f, &line, pos + 12)
421 < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700422 wpa_printf(MSG_ERROR, "Line %d: failed to "
423 "process blob.", line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 errors++;
425 continue;
426 }
427#endif /* CONFIG_NO_CONFIG_BLOBS */
428 } else if (wpa_config_process_global(config, pos, line) < 0) {
429 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
430 "line '%s'.", line, pos);
431 errors++;
432 continue;
433 }
434 }
435
436 fclose(f);
437
Dmitry Shmidtb9551352013-04-16 10:41:54 -0700438 if (head)
439 config->ssid = head;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 wpa_config_debug_dump_networks(config);
Dmitry Shmidtb9551352013-04-16 10:41:54 -0700441 if (cred_head)
442 config->cred = cred_head;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443
444#ifndef WPA_IGNORE_CONFIG_ERRORS
445 if (errors) {
446 wpa_config_free(config);
447 config = NULL;
448 head = NULL;
449 }
450#endif /* WPA_IGNORE_CONFIG_ERRORS */
451
452 return config;
453}
454
455
456#ifndef CONFIG_NO_CONFIG_WRITE
457
458static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
459{
460 char *value = wpa_config_get(ssid, field);
461 if (value == NULL)
462 return;
463 fprintf(f, "\t%s=%s\n", field, value);
464 os_free(value);
465}
466
467
468static void write_int(FILE *f, const char *field, int value, int def)
469{
470 if (value == def)
471 return;
472 fprintf(f, "\t%s=%d\n", field, value);
473}
474
475
476static void write_bssid(FILE *f, struct wpa_ssid *ssid)
477{
478 char *value = wpa_config_get(ssid, "bssid");
479 if (value == NULL)
480 return;
481 fprintf(f, "\tbssid=%s\n", value);
482 os_free(value);
483}
484
485
486static void write_psk(FILE *f, struct wpa_ssid *ssid)
487{
488 char *value = wpa_config_get(ssid, "psk");
489 if (value == NULL)
490 return;
491 fprintf(f, "\tpsk=%s\n", value);
492 os_free(value);
493}
494
495
496static void write_proto(FILE *f, struct wpa_ssid *ssid)
497{
498 char *value;
499
500 if (ssid->proto == DEFAULT_PROTO)
501 return;
502
503 value = wpa_config_get(ssid, "proto");
504 if (value == NULL)
505 return;
506 if (value[0])
507 fprintf(f, "\tproto=%s\n", value);
508 os_free(value);
509}
510
511
512static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
513{
514 char *value;
515
516 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
517 return;
518
519 value = wpa_config_get(ssid, "key_mgmt");
520 if (value == NULL)
521 return;
522 if (value[0])
523 fprintf(f, "\tkey_mgmt=%s\n", value);
524 os_free(value);
525}
526
527
528static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
529{
530 char *value;
531
532 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
533 return;
534
535 value = wpa_config_get(ssid, "pairwise");
536 if (value == NULL)
537 return;
538 if (value[0])
539 fprintf(f, "\tpairwise=%s\n", value);
540 os_free(value);
541}
542
543
544static void write_group(FILE *f, struct wpa_ssid *ssid)
545{
546 char *value;
547
548 if (ssid->group_cipher == DEFAULT_GROUP)
549 return;
550
551 value = wpa_config_get(ssid, "group");
552 if (value == NULL)
553 return;
554 if (value[0])
555 fprintf(f, "\tgroup=%s\n", value);
556 os_free(value);
557}
558
559
560static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
561{
562 char *value;
563
564 if (ssid->auth_alg == 0)
565 return;
566
567 value = wpa_config_get(ssid, "auth_alg");
568 if (value == NULL)
569 return;
570 if (value[0])
571 fprintf(f, "\tauth_alg=%s\n", value);
572 os_free(value);
573}
574
575
576#ifdef IEEE8021X_EAPOL
577static void write_eap(FILE *f, struct wpa_ssid *ssid)
578{
579 char *value;
580
581 value = wpa_config_get(ssid, "eap");
582 if (value == NULL)
583 return;
584
585 if (value[0])
586 fprintf(f, "\teap=%s\n", value);
587 os_free(value);
588}
589#endif /* IEEE8021X_EAPOL */
590
591
592static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
593{
594 char field[20], *value;
595 int res;
596
597 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
598 if (res < 0 || (size_t) res >= sizeof(field))
599 return;
600 value = wpa_config_get(ssid, field);
601 if (value) {
602 fprintf(f, "\t%s=%s\n", field, value);
603 os_free(value);
604 }
605}
606
607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800608#ifdef CONFIG_P2P
609static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
610{
611 char *value = wpa_config_get(ssid, "p2p_client_list");
612 if (value == NULL)
613 return;
614 fprintf(f, "\tp2p_client_list=%s\n", value);
615 os_free(value);
616}
617#endif /* CONFIG_P2P */
618
619
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
621{
622 int i;
623
624#define STR(t) write_str(f, #t, ssid)
625#define INT(t) write_int(f, #t, ssid->t, 0)
626#define INTe(t) write_int(f, #t, ssid->eap.t, 0)
627#define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
628#define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
629
630 STR(ssid);
631 INT(scan_ssid);
632 write_bssid(f, ssid);
633 write_psk(f, ssid);
634 write_proto(f, ssid);
635 write_key_mgmt(f, ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700636 INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 write_pairwise(f, ssid);
638 write_group(f, ssid);
639 write_auth_alg(f, ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700640 STR(bgscan);
641 STR(autoscan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642#ifdef IEEE8021X_EAPOL
643 write_eap(f, ssid);
644 STR(identity);
645 STR(anonymous_identity);
646 STR(password);
647 STR(ca_cert);
648 STR(ca_path);
649 STR(client_cert);
650 STR(private_key);
651 STR(private_key_passwd);
652 STR(dh_file);
653 STR(subject_match);
654 STR(altsubject_match);
655 STR(ca_cert2);
656 STR(ca_path2);
657 STR(client_cert2);
658 STR(private_key2);
659 STR(private_key2_passwd);
660 STR(dh_file2);
661 STR(subject_match2);
662 STR(altsubject_match2);
663 STR(phase1);
664 STR(phase2);
665 STR(pcsc);
666 STR(pin);
667 STR(engine_id);
668 STR(key_id);
669 STR(cert_id);
670 STR(ca_cert_id);
671 STR(key2_id);
672 STR(pin2);
673 STR(engine2_id);
674 STR(cert2_id);
675 STR(ca_cert2_id);
676 INTe(engine);
677 INTe(engine2);
678 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
679#endif /* IEEE8021X_EAPOL */
680 for (i = 0; i < 4; i++)
681 write_wep_key(f, i, ssid);
682 INT(wep_tx_keyidx);
683 INT(priority);
684#ifdef IEEE8021X_EAPOL
685 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
686 STR(pac_file);
687 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
688#endif /* IEEE8021X_EAPOL */
689 INT(mode);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800690 INT(frequency);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800691 write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700692 INT(disabled);
693 INT(peerkey);
694#ifdef CONFIG_IEEE80211W
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800695 write_int(f, "ieee80211w", ssid->ieee80211w,
696 MGMT_FRAME_PROTECTION_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697#endif /* CONFIG_IEEE80211W */
698 STR(id_str);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800699#ifdef CONFIG_P2P
700 write_p2p_client_list(f, ssid);
701#endif /* CONFIG_P2P */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800702 INT(dtim_period);
703 INT(beacon_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704
705#undef STR
706#undef INT
707#undef INT_DEF
708}
709
710
Dmitry Shmidt04949592012-07-19 12:16:46 -0700711static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
712{
713 if (cred->priority)
714 fprintf(f, "\tpriority=%d\n", cred->priority);
715 if (cred->pcsc)
716 fprintf(f, "\tpcsc=%d\n", cred->pcsc);
717 if (cred->realm)
718 fprintf(f, "\trealm=\"%s\"\n", cred->realm);
719 if (cred->username)
720 fprintf(f, "\tusername=\"%s\"\n", cred->username);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800721 if (cred->password && cred->ext_password)
722 fprintf(f, "\tpassword=ext:%s\n", cred->password);
723 else if (cred->password)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700724 fprintf(f, "\tpassword=\"%s\"\n", cred->password);
725 if (cred->ca_cert)
726 fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800727 if (cred->client_cert)
728 fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
729 if (cred->private_key)
730 fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
731 if (cred->private_key_passwd)
732 fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
733 cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700734 if (cred->imsi)
735 fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
736 if (cred->milenage)
737 fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
738 if (cred->domain)
739 fprintf(f, "\tdomain=\"%s\"\n", cred->domain);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800740 if (cred->roaming_consortium_len) {
741 size_t i;
742 fprintf(f, "\troaming_consortium=");
743 for (i = 0; i < cred->roaming_consortium_len; i++)
744 fprintf(f, "%02x", cred->roaming_consortium[i]);
745 fprintf(f, "\n");
746 }
747 if (cred->eap_method) {
748 const char *name;
749 name = eap_get_name(cred->eap_method[0].vendor,
750 cred->eap_method[0].method);
751 fprintf(f, "\teap=%s\n", name);
752 }
753 if (cred->phase1)
754 fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
755 if (cred->phase2)
756 fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
757 if (cred->excluded_ssid) {
758 size_t i, j;
759 for (i = 0; i < cred->num_excluded_ssid; i++) {
760 struct excluded_ssid *e = &cred->excluded_ssid[i];
761 fprintf(f, "\texcluded_ssid=");
762 for (j = 0; j < e->ssid_len; j++)
763 fprintf(f, "%02x", e->ssid[j]);
764 fprintf(f, "\n");
765 }
766 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700767}
768
769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770#ifndef CONFIG_NO_CONFIG_BLOBS
771static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
772{
773 unsigned char *encoded;
774
775 encoded = base64_encode(blob->data, blob->len, NULL);
776 if (encoded == NULL)
777 return -1;
778
779 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
780 os_free(encoded);
781 return 0;
782}
783#endif /* CONFIG_NO_CONFIG_BLOBS */
784
785
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786static void write_global_bin(FILE *f, const char *field,
787 const struct wpabuf *val)
788{
789 size_t i;
790 const u8 *pos;
791
792 if (val == NULL)
793 return;
794
795 fprintf(f, "%s=", field);
796 pos = wpabuf_head(val);
797 for (i = 0; i < wpabuf_len(val); i++)
798 fprintf(f, "%02X", *pos++);
799 fprintf(f, "\n");
800}
801
802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803static void wpa_config_write_global(FILE *f, struct wpa_config *config)
804{
805#ifdef CONFIG_CTRL_IFACE
806 if (config->ctrl_interface)
807 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
808 if (config->ctrl_interface_group)
809 fprintf(f, "ctrl_interface_group=%s\n",
810 config->ctrl_interface_group);
811#endif /* CONFIG_CTRL_IFACE */
812 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
813 fprintf(f, "eapol_version=%d\n", config->eapol_version);
814 if (config->ap_scan != DEFAULT_AP_SCAN)
815 fprintf(f, "ap_scan=%d\n", config->ap_scan);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700816 if (config->disable_scan_offload)
817 fprintf(f, "disable_scan_offload=%d\n",
818 config->disable_scan_offload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
820 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
821 if (config->opensc_engine_path)
822 fprintf(f, "opensc_engine_path=%s\n",
823 config->opensc_engine_path);
824 if (config->pkcs11_engine_path)
825 fprintf(f, "pkcs11_engine_path=%s\n",
826 config->pkcs11_engine_path);
827 if (config->pkcs11_module_path)
828 fprintf(f, "pkcs11_module_path=%s\n",
829 config->pkcs11_module_path);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700830 if (config->pcsc_reader)
831 fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
832 if (config->pcsc_pin)
833 fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 if (config->driver_param)
835 fprintf(f, "driver_param=%s\n", config->driver_param);
836 if (config->dot11RSNAConfigPMKLifetime)
837 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
838 config->dot11RSNAConfigPMKLifetime);
839 if (config->dot11RSNAConfigPMKReauthThreshold)
840 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
841 config->dot11RSNAConfigPMKReauthThreshold);
842 if (config->dot11RSNAConfigSATimeout)
843 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
844 config->dot11RSNAConfigSATimeout);
845 if (config->update_config)
846 fprintf(f, "update_config=%d\n", config->update_config);
847#ifdef CONFIG_WPS
848 if (!is_nil_uuid(config->uuid)) {
849 char buf[40];
850 uuid_bin2str(config->uuid, buf, sizeof(buf));
851 fprintf(f, "uuid=%s\n", buf);
852 }
853 if (config->device_name)
854 fprintf(f, "device_name=%s\n", config->device_name);
855 if (config->manufacturer)
856 fprintf(f, "manufacturer=%s\n", config->manufacturer);
857 if (config->model_name)
858 fprintf(f, "model_name=%s\n", config->model_name);
859 if (config->model_number)
860 fprintf(f, "model_number=%s\n", config->model_number);
861 if (config->serial_number)
862 fprintf(f, "serial_number=%s\n", config->serial_number);
863 {
864 char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
865 buf = wps_dev_type_bin2str(config->device_type,
866 _buf, sizeof(_buf));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867 if (os_strcmp(buf, "0-00000000-0") != 0)
868 fprintf(f, "device_type=%s\n", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 }
870 if (WPA_GET_BE32(config->os_version))
871 fprintf(f, "os_version=%08x\n",
872 WPA_GET_BE32(config->os_version));
873 if (config->config_methods)
874 fprintf(f, "config_methods=%s\n", config->config_methods);
875 if (config->wps_cred_processing)
876 fprintf(f, "wps_cred_processing=%d\n",
877 config->wps_cred_processing);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700878 if (config->wps_vendor_ext_m1) {
879 int i, len = wpabuf_len(config->wps_vendor_ext_m1);
880 const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
881 if (len > 0) {
882 fprintf(f, "wps_vendor_ext_m1=");
883 for (i = 0; i < len; i++)
884 fprintf(f, "%02x", *p++);
885 fprintf(f, "\n");
886 }
887 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888#endif /* CONFIG_WPS */
889#ifdef CONFIG_P2P
890 if (config->p2p_listen_reg_class)
891 fprintf(f, "p2p_listen_reg_class=%u\n",
892 config->p2p_listen_reg_class);
893 if (config->p2p_listen_channel)
894 fprintf(f, "p2p_listen_channel=%u\n",
895 config->p2p_listen_channel);
896 if (config->p2p_oper_reg_class)
897 fprintf(f, "p2p_oper_reg_class=%u\n",
898 config->p2p_oper_reg_class);
899 if (config->p2p_oper_channel)
900 fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
901 if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
902 fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
903 if (config->p2p_ssid_postfix)
904 fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
905 if (config->persistent_reconnect)
906 fprintf(f, "persistent_reconnect=%u\n",
907 config->persistent_reconnect);
908 if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
909 fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
910 if (config->p2p_group_idle)
911 fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700912 if (config->p2p_pref_chan) {
913 unsigned int i;
914 fprintf(f, "p2p_pref_chan=");
915 for (i = 0; i < config->num_p2p_pref_chan; i++) {
916 fprintf(f, "%s%u:%u", i > 0 ? "," : "",
917 config->p2p_pref_chan[i].op_class,
918 config->p2p_pref_chan[i].chan);
919 }
920 fprintf(f, "\n");
921 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800922 if (config->p2p_go_ht40)
923 fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40);
924 if (config->p2p_disabled)
925 fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled);
926 if (config->p2p_no_group_iface)
927 fprintf(f, "p2p_no_group_iface=%u\n",
928 config->p2p_no_group_iface);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700929 if (config->p2p_ignore_shared_freq)
930 fprintf(f, "p2p_ignore_shared_freq=%u\n",
931 config->p2p_ignore_shared_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932#endif /* CONFIG_P2P */
933 if (config->country[0] && config->country[1]) {
934 fprintf(f, "country=%c%c\n",
935 config->country[0], config->country[1]);
936 }
937 if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
938 fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
939 if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
940 fprintf(f, "bss_expiration_age=%u\n",
941 config->bss_expiration_age);
942 if (config->bss_expiration_scan_count !=
943 DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
944 fprintf(f, "bss_expiration_scan_count=%u\n",
945 config->bss_expiration_scan_count);
946 if (config->filter_ssids)
947 fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
948 if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
949 fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
950 if (config->disassoc_low_ack)
951 fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700952#ifdef CONFIG_HS20
953 if (config->hs20)
954 fprintf(f, "hs20=1\n");
955#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800956#ifdef CONFIG_INTERWORKING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800957 if (config->interworking)
958 fprintf(f, "interworking=%u\n", config->interworking);
959 if (!is_zero_ether_addr(config->hessid))
960 fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
961 if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
962 fprintf(f, "access_network_type=%d\n",
963 config->access_network_type);
964#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700965 if (config->pbc_in_m1)
966 fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800967 if (config->wps_nfc_pw_from_config) {
968 if (config->wps_nfc_dev_pw_id)
969 fprintf(f, "wps_nfc_dev_pw_id=%d\n",
970 config->wps_nfc_dev_pw_id);
971 write_global_bin(f, "wps_nfc_dh_pubkey",
972 config->wps_nfc_dh_pubkey);
973 write_global_bin(f, "wps_nfc_dh_privkey",
974 config->wps_nfc_dh_privkey);
975 write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
976 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700977
978 if (config->ext_password_backend)
979 fprintf(f, "ext_password_backend=%s\n",
980 config->ext_password_backend);
981 if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
982 fprintf(f, "p2p_go_max_inactivity=%d\n",
983 config->p2p_go_max_inactivity);
984 if (config->auto_interworking)
985 fprintf(f, "auto_interworking=%d\n",
986 config->auto_interworking);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800987 if (config->okc)
988 fprintf(f, "okc=%d\n", config->okc);
989 if (config->pmf)
990 fprintf(f, "pmf=%d\n", config->pmf);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800991 if (config->dtim_period)
992 fprintf(f, "dtim_period=%d\n", config->dtim_period);
993 if (config->beacon_int)
994 fprintf(f, "beacon_int=%d\n", config->beacon_int);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800995
996 if (config->sae_groups) {
997 int i;
998 fprintf(f, "sae_groups=");
999 for (i = 0; config->sae_groups[i] >= 0; i++) {
1000 fprintf(f, "%s%d", i > 0 ? " " : "",
1001 config->sae_groups[i]);
1002 }
1003 fprintf(f, "\n");
1004 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001005
1006 if (config->ap_vendor_elements) {
1007 int i, len = wpabuf_len(config->ap_vendor_elements);
1008 const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
1009 if (len > 0) {
1010 fprintf(f, "ap_vendor_elements=");
1011 for (i = 0; i < len; i++)
1012 fprintf(f, "%02x", *p++);
1013 fprintf(f, "\n");
1014 }
1015 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001016
1017 if (config->ignore_old_scan_res)
1018 fprintf(f, "ignore_old_scan_res=%d\n",
1019 config->ignore_old_scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020}
1021
1022#endif /* CONFIG_NO_CONFIG_WRITE */
1023
1024
1025int wpa_config_write(const char *name, struct wpa_config *config)
1026{
1027#ifndef CONFIG_NO_CONFIG_WRITE
1028 FILE *f;
1029 struct wpa_ssid *ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001030 struct wpa_cred *cred;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031#ifndef CONFIG_NO_CONFIG_BLOBS
1032 struct wpa_config_blob *blob;
1033#endif /* CONFIG_NO_CONFIG_BLOBS */
1034 int ret = 0;
1035
1036 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
1037
1038 f = fopen(name, "w");
1039 if (f == NULL) {
1040 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
1041 return -1;
1042 }
1043
1044 wpa_config_write_global(f, config);
1045
Dmitry Shmidt04949592012-07-19 12:16:46 -07001046 for (cred = config->cred; cred; cred = cred->next) {
1047 fprintf(f, "\ncred={\n");
1048 wpa_config_write_cred(f, cred);
1049 fprintf(f, "}\n");
1050 }
1051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 for (ssid = config->ssid; ssid; ssid = ssid->next) {
1053 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
1054 continue; /* do not save temporary networks */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001055 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
1056 !ssid->passphrase)
1057 continue; /* do not save invalid network */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058 fprintf(f, "\nnetwork={\n");
1059 wpa_config_write_network(f, ssid);
1060 fprintf(f, "}\n");
1061 }
1062
1063#ifndef CONFIG_NO_CONFIG_BLOBS
1064 for (blob = config->blobs; blob; blob = blob->next) {
1065 ret = wpa_config_write_blob(f, blob);
1066 if (ret)
1067 break;
1068 }
1069#endif /* CONFIG_NO_CONFIG_BLOBS */
1070
1071 fclose(f);
1072
1073 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
1074 name, ret ? "un" : "");
1075 return ret;
1076#else /* CONFIG_NO_CONFIG_WRITE */
1077 return -1;
1078#endif /* CONFIG_NO_CONFIG_WRITE */
1079}