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