config_file.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. write_pairwise(f, ssid);
  489. write_group(f, ssid);
  490. write_auth_alg(f, ssid);
  491. #ifdef IEEE8021X_EAPOL
  492. write_eap(f, ssid);
  493. STR(identity);
  494. STR(anonymous_identity);
  495. STR(password);
  496. STR(ca_cert);
  497. STR(ca_path);
  498. STR(client_cert);
  499. STR(private_key);
  500. STR(private_key_passwd);
  501. STR(dh_file);
  502. STR(subject_match);
  503. STR(altsubject_match);
  504. STR(ca_cert2);
  505. STR(ca_path2);
  506. STR(client_cert2);
  507. STR(private_key2);
  508. STR(private_key2_passwd);
  509. STR(dh_file2);
  510. STR(subject_match2);
  511. STR(altsubject_match2);
  512. STR(phase1);
  513. STR(phase2);
  514. STR(pcsc);
  515. STR(pin);
  516. STR(engine_id);
  517. STR(key_id);
  518. STR(cert_id);
  519. STR(ca_cert_id);
  520. STR(key2_id);
  521. STR(pin2);
  522. STR(engine2_id);
  523. STR(cert2_id);
  524. STR(ca_cert2_id);
  525. INTe(engine);
  526. INTe(engine2);
  527. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  528. #endif /* IEEE8021X_EAPOL */
  529. for (i = 0; i < 4; i++)
  530. write_wep_key(f, i, ssid);
  531. INT(wep_tx_keyidx);
  532. INT(priority);
  533. #ifdef IEEE8021X_EAPOL
  534. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  535. STR(pac_file);
  536. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  537. #endif /* IEEE8021X_EAPOL */
  538. INT(mode);
  539. INT(proactive_key_caching);
  540. INT(disabled);
  541. INT(peerkey);
  542. #ifdef CONFIG_IEEE80211W
  543. INT(ieee80211w);
  544. #endif /* CONFIG_IEEE80211W */
  545. STR(id_str);
  546. #ifdef CONFIG_P2P
  547. write_p2p_client_list(f, ssid);
  548. #endif /* CONFIG_P2P */
  549. #undef STR
  550. #undef INT
  551. #undef INT_DEF
  552. }
  553. static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
  554. {
  555. if (cred->priority)
  556. fprintf(f, "\tpriority=%d\n", cred->priority);
  557. if (cred->pcsc)
  558. fprintf(f, "\tpcsc=%d\n", cred->pcsc);
  559. if (cred->realm)
  560. fprintf(f, "\trealm=\"%s\"\n", cred->realm);
  561. if (cred->username)
  562. fprintf(f, "\tusername=\"%s\"\n", cred->username);
  563. if (cred->password)
  564. fprintf(f, "\tpassword=\"%s\"\n", cred->password);
  565. if (cred->ca_cert)
  566. fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
  567. if (cred->imsi)
  568. fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
  569. if (cred->milenage)
  570. fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
  571. if (cred->domain)
  572. fprintf(f, "\tdomain=\"%s\"\n", cred->domain);
  573. }
  574. #ifndef CONFIG_NO_CONFIG_BLOBS
  575. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  576. {
  577. unsigned char *encoded;
  578. encoded = base64_encode(blob->data, blob->len, NULL);
  579. if (encoded == NULL)
  580. return -1;
  581. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  582. os_free(encoded);
  583. return 0;
  584. }
  585. #endif /* CONFIG_NO_CONFIG_BLOBS */
  586. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  587. {
  588. #ifdef CONFIG_CTRL_IFACE
  589. if (config->ctrl_interface)
  590. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  591. if (config->ctrl_interface_group)
  592. fprintf(f, "ctrl_interface_group=%s\n",
  593. config->ctrl_interface_group);
  594. #endif /* CONFIG_CTRL_IFACE */
  595. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  596. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  597. if (config->ap_scan != DEFAULT_AP_SCAN)
  598. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  599. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  600. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  601. if (config->opensc_engine_path)
  602. fprintf(f, "opensc_engine_path=%s\n",
  603. config->opensc_engine_path);
  604. if (config->pkcs11_engine_path)
  605. fprintf(f, "pkcs11_engine_path=%s\n",
  606. config->pkcs11_engine_path);
  607. if (config->pkcs11_module_path)
  608. fprintf(f, "pkcs11_module_path=%s\n",
  609. config->pkcs11_module_path);
  610. if (config->pcsc_reader)
  611. fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
  612. if (config->pcsc_pin)
  613. fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
  614. if (config->driver_param)
  615. fprintf(f, "driver_param=%s\n", config->driver_param);
  616. if (config->dot11RSNAConfigPMKLifetime)
  617. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  618. config->dot11RSNAConfigPMKLifetime);
  619. if (config->dot11RSNAConfigPMKReauthThreshold)
  620. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  621. config->dot11RSNAConfigPMKReauthThreshold);
  622. if (config->dot11RSNAConfigSATimeout)
  623. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  624. config->dot11RSNAConfigSATimeout);
  625. if (config->update_config)
  626. fprintf(f, "update_config=%d\n", config->update_config);
  627. #ifdef CONFIG_WPS
  628. if (!is_nil_uuid(config->uuid)) {
  629. char buf[40];
  630. uuid_bin2str(config->uuid, buf, sizeof(buf));
  631. fprintf(f, "uuid=%s\n", buf);
  632. }
  633. if (config->device_name)
  634. fprintf(f, "device_name=%s\n", config->device_name);
  635. if (config->manufacturer)
  636. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  637. if (config->model_name)
  638. fprintf(f, "model_name=%s\n", config->model_name);
  639. if (config->model_number)
  640. fprintf(f, "model_number=%s\n", config->model_number);
  641. if (config->serial_number)
  642. fprintf(f, "serial_number=%s\n", config->serial_number);
  643. {
  644. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  645. buf = wps_dev_type_bin2str(config->device_type,
  646. _buf, sizeof(_buf));
  647. if (os_strcmp(buf, "0-00000000-0") != 0)
  648. fprintf(f, "device_type=%s\n", buf);
  649. }
  650. if (WPA_GET_BE32(config->os_version))
  651. fprintf(f, "os_version=%08x\n",
  652. WPA_GET_BE32(config->os_version));
  653. if (config->config_methods)
  654. fprintf(f, "config_methods=%s\n", config->config_methods);
  655. if (config->wps_cred_processing)
  656. fprintf(f, "wps_cred_processing=%d\n",
  657. config->wps_cred_processing);
  658. #endif /* CONFIG_WPS */
  659. #ifdef CONFIG_P2P
  660. if (config->p2p_listen_reg_class)
  661. fprintf(f, "p2p_listen_reg_class=%u\n",
  662. config->p2p_listen_reg_class);
  663. if (config->p2p_listen_channel)
  664. fprintf(f, "p2p_listen_channel=%u\n",
  665. config->p2p_listen_channel);
  666. if (config->p2p_oper_reg_class)
  667. fprintf(f, "p2p_oper_reg_class=%u\n",
  668. config->p2p_oper_reg_class);
  669. if (config->p2p_oper_channel)
  670. fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
  671. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  672. fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
  673. if (config->p2p_ssid_postfix)
  674. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  675. if (config->persistent_reconnect)
  676. fprintf(f, "persistent_reconnect=%u\n",
  677. config->persistent_reconnect);
  678. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  679. fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
  680. if (config->p2p_group_idle)
  681. fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
  682. if (config->p2p_pref_chan) {
  683. unsigned int i;
  684. fprintf(f, "p2p_pref_chan=");
  685. for (i = 0; i < config->num_p2p_pref_chan; i++) {
  686. fprintf(f, "%s%u:%u", i > 0 ? "," : "",
  687. config->p2p_pref_chan[i].op_class,
  688. config->p2p_pref_chan[i].chan);
  689. }
  690. fprintf(f, "\n");
  691. }
  692. #endif /* CONFIG_P2P */
  693. if (config->country[0] && config->country[1]) {
  694. fprintf(f, "country=%c%c\n",
  695. config->country[0], config->country[1]);
  696. }
  697. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  698. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  699. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  700. fprintf(f, "bss_expiration_age=%u\n",
  701. config->bss_expiration_age);
  702. if (config->bss_expiration_scan_count !=
  703. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  704. fprintf(f, "bss_expiration_scan_count=%u\n",
  705. config->bss_expiration_scan_count);
  706. if (config->filter_ssids)
  707. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  708. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  709. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  710. if (config->disassoc_low_ack)
  711. fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
  712. #ifdef CONFIG_INTERWORKING
  713. if (config->interworking)
  714. fprintf(f, "interworking=%u\n", config->interworking);
  715. if (!is_zero_ether_addr(config->hessid))
  716. fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
  717. if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
  718. fprintf(f, "access_network_type=%d\n",
  719. config->access_network_type);
  720. #endif /* CONFIG_INTERWORKING */
  721. }
  722. #endif /* CONFIG_NO_CONFIG_WRITE */
  723. int wpa_config_write(const char *name, struct wpa_config *config)
  724. {
  725. #ifndef CONFIG_NO_CONFIG_WRITE
  726. FILE *f;
  727. struct wpa_ssid *ssid;
  728. struct wpa_cred *cred;
  729. #ifndef CONFIG_NO_CONFIG_BLOBS
  730. struct wpa_config_blob *blob;
  731. #endif /* CONFIG_NO_CONFIG_BLOBS */
  732. int ret = 0;
  733. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  734. f = fopen(name, "w");
  735. if (f == NULL) {
  736. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  737. return -1;
  738. }
  739. wpa_config_write_global(f, config);
  740. for (cred = config->cred; cred; cred = cred->next) {
  741. fprintf(f, "\ncred={\n");
  742. wpa_config_write_cred(f, cred);
  743. fprintf(f, "}\n");
  744. }
  745. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  746. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  747. continue; /* do not save temporary networks */
  748. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  749. !ssid->passphrase)
  750. continue; /* do not save invalid network */
  751. fprintf(f, "\nnetwork={\n");
  752. wpa_config_write_network(f, ssid);
  753. fprintf(f, "}\n");
  754. }
  755. #ifndef CONFIG_NO_CONFIG_BLOBS
  756. for (blob = config->blobs; blob; blob = blob->next) {
  757. ret = wpa_config_write_blob(f, blob);
  758. if (ret)
  759. break;
  760. }
  761. #endif /* CONFIG_NO_CONFIG_BLOBS */
  762. fclose(f);
  763. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  764. name, ret ? "un" : "");
  765. return ret;
  766. #else /* CONFIG_NO_CONFIG_WRITE */
  767. return -1;
  768. #endif /* CONFIG_NO_CONFIG_WRITE */
  769. }