config_file.c 24 KB

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