config_winreg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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_memdup(data, datalen);
  88. if (blob->name == NULL || blob->data == NULL) {
  89. wpa_config_free_blob(blob);
  90. errors++;
  91. break;
  92. }
  93. blob->len = datalen;
  94. wpa_config_set_blob(config, blob);
  95. }
  96. RegCloseKey(bhk);
  97. return errors ? -1 : 0;
  98. }
  99. static int wpa_config_read_reg_dword(HKEY hk, const TCHAR *name, int *_val)
  100. {
  101. DWORD val, buflen;
  102. LONG ret;
  103. buflen = sizeof(val);
  104. ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) &val, &buflen);
  105. if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
  106. wpa_printf(MSG_DEBUG, TSTR "=%d", name, (int) val);
  107. *_val = val;
  108. return 0;
  109. }
  110. return -1;
  111. }
  112. static char * wpa_config_read_reg_string(HKEY hk, const TCHAR *name)
  113. {
  114. DWORD buflen;
  115. LONG ret;
  116. TCHAR *val;
  117. buflen = 0;
  118. ret = RegQueryValueEx(hk, name, NULL, NULL, NULL, &buflen);
  119. if (ret != ERROR_SUCCESS)
  120. return NULL;
  121. val = os_malloc(buflen);
  122. if (val == NULL)
  123. return NULL;
  124. ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) val, &buflen);
  125. if (ret != ERROR_SUCCESS) {
  126. os_free(val);
  127. return NULL;
  128. }
  129. wpa_unicode2ascii_inplace(val);
  130. wpa_printf(MSG_DEBUG, TSTR "=%s", name, (char *) val);
  131. return (char *) val;
  132. }
  133. #ifdef CONFIG_WPS
  134. static int wpa_config_read_global_uuid(struct wpa_config *config, HKEY hk)
  135. {
  136. char *str;
  137. int ret = 0;
  138. str = wpa_config_read_reg_string(hk, TEXT("uuid"));
  139. if (str == NULL)
  140. return 0;
  141. if (uuid_str2bin(str, config->uuid))
  142. ret = -1;
  143. os_free(str);
  144. return ret;
  145. }
  146. static int wpa_config_read_global_os_version(struct wpa_config *config,
  147. HKEY hk)
  148. {
  149. char *str;
  150. int ret = 0;
  151. str = wpa_config_read_reg_string(hk, TEXT("os_version"));
  152. if (str == NULL)
  153. return 0;
  154. if (hexstr2bin(str, config->os_version, 4))
  155. ret = -1;
  156. os_free(str);
  157. return ret;
  158. }
  159. #endif /* CONFIG_WPS */
  160. static int wpa_config_read_global(struct wpa_config *config, HKEY hk)
  161. {
  162. int errors = 0;
  163. int val;
  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. wpa_config_read_reg_dword(hk, TEXT("auto_uuid"), &config->auto_uuid);
  192. config->device_name = wpa_config_read_reg_string(
  193. hk, TEXT("device_name"));
  194. config->manufacturer = wpa_config_read_reg_string(
  195. hk, TEXT("manufacturer"));
  196. config->model_name = wpa_config_read_reg_string(
  197. hk, TEXT("model_name"));
  198. config->serial_number = wpa_config_read_reg_string(
  199. hk, TEXT("serial_number"));
  200. {
  201. char *t = wpa_config_read_reg_string(
  202. hk, TEXT("device_type"));
  203. if (t && wps_dev_type_str2bin(t, config->device_type))
  204. errors++;
  205. os_free(t);
  206. }
  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. wpa_config_read_reg_dword(hk, TEXT("p2p_group_idle"),
  218. (int *) &config->p2p_group_idle);
  219. #endif /* CONFIG_P2P */
  220. wpa_config_read_reg_dword(hk, TEXT("bss_max_count"),
  221. (int *) &config->bss_max_count);
  222. wpa_config_read_reg_dword(hk, TEXT("filter_ssids"),
  223. &config->filter_ssids);
  224. wpa_config_read_reg_dword(hk, TEXT("max_num_sta"),
  225. (int *) &config->max_num_sta);
  226. wpa_config_read_reg_dword(hk, TEXT("disassoc_low_ack"),
  227. (int *) &config->disassoc_low_ack);
  228. wpa_config_read_reg_dword(hk, TEXT("okc"), &config->okc);
  229. wpa_config_read_reg_dword(hk, TEXT("pmf"), &val);
  230. config->pmf = val;
  231. return errors ? -1 : 0;
  232. }
  233. static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw,
  234. int id)
  235. {
  236. HKEY nhk;
  237. LONG ret;
  238. DWORD i;
  239. struct wpa_ssid *ssid;
  240. int errors = 0;
  241. ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk);
  242. if (ret != ERROR_SUCCESS) {
  243. wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
  244. "network '" TSTR "'", netw);
  245. return NULL;
  246. }
  247. wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw);
  248. ssid = os_zalloc(sizeof(*ssid));
  249. if (ssid == NULL) {
  250. RegCloseKey(nhk);
  251. return NULL;
  252. }
  253. dl_list_init(&ssid->psk_list);
  254. ssid->id = id;
  255. wpa_config_set_network_defaults(ssid);
  256. for (i = 0; ; i++) {
  257. TCHAR name[255], data[1024];
  258. DWORD namelen, datalen, type;
  259. namelen = 255;
  260. datalen = sizeof(data);
  261. ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type,
  262. (LPBYTE) data, &datalen);
  263. if (ret == ERROR_NO_MORE_ITEMS)
  264. break;
  265. if (ret != ERROR_SUCCESS) {
  266. wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x",
  267. (unsigned int) ret);
  268. break;
  269. }
  270. if (namelen >= 255)
  271. namelen = 255 - 1;
  272. name[namelen] = TEXT('\0');
  273. if (datalen >= 1024)
  274. datalen = 1024 - 1;
  275. data[datalen] = TEXT('\0');
  276. wpa_unicode2ascii_inplace(name);
  277. wpa_unicode2ascii_inplace(data);
  278. if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0)
  279. errors++;
  280. }
  281. RegCloseKey(nhk);
  282. if (ssid->passphrase) {
  283. if (ssid->psk_set) {
  284. wpa_printf(MSG_ERROR, "Both PSK and passphrase "
  285. "configured for network '" TSTR "'.", netw);
  286. errors++;
  287. }
  288. wpa_config_update_psk(ssid);
  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, struct wpa_config *cfgp)
  360. {
  361. TCHAR buf[256];
  362. int errors = 0;
  363. struct wpa_config *config;
  364. HKEY hk;
  365. LONG ret;
  366. if (name == NULL)
  367. return NULL;
  368. if (cfgp)
  369. config = cfgp;
  370. else
  371. config = wpa_config_alloc_empty(NULL, NULL);
  372. if (config == NULL)
  373. return NULL;
  374. wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);
  375. #ifdef UNICODE
  376. _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
  377. #else /* UNICODE */
  378. os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
  379. #endif /* UNICODE */
  380. ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk);
  381. if (ret != ERROR_SUCCESS) {
  382. wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
  383. "configuration registry HKLM\\" TSTR, buf);
  384. os_free(config);
  385. return NULL;
  386. }
  387. if (wpa_config_read_global(config, hk))
  388. errors++;
  389. if (wpa_config_read_networks(config, hk))
  390. errors++;
  391. if (wpa_config_read_blobs(config, hk))
  392. errors++;
  393. wpa_config_debug_dump_networks(config);
  394. RegCloseKey(hk);
  395. if (errors) {
  396. wpa_config_free(config);
  397. config = NULL;
  398. }
  399. return config;
  400. }
  401. static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val,
  402. int def)
  403. {
  404. LONG ret;
  405. DWORD _val = val;
  406. if (val == def) {
  407. RegDeleteValue(hk, name);
  408. return 0;
  409. }
  410. ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val,
  411. sizeof(_val));
  412. if (ret != ERROR_SUCCESS) {
  413. wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d",
  414. name, val, (int) GetLastError());
  415. return -1;
  416. }
  417. return 0;
  418. }
  419. static int wpa_config_write_reg_string(HKEY hk, const char *name,
  420. const char *val)
  421. {
  422. LONG ret;
  423. TCHAR *_name, *_val;
  424. _name = wpa_strdup_tchar(name);
  425. if (_name == NULL)
  426. return -1;
  427. if (val == NULL) {
  428. RegDeleteValue(hk, _name);
  429. os_free(_name);
  430. return 0;
  431. }
  432. _val = wpa_strdup_tchar(val);
  433. if (_val == NULL) {
  434. os_free(_name);
  435. return -1;
  436. }
  437. ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val,
  438. (os_strlen(val) + 1) * sizeof(TCHAR));
  439. if (ret != ERROR_SUCCESS) {
  440. wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': "
  441. "error %d", name, val, (int) GetLastError());
  442. os_free(_name);
  443. os_free(_val);
  444. return -1;
  445. }
  446. os_free(_name);
  447. os_free(_val);
  448. return 0;
  449. }
  450. static int wpa_config_write_global(struct wpa_config *config, HKEY hk)
  451. {
  452. #ifdef CONFIG_CTRL_IFACE
  453. wpa_config_write_reg_string(hk, "ctrl_interface",
  454. config->ctrl_interface);
  455. #endif /* CONFIG_CTRL_IFACE */
  456. wpa_config_write_reg_dword(hk, TEXT("eapol_version"),
  457. config->eapol_version,
  458. DEFAULT_EAPOL_VERSION);
  459. wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan,
  460. DEFAULT_AP_SCAN);
  461. wpa_config_write_reg_dword(hk, TEXT("fast_reauth"),
  462. config->fast_reauth, DEFAULT_FAST_REAUTH);
  463. wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
  464. config->dot11RSNAConfigPMKLifetime, 0);
  465. wpa_config_write_reg_dword(hk,
  466. TEXT("dot11RSNAConfigPMKReauthThreshold"),
  467. config->dot11RSNAConfigPMKReauthThreshold,
  468. 0);
  469. wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
  470. config->dot11RSNAConfigSATimeout, 0);
  471. wpa_config_write_reg_dword(hk, TEXT("update_config"),
  472. config->update_config,
  473. 0);
  474. #ifdef CONFIG_WPS
  475. if (!is_nil_uuid(config->uuid)) {
  476. char buf[40];
  477. uuid_bin2str(config->uuid, buf, sizeof(buf));
  478. wpa_config_write_reg_string(hk, "uuid", buf);
  479. }
  480. wpa_config_write_reg_dword(hk, TEXT("auto_uuid"), config->auto_uuid,
  481. 0);
  482. wpa_config_write_reg_string(hk, "device_name", config->device_name);
  483. wpa_config_write_reg_string(hk, "manufacturer", config->manufacturer);
  484. wpa_config_write_reg_string(hk, "model_name", config->model_name);
  485. wpa_config_write_reg_string(hk, "model_number", config->model_number);
  486. wpa_config_write_reg_string(hk, "serial_number",
  487. config->serial_number);
  488. {
  489. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  490. buf = wps_dev_type_bin2str(config->device_type,
  491. _buf, sizeof(_buf));
  492. wpa_config_write_reg_string(hk, "device_type", buf);
  493. }
  494. wpa_config_write_reg_string(hk, "config_methods",
  495. config->config_methods);
  496. if (WPA_GET_BE32(config->os_version)) {
  497. char vbuf[10];
  498. os_snprintf(vbuf, sizeof(vbuf), "%08x",
  499. WPA_GET_BE32(config->os_version));
  500. wpa_config_write_reg_string(hk, "os_version", vbuf);
  501. }
  502. wpa_config_write_reg_dword(hk, TEXT("wps_cred_processing"),
  503. config->wps_cred_processing, 0);
  504. #endif /* CONFIG_WPS */
  505. #ifdef CONFIG_P2P
  506. wpa_config_write_reg_string(hk, "p2p_ssid_postfix",
  507. config->p2p_ssid_postfix);
  508. wpa_config_write_reg_dword(hk, TEXT("p2p_group_idle"),
  509. config->p2p_group_idle, 0);
  510. #endif /* CONFIG_P2P */
  511. wpa_config_write_reg_dword(hk, TEXT("bss_max_count"),
  512. config->bss_max_count,
  513. DEFAULT_BSS_MAX_COUNT);
  514. wpa_config_write_reg_dword(hk, TEXT("filter_ssids"),
  515. config->filter_ssids, 0);
  516. wpa_config_write_reg_dword(hk, TEXT("max_num_sta"),
  517. config->max_num_sta, DEFAULT_MAX_NUM_STA);
  518. wpa_config_write_reg_dword(hk, TEXT("ap_isolate"),
  519. config->ap_isolate, DEFAULT_AP_ISOLATE);
  520. wpa_config_write_reg_dword(hk, TEXT("disassoc_low_ack"),
  521. config->disassoc_low_ack, 0);
  522. wpa_config_write_reg_dword(hk, TEXT("okc"), config->okc, 0);
  523. wpa_config_write_reg_dword(hk, TEXT("pmf"), config->pmf, 0);
  524. wpa_config_write_reg_dword(hk, TEXT("external_sim"),
  525. config->external_sim, 0);
  526. return 0;
  527. }
  528. static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key)
  529. {
  530. HKEY nhk;
  531. int i, errors = 0;
  532. LONG ret;
  533. ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk);
  534. if (ret != ERROR_SUCCESS) {
  535. wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR
  536. "' for subkey deletion: error 0x%x (%d)", key,
  537. (unsigned int) ret, (int) GetLastError());
  538. return 0;
  539. }
  540. for (i = 0; ; i++) {
  541. TCHAR name[255];
  542. DWORD namelen;
  543. namelen = 255;
  544. ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
  545. NULL);
  546. if (ret == ERROR_NO_MORE_ITEMS)
  547. break;
  548. if (ret != ERROR_SUCCESS) {
  549. wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)",
  550. (unsigned int) ret, (int) GetLastError());
  551. break;
  552. }
  553. if (namelen >= 255)
  554. namelen = 255 - 1;
  555. name[namelen] = TEXT('\0');
  556. ret = RegDeleteKey(nhk, name);
  557. if (ret != ERROR_SUCCESS) {
  558. wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)",
  559. (unsigned int) ret, (int) GetLastError());
  560. errors++;
  561. }
  562. }
  563. RegCloseKey(nhk);
  564. return errors ? -1 : 0;
  565. }
  566. static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid)
  567. {
  568. char *value = wpa_config_get(ssid, field);
  569. if (value == NULL)
  570. return;
  571. wpa_config_write_reg_string(hk, field, value);
  572. os_free(value);
  573. }
  574. static void write_int(HKEY hk, const char *field, int value, int def)
  575. {
  576. char val[20];
  577. if (value == def)
  578. return;
  579. os_snprintf(val, sizeof(val), "%d", value);
  580. wpa_config_write_reg_string(hk, field, val);
  581. }
  582. static void write_bssid(HKEY hk, struct wpa_ssid *ssid)
  583. {
  584. char *value = wpa_config_get(ssid, "bssid");
  585. if (value == NULL)
  586. return;
  587. wpa_config_write_reg_string(hk, "bssid", value);
  588. os_free(value);
  589. }
  590. static void write_psk(HKEY hk, struct wpa_ssid *ssid)
  591. {
  592. char *value = wpa_config_get(ssid, "psk");
  593. if (value == NULL)
  594. return;
  595. wpa_config_write_reg_string(hk, "psk", value);
  596. os_free(value);
  597. }
  598. static void write_proto(HKEY hk, struct wpa_ssid *ssid)
  599. {
  600. char *value;
  601. if (ssid->proto == DEFAULT_PROTO)
  602. return;
  603. value = wpa_config_get(ssid, "proto");
  604. if (value == NULL)
  605. return;
  606. if (value[0])
  607. wpa_config_write_reg_string(hk, "proto", value);
  608. os_free(value);
  609. }
  610. static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid)
  611. {
  612. char *value;
  613. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  614. return;
  615. value = wpa_config_get(ssid, "key_mgmt");
  616. if (value == NULL)
  617. return;
  618. if (value[0])
  619. wpa_config_write_reg_string(hk, "key_mgmt", value);
  620. os_free(value);
  621. }
  622. static void write_pairwise(HKEY hk, struct wpa_ssid *ssid)
  623. {
  624. char *value;
  625. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  626. return;
  627. value = wpa_config_get(ssid, "pairwise");
  628. if (value == NULL)
  629. return;
  630. if (value[0])
  631. wpa_config_write_reg_string(hk, "pairwise", value);
  632. os_free(value);
  633. }
  634. static void write_group(HKEY hk, struct wpa_ssid *ssid)
  635. {
  636. char *value;
  637. if (ssid->group_cipher == DEFAULT_GROUP)
  638. return;
  639. value = wpa_config_get(ssid, "group");
  640. if (value == NULL)
  641. return;
  642. if (value[0])
  643. wpa_config_write_reg_string(hk, "group", value);
  644. os_free(value);
  645. }
  646. static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid)
  647. {
  648. char *value;
  649. if (ssid->auth_alg == 0)
  650. return;
  651. value = wpa_config_get(ssid, "auth_alg");
  652. if (value == NULL)
  653. return;
  654. if (value[0])
  655. wpa_config_write_reg_string(hk, "auth_alg", value);
  656. os_free(value);
  657. }
  658. #ifdef IEEE8021X_EAPOL
  659. static void write_eap(HKEY hk, struct wpa_ssid *ssid)
  660. {
  661. char *value;
  662. value = wpa_config_get(ssid, "eap");
  663. if (value == NULL)
  664. return;
  665. if (value[0])
  666. wpa_config_write_reg_string(hk, "eap", value);
  667. os_free(value);
  668. }
  669. #endif /* IEEE8021X_EAPOL */
  670. static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid)
  671. {
  672. char field[20], *value;
  673. os_snprintf(field, sizeof(field), "wep_key%d", idx);
  674. value = wpa_config_get(ssid, field);
  675. if (value) {
  676. wpa_config_write_reg_string(hk, field, value);
  677. os_free(value);
  678. }
  679. }
  680. static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id)
  681. {
  682. int i, errors = 0;
  683. HKEY nhk, netw;
  684. LONG ret;
  685. TCHAR name[5];
  686. ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk);
  687. if (ret != ERROR_SUCCESS) {
  688. wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key "
  689. "for subkey addition: error 0x%x (%d)",
  690. (unsigned int) ret, (int) GetLastError());
  691. return 0;
  692. }
  693. #ifdef UNICODE
  694. wsprintf(name, L"%04d", id);
  695. #else /* UNICODE */
  696. os_snprintf(name, sizeof(name), "%04d", id);
  697. #endif /* UNICODE */
  698. ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw,
  699. NULL);
  700. RegCloseKey(nhk);
  701. if (ret != ERROR_SUCCESS) {
  702. wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':"
  703. " error 0x%x (%d)",
  704. name, (unsigned int) ret, (int) GetLastError());
  705. return -1;
  706. }
  707. #define STR(t) write_str(netw, #t, ssid)
  708. #define INT(t) write_int(netw, #t, ssid->t, 0)
  709. #define INTe(t) write_int(netw, #t, ssid->eap.t, 0)
  710. #define INT_DEF(t, def) write_int(netw, #t, ssid->t, def)
  711. #define INT_DEFe(t, def) write_int(netw, #t, ssid->eap.t, def)
  712. STR(ssid);
  713. INT(scan_ssid);
  714. write_bssid(netw, ssid);
  715. write_psk(netw, ssid);
  716. STR(sae_password);
  717. write_proto(netw, ssid);
  718. write_key_mgmt(netw, ssid);
  719. write_pairwise(netw, ssid);
  720. write_group(netw, ssid);
  721. write_auth_alg(netw, ssid);
  722. #ifdef IEEE8021X_EAPOL
  723. write_eap(netw, ssid);
  724. STR(identity);
  725. STR(anonymous_identity);
  726. STR(imsi_identity);
  727. STR(password);
  728. STR(ca_cert);
  729. STR(ca_path);
  730. STR(client_cert);
  731. STR(private_key);
  732. STR(private_key_passwd);
  733. STR(dh_file);
  734. STR(subject_match);
  735. STR(altsubject_match);
  736. STR(ca_cert2);
  737. STR(ca_path2);
  738. STR(client_cert2);
  739. STR(private_key2);
  740. STR(private_key2_passwd);
  741. STR(dh_file2);
  742. STR(subject_match2);
  743. STR(altsubject_match2);
  744. STR(phase1);
  745. STR(phase2);
  746. STR(pcsc);
  747. STR(pin);
  748. STR(engine_id);
  749. STR(key_id);
  750. STR(cert_id);
  751. STR(ca_cert_id);
  752. STR(key2_id);
  753. STR(pin2);
  754. STR(engine2_id);
  755. STR(cert2_id);
  756. STR(ca_cert2_id);
  757. INTe(engine);
  758. INTe(engine2);
  759. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  760. #endif /* IEEE8021X_EAPOL */
  761. for (i = 0; i < 4; i++)
  762. write_wep_key(netw, i, ssid);
  763. INT(wep_tx_keyidx);
  764. INT(priority);
  765. #ifdef IEEE8021X_EAPOL
  766. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  767. STR(pac_file);
  768. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  769. #endif /* IEEE8021X_EAPOL */
  770. INT(mode);
  771. write_int(netw, "proactive_key_caching", ssid->proactive_key_caching,
  772. -1);
  773. INT(disabled);
  774. #ifdef CONFIG_IEEE80211W
  775. write_int(netw, "ieee80211w", ssid->ieee80211w,
  776. MGMT_FRAME_PROTECTION_DEFAULT);
  777. #endif /* CONFIG_IEEE80211W */
  778. STR(id_str);
  779. #ifdef CONFIG_HS20
  780. INT(update_identifier);
  781. #endif /* CONFIG_HS20 */
  782. INT(group_rekey);
  783. #undef STR
  784. #undef INT
  785. #undef INT_DEF
  786. RegCloseKey(netw);
  787. return errors ? -1 : 0;
  788. }
  789. static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob)
  790. {
  791. HKEY bhk;
  792. LONG ret;
  793. TCHAR *name;
  794. ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL,
  795. &bhk, NULL);
  796. if (ret != ERROR_SUCCESS) {
  797. wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: "
  798. "error 0x%x (%d)",
  799. (unsigned int) ret, (int) GetLastError());
  800. return -1;
  801. }
  802. name = wpa_strdup_tchar(blob->name);
  803. ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data,
  804. blob->len);
  805. if (ret != ERROR_SUCCESS) {
  806. wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': "
  807. "error 0x%x (%d)", blob->name, (unsigned int) ret,
  808. (int) GetLastError());
  809. RegCloseKey(bhk);
  810. os_free(name);
  811. return -1;
  812. }
  813. os_free(name);
  814. RegCloseKey(bhk);
  815. return 0;
  816. }
  817. int wpa_config_write(const char *name, struct wpa_config *config)
  818. {
  819. TCHAR buf[256];
  820. HKEY hk;
  821. LONG ret;
  822. int errors = 0;
  823. struct wpa_ssid *ssid;
  824. struct wpa_config_blob *blob;
  825. int id;
  826. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  827. #ifdef UNICODE
  828. _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
  829. #else /* UNICODE */
  830. os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
  831. #endif /* UNICODE */
  832. ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk);
  833. if (ret != ERROR_SUCCESS) {
  834. wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
  835. "configuration registry %s: error %d", buf,
  836. (int) GetLastError());
  837. return -1;
  838. }
  839. if (wpa_config_write_global(config, hk)) {
  840. wpa_printf(MSG_ERROR, "Failed to write global configuration "
  841. "data");
  842. errors++;
  843. }
  844. wpa_config_delete_subkeys(hk, TEXT("networks"));
  845. for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) {
  846. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  847. continue; /* do not save temporary WPS networks */
  848. if (wpa_config_write_network(hk, ssid, id))
  849. errors++;
  850. }
  851. RegDeleteKey(hk, TEXT("blobs"));
  852. for (blob = config->blobs; blob; blob = blob->next) {
  853. if (wpa_config_write_blob(hk, blob))
  854. errors++;
  855. }
  856. RegCloseKey(hk);
  857. wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully",
  858. name, errors ? "un" : "");
  859. return errors ? -1 : 0;
  860. }