config_file.c 21 KB

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