config_file.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. { STR(opensc_engine_path) },
  366. { STR(pkcs11_engine_path) },
  367. { STR(pkcs11_module_path) },
  368. { STR(driver_param) },
  369. { INT(dot11RSNAConfigPMKLifetime) },
  370. { INT(dot11RSNAConfigPMKReauthThreshold) },
  371. { INT(dot11RSNAConfigSATimeout) },
  372. #ifndef CONFIG_NO_CONFIG_WRITE
  373. { INT(update_config) },
  374. #endif /* CONFIG_NO_CONFIG_WRITE */
  375. { FUNC_NO_VAR(load_dynamic_eap) },
  376. #ifdef CONFIG_WPS
  377. { FUNC(uuid) },
  378. { STR_RANGE(device_name, 0, 32) },
  379. { STR_RANGE(manufacturer, 0, 64) },
  380. { STR_RANGE(model_name, 0, 32) },
  381. { STR_RANGE(model_number, 0, 32) },
  382. { STR_RANGE(serial_number, 0, 32) },
  383. { STR(device_type) },
  384. { FUNC(os_version) },
  385. { STR(config_methods) },
  386. { INT_RANGE(wps_cred_processing, 0, 2) },
  387. #endif /* CONFIG_WPS */
  388. { FUNC(country) },
  389. { INT(bss_max_count) },
  390. { INT_RANGE(filter_ssids, 0, 1) }
  391. };
  392. #undef FUNC
  393. #undef _INT
  394. #undef INT
  395. #undef INT_RANGE
  396. #undef _STR
  397. #undef STR
  398. #undef STR_RANGE
  399. #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
  400. static int wpa_config_process_global(struct wpa_config *config, char *pos,
  401. int line)
  402. {
  403. size_t i;
  404. int ret = 0;
  405. for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
  406. const struct global_parse_data *field = &global_fields[i];
  407. size_t flen = os_strlen(field->name);
  408. if (os_strncmp(pos, field->name, flen) != 0 ||
  409. pos[flen] != '=')
  410. continue;
  411. if (field->parser(field, config, line, pos + flen + 1)) {
  412. wpa_printf(MSG_ERROR, "Line %d: failed to "
  413. "parse '%s'.", line, pos);
  414. ret = -1;
  415. }
  416. break;
  417. }
  418. if (i == NUM_GLOBAL_FIELDS) {
  419. wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
  420. line, pos);
  421. ret = -1;
  422. }
  423. return ret;
  424. }
  425. struct wpa_config * wpa_config_read(const char *name)
  426. {
  427. FILE *f;
  428. char buf[256], *pos;
  429. int errors = 0, line = 0;
  430. struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
  431. struct wpa_config *config;
  432. int id = 0;
  433. config = wpa_config_alloc_empty(NULL, NULL);
  434. if (config == NULL)
  435. return NULL;
  436. wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
  437. f = fopen(name, "r");
  438. if (f == NULL) {
  439. os_free(config);
  440. return NULL;
  441. }
  442. while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
  443. if (os_strcmp(pos, "network={") == 0) {
  444. ssid = wpa_config_read_network(f, &line, id++);
  445. if (ssid == NULL) {
  446. wpa_printf(MSG_ERROR, "Line %d: failed to "
  447. "parse network block.", line);
  448. errors++;
  449. continue;
  450. }
  451. if (head == NULL) {
  452. head = tail = ssid;
  453. } else {
  454. tail->next = ssid;
  455. tail = ssid;
  456. }
  457. if (wpa_config_add_prio_network(config, ssid)) {
  458. wpa_printf(MSG_ERROR, "Line %d: failed to add "
  459. "network block to priority list.",
  460. line);
  461. errors++;
  462. continue;
  463. }
  464. #ifndef CONFIG_NO_CONFIG_BLOBS
  465. } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
  466. if (wpa_config_process_blob(config, f, &line, pos + 12)
  467. < 0) {
  468. errors++;
  469. continue;
  470. }
  471. #endif /* CONFIG_NO_CONFIG_BLOBS */
  472. } else if (wpa_config_process_global(config, pos, line) < 0) {
  473. wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
  474. "line '%s'.", line, pos);
  475. errors++;
  476. continue;
  477. }
  478. }
  479. fclose(f);
  480. config->ssid = head;
  481. wpa_config_debug_dump_networks(config);
  482. if (errors) {
  483. wpa_config_free(config);
  484. config = NULL;
  485. head = NULL;
  486. }
  487. return config;
  488. }
  489. #ifndef CONFIG_NO_CONFIG_WRITE
  490. static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
  491. {
  492. char *value = wpa_config_get(ssid, field);
  493. if (value == NULL)
  494. return;
  495. fprintf(f, "\t%s=%s\n", field, value);
  496. os_free(value);
  497. }
  498. static void write_int(FILE *f, const char *field, int value, int def)
  499. {
  500. if (value == def)
  501. return;
  502. fprintf(f, "\t%s=%d\n", field, value);
  503. }
  504. static void write_bssid(FILE *f, struct wpa_ssid *ssid)
  505. {
  506. char *value = wpa_config_get(ssid, "bssid");
  507. if (value == NULL)
  508. return;
  509. fprintf(f, "\tbssid=%s\n", value);
  510. os_free(value);
  511. }
  512. static void write_psk(FILE *f, struct wpa_ssid *ssid)
  513. {
  514. char *value = wpa_config_get(ssid, "psk");
  515. if (value == NULL)
  516. return;
  517. fprintf(f, "\tpsk=%s\n", value);
  518. os_free(value);
  519. }
  520. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  521. {
  522. char *value;
  523. if (ssid->proto == DEFAULT_PROTO)
  524. return;
  525. value = wpa_config_get(ssid, "proto");
  526. if (value == NULL)
  527. return;
  528. if (value[0])
  529. fprintf(f, "\tproto=%s\n", value);
  530. os_free(value);
  531. }
  532. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  533. {
  534. char *value;
  535. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  536. return;
  537. value = wpa_config_get(ssid, "key_mgmt");
  538. if (value == NULL)
  539. return;
  540. if (value[0])
  541. fprintf(f, "\tkey_mgmt=%s\n", value);
  542. os_free(value);
  543. }
  544. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  545. {
  546. char *value;
  547. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  548. return;
  549. value = wpa_config_get(ssid, "pairwise");
  550. if (value == NULL)
  551. return;
  552. if (value[0])
  553. fprintf(f, "\tpairwise=%s\n", value);
  554. os_free(value);
  555. }
  556. static void write_group(FILE *f, struct wpa_ssid *ssid)
  557. {
  558. char *value;
  559. if (ssid->group_cipher == DEFAULT_GROUP)
  560. return;
  561. value = wpa_config_get(ssid, "group");
  562. if (value == NULL)
  563. return;
  564. if (value[0])
  565. fprintf(f, "\tgroup=%s\n", value);
  566. os_free(value);
  567. }
  568. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  569. {
  570. char *value;
  571. if (ssid->auth_alg == 0)
  572. return;
  573. value = wpa_config_get(ssid, "auth_alg");
  574. if (value == NULL)
  575. return;
  576. if (value[0])
  577. fprintf(f, "\tauth_alg=%s\n", value);
  578. os_free(value);
  579. }
  580. #ifdef IEEE8021X_EAPOL
  581. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  582. {
  583. char *value;
  584. value = wpa_config_get(ssid, "eap");
  585. if (value == NULL)
  586. return;
  587. if (value[0])
  588. fprintf(f, "\teap=%s\n", value);
  589. os_free(value);
  590. }
  591. #endif /* IEEE8021X_EAPOL */
  592. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  593. {
  594. char field[20], *value;
  595. int res;
  596. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  597. if (res < 0 || (size_t) res >= sizeof(field))
  598. return;
  599. value = wpa_config_get(ssid, field);
  600. if (value) {
  601. fprintf(f, "\t%s=%s\n", field, value);
  602. os_free(value);
  603. }
  604. }
  605. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  606. {
  607. int i;
  608. #define STR(t) write_str(f, #t, ssid)
  609. #define INT(t) write_int(f, #t, ssid->t, 0)
  610. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  611. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  612. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  613. STR(ssid);
  614. INT(scan_ssid);
  615. write_bssid(f, ssid);
  616. write_psk(f, ssid);
  617. write_proto(f, ssid);
  618. write_key_mgmt(f, ssid);
  619. write_pairwise(f, ssid);
  620. write_group(f, ssid);
  621. write_auth_alg(f, ssid);
  622. #ifdef IEEE8021X_EAPOL
  623. write_eap(f, ssid);
  624. STR(identity);
  625. STR(anonymous_identity);
  626. STR(password);
  627. STR(ca_cert);
  628. STR(ca_path);
  629. STR(client_cert);
  630. STR(private_key);
  631. STR(private_key_passwd);
  632. STR(dh_file);
  633. STR(subject_match);
  634. STR(altsubject_match);
  635. STR(ca_cert2);
  636. STR(ca_path2);
  637. STR(client_cert2);
  638. STR(private_key2);
  639. STR(private_key2_passwd);
  640. STR(dh_file2);
  641. STR(subject_match2);
  642. STR(altsubject_match2);
  643. STR(phase1);
  644. STR(phase2);
  645. STR(pcsc);
  646. STR(pin);
  647. STR(engine_id);
  648. STR(key_id);
  649. STR(cert_id);
  650. STR(ca_cert_id);
  651. STR(key2_id);
  652. STR(pin2);
  653. STR(engine2_id);
  654. STR(cert2_id);
  655. STR(ca_cert2_id);
  656. INTe(engine);
  657. INTe(engine2);
  658. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  659. #endif /* IEEE8021X_EAPOL */
  660. for (i = 0; i < 4; i++)
  661. write_wep_key(f, i, ssid);
  662. INT(wep_tx_keyidx);
  663. INT(priority);
  664. #ifdef IEEE8021X_EAPOL
  665. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  666. STR(pac_file);
  667. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  668. #endif /* IEEE8021X_EAPOL */
  669. INT(mode);
  670. INT(proactive_key_caching);
  671. INT(disabled);
  672. INT(peerkey);
  673. #ifdef CONFIG_IEEE80211W
  674. INT(ieee80211w);
  675. #endif /* CONFIG_IEEE80211W */
  676. STR(id_str);
  677. #undef STR
  678. #undef INT
  679. #undef INT_DEF
  680. }
  681. #ifndef CONFIG_NO_CONFIG_BLOBS
  682. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  683. {
  684. unsigned char *encoded;
  685. encoded = base64_encode(blob->data, blob->len, NULL);
  686. if (encoded == NULL)
  687. return -1;
  688. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  689. os_free(encoded);
  690. return 0;
  691. }
  692. #endif /* CONFIG_NO_CONFIG_BLOBS */
  693. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  694. {
  695. #ifdef CONFIG_CTRL_IFACE
  696. if (config->ctrl_interface)
  697. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  698. if (config->ctrl_interface_group)
  699. fprintf(f, "ctrl_interface_group=%s\n",
  700. config->ctrl_interface_group);
  701. #endif /* CONFIG_CTRL_IFACE */
  702. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  703. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  704. if (config->ap_scan != DEFAULT_AP_SCAN)
  705. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  706. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  707. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  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. if (config->driver_param)
  718. fprintf(f, "driver_param=%s\n", config->driver_param);
  719. if (config->dot11RSNAConfigPMKLifetime)
  720. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  721. config->dot11RSNAConfigPMKLifetime);
  722. if (config->dot11RSNAConfigPMKReauthThreshold)
  723. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  724. config->dot11RSNAConfigPMKReauthThreshold);
  725. if (config->dot11RSNAConfigSATimeout)
  726. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  727. config->dot11RSNAConfigSATimeout);
  728. if (config->update_config)
  729. fprintf(f, "update_config=%d\n", config->update_config);
  730. #ifdef CONFIG_WPS
  731. if (!is_nil_uuid(config->uuid)) {
  732. char buf[40];
  733. uuid_bin2str(config->uuid, buf, sizeof(buf));
  734. fprintf(f, "uuid=%s\n", buf);
  735. }
  736. if (config->device_name)
  737. fprintf(f, "device_name=%s\n", config->device_name);
  738. if (config->manufacturer)
  739. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  740. if (config->model_name)
  741. fprintf(f, "model_name=%s\n", config->model_name);
  742. if (config->model_number)
  743. fprintf(f, "model_number=%s\n", config->model_number);
  744. if (config->serial_number)
  745. fprintf(f, "serial_number=%s\n", config->serial_number);
  746. if (config->device_type)
  747. fprintf(f, "device_type=%s\n", config->device_type);
  748. if (WPA_GET_BE32(config->os_version))
  749. fprintf(f, "os_version=%08x\n",
  750. WPA_GET_BE32(config->os_version));
  751. if (config->config_methods)
  752. fprintf(f, "config_methods=%s\n", config->config_methods);
  753. if (config->wps_cred_processing)
  754. fprintf(f, "wps_cred_processing=%d\n",
  755. config->wps_cred_processing);
  756. #endif /* CONFIG_WPS */
  757. if (config->country[0] && config->country[1]) {
  758. fprintf(f, "country=%c%c\n",
  759. config->country[0], config->country[1]);
  760. }
  761. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  762. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  763. if (config->filter_ssids)
  764. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  765. }
  766. #endif /* CONFIG_NO_CONFIG_WRITE */
  767. int wpa_config_write(const char *name, struct wpa_config *config)
  768. {
  769. #ifndef CONFIG_NO_CONFIG_WRITE
  770. FILE *f;
  771. struct wpa_ssid *ssid;
  772. #ifndef CONFIG_NO_CONFIG_BLOBS
  773. struct wpa_config_blob *blob;
  774. #endif /* CONFIG_NO_CONFIG_BLOBS */
  775. int ret = 0;
  776. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  777. f = fopen(name, "w");
  778. if (f == NULL) {
  779. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  780. return -1;
  781. }
  782. wpa_config_write_global(f, config);
  783. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  784. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  785. continue; /* do not save temporary WPS networks */
  786. fprintf(f, "\nnetwork={\n");
  787. wpa_config_write_network(f, ssid);
  788. fprintf(f, "}\n");
  789. }
  790. #ifndef CONFIG_NO_CONFIG_BLOBS
  791. for (blob = config->blobs; blob; blob = blob->next) {
  792. ret = wpa_config_write_blob(f, blob);
  793. if (ret)
  794. break;
  795. }
  796. #endif /* CONFIG_NO_CONFIG_BLOBS */
  797. fclose(f);
  798. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  799. name, ret ? "un" : "");
  800. return ret;
  801. #else /* CONFIG_NO_CONFIG_WRITE */
  802. return -1;
  803. #endif /* CONFIG_NO_CONFIG_WRITE */
  804. }