Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. {
  206. char *t = wpa_config_read_reg_string(
  207. hk, TEXT("device_type"));
  208. if (t && wps_dev_type_str2bin(t, config->device_type))
  209. errors++;
  210. os_free(t);
  211. }
  212. config->config_methods = wpa_config_read_reg_string(
  213. hk, TEXT("config_methods"));
  214. if (wpa_config_read_global_os_version(config, hk))
  215. errors++;
  216. wpa_config_read_reg_dword(hk, TEXT("wps_cred_processing"),
  217. &config->wps_cred_processing);
  218. #endif /* CONFIG_WPS */
  219. #ifdef CONFIG_P2P
  220. config->p2p_ssid_postfix = wpa_config_read_reg_string(
  221. hk, TEXT("p2p_ssid_postfix"));
  222. wpa_config_read_reg_dword(hk, TEXT("p2p_group_idle"),
  223. (int *) &config->p2p_group_idle);
  224. #endif /* CONFIG_P2P */
  225. wpa_config_read_reg_dword(hk, TEXT("bss_max_count"),
  226. (int *) &config->bss_max_count);
  227. wpa_config_read_reg_dword(hk, TEXT("filter_ssids"),
  228. &config->filter_ssids);
  229. wpa_config_read_reg_dword(hk, TEXT("max_num_sta"),
  230. (int *) &config->max_num_sta);
  231. wpa_config_read_reg_dword(hk, TEXT("disassoc_low_ack"),
  232. (int *) &config->disassoc_low_ack);
  233. return errors ? -1 : 0;
  234. }
  235. static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw,
  236. int id)
  237. {
  238. HKEY nhk;
  239. LONG ret;
  240. DWORD i;
  241. struct wpa_ssid *ssid;
  242. int errors = 0;
  243. ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk);
  244. if (ret != ERROR_SUCCESS) {
  245. wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
  246. "network '" TSTR "'", netw);
  247. return NULL;
  248. }
  249. wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw);
  250. ssid = os_zalloc(sizeof(*ssid));
  251. if (ssid == NULL) {
  252. RegCloseKey(nhk);
  253. return NULL;
  254. }
  255. ssid->id = id;
  256. wpa_config_set_network_defaults(ssid);
  257. for (i = 0; ; i++) {
  258. TCHAR name[255], data[1024];
  259. DWORD namelen, datalen, type;
  260. namelen = 255;
  261. datalen = sizeof(data);
  262. ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type,
  263. (LPBYTE) data, &datalen);
  264. if (ret == ERROR_NO_MORE_ITEMS)
  265. break;
  266. if (ret != ERROR_SUCCESS) {
  267. wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x",
  268. (unsigned int) ret);
  269. break;
  270. }
  271. if (namelen >= 255)
  272. namelen = 255 - 1;
  273. name[namelen] = TEXT('\0');
  274. if (datalen >= 1024)
  275. datalen = 1024 - 1;
  276. data[datalen] = TEXT('\0');
  277. wpa_unicode2ascii_inplace(name);
  278. wpa_unicode2ascii_inplace(data);
  279. if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0)
  280. errors++;
  281. }
  282. RegCloseKey(nhk);
  283. if (ssid->passphrase) {
  284. if (ssid->psk_set) {
  285. wpa_printf(MSG_ERROR, "Both PSK and passphrase "
  286. "configured for network '" TSTR "'.", netw);
  287. errors++;
  288. }
  289. wpa_config_update_psk(ssid);
  290. }
  291. if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
  292. WPA_KEY_MGMT_PSK_SHA256)) &&
  293. !ssid->psk_set) {
  294. wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, "
  295. "but no PSK configured for network '" TSTR "'.",
  296. netw);
  297. errors++;
  298. }
  299. if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
  300. !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  301. !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
  302. /* Group cipher cannot be stronger than the pairwise cipher. */
  303. wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher "
  304. "list since it was not allowed for pairwise "
  305. "cipher for network '" TSTR "'.", netw);
  306. ssid->group_cipher &= ~WPA_CIPHER_CCMP;
  307. }
  308. if (errors) {
  309. wpa_config_free_ssid(ssid);
  310. ssid = NULL;
  311. }
  312. return ssid;
  313. }
  314. static int wpa_config_read_networks(struct wpa_config *config, HKEY hk)
  315. {
  316. HKEY nhk;
  317. struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
  318. int errors = 0;
  319. LONG ret;
  320. DWORD i;
  321. ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS,
  322. &nhk);
  323. if (ret != ERROR_SUCCESS) {
  324. wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks "
  325. "registry key");
  326. return -1;
  327. }
  328. for (i = 0; ; i++) {
  329. TCHAR name[255];
  330. DWORD namelen;
  331. namelen = 255;
  332. ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
  333. NULL);
  334. if (ret == ERROR_NO_MORE_ITEMS)
  335. break;
  336. if (ret != ERROR_SUCCESS) {
  337. wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x",
  338. (unsigned int) ret);
  339. break;
  340. }
  341. if (namelen >= 255)
  342. namelen = 255 - 1;
  343. name[namelen] = '\0';
  344. ssid = wpa_config_read_network(nhk, name, i);
  345. if (ssid == NULL) {
  346. wpa_printf(MSG_ERROR, "Failed to parse network "
  347. "profile '%s'.", name);
  348. errors++;
  349. continue;
  350. }
  351. if (head == NULL) {
  352. head = tail = ssid;
  353. } else {
  354. tail->next = ssid;
  355. tail = ssid;
  356. }
  357. if (wpa_config_add_prio_network(config, ssid)) {
  358. wpa_printf(MSG_ERROR, "Failed to add network profile "
  359. "'%s' to priority list.", name);
  360. errors++;
  361. continue;
  362. }
  363. }
  364. RegCloseKey(nhk);
  365. config->ssid = head;
  366. return errors ? -1 : 0;
  367. }
  368. struct wpa_config * wpa_config_read(const char *name)
  369. {
  370. TCHAR buf[256];
  371. int errors = 0;
  372. struct wpa_config *config;
  373. HKEY hk;
  374. LONG ret;
  375. config = wpa_config_alloc_empty(NULL, NULL);
  376. if (config == NULL)
  377. return NULL;
  378. wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);
  379. #ifdef UNICODE
  380. _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
  381. #else /* UNICODE */
  382. os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
  383. #endif /* UNICODE */
  384. ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk);
  385. if (ret != ERROR_SUCCESS) {
  386. wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
  387. "configuration registry HKLM\\" TSTR, buf);
  388. os_free(config);
  389. return NULL;
  390. }
  391. if (wpa_config_read_global(config, hk))
  392. errors++;
  393. if (wpa_config_read_networks(config, hk))
  394. errors++;
  395. if (wpa_config_read_blobs(config, hk))
  396. errors++;
  397. wpa_config_debug_dump_networks(config);
  398. RegCloseKey(hk);
  399. if (errors) {
  400. wpa_config_free(config);
  401. config = NULL;
  402. }
  403. return config;
  404. }
  405. static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val,
  406. int def)
  407. {
  408. LONG ret;
  409. DWORD _val = val;
  410. if (val == def) {
  411. RegDeleteValue(hk, name);
  412. return 0;
  413. }
  414. ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val,
  415. sizeof(_val));
  416. if (ret != ERROR_SUCCESS) {
  417. wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d",
  418. name, val, (int) GetLastError());
  419. return -1;
  420. }
  421. return 0;
  422. }
  423. static int wpa_config_write_reg_string(HKEY hk, const char *name,
  424. const char *val)
  425. {
  426. LONG ret;
  427. TCHAR *_name, *_val;
  428. _name = wpa_strdup_tchar(name);
  429. if (_name == NULL)
  430. return -1;
  431. if (val == NULL) {
  432. RegDeleteValue(hk, _name);
  433. os_free(_name);
  434. return 0;
  435. }
  436. _val = wpa_strdup_tchar(val);
  437. if (_val == NULL) {
  438. os_free(_name);
  439. return -1;
  440. }
  441. ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val,
  442. (os_strlen(val) + 1) * sizeof(TCHAR));
  443. if (ret != ERROR_SUCCESS) {
  444. wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': "
  445. "error %d", name, val, (int) GetLastError());
  446. os_free(_name);
  447. os_free(_val);
  448. return -1;
  449. }
  450. os_free(_name);
  451. os_free(_val);
  452. return 0;
  453. }
  454. static int wpa_config_write_global(struct wpa_config *config, HKEY hk)
  455. {
  456. #ifdef CONFIG_CTRL_IFACE
  457. wpa_config_write_reg_string(hk, "ctrl_interface",
  458. config->ctrl_interface);
  459. #endif /* CONFIG_CTRL_IFACE */
  460. wpa_config_write_reg_dword(hk, TEXT("eapol_version"),
  461. config->eapol_version,
  462. DEFAULT_EAPOL_VERSION);
  463. wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan,
  464. DEFAULT_AP_SCAN);
  465. wpa_config_write_reg_dword(hk, TEXT("fast_reauth"),
  466. config->fast_reauth, DEFAULT_FAST_REAUTH);
  467. wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
  468. config->dot11RSNAConfigPMKLifetime, 0);
  469. wpa_config_write_reg_dword(hk,
  470. TEXT("dot11RSNAConfigPMKReauthThreshold"),
  471. config->dot11RSNAConfigPMKReauthThreshold,
  472. 0);
  473. wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
  474. config->dot11RSNAConfigSATimeout, 0);
  475. wpa_config_write_reg_dword(hk, TEXT("update_config"),
  476. config->update_config,
  477. 0);
  478. #ifdef CONFIG_WPS
  479. if (!is_nil_uuid(config->uuid)) {
  480. char buf[40];
  481. uuid_bin2str(config->uuid, buf, sizeof(buf));
  482. wpa_config_write_reg_string(hk, "uuid", buf);
  483. }
  484. wpa_config_write_reg_string(hk, "device_name", config->device_name);
  485. wpa_config_write_reg_string(hk, "manufacturer", config->manufacturer);
  486. wpa_config_write_reg_string(hk, "model_name", config->model_name);
  487. wpa_config_write_reg_string(hk, "model_number", config->model_number);
  488. wpa_config_write_reg_string(hk, "serial_number",
  489. config->serial_number);
  490. {
  491. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  492. buf = wps_dev_type_bin2str(config->device_type,
  493. _buf, sizeof(_buf));
  494. wpa_config_write_reg_string(hk, "device_type", buf);
  495. }
  496. wpa_config_write_reg_string(hk, "config_methods",
  497. config->config_methods);
  498. if (WPA_GET_BE32(config->os_version)) {
  499. char vbuf[10];
  500. os_snprintf(vbuf, sizeof(vbuf), "%08x",
  501. WPA_GET_BE32(config->os_version));
  502. wpa_config_write_reg_string(hk, "os_version", vbuf);
  503. }
  504. wpa_config_write_reg_dword(hk, TEXT("wps_cred_processing"),
  505. config->wps_cred_processing, 0);
  506. #endif /* CONFIG_WPS */
  507. #ifdef CONFIG_P2P
  508. wpa_config_write_reg_string(hk, "p2p_ssid_postfix",
  509. config->p2p_ssid_postfix);
  510. wpa_config_write_reg_dword(hk, TEXT("p2p_group_idle"),
  511. config->p2p_group_idle, 0);
  512. #endif /* CONFIG_P2P */
  513. wpa_config_write_reg_dword(hk, TEXT("bss_max_count"),
  514. config->bss_max_count,
  515. DEFAULT_BSS_MAX_COUNT);
  516. wpa_config_write_reg_dword(hk, TEXT("filter_ssids"),
  517. config->filter_ssids, 0);
  518. wpa_config_write_reg_dword(hk, TEXT("max_num_sta"),
  519. config->max_num_sta, DEFAULT_MAX_NUM_STA);
  520. wpa_config_write_reg_dword(hk, TEXT("disassoc_low_ack"),
  521. config->disassoc_low_ack, 0);
  522. return 0;
  523. }
  524. static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key)
  525. {
  526. HKEY nhk;
  527. int i, errors = 0;
  528. LONG ret;
  529. ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk);
  530. if (ret != ERROR_SUCCESS) {
  531. wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR
  532. "' for subkey deletion: error 0x%x (%d)", key,
  533. (unsigned int) ret, (int) GetLastError());
  534. return 0;
  535. }
  536. for (i = 0; ; i++) {
  537. TCHAR name[255];
  538. DWORD namelen;
  539. namelen = 255;
  540. ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
  541. NULL);
  542. if (ret == ERROR_NO_MORE_ITEMS)
  543. break;
  544. if (ret != ERROR_SUCCESS) {
  545. wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)",
  546. (unsigned int) ret, (int) GetLastError());
  547. break;
  548. }
  549. if (namelen >= 255)
  550. namelen = 255 - 1;
  551. name[namelen] = TEXT('\0');
  552. ret = RegDeleteKey(nhk, name);
  553. if (ret != ERROR_SUCCESS) {
  554. wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)",
  555. (unsigned int) ret, (int) GetLastError());
  556. errors++;
  557. }
  558. }
  559. RegCloseKey(nhk);
  560. return errors ? -1 : 0;
  561. }
  562. static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid)
  563. {
  564. char *value = wpa_config_get(ssid, field);
  565. if (value == NULL)
  566. return;
  567. wpa_config_write_reg_string(hk, field, value);
  568. os_free(value);
  569. }
  570. static void write_int(HKEY hk, const char *field, int value, int def)
  571. {
  572. char val[20];
  573. if (value == def)
  574. return;
  575. os_snprintf(val, sizeof(val), "%d", value);
  576. wpa_config_write_reg_string(hk, field, val);
  577. }
  578. static void write_bssid(HKEY hk, struct wpa_ssid *ssid)
  579. {
  580. char *value = wpa_config_get(ssid, "bssid");
  581. if (value == NULL)
  582. return;
  583. wpa_config_write_reg_string(hk, "bssid", value);
  584. os_free(value);
  585. }
  586. static void write_psk(HKEY hk, struct wpa_ssid *ssid)
  587. {
  588. char *value = wpa_config_get(ssid, "psk");
  589. if (value == NULL)
  590. return;
  591. wpa_config_write_reg_string(hk, "psk", value);
  592. os_free(value);
  593. }
  594. static void write_proto(HKEY hk, struct wpa_ssid *ssid)
  595. {
  596. char *value;
  597. if (ssid->proto == DEFAULT_PROTO)
  598. return;
  599. value = wpa_config_get(ssid, "proto");
  600. if (value == NULL)
  601. return;
  602. if (value[0])
  603. wpa_config_write_reg_string(hk, "proto", value);
  604. os_free(value);
  605. }
  606. static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid)
  607. {
  608. char *value;
  609. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  610. return;
  611. value = wpa_config_get(ssid, "key_mgmt");
  612. if (value == NULL)
  613. return;
  614. if (value[0])
  615. wpa_config_write_reg_string(hk, "key_mgmt", value);
  616. os_free(value);
  617. }
  618. static void write_pairwise(HKEY hk, struct wpa_ssid *ssid)
  619. {
  620. char *value;
  621. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  622. return;
  623. value = wpa_config_get(ssid, "pairwise");
  624. if (value == NULL)
  625. return;
  626. if (value[0])
  627. wpa_config_write_reg_string(hk, "pairwise", value);
  628. os_free(value);
  629. }
  630. static void write_group(HKEY hk, struct wpa_ssid *ssid)
  631. {
  632. char *value;
  633. if (ssid->group_cipher == DEFAULT_GROUP)
  634. return;
  635. value = wpa_config_get(ssid, "group");
  636. if (value == NULL)
  637. return;
  638. if (value[0])
  639. wpa_config_write_reg_string(hk, "group", value);
  640. os_free(value);
  641. }
  642. static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid)
  643. {
  644. char *value;
  645. if (ssid->auth_alg == 0)
  646. return;
  647. value = wpa_config_get(ssid, "auth_alg");
  648. if (value == NULL)
  649. return;
  650. if (value[0])
  651. wpa_config_write_reg_string(hk, "auth_alg", value);
  652. os_free(value);
  653. }
  654. #ifdef IEEE8021X_EAPOL
  655. static void write_eap(HKEY hk, struct wpa_ssid *ssid)
  656. {
  657. char *value;
  658. value = wpa_config_get(ssid, "eap");
  659. if (value == NULL)
  660. return;
  661. if (value[0])
  662. wpa_config_write_reg_string(hk, "eap", value);
  663. os_free(value);
  664. }
  665. #endif /* IEEE8021X_EAPOL */
  666. static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid)
  667. {
  668. char field[20], *value;
  669. os_snprintf(field, sizeof(field), "wep_key%d", idx);
  670. value = wpa_config_get(ssid, field);
  671. if (value) {
  672. wpa_config_write_reg_string(hk, field, value);
  673. os_free(value);
  674. }
  675. }
  676. static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id)
  677. {
  678. int i, errors = 0;
  679. HKEY nhk, netw;
  680. LONG ret;
  681. TCHAR name[5];
  682. ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk);
  683. if (ret != ERROR_SUCCESS) {
  684. wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key "
  685. "for subkey addition: error 0x%x (%d)",
  686. (unsigned int) ret, (int) GetLastError());
  687. return 0;
  688. }
  689. #ifdef UNICODE
  690. wsprintf(name, L"%04d", id);
  691. #else /* UNICODE */
  692. os_snprintf(name, sizeof(name), "%04d", id);
  693. #endif /* UNICODE */
  694. ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw,
  695. NULL);
  696. RegCloseKey(nhk);
  697. if (ret != ERROR_SUCCESS) {
  698. wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':"
  699. " error 0x%x (%d)",
  700. name, (unsigned int) ret, (int) GetLastError());
  701. return -1;
  702. }
  703. #define STR(t) write_str(netw, #t, ssid)
  704. #define INT(t) write_int(netw, #t, ssid->t, 0)
  705. #define INTe(t) write_int(netw, #t, ssid->eap.t, 0)
  706. #define INT_DEF(t, def) write_int(netw, #t, ssid->t, def)
  707. #define INT_DEFe(t, def) write_int(netw, #t, ssid->eap.t, def)
  708. STR(ssid);
  709. INT(scan_ssid);
  710. write_bssid(netw, ssid);
  711. write_psk(netw, ssid);
  712. write_proto(netw, ssid);
  713. write_key_mgmt(netw, ssid);
  714. write_pairwise(netw, ssid);
  715. write_group(netw, ssid);
  716. write_auth_alg(netw, ssid);
  717. #ifdef IEEE8021X_EAPOL
  718. write_eap(netw, ssid);
  719. STR(identity);
  720. STR(anonymous_identity);
  721. STR(password);
  722. STR(ca_cert);
  723. STR(ca_path);
  724. STR(client_cert);
  725. STR(private_key);
  726. STR(private_key_passwd);
  727. STR(dh_file);
  728. STR(subject_match);
  729. STR(altsubject_match);
  730. STR(ca_cert2);
  731. STR(ca_path2);
  732. STR(client_cert2);
  733. STR(private_key2);
  734. STR(private_key2_passwd);
  735. STR(dh_file2);
  736. STR(subject_match2);
  737. STR(altsubject_match2);
  738. STR(phase1);
  739. STR(phase2);
  740. STR(pcsc);
  741. STR(pin);
  742. STR(engine_id);
  743. STR(key_id);
  744. STR(cert_id);
  745. STR(ca_cert_id);
  746. STR(key2_id);
  747. STR(pin2);
  748. STR(engine2_id);
  749. STR(cert2_id);
  750. STR(ca_cert2_id);
  751. INTe(engine);
  752. INTe(engine2);
  753. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  754. #endif /* IEEE8021X_EAPOL */
  755. for (i = 0; i < 4; i++)
  756. write_wep_key(netw, i, ssid);
  757. INT(wep_tx_keyidx);
  758. INT(priority);
  759. #ifdef IEEE8021X_EAPOL
  760. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  761. STR(pac_file);
  762. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  763. #endif /* IEEE8021X_EAPOL */
  764. INT(mode);
  765. INT(proactive_key_caching);
  766. INT(disabled);
  767. INT(peerkey);
  768. #ifdef CONFIG_IEEE80211W
  769. INT(ieee80211w);
  770. #endif /* CONFIG_IEEE80211W */
  771. STR(id_str);
  772. #undef STR
  773. #undef INT
  774. #undef INT_DEF
  775. RegCloseKey(netw);
  776. return errors ? -1 : 0;
  777. }
  778. static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob)
  779. {
  780. HKEY bhk;
  781. LONG ret;
  782. TCHAR *name;
  783. ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL,
  784. &bhk, NULL);
  785. if (ret != ERROR_SUCCESS) {
  786. wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: "
  787. "error 0x%x (%d)",
  788. (unsigned int) ret, (int) GetLastError());
  789. return -1;
  790. }
  791. name = wpa_strdup_tchar(blob->name);
  792. ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data,
  793. blob->len);
  794. if (ret != ERROR_SUCCESS) {
  795. wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': "
  796. "error 0x%x (%d)", blob->name, (unsigned int) ret,
  797. (int) GetLastError());
  798. RegCloseKey(bhk);
  799. os_free(name);
  800. return -1;
  801. }
  802. os_free(name);
  803. RegCloseKey(bhk);
  804. return 0;
  805. }
  806. int wpa_config_write(const char *name, struct wpa_config *config)
  807. {
  808. TCHAR buf[256];
  809. HKEY hk;
  810. LONG ret;
  811. int errors = 0;
  812. struct wpa_ssid *ssid;
  813. struct wpa_config_blob *blob;
  814. int id;
  815. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  816. #ifdef UNICODE
  817. _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
  818. #else /* UNICODE */
  819. os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
  820. #endif /* UNICODE */
  821. ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk);
  822. if (ret != ERROR_SUCCESS) {
  823. wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
  824. "configuration registry %s: error %d", buf,
  825. (int) GetLastError());
  826. return -1;
  827. }
  828. if (wpa_config_write_global(config, hk)) {
  829. wpa_printf(MSG_ERROR, "Failed to write global configuration "
  830. "data");
  831. errors++;
  832. }
  833. wpa_config_delete_subkeys(hk, TEXT("networks"));
  834. for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) {
  835. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  836. continue; /* do not save temporary WPS networks */
  837. if (wpa_config_write_network(hk, ssid, id))
  838. errors++;
  839. }
  840. RegDeleteKey(hk, TEXT("blobs"));
  841. for (blob = config->blobs; blob; blob = blob->next) {
  842. if (wpa_config_write_blob(hk, blob))
  843. errors++;
  844. }
  845. RegCloseKey(hk);
  846. wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully",
  847. name, errors ? "un" : "");
  848. return errors ? -1 : 0;
  849. }