Use common driver code for Linux hwaddr get/set
[hostap-gosc2009.git] / wpa_supplicant / config_winreg.c
blob6bd003b6b2b2a607b1d7c9d7a988c96f50e8f6a3
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 return errors ? -1 : 0;
264 static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw,
265 int id)
267 HKEY nhk;
268 LONG ret;
269 DWORD i;
270 struct wpa_ssid *ssid;
271 int errors = 0;
273 ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk);
274 if (ret != ERROR_SUCCESS) {
275 wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
276 "network '" TSTR "'", netw);
277 return NULL;
280 wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw);
281 ssid = os_zalloc(sizeof(*ssid));
282 if (ssid == NULL) {
283 RegCloseKey(nhk);
284 return NULL;
286 ssid->id = id;
288 wpa_config_set_network_defaults(ssid);
290 for (i = 0; ; i++) {
291 TCHAR name[255], data[1024];
292 DWORD namelen, datalen, type;
294 namelen = 255;
295 datalen = sizeof(data);
296 ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type,
297 (LPBYTE) data, &datalen);
299 if (ret == ERROR_NO_MORE_ITEMS)
300 break;
302 if (ret != ERROR_SUCCESS) {
303 wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x",
304 (unsigned int) ret);
305 break;
308 if (namelen >= 255)
309 namelen = 255 - 1;
310 name[namelen] = TEXT('\0');
312 if (datalen >= 1024)
313 datalen = 1024 - 1;
314 data[datalen] = TEXT('\0');
316 wpa_unicode2ascii_inplace(name);
317 wpa_unicode2ascii_inplace(data);
318 if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0)
319 errors++;
322 RegCloseKey(nhk);
324 if (ssid->passphrase) {
325 if (ssid->psk_set) {
326 wpa_printf(MSG_ERROR, "Both PSK and passphrase "
327 "configured for network '" TSTR "'.", netw);
328 errors++;
330 wpa_config_update_psk(ssid);
333 if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
334 WPA_KEY_MGMT_PSK_SHA256)) &&
335 !ssid->psk_set) {
336 wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, "
337 "but no PSK configured for network '" TSTR "'.",
338 netw);
339 errors++;
342 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
343 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
344 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
345 /* Group cipher cannot be stronger than the pairwise cipher. */
346 wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher "
347 "list since it was not allowed for pairwise "
348 "cipher for network '" TSTR "'.", netw);
349 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
352 if (errors) {
353 wpa_config_free_ssid(ssid);
354 ssid = NULL;
357 return ssid;
361 static int wpa_config_read_networks(struct wpa_config *config, HKEY hk)
363 HKEY nhk;
364 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
365 int errors = 0;
366 LONG ret;
367 DWORD i;
369 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS,
370 &nhk);
371 if (ret != ERROR_SUCCESS) {
372 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks "
373 "registry key");
374 return -1;
377 for (i = 0; ; i++) {
378 TCHAR name[255];
379 DWORD namelen;
381 namelen = 255;
382 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
383 NULL);
385 if (ret == ERROR_NO_MORE_ITEMS)
386 break;
388 if (ret != ERROR_SUCCESS) {
389 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x",
390 (unsigned int) ret);
391 break;
394 if (namelen >= 255)
395 namelen = 255 - 1;
396 name[namelen] = '\0';
398 ssid = wpa_config_read_network(nhk, name, i);
399 if (ssid == NULL) {
400 wpa_printf(MSG_ERROR, "Failed to parse network "
401 "profile '%s'.", name);
402 errors++;
403 continue;
405 if (head == NULL) {
406 head = tail = ssid;
407 } else {
408 tail->next = ssid;
409 tail = ssid;
411 if (wpa_config_add_prio_network(config, ssid)) {
412 wpa_printf(MSG_ERROR, "Failed to add network profile "
413 "'%s' to priority list.", name);
414 errors++;
415 continue;
419 RegCloseKey(nhk);
421 config->ssid = head;
423 return errors ? -1 : 0;
427 struct wpa_config * wpa_config_read(const char *name)
429 TCHAR buf[256];
430 int errors = 0;
431 struct wpa_config *config;
432 HKEY hk;
433 LONG ret;
435 config = wpa_config_alloc_empty(NULL, NULL);
436 if (config == NULL)
437 return NULL;
438 wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);
440 #ifdef UNICODE
441 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
442 #else /* UNICODE */
443 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
444 #endif /* UNICODE */
446 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk);
447 if (ret != ERROR_SUCCESS) {
448 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
449 "configuration registry HKLM\\" TSTR, buf);
450 os_free(config);
451 return NULL;
454 if (wpa_config_read_global(config, hk))
455 errors++;
457 if (wpa_config_read_networks(config, hk))
458 errors++;
460 if (wpa_config_read_blobs(config, hk))
461 errors++;
463 wpa_config_debug_dump_networks(config);
465 RegCloseKey(hk);
467 if (errors) {
468 wpa_config_free(config);
469 config = NULL;
472 return config;
476 static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val,
477 int def)
479 LONG ret;
480 DWORD _val = val;
482 if (val == def) {
483 RegDeleteValue(hk, name);
484 return 0;
487 ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val,
488 sizeof(_val));
489 if (ret != ERROR_SUCCESS) {
490 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d",
491 name, val, (int) GetLastError());
492 return -1;
495 return 0;
499 static int wpa_config_write_reg_string(HKEY hk, const char *name,
500 const char *val)
502 LONG ret;
503 TCHAR *_name, *_val;
505 _name = wpa_strdup_tchar(name);
506 if (_name == NULL)
507 return -1;
509 if (val == NULL) {
510 RegDeleteValue(hk, _name);
511 os_free(_name);
512 return 0;
515 _val = wpa_strdup_tchar(val);
516 if (_val == NULL) {
517 os_free(_name);
518 return -1;
520 ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val,
521 (os_strlen(val) + 1) * sizeof(TCHAR));
522 if (ret != ERROR_SUCCESS) {
523 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': "
524 "error %d", name, val, (int) GetLastError());
525 os_free(_name);
526 os_free(_val);
527 return -1;
530 os_free(_name);
531 os_free(_val);
532 return 0;
536 static int wpa_config_write_global(struct wpa_config *config, HKEY hk)
538 #ifdef CONFIG_CTRL_IFACE
539 wpa_config_write_reg_string(hk, "ctrl_interface",
540 config->ctrl_interface);
541 #endif /* CONFIG_CTRL_IFACE */
543 wpa_config_write_reg_dword(hk, TEXT("eapol_version"),
544 config->eapol_version,
545 DEFAULT_EAPOL_VERSION);
546 wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan,
547 DEFAULT_AP_SCAN);
548 wpa_config_write_reg_dword(hk, TEXT("fast_reauth"),
549 config->fast_reauth, DEFAULT_FAST_REAUTH);
550 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
551 config->dot11RSNAConfigPMKLifetime, 0);
552 wpa_config_write_reg_dword(hk,
553 TEXT("dot11RSNAConfigPMKReauthThreshold"),
554 config->dot11RSNAConfigPMKReauthThreshold,
556 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
557 config->dot11RSNAConfigSATimeout, 0);
558 wpa_config_write_reg_dword(hk, TEXT("update_config"),
559 config->update_config,
561 #ifdef CONFIG_WPS
562 if (!is_nil_uuid(config->uuid)) {
563 char buf[40];
564 uuid_bin2str(config->uuid, buf, sizeof(buf));
565 wpa_config_write_reg_string(hk, "uuid", buf);
567 wpa_config_write_reg_string(hk, "device_name", config->device_name);
568 wpa_config_write_reg_string(hk, "manufacturer", config->manufacturer);
569 wpa_config_write_reg_string(hk, "model_name", config->model_name);
570 wpa_config_write_reg_string(hk, "model_number", config->model_number);
571 wpa_config_write_reg_string(hk, "serial_number",
572 config->serial_number);
573 wpa_config_write_reg_string(hk, "device_type", config->device_type);
574 wpa_config_write_reg_string(hk, "config_methods",
575 config->config_methods);
576 if (WPA_GET_BE32(config->os_version)) {
577 char vbuf[10];
578 os_snprintf(vbuf, sizeof(vbuf), "%08x",
579 WPA_GET_BE32(config->os_version));
580 wpa_config_write_reg_string(hk, "os_version", vbuf);
582 wpa_config_write_reg_dword(hk, TEXT("wps_cred_processing"),
583 config->wps_cred_processing, 0);
584 #endif /* CONFIG_WPS */
586 return 0;
590 static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key)
592 HKEY nhk;
593 int i, errors = 0;
594 LONG ret;
596 ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk);
597 if (ret != ERROR_SUCCESS) {
598 wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR
599 "' for subkey deletion: error 0x%x (%d)", key,
600 (unsigned int) ret, (int) GetLastError());
601 return 0;
604 for (i = 0; ; i++) {
605 TCHAR name[255];
606 DWORD namelen;
608 namelen = 255;
609 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
610 NULL);
612 if (ret == ERROR_NO_MORE_ITEMS)
613 break;
615 if (ret != ERROR_SUCCESS) {
616 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)",
617 (unsigned int) ret, (int) GetLastError());
618 break;
621 if (namelen >= 255)
622 namelen = 255 - 1;
623 name[namelen] = TEXT('\0');
625 ret = RegDeleteKey(nhk, name);
626 if (ret != ERROR_SUCCESS) {
627 wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)",
628 (unsigned int) ret, (int) GetLastError());
629 errors++;
633 RegCloseKey(nhk);
635 return errors ? -1 : 0;
639 static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid)
641 char *value = wpa_config_get(ssid, field);
642 if (value == NULL)
643 return;
644 wpa_config_write_reg_string(hk, field, value);
645 os_free(value);
649 static void write_int(HKEY hk, const char *field, int value, int def)
651 char val[20];
652 if (value == def)
653 return;
654 os_snprintf(val, sizeof(val), "%d", value);
655 wpa_config_write_reg_string(hk, field, val);
659 static void write_bssid(HKEY hk, struct wpa_ssid *ssid)
661 char *value = wpa_config_get(ssid, "bssid");
662 if (value == NULL)
663 return;
664 wpa_config_write_reg_string(hk, "bssid", value);
665 os_free(value);
669 static void write_psk(HKEY hk, struct wpa_ssid *ssid)
671 char *value = wpa_config_get(ssid, "psk");
672 if (value == NULL)
673 return;
674 wpa_config_write_reg_string(hk, "psk", value);
675 os_free(value);
679 static void write_proto(HKEY hk, struct wpa_ssid *ssid)
681 char *value;
683 if (ssid->proto == DEFAULT_PROTO)
684 return;
686 value = wpa_config_get(ssid, "proto");
687 if (value == NULL)
688 return;
689 if (value[0])
690 wpa_config_write_reg_string(hk, "proto", value);
691 os_free(value);
695 static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid)
697 char *value;
699 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
700 return;
702 value = wpa_config_get(ssid, "key_mgmt");
703 if (value == NULL)
704 return;
705 if (value[0])
706 wpa_config_write_reg_string(hk, "key_mgmt", value);
707 os_free(value);
711 static void write_pairwise(HKEY hk, struct wpa_ssid *ssid)
713 char *value;
715 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
716 return;
718 value = wpa_config_get(ssid, "pairwise");
719 if (value == NULL)
720 return;
721 if (value[0])
722 wpa_config_write_reg_string(hk, "pairwise", value);
723 os_free(value);
727 static void write_group(HKEY hk, struct wpa_ssid *ssid)
729 char *value;
731 if (ssid->group_cipher == DEFAULT_GROUP)
732 return;
734 value = wpa_config_get(ssid, "group");
735 if (value == NULL)
736 return;
737 if (value[0])
738 wpa_config_write_reg_string(hk, "group", value);
739 os_free(value);
743 static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid)
745 char *value;
747 if (ssid->auth_alg == 0)
748 return;
750 value = wpa_config_get(ssid, "auth_alg");
751 if (value == NULL)
752 return;
753 if (value[0])
754 wpa_config_write_reg_string(hk, "auth_alg", value);
755 os_free(value);
759 #ifdef IEEE8021X_EAPOL
760 static void write_eap(HKEY hk, struct wpa_ssid *ssid)
762 char *value;
764 value = wpa_config_get(ssid, "eap");
765 if (value == NULL)
766 return;
768 if (value[0])
769 wpa_config_write_reg_string(hk, "eap", value);
770 os_free(value);
772 #endif /* IEEE8021X_EAPOL */
775 static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid)
777 char field[20], *value;
779 os_snprintf(field, sizeof(field), "wep_key%d", idx);
780 value = wpa_config_get(ssid, field);
781 if (value) {
782 wpa_config_write_reg_string(hk, field, value);
783 os_free(value);
788 static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id)
790 int i, errors = 0;
791 HKEY nhk, netw;
792 LONG ret;
793 TCHAR name[5];
795 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk);
796 if (ret != ERROR_SUCCESS) {
797 wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key "
798 "for subkey addition: error 0x%x (%d)",
799 (unsigned int) ret, (int) GetLastError());
800 return 0;
803 #ifdef UNICODE
804 wsprintf(name, L"%04d", id);
805 #else /* UNICODE */
806 os_snprintf(name, sizeof(name), "%04d", id);
807 #endif /* UNICODE */
808 ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw,
809 NULL);
810 RegCloseKey(nhk);
811 if (ret != ERROR_SUCCESS) {
812 wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':"
813 " error 0x%x (%d)",
814 name, (unsigned int) ret, (int) GetLastError());
815 return -1;
818 #define STR(t) write_str(netw, #t, ssid)
819 #define INT(t) write_int(netw, #t, ssid->t, 0)
820 #define INTe(t) write_int(netw, #t, ssid->eap.t, 0)
821 #define INT_DEF(t, def) write_int(netw, #t, ssid->t, def)
822 #define INT_DEFe(t, def) write_int(netw, #t, ssid->eap.t, def)
824 STR(ssid);
825 INT(scan_ssid);
826 write_bssid(netw, ssid);
827 write_psk(netw, ssid);
828 write_proto(netw, ssid);
829 write_key_mgmt(netw, ssid);
830 write_pairwise(netw, ssid);
831 write_group(netw, ssid);
832 write_auth_alg(netw, ssid);
833 #ifdef IEEE8021X_EAPOL
834 write_eap(netw, ssid);
835 STR(identity);
836 STR(anonymous_identity);
837 STR(password);
838 STR(ca_cert);
839 STR(ca_path);
840 STR(client_cert);
841 STR(private_key);
842 STR(private_key_passwd);
843 STR(dh_file);
844 STR(subject_match);
845 STR(altsubject_match);
846 STR(ca_cert2);
847 STR(ca_path2);
848 STR(client_cert2);
849 STR(private_key2);
850 STR(private_key2_passwd);
851 STR(dh_file2);
852 STR(subject_match2);
853 STR(altsubject_match2);
854 STR(phase1);
855 STR(phase2);
856 STR(pcsc);
857 STR(pin);
858 STR(engine_id);
859 STR(key_id);
860 STR(cert_id);
861 STR(ca_cert_id);
862 STR(key2_id);
863 STR(pin2);
864 STR(engine2_id);
865 STR(cert2_id);
866 STR(ca_cert2_id);
867 INTe(engine);
868 INTe(engine2);
869 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
870 #endif /* IEEE8021X_EAPOL */
871 for (i = 0; i < 4; i++)
872 write_wep_key(netw, i, ssid);
873 INT(wep_tx_keyidx);
874 INT(priority);
875 #ifdef IEEE8021X_EAPOL
876 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
877 STR(pac_file);
878 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
879 #endif /* IEEE8021X_EAPOL */
880 INT(mode);
881 INT(proactive_key_caching);
882 INT(disabled);
883 INT(peerkey);
884 #ifdef CONFIG_IEEE80211W
885 INT(ieee80211w);
886 #endif /* CONFIG_IEEE80211W */
887 STR(id_str);
889 #undef STR
890 #undef INT
891 #undef INT_DEF
893 RegCloseKey(netw);
895 return errors ? -1 : 0;
899 static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob)
901 HKEY bhk;
902 LONG ret;
903 TCHAR *name;
905 ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL,
906 &bhk, NULL);
907 if (ret != ERROR_SUCCESS) {
908 wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: "
909 "error 0x%x (%d)",
910 (unsigned int) ret, (int) GetLastError());
911 return -1;
914 name = wpa_strdup_tchar(blob->name);
915 ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data,
916 blob->len);
917 if (ret != ERROR_SUCCESS) {
918 wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': "
919 "error 0x%x (%d)", blob->name, (unsigned int) ret,
920 (int) GetLastError());
921 RegCloseKey(bhk);
922 os_free(name);
923 return -1;
925 os_free(name);
927 RegCloseKey(bhk);
929 return 0;
933 int wpa_config_write(const char *name, struct wpa_config *config)
935 TCHAR buf[256];
936 HKEY hk;
937 LONG ret;
938 int errors = 0;
939 struct wpa_ssid *ssid;
940 struct wpa_config_blob *blob;
941 int id;
943 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
945 #ifdef UNICODE
946 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
947 #else /* UNICODE */
948 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
949 #endif /* UNICODE */
951 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk);
952 if (ret != ERROR_SUCCESS) {
953 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
954 "configuration registry %s: error %d", buf,
955 (int) GetLastError());
956 return -1;
959 if (wpa_config_write_global(config, hk)) {
960 wpa_printf(MSG_ERROR, "Failed to write global configuration "
961 "data");
962 errors++;
965 wpa_config_delete_subkeys(hk, TEXT("networks"));
966 for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) {
967 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
968 continue; /* do not save temporary WPS networks */
969 if (wpa_config_write_network(hk, ssid, id))
970 errors++;
973 RegDeleteKey(hk, TEXT("blobs"));
974 for (blob = config->blobs; blob; blob = blob->next) {
975 if (wpa_config_write_blob(hk, blob))
976 errors++;
979 RegCloseKey(hk);
981 wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully",
982 name, errors ? "un" : "");
983 return errors ? -1 : 0;