config_file.c 41 KB

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