config_file.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  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. #ifdef ANDROID
  14. #include <sys/stat.h>
  15. #endif /* ANDROID */
  16. #include "common.h"
  17. #include "config.h"
  18. #include "base64.h"
  19. #include "uuid.h"
  20. #include "common/ieee802_1x_defs.h"
  21. #include "p2p/p2p.h"
  22. #include "eap_peer/eap_methods.h"
  23. #include "eap_peer/eap.h"
  24. static int newline_terminated(const char *buf, size_t buflen)
  25. {
  26. size_t len = os_strlen(buf);
  27. if (len == 0)
  28. return 0;
  29. if (len == buflen - 1 && buf[buflen - 1] != '\r' &&
  30. buf[len - 1] != '\n')
  31. return 0;
  32. return 1;
  33. }
  34. static void skip_line_end(FILE *stream)
  35. {
  36. char buf[100];
  37. while (fgets(buf, sizeof(buf), stream)) {
  38. buf[sizeof(buf) - 1] = '\0';
  39. if (newline_terminated(buf, sizeof(buf)))
  40. return;
  41. }
  42. }
  43. /**
  44. * wpa_config_get_line - Read the next configuration file line
  45. * @s: Buffer for the line
  46. * @size: The buffer length
  47. * @stream: File stream to read from
  48. * @line: Pointer to a variable storing the file line number
  49. * @_pos: Buffer for the pointer to the beginning of data on the text line or
  50. * %NULL if not needed (returned value used instead)
  51. * Returns: Pointer to the beginning of data on the text line or %NULL if no
  52. * more text lines are available.
  53. *
  54. * This function reads the next non-empty line from the configuration file and
  55. * removes comments. The returned string is guaranteed to be null-terminated.
  56. */
  57. static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
  58. char **_pos)
  59. {
  60. char *pos, *end, *sstart;
  61. while (fgets(s, size, stream)) {
  62. (*line)++;
  63. s[size - 1] = '\0';
  64. if (!newline_terminated(s, size)) {
  65. /*
  66. * The line was truncated - skip rest of it to avoid
  67. * confusing error messages.
  68. */
  69. wpa_printf(MSG_INFO, "Long line in configuration file "
  70. "truncated");
  71. skip_line_end(stream);
  72. }
  73. pos = s;
  74. /* Skip white space from the beginning of line. */
  75. while (*pos == ' ' || *pos == '\t' || *pos == '\r')
  76. pos++;
  77. /* Skip comment lines and empty lines */
  78. if (*pos == '#' || *pos == '\n' || *pos == '\0')
  79. continue;
  80. /*
  81. * Remove # comments unless they are within a double quoted
  82. * string.
  83. */
  84. sstart = os_strchr(pos, '"');
  85. if (sstart)
  86. sstart = os_strrchr(sstart + 1, '"');
  87. if (!sstart)
  88. sstart = pos;
  89. end = os_strchr(sstart, '#');
  90. if (end)
  91. *end-- = '\0';
  92. else
  93. end = pos + os_strlen(pos) - 1;
  94. /* Remove trailing white space. */
  95. while (end > pos &&
  96. (*end == '\n' || *end == ' ' || *end == '\t' ||
  97. *end == '\r'))
  98. *end-- = '\0';
  99. if (*pos == '\0')
  100. continue;
  101. if (_pos)
  102. *_pos = pos;
  103. return pos;
  104. }
  105. if (_pos)
  106. *_pos = NULL;
  107. return NULL;
  108. }
  109. static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
  110. {
  111. int errors = 0;
  112. if (ssid->passphrase) {
  113. if (ssid->psk_set) {
  114. wpa_printf(MSG_ERROR, "Line %d: both PSK and "
  115. "passphrase configured.", line);
  116. errors++;
  117. }
  118. wpa_config_update_psk(ssid);
  119. }
  120. if (ssid->disabled == 2)
  121. ssid->p2p_persistent_group = 1;
  122. if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
  123. !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  124. !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
  125. /* Group cipher cannot be stronger than the pairwise cipher. */
  126. wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
  127. " list since it was not allowed for pairwise "
  128. "cipher", line);
  129. ssid->group_cipher &= ~WPA_CIPHER_CCMP;
  130. }
  131. if (ssid->mode == WPAS_MODE_MESH &&
  132. (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
  133. ssid->key_mgmt != WPA_KEY_MGMT_SAE)) {
  134. wpa_printf(MSG_ERROR,
  135. "Line %d: key_mgmt for mesh network should be open or SAE",
  136. line);
  137. errors++;
  138. }
  139. return errors;
  140. }
  141. static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
  142. {
  143. struct wpa_ssid *ssid;
  144. int errors = 0, end = 0;
  145. char buf[2000], *pos, *pos2;
  146. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
  147. *line);
  148. ssid = os_zalloc(sizeof(*ssid));
  149. if (ssid == NULL)
  150. return NULL;
  151. dl_list_init(&ssid->psk_list);
  152. ssid->id = id;
  153. wpa_config_set_network_defaults(ssid);
  154. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  155. if (os_strcmp(pos, "}") == 0) {
  156. end = 1;
  157. break;
  158. }
  159. pos2 = os_strchr(pos, '=');
  160. if (pos2 == NULL) {
  161. wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
  162. "'%s'.", *line, pos);
  163. errors++;
  164. continue;
  165. }
  166. *pos2++ = '\0';
  167. if (*pos2 == '"') {
  168. if (os_strchr(pos2 + 1, '"') == NULL) {
  169. wpa_printf(MSG_ERROR, "Line %d: invalid "
  170. "quotation '%s'.", *line, pos2);
  171. errors++;
  172. continue;
  173. }
  174. }
  175. if (wpa_config_set(ssid, pos, pos2, *line) < 0)
  176. errors++;
  177. }
  178. if (!end) {
  179. wpa_printf(MSG_ERROR, "Line %d: network block was not "
  180. "terminated properly.", *line);
  181. errors++;
  182. }
  183. errors += wpa_config_validate_network(ssid, *line);
  184. if (errors) {
  185. wpa_config_free_ssid(ssid);
  186. ssid = NULL;
  187. }
  188. return ssid;
  189. }
  190. static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id)
  191. {
  192. struct wpa_cred *cred;
  193. int errors = 0, end = 0;
  194. char buf[256], *pos, *pos2;
  195. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line);
  196. cred = os_zalloc(sizeof(*cred));
  197. if (cred == NULL)
  198. return NULL;
  199. cred->id = id;
  200. cred->sim_num = DEFAULT_USER_SELECTED_SIM;
  201. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  202. if (os_strcmp(pos, "}") == 0) {
  203. end = 1;
  204. break;
  205. }
  206. pos2 = os_strchr(pos, '=');
  207. if (pos2 == NULL) {
  208. wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
  209. "'%s'.", *line, pos);
  210. errors++;
  211. continue;
  212. }
  213. *pos2++ = '\0';
  214. if (*pos2 == '"') {
  215. if (os_strchr(pos2 + 1, '"') == NULL) {
  216. wpa_printf(MSG_ERROR, "Line %d: invalid "
  217. "quotation '%s'.", *line, pos2);
  218. errors++;
  219. continue;
  220. }
  221. }
  222. if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
  223. errors++;
  224. }
  225. if (!end) {
  226. wpa_printf(MSG_ERROR, "Line %d: cred block was not "
  227. "terminated properly.", *line);
  228. errors++;
  229. }
  230. if (errors) {
  231. wpa_config_free_cred(cred);
  232. cred = NULL;
  233. }
  234. return cred;
  235. }
  236. #ifndef CONFIG_NO_CONFIG_BLOBS
  237. static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
  238. const char *name)
  239. {
  240. struct wpa_config_blob *blob;
  241. char buf[256], *pos;
  242. unsigned char *encoded = NULL, *nencoded;
  243. int end = 0;
  244. size_t encoded_len = 0, len;
  245. wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
  246. *line, name);
  247. while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
  248. if (os_strcmp(pos, "}") == 0) {
  249. end = 1;
  250. break;
  251. }
  252. len = os_strlen(pos);
  253. nencoded = os_realloc(encoded, encoded_len + len);
  254. if (nencoded == NULL) {
  255. wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
  256. "blob", *line);
  257. os_free(encoded);
  258. return NULL;
  259. }
  260. encoded = nencoded;
  261. os_memcpy(encoded + encoded_len, pos, len);
  262. encoded_len += len;
  263. }
  264. if (!end || !encoded) {
  265. wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
  266. "properly", *line);
  267. os_free(encoded);
  268. return NULL;
  269. }
  270. blob = os_zalloc(sizeof(*blob));
  271. if (blob == NULL) {
  272. os_free(encoded);
  273. return NULL;
  274. }
  275. blob->name = os_strdup(name);
  276. blob->data = base64_decode(encoded, encoded_len, &blob->len);
  277. os_free(encoded);
  278. if (blob->name == NULL || blob->data == NULL) {
  279. wpa_config_free_blob(blob);
  280. return NULL;
  281. }
  282. return blob;
  283. }
  284. static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
  285. int *line, char *bname)
  286. {
  287. char *name_end;
  288. struct wpa_config_blob *blob;
  289. name_end = os_strchr(bname, '=');
  290. if (name_end == NULL) {
  291. wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
  292. *line);
  293. return -1;
  294. }
  295. *name_end = '\0';
  296. blob = wpa_config_read_blob(f, line, bname);
  297. if (blob == NULL) {
  298. wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
  299. *line, bname);
  300. return -1;
  301. }
  302. wpa_config_set_blob(config, blob);
  303. return 0;
  304. }
  305. #endif /* CONFIG_NO_CONFIG_BLOBS */
  306. struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
  307. {
  308. FILE *f;
  309. char buf[512], *pos;
  310. int errors = 0, line = 0;
  311. struct wpa_ssid *ssid, *tail, *head;
  312. struct wpa_cred *cred, *cred_tail, *cred_head;
  313. struct wpa_config *config;
  314. int id = 0;
  315. int cred_id = 0;
  316. if (name == NULL)
  317. return NULL;
  318. if (cfgp)
  319. config = cfgp;
  320. else
  321. config = wpa_config_alloc_empty(NULL, NULL);
  322. if (config == NULL) {
  323. wpa_printf(MSG_ERROR, "Failed to allocate config file "
  324. "structure");
  325. return NULL;
  326. }
  327. tail = head = config->ssid;
  328. while (tail && tail->next)
  329. tail = tail->next;
  330. cred_tail = cred_head = config->cred;
  331. while (cred_tail && cred_tail->next)
  332. cred_tail = cred_tail->next;
  333. wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
  334. f = fopen(name, "r");
  335. if (f == NULL) {
  336. wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
  337. "error: %s", name, strerror(errno));
  338. os_free(config);
  339. return NULL;
  340. }
  341. while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
  342. if (os_strcmp(pos, "network={") == 0) {
  343. ssid = wpa_config_read_network(f, &line, id++);
  344. if (ssid == NULL) {
  345. wpa_printf(MSG_ERROR, "Line %d: failed to "
  346. "parse network block.", line);
  347. errors++;
  348. continue;
  349. }
  350. if (head == NULL) {
  351. head = tail = ssid;
  352. } else {
  353. tail->next = ssid;
  354. tail = ssid;
  355. }
  356. if (wpa_config_add_prio_network(config, ssid)) {
  357. wpa_printf(MSG_ERROR, "Line %d: failed to add "
  358. "network block to priority list.",
  359. line);
  360. errors++;
  361. continue;
  362. }
  363. } else if (os_strcmp(pos, "cred={") == 0) {
  364. cred = wpa_config_read_cred(f, &line, cred_id++);
  365. if (cred == NULL) {
  366. wpa_printf(MSG_ERROR, "Line %d: failed to "
  367. "parse cred block.", line);
  368. errors++;
  369. continue;
  370. }
  371. if (cred_head == NULL) {
  372. cred_head = cred_tail = cred;
  373. } else {
  374. cred_tail->next = cred;
  375. cred_tail = cred;
  376. }
  377. #ifndef CONFIG_NO_CONFIG_BLOBS
  378. } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
  379. if (wpa_config_process_blob(config, f, &line, pos + 12)
  380. < 0) {
  381. wpa_printf(MSG_ERROR, "Line %d: failed to "
  382. "process blob.", line);
  383. errors++;
  384. continue;
  385. }
  386. #endif /* CONFIG_NO_CONFIG_BLOBS */
  387. } else if (wpa_config_process_global(config, pos, line) < 0) {
  388. wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
  389. "line '%s'.", line, pos);
  390. errors++;
  391. continue;
  392. }
  393. }
  394. fclose(f);
  395. config->ssid = head;
  396. wpa_config_debug_dump_networks(config);
  397. config->cred = cred_head;
  398. #ifndef WPA_IGNORE_CONFIG_ERRORS
  399. if (errors) {
  400. wpa_config_free(config);
  401. config = NULL;
  402. head = NULL;
  403. }
  404. #endif /* WPA_IGNORE_CONFIG_ERRORS */
  405. return config;
  406. }
  407. #ifndef CONFIG_NO_CONFIG_WRITE
  408. static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
  409. {
  410. char *value = wpa_config_get(ssid, field);
  411. if (value == NULL)
  412. return;
  413. fprintf(f, "\t%s=%s\n", field, value);
  414. os_free(value);
  415. }
  416. static void write_int(FILE *f, const char *field, int value, int def)
  417. {
  418. if (value == def)
  419. return;
  420. fprintf(f, "\t%s=%d\n", field, value);
  421. }
  422. static void write_bssid(FILE *f, struct wpa_ssid *ssid)
  423. {
  424. char *value = wpa_config_get(ssid, "bssid");
  425. if (value == NULL)
  426. return;
  427. fprintf(f, "\tbssid=%s\n", value);
  428. os_free(value);
  429. }
  430. static void write_bssid_hint(FILE *f, struct wpa_ssid *ssid)
  431. {
  432. char *value = wpa_config_get(ssid, "bssid_hint");
  433. if (!value)
  434. return;
  435. fprintf(f, "\tbssid_hint=%s\n", value);
  436. os_free(value);
  437. }
  438. static void write_psk(FILE *f, struct wpa_ssid *ssid)
  439. {
  440. char *value;
  441. if (ssid->mem_only_psk)
  442. return;
  443. value = wpa_config_get(ssid, "psk");
  444. if (value == NULL)
  445. return;
  446. fprintf(f, "\tpsk=%s\n", value);
  447. os_free(value);
  448. }
  449. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  450. {
  451. char *value;
  452. if (ssid->proto == DEFAULT_PROTO)
  453. return;
  454. value = wpa_config_get(ssid, "proto");
  455. if (value == NULL)
  456. return;
  457. if (value[0])
  458. fprintf(f, "\tproto=%s\n", value);
  459. os_free(value);
  460. }
  461. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  462. {
  463. char *value;
  464. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  465. return;
  466. value = wpa_config_get(ssid, "key_mgmt");
  467. if (value == NULL)
  468. return;
  469. if (value[0])
  470. fprintf(f, "\tkey_mgmt=%s\n", value);
  471. os_free(value);
  472. }
  473. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  474. {
  475. char *value;
  476. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  477. return;
  478. value = wpa_config_get(ssid, "pairwise");
  479. if (value == NULL)
  480. return;
  481. if (value[0])
  482. fprintf(f, "\tpairwise=%s\n", value);
  483. os_free(value);
  484. }
  485. static void write_group(FILE *f, struct wpa_ssid *ssid)
  486. {
  487. char *value;
  488. if (ssid->group_cipher == DEFAULT_GROUP)
  489. return;
  490. value = wpa_config_get(ssid, "group");
  491. if (value == NULL)
  492. return;
  493. if (value[0])
  494. fprintf(f, "\tgroup=%s\n", value);
  495. os_free(value);
  496. }
  497. static void write_group_mgmt(FILE *f, struct wpa_ssid *ssid)
  498. {
  499. char *value;
  500. if (!ssid->group_mgmt_cipher)
  501. return;
  502. value = wpa_config_get(ssid, "group_mgmt");
  503. if (!value)
  504. return;
  505. if (value[0])
  506. fprintf(f, "\tgroup_mgmt=%s\n", value);
  507. os_free(value);
  508. }
  509. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  510. {
  511. char *value;
  512. if (ssid->auth_alg == 0)
  513. return;
  514. value = wpa_config_get(ssid, "auth_alg");
  515. if (value == NULL)
  516. return;
  517. if (value[0])
  518. fprintf(f, "\tauth_alg=%s\n", value);
  519. os_free(value);
  520. }
  521. #ifdef IEEE8021X_EAPOL
  522. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  523. {
  524. char *value;
  525. value = wpa_config_get(ssid, "eap");
  526. if (value == NULL)
  527. return;
  528. if (value[0])
  529. fprintf(f, "\teap=%s\n", value);
  530. os_free(value);
  531. }
  532. #endif /* IEEE8021X_EAPOL */
  533. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  534. {
  535. char field[20], *value;
  536. int res;
  537. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  538. if (os_snprintf_error(sizeof(field), res))
  539. return;
  540. value = wpa_config_get(ssid, field);
  541. if (value) {
  542. fprintf(f, "\t%s=%s\n", field, value);
  543. os_free(value);
  544. }
  545. }
  546. #ifdef CONFIG_P2P
  547. static void write_go_p2p_dev_addr(FILE *f, struct wpa_ssid *ssid)
  548. {
  549. char *value = wpa_config_get(ssid, "go_p2p_dev_addr");
  550. if (value == NULL)
  551. return;
  552. fprintf(f, "\tgo_p2p_dev_addr=%s\n", value);
  553. os_free(value);
  554. }
  555. static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
  556. {
  557. char *value = wpa_config_get(ssid, "p2p_client_list");
  558. if (value == NULL)
  559. return;
  560. fprintf(f, "\tp2p_client_list=%s\n", value);
  561. os_free(value);
  562. }
  563. static void write_psk_list(FILE *f, struct wpa_ssid *ssid)
  564. {
  565. struct psk_list_entry *psk;
  566. char hex[32 * 2 + 1];
  567. dl_list_for_each(psk, &ssid->psk_list, struct psk_list_entry, list) {
  568. wpa_snprintf_hex(hex, sizeof(hex), psk->psk, sizeof(psk->psk));
  569. fprintf(f, "\tpsk_list=%s" MACSTR "-%s\n",
  570. psk->p2p ? "P2P-" : "", MAC2STR(psk->addr), hex);
  571. }
  572. }
  573. #endif /* CONFIG_P2P */
  574. #ifdef CONFIG_MACSEC
  575. static void write_mka_cak(FILE *f, struct wpa_ssid *ssid)
  576. {
  577. char *value;
  578. if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
  579. return;
  580. value = wpa_config_get(ssid, "mka_cak");
  581. if (!value)
  582. return;
  583. fprintf(f, "\tmka_cak=%s\n", value);
  584. os_free(value);
  585. }
  586. static void write_mka_ckn(FILE *f, struct wpa_ssid *ssid)
  587. {
  588. char *value;
  589. if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
  590. return;
  591. value = wpa_config_get(ssid, "mka_ckn");
  592. if (!value)
  593. return;
  594. fprintf(f, "\tmka_ckn=%s\n", value);
  595. os_free(value);
  596. }
  597. #endif /* CONFIG_MACSEC */
  598. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  599. {
  600. int i;
  601. #define STR(t) write_str(f, #t, ssid)
  602. #define INT(t) write_int(f, #t, ssid->t, 0)
  603. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  604. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  605. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  606. STR(ssid);
  607. INT(scan_ssid);
  608. write_bssid(f, ssid);
  609. write_bssid_hint(f, ssid);
  610. write_str(f, "bssid_blacklist", ssid);
  611. write_str(f, "bssid_whitelist", ssid);
  612. write_psk(f, ssid);
  613. INT(mem_only_psk);
  614. STR(sae_password);
  615. write_proto(f, ssid);
  616. write_key_mgmt(f, ssid);
  617. INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
  618. write_pairwise(f, ssid);
  619. write_group(f, ssid);
  620. write_group_mgmt(f, ssid);
  621. write_auth_alg(f, ssid);
  622. STR(bgscan);
  623. STR(autoscan);
  624. STR(scan_freq);
  625. #ifdef IEEE8021X_EAPOL
  626. write_eap(f, ssid);
  627. STR(identity);
  628. STR(anonymous_identity);
  629. STR(password);
  630. STR(ca_cert);
  631. STR(ca_path);
  632. STR(client_cert);
  633. STR(private_key);
  634. STR(private_key_passwd);
  635. STR(dh_file);
  636. STR(subject_match);
  637. STR(altsubject_match);
  638. STR(domain_suffix_match);
  639. STR(domain_match);
  640. STR(ca_cert2);
  641. STR(ca_path2);
  642. STR(client_cert2);
  643. STR(private_key2);
  644. STR(private_key2_passwd);
  645. STR(dh_file2);
  646. STR(subject_match2);
  647. STR(altsubject_match2);
  648. STR(domain_suffix_match2);
  649. STR(domain_match2);
  650. STR(phase1);
  651. STR(phase2);
  652. STR(pcsc);
  653. STR(pin);
  654. STR(engine_id);
  655. STR(key_id);
  656. STR(cert_id);
  657. STR(ca_cert_id);
  658. STR(key2_id);
  659. STR(pin2);
  660. STR(engine2_id);
  661. STR(cert2_id);
  662. STR(ca_cert2_id);
  663. INTe(engine);
  664. INTe(engine2);
  665. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  666. STR(openssl_ciphers);
  667. INTe(erp);
  668. #endif /* IEEE8021X_EAPOL */
  669. for (i = 0; i < 4; i++)
  670. write_wep_key(f, i, ssid);
  671. INT(wep_tx_keyidx);
  672. INT(priority);
  673. #ifdef IEEE8021X_EAPOL
  674. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  675. STR(pac_file);
  676. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  677. INTe(ocsp);
  678. INT_DEFe(sim_num, DEFAULT_USER_SELECTED_SIM);
  679. #endif /* IEEE8021X_EAPOL */
  680. INT(mode);
  681. INT(no_auto_peer);
  682. INT(frequency);
  683. INT(fixed_freq);
  684. #ifdef CONFIG_ACS
  685. INT(acs);
  686. #endif /* CONFIG_ACS */
  687. write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
  688. INT(disabled);
  689. INT(peerkey);
  690. INT(mixed_cell);
  691. INT(vht);
  692. INT_DEF(ht, 1);
  693. INT(ht40);
  694. INT(max_oper_chwidth);
  695. INT(vht_center_freq1);
  696. INT(vht_center_freq2);
  697. INT(pbss);
  698. INT(wps_disabled);
  699. INT(fils_dh_group);
  700. #ifdef CONFIG_IEEE80211W
  701. write_int(f, "ieee80211w", ssid->ieee80211w,
  702. MGMT_FRAME_PROTECTION_DEFAULT);
  703. #endif /* CONFIG_IEEE80211W */
  704. STR(id_str);
  705. #ifdef CONFIG_P2P
  706. write_go_p2p_dev_addr(f, ssid);
  707. write_p2p_client_list(f, ssid);
  708. write_psk_list(f, ssid);
  709. #endif /* CONFIG_P2P */
  710. INT(ap_max_inactivity);
  711. INT(dtim_period);
  712. INT(beacon_int);
  713. #ifdef CONFIG_MACSEC
  714. INT(macsec_policy);
  715. write_mka_cak(f, ssid);
  716. write_mka_ckn(f, ssid);
  717. INT(macsec_integ_only);
  718. INT(macsec_port);
  719. INT_DEF(mka_priority, DEFAULT_PRIO_NOT_KEY_SERVER);
  720. #endif /* CONFIG_MACSEC */
  721. #ifdef CONFIG_HS20
  722. INT(update_identifier);
  723. #endif /* CONFIG_HS20 */
  724. write_int(f, "mac_addr", ssid->mac_addr, -1);
  725. #ifdef CONFIG_MESH
  726. STR(mesh_basic_rates);
  727. INT_DEF(dot11MeshMaxRetries, DEFAULT_MESH_MAX_RETRIES);
  728. INT_DEF(dot11MeshRetryTimeout, DEFAULT_MESH_RETRY_TIMEOUT);
  729. INT_DEF(dot11MeshConfirmTimeout, DEFAULT_MESH_CONFIRM_TIMEOUT);
  730. INT_DEF(dot11MeshHoldingTimeout, DEFAULT_MESH_HOLDING_TIMEOUT);
  731. INT_DEF(mesh_rssi_threshold, DEFAULT_MESH_RSSI_THRESHOLD);
  732. #endif /* CONFIG_MESH */
  733. INT(wpa_ptk_rekey);
  734. INT(group_rekey);
  735. INT(ignore_broadcast_ssid);
  736. #ifdef CONFIG_DPP
  737. STR(dpp_connector);
  738. STR(dpp_netaccesskey);
  739. INT(dpp_netaccesskey_expiry);
  740. STR(dpp_csign);
  741. #endif /* CONFIG_DPP */
  742. INT(owe_group);
  743. #ifdef CONFIG_HT_OVERRIDES
  744. INT_DEF(disable_ht, DEFAULT_DISABLE_HT);
  745. INT_DEF(disable_ht40, DEFAULT_DISABLE_HT40);
  746. INT_DEF(disable_sgi, DEFAULT_DISABLE_SGI);
  747. INT_DEF(disable_ldpc, DEFAULT_DISABLE_LDPC);
  748. INT(ht40_intolerant);
  749. INT_DEF(disable_max_amsdu, DEFAULT_DISABLE_MAX_AMSDU);
  750. INT_DEF(ampdu_factor, DEFAULT_AMPDU_FACTOR);
  751. INT_DEF(ampdu_density, DEFAULT_AMPDU_DENSITY);
  752. STR(ht_mcs);
  753. #endif /* CONFIG_HT_OVERRIDES */
  754. #ifdef CONFIG_VHT_OVERRIDES
  755. INT(disable_vht);
  756. INT(vht_capa);
  757. INT(vht_capa_mask);
  758. INT_DEF(vht_rx_mcs_nss_1, -1);
  759. INT_DEF(vht_rx_mcs_nss_2, -1);
  760. INT_DEF(vht_rx_mcs_nss_3, -1);
  761. INT_DEF(vht_rx_mcs_nss_4, -1);
  762. INT_DEF(vht_rx_mcs_nss_5, -1);
  763. INT_DEF(vht_rx_mcs_nss_6, -1);
  764. INT_DEF(vht_rx_mcs_nss_7, -1);
  765. INT_DEF(vht_rx_mcs_nss_8, -1);
  766. INT_DEF(vht_tx_mcs_nss_1, -1);
  767. INT_DEF(vht_tx_mcs_nss_2, -1);
  768. INT_DEF(vht_tx_mcs_nss_3, -1);
  769. INT_DEF(vht_tx_mcs_nss_4, -1);
  770. INT_DEF(vht_tx_mcs_nss_5, -1);
  771. INT_DEF(vht_tx_mcs_nss_6, -1);
  772. INT_DEF(vht_tx_mcs_nss_7, -1);
  773. INT_DEF(vht_tx_mcs_nss_8, -1);
  774. #endif /* CONFIG_VHT_OVERRIDES */
  775. #undef STR
  776. #undef INT
  777. #undef INT_DEF
  778. }
  779. static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
  780. {
  781. size_t i;
  782. if (cred->priority)
  783. fprintf(f, "\tpriority=%d\n", cred->priority);
  784. if (cred->pcsc)
  785. fprintf(f, "\tpcsc=%d\n", cred->pcsc);
  786. if (cred->realm)
  787. fprintf(f, "\trealm=\"%s\"\n", cred->realm);
  788. if (cred->username)
  789. fprintf(f, "\tusername=\"%s\"\n", cred->username);
  790. if (cred->password && cred->ext_password)
  791. fprintf(f, "\tpassword=ext:%s\n", cred->password);
  792. else if (cred->password)
  793. fprintf(f, "\tpassword=\"%s\"\n", cred->password);
  794. if (cred->ca_cert)
  795. fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
  796. if (cred->client_cert)
  797. fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
  798. if (cred->private_key)
  799. fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
  800. if (cred->private_key_passwd)
  801. fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
  802. cred->private_key_passwd);
  803. if (cred->imsi)
  804. fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
  805. if (cred->milenage)
  806. fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
  807. for (i = 0; i < cred->num_domain; i++)
  808. fprintf(f, "\tdomain=\"%s\"\n", cred->domain[i]);
  809. if (cred->domain_suffix_match)
  810. fprintf(f, "\tdomain_suffix_match=\"%s\"\n",
  811. cred->domain_suffix_match);
  812. if (cred->roaming_consortium_len) {
  813. fprintf(f, "\troaming_consortium=");
  814. for (i = 0; i < cred->roaming_consortium_len; i++)
  815. fprintf(f, "%02x", cred->roaming_consortium[i]);
  816. fprintf(f, "\n");
  817. }
  818. if (cred->eap_method) {
  819. const char *name;
  820. name = eap_get_name(cred->eap_method[0].vendor,
  821. cred->eap_method[0].method);
  822. if (name)
  823. fprintf(f, "\teap=%s\n", name);
  824. }
  825. if (cred->phase1)
  826. fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
  827. if (cred->phase2)
  828. fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
  829. if (cred->excluded_ssid) {
  830. size_t j;
  831. for (i = 0; i < cred->num_excluded_ssid; i++) {
  832. struct excluded_ssid *e = &cred->excluded_ssid[i];
  833. fprintf(f, "\texcluded_ssid=");
  834. for (j = 0; j < e->ssid_len; j++)
  835. fprintf(f, "%02x", e->ssid[j]);
  836. fprintf(f, "\n");
  837. }
  838. }
  839. if (cred->roaming_partner) {
  840. for (i = 0; i < cred->num_roaming_partner; i++) {
  841. struct roaming_partner *p = &cred->roaming_partner[i];
  842. fprintf(f, "\troaming_partner=\"%s,%d,%u,%s\"\n",
  843. p->fqdn, p->exact_match, p->priority,
  844. p->country);
  845. }
  846. }
  847. if (cred->update_identifier)
  848. fprintf(f, "\tupdate_identifier=%d\n", cred->update_identifier);
  849. if (cred->provisioning_sp)
  850. fprintf(f, "\tprovisioning_sp=\"%s\"\n", cred->provisioning_sp);
  851. if (cred->sp_priority)
  852. fprintf(f, "\tsp_priority=%d\n", cred->sp_priority);
  853. if (cred->min_dl_bandwidth_home)
  854. fprintf(f, "\tmin_dl_bandwidth_home=%u\n",
  855. cred->min_dl_bandwidth_home);
  856. if (cred->min_ul_bandwidth_home)
  857. fprintf(f, "\tmin_ul_bandwidth_home=%u\n",
  858. cred->min_ul_bandwidth_home);
  859. if (cred->min_dl_bandwidth_roaming)
  860. fprintf(f, "\tmin_dl_bandwidth_roaming=%u\n",
  861. cred->min_dl_bandwidth_roaming);
  862. if (cred->min_ul_bandwidth_roaming)
  863. fprintf(f, "\tmin_ul_bandwidth_roaming=%u\n",
  864. cred->min_ul_bandwidth_roaming);
  865. if (cred->max_bss_load)
  866. fprintf(f, "\tmax_bss_load=%u\n",
  867. cred->max_bss_load);
  868. if (cred->ocsp)
  869. fprintf(f, "\tocsp=%d\n", cred->ocsp);
  870. if (cred->num_req_conn_capab) {
  871. for (i = 0; i < cred->num_req_conn_capab; i++) {
  872. int *ports;
  873. fprintf(f, "\treq_conn_capab=%u",
  874. cred->req_conn_capab_proto[i]);
  875. ports = cred->req_conn_capab_port[i];
  876. if (ports) {
  877. int j;
  878. for (j = 0; ports[j] != -1; j++) {
  879. fprintf(f, "%s%d", j > 0 ? "," : ":",
  880. ports[j]);
  881. }
  882. }
  883. fprintf(f, "\n");
  884. }
  885. }
  886. if (cred->required_roaming_consortium_len) {
  887. fprintf(f, "\trequired_roaming_consortium=");
  888. for (i = 0; i < cred->required_roaming_consortium_len; i++)
  889. fprintf(f, "%02x",
  890. cred->required_roaming_consortium[i]);
  891. fprintf(f, "\n");
  892. }
  893. if (cred->sim_num != DEFAULT_USER_SELECTED_SIM)
  894. fprintf(f, "\tsim_num=%d\n", cred->sim_num);
  895. }
  896. #ifndef CONFIG_NO_CONFIG_BLOBS
  897. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  898. {
  899. unsigned char *encoded;
  900. encoded = base64_encode(blob->data, blob->len, NULL);
  901. if (encoded == NULL)
  902. return -1;
  903. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  904. os_free(encoded);
  905. return 0;
  906. }
  907. #endif /* CONFIG_NO_CONFIG_BLOBS */
  908. static void write_global_bin(FILE *f, const char *field,
  909. const struct wpabuf *val)
  910. {
  911. size_t i;
  912. const u8 *pos;
  913. if (val == NULL)
  914. return;
  915. fprintf(f, "%s=", field);
  916. pos = wpabuf_head(val);
  917. for (i = 0; i < wpabuf_len(val); i++)
  918. fprintf(f, "%02X", *pos++);
  919. fprintf(f, "\n");
  920. }
  921. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  922. {
  923. #ifdef CONFIG_CTRL_IFACE
  924. if (config->ctrl_interface)
  925. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  926. if (config->ctrl_interface_group)
  927. fprintf(f, "ctrl_interface_group=%s\n",
  928. config->ctrl_interface_group);
  929. #endif /* CONFIG_CTRL_IFACE */
  930. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  931. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  932. if (config->ap_scan != DEFAULT_AP_SCAN)
  933. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  934. if (config->disable_scan_offload)
  935. fprintf(f, "disable_scan_offload=%d\n",
  936. config->disable_scan_offload);
  937. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  938. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  939. if (config->opensc_engine_path)
  940. fprintf(f, "opensc_engine_path=%s\n",
  941. config->opensc_engine_path);
  942. if (config->pkcs11_engine_path)
  943. fprintf(f, "pkcs11_engine_path=%s\n",
  944. config->pkcs11_engine_path);
  945. if (config->pkcs11_module_path)
  946. fprintf(f, "pkcs11_module_path=%s\n",
  947. config->pkcs11_module_path);
  948. if (config->openssl_ciphers)
  949. fprintf(f, "openssl_ciphers=%s\n", config->openssl_ciphers);
  950. if (config->pcsc_reader)
  951. fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
  952. if (config->pcsc_pin)
  953. fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
  954. if (config->driver_param)
  955. fprintf(f, "driver_param=%s\n", config->driver_param);
  956. if (config->dot11RSNAConfigPMKLifetime)
  957. fprintf(f, "dot11RSNAConfigPMKLifetime=%u\n",
  958. config->dot11RSNAConfigPMKLifetime);
  959. if (config->dot11RSNAConfigPMKReauthThreshold)
  960. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%u\n",
  961. config->dot11RSNAConfigPMKReauthThreshold);
  962. if (config->dot11RSNAConfigSATimeout)
  963. fprintf(f, "dot11RSNAConfigSATimeout=%u\n",
  964. config->dot11RSNAConfigSATimeout);
  965. if (config->update_config)
  966. fprintf(f, "update_config=%d\n", config->update_config);
  967. #ifdef CONFIG_WPS
  968. if (!is_nil_uuid(config->uuid)) {
  969. char buf[40];
  970. uuid_bin2str(config->uuid, buf, sizeof(buf));
  971. fprintf(f, "uuid=%s\n", buf);
  972. }
  973. if (config->auto_uuid)
  974. fprintf(f, "auto_uuid=%d\n", config->auto_uuid);
  975. if (config->device_name)
  976. fprintf(f, "device_name=%s\n", config->device_name);
  977. if (config->manufacturer)
  978. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  979. if (config->model_name)
  980. fprintf(f, "model_name=%s\n", config->model_name);
  981. if (config->model_number)
  982. fprintf(f, "model_number=%s\n", config->model_number);
  983. if (config->serial_number)
  984. fprintf(f, "serial_number=%s\n", config->serial_number);
  985. {
  986. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  987. buf = wps_dev_type_bin2str(config->device_type,
  988. _buf, sizeof(_buf));
  989. if (os_strcmp(buf, "0-00000000-0") != 0)
  990. fprintf(f, "device_type=%s\n", buf);
  991. }
  992. if (WPA_GET_BE32(config->os_version))
  993. fprintf(f, "os_version=%08x\n",
  994. WPA_GET_BE32(config->os_version));
  995. if (config->config_methods)
  996. fprintf(f, "config_methods=%s\n", config->config_methods);
  997. if (config->wps_cred_processing)
  998. fprintf(f, "wps_cred_processing=%d\n",
  999. config->wps_cred_processing);
  1000. if (config->wps_vendor_ext_m1) {
  1001. int i, len = wpabuf_len(config->wps_vendor_ext_m1);
  1002. const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
  1003. if (len > 0) {
  1004. fprintf(f, "wps_vendor_ext_m1=");
  1005. for (i = 0; i < len; i++)
  1006. fprintf(f, "%02x", *p++);
  1007. fprintf(f, "\n");
  1008. }
  1009. }
  1010. #endif /* CONFIG_WPS */
  1011. #ifdef CONFIG_P2P
  1012. {
  1013. int i;
  1014. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  1015. for (i = 0; i < config->num_sec_device_types; i++) {
  1016. buf = wps_dev_type_bin2str(config->sec_device_type[i],
  1017. _buf, sizeof(_buf));
  1018. if (buf)
  1019. fprintf(f, "sec_device_type=%s\n", buf);
  1020. }
  1021. }
  1022. if (config->p2p_listen_reg_class)
  1023. fprintf(f, "p2p_listen_reg_class=%d\n",
  1024. config->p2p_listen_reg_class);
  1025. if (config->p2p_listen_channel)
  1026. fprintf(f, "p2p_listen_channel=%d\n",
  1027. config->p2p_listen_channel);
  1028. if (config->p2p_oper_reg_class)
  1029. fprintf(f, "p2p_oper_reg_class=%d\n",
  1030. config->p2p_oper_reg_class);
  1031. if (config->p2p_oper_channel)
  1032. fprintf(f, "p2p_oper_channel=%d\n", config->p2p_oper_channel);
  1033. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  1034. fprintf(f, "p2p_go_intent=%d\n", config->p2p_go_intent);
  1035. if (config->p2p_ssid_postfix)
  1036. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  1037. if (config->persistent_reconnect)
  1038. fprintf(f, "persistent_reconnect=%d\n",
  1039. config->persistent_reconnect);
  1040. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  1041. fprintf(f, "p2p_intra_bss=%d\n", config->p2p_intra_bss);
  1042. if (config->p2p_group_idle)
  1043. fprintf(f, "p2p_group_idle=%d\n", config->p2p_group_idle);
  1044. if (config->p2p_passphrase_len)
  1045. fprintf(f, "p2p_passphrase_len=%u\n",
  1046. config->p2p_passphrase_len);
  1047. if (config->p2p_pref_chan) {
  1048. unsigned int i;
  1049. fprintf(f, "p2p_pref_chan=");
  1050. for (i = 0; i < config->num_p2p_pref_chan; i++) {
  1051. fprintf(f, "%s%u:%u", i > 0 ? "," : "",
  1052. config->p2p_pref_chan[i].op_class,
  1053. config->p2p_pref_chan[i].chan);
  1054. }
  1055. fprintf(f, "\n");
  1056. }
  1057. if (config->p2p_no_go_freq.num) {
  1058. char *val = freq_range_list_str(&config->p2p_no_go_freq);
  1059. if (val) {
  1060. fprintf(f, "p2p_no_go_freq=%s\n", val);
  1061. os_free(val);
  1062. }
  1063. }
  1064. if (config->p2p_add_cli_chan)
  1065. fprintf(f, "p2p_add_cli_chan=%d\n", config->p2p_add_cli_chan);
  1066. if (config->p2p_optimize_listen_chan !=
  1067. DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN)
  1068. fprintf(f, "p2p_optimize_listen_chan=%d\n",
  1069. config->p2p_optimize_listen_chan);
  1070. if (config->p2p_go_ht40)
  1071. fprintf(f, "p2p_go_ht40=%d\n", config->p2p_go_ht40);
  1072. if (config->p2p_go_vht)
  1073. fprintf(f, "p2p_go_vht=%d\n", config->p2p_go_vht);
  1074. if (config->p2p_go_ctwindow != DEFAULT_P2P_GO_CTWINDOW)
  1075. fprintf(f, "p2p_go_ctwindow=%d\n", config->p2p_go_ctwindow);
  1076. if (config->p2p_disabled)
  1077. fprintf(f, "p2p_disabled=%d\n", config->p2p_disabled);
  1078. if (config->p2p_no_group_iface)
  1079. fprintf(f, "p2p_no_group_iface=%d\n",
  1080. config->p2p_no_group_iface);
  1081. if (config->p2p_ignore_shared_freq)
  1082. fprintf(f, "p2p_ignore_shared_freq=%d\n",
  1083. config->p2p_ignore_shared_freq);
  1084. if (config->p2p_cli_probe)
  1085. fprintf(f, "p2p_cli_probe=%d\n", config->p2p_cli_probe);
  1086. if (config->p2p_go_freq_change_policy != DEFAULT_P2P_GO_FREQ_MOVE)
  1087. fprintf(f, "p2p_go_freq_change_policy=%u\n",
  1088. config->p2p_go_freq_change_policy);
  1089. if (WPA_GET_BE32(config->ip_addr_go))
  1090. fprintf(f, "ip_addr_go=%u.%u.%u.%u\n",
  1091. config->ip_addr_go[0], config->ip_addr_go[1],
  1092. config->ip_addr_go[2], config->ip_addr_go[3]);
  1093. if (WPA_GET_BE32(config->ip_addr_mask))
  1094. fprintf(f, "ip_addr_mask=%u.%u.%u.%u\n",
  1095. config->ip_addr_mask[0], config->ip_addr_mask[1],
  1096. config->ip_addr_mask[2], config->ip_addr_mask[3]);
  1097. if (WPA_GET_BE32(config->ip_addr_start))
  1098. fprintf(f, "ip_addr_start=%u.%u.%u.%u\n",
  1099. config->ip_addr_start[0], config->ip_addr_start[1],
  1100. config->ip_addr_start[2], config->ip_addr_start[3]);
  1101. if (WPA_GET_BE32(config->ip_addr_end))
  1102. fprintf(f, "ip_addr_end=%u.%u.%u.%u\n",
  1103. config->ip_addr_end[0], config->ip_addr_end[1],
  1104. config->ip_addr_end[2], config->ip_addr_end[3]);
  1105. #endif /* CONFIG_P2P */
  1106. if (config->country[0] && config->country[1]) {
  1107. fprintf(f, "country=%c%c\n",
  1108. config->country[0], config->country[1]);
  1109. }
  1110. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  1111. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  1112. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  1113. fprintf(f, "bss_expiration_age=%u\n",
  1114. config->bss_expiration_age);
  1115. if (config->bss_expiration_scan_count !=
  1116. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  1117. fprintf(f, "bss_expiration_scan_count=%u\n",
  1118. config->bss_expiration_scan_count);
  1119. if (config->filter_ssids)
  1120. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  1121. if (config->filter_rssi)
  1122. fprintf(f, "filter_rssi=%d\n", config->filter_rssi);
  1123. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  1124. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  1125. if (config->disassoc_low_ack)
  1126. fprintf(f, "disassoc_low_ack=%d\n", config->disassoc_low_ack);
  1127. #ifdef CONFIG_HS20
  1128. if (config->hs20)
  1129. fprintf(f, "hs20=1\n");
  1130. #endif /* CONFIG_HS20 */
  1131. #ifdef CONFIG_INTERWORKING
  1132. if (config->interworking)
  1133. fprintf(f, "interworking=%d\n", config->interworking);
  1134. if (!is_zero_ether_addr(config->hessid))
  1135. fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
  1136. if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
  1137. fprintf(f, "access_network_type=%d\n",
  1138. config->access_network_type);
  1139. if (config->go_interworking)
  1140. fprintf(f, "go_interworking=%d\n", config->go_interworking);
  1141. if (config->go_access_network_type)
  1142. fprintf(f, "go_access_network_type=%d\n",
  1143. config->go_access_network_type);
  1144. if (config->go_internet)
  1145. fprintf(f, "go_internet=%d\n", config->go_internet);
  1146. if (config->go_venue_group)
  1147. fprintf(f, "go_venue_group=%d\n", config->go_venue_group);
  1148. if (config->go_venue_type)
  1149. fprintf(f, "go_venue_type=%d\n", config->go_venue_type);
  1150. #endif /* CONFIG_INTERWORKING */
  1151. if (config->pbc_in_m1)
  1152. fprintf(f, "pbc_in_m1=%d\n", config->pbc_in_m1);
  1153. if (config->wps_nfc_pw_from_config) {
  1154. if (config->wps_nfc_dev_pw_id)
  1155. fprintf(f, "wps_nfc_dev_pw_id=%d\n",
  1156. config->wps_nfc_dev_pw_id);
  1157. write_global_bin(f, "wps_nfc_dh_pubkey",
  1158. config->wps_nfc_dh_pubkey);
  1159. write_global_bin(f, "wps_nfc_dh_privkey",
  1160. config->wps_nfc_dh_privkey);
  1161. write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
  1162. }
  1163. if (config->ext_password_backend)
  1164. fprintf(f, "ext_password_backend=%s\n",
  1165. config->ext_password_backend);
  1166. if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
  1167. fprintf(f, "p2p_go_max_inactivity=%d\n",
  1168. config->p2p_go_max_inactivity);
  1169. if (config->auto_interworking)
  1170. fprintf(f, "auto_interworking=%d\n",
  1171. config->auto_interworking);
  1172. if (config->okc)
  1173. fprintf(f, "okc=%d\n", config->okc);
  1174. if (config->pmf)
  1175. fprintf(f, "pmf=%d\n", config->pmf);
  1176. if (config->dtim_period)
  1177. fprintf(f, "dtim_period=%d\n", config->dtim_period);
  1178. if (config->beacon_int)
  1179. fprintf(f, "beacon_int=%d\n", config->beacon_int);
  1180. if (config->sae_groups) {
  1181. int i;
  1182. fprintf(f, "sae_groups=");
  1183. for (i = 0; config->sae_groups[i] > 0; i++) {
  1184. fprintf(f, "%s%d", i > 0 ? " " : "",
  1185. config->sae_groups[i]);
  1186. }
  1187. fprintf(f, "\n");
  1188. }
  1189. if (config->ap_vendor_elements) {
  1190. int i, len = wpabuf_len(config->ap_vendor_elements);
  1191. const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
  1192. if (len > 0) {
  1193. fprintf(f, "ap_vendor_elements=");
  1194. for (i = 0; i < len; i++)
  1195. fprintf(f, "%02x", *p++);
  1196. fprintf(f, "\n");
  1197. }
  1198. }
  1199. if (config->ignore_old_scan_res)
  1200. fprintf(f, "ignore_old_scan_res=%d\n",
  1201. config->ignore_old_scan_res);
  1202. if (config->freq_list && config->freq_list[0]) {
  1203. int i;
  1204. fprintf(f, "freq_list=");
  1205. for (i = 0; config->freq_list[i]; i++) {
  1206. fprintf(f, "%s%d", i > 0 ? " " : "",
  1207. config->freq_list[i]);
  1208. }
  1209. fprintf(f, "\n");
  1210. }
  1211. if (config->scan_cur_freq != DEFAULT_SCAN_CUR_FREQ)
  1212. fprintf(f, "scan_cur_freq=%d\n", config->scan_cur_freq);
  1213. if (config->sched_scan_interval)
  1214. fprintf(f, "sched_scan_interval=%u\n",
  1215. config->sched_scan_interval);
  1216. if (config->sched_scan_start_delay)
  1217. fprintf(f, "sched_scan_start_delay=%u\n",
  1218. config->sched_scan_start_delay);
  1219. if (config->external_sim)
  1220. fprintf(f, "external_sim=%d\n", config->external_sim);
  1221. if (config->tdls_external_control)
  1222. fprintf(f, "tdls_external_control=%d\n",
  1223. config->tdls_external_control);
  1224. if (config->wowlan_triggers)
  1225. fprintf(f, "wowlan_triggers=%s\n",
  1226. config->wowlan_triggers);
  1227. if (config->bgscan)
  1228. fprintf(f, "bgscan=\"%s\"\n", config->bgscan);
  1229. if (config->autoscan)
  1230. fprintf(f, "autoscan=%s\n", config->autoscan);
  1231. if (config->p2p_search_delay != DEFAULT_P2P_SEARCH_DELAY)
  1232. fprintf(f, "p2p_search_delay=%u\n",
  1233. config->p2p_search_delay);
  1234. if (config->mac_addr)
  1235. fprintf(f, "mac_addr=%d\n", config->mac_addr);
  1236. if (config->rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
  1237. fprintf(f, "rand_addr_lifetime=%u\n",
  1238. config->rand_addr_lifetime);
  1239. if (config->preassoc_mac_addr)
  1240. fprintf(f, "preassoc_mac_addr=%d\n", config->preassoc_mac_addr);
  1241. if (config->key_mgmt_offload != DEFAULT_KEY_MGMT_OFFLOAD)
  1242. fprintf(f, "key_mgmt_offload=%d\n", config->key_mgmt_offload);
  1243. if (config->user_mpm != DEFAULT_USER_MPM)
  1244. fprintf(f, "user_mpm=%d\n", config->user_mpm);
  1245. if (config->max_peer_links != DEFAULT_MAX_PEER_LINKS)
  1246. fprintf(f, "max_peer_links=%d\n", config->max_peer_links);
  1247. if (config->cert_in_cb != DEFAULT_CERT_IN_CB)
  1248. fprintf(f, "cert_in_cb=%d\n", config->cert_in_cb);
  1249. if (config->mesh_max_inactivity != DEFAULT_MESH_MAX_INACTIVITY)
  1250. fprintf(f, "mesh_max_inactivity=%d\n",
  1251. config->mesh_max_inactivity);
  1252. if (config->dot11RSNASAERetransPeriod !=
  1253. DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD)
  1254. fprintf(f, "dot11RSNASAERetransPeriod=%d\n",
  1255. config->dot11RSNASAERetransPeriod);
  1256. if (config->passive_scan)
  1257. fprintf(f, "passive_scan=%d\n", config->passive_scan);
  1258. if (config->reassoc_same_bss_optim)
  1259. fprintf(f, "reassoc_same_bss_optim=%d\n",
  1260. config->reassoc_same_bss_optim);
  1261. if (config->wps_priority)
  1262. fprintf(f, "wps_priority=%d\n", config->wps_priority);
  1263. if (config->wpa_rsc_relaxation != DEFAULT_WPA_RSC_RELAXATION)
  1264. fprintf(f, "wpa_rsc_relaxation=%d\n",
  1265. config->wpa_rsc_relaxation);
  1266. if (config->sched_scan_plans)
  1267. fprintf(f, "sched_scan_plans=%s\n", config->sched_scan_plans);
  1268. #ifdef CONFIG_MBO
  1269. if (config->non_pref_chan)
  1270. fprintf(f, "non_pref_chan=%s\n", config->non_pref_chan);
  1271. if (config->mbo_cell_capa != DEFAULT_MBO_CELL_CAPA)
  1272. fprintf(f, "mbo_cell_capa=%u\n", config->mbo_cell_capa);
  1273. if (config->disassoc_imminent_rssi_threshold !=
  1274. DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD)
  1275. fprintf(f, "disassoc_imminent_rssi_threshold=%d\n",
  1276. config->disassoc_imminent_rssi_threshold);
  1277. if (config->oce != DEFAULT_OCE_SUPPORT)
  1278. fprintf(f, "oce=%u\n", config->oce);
  1279. #endif /* CONFIG_MBO */
  1280. if (config->gas_address3)
  1281. fprintf(f, "gas_address3=%d\n", config->gas_address3);
  1282. if (config->ftm_responder)
  1283. fprintf(f, "ftm_responder=%d\n", config->ftm_responder);
  1284. if (config->ftm_initiator)
  1285. fprintf(f, "ftm_initiator=%d\n", config->ftm_initiator);
  1286. if (config->osu_dir)
  1287. fprintf(f, "osu_dir=%s\n", config->osu_dir);
  1288. if (config->fst_group_id)
  1289. fprintf(f, "fst_group_id=%s\n", config->fst_group_id);
  1290. if (config->fst_priority)
  1291. fprintf(f, "fst_priority=%d\n", config->fst_priority);
  1292. if (config->fst_llt)
  1293. fprintf(f, "fst_llt=%d\n", config->fst_llt);
  1294. if (config->gas_rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
  1295. fprintf(f, "gas_rand_addr_lifetime=%u\n",
  1296. config->gas_rand_addr_lifetime);
  1297. if (config->gas_rand_mac_addr)
  1298. fprintf(f, "gas_rand_mac_addr=%d\n", config->gas_rand_mac_addr);
  1299. if (config->dpp_config_processing)
  1300. fprintf(f, "dpp_config_processing=%d\n",
  1301. config->dpp_config_processing);
  1302. }
  1303. #endif /* CONFIG_NO_CONFIG_WRITE */
  1304. int wpa_config_write(const char *name, struct wpa_config *config)
  1305. {
  1306. #ifndef CONFIG_NO_CONFIG_WRITE
  1307. FILE *f;
  1308. struct wpa_ssid *ssid;
  1309. struct wpa_cred *cred;
  1310. #ifndef CONFIG_NO_CONFIG_BLOBS
  1311. struct wpa_config_blob *blob;
  1312. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1313. int ret = 0;
  1314. const char *orig_name = name;
  1315. int tmp_len = os_strlen(name) + 5; /* allow space for .tmp suffix */
  1316. char *tmp_name = os_malloc(tmp_len);
  1317. if (tmp_name) {
  1318. os_snprintf(tmp_name, tmp_len, "%s.tmp", name);
  1319. name = tmp_name;
  1320. }
  1321. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  1322. f = fopen(name, "w");
  1323. if (f == NULL) {
  1324. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  1325. os_free(tmp_name);
  1326. return -1;
  1327. }
  1328. wpa_config_write_global(f, config);
  1329. for (cred = config->cred; cred; cred = cred->next) {
  1330. if (cred->temporary)
  1331. continue;
  1332. fprintf(f, "\ncred={\n");
  1333. wpa_config_write_cred(f, cred);
  1334. fprintf(f, "}\n");
  1335. }
  1336. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  1337. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  1338. continue; /* do not save temporary networks */
  1339. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  1340. !ssid->passphrase)
  1341. continue; /* do not save invalid network */
  1342. fprintf(f, "\nnetwork={\n");
  1343. wpa_config_write_network(f, ssid);
  1344. fprintf(f, "}\n");
  1345. }
  1346. #ifndef CONFIG_NO_CONFIG_BLOBS
  1347. for (blob = config->blobs; blob; blob = blob->next) {
  1348. ret = wpa_config_write_blob(f, blob);
  1349. if (ret)
  1350. break;
  1351. }
  1352. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1353. os_fdatasync(f);
  1354. fclose(f);
  1355. if (tmp_name) {
  1356. int chmod_ret = 0;
  1357. #ifdef ANDROID
  1358. chmod_ret = chmod(tmp_name,
  1359. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  1360. #endif /* ANDROID */
  1361. if (chmod_ret != 0 || rename(tmp_name, orig_name) != 0)
  1362. ret = -1;
  1363. os_free(tmp_name);
  1364. }
  1365. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  1366. orig_name, ret ? "un" : "");
  1367. return ret;
  1368. #else /* CONFIG_NO_CONFIG_WRITE */
  1369. return -1;
  1370. #endif /* CONFIG_NO_CONFIG_WRITE */
  1371. }