config_file.c 28 KB

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