config_file.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. struct global_parse_data {
  229. char *name;
  230. int (*parser)(const struct global_parse_data *data,
  231. struct wpa_config *config, int line, const char *value);
  232. void *param1, *param2, *param3;
  233. };
  234. static int wpa_config_parse_int(const struct global_parse_data *data,
  235. struct wpa_config *config, int line,
  236. const char *pos)
  237. {
  238. int *dst;
  239. dst = (int *) (((u8 *) config) + (long) data->param1);
  240. *dst = atoi(pos);
  241. wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
  242. return 0;
  243. }
  244. static int wpa_config_parse_str(const struct global_parse_data *data,
  245. struct wpa_config *config, int line,
  246. const char *pos)
  247. {
  248. size_t len;
  249. char **dst, *tmp;
  250. len = os_strlen(pos);
  251. if (data->param2 && len < (size_t) data->param2) {
  252. wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
  253. "min_len=%ld)", line, data->name,
  254. (unsigned long) len, (long) data->param2);
  255. return -1;
  256. }
  257. if (data->param3 && len > (size_t) data->param3) {
  258. wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
  259. "max_len=%ld)", line, data->name,
  260. (unsigned long) len, (long) data->param3);
  261. return -1;
  262. }
  263. tmp = os_strdup(pos);
  264. if (tmp == NULL)
  265. return -1;
  266. dst = (char **) (((u8 *) config) + (long) data->param1);
  267. os_free(*dst);
  268. *dst = tmp;
  269. wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
  270. return 0;
  271. }
  272. static int wpa_config_process_country(const struct global_parse_data *data,
  273. struct wpa_config *config, int line,
  274. const char *pos)
  275. {
  276. if (!pos[0] || !pos[1]) {
  277. wpa_printf(MSG_DEBUG, "Invalid country set");
  278. return -1;
  279. }
  280. config->country[0] = pos[0];
  281. config->country[1] = pos[1];
  282. wpa_printf(MSG_DEBUG, "country='%c%c'",
  283. config->country[0], config->country[1]);
  284. return 0;
  285. }
  286. static int wpa_config_process_eapol_version(
  287. const struct global_parse_data *data, struct wpa_config *config,
  288. int line, const char *pos)
  289. {
  290. config->eapol_version = atoi(pos);
  291. if (config->eapol_version < 1 || config->eapol_version > 2) {
  292. wpa_printf(MSG_ERROR, "Line %d: Invalid EAPOL version (%d): "
  293. "'%s'.", line, config->eapol_version, pos);
  294. return -1;
  295. }
  296. wpa_printf(MSG_DEBUG, "eapol_version=%d", config->eapol_version);
  297. return 0;
  298. }
  299. static int wpa_config_process_load_dynamic_eap(
  300. const struct global_parse_data *data, struct wpa_config *config,
  301. int line, const char *so)
  302. {
  303. int ret;
  304. wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
  305. ret = eap_peer_method_load(so);
  306. if (ret == -2) {
  307. wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
  308. "reloading.");
  309. } else if (ret) {
  310. wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
  311. "method '%s'.", line, so);
  312. return -1;
  313. }
  314. return 0;
  315. }
  316. #ifdef CONFIG_WPS
  317. static int wpa_config_process_uuid(const struct global_parse_data *data,
  318. struct wpa_config *config, int line,
  319. const char *pos)
  320. {
  321. char buf[40];
  322. if (uuid_str2bin(pos, config->uuid)) {
  323. wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
  324. return -1;
  325. }
  326. uuid_bin2str(config->uuid, buf, sizeof(buf));
  327. wpa_printf(MSG_DEBUG, "uuid=%s", buf);
  328. return 0;
  329. }
  330. static int wpa_config_process_os_version(const struct global_parse_data *data,
  331. struct wpa_config *config, int line,
  332. const char *pos)
  333. {
  334. if (hexstr2bin(pos, config->os_version, 4)) {
  335. wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
  336. return -1;
  337. }
  338. wpa_printf(MSG_DEBUG, "os_version=%08x",
  339. WPA_GET_BE32(config->os_version));
  340. return 0;
  341. }
  342. #endif /* CONFIG_WPS */
  343. #ifdef OFFSET
  344. #undef OFFSET
  345. #endif /* OFFSET */
  346. /* OFFSET: Get offset of a variable within the wpa_config structure */
  347. #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
  348. #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
  349. #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
  350. #define INT(f) #f, wpa_config_parse_int, OFFSET(f), NULL, NULL
  351. #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
  352. #define STR(f) _STR(f), NULL, NULL
  353. #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
  354. static const struct global_parse_data global_fields[] = {
  355. #ifdef CONFIG_CTRL_IFACE
  356. { STR(ctrl_interface) },
  357. { STR(ctrl_interface_group) } /* deprecated */,
  358. #endif /* CONFIG_CTRL_IFACE */
  359. { FUNC(eapol_version) },
  360. { INT(ap_scan) },
  361. { INT(fast_reauth) },
  362. #ifdef EAP_TLS_OPENSSL
  363. { STR(opensc_engine_path) },
  364. { STR(pkcs11_engine_path) },
  365. { STR(pkcs11_module_path) },
  366. #endif /* EAP_TLS_OPENSSL */
  367. { STR(driver_param) },
  368. { INT(dot11RSNAConfigPMKLifetime) },
  369. { INT(dot11RSNAConfigPMKReauthThreshold) },
  370. { INT(dot11RSNAConfigSATimeout) },
  371. #ifndef CONFIG_NO_CONFIG_WRITE
  372. { INT(update_config) },
  373. #endif /* CONFIG_NO_CONFIG_WRITE */
  374. { FUNC_NO_VAR(load_dynamic_eap) },
  375. #ifdef CONFIG_WPS
  376. { FUNC(uuid) },
  377. { STR_RANGE(device_name, 0, 32) },
  378. { STR_RANGE(manufacturer, 0, 64) },
  379. { STR_RANGE(model_name, 0, 32) },
  380. { STR_RANGE(model_number, 0, 32) },
  381. { STR_RANGE(serial_number, 0, 32) },
  382. { STR(device_type) },
  383. { FUNC(os_version) },
  384. #endif /* CONFIG_WPS */
  385. { FUNC(country) }
  386. };
  387. #undef FUNC
  388. #undef INT
  389. #undef _STR
  390. #undef STR
  391. #undef STR_RANGE
  392. #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
  393. static int wpa_config_process_global(struct wpa_config *config, char *pos,
  394. int line)
  395. {
  396. size_t i;
  397. int ret = 0;
  398. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  399. const struct global_parse_data *field = &global_fields[i];
  400. size_t flen = os_strlen(field->name);
  401. if (os_strncmp(pos, field->name, flen) != 0 ||
  402. pos[flen] != '=')
  403. continue;
  404. if (field->parser(field, config, line, pos + flen + 1)) {
  405. wpa_printf(MSG_ERROR, "Line %d: failed to "
  406. "parse '%s'.", line, pos);
  407. ret = -1;
  408. }
  409. break;
  410. }
  411. if (i == NUM_GLOBAL_FIELDS) {
  412. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  413. line, pos);
  414. ret = -1;
  415. }
  416. return ret;
  417. }
  418. struct wpa_config * wpa_config_read(const char *name)
  419. {
  420. FILE *f;
  421. char buf[256], *pos;
  422. int errors = 0, line = 0;
  423. struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
  424. struct wpa_config *config;
  425. int id = 0;
  426. config = wpa_config_alloc_empty(NULL, NULL);
  427. if (config == NULL)
  428. return NULL;
  429. wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
  430. f = fopen(name, "r");
  431. if (f == NULL) {
  432. os_free(config);
  433. return NULL;
  434. }
  435. while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
  436. if (os_strcmp(pos, "network={") == 0) {
  437. ssid = wpa_config_read_network(f, &line, id++);
  438. if (ssid == NULL) {
  439. wpa_printf(MSG_ERROR, "Line %d: failed to "
  440. "parse network block.", line);
  441. errors++;
  442. continue;
  443. }
  444. if (head == NULL) {
  445. head = tail = ssid;
  446. } else {
  447. tail->next = ssid;
  448. tail = ssid;
  449. }
  450. if (wpa_config_add_prio_network(config, ssid)) {
  451. wpa_printf(MSG_ERROR, "Line %d: failed to add "
  452. "network block to priority list.",
  453. line);
  454. errors++;
  455. continue;
  456. }
  457. #ifndef CONFIG_NO_CONFIG_BLOBS
  458. } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
  459. if (wpa_config_process_blob(config, f, &line, pos + 12)
  460. < 0) {
  461. errors++;
  462. continue;
  463. }
  464. #endif /* CONFIG_NO_CONFIG_BLOBS */
  465. } else if (wpa_config_process_global(config, pos, line) < 0) {
  466. wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
  467. "line '%s'.", line, pos);
  468. errors++;
  469. continue;
  470. }
  471. }
  472. fclose(f);
  473. config->ssid = head;
  474. wpa_config_debug_dump_networks(config);
  475. if (errors) {
  476. wpa_config_free(config);
  477. config = NULL;
  478. head = NULL;
  479. }
  480. return config;
  481. }
  482. #ifndef CONFIG_NO_CONFIG_WRITE
  483. static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
  484. {
  485. char *value = wpa_config_get(ssid, field);
  486. if (value == NULL)
  487. return;
  488. fprintf(f, "\t%s=%s\n", field, value);
  489. os_free(value);
  490. }
  491. static void write_int(FILE *f, const char *field, int value, int def)
  492. {
  493. if (value == def)
  494. return;
  495. fprintf(f, "\t%s=%d\n", field, value);
  496. }
  497. static void write_bssid(FILE *f, struct wpa_ssid *ssid)
  498. {
  499. char *value = wpa_config_get(ssid, "bssid");
  500. if (value == NULL)
  501. return;
  502. fprintf(f, "\tbssid=%s\n", value);
  503. os_free(value);
  504. }
  505. static void write_psk(FILE *f, struct wpa_ssid *ssid)
  506. {
  507. char *value = wpa_config_get(ssid, "psk");
  508. if (value == NULL)
  509. return;
  510. fprintf(f, "\tpsk=%s\n", value);
  511. os_free(value);
  512. }
  513. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  514. {
  515. char *value;
  516. if (ssid->proto == DEFAULT_PROTO)
  517. return;
  518. value = wpa_config_get(ssid, "proto");
  519. if (value == NULL)
  520. return;
  521. if (value[0])
  522. fprintf(f, "\tproto=%s\n", value);
  523. os_free(value);
  524. }
  525. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  526. {
  527. char *value;
  528. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  529. return;
  530. value = wpa_config_get(ssid, "key_mgmt");
  531. if (value == NULL)
  532. return;
  533. if (value[0])
  534. fprintf(f, "\tkey_mgmt=%s\n", value);
  535. os_free(value);
  536. }
  537. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  538. {
  539. char *value;
  540. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  541. return;
  542. value = wpa_config_get(ssid, "pairwise");
  543. if (value == NULL)
  544. return;
  545. if (value[0])
  546. fprintf(f, "\tpairwise=%s\n", value);
  547. os_free(value);
  548. }
  549. static void write_group(FILE *f, struct wpa_ssid *ssid)
  550. {
  551. char *value;
  552. if (ssid->group_cipher == DEFAULT_GROUP)
  553. return;
  554. value = wpa_config_get(ssid, "group");
  555. if (value == NULL)
  556. return;
  557. if (value[0])
  558. fprintf(f, "\tgroup=%s\n", value);
  559. os_free(value);
  560. }
  561. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  562. {
  563. char *value;
  564. if (ssid->auth_alg == 0)
  565. return;
  566. value = wpa_config_get(ssid, "auth_alg");
  567. if (value == NULL)
  568. return;
  569. if (value[0])
  570. fprintf(f, "\tauth_alg=%s\n", value);
  571. os_free(value);
  572. }
  573. #ifdef IEEE8021X_EAPOL
  574. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  575. {
  576. char *value;
  577. value = wpa_config_get(ssid, "eap");
  578. if (value == NULL)
  579. return;
  580. if (value[0])
  581. fprintf(f, "\teap=%s\n", value);
  582. os_free(value);
  583. }
  584. #endif /* IEEE8021X_EAPOL */
  585. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  586. {
  587. char field[20], *value;
  588. int res;
  589. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  590. if (res < 0 || (size_t) res >= sizeof(field))
  591. return;
  592. value = wpa_config_get(ssid, field);
  593. if (value) {
  594. fprintf(f, "\t%s=%s\n", field, value);
  595. os_free(value);
  596. }
  597. }
  598. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  599. {
  600. int i;
  601. #define STR(t) write_str(f, #t, ssid)
  602. #define INT(t) write_int(f, #t, ssid->t, 0)
  603. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  604. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  605. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  606. STR(ssid);
  607. INT(scan_ssid);
  608. write_bssid(f, ssid);
  609. write_psk(f, ssid);
  610. write_proto(f, ssid);
  611. write_key_mgmt(f, ssid);
  612. write_pairwise(f, ssid);
  613. write_group(f, ssid);
  614. write_auth_alg(f, ssid);
  615. #ifdef IEEE8021X_EAPOL
  616. write_eap(f, ssid);
  617. STR(identity);
  618. STR(anonymous_identity);
  619. STR(password);
  620. STR(ca_cert);
  621. STR(ca_path);
  622. STR(client_cert);
  623. STR(private_key);
  624. STR(private_key_passwd);
  625. STR(dh_file);
  626. STR(subject_match);
  627. STR(altsubject_match);
  628. STR(ca_cert2);
  629. STR(ca_path2);
  630. STR(client_cert2);
  631. STR(private_key2);
  632. STR(private_key2_passwd);
  633. STR(dh_file2);
  634. STR(subject_match2);
  635. STR(altsubject_match2);
  636. STR(phase1);
  637. STR(phase2);
  638. STR(pcsc);
  639. STR(pin);
  640. STR(engine_id);
  641. STR(key_id);
  642. STR(cert_id);
  643. STR(ca_cert_id);
  644. STR(key2_id);
  645. STR(pin2);
  646. STR(engine2_id);
  647. STR(cert2_id);
  648. STR(ca_cert2_id);
  649. INTe(engine);
  650. INTe(engine2);
  651. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  652. #endif /* IEEE8021X_EAPOL */
  653. for (i = 0; i < 4; i++)
  654. write_wep_key(f, i, ssid);
  655. INT(wep_tx_keyidx);
  656. INT(priority);
  657. #ifdef IEEE8021X_EAPOL
  658. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  659. STR(pac_file);
  660. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  661. #endif /* IEEE8021X_EAPOL */
  662. INT(mode);
  663. INT(proactive_key_caching);
  664. INT(disabled);
  665. INT(peerkey);
  666. #ifdef CONFIG_IEEE80211W
  667. INT(ieee80211w);
  668. #endif /* CONFIG_IEEE80211W */
  669. STR(id_str);
  670. #undef STR
  671. #undef INT
  672. #undef INT_DEF
  673. }
  674. #ifndef CONFIG_NO_CONFIG_BLOBS
  675. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  676. {
  677. unsigned char *encoded;
  678. encoded = base64_encode(blob->data, blob->len, NULL);
  679. if (encoded == NULL)
  680. return -1;
  681. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  682. os_free(encoded);
  683. return 0;
  684. }
  685. #endif /* CONFIG_NO_CONFIG_BLOBS */
  686. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  687. {
  688. #ifdef CONFIG_CTRL_IFACE
  689. if (config->ctrl_interface)
  690. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  691. if (config->ctrl_interface_group)
  692. fprintf(f, "ctrl_interface_group=%s\n",
  693. config->ctrl_interface_group);
  694. #endif /* CONFIG_CTRL_IFACE */
  695. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  696. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  697. if (config->ap_scan != DEFAULT_AP_SCAN)
  698. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  699. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  700. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  701. #ifdef EAP_TLS_OPENSSL
  702. if (config->opensc_engine_path)
  703. fprintf(f, "opensc_engine_path=%s\n",
  704. config->opensc_engine_path);
  705. if (config->pkcs11_engine_path)
  706. fprintf(f, "pkcs11_engine_path=%s\n",
  707. config->pkcs11_engine_path);
  708. if (config->pkcs11_module_path)
  709. fprintf(f, "pkcs11_module_path=%s\n",
  710. config->pkcs11_module_path);
  711. #endif /* EAP_TLS_OPENSSL */
  712. if (config->driver_param)
  713. fprintf(f, "driver_param=%s\n", config->driver_param);
  714. if (config->dot11RSNAConfigPMKLifetime)
  715. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  716. config->dot11RSNAConfigPMKLifetime);
  717. if (config->dot11RSNAConfigPMKReauthThreshold)
  718. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  719. config->dot11RSNAConfigPMKReauthThreshold);
  720. if (config->dot11RSNAConfigSATimeout)
  721. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  722. config->dot11RSNAConfigSATimeout);
  723. if (config->update_config)
  724. fprintf(f, "update_config=%d\n", config->update_config);
  725. #ifdef CONFIG_WPS
  726. if (is_nil_uuid(config->uuid)) {
  727. char buf[40];
  728. uuid_bin2str(config->uuid, buf, sizeof(buf));
  729. fprintf(f, "uuid=%s\n", buf);
  730. }
  731. if (config->device_name)
  732. fprintf(f, "device_name=%s\n", config->device_name);
  733. if (config->manufacturer)
  734. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  735. if (config->model_name)
  736. fprintf(f, "model_name=%s\n", config->model_name);
  737. if (config->model_number)
  738. fprintf(f, "model_number=%s\n", config->model_number);
  739. if (config->serial_number)
  740. fprintf(f, "serial_number=%s\n", config->serial_number);
  741. if (config->device_type)
  742. fprintf(f, "device_type=%s\n", config->device_type);
  743. if (config->os_version)
  744. fprintf(f, "os_version=%08x\n",
  745. WPA_GET_BE32(config->os_version));
  746. #endif /* CONFIG_WPS */
  747. if (config->country[0] && config->country[1]) {
  748. fprintf(f, "country=%c%c\n",
  749. config->country[0], config->country[1]);
  750. }
  751. }
  752. #endif /* CONFIG_NO_CONFIG_WRITE */
  753. int wpa_config_write(const char *name, struct wpa_config *config)
  754. {
  755. #ifndef CONFIG_NO_CONFIG_WRITE
  756. FILE *f;
  757. struct wpa_ssid *ssid;
  758. #ifndef CONFIG_NO_CONFIG_BLOBS
  759. struct wpa_config_blob *blob;
  760. #endif /* CONFIG_NO_CONFIG_BLOBS */
  761. int ret = 0;
  762. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  763. f = fopen(name, "w");
  764. if (f == NULL) {
  765. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  766. return -1;
  767. }
  768. wpa_config_write_global(f, config);
  769. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  770. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  771. continue; /* do not save temporary WPS networks */
  772. fprintf(f, "\nnetwork={\n");
  773. wpa_config_write_network(f, ssid);
  774. fprintf(f, "}\n");
  775. }
  776. #ifndef CONFIG_NO_CONFIG_BLOBS
  777. for (blob = config->blobs; blob; blob = blob->next) {
  778. ret = wpa_config_write_blob(f, blob);
  779. if (ret)
  780. break;
  781. }
  782. #endif /* CONFIG_NO_CONFIG_BLOBS */
  783. fclose(f);
  784. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  785. name, ret ? "un" : "");
  786. return ret;
  787. #else /* CONFIG_NO_CONFIG_WRITE */
  788. return -1;
  789. #endif /* CONFIG_NO_CONFIG_WRITE */
  790. }