config_file.c 32 KB

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