config_file.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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. cred->sim_num = DEFAULT_USER_SELECTED_SIM;
  187. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  188. if (os_strcmp(pos, "}") == 0) {
  189. end = 1;
  190. break;
  191. }
  192. pos2 = os_strchr(pos, '=');
  193. if (pos2 == NULL) {
  194. wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
  195. "'%s'.", *line, pos);
  196. errors++;
  197. continue;
  198. }
  199. *pos2++ = '\0';
  200. if (*pos2 == '"') {
  201. if (os_strchr(pos2 + 1, '"') == NULL) {
  202. wpa_printf(MSG_ERROR, "Line %d: invalid "
  203. "quotation '%s'.", *line, pos2);
  204. errors++;
  205. continue;
  206. }
  207. }
  208. if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
  209. errors++;
  210. }
  211. if (!end) {
  212. wpa_printf(MSG_ERROR, "Line %d: cred block was not "
  213. "terminated properly.", *line);
  214. errors++;
  215. }
  216. if (errors) {
  217. wpa_config_free_cred(cred);
  218. cred = NULL;
  219. }
  220. return cred;
  221. }
  222. #ifndef CONFIG_NO_CONFIG_BLOBS
  223. static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
  224. const char *name)
  225. {
  226. struct wpa_config_blob *blob;
  227. char buf[256], *pos;
  228. unsigned char *encoded = NULL, *nencoded;
  229. int end = 0;
  230. size_t encoded_len = 0, len;
  231. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
  232. *line, name);
  233. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  234. if (os_strcmp(pos, "}") == 0) {
  235. end = 1;
  236. break;
  237. }
  238. len = os_strlen(pos);
  239. nencoded = os_realloc(encoded, encoded_len + len);
  240. if (nencoded == NULL) {
  241. wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
  242. "blob", *line);
  243. os_free(encoded);
  244. return NULL;
  245. }
  246. encoded = nencoded;
  247. os_memcpy(encoded + encoded_len, pos, len);
  248. encoded_len += len;
  249. }
  250. if (!end) {
  251. wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
  252. "properly", *line);
  253. os_free(encoded);
  254. return NULL;
  255. }
  256. blob = os_zalloc(sizeof(*blob));
  257. if (blob == NULL) {
  258. os_free(encoded);
  259. return NULL;
  260. }
  261. blob->name = os_strdup(name);
  262. blob->data = base64_decode(encoded, encoded_len, &blob->len);
  263. os_free(encoded);
  264. if (blob->name == NULL || blob->data == NULL) {
  265. wpa_config_free_blob(blob);
  266. return NULL;
  267. }
  268. return blob;
  269. }
  270. static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
  271. int *line, char *bname)
  272. {
  273. char *name_end;
  274. struct wpa_config_blob *blob;
  275. name_end = os_strchr(bname, '=');
  276. if (name_end == NULL) {
  277. wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
  278. *line);
  279. return -1;
  280. }
  281. *name_end = '\0';
  282. blob = wpa_config_read_blob(f, line, bname);
  283. if (blob == NULL) {
  284. wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
  285. *line, bname);
  286. return -1;
  287. }
  288. wpa_config_set_blob(config, blob);
  289. return 0;
  290. }
  291. #endif /* CONFIG_NO_CONFIG_BLOBS */
  292. struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
  293. {
  294. FILE *f;
  295. char buf[512], *pos;
  296. int errors = 0, line = 0;
  297. struct wpa_ssid *ssid, *tail, *head;
  298. struct wpa_cred *cred, *cred_tail, *cred_head;
  299. struct wpa_config *config;
  300. int id = 0;
  301. int cred_id = 0;
  302. if (name == NULL)
  303. return NULL;
  304. if (cfgp)
  305. config = cfgp;
  306. else
  307. config = wpa_config_alloc_empty(NULL, NULL);
  308. if (config == NULL) {
  309. wpa_printf(MSG_ERROR, "Failed to allocate config file "
  310. "structure");
  311. return NULL;
  312. }
  313. tail = head = config->ssid;
  314. while (tail && tail->next)
  315. tail = tail->next;
  316. cred_tail = cred_head = config->cred;
  317. while (cred_tail && cred_tail->next)
  318. cred_tail = cred_tail->next;
  319. wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
  320. f = fopen(name, "r");
  321. if (f == NULL) {
  322. wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
  323. "error: %s", name, strerror(errno));
  324. os_free(config);
  325. return NULL;
  326. }
  327. while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
  328. if (os_strcmp(pos, "network={") == 0) {
  329. ssid = wpa_config_read_network(f, &line, id++);
  330. if (ssid == NULL) {
  331. wpa_printf(MSG_ERROR, "Line %d: failed to "
  332. "parse network block.", line);
  333. errors++;
  334. continue;
  335. }
  336. if (head == NULL) {
  337. head = tail = ssid;
  338. } else {
  339. tail->next = ssid;
  340. tail = ssid;
  341. }
  342. if (wpa_config_add_prio_network(config, ssid)) {
  343. wpa_printf(MSG_ERROR, "Line %d: failed to add "
  344. "network block to priority list.",
  345. line);
  346. errors++;
  347. continue;
  348. }
  349. } else if (os_strcmp(pos, "cred={") == 0) {
  350. cred = wpa_config_read_cred(f, &line, cred_id++);
  351. if (cred == NULL) {
  352. wpa_printf(MSG_ERROR, "Line %d: failed to "
  353. "parse cred block.", line);
  354. errors++;
  355. continue;
  356. }
  357. if (cred_head == NULL) {
  358. cred_head = cred_tail = cred;
  359. } else {
  360. cred_tail->next = cred;
  361. cred_tail = cred;
  362. }
  363. #ifndef CONFIG_NO_CONFIG_BLOBS
  364. } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
  365. if (wpa_config_process_blob(config, f, &line, pos + 12)
  366. < 0) {
  367. wpa_printf(MSG_ERROR, "Line %d: failed to "
  368. "process blob.", line);
  369. errors++;
  370. continue;
  371. }
  372. #endif /* CONFIG_NO_CONFIG_BLOBS */
  373. } else if (wpa_config_process_global(config, pos, line) < 0) {
  374. wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
  375. "line '%s'.", line, pos);
  376. errors++;
  377. continue;
  378. }
  379. }
  380. fclose(f);
  381. config->ssid = head;
  382. wpa_config_debug_dump_networks(config);
  383. config->cred = cred_head;
  384. #ifndef WPA_IGNORE_CONFIG_ERRORS
  385. if (errors) {
  386. wpa_config_free(config);
  387. config = NULL;
  388. head = NULL;
  389. }
  390. #endif /* WPA_IGNORE_CONFIG_ERRORS */
  391. return config;
  392. }
  393. #ifndef CONFIG_NO_CONFIG_WRITE
  394. static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
  395. {
  396. char *value = wpa_config_get(ssid, field);
  397. if (value == NULL)
  398. return;
  399. fprintf(f, "\t%s=%s\n", field, value);
  400. os_free(value);
  401. }
  402. static void write_int(FILE *f, const char *field, int value, int def)
  403. {
  404. if (value == def)
  405. return;
  406. fprintf(f, "\t%s=%d\n", field, value);
  407. }
  408. static void write_bssid(FILE *f, struct wpa_ssid *ssid)
  409. {
  410. char *value = wpa_config_get(ssid, "bssid");
  411. if (value == NULL)
  412. return;
  413. fprintf(f, "\tbssid=%s\n", value);
  414. os_free(value);
  415. }
  416. static void write_psk(FILE *f, struct wpa_ssid *ssid)
  417. {
  418. char *value = wpa_config_get(ssid, "psk");
  419. if (value == NULL)
  420. return;
  421. fprintf(f, "\tpsk=%s\n", value);
  422. os_free(value);
  423. }
  424. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  425. {
  426. char *value;
  427. if (ssid->proto == DEFAULT_PROTO)
  428. return;
  429. value = wpa_config_get(ssid, "proto");
  430. if (value == NULL)
  431. return;
  432. if (value[0])
  433. fprintf(f, "\tproto=%s\n", value);
  434. os_free(value);
  435. }
  436. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  437. {
  438. char *value;
  439. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  440. return;
  441. value = wpa_config_get(ssid, "key_mgmt");
  442. if (value == NULL)
  443. return;
  444. if (value[0])
  445. fprintf(f, "\tkey_mgmt=%s\n", value);
  446. os_free(value);
  447. }
  448. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  449. {
  450. char *value;
  451. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  452. return;
  453. value = wpa_config_get(ssid, "pairwise");
  454. if (value == NULL)
  455. return;
  456. if (value[0])
  457. fprintf(f, "\tpairwise=%s\n", value);
  458. os_free(value);
  459. }
  460. static void write_group(FILE *f, struct wpa_ssid *ssid)
  461. {
  462. char *value;
  463. if (ssid->group_cipher == DEFAULT_GROUP)
  464. return;
  465. value = wpa_config_get(ssid, "group");
  466. if (value == NULL)
  467. return;
  468. if (value[0])
  469. fprintf(f, "\tgroup=%s\n", value);
  470. os_free(value);
  471. }
  472. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  473. {
  474. char *value;
  475. if (ssid->auth_alg == 0)
  476. return;
  477. value = wpa_config_get(ssid, "auth_alg");
  478. if (value == NULL)
  479. return;
  480. if (value[0])
  481. fprintf(f, "\tauth_alg=%s\n", value);
  482. os_free(value);
  483. }
  484. #ifdef IEEE8021X_EAPOL
  485. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  486. {
  487. char *value;
  488. value = wpa_config_get(ssid, "eap");
  489. if (value == NULL)
  490. return;
  491. if (value[0])
  492. fprintf(f, "\teap=%s\n", value);
  493. os_free(value);
  494. }
  495. #endif /* IEEE8021X_EAPOL */
  496. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  497. {
  498. char field[20], *value;
  499. int res;
  500. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  501. if (res < 0 || (size_t) res >= sizeof(field))
  502. return;
  503. value = wpa_config_get(ssid, field);
  504. if (value) {
  505. fprintf(f, "\t%s=%s\n", field, value);
  506. os_free(value);
  507. }
  508. }
  509. #ifdef CONFIG_P2P
  510. static void write_go_p2p_dev_addr(FILE *f, struct wpa_ssid *ssid)
  511. {
  512. char *value = wpa_config_get(ssid, "go_p2p_dev_addr");
  513. if (value == NULL)
  514. return;
  515. fprintf(f, "\tgo_p2p_dev_addr=%s\n", value);
  516. os_free(value);
  517. }
  518. static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
  519. {
  520. char *value = wpa_config_get(ssid, "p2p_client_list");
  521. if (value == NULL)
  522. return;
  523. fprintf(f, "\tp2p_client_list=%s\n", value);
  524. os_free(value);
  525. }
  526. static void write_psk_list(FILE *f, struct wpa_ssid *ssid)
  527. {
  528. struct psk_list_entry *psk;
  529. char hex[32 * 2 + 1];
  530. dl_list_for_each(psk, &ssid->psk_list, struct psk_list_entry, list) {
  531. wpa_snprintf_hex(hex, sizeof(hex), psk->psk, sizeof(psk->psk));
  532. fprintf(f, "\tpsk_list=%s" MACSTR "-%s\n",
  533. psk->p2p ? "P2P-" : "", MAC2STR(psk->addr), hex);
  534. }
  535. }
  536. #endif /* CONFIG_P2P */
  537. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  538. {
  539. int i;
  540. #define STR(t) write_str(f, #t, ssid)
  541. #define INT(t) write_int(f, #t, ssid->t, 0)
  542. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  543. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  544. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  545. STR(ssid);
  546. INT(scan_ssid);
  547. write_bssid(f, ssid);
  548. write_psk(f, ssid);
  549. write_proto(f, ssid);
  550. write_key_mgmt(f, ssid);
  551. INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
  552. write_pairwise(f, ssid);
  553. write_group(f, ssid);
  554. write_auth_alg(f, ssid);
  555. STR(bgscan);
  556. STR(autoscan);
  557. STR(scan_freq);
  558. #ifdef IEEE8021X_EAPOL
  559. write_eap(f, ssid);
  560. STR(identity);
  561. STR(anonymous_identity);
  562. STR(password);
  563. STR(ca_cert);
  564. STR(ca_path);
  565. STR(client_cert);
  566. STR(private_key);
  567. STR(private_key_passwd);
  568. STR(dh_file);
  569. STR(subject_match);
  570. STR(altsubject_match);
  571. STR(domain_suffix_match);
  572. STR(ca_cert2);
  573. STR(ca_path2);
  574. STR(client_cert2);
  575. STR(private_key2);
  576. STR(private_key2_passwd);
  577. STR(dh_file2);
  578. STR(subject_match2);
  579. STR(altsubject_match2);
  580. STR(domain_suffix_match2);
  581. STR(phase1);
  582. STR(phase2);
  583. STR(pcsc);
  584. STR(pin);
  585. STR(engine_id);
  586. STR(key_id);
  587. STR(cert_id);
  588. STR(ca_cert_id);
  589. STR(key2_id);
  590. STR(pin2);
  591. STR(engine2_id);
  592. STR(cert2_id);
  593. STR(ca_cert2_id);
  594. INTe(engine);
  595. INTe(engine2);
  596. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  597. #endif /* IEEE8021X_EAPOL */
  598. for (i = 0; i < 4; i++)
  599. write_wep_key(f, i, ssid);
  600. INT(wep_tx_keyidx);
  601. INT(priority);
  602. #ifdef IEEE8021X_EAPOL
  603. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  604. STR(pac_file);
  605. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  606. INTe(ocsp);
  607. INT_DEFe(sim_num, DEFAULT_USER_SELECTED_SIM);
  608. #endif /* IEEE8021X_EAPOL */
  609. INT(mode);
  610. INT(frequency);
  611. write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
  612. INT(disabled);
  613. INT(peerkey);
  614. #ifdef CONFIG_IEEE80211W
  615. write_int(f, "ieee80211w", ssid->ieee80211w,
  616. MGMT_FRAME_PROTECTION_DEFAULT);
  617. #endif /* CONFIG_IEEE80211W */
  618. STR(id_str);
  619. #ifdef CONFIG_P2P
  620. write_go_p2p_dev_addr(f, ssid);
  621. write_p2p_client_list(f, ssid);
  622. write_psk_list(f, ssid);
  623. #endif /* CONFIG_P2P */
  624. INT(dtim_period);
  625. INT(beacon_int);
  626. #ifdef CONFIG_MACSEC
  627. INT(macsec_policy);
  628. #endif /* CONFIG_MACSEC */
  629. #ifdef CONFIG_HS20
  630. INT(update_identifier);
  631. #endif /* CONFIG_HS20 */
  632. write_int(f, "mac_addr", ssid->mac_addr, -1);
  633. #ifdef CONFIG_MESH
  634. STR(mesh_ht_mode);
  635. STR(mesh_basic_rates);
  636. #endif /* CONFIG_MESH */
  637. #undef STR
  638. #undef INT
  639. #undef INT_DEF
  640. }
  641. static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
  642. {
  643. size_t i;
  644. if (cred->priority)
  645. fprintf(f, "\tpriority=%d\n", cred->priority);
  646. if (cred->pcsc)
  647. fprintf(f, "\tpcsc=%d\n", cred->pcsc);
  648. if (cred->realm)
  649. fprintf(f, "\trealm=\"%s\"\n", cred->realm);
  650. if (cred->username)
  651. fprintf(f, "\tusername=\"%s\"\n", cred->username);
  652. if (cred->password && cred->ext_password)
  653. fprintf(f, "\tpassword=ext:%s\n", cred->password);
  654. else if (cred->password)
  655. fprintf(f, "\tpassword=\"%s\"\n", cred->password);
  656. if (cred->ca_cert)
  657. fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
  658. if (cred->client_cert)
  659. fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
  660. if (cred->private_key)
  661. fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
  662. if (cred->private_key_passwd)
  663. fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
  664. cred->private_key_passwd);
  665. if (cred->imsi)
  666. fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
  667. if (cred->milenage)
  668. fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
  669. for (i = 0; i < cred->num_domain; i++)
  670. fprintf(f, "\tdomain=\"%s\"\n", cred->domain[i]);
  671. if (cred->domain_suffix_match)
  672. fprintf(f, "\tdomain_suffix_match=\"%s\"\n",
  673. cred->domain_suffix_match);
  674. if (cred->roaming_consortium_len) {
  675. fprintf(f, "\troaming_consortium=");
  676. for (i = 0; i < cred->roaming_consortium_len; i++)
  677. fprintf(f, "%02x", cred->roaming_consortium[i]);
  678. fprintf(f, "\n");
  679. }
  680. if (cred->eap_method) {
  681. const char *name;
  682. name = eap_get_name(cred->eap_method[0].vendor,
  683. cred->eap_method[0].method);
  684. if (name)
  685. fprintf(f, "\teap=%s\n", name);
  686. }
  687. if (cred->phase1)
  688. fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
  689. if (cred->phase2)
  690. fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
  691. if (cred->excluded_ssid) {
  692. size_t j;
  693. for (i = 0; i < cred->num_excluded_ssid; i++) {
  694. struct excluded_ssid *e = &cred->excluded_ssid[i];
  695. fprintf(f, "\texcluded_ssid=");
  696. for (j = 0; j < e->ssid_len; j++)
  697. fprintf(f, "%02x", e->ssid[j]);
  698. fprintf(f, "\n");
  699. }
  700. }
  701. if (cred->roaming_partner) {
  702. for (i = 0; i < cred->num_roaming_partner; i++) {
  703. struct roaming_partner *p = &cred->roaming_partner[i];
  704. fprintf(f, "\troaming_partner=\"%s,%d,%u,%s\"\n",
  705. p->fqdn, p->exact_match, p->priority,
  706. p->country);
  707. }
  708. }
  709. if (cred->update_identifier)
  710. fprintf(f, "\tupdate_identifier=%d\n", cred->update_identifier);
  711. if (cred->provisioning_sp)
  712. fprintf(f, "\tprovisioning_sp=\"%s\"\n", cred->provisioning_sp);
  713. if (cred->sp_priority)
  714. fprintf(f, "\tsp_priority=%d\n", cred->sp_priority);
  715. if (cred->min_dl_bandwidth_home)
  716. fprintf(f, "\tmin_dl_bandwidth_home=%u\n",
  717. cred->min_dl_bandwidth_home);
  718. if (cred->min_ul_bandwidth_home)
  719. fprintf(f, "\tmin_ul_bandwidth_home=%u\n",
  720. cred->min_ul_bandwidth_home);
  721. if (cred->min_dl_bandwidth_roaming)
  722. fprintf(f, "\tmin_dl_bandwidth_roaming=%u\n",
  723. cred->min_dl_bandwidth_roaming);
  724. if (cred->min_ul_bandwidth_roaming)
  725. fprintf(f, "\tmin_ul_bandwidth_roaming=%u\n",
  726. cred->min_ul_bandwidth_roaming);
  727. if (cred->max_bss_load)
  728. fprintf(f, "\tmax_bss_load=%u\n",
  729. cred->max_bss_load);
  730. if (cred->ocsp)
  731. fprintf(f, "\tocsp=%d\n", cred->ocsp);
  732. if (cred->num_req_conn_capab) {
  733. for (i = 0; i < cred->num_req_conn_capab; i++) {
  734. int *ports;
  735. fprintf(f, "\treq_conn_capab=%u",
  736. cred->req_conn_capab_proto[i]);
  737. ports = cred->req_conn_capab_port[i];
  738. if (ports) {
  739. int j;
  740. for (j = 0; ports[j] != -1; j++) {
  741. fprintf(f, "%s%d", j > 0 ? "," : ":",
  742. ports[j]);
  743. }
  744. }
  745. fprintf(f, "\n");
  746. }
  747. }
  748. if (cred->required_roaming_consortium_len) {
  749. fprintf(f, "\trequired_roaming_consortium=");
  750. for (i = 0; i < cred->required_roaming_consortium_len; i++)
  751. fprintf(f, "%02x",
  752. cred->required_roaming_consortium[i]);
  753. fprintf(f, "\n");
  754. }
  755. if (cred->sim_num != DEFAULT_USER_SELECTED_SIM)
  756. fprintf(f, "\tsim_num=%d\n", cred->sim_num);
  757. }
  758. #ifndef CONFIG_NO_CONFIG_BLOBS
  759. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  760. {
  761. unsigned char *encoded;
  762. encoded = base64_encode(blob->data, blob->len, NULL);
  763. if (encoded == NULL)
  764. return -1;
  765. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  766. os_free(encoded);
  767. return 0;
  768. }
  769. #endif /* CONFIG_NO_CONFIG_BLOBS */
  770. static void write_global_bin(FILE *f, const char *field,
  771. const struct wpabuf *val)
  772. {
  773. size_t i;
  774. const u8 *pos;
  775. if (val == NULL)
  776. return;
  777. fprintf(f, "%s=", field);
  778. pos = wpabuf_head(val);
  779. for (i = 0; i < wpabuf_len(val); i++)
  780. fprintf(f, "%02X", *pos++);
  781. fprintf(f, "\n");
  782. }
  783. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  784. {
  785. #ifdef CONFIG_CTRL_IFACE
  786. if (config->ctrl_interface)
  787. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  788. if (config->ctrl_interface_group)
  789. fprintf(f, "ctrl_interface_group=%s\n",
  790. config->ctrl_interface_group);
  791. #endif /* CONFIG_CTRL_IFACE */
  792. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  793. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  794. if (config->ap_scan != DEFAULT_AP_SCAN)
  795. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  796. if (config->disable_scan_offload)
  797. fprintf(f, "disable_scan_offload=%d\n",
  798. config->disable_scan_offload);
  799. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  800. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  801. if (config->opensc_engine_path)
  802. fprintf(f, "opensc_engine_path=%s\n",
  803. config->opensc_engine_path);
  804. if (config->pkcs11_engine_path)
  805. fprintf(f, "pkcs11_engine_path=%s\n",
  806. config->pkcs11_engine_path);
  807. if (config->pkcs11_module_path)
  808. fprintf(f, "pkcs11_module_path=%s\n",
  809. config->pkcs11_module_path);
  810. if (config->openssl_ciphers)
  811. fprintf(f, "openssl_ciphers=%s\n", config->openssl_ciphers);
  812. if (config->pcsc_reader)
  813. fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
  814. if (config->pcsc_pin)
  815. fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
  816. if (config->driver_param)
  817. fprintf(f, "driver_param=%s\n", config->driver_param);
  818. if (config->dot11RSNAConfigPMKLifetime)
  819. fprintf(f, "dot11RSNAConfigPMKLifetime=%d\n",
  820. config->dot11RSNAConfigPMKLifetime);
  821. if (config->dot11RSNAConfigPMKReauthThreshold)
  822. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%d\n",
  823. config->dot11RSNAConfigPMKReauthThreshold);
  824. if (config->dot11RSNAConfigSATimeout)
  825. fprintf(f, "dot11RSNAConfigSATimeout=%d\n",
  826. config->dot11RSNAConfigSATimeout);
  827. if (config->update_config)
  828. fprintf(f, "update_config=%d\n", config->update_config);
  829. #ifdef CONFIG_WPS
  830. if (!is_nil_uuid(config->uuid)) {
  831. char buf[40];
  832. uuid_bin2str(config->uuid, buf, sizeof(buf));
  833. fprintf(f, "uuid=%s\n", buf);
  834. }
  835. if (config->device_name)
  836. fprintf(f, "device_name=%s\n", config->device_name);
  837. if (config->manufacturer)
  838. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  839. if (config->model_name)
  840. fprintf(f, "model_name=%s\n", config->model_name);
  841. if (config->model_number)
  842. fprintf(f, "model_number=%s\n", config->model_number);
  843. if (config->serial_number)
  844. fprintf(f, "serial_number=%s\n", config->serial_number);
  845. {
  846. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  847. buf = wps_dev_type_bin2str(config->device_type,
  848. _buf, sizeof(_buf));
  849. if (os_strcmp(buf, "0-00000000-0") != 0)
  850. fprintf(f, "device_type=%s\n", buf);
  851. }
  852. if (WPA_GET_BE32(config->os_version))
  853. fprintf(f, "os_version=%08x\n",
  854. WPA_GET_BE32(config->os_version));
  855. if (config->config_methods)
  856. fprintf(f, "config_methods=%s\n", config->config_methods);
  857. if (config->wps_cred_processing)
  858. fprintf(f, "wps_cred_processing=%d\n",
  859. config->wps_cred_processing);
  860. if (config->wps_vendor_ext_m1) {
  861. int i, len = wpabuf_len(config->wps_vendor_ext_m1);
  862. const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
  863. if (len > 0) {
  864. fprintf(f, "wps_vendor_ext_m1=");
  865. for (i = 0; i < len; i++)
  866. fprintf(f, "%02x", *p++);
  867. fprintf(f, "\n");
  868. }
  869. }
  870. #endif /* CONFIG_WPS */
  871. #ifdef CONFIG_P2P
  872. if (config->p2p_listen_reg_class)
  873. fprintf(f, "p2p_listen_reg_class=%u\n",
  874. config->p2p_listen_reg_class);
  875. if (config->p2p_listen_channel)
  876. fprintf(f, "p2p_listen_channel=%u\n",
  877. config->p2p_listen_channel);
  878. if (config->p2p_oper_reg_class)
  879. fprintf(f, "p2p_oper_reg_class=%u\n",
  880. config->p2p_oper_reg_class);
  881. if (config->p2p_oper_channel)
  882. fprintf(f, "p2p_oper_channel=%u\n", config->p2p_oper_channel);
  883. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  884. fprintf(f, "p2p_go_intent=%u\n", config->p2p_go_intent);
  885. if (config->p2p_ssid_postfix)
  886. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  887. if (config->persistent_reconnect)
  888. fprintf(f, "persistent_reconnect=%u\n",
  889. config->persistent_reconnect);
  890. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  891. fprintf(f, "p2p_intra_bss=%u\n", config->p2p_intra_bss);
  892. if (config->p2p_group_idle)
  893. fprintf(f, "p2p_group_idle=%u\n", config->p2p_group_idle);
  894. if (config->p2p_passphrase_len)
  895. fprintf(f, "p2p_passphrase_len=%u\n",
  896. config->p2p_passphrase_len);
  897. if (config->p2p_pref_chan) {
  898. unsigned int i;
  899. fprintf(f, "p2p_pref_chan=");
  900. for (i = 0; i < config->num_p2p_pref_chan; i++) {
  901. fprintf(f, "%s%u:%u", i > 0 ? "," : "",
  902. config->p2p_pref_chan[i].op_class,
  903. config->p2p_pref_chan[i].chan);
  904. }
  905. fprintf(f, "\n");
  906. }
  907. if (config->p2p_no_go_freq.num) {
  908. char *val = freq_range_list_str(&config->p2p_no_go_freq);
  909. if (val) {
  910. fprintf(f, "p2p_no_go_freq=%s\n", val);
  911. os_free(val);
  912. }
  913. }
  914. if (config->p2p_add_cli_chan)
  915. fprintf(f, "p2p_add_cli_chan=%d\n", config->p2p_add_cli_chan);
  916. if (config->p2p_optimize_listen_chan !=
  917. DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN)
  918. fprintf(f, "p2p_optimize_listen_chan=%d\n",
  919. config->p2p_optimize_listen_chan);
  920. if (config->p2p_go_ht40)
  921. fprintf(f, "p2p_go_ht40=%u\n", config->p2p_go_ht40);
  922. if (config->p2p_go_vht)
  923. fprintf(f, "p2p_go_vht=%u\n", config->p2p_go_vht);
  924. if (config->p2p_disabled)
  925. fprintf(f, "p2p_disabled=%u\n", config->p2p_disabled);
  926. if (config->p2p_no_group_iface)
  927. fprintf(f, "p2p_no_group_iface=%u\n",
  928. config->p2p_no_group_iface);
  929. if (config->p2p_ignore_shared_freq)
  930. fprintf(f, "p2p_ignore_shared_freq=%u\n",
  931. config->p2p_ignore_shared_freq);
  932. #endif /* CONFIG_P2P */
  933. if (config->country[0] && config->country[1]) {
  934. fprintf(f, "country=%c%c\n",
  935. config->country[0], config->country[1]);
  936. }
  937. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  938. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  939. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  940. fprintf(f, "bss_expiration_age=%u\n",
  941. config->bss_expiration_age);
  942. if (config->bss_expiration_scan_count !=
  943. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  944. fprintf(f, "bss_expiration_scan_count=%u\n",
  945. config->bss_expiration_scan_count);
  946. if (config->filter_ssids)
  947. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  948. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  949. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  950. if (config->disassoc_low_ack)
  951. fprintf(f, "disassoc_low_ack=%u\n", config->disassoc_low_ack);
  952. #ifdef CONFIG_HS20
  953. if (config->hs20)
  954. fprintf(f, "hs20=1\n");
  955. #endif /* CONFIG_HS20 */
  956. #ifdef CONFIG_INTERWORKING
  957. if (config->interworking)
  958. fprintf(f, "interworking=%u\n", config->interworking);
  959. if (!is_zero_ether_addr(config->hessid))
  960. fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
  961. if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
  962. fprintf(f, "access_network_type=%d\n",
  963. config->access_network_type);
  964. #endif /* CONFIG_INTERWORKING */
  965. if (config->pbc_in_m1)
  966. fprintf(f, "pbc_in_m1=%u\n", config->pbc_in_m1);
  967. if (config->wps_nfc_pw_from_config) {
  968. if (config->wps_nfc_dev_pw_id)
  969. fprintf(f, "wps_nfc_dev_pw_id=%d\n",
  970. config->wps_nfc_dev_pw_id);
  971. write_global_bin(f, "wps_nfc_dh_pubkey",
  972. config->wps_nfc_dh_pubkey);
  973. write_global_bin(f, "wps_nfc_dh_privkey",
  974. config->wps_nfc_dh_privkey);
  975. write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
  976. }
  977. if (config->ext_password_backend)
  978. fprintf(f, "ext_password_backend=%s\n",
  979. config->ext_password_backend);
  980. if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
  981. fprintf(f, "p2p_go_max_inactivity=%d\n",
  982. config->p2p_go_max_inactivity);
  983. if (config->auto_interworking)
  984. fprintf(f, "auto_interworking=%d\n",
  985. config->auto_interworking);
  986. if (config->okc)
  987. fprintf(f, "okc=%d\n", config->okc);
  988. if (config->pmf)
  989. fprintf(f, "pmf=%d\n", config->pmf);
  990. if (config->dtim_period)
  991. fprintf(f, "dtim_period=%d\n", config->dtim_period);
  992. if (config->beacon_int)
  993. fprintf(f, "beacon_int=%d\n", config->beacon_int);
  994. if (config->sae_groups) {
  995. int i;
  996. fprintf(f, "sae_groups=");
  997. for (i = 0; config->sae_groups[i] >= 0; i++) {
  998. fprintf(f, "%s%d", i > 0 ? " " : "",
  999. config->sae_groups[i]);
  1000. }
  1001. fprintf(f, "\n");
  1002. }
  1003. if (config->ap_vendor_elements) {
  1004. int i, len = wpabuf_len(config->ap_vendor_elements);
  1005. const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
  1006. if (len > 0) {
  1007. fprintf(f, "ap_vendor_elements=");
  1008. for (i = 0; i < len; i++)
  1009. fprintf(f, "%02x", *p++);
  1010. fprintf(f, "\n");
  1011. }
  1012. }
  1013. if (config->ignore_old_scan_res)
  1014. fprintf(f, "ignore_old_scan_res=%d\n",
  1015. config->ignore_old_scan_res);
  1016. if (config->freq_list && config->freq_list[0]) {
  1017. int i;
  1018. fprintf(f, "freq_list=");
  1019. for (i = 0; config->freq_list[i]; i++) {
  1020. fprintf(f, "%s%u", i > 0 ? " " : "",
  1021. config->freq_list[i]);
  1022. }
  1023. fprintf(f, "\n");
  1024. }
  1025. if (config->scan_cur_freq != DEFAULT_SCAN_CUR_FREQ)
  1026. fprintf(f, "scan_cur_freq=%d\n", config->scan_cur_freq);
  1027. if (config->sched_scan_interval)
  1028. fprintf(f, "sched_scan_interval=%u\n",
  1029. config->sched_scan_interval);
  1030. if (config->external_sim)
  1031. fprintf(f, "external_sim=%d\n", config->external_sim);
  1032. if (config->tdls_external_control)
  1033. fprintf(f, "tdls_external_control=%d\n",
  1034. config->tdls_external_control);
  1035. if (config->wowlan_triggers)
  1036. fprintf(f, "wowlan_triggers=%s\n",
  1037. config->wowlan_triggers);
  1038. if (config->bgscan)
  1039. fprintf(f, "bgscan=\"%s\"\n", config->bgscan);
  1040. if (config->p2p_search_delay != DEFAULT_P2P_SEARCH_DELAY)
  1041. fprintf(f, "p2p_search_delay=%u\n",
  1042. config->p2p_search_delay);
  1043. if (config->mac_addr)
  1044. fprintf(f, "mac_addr=%d\n", config->mac_addr);
  1045. if (config->rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
  1046. fprintf(f, "rand_addr_lifetime=%u\n",
  1047. config->rand_addr_lifetime);
  1048. if (config->preassoc_mac_addr)
  1049. fprintf(f, "preassoc_mac_addr=%d\n", config->preassoc_mac_addr);
  1050. if (config->key_mgmt_offload != DEFAULT_KEY_MGMT_OFFLOAD)
  1051. fprintf(f, "key_mgmt_offload=%u\n", config->key_mgmt_offload);
  1052. if (config->user_mpm != DEFAULT_USER_MPM)
  1053. fprintf(f, "user_mpm=%d\n", config->user_mpm);
  1054. }
  1055. #endif /* CONFIG_NO_CONFIG_WRITE */
  1056. int wpa_config_write(const char *name, struct wpa_config *config)
  1057. {
  1058. #ifndef CONFIG_NO_CONFIG_WRITE
  1059. FILE *f;
  1060. struct wpa_ssid *ssid;
  1061. struct wpa_cred *cred;
  1062. #ifndef CONFIG_NO_CONFIG_BLOBS
  1063. struct wpa_config_blob *blob;
  1064. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1065. int ret = 0;
  1066. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  1067. f = fopen(name, "w");
  1068. if (f == NULL) {
  1069. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  1070. return -1;
  1071. }
  1072. wpa_config_write_global(f, config);
  1073. for (cred = config->cred; cred; cred = cred->next) {
  1074. if (cred->temporary)
  1075. continue;
  1076. fprintf(f, "\ncred={\n");
  1077. wpa_config_write_cred(f, cred);
  1078. fprintf(f, "}\n");
  1079. }
  1080. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  1081. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  1082. continue; /* do not save temporary networks */
  1083. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  1084. !ssid->passphrase)
  1085. continue; /* do not save invalid network */
  1086. fprintf(f, "\nnetwork={\n");
  1087. wpa_config_write_network(f, ssid);
  1088. fprintf(f, "}\n");
  1089. }
  1090. #ifndef CONFIG_NO_CONFIG_BLOBS
  1091. for (blob = config->blobs; blob; blob = blob->next) {
  1092. ret = wpa_config_write_blob(f, blob);
  1093. if (ret)
  1094. break;
  1095. }
  1096. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1097. fclose(f);
  1098. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  1099. name, ret ? "un" : "");
  1100. return ret;
  1101. #else /* CONFIG_NO_CONFIG_WRITE */
  1102. return -1;
  1103. #endif /* CONFIG_NO_CONFIG_WRITE */
  1104. }