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