revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / WirelessManager / wpa_supplicant / config_winreg.c
blob9a7825a00153e5ae2c92034b6f6d6b6907b33289
1 /*
2 * WPA Supplicant / Configuration backend: Windows registry
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 Windows registry. All the
15 * configuration information is stored in the registry and the format for
16 * network configuration fields is same as described in the sample
17 * configuration file, wpa_supplicant.conf.
19 * Configuration data is in
20 * \a HKEY_LOCAL_MACHINE\\SOFTWARE\\%wpa_supplicant\\configs
21 * key. Each configuration profile has its own key under this. In terms of text
22 * files, each profile would map to a separate text file with possibly multiple
23 * networks. Under each profile, there is a networks key that lists all
24 * networks as a subkey. Each network has set of values in the same way as
25 * network block in the configuration file. In addition, blobs subkey has
26 * possible blobs as values.
28 * Example network configuration block:
29 * \verbatim
30 HKEY_LOCAL_MACHINE\SOFTWARE\wpa_supplicant\configs\test\networks\0000
31 ssid="example"
32 key_mgmt=WPA-PSK
33 \endverbatim
36 #include "includes.h"
38 #include "common.h"
39 #include "uuid.h"
40 #include "config.h"
42 #ifndef WPA_KEY_ROOT
43 #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
44 #endif
45 #ifndef WPA_KEY_PREFIX
46 #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
47 #endif
49 #ifdef UNICODE
50 #define TSTR "%S"
51 #else /* UNICODE */
52 #define TSTR "%s"
53 #endif /* UNICODE */
56 static int wpa_config_read_blobs(struct wpa_config *config, HKEY hk)
58 struct wpa_config_blob *blob;
59 int errors = 0;
60 HKEY bhk;
61 LONG ret;
62 DWORD i;
64 ret = RegOpenKeyEx(hk, TEXT("blobs"), 0, KEY_QUERY_VALUE, &bhk);
65 if (ret != ERROR_SUCCESS) {
66 wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
67 "blobs key");
68 return 0; /* assume no blobs */
71 for (i = 0; ; i++) {
72 #define TNAMELEN 255
73 TCHAR name[TNAMELEN];
74 char data[4096];
75 DWORD namelen, datalen, type;
77 namelen = TNAMELEN;
78 datalen = sizeof(data);
79 ret = RegEnumValue(bhk, i, name, &namelen, NULL, &type,
80 (LPBYTE) data, &datalen);
82 if (ret == ERROR_NO_MORE_ITEMS)
83 break;
85 if (ret != ERROR_SUCCESS) {
86 wpa_printf(MSG_DEBUG, "RegEnumValue failed: 0x%x",
87 (unsigned int) ret);
88 break;
91 if (namelen >= TNAMELEN)
92 namelen = TNAMELEN - 1;
93 name[namelen] = TEXT('\0');
94 wpa_unicode2ascii_inplace(name);
96 if (datalen >= sizeof(data))
97 datalen = sizeof(data) - 1;
99 wpa_printf(MSG_MSGDUMP, "blob %d: field='%s' len %d",
100 (int) i, name, (int) datalen);
102 blob = os_zalloc(sizeof(*blob));
103 if (blob == NULL) {
104 errors++;
105 break;
107 blob->name = os_strdup((char *) name);
108 blob->data = os_malloc(datalen);
109 if (blob->name == NULL || blob->data == NULL) {
110 wpa_config_free_blob(blob);
111 errors++;
112 break;
114 os_memcpy(blob->data, data, datalen);
115 blob->len = datalen;
117 wpa_config_set_blob(config, blob);
120 RegCloseKey(bhk);
122 return errors ? -1 : 0;
126 static int wpa_config_read_reg_dword(HKEY hk, const TCHAR *name, int *_val)
128 DWORD val, buflen;
129 LONG ret;
131 buflen = sizeof(val);
132 ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) &val, &buflen);
133 if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
134 wpa_printf(MSG_DEBUG, TSTR "=%d", name, (int) val);
135 *_val = val;
136 return 0;
139 return -1;
143 static char * wpa_config_read_reg_string(HKEY hk, const TCHAR *name)
145 DWORD buflen;
146 LONG ret;
147 TCHAR *val;
149 buflen = 0;
150 ret = RegQueryValueEx(hk, name, NULL, NULL, NULL, &buflen);
151 if (ret != ERROR_SUCCESS)
152 return NULL;
153 val = os_malloc(buflen);
154 if (val == NULL)
155 return NULL;
157 ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) val, &buflen);
158 if (ret != ERROR_SUCCESS) {
159 os_free(val);
160 return NULL;
163 wpa_unicode2ascii_inplace(val);
164 wpa_printf(MSG_DEBUG, TSTR "=%s", name, (char *) val);
165 return (char *) val;
169 #ifdef CONFIG_WPS
170 static int wpa_config_read_global_uuid(struct wpa_config *config, HKEY hk)
172 char *str;
173 int ret = 0;
175 str = wpa_config_read_reg_string(hk, TEXT("uuid"));
176 if (str == NULL)
177 return 0;
179 if (uuid_str2bin(str, config->uuid))
180 ret = -1;
182 os_free(str);
184 return ret;
188 static int wpa_config_read_global_os_version(struct wpa_config *config,
189 HKEY hk)
191 char *str;
192 int ret = 0;
194 str = wpa_config_read_reg_string(hk, TEXT("os_version"));
195 if (str == NULL)
196 return 0;
198 if (hexstr2bin(str, config->os_version, 4))
199 ret = -1;
201 os_free(str);
203 return ret;
205 #endif /* CONFIG_WPS */
208 static int wpa_config_read_global(struct wpa_config *config, HKEY hk)
210 int errors = 0;
212 wpa_config_read_reg_dword(hk, TEXT("ap_scan"), &config->ap_scan);
213 wpa_config_read_reg_dword(hk, TEXT("fast_reauth"),
214 &config->fast_reauth);
215 wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
216 (int *) &config->dot11RSNAConfigPMKLifetime);
217 wpa_config_read_reg_dword(hk,
218 TEXT("dot11RSNAConfigPMKReauthThreshold"),
219 (int *)
220 &config->dot11RSNAConfigPMKReauthThreshold);
221 wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
222 (int *) &config->dot11RSNAConfigSATimeout);
223 wpa_config_read_reg_dword(hk, TEXT("update_config"),
224 &config->update_config);
226 if (wpa_config_read_reg_dword(hk, TEXT("eapol_version"),
227 &config->eapol_version) == 0) {
228 if (config->eapol_version < 1 ||
229 config->eapol_version > 2) {
230 wpa_printf(MSG_ERROR, "Invalid EAPOL version (%d)",
231 config->eapol_version);
232 errors++;
236 config->ctrl_interface = wpa_config_read_reg_string(
237 hk, TEXT("ctrl_interface"));
239 #ifdef CONFIG_WPS
240 if (wpa_config_read_global_uuid(config, hk))
241 errors++;
242 config->device_name = wpa_config_read_reg_string(
243 hk, TEXT("device_name"));
244 config->manufacturer = wpa_config_read_reg_string(
245 hk, TEXT("manufacturer"));
246 config->model_name = wpa_config_read_reg_string(
247 hk, TEXT("model_name"));
248 config->serial_number = wpa_config_read_reg_string(
249 hk, TEXT("serial_number"));
250 config->device_type = wpa_config_read_reg_string(
251 hk, TEXT("device_type"));
252 config->config_methods = wpa_config_read_reg_string(
253 hk, TEXT("config_methods"));
254 if (wpa_config_read_global_os_version(config, hk))
255 errors++;
256 wpa_config_read_reg_dword(hk, TEXT("wps_cred_processing"),
257 &config->wps_cred_processing);
258 #endif /* CONFIG_WPS */
260 wpa_config_read_reg_dword(hk, TEXT("bss_max_count"),
261 (int *) &config->bss_max_count);
262 wpa_config_read_reg_dword(hk, TEXT("filter_ssids"),
263 &config->filter_ssids);
265 return errors ? -1 : 0;
269 static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw,
270 int id)
272 HKEY nhk;
273 LONG ret;
274 DWORD i;
275 struct wpa_ssid *ssid;
276 int errors = 0;
278 ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk);
279 if (ret != ERROR_SUCCESS) {
280 wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
281 "network '" TSTR "'", netw);
282 return NULL;
285 wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw);
286 ssid = os_zalloc(sizeof(*ssid));
287 if (ssid == NULL) {
288 RegCloseKey(nhk);
289 return NULL;
291 ssid->id = id;
293 wpa_config_set_network_defaults(ssid);
295 for (i = 0; ; i++) {
296 TCHAR name[255], data[1024];
297 DWORD namelen, datalen, type;
299 namelen = 255;
300 datalen = sizeof(data);
301 ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type,
302 (LPBYTE) data, &datalen);
304 if (ret == ERROR_NO_MORE_ITEMS)
305 break;
307 if (ret != ERROR_SUCCESS) {
308 wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x",
309 (unsigned int) ret);
310 break;
313 if (namelen >= 255)
314 namelen = 255 - 1;
315 name[namelen] = TEXT('\0');
317 if (datalen >= 1024)
318 datalen = 1024 - 1;
319 data[datalen] = TEXT('\0');
321 wpa_unicode2ascii_inplace(name);
322 wpa_unicode2ascii_inplace(data);
323 if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0)
324 errors++;
327 RegCloseKey(nhk);
329 if (ssid->passphrase) {
330 if (ssid->psk_set) {
331 wpa_printf(MSG_ERROR, "Both PSK and passphrase "
332 "configured for network '" TSTR "'.", netw);
333 errors++;
335 wpa_config_update_psk(ssid);
338 if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
339 WPA_KEY_MGMT_PSK_SHA256)) &&
340 !ssid->psk_set) {
341 wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, "
342 "but no PSK configured for network '" TSTR "'.",
343 netw);
344 errors++;
347 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
348 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
349 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
350 /* Group cipher cannot be stronger than the pairwise cipher. */
351 wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher "
352 "list since it was not allowed for pairwise "
353 "cipher for network '" TSTR "'.", netw);
354 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
357 if (errors) {
358 wpa_config_free_ssid(ssid);
359 ssid = NULL;
362 return ssid;
366 static int wpa_config_read_networks(struct wpa_config *config, HKEY hk)
368 HKEY nhk;
369 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
370 int errors = 0;
371 LONG ret;
372 DWORD i;
374 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS,
375 &nhk);
376 if (ret != ERROR_SUCCESS) {
377 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks "
378 "registry key");
379 return -1;
382 for (i = 0; ; i++) {
383 TCHAR name[255];
384 DWORD namelen;
386 namelen = 255;
387 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
388 NULL);
390 if (ret == ERROR_NO_MORE_ITEMS)
391 break;
393 if (ret != ERROR_SUCCESS) {
394 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x",
395 (unsigned int) ret);
396 break;
399 if (namelen >= 255)
400 namelen = 255 - 1;
401 name[namelen] = '\0';
403 ssid = wpa_config_read_network(nhk, name, i);
404 if (ssid == NULL) {
405 wpa_printf(MSG_ERROR, "Failed to parse network "
406 "profile '%s'.", name);
407 errors++;
408 continue;
410 if (head == NULL) {
411 head = tail = ssid;
412 } else {
413 tail->next = ssid;
414 tail = ssid;
416 if (wpa_config_add_prio_network(config, ssid)) {
417 wpa_printf(MSG_ERROR, "Failed to add network profile "
418 "'%s' to priority list.", name);
419 errors++;
420 continue;
424 RegCloseKey(nhk);
426 config->ssid = head;
428 return errors ? -1 : 0;
432 struct wpa_config * wpa_config_read(const char *name)
434 TCHAR buf[256];
435 int errors = 0;
436 struct wpa_config *config;
437 HKEY hk;
438 LONG ret;
440 config = wpa_config_alloc_empty(NULL, NULL);
441 if (config == NULL)
442 return NULL;
443 wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);
445 #ifdef UNICODE
446 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
447 #else /* UNICODE */
448 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
449 #endif /* UNICODE */
451 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk);
452 if (ret != ERROR_SUCCESS) {
453 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
454 "configuration registry HKLM\\" TSTR, buf);
455 os_free(config);
456 return NULL;
459 if (wpa_config_read_global(config, hk))
460 errors++;
462 if (wpa_config_read_networks(config, hk))
463 errors++;
465 if (wpa_config_read_blobs(config, hk))
466 errors++;
468 wpa_config_debug_dump_networks(config);
470 RegCloseKey(hk);
472 if (errors) {
473 wpa_config_free(config);
474 config = NULL;
477 return config;
481 static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val,
482 int def)
484 LONG ret;
485 DWORD _val = val;
487 if (val == def) {
488 RegDeleteValue(hk, name);
489 return 0;
492 ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val,
493 sizeof(_val));
494 if (ret != ERROR_SUCCESS) {
495 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d",
496 name, val, (int) GetLastError());
497 return -1;
500 return 0;
504 static int wpa_config_write_reg_string(HKEY hk, const char *name,
505 const char *val)
507 LONG ret;
508 TCHAR *_name, *_val;
510 _name = wpa_strdup_tchar(name);
511 if (_name == NULL)
512 return -1;
514 if (val == NULL) {
515 RegDeleteValue(hk, _name);
516 os_free(_name);
517 return 0;
520 _val = wpa_strdup_tchar(val);
521 if (_val == NULL) {
522 os_free(_name);
523 return -1;
525 ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val,
526 (os_strlen(val) + 1) * sizeof(TCHAR));
527 if (ret != ERROR_SUCCESS) {
528 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': "
529 "error %d", name, val, (int) GetLastError());
530 os_free(_name);
531 os_free(_val);
532 return -1;
535 os_free(_name);
536 os_free(_val);
537 return 0;
541 static int wpa_config_write_global(struct wpa_config *config, HKEY hk)
543 #ifdef CONFIG_CTRL_IFACE
544 wpa_config_write_reg_string(hk, "ctrl_interface",
545 config->ctrl_interface);
546 #endif /* CONFIG_CTRL_IFACE */
548 wpa_config_write_reg_dword(hk, TEXT("eapol_version"),
549 config->eapol_version,
550 DEFAULT_EAPOL_VERSION);
551 wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan,
552 DEFAULT_AP_SCAN);
553 wpa_config_write_reg_dword(hk, TEXT("fast_reauth"),
554 config->fast_reauth, DEFAULT_FAST_REAUTH);
555 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
556 config->dot11RSNAConfigPMKLifetime, 0);
557 wpa_config_write_reg_dword(hk,
558 TEXT("dot11RSNAConfigPMKReauthThreshold"),
559 config->dot11RSNAConfigPMKReauthThreshold,
561 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
562 config->dot11RSNAConfigSATimeout, 0);
563 wpa_config_write_reg_dword(hk, TEXT("update_config"),
564 config->update_config,
566 #ifdef CONFIG_WPS
567 if (!is_nil_uuid(config->uuid)) {
568 char buf[40];
569 uuid_bin2str(config->uuid, buf, sizeof(buf));
570 wpa_config_write_reg_string(hk, "uuid", buf);
572 wpa_config_write_reg_string(hk, "device_name", config->device_name);
573 wpa_config_write_reg_string(hk, "manufacturer", config->manufacturer);
574 wpa_config_write_reg_string(hk, "model_name", config->model_name);
575 wpa_config_write_reg_string(hk, "model_number", config->model_number);
576 wpa_config_write_reg_string(hk, "serial_number",
577 config->serial_number);
578 wpa_config_write_reg_string(hk, "device_type", config->device_type);
579 wpa_config_write_reg_string(hk, "config_methods",
580 config->config_methods);
581 if (WPA_GET_BE32(config->os_version)) {
582 char vbuf[10];
583 os_snprintf(vbuf, sizeof(vbuf), "%08x",
584 WPA_GET_BE32(config->os_version));
585 wpa_config_write_reg_string(hk, "os_version", vbuf);
587 wpa_config_write_reg_dword(hk, TEXT("wps_cred_processing"),
588 config->wps_cred_processing, 0);
589 #endif /* CONFIG_WPS */
591 wpa_config_write_reg_dword(hk, TEXT("bss_max_count"),
592 config->bss_max_count,
593 DEFAULT_BSS_MAX_COUNT);
594 wpa_config_write_reg_dword(hk, TEXT("filter_ssids"),
595 config->filter_ssids, 0);
597 return 0;
601 static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key)
603 HKEY nhk;
604 int i, errors = 0;
605 LONG ret;
607 ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk);
608 if (ret != ERROR_SUCCESS) {
609 wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR
610 "' for subkey deletion: error 0x%x (%d)", key,
611 (unsigned int) ret, (int) GetLastError());
612 return 0;
615 for (i = 0; ; i++) {
616 TCHAR name[255];
617 DWORD namelen;
619 namelen = 255;
620 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
621 NULL);
623 if (ret == ERROR_NO_MORE_ITEMS)
624 break;
626 if (ret != ERROR_SUCCESS) {
627 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)",
628 (unsigned int) ret, (int) GetLastError());
629 break;
632 if (namelen >= 255)
633 namelen = 255 - 1;
634 name[namelen] = TEXT('\0');
636 ret = RegDeleteKey(nhk, name);
637 if (ret != ERROR_SUCCESS) {
638 wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)",
639 (unsigned int) ret, (int) GetLastError());
640 errors++;
644 RegCloseKey(nhk);
646 return errors ? -1 : 0;
650 static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid)
652 char *value = wpa_config_get(ssid, field);
653 if (value == NULL)
654 return;
655 wpa_config_write_reg_string(hk, field, value);
656 os_free(value);
660 static void write_int(HKEY hk, const char *field, int value, int def)
662 char val[20];
663 if (value == def)
664 return;
665 os_snprintf(val, sizeof(val), "%d", value);
666 wpa_config_write_reg_string(hk, field, val);
670 static void write_bssid(HKEY hk, struct wpa_ssid *ssid)
672 char *value = wpa_config_get(ssid, "bssid");
673 if (value == NULL)
674 return;
675 wpa_config_write_reg_string(hk, "bssid", value);
676 os_free(value);
680 static void write_psk(HKEY hk, struct wpa_ssid *ssid)
682 char *value = wpa_config_get(ssid, "psk");
683 if (value == NULL)
684 return;
685 wpa_config_write_reg_string(hk, "psk", value);
686 os_free(value);
690 static void write_proto(HKEY hk, struct wpa_ssid *ssid)
692 char *value;
694 if (ssid->proto == DEFAULT_PROTO)
695 return;
697 value = wpa_config_get(ssid, "proto");
698 if (value == NULL)
699 return;
700 if (value[0])
701 wpa_config_write_reg_string(hk, "proto", value);
702 os_free(value);
706 static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid)
708 char *value;
710 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
711 return;
713 value = wpa_config_get(ssid, "key_mgmt");
714 if (value == NULL)
715 return;
716 if (value[0])
717 wpa_config_write_reg_string(hk, "key_mgmt", value);
718 os_free(value);
722 static void write_pairwise(HKEY hk, struct wpa_ssid *ssid)
724 char *value;
726 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
727 return;
729 value = wpa_config_get(ssid, "pairwise");
730 if (value == NULL)
731 return;
732 if (value[0])
733 wpa_config_write_reg_string(hk, "pairwise", value);
734 os_free(value);
738 static void write_group(HKEY hk, struct wpa_ssid *ssid)
740 char *value;
742 if (ssid->group_cipher == DEFAULT_GROUP)
743 return;
745 value = wpa_config_get(ssid, "group");
746 if (value == NULL)
747 return;
748 if (value[0])
749 wpa_config_write_reg_string(hk, "group", value);
750 os_free(value);
754 static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid)
756 char *value;
758 if (ssid->auth_alg == 0)
759 return;
761 value = wpa_config_get(ssid, "auth_alg");
762 if (value == NULL)
763 return;
764 if (value[0])
765 wpa_config_write_reg_string(hk, "auth_alg", value);
766 os_free(value);
770 #ifdef IEEE8021X_EAPOL
771 static void write_eap(HKEY hk, struct wpa_ssid *ssid)
773 char *value;
775 value = wpa_config_get(ssid, "eap");
776 if (value == NULL)
777 return;
779 if (value[0])
780 wpa_config_write_reg_string(hk, "eap", value);
781 os_free(value);
783 #endif /* IEEE8021X_EAPOL */
786 static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid)
788 char field[20], *value;
790 os_snprintf(field, sizeof(field), "wep_key%d", idx);
791 value = wpa_config_get(ssid, field);
792 if (value) {
793 wpa_config_write_reg_string(hk, field, value);
794 os_free(value);
799 static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id)
801 int i, errors = 0;
802 HKEY nhk, netw;
803 LONG ret;
804 TCHAR name[5];
806 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk);
807 if (ret != ERROR_SUCCESS) {
808 wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key "
809 "for subkey addition: error 0x%x (%d)",
810 (unsigned int) ret, (int) GetLastError());
811 return 0;
814 #ifdef UNICODE
815 wsprintf(name, L"%04d", id);
816 #else /* UNICODE */
817 os_snprintf(name, sizeof(name), "%04d", id);
818 #endif /* UNICODE */
819 ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw,
820 NULL);
821 RegCloseKey(nhk);
822 if (ret != ERROR_SUCCESS) {
823 wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':"
824 " error 0x%x (%d)",
825 name, (unsigned int) ret, (int) GetLastError());
826 return -1;
829 #define STR(t) write_str(netw, #t, ssid)
830 #define INT(t) write_int(netw, #t, ssid->t, 0)
831 #define INTe(t) write_int(netw, #t, ssid->eap.t, 0)
832 #define INT_DEF(t, def) write_int(netw, #t, ssid->t, def)
833 #define INT_DEFe(t, def) write_int(netw, #t, ssid->eap.t, def)
835 STR(ssid);
836 INT(scan_ssid);
837 write_bssid(netw, ssid);
838 write_psk(netw, ssid);
839 write_proto(netw, ssid);
840 write_key_mgmt(netw, ssid);
841 write_pairwise(netw, ssid);
842 write_group(netw, ssid);
843 write_auth_alg(netw, ssid);
844 #ifdef IEEE8021X_EAPOL
845 write_eap(netw, ssid);
846 STR(identity);
847 STR(anonymous_identity);
848 STR(password);
849 STR(ca_cert);
850 STR(ca_path);
851 STR(client_cert);
852 STR(private_key);
853 STR(private_key_passwd);
854 STR(dh_file);
855 STR(subject_match);
856 STR(altsubject_match);
857 STR(ca_cert2);
858 STR(ca_path2);
859 STR(client_cert2);
860 STR(private_key2);
861 STR(private_key2_passwd);
862 STR(dh_file2);
863 STR(subject_match2);
864 STR(altsubject_match2);
865 STR(phase1);
866 STR(phase2);
867 STR(pcsc);
868 STR(pin);
869 STR(engine_id);
870 STR(key_id);
871 STR(cert_id);
872 STR(ca_cert_id);
873 STR(key2_id);
874 STR(pin2);
875 STR(engine2_id);
876 STR(cert2_id);
877 STR(ca_cert2_id);
878 INTe(engine);
879 INTe(engine2);
880 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
881 #endif /* IEEE8021X_EAPOL */
882 for (i = 0; i < 4; i++)
883 write_wep_key(netw, i, ssid);
884 INT(wep_tx_keyidx);
885 INT(priority);
886 #ifdef IEEE8021X_EAPOL
887 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
888 STR(pac_file);
889 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
890 #endif /* IEEE8021X_EAPOL */
891 INT(mode);
892 INT(proactive_key_caching);
893 INT(disabled);
894 INT(peerkey);
895 #ifdef CONFIG_IEEE80211W
896 INT(ieee80211w);
897 #endif /* CONFIG_IEEE80211W */
898 STR(id_str);
900 #undef STR
901 #undef INT
902 #undef INT_DEF
904 RegCloseKey(netw);
906 return errors ? -1 : 0;
910 static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob)
912 HKEY bhk;
913 LONG ret;
914 TCHAR *name;
916 ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL,
917 &bhk, NULL);
918 if (ret != ERROR_SUCCESS) {
919 wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: "
920 "error 0x%x (%d)",
921 (unsigned int) ret, (int) GetLastError());
922 return -1;
925 name = wpa_strdup_tchar(blob->name);
926 ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data,
927 blob->len);
928 if (ret != ERROR_SUCCESS) {
929 wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': "
930 "error 0x%x (%d)", blob->name, (unsigned int) ret,
931 (int) GetLastError());
932 RegCloseKey(bhk);
933 os_free(name);
934 return -1;
936 os_free(name);
938 RegCloseKey(bhk);
940 return 0;
944 int wpa_config_write(const char *name, struct wpa_config *config)
946 TCHAR buf[256];
947 HKEY hk;
948 LONG ret;
949 int errors = 0;
950 struct wpa_ssid *ssid;
951 struct wpa_config_blob *blob;
952 int id;
954 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
956 #ifdef UNICODE
957 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
958 #else /* UNICODE */
959 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
960 #endif /* UNICODE */
962 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk);
963 if (ret != ERROR_SUCCESS) {
964 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
965 "configuration registry %s: error %d", buf,
966 (int) GetLastError());
967 return -1;
970 if (wpa_config_write_global(config, hk)) {
971 wpa_printf(MSG_ERROR, "Failed to write global configuration "
972 "data");
973 errors++;
976 wpa_config_delete_subkeys(hk, TEXT("networks"));
977 for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) {
978 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
979 continue; /* do not save temporary WPS networks */
980 if (wpa_config_write_network(hk, ssid, id))
981 errors++;
984 RegDeleteKey(hk, TEXT("blobs"));
985 for (blob = config->blobs; blob; blob = blob->next) {
986 if (wpa_config_write_blob(hk, blob))
987 errors++;
990 RegCloseKey(hk);
992 wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully",
993 name, errors ? "un" : "");
994 return errors ? -1 : 0;