config_file.c 19 KB

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