]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * WPA Supplicant / Configuration backend: Windows registry | |
56586197 | 3 | * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi> |
6fc6879b JM |
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 | * | |
b39d1280 | 14 | * This file implements a configuration backend for Windows registry. All the |
6fc6879b JM |
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. | |
18 | * | |
b39d1280 JM |
19 | * Configuration data is in |
20 | * \a HKEY_LOCAL_MACHINE\\SOFTWARE\\%wpa_supplicant\\configs | |
6fc6879b JM |
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. | |
27 | * | |
b39d1280 JM |
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 | |
6fc6879b JM |
34 | */ |
35 | ||
36 | #include "includes.h" | |
37 | ||
38 | #include "common.h" | |
f855f923 | 39 | #include "uuid.h" |
6fc6879b JM |
40 | #include "config.h" |
41 | ||
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 | |
48 | ||
49 | #ifdef UNICODE | |
50 | #define TSTR "%S" | |
51 | #else /* UNICODE */ | |
52 | #define TSTR "%s" | |
53 | #endif /* UNICODE */ | |
54 | ||
55 | ||
56 | static int wpa_config_read_blobs(struct wpa_config *config, HKEY hk) | |
57 | { | |
58 | struct wpa_config_blob *blob; | |
59 | int errors = 0; | |
60 | HKEY bhk; | |
61 | LONG ret; | |
62 | DWORD i; | |
63 | ||
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 */ | |
69 | } | |
70 | ||
71 | for (i = 0; ; i++) { | |
72 | #define TNAMELEN 255 | |
73 | TCHAR name[TNAMELEN]; | |
74 | char data[4096]; | |
75 | DWORD namelen, datalen, type; | |
76 | ||
77 | namelen = TNAMELEN; | |
78 | datalen = sizeof(data); | |
79 | ret = RegEnumValue(bhk, i, name, &namelen, NULL, &type, | |
80 | (LPBYTE) data, &datalen); | |
81 | ||
82 | if (ret == ERROR_NO_MORE_ITEMS) | |
83 | break; | |
84 | ||
85 | if (ret != ERROR_SUCCESS) { | |
86 | wpa_printf(MSG_DEBUG, "RegEnumValue failed: 0x%x", | |
87 | (unsigned int) ret); | |
88 | break; | |
89 | } | |
90 | ||
91 | if (namelen >= TNAMELEN) | |
92 | namelen = TNAMELEN - 1; | |
93 | name[namelen] = TEXT('\0'); | |
94 | wpa_unicode2ascii_inplace(name); | |
95 | ||
96 | if (datalen >= sizeof(data)) | |
97 | datalen = sizeof(data) - 1; | |
98 | ||
99 | wpa_printf(MSG_MSGDUMP, "blob %d: field='%s' len %d", | |
100 | (int) i, name, (int) datalen); | |
101 | ||
102 | blob = os_zalloc(sizeof(*blob)); | |
103 | if (blob == NULL) { | |
104 | errors++; | |
105 | break; | |
106 | } | |
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; | |
113 | } | |
114 | os_memcpy(blob->data, data, datalen); | |
115 | blob->len = datalen; | |
116 | ||
117 | wpa_config_set_blob(config, blob); | |
118 | } | |
119 | ||
120 | RegCloseKey(bhk); | |
121 | ||
122 | return errors ? -1 : 0; | |
123 | } | |
124 | ||
125 | ||
126 | static int wpa_config_read_reg_dword(HKEY hk, const TCHAR *name, int *_val) | |
127 | { | |
128 | DWORD val, buflen; | |
129 | LONG ret; | |
130 | ||
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; | |
137 | } | |
138 | ||
139 | return -1; | |
140 | } | |
141 | ||
142 | ||
143 | static char * wpa_config_read_reg_string(HKEY hk, const TCHAR *name) | |
144 | { | |
145 | DWORD buflen; | |
146 | LONG ret; | |
147 | TCHAR *val; | |
148 | ||
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; | |
156 | ||
157 | ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) val, &buflen); | |
158 | if (ret != ERROR_SUCCESS) { | |
159 | os_free(val); | |
160 | return NULL; | |
161 | } | |
162 | ||
163 | wpa_unicode2ascii_inplace(val); | |
164 | wpa_printf(MSG_DEBUG, TSTR "=%s", name, (char *) val); | |
165 | return (char *) val; | |
166 | } | |
167 | ||
168 | ||
f855f923 JM |
169 | #ifdef CONFIG_WPS |
170 | static int wpa_config_read_global_uuid(struct wpa_config *config, HKEY hk) | |
171 | { | |
172 | char *str; | |
173 | int ret = 0; | |
174 | ||
175 | str = wpa_config_read_reg_string(hk, TEXT("uuid")); | |
176 | if (str == NULL) | |
177 | return 0; | |
178 | ||
179 | if (uuid_str2bin(str, config->uuid)) | |
180 | ret = -1; | |
181 | ||
182 | os_free(str); | |
183 | ||
184 | return ret; | |
185 | } | |
3c0b7aa4 JM |
186 | |
187 | ||
188 | static int wpa_config_read_global_os_version(struct wpa_config *config, | |
189 | HKEY hk) | |
190 | { | |
191 | char *str; | |
192 | int ret = 0; | |
193 | ||
194 | str = wpa_config_read_reg_string(hk, TEXT("os_version")); | |
195 | if (str == NULL) | |
196 | return 0; | |
197 | ||
198 | if (hexstr2bin(str, config->os_version, 4)) | |
199 | ret = -1; | |
200 | ||
201 | os_free(str); | |
202 | ||
203 | return ret; | |
204 | } | |
f855f923 JM |
205 | #endif /* CONFIG_WPS */ |
206 | ||
207 | ||
6fc6879b JM |
208 | static int wpa_config_read_global(struct wpa_config *config, HKEY hk) |
209 | { | |
210 | int errors = 0; | |
211 | ||
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"), | |
3c0b7aa4 | 216 | (int *) &config->dot11RSNAConfigPMKLifetime); |
6fc6879b JM |
217 | wpa_config_read_reg_dword(hk, |
218 | TEXT("dot11RSNAConfigPMKReauthThreshold"), | |
3c0b7aa4 | 219 | (int *) |
6fc6879b JM |
220 | &config->dot11RSNAConfigPMKReauthThreshold); |
221 | wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"), | |
3c0b7aa4 | 222 | (int *) &config->dot11RSNAConfigSATimeout); |
6fc6879b JM |
223 | wpa_config_read_reg_dword(hk, TEXT("update_config"), |
224 | &config->update_config); | |
225 | ||
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++; | |
233 | } | |
234 | } | |
235 | ||
236 | config->ctrl_interface = wpa_config_read_reg_string( | |
237 | hk, TEXT("ctrl_interface")); | |
238 | ||
f855f923 JM |
239 | #ifdef CONFIG_WPS |
240 | if (wpa_config_read_global_uuid(config, hk)) | |
241 | errors++; | |
3c0b7aa4 JM |
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")); | |
c0e4dd9e JM |
252 | config->config_methods = wpa_config_read_reg_string( |
253 | hk, TEXT("config_methods")); | |
3c0b7aa4 JM |
254 | if (wpa_config_read_global_os_version(config, hk)) |
255 | errors++; | |
47662164 JM |
256 | wpa_config_read_reg_dword(hk, TEXT("wps_cred_processing"), |
257 | &config->wps_cred_processing); | |
f855f923 | 258 | #endif /* CONFIG_WPS */ |
e3768e7c JM |
259 | #ifdef CONFIG_P2P |
260 | config->p2p_ssid_postfix = wpa_config_read_reg_string( | |
261 | hk, TEXT("p2p_ssid_postfix")); | |
3071e181 JM |
262 | wpa_config_read_reg_dword(hk, TEXT("p2p_group_idle"), |
263 | (int *) &config->p2p_group_idle); | |
e3768e7c | 264 | #endif /* CONFIG_P2P */ |
f855f923 | 265 | |
c9c38b09 | 266 | wpa_config_read_reg_dword(hk, TEXT("bss_max_count"), |
de1267d4 | 267 | (int *) &config->bss_max_count); |
3812464c JM |
268 | wpa_config_read_reg_dword(hk, TEXT("filter_ssids"), |
269 | &config->filter_ssids); | |
dae608d5 JM |
270 | wpa_config_read_reg_dword(hk, TEXT("max_num_sta"), |
271 | (int *) &config->max_num_sta); | |
c9c38b09 | 272 | |
6fc6879b JM |
273 | return errors ? -1 : 0; |
274 | } | |
275 | ||
276 | ||
277 | static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw, | |
278 | int id) | |
279 | { | |
280 | HKEY nhk; | |
281 | LONG ret; | |
282 | DWORD i; | |
283 | struct wpa_ssid *ssid; | |
284 | int errors = 0; | |
285 | ||
286 | ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk); | |
287 | if (ret != ERROR_SUCCESS) { | |
288 | wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config " | |
289 | "network '" TSTR "'", netw); | |
290 | return NULL; | |
291 | } | |
292 | ||
293 | wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw); | |
294 | ssid = os_zalloc(sizeof(*ssid)); | |
295 | if (ssid == NULL) { | |
296 | RegCloseKey(nhk); | |
297 | return NULL; | |
298 | } | |
299 | ssid->id = id; | |
300 | ||
301 | wpa_config_set_network_defaults(ssid); | |
302 | ||
303 | for (i = 0; ; i++) { | |
304 | TCHAR name[255], data[1024]; | |
305 | DWORD namelen, datalen, type; | |
306 | ||
307 | namelen = 255; | |
308 | datalen = sizeof(data); | |
309 | ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type, | |
310 | (LPBYTE) data, &datalen); | |
311 | ||
312 | if (ret == ERROR_NO_MORE_ITEMS) | |
313 | break; | |
314 | ||
315 | if (ret != ERROR_SUCCESS) { | |
316 | wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x", | |
317 | (unsigned int) ret); | |
318 | break; | |
319 | } | |
320 | ||
321 | if (namelen >= 255) | |
322 | namelen = 255 - 1; | |
323 | name[namelen] = TEXT('\0'); | |
324 | ||
325 | if (datalen >= 1024) | |
326 | datalen = 1024 - 1; | |
327 | data[datalen] = TEXT('\0'); | |
328 | ||
329 | wpa_unicode2ascii_inplace(name); | |
330 | wpa_unicode2ascii_inplace(data); | |
331 | if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0) | |
332 | errors++; | |
333 | } | |
334 | ||
335 | RegCloseKey(nhk); | |
336 | ||
337 | if (ssid->passphrase) { | |
338 | if (ssid->psk_set) { | |
339 | wpa_printf(MSG_ERROR, "Both PSK and passphrase " | |
340 | "configured for network '" TSTR "'.", netw); | |
341 | errors++; | |
342 | } | |
343 | wpa_config_update_psk(ssid); | |
344 | } | |
345 | ||
56586197 JM |
346 | if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | |
347 | WPA_KEY_MGMT_PSK_SHA256)) && | |
6fc6879b JM |
348 | !ssid->psk_set) { |
349 | wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, " | |
350 | "but no PSK configured for network '" TSTR "'.", | |
351 | netw); | |
352 | errors++; | |
353 | } | |
354 | ||
355 | if ((ssid->group_cipher & WPA_CIPHER_CCMP) && | |
356 | !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) && | |
357 | !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) { | |
358 | /* Group cipher cannot be stronger than the pairwise cipher. */ | |
359 | wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher " | |
360 | "list since it was not allowed for pairwise " | |
361 | "cipher for network '" TSTR "'.", netw); | |
362 | ssid->group_cipher &= ~WPA_CIPHER_CCMP; | |
363 | } | |
364 | ||
365 | if (errors) { | |
366 | wpa_config_free_ssid(ssid); | |
367 | ssid = NULL; | |
368 | } | |
369 | ||
370 | return ssid; | |
371 | } | |
372 | ||
373 | ||
374 | static int wpa_config_read_networks(struct wpa_config *config, HKEY hk) | |
375 | { | |
376 | HKEY nhk; | |
377 | struct wpa_ssid *ssid, *tail = NULL, *head = NULL; | |
378 | int errors = 0; | |
379 | LONG ret; | |
380 | DWORD i; | |
381 | ||
382 | ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS, | |
383 | &nhk); | |
384 | if (ret != ERROR_SUCCESS) { | |
385 | wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks " | |
386 | "registry key"); | |
387 | return -1; | |
388 | } | |
389 | ||
390 | for (i = 0; ; i++) { | |
391 | TCHAR name[255]; | |
392 | DWORD namelen; | |
393 | ||
394 | namelen = 255; | |
395 | ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL, | |
396 | NULL); | |
397 | ||
398 | if (ret == ERROR_NO_MORE_ITEMS) | |
399 | break; | |
400 | ||
401 | if (ret != ERROR_SUCCESS) { | |
402 | wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x", | |
403 | (unsigned int) ret); | |
404 | break; | |
405 | } | |
406 | ||
407 | if (namelen >= 255) | |
408 | namelen = 255 - 1; | |
409 | name[namelen] = '\0'; | |
410 | ||
411 | ssid = wpa_config_read_network(nhk, name, i); | |
412 | if (ssid == NULL) { | |
413 | wpa_printf(MSG_ERROR, "Failed to parse network " | |
414 | "profile '%s'.", name); | |
415 | errors++; | |
416 | continue; | |
417 | } | |
418 | if (head == NULL) { | |
419 | head = tail = ssid; | |
420 | } else { | |
421 | tail->next = ssid; | |
422 | tail = ssid; | |
423 | } | |
424 | if (wpa_config_add_prio_network(config, ssid)) { | |
425 | wpa_printf(MSG_ERROR, "Failed to add network profile " | |
426 | "'%s' to priority list.", name); | |
427 | errors++; | |
428 | continue; | |
429 | } | |
430 | } | |
431 | ||
432 | RegCloseKey(nhk); | |
433 | ||
434 | config->ssid = head; | |
435 | ||
436 | return errors ? -1 : 0; | |
437 | } | |
438 | ||
439 | ||
440 | struct wpa_config * wpa_config_read(const char *name) | |
441 | { | |
442 | TCHAR buf[256]; | |
443 | int errors = 0; | |
444 | struct wpa_config *config; | |
445 | HKEY hk; | |
446 | LONG ret; | |
447 | ||
448 | config = wpa_config_alloc_empty(NULL, NULL); | |
449 | if (config == NULL) | |
450 | return NULL; | |
451 | wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name); | |
452 | ||
453 | #ifdef UNICODE | |
454 | _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name); | |
455 | #else /* UNICODE */ | |
456 | os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name); | |
457 | #endif /* UNICODE */ | |
458 | ||
459 | ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk); | |
460 | if (ret != ERROR_SUCCESS) { | |
461 | wpa_printf(MSG_ERROR, "Could not open wpa_supplicant " | |
462 | "configuration registry HKLM\\" TSTR, buf); | |
463 | os_free(config); | |
464 | return NULL; | |
465 | } | |
466 | ||
467 | if (wpa_config_read_global(config, hk)) | |
468 | errors++; | |
469 | ||
470 | if (wpa_config_read_networks(config, hk)) | |
471 | errors++; | |
472 | ||
473 | if (wpa_config_read_blobs(config, hk)) | |
474 | errors++; | |
475 | ||
476 | wpa_config_debug_dump_networks(config); | |
477 | ||
478 | RegCloseKey(hk); | |
479 | ||
480 | if (errors) { | |
481 | wpa_config_free(config); | |
482 | config = NULL; | |
483 | } | |
484 | ||
485 | return config; | |
486 | } | |
487 | ||
488 | ||
489 | static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val, | |
490 | int def) | |
491 | { | |
492 | LONG ret; | |
493 | DWORD _val = val; | |
494 | ||
495 | if (val == def) { | |
496 | RegDeleteValue(hk, name); | |
497 | return 0; | |
498 | } | |
499 | ||
500 | ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val, | |
501 | sizeof(_val)); | |
502 | if (ret != ERROR_SUCCESS) { | |
503 | wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d", | |
504 | name, val, (int) GetLastError()); | |
505 | return -1; | |
506 | } | |
507 | ||
508 | return 0; | |
509 | } | |
510 | ||
511 | ||
512 | static int wpa_config_write_reg_string(HKEY hk, const char *name, | |
513 | const char *val) | |
514 | { | |
515 | LONG ret; | |
516 | TCHAR *_name, *_val; | |
517 | ||
518 | _name = wpa_strdup_tchar(name); | |
519 | if (_name == NULL) | |
520 | return -1; | |
521 | ||
522 | if (val == NULL) { | |
523 | RegDeleteValue(hk, _name); | |
524 | os_free(_name); | |
525 | return 0; | |
526 | } | |
527 | ||
528 | _val = wpa_strdup_tchar(val); | |
529 | if (_val == NULL) { | |
530 | os_free(_name); | |
531 | return -1; | |
532 | } | |
533 | ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val, | |
534 | (os_strlen(val) + 1) * sizeof(TCHAR)); | |
535 | if (ret != ERROR_SUCCESS) { | |
536 | wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': " | |
537 | "error %d", name, val, (int) GetLastError()); | |
538 | os_free(_name); | |
539 | os_free(_val); | |
540 | return -1; | |
541 | } | |
542 | ||
543 | os_free(_name); | |
544 | os_free(_val); | |
545 | return 0; | |
546 | } | |
547 | ||
548 | ||
549 | static int wpa_config_write_global(struct wpa_config *config, HKEY hk) | |
550 | { | |
551 | #ifdef CONFIG_CTRL_IFACE | |
552 | wpa_config_write_reg_string(hk, "ctrl_interface", | |
553 | config->ctrl_interface); | |
554 | #endif /* CONFIG_CTRL_IFACE */ | |
555 | ||
556 | wpa_config_write_reg_dword(hk, TEXT("eapol_version"), | |
557 | config->eapol_version, | |
558 | DEFAULT_EAPOL_VERSION); | |
559 | wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan, | |
560 | DEFAULT_AP_SCAN); | |
561 | wpa_config_write_reg_dword(hk, TEXT("fast_reauth"), | |
562 | config->fast_reauth, DEFAULT_FAST_REAUTH); | |
563 | wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"), | |
564 | config->dot11RSNAConfigPMKLifetime, 0); | |
565 | wpa_config_write_reg_dword(hk, | |
566 | TEXT("dot11RSNAConfigPMKReauthThreshold"), | |
567 | config->dot11RSNAConfigPMKReauthThreshold, | |
568 | 0); | |
569 | wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"), | |
570 | config->dot11RSNAConfigSATimeout, 0); | |
571 | wpa_config_write_reg_dword(hk, TEXT("update_config"), | |
572 | config->update_config, | |
573 | 0); | |
f855f923 | 574 | #ifdef CONFIG_WPS |
9be09636 | 575 | if (!is_nil_uuid(config->uuid)) { |
f855f923 JM |
576 | char buf[40]; |
577 | uuid_bin2str(config->uuid, buf, sizeof(buf)); | |
578 | wpa_config_write_reg_string(hk, "uuid", buf); | |
579 | } | |
3c0b7aa4 JM |
580 | wpa_config_write_reg_string(hk, "device_name", config->device_name); |
581 | wpa_config_write_reg_string(hk, "manufacturer", config->manufacturer); | |
582 | wpa_config_write_reg_string(hk, "model_name", config->model_name); | |
583 | wpa_config_write_reg_string(hk, "model_number", config->model_number); | |
584 | wpa_config_write_reg_string(hk, "serial_number", | |
585 | config->serial_number); | |
586 | wpa_config_write_reg_string(hk, "device_type", config->device_type); | |
c0e4dd9e JM |
587 | wpa_config_write_reg_string(hk, "config_methods", |
588 | config->config_methods); | |
3c0b7aa4 JM |
589 | if (WPA_GET_BE32(config->os_version)) { |
590 | char vbuf[10]; | |
591 | os_snprintf(vbuf, sizeof(vbuf), "%08x", | |
592 | WPA_GET_BE32(config->os_version)); | |
593 | wpa_config_write_reg_string(hk, "os_version", vbuf); | |
594 | } | |
47662164 JM |
595 | wpa_config_write_reg_dword(hk, TEXT("wps_cred_processing"), |
596 | config->wps_cred_processing, 0); | |
f855f923 | 597 | #endif /* CONFIG_WPS */ |
e3768e7c JM |
598 | #ifdef CONFIG_P2P |
599 | wpa_config_write_reg_string(hk, "p2p_ssid_postfix", | |
600 | config->p2p_ssid_postfix); | |
3071e181 JM |
601 | wpa_config_write_reg_dword(hk, TEXT("p2p_group_idle"), |
602 | config->p2p_group_idle, 0); | |
e3768e7c | 603 | #endif /* CONFIG_P2P */ |
6fc6879b | 604 | |
c9c38b09 JM |
605 | wpa_config_write_reg_dword(hk, TEXT("bss_max_count"), |
606 | config->bss_max_count, | |
607 | DEFAULT_BSS_MAX_COUNT); | |
3812464c JM |
608 | wpa_config_write_reg_dword(hk, TEXT("filter_ssids"), |
609 | config->filter_ssids, 0); | |
dae608d5 JM |
610 | wpa_config_write_reg_dword(hk, TEXT("max_num_sta"), |
611 | config->max_num_sta, DEFAULT_MAX_NUM_STA); | |
c9c38b09 | 612 | |
6fc6879b JM |
613 | return 0; |
614 | } | |
615 | ||
616 | ||
617 | static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key) | |
618 | { | |
619 | HKEY nhk; | |
620 | int i, errors = 0; | |
621 | LONG ret; | |
622 | ||
623 | ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk); | |
624 | if (ret != ERROR_SUCCESS) { | |
625 | wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR | |
626 | "' for subkey deletion: error 0x%x (%d)", key, | |
627 | (unsigned int) ret, (int) GetLastError()); | |
628 | return 0; | |
629 | } | |
630 | ||
631 | for (i = 0; ; i++) { | |
632 | TCHAR name[255]; | |
633 | DWORD namelen; | |
634 | ||
635 | namelen = 255; | |
636 | ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL, | |
637 | NULL); | |
638 | ||
639 | if (ret == ERROR_NO_MORE_ITEMS) | |
640 | break; | |
641 | ||
642 | if (ret != ERROR_SUCCESS) { | |
643 | wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)", | |
644 | (unsigned int) ret, (int) GetLastError()); | |
645 | break; | |
646 | } | |
647 | ||
648 | if (namelen >= 255) | |
649 | namelen = 255 - 1; | |
650 | name[namelen] = TEXT('\0'); | |
651 | ||
652 | ret = RegDeleteKey(nhk, name); | |
653 | if (ret != ERROR_SUCCESS) { | |
654 | wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)", | |
655 | (unsigned int) ret, (int) GetLastError()); | |
656 | errors++; | |
657 | } | |
658 | } | |
659 | ||
660 | RegCloseKey(nhk); | |
661 | ||
662 | return errors ? -1 : 0; | |
663 | } | |
664 | ||
665 | ||
666 | static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid) | |
667 | { | |
668 | char *value = wpa_config_get(ssid, field); | |
669 | if (value == NULL) | |
670 | return; | |
671 | wpa_config_write_reg_string(hk, field, value); | |
672 | os_free(value); | |
673 | } | |
674 | ||
675 | ||
676 | static void write_int(HKEY hk, const char *field, int value, int def) | |
677 | { | |
678 | char val[20]; | |
679 | if (value == def) | |
680 | return; | |
681 | os_snprintf(val, sizeof(val), "%d", value); | |
682 | wpa_config_write_reg_string(hk, field, val); | |
683 | } | |
684 | ||
685 | ||
686 | static void write_bssid(HKEY hk, struct wpa_ssid *ssid) | |
687 | { | |
688 | char *value = wpa_config_get(ssid, "bssid"); | |
689 | if (value == NULL) | |
690 | return; | |
691 | wpa_config_write_reg_string(hk, "bssid", value); | |
692 | os_free(value); | |
693 | } | |
694 | ||
695 | ||
696 | static void write_psk(HKEY hk, struct wpa_ssid *ssid) | |
697 | { | |
698 | char *value = wpa_config_get(ssid, "psk"); | |
699 | if (value == NULL) | |
700 | return; | |
701 | wpa_config_write_reg_string(hk, "psk", value); | |
702 | os_free(value); | |
703 | } | |
704 | ||
705 | ||
706 | static void write_proto(HKEY hk, struct wpa_ssid *ssid) | |
707 | { | |
708 | char *value; | |
709 | ||
710 | if (ssid->proto == DEFAULT_PROTO) | |
711 | return; | |
712 | ||
713 | value = wpa_config_get(ssid, "proto"); | |
714 | if (value == NULL) | |
715 | return; | |
716 | if (value[0]) | |
717 | wpa_config_write_reg_string(hk, "proto", value); | |
718 | os_free(value); | |
719 | } | |
720 | ||
721 | ||
722 | static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid) | |
723 | { | |
724 | char *value; | |
725 | ||
726 | if (ssid->key_mgmt == DEFAULT_KEY_MGMT) | |
727 | return; | |
728 | ||
729 | value = wpa_config_get(ssid, "key_mgmt"); | |
730 | if (value == NULL) | |
731 | return; | |
732 | if (value[0]) | |
733 | wpa_config_write_reg_string(hk, "key_mgmt", value); | |
734 | os_free(value); | |
735 | } | |
736 | ||
737 | ||
738 | static void write_pairwise(HKEY hk, struct wpa_ssid *ssid) | |
739 | { | |
740 | char *value; | |
741 | ||
742 | if (ssid->pairwise_cipher == DEFAULT_PAIRWISE) | |
743 | return; | |
744 | ||
745 | value = wpa_config_get(ssid, "pairwise"); | |
746 | if (value == NULL) | |
747 | return; | |
748 | if (value[0]) | |
749 | wpa_config_write_reg_string(hk, "pairwise", value); | |
750 | os_free(value); | |
751 | } | |
752 | ||
753 | ||
754 | static void write_group(HKEY hk, struct wpa_ssid *ssid) | |
755 | { | |
756 | char *value; | |
757 | ||
758 | if (ssid->group_cipher == DEFAULT_GROUP) | |
759 | return; | |
760 | ||
761 | value = wpa_config_get(ssid, "group"); | |
762 | if (value == NULL) | |
763 | return; | |
764 | if (value[0]) | |
765 | wpa_config_write_reg_string(hk, "group", value); | |
766 | os_free(value); | |
767 | } | |
768 | ||
769 | ||
770 | static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid) | |
771 | { | |
772 | char *value; | |
773 | ||
774 | if (ssid->auth_alg == 0) | |
775 | return; | |
776 | ||
777 | value = wpa_config_get(ssid, "auth_alg"); | |
778 | if (value == NULL) | |
779 | return; | |
780 | if (value[0]) | |
781 | wpa_config_write_reg_string(hk, "auth_alg", value); | |
782 | os_free(value); | |
783 | } | |
784 | ||
785 | ||
786 | #ifdef IEEE8021X_EAPOL | |
787 | static void write_eap(HKEY hk, struct wpa_ssid *ssid) | |
788 | { | |
789 | char *value; | |
790 | ||
791 | value = wpa_config_get(ssid, "eap"); | |
792 | if (value == NULL) | |
793 | return; | |
794 | ||
795 | if (value[0]) | |
796 | wpa_config_write_reg_string(hk, "eap", value); | |
797 | os_free(value); | |
798 | } | |
799 | #endif /* IEEE8021X_EAPOL */ | |
800 | ||
801 | ||
802 | static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid) | |
803 | { | |
804 | char field[20], *value; | |
805 | ||
806 | os_snprintf(field, sizeof(field), "wep_key%d", idx); | |
807 | value = wpa_config_get(ssid, field); | |
808 | if (value) { | |
809 | wpa_config_write_reg_string(hk, field, value); | |
810 | os_free(value); | |
811 | } | |
812 | } | |
813 | ||
814 | ||
815 | static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id) | |
816 | { | |
817 | int i, errors = 0; | |
818 | HKEY nhk, netw; | |
819 | LONG ret; | |
820 | TCHAR name[5]; | |
821 | ||
822 | ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk); | |
823 | if (ret != ERROR_SUCCESS) { | |
824 | wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key " | |
825 | "for subkey addition: error 0x%x (%d)", | |
826 | (unsigned int) ret, (int) GetLastError()); | |
827 | return 0; | |
828 | } | |
829 | ||
830 | #ifdef UNICODE | |
831 | wsprintf(name, L"%04d", id); | |
832 | #else /* UNICODE */ | |
833 | os_snprintf(name, sizeof(name), "%04d", id); | |
834 | #endif /* UNICODE */ | |
835 | ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw, | |
836 | NULL); | |
837 | RegCloseKey(nhk); | |
838 | if (ret != ERROR_SUCCESS) { | |
839 | wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':" | |
840 | " error 0x%x (%d)", | |
841 | name, (unsigned int) ret, (int) GetLastError()); | |
842 | return -1; | |
843 | } | |
844 | ||
845 | #define STR(t) write_str(netw, #t, ssid) | |
846 | #define INT(t) write_int(netw, #t, ssid->t, 0) | |
847 | #define INTe(t) write_int(netw, #t, ssid->eap.t, 0) | |
848 | #define INT_DEF(t, def) write_int(netw, #t, ssid->t, def) | |
849 | #define INT_DEFe(t, def) write_int(netw, #t, ssid->eap.t, def) | |
850 | ||
851 | STR(ssid); | |
852 | INT(scan_ssid); | |
853 | write_bssid(netw, ssid); | |
854 | write_psk(netw, ssid); | |
855 | write_proto(netw, ssid); | |
856 | write_key_mgmt(netw, ssid); | |
857 | write_pairwise(netw, ssid); | |
858 | write_group(netw, ssid); | |
859 | write_auth_alg(netw, ssid); | |
860 | #ifdef IEEE8021X_EAPOL | |
861 | write_eap(netw, ssid); | |
862 | STR(identity); | |
863 | STR(anonymous_identity); | |
864 | STR(password); | |
865 | STR(ca_cert); | |
866 | STR(ca_path); | |
867 | STR(client_cert); | |
868 | STR(private_key); | |
869 | STR(private_key_passwd); | |
870 | STR(dh_file); | |
871 | STR(subject_match); | |
872 | STR(altsubject_match); | |
873 | STR(ca_cert2); | |
874 | STR(ca_path2); | |
875 | STR(client_cert2); | |
876 | STR(private_key2); | |
877 | STR(private_key2_passwd); | |
878 | STR(dh_file2); | |
879 | STR(subject_match2); | |
880 | STR(altsubject_match2); | |
881 | STR(phase1); | |
882 | STR(phase2); | |
883 | STR(pcsc); | |
884 | STR(pin); | |
885 | STR(engine_id); | |
886 | STR(key_id); | |
61ee0f71 DS |
887 | STR(cert_id); |
888 | STR(ca_cert_id); | |
889 | STR(key2_id); | |
98842d51 CL |
890 | STR(pin2); |
891 | STR(engine2_id); | |
61ee0f71 DS |
892 | STR(cert2_id); |
893 | STR(ca_cert2_id); | |
6fc6879b | 894 | INTe(engine); |
98842d51 | 895 | INTe(engine2); |
6fc6879b JM |
896 | INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS); |
897 | #endif /* IEEE8021X_EAPOL */ | |
898 | for (i = 0; i < 4; i++) | |
899 | write_wep_key(netw, i, ssid); | |
900 | INT(wep_tx_keyidx); | |
901 | INT(priority); | |
902 | #ifdef IEEE8021X_EAPOL | |
903 | INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND); | |
904 | STR(pac_file); | |
905 | INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE); | |
906 | #endif /* IEEE8021X_EAPOL */ | |
907 | INT(mode); | |
908 | INT(proactive_key_caching); | |
909 | INT(disabled); | |
910 | INT(peerkey); | |
911 | #ifdef CONFIG_IEEE80211W | |
912 | INT(ieee80211w); | |
913 | #endif /* CONFIG_IEEE80211W */ | |
914 | STR(id_str); | |
915 | ||
916 | #undef STR | |
917 | #undef INT | |
918 | #undef INT_DEF | |
919 | ||
920 | RegCloseKey(netw); | |
921 | ||
922 | return errors ? -1 : 0; | |
923 | } | |
924 | ||
925 | ||
926 | static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob) | |
927 | { | |
928 | HKEY bhk; | |
929 | LONG ret; | |
930 | TCHAR *name; | |
931 | ||
932 | ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL, | |
933 | &bhk, NULL); | |
934 | if (ret != ERROR_SUCCESS) { | |
935 | wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: " | |
936 | "error 0x%x (%d)", | |
937 | (unsigned int) ret, (int) GetLastError()); | |
938 | return -1; | |
939 | } | |
940 | ||
941 | name = wpa_strdup_tchar(blob->name); | |
942 | ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data, | |
943 | blob->len); | |
944 | if (ret != ERROR_SUCCESS) { | |
945 | wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': " | |
946 | "error 0x%x (%d)", blob->name, (unsigned int) ret, | |
947 | (int) GetLastError()); | |
948 | RegCloseKey(bhk); | |
949 | os_free(name); | |
950 | return -1; | |
951 | } | |
952 | os_free(name); | |
953 | ||
954 | RegCloseKey(bhk); | |
955 | ||
956 | return 0; | |
957 | } | |
958 | ||
959 | ||
960 | int wpa_config_write(const char *name, struct wpa_config *config) | |
961 | { | |
962 | TCHAR buf[256]; | |
963 | HKEY hk; | |
964 | LONG ret; | |
965 | int errors = 0; | |
966 | struct wpa_ssid *ssid; | |
967 | struct wpa_config_blob *blob; | |
968 | int id; | |
969 | ||
970 | wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name); | |
971 | ||
972 | #ifdef UNICODE | |
973 | _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name); | |
974 | #else /* UNICODE */ | |
975 | os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name); | |
976 | #endif /* UNICODE */ | |
977 | ||
978 | ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk); | |
979 | if (ret != ERROR_SUCCESS) { | |
980 | wpa_printf(MSG_ERROR, "Could not open wpa_supplicant " | |
981 | "configuration registry %s: error %d", buf, | |
982 | (int) GetLastError()); | |
983 | return -1; | |
984 | } | |
985 | ||
986 | if (wpa_config_write_global(config, hk)) { | |
987 | wpa_printf(MSG_ERROR, "Failed to write global configuration " | |
988 | "data"); | |
989 | errors++; | |
990 | } | |
991 | ||
992 | wpa_config_delete_subkeys(hk, TEXT("networks")); | |
993 | for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) { | |
ad08c363 JM |
994 | if (ssid->key_mgmt == WPA_KEY_MGMT_WPS) |
995 | continue; /* do not save temporary WPS networks */ | |
6fc6879b JM |
996 | if (wpa_config_write_network(hk, ssid, id)) |
997 | errors++; | |
998 | } | |
999 | ||
1000 | RegDeleteKey(hk, TEXT("blobs")); | |
1001 | for (blob = config->blobs; blob; blob = blob->next) { | |
1002 | if (wpa_config_write_blob(hk, blob)) | |
1003 | errors++; | |
1004 | } | |
1005 | ||
1006 | RegCloseKey(hk); | |
1007 | ||
1008 | wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully", | |
1009 | name, errors ? "un" : ""); | |
1010 | return errors ? -1 : 0; | |
1011 | } |