config_file.c 22 KB

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