config_file.c 21 KB

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