config_file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
  573. INT(disabled);
  574. INT(peerkey);
  575. #ifdef CONFIG_IEEE80211W
  576. write_int(f, "ieee80211w", ssid->ieee80211w,
  577. MGMT_FRAME_PROTECTION_DEFAULT);
  578. #endif /* CONFIG_IEEE80211W */
  579. STR(id_str);
  580. #ifdef CONFIG_P2P
  581. write_p2p_client_list(f, ssid);
  582. #endif /* CONFIG_P2P */
  583. #undef STR
  584. #undef INT
  585. #undef INT_DEF
  586. }
  587. static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
  588. {
  589. if (cred->priority)
  590. fprintf(f, "\tpriority=%d\n", cred->priority);
  591. if (cred->pcsc)
  592. fprintf(f, "\tpcsc=%d\n", cred->pcsc);
  593. if (cred->realm)
  594. fprintf(f, "\trealm=\"%s\"\n", cred->realm);
  595. if (cred->username)
  596. fprintf(f, "\tusername=\"%s\"\n", cred->username);
  597. if (cred->password)
  598. fprintf(f, "\tpassword=\"%s\"\n", cred->password);
  599. if (cred->ca_cert)
  600. fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
  601. if (cred->imsi)
  602. fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
  603. if (cred->milenage)
  604. fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
  605. if (cred->domain)
  606. fprintf(f, "\tdomain=\"%s\"\n", cred->domain);
  607. }
  608. #ifndef CONFIG_NO_CONFIG_BLOBS
  609. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  610. {
  611. unsigned char *encoded;
  612. encoded = base64_encode(blob->data, blob->len, NULL);
  613. if (encoded == NULL)
  614. return -1;
  615. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  616. os_free(encoded);
  617. return 0;
  618. }
  619. #endif /* CONFIG_NO_CONFIG_BLOBS */
  620. static void write_global_bin(FILE *f, const char *field,
  621. const struct wpabuf *val)
  622. {
  623. size_t i;
  624. const u8 *pos;
  625. if (val == NULL)
  626. return;
  627. fprintf(f, "%s=", field);
  628. pos = wpabuf_head(val);
  629. for (i = 0; i < wpabuf_len(val); i++)
  630. fprintf(f, "%02X", *pos++);
  631. fprintf(f, "\n");
  632. }
  633. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  634. {
  635. #ifdef CONFIG_CTRL_IFACE
  636. if (config->ctrl_interface)
  637. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  638. if (config->ctrl_interface_group)
  639. fprintf(f, "ctrl_interface_group=%s\n",
  640. config->ctrl_interface_group);
  641. #endif /* CONFIG_CTRL_IFACE */
  642. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  643. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  644. if (config->ap_scan != DEFAULT_AP_SCAN)
  645. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  646. if (config->disable_scan_offload)
  647. fprintf(f, "disable_scan_offload=%d\n",
  648. config->disable_scan_offload);
  649. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  650. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  651. if (config->opensc_engine_path)
  652. fprintf(f, "opensc_engine_path=%s\n",
  653. config->opensc_engine_path);
  654. if (config->pkcs11_engine_path)
  655. fprintf(f, "pkcs11_engine_path=%s\n",
  656. config->pkcs11_engine_path);
  657. if (config->pkcs11_module_path)
  658. fprintf(f, "pkcs11_module_path=%s\n",
  659. config->pkcs11_module_path);
  660. if (config->pcsc_reader)
  661. fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
  662. if (config->pcsc_pin)
  663. fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
  664. if (config->driver_param)
  665. fprintf(f, "driver_param=%s\n", config->driver_param);
  666. if (config->dot11RSNAConfigPMKLifetime)
  667. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  668. config->dot11RSNAConfigPMKLifetime);
  669. if (config->dot11RSNAConfigPMKReauthThreshold)
  670. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  671. config->dot11RSNAConfigPMKReauthThreshold);
  672. if (config->dot11RSNAConfigSATimeout)
  673. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  674. config->dot11RSNAConfigSATimeout);
  675. if (config->update_config)
  676. fprintf(f, "update_config=%d\n", config->update_config);
  677. #ifdef CONFIG_WPS
  678. if (!is_nil_uuid(config->uuid)) {
  679. char buf[40];
  680. uuid_bin2str(config->uuid, buf, sizeof(buf));
  681. fprintf(f, "uuid=%s\n", buf);
  682. }
  683. if (config->device_name)
  684. fprintf(f, "device_name=%s\n", config->device_name);
  685. if (config->manufacturer)
  686. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  687. if (config->model_name)
  688. fprintf(f, "model_name=%s\n", config->model_name);
  689. if (config->model_number)
  690. fprintf(f, "model_number=%s\n", config->model_number);
  691. if (config->serial_number)
  692. fprintf(f, "serial_number=%s\n", config->serial_number);
  693. {
  694. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  695. buf = wps_dev_type_bin2str(config->device_type,
  696. _buf, sizeof(_buf));
  697. if (os_strcmp(buf, "0-00000000-0") != 0)
  698. fprintf(f, "device_type=%s\n", buf);
  699. }
  700. if (WPA_GET_BE32(config->os_version))
  701. fprintf(f, "os_version=%08x\n",
  702. WPA_GET_BE32(config->os_version));
  703. if (config->config_methods)
  704. fprintf(f, "config_methods=%s\n", config->config_methods);
  705. if (config->wps_cred_processing)
  706. fprintf(f, "wps_cred_processing=%d\n",
  707. config->wps_cred_processing);
  708. if (config->wps_vendor_ext_m1) {
  709. int i, len = wpabuf_len(config->wps_vendor_ext_m1);
  710. const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
  711. if (len > 0) {
  712. fprintf(f, "wps_vendor_ext_m1=");
  713. for (i = 0; i < len; i++)
  714. fprintf(f, "%02x", *p++);
  715. fprintf(f, "\n");
  716. }
  717. }
  718. #endif /* CONFIG_WPS */
  719. #ifdef CONFIG_P2P
  720. if (config->p2p_listen_reg_class)
  721. fprintf(f, "p2p_listen_reg_class=%u\n",
  722. config->p2p_listen_reg_class);
  723. if (config->p2p_listen_channel)
  724. fprintf(f, "p2p_listen_channel=%u\n",
  725. config->p2p_listen_channel);
  726. if (config->p2p_oper_reg_class)
  727. fprintf(f, "p2p_oper_reg_class=%u\n",
  728. config->p2p_oper_reg_class);
  729. if (config->p2p_oper_channel)
  730. fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
  731. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  732. fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
  733. if (config->p2p_ssid_postfix)
  734. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  735. if (config->persistent_reconnect)
  736. fprintf(f, "persistent_reconnect=%u\n",
  737. config->persistent_reconnect);
  738. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  739. fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
  740. if (config->p2p_group_idle)
  741. fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
  742. if (config->p2p_pref_chan) {
  743. unsigned int i;
  744. fprintf(f, "p2p_pref_chan=");
  745. for (i = 0; i < config->num_p2p_pref_chan; i++) {
  746. fprintf(f, "%s%u:%u", i > 0 ? "," : "",
  747. config->p2p_pref_chan[i].op_class,
  748. config->p2p_pref_chan[i].chan);
  749. }
  750. fprintf(f, "\n");
  751. }
  752. if (config->p2p_go_ht40)
  753. fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40);
  754. if (config->p2p_disabled)
  755. fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled);
  756. if (config->p2p_no_group_iface)
  757. fprintf(f, "p2p_no_group_iface=%u\n",
  758. config->p2p_no_group_iface);
  759. #endif /* CONFIG_P2P */
  760. if (config->country[0] && config->country[1]) {
  761. fprintf(f, "country=%c%c\n",
  762. config->country[0], config->country[1]);
  763. }
  764. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  765. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  766. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  767. fprintf(f, "bss_expiration_age=%u\n",
  768. config->bss_expiration_age);
  769. if (config->bss_expiration_scan_count !=
  770. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  771. fprintf(f, "bss_expiration_scan_count=%u\n",
  772. config->bss_expiration_scan_count);
  773. if (config->filter_ssids)
  774. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  775. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  776. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  777. if (config->disassoc_low_ack)
  778. fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
  779. #ifdef CONFIG_HS20
  780. if (config->hs20)
  781. fprintf(f, "hs20=1\n");
  782. #endif /* CONFIG_HS20 */
  783. #ifdef CONFIG_INTERWORKING
  784. if (config->interworking)
  785. fprintf(f, "interworking=%u\n", config->interworking);
  786. if (!is_zero_ether_addr(config->hessid))
  787. fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
  788. if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
  789. fprintf(f, "access_network_type=%d\n",
  790. config->access_network_type);
  791. #endif /* CONFIG_INTERWORKING */
  792. if (config->pbc_in_m1)
  793. fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1);
  794. if (config->wps_nfc_dev_pw_id)
  795. fprintf(f, "wps_nfc_dev_pw_id=%d\n",
  796. config->wps_nfc_dev_pw_id);
  797. write_global_bin(f, "wps_nfc_dh_pubkey", config->wps_nfc_dh_pubkey);
  798. write_global_bin(f, "wps_nfc_dh_privkey", config->wps_nfc_dh_privkey);
  799. write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
  800. if (config->ext_password_backend)
  801. fprintf(f, "ext_password_backend=%s\n",
  802. config->ext_password_backend);
  803. if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
  804. fprintf(f, "p2p_go_max_inactivity=%d\n",
  805. config->p2p_go_max_inactivity);
  806. if (config->auto_interworking)
  807. fprintf(f, "auto_interworking=%d\n",
  808. config->auto_interworking);
  809. if (config->okc)
  810. fprintf(f, "okc=%d\n", config->okc);
  811. if (config->pmf)
  812. fprintf(f, "pmf=%d\n", config->pmf);
  813. }
  814. #endif /* CONFIG_NO_CONFIG_WRITE */
  815. int wpa_config_write(const char *name, struct wpa_config *config)
  816. {
  817. #ifndef CONFIG_NO_CONFIG_WRITE
  818. FILE *f;
  819. struct wpa_ssid *ssid;
  820. struct wpa_cred *cred;
  821. #ifndef CONFIG_NO_CONFIG_BLOBS
  822. struct wpa_config_blob *blob;
  823. #endif /* CONFIG_NO_CONFIG_BLOBS */
  824. int ret = 0;
  825. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  826. f = fopen(name, "w");
  827. if (f == NULL) {
  828. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  829. return -1;
  830. }
  831. wpa_config_write_global(f, config);
  832. for (cred = config->cred; cred; cred = cred->next) {
  833. fprintf(f, "\ncred={\n");
  834. wpa_config_write_cred(f, cred);
  835. fprintf(f, "}\n");
  836. }
  837. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  838. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  839. continue; /* do not save temporary networks */
  840. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  841. !ssid->passphrase)
  842. continue; /* do not save invalid network */
  843. fprintf(f, "\nnetwork={\n");
  844. wpa_config_write_network(f, ssid);
  845. fprintf(f, "}\n");
  846. }
  847. #ifndef CONFIG_NO_CONFIG_BLOBS
  848. for (blob = config->blobs; blob; blob = blob->next) {
  849. ret = wpa_config_write_blob(f, blob);
  850. if (ret)
  851. break;
  852. }
  853. #endif /* CONFIG_NO_CONFIG_BLOBS */
  854. fclose(f);
  855. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  856. name, ret ? "un" : "");
  857. return ret;
  858. #else /* CONFIG_NO_CONFIG_WRITE */
  859. return -1;
  860. #endif /* CONFIG_NO_CONFIG_WRITE */
  861. }