config_file.c 38 KB

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