dbus: Remove duplication of object methods/signals/properties
[hostap-gosc2009.git] / wpa_supplicant / config_file.c
blob5b6233e63fc8b6cfcac0f39aad6977ab638c0ea2
1 /*
2 * WPA Supplicant / Configuration backend: text file
3 * Copyright (c) 2003-2008, 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
10 * license.
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.
19 #include "includes.h"
21 #include "common.h"
22 #include "config.h"
23 #include "base64.h"
24 #include "uuid.h"
25 #include "eap_peer/eap_methods.h"
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.
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.
42 static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
43 char **_pos)
45 char *pos, *end, *sstart;
47 while (fgets(s, size, stream)) {
48 (*line)++;
49 s[size - 1] = '\0';
50 pos = s;
52 /* Skip white space from the beginning of line. */
53 while (*pos == ' ' || *pos == '\t' || *pos == '\r')
54 pos++;
56 /* Skip comment lines and empty lines */
57 if (*pos == '#' || *pos == '\n' || *pos == '\0')
58 continue;
61 * Remove # comments unless they are within a double quoted
62 * string.
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;
75 /* Remove trailing white space. */
76 while (end > pos &&
77 (*end == '\n' || *end == ' ' || *end == '\t' ||
78 *end == '\r'))
79 *end-- = '\0';
81 if (*pos == '\0')
82 continue;
84 if (_pos)
85 *_pos = pos;
86 return pos;
89 if (_pos)
90 *_pos = NULL;
91 return NULL;
95 static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
97 int errors = 0;
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++;
105 wpa_config_update_psk(ssid);
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++;
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;
126 return errors;
130 static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
132 struct wpa_ssid *ssid;
133 int errors = 0, end = 0;
134 char buf[256], *pos, *pos2;
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;
143 wpa_config_set_network_defaults(ssid);
145 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
146 if (os_strcmp(pos, "}") == 0) {
147 end = 1;
148 break;
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;
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;
169 if (wpa_config_set(ssid, pos, pos2, *line) < 0)
170 errors++;
173 if (!end) {
174 wpa_printf(MSG_ERROR, "Line %d: network block was not "
175 "terminated properly.", *line);
176 errors++;
179 errors += wpa_config_validate_network(ssid, *line);
181 if (errors) {
182 wpa_config_free_ssid(ssid);
183 ssid = NULL;
186 return ssid;
190 #ifndef CONFIG_NO_CONFIG_BLOBS
191 static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
192 const char *name)
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;
200 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
201 *line, name);
203 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
204 if (os_strcmp(pos, "}") == 0) {
205 end = 1;
206 break;
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;
217 encoded = nencoded;
218 os_memcpy(encoded + encoded_len, pos, len);
219 encoded_len += len;
222 if (!end) {
223 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
224 "properly", *line);
225 os_free(encoded);
226 return NULL;
229 blob = os_zalloc(sizeof(*blob));
230 if (blob == NULL) {
231 os_free(encoded);
232 return NULL;
234 blob->name = os_strdup(name);
235 blob->data = base64_decode(encoded, encoded_len, &blob->len);
236 os_free(encoded);
238 if (blob->name == NULL || blob->data == NULL) {
239 wpa_config_free_blob(blob);
240 return NULL;
243 return blob;
247 static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
248 int *line, char *bname)
250 char *name_end;
251 struct wpa_config_blob *blob;
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;
259 *name_end = '\0';
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;
267 wpa_config_set_blob(config, blob);
268 return 0;
270 #endif /* CONFIG_NO_CONFIG_BLOBS */
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 void *param1, *param2, *param3;
281 static int wpa_config_parse_int(const struct global_parse_data *data,
282 struct wpa_config *config, int line,
283 const char *pos)
285 int *dst;
286 dst = (int *) (((u8 *) config) + (long) data->param1);
287 *dst = atoi(pos);
288 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
290 if (data->param2 && *dst < (long) data->param2) {
291 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
292 "min_value=%ld)", line, data->name, *dst,
293 (long) data->param2);
294 *dst = (long) data->param2;
295 return -1;
298 if (data->param3 && *dst > (long) data->param3) {
299 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
300 "max_value=%ld)", line, data->name, *dst,
301 (long) data->param3);
302 *dst = (long) data->param3;
303 return -1;
306 return 0;
310 static int wpa_config_parse_str(const struct global_parse_data *data,
311 struct wpa_config *config, int line,
312 const char *pos)
314 size_t len;
315 char **dst, *tmp;
317 len = os_strlen(pos);
318 if (data->param2 && len < (size_t) data->param2) {
319 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
320 "min_len=%ld)", line, data->name,
321 (unsigned long) len, (long) data->param2);
322 return -1;
325 if (data->param3 && len > (size_t) data->param3) {
326 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
327 "max_len=%ld)", line, data->name,
328 (unsigned long) len, (long) data->param3);
329 return -1;
332 tmp = os_strdup(pos);
333 if (tmp == NULL)
334 return -1;
336 dst = (char **) (((u8 *) config) + (long) data->param1);
337 os_free(*dst);
338 *dst = tmp;
339 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
341 return 0;
345 static int wpa_config_process_country(const struct global_parse_data *data,
346 struct wpa_config *config, int line,
347 const char *pos)
349 if (!pos[0] || !pos[1]) {
350 wpa_printf(MSG_DEBUG, "Invalid country set");
351 return -1;
353 config->country[0] = pos[0];
354 config->country[1] = pos[1];
355 wpa_printf(MSG_DEBUG, "country='%c%c'",
356 config->country[0], config->country[1]);
357 return 0;
361 static int wpa_config_process_load_dynamic_eap(
362 const struct global_parse_data *data, struct wpa_config *config,
363 int line, const char *so)
365 int ret;
366 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
367 ret = eap_peer_method_load(so);
368 if (ret == -2) {
369 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
370 "reloading.");
371 } else if (ret) {
372 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
373 "method '%s'.", line, so);
374 return -1;
377 return 0;
381 #ifdef CONFIG_WPS
383 static int wpa_config_process_uuid(const struct global_parse_data *data,
384 struct wpa_config *config, int line,
385 const char *pos)
387 char buf[40];
388 if (uuid_str2bin(pos, config->uuid)) {
389 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
390 return -1;
392 uuid_bin2str(config->uuid, buf, sizeof(buf));
393 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
394 return 0;
398 static int wpa_config_process_os_version(const struct global_parse_data *data,
399 struct wpa_config *config, int line,
400 const char *pos)
402 if (hexstr2bin(pos, config->os_version, 4)) {
403 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
404 return -1;
406 wpa_printf(MSG_DEBUG, "os_version=%08x",
407 WPA_GET_BE32(config->os_version));
408 return 0;
411 #endif /* CONFIG_WPS */
414 #ifdef OFFSET
415 #undef OFFSET
416 #endif /* OFFSET */
417 /* OFFSET: Get offset of a variable within the wpa_config structure */
418 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
420 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
421 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
422 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f)
423 #define INT(f) _INT(f), NULL, NULL
424 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
425 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
426 #define STR(f) _STR(f), NULL, NULL
427 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
429 static const struct global_parse_data global_fields[] = {
430 #ifdef CONFIG_CTRL_IFACE
431 { STR(ctrl_interface) },
432 { STR(ctrl_interface_group) } /* deprecated */,
433 #endif /* CONFIG_CTRL_IFACE */
434 { INT_RANGE(eapol_version, 1, 2) },
435 { INT(ap_scan) },
436 { INT(fast_reauth) },
437 { STR(opensc_engine_path) },
438 { STR(pkcs11_engine_path) },
439 { STR(pkcs11_module_path) },
440 { STR(driver_param) },
441 { INT(dot11RSNAConfigPMKLifetime) },
442 { INT(dot11RSNAConfigPMKReauthThreshold) },
443 { INT(dot11RSNAConfigSATimeout) },
444 #ifndef CONFIG_NO_CONFIG_WRITE
445 { INT(update_config) },
446 #endif /* CONFIG_NO_CONFIG_WRITE */
447 { FUNC_NO_VAR(load_dynamic_eap) },
448 #ifdef CONFIG_WPS
449 { FUNC(uuid) },
450 { STR_RANGE(device_name, 0, 32) },
451 { STR_RANGE(manufacturer, 0, 64) },
452 { STR_RANGE(model_name, 0, 32) },
453 { STR_RANGE(model_number, 0, 32) },
454 { STR_RANGE(serial_number, 0, 32) },
455 { STR(device_type) },
456 { FUNC(os_version) },
457 { STR(config_methods) },
458 { INT_RANGE(wps_cred_processing, 0, 2) },
459 #endif /* CONFIG_WPS */
460 { FUNC(country) }
463 #undef FUNC
464 #undef _INT
465 #undef INT
466 #undef INT_RANGE
467 #undef _STR
468 #undef STR
469 #undef STR_RANGE
470 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
473 static int wpa_config_process_global(struct wpa_config *config, char *pos,
474 int line)
476 size_t i;
477 int ret = 0;
479 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
480 const struct global_parse_data *field = &global_fields[i];
481 size_t flen = os_strlen(field->name);
482 if (os_strncmp(pos, field->name, flen) != 0 ||
483 pos[flen] != '=')
484 continue;
486 if (field->parser(field, config, line, pos + flen + 1)) {
487 wpa_printf(MSG_ERROR, "Line %d: failed to "
488 "parse '%s'.", line, pos);
489 ret = -1;
491 break;
493 if (i == NUM_GLOBAL_FIELDS) {
494 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
495 line, pos);
496 ret = -1;
499 return ret;
503 struct wpa_config * wpa_config_read(const char *name)
505 FILE *f;
506 char buf[256], *pos;
507 int errors = 0, line = 0;
508 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
509 struct wpa_config *config;
510 int id = 0;
512 config = wpa_config_alloc_empty(NULL, NULL);
513 if (config == NULL)
514 return NULL;
515 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
516 f = fopen(name, "r");
517 if (f == NULL) {
518 os_free(config);
519 return NULL;
522 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
523 if (os_strcmp(pos, "network={") == 0) {
524 ssid = wpa_config_read_network(f, &line, id++);
525 if (ssid == NULL) {
526 wpa_printf(MSG_ERROR, "Line %d: failed to "
527 "parse network block.", line);
528 errors++;
529 continue;
531 if (head == NULL) {
532 head = tail = ssid;
533 } else {
534 tail->next = ssid;
535 tail = ssid;
537 if (wpa_config_add_prio_network(config, ssid)) {
538 wpa_printf(MSG_ERROR, "Line %d: failed to add "
539 "network block to priority list.",
540 line);
541 errors++;
542 continue;
544 #ifndef CONFIG_NO_CONFIG_BLOBS
545 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
546 if (wpa_config_process_blob(config, f, &line, pos + 12)
547 < 0) {
548 errors++;
549 continue;
551 #endif /* CONFIG_NO_CONFIG_BLOBS */
552 } else if (wpa_config_process_global(config, pos, line) < 0) {
553 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
554 "line '%s'.", line, pos);
555 errors++;
556 continue;
560 fclose(f);
562 config->ssid = head;
563 wpa_config_debug_dump_networks(config);
565 if (errors) {
566 wpa_config_free(config);
567 config = NULL;
568 head = NULL;
571 return config;
575 #ifndef CONFIG_NO_CONFIG_WRITE
577 static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
579 char *value = wpa_config_get(ssid, field);
580 if (value == NULL)
581 return;
582 fprintf(f, "\t%s=%s\n", field, value);
583 os_free(value);
587 static void write_int(FILE *f, const char *field, int value, int def)
589 if (value == def)
590 return;
591 fprintf(f, "\t%s=%d\n", field, value);
595 static void write_bssid(FILE *f, struct wpa_ssid *ssid)
597 char *value = wpa_config_get(ssid, "bssid");
598 if (value == NULL)
599 return;
600 fprintf(f, "\tbssid=%s\n", value);
601 os_free(value);
605 static void write_psk(FILE *f, struct wpa_ssid *ssid)
607 char *value = wpa_config_get(ssid, "psk");
608 if (value == NULL)
609 return;
610 fprintf(f, "\tpsk=%s\n", value);
611 os_free(value);
615 static void write_proto(FILE *f, struct wpa_ssid *ssid)
617 char *value;
619 if (ssid->proto == DEFAULT_PROTO)
620 return;
622 value = wpa_config_get(ssid, "proto");
623 if (value == NULL)
624 return;
625 if (value[0])
626 fprintf(f, "\tproto=%s\n", value);
627 os_free(value);
631 static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
633 char *value;
635 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
636 return;
638 value = wpa_config_get(ssid, "key_mgmt");
639 if (value == NULL)
640 return;
641 if (value[0])
642 fprintf(f, "\tkey_mgmt=%s\n", value);
643 os_free(value);
647 static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
649 char *value;
651 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
652 return;
654 value = wpa_config_get(ssid, "pairwise");
655 if (value == NULL)
656 return;
657 if (value[0])
658 fprintf(f, "\tpairwise=%s\n", value);
659 os_free(value);
663 static void write_group(FILE *f, struct wpa_ssid *ssid)
665 char *value;
667 if (ssid->group_cipher == DEFAULT_GROUP)
668 return;
670 value = wpa_config_get(ssid, "group");
671 if (value == NULL)
672 return;
673 if (value[0])
674 fprintf(f, "\tgroup=%s\n", value);
675 os_free(value);
679 static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
681 char *value;
683 if (ssid->auth_alg == 0)
684 return;
686 value = wpa_config_get(ssid, "auth_alg");
687 if (value == NULL)
688 return;
689 if (value[0])
690 fprintf(f, "\tauth_alg=%s\n", value);
691 os_free(value);
695 #ifdef IEEE8021X_EAPOL
696 static void write_eap(FILE *f, struct wpa_ssid *ssid)
698 char *value;
700 value = wpa_config_get(ssid, "eap");
701 if (value == NULL)
702 return;
704 if (value[0])
705 fprintf(f, "\teap=%s\n", value);
706 os_free(value);
708 #endif /* IEEE8021X_EAPOL */
711 static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
713 char field[20], *value;
714 int res;
716 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
717 if (res < 0 || (size_t) res >= sizeof(field))
718 return;
719 value = wpa_config_get(ssid, field);
720 if (value) {
721 fprintf(f, "\t%s=%s\n", field, value);
722 os_free(value);
727 static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
729 int i;
731 #define STR(t) write_str(f, #t, ssid)
732 #define INT(t) write_int(f, #t, ssid->t, 0)
733 #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
734 #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
735 #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
737 STR(ssid);
738 INT(scan_ssid);
739 write_bssid(f, ssid);
740 write_psk(f, ssid);
741 write_proto(f, ssid);
742 write_key_mgmt(f, ssid);
743 write_pairwise(f, ssid);
744 write_group(f, ssid);
745 write_auth_alg(f, ssid);
746 #ifdef IEEE8021X_EAPOL
747 write_eap(f, ssid);
748 STR(identity);
749 STR(anonymous_identity);
750 STR(password);
751 STR(ca_cert);
752 STR(ca_path);
753 STR(client_cert);
754 STR(private_key);
755 STR(private_key_passwd);
756 STR(dh_file);
757 STR(subject_match);
758 STR(altsubject_match);
759 STR(ca_cert2);
760 STR(ca_path2);
761 STR(client_cert2);
762 STR(private_key2);
763 STR(private_key2_passwd);
764 STR(dh_file2);
765 STR(subject_match2);
766 STR(altsubject_match2);
767 STR(phase1);
768 STR(phase2);
769 STR(pcsc);
770 STR(pin);
771 STR(engine_id);
772 STR(key_id);
773 STR(cert_id);
774 STR(ca_cert_id);
775 STR(key2_id);
776 STR(pin2);
777 STR(engine2_id);
778 STR(cert2_id);
779 STR(ca_cert2_id);
780 INTe(engine);
781 INTe(engine2);
782 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
783 #endif /* IEEE8021X_EAPOL */
784 for (i = 0; i < 4; i++)
785 write_wep_key(f, i, ssid);
786 INT(wep_tx_keyidx);
787 INT(priority);
788 #ifdef IEEE8021X_EAPOL
789 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
790 STR(pac_file);
791 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
792 #endif /* IEEE8021X_EAPOL */
793 INT(mode);
794 INT(proactive_key_caching);
795 INT(disabled);
796 INT(peerkey);
797 #ifdef CONFIG_IEEE80211W
798 INT(ieee80211w);
799 #endif /* CONFIG_IEEE80211W */
800 STR(id_str);
802 #undef STR
803 #undef INT
804 #undef INT_DEF
808 #ifndef CONFIG_NO_CONFIG_BLOBS
809 static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
811 unsigned char *encoded;
813 encoded = base64_encode(blob->data, blob->len, NULL);
814 if (encoded == NULL)
815 return -1;
817 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
818 os_free(encoded);
819 return 0;
821 #endif /* CONFIG_NO_CONFIG_BLOBS */
824 static void wpa_config_write_global(FILE *f, struct wpa_config *config)
826 #ifdef CONFIG_CTRL_IFACE
827 if (config->ctrl_interface)
828 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
829 if (config->ctrl_interface_group)
830 fprintf(f, "ctrl_interface_group=%s\n",
831 config->ctrl_interface_group);
832 #endif /* CONFIG_CTRL_IFACE */
833 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
834 fprintf(f, "eapol_version=%d\n", config->eapol_version);
835 if (config->ap_scan != DEFAULT_AP_SCAN)
836 fprintf(f, "ap_scan=%d\n", config->ap_scan);
837 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
838 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
839 if (config->opensc_engine_path)
840 fprintf(f, "opensc_engine_path=%s\n",
841 config->opensc_engine_path);
842 if (config->pkcs11_engine_path)
843 fprintf(f, "pkcs11_engine_path=%s\n",
844 config->pkcs11_engine_path);
845 if (config->pkcs11_module_path)
846 fprintf(f, "pkcs11_module_path=%s\n",
847 config->pkcs11_module_path);
848 if (config->driver_param)
849 fprintf(f, "driver_param=%s\n", config->driver_param);
850 if (config->dot11RSNAConfigPMKLifetime)
851 fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
852 config->dot11RSNAConfigPMKLifetime);
853 if (config->dot11RSNAConfigPMKReauthThreshold)
854 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
855 config->dot11RSNAConfigPMKReauthThreshold);
856 if (config->dot11RSNAConfigSATimeout)
857 fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
858 config->dot11RSNAConfigSATimeout);
859 if (config->update_config)
860 fprintf(f, "update_config=%d\n", config->update_config);
861 #ifdef CONFIG_WPS
862 if (!is_nil_uuid(config->uuid)) {
863 char buf[40];
864 uuid_bin2str(config->uuid, buf, sizeof(buf));
865 fprintf(f, "uuid=%s\n", buf);
867 if (config->device_name)
868 fprintf(f, "device_name=%s\n", config->device_name);
869 if (config->manufacturer)
870 fprintf(f, "manufacturer=%s\n", config->manufacturer);
871 if (config->model_name)
872 fprintf(f, "model_name=%s\n", config->model_name);
873 if (config->model_number)
874 fprintf(f, "model_number=%s\n", config->model_number);
875 if (config->serial_number)
876 fprintf(f, "serial_number=%s\n", config->serial_number);
877 if (config->device_type)
878 fprintf(f, "device_type=%s\n", config->device_type);
879 if (WPA_GET_BE32(config->os_version))
880 fprintf(f, "os_version=%08x\n",
881 WPA_GET_BE32(config->os_version));
882 if (config->config_methods)
883 fprintf(f, "config_methods=%s\n", config->config_methods);
884 if (config->wps_cred_processing)
885 fprintf(f, "wps_cred_processing=%d\n",
886 config->wps_cred_processing);
887 #endif /* CONFIG_WPS */
888 if (config->country[0] && config->country[1]) {
889 fprintf(f, "country=%c%c\n",
890 config->country[0], config->country[1]);
894 #endif /* CONFIG_NO_CONFIG_WRITE */
897 int wpa_config_write(const char *name, struct wpa_config *config)
899 #ifndef CONFIG_NO_CONFIG_WRITE
900 FILE *f;
901 struct wpa_ssid *ssid;
902 #ifndef CONFIG_NO_CONFIG_BLOBS
903 struct wpa_config_blob *blob;
904 #endif /* CONFIG_NO_CONFIG_BLOBS */
905 int ret = 0;
907 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
909 f = fopen(name, "w");
910 if (f == NULL) {
911 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
912 return -1;
915 wpa_config_write_global(f, config);
917 for (ssid = config->ssid; ssid; ssid = ssid->next) {
918 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
919 continue; /* do not save temporary WPS networks */
920 fprintf(f, "\nnetwork={\n");
921 wpa_config_write_network(f, ssid);
922 fprintf(f, "}\n");
925 #ifndef CONFIG_NO_CONFIG_BLOBS
926 for (blob = config->blobs; blob; blob = blob->next) {
927 ret = wpa_config_write_blob(f, blob);
928 if (ret)
929 break;
931 #endif /* CONFIG_NO_CONFIG_BLOBS */
933 fclose(f);
935 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
936 name, ret ? "un" : "");
937 return ret;
938 #else /* CONFIG_NO_CONFIG_WRITE */
939 return -1;
940 #endif /* CONFIG_NO_CONFIG_WRITE */