config_winreg.c 24 KB

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