Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

config_file.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. /**
  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. struct wpa_config * wpa_config_read(const char *name)
  228. {
  229. FILE *f;
  230. char buf[256], *pos;
  231. int errors = 0, line = 0;
  232. struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
  233. struct wpa_config *config;
  234. int id = 0;
  235. config = wpa_config_alloc_empty(NULL, NULL);
  236. if (config == NULL)
  237. return NULL;
  238. wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
  239. f = fopen(name, "r");
  240. if (f == NULL) {
  241. os_free(config);
  242. return NULL;
  243. }
  244. while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
  245. if (os_strcmp(pos, "network={") == 0) {
  246. ssid = wpa_config_read_network(f, &line, id++);
  247. if (ssid == NULL) {
  248. wpa_printf(MSG_ERROR, "Line %d: failed to "
  249. "parse network block.", line);
  250. errors++;
  251. continue;
  252. }
  253. if (head == NULL) {
  254. head = tail = ssid;
  255. } else {
  256. tail->next = ssid;
  257. tail = ssid;
  258. }
  259. if (wpa_config_add_prio_network(config, ssid)) {
  260. wpa_printf(MSG_ERROR, "Line %d: failed to add "
  261. "network block to priority list.",
  262. line);
  263. errors++;
  264. continue;
  265. }
  266. #ifndef CONFIG_NO_CONFIG_BLOBS
  267. } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
  268. if (wpa_config_process_blob(config, f, &line, pos + 12)
  269. < 0) {
  270. errors++;
  271. continue;
  272. }
  273. #endif /* CONFIG_NO_CONFIG_BLOBS */
  274. } else if (wpa_config_process_global(config, pos, line) < 0) {
  275. wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
  276. "line '%s'.", line, pos);
  277. errors++;
  278. continue;
  279. }
  280. }
  281. fclose(f);
  282. config->ssid = head;
  283. wpa_config_debug_dump_networks(config);
  284. #ifndef WPA_IGNORE_CONFIG_ERRORS
  285. if (errors) {
  286. wpa_config_free(config);
  287. config = NULL;
  288. head = NULL;
  289. }
  290. #endif /* WPA_IGNORE_CONFIG_ERRORS */
  291. return config;
  292. }
  293. #ifndef CONFIG_NO_CONFIG_WRITE
  294. static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
  295. {
  296. char *value = wpa_config_get(ssid, field);
  297. if (value == NULL)
  298. return;
  299. fprintf(f, "\t%s=%s\n", field, value);
  300. os_free(value);
  301. }
  302. static void write_int(FILE *f, const char *field, int value, int def)
  303. {
  304. if (value == def)
  305. return;
  306. fprintf(f, "\t%s=%d\n", field, value);
  307. }
  308. static void write_bssid(FILE *f, struct wpa_ssid *ssid)
  309. {
  310. char *value = wpa_config_get(ssid, "bssid");
  311. if (value == NULL)
  312. return;
  313. fprintf(f, "\tbssid=%s\n", value);
  314. os_free(value);
  315. }
  316. static void write_psk(FILE *f, struct wpa_ssid *ssid)
  317. {
  318. char *value = wpa_config_get(ssid, "psk");
  319. if (value == NULL)
  320. return;
  321. fprintf(f, "\tpsk=%s\n", value);
  322. os_free(value);
  323. }
  324. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  325. {
  326. char *value;
  327. if (ssid->proto == DEFAULT_PROTO)
  328. return;
  329. value = wpa_config_get(ssid, "proto");
  330. if (value == NULL)
  331. return;
  332. if (value[0])
  333. fprintf(f, "\tproto=%s\n", value);
  334. os_free(value);
  335. }
  336. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  337. {
  338. char *value;
  339. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  340. return;
  341. value = wpa_config_get(ssid, "key_mgmt");
  342. if (value == NULL)
  343. return;
  344. if (value[0])
  345. fprintf(f, "\tkey_mgmt=%s\n", value);
  346. os_free(value);
  347. }
  348. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  349. {
  350. char *value;
  351. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  352. return;
  353. value = wpa_config_get(ssid, "pairwise");
  354. if (value == NULL)
  355. return;
  356. if (value[0])
  357. fprintf(f, "\tpairwise=%s\n", value);
  358. os_free(value);
  359. }
  360. static void write_group(FILE *f, struct wpa_ssid *ssid)
  361. {
  362. char *value;
  363. if (ssid->group_cipher == DEFAULT_GROUP)
  364. return;
  365. value = wpa_config_get(ssid, "group");
  366. if (value == NULL)
  367. return;
  368. if (value[0])
  369. fprintf(f, "\tgroup=%s\n", value);
  370. os_free(value);
  371. }
  372. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  373. {
  374. char *value;
  375. if (ssid->auth_alg == 0)
  376. return;
  377. value = wpa_config_get(ssid, "auth_alg");
  378. if (value == NULL)
  379. return;
  380. if (value[0])
  381. fprintf(f, "\tauth_alg=%s\n", value);
  382. os_free(value);
  383. }
  384. #ifdef IEEE8021X_EAPOL
  385. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  386. {
  387. char *value;
  388. value = wpa_config_get(ssid, "eap");
  389. if (value == NULL)
  390. return;
  391. if (value[0])
  392. fprintf(f, "\teap=%s\n", value);
  393. os_free(value);
  394. }
  395. #endif /* IEEE8021X_EAPOL */
  396. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  397. {
  398. char field[20], *value;
  399. int res;
  400. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  401. if (res < 0 || (size_t) res >= sizeof(field))
  402. return;
  403. value = wpa_config_get(ssid, field);
  404. if (value) {
  405. fprintf(f, "\t%s=%s\n", field, value);
  406. os_free(value);
  407. }
  408. }
  409. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  410. {
  411. int i;
  412. #define STR(t) write_str(f, #t, ssid)
  413. #define INT(t) write_int(f, #t, ssid->t, 0)
  414. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  415. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  416. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  417. STR(ssid);
  418. INT(scan_ssid);
  419. write_bssid(f, ssid);
  420. write_psk(f, ssid);
  421. write_proto(f, ssid);
  422. write_key_mgmt(f, ssid);
  423. write_pairwise(f, ssid);
  424. write_group(f, ssid);
  425. write_auth_alg(f, ssid);
  426. #ifdef IEEE8021X_EAPOL
  427. write_eap(f, ssid);
  428. STR(identity);
  429. STR(anonymous_identity);
  430. STR(password);
  431. STR(ca_cert);
  432. STR(ca_path);
  433. STR(client_cert);
  434. STR(private_key);
  435. STR(private_key_passwd);
  436. STR(dh_file);
  437. STR(subject_match);
  438. STR(altsubject_match);
  439. STR(ca_cert2);
  440. STR(ca_path2);
  441. STR(client_cert2);
  442. STR(private_key2);
  443. STR(private_key2_passwd);
  444. STR(dh_file2);
  445. STR(subject_match2);
  446. STR(altsubject_match2);
  447. STR(phase1);
  448. STR(phase2);
  449. STR(pcsc);
  450. STR(pin);
  451. STR(engine_id);
  452. STR(key_id);
  453. STR(cert_id);
  454. STR(ca_cert_id);
  455. STR(key2_id);
  456. STR(pin2);
  457. STR(engine2_id);
  458. STR(cert2_id);
  459. STR(ca_cert2_id);
  460. INTe(engine);
  461. INTe(engine2);
  462. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  463. #endif /* IEEE8021X_EAPOL */
  464. for (i = 0; i < 4; i++)
  465. write_wep_key(f, i, ssid);
  466. INT(wep_tx_keyidx);
  467. INT(priority);
  468. #ifdef IEEE8021X_EAPOL
  469. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  470. STR(pac_file);
  471. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  472. #endif /* IEEE8021X_EAPOL */
  473. INT(mode);
  474. INT(proactive_key_caching);
  475. INT(disabled);
  476. INT(peerkey);
  477. #ifdef CONFIG_IEEE80211W
  478. INT(ieee80211w);
  479. #endif /* CONFIG_IEEE80211W */
  480. STR(id_str);
  481. #undef STR
  482. #undef INT
  483. #undef INT_DEF
  484. }
  485. #ifndef CONFIG_NO_CONFIG_BLOBS
  486. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  487. {
  488. unsigned char *encoded;
  489. encoded = base64_encode(blob->data, blob->len, NULL);
  490. if (encoded == NULL)
  491. return -1;
  492. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  493. os_free(encoded);
  494. return 0;
  495. }
  496. #endif /* CONFIG_NO_CONFIG_BLOBS */
  497. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  498. {
  499. #ifdef CONFIG_CTRL_IFACE
  500. if (config->ctrl_interface)
  501. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  502. if (config->ctrl_interface_group)
  503. fprintf(f, "ctrl_interface_group=%s\n",
  504. config->ctrl_interface_group);
  505. #endif /* CONFIG_CTRL_IFACE */
  506. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  507. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  508. if (config->ap_scan != DEFAULT_AP_SCAN)
  509. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  510. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  511. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  512. if (config->opensc_engine_path)
  513. fprintf(f, "opensc_engine_path=%s\n",
  514. config->opensc_engine_path);
  515. if (config->pkcs11_engine_path)
  516. fprintf(f, "pkcs11_engine_path=%s\n",
  517. config->pkcs11_engine_path);
  518. if (config->pkcs11_module_path)
  519. fprintf(f, "pkcs11_module_path=%s\n",
  520. config->pkcs11_module_path);
  521. if (config->driver_param)
  522. fprintf(f, "driver_param=%s\n", config->driver_param);
  523. if (config->dot11RSNAConfigPMKLifetime)
  524. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  525. config->dot11RSNAConfigPMKLifetime);
  526. if (config->dot11RSNAConfigPMKReauthThreshold)
  527. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  528. config->dot11RSNAConfigPMKReauthThreshold);
  529. if (config->dot11RSNAConfigSATimeout)
  530. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  531. config->dot11RSNAConfigSATimeout);
  532. if (config->update_config)
  533. fprintf(f, "update_config=%d\n", config->update_config);
  534. #ifdef CONFIG_WPS
  535. if (!is_nil_uuid(config->uuid)) {
  536. char buf[40];
  537. uuid_bin2str(config->uuid, buf, sizeof(buf));
  538. fprintf(f, "uuid=%s\n", buf);
  539. }
  540. if (config->rf_bands != WPS_RF_24GHZ)
  541. fprintf(f, "rf_bands=%d\n", config->rf_bands);
  542. if (config->device_name)
  543. fprintf(f, "device_name=%s\n", config->device_name);
  544. if (config->manufacturer)
  545. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  546. if (config->model_name)
  547. fprintf(f, "model_name=%s\n", config->model_name);
  548. if (config->model_number)
  549. fprintf(f, "model_number=%s\n", config->model_number);
  550. if (config->serial_number)
  551. fprintf(f, "serial_number=%s\n", config->serial_number);
  552. {
  553. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  554. buf = wps_dev_type_bin2str(config->device_type,
  555. _buf, sizeof(_buf));
  556. fprintf(f, "device_type=%s\n", buf);
  557. }
  558. if (WPA_GET_BE32(config->os_version))
  559. fprintf(f, "os_version=%08x\n",
  560. WPA_GET_BE32(config->os_version));
  561. if (config->config_methods)
  562. fprintf(f, "config_methods=%s\n", config->config_methods);
  563. if (config->wps_cred_processing)
  564. fprintf(f, "wps_cred_processing=%d\n",
  565. config->wps_cred_processing);
  566. #endif /* CONFIG_WPS */
  567. #ifdef CONFIG_P2P
  568. if (config->p2p_listen_reg_class)
  569. fprintf(f, "p2p_listen_reg_class=%u\n",
  570. config->p2p_listen_reg_class);
  571. if (config->p2p_listen_channel)
  572. fprintf(f, "p2p_listen_channel=%u\n",
  573. config->p2p_listen_channel);
  574. if (config->p2p_oper_reg_class)
  575. fprintf(f, "p2p_oper_reg_class=%u\n",
  576. config->p2p_oper_reg_class);
  577. if (config->p2p_oper_channel)
  578. fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
  579. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  580. fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
  581. if (config->p2p_ssid_postfix)
  582. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  583. if (config->persistent_reconnect)
  584. fprintf(f, "persistent_reconnect=%u\n",
  585. config->persistent_reconnect);
  586. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  587. fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
  588. if (config->p2p_group_idle)
  589. fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
  590. #endif /* CONFIG_P2P */
  591. if (config->country[0] && config->country[1]) {
  592. fprintf(f, "country=%c%c\n",
  593. config->country[0], config->country[1]);
  594. }
  595. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  596. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  597. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  598. fprintf(f, "bss_expiration_age=%u\n",
  599. config->bss_expiration_age);
  600. if (config->bss_expiration_scan_count !=
  601. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  602. fprintf(f, "bss_expiration_scan_count=%u\n",
  603. config->bss_expiration_scan_count);
  604. if (config->filter_ssids)
  605. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  606. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  607. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  608. if (config->disassoc_low_ack)
  609. fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
  610. }
  611. #endif /* CONFIG_NO_CONFIG_WRITE */
  612. int wpa_config_write(const char *name, struct wpa_config *config)
  613. {
  614. #ifndef CONFIG_NO_CONFIG_WRITE
  615. FILE *f;
  616. struct wpa_ssid *ssid;
  617. #ifndef CONFIG_NO_CONFIG_BLOBS
  618. struct wpa_config_blob *blob;
  619. #endif /* CONFIG_NO_CONFIG_BLOBS */
  620. int ret = 0;
  621. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  622. f = fopen(name, "w");
  623. if (f == NULL) {
  624. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  625. return -1;
  626. }
  627. wpa_config_write_global(f, config);
  628. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  629. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  630. continue; /* do not save temporary networks */
  631. fprintf(f, "\nnetwork={\n");
  632. wpa_config_write_network(f, ssid);
  633. fprintf(f, "}\n");
  634. }
  635. #ifndef CONFIG_NO_CONFIG_BLOBS
  636. for (blob = config->blobs; blob; blob = blob->next) {
  637. ret = wpa_config_write_blob(f, blob);
  638. if (ret)
  639. break;
  640. }
  641. #endif /* CONFIG_NO_CONFIG_BLOBS */
  642. fclose(f);
  643. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  644. name, ret ? "un" : "");
  645. return ret;
  646. #else /* CONFIG_NO_CONFIG_WRITE */
  647. return -1;
  648. #endif /* CONFIG_NO_CONFIG_WRITE */
  649. }