config_file.c 19 KB

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