config_winreg.c 23 KB

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