config_file.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. * WPA Supplicant / Configuration backend: text file
  3. * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. *
  14. * This file implements a configuration backend for text files. All the
  15. * configuration information is stored in a text file that uses a format
  16. * described in the sample configuration file, wpa_supplicant.conf.
  17. */
  18. #include "includes.h"
  19. #include "common.h"
  20. #include "config.h"
  21. #include "base64.h"
  22. #include "uuid.h"
  23. #include "eap_peer/eap_methods.h"
  24. /**
  25. * wpa_config_get_line - Read the next configuration file line
  26. * @s: Buffer for the line
  27. * @size: The buffer length
  28. * @stream: File stream to read from
  29. * @line: Pointer to a variable storing the file line number
  30. * @_pos: Buffer for the pointer to the beginning of data on the text line or
  31. * %NULL if not needed (returned value used instead)
  32. * Returns: Pointer to the beginning of data on the text line or %NULL if no
  33. * more text lines are available.
  34. *
  35. * This function reads the next non-empty line from the configuration file and
  36. * removes comments. The returned string is guaranteed to be null-terminated.
  37. */
  38. static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
  39. char **_pos)
  40. {
  41. char *pos, *end, *sstart;
  42. while (fgets(s, size, stream)) {
  43. (*line)++;
  44. s[size - 1] = '\0';
  45. pos = s;
  46. /* Skip white space from the beginning of line. */
  47. while (*pos == ' ' || *pos == '\t' || *pos == '\r')
  48. pos++;
  49. /* Skip comment lines and empty lines */
  50. if (*pos == '#' || *pos == '\n' || *pos == '\0')
  51. continue;
  52. /*
  53. * Remove # comments unless they are within a double quoted
  54. * string.
  55. */
  56. sstart = os_strchr(pos, '"');
  57. if (sstart)
  58. sstart = os_strrchr(sstart + 1, '"');
  59. if (!sstart)
  60. sstart = pos;
  61. end = os_strchr(sstart, '#');
  62. if (end)
  63. *end-- = '\0';
  64. else
  65. end = pos + os_strlen(pos) - 1;
  66. /* Remove trailing white space. */
  67. while (end > pos &&
  68. (*end == '\n' || *end == ' ' || *end == '\t' ||
  69. *end == '\r'))
  70. *end-- = '\0';
  71. if (*pos == '\0')
  72. continue;
  73. if (_pos)
  74. *_pos = pos;
  75. return pos;
  76. }
  77. if (_pos)
  78. *_pos = NULL;
  79. return NULL;
  80. }
  81. static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
  82. {
  83. int errors = 0;
  84. if (ssid->passphrase) {
  85. if (ssid->psk_set) {
  86. wpa_printf(MSG_ERROR, "Line %d: both PSK and "
  87. "passphrase configured.", line);
  88. errors++;
  89. }
  90. wpa_config_update_psk(ssid);
  91. }
  92. if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
  93. WPA_KEY_MGMT_PSK_SHA256)) &&
  94. !ssid->psk_set) {
  95. wpa_printf(MSG_ERROR, "Line %d: WPA-PSK accepted for key "
  96. "management, but no PSK configured.", line);
  97. errors++;
  98. }
  99. if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
  100. !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  101. !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
  102. /* Group cipher cannot be stronger than the pairwise cipher. */
  103. wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
  104. " list since it was not allowed for pairwise "
  105. "cipher", line);
  106. ssid->group_cipher &= ~WPA_CIPHER_CCMP;
  107. }
  108. return errors;
  109. }
  110. static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
  111. {
  112. struct wpa_ssid *ssid;
  113. int errors = 0, end = 0;
  114. char buf[256], *pos, *pos2;
  115. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
  116. *line);
  117. ssid = os_zalloc(sizeof(*ssid));
  118. if (ssid == NULL)
  119. return NULL;
  120. ssid->id = id;
  121. wpa_config_set_network_defaults(ssid);
  122. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  123. if (os_strcmp(pos, "}") == 0) {
  124. end = 1;
  125. break;
  126. }
  127. pos2 = os_strchr(pos, '=');
  128. if (pos2 == NULL) {
  129. wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
  130. "'%s'.", *line, pos);
  131. errors++;
  132. continue;
  133. }
  134. *pos2++ = '\0';
  135. if (*pos2 == '"') {
  136. if (os_strchr(pos2 + 1, '"') == NULL) {
  137. wpa_printf(MSG_ERROR, "Line %d: invalid "
  138. "quotation '%s'.", *line, pos2);
  139. errors++;
  140. continue;
  141. }
  142. }
  143. if (wpa_config_set(ssid, pos, pos2, *line) < 0)
  144. errors++;
  145. }
  146. if (!end) {
  147. wpa_printf(MSG_ERROR, "Line %d: network block was not "
  148. "terminated properly.", *line);
  149. errors++;
  150. }
  151. errors += wpa_config_validate_network(ssid, *line);
  152. if (errors) {
  153. wpa_config_free_ssid(ssid);
  154. ssid = NULL;
  155. }
  156. return ssid;
  157. }
  158. #ifndef CONFIG_NO_CONFIG_BLOBS
  159. static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
  160. const char *name)
  161. {
  162. struct wpa_config_blob *blob;
  163. char buf[256], *pos;
  164. unsigned char *encoded = NULL, *nencoded;
  165. int end = 0;
  166. size_t encoded_len = 0, len;
  167. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
  168. *line, name);
  169. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  170. if (os_strcmp(pos, "}") == 0) {
  171. end = 1;
  172. break;
  173. }
  174. len = os_strlen(pos);
  175. nencoded = os_realloc(encoded, encoded_len + len);
  176. if (nencoded == NULL) {
  177. wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
  178. "blob", *line);
  179. os_free(encoded);
  180. return NULL;
  181. }
  182. encoded = nencoded;
  183. os_memcpy(encoded + encoded_len, pos, len);
  184. encoded_len += len;
  185. }
  186. if (!end) {
  187. wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
  188. "properly", *line);
  189. os_free(encoded);
  190. return NULL;
  191. }
  192. blob = os_zalloc(sizeof(*blob));
  193. if (blob == NULL) {
  194. os_free(encoded);
  195. return NULL;
  196. }
  197. blob->name = os_strdup(name);
  198. blob->data = base64_decode(encoded, encoded_len, &blob->len);
  199. os_free(encoded);
  200. if (blob->name == NULL || blob->data == NULL) {
  201. wpa_config_free_blob(blob);
  202. return NULL;
  203. }
  204. return blob;
  205. }
  206. static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
  207. int *line, char *bname)
  208. {
  209. char *name_end;
  210. struct wpa_config_blob *blob;
  211. name_end = os_strchr(bname, '=');
  212. if (name_end == NULL) {
  213. wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
  214. *line);
  215. return -1;
  216. }
  217. *name_end = '\0';
  218. blob = wpa_config_read_blob(f, line, bname);
  219. if (blob == NULL) {
  220. wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
  221. *line, bname);
  222. return -1;
  223. }
  224. wpa_config_set_blob(config, blob);
  225. return 0;
  226. }
  227. #endif /* CONFIG_NO_CONFIG_BLOBS */
  228. #ifdef CONFIG_CTRL_IFACE
  229. static int wpa_config_process_ctrl_interface(struct wpa_config *config,
  230. char *pos)
  231. {
  232. os_free(config->ctrl_interface);
  233. config->ctrl_interface = os_strdup(pos);
  234. wpa_printf(MSG_DEBUG, "ctrl_interface='%s'", config->ctrl_interface);
  235. return 0;
  236. }
  237. static int wpa_config_process_ctrl_interface_group(struct wpa_config *config,
  238. char *pos)
  239. {
  240. os_free(config->ctrl_interface_group);
  241. config->ctrl_interface_group = os_strdup(pos);
  242. wpa_printf(MSG_DEBUG, "ctrl_interface_group='%s' (DEPRECATED)",
  243. config->ctrl_interface_group);
  244. return 0;
  245. }
  246. #endif /* CONFIG_CTRL_IFACE */
  247. static int wpa_config_process_eapol_version(struct wpa_config *config,
  248. int line, char *pos)
  249. {
  250. config->eapol_version = atoi(pos);
  251. if (config->eapol_version < 1 || config->eapol_version > 2) {
  252. wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): "
  253. "'%s'.", line, config->eapol_version, pos);
  254. return -1;
  255. }
  256. wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version);
  257. return 0;
  258. }
  259. static int wpa_config_process_ap_scan(struct wpa_config *config, char *pos)
  260. {
  261. config->ap_scan = atoi(pos);
  262. wpa_printf(MSG_DEBUG, "ap_scan=%d", config->ap_scan);
  263. return 0;
  264. }
  265. static int wpa_config_process_fast_reauth(struct wpa_config *config, char *pos)
  266. {
  267. config->fast_reauth = atoi(pos);
  268. wpa_printf(MSG_DEBUG, "fast_reauth=%d", config->fast_reauth);
  269. return 0;
  270. }
  271. #ifdef EAP_TLS_OPENSSL
  272. static int wpa_config_process_opensc_engine_path(struct wpa_config *config,
  273. char *pos)
  274. {
  275. os_free(config->opensc_engine_path);
  276. config->opensc_engine_path = os_strdup(pos);
  277. wpa_printf(MSG_DEBUG, "opensc_engine_path='%s'",
  278. config->opensc_engine_path);
  279. return 0;
  280. }
  281. static int wpa_config_process_pkcs11_engine_path(struct wpa_config *config,
  282. char *pos)
  283. {
  284. os_free(config->pkcs11_engine_path);
  285. config->pkcs11_engine_path = os_strdup(pos);
  286. wpa_printf(MSG_DEBUG, "pkcs11_engine_path='%s'",
  287. config->pkcs11_engine_path);
  288. return 0;
  289. }
  290. static int wpa_config_process_pkcs11_module_path(struct wpa_config *config,
  291. char *pos)
  292. {
  293. os_free(config->pkcs11_module_path);
  294. config->pkcs11_module_path = os_strdup(pos);
  295. wpa_printf(MSG_DEBUG, "pkcs11_module_path='%s'",
  296. config->pkcs11_module_path);
  297. return 0;
  298. }
  299. #endif /* EAP_TLS_OPENSSL */
  300. static int wpa_config_process_driver_param(struct wpa_config *config,
  301. char *pos)
  302. {
  303. os_free(config->driver_param);
  304. config->driver_param = os_strdup(pos);
  305. wpa_printf(MSG_DEBUG, "driver_param='%s'", config->driver_param);
  306. return 0;
  307. }
  308. static int wpa_config_process_pmk_lifetime(struct wpa_config *config,
  309. char *pos)
  310. {
  311. config->dot11RSNAConfigPMKLifetime = atoi(pos);
  312. wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKLifetime=%d",
  313. config->dot11RSNAConfigPMKLifetime);
  314. return 0;
  315. }
  316. static int wpa_config_process_pmk_reauth_threshold(struct wpa_config *config,
  317. char *pos)
  318. {
  319. config->dot11RSNAConfigPMKReauthThreshold = atoi(pos);
  320. wpa_printf(MSG_DEBUG, "dot11RSNAConfigPMKReauthThreshold=%d",
  321. config->dot11RSNAConfigPMKReauthThreshold);
  322. return 0;
  323. }
  324. static int wpa_config_process_sa_timeout(struct wpa_config *config, char *pos)
  325. {
  326. config->dot11RSNAConfigSATimeout = atoi(pos);
  327. wpa_printf(MSG_DEBUG, "dot11RSNAConfigSATimeout=%d",
  328. config->dot11RSNAConfigSATimeout);
  329. return 0;
  330. }
  331. #ifndef CONFIG_NO_CONFIG_WRITE
  332. static int wpa_config_process_update_config(struct wpa_config *config,
  333. char *pos)
  334. {
  335. config->update_config = atoi(pos);
  336. wpa_printf(MSG_DEBUG, "update_config=%d", config->update_config);
  337. return 0;
  338. }
  339. #endif /* CONFIG_NO_CONFIG_WRITE */
  340. static int wpa_config_process_load_dynamic_eap(int line, char *so)
  341. {
  342. int ret;
  343. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  344. ret = eap_peer_method_load(so);
  345. if (ret == -2) {
  346. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  347. "reloading.");
  348. } else if (ret) {
  349. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  350. "method '%s'.", line, so);
  351. return -1;
  352. }
  353. return 0;
  354. }
  355. #ifdef CONFIG_WPS
  356. static int wpa_config_process_uuid(struct wpa_config *config, int line,
  357. char *pos)
  358. {
  359. char buf[40];
  360. if (uuid_str2bin(pos, config->uuid)) {
  361. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  362. return -1;
  363. }
  364. uuid_bin2str(config->uuid, buf, sizeof(buf));
  365. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  366. return 0;
  367. }
  368. #endif /* CONFIG_WPS */
  369. static int wpa_config_process_global(struct wpa_config *config, char *pos,
  370. int line)
  371. {
  372. #ifdef CONFIG_CTRL_IFACE
  373. if (os_strncmp(pos, "ctrl_interface=", 15) == 0)
  374. return wpa_config_process_ctrl_interface(config, pos + 15);
  375. if (os_strncmp(pos, "ctrl_interface_group=", 21) == 0)
  376. return wpa_config_process_ctrl_interface_group(config,
  377. pos + 21);
  378. #endif /* CONFIG_CTRL_IFACE */
  379. if (os_strncmp(pos, "eapol_version=", 14) == 0)
  380. return wpa_config_process_eapol_version(config, line,
  381. pos + 14);
  382. if (os_strncmp(pos, "ap_scan=", 8) == 0)
  383. return wpa_config_process_ap_scan(config, pos + 8);
  384. if (os_strncmp(pos, "fast_reauth=", 12) == 0)
  385. return wpa_config_process_fast_reauth(config, pos + 12);
  386. #ifdef EAP_TLS_OPENSSL
  387. if (os_strncmp(pos, "opensc_engine_path=", 19) == 0)
  388. return wpa_config_process_opensc_engine_path(config, pos + 19);
  389. if (os_strncmp(pos, "pkcs11_engine_path=", 19) == 0)
  390. return wpa_config_process_pkcs11_engine_path(config, pos + 19);
  391. if (os_strncmp(pos, "pkcs11_module_path=", 19) == 0)
  392. return wpa_config_process_pkcs11_module_path(config, pos + 19);
  393. #endif /* EAP_TLS_OPENSSL */
  394. if (os_strncmp(pos, "driver_param=", 13) == 0)
  395. return wpa_config_process_driver_param(config, pos + 13);
  396. if (os_strncmp(pos, "dot11RSNAConfigPMKLifetime=", 27) == 0)
  397. return wpa_config_process_pmk_lifetime(config, pos + 27);
  398. if (os_strncmp(pos, "dot11RSNAConfigPMKReauthThreshold=", 34) == 0)
  399. return wpa_config_process_pmk_reauth_threshold(config,
  400. pos + 34);
  401. if (os_strncmp(pos, "dot11RSNAConfigSATimeout=", 25) == 0)
  402. return wpa_config_process_sa_timeout(config, pos + 25);
  403. #ifndef CONFIG_NO_CONFIG_WRITE
  404. if (os_strncmp(pos, "update_config=", 14) == 0)
  405. return wpa_config_process_update_config(config, pos + 14);
  406. #endif /* CONFIG_NO_CONFIG_WRITE */
  407. if (os_strncmp(pos, "load_dynamic_eap=", 17) == 0)
  408. return wpa_config_process_load_dynamic_eap(line, pos + 17);
  409. #ifdef CONFIG_WPS
  410. if (os_strncmp(pos, "uuid=", 5) == 0)
  411. return wpa_config_process_uuid(config, line, pos + 5);
  412. #endif /* CONFIG_WPS */
  413. return -1;
  414. }
  415. struct wpa_config * wpa_config_read(const char *name)
  416. {
  417. FILE *f;
  418. char buf[256], *pos;
  419. int errors = 0, line = 0;
  420. struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
  421. struct wpa_config *config;
  422. int id = 0;
  423. config = wpa_config_alloc_empty(NULL, NULL);
  424. if (config == NULL)
  425. return NULL;
  426. wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
  427. f = fopen(name, "r");
  428. if (f == NULL) {
  429. os_free(config);
  430. return NULL;
  431. }
  432. while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
  433. if (os_strcmp(pos, "network={") == 0) {
  434. ssid = wpa_config_read_network(f, &line, id++);
  435. if (ssid == NULL) {
  436. wpa_printf(MSG_ERROR, "Line %d: failed to "
  437. "parse network block.", line);
  438. errors++;
  439. continue;
  440. }
  441. if (head == NULL) {
  442. head = tail = ssid;
  443. } else {
  444. tail->next = ssid;
  445. tail = ssid;
  446. }
  447. if (wpa_config_add_prio_network(config, ssid)) {
  448. wpa_printf(MSG_ERROR, "Line %d: failed to add "
  449. "network block to priority list.",
  450. line);
  451. errors++;
  452. continue;
  453. }
  454. #ifndef CONFIG_NO_CONFIG_BLOBS
  455. } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
  456. if (wpa_config_process_blob(config, f, &line, pos + 12)
  457. < 0) {
  458. errors++;
  459. continue;
  460. }
  461. #endif /* CONFIG_NO_CONFIG_BLOBS */
  462. } else if (wpa_config_process_global(config, pos, line) < 0) {
  463. wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
  464. "line '%s'.", line, pos);
  465. errors++;
  466. continue;
  467. }
  468. }
  469. fclose(f);
  470. config->ssid = head;
  471. wpa_config_debug_dump_networks(config);
  472. if (errors) {
  473. wpa_config_free(config);
  474. config = NULL;
  475. head = NULL;
  476. }
  477. return config;
  478. }
  479. #ifndef CONFIG_NO_CONFIG_WRITE
  480. static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
  481. {
  482. char *value = wpa_config_get(ssid, field);
  483. if (value == NULL)
  484. return;
  485. fprintf(f, "\t%s=%s\n", field, value);
  486. os_free(value);
  487. }
  488. static void write_int(FILE *f, const char *field, int value, int def)
  489. {
  490. if (value == def)
  491. return;
  492. fprintf(f, "\t%s=%d\n", field, value);
  493. }
  494. static void write_bssid(FILE *f, struct wpa_ssid *ssid)
  495. {
  496. char *value = wpa_config_get(ssid, "bssid");
  497. if (value == NULL)
  498. return;
  499. fprintf(f, "\tbssid=%s\n", value);
  500. os_free(value);
  501. }
  502. static void write_psk(FILE *f, struct wpa_ssid *ssid)
  503. {
  504. char *value = wpa_config_get(ssid, "psk");
  505. if (value == NULL)
  506. return;
  507. fprintf(f, "\tpsk=%s\n", value);
  508. os_free(value);
  509. }
  510. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  511. {
  512. char *value;
  513. if (ssid->proto == DEFAULT_PROTO)
  514. return;
  515. value = wpa_config_get(ssid, "proto");
  516. if (value == NULL)
  517. return;
  518. if (value[0])
  519. fprintf(f, "\tproto=%s\n", value);
  520. os_free(value);
  521. }
  522. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  523. {
  524. char *value;
  525. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  526. return;
  527. value = wpa_config_get(ssid, "key_mgmt");
  528. if (value == NULL)
  529. return;
  530. if (value[0])
  531. fprintf(f, "\tkey_mgmt=%s\n", value);
  532. os_free(value);
  533. }
  534. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  535. {
  536. char *value;
  537. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  538. return;
  539. value = wpa_config_get(ssid, "pairwise");
  540. if (value == NULL)
  541. return;
  542. if (value[0])
  543. fprintf(f, "\tpairwise=%s\n", value);
  544. os_free(value);
  545. }
  546. static void write_group(FILE *f, struct wpa_ssid *ssid)
  547. {
  548. char *value;
  549. if (ssid->group_cipher == DEFAULT_GROUP)
  550. return;
  551. value = wpa_config_get(ssid, "group");
  552. if (value == NULL)
  553. return;
  554. if (value[0])
  555. fprintf(f, "\tgroup=%s\n", value);
  556. os_free(value);
  557. }
  558. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  559. {
  560. char *value;
  561. if (ssid->auth_alg == 0)
  562. return;
  563. value = wpa_config_get(ssid, "auth_alg");
  564. if (value == NULL)
  565. return;
  566. if (value[0])
  567. fprintf(f, "\tauth_alg=%s\n", value);
  568. os_free(value);
  569. }
  570. #ifdef IEEE8021X_EAPOL
  571. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  572. {
  573. char *value;
  574. value = wpa_config_get(ssid, "eap");
  575. if (value == NULL)
  576. return;
  577. if (value[0])
  578. fprintf(f, "\teap=%s\n", value);
  579. os_free(value);
  580. }
  581. #endif /* IEEE8021X_EAPOL */
  582. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  583. {
  584. char field[20], *value;
  585. int res;
  586. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  587. if (res < 0 || (size_t) res >= sizeof(field))
  588. return;
  589. value = wpa_config_get(ssid, field);
  590. if (value) {
  591. fprintf(f, "\t%s=%s\n", field, value);
  592. os_free(value);
  593. }
  594. }
  595. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  596. {
  597. int i;
  598. #define STR(t) write_str(f, #t, ssid)
  599. #define INT(t) write_int(f, #t, ssid->t, 0)
  600. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  601. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  602. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  603. STR(ssid);
  604. INT(scan_ssid);
  605. write_bssid(f, ssid);
  606. write_psk(f, ssid);
  607. write_proto(f, ssid);
  608. write_key_mgmt(f, ssid);
  609. write_pairwise(f, ssid);
  610. write_group(f, ssid);
  611. write_auth_alg(f, ssid);
  612. #ifdef IEEE8021X_EAPOL
  613. write_eap(f, ssid);
  614. STR(identity);
  615. STR(anonymous_identity);
  616. STR(password);
  617. STR(ca_cert);
  618. STR(ca_path);
  619. STR(client_cert);
  620. STR(private_key);
  621. STR(private_key_passwd);
  622. STR(dh_file);
  623. STR(subject_match);
  624. STR(altsubject_match);
  625. STR(ca_cert2);
  626. STR(ca_path2);
  627. STR(client_cert2);
  628. STR(private_key2);
  629. STR(private_key2_passwd);
  630. STR(dh_file2);
  631. STR(subject_match2);
  632. STR(altsubject_match2);
  633. STR(phase1);
  634. STR(phase2);
  635. STR(pcsc);
  636. STR(pin);
  637. STR(engine_id);
  638. STR(key_id);
  639. STR(cert_id);
  640. STR(ca_cert_id);
  641. STR(key2_id);
  642. STR(pin2);
  643. STR(engine2_id);
  644. STR(cert2_id);
  645. STR(ca_cert2_id);
  646. INTe(engine);
  647. INTe(engine2);
  648. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  649. #endif /* IEEE8021X_EAPOL */
  650. for (i = 0; i < 4; i++)
  651. write_wep_key(f, i, ssid);
  652. INT(wep_tx_keyidx);
  653. INT(priority);
  654. #ifdef IEEE8021X_EAPOL
  655. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  656. STR(pac_file);
  657. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  658. #endif /* IEEE8021X_EAPOL */
  659. INT(mode);
  660. INT(proactive_key_caching);
  661. INT(disabled);
  662. INT(peerkey);
  663. #ifdef CONFIG_IEEE80211W
  664. INT(ieee80211w);
  665. #endif /* CONFIG_IEEE80211W */
  666. STR(id_str);
  667. #undef STR
  668. #undef INT
  669. #undef INT_DEF
  670. }
  671. #ifndef CONFIG_NO_CONFIG_BLOBS
  672. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  673. {
  674. unsigned char *encoded;
  675. encoded = base64_encode(blob->data, blob->len, NULL);
  676. if (encoded == NULL)
  677. return -1;
  678. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  679. os_free(encoded);
  680. return 0;
  681. }
  682. #endif /* CONFIG_NO_CONFIG_BLOBS */
  683. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  684. {
  685. #ifdef CONFIG_CTRL_IFACE
  686. if (config->ctrl_interface)
  687. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  688. if (config->ctrl_interface_group)
  689. fprintf(f, "ctrl_interface_group=%s\n",
  690. config->ctrl_interface_group);
  691. #endif /* CONFIG_CTRL_IFACE */
  692. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  693. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  694. if (config->ap_scan != DEFAULT_AP_SCAN)
  695. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  696. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  697. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  698. #ifdef EAP_TLS_OPENSSL
  699. if (config->opensc_engine_path)
  700. fprintf(f, "opensc_engine_path=%s\n",
  701. config->opensc_engine_path);
  702. if (config->pkcs11_engine_path)
  703. fprintf(f, "pkcs11_engine_path=%s\n",
  704. config->pkcs11_engine_path);
  705. if (config->pkcs11_module_path)
  706. fprintf(f, "pkcs11_module_path=%s\n",
  707. config->pkcs11_module_path);
  708. #endif /* EAP_TLS_OPENSSL */
  709. if (config->driver_param)
  710. fprintf(f, "driver_param=%s\n", config->driver_param);
  711. if (config->dot11RSNAConfigPMKLifetime)
  712. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  713. config->dot11RSNAConfigPMKLifetime);
  714. if (config->dot11RSNAConfigPMKReauthThreshold)
  715. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  716. config->dot11RSNAConfigPMKReauthThreshold);
  717. if (config->dot11RSNAConfigSATimeout)
  718. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  719. config->dot11RSNAConfigSATimeout);
  720. if (config->update_config)
  721. fprintf(f, "update_config=%d\n", config->update_config);
  722. #ifdef CONFIG_WPS
  723. if (is_nil_uuid(config->uuid)) {
  724. char buf[40];
  725. uuid_bin2str(config->uuid, buf, sizeof(buf));
  726. fprintf(f, "uuid=%s\n", buf);
  727. }
  728. #endif /* CONFIG_WPS */
  729. }
  730. #endif /* CONFIG_NO_CONFIG_WRITE */
  731. int wpa_config_write(const char *name, struct wpa_config *config)
  732. {
  733. #ifndef CONFIG_NO_CONFIG_WRITE
  734. FILE *f;
  735. struct wpa_ssid *ssid;
  736. #ifndef CONFIG_NO_CONFIG_BLOBS
  737. struct wpa_config_blob *blob;
  738. #endif /* CONFIG_NO_CONFIG_BLOBS */
  739. int ret = 0;
  740. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  741. f = fopen(name, "w");
  742. if (f == NULL) {
  743. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  744. return -1;
  745. }
  746. wpa_config_write_global(f, config);
  747. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  748. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  749. continue; /* do not save temporary WPS networks */
  750. fprintf(f, "\nnetwork={\n");
  751. wpa_config_write_network(f, ssid);
  752. fprintf(f, "}\n");
  753. }
  754. #ifndef CONFIG_NO_CONFIG_BLOBS
  755. for (blob = config->blobs; blob; blob = blob->next) {
  756. ret = wpa_config_write_blob(f, blob);
  757. if (ret)
  758. break;
  759. }
  760. #endif /* CONFIG_NO_CONFIG_BLOBS */
  761. fclose(f);
  762. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  763. name, ret ? "un" : "");
  764. return ret;
  765. #else /* CONFIG_NO_CONFIG_WRITE */
  766. return -1;
  767. #endif /* CONFIG_NO_CONFIG_WRITE */
  768. }