config_file.c 22 KB

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