1 /* 2 * WPA Supplicant / Configuration backend: Windows registry 3 * Copyright (c) 2003-2008, Jouni Malinen <j (at) w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 * 8 * This file implements a configuration backend for Windows registry. All the 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 * 13 * Configuration data is in 14 * \a HKEY_LOCAL_MACHINE\\SOFTWARE\\%wpa_supplicant\\configs 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 * 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 28 */ 29 30 #include "includes.h" 31 32 #include "common.h" 33 #include "uuid.h" 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 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 } 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 } 199 #endif /* CONFIG_WPS */ 200 201 202 static int wpa_config_read_global(struct wpa_config *config, HKEY hk) 203 { 204 int errors = 0; 205 int val; 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"), 211 (int *) &config->dot11RSNAConfigPMKLifetime); 212 wpa_config_read_reg_dword(hk, 213 TEXT("dot11RSNAConfigPMKReauthThreshold"), 214 (int *) 215 &config->dot11RSNAConfigPMKReauthThreshold); 216 wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"), 217 (int *) &config->dot11RSNAConfigSATimeout); 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 234 #ifdef CONFIG_WPS 235 if (wpa_config_read_global_uuid(config, hk)) 236 errors++; 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")); 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 } 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 */ 259 #ifdef CONFIG_P2P 260 config->p2p_ssid_postfix = wpa_config_read_reg_string( 261 hk, TEXT("p2p_ssid_postfix")); 262 wpa_config_read_reg_dword(hk, TEXT("p2p_group_idle"), 263 (int *) &config->p2p_group_idle); 264 #endif /* CONFIG_P2P */ 265 266 wpa_config_read_reg_dword(hk, TEXT("bss_max_count"), 267 (int *) &config->bss_max_count); 268 wpa_config_read_reg_dword(hk, TEXT("filter_ssids"), 269 &config->filter_ssids); 270 wpa_config_read_reg_dword(hk, TEXT("max_num_sta"), 271 (int *) &config->max_num_sta); 272 wpa_config_read_reg_dword(hk, TEXT("disassoc_low_ack"), 273 (int *) &config->disassoc_low_ack); 274 275 wpa_config_read_reg_dword(hk, TEXT("okc"), &config->okc); 276 wpa_config_read_reg_dword(hk, TEXT("pmf"), &val); 277 config->pmf = val; 278 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 dl_list_init(&ssid->psk_list); 306 ssid->id = id; 307 308 wpa_config_set_network_defaults(ssid); 309 310 for (i = 0; ; i++) { 311 TCHAR name[255], data[1024]; 312 DWORD namelen, datalen, type; 313 314 namelen = 255; 315 datalen = sizeof(data); 316 ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type, 317 (LPBYTE) data, &datalen); 318 319 if (ret == ERROR_NO_MORE_ITEMS) 320 break; 321 322 if (ret != ERROR_SUCCESS) { 323 wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x", 324 (unsigned int) ret); 325 break; 326 } 327 328 if (namelen >= 255) 329 namelen = 255 - 1; 330 name[namelen] = TEXT('\0'); 331 332 if (datalen >= 1024) 333 datalen = 1024 - 1; 334 data[datalen] = TEXT('\0'); 335 336 wpa_unicode2ascii_inplace(name); 337 wpa_unicode2ascii_inplace(data); 338 if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0) 339 errors++; 340 } 341 342 RegCloseKey(nhk); 343 344 if (ssid->passphrase) { 345 if (ssid->psk_set) { 346 wpa_printf(MSG_ERROR, "Both PSK and passphrase " 347 "configured for network '" TSTR "'.", netw); 348 errors++; 349 } 350 wpa_config_update_psk(ssid); 351 } 352 353 if ((ssid->group_cipher & WPA_CIPHER_CCMP) && 354 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) && 355 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) { 356 /* Group cipher cannot be stronger than the pairwise cipher. */ 357 wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher " 358 "list since it was not allowed for pairwise " 359 "cipher for network '" TSTR "'.", netw); 360 ssid->group_cipher &= ~WPA_CIPHER_CCMP; 361 } 362 363 if (errors) { 364 wpa_config_free_ssid(ssid); 365 ssid = NULL; 366 } 367 368 return ssid; 369 } 370 371 372 static int wpa_config_read_networks(struct wpa_config *config, HKEY hk) 373 { 374 HKEY nhk; 375 struct wpa_ssid *ssid, *tail = NULL, *head = NULL; 376 int errors = 0; 377 LONG ret; 378 DWORD i; 379 380 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS, 381 &nhk); 382 if (ret != ERROR_SUCCESS) { 383 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks " 384 "registry key"); 385 return -1; 386 } 387 388 for (i = 0; ; i++) { 389 TCHAR name[255]; 390 DWORD namelen; 391 392 namelen = 255; 393 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL, 394 NULL); 395 396 if (ret == ERROR_NO_MORE_ITEMS) 397 break; 398 399 if (ret != ERROR_SUCCESS) { 400 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x", 401 (unsigned int) ret); 402 break; 403 } 404 405 if (namelen >= 255) 406 namelen = 255 - 1; 407 name[namelen] = '\0'; 408 409 ssid = wpa_config_read_network(nhk, name, i); 410 if (ssid == NULL) { 411 wpa_printf(MSG_ERROR, "Failed to parse network " 412 "profile '%s'.", name); 413 errors++; 414 continue; 415 } 416 if (head == NULL) { 417 head = tail = ssid; 418 } else { 419 tail->next = ssid; 420 tail = ssid; 421 } 422 if (wpa_config_add_prio_network(config, ssid)) { 423 wpa_printf(MSG_ERROR, "Failed to add network profile " 424 "'%s' to priority list.", name); 425 errors++; 426 continue; 427 } 428 } 429 430 RegCloseKey(nhk); 431 432 config->ssid = head; 433 434 return errors ? -1 : 0; 435 } 436 437 438 struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp) 439 { 440 TCHAR buf[256]; 441 int errors = 0; 442 struct wpa_config *config; 443 HKEY hk; 444 LONG ret; 445 446 if (name == NULL) 447 return NULL; 448 if (cfgp) 449 config = cfgp; 450 else 451 config = wpa_config_alloc_empty(NULL, NULL); 452 if (config == NULL) 453 return NULL; 454 wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name); 455 456 #ifdef UNICODE 457 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name); 458 #else /* UNICODE */ 459 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name); 460 #endif /* UNICODE */ 461 462 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk); 463 if (ret != ERROR_SUCCESS) { 464 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant " 465 "configuration registry HKLM\\" TSTR, buf); 466 os_free(config); 467 return NULL; 468 } 469 470 if (wpa_config_read_global(config, hk)) 471 errors++; 472 473 if (wpa_config_read_networks(config, hk)) 474 errors++; 475 476 if (wpa_config_read_blobs(config, hk)) 477 errors++; 478 479 wpa_config_debug_dump_networks(config); 480 481 RegCloseKey(hk); 482 483 if (errors) { 484 wpa_config_free(config); 485 config = NULL; 486 } 487 488 return config; 489 } 490 491 492 static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val, 493 int def) 494 { 495 LONG ret; 496 DWORD _val = val; 497 498 if (val == def) { 499 RegDeleteValue(hk, name); 500 return 0; 501 } 502 503 ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val, 504 sizeof(_val)); 505 if (ret != ERROR_SUCCESS) { 506 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d", 507 name, val, (int) GetLastError()); 508 return -1; 509 } 510 511 return 0; 512 } 513 514 515 static int wpa_config_write_reg_string(HKEY hk, const char *name, 516 const char *val) 517 { 518 LONG ret; 519 TCHAR *_name, *_val; 520 521 _name = wpa_strdup_tchar(name); 522 if (_name == NULL) 523 return -1; 524 525 if (val == NULL) { 526 RegDeleteValue(hk, _name); 527 os_free(_name); 528 return 0; 529 } 530 531 _val = wpa_strdup_tchar(val); 532 if (_val == NULL) { 533 os_free(_name); 534 return -1; 535 } 536 ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val, 537 (os_strlen(val) + 1) * sizeof(TCHAR)); 538 if (ret != ERROR_SUCCESS) { 539 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': " 540 "error %d", name, val, (int) GetLastError()); 541 os_free(_name); 542 os_free(_val); 543 return -1; 544 } 545 546 os_free(_name); 547 os_free(_val); 548 return 0; 549 } 550 551 552 static int wpa_config_write_global(struct wpa_config *config, HKEY hk) 553 { 554 #ifdef CONFIG_CTRL_IFACE 555 wpa_config_write_reg_string(hk, "ctrl_interface", 556 config->ctrl_interface); 557 #endif /* CONFIG_CTRL_IFACE */ 558 559 wpa_config_write_reg_dword(hk, TEXT("eapol_version"), 560 config->eapol_version, 561 DEFAULT_EAPOL_VERSION); 562 wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan, 563 DEFAULT_AP_SCAN); 564 wpa_config_write_reg_dword(hk, TEXT("fast_reauth"), 565 config->fast_reauth, DEFAULT_FAST_REAUTH); 566 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"), 567 config->dot11RSNAConfigPMKLifetime, 0); 568 wpa_config_write_reg_dword(hk, 569 TEXT("dot11RSNAConfigPMKReauthThreshold"), 570 config->dot11RSNAConfigPMKReauthThreshold, 571 0); 572 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"), 573 config->dot11RSNAConfigSATimeout, 0); 574 wpa_config_write_reg_dword(hk, TEXT("update_config"), 575 config->update_config, 576 0); 577 #ifdef CONFIG_WPS 578 if (!is_nil_uuid(config->uuid)) { 579 char buf[40]; 580 uuid_bin2str(config->uuid, buf, sizeof(buf)); 581 wpa_config_write_reg_string(hk, "uuid", buf); 582 } 583 wpa_config_write_reg_string(hk, "device_name", config->device_name); 584 wpa_config_write_reg_string(hk, "manufacturer", config->manufacturer); 585 wpa_config_write_reg_string(hk, "model_name", config->model_name); 586 wpa_config_write_reg_string(hk, "model_number", config->model_number); 587 wpa_config_write_reg_string(hk, "serial_number", 588 config->serial_number); 589 { 590 char _buf[WPS_DEV_TYPE_BUFSIZE], *buf; 591 buf = wps_dev_type_bin2str(config->device_type, 592 _buf, sizeof(_buf)); 593 wpa_config_write_reg_string(hk, "device_type", buf); 594 } 595 wpa_config_write_reg_string(hk, "config_methods", 596 config->config_methods); 597 if (WPA_GET_BE32(config->os_version)) { 598 char vbuf[10]; 599 os_snprintf(vbuf, sizeof(vbuf), "%08x", 600 WPA_GET_BE32(config->os_version)); 601 wpa_config_write_reg_string(hk, "os_version", vbuf); 602 } 603 wpa_config_write_reg_dword(hk, TEXT("wps_cred_processing"), 604 config->wps_cred_processing, 0); 605 #endif /* CONFIG_WPS */ 606 #ifdef CONFIG_P2P 607 wpa_config_write_reg_string(hk, "p2p_ssid_postfix", 608 config->p2p_ssid_postfix); 609 wpa_config_write_reg_dword(hk, TEXT("p2p_group_idle"), 610 config->p2p_group_idle, 0); 611 #endif /* CONFIG_P2P */ 612 613 wpa_config_write_reg_dword(hk, TEXT("bss_max_count"), 614 config->bss_max_count, 615 DEFAULT_BSS_MAX_COUNT); 616 wpa_config_write_reg_dword(hk, TEXT("filter_ssids"), 617 config->filter_ssids, 0); 618 wpa_config_write_reg_dword(hk, TEXT("max_num_sta"), 619 config->max_num_sta, DEFAULT_MAX_NUM_STA); 620 wpa_config_write_reg_dword(hk, TEXT("disassoc_low_ack"), 621 config->disassoc_low_ack, 0); 622 623 wpa_config_write_reg_dword(hk, TEXT("okc"), config->okc, 0); 624 wpa_config_write_reg_dword(hk, TEXT("pmf"), config->pmf, 0); 625 626 return 0; 627 } 628 629 630 static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key) 631 { 632 HKEY nhk; 633 int i, errors = 0; 634 LONG ret; 635 636 ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk); 637 if (ret != ERROR_SUCCESS) { 638 wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR 639 "' for subkey deletion: error 0x%x (%d)", key, 640 (unsigned int) ret, (int) GetLastError()); 641 return 0; 642 } 643 644 for (i = 0; ; i++) { 645 TCHAR name[255]; 646 DWORD namelen; 647 648 namelen = 255; 649 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL, 650 NULL); 651 652 if (ret == ERROR_NO_MORE_ITEMS) 653 break; 654 655 if (ret != ERROR_SUCCESS) { 656 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)", 657 (unsigned int) ret, (int) GetLastError()); 658 break; 659 } 660 661 if (namelen >= 255) 662 namelen = 255 - 1; 663 name[namelen] = TEXT('\0'); 664 665 ret = RegDeleteKey(nhk, name); 666 if (ret != ERROR_SUCCESS) { 667 wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)", 668 (unsigned int) ret, (int) GetLastError()); 669 errors++; 670 } 671 } 672 673 RegCloseKey(nhk); 674 675 return errors ? -1 : 0; 676 } 677 678 679 static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid) 680 { 681 char *value = wpa_config_get(ssid, field); 682 if (value == NULL) 683 return; 684 wpa_config_write_reg_string(hk, field, value); 685 os_free(value); 686 } 687 688 689 static void write_int(HKEY hk, const char *field, int value, int def) 690 { 691 char val[20]; 692 if (value == def) 693 return; 694 os_snprintf(val, sizeof(val), "%d", value); 695 wpa_config_write_reg_string(hk, field, val); 696 } 697 698 699 static void write_bssid(HKEY hk, struct wpa_ssid *ssid) 700 { 701 char *value = wpa_config_get(ssid, "bssid"); 702 if (value == NULL) 703 return; 704 wpa_config_write_reg_string(hk, "bssid", value); 705 os_free(value); 706 } 707 708 709 static void write_psk(HKEY hk, struct wpa_ssid *ssid) 710 { 711 char *value = wpa_config_get(ssid, "psk"); 712 if (value == NULL) 713 return; 714 wpa_config_write_reg_string(hk, "psk", value); 715 os_free(value); 716 } 717 718 719 static void write_proto(HKEY hk, struct wpa_ssid *ssid) 720 { 721 char *value; 722 723 if (ssid->proto == DEFAULT_PROTO) 724 return; 725 726 value = wpa_config_get(ssid, "proto"); 727 if (value == NULL) 728 return; 729 if (value[0]) 730 wpa_config_write_reg_string(hk, "proto", value); 731 os_free(value); 732 } 733 734 735 static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid) 736 { 737 char *value; 738 739 if (ssid->key_mgmt == DEFAULT_KEY_MGMT) 740 return; 741 742 value = wpa_config_get(ssid, "key_mgmt"); 743 if (value == NULL) 744 return; 745 if (value[0]) 746 wpa_config_write_reg_string(hk, "key_mgmt", value); 747 os_free(value); 748 } 749 750 751 static void write_pairwise(HKEY hk, struct wpa_ssid *ssid) 752 { 753 char *value; 754 755 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE) 756 return; 757 758 value = wpa_config_get(ssid, "pairwise"); 759 if (value == NULL) 760 return; 761 if (value[0]) 762 wpa_config_write_reg_string(hk, "pairwise", value); 763 os_free(value); 764 } 765 766 767 static void write_group(HKEY hk, struct wpa_ssid *ssid) 768 { 769 char *value; 770 771 if (ssid->group_cipher == DEFAULT_GROUP) 772 return; 773 774 value = wpa_config_get(ssid, "group"); 775 if (value == NULL) 776 return; 777 if (value[0]) 778 wpa_config_write_reg_string(hk, "group", value); 779 os_free(value); 780 } 781 782 783 static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid) 784 { 785 char *value; 786 787 if (ssid->auth_alg == 0) 788 return; 789 790 value = wpa_config_get(ssid, "auth_alg"); 791 if (value == NULL) 792 return; 793 if (value[0]) 794 wpa_config_write_reg_string(hk, "auth_alg", value); 795 os_free(value); 796 } 797 798 799 #ifdef IEEE8021X_EAPOL 800 static void write_eap(HKEY hk, struct wpa_ssid *ssid) 801 { 802 char *value; 803 804 value = wpa_config_get(ssid, "eap"); 805 if (value == NULL) 806 return; 807 808 if (value[0]) 809 wpa_config_write_reg_string(hk, "eap", value); 810 os_free(value); 811 } 812 #endif /* IEEE8021X_EAPOL */ 813 814 815 static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid) 816 { 817 char field[20], *value; 818 819 os_snprintf(field, sizeof(field), "wep_key%d", idx); 820 value = wpa_config_get(ssid, field); 821 if (value) { 822 wpa_config_write_reg_string(hk, field, value); 823 os_free(value); 824 } 825 } 826 827 828 static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id) 829 { 830 int i, errors = 0; 831 HKEY nhk, netw; 832 LONG ret; 833 TCHAR name[5]; 834 835 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk); 836 if (ret != ERROR_SUCCESS) { 837 wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key " 838 "for subkey addition: error 0x%x (%d)", 839 (unsigned int) ret, (int) GetLastError()); 840 return 0; 841 } 842 843 #ifdef UNICODE 844 wsprintf(name, L"%04d", id); 845 #else /* UNICODE */ 846 os_snprintf(name, sizeof(name), "%04d", id); 847 #endif /* UNICODE */ 848 ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw, 849 NULL); 850 RegCloseKey(nhk); 851 if (ret != ERROR_SUCCESS) { 852 wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':" 853 " error 0x%x (%d)", 854 name, (unsigned int) ret, (int) GetLastError()); 855 return -1; 856 } 857 858 #define STR(t) write_str(netw, #t, ssid) 859 #define INT(t) write_int(netw, #t, ssid->t, 0) 860 #define INTe(t) write_int(netw, #t, ssid->eap.t, 0) 861 #define INT_DEF(t, def) write_int(netw, #t, ssid->t, def) 862 #define INT_DEFe(t, def) write_int(netw, #t, ssid->eap.t, def) 863 864 STR(ssid); 865 INT(scan_ssid); 866 write_bssid(netw, ssid); 867 write_psk(netw, ssid); 868 write_proto(netw, ssid); 869 write_key_mgmt(netw, ssid); 870 write_pairwise(netw, ssid); 871 write_group(netw, ssid); 872 write_auth_alg(netw, ssid); 873 #ifdef IEEE8021X_EAPOL 874 write_eap(netw, ssid); 875 STR(identity); 876 STR(anonymous_identity); 877 STR(password); 878 STR(ca_cert); 879 STR(ca_path); 880 STR(client_cert); 881 STR(private_key); 882 STR(private_key_passwd); 883 STR(dh_file); 884 STR(subject_match); 885 STR(altsubject_match); 886 STR(ca_cert2); 887 STR(ca_path2); 888 STR(client_cert2); 889 STR(private_key2); 890 STR(private_key2_passwd); 891 STR(dh_file2); 892 STR(subject_match2); 893 STR(altsubject_match2); 894 STR(phase1); 895 STR(phase2); 896 STR(pcsc); 897 STR(pin); 898 STR(engine_id); 899 STR(key_id); 900 STR(cert_id); 901 STR(ca_cert_id); 902 STR(key2_id); 903 STR(pin2); 904 STR(engine2_id); 905 STR(cert2_id); 906 STR(ca_cert2_id); 907 INTe(engine); 908 INTe(engine2); 909 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS); 910 #endif /* IEEE8021X_EAPOL */ 911 for (i = 0; i < 4; i++) 912 write_wep_key(netw, i, ssid); 913 INT(wep_tx_keyidx); 914 INT(priority); 915 #ifdef IEEE8021X_EAPOL 916 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND); 917 STR(pac_file); 918 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE); 919 #endif /* IEEE8021X_EAPOL */ 920 INT(mode); 921 write_int(netw, "proactive_key_caching", ssid->proactive_key_caching, 922 -1); 923 INT(disabled); 924 INT(peerkey); 925 #ifdef CONFIG_IEEE80211W 926 write_int(netw, "ieee80211w", ssid->ieee80211w, 927 MGMT_FRAME_PROTECTION_DEFAULT); 928 #endif /* CONFIG_IEEE80211W */ 929 STR(id_str); 930 931 #undef STR 932 #undef INT 933 #undef INT_DEF 934 935 RegCloseKey(netw); 936 937 return errors ? -1 : 0; 938 } 939 940 941 static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob) 942 { 943 HKEY bhk; 944 LONG ret; 945 TCHAR *name; 946 947 ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL, 948 &bhk, NULL); 949 if (ret != ERROR_SUCCESS) { 950 wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: " 951 "error 0x%x (%d)", 952 (unsigned int) ret, (int) GetLastError()); 953 return -1; 954 } 955 956 name = wpa_strdup_tchar(blob->name); 957 ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data, 958 blob->len); 959 if (ret != ERROR_SUCCESS) { 960 wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': " 961 "error 0x%x (%d)", blob->name, (unsigned int) ret, 962 (int) GetLastError()); 963 RegCloseKey(bhk); 964 os_free(name); 965 return -1; 966 } 967 os_free(name); 968 969 RegCloseKey(bhk); 970 971 return 0; 972 } 973 974 975 int wpa_config_write(const char *name, struct wpa_config *config) 976 { 977 TCHAR buf[256]; 978 HKEY hk; 979 LONG ret; 980 int errors = 0; 981 struct wpa_ssid *ssid; 982 struct wpa_config_blob *blob; 983 int id; 984 985 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name); 986 987 #ifdef UNICODE 988 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name); 989 #else /* UNICODE */ 990 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name); 991 #endif /* UNICODE */ 992 993 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk); 994 if (ret != ERROR_SUCCESS) { 995 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant " 996 "configuration registry %s: error %d", buf, 997 (int) GetLastError()); 998 return -1; 999 } 1000 1001 if (wpa_config_write_global(config, hk)) { 1002 wpa_printf(MSG_ERROR, "Failed to write global configuration " 1003 "data"); 1004 errors++; 1005 } 1006 1007 wpa_config_delete_subkeys(hk, TEXT("networks")); 1008 for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) { 1009 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS) 1010 continue; /* do not save temporary WPS networks */ 1011 if (wpa_config_write_network(hk, ssid, id)) 1012 errors++; 1013 } 1014 1015 RegDeleteKey(hk, TEXT("blobs")); 1016 for (blob = config->blobs; blob; blob = blob->next) { 1017 if (wpa_config_write_blob(hk, blob)) 1018 errors++; 1019 } 1020 1021 RegCloseKey(hk); 1022 1023 wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully", 1024 name, errors ? "un" : ""); 1025 return errors ? -1 : 0; 1026 } 1027