📄 config_winreg.c
字号:
/* * WPA Supplicant / Configuration backend: Windows registry * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Alternatively, this software may be distributed under the terms of BSD * license. * * See README and COPYING for more details. * * This file implements a configuration backend for Windows registry.. All the * configuration information is stored in the registry and the format for * network configuration fields is same as described in the sample * configuration file, wpa_supplicant.conf. * * Configuration data is in HKEY_LOCAL_MACHINE\SOFTWARE\wpa_supplicant\configs * key. Each configuration profile has its own key under this. In terms of text * files, each profile would map to a separate text file with possibly multiple * networks. Under each profile, there is a networks key that lists all * networks as a subkey. Each network has set of values in the same way as * network block in the configuration file. In addition, blobs subkey has * possible blobs as values. * * HKEY_LOCAL_MACHINE\SOFTWARE\wpa_supplicant\configs\test\networks\0000 * ssid="example" * key_mgmt=WPA-PSK */#include "includes.h"#include "common.h"#include "config.h"#ifndef WPA_KEY_ROOT#define WPA_KEY_ROOT HKEY_LOCAL_MACHINE#endif#ifndef WPA_KEY_PREFIX#define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")#endif#ifdef UNICODE#define TSTR "%S"#else /* UNICODE */#define TSTR "%s"#endif /* UNICODE */static int wpa_config_read_blobs(struct wpa_config *config, HKEY hk){ struct wpa_config_blob *blob; int errors = 0; HKEY bhk; LONG ret; DWORD i; ret = RegOpenKeyEx(hk, TEXT("blobs"), 0, KEY_QUERY_VALUE, &bhk); if (ret != ERROR_SUCCESS) { wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config " "blobs key"); return 0; /* assume no blobs */ } for (i = 0; ; i++) {#define TNAMELEN 255 TCHAR name[TNAMELEN]; char data[4096]; DWORD namelen, datalen, type; namelen = TNAMELEN; datalen = sizeof(data); ret = RegEnumValue(bhk, i, name, &namelen, NULL, &type, (LPBYTE) data, &datalen); if (ret == ERROR_NO_MORE_ITEMS) break; if (ret != ERROR_SUCCESS) { wpa_printf(MSG_DEBUG, "RegEnumValue failed: 0x%x", (unsigned int) ret); break; } if (namelen >= TNAMELEN) namelen = TNAMELEN - 1; name[namelen] = TEXT('\0'); wpa_unicode2ascii_inplace(name); if (datalen >= sizeof(data)) datalen = sizeof(data) - 1; wpa_printf(MSG_MSGDUMP, "blob %d: field='%s' len %d", (int) i, name, (int) datalen); blob = os_zalloc(sizeof(*blob)); if (blob == NULL) { errors++; break; } blob->name = os_strdup((char *) name); blob->data = os_malloc(datalen); if (blob->name == NULL || blob->data == NULL) { wpa_config_free_blob(blob); errors++; break; } os_memcpy(blob->data, data, datalen); blob->len = datalen; wpa_config_set_blob(config, blob); } RegCloseKey(bhk); return errors ? -1 : 0;}static int wpa_config_read_reg_dword(HKEY hk, const TCHAR *name, int *_val){ DWORD val, buflen; LONG ret; buflen = sizeof(val); ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) &val, &buflen); if (ret == ERROR_SUCCESS && buflen == sizeof(val)) { wpa_printf(MSG_DEBUG, TSTR "=%d", name, (int) val); *_val = val; return 0; } return -1;}static char * wpa_config_read_reg_string(HKEY hk, const TCHAR *name){ DWORD buflen; LONG ret; TCHAR *val; buflen = 0; ret = RegQueryValueEx(hk, name, NULL, NULL, NULL, &buflen); if (ret != ERROR_SUCCESS) return NULL; val = os_malloc(buflen); if (val == NULL) return NULL; ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) val, &buflen); if (ret != ERROR_SUCCESS) { os_free(val); return NULL; } wpa_unicode2ascii_inplace(val); wpa_printf(MSG_DEBUG, TSTR "=%s", name, (char *) val); return (char *) val;}static int wpa_config_read_global(struct wpa_config *config, HKEY hk){ int errors = 0; wpa_config_read_reg_dword(hk, TEXT("ap_scan"), &config->ap_scan); wpa_config_read_reg_dword(hk, TEXT("fast_reauth"), &config->fast_reauth); wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"), &config->dot11RSNAConfigPMKLifetime); wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigPMKReauthThreshold"), &config->dot11RSNAConfigPMKReauthThreshold); wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"), &config->dot11RSNAConfigSATimeout); wpa_config_read_reg_dword(hk, TEXT("update_config"), &config->update_config); if (wpa_config_read_reg_dword(hk, TEXT("eapol_version"), &config->eapol_version) == 0) { if (config->eapol_version < 1 || config->eapol_version > 2) { wpa_printf(MSG_ERROR, "Invalid EAPOL version (%d)", config->eapol_version); errors++; } } config->ctrl_interface = wpa_config_read_reg_string( hk, TEXT("ctrl_interface")); return errors ? -1 : 0;}static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw, int id){ HKEY nhk; LONG ret; DWORD i; struct wpa_ssid *ssid; int errors = 0; ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk); if (ret != ERROR_SUCCESS) { wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config " "network '" TSTR "'", netw); return NULL; } wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw); ssid = os_zalloc(sizeof(*ssid)); if (ssid == NULL) { RegCloseKey(nhk); return NULL; } ssid->id = id; wpa_config_set_network_defaults(ssid); for (i = 0; ; i++) { TCHAR name[255], data[1024]; DWORD namelen, datalen, type; namelen = 255; datalen = sizeof(data); ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type, (LPBYTE) data, &datalen); if (ret == ERROR_NO_MORE_ITEMS) break; if (ret != ERROR_SUCCESS) { wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x", (unsigned int) ret); break; } if (namelen >= 255) namelen = 255 - 1; name[namelen] = TEXT('\0'); if (datalen >= 1024) datalen = 1024 - 1; data[datalen] = TEXT('\0'); wpa_unicode2ascii_inplace(name); wpa_unicode2ascii_inplace(data); if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0) errors++; } RegCloseKey(nhk); if (ssid->passphrase) { if (ssid->psk_set) { wpa_printf(MSG_ERROR, "Both PSK and passphrase " "configured for network '" TSTR "'.", netw); errors++; } wpa_config_update_psk(ssid); } if ((ssid->key_mgmt & WPA_KEY_MGMT_PSK) && !ssid->psk_set) { wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, " "but no PSK configured for network '" TSTR "'.", netw); errors++; } if ((ssid->group_cipher & WPA_CIPHER_CCMP) && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) && !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) { /* Group cipher cannot be stronger than the pairwise cipher. */ wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher " "list since it was not allowed for pairwise " "cipher for network '" TSTR "'.", netw); ssid->group_cipher &= ~WPA_CIPHER_CCMP; } if (errors) { wpa_config_free_ssid(ssid); ssid = NULL; } return ssid;}static int wpa_config_read_networks(struct wpa_config *config, HKEY hk){ HKEY nhk; struct wpa_ssid *ssid, *tail = NULL, *head = NULL; int errors = 0; LONG ret; DWORD i; ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS, &nhk); if (ret != ERROR_SUCCESS) { wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks " "registry key"); return -1; } for (i = 0; ; i++) { TCHAR name[255]; DWORD namelen; namelen = 255; ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL, NULL); if (ret == ERROR_NO_MORE_ITEMS) break; if (ret != ERROR_SUCCESS) { wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x", (unsigned int) ret); break; } if (namelen >= 255) namelen = 255 - 1; name[namelen] = '\0'; ssid = wpa_config_read_network(nhk, name, i); if (ssid == NULL) { wpa_printf(MSG_ERROR, "Failed to parse network " "profile '%s'.", name); errors++; continue; } if (head == NULL) { head = tail = ssid; } else { tail->next = ssid; tail = ssid; } if (wpa_config_add_prio_network(config, ssid)) { wpa_printf(MSG_ERROR, "Failed to add network profile " "'%s' to priority list.", name); errors++; continue; } } RegCloseKey(nhk); config->ssid = head; return errors ? -1 : 0;}struct wpa_config * wpa_config_read(const char *name){ TCHAR buf[256]; int errors = 0; struct wpa_config *config; HKEY hk; LONG ret; config = wpa_config_alloc_empty(NULL, NULL); if (config == NULL) return NULL; wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);#ifdef UNICODE _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);#else /* UNICODE */ os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);#endif /* UNICODE */ ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk); if (ret != ERROR_SUCCESS) { wpa_printf(MSG_ERROR, "Could not open wpa_supplicant " "configuration registry HKLM\\" TSTR, buf); os_free(config); return NULL; } if (wpa_config_read_global(config, hk)) errors++; if (wpa_config_read_networks(config, hk)) errors++; if (wpa_config_read_blobs(config, hk)) errors++; wpa_config_debug_dump_networks(config); RegCloseKey(hk); if (errors) { wpa_config_free(config); config = NULL; } return config;}static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val, int def){ LONG ret; DWORD _val = val; if (val == def) { RegDeleteValue(hk, name); return 0; } ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val, sizeof(_val)); if (ret != ERROR_SUCCESS) { wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d", name, val, (int) GetLastError()); return -1; } return 0;}static int wpa_config_write_reg_string(HKEY hk, const char *name, const char *val){ LONG ret; TCHAR *_name, *_val; _name = wpa_strdup_tchar(name); if (_name == NULL) return -1; if (val == NULL) { RegDeleteValue(hk, _name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -