config_file.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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) {
  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_psk(FILE *f, struct wpa_ssid *ssid)
  431. {
  432. char *value;
  433. if (ssid->mem_only_psk)
  434. return;
  435. value = wpa_config_get(ssid, "psk");
  436. if (value == NULL)
  437. return;
  438. fprintf(f, "\tpsk=%s\n", value);
  439. os_free(value);
  440. }
  441. static void write_proto(FILE *f, struct wpa_ssid *ssid)
  442. {
  443. char *value;
  444. if (ssid->proto == DEFAULT_PROTO)
  445. return;
  446. value = wpa_config_get(ssid, "proto");
  447. if (value == NULL)
  448. return;
  449. if (value[0])
  450. fprintf(f, "\tproto=%s\n", value);
  451. os_free(value);
  452. }
  453. static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
  454. {
  455. char *value;
  456. if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
  457. return;
  458. value = wpa_config_get(ssid, "key_mgmt");
  459. if (value == NULL)
  460. return;
  461. if (value[0])
  462. fprintf(f, "\tkey_mgmt=%s\n", value);
  463. os_free(value);
  464. }
  465. static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
  466. {
  467. char *value;
  468. if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
  469. return;
  470. value = wpa_config_get(ssid, "pairwise");
  471. if (value == NULL)
  472. return;
  473. if (value[0])
  474. fprintf(f, "\tpairwise=%s\n", value);
  475. os_free(value);
  476. }
  477. static void write_group(FILE *f, struct wpa_ssid *ssid)
  478. {
  479. char *value;
  480. if (ssid->group_cipher == DEFAULT_GROUP)
  481. return;
  482. value = wpa_config_get(ssid, "group");
  483. if (value == NULL)
  484. return;
  485. if (value[0])
  486. fprintf(f, "\tgroup=%s\n", value);
  487. os_free(value);
  488. }
  489. static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
  490. {
  491. char *value;
  492. if (ssid->auth_alg == 0)
  493. return;
  494. value = wpa_config_get(ssid, "auth_alg");
  495. if (value == NULL)
  496. return;
  497. if (value[0])
  498. fprintf(f, "\tauth_alg=%s\n", value);
  499. os_free(value);
  500. }
  501. #ifdef IEEE8021X_EAPOL
  502. static void write_eap(FILE *f, struct wpa_ssid *ssid)
  503. {
  504. char *value;
  505. value = wpa_config_get(ssid, "eap");
  506. if (value == NULL)
  507. return;
  508. if (value[0])
  509. fprintf(f, "\teap=%s\n", value);
  510. os_free(value);
  511. }
  512. #endif /* IEEE8021X_EAPOL */
  513. static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
  514. {
  515. char field[20], *value;
  516. int res;
  517. res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
  518. if (os_snprintf_error(sizeof(field), res))
  519. return;
  520. value = wpa_config_get(ssid, field);
  521. if (value) {
  522. fprintf(f, "\t%s=%s\n", field, value);
  523. os_free(value);
  524. }
  525. }
  526. #ifdef CONFIG_P2P
  527. static void write_go_p2p_dev_addr(FILE *f, struct wpa_ssid *ssid)
  528. {
  529. char *value = wpa_config_get(ssid, "go_p2p_dev_addr");
  530. if (value == NULL)
  531. return;
  532. fprintf(f, "\tgo_p2p_dev_addr=%s\n", value);
  533. os_free(value);
  534. }
  535. static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
  536. {
  537. char *value = wpa_config_get(ssid, "p2p_client_list");
  538. if (value == NULL)
  539. return;
  540. fprintf(f, "\tp2p_client_list=%s\n", value);
  541. os_free(value);
  542. }
  543. static void write_psk_list(FILE *f, struct wpa_ssid *ssid)
  544. {
  545. struct psk_list_entry *psk;
  546. char hex[32 * 2 + 1];
  547. dl_list_for_each(psk, &ssid->psk_list, struct psk_list_entry, list) {
  548. wpa_snprintf_hex(hex, sizeof(hex), psk->psk, sizeof(psk->psk));
  549. fprintf(f, "\tpsk_list=%s" MACSTR "-%s\n",
  550. psk->p2p ? "P2P-" : "", MAC2STR(psk->addr), hex);
  551. }
  552. }
  553. #endif /* CONFIG_P2P */
  554. #ifdef CONFIG_MACSEC
  555. static void write_mka_cak(FILE *f, struct wpa_ssid *ssid)
  556. {
  557. char *value;
  558. if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
  559. return;
  560. value = wpa_config_get(ssid, "mka_cak");
  561. if (!value)
  562. return;
  563. fprintf(f, "\tmka_cak=%s\n", value);
  564. os_free(value);
  565. }
  566. static void write_mka_ckn(FILE *f, struct wpa_ssid *ssid)
  567. {
  568. char *value;
  569. if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
  570. return;
  571. value = wpa_config_get(ssid, "mka_ckn");
  572. if (!value)
  573. return;
  574. fprintf(f, "\tmka_ckn=%s\n", value);
  575. os_free(value);
  576. }
  577. #endif /* CONFIG_MACSEC */
  578. static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
  579. {
  580. int i;
  581. #define STR(t) write_str(f, #t, ssid)
  582. #define INT(t) write_int(f, #t, ssid->t, 0)
  583. #define INTe(t) write_int(f, #t, ssid->eap.t, 0)
  584. #define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
  585. #define INT_DEFe(t, def) write_int(f, #t, ssid->eap.t, def)
  586. STR(ssid);
  587. INT(scan_ssid);
  588. write_bssid(f, ssid);
  589. write_str(f, "bssid_blacklist", ssid);
  590. write_str(f, "bssid_whitelist", ssid);
  591. write_psk(f, ssid);
  592. INT(mem_only_psk);
  593. write_proto(f, ssid);
  594. write_key_mgmt(f, ssid);
  595. INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
  596. write_pairwise(f, ssid);
  597. write_group(f, ssid);
  598. write_auth_alg(f, ssid);
  599. STR(bgscan);
  600. STR(autoscan);
  601. STR(scan_freq);
  602. #ifdef IEEE8021X_EAPOL
  603. write_eap(f, ssid);
  604. STR(identity);
  605. STR(anonymous_identity);
  606. STR(password);
  607. STR(ca_cert);
  608. STR(ca_path);
  609. STR(client_cert);
  610. STR(private_key);
  611. STR(private_key_passwd);
  612. STR(dh_file);
  613. STR(subject_match);
  614. STR(altsubject_match);
  615. STR(domain_suffix_match);
  616. STR(domain_match);
  617. STR(ca_cert2);
  618. STR(ca_path2);
  619. STR(client_cert2);
  620. STR(private_key2);
  621. STR(private_key2_passwd);
  622. STR(dh_file2);
  623. STR(subject_match2);
  624. STR(altsubject_match2);
  625. STR(domain_suffix_match2);
  626. STR(domain_match2);
  627. STR(phase1);
  628. STR(phase2);
  629. STR(pcsc);
  630. STR(pin);
  631. STR(engine_id);
  632. STR(key_id);
  633. STR(cert_id);
  634. STR(ca_cert_id);
  635. STR(key2_id);
  636. STR(pin2);
  637. STR(engine2_id);
  638. STR(cert2_id);
  639. STR(ca_cert2_id);
  640. INTe(engine);
  641. INTe(engine2);
  642. INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
  643. STR(openssl_ciphers);
  644. INTe(erp);
  645. #endif /* IEEE8021X_EAPOL */
  646. for (i = 0; i < 4; i++)
  647. write_wep_key(f, i, ssid);
  648. INT(wep_tx_keyidx);
  649. INT(priority);
  650. #ifdef IEEE8021X_EAPOL
  651. INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
  652. STR(pac_file);
  653. INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
  654. INTe(ocsp);
  655. INT_DEFe(sim_num, DEFAULT_USER_SELECTED_SIM);
  656. #endif /* IEEE8021X_EAPOL */
  657. INT(mode);
  658. INT(no_auto_peer);
  659. INT_DEF(mesh_rssi_threshold, DEFAULT_MESH_RSSI_THRESHOLD);
  660. INT(frequency);
  661. INT(fixed_freq);
  662. #ifdef CONFIG_ACS
  663. INT(acs);
  664. #endif /* CONFIG_ACS */
  665. write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
  666. INT(disabled);
  667. INT(peerkey);
  668. INT(mixed_cell);
  669. INT(vht);
  670. INT_DEF(ht, 1);
  671. INT(ht40);
  672. INT(max_oper_chwidth);
  673. INT(vht_center_freq1);
  674. INT(vht_center_freq2);
  675. INT(pbss);
  676. INT(wps_disabled);
  677. INT(fils_dh_group);
  678. #ifdef CONFIG_IEEE80211W
  679. write_int(f, "ieee80211w", ssid->ieee80211w,
  680. MGMT_FRAME_PROTECTION_DEFAULT);
  681. #endif /* CONFIG_IEEE80211W */
  682. STR(id_str);
  683. #ifdef CONFIG_P2P
  684. write_go_p2p_dev_addr(f, ssid);
  685. write_p2p_client_list(f, ssid);
  686. write_psk_list(f, ssid);
  687. #endif /* CONFIG_P2P */
  688. INT(ap_max_inactivity);
  689. INT(dtim_period);
  690. INT(beacon_int);
  691. #ifdef CONFIG_MACSEC
  692. INT(macsec_policy);
  693. write_mka_cak(f, ssid);
  694. write_mka_ckn(f, ssid);
  695. INT(macsec_integ_only);
  696. INT(macsec_port);
  697. INT_DEF(mka_priority, DEFAULT_PRIO_NOT_KEY_SERVER);
  698. #endif /* CONFIG_MACSEC */
  699. #ifdef CONFIG_HS20
  700. INT(update_identifier);
  701. #endif /* CONFIG_HS20 */
  702. write_int(f, "mac_addr", ssid->mac_addr, -1);
  703. #ifdef CONFIG_MESH
  704. STR(mesh_basic_rates);
  705. INT_DEF(dot11MeshMaxRetries, DEFAULT_MESH_MAX_RETRIES);
  706. INT_DEF(dot11MeshRetryTimeout, DEFAULT_MESH_RETRY_TIMEOUT);
  707. INT_DEF(dot11MeshConfirmTimeout, DEFAULT_MESH_CONFIRM_TIMEOUT);
  708. INT_DEF(dot11MeshHoldingTimeout, DEFAULT_MESH_HOLDING_TIMEOUT);
  709. #endif /* CONFIG_MESH */
  710. INT(wpa_ptk_rekey);
  711. INT(group_rekey);
  712. INT(ignore_broadcast_ssid);
  713. #ifdef CONFIG_HT_OVERRIDES
  714. INT_DEF(disable_ht, DEFAULT_DISABLE_HT);
  715. INT_DEF(disable_ht40, DEFAULT_DISABLE_HT40);
  716. INT_DEF(disable_sgi, DEFAULT_DISABLE_SGI);
  717. INT_DEF(disable_ldpc, DEFAULT_DISABLE_LDPC);
  718. INT(ht40_intolerant);
  719. INT_DEF(disable_max_amsdu, DEFAULT_DISABLE_MAX_AMSDU);
  720. INT_DEF(ampdu_factor, DEFAULT_AMPDU_FACTOR);
  721. INT_DEF(ampdu_density, DEFAULT_AMPDU_DENSITY);
  722. STR(ht_mcs);
  723. #endif /* CONFIG_HT_OVERRIDES */
  724. #ifdef CONFIG_VHT_OVERRIDES
  725. INT(disable_vht);
  726. INT(vht_capa);
  727. INT(vht_capa_mask);
  728. INT_DEF(vht_rx_mcs_nss_1, -1);
  729. INT_DEF(vht_rx_mcs_nss_2, -1);
  730. INT_DEF(vht_rx_mcs_nss_3, -1);
  731. INT_DEF(vht_rx_mcs_nss_4, -1);
  732. INT_DEF(vht_rx_mcs_nss_5, -1);
  733. INT_DEF(vht_rx_mcs_nss_6, -1);
  734. INT_DEF(vht_rx_mcs_nss_7, -1);
  735. INT_DEF(vht_rx_mcs_nss_8, -1);
  736. INT_DEF(vht_tx_mcs_nss_1, -1);
  737. INT_DEF(vht_tx_mcs_nss_2, -1);
  738. INT_DEF(vht_tx_mcs_nss_3, -1);
  739. INT_DEF(vht_tx_mcs_nss_4, -1);
  740. INT_DEF(vht_tx_mcs_nss_5, -1);
  741. INT_DEF(vht_tx_mcs_nss_6, -1);
  742. INT_DEF(vht_tx_mcs_nss_7, -1);
  743. INT_DEF(vht_tx_mcs_nss_8, -1);
  744. #endif /* CONFIG_VHT_OVERRIDES */
  745. #undef STR
  746. #undef INT
  747. #undef INT_DEF
  748. }
  749. static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
  750. {
  751. size_t i;
  752. if (cred->priority)
  753. fprintf(f, "\tpriority=%d\n", cred->priority);
  754. if (cred->pcsc)
  755. fprintf(f, "\tpcsc=%d\n", cred->pcsc);
  756. if (cred->realm)
  757. fprintf(f, "\trealm=\"%s\"\n", cred->realm);
  758. if (cred->username)
  759. fprintf(f, "\tusername=\"%s\"\n", cred->username);
  760. if (cred->password && cred->ext_password)
  761. fprintf(f, "\tpassword=ext:%s\n", cred->password);
  762. else if (cred->password)
  763. fprintf(f, "\tpassword=\"%s\"\n", cred->password);
  764. if (cred->ca_cert)
  765. fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
  766. if (cred->client_cert)
  767. fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
  768. if (cred->private_key)
  769. fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
  770. if (cred->private_key_passwd)
  771. fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
  772. cred->private_key_passwd);
  773. if (cred->imsi)
  774. fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
  775. if (cred->milenage)
  776. fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
  777. for (i = 0; i < cred->num_domain; i++)
  778. fprintf(f, "\tdomain=\"%s\"\n", cred->domain[i]);
  779. if (cred->domain_suffix_match)
  780. fprintf(f, "\tdomain_suffix_match=\"%s\"\n",
  781. cred->domain_suffix_match);
  782. if (cred->roaming_consortium_len) {
  783. fprintf(f, "\troaming_consortium=");
  784. for (i = 0; i < cred->roaming_consortium_len; i++)
  785. fprintf(f, "%02x", cred->roaming_consortium[i]);
  786. fprintf(f, "\n");
  787. }
  788. if (cred->eap_method) {
  789. const char *name;
  790. name = eap_get_name(cred->eap_method[0].vendor,
  791. cred->eap_method[0].method);
  792. if (name)
  793. fprintf(f, "\teap=%s\n", name);
  794. }
  795. if (cred->phase1)
  796. fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
  797. if (cred->phase2)
  798. fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
  799. if (cred->excluded_ssid) {
  800. size_t j;
  801. for (i = 0; i < cred->num_excluded_ssid; i++) {
  802. struct excluded_ssid *e = &cred->excluded_ssid[i];
  803. fprintf(f, "\texcluded_ssid=");
  804. for (j = 0; j < e->ssid_len; j++)
  805. fprintf(f, "%02x", e->ssid[j]);
  806. fprintf(f, "\n");
  807. }
  808. }
  809. if (cred->roaming_partner) {
  810. for (i = 0; i < cred->num_roaming_partner; i++) {
  811. struct roaming_partner *p = &cred->roaming_partner[i];
  812. fprintf(f, "\troaming_partner=\"%s,%d,%u,%s\"\n",
  813. p->fqdn, p->exact_match, p->priority,
  814. p->country);
  815. }
  816. }
  817. if (cred->update_identifier)
  818. fprintf(f, "\tupdate_identifier=%d\n", cred->update_identifier);
  819. if (cred->provisioning_sp)
  820. fprintf(f, "\tprovisioning_sp=\"%s\"\n", cred->provisioning_sp);
  821. if (cred->sp_priority)
  822. fprintf(f, "\tsp_priority=%d\n", cred->sp_priority);
  823. if (cred->min_dl_bandwidth_home)
  824. fprintf(f, "\tmin_dl_bandwidth_home=%u\n",
  825. cred->min_dl_bandwidth_home);
  826. if (cred->min_ul_bandwidth_home)
  827. fprintf(f, "\tmin_ul_bandwidth_home=%u\n",
  828. cred->min_ul_bandwidth_home);
  829. if (cred->min_dl_bandwidth_roaming)
  830. fprintf(f, "\tmin_dl_bandwidth_roaming=%u\n",
  831. cred->min_dl_bandwidth_roaming);
  832. if (cred->min_ul_bandwidth_roaming)
  833. fprintf(f, "\tmin_ul_bandwidth_roaming=%u\n",
  834. cred->min_ul_bandwidth_roaming);
  835. if (cred->max_bss_load)
  836. fprintf(f, "\tmax_bss_load=%u\n",
  837. cred->max_bss_load);
  838. if (cred->ocsp)
  839. fprintf(f, "\tocsp=%d\n", cred->ocsp);
  840. if (cred->num_req_conn_capab) {
  841. for (i = 0; i < cred->num_req_conn_capab; i++) {
  842. int *ports;
  843. fprintf(f, "\treq_conn_capab=%u",
  844. cred->req_conn_capab_proto[i]);
  845. ports = cred->req_conn_capab_port[i];
  846. if (ports) {
  847. int j;
  848. for (j = 0; ports[j] != -1; j++) {
  849. fprintf(f, "%s%d", j > 0 ? "," : ":",
  850. ports[j]);
  851. }
  852. }
  853. fprintf(f, "\n");
  854. }
  855. }
  856. if (cred->required_roaming_consortium_len) {
  857. fprintf(f, "\trequired_roaming_consortium=");
  858. for (i = 0; i < cred->required_roaming_consortium_len; i++)
  859. fprintf(f, "%02x",
  860. cred->required_roaming_consortium[i]);
  861. fprintf(f, "\n");
  862. }
  863. if (cred->sim_num != DEFAULT_USER_SELECTED_SIM)
  864. fprintf(f, "\tsim_num=%d\n", cred->sim_num);
  865. }
  866. #ifndef CONFIG_NO_CONFIG_BLOBS
  867. static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
  868. {
  869. unsigned char *encoded;
  870. encoded = base64_encode(blob->data, blob->len, NULL);
  871. if (encoded == NULL)
  872. return -1;
  873. fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
  874. os_free(encoded);
  875. return 0;
  876. }
  877. #endif /* CONFIG_NO_CONFIG_BLOBS */
  878. static void write_global_bin(FILE *f, const char *field,
  879. const struct wpabuf *val)
  880. {
  881. size_t i;
  882. const u8 *pos;
  883. if (val == NULL)
  884. return;
  885. fprintf(f, "%s=", field);
  886. pos = wpabuf_head(val);
  887. for (i = 0; i < wpabuf_len(val); i++)
  888. fprintf(f, "%02X", *pos++);
  889. fprintf(f, "\n");
  890. }
  891. static void wpa_config_write_global(FILE *f, struct wpa_config *config)
  892. {
  893. #ifdef CONFIG_CTRL_IFACE
  894. if (config->ctrl_interface)
  895. fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
  896. if (config->ctrl_interface_group)
  897. fprintf(f, "ctrl_interface_group=%s\n",
  898. config->ctrl_interface_group);
  899. #endif /* CONFIG_CTRL_IFACE */
  900. if (config->eapol_version != DEFAULT_EAPOL_VERSION)
  901. fprintf(f, "eapol_version=%d\n", config->eapol_version);
  902. if (config->ap_scan != DEFAULT_AP_SCAN)
  903. fprintf(f, "ap_scan=%d\n", config->ap_scan);
  904. if (config->disable_scan_offload)
  905. fprintf(f, "disable_scan_offload=%d\n",
  906. config->disable_scan_offload);
  907. if (config->fast_reauth != DEFAULT_FAST_REAUTH)
  908. fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
  909. if (config->opensc_engine_path)
  910. fprintf(f, "opensc_engine_path=%s\n",
  911. config->opensc_engine_path);
  912. if (config->pkcs11_engine_path)
  913. fprintf(f, "pkcs11_engine_path=%s\n",
  914. config->pkcs11_engine_path);
  915. if (config->pkcs11_module_path)
  916. fprintf(f, "pkcs11_module_path=%s\n",
  917. config->pkcs11_module_path);
  918. if (config->openssl_ciphers)
  919. fprintf(f, "openssl_ciphers=%s\n", config->openssl_ciphers);
  920. if (config->pcsc_reader)
  921. fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
  922. if (config->pcsc_pin)
  923. fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
  924. if (config->driver_param)
  925. fprintf(f, "driver_param=%s\n", config->driver_param);
  926. if (config->dot11RSNAConfigPMKLifetime)
  927. fprintf(f, "dot11RSNAConfigPMKLifetime=%u\n",
  928. config->dot11RSNAConfigPMKLifetime);
  929. if (config->dot11RSNAConfigPMKReauthThreshold)
  930. fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%u\n",
  931. config->dot11RSNAConfigPMKReauthThreshold);
  932. if (config->dot11RSNAConfigSATimeout)
  933. fprintf(f, "dot11RSNAConfigSATimeout=%u\n",
  934. config->dot11RSNAConfigSATimeout);
  935. if (config->update_config)
  936. fprintf(f, "update_config=%d\n", config->update_config);
  937. #ifdef CONFIG_WPS
  938. if (!is_nil_uuid(config->uuid)) {
  939. char buf[40];
  940. uuid_bin2str(config->uuid, buf, sizeof(buf));
  941. fprintf(f, "uuid=%s\n", buf);
  942. }
  943. if (config->auto_uuid)
  944. fprintf(f, "auto_uuid=%d\n", config->auto_uuid);
  945. if (config->device_name)
  946. fprintf(f, "device_name=%s\n", config->device_name);
  947. if (config->manufacturer)
  948. fprintf(f, "manufacturer=%s\n", config->manufacturer);
  949. if (config->model_name)
  950. fprintf(f, "model_name=%s\n", config->model_name);
  951. if (config->model_number)
  952. fprintf(f, "model_number=%s\n", config->model_number);
  953. if (config->serial_number)
  954. fprintf(f, "serial_number=%s\n", config->serial_number);
  955. {
  956. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  957. buf = wps_dev_type_bin2str(config->device_type,
  958. _buf, sizeof(_buf));
  959. if (os_strcmp(buf, "0-00000000-0") != 0)
  960. fprintf(f, "device_type=%s\n", buf);
  961. }
  962. if (WPA_GET_BE32(config->os_version))
  963. fprintf(f, "os_version=%08x\n",
  964. WPA_GET_BE32(config->os_version));
  965. if (config->config_methods)
  966. fprintf(f, "config_methods=%s\n", config->config_methods);
  967. if (config->wps_cred_processing)
  968. fprintf(f, "wps_cred_processing=%d\n",
  969. config->wps_cred_processing);
  970. if (config->wps_vendor_ext_m1) {
  971. int i, len = wpabuf_len(config->wps_vendor_ext_m1);
  972. const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
  973. if (len > 0) {
  974. fprintf(f, "wps_vendor_ext_m1=");
  975. for (i = 0; i < len; i++)
  976. fprintf(f, "%02x", *p++);
  977. fprintf(f, "\n");
  978. }
  979. }
  980. #endif /* CONFIG_WPS */
  981. #ifdef CONFIG_P2P
  982. {
  983. int i;
  984. char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
  985. for (i = 0; i < config->num_sec_device_types; i++) {
  986. buf = wps_dev_type_bin2str(config->sec_device_type[i],
  987. _buf, sizeof(_buf));
  988. if (buf)
  989. fprintf(f, "sec_device_type=%s\n", buf);
  990. }
  991. }
  992. if (config->p2p_listen_reg_class)
  993. fprintf(f, "p2p_listen_reg_class=%d\n",
  994. config->p2p_listen_reg_class);
  995. if (config->p2p_listen_channel)
  996. fprintf(f, "p2p_listen_channel=%d\n",
  997. config->p2p_listen_channel);
  998. if (config->p2p_oper_reg_class)
  999. fprintf(f, "p2p_oper_reg_class=%d\n",
  1000. config->p2p_oper_reg_class);
  1001. if (config->p2p_oper_channel)
  1002. fprintf(f, "p2p_oper_channel=%d\n", config->p2p_oper_channel);
  1003. if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
  1004. fprintf(f, "p2p_go_intent=%d\n", config->p2p_go_intent);
  1005. if (config->p2p_ssid_postfix)
  1006. fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
  1007. if (config->persistent_reconnect)
  1008. fprintf(f, "persistent_reconnect=%d\n",
  1009. config->persistent_reconnect);
  1010. if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
  1011. fprintf(f, "p2p_intra_bss=%d\n", config->p2p_intra_bss);
  1012. if (config->p2p_group_idle)
  1013. fprintf(f, "p2p_group_idle=%d\n", config->p2p_group_idle);
  1014. if (config->p2p_passphrase_len)
  1015. fprintf(f, "p2p_passphrase_len=%u\n",
  1016. config->p2p_passphrase_len);
  1017. if (config->p2p_pref_chan) {
  1018. unsigned int i;
  1019. fprintf(f, "p2p_pref_chan=");
  1020. for (i = 0; i < config->num_p2p_pref_chan; i++) {
  1021. fprintf(f, "%s%u:%u", i > 0 ? "," : "",
  1022. config->p2p_pref_chan[i].op_class,
  1023. config->p2p_pref_chan[i].chan);
  1024. }
  1025. fprintf(f, "\n");
  1026. }
  1027. if (config->p2p_no_go_freq.num) {
  1028. char *val = freq_range_list_str(&config->p2p_no_go_freq);
  1029. if (val) {
  1030. fprintf(f, "p2p_no_go_freq=%s\n", val);
  1031. os_free(val);
  1032. }
  1033. }
  1034. if (config->p2p_add_cli_chan)
  1035. fprintf(f, "p2p_add_cli_chan=%d\n", config->p2p_add_cli_chan);
  1036. if (config->p2p_optimize_listen_chan !=
  1037. DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN)
  1038. fprintf(f, "p2p_optimize_listen_chan=%d\n",
  1039. config->p2p_optimize_listen_chan);
  1040. if (config->p2p_go_ht40)
  1041. fprintf(f, "p2p_go_ht40=%d\n", config->p2p_go_ht40);
  1042. if (config->p2p_go_vht)
  1043. fprintf(f, "p2p_go_vht=%d\n", config->p2p_go_vht);
  1044. if (config->p2p_go_ctwindow != DEFAULT_P2P_GO_CTWINDOW)
  1045. fprintf(f, "p2p_go_ctwindow=%d\n", config->p2p_go_ctwindow);
  1046. if (config->p2p_disabled)
  1047. fprintf(f, "p2p_disabled=%d\n", config->p2p_disabled);
  1048. if (config->p2p_no_group_iface)
  1049. fprintf(f, "p2p_no_group_iface=%d\n",
  1050. config->p2p_no_group_iface);
  1051. if (config->p2p_ignore_shared_freq)
  1052. fprintf(f, "p2p_ignore_shared_freq=%d\n",
  1053. config->p2p_ignore_shared_freq);
  1054. if (config->p2p_cli_probe)
  1055. fprintf(f, "p2p_cli_probe=%d\n", config->p2p_cli_probe);
  1056. if (config->p2p_go_freq_change_policy != DEFAULT_P2P_GO_FREQ_MOVE)
  1057. fprintf(f, "p2p_go_freq_change_policy=%u\n",
  1058. config->p2p_go_freq_change_policy);
  1059. if (WPA_GET_BE32(config->ip_addr_go))
  1060. fprintf(f, "ip_addr_go=%u.%u.%u.%u\n",
  1061. config->ip_addr_go[0], config->ip_addr_go[1],
  1062. config->ip_addr_go[2], config->ip_addr_go[3]);
  1063. if (WPA_GET_BE32(config->ip_addr_mask))
  1064. fprintf(f, "ip_addr_mask=%u.%u.%u.%u\n",
  1065. config->ip_addr_mask[0], config->ip_addr_mask[1],
  1066. config->ip_addr_mask[2], config->ip_addr_mask[3]);
  1067. if (WPA_GET_BE32(config->ip_addr_start))
  1068. fprintf(f, "ip_addr_start=%u.%u.%u.%u\n",
  1069. config->ip_addr_start[0], config->ip_addr_start[1],
  1070. config->ip_addr_start[2], config->ip_addr_start[3]);
  1071. if (WPA_GET_BE32(config->ip_addr_end))
  1072. fprintf(f, "ip_addr_end=%u.%u.%u.%u\n",
  1073. config->ip_addr_end[0], config->ip_addr_end[1],
  1074. config->ip_addr_end[2], config->ip_addr_end[3]);
  1075. #endif /* CONFIG_P2P */
  1076. if (config->country[0] && config->country[1]) {
  1077. fprintf(f, "country=%c%c\n",
  1078. config->country[0], config->country[1]);
  1079. }
  1080. if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
  1081. fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
  1082. if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
  1083. fprintf(f, "bss_expiration_age=%u\n",
  1084. config->bss_expiration_age);
  1085. if (config->bss_expiration_scan_count !=
  1086. DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
  1087. fprintf(f, "bss_expiration_scan_count=%u\n",
  1088. config->bss_expiration_scan_count);
  1089. if (config->filter_ssids)
  1090. fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
  1091. if (config->filter_rssi)
  1092. fprintf(f, "filter_rssi=%d\n", config->filter_rssi);
  1093. if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
  1094. fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
  1095. if (config->disassoc_low_ack)
  1096. fprintf(f, "disassoc_low_ack=%d\n", config->disassoc_low_ack);
  1097. #ifdef CONFIG_HS20
  1098. if (config->hs20)
  1099. fprintf(f, "hs20=1\n");
  1100. #endif /* CONFIG_HS20 */
  1101. #ifdef CONFIG_INTERWORKING
  1102. if (config->interworking)
  1103. fprintf(f, "interworking=%d\n", config->interworking);
  1104. if (!is_zero_ether_addr(config->hessid))
  1105. fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
  1106. if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
  1107. fprintf(f, "access_network_type=%d\n",
  1108. config->access_network_type);
  1109. #endif /* CONFIG_INTERWORKING */
  1110. if (config->pbc_in_m1)
  1111. fprintf(f, "pbc_in_m1=%d\n", config->pbc_in_m1);
  1112. if (config->wps_nfc_pw_from_config) {
  1113. if (config->wps_nfc_dev_pw_id)
  1114. fprintf(f, "wps_nfc_dev_pw_id=%d\n",
  1115. config->wps_nfc_dev_pw_id);
  1116. write_global_bin(f, "wps_nfc_dh_pubkey",
  1117. config->wps_nfc_dh_pubkey);
  1118. write_global_bin(f, "wps_nfc_dh_privkey",
  1119. config->wps_nfc_dh_privkey);
  1120. write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
  1121. }
  1122. if (config->ext_password_backend)
  1123. fprintf(f, "ext_password_backend=%s\n",
  1124. config->ext_password_backend);
  1125. if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
  1126. fprintf(f, "p2p_go_max_inactivity=%d\n",
  1127. config->p2p_go_max_inactivity);
  1128. if (config->auto_interworking)
  1129. fprintf(f, "auto_interworking=%d\n",
  1130. config->auto_interworking);
  1131. if (config->okc)
  1132. fprintf(f, "okc=%d\n", config->okc);
  1133. if (config->pmf)
  1134. fprintf(f, "pmf=%d\n", config->pmf);
  1135. if (config->dtim_period)
  1136. fprintf(f, "dtim_period=%d\n", config->dtim_period);
  1137. if (config->beacon_int)
  1138. fprintf(f, "beacon_int=%d\n", config->beacon_int);
  1139. if (config->sae_groups) {
  1140. int i;
  1141. fprintf(f, "sae_groups=");
  1142. for (i = 0; config->sae_groups[i] > 0; i++) {
  1143. fprintf(f, "%s%d", i > 0 ? " " : "",
  1144. config->sae_groups[i]);
  1145. }
  1146. fprintf(f, "\n");
  1147. }
  1148. if (config->ap_vendor_elements) {
  1149. int i, len = wpabuf_len(config->ap_vendor_elements);
  1150. const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
  1151. if (len > 0) {
  1152. fprintf(f, "ap_vendor_elements=");
  1153. for (i = 0; i < len; i++)
  1154. fprintf(f, "%02x", *p++);
  1155. fprintf(f, "\n");
  1156. }
  1157. }
  1158. if (config->ignore_old_scan_res)
  1159. fprintf(f, "ignore_old_scan_res=%d\n",
  1160. config->ignore_old_scan_res);
  1161. if (config->freq_list && config->freq_list[0]) {
  1162. int i;
  1163. fprintf(f, "freq_list=");
  1164. for (i = 0; config->freq_list[i]; i++) {
  1165. fprintf(f, "%s%d", i > 0 ? " " : "",
  1166. config->freq_list[i]);
  1167. }
  1168. fprintf(f, "\n");
  1169. }
  1170. if (config->scan_cur_freq != DEFAULT_SCAN_CUR_FREQ)
  1171. fprintf(f, "scan_cur_freq=%d\n", config->scan_cur_freq);
  1172. if (config->sched_scan_interval)
  1173. fprintf(f, "sched_scan_interval=%u\n",
  1174. config->sched_scan_interval);
  1175. if (config->sched_scan_start_delay)
  1176. fprintf(f, "sched_scan_start_delay=%u\n",
  1177. config->sched_scan_start_delay);
  1178. if (config->external_sim)
  1179. fprintf(f, "external_sim=%d\n", config->external_sim);
  1180. if (config->tdls_external_control)
  1181. fprintf(f, "tdls_external_control=%d\n",
  1182. config->tdls_external_control);
  1183. if (config->wowlan_triggers)
  1184. fprintf(f, "wowlan_triggers=%s\n",
  1185. config->wowlan_triggers);
  1186. if (config->bgscan)
  1187. fprintf(f, "bgscan=\"%s\"\n", config->bgscan);
  1188. if (config->autoscan)
  1189. fprintf(f, "autoscan=%s\n", config->autoscan);
  1190. if (config->p2p_search_delay != DEFAULT_P2P_SEARCH_DELAY)
  1191. fprintf(f, "p2p_search_delay=%u\n",
  1192. config->p2p_search_delay);
  1193. if (config->mac_addr)
  1194. fprintf(f, "mac_addr=%d\n", config->mac_addr);
  1195. if (config->rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
  1196. fprintf(f, "rand_addr_lifetime=%u\n",
  1197. config->rand_addr_lifetime);
  1198. if (config->preassoc_mac_addr)
  1199. fprintf(f, "preassoc_mac_addr=%d\n", config->preassoc_mac_addr);
  1200. if (config->key_mgmt_offload != DEFAULT_KEY_MGMT_OFFLOAD)
  1201. fprintf(f, "key_mgmt_offload=%d\n", config->key_mgmt_offload);
  1202. if (config->user_mpm != DEFAULT_USER_MPM)
  1203. fprintf(f, "user_mpm=%d\n", config->user_mpm);
  1204. if (config->max_peer_links != DEFAULT_MAX_PEER_LINKS)
  1205. fprintf(f, "max_peer_links=%d\n", config->max_peer_links);
  1206. if (config->cert_in_cb != DEFAULT_CERT_IN_CB)
  1207. fprintf(f, "cert_in_cb=%d\n", config->cert_in_cb);
  1208. if (config->mesh_max_inactivity != DEFAULT_MESH_MAX_INACTIVITY)
  1209. fprintf(f, "mesh_max_inactivity=%d\n",
  1210. config->mesh_max_inactivity);
  1211. if (config->dot11RSNASAERetransPeriod !=
  1212. DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD)
  1213. fprintf(f, "dot11RSNASAERetransPeriod=%d\n",
  1214. config->dot11RSNASAERetransPeriod);
  1215. if (config->passive_scan)
  1216. fprintf(f, "passive_scan=%d\n", config->passive_scan);
  1217. if (config->reassoc_same_bss_optim)
  1218. fprintf(f, "reassoc_same_bss_optim=%d\n",
  1219. config->reassoc_same_bss_optim);
  1220. if (config->wps_priority)
  1221. fprintf(f, "wps_priority=%d\n", config->wps_priority);
  1222. if (config->wpa_rsc_relaxation != DEFAULT_WPA_RSC_RELAXATION)
  1223. fprintf(f, "wpa_rsc_relaxation=%d\n",
  1224. config->wpa_rsc_relaxation);
  1225. if (config->sched_scan_plans)
  1226. fprintf(f, "sched_scan_plans=%s\n", config->sched_scan_plans);
  1227. #ifdef CONFIG_MBO
  1228. if (config->non_pref_chan)
  1229. fprintf(f, "non_pref_chan=%s\n", config->non_pref_chan);
  1230. if (config->mbo_cell_capa != DEFAULT_MBO_CELL_CAPA)
  1231. fprintf(f, "mbo_cell_capa=%u\n", config->mbo_cell_capa);
  1232. if (config->disassoc_imminent_rssi_threshold !=
  1233. DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD)
  1234. fprintf(f, "disassoc_imminent_rssi_threshold=%d\n",
  1235. config->disassoc_imminent_rssi_threshold);
  1236. #endif /* CONFIG_MBO */
  1237. if (config->gas_address3)
  1238. fprintf(f, "gas_address3=%d\n", config->gas_address3);
  1239. if (config->ftm_responder)
  1240. fprintf(f, "ftm_responder=%d\n", config->ftm_responder);
  1241. if (config->ftm_initiator)
  1242. fprintf(f, "ftm_initiator=%d\n", config->ftm_initiator);
  1243. if (config->osu_dir)
  1244. fprintf(f, "osu_dir=%s\n", config->osu_dir);
  1245. if (config->fst_group_id)
  1246. fprintf(f, "fst_group_id=%s\n", config->fst_group_id);
  1247. if (config->fst_priority)
  1248. fprintf(f, "fst_priority=%d\n", config->fst_priority);
  1249. if (config->fst_llt)
  1250. fprintf(f, "fst_llt=%d\n", config->fst_llt);
  1251. if (config->gas_rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
  1252. fprintf(f, "gas_rand_addr_lifetime=%u\n",
  1253. config->gas_rand_addr_lifetime);
  1254. if (config->gas_rand_mac_addr)
  1255. fprintf(f, "gas_rand_mac_addr=%d\n", config->gas_rand_mac_addr);
  1256. }
  1257. #endif /* CONFIG_NO_CONFIG_WRITE */
  1258. int wpa_config_write(const char *name, struct wpa_config *config)
  1259. {
  1260. #ifndef CONFIG_NO_CONFIG_WRITE
  1261. FILE *f;
  1262. struct wpa_ssid *ssid;
  1263. struct wpa_cred *cred;
  1264. #ifndef CONFIG_NO_CONFIG_BLOBS
  1265. struct wpa_config_blob *blob;
  1266. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1267. int ret = 0;
  1268. const char *orig_name = name;
  1269. int tmp_len = os_strlen(name) + 5; /* allow space for .tmp suffix */
  1270. char *tmp_name = os_malloc(tmp_len);
  1271. if (tmp_name) {
  1272. os_snprintf(tmp_name, tmp_len, "%s.tmp", name);
  1273. name = tmp_name;
  1274. }
  1275. wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
  1276. f = fopen(name, "w");
  1277. if (f == NULL) {
  1278. wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
  1279. os_free(tmp_name);
  1280. return -1;
  1281. }
  1282. wpa_config_write_global(f, config);
  1283. for (cred = config->cred; cred; cred = cred->next) {
  1284. if (cred->temporary)
  1285. continue;
  1286. fprintf(f, "\ncred={\n");
  1287. wpa_config_write_cred(f, cred);
  1288. fprintf(f, "}\n");
  1289. }
  1290. for (ssid = config->ssid; ssid; ssid = ssid->next) {
  1291. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
  1292. continue; /* do not save temporary networks */
  1293. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  1294. !ssid->passphrase)
  1295. continue; /* do not save invalid network */
  1296. fprintf(f, "\nnetwork={\n");
  1297. wpa_config_write_network(f, ssid);
  1298. fprintf(f, "}\n");
  1299. }
  1300. #ifndef CONFIG_NO_CONFIG_BLOBS
  1301. for (blob = config->blobs; blob; blob = blob->next) {
  1302. ret = wpa_config_write_blob(f, blob);
  1303. if (ret)
  1304. break;
  1305. }
  1306. #endif /* CONFIG_NO_CONFIG_BLOBS */
  1307. os_fdatasync(f);
  1308. fclose(f);
  1309. if (tmp_name) {
  1310. int chmod_ret = 0;
  1311. #ifdef ANDROID
  1312. chmod_ret = chmod(tmp_name,
  1313. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  1314. #endif /* ANDROID */
  1315. if (chmod_ret != 0 || rename(tmp_name, orig_name) != 0)
  1316. ret = -1;
  1317. os_free(tmp_name);
  1318. }
  1319. wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
  1320. orig_name, ret ? "un" : "");
  1321. return ret;
  1322. #else /* CONFIG_NO_CONFIG_WRITE */
  1323. return -1;
  1324. #endif /* CONFIG_NO_CONFIG_WRITE */
  1325. }