config_winreg.c 24 KB

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