config_file.c 23 KB

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