config_file.c 37 KB

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