config_file.c 21 KB

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