]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/config_file.c
First step in cleaning up global config parser: use parse_data array
[thirdparty/hostap.git] / wpa_supplicant / config_file.c
1 /*
2 * WPA Supplicant / Configuration backend: text file
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
4 *
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.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 *
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.
17 */
18
19 #include "includes.h"
20
21 #include "common.h"
22 #include "config.h"
23 #include "base64.h"
24 #include "uuid.h"
25 #include "eap_peer/eap_methods.h"
26
27
28 /**
29 * wpa_config_get_line - Read the next configuration file line
30 * @s: Buffer for the line
31 * @size: The buffer length
32 * @stream: File stream to read from
33 * @line: Pointer to a variable storing the file line number
34 * @_pos: Buffer for the pointer to the beginning of data on the text line or
35 * %NULL if not needed (returned value used instead)
36 * Returns: Pointer to the beginning of data on the text line or %NULL if no
37 * more text lines are available.
38 *
39 * This function reads the next non-empty line from the configuration file and
40 * removes comments. The returned string is guaranteed to be null-terminated.
41 */
42 static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
43 char **_pos)
44 {
45 char *pos, *end, *sstart;
46
47 while (fgets(s, size, stream)) {
48 (*line)++;
49 s[size - 1] = '\0';
50 pos = s;
51
52 /* Skip white space from the beginning of line. */
53 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
54 pos++;
55
56 /* Skip comment lines and empty lines */
57 if (*pos == '#' || *pos == '\n' || *pos == '\0')
58 continue;
59
60 /*
61 * Remove # comments unless they are within a double quoted
62 * string.
63 */
64 sstart = os_strchr(pos, '"');
65 if (sstart)
66 sstart = os_strrchr(sstart + 1, '"');
67 if (!sstart)
68 sstart = pos;
69 end = os_strchr(sstart, '#');
70 if (end)
71 *end-- = '\0';
72 else
73 end = pos + os_strlen(pos) - 1;
74
75 /* Remove trailing white space. */
76 while (end > pos &&
77 (*end == '\n' || *end == ' ' || *end == '\t' ||
78 *end == '\r'))
79 *end-- = '\0';
80
81 if (*pos == '\0')
82 continue;
83
84 if (_pos)
85 *_pos = pos;
86 return pos;
87 }
88
89 if (_pos)
90 *_pos = NULL;
91 return NULL;
92 }
93
94
95 static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
96 {
97 int errors = 0;
98
99 if (ssid->passphrase) {
100 if (ssid->psk_set) {
101 wpa_printf(MSG_ERROR, "Line %d: both PSK and "
102 "passphrase configured.", line);
103 errors++;
104 }
105 wpa_config_update_psk(ssid);
106 }
107
108 if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
109 WPA_KEY_MGMT_PSK_SHA256)) &&
110 !ssid->psk_set) {
111 wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
112 "management, but no PSK configured.", line);
113 errors++;
114 }
115
116 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
117 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
118 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
119 /* Group cipher cannot be stronger than the pairwise cipher. */
120 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
121 " list since it was not allowed for pairwise "
122 "cipher", line);
123 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
124 }
125
126 return errors;
127 }
128
129
130 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
131 {
132 struct wpa_ssid *ssid;
133 int errors = 0, end = 0;
134 char buf[256], *pos, *pos2;
135
136 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
137 *line);
138 ssid = os_zalloc(sizeof(*ssid));
139 if (ssid == NULL)
140 return NULL;
141 ssid->id = id;
142
143 wpa_config_set_network_defaults(ssid);
144
145 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
146 if (os_strcmp(pos, "}") == 0) {
147 end = 1;
148 break;
149 }
150
151 pos2 = os_strchr(pos, '=');
152 if (pos2 == NULL) {
153 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
154 "'%s'.", *line, pos);
155 errors++;
156 continue;
157 }
158
159 *pos2++ = '\0';
160 if (*pos2 == '"') {
161 if (os_strchr(pos2 + 1, '"') == NULL) {
162 wpa_printf(MSG_ERROR, "Line %d: invalid "
163 "quotation '%s'.", *line, pos2);
164 errors++;
165 continue;
166 }
167 }
168
169 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
170 errors++;
171 }
172
173 if (!end) {
174 wpa_printf(MSG_ERROR, "Line %d: network block was not "
175 "terminated properly.", *line);
176 errors++;
177 }
178
179 errors += wpa_config_validate_network(ssid, *line);
180
181 if (errors) {
182 wpa_config_free_ssid(ssid);
183 ssid = NULL;
184 }
185
186 return ssid;
187 }
188
189
190 #ifndef CONFIG_NO_CONFIG_BLOBS
191 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
192 const char *name)
193 {
194 struct wpa_config_blob *blob;
195 char buf[256], *pos;
196 unsigned char *encoded = NULL, *nencoded;
197 int end = 0;
198 size_t encoded_len = 0, len;
199
200 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
201 *line, name);
202
203 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
204 if (os_strcmp(pos, "}") == 0) {
205 end = 1;
206 break;
207 }
208
209 len = os_strlen(pos);
210 nencoded = os_realloc(encoded, encoded_len + len);
211 if (nencoded == NULL) {
212 wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
213 "blob", *line);
214 os_free(encoded);
215 return NULL;
216 }
217 encoded = nencoded;
218 os_memcpy(encoded + encoded_len, pos, len);
219 encoded_len += len;
220 }
221
222 if (!end) {
223 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
224 "properly", *line);
225 os_free(encoded);
226 return NULL;
227 }
228
229 blob = os_zalloc(sizeof(*blob));
230 if (blob == NULL) {
231 os_free(encoded);
232 return NULL;
233 }
234 blob->name = os_strdup(name);
235 blob->data = base64_decode(encoded, encoded_len, &blob->len);
236 os_free(encoded);
237
238 if (blob->name == NULL || blob->data == NULL) {
239 wpa_config_free_blob(blob);
240 return NULL;
241 }
242
243 return blob;
244 }
245
246
247 static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
248 int *line, char *bname)
249 {
250 char *name_end;
251 struct wpa_config_blob *blob;
252
253 name_end = os_strchr(bname, '=');
254 if (name_end == NULL) {
255 wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
256 *line);
257 return -1;
258 }
259 *name_end = '\0';
260
261 blob = wpa_config_read_blob(f, line, bname);
262 if (blob == NULL) {
263 wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
264 *line, bname);
265 return -1;
266 }
267 wpa_config_set_blob(config, blob);
268 return 0;
269 }
270 #endif /* CONFIG_NO_CONFIG_BLOBS */
271
272
273 struct global_parse_data {
274 char *name;
275 int (*parser)(const struct global_parse_data *data,
276 struct wpa_config *config, int line, const char *value);
277 };
278
279
280 static int wpa_config_process_country(const struct global_parse_data *data,
281 struct wpa_config *config, int line,
282 const char *pos)
283 {
284 if (!pos[0] || !pos[1]) {
285 wpa_printf(MSG_DEBUG, "Invalid country set");
286 return -1;
287 }
288 config->country[0] = pos[0];
289 config->country[1] = pos[1];
290 wpa_printf(MSG_DEBUG, "country='%c%c'",
291 config->country[0], config->country[1]);
292 return 0;
293 }
294
295
296 #ifdef CONFIG_CTRL_IFACE
297 static int wpa_config_process_ctrl_interface(
298 const struct global_parse_data *data, struct wpa_config *config,
299 int line, const char *pos)
300 {
301 os_free(config->ctrl_interface);
302 config->ctrl_interface = os_strdup(pos);
303 wpa_printf(MSG_DEBUG, "ctrl_interface='%s'", config->ctrl_interface);
304 return 0;
305 }
306
307
308 static int wpa_config_process_ctrl_interface_group(
309 const struct global_parse_data *data, struct wpa_config *config,
310 int line, const char *pos)
311 {
312 os_free(config->ctrl_interface_group);
313 config->ctrl_interface_group = os_strdup(pos);
314 wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' (DEPRECATED)",
315 config->ctrl_interface_group);
316 return 0;
317 }
318 #endif /* CONFIG_CTRL_IFACE */
319
320
321 static int wpa_config_process_eapol_version(
322 const struct global_parse_data *data, struct wpa_config *config,
323 int line, const char *pos)
324 {
325 config->eapol_version = atoi(pos);
326 if (config->eapol_version < 1 || config->eapol_version > 2) {
327 wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): "
328 "'%s'.", line, config->eapol_version, pos);
329 return -1;
330 }
331 wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version);
332 return 0;
333 }
334
335
336 static int wpa_config_process_ap_scan(const struct global_parse_data *data,
337 struct wpa_config *config, int line,
338 const char *pos)
339 {
340 config->ap_scan = atoi(pos);
341 wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
342 return 0;
343 }
344
345
346 static int wpa_config_process_fast_reauth(const struct global_parse_data *data,
347 struct wpa_config *config, int line,
348 const char *pos)
349 {
350 config->fast_reauth = atoi(pos);
351 wpa_printf(MSG_DEBUG, "fast_reauth=%d", config->fast_reauth);
352 return 0;
353 }
354
355
356 #ifdef EAP_TLS_OPENSSL
357
358 static int wpa_config_process_opensc_engine_path(
359 const struct global_parse_data *data, struct wpa_config *config,
360 int line, const char *pos)
361 {
362 os_free(config->opensc_engine_path);
363 config->opensc_engine_path = os_strdup(pos);
364 wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
365 config->opensc_engine_path);
366 return 0;
367 }
368
369
370 static int wpa_config_process_pkcs11_engine_path(
371 const struct global_parse_data *data, struct wpa_config *config,
372 int line, const char *pos)
373 {
374 os_free(config->pkcs11_engine_path);
375 config->pkcs11_engine_path = os_strdup(pos);
376 wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
377 config->pkcs11_engine_path);
378 return 0;
379 }
380
381
382 static int wpa_config_process_pkcs11_module_path(
383 const struct global_parse_data *data, struct wpa_config *config,
384 int line, const char *pos)
385 {
386 os_free(config->pkcs11_module_path);
387 config->pkcs11_module_path = os_strdup(pos);
388 wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
389 config->pkcs11_module_path);
390 return 0;
391 }
392
393 #endif /* EAP_TLS_OPENSSL */
394
395
396 static int wpa_config_process_driver_param(
397 const struct global_parse_data *data, struct wpa_config *config,
398 int line, const char *pos)
399 {
400 os_free(config->driver_param);
401 config->driver_param = os_strdup(pos);
402 wpa_printf(MSG_DEBUG, "driver_param='%s'", config->driver_param);
403 return 0;
404 }
405
406
407 static int wpa_config_process_dot11RSNAConfigPMKLifetime(
408 const struct global_parse_data *data,
409 struct wpa_config *config, int line, const char *pos)
410 {
411 config->dot11RSNAConfigPMKLifetime = atoi(pos);
412 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
413 config->dot11RSNAConfigPMKLifetime);
414 return 0;
415 }
416
417
418 static int wpa_config_process_dot11RSNAConfigPMKReauthThreshold(
419 const struct global_parse_data *data,
420 struct wpa_config *config, int line, const char *pos)
421 {
422 config->dot11RSNAConfigPMKReauthThreshold = atoi(pos);
423 wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKReauthThreshold=%d",
424 config->dot11RSNAConfigPMKReauthThreshold);
425 return 0;
426 }
427
428
429 static int wpa_config_process_dot11RSNAConfigSATimeout(
430 const struct global_parse_data *data,
431 struct wpa_config *config, int line, const char *pos)
432 {
433 config->dot11RSNAConfigSATimeout = atoi(pos);
434 wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
435 config->dot11RSNAConfigSATimeout);
436 return 0;
437 }
438
439
440 #ifndef CONFIG_NO_CONFIG_WRITE
441 static int wpa_config_process_update_config(
442 const struct global_parse_data *data, struct wpa_config *config,
443 int line, const char *pos)
444 {
445 config->update_config = atoi(pos);
446 wpa_printf(MSG_DEBUG, "update_config=%d", config->update_config);
447 return 0;
448 }
449 #endif /* CONFIG_NO_CONFIG_WRITE */
450
451
452 static int wpa_config_process_load_dynamic_eap(
453 const struct global_parse_data *data, struct wpa_config *config,
454 int line, const char *so)
455 {
456 int ret;
457 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
458 ret = eap_peer_method_load(so);
459 if (ret == -2) {
460 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
461 "reloading.");
462 } else if (ret) {
463 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
464 "method '%s'.", line, so);
465 return -1;
466 }
467
468 return 0;
469 }
470
471
472 #ifdef CONFIG_WPS
473
474 static int wpa_config_process_uuid(const struct global_parse_data *data,
475 struct wpa_config *config, int line,
476 const char *pos)
477 {
478 char buf[40];
479 if (uuid_str2bin(pos, config->uuid)) {
480 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
481 return -1;
482 }
483 uuid_bin2str(config->uuid, buf, sizeof(buf));
484 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
485 return 0;
486 }
487
488
489 static int wpa_config_process_device_name(const struct global_parse_data *data,
490 struct wpa_config *config, int line,
491 const char *pos)
492 {
493 if (os_strlen(pos) > 32)
494 return -1;
495 os_free(config->device_name);
496 config->device_name = os_strdup(pos);
497 wpa_printf(MSG_DEBUG, "device_name='%s'", config->device_name);
498 return 0;
499 }
500
501
502 static int wpa_config_process_manufacturer(
503 const struct global_parse_data *data, struct wpa_config *config,
504 int line, const char *pos)
505 {
506 if (os_strlen(pos) > 64)
507 return -1;
508 os_free(config->manufacturer);
509 config->manufacturer = os_strdup(pos);
510 wpa_printf(MSG_DEBUG, "manufacturer='%s'", config->manufacturer);
511 return 0;
512 }
513
514
515 static int wpa_config_process_model_name(const struct global_parse_data *data,
516 struct wpa_config *config, int line,
517 const char *pos)
518 {
519 if (os_strlen(pos) > 32)
520 return -1;
521 os_free(config->model_name);
522 config->model_name = os_strdup(pos);
523 wpa_printf(MSG_DEBUG, "model_name='%s'", config->model_name);
524 return 0;
525 }
526
527
528 static int wpa_config_process_model_number(
529 const struct global_parse_data *data, struct wpa_config *config,
530 int line, const char *pos)
531 {
532 if (os_strlen(pos) > 32)
533 return -1;
534 os_free(config->model_number);
535 config->model_number = os_strdup(pos);
536 wpa_printf(MSG_DEBUG, "model_number='%s'", config->model_number);
537 return 0;
538 }
539
540
541 static int wpa_config_process_serial_number(
542 const struct global_parse_data *data, struct wpa_config *config,
543 int line, const char *pos)
544 {
545 if (os_strlen(pos) > 32)
546 return -1;
547 os_free(config->serial_number);
548 config->serial_number = os_strdup(pos);
549 wpa_printf(MSG_DEBUG, "serial_number='%s'", config->serial_number);
550 return 0;
551 }
552
553
554 static int wpa_config_process_device_type(const struct global_parse_data *data,
555 struct wpa_config *config, int line,
556 const char *pos)
557 {
558 os_free(config->device_type);
559 config->device_type = os_strdup(pos);
560 wpa_printf(MSG_DEBUG, "device_type='%s'", config->device_type);
561 return 0;
562 }
563
564
565 static int wpa_config_process_os_version(const struct global_parse_data *data,
566 struct wpa_config *config, int line,
567 const char *pos)
568 {
569 if (hexstr2bin(pos, config->os_version, 4)) {
570 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
571 return -1;
572 }
573 wpa_printf(MSG_DEBUG, "os_version=%08x",
574 WPA_GET_BE32(config->os_version));
575 return 0;
576 }
577
578 #endif /* CONFIG_WPS */
579
580
581 #define FUNC(f) #f, wpa_config_process_ ## f
582
583 static const struct global_parse_data global_fields[] = {
584 #ifdef CONFIG_CTRL_IFACE
585 { FUNC(ctrl_interface) },
586 { FUNC(ctrl_interface_group) },
587 #endif /* CONFIG_CTRL_IFACE */
588 { FUNC(eapol_version) },
589 { FUNC(ap_scan) },
590 { FUNC(fast_reauth) },
591 #ifdef EAP_TLS_OPENSSL
592 { FUNC(opensc_engine_path) },
593 { FUNC(pkcs11_engine_path) },
594 { FUNC(pkcs11_module_path) },
595 #endif /* EAP_TLS_OPENSSL */
596 { FUNC(driver_param) },
597 { FUNC(dot11RSNAConfigPMKLifetime) },
598 { FUNC(dot11RSNAConfigPMKReauthThreshold) },
599 { FUNC(dot11RSNAConfigSATimeout) },
600 #ifndef CONFIG_NO_CONFIG_WRITE
601 { FUNC(update_config) },
602 #endif /* CONFIG_NO_CONFIG_WRITE */
603 { FUNC(load_dynamic_eap) },
604 #ifdef CONFIG_WPS
605 { FUNC(uuid) },
606 { FUNC(device_name) },
607 { FUNC(manufacturer) },
608 { FUNC(model_name) },
609 { FUNC(model_number) },
610 { FUNC(serial_number) },
611 { FUNC(device_type) },
612 { FUNC(os_version) },
613 #endif /* CONFIG_WPS */
614 { FUNC(country) }
615 };
616 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
617
618
619 static int wpa_config_process_global(struct wpa_config *config, char *pos,
620 int line)
621 {
622 size_t i;
623 int ret = 0;
624
625 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
626 const struct global_parse_data *field = &global_fields[i];
627 size_t flen = os_strlen(field->name);
628 if (os_strncmp(pos, field->name, flen) != 0 ||
629 pos[flen] != '=')
630 continue;
631
632 if (field->parser(field, config, line, pos + flen + 1)) {
633 wpa_printf(MSG_ERROR, "Line %d: failed to "
634 "parse '%s'.", line, pos);
635 ret = -1;
636 }
637 break;
638 }
639 if (i == NUM_GLOBAL_FIELDS) {
640 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
641 line, pos);
642 ret = -1;
643 }
644
645 return ret;
646 }
647
648
649 struct wpa_config * wpa_config_read(const char *name)
650 {
651 FILE *f;
652 char buf[256], *pos;
653 int errors = 0, line = 0;
654 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
655 struct wpa_config *config;
656 int id = 0;
657
658 config = wpa_config_alloc_empty(NULL, NULL);
659 if (config == NULL)
660 return NULL;
661 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
662 f = fopen(name, "r");
663 if (f == NULL) {
664 os_free(config);
665 return NULL;
666 }
667
668 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
669 if (os_strcmp(pos, "network={") == 0) {
670 ssid = wpa_config_read_network(f, &line, id++);
671 if (ssid == NULL) {
672 wpa_printf(MSG_ERROR, "Line %d: failed to "
673 "parse network block.", line);
674 errors++;
675 continue;
676 }
677 if (head == NULL) {
678 head = tail = ssid;
679 } else {
680 tail->next = ssid;
681 tail = ssid;
682 }
683 if (wpa_config_add_prio_network(config, ssid)) {
684 wpa_printf(MSG_ERROR, "Line %d: failed to add "
685 "network block to priority list.",
686 line);
687 errors++;
688 continue;
689 }
690 #ifndef CONFIG_NO_CONFIG_BLOBS
691 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
692 if (wpa_config_process_blob(config, f, &line, pos + 12)
693 < 0) {
694 errors++;
695 continue;
696 }
697 #endif /* CONFIG_NO_CONFIG_BLOBS */
698 } else if (wpa_config_process_global(config, pos, line) < 0) {
699 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
700 "line '%s'.", line, pos);
701 errors++;
702 continue;
703 }
704 }
705
706 fclose(f);
707
708 config->ssid = head;
709 wpa_config_debug_dump_networks(config);
710
711 if (errors) {
712 wpa_config_free(config);
713 config = NULL;
714 head = NULL;
715 }
716
717 return config;
718 }
719
720
721 #ifndef CONFIG_NO_CONFIG_WRITE
722
723 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
724 {
725 char *value = wpa_config_get(ssid, field);
726 if (value == NULL)
727 return;
728 fprintf(f, "\t%s=%s\n", field, value);
729 os_free(value);
730 }
731
732
733 static void write_int(FILE *f, const char *field, int value, int def)
734 {
735 if (value == def)
736 return;
737 fprintf(f, "\t%s=%d\n", field, value);
738 }
739
740
741 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
742 {
743 char *value = wpa_config_get(ssid, "bssid");
744 if (value == NULL)
745 return;
746 fprintf(f, "\tbssid=%s\n", value);
747 os_free(value);
748 }
749
750
751 static void write_psk(FILE *f, struct wpa_ssid *ssid)
752 {
753 char *value = wpa_config_get(ssid, "psk");
754 if (value == NULL)
755 return;
756 fprintf(f, "\tpsk=%s\n", value);
757 os_free(value);
758 }
759
760
761 static void write_proto(FILE *f, struct wpa_ssid *ssid)
762 {
763 char *value;
764
765 if (ssid->proto == DEFAULT_PROTO)
766 return;
767
768 value = wpa_config_get(ssid, "proto");
769 if (value == NULL)
770 return;
771 if (value[0])
772 fprintf(f, "\tproto=%s\n", value);
773 os_free(value);
774 }
775
776
777 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
778 {
779 char *value;
780
781 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
782 return;
783
784 value = wpa_config_get(ssid, "key_mgmt");
785 if (value == NULL)
786 return;
787 if (value[0])
788 fprintf(f, "\tkey_mgmt=%s\n", value);
789 os_free(value);
790 }
791
792
793 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
794 {
795 char *value;
796
797 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
798 return;
799
800 value = wpa_config_get(ssid, "pairwise");
801 if (value == NULL)
802 return;
803 if (value[0])
804 fprintf(f, "\tpairwise=%s\n", value);
805 os_free(value);
806 }
807
808
809 static void write_group(FILE *f, struct wpa_ssid *ssid)
810 {
811 char *value;
812
813 if (ssid->group_cipher == DEFAULT_GROUP)
814 return;
815
816 value = wpa_config_get(ssid, "group");
817 if (value == NULL)
818 return;
819 if (value[0])
820 fprintf(f, "\tgroup=%s\n", value);
821 os_free(value);
822 }
823
824
825 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
826 {
827 char *value;
828
829 if (ssid->auth_alg == 0)
830 return;
831
832 value = wpa_config_get(ssid, "auth_alg");
833 if (value == NULL)
834 return;
835 if (value[0])
836 fprintf(f, "\tauth_alg=%s\n", value);
837 os_free(value);
838 }
839
840
841 #ifdef IEEE8021X_EAPOL
842 static void write_eap(FILE *f, struct wpa_ssid *ssid)
843 {
844 char *value;
845
846 value = wpa_config_get(ssid, "eap");
847 if (value == NULL)
848 return;
849
850 if (value[0])
851 fprintf(f, "\teap=%s\n", value);
852 os_free(value);
853 }
854 #endif /* IEEE8021X_EAPOL */
855
856
857 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
858 {
859 char field[20], *value;
860 int res;
861
862 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
863 if (res < 0 || (size_t) res >= sizeof(field))
864 return;
865 value = wpa_config_get(ssid, field);
866 if (value) {
867 fprintf(f, "\t%s=%s\n", field, value);
868 os_free(value);
869 }
870 }
871
872
873 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
874 {
875 int i;
876
877 #define STR(t) write_str(f, #t, ssid)
878 #define INT(t) write_int(f, #t, ssid->t, 0)
879 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
880 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
881 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
882
883 STR(ssid);
884 INT(scan_ssid);
885 write_bssid(f, ssid);
886 write_psk(f, ssid);
887 write_proto(f, ssid);
888 write_key_mgmt(f, ssid);
889 write_pairwise(f, ssid);
890 write_group(f, ssid);
891 write_auth_alg(f, ssid);
892 #ifdef IEEE8021X_EAPOL
893 write_eap(f, ssid);
894 STR(identity);
895 STR(anonymous_identity);
896 STR(password);
897 STR(ca_cert);
898 STR(ca_path);
899 STR(client_cert);
900 STR(private_key);
901 STR(private_key_passwd);
902 STR(dh_file);
903 STR(subject_match);
904 STR(altsubject_match);
905 STR(ca_cert2);
906 STR(ca_path2);
907 STR(client_cert2);
908 STR(private_key2);
909 STR(private_key2_passwd);
910 STR(dh_file2);
911 STR(subject_match2);
912 STR(altsubject_match2);
913 STR(phase1);
914 STR(phase2);
915 STR(pcsc);
916 STR(pin);
917 STR(engine_id);
918 STR(key_id);
919 STR(cert_id);
920 STR(ca_cert_id);
921 STR(key2_id);
922 STR(pin2);
923 STR(engine2_id);
924 STR(cert2_id);
925 STR(ca_cert2_id);
926 INTe(engine);
927 INTe(engine2);
928 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
929 #endif /* IEEE8021X_EAPOL */
930 for (i = 0; i < 4; i++)
931 write_wep_key(f, i, ssid);
932 INT(wep_tx_keyidx);
933 INT(priority);
934 #ifdef IEEE8021X_EAPOL
935 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
936 STR(pac_file);
937 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
938 #endif /* IEEE8021X_EAPOL */
939 INT(mode);
940 INT(proactive_key_caching);
941 INT(disabled);
942 INT(peerkey);
943 #ifdef CONFIG_IEEE80211W
944 INT(ieee80211w);
945 #endif /* CONFIG_IEEE80211W */
946 STR(id_str);
947
948 #undef STR
949 #undef INT
950 #undef INT_DEF
951 }
952
953
954 #ifndef CONFIG_NO_CONFIG_BLOBS
955 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
956 {
957 unsigned char *encoded;
958
959 encoded = base64_encode(blob->data, blob->len, NULL);
960 if (encoded == NULL)
961 return -1;
962
963 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
964 os_free(encoded);
965 return 0;
966 }
967 #endif /* CONFIG_NO_CONFIG_BLOBS */
968
969
970 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
971 {
972 #ifdef CONFIG_CTRL_IFACE
973 if (config->ctrl_interface)
974 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
975 if (config->ctrl_interface_group)
976 fprintf(f, "ctrl_interface_group=%s\n",
977 config->ctrl_interface_group);
978 #endif /* CONFIG_CTRL_IFACE */
979 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
980 fprintf(f, "eapol_version=%d\n", config->eapol_version);
981 if (config->ap_scan != DEFAULT_AP_SCAN)
982 fprintf(f, "ap_scan=%d\n", config->ap_scan);
983 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
984 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
985 #ifdef EAP_TLS_OPENSSL
986 if (config->opensc_engine_path)
987 fprintf(f, "opensc_engine_path=%s\n",
988 config->opensc_engine_path);
989 if (config->pkcs11_engine_path)
990 fprintf(f, "pkcs11_engine_path=%s\n",
991 config->pkcs11_engine_path);
992 if (config->pkcs11_module_path)
993 fprintf(f, "pkcs11_module_path=%s\n",
994 config->pkcs11_module_path);
995 #endif /* EAP_TLS_OPENSSL */
996 if (config->driver_param)
997 fprintf(f, "driver_param=%s\n", config->driver_param);
998 if (config->dot11RSNAConfigPMKLifetime)
999 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
1000 config->dot11RSNAConfigPMKLifetime);
1001 if (config->dot11RSNAConfigPMKReauthThreshold)
1002 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
1003 config->dot11RSNAConfigPMKReauthThreshold);
1004 if (config->dot11RSNAConfigSATimeout)
1005 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
1006 config->dot11RSNAConfigSATimeout);
1007 if (config->update_config)
1008 fprintf(f, "update_config=%d\n", config->update_config);
1009 #ifdef CONFIG_WPS
1010 if (is_nil_uuid(config->uuid)) {
1011 char buf[40];
1012 uuid_bin2str(config->uuid, buf, sizeof(buf));
1013 fprintf(f, "uuid=%s\n", buf);
1014 }
1015 if (config->device_name)
1016 fprintf(f, "device_name=%s\n", config->device_name);
1017 if (config->manufacturer)
1018 fprintf(f, "manufacturer=%s\n", config->manufacturer);
1019 if (config->model_name)
1020 fprintf(f, "model_name=%s\n", config->model_name);
1021 if (config->model_number)
1022 fprintf(f, "model_number=%s\n", config->model_number);
1023 if (config->serial_number)
1024 fprintf(f, "serial_number=%s\n", config->serial_number);
1025 if (config->device_type)
1026 fprintf(f, "device_type=%s\n", config->device_type);
1027 if (config->os_version)
1028 fprintf(f, "os_version=%08x\n",
1029 WPA_GET_BE32(config->os_version));
1030 #endif /* CONFIG_WPS */
1031 if (config->country[0] && config->country[1]) {
1032 fprintf(f, "country=%c%c\n",
1033 config->country[0], config->country[1]);
1034 }
1035 }
1036
1037 #endif /* CONFIG_NO_CONFIG_WRITE */
1038
1039
1040 int wpa_config_write(const char *name, struct wpa_config *config)
1041 {
1042 #ifndef CONFIG_NO_CONFIG_WRITE
1043 FILE *f;
1044 struct wpa_ssid *ssid;
1045 #ifndef CONFIG_NO_CONFIG_BLOBS
1046 struct wpa_config_blob *blob;
1047 #endif /* CONFIG_NO_CONFIG_BLOBS */
1048 int ret = 0;
1049
1050 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
1051
1052 f = fopen(name, "w");
1053 if (f == NULL) {
1054 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
1055 return -1;
1056 }
1057
1058 wpa_config_write_global(f, config);
1059
1060 for (ssid = config->ssid; ssid; ssid = ssid->next) {
1061 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
1062 continue; /* do not save temporary WPS networks */
1063 fprintf(f, "\nnetwork={\n");
1064 wpa_config_write_network(f, ssid);
1065 fprintf(f, "}\n");
1066 }
1067
1068 #ifndef CONFIG_NO_CONFIG_BLOBS
1069 for (blob = config->blobs; blob; blob = blob->next) {
1070 ret = wpa_config_write_blob(f, blob);
1071 if (ret)
1072 break;
1073 }
1074 #endif /* CONFIG_NO_CONFIG_BLOBS */
1075
1076 fclose(f);
1077
1078 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
1079 name, ret ? "un" : "");
1080 return ret;
1081 #else /* CONFIG_NO_CONFIG_WRITE */
1082 return -1;
1083 #endif /* CONFIG_NO_CONFIG_WRITE */
1084 }